xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision 10c642d0772ac1391ae4f9fdeb13217ab019117a)
13f542974SPaul B Schroeder /*
23f542974SPaul B Schroeder  * This program is free software; you can redistribute it and/or modify
33f542974SPaul B Schroeder  * it under the terms of the GNU General Public License as published by
43f542974SPaul B Schroeder  * the Free Software Foundation; either version 2 of the License, or
53f542974SPaul B Schroeder  * (at your option) any later version.
63f542974SPaul B Schroeder  *
73f542974SPaul B Schroeder  * This program is distributed in the hope that it will be useful,
83f542974SPaul B Schroeder  * but WITHOUT ANY WARRANTY; without even the implied warranty of
93f542974SPaul B Schroeder  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
103f542974SPaul B Schroeder  * GNU General Public License for more details.
113f542974SPaul B Schroeder  *
123f542974SPaul B Schroeder  * You should have received a copy of the GNU General Public License
133f542974SPaul B Schroeder  * along with this program; if not, write to the Free Software
143f542974SPaul B Schroeder  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
153f542974SPaul B Schroeder  *
163f542974SPaul B Schroeder  * Clean ups from Moschip version and a few ioctl implementations by:
173f542974SPaul B Schroeder  *	Paul B Schroeder <pschroeder "at" uplogix "dot" com>
183f542974SPaul B Schroeder  *
193f542974SPaul B Schroeder  * Originally based on drivers/usb/serial/io_edgeport.c which is:
203f542974SPaul B Schroeder  *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
213f542974SPaul B Schroeder  *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
223f542974SPaul B Schroeder  *
233f542974SPaul B Schroeder  */
243f542974SPaul B Schroeder 
253f542974SPaul B Schroeder #include <linux/kernel.h>
263f542974SPaul B Schroeder #include <linux/errno.h>
273f542974SPaul B Schroeder #include <linux/init.h>
283f542974SPaul B Schroeder #include <linux/slab.h>
293f542974SPaul B Schroeder #include <linux/tty.h>
303f542974SPaul B Schroeder #include <linux/tty_driver.h>
313f542974SPaul B Schroeder #include <linux/tty_flip.h>
323f542974SPaul B Schroeder #include <linux/module.h>
333f542974SPaul B Schroeder #include <linux/serial.h>
343f542974SPaul B Schroeder #include <linux/usb.h>
353f542974SPaul B Schroeder #include <linux/usb/serial.h>
36880af9dbSAlan Cox #include <linux/uaccess.h>
373f542974SPaul B Schroeder 
383f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
393f542974SPaul B Schroeder 
403f542974SPaul B Schroeder /*
413f542974SPaul B Schroeder  * 16C50 UART register defines
423f542974SPaul B Schroeder  */
433f542974SPaul B Schroeder 
443f542974SPaul B Schroeder #define LCR_BITS_5             0x00	/* 5 bits/char */
453f542974SPaul B Schroeder #define LCR_BITS_6             0x01	/* 6 bits/char */
463f542974SPaul B Schroeder #define LCR_BITS_7             0x02	/* 7 bits/char */
473f542974SPaul B Schroeder #define LCR_BITS_8             0x03	/* 8 bits/char */
483f542974SPaul B Schroeder #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
493f542974SPaul B Schroeder 
503f542974SPaul B Schroeder #define LCR_STOP_1             0x00	/* 1 stop bit */
513f542974SPaul B Schroeder #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
523f542974SPaul B Schroeder #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
533f542974SPaul B Schroeder #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
543f542974SPaul B Schroeder 
553f542974SPaul B Schroeder #define LCR_PAR_NONE           0x00	/* No parity */
563f542974SPaul B Schroeder #define LCR_PAR_ODD            0x08	/* Odd parity */
573f542974SPaul B Schroeder #define LCR_PAR_EVEN           0x18	/* Even parity */
583f542974SPaul B Schroeder #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
593f542974SPaul B Schroeder #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
603f542974SPaul B Schroeder #define LCR_PAR_MASK           0x38	/* Mask for parity field */
613f542974SPaul B Schroeder 
623f542974SPaul B Schroeder #define LCR_SET_BREAK          0x40	/* Set Break condition */
633f542974SPaul B Schroeder #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
643f542974SPaul B Schroeder 
653f542974SPaul B Schroeder #define MCR_DTR                0x01	/* Assert DTR */
663f542974SPaul B Schroeder #define MCR_RTS                0x02	/* Assert RTS */
673f542974SPaul B Schroeder #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
683f542974SPaul B Schroeder #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
693f542974SPaul B Schroeder #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
703f542974SPaul B Schroeder #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
713f542974SPaul B Schroeder 
723f542974SPaul B Schroeder #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
733f542974SPaul B Schroeder #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
743f542974SPaul B Schroeder #define MOS7840_MSR_RI         0x40	/* Current state of RI */
753f542974SPaul B Schroeder #define MOS7840_MSR_CD         0x80	/* Current state of CD */
763f542974SPaul B Schroeder 
773f542974SPaul B Schroeder /*
783f542974SPaul B Schroeder  * Defines used for sending commands to port
793f542974SPaul B Schroeder  */
803f542974SPaul B Schroeder 
811e658489SMark Ferrell #define MOS_WDR_TIMEOUT		5000	/* default urb timeout */
823f542974SPaul B Schroeder 
833f542974SPaul B Schroeder #define MOS_PORT1       0x0200
843f542974SPaul B Schroeder #define MOS_PORT2       0x0300
853f542974SPaul B Schroeder #define MOS_VENREG      0x0000
863f542974SPaul B Schroeder #define MOS_MAX_PORT	0x02
873f542974SPaul B Schroeder #define MOS_WRITE       0x0E
883f542974SPaul B Schroeder #define MOS_READ        0x0D
893f542974SPaul B Schroeder 
903f542974SPaul B Schroeder /* Requests */
913f542974SPaul B Schroeder #define MCS_RD_RTYPE    0xC0
923f542974SPaul B Schroeder #define MCS_WR_RTYPE    0x40
933f542974SPaul B Schroeder #define MCS_RDREQ       0x0D
943f542974SPaul B Schroeder #define MCS_WRREQ       0x0E
953f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT        500
963f542974SPaul B Schroeder #define VENDOR_READ_LENGTH      (0x01)
973f542974SPaul B Schroeder 
983f542974SPaul B Schroeder #define MAX_NAME_LEN    64
993f542974SPaul B Schroeder 
100880af9dbSAlan Cox #define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
101880af9dbSAlan Cox #define ZLP_REG5  0x3E		/* Zero_Flag_Reg5    62 */
1023f542974SPaul B Schroeder 
1033f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */
1043f542974SPaul B Schroeder #define TIOCEXBAUD     0x5462
1053f542974SPaul B Schroeder 
1063f542974SPaul B Schroeder /* vendor id and device id defines */
1073f542974SPaul B Schroeder 
10811e1abb4SDavid Ludlow /* The native mos7840/7820 component */
1093f542974SPaul B Schroeder #define USB_VENDOR_ID_MOSCHIP           0x9710
1103f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7840          0x7840
1113f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7820          0x7820
1120eafe4deSDonald #define MOSCHIP_DEVICE_ID_7810          0x7810
11311e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden
11411e1abb4SDavid Ludlow  * in vendor-specific implementations.  Such devices can be handled
11568e24113SGreg Kroah-Hartman  * by making a change here, in id_table.
11611e1abb4SDavid Ludlow  */
11711e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB              0x0856
118acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
119870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
120acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
121870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
122acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
123acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
124acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
125acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
12611e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
127caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
128870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
129870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
130870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL2_4        0xAC24
1313f542974SPaul B Schroeder 
1329d498beaSRussell Lang /* This driver also supports
1339d498beaSRussell Lang  * ATEN UC2324 device using Moschip MCS7840
1349d498beaSRussell Lang  * ATEN UC2322 device using Moschip MCS7820
1359d498beaSRussell Lang  */
136e9b8cffaSTony Cook #define USB_VENDOR_ID_ATENINTL		0x0557
137e9b8cffaSTony Cook #define ATENINTL_DEVICE_ID_UC2324	0x2011
1389d498beaSRussell Lang #define ATENINTL_DEVICE_ID_UC2322	0x7820
139e9b8cffaSTony Cook 
14011e1abb4SDavid Ludlow /* Interrupt Routine Defines    */
1413f542974SPaul B Schroeder 
1423f542974SPaul B Schroeder #define SERIAL_IIR_RLS      0x06
1433f542974SPaul B Schroeder #define SERIAL_IIR_MS       0x00
1443f542974SPaul B Schroeder 
1453f542974SPaul B Schroeder /*
1463f542974SPaul B Schroeder  *  Emulation of the bit mask on the LINE STATUS REGISTER.
1473f542974SPaul B Schroeder  */
1483f542974SPaul B Schroeder #define SERIAL_LSR_DR       0x0001
1493f542974SPaul B Schroeder #define SERIAL_LSR_OE       0x0002
1503f542974SPaul B Schroeder #define SERIAL_LSR_PE       0x0004
1513f542974SPaul B Schroeder #define SERIAL_LSR_FE       0x0008
1523f542974SPaul B Schroeder #define SERIAL_LSR_BI       0x0010
1533f542974SPaul B Schroeder 
1543f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS   0x10
1553f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR   0x20
1563f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI    0x40
1573f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD    0x80
1583f542974SPaul B Schroeder 
159880af9dbSAlan Cox /* Serial Port register Address */
1603f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
1613f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
1623f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
1633f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
1643f542974SPaul B Schroeder #define LINE_STATUS_REGISTER       ((__u16)(0x05))
1653f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
1663f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
1673f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
1683f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
1693f542974SPaul B Schroeder 
1703f542974SPaul B Schroeder #define CLK_MULTI_REGISTER         ((__u16)(0x02))
1713f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
172093ea2d3SDonald Lee #define GPIO_REGISTER              ((__u16)(0x07))
1733f542974SPaul B Schroeder 
1743f542974SPaul B Schroeder #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
1753f542974SPaul B Schroeder 
1763f542974SPaul B Schroeder /*
1773f542974SPaul B Schroeder  * URB POOL related defines
1783f542974SPaul B Schroeder  */
1793f542974SPaul B Schroeder #define NUM_URBS                        16	/* URB Count */
1803f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
1813f542974SPaul B Schroeder 
1820eafe4deSDonald /* LED on/off milliseconds*/
1830eafe4deSDonald #define LED_ON_MS	500
1840eafe4deSDonald #define LED_OFF_MS	500
1850eafe4deSDonald 
186d8a083ccSJohan Hovold enum mos7840_flag {
187d8a083ccSJohan Hovold 	MOS7840_FLAG_CTRL_BUSY,
18805cf0decSJohan Hovold 	MOS7840_FLAG_LED_BUSY,
189d8a083ccSJohan Hovold };
1903f542974SPaul B Schroeder 
191b9c87663STony Zelenoff static const struct usb_device_id id_table[] = {
1923f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
1933f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
1940eafe4deSDonald 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
195acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
196870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
197acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
198870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
199acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
200acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
201acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
202acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
20311e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
204870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
205acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
206870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
20727f1281dSBlaise Gassend 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
208e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
2099d498beaSRussell Lang 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
2103f542974SPaul B Schroeder 	{}			/* terminating entry */
2113f542974SPaul B Schroeder };
21268e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
2133f542974SPaul B Schroeder 
2143f542974SPaul B Schroeder /* This structure holds all of the local port information */
2153f542974SPaul B Schroeder 
2163f542974SPaul B Schroeder struct moschip_port {
2173f542974SPaul B Schroeder 	int port_num;		/*Actual port number in the device(1,2,etc) */
2183f542974SPaul B Schroeder 	struct urb *write_urb;	/* write URB for this port */
2193f542974SPaul B Schroeder 	struct urb *read_urb;	/* read URB for this port */
2203f542974SPaul B Schroeder 	__u8 shadowLCR;		/* last LCR value received */
2213f542974SPaul B Schroeder 	__u8 shadowMCR;		/* last MCR value received */
2223f542974SPaul B Schroeder 	char open;
2230de9a702SOliver Neukum 	char open_ports;
2243f542974SPaul B Schroeder 	struct usb_serial_port *port;	/* loop back to the owner of this object */
2253f542974SPaul B Schroeder 
2263f542974SPaul B Schroeder 	/* Offsets */
2273f542974SPaul B Schroeder 	__u8 SpRegOffset;
2283f542974SPaul B Schroeder 	__u8 ControlRegOffset;
2293f542974SPaul B Schroeder 	__u8 DcrRegOffset;
230880af9dbSAlan Cox 	/* for processing control URBS in interrupt context */
2313f542974SPaul B Schroeder 	struct urb *control_urb;
2320de9a702SOliver Neukum 	struct usb_ctrlrequest *dr;
2333f542974SPaul B Schroeder 	char *ctrl_buf;
2343f542974SPaul B Schroeder 	int MsrLsr;
2353f542974SPaul B Schroeder 
2360de9a702SOliver Neukum 	spinlock_t pool_lock;
2373f542974SPaul B Schroeder 	struct urb *write_urb_pool[NUM_URBS];
2380de9a702SOliver Neukum 	char busy[NUM_URBS];
23950de36f7SGreg Kroah-Hartman 	bool read_urb_busy;
2403f542974SPaul B Schroeder 
2410eafe4deSDonald 	/* For device(s) with LED indicator */
2420eafe4deSDonald 	bool has_led;
2430eafe4deSDonald 	struct timer_list led_timer1;	/* Timer for LED on */
2440eafe4deSDonald 	struct timer_list led_timer2;	/* Timer for LED off */
24505cf0decSJohan Hovold 	struct urb *led_urb;
24605cf0decSJohan Hovold 	struct usb_ctrlrequest *led_dr;
247d8a083ccSJohan Hovold 
248d8a083ccSJohan Hovold 	unsigned long flags;
2490eafe4deSDonald };
2503f542974SPaul B Schroeder 
2513f542974SPaul B Schroeder /*
2523f542974SPaul B Schroeder  * mos7840_set_reg_sync
2533f542974SPaul B Schroeder  * 	To set the Control register by calling usb_fill_control_urb function
2543f542974SPaul B Schroeder  *	by passing usb_sndctrlpipe function as parameter.
2553f542974SPaul B Schroeder  */
2563f542974SPaul B Schroeder 
2573f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2583f542974SPaul B Schroeder 				__u16 val)
2593f542974SPaul B Schroeder {
2603f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2613f542974SPaul B Schroeder 	val = val & 0x00ff;
2629c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
2633f542974SPaul B Schroeder 
2643f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2653f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2663f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2673f542974SPaul B Schroeder }
2683f542974SPaul B Schroeder 
2693f542974SPaul B Schroeder /*
2703f542974SPaul B Schroeder  * mos7840_get_reg_sync
2713f542974SPaul B Schroeder  * 	To set the Uart register by calling usb_fill_control_urb function by
2723f542974SPaul B Schroeder  *	passing usb_rcvctrlpipe function as parameter.
2733f542974SPaul B Schroeder  */
2743f542974SPaul B Schroeder 
2753f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
2763f542974SPaul B Schroeder 				__u16 *val)
2773f542974SPaul B Schroeder {
2783f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2793f542974SPaul B Schroeder 	int ret = 0;
2809e221a35SJohan Hovold 	u8 *buf;
2819e221a35SJohan Hovold 
2829e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2839e221a35SJohan Hovold 	if (!buf)
2849e221a35SJohan Hovold 		return -ENOMEM;
2853f542974SPaul B Schroeder 
2863f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
2879e221a35SJohan Hovold 			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
2883f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
2899e221a35SJohan Hovold 	*val = buf[0];
2909c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
2919e221a35SJohan Hovold 
2929e221a35SJohan Hovold 	kfree(buf);
2933f542974SPaul B Schroeder 	return ret;
2943f542974SPaul B Schroeder }
2953f542974SPaul B Schroeder 
2963f542974SPaul B Schroeder /*
2973f542974SPaul B Schroeder  * mos7840_set_uart_reg
2983f542974SPaul B Schroeder  *	To set the Uart register by calling usb_fill_control_urb function by
2993f542974SPaul B Schroeder  *	passing usb_sndctrlpipe function as parameter.
3003f542974SPaul B Schroeder  */
3013f542974SPaul B Schroeder 
3023f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
3033f542974SPaul B Schroeder 				__u16 val)
3043f542974SPaul B Schroeder {
3053f542974SPaul B Schroeder 
3063f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3073f542974SPaul B Schroeder 	val = val & 0x00ff;
308880af9dbSAlan Cox 	/* For the UART control registers, the application number need
309880af9dbSAlan Cox 	   to be Or'ed */
3100de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3111143832eSGreg Kroah-Hartman 		val |= ((__u16)port->port_number + 1) << 8;
3123f542974SPaul B Schroeder 	} else {
3131143832eSGreg Kroah-Hartman 		if (port->port_number == 0) {
3141143832eSGreg Kroah-Hartman 			val |= ((__u16)port->port_number + 1) << 8;
3153f542974SPaul B Schroeder 		} else {
3161143832eSGreg Kroah-Hartman 			val |= ((__u16)port->port_number + 2) << 8;
3173f542974SPaul B Schroeder 		}
3183f542974SPaul B Schroeder 	}
3199c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
3203f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
3213f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
3223f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
3233f542974SPaul B Schroeder 
3243f542974SPaul B Schroeder }
3253f542974SPaul B Schroeder 
3263f542974SPaul B Schroeder /*
3273f542974SPaul B Schroeder  * mos7840_get_uart_reg
3283f542974SPaul B Schroeder  *	To set the Control register by calling usb_fill_control_urb function
3293f542974SPaul B Schroeder  *	by passing usb_rcvctrlpipe function as parameter.
3303f542974SPaul B Schroeder  */
3313f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3323f542974SPaul B Schroeder 				__u16 *val)
3333f542974SPaul B Schroeder {
3343f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3353f542974SPaul B Schroeder 	int ret = 0;
3363f542974SPaul B Schroeder 	__u16 Wval;
3379e221a35SJohan Hovold 	u8 *buf;
3389e221a35SJohan Hovold 
3399e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3409e221a35SJohan Hovold 	if (!buf)
3419e221a35SJohan Hovold 		return -ENOMEM;
3423f542974SPaul B Schroeder 
3433f542974SPaul B Schroeder 	/* Wval  is same as application number */
3440de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3451143832eSGreg Kroah-Hartman 		Wval = ((__u16)port->port_number + 1) << 8;
3463f542974SPaul B Schroeder 	} else {
3471143832eSGreg Kroah-Hartman 		if (port->port_number == 0) {
3481143832eSGreg Kroah-Hartman 			Wval = ((__u16)port->port_number + 1) << 8;
3493f542974SPaul B Schroeder 		} else {
3501143832eSGreg Kroah-Hartman 			Wval = ((__u16)port->port_number + 2) << 8;
3513f542974SPaul B Schroeder 		}
3523f542974SPaul B Schroeder 	}
3539c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
3543f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3559e221a35SJohan Hovold 			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
3563f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
3579e221a35SJohan Hovold 	*val = buf[0];
3589e221a35SJohan Hovold 
3599e221a35SJohan Hovold 	kfree(buf);
3603f542974SPaul B Schroeder 	return ret;
3613f542974SPaul B Schroeder }
3623f542974SPaul B Schroeder 
3639c134a14SGreg Kroah-Hartman static void mos7840_dump_serial_port(struct usb_serial_port *port,
3649c134a14SGreg Kroah-Hartman 				     struct moschip_port *mos7840_port)
3653f542974SPaul B Schroeder {
3663f542974SPaul B Schroeder 
3679c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
3689c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
3699c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
3703f542974SPaul B Schroeder 
3713f542974SPaul B Schroeder }
3723f542974SPaul B Schroeder 
3733f542974SPaul B Schroeder /************************************************************************/
3743f542974SPaul B Schroeder /************************************************************************/
3753f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3763f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3773f542974SPaul B Schroeder /************************************************************************/
3783f542974SPaul B Schroeder /************************************************************************/
3793f542974SPaul B Schroeder 
3803f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port,
3813f542974SPaul B Schroeder 					    struct moschip_port *data)
3823f542974SPaul B Schroeder {
3833f542974SPaul B Schroeder 	usb_set_serial_port_data(port, (void *)data);
3843f542974SPaul B Schroeder }
3853f542974SPaul B Schroeder 
3863f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct
3873f542974SPaul B Schroeder 							    usb_serial_port
3883f542974SPaul B Schroeder 							    *port)
3893f542974SPaul B Schroeder {
3903f542974SPaul B Schroeder 	return (struct moschip_port *)usb_get_serial_port_data(port);
3913f542974SPaul B Schroeder }
3923f542974SPaul B Schroeder 
3930de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
3943f542974SPaul B Schroeder {
3953f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
3963f542974SPaul B Schroeder 	struct async_icount *icount;
3973f542974SPaul B Schroeder 	mos7840_port = port;
3983f542974SPaul B Schroeder 	if (new_msr &
3993f542974SPaul B Schroeder 	    (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
4003f542974SPaul B Schroeder 	     MOS_MSR_DELTA_CD)) {
4018c1a07ffSJohan Hovold 		icount = &mos7840_port->port->icount;
4023f542974SPaul B Schroeder 
4033f542974SPaul B Schroeder 		/* update input line counters */
404c9fac853SJohan Hovold 		if (new_msr & MOS_MSR_DELTA_CTS)
4053f542974SPaul B Schroeder 			icount->cts++;
406c9fac853SJohan Hovold 		if (new_msr & MOS_MSR_DELTA_DSR)
4073f542974SPaul B Schroeder 			icount->dsr++;
408c9fac853SJohan Hovold 		if (new_msr & MOS_MSR_DELTA_CD)
4093f542974SPaul B Schroeder 			icount->dcd++;
410c9fac853SJohan Hovold 		if (new_msr & MOS_MSR_DELTA_RI)
4113f542974SPaul B Schroeder 			icount->rng++;
412e670c6afSJohan Hovold 
4130c613371SJohan Hovold 		wake_up_interruptible(&port->port->port.delta_msr_wait);
4143f542974SPaul B Schroeder 	}
4153f542974SPaul B Schroeder }
4163f542974SPaul B Schroeder 
4170de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
4183f542974SPaul B Schroeder {
4193f542974SPaul B Schroeder 	struct async_icount *icount;
4203f542974SPaul B Schroeder 
4213f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
422880af9dbSAlan Cox 		/*
423880af9dbSAlan Cox 		 * Parity and Framing errors only count if they
424880af9dbSAlan Cox 		 * occur exclusive of a break being
425880af9dbSAlan Cox 		 * received.
426880af9dbSAlan Cox 		 */
4273f542974SPaul B Schroeder 		new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
4283f542974SPaul B Schroeder 	}
4293f542974SPaul B Schroeder 
4303f542974SPaul B Schroeder 	/* update input line counters */
4318c1a07ffSJohan Hovold 	icount = &port->port->icount;
432c9fac853SJohan Hovold 	if (new_lsr & SERIAL_LSR_BI)
4333f542974SPaul B Schroeder 		icount->brk++;
434c9fac853SJohan Hovold 	if (new_lsr & SERIAL_LSR_OE)
4353f542974SPaul B Schroeder 		icount->overrun++;
436c9fac853SJohan Hovold 	if (new_lsr & SERIAL_LSR_PE)
4373f542974SPaul B Schroeder 		icount->parity++;
438c9fac853SJohan Hovold 	if (new_lsr & SERIAL_LSR_FE)
4393f542974SPaul B Schroeder 		icount->frame++;
4403f542974SPaul B Schroeder }
4413f542974SPaul B Schroeder 
4423f542974SPaul B Schroeder /************************************************************************/
4433f542974SPaul B Schroeder /************************************************************************/
4443f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4453f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4463f542974SPaul B Schroeder /************************************************************************/
4473f542974SPaul B Schroeder /************************************************************************/
4483f542974SPaul B Schroeder 
4497d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb)
4503f542974SPaul B Schroeder {
4513f542974SPaul B Schroeder 	unsigned char *data;
4523f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4539c134a14SGreg Kroah-Hartman 	struct device *dev = &urb->dev->dev;
4543f542974SPaul B Schroeder 	__u8 regval = 0x0;
4550643c724SGreg Kroah-Hartman 	int status = urb->status;
4563f542974SPaul B Schroeder 
457cdc97792SMing Lei 	mos7840_port = urb->context;
4580de9a702SOliver Neukum 
4590643c724SGreg Kroah-Hartman 	switch (status) {
4603f542974SPaul B Schroeder 	case 0:
4613f542974SPaul B Schroeder 		/* success */
4623f542974SPaul B Schroeder 		break;
4633f542974SPaul B Schroeder 	case -ECONNRESET:
4643f542974SPaul B Schroeder 	case -ENOENT:
4653f542974SPaul B Schroeder 	case -ESHUTDOWN:
4663f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
4679c134a14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
468d8a083ccSJohan Hovold 		goto out;
4693f542974SPaul B Schroeder 	default:
4709c134a14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
471d8a083ccSJohan Hovold 		goto out;
4723f542974SPaul B Schroeder 	}
4733f542974SPaul B Schroeder 
4749c134a14SGreg Kroah-Hartman 	dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
4759c134a14SGreg Kroah-Hartman 	dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
4763f542974SPaul B Schroeder 		mos7840_port->MsrLsr, mos7840_port->port_num);
4773f542974SPaul B Schroeder 	data = urb->transfer_buffer;
4783f542974SPaul B Schroeder 	regval = (__u8) data[0];
4799c134a14SGreg Kroah-Hartman 	dev_dbg(dev, "%s data is %x\n", __func__, regval);
4803f542974SPaul B Schroeder 	if (mos7840_port->MsrLsr == 0)
4813f542974SPaul B Schroeder 		mos7840_handle_new_msr(mos7840_port, regval);
4823f542974SPaul B Schroeder 	else if (mos7840_port->MsrLsr == 1)
4833f542974SPaul B Schroeder 		mos7840_handle_new_lsr(mos7840_port, regval);
484d8a083ccSJohan Hovold out:
485d8a083ccSJohan Hovold 	clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
4863f542974SPaul B Schroeder }
4873f542974SPaul B Schroeder 
4883f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
4893f542974SPaul B Schroeder 			   __u16 *val)
4903f542974SPaul B Schroeder {
4913f542974SPaul B Schroeder 	struct usb_device *dev = mcs->port->serial->dev;
4920de9a702SOliver Neukum 	struct usb_ctrlrequest *dr = mcs->dr;
4930de9a702SOliver Neukum 	unsigned char *buffer = mcs->ctrl_buf;
4940de9a702SOliver Neukum 	int ret;
4953f542974SPaul B Schroeder 
496d8a083ccSJohan Hovold 	if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
497d8a083ccSJohan Hovold 		return -EBUSY;
498d8a083ccSJohan Hovold 
4993f542974SPaul B Schroeder 	dr->bRequestType = MCS_RD_RTYPE;
5003f542974SPaul B Schroeder 	dr->bRequest = MCS_RDREQ;
501880af9dbSAlan Cox 	dr->wValue = cpu_to_le16(Wval);	/* 0 */
5023f542974SPaul B Schroeder 	dr->wIndex = cpu_to_le16(reg);
5033f542974SPaul B Schroeder 	dr->wLength = cpu_to_le16(2);
5043f542974SPaul B Schroeder 
5053f542974SPaul B Schroeder 	usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
5063f542974SPaul B Schroeder 			     (unsigned char *)dr, buffer, 2,
5073f542974SPaul B Schroeder 			     mos7840_control_callback, mcs);
5083f542974SPaul B Schroeder 	mcs->control_urb->transfer_buffer_length = 2;
5093f542974SPaul B Schroeder 	ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
510d8a083ccSJohan Hovold 	if (ret)
511d8a083ccSJohan Hovold 		clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
512d8a083ccSJohan Hovold 
5133f542974SPaul B Schroeder 	return ret;
5143f542974SPaul B Schroeder }
5153f542974SPaul B Schroeder 
5160eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb)
5170eafe4deSDonald {
5180eafe4deSDonald 	switch (urb->status) {
5190eafe4deSDonald 	case 0:
5200eafe4deSDonald 		/* Success */
5210eafe4deSDonald 		break;
5220eafe4deSDonald 	case -ECONNRESET:
5230eafe4deSDonald 	case -ENOENT:
5240eafe4deSDonald 	case -ESHUTDOWN:
5250eafe4deSDonald 		/* This urb is terminated, clean up */
5269c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
5279c134a14SGreg Kroah-Hartman 			__func__, urb->status);
5280eafe4deSDonald 		break;
5290eafe4deSDonald 	default:
5309c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
5319c134a14SGreg Kroah-Hartman 			__func__, urb->status);
5320eafe4deSDonald 	}
5330eafe4deSDonald }
5340eafe4deSDonald 
5350eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
5360eafe4deSDonald 				__u16 reg)
5370eafe4deSDonald {
5380eafe4deSDonald 	struct usb_device *dev = mcs->port->serial->dev;
53905cf0decSJohan Hovold 	struct usb_ctrlrequest *dr = mcs->led_dr;
5400eafe4deSDonald 
5410eafe4deSDonald 	dr->bRequestType = MCS_WR_RTYPE;
5420eafe4deSDonald 	dr->bRequest = MCS_WRREQ;
5430eafe4deSDonald 	dr->wValue = cpu_to_le16(wval);
5440eafe4deSDonald 	dr->wIndex = cpu_to_le16(reg);
5450eafe4deSDonald 	dr->wLength = cpu_to_le16(0);
5460eafe4deSDonald 
54705cf0decSJohan Hovold 	usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
5480eafe4deSDonald 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
5490eafe4deSDonald 
55005cf0decSJohan Hovold 	usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
5510eafe4deSDonald }
5520eafe4deSDonald 
5530eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
5540eafe4deSDonald 				__u16 val)
5550eafe4deSDonald {
5560eafe4deSDonald 	struct usb_device *dev = port->serial->dev;
5570eafe4deSDonald 
5580eafe4deSDonald 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
5590eafe4deSDonald 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
5600eafe4deSDonald }
5610eafe4deSDonald 
5620eafe4deSDonald static void mos7840_led_off(unsigned long arg)
5630eafe4deSDonald {
5640eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
5650eafe4deSDonald 
5660eafe4deSDonald 	/* Turn off LED */
5670eafe4deSDonald 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
5680eafe4deSDonald 	mod_timer(&mcs->led_timer2,
5690eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_OFF_MS));
5700eafe4deSDonald }
5710eafe4deSDonald 
5720eafe4deSDonald static void mos7840_led_flag_off(unsigned long arg)
5730eafe4deSDonald {
5740eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
5750eafe4deSDonald 
57605cf0decSJohan Hovold 	clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
57705cf0decSJohan Hovold }
57805cf0decSJohan Hovold 
57905cf0decSJohan Hovold static void mos7840_led_activity(struct usb_serial_port *port)
58005cf0decSJohan Hovold {
58105cf0decSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
58205cf0decSJohan Hovold 
58305cf0decSJohan Hovold 	if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
58405cf0decSJohan Hovold 		return;
58505cf0decSJohan Hovold 
58605cf0decSJohan Hovold 	mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
58705cf0decSJohan Hovold 	mod_timer(&mos7840_port->led_timer1,
58805cf0decSJohan Hovold 				jiffies + msecs_to_jiffies(LED_ON_MS));
5890eafe4deSDonald }
5900eafe4deSDonald 
5913f542974SPaul B Schroeder /*****************************************************************************
5923f542974SPaul B Schroeder  * mos7840_interrupt_callback
5933f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
5943f542974SPaul B Schroeder  *	interrupt endpoint.
5953f542974SPaul B Schroeder  *****************************************************************************/
5963f542974SPaul B Schroeder 
5977d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb)
5983f542974SPaul B Schroeder {
5993f542974SPaul B Schroeder 	int result;
6003f542974SPaul B Schroeder 	int length;
6013f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
6023f542974SPaul B Schroeder 	struct usb_serial *serial;
6033f542974SPaul B Schroeder 	__u16 Data;
6043f542974SPaul B Schroeder 	unsigned char *data;
6053f542974SPaul B Schroeder 	__u8 sp[5], st;
6060de9a702SOliver Neukum 	int i, rv = 0;
6070de9a702SOliver Neukum 	__u16 wval, wreg = 0;
6080643c724SGreg Kroah-Hartman 	int status = urb->status;
6093f542974SPaul B Schroeder 
6100643c724SGreg Kroah-Hartman 	switch (status) {
6113f542974SPaul B Schroeder 	case 0:
6123f542974SPaul B Schroeder 		/* success */
6133f542974SPaul B Schroeder 		break;
6143f542974SPaul B Schroeder 	case -ECONNRESET:
6153f542974SPaul B Schroeder 	case -ENOENT:
6163f542974SPaul B Schroeder 	case -ESHUTDOWN:
6173f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
6189c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
6199c134a14SGreg Kroah-Hartman 			__func__, status);
6203f542974SPaul B Schroeder 		return;
6213f542974SPaul B Schroeder 	default:
6229c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
6239c134a14SGreg Kroah-Hartman 			__func__, status);
6243f542974SPaul B Schroeder 		goto exit;
6253f542974SPaul B Schroeder 	}
6263f542974SPaul B Schroeder 
6273f542974SPaul B Schroeder 	length = urb->actual_length;
6283f542974SPaul B Schroeder 	data = urb->transfer_buffer;
6293f542974SPaul B Schroeder 
630cdc97792SMing Lei 	serial = urb->context;
6313f542974SPaul B Schroeder 
6323f542974SPaul B Schroeder 	/* Moschip get 5 bytes
6333f542974SPaul B Schroeder 	 * Byte 1 IIR Port 1 (port.number is 0)
6343f542974SPaul B Schroeder 	 * Byte 2 IIR Port 2 (port.number is 1)
6353f542974SPaul B Schroeder 	 * Byte 3 IIR Port 3 (port.number is 2)
6363f542974SPaul B Schroeder 	 * Byte 4 IIR Port 4 (port.number is 3)
6373f542974SPaul B Schroeder 	 * Byte 5 FIFO status for both */
6383f542974SPaul B Schroeder 
6393f542974SPaul B Schroeder 	if (length && length > 5) {
6409c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
6413f542974SPaul B Schroeder 		return;
6423f542974SPaul B Schroeder 	}
6433f542974SPaul B Schroeder 
6443f542974SPaul B Schroeder 	sp[0] = (__u8) data[0];
6453f542974SPaul B Schroeder 	sp[1] = (__u8) data[1];
6463f542974SPaul B Schroeder 	sp[2] = (__u8) data[2];
6473f542974SPaul B Schroeder 	sp[3] = (__u8) data[3];
6483f542974SPaul B Schroeder 	st = (__u8) data[4];
6493f542974SPaul B Schroeder 
6503f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; i++) {
6513f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
6521143832eSGreg Kroah-Hartman 		wval = ((__u16)serial->port[i]->port_number + 1) << 8;
6533f542974SPaul B Schroeder 		if (mos7840_port->open) {
6543f542974SPaul B Schroeder 			if (sp[i] & 0x01) {
6559c134a14SGreg Kroah-Hartman 				dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
6563f542974SPaul B Schroeder 			} else {
6573f542974SPaul B Schroeder 				switch (sp[i] & 0x0f) {
6583f542974SPaul B Schroeder 				case SERIAL_IIR_RLS:
6599c134a14SGreg Kroah-Hartman 					dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
6609c134a14SGreg Kroah-Hartman 					dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
6613f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 1;
6620de9a702SOliver Neukum 					wreg = LINE_STATUS_REGISTER;
6633f542974SPaul B Schroeder 					break;
6643f542974SPaul B Schroeder 				case SERIAL_IIR_MS:
6659c134a14SGreg Kroah-Hartman 					dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
6663f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 0;
6670de9a702SOliver Neukum 					wreg = MODEM_STATUS_REGISTER;
6683f542974SPaul B Schroeder 					break;
6693f542974SPaul B Schroeder 				}
6700de9a702SOliver Neukum 				rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
6713f542974SPaul B Schroeder 			}
6723f542974SPaul B Schroeder 		}
6733f542974SPaul B Schroeder 	}
674880af9dbSAlan Cox 	if (!(rv < 0))
675880af9dbSAlan Cox 		/* the completion handler for the control urb will resubmit */
6760de9a702SOliver Neukum 		return;
6773f542974SPaul B Schroeder exit:
6783f542974SPaul B Schroeder 	result = usb_submit_urb(urb, GFP_ATOMIC);
6793f542974SPaul B Schroeder 	if (result) {
6803f542974SPaul B Schroeder 		dev_err(&urb->dev->dev,
6813f542974SPaul B Schroeder 			"%s - Error %d submitting interrupt urb\n",
682441b62c1SHarvey Harrison 			__func__, result);
6833f542974SPaul B Schroeder 	}
6843f542974SPaul B Schroeder }
6853f542974SPaul B Schroeder 
6863f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port,
6873f542974SPaul B Schroeder 				       const char *function)
6883f542974SPaul B Schroeder {
6893f542974SPaul B Schroeder 	if (!port) {
6909c134a14SGreg Kroah-Hartman 		pr_debug("%s - port == NULL\n", function);
6913f542974SPaul B Schroeder 		return -1;
6923f542974SPaul B Schroeder 	}
6933f542974SPaul B Schroeder 	if (!port->serial) {
6949c134a14SGreg Kroah-Hartman 		pr_debug("%s - port->serial == NULL\n", function);
6953f542974SPaul B Schroeder 		return -1;
6963f542974SPaul B Schroeder 	}
6973f542974SPaul B Schroeder 
6983f542974SPaul B Schroeder 	return 0;
6993f542974SPaul B Schroeder }
7003f542974SPaul B Schroeder 
7013f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
7023f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
7033f542974SPaul B Schroeder 					 const char *function)
7043f542974SPaul B Schroeder {
7053f542974SPaul B Schroeder 	if (!serial) {
7069c134a14SGreg Kroah-Hartman 		pr_debug("%s - serial == NULL\n", function);
7073f542974SPaul B Schroeder 		return -1;
7083f542974SPaul B Schroeder 	}
7093f542974SPaul B Schroeder 	if (!serial->type) {
7109c134a14SGreg Kroah-Hartman 		pr_debug("%s - serial->type == NULL!\n", function);
7113f542974SPaul B Schroeder 		return -1;
7123f542974SPaul B Schroeder 	}
7133f542974SPaul B Schroeder 
7143f542974SPaul B Schroeder 	return 0;
7153f542974SPaul B Schroeder }
7163f542974SPaul B Schroeder 
7173f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
7183f542974SPaul B Schroeder 						 const char *function)
7193f542974SPaul B Schroeder {
7203f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
7213f542974SPaul B Schroeder 	if (!port ||
7223f542974SPaul B Schroeder 	    mos7840_port_paranoia_check(port, function) ||
7233f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
724880af9dbSAlan Cox 		/* then say that we don't have a valid usb_serial thing,
725880af9dbSAlan Cox 		 * which will end up genrating -ENODEV return values */
7263f542974SPaul B Schroeder 		return NULL;
7273f542974SPaul B Schroeder 	}
7283f542974SPaul B Schroeder 
7293f542974SPaul B Schroeder 	return port->serial;
7303f542974SPaul B Schroeder }
7313f542974SPaul B Schroeder 
7323f542974SPaul B Schroeder /*****************************************************************************
7333f542974SPaul B Schroeder  * mos7840_bulk_in_callback
7343f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
7353f542974SPaul B Schroeder  *	bulk in endpoint.
7363f542974SPaul B Schroeder  *****************************************************************************/
7373f542974SPaul B Schroeder 
7387d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
7393f542974SPaul B Schroeder {
7400643c724SGreg Kroah-Hartman 	int retval;
7413f542974SPaul B Schroeder 	unsigned char *data;
7423f542974SPaul B Schroeder 	struct usb_serial *serial;
7433f542974SPaul B Schroeder 	struct usb_serial_port *port;
7443f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
7450643c724SGreg Kroah-Hartman 	int status = urb->status;
7463f542974SPaul B Schroeder 
747cdc97792SMing Lei 	mos7840_port = urb->context;
7489c134a14SGreg Kroah-Hartman 	if (!mos7840_port)
74950de36f7SGreg Kroah-Hartman 		return;
75050de36f7SGreg Kroah-Hartman 
75150de36f7SGreg Kroah-Hartman 	if (status) {
7529c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
75350de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7543f542974SPaul B Schroeder 		return;
7553f542974SPaul B Schroeder 	}
7563f542974SPaul B Schroeder 
7579c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
758441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
75950de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7603f542974SPaul B Schroeder 		return;
7613f542974SPaul B Schroeder 	}
7623f542974SPaul B Schroeder 
763441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
7643f542974SPaul B Schroeder 	if (!serial) {
76550de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7663f542974SPaul B Schroeder 		return;
7673f542974SPaul B Schroeder 	}
7683f542974SPaul B Schroeder 
7693f542974SPaul B Schroeder 	data = urb->transfer_buffer;
77059d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
7713f542974SPaul B Schroeder 
7723f542974SPaul B Schroeder 	if (urb->actual_length) {
77305c7cd39SJiri Slaby 		struct tty_port *tport = &mos7840_port->port->port;
77405c7cd39SJiri Slaby 		tty_insert_flip_string(tport, data, urb->actual_length);
7752e124b4aSJiri Slaby 		tty_flip_buffer_push(tport);
7768c1a07ffSJohan Hovold 		port->icount.rx += urb->actual_length;
7778c1a07ffSJohan Hovold 		dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
7783f542974SPaul B Schroeder 	}
7793f542974SPaul B Schroeder 
7803f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
7819c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
78250de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7833f542974SPaul B Schroeder 		return;
7843f542974SPaul B Schroeder 	}
7853f542974SPaul B Schroeder 
78605cf0decSJohan Hovold 	if (mos7840_port->has_led)
78705cf0decSJohan Hovold 		mos7840_led_activity(port);
7880de9a702SOliver Neukum 
78950de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
7900643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
7913f542974SPaul B Schroeder 
7920643c724SGreg Kroah-Hartman 	if (retval) {
7939c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
79450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7953f542974SPaul B Schroeder 	}
7963f542974SPaul B Schroeder }
7973f542974SPaul B Schroeder 
7983f542974SPaul B Schroeder /*****************************************************************************
7993f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
800880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
801880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
8023f542974SPaul B Schroeder  *****************************************************************************/
8033f542974SPaul B Schroeder 
8047d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
8053f542974SPaul B Schroeder {
8063f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8079c134a14SGreg Kroah-Hartman 	struct usb_serial_port *port;
8080643c724SGreg Kroah-Hartman 	int status = urb->status;
8090de9a702SOliver Neukum 	int i;
8100de9a702SOliver Neukum 
811cdc97792SMing Lei 	mos7840_port = urb->context;
8129c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
8130de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
8140de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
8150de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
8160de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
8170de9a702SOliver Neukum 			break;
8180de9a702SOliver Neukum 		}
8190de9a702SOliver Neukum 	}
8200de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
8210de9a702SOliver Neukum 
8220643c724SGreg Kroah-Hartman 	if (status) {
8239c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
8243f542974SPaul B Schroeder 		return;
8253f542974SPaul B Schroeder 	}
8263f542974SPaul B Schroeder 
8279c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
8283f542974SPaul B Schroeder 		return;
8293f542974SPaul B Schroeder 
8306aad04f2SJiri Slaby 	if (mos7840_port->open)
8316aad04f2SJiri Slaby 		tty_port_tty_wakeup(&port->port);
8323f542974SPaul B Schroeder 
8333f542974SPaul B Schroeder }
8343f542974SPaul B Schroeder 
8353f542974SPaul B Schroeder /************************************************************************/
8363f542974SPaul B Schroeder /*       D R I V E R  T T Y  I N T E R F A C E  F U N C T I O N S       */
8373f542974SPaul B Schroeder /************************************************************************/
8383f542974SPaul B Schroeder 
8393f542974SPaul B Schroeder /*****************************************************************************
8403f542974SPaul B Schroeder  * mos7840_open
8413f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
8423f542974SPaul B Schroeder  *	If successful, we return 0
8433f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
8443f542974SPaul B Schroeder  *****************************************************************************/
8453f542974SPaul B Schroeder 
846a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
8473f542974SPaul B Schroeder {
8483f542974SPaul B Schroeder 	int response;
8493f542974SPaul B Schroeder 	int j;
8503f542974SPaul B Schroeder 	struct usb_serial *serial;
8513f542974SPaul B Schroeder 	struct urb *urb;
8523f542974SPaul B Schroeder 	__u16 Data;
8533f542974SPaul B Schroeder 	int status;
8543f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8550de9a702SOliver Neukum 	struct moschip_port *port0;
8563f542974SPaul B Schroeder 
8579c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
8583f542974SPaul B Schroeder 		return -ENODEV;
8593f542974SPaul B Schroeder 
8603f542974SPaul B Schroeder 	serial = port->serial;
8613f542974SPaul B Schroeder 
8629c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
8633f542974SPaul B Schroeder 		return -ENODEV;
8643f542974SPaul B Schroeder 
8653f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
8660de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
8673f542974SPaul B Schroeder 
8680de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
8693f542974SPaul B Schroeder 		return -ENODEV;
8703f542974SPaul B Schroeder 
8713f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
8723f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
8730de9a702SOliver Neukum 	port0->open_ports++;
8743f542974SPaul B Schroeder 
8753f542974SPaul B Schroeder 	/* Initialising the write urb pool */
8763f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
8770de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
8783f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
879*10c642d0SJohan Hovold 		if (!urb)
8803f542974SPaul B Schroeder 			continue;
8813f542974SPaul B Schroeder 
882880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
883880af9dbSAlan Cox 								GFP_KERNEL);
8843f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
8850de9a702SOliver Neukum 			usb_free_urb(urb);
8860de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
8873f542974SPaul B Schroeder 			continue;
8883f542974SPaul B Schroeder 		}
8893f542974SPaul B Schroeder 	}
8903f542974SPaul B Schroeder 
8913f542974SPaul B Schroeder /*****************************************************************************
8923f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
8933f542974SPaul B Schroeder  *
8943f542974SPaul B Schroeder  * Register Index
8953f542974SPaul B Schroeder  * 1 : IER
8963f542974SPaul B Schroeder  * 2 : FCR
8973f542974SPaul B Schroeder  * 3 : LCR
8983f542974SPaul B Schroeder  * 4 : MCR
8993f542974SPaul B Schroeder  *
9003f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
9013f542974SPaul B Schroeder  *****************************************************************************/
9023f542974SPaul B Schroeder 
903880af9dbSAlan Cox 	/* NEED to check the following Block */
9043f542974SPaul B Schroeder 
9053f542974SPaul B Schroeder 	Data = 0x0;
9063f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
9073f542974SPaul B Schroeder 	if (status < 0) {
9089c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Spreg failed\n");
9095f8a2e68SJohan Hovold 		goto err;
9103f542974SPaul B Schroeder 	}
9113f542974SPaul B Schroeder 	Data |= 0x80;
9123f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9133f542974SPaul B Schroeder 	if (status < 0) {
9149c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
9155f8a2e68SJohan Hovold 		goto err;
9163f542974SPaul B Schroeder 	}
9173f542974SPaul B Schroeder 
9183f542974SPaul B Schroeder 	Data &= ~0x80;
9193f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9203f542974SPaul B Schroeder 	if (status < 0) {
9219c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
9225f8a2e68SJohan Hovold 		goto err;
9233f542974SPaul B Schroeder 	}
924880af9dbSAlan Cox 	/* End of block to be checked */
9253f542974SPaul B Schroeder 
9263f542974SPaul B Schroeder 	Data = 0x0;
927880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
928880af9dbSAlan Cox 									&Data);
9293f542974SPaul B Schroeder 	if (status < 0) {
9309c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Controlreg failed\n");
9315f8a2e68SJohan Hovold 		goto err;
9323f542974SPaul B Schroeder 	}
933880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
934880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
935880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
936880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
9373f542974SPaul B Schroeder 	if (status < 0) {
9389c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Controlreg failed\n");
9395f8a2e68SJohan Hovold 		goto err;
9403f542974SPaul B Schroeder 	}
941880af9dbSAlan Cox 	/* do register settings here */
942880af9dbSAlan Cox 	/* Set all regs to the device default values. */
943880af9dbSAlan Cox 	/***********************************
944880af9dbSAlan Cox 	 * First Disable all interrupts.
945880af9dbSAlan Cox 	 ***********************************/
9463f542974SPaul B Schroeder 	Data = 0x00;
9473f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
9483f542974SPaul B Schroeder 	if (status < 0) {
9499c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "disabling interrupts failed\n");
9505f8a2e68SJohan Hovold 		goto err;
9513f542974SPaul B Schroeder 	}
952880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
9533f542974SPaul B Schroeder 	Data = 0x00;
9543f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9553f542974SPaul B Schroeder 	if (status < 0) {
9569c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
9575f8a2e68SJohan Hovold 		goto err;
9583f542974SPaul B Schroeder 	}
9593f542974SPaul B Schroeder 
9603f542974SPaul B Schroeder 	Data = 0xcf;
9613f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9623f542974SPaul B Schroeder 	if (status < 0) {
9639c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
9645f8a2e68SJohan Hovold 		goto err;
9653f542974SPaul B Schroeder 	}
9663f542974SPaul B Schroeder 
9673f542974SPaul B Schroeder 	Data = 0x03;
9683f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9693f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9703f542974SPaul B Schroeder 
9713f542974SPaul B Schroeder 	Data = 0x0b;
9723f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
9733f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
9743f542974SPaul B Schroeder 
9753f542974SPaul B Schroeder 	Data = 0x00;
9763f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
9773f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9783f542974SPaul B Schroeder 
979880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
9803f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9813f542974SPaul B Schroeder 
9823f542974SPaul B Schroeder 	Data = 0x0c;
9833f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
9843f542974SPaul B Schroeder 
9853f542974SPaul B Schroeder 	Data = 0x0;
9863f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
9873f542974SPaul B Schroeder 
9883f542974SPaul B Schroeder 	Data = 0x00;
9893f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
9903f542974SPaul B Schroeder 
9913f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
9923f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9933f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9943f542974SPaul B Schroeder 
995880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
9963f542974SPaul B Schroeder 	Data = 0x0;
9973f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
9983f542974SPaul B Schroeder 
9993f542974SPaul B Schroeder 	Data = Data | 0x0c;
10003f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10013f542974SPaul B Schroeder 
10023f542974SPaul B Schroeder 	Data = Data & ~0x0c;
10033f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1004880af9dbSAlan Cox 	/* Finally enable all interrupts */
10053f542974SPaul B Schroeder 	Data = 0x0c;
10063f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
10073f542974SPaul B Schroeder 
1008880af9dbSAlan Cox 	/* clearing rx_disable */
10093f542974SPaul B Schroeder 	Data = 0x0;
1010880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1011880af9dbSAlan Cox 									&Data);
10123f542974SPaul B Schroeder 	Data = Data & ~0x20;
1013880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1014880af9dbSAlan Cox 									Data);
10153f542974SPaul B Schroeder 
1016880af9dbSAlan Cox 	/* rx_negate */
10173f542974SPaul B Schroeder 	Data = 0x0;
1018880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1019880af9dbSAlan Cox 									&Data);
10203f542974SPaul B Schroeder 	Data = Data | 0x10;
1021880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1022880af9dbSAlan Cox 									Data);
10233f542974SPaul B Schroeder 
10243f542974SPaul B Schroeder 	/* Check to see if we've set up our endpoint info yet    *
10253f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the structures *
10263f542974SPaul B Schroeder 	 * were not set up at that time.)                        */
10270de9a702SOliver Neukum 	if (port0->open_ports == 1) {
10283f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_buffer == NULL) {
10293f542974SPaul B Schroeder 			/* set up interrupt urb */
10303f542974SPaul B Schroeder 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
10313f542974SPaul B Schroeder 				serial->dev,
10323f542974SPaul B Schroeder 				usb_rcvintpipe(serial->dev,
1033880af9dbSAlan Cox 				serial->port[0]->interrupt_in_endpointAddress),
10343f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_buffer,
10353f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_urb->
10363f542974SPaul B Schroeder 				transfer_buffer_length,
10373f542974SPaul B Schroeder 				mos7840_interrupt_callback,
10383f542974SPaul B Schroeder 				serial,
1039880af9dbSAlan Cox 				serial->port[0]->interrupt_in_urb->interval);
10403f542974SPaul B Schroeder 
10413f542974SPaul B Schroeder 			/* start interrupt read for mos7840               *
10423f542974SPaul B Schroeder 			 * will continue as long as mos7840 is connected  */
10433f542974SPaul B Schroeder 
10443f542974SPaul B Schroeder 			response =
10453f542974SPaul B Schroeder 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
10463f542974SPaul B Schroeder 					   GFP_KERNEL);
10473f542974SPaul B Schroeder 			if (response) {
1048194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "%s - Error %d submitting "
1049194343d9SGreg Kroah-Hartman 					"interrupt urb\n", __func__, response);
10503f542974SPaul B Schroeder 			}
10513f542974SPaul B Schroeder 
10523f542974SPaul B Schroeder 		}
10533f542974SPaul B Schroeder 
10543f542974SPaul B Schroeder 	}
10553f542974SPaul B Schroeder 
10563f542974SPaul B Schroeder 	/* see if we've set up our endpoint info yet   *
10573f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the  *
10583f542974SPaul B Schroeder 	 * structures were not set up at that time.)   */
10593f542974SPaul B Schroeder 
10601143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "port number is %d\n", port->port_number);
1061e5b1e206SGreg Kroah-Hartman 	dev_dbg(&port->dev, "minor number is %d\n", port->minor);
10629c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
10639c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
10649c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
10659c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
10663f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
10673f542974SPaul B Schroeder 
10683f542974SPaul B Schroeder 	/* set up our bulk in urb */
10691143832eSGreg Kroah-Hartman 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1070093ea2d3SDonald Lee 		usb_fill_bulk_urb(mos7840_port->read_urb,
1071093ea2d3SDonald Lee 			serial->dev,
1072093ea2d3SDonald Lee 			usb_rcvbulkpipe(serial->dev,
1073093ea2d3SDonald Lee 				(port->bulk_in_endpointAddress) + 2),
1074093ea2d3SDonald Lee 			port->bulk_in_buffer,
1075093ea2d3SDonald Lee 			mos7840_port->read_urb->transfer_buffer_length,
1076093ea2d3SDonald Lee 			mos7840_bulk_in_callback, mos7840_port);
1077093ea2d3SDonald Lee 	} else {
10783f542974SPaul B Schroeder 		usb_fill_bulk_urb(mos7840_port->read_urb,
10793f542974SPaul B Schroeder 			serial->dev,
10803f542974SPaul B Schroeder 			usb_rcvbulkpipe(serial->dev,
10813f542974SPaul B Schroeder 				port->bulk_in_endpointAddress),
10823f542974SPaul B Schroeder 			port->bulk_in_buffer,
10833f542974SPaul B Schroeder 			mos7840_port->read_urb->transfer_buffer_length,
10843f542974SPaul B Schroeder 			mos7840_bulk_in_callback, mos7840_port);
1085093ea2d3SDonald Lee 	}
10863f542974SPaul B Schroeder 
10879c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
108850de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
10893f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
10903f542974SPaul B Schroeder 	if (response) {
1091194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1092194343d9SGreg Kroah-Hartman 			__func__, response);
109350de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
10943f542974SPaul B Schroeder 	}
10953f542974SPaul B Schroeder 
10963f542974SPaul B Schroeder 	/* initialize our port settings */
1097880af9dbSAlan Cox 	/* Must set to enable ints! */
1098880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
10993f542974SPaul B Schroeder 	/* send a open port command */
11003f542974SPaul B Schroeder 	mos7840_port->open = 1;
1101880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
11023f542974SPaul B Schroeder 
11033f542974SPaul B Schroeder 	return 0;
11045f8a2e68SJohan Hovold err:
11055f8a2e68SJohan Hovold 	for (j = 0; j < NUM_URBS; ++j) {
11065f8a2e68SJohan Hovold 		urb = mos7840_port->write_urb_pool[j];
11075f8a2e68SJohan Hovold 		if (!urb)
11085f8a2e68SJohan Hovold 			continue;
11095f8a2e68SJohan Hovold 		kfree(urb->transfer_buffer);
11105f8a2e68SJohan Hovold 		usb_free_urb(urb);
11115f8a2e68SJohan Hovold 	}
11125f8a2e68SJohan Hovold 	return status;
11133f542974SPaul B Schroeder }
11143f542974SPaul B Schroeder 
11153f542974SPaul B Schroeder /*****************************************************************************
11163f542974SPaul B Schroeder  * mos7840_chars_in_buffer
11173f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
11183f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
11193f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
11203f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
11213f542974SPaul B Schroeder  *	system,
112295da310eSAlan Cox  *	Otherwise we return zero.
11233f542974SPaul B Schroeder  *****************************************************************************/
11243f542974SPaul B Schroeder 
112595da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
11263f542974SPaul B Schroeder {
112795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11283f542974SPaul B Schroeder 	int i;
11293f542974SPaul B Schroeder 	int chars = 0;
11300de9a702SOliver Neukum 	unsigned long flags;
11313f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11323f542974SPaul B Schroeder 
11339c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
113495da310eSAlan Cox 		return 0;
11353f542974SPaul B Schroeder 
11363f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11373363155bSGreg Kroah-Hartman 	if (mos7840_port == NULL)
113895da310eSAlan Cox 		return 0;
11393f542974SPaul B Schroeder 
11400de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
11415c263b92SMark Ferrell 	for (i = 0; i < NUM_URBS; ++i) {
11425c263b92SMark Ferrell 		if (mos7840_port->busy[i]) {
11435c263b92SMark Ferrell 			struct urb *urb = mos7840_port->write_urb_pool[i];
11445c263b92SMark Ferrell 			chars += urb->transfer_buffer_length;
11455c263b92SMark Ferrell 		}
11465c263b92SMark Ferrell 	}
11470de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
11489c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
11490de9a702SOliver Neukum 	return chars;
11503f542974SPaul B Schroeder 
11513f542974SPaul B Schroeder }
11523f542974SPaul B Schroeder 
11533f542974SPaul B Schroeder /*****************************************************************************
11543f542974SPaul B Schroeder  * mos7840_close
11553f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
11563f542974SPaul B Schroeder  *****************************************************************************/
11573f542974SPaul B Schroeder 
1158335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
11593f542974SPaul B Schroeder {
11603f542974SPaul B Schroeder 	struct usb_serial *serial;
11613f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11620de9a702SOliver Neukum 	struct moschip_port *port0;
11633f542974SPaul B Schroeder 	int j;
11643f542974SPaul B Schroeder 	__u16 Data;
11653f542974SPaul B Schroeder 
11669c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
11673f542974SPaul B Schroeder 		return;
11683f542974SPaul B Schroeder 
1169441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
11709c134a14SGreg Kroah-Hartman 	if (!serial)
11713f542974SPaul B Schroeder 		return;
11723f542974SPaul B Schroeder 
11733f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11740de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
11753f542974SPaul B Schroeder 
11760de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
11773f542974SPaul B Schroeder 		return;
11783f542974SPaul B Schroeder 
11793f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
11803f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
11813f542974SPaul B Schroeder 
11823f542974SPaul B Schroeder 	/* Freeing Write URBs */
11833f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
11843f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
11853f542974SPaul B Schroeder 			if (mos7840_port->write_urb_pool[j]->transfer_buffer)
11863f542974SPaul B Schroeder 				kfree(mos7840_port->write_urb_pool[j]->
11873f542974SPaul B Schroeder 				      transfer_buffer);
11883f542974SPaul B Schroeder 
11893f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
11903f542974SPaul B Schroeder 		}
11913f542974SPaul B Schroeder 	}
11923f542974SPaul B Schroeder 
11933f542974SPaul B Schroeder 	usb_kill_urb(mos7840_port->write_urb);
11943f542974SPaul B Schroeder 	usb_kill_urb(mos7840_port->read_urb);
119550de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = false;
1196bb3529c6SJohan Hovold 
11970de9a702SOliver Neukum 	port0->open_ports--;
11981143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports);
11990de9a702SOliver Neukum 	if (port0->open_ports == 0) {
12003f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_urb) {
12019c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
12020de9a702SOliver Neukum 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
12033f542974SPaul B Schroeder 		}
12043f542974SPaul B Schroeder 	}
12053f542974SPaul B Schroeder 
12063f542974SPaul B Schroeder 	if (mos7840_port->write_urb) {
12073f542974SPaul B Schroeder 		/* if this urb had a transfer buffer already (old tx) free it */
12083f542974SPaul B Schroeder 		kfree(mos7840_port->write_urb->transfer_buffer);
12093f542974SPaul B Schroeder 		usb_free_urb(mos7840_port->write_urb);
12103f542974SPaul B Schroeder 	}
12113f542974SPaul B Schroeder 
12123f542974SPaul B Schroeder 	Data = 0x0;
12133f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
12143f542974SPaul B Schroeder 
12153f542974SPaul B Schroeder 	Data = 0x00;
12163f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
12173f542974SPaul B Schroeder 
12183f542974SPaul B Schroeder 	mos7840_port->open = 0;
12193f542974SPaul B Schroeder }
12203f542974SPaul B Schroeder 
12213f542974SPaul B Schroeder /*****************************************************************************
12223f542974SPaul B Schroeder  * mos7840_break
12233f542974SPaul B Schroeder  *	this function sends a break to the port
12243f542974SPaul B Schroeder  *****************************************************************************/
122595da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
12263f542974SPaul B Schroeder {
122795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
12283f542974SPaul B Schroeder 	unsigned char data;
12293f542974SPaul B Schroeder 	struct usb_serial *serial;
12303f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12313f542974SPaul B Schroeder 
12329c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
12333f542974SPaul B Schroeder 		return;
12343f542974SPaul B Schroeder 
1235441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
12369c134a14SGreg Kroah-Hartman 	if (!serial)
12373f542974SPaul B Schroeder 		return;
12383f542974SPaul B Schroeder 
12393f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12403f542974SPaul B Schroeder 
1241880af9dbSAlan Cox 	if (mos7840_port == NULL)
12423f542974SPaul B Schroeder 		return;
12433f542974SPaul B Schroeder 
124495da310eSAlan Cox 	if (break_state == -1)
12453f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
124695da310eSAlan Cox 	else
12473f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
12483f542974SPaul B Schroeder 
12496b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
12503f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
12519c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
12523f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
12533f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
12543f542974SPaul B Schroeder }
12553f542974SPaul B Schroeder 
12563f542974SPaul B Schroeder /*****************************************************************************
12573f542974SPaul B Schroeder  * mos7840_write_room
12583f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
12593f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
12603f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
12613f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
12623f542974SPaul B Schroeder  *****************************************************************************/
12633f542974SPaul B Schroeder 
126495da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
12653f542974SPaul B Schroeder {
126695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
12673f542974SPaul B Schroeder 	int i;
12683f542974SPaul B Schroeder 	int room = 0;
12690de9a702SOliver Neukum 	unsigned long flags;
12703f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12713f542974SPaul B Schroeder 
12729c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
12733f542974SPaul B Schroeder 		return -1;
12743f542974SPaul B Schroeder 
12753f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12769c134a14SGreg Kroah-Hartman 	if (mos7840_port == NULL)
12773f542974SPaul B Schroeder 		return -1;
12783f542974SPaul B Schroeder 
12790de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
12803f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
1281880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
12823f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
12833f542974SPaul B Schroeder 	}
12840de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
12853f542974SPaul B Schroeder 
12860de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
12879c134a14SGreg Kroah-Hartman 	dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
12880de9a702SOliver Neukum 	return room;
12893f542974SPaul B Schroeder 
12903f542974SPaul B Schroeder }
12913f542974SPaul B Schroeder 
12923f542974SPaul B Schroeder /*****************************************************************************
12933f542974SPaul B Schroeder  * mos7840_write
12943f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
12953f542974SPaul B Schroeder  *	the port.
12963f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
12973f542974SPaul B Schroeder  *      return a negative error number.
12983f542974SPaul B Schroeder  *****************************************************************************/
12993f542974SPaul B Schroeder 
130095da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
13013f542974SPaul B Schroeder 			 const unsigned char *data, int count)
13023f542974SPaul B Schroeder {
13033f542974SPaul B Schroeder 	int status;
13043f542974SPaul B Schroeder 	int i;
13053f542974SPaul B Schroeder 	int bytes_sent = 0;
13063f542974SPaul B Schroeder 	int transfer_size;
13070de9a702SOliver Neukum 	unsigned long flags;
13083f542974SPaul B Schroeder 
13093f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13103f542974SPaul B Schroeder 	struct usb_serial *serial;
13113f542974SPaul B Schroeder 	struct urb *urb;
1312880af9dbSAlan Cox 	/* __u16 Data; */
13133f542974SPaul B Schroeder 	const unsigned char *current_position = data;
13143f542974SPaul B Schroeder 	unsigned char *data1;
13153f542974SPaul B Schroeder 
13163f542974SPaul B Schroeder #ifdef NOTMOS7840
13173f542974SPaul B Schroeder 	Data = 0x00;
13183f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
13193f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
13209c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
13219c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
13223f542974SPaul B Schroeder 
1323880af9dbSAlan Cox 	/* Data = 0x03; */
1324880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1325880af9dbSAlan Cox 	/* mos7840_port->shadowLCR=Data;//Need to add later */
13263f542974SPaul B Schroeder 
1327880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
13283f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
13293f542974SPaul B Schroeder 
1330880af9dbSAlan Cox 	/* Data = 0x0c; */
1331880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
13323f542974SPaul B Schroeder 	Data = 0x00;
13333f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
13349c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
13353f542974SPaul B Schroeder 
13363f542974SPaul B Schroeder 	Data = 0x0;
13373f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
13389c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
13393f542974SPaul B Schroeder 
13403f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
13419c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
13423f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
13433f542974SPaul B Schroeder #endif
13443f542974SPaul B Schroeder 
13459c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
13463f542974SPaul B Schroeder 		return -1;
13473f542974SPaul B Schroeder 
13483f542974SPaul B Schroeder 	serial = port->serial;
13499c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
13503f542974SPaul B Schroeder 		return -1;
13513f542974SPaul B Schroeder 
13523f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13539c134a14SGreg Kroah-Hartman 	if (mos7840_port == NULL)
13543f542974SPaul B Schroeder 		return -1;
13553f542974SPaul B Schroeder 
13563f542974SPaul B Schroeder 	/* try to find a free urb in the list */
13573f542974SPaul B Schroeder 	urb = NULL;
13583f542974SPaul B Schroeder 
13590de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
13603f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
13610de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
13620de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
13633f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
13649c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "URB:%d\n", i);
13653f542974SPaul B Schroeder 			break;
13663f542974SPaul B Schroeder 		}
13673f542974SPaul B Schroeder 	}
13680de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
13693f542974SPaul B Schroeder 
13703f542974SPaul B Schroeder 	if (urb == NULL) {
13719c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
13723f542974SPaul B Schroeder 		goto exit;
13733f542974SPaul B Schroeder 	}
13743f542974SPaul B Schroeder 
13753f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
13763f542974SPaul B Schroeder 		urb->transfer_buffer =
13773f542974SPaul B Schroeder 		    kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1378*10c642d0SJohan Hovold 		if (!urb->transfer_buffer)
13793f542974SPaul B Schroeder 			goto exit;
13803f542974SPaul B Schroeder 	}
13813f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
13823f542974SPaul B Schroeder 
13833f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
13843f542974SPaul B Schroeder 
13853f542974SPaul B Schroeder 	/* fill urb with data and submit  */
13861143832eSGreg Kroah-Hartman 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1387093ea2d3SDonald Lee 		usb_fill_bulk_urb(urb,
1388093ea2d3SDonald Lee 			serial->dev,
1389093ea2d3SDonald Lee 			usb_sndbulkpipe(serial->dev,
1390093ea2d3SDonald Lee 				(port->bulk_out_endpointAddress) + 2),
1391093ea2d3SDonald Lee 			urb->transfer_buffer,
1392093ea2d3SDonald Lee 			transfer_size,
1393093ea2d3SDonald Lee 			mos7840_bulk_out_data_callback, mos7840_port);
1394093ea2d3SDonald Lee 	} else {
13953f542974SPaul B Schroeder 		usb_fill_bulk_urb(urb,
13963f542974SPaul B Schroeder 			serial->dev,
13973f542974SPaul B Schroeder 			usb_sndbulkpipe(serial->dev,
13983f542974SPaul B Schroeder 				port->bulk_out_endpointAddress),
13993f542974SPaul B Schroeder 			urb->transfer_buffer,
14003f542974SPaul B Schroeder 			transfer_size,
14013f542974SPaul B Schroeder 			mos7840_bulk_out_data_callback, mos7840_port);
1402093ea2d3SDonald Lee 	}
14033f542974SPaul B Schroeder 
14043f542974SPaul B Schroeder 	data1 = urb->transfer_buffer;
14059c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
14063f542974SPaul B Schroeder 
140705cf0decSJohan Hovold 	if (mos7840_port->has_led)
140805cf0decSJohan Hovold 		mos7840_led_activity(port);
14090eafe4deSDonald 
14103f542974SPaul B Schroeder 	/* send it down the pipe */
14113f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
14123f542974SPaul B Schroeder 
14133f542974SPaul B Schroeder 	if (status) {
14140de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
141522a416c4SJohan Hovold 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1416194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
14173f542974SPaul B Schroeder 		bytes_sent = status;
14183f542974SPaul B Schroeder 		goto exit;
14193f542974SPaul B Schroeder 	}
14203f542974SPaul B Schroeder 	bytes_sent = transfer_size;
14218c1a07ffSJohan Hovold 	port->icount.tx += transfer_size;
14228c1a07ffSJohan Hovold 	dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
14233f542974SPaul B Schroeder exit:
14243f542974SPaul B Schroeder 	return bytes_sent;
14253f542974SPaul B Schroeder 
14263f542974SPaul B Schroeder }
14273f542974SPaul B Schroeder 
14283f542974SPaul B Schroeder /*****************************************************************************
14293f542974SPaul B Schroeder  * mos7840_throttle
14303f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
14313f542974SPaul B Schroeder  *	being read from the port.
14323f542974SPaul B Schroeder  *****************************************************************************/
14333f542974SPaul B Schroeder 
143495da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
14353f542974SPaul B Schroeder {
143695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14373f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14383f542974SPaul B Schroeder 	int status;
14393f542974SPaul B Schroeder 
14409c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
14413f542974SPaul B Schroeder 		return;
14423f542974SPaul B Schroeder 
14433f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14443f542974SPaul B Schroeder 
14453f542974SPaul B Schroeder 	if (mos7840_port == NULL)
14463f542974SPaul B Schroeder 		return;
14473f542974SPaul B Schroeder 
14483f542974SPaul B Schroeder 	if (!mos7840_port->open) {
14499c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "port not opened\n");
14503f542974SPaul B Schroeder 		return;
14513f542974SPaul B Schroeder 	}
14523f542974SPaul B Schroeder 
14533f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
14543f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
14553f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
145695da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
145795da310eSAlan Cox 		if (status <= 0)
14583f542974SPaul B Schroeder 			return;
14593f542974SPaul B Schroeder 	}
14603f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
1461adc8d746SAlan Cox 	if (tty->termios.c_cflag & CRTSCTS) {
14623f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
146395da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
14643f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
146595da310eSAlan Cox 		if (status < 0)
14663f542974SPaul B Schroeder 			return;
14673f542974SPaul B Schroeder 	}
14683f542974SPaul B Schroeder }
14693f542974SPaul B Schroeder 
14703f542974SPaul B Schroeder /*****************************************************************************
14713f542974SPaul B Schroeder  * mos7840_unthrottle
1472880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
1473880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
1474880af9dbSAlan Cox  *	called)
14753f542974SPaul B Schroeder  *****************************************************************************/
147695da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
14773f542974SPaul B Schroeder {
147895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14793f542974SPaul B Schroeder 	int status;
14803f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
14813f542974SPaul B Schroeder 
14829c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
14833f542974SPaul B Schroeder 		return;
14843f542974SPaul B Schroeder 
14853f542974SPaul B Schroeder 	if (mos7840_port == NULL)
14863f542974SPaul B Schroeder 		return;
14873f542974SPaul B Schroeder 
14883f542974SPaul B Schroeder 	if (!mos7840_port->open) {
14899c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
14903f542974SPaul B Schroeder 		return;
14913f542974SPaul B Schroeder 	}
14923f542974SPaul B Schroeder 
14933f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
14943f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
14953f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
149695da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
149795da310eSAlan Cox 		if (status <= 0)
14983f542974SPaul B Schroeder 			return;
14993f542974SPaul B Schroeder 	}
15003f542974SPaul B Schroeder 
15013f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
1502adc8d746SAlan Cox 	if (tty->termios.c_cflag & CRTSCTS) {
15033f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
150495da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
15053f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
150695da310eSAlan Cox 		if (status < 0)
15073f542974SPaul B Schroeder 			return;
15083f542974SPaul B Schroeder 	}
15093f542974SPaul B Schroeder }
15103f542974SPaul B Schroeder 
151160b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
15123f542974SPaul B Schroeder {
151395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
15143f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15153f542974SPaul B Schroeder 	unsigned int result;
15163f542974SPaul B Schroeder 	__u16 msr;
15173f542974SPaul B Schroeder 	__u16 mcr;
1518880af9dbSAlan Cox 	int status;
15193f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15203f542974SPaul B Schroeder 
15213f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15223f542974SPaul B Schroeder 		return -ENODEV;
15233f542974SPaul B Schroeder 
15243f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1525a91ccd26SJohan Hovold 	if (status != 1)
1526a91ccd26SJohan Hovold 		return -EIO;
15273f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1528a91ccd26SJohan Hovold 	if (status != 1)
1529a91ccd26SJohan Hovold 		return -EIO;
15303f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
15313f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
15323f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
15333f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
15343f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
15353f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
15363f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
15373f542974SPaul B Schroeder 
15389c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
15393f542974SPaul B Schroeder 
15403f542974SPaul B Schroeder 	return result;
15413f542974SPaul B Schroeder }
15423f542974SPaul B Schroeder 
154320b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
15443f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
15453f542974SPaul B Schroeder {
154695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
15473f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15483f542974SPaul B Schroeder 	unsigned int mcr;
154987521c46SRoel Kluin 	int status;
15503f542974SPaul B Schroeder 
15513f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15523f542974SPaul B Schroeder 
15533f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15543f542974SPaul B Schroeder 		return -ENODEV;
15553f542974SPaul B Schroeder 
1556e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
15573f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
15583f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
15593f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
15603f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
15613f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
15623f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
15633f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
15643f542974SPaul B Schroeder 
15653f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
15663f542974SPaul B Schroeder 		mcr |= MCR_RTS;
15673f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
15683f542974SPaul B Schroeder 		mcr |= MCR_DTR;
15693f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
15703f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
15713f542974SPaul B Schroeder 
15723f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
15733f542974SPaul B Schroeder 
15743f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
15753f542974SPaul B Schroeder 	if (status < 0) {
15769c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
157787521c46SRoel Kluin 		return status;
15783f542974SPaul B Schroeder 	}
15793f542974SPaul B Schroeder 
15803f542974SPaul B Schroeder 	return 0;
15813f542974SPaul B Schroeder }
15823f542974SPaul B Schroeder 
15833f542974SPaul B Schroeder /*****************************************************************************
15843f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
15853f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
15863f542974SPaul B Schroeder  *	baud rate.
15873f542974SPaul B Schroeder  *****************************************************************************/
15889c134a14SGreg Kroah-Hartman static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
15899c134a14SGreg Kroah-Hartman 					  int baudRate, int *divisor,
15903f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
15913f542974SPaul B Schroeder {
15929c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
15933f542974SPaul B Schroeder 
15943f542974SPaul B Schroeder 	if (baudRate <= 115200) {
15953f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
15963f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
15973f542974SPaul B Schroeder 	}
15983f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
15993f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
16003f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
16013f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
16023f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
16033f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
16043f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
16053f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
16063f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
16073f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
16083f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
16093f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
16103f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
16113f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
16123f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
16133f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
16143f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
16153f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
16163f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
16173f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
16183f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
16193f542974SPaul B Schroeder 	}
16203f542974SPaul B Schroeder 	return 0;
16213f542974SPaul B Schroeder 
16223f542974SPaul B Schroeder #ifdef NOTMCS7840
16233f542974SPaul B Schroeder 
16243f542974SPaul B Schroeder 	for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
16253f542974SPaul B Schroeder 		if (mos7840_divisor_table[i].BaudRate == baudrate) {
16263f542974SPaul B Schroeder 			*divisor = mos7840_divisor_table[i].Divisor;
16273f542974SPaul B Schroeder 			return 0;
16283f542974SPaul B Schroeder 		}
16293f542974SPaul B Schroeder 	}
16303f542974SPaul B Schroeder 
16313f542974SPaul B Schroeder 	/* After trying for all the standard baud rates    *
16323f542974SPaul B Schroeder 	 * Try calculating the divisor for this baud rate  */
16333f542974SPaul B Schroeder 
16343f542974SPaul B Schroeder 	if (baudrate > 75 && baudrate < 230400) {
16353f542974SPaul B Schroeder 		/* get the divisor */
16363f542974SPaul B Schroeder 		custom = (__u16) (230400L / baudrate);
16373f542974SPaul B Schroeder 
16383f542974SPaul B Schroeder 		/* Check for round off */
16393f542974SPaul B Schroeder 		round1 = (__u16) (2304000L / baudrate);
16403f542974SPaul B Schroeder 		round = (__u16) (round1 - (custom * 10));
1641880af9dbSAlan Cox 		if (round > 4)
16423f542974SPaul B Schroeder 			custom++;
16433f542974SPaul B Schroeder 		*divisor = custom;
16443f542974SPaul B Schroeder 
16459c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
16463f542974SPaul B Schroeder 		return 0;
16473f542974SPaul B Schroeder 	}
16483f542974SPaul B Schroeder 
16499c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
16503f542974SPaul B Schroeder 	return -1;
16513f542974SPaul B Schroeder #endif
16523f542974SPaul B Schroeder }
16533f542974SPaul B Schroeder 
16543f542974SPaul B Schroeder /*****************************************************************************
16553f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
16563f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
16573f542974SPaul B Schroeder  *	specified port.
16583f542974SPaul B Schroeder  *****************************************************************************/
16593f542974SPaul B Schroeder 
16603f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
16613f542974SPaul B Schroeder 					    int baudRate)
16623f542974SPaul B Schroeder {
16633f542974SPaul B Schroeder 	int divisor = 0;
16643f542974SPaul B Schroeder 	int status;
16653f542974SPaul B Schroeder 	__u16 Data;
16663f542974SPaul B Schroeder 	unsigned char number;
16673f542974SPaul B Schroeder 	__u16 clk_sel_val;
16683f542974SPaul B Schroeder 	struct usb_serial_port *port;
16693f542974SPaul B Schroeder 
16703f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16713f542974SPaul B Schroeder 		return -1;
16723f542974SPaul B Schroeder 
16739c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
16749c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
16753f542974SPaul B Schroeder 		return -1;
16763f542974SPaul B Schroeder 
16779c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(port->serial, __func__))
16783f542974SPaul B Schroeder 		return -1;
16793f542974SPaul B Schroeder 
16801143832eSGreg Kroah-Hartman 	number = mos7840_port->port->port_number;
16813f542974SPaul B Schroeder 
16821143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1683880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
16843f542974SPaul B Schroeder 	if (baudRate > 115200) {
16853f542974SPaul B Schroeder #ifdef HW_flow_control
1686880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1687880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
16883f542974SPaul B Schroeder 		Data = 0x2b;
16893f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1690880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1691880af9dbSAlan Cox 									Data);
16923f542974SPaul B Schroeder 		if (status < 0) {
16939c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
16943f542974SPaul B Schroeder 			return -1;
16953f542974SPaul B Schroeder 		}
16963f542974SPaul B Schroeder #endif
16973f542974SPaul B Schroeder 
16983f542974SPaul B Schroeder 	} else {
16993f542974SPaul B Schroeder #ifdef HW_flow_control
1700880af9dbSAlan Cox 		/* setting h/w flow control bit to 0 */
17013f542974SPaul B Schroeder 		Data = 0xb;
17023f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1703880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1704880af9dbSAlan Cox 									Data);
17053f542974SPaul B Schroeder 		if (status < 0) {
17069c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
17073f542974SPaul B Schroeder 			return -1;
17083f542974SPaul B Schroeder 		}
17093f542974SPaul B Schroeder #endif
17103f542974SPaul B Schroeder 
17113f542974SPaul B Schroeder 	}
17123f542974SPaul B Schroeder 
1713880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
17143f542974SPaul B Schroeder 		clk_sel_val = 0x0;
17153f542974SPaul B Schroeder 		Data = 0x0;
17169c134a14SGreg Kroah-Hartman 		status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
17173f542974SPaul B Schroeder 						   &clk_sel_val);
1718880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
17193f542974SPaul B Schroeder 								 &Data);
17203f542974SPaul B Schroeder 		if (status < 0) {
17219c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
17223f542974SPaul B Schroeder 			return -1;
17233f542974SPaul B Schroeder 		}
17243f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1725880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1726880af9dbSAlan Cox 								Data);
17273f542974SPaul B Schroeder 		if (status < 0) {
17289c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
17293f542974SPaul B Schroeder 			return -1;
17303f542974SPaul B Schroeder 		}
17313f542974SPaul B Schroeder 		/* Calculate the Divisor */
17323f542974SPaul B Schroeder 
17333f542974SPaul B Schroeder 		if (status) {
1734194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
17353f542974SPaul B Schroeder 			return status;
17363f542974SPaul B Schroeder 		}
17373f542974SPaul B Schroeder 		/* Enable access to divisor latch */
17383f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
17393f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
17403f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
17413f542974SPaul B Schroeder 
17423f542974SPaul B Schroeder 		/* Write the divisor */
17433f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
17449c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
17453f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
17463f542974SPaul B Schroeder 
17473f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
17489c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
17493f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
17503f542974SPaul B Schroeder 
17513f542974SPaul B Schroeder 		/* Disable access to divisor latch */
17523f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
17533f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
17543f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
17553f542974SPaul B Schroeder 
17563f542974SPaul B Schroeder 	}
17573f542974SPaul B Schroeder 	return status;
17583f542974SPaul B Schroeder }
17593f542974SPaul B Schroeder 
17603f542974SPaul B Schroeder /*****************************************************************************
17613f542974SPaul B Schroeder  * mos7840_change_port_settings
17623f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
17633f542974SPaul B Schroeder  *      the specified new settings.
17643f542974SPaul B Schroeder  *****************************************************************************/
17653f542974SPaul B Schroeder 
176695da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
176795da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
17683f542974SPaul B Schroeder {
17693f542974SPaul B Schroeder 	int baud;
17703f542974SPaul B Schroeder 	unsigned cflag;
17713f542974SPaul B Schroeder 	unsigned iflag;
17723f542974SPaul B Schroeder 	__u8 lData;
17733f542974SPaul B Schroeder 	__u8 lParity;
17743f542974SPaul B Schroeder 	__u8 lStop;
17753f542974SPaul B Schroeder 	int status;
17763f542974SPaul B Schroeder 	__u16 Data;
17773f542974SPaul B Schroeder 	struct usb_serial_port *port;
17783f542974SPaul B Schroeder 	struct usb_serial *serial;
17793f542974SPaul B Schroeder 
17803f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17813f542974SPaul B Schroeder 		return;
17823f542974SPaul B Schroeder 
17839c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
17843f542974SPaul B Schroeder 
17859c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
17863f542974SPaul B Schroeder 		return;
17873f542974SPaul B Schroeder 
17889c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(port->serial, __func__))
17893f542974SPaul B Schroeder 		return;
17903f542974SPaul B Schroeder 
17913f542974SPaul B Schroeder 	serial = port->serial;
17923f542974SPaul B Schroeder 
17933f542974SPaul B Schroeder 	if (!mos7840_port->open) {
17949c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
17953f542974SPaul B Schroeder 		return;
17963f542974SPaul B Schroeder 	}
17973f542974SPaul B Schroeder 
17983f542974SPaul B Schroeder 	lData = LCR_BITS_8;
17993f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
18003f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
18013f542974SPaul B Schroeder 
1802adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
1803adc8d746SAlan Cox 	iflag = tty->termios.c_iflag;
18043f542974SPaul B Schroeder 
18053f542974SPaul B Schroeder 	/* Change the number of bits */
18063f542974SPaul B Schroeder 	switch (cflag & CSIZE) {
18073f542974SPaul B Schroeder 	case CS5:
18083f542974SPaul B Schroeder 		lData = LCR_BITS_5;
18093f542974SPaul B Schroeder 		break;
18103f542974SPaul B Schroeder 
18113f542974SPaul B Schroeder 	case CS6:
18123f542974SPaul B Schroeder 		lData = LCR_BITS_6;
18133f542974SPaul B Schroeder 		break;
18143f542974SPaul B Schroeder 
18153f542974SPaul B Schroeder 	case CS7:
18163f542974SPaul B Schroeder 		lData = LCR_BITS_7;
18173f542974SPaul B Schroeder 		break;
181878692cc3SColin Leitner 
18193f542974SPaul B Schroeder 	default:
18203f542974SPaul B Schroeder 	case CS8:
18213f542974SPaul B Schroeder 		lData = LCR_BITS_8;
18223f542974SPaul B Schroeder 		break;
18233f542974SPaul B Schroeder 	}
182478692cc3SColin Leitner 
18253f542974SPaul B Schroeder 	/* Change the Parity bit */
18263f542974SPaul B Schroeder 	if (cflag & PARENB) {
18273f542974SPaul B Schroeder 		if (cflag & PARODD) {
18283f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
18299c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
18303f542974SPaul B Schroeder 		} else {
18313f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
18329c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
18333f542974SPaul B Schroeder 		}
18343f542974SPaul B Schroeder 
18353f542974SPaul B Schroeder 	} else {
18369c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
18373f542974SPaul B Schroeder 	}
18383f542974SPaul B Schroeder 
1839880af9dbSAlan Cox 	if (cflag & CMSPAR)
18403f542974SPaul B Schroeder 		lParity = lParity | 0x20;
18413f542974SPaul B Schroeder 
18423f542974SPaul B Schroeder 	/* Change the Stop bit */
18433f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
18443f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
18459c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
18463f542974SPaul B Schroeder 	} else {
18473f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
18489c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
18493f542974SPaul B Schroeder 	}
18503f542974SPaul B Schroeder 
18513f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
18523f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
18533f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
18543f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
18553f542974SPaul B Schroeder 
18569c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
18573f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
18583f542974SPaul B Schroeder 	/* Disable Interrupts */
18593f542974SPaul B Schroeder 	Data = 0x00;
18603f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
18613f542974SPaul B Schroeder 
18623f542974SPaul B Schroeder 	Data = 0x00;
18633f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
18643f542974SPaul B Schroeder 
18653f542974SPaul B Schroeder 	Data = 0xcf;
18663f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
18673f542974SPaul B Schroeder 
18683f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
18693f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
18703f542974SPaul B Schroeder 
18713f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
18723f542974SPaul B Schroeder 
18733f542974SPaul B Schroeder 	Data = 0x00b;
18743f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
18753f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
18763f542974SPaul B Schroeder 	Data = 0x00b;
18773f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
18783f542974SPaul B Schroeder 
18793f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
18803f542974SPaul B Schroeder 
18813f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
1882880af9dbSAlan Cox 	if (cflag & CBAUD)
18833f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
18843f542974SPaul B Schroeder 
1885880af9dbSAlan Cox 	if (cflag & CRTSCTS)
18863f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
1887880af9dbSAlan Cox 	else
18883f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
18893f542974SPaul B Schroeder 
18903f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
18913f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
18923f542974SPaul B Schroeder 
18933f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
18943f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
18953f542974SPaul B Schroeder 
18963f542974SPaul B Schroeder 	if (!baud) {
18973f542974SPaul B Schroeder 		/* pick a default, any default... */
18989c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "Picked default baud...\n");
18993f542974SPaul B Schroeder 		baud = 9600;
19003f542974SPaul B Schroeder 	}
19013f542974SPaul B Schroeder 
19029c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
19033f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
19043f542974SPaul B Schroeder 
19053f542974SPaul B Schroeder 	/* Enable Interrupts */
19063f542974SPaul B Schroeder 	Data = 0x0c;
19073f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
19083f542974SPaul B Schroeder 
190950de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
191050de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
19113f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
19123f542974SPaul B Schroeder 		if (status) {
19139c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
19143f542974SPaul B Schroeder 			    status);
191550de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
19163f542974SPaul B Schroeder 		}
19173f542974SPaul B Schroeder 	}
19189c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
19193f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
19203f542974SPaul B Schroeder }
19213f542974SPaul B Schroeder 
19223f542974SPaul B Schroeder /*****************************************************************************
19233f542974SPaul B Schroeder  * mos7840_set_termios
19243f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
19253f542974SPaul B Schroeder  *	the termios structure
19263f542974SPaul B Schroeder  *****************************************************************************/
19273f542974SPaul B Schroeder 
192895da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
192995da310eSAlan Cox 				struct usb_serial_port *port,
1930606d099cSAlan Cox 				struct ktermios *old_termios)
19313f542974SPaul B Schroeder {
19323f542974SPaul B Schroeder 	int status;
19333f542974SPaul B Schroeder 	unsigned int cflag;
19343f542974SPaul B Schroeder 	struct usb_serial *serial;
19353f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
19363363155bSGreg Kroah-Hartman 
19379c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
19383f542974SPaul B Schroeder 		return;
19393f542974SPaul B Schroeder 
19403f542974SPaul B Schroeder 	serial = port->serial;
19413f542974SPaul B Schroeder 
19429c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
19433f542974SPaul B Schroeder 		return;
19443f542974SPaul B Schroeder 
19453f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
19463f542974SPaul B Schroeder 
19473f542974SPaul B Schroeder 	if (mos7840_port == NULL)
19483f542974SPaul B Schroeder 		return;
19493f542974SPaul B Schroeder 
19503f542974SPaul B Schroeder 	if (!mos7840_port->open) {
19519c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
19523f542974SPaul B Schroeder 		return;
19533f542974SPaul B Schroeder 	}
19543f542974SPaul B Schroeder 
19559c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s", "setting termios - \n");
19563f542974SPaul B Schroeder 
1957adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
19583f542974SPaul B Schroeder 
19599c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
1960adc8d746SAlan Cox 		tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
19619c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
19623f542974SPaul B Schroeder 		old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
19633f542974SPaul B Schroeder 
19643f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
19653f542974SPaul B Schroeder 
196695da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
19673f542974SPaul B Schroeder 
19683f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
19699c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
19703f542974SPaul B Schroeder 		return;
19713f542974SPaul B Schroeder 	}
19723f542974SPaul B Schroeder 
197350de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
197450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
19753f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
19763f542974SPaul B Schroeder 		if (status) {
19779c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
19783f542974SPaul B Schroeder 			    status);
197950de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
19803f542974SPaul B Schroeder 		}
19813f542974SPaul B Schroeder 	}
19823f542974SPaul B Schroeder }
19833f542974SPaul B Schroeder 
19843f542974SPaul B Schroeder /*****************************************************************************
19853f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
19863f542974SPaul B Schroeder  *
19873f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
19883f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
19893f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
19903f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
19913f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
19923f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
19933f542974SPaul B Schroeder  *****************************************************************************/
19943f542974SPaul B Schroeder 
199595da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
199697c4965dSAl Viro 				unsigned int __user *value)
19973f542974SPaul B Schroeder {
19983f542974SPaul B Schroeder 	int count;
19993f542974SPaul B Schroeder 	unsigned int result = 0;
20003f542974SPaul B Schroeder 
200195da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
20029c134a14SGreg Kroah-Hartman 	if (count == 0)
20033f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
20043f542974SPaul B Schroeder 
20053f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
20063f542974SPaul B Schroeder 		return -EFAULT;
20073f542974SPaul B Schroeder 	return 0;
20083f542974SPaul B Schroeder }
20093f542974SPaul B Schroeder 
20103f542974SPaul B Schroeder /*****************************************************************************
20113f542974SPaul B Schroeder  * mos7840_get_serial_info
20123f542974SPaul B Schroeder  *      function to get information about serial port
20133f542974SPaul B Schroeder  *****************************************************************************/
20143f542974SPaul B Schroeder 
20153f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
201697c4965dSAl Viro 				   struct serial_struct __user *retinfo)
20173f542974SPaul B Schroeder {
20183f542974SPaul B Schroeder 	struct serial_struct tmp;
20193f542974SPaul B Schroeder 
20203f542974SPaul B Schroeder 	if (mos7840_port == NULL)
20213f542974SPaul B Schroeder 		return -1;
20223f542974SPaul B Schroeder 
20233f542974SPaul B Schroeder 	if (!retinfo)
20243f542974SPaul B Schroeder 		return -EFAULT;
20253f542974SPaul B Schroeder 
20263f542974SPaul B Schroeder 	memset(&tmp, 0, sizeof(tmp));
20273f542974SPaul B Schroeder 
20283f542974SPaul B Schroeder 	tmp.type = PORT_16550A;
2029e5b1e206SGreg Kroah-Hartman 	tmp.line = mos7840_port->port->minor;
20301143832eSGreg Kroah-Hartman 	tmp.port = mos7840_port->port->port_number;
20313f542974SPaul B Schroeder 	tmp.irq = 0;
20323f542974SPaul B Schroeder 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
20333f542974SPaul B Schroeder 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
20343f542974SPaul B Schroeder 	tmp.baud_base = 9600;
20353f542974SPaul B Schroeder 	tmp.close_delay = 5 * HZ;
20363f542974SPaul B Schroeder 	tmp.closing_wait = 30 * HZ;
20373f542974SPaul B Schroeder 
20383f542974SPaul B Schroeder 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
20393f542974SPaul B Schroeder 		return -EFAULT;
20403f542974SPaul B Schroeder 	return 0;
20413f542974SPaul B Schroeder }
20423f542974SPaul B Schroeder 
20433f542974SPaul B Schroeder /*****************************************************************************
20443f542974SPaul B Schroeder  * SerialIoctl
20453f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
20463f542974SPaul B Schroeder  *****************************************************************************/
20473f542974SPaul B Schroeder 
204800a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
20493f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
20503f542974SPaul B Schroeder {
205195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
205297c4965dSAl Viro 	void __user *argp = (void __user *)arg;
20533f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
20543f542974SPaul B Schroeder 
20559c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
20563f542974SPaul B Schroeder 		return -1;
20573f542974SPaul B Schroeder 
20583f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
20593f542974SPaul B Schroeder 
20603f542974SPaul B Schroeder 	if (mos7840_port == NULL)
20613f542974SPaul B Schroeder 		return -1;
20623f542974SPaul B Schroeder 
20633f542974SPaul B Schroeder 	switch (cmd) {
20643f542974SPaul B Schroeder 		/* return number of bytes available */
20653f542974SPaul B Schroeder 
20663f542974SPaul B Schroeder 	case TIOCSERGETLSR:
20679c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
206895da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
20693f542974SPaul B Schroeder 
20703f542974SPaul B Schroeder 	case TIOCGSERIAL:
20719c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
207297c4965dSAl Viro 		return mos7840_get_serial_info(mos7840_port, argp);
20733f542974SPaul B Schroeder 
20743f542974SPaul B Schroeder 	case TIOCSSERIAL:
20759c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
20763f542974SPaul B Schroeder 		break;
20773f542974SPaul B Schroeder 	default:
20783f542974SPaul B Schroeder 		break;
20793f542974SPaul B Schroeder 	}
20803f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
20813f542974SPaul B Schroeder }
20823f542974SPaul B Schroeder 
20830eafe4deSDonald static int mos7810_check(struct usb_serial *serial)
20843f542974SPaul B Schroeder {
20850eafe4deSDonald 	int i, pass_count = 0;
208615ee89c3SJohan Hovold 	u8 *buf;
20870eafe4deSDonald 	__u16 data = 0, mcr_data = 0;
20880eafe4deSDonald 	__u16 test_pattern = 0x55AA;
208915ee89c3SJohan Hovold 	int res;
209015ee89c3SJohan Hovold 
209115ee89c3SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
209215ee89c3SJohan Hovold 	if (!buf)
209315ee89c3SJohan Hovold 		return 0;	/* failed to identify 7810 */
20943f542974SPaul B Schroeder 
20950eafe4deSDonald 	/* Store MCR setting */
209615ee89c3SJohan Hovold 	res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
20970eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
209815ee89c3SJohan Hovold 		buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
209915ee89c3SJohan Hovold 	if (res == VENDOR_READ_LENGTH)
210015ee89c3SJohan Hovold 		mcr_data = *buf;
21010eafe4deSDonald 
21020eafe4deSDonald 	for (i = 0; i < 16; i++) {
21030eafe4deSDonald 		/* Send the 1-bit test pattern out to MCS7810 test pin */
21040eafe4deSDonald 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
21050eafe4deSDonald 			MCS_WRREQ, MCS_WR_RTYPE,
21060eafe4deSDonald 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
21070eafe4deSDonald 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
21080eafe4deSDonald 
21090eafe4deSDonald 		/* Read the test pattern back */
211015ee89c3SJohan Hovold 		res = usb_control_msg(serial->dev,
211115ee89c3SJohan Hovold 				usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
211215ee89c3SJohan Hovold 				MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2113093ea2d3SDonald Lee 				VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
211415ee89c3SJohan Hovold 		if (res == VENDOR_READ_LENGTH)
211515ee89c3SJohan Hovold 			data = *buf;
2116093ea2d3SDonald Lee 
21170eafe4deSDonald 		/* If this is a MCS7810 device, both test patterns must match */
21180eafe4deSDonald 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
21190eafe4deSDonald 			break;
21200eafe4deSDonald 
21210eafe4deSDonald 		pass_count++;
21223f542974SPaul B Schroeder 	}
2123093ea2d3SDonald Lee 
21240eafe4deSDonald 	/* Restore MCR setting */
21250eafe4deSDonald 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
21260eafe4deSDonald 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
21270eafe4deSDonald 		0, MOS_WDR_TIMEOUT);
21280eafe4deSDonald 
212915ee89c3SJohan Hovold 	kfree(buf);
213015ee89c3SJohan Hovold 
21310eafe4deSDonald 	if (pass_count == 16)
21320eafe4deSDonald 		return 1;
21330eafe4deSDonald 
21340eafe4deSDonald 	return 0;
21350eafe4deSDonald }
21360eafe4deSDonald 
213740c24f28SJohan Hovold static int mos7840_probe(struct usb_serial *serial,
213840c24f28SJohan Hovold 				const struct usb_device_id *id)
21390eafe4deSDonald {
2140d551ec9bSJohan Hovold 	u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
214115ee89c3SJohan Hovold 	u8 *buf;
214240c24f28SJohan Hovold 	int device_type;
214340c24f28SJohan Hovold 
214440c24f28SJohan Hovold 	if (product == MOSCHIP_DEVICE_ID_7810 ||
214540c24f28SJohan Hovold 		product == MOSCHIP_DEVICE_ID_7820) {
214640c24f28SJohan Hovold 		device_type = product;
214740c24f28SJohan Hovold 		goto out;
214840c24f28SJohan Hovold 	}
21490eafe4deSDonald 
215015ee89c3SJohan Hovold 	buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
215140c24f28SJohan Hovold 	if (!buf)
215240c24f28SJohan Hovold 		return -ENOMEM;
215340c24f28SJohan Hovold 
21540eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
215515ee89c3SJohan Hovold 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
21560eafe4deSDonald 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
21570eafe4deSDonald 
21580eafe4deSDonald 	/* For a MCS7840 device GPIO0 must be set to 1 */
215940c24f28SJohan Hovold 	if (buf[0] & 0x01)
21600eafe4deSDonald 		device_type = MOSCHIP_DEVICE_ID_7840;
21610eafe4deSDonald 	else if (mos7810_check(serial))
21620eafe4deSDonald 		device_type = MOSCHIP_DEVICE_ID_7810;
21630eafe4deSDonald 	else
21640eafe4deSDonald 		device_type = MOSCHIP_DEVICE_ID_7820;
216540c24f28SJohan Hovold 
216640c24f28SJohan Hovold 	kfree(buf);
216740c24f28SJohan Hovold out:
2168683a0e4dSJohan Hovold 	usb_set_serial_data(serial, (void *)(unsigned long)device_type);
216940c24f28SJohan Hovold 
217040c24f28SJohan Hovold 	return 0;
21710eafe4deSDonald }
21720eafe4deSDonald 
217340c24f28SJohan Hovold static int mos7840_calc_num_ports(struct usb_serial *serial)
217440c24f28SJohan Hovold {
2175683a0e4dSJohan Hovold 	int device_type = (unsigned long)usb_get_serial_data(serial);
217640c24f28SJohan Hovold 	int mos7840_num_ports;
217740c24f28SJohan Hovold 
21780eafe4deSDonald 	mos7840_num_ports = (device_type >> 4) & 0x000F;
21790eafe4deSDonald 
21803f542974SPaul B Schroeder 	return mos7840_num_ports;
21813f542974SPaul B Schroeder }
21823f542974SPaul B Schroeder 
218380c00750SJohan Hovold static int mos7840_port_probe(struct usb_serial_port *port)
21843f542974SPaul B Schroeder {
218580c00750SJohan Hovold 	struct usb_serial *serial = port->serial;
2186683a0e4dSJohan Hovold 	int device_type = (unsigned long)usb_get_serial_data(serial);
21873f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
218880c00750SJohan Hovold 	int status;
218980c00750SJohan Hovold 	int pnum;
21903f542974SPaul B Schroeder 	__u16 Data;
21913f542974SPaul B Schroeder 
21923f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
21933f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
21943f542974SPaul B Schroeder 
21951143832eSGreg Kroah-Hartman 	pnum = port->port_number;
219680c00750SJohan Hovold 
2197ae685effSJohan Hovold 	dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
21987ac9da10SBurman Yan 	mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2199*10c642d0SJohan Hovold 	if (!mos7840_port)
220080c00750SJohan Hovold 		return -ENOMEM;
22013f542974SPaul B Schroeder 
2202880af9dbSAlan Cox 	/* Initialize all port interrupt end point to port 0 int
2203880af9dbSAlan Cox 	 * endpoint. Our device has only one interrupt end point
2204880af9dbSAlan Cox 	 * common to all port */
22053f542974SPaul B Schroeder 
220680c00750SJohan Hovold 	mos7840_port->port = port;
220780c00750SJohan Hovold 	mos7840_set_port_private(port, mos7840_port);
22080de9a702SOliver Neukum 	spin_lock_init(&mos7840_port->pool_lock);
22093f542974SPaul B Schroeder 
221037768adfSTony Cook 	/* minor is not initialised until later by
221137768adfSTony Cook 	 * usb-serial.c:get_free_serial() and cannot therefore be used
221237768adfSTony Cook 	 * to index device instances */
221380c00750SJohan Hovold 	mos7840_port->port_num = pnum + 1;
2214e5b1e206SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
221580c00750SJohan Hovold 	dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
22163f542974SPaul B Schroeder 
22173f542974SPaul B Schroeder 	if (mos7840_port->port_num == 1) {
22183f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0x0;
22193f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0x1;
22203f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x4;
2221ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
22223f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0x8;
22233f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0x9;
22243f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x16;
2225ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
22263f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0xa;
22273f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0xb;
22283f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x19;
2229ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
22303f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0xa;
22313f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0xb;
22323f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x19;
2233ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
22343f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0xc;
22353f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0xd;
22363f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x1c;
22373f542974SPaul B Schroeder 	}
223880c00750SJohan Hovold 	mos7840_dump_serial_port(port, mos7840_port);
223980c00750SJohan Hovold 	mos7840_set_port_private(port, mos7840_port);
22403f542974SPaul B Schroeder 
2241880af9dbSAlan Cox 	/* enable rx_disable bit in control register */
224280c00750SJohan Hovold 	status = mos7840_get_reg_sync(port,
22433f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, &Data);
22443f542974SPaul B Schroeder 	if (status < 0) {
224580c00750SJohan Hovold 		dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2246ae685effSJohan Hovold 		goto out;
22473f542974SPaul B Schroeder 	} else
224880c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2249880af9dbSAlan Cox 	Data |= 0x08;	/* setting driver done bit */
2250880af9dbSAlan Cox 	Data |= 0x04;	/* sp1_bit to have cts change reflect in
2251880af9dbSAlan Cox 			   modem status reg */
22523f542974SPaul B Schroeder 
2253880af9dbSAlan Cox 	/* Data |= 0x20; //rx_disable bit */
225480c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
22553f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, Data);
22563f542974SPaul B Schroeder 	if (status < 0) {
225780c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2258ae685effSJohan Hovold 		goto out;
22593f542974SPaul B Schroeder 	} else
226080c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
22613f542974SPaul B Schroeder 
2262880af9dbSAlan Cox 	/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2263880af9dbSAlan Cox 	   and 0x24 in DCR3 */
22643f542974SPaul B Schroeder 	Data = 0x01;
226580c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
2266880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 0), Data);
22673f542974SPaul B Schroeder 	if (status < 0) {
226880c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2269ae685effSJohan Hovold 		goto out;
22703f542974SPaul B Schroeder 	} else
227180c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
22723f542974SPaul B Schroeder 
22733f542974SPaul B Schroeder 	Data = 0x05;
227480c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
2275880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 1), Data);
22763f542974SPaul B Schroeder 	if (status < 0) {
227780c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2278ae685effSJohan Hovold 		goto out;
22793f542974SPaul B Schroeder 	} else
228080c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
22813f542974SPaul B Schroeder 
22823f542974SPaul B Schroeder 	Data = 0x24;
228380c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
2284880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 2), Data);
22853f542974SPaul B Schroeder 	if (status < 0) {
228680c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2287ae685effSJohan Hovold 		goto out;
22883f542974SPaul B Schroeder 	} else
228980c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
22903f542974SPaul B Schroeder 
2291880af9dbSAlan Cox 	/* write values in clkstart0x0 and clkmulti 0x20 */
22923f542974SPaul B Schroeder 	Data = 0x0;
2293ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
22943f542974SPaul B Schroeder 	if (status < 0) {
229580c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2296ae685effSJohan Hovold 		goto out;
22973f542974SPaul B Schroeder 	} else
229880c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
22993f542974SPaul B Schroeder 
23003f542974SPaul B Schroeder 	Data = 0x20;
2301ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
23023f542974SPaul B Schroeder 	if (status < 0) {
230380c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
23040de9a702SOliver Neukum 		goto error;
23053f542974SPaul B Schroeder 	} else
230680c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
23073f542974SPaul B Schroeder 
2308880af9dbSAlan Cox 	/* write value 0x0 to scratchpad register */
23093f542974SPaul B Schroeder 	Data = 0x00;
2310ae685effSJohan Hovold 	status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
23113f542974SPaul B Schroeder 	if (status < 0) {
231280c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2313ae685effSJohan Hovold 		goto out;
23143f542974SPaul B Schroeder 	} else
231580c00750SJohan Hovold 		dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
23163f542974SPaul B Schroeder 
2317880af9dbSAlan Cox 	/* Zero Length flag register */
2318ae685effSJohan Hovold 	if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
23193f542974SPaul B Schroeder 		Data = 0xff;
232080c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
23213f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
2322880af9dbSAlan Cox 					((__u16)mos7840_port->port_num)), Data);
232380c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
23249c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
23253f542974SPaul B Schroeder 		if (status < 0) {
232680c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2327ae685effSJohan Hovold 			goto out;
23283f542974SPaul B Schroeder 		} else
232980c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
23303f542974SPaul B Schroeder 	} else {
23313f542974SPaul B Schroeder 		Data = 0xff;
233280c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
23333f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
2334880af9dbSAlan Cox 					((__u16)mos7840_port->port_num) - 0x1), Data);
233580c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
23369c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
23373f542974SPaul B Schroeder 		if (status < 0) {
233880c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2339ae685effSJohan Hovold 			goto out;
23403f542974SPaul B Schroeder 		} else
234180c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
23423f542974SPaul B Schroeder 
23433f542974SPaul B Schroeder 	}
23440de9a702SOliver Neukum 	mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
23453f542974SPaul B Schroeder 	mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2346880af9dbSAlan Cox 	mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2347880af9dbSAlan Cox 			GFP_KERNEL);
2348880af9dbSAlan Cox 	if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2349880af9dbSAlan Cox 			!mos7840_port->dr) {
23500de9a702SOliver Neukum 		status = -ENOMEM;
23510de9a702SOliver Neukum 		goto error;
23520de9a702SOliver Neukum 	}
23530eafe4deSDonald 
23540eafe4deSDonald 	mos7840_port->has_led = false;
23550eafe4deSDonald 
23560eafe4deSDonald 	/* Initialize LED timers */
23570eafe4deSDonald 	if (device_type == MOSCHIP_DEVICE_ID_7810) {
23580eafe4deSDonald 		mos7840_port->has_led = true;
23590eafe4deSDonald 
236005cf0decSJohan Hovold 		mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
236105cf0decSJohan Hovold 		mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
236205cf0decSJohan Hovold 								GFP_KERNEL);
236305cf0decSJohan Hovold 		if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
236405cf0decSJohan Hovold 			status = -ENOMEM;
236505cf0decSJohan Hovold 			goto error;
236605cf0decSJohan Hovold 		}
236705cf0decSJohan Hovold 
23680eafe4deSDonald 		init_timer(&mos7840_port->led_timer1);
23690eafe4deSDonald 		mos7840_port->led_timer1.function = mos7840_led_off;
23700eafe4deSDonald 		mos7840_port->led_timer1.expires =
23710eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_ON_MS);
2372ae685effSJohan Hovold 		mos7840_port->led_timer1.data = (unsigned long)mos7840_port;
23730eafe4deSDonald 
23740eafe4deSDonald 		init_timer(&mos7840_port->led_timer2);
2375ae685effSJohan Hovold 		mos7840_port->led_timer2.function = mos7840_led_flag_off;
23760eafe4deSDonald 		mos7840_port->led_timer2.expires =
23770eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_OFF_MS);
2378ae685effSJohan Hovold 		mos7840_port->led_timer2.data = (unsigned long)mos7840_port;
23790eafe4deSDonald 
23800eafe4deSDonald 		/* Turn off LED */
2381ae685effSJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
23820eafe4deSDonald 	}
2383ae685effSJohan Hovold out:
238480c00750SJohan Hovold 	if (pnum == serial->num_ports - 1) {
2385880af9dbSAlan Cox 		/* Zero Length flag enable */
23863f542974SPaul B Schroeder 		Data = 0x0f;
23873f542974SPaul B Schroeder 		status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
23883f542974SPaul B Schroeder 		if (status < 0) {
238980c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
23907ced46c3SRoel Kluin 			goto error;
23913f542974SPaul B Schroeder 		} else
239280c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
23933f542974SPaul B Schroeder 
23943f542974SPaul B Schroeder 		/* setting configuration feature to one */
23953f542974SPaul B Schroeder 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
239680c00750SJohan Hovold 				0x03, 0x00, 0x01, 0x00, NULL, 0x00,
239780c00750SJohan Hovold 				MOS_WDR_TIMEOUT);
239880c00750SJohan Hovold 	}
23993f542974SPaul B Schroeder 	return 0;
24000de9a702SOliver Neukum error:
240105cf0decSJohan Hovold 	kfree(mos7840_port->led_dr);
240205cf0decSJohan Hovold 	usb_free_urb(mos7840_port->led_urb);
24030de9a702SOliver Neukum 	kfree(mos7840_port->dr);
24040de9a702SOliver Neukum 	kfree(mos7840_port->ctrl_buf);
24050de9a702SOliver Neukum 	usb_free_urb(mos7840_port->control_urb);
24060de9a702SOliver Neukum 	kfree(mos7840_port);
240780c00750SJohan Hovold 
24080de9a702SOliver Neukum 	return status;
24093f542974SPaul B Schroeder }
24103f542974SPaul B Schroeder 
241180c00750SJohan Hovold static int mos7840_port_remove(struct usb_serial_port *port)
24123f542974SPaul B Schroeder {
24133f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
24143f542974SPaul B Schroeder 
241580c00750SJohan Hovold 	mos7840_port = mos7840_get_port_private(port);
24163f542974SPaul B Schroeder 
24170eafe4deSDonald 	if (mos7840_port->has_led) {
24180eafe4deSDonald 		/* Turn off LED */
241980c00750SJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
24200eafe4deSDonald 
24210eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer1);
24220eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer2);
242305cf0decSJohan Hovold 
242405cf0decSJohan Hovold 		usb_kill_urb(mos7840_port->led_urb);
242505cf0decSJohan Hovold 		usb_free_urb(mos7840_port->led_urb);
242605cf0decSJohan Hovold 		kfree(mos7840_port->led_dr);
24270eafe4deSDonald 	}
242880c00750SJohan Hovold 	usb_kill_urb(mos7840_port->control_urb);
242965a4cdbbSJohan Hovold 	usb_free_urb(mos7840_port->control_urb);
24300de9a702SOliver Neukum 	kfree(mos7840_port->ctrl_buf);
24310de9a702SOliver Neukum 	kfree(mos7840_port->dr);
24323f542974SPaul B Schroeder 	kfree(mos7840_port);
243380c00750SJohan Hovold 
243480c00750SJohan Hovold 	return 0;
24353f542974SPaul B Schroeder }
24363f542974SPaul B Schroeder 
24373f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
24383f542974SPaul B Schroeder 	.driver = {
24393f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
24403f542974SPaul B Schroeder 		   .name = "mos7840",
24413f542974SPaul B Schroeder 		   },
24423f542974SPaul B Schroeder 	.description = DRIVER_DESC,
244368e24113SGreg Kroah-Hartman 	.id_table = id_table,
24443f542974SPaul B Schroeder 	.num_ports = 4,
24453f542974SPaul B Schroeder 	.open = mos7840_open,
24463f542974SPaul B Schroeder 	.close = mos7840_close,
24473f542974SPaul B Schroeder 	.write = mos7840_write,
24483f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
24493f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
24503f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
24513f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
24523f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
245340c24f28SJohan Hovold 	.probe = mos7840_probe,
24543f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
24553f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
24563f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
24573f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
24583f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
24590c613371SJohan Hovold 	.tiocmiwait = usb_serial_generic_tiocmiwait,
24608c1a07ffSJohan Hovold 	.get_icount = usb_serial_generic_get_icount,
246180c00750SJohan Hovold 	.port_probe = mos7840_port_probe,
246280c00750SJohan Hovold 	.port_remove = mos7840_port_remove,
24633f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
24643f542974SPaul B Schroeder 	.read_int_callback = mos7840_interrupt_callback,
24653f542974SPaul B Schroeder };
24663f542974SPaul B Schroeder 
24674d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
24684d2a7affSAlan Stern 	&moschip7840_4port_device, NULL
24694d2a7affSAlan Stern };
24704d2a7affSAlan Stern 
247168e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
24723f542974SPaul B Schroeder 
24733f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
24743f542974SPaul B Schroeder MODULE_LICENSE("GPL");
2475