xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision cd8db057e93ddaacbec025b567490555d2bca280)
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/slab.h>
283f542974SPaul B Schroeder #include <linux/tty.h>
293f542974SPaul B Schroeder #include <linux/tty_driver.h>
303f542974SPaul B Schroeder #include <linux/tty_flip.h>
313f542974SPaul B Schroeder #include <linux/module.h>
323f542974SPaul B Schroeder #include <linux/serial.h>
333f542974SPaul B Schroeder #include <linux/usb.h>
343f542974SPaul B Schroeder #include <linux/usb/serial.h>
35880af9dbSAlan Cox #include <linux/uaccess.h>
363f542974SPaul B Schroeder 
373f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
383f542974SPaul B Schroeder 
393f542974SPaul B Schroeder /*
403f542974SPaul B Schroeder  * 16C50 UART register defines
413f542974SPaul B Schroeder  */
423f542974SPaul B Schroeder 
433f542974SPaul B Schroeder #define LCR_BITS_5             0x00	/* 5 bits/char */
443f542974SPaul B Schroeder #define LCR_BITS_6             0x01	/* 6 bits/char */
453f542974SPaul B Schroeder #define LCR_BITS_7             0x02	/* 7 bits/char */
463f542974SPaul B Schroeder #define LCR_BITS_8             0x03	/* 8 bits/char */
473f542974SPaul B Schroeder #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
483f542974SPaul B Schroeder 
493f542974SPaul B Schroeder #define LCR_STOP_1             0x00	/* 1 stop bit */
503f542974SPaul B Schroeder #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
513f542974SPaul B Schroeder #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
523f542974SPaul B Schroeder #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
533f542974SPaul B Schroeder 
543f542974SPaul B Schroeder #define LCR_PAR_NONE           0x00	/* No parity */
553f542974SPaul B Schroeder #define LCR_PAR_ODD            0x08	/* Odd parity */
563f542974SPaul B Schroeder #define LCR_PAR_EVEN           0x18	/* Even parity */
573f542974SPaul B Schroeder #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
583f542974SPaul B Schroeder #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
593f542974SPaul B Schroeder #define LCR_PAR_MASK           0x38	/* Mask for parity field */
603f542974SPaul B Schroeder 
613f542974SPaul B Schroeder #define LCR_SET_BREAK          0x40	/* Set Break condition */
623f542974SPaul B Schroeder #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
633f542974SPaul B Schroeder 
643f542974SPaul B Schroeder #define MCR_DTR                0x01	/* Assert DTR */
653f542974SPaul B Schroeder #define MCR_RTS                0x02	/* Assert RTS */
663f542974SPaul B Schroeder #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
673f542974SPaul B Schroeder #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
683f542974SPaul B Schroeder #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
693f542974SPaul B Schroeder #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
703f542974SPaul B Schroeder 
713f542974SPaul B Schroeder #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
723f542974SPaul B Schroeder #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
733f542974SPaul B Schroeder #define MOS7840_MSR_RI         0x40	/* Current state of RI */
743f542974SPaul B Schroeder #define MOS7840_MSR_CD         0x80	/* Current state of CD */
753f542974SPaul B Schroeder 
763f542974SPaul B Schroeder /*
773f542974SPaul B Schroeder  * Defines used for sending commands to port
783f542974SPaul B Schroeder  */
793f542974SPaul B Schroeder 
801e658489SMark Ferrell #define MOS_WDR_TIMEOUT		5000	/* default urb timeout */
813f542974SPaul B Schroeder 
823f542974SPaul B Schroeder #define MOS_PORT1       0x0200
833f542974SPaul B Schroeder #define MOS_PORT2       0x0300
843f542974SPaul B Schroeder #define MOS_VENREG      0x0000
853f542974SPaul B Schroeder #define MOS_MAX_PORT	0x02
863f542974SPaul B Schroeder #define MOS_WRITE       0x0E
873f542974SPaul B Schroeder #define MOS_READ        0x0D
883f542974SPaul B Schroeder 
893f542974SPaul B Schroeder /* Requests */
903f542974SPaul B Schroeder #define MCS_RD_RTYPE    0xC0
913f542974SPaul B Schroeder #define MCS_WR_RTYPE    0x40
923f542974SPaul B Schroeder #define MCS_RDREQ       0x0D
933f542974SPaul B Schroeder #define MCS_WRREQ       0x0E
943f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT        500
953f542974SPaul B Schroeder #define VENDOR_READ_LENGTH      (0x01)
963f542974SPaul B Schroeder 
973f542974SPaul B Schroeder #define MAX_NAME_LEN    64
983f542974SPaul B Schroeder 
99880af9dbSAlan Cox #define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
100880af9dbSAlan Cox #define ZLP_REG5  0x3E		/* Zero_Flag_Reg5    62 */
1013f542974SPaul B Schroeder 
1023f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */
1033f542974SPaul B Schroeder #define TIOCEXBAUD     0x5462
1043f542974SPaul B Schroeder 
1053f542974SPaul B Schroeder /* vendor id and device id defines */
1063f542974SPaul B Schroeder 
10711e1abb4SDavid Ludlow /* The native mos7840/7820 component */
1083f542974SPaul B Schroeder #define USB_VENDOR_ID_MOSCHIP           0x9710
1093f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7840          0x7840
1103f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7820          0x7820
1110eafe4deSDonald #define MOSCHIP_DEVICE_ID_7810          0x7810
11211e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden
11311e1abb4SDavid Ludlow  * in vendor-specific implementations.  Such devices can be handled
11468e24113SGreg Kroah-Hartman  * by making a change here, in id_table.
11511e1abb4SDavid Ludlow  */
11611e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB              0x0856
117acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
118870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
119acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
120870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
121acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
122acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
123acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
124acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
12511e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
126caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
127870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
128870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
129870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL2_4        0xAC24
1303f542974SPaul B Schroeder 
1319d498beaSRussell Lang /* This driver also supports
1329d498beaSRussell Lang  * ATEN UC2324 device using Moschip MCS7840
1339d498beaSRussell Lang  * ATEN UC2322 device using Moschip MCS7820
1349d498beaSRussell Lang  */
135e9b8cffaSTony Cook #define USB_VENDOR_ID_ATENINTL		0x0557
136e9b8cffaSTony Cook #define ATENINTL_DEVICE_ID_UC2324	0x2011
1379d498beaSRussell Lang #define ATENINTL_DEVICE_ID_UC2322	0x7820
138e9b8cffaSTony Cook 
13911e1abb4SDavid Ludlow /* Interrupt Routine Defines    */
1403f542974SPaul B Schroeder 
1413f542974SPaul B Schroeder #define SERIAL_IIR_RLS      0x06
1423f542974SPaul B Schroeder #define SERIAL_IIR_MS       0x00
1433f542974SPaul B Schroeder 
1443f542974SPaul B Schroeder /*
1453f542974SPaul B Schroeder  *  Emulation of the bit mask on the LINE STATUS REGISTER.
1463f542974SPaul B Schroeder  */
1473f542974SPaul B Schroeder #define SERIAL_LSR_DR       0x0001
1483f542974SPaul B Schroeder #define SERIAL_LSR_OE       0x0002
1493f542974SPaul B Schroeder #define SERIAL_LSR_PE       0x0004
1503f542974SPaul B Schroeder #define SERIAL_LSR_FE       0x0008
1513f542974SPaul B Schroeder #define SERIAL_LSR_BI       0x0010
1523f542974SPaul B Schroeder 
1533f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS   0x10
1543f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR   0x20
1553f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI    0x40
1563f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD    0x80
1573f542974SPaul B Schroeder 
158880af9dbSAlan Cox /* Serial Port register Address */
1593f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
1603f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
1613f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
1623f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
1633f542974SPaul B Schroeder #define LINE_STATUS_REGISTER       ((__u16)(0x05))
1643f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
1653f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
1663f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
1673f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
1683f542974SPaul B Schroeder 
1693f542974SPaul B Schroeder #define CLK_MULTI_REGISTER         ((__u16)(0x02))
1703f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
171093ea2d3SDonald Lee #define GPIO_REGISTER              ((__u16)(0x07))
1723f542974SPaul B Schroeder 
1733f542974SPaul B Schroeder #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
1743f542974SPaul B Schroeder 
1753f542974SPaul B Schroeder /*
1763f542974SPaul B Schroeder  * URB POOL related defines
1773f542974SPaul B Schroeder  */
1783f542974SPaul B Schroeder #define NUM_URBS                        16	/* URB Count */
1793f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
1803f542974SPaul B Schroeder 
1810eafe4deSDonald /* LED on/off milliseconds*/
1820eafe4deSDonald #define LED_ON_MS	500
1830eafe4deSDonald #define LED_OFF_MS	500
1840eafe4deSDonald 
185d8a083ccSJohan Hovold enum mos7840_flag {
186d8a083ccSJohan Hovold 	MOS7840_FLAG_CTRL_BUSY,
18705cf0decSJohan Hovold 	MOS7840_FLAG_LED_BUSY,
188d8a083ccSJohan Hovold };
1893f542974SPaul B Schroeder 
190b9c87663STony Zelenoff static const struct usb_device_id id_table[] = {
1913f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
1923f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
1930eafe4deSDonald 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
194acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
195870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
196acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
197870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
198acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
199acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
200acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
201acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
20211e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
203870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
204acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
205870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
20627f1281dSBlaise Gassend 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
207e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
2089d498beaSRussell Lang 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
2093f542974SPaul B Schroeder 	{}			/* terminating entry */
2103f542974SPaul B Schroeder };
21168e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
2123f542974SPaul B Schroeder 
2133f542974SPaul B Schroeder /* This structure holds all of the local port information */
2143f542974SPaul B Schroeder 
2153f542974SPaul B Schroeder struct moschip_port {
2163f542974SPaul B Schroeder 	int port_num;		/*Actual port number in the device(1,2,etc) */
2173f542974SPaul B Schroeder 	struct urb *read_urb;	/* read URB for this port */
2183f542974SPaul B Schroeder 	__u8 shadowLCR;		/* last LCR value received */
2193f542974SPaul B Schroeder 	__u8 shadowMCR;		/* last MCR value received */
2203f542974SPaul B Schroeder 	char open;
2210de9a702SOliver Neukum 	char open_ports;
2223f542974SPaul B Schroeder 	struct usb_serial_port *port;	/* loop back to the owner of this object */
2233f542974SPaul B Schroeder 
2243f542974SPaul B Schroeder 	/* Offsets */
2253f542974SPaul B Schroeder 	__u8 SpRegOffset;
2263f542974SPaul B Schroeder 	__u8 ControlRegOffset;
2273f542974SPaul B Schroeder 	__u8 DcrRegOffset;
228880af9dbSAlan Cox 	/* for processing control URBS in interrupt context */
2293f542974SPaul B Schroeder 	struct urb *control_urb;
2300de9a702SOliver Neukum 	struct usb_ctrlrequest *dr;
2313f542974SPaul B Schroeder 	char *ctrl_buf;
2323f542974SPaul B Schroeder 	int MsrLsr;
2333f542974SPaul B Schroeder 
2340de9a702SOliver Neukum 	spinlock_t pool_lock;
2353f542974SPaul B Schroeder 	struct urb *write_urb_pool[NUM_URBS];
2360de9a702SOliver Neukum 	char busy[NUM_URBS];
23750de36f7SGreg Kroah-Hartman 	bool read_urb_busy;
2383f542974SPaul B Schroeder 
2390eafe4deSDonald 	/* For device(s) with LED indicator */
2400eafe4deSDonald 	bool has_led;
2410eafe4deSDonald 	struct timer_list led_timer1;	/* Timer for LED on */
2420eafe4deSDonald 	struct timer_list led_timer2;	/* Timer for LED off */
24305cf0decSJohan Hovold 	struct urb *led_urb;
24405cf0decSJohan Hovold 	struct usb_ctrlrequest *led_dr;
245d8a083ccSJohan Hovold 
246d8a083ccSJohan Hovold 	unsigned long flags;
2470eafe4deSDonald };
2483f542974SPaul B Schroeder 
2493f542974SPaul B Schroeder /*
2503f542974SPaul B Schroeder  * mos7840_set_reg_sync
2513f542974SPaul B Schroeder  * 	To set the Control register by calling usb_fill_control_urb function
2523f542974SPaul B Schroeder  *	by passing usb_sndctrlpipe function as parameter.
2533f542974SPaul B Schroeder  */
2543f542974SPaul B Schroeder 
2553f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2563f542974SPaul B Schroeder 				__u16 val)
2573f542974SPaul B Schroeder {
2583f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2593f542974SPaul B Schroeder 	val = val & 0x00ff;
2609c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
2613f542974SPaul B Schroeder 
2623f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2633f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2643f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2653f542974SPaul B Schroeder }
2663f542974SPaul B Schroeder 
2673f542974SPaul B Schroeder /*
2683f542974SPaul B Schroeder  * mos7840_get_reg_sync
2693f542974SPaul B Schroeder  * 	To set the Uart register by calling usb_fill_control_urb function by
2703f542974SPaul B Schroeder  *	passing usb_rcvctrlpipe function as parameter.
2713f542974SPaul B Schroeder  */
2723f542974SPaul B Schroeder 
2733f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
2743f542974SPaul B Schroeder 				__u16 *val)
2753f542974SPaul B Schroeder {
2763f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2773f542974SPaul B Schroeder 	int ret = 0;
2789e221a35SJohan Hovold 	u8 *buf;
2799e221a35SJohan Hovold 
2809e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2819e221a35SJohan Hovold 	if (!buf)
2829e221a35SJohan Hovold 		return -ENOMEM;
2833f542974SPaul B Schroeder 
2843f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
2859e221a35SJohan Hovold 			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
2863f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
287*cd8db057SJohan Hovold 	if (ret < VENDOR_READ_LENGTH) {
288*cd8db057SJohan Hovold 		if (ret >= 0)
289*cd8db057SJohan Hovold 			ret = -EIO;
290*cd8db057SJohan Hovold 		goto out;
291*cd8db057SJohan Hovold 	}
292*cd8db057SJohan Hovold 
2939e221a35SJohan Hovold 	*val = buf[0];
2949c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
295*cd8db057SJohan Hovold out:
2969e221a35SJohan Hovold 	kfree(buf);
2973f542974SPaul B Schroeder 	return ret;
2983f542974SPaul B Schroeder }
2993f542974SPaul B Schroeder 
3003f542974SPaul B Schroeder /*
3013f542974SPaul B Schroeder  * mos7840_set_uart_reg
3023f542974SPaul B Schroeder  *	To set the Uart register by calling usb_fill_control_urb function by
3033f542974SPaul B Schroeder  *	passing usb_sndctrlpipe function as parameter.
3043f542974SPaul B Schroeder  */
3053f542974SPaul B Schroeder 
3063f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
3073f542974SPaul B Schroeder 				__u16 val)
3083f542974SPaul B Schroeder {
3093f542974SPaul B Schroeder 
3103f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3113f542974SPaul B Schroeder 	val = val & 0x00ff;
312880af9dbSAlan Cox 	/* For the UART control registers, the application number need
313880af9dbSAlan Cox 	   to be Or'ed */
3140de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3151143832eSGreg Kroah-Hartman 		val |= ((__u16)port->port_number + 1) << 8;
3163f542974SPaul B Schroeder 	} else {
3171143832eSGreg Kroah-Hartman 		if (port->port_number == 0) {
3181143832eSGreg Kroah-Hartman 			val |= ((__u16)port->port_number + 1) << 8;
3193f542974SPaul B Schroeder 		} else {
3201143832eSGreg Kroah-Hartman 			val |= ((__u16)port->port_number + 2) << 8;
3213f542974SPaul B Schroeder 		}
3223f542974SPaul B Schroeder 	}
3239c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
3243f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
3253f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
3263f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
3273f542974SPaul B Schroeder 
3283f542974SPaul B Schroeder }
3293f542974SPaul B Schroeder 
3303f542974SPaul B Schroeder /*
3313f542974SPaul B Schroeder  * mos7840_get_uart_reg
3323f542974SPaul B Schroeder  *	To set the Control register by calling usb_fill_control_urb function
3333f542974SPaul B Schroeder  *	by passing usb_rcvctrlpipe function as parameter.
3343f542974SPaul B Schroeder  */
3353f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3363f542974SPaul B Schroeder 				__u16 *val)
3373f542974SPaul B Schroeder {
3383f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3393f542974SPaul B Schroeder 	int ret = 0;
3403f542974SPaul B Schroeder 	__u16 Wval;
3419e221a35SJohan Hovold 	u8 *buf;
3429e221a35SJohan Hovold 
3439e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3449e221a35SJohan Hovold 	if (!buf)
3459e221a35SJohan Hovold 		return -ENOMEM;
3463f542974SPaul B Schroeder 
3473f542974SPaul B Schroeder 	/* Wval  is same as application number */
3480de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3491143832eSGreg Kroah-Hartman 		Wval = ((__u16)port->port_number + 1) << 8;
3503f542974SPaul B Schroeder 	} else {
3511143832eSGreg Kroah-Hartman 		if (port->port_number == 0) {
3521143832eSGreg Kroah-Hartman 			Wval = ((__u16)port->port_number + 1) << 8;
3533f542974SPaul B Schroeder 		} else {
3541143832eSGreg Kroah-Hartman 			Wval = ((__u16)port->port_number + 2) << 8;
3553f542974SPaul B Schroeder 		}
3563f542974SPaul B Schroeder 	}
3579c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
3583f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3599e221a35SJohan Hovold 			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
3603f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
361*cd8db057SJohan Hovold 	if (ret < VENDOR_READ_LENGTH) {
362*cd8db057SJohan Hovold 		if (ret >= 0)
363*cd8db057SJohan Hovold 			ret = -EIO;
364*cd8db057SJohan Hovold 		goto out;
365*cd8db057SJohan Hovold 	}
3669e221a35SJohan Hovold 	*val = buf[0];
367*cd8db057SJohan Hovold out:
3689e221a35SJohan Hovold 	kfree(buf);
3693f542974SPaul B Schroeder 	return ret;
3703f542974SPaul B Schroeder }
3713f542974SPaul B Schroeder 
3729c134a14SGreg Kroah-Hartman static void mos7840_dump_serial_port(struct usb_serial_port *port,
3739c134a14SGreg Kroah-Hartman 				     struct moschip_port *mos7840_port)
3743f542974SPaul B Schroeder {
3753f542974SPaul B Schroeder 
3769c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
3779c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
3789c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
3793f542974SPaul B Schroeder 
3803f542974SPaul B Schroeder }
3813f542974SPaul B Schroeder 
3823f542974SPaul B Schroeder /************************************************************************/
3833f542974SPaul B Schroeder /************************************************************************/
3843f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3853f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3863f542974SPaul B Schroeder /************************************************************************/
3873f542974SPaul B Schroeder /************************************************************************/
3883f542974SPaul B Schroeder 
3893f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port,
3903f542974SPaul B Schroeder 					    struct moschip_port *data)
3913f542974SPaul B Schroeder {
3923f542974SPaul B Schroeder 	usb_set_serial_port_data(port, (void *)data);
3933f542974SPaul B Schroeder }
3943f542974SPaul B Schroeder 
3953f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct
3963f542974SPaul B Schroeder 							    usb_serial_port
3973f542974SPaul B Schroeder 							    *port)
3983f542974SPaul B Schroeder {
3993f542974SPaul B Schroeder 	return (struct moschip_port *)usb_get_serial_port_data(port);
4003f542974SPaul B Schroeder }
4013f542974SPaul B Schroeder 
4020de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
4033f542974SPaul B Schroeder {
4043f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4053f542974SPaul B Schroeder 	struct async_icount *icount;
4063f542974SPaul B Schroeder 	mos7840_port = port;
4073f542974SPaul B Schroeder 	if (new_msr &
4083f542974SPaul B Schroeder 	    (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
4093f542974SPaul B Schroeder 	     MOS_MSR_DELTA_CD)) {
4108c1a07ffSJohan Hovold 		icount = &mos7840_port->port->icount;
4113f542974SPaul B Schroeder 
4123f542974SPaul B Schroeder 		/* update input line counters */
413c9fac853SJohan Hovold 		if (new_msr & MOS_MSR_DELTA_CTS)
4143f542974SPaul B Schroeder 			icount->cts++;
415c9fac853SJohan Hovold 		if (new_msr & MOS_MSR_DELTA_DSR)
4163f542974SPaul B Schroeder 			icount->dsr++;
417c9fac853SJohan Hovold 		if (new_msr & MOS_MSR_DELTA_CD)
4183f542974SPaul B Schroeder 			icount->dcd++;
419c9fac853SJohan Hovold 		if (new_msr & MOS_MSR_DELTA_RI)
4203f542974SPaul B Schroeder 			icount->rng++;
421e670c6afSJohan Hovold 
4220c613371SJohan Hovold 		wake_up_interruptible(&port->port->port.delta_msr_wait);
4233f542974SPaul B Schroeder 	}
4243f542974SPaul B Schroeder }
4253f542974SPaul B Schroeder 
4260de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
4273f542974SPaul B Schroeder {
4283f542974SPaul B Schroeder 	struct async_icount *icount;
4293f542974SPaul B Schroeder 
4303f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
431880af9dbSAlan Cox 		/*
432880af9dbSAlan Cox 		 * Parity and Framing errors only count if they
433880af9dbSAlan Cox 		 * occur exclusive of a break being
434880af9dbSAlan Cox 		 * received.
435880af9dbSAlan Cox 		 */
4363f542974SPaul B Schroeder 		new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
4373f542974SPaul B Schroeder 	}
4383f542974SPaul B Schroeder 
4393f542974SPaul B Schroeder 	/* update input line counters */
4408c1a07ffSJohan Hovold 	icount = &port->port->icount;
441c9fac853SJohan Hovold 	if (new_lsr & SERIAL_LSR_BI)
4423f542974SPaul B Schroeder 		icount->brk++;
443c9fac853SJohan Hovold 	if (new_lsr & SERIAL_LSR_OE)
4443f542974SPaul B Schroeder 		icount->overrun++;
445c9fac853SJohan Hovold 	if (new_lsr & SERIAL_LSR_PE)
4463f542974SPaul B Schroeder 		icount->parity++;
447c9fac853SJohan Hovold 	if (new_lsr & SERIAL_LSR_FE)
4483f542974SPaul B Schroeder 		icount->frame++;
4493f542974SPaul B Schroeder }
4503f542974SPaul B Schroeder 
4513f542974SPaul B Schroeder /************************************************************************/
4523f542974SPaul B Schroeder /************************************************************************/
4533f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4543f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4553f542974SPaul B Schroeder /************************************************************************/
4563f542974SPaul B Schroeder /************************************************************************/
4573f542974SPaul B Schroeder 
4587d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb)
4593f542974SPaul B Schroeder {
4603f542974SPaul B Schroeder 	unsigned char *data;
4613f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4629c134a14SGreg Kroah-Hartman 	struct device *dev = &urb->dev->dev;
4633f542974SPaul B Schroeder 	__u8 regval = 0x0;
4640643c724SGreg Kroah-Hartman 	int status = urb->status;
4653f542974SPaul B Schroeder 
466cdc97792SMing Lei 	mos7840_port = urb->context;
4670de9a702SOliver Neukum 
4680643c724SGreg Kroah-Hartman 	switch (status) {
4693f542974SPaul B Schroeder 	case 0:
4703f542974SPaul B Schroeder 		/* success */
4713f542974SPaul B Schroeder 		break;
4723f542974SPaul B Schroeder 	case -ECONNRESET:
4733f542974SPaul B Schroeder 	case -ENOENT:
4743f542974SPaul B Schroeder 	case -ESHUTDOWN:
4753f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
4769c134a14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
477d8a083ccSJohan Hovold 		goto out;
4783f542974SPaul B Schroeder 	default:
4799c134a14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
480d8a083ccSJohan Hovold 		goto out;
4813f542974SPaul B Schroeder 	}
4823f542974SPaul B Schroeder 
4839c134a14SGreg Kroah-Hartman 	dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
4849c134a14SGreg Kroah-Hartman 	dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
4853f542974SPaul B Schroeder 		mos7840_port->MsrLsr, mos7840_port->port_num);
4863f542974SPaul B Schroeder 	data = urb->transfer_buffer;
4873f542974SPaul B Schroeder 	regval = (__u8) data[0];
4889c134a14SGreg Kroah-Hartman 	dev_dbg(dev, "%s data is %x\n", __func__, regval);
4893f542974SPaul B Schroeder 	if (mos7840_port->MsrLsr == 0)
4903f542974SPaul B Schroeder 		mos7840_handle_new_msr(mos7840_port, regval);
4913f542974SPaul B Schroeder 	else if (mos7840_port->MsrLsr == 1)
4923f542974SPaul B Schroeder 		mos7840_handle_new_lsr(mos7840_port, regval);
493d8a083ccSJohan Hovold out:
494d8a083ccSJohan Hovold 	clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
4953f542974SPaul B Schroeder }
4963f542974SPaul B Schroeder 
4973f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
4983f542974SPaul B Schroeder 			   __u16 *val)
4993f542974SPaul B Schroeder {
5003f542974SPaul B Schroeder 	struct usb_device *dev = mcs->port->serial->dev;
5010de9a702SOliver Neukum 	struct usb_ctrlrequest *dr = mcs->dr;
5020de9a702SOliver Neukum 	unsigned char *buffer = mcs->ctrl_buf;
5030de9a702SOliver Neukum 	int ret;
5043f542974SPaul B Schroeder 
505d8a083ccSJohan Hovold 	if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
506d8a083ccSJohan Hovold 		return -EBUSY;
507d8a083ccSJohan Hovold 
5083f542974SPaul B Schroeder 	dr->bRequestType = MCS_RD_RTYPE;
5093f542974SPaul B Schroeder 	dr->bRequest = MCS_RDREQ;
510880af9dbSAlan Cox 	dr->wValue = cpu_to_le16(Wval);	/* 0 */
5113f542974SPaul B Schroeder 	dr->wIndex = cpu_to_le16(reg);
5123f542974SPaul B Schroeder 	dr->wLength = cpu_to_le16(2);
5133f542974SPaul B Schroeder 
5143f542974SPaul B Schroeder 	usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
5153f542974SPaul B Schroeder 			     (unsigned char *)dr, buffer, 2,
5163f542974SPaul B Schroeder 			     mos7840_control_callback, mcs);
5173f542974SPaul B Schroeder 	mcs->control_urb->transfer_buffer_length = 2;
5183f542974SPaul B Schroeder 	ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
519d8a083ccSJohan Hovold 	if (ret)
520d8a083ccSJohan Hovold 		clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
521d8a083ccSJohan Hovold 
5223f542974SPaul B Schroeder 	return ret;
5233f542974SPaul B Schroeder }
5243f542974SPaul B Schroeder 
5250eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb)
5260eafe4deSDonald {
5270eafe4deSDonald 	switch (urb->status) {
5280eafe4deSDonald 	case 0:
5290eafe4deSDonald 		/* Success */
5300eafe4deSDonald 		break;
5310eafe4deSDonald 	case -ECONNRESET:
5320eafe4deSDonald 	case -ENOENT:
5330eafe4deSDonald 	case -ESHUTDOWN:
5340eafe4deSDonald 		/* This urb is terminated, clean up */
535d9a38a87SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
5369c134a14SGreg Kroah-Hartman 			__func__, urb->status);
5370eafe4deSDonald 		break;
5380eafe4deSDonald 	default:
539d9a38a87SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
5409c134a14SGreg Kroah-Hartman 			__func__, urb->status);
5410eafe4deSDonald 	}
5420eafe4deSDonald }
5430eafe4deSDonald 
5440eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
5450eafe4deSDonald 				__u16 reg)
5460eafe4deSDonald {
5470eafe4deSDonald 	struct usb_device *dev = mcs->port->serial->dev;
54805cf0decSJohan Hovold 	struct usb_ctrlrequest *dr = mcs->led_dr;
5490eafe4deSDonald 
5500eafe4deSDonald 	dr->bRequestType = MCS_WR_RTYPE;
5510eafe4deSDonald 	dr->bRequest = MCS_WRREQ;
5520eafe4deSDonald 	dr->wValue = cpu_to_le16(wval);
5530eafe4deSDonald 	dr->wIndex = cpu_to_le16(reg);
5540eafe4deSDonald 	dr->wLength = cpu_to_le16(0);
5550eafe4deSDonald 
55605cf0decSJohan Hovold 	usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
5570eafe4deSDonald 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
5580eafe4deSDonald 
55905cf0decSJohan Hovold 	usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
5600eafe4deSDonald }
5610eafe4deSDonald 
5620eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
5630eafe4deSDonald 				__u16 val)
5640eafe4deSDonald {
5650eafe4deSDonald 	struct usb_device *dev = port->serial->dev;
5660eafe4deSDonald 
5670eafe4deSDonald 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
5680eafe4deSDonald 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
5690eafe4deSDonald }
5700eafe4deSDonald 
5710eafe4deSDonald static void mos7840_led_off(unsigned long arg)
5720eafe4deSDonald {
5730eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
5740eafe4deSDonald 
5750eafe4deSDonald 	/* Turn off LED */
5760eafe4deSDonald 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
5770eafe4deSDonald 	mod_timer(&mcs->led_timer2,
5780eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_OFF_MS));
5790eafe4deSDonald }
5800eafe4deSDonald 
5810eafe4deSDonald static void mos7840_led_flag_off(unsigned long arg)
5820eafe4deSDonald {
5830eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
5840eafe4deSDonald 
58505cf0decSJohan Hovold 	clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
58605cf0decSJohan Hovold }
58705cf0decSJohan Hovold 
58805cf0decSJohan Hovold static void mos7840_led_activity(struct usb_serial_port *port)
58905cf0decSJohan Hovold {
59005cf0decSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
59105cf0decSJohan Hovold 
59205cf0decSJohan Hovold 	if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
59305cf0decSJohan Hovold 		return;
59405cf0decSJohan Hovold 
59505cf0decSJohan Hovold 	mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
59605cf0decSJohan Hovold 	mod_timer(&mos7840_port->led_timer1,
59705cf0decSJohan Hovold 				jiffies + msecs_to_jiffies(LED_ON_MS));
5980eafe4deSDonald }
5990eafe4deSDonald 
6003f542974SPaul B Schroeder /*****************************************************************************
6013f542974SPaul B Schroeder  * mos7840_interrupt_callback
6023f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
6033f542974SPaul B Schroeder  *	interrupt endpoint.
6043f542974SPaul B Schroeder  *****************************************************************************/
6053f542974SPaul B Schroeder 
6067d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb)
6073f542974SPaul B Schroeder {
6083f542974SPaul B Schroeder 	int result;
6093f542974SPaul B Schroeder 	int length;
6103f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
6113f542974SPaul B Schroeder 	struct usb_serial *serial;
6123f542974SPaul B Schroeder 	__u16 Data;
6133f542974SPaul B Schroeder 	unsigned char *data;
6143f542974SPaul B Schroeder 	__u8 sp[5], st;
6150de9a702SOliver Neukum 	int i, rv = 0;
6160de9a702SOliver Neukum 	__u16 wval, wreg = 0;
6170643c724SGreg Kroah-Hartman 	int status = urb->status;
6183f542974SPaul B Schroeder 
6190643c724SGreg Kroah-Hartman 	switch (status) {
6203f542974SPaul B Schroeder 	case 0:
6213f542974SPaul B Schroeder 		/* success */
6223f542974SPaul B Schroeder 		break;
6233f542974SPaul B Schroeder 	case -ECONNRESET:
6243f542974SPaul B Schroeder 	case -ENOENT:
6253f542974SPaul B Schroeder 	case -ESHUTDOWN:
6263f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
6279c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
6289c134a14SGreg Kroah-Hartman 			__func__, status);
6293f542974SPaul B Schroeder 		return;
6303f542974SPaul B Schroeder 	default:
6319c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
6329c134a14SGreg Kroah-Hartman 			__func__, status);
6333f542974SPaul B Schroeder 		goto exit;
6343f542974SPaul B Schroeder 	}
6353f542974SPaul B Schroeder 
6363f542974SPaul B Schroeder 	length = urb->actual_length;
6373f542974SPaul B Schroeder 	data = urb->transfer_buffer;
6383f542974SPaul B Schroeder 
639cdc97792SMing Lei 	serial = urb->context;
6403f542974SPaul B Schroeder 
6413f542974SPaul B Schroeder 	/* Moschip get 5 bytes
6423f542974SPaul B Schroeder 	 * Byte 1 IIR Port 1 (port.number is 0)
6433f542974SPaul B Schroeder 	 * Byte 2 IIR Port 2 (port.number is 1)
6443f542974SPaul B Schroeder 	 * Byte 3 IIR Port 3 (port.number is 2)
6453f542974SPaul B Schroeder 	 * Byte 4 IIR Port 4 (port.number is 3)
6463f542974SPaul B Schroeder 	 * Byte 5 FIFO status for both */
6473f542974SPaul B Schroeder 
648365a0442SGeyslan G. Bem 	if (length > 5) {
6499c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
6503f542974SPaul B Schroeder 		return;
6513f542974SPaul B Schroeder 	}
6523f542974SPaul B Schroeder 
6533f542974SPaul B Schroeder 	sp[0] = (__u8) data[0];
6543f542974SPaul B Schroeder 	sp[1] = (__u8) data[1];
6553f542974SPaul B Schroeder 	sp[2] = (__u8) data[2];
6563f542974SPaul B Schroeder 	sp[3] = (__u8) data[3];
6573f542974SPaul B Schroeder 	st = (__u8) data[4];
6583f542974SPaul B Schroeder 
6593f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; i++) {
6603f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
6611143832eSGreg Kroah-Hartman 		wval = ((__u16)serial->port[i]->port_number + 1) << 8;
6623f542974SPaul B Schroeder 		if (mos7840_port->open) {
6633f542974SPaul B Schroeder 			if (sp[i] & 0x01) {
6649c134a14SGreg Kroah-Hartman 				dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
6653f542974SPaul B Schroeder 			} else {
6663f542974SPaul B Schroeder 				switch (sp[i] & 0x0f) {
6673f542974SPaul B Schroeder 				case SERIAL_IIR_RLS:
6689c134a14SGreg Kroah-Hartman 					dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
6699c134a14SGreg Kroah-Hartman 					dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
6703f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 1;
6710de9a702SOliver Neukum 					wreg = LINE_STATUS_REGISTER;
6723f542974SPaul B Schroeder 					break;
6733f542974SPaul B Schroeder 				case SERIAL_IIR_MS:
6749c134a14SGreg Kroah-Hartman 					dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
6753f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 0;
6760de9a702SOliver Neukum 					wreg = MODEM_STATUS_REGISTER;
6773f542974SPaul B Schroeder 					break;
6783f542974SPaul B Schroeder 				}
6790de9a702SOliver Neukum 				rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
6803f542974SPaul B Schroeder 			}
6813f542974SPaul B Schroeder 		}
6823f542974SPaul B Schroeder 	}
683880af9dbSAlan Cox 	if (!(rv < 0))
684880af9dbSAlan Cox 		/* the completion handler for the control urb will resubmit */
6850de9a702SOliver Neukum 		return;
6863f542974SPaul B Schroeder exit:
6873f542974SPaul B Schroeder 	result = usb_submit_urb(urb, GFP_ATOMIC);
6883f542974SPaul B Schroeder 	if (result) {
6893f542974SPaul B Schroeder 		dev_err(&urb->dev->dev,
6903f542974SPaul B Schroeder 			"%s - Error %d submitting interrupt urb\n",
691441b62c1SHarvey Harrison 			__func__, result);
6923f542974SPaul B Schroeder 	}
6933f542974SPaul B Schroeder }
6943f542974SPaul B Schroeder 
6953f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port,
6963f542974SPaul B Schroeder 				       const char *function)
6973f542974SPaul B Schroeder {
6983f542974SPaul B Schroeder 	if (!port) {
6999c134a14SGreg Kroah-Hartman 		pr_debug("%s - port == NULL\n", function);
7003f542974SPaul B Schroeder 		return -1;
7013f542974SPaul B Schroeder 	}
7023f542974SPaul B Schroeder 	if (!port->serial) {
7039c134a14SGreg Kroah-Hartman 		pr_debug("%s - port->serial == NULL\n", function);
7043f542974SPaul B Schroeder 		return -1;
7053f542974SPaul B Schroeder 	}
7063f542974SPaul B Schroeder 
7073f542974SPaul B Schroeder 	return 0;
7083f542974SPaul B Schroeder }
7093f542974SPaul B Schroeder 
7103f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
7113f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
7123f542974SPaul B Schroeder 					 const char *function)
7133f542974SPaul B Schroeder {
7143f542974SPaul B Schroeder 	if (!serial) {
7159c134a14SGreg Kroah-Hartman 		pr_debug("%s - serial == NULL\n", function);
7163f542974SPaul B Schroeder 		return -1;
7173f542974SPaul B Schroeder 	}
7183f542974SPaul B Schroeder 	if (!serial->type) {
7199c134a14SGreg Kroah-Hartman 		pr_debug("%s - serial->type == NULL!\n", function);
7203f542974SPaul B Schroeder 		return -1;
7213f542974SPaul B Schroeder 	}
7223f542974SPaul B Schroeder 
7233f542974SPaul B Schroeder 	return 0;
7243f542974SPaul B Schroeder }
7253f542974SPaul B Schroeder 
7263f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
7273f542974SPaul B Schroeder 						 const char *function)
7283f542974SPaul B Schroeder {
7293f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
7303f542974SPaul B Schroeder 	if (!port ||
7313f542974SPaul B Schroeder 	    mos7840_port_paranoia_check(port, function) ||
7323f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
733880af9dbSAlan Cox 		/* then say that we don't have a valid usb_serial thing,
734880af9dbSAlan Cox 		 * which will end up genrating -ENODEV return values */
7353f542974SPaul B Schroeder 		return NULL;
7363f542974SPaul B Schroeder 	}
7373f542974SPaul B Schroeder 
7383f542974SPaul B Schroeder 	return port->serial;
7393f542974SPaul B Schroeder }
7403f542974SPaul B Schroeder 
7413f542974SPaul B Schroeder /*****************************************************************************
7423f542974SPaul B Schroeder  * mos7840_bulk_in_callback
7433f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
7443f542974SPaul B Schroeder  *	bulk in endpoint.
7453f542974SPaul B Schroeder  *****************************************************************************/
7463f542974SPaul B Schroeder 
7477d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
7483f542974SPaul B Schroeder {
7490643c724SGreg Kroah-Hartman 	int retval;
7503f542974SPaul B Schroeder 	unsigned char *data;
7513f542974SPaul B Schroeder 	struct usb_serial *serial;
7523f542974SPaul B Schroeder 	struct usb_serial_port *port;
7533f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
7540643c724SGreg Kroah-Hartman 	int status = urb->status;
7553f542974SPaul B Schroeder 
756cdc97792SMing Lei 	mos7840_port = urb->context;
7579c134a14SGreg Kroah-Hartman 	if (!mos7840_port)
75850de36f7SGreg Kroah-Hartman 		return;
75950de36f7SGreg Kroah-Hartman 
76050de36f7SGreg Kroah-Hartman 	if (status) {
7619c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
76250de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7633f542974SPaul B Schroeder 		return;
7643f542974SPaul B Schroeder 	}
7653f542974SPaul B Schroeder 
7669c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
767441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
76850de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7693f542974SPaul B Schroeder 		return;
7703f542974SPaul B Schroeder 	}
7713f542974SPaul B Schroeder 
772441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
7733f542974SPaul B Schroeder 	if (!serial) {
77450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7753f542974SPaul B Schroeder 		return;
7763f542974SPaul B Schroeder 	}
7773f542974SPaul B Schroeder 
7783f542974SPaul B Schroeder 	data = urb->transfer_buffer;
77959d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
7803f542974SPaul B Schroeder 
7813f542974SPaul B Schroeder 	if (urb->actual_length) {
78205c7cd39SJiri Slaby 		struct tty_port *tport = &mos7840_port->port->port;
78305c7cd39SJiri Slaby 		tty_insert_flip_string(tport, data, urb->actual_length);
7842e124b4aSJiri Slaby 		tty_flip_buffer_push(tport);
7858c1a07ffSJohan Hovold 		port->icount.rx += urb->actual_length;
7868c1a07ffSJohan Hovold 		dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
7873f542974SPaul B Schroeder 	}
7883f542974SPaul B Schroeder 
7893f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
7909c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
79150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7923f542974SPaul B Schroeder 		return;
7933f542974SPaul B Schroeder 	}
7943f542974SPaul B Schroeder 
79505cf0decSJohan Hovold 	if (mos7840_port->has_led)
79605cf0decSJohan Hovold 		mos7840_led_activity(port);
7970de9a702SOliver Neukum 
79850de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
7990643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
8003f542974SPaul B Schroeder 
8010643c724SGreg Kroah-Hartman 	if (retval) {
8029c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
80350de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8043f542974SPaul B Schroeder 	}
8053f542974SPaul B Schroeder }
8063f542974SPaul B Schroeder 
8073f542974SPaul B Schroeder /*****************************************************************************
8083f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
809880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
810880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
8113f542974SPaul B Schroeder  *****************************************************************************/
8123f542974SPaul B Schroeder 
8137d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
8143f542974SPaul B Schroeder {
8153f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8169c134a14SGreg Kroah-Hartman 	struct usb_serial_port *port;
8170643c724SGreg Kroah-Hartman 	int status = urb->status;
8180de9a702SOliver Neukum 	int i;
8190de9a702SOliver Neukum 
820cdc97792SMing Lei 	mos7840_port = urb->context;
8219c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
8220de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
8230de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
8240de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
8250de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
8260de9a702SOliver Neukum 			break;
8270de9a702SOliver Neukum 		}
8280de9a702SOliver Neukum 	}
8290de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
8300de9a702SOliver Neukum 
8310643c724SGreg Kroah-Hartman 	if (status) {
8329c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
8333f542974SPaul B Schroeder 		return;
8343f542974SPaul B Schroeder 	}
8353f542974SPaul B Schroeder 
8369c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
8373f542974SPaul B Schroeder 		return;
8383f542974SPaul B Schroeder 
8396aad04f2SJiri Slaby 	if (mos7840_port->open)
8406aad04f2SJiri Slaby 		tty_port_tty_wakeup(&port->port);
8413f542974SPaul B Schroeder 
8423f542974SPaul B Schroeder }
8433f542974SPaul B Schroeder 
8443f542974SPaul B Schroeder /************************************************************************/
8453f542974SPaul 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       */
8463f542974SPaul B Schroeder /************************************************************************/
8473f542974SPaul B Schroeder 
8483f542974SPaul B Schroeder /*****************************************************************************
8493f542974SPaul B Schroeder  * mos7840_open
8503f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
8513f542974SPaul B Schroeder  *	If successful, we return 0
8523f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
8533f542974SPaul B Schroeder  *****************************************************************************/
8543f542974SPaul B Schroeder 
855a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
8563f542974SPaul B Schroeder {
8573f542974SPaul B Schroeder 	int response;
8583f542974SPaul B Schroeder 	int j;
8593f542974SPaul B Schroeder 	struct usb_serial *serial;
8603f542974SPaul B Schroeder 	struct urb *urb;
8613f542974SPaul B Schroeder 	__u16 Data;
8623f542974SPaul B Schroeder 	int status;
8633f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8640de9a702SOliver Neukum 	struct moschip_port *port0;
8653f542974SPaul B Schroeder 
8669c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
8673f542974SPaul B Schroeder 		return -ENODEV;
8683f542974SPaul B Schroeder 
8693f542974SPaul B Schroeder 	serial = port->serial;
8703f542974SPaul B Schroeder 
8719c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
8723f542974SPaul B Schroeder 		return -ENODEV;
8733f542974SPaul B Schroeder 
8743f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
8750de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
8763f542974SPaul B Schroeder 
8770de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
8783f542974SPaul B Schroeder 		return -ENODEV;
8793f542974SPaul B Schroeder 
8803f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
8813f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
8820de9a702SOliver Neukum 	port0->open_ports++;
8833f542974SPaul B Schroeder 
8843f542974SPaul B Schroeder 	/* Initialising the write urb pool */
8853f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
8860de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
8873f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
88810c642d0SJohan Hovold 		if (!urb)
8893f542974SPaul B Schroeder 			continue;
8903f542974SPaul B Schroeder 
891880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
892880af9dbSAlan Cox 								GFP_KERNEL);
8933f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
8940de9a702SOliver Neukum 			usb_free_urb(urb);
8950de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
8963f542974SPaul B Schroeder 			continue;
8973f542974SPaul B Schroeder 		}
8983f542974SPaul B Schroeder 	}
8993f542974SPaul B Schroeder 
9003f542974SPaul B Schroeder /*****************************************************************************
9013f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
9023f542974SPaul B Schroeder  *
9033f542974SPaul B Schroeder  * Register Index
9043f542974SPaul B Schroeder  * 1 : IER
9053f542974SPaul B Schroeder  * 2 : FCR
9063f542974SPaul B Schroeder  * 3 : LCR
9073f542974SPaul B Schroeder  * 4 : MCR
9083f542974SPaul B Schroeder  *
9093f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
9103f542974SPaul B Schroeder  *****************************************************************************/
9113f542974SPaul B Schroeder 
912880af9dbSAlan Cox 	/* NEED to check the following Block */
9133f542974SPaul B Schroeder 
9143f542974SPaul B Schroeder 	Data = 0x0;
9153f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
9163f542974SPaul B Schroeder 	if (status < 0) {
9179c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Spreg failed\n");
9185f8a2e68SJohan Hovold 		goto err;
9193f542974SPaul B Schroeder 	}
9203f542974SPaul B Schroeder 	Data |= 0x80;
9213f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9223f542974SPaul B Schroeder 	if (status < 0) {
9239c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
9245f8a2e68SJohan Hovold 		goto err;
9253f542974SPaul B Schroeder 	}
9263f542974SPaul B Schroeder 
9273f542974SPaul B Schroeder 	Data &= ~0x80;
9283f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9293f542974SPaul B Schroeder 	if (status < 0) {
9309c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
9315f8a2e68SJohan Hovold 		goto err;
9323f542974SPaul B Schroeder 	}
933880af9dbSAlan Cox 	/* End of block to be checked */
9343f542974SPaul B Schroeder 
9353f542974SPaul B Schroeder 	Data = 0x0;
936880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
937880af9dbSAlan Cox 									&Data);
9383f542974SPaul B Schroeder 	if (status < 0) {
9399c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Controlreg failed\n");
9405f8a2e68SJohan Hovold 		goto err;
9413f542974SPaul B Schroeder 	}
942880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
943880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
944880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
945880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
9463f542974SPaul B Schroeder 	if (status < 0) {
9479c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Controlreg failed\n");
9485f8a2e68SJohan Hovold 		goto err;
9493f542974SPaul B Schroeder 	}
950880af9dbSAlan Cox 	/* do register settings here */
951880af9dbSAlan Cox 	/* Set all regs to the device default values. */
952880af9dbSAlan Cox 	/***********************************
953880af9dbSAlan Cox 	 * First Disable all interrupts.
954880af9dbSAlan Cox 	 ***********************************/
9553f542974SPaul B Schroeder 	Data = 0x00;
9563f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
9573f542974SPaul B Schroeder 	if (status < 0) {
9589c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "disabling interrupts failed\n");
9595f8a2e68SJohan Hovold 		goto err;
9603f542974SPaul B Schroeder 	}
961880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
9623f542974SPaul B Schroeder 	Data = 0x00;
9633f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9643f542974SPaul B Schroeder 	if (status < 0) {
9659c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
9665f8a2e68SJohan Hovold 		goto err;
9673f542974SPaul B Schroeder 	}
9683f542974SPaul B Schroeder 
9693f542974SPaul B Schroeder 	Data = 0xcf;
9703f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9713f542974SPaul B Schroeder 	if (status < 0) {
9729c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
9735f8a2e68SJohan Hovold 		goto err;
9743f542974SPaul B Schroeder 	}
9753f542974SPaul B Schroeder 
9763f542974SPaul B Schroeder 	Data = 0x03;
9773f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9783f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9793f542974SPaul B Schroeder 
9803f542974SPaul B Schroeder 	Data = 0x0b;
9813f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
9823f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
9833f542974SPaul B Schroeder 
9843f542974SPaul B Schroeder 	Data = 0x00;
9853f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
9863f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9873f542974SPaul B Schroeder 
988880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
9893f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9903f542974SPaul B Schroeder 
9913f542974SPaul B Schroeder 	Data = 0x0c;
9923f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
9933f542974SPaul B Schroeder 
9943f542974SPaul B Schroeder 	Data = 0x0;
9953f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
9963f542974SPaul B Schroeder 
9973f542974SPaul B Schroeder 	Data = 0x00;
9983f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
9993f542974SPaul B Schroeder 
10003f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
10013f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10023f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10033f542974SPaul B Schroeder 
1004880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
10053f542974SPaul B Schroeder 	Data = 0x0;
10063f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
10073f542974SPaul B Schroeder 
10083f542974SPaul B Schroeder 	Data = Data | 0x0c;
10093f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10103f542974SPaul B Schroeder 
10113f542974SPaul B Schroeder 	Data = Data & ~0x0c;
10123f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1013880af9dbSAlan Cox 	/* Finally enable all interrupts */
10143f542974SPaul B Schroeder 	Data = 0x0c;
10153f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
10163f542974SPaul B Schroeder 
1017880af9dbSAlan Cox 	/* clearing rx_disable */
10183f542974SPaul B Schroeder 	Data = 0x0;
1019880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1020880af9dbSAlan Cox 									&Data);
10213f542974SPaul B Schroeder 	Data = Data & ~0x20;
1022880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1023880af9dbSAlan Cox 									Data);
10243f542974SPaul B Schroeder 
1025880af9dbSAlan Cox 	/* rx_negate */
10263f542974SPaul B Schroeder 	Data = 0x0;
1027880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1028880af9dbSAlan Cox 									&Data);
10293f542974SPaul B Schroeder 	Data = Data | 0x10;
1030880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1031880af9dbSAlan Cox 									Data);
10323f542974SPaul B Schroeder 
10333f542974SPaul B Schroeder 	/* Check to see if we've set up our endpoint info yet    *
10343f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the structures *
10353f542974SPaul B Schroeder 	 * were not set up at that time.)                        */
10360de9a702SOliver Neukum 	if (port0->open_ports == 1) {
10373f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_buffer == NULL) {
10383f542974SPaul B Schroeder 			/* set up interrupt urb */
10393f542974SPaul B Schroeder 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
10403f542974SPaul B Schroeder 				serial->dev,
10413f542974SPaul B Schroeder 				usb_rcvintpipe(serial->dev,
1042880af9dbSAlan Cox 				serial->port[0]->interrupt_in_endpointAddress),
10433f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_buffer,
10443f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_urb->
10453f542974SPaul B Schroeder 				transfer_buffer_length,
10463f542974SPaul B Schroeder 				mos7840_interrupt_callback,
10473f542974SPaul B Schroeder 				serial,
1048880af9dbSAlan Cox 				serial->port[0]->interrupt_in_urb->interval);
10493f542974SPaul B Schroeder 
1050472d7e55SJohan Hovold 			/* start interrupt read for mos7840 */
10513f542974SPaul B Schroeder 			response =
10523f542974SPaul B Schroeder 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
10533f542974SPaul B Schroeder 					   GFP_KERNEL);
10543f542974SPaul B Schroeder 			if (response) {
1055194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "%s - Error %d submitting "
1056194343d9SGreg Kroah-Hartman 					"interrupt urb\n", __func__, response);
10573f542974SPaul B Schroeder 			}
10583f542974SPaul B Schroeder 
10593f542974SPaul B Schroeder 		}
10603f542974SPaul B Schroeder 
10613f542974SPaul B Schroeder 	}
10623f542974SPaul B Schroeder 
10633f542974SPaul B Schroeder 	/* see if we've set up our endpoint info yet   *
10643f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the  *
10653f542974SPaul B Schroeder 	 * structures were not set up at that time.)   */
10663f542974SPaul B Schroeder 
10671143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "port number is %d\n", port->port_number);
1068e5b1e206SGreg Kroah-Hartman 	dev_dbg(&port->dev, "minor number is %d\n", port->minor);
10699c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
10709c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
10719c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
10729c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
10733f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
10743f542974SPaul B Schroeder 
10753f542974SPaul B Schroeder 	/* set up our bulk in urb */
10761143832eSGreg Kroah-Hartman 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1077093ea2d3SDonald Lee 		usb_fill_bulk_urb(mos7840_port->read_urb,
1078093ea2d3SDonald Lee 			serial->dev,
1079093ea2d3SDonald Lee 			usb_rcvbulkpipe(serial->dev,
1080093ea2d3SDonald Lee 				(port->bulk_in_endpointAddress) + 2),
1081093ea2d3SDonald Lee 			port->bulk_in_buffer,
1082093ea2d3SDonald Lee 			mos7840_port->read_urb->transfer_buffer_length,
1083093ea2d3SDonald Lee 			mos7840_bulk_in_callback, mos7840_port);
1084093ea2d3SDonald Lee 	} else {
10853f542974SPaul B Schroeder 		usb_fill_bulk_urb(mos7840_port->read_urb,
10863f542974SPaul B Schroeder 			serial->dev,
10873f542974SPaul B Schroeder 			usb_rcvbulkpipe(serial->dev,
10883f542974SPaul B Schroeder 				port->bulk_in_endpointAddress),
10893f542974SPaul B Schroeder 			port->bulk_in_buffer,
10903f542974SPaul B Schroeder 			mos7840_port->read_urb->transfer_buffer_length,
10913f542974SPaul B Schroeder 			mos7840_bulk_in_callback, mos7840_port);
1092093ea2d3SDonald Lee 	}
10933f542974SPaul B Schroeder 
10949c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
109550de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
10963f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
10973f542974SPaul B Schroeder 	if (response) {
1098194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1099194343d9SGreg Kroah-Hartman 			__func__, response);
110050de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
11013f542974SPaul B Schroeder 	}
11023f542974SPaul B Schroeder 
11033f542974SPaul B Schroeder 	/* initialize our port settings */
1104880af9dbSAlan Cox 	/* Must set to enable ints! */
1105880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
11063f542974SPaul B Schroeder 	/* send a open port command */
11073f542974SPaul B Schroeder 	mos7840_port->open = 1;
1108880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
11093f542974SPaul B Schroeder 
11103f542974SPaul B Schroeder 	return 0;
11115f8a2e68SJohan Hovold err:
11125f8a2e68SJohan Hovold 	for (j = 0; j < NUM_URBS; ++j) {
11135f8a2e68SJohan Hovold 		urb = mos7840_port->write_urb_pool[j];
11145f8a2e68SJohan Hovold 		if (!urb)
11155f8a2e68SJohan Hovold 			continue;
11165f8a2e68SJohan Hovold 		kfree(urb->transfer_buffer);
11175f8a2e68SJohan Hovold 		usb_free_urb(urb);
11185f8a2e68SJohan Hovold 	}
11195f8a2e68SJohan Hovold 	return status;
11203f542974SPaul B Schroeder }
11213f542974SPaul B Schroeder 
11223f542974SPaul B Schroeder /*****************************************************************************
11233f542974SPaul B Schroeder  * mos7840_chars_in_buffer
11243f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
11253f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
11263f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
11273f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
11283f542974SPaul B Schroeder  *	system,
112995da310eSAlan Cox  *	Otherwise we return zero.
11303f542974SPaul B Schroeder  *****************************************************************************/
11313f542974SPaul B Schroeder 
113295da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
11333f542974SPaul B Schroeder {
113495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11353f542974SPaul B Schroeder 	int i;
11363f542974SPaul B Schroeder 	int chars = 0;
11370de9a702SOliver Neukum 	unsigned long flags;
11383f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11393f542974SPaul B Schroeder 
11409c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
114195da310eSAlan Cox 		return 0;
11423f542974SPaul B Schroeder 
11433f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11443363155bSGreg Kroah-Hartman 	if (mos7840_port == NULL)
114595da310eSAlan Cox 		return 0;
11463f542974SPaul B Schroeder 
11470de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
11485c263b92SMark Ferrell 	for (i = 0; i < NUM_URBS; ++i) {
11495c263b92SMark Ferrell 		if (mos7840_port->busy[i]) {
11505c263b92SMark Ferrell 			struct urb *urb = mos7840_port->write_urb_pool[i];
11515c263b92SMark Ferrell 			chars += urb->transfer_buffer_length;
11525c263b92SMark Ferrell 		}
11535c263b92SMark Ferrell 	}
11540de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
11559c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
11560de9a702SOliver Neukum 	return chars;
11573f542974SPaul B Schroeder 
11583f542974SPaul B Schroeder }
11593f542974SPaul B Schroeder 
11603f542974SPaul B Schroeder /*****************************************************************************
11613f542974SPaul B Schroeder  * mos7840_close
11623f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
11633f542974SPaul B Schroeder  *****************************************************************************/
11643f542974SPaul B Schroeder 
1165335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
11663f542974SPaul B Schroeder {
11673f542974SPaul B Schroeder 	struct usb_serial *serial;
11683f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11690de9a702SOliver Neukum 	struct moschip_port *port0;
11703f542974SPaul B Schroeder 	int j;
11713f542974SPaul B Schroeder 	__u16 Data;
11723f542974SPaul B Schroeder 
11739c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
11743f542974SPaul B Schroeder 		return;
11753f542974SPaul B Schroeder 
1176441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
11779c134a14SGreg Kroah-Hartman 	if (!serial)
11783f542974SPaul B Schroeder 		return;
11793f542974SPaul B Schroeder 
11803f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11810de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
11823f542974SPaul B Schroeder 
11830de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
11843f542974SPaul B Schroeder 		return;
11853f542974SPaul B Schroeder 
11863f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
11873f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
11883f542974SPaul B Schroeder 
11893f542974SPaul B Schroeder 	/* Freeing Write URBs */
11903f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
11913f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
119291c72df1SFabian Frederick 			kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
11933f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
11943f542974SPaul B Schroeder 		}
11953f542974SPaul B Schroeder 	}
11963f542974SPaul B Schroeder 
11973f542974SPaul B Schroeder 	usb_kill_urb(mos7840_port->read_urb);
119850de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = false;
1199bb3529c6SJohan Hovold 
12000de9a702SOliver Neukum 	port0->open_ports--;
12011143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports);
12020de9a702SOliver Neukum 	if (port0->open_ports == 0) {
12033f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_urb) {
12049c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
12050de9a702SOliver Neukum 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
12063f542974SPaul B Schroeder 		}
12073f542974SPaul B Schroeder 	}
12083f542974SPaul B Schroeder 
12093f542974SPaul B Schroeder 	Data = 0x0;
12103f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
12113f542974SPaul B Schroeder 
12123f542974SPaul B Schroeder 	Data = 0x00;
12133f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
12143f542974SPaul B Schroeder 
12153f542974SPaul B Schroeder 	mos7840_port->open = 0;
12163f542974SPaul B Schroeder }
12173f542974SPaul B Schroeder 
12183f542974SPaul B Schroeder /*****************************************************************************
12193f542974SPaul B Schroeder  * mos7840_break
12203f542974SPaul B Schroeder  *	this function sends a break to the port
12213f542974SPaul B Schroeder  *****************************************************************************/
122295da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
12233f542974SPaul B Schroeder {
122495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
12253f542974SPaul B Schroeder 	unsigned char data;
12263f542974SPaul B Schroeder 	struct usb_serial *serial;
12273f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12283f542974SPaul B Schroeder 
12299c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
12303f542974SPaul B Schroeder 		return;
12313f542974SPaul B Schroeder 
1232441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
12339c134a14SGreg Kroah-Hartman 	if (!serial)
12343f542974SPaul B Schroeder 		return;
12353f542974SPaul B Schroeder 
12363f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12373f542974SPaul B Schroeder 
1238880af9dbSAlan Cox 	if (mos7840_port == NULL)
12393f542974SPaul B Schroeder 		return;
12403f542974SPaul B Schroeder 
124195da310eSAlan Cox 	if (break_state == -1)
12423f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
124395da310eSAlan Cox 	else
12443f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
12453f542974SPaul B Schroeder 
12466b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
12473f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
12489c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
12493f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
12503f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
12513f542974SPaul B Schroeder }
12523f542974SPaul B Schroeder 
12533f542974SPaul B Schroeder /*****************************************************************************
12543f542974SPaul B Schroeder  * mos7840_write_room
12553f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
12563f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
12573f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
12583f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
12593f542974SPaul B Schroeder  *****************************************************************************/
12603f542974SPaul B Schroeder 
126195da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
12623f542974SPaul B Schroeder {
126395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
12643f542974SPaul B Schroeder 	int i;
12653f542974SPaul B Schroeder 	int room = 0;
12660de9a702SOliver Neukum 	unsigned long flags;
12673f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12683f542974SPaul B Schroeder 
12699c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
12703f542974SPaul B Schroeder 		return -1;
12713f542974SPaul B Schroeder 
12723f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12739c134a14SGreg Kroah-Hartman 	if (mos7840_port == NULL)
12743f542974SPaul B Schroeder 		return -1;
12753f542974SPaul B Schroeder 
12760de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
12773f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
1278880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
12793f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
12803f542974SPaul B Schroeder 	}
12810de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
12823f542974SPaul B Schroeder 
12830de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
12849c134a14SGreg Kroah-Hartman 	dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
12850de9a702SOliver Neukum 	return room;
12863f542974SPaul B Schroeder 
12873f542974SPaul B Schroeder }
12883f542974SPaul B Schroeder 
12893f542974SPaul B Schroeder /*****************************************************************************
12903f542974SPaul B Schroeder  * mos7840_write
12913f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
12923f542974SPaul B Schroeder  *	the port.
12933f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
12943f542974SPaul B Schroeder  *      return a negative error number.
12953f542974SPaul B Schroeder  *****************************************************************************/
12963f542974SPaul B Schroeder 
129795da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
12983f542974SPaul B Schroeder 			 const unsigned char *data, int count)
12993f542974SPaul B Schroeder {
13003f542974SPaul B Schroeder 	int status;
13013f542974SPaul B Schroeder 	int i;
13023f542974SPaul B Schroeder 	int bytes_sent = 0;
13033f542974SPaul B Schroeder 	int transfer_size;
13040de9a702SOliver Neukum 	unsigned long flags;
13053f542974SPaul B Schroeder 
13063f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13073f542974SPaul B Schroeder 	struct usb_serial *serial;
13083f542974SPaul B Schroeder 	struct urb *urb;
1309880af9dbSAlan Cox 	/* __u16 Data; */
13103f542974SPaul B Schroeder 	const unsigned char *current_position = data;
13113f542974SPaul B Schroeder 	unsigned char *data1;
13123f542974SPaul B Schroeder 
13139c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
13143f542974SPaul B Schroeder 		return -1;
13153f542974SPaul B Schroeder 
13163f542974SPaul B Schroeder 	serial = port->serial;
13179c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
13183f542974SPaul B Schroeder 		return -1;
13193f542974SPaul B Schroeder 
13203f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13219c134a14SGreg Kroah-Hartman 	if (mos7840_port == NULL)
13223f542974SPaul B Schroeder 		return -1;
13233f542974SPaul B Schroeder 
13243f542974SPaul B Schroeder 	/* try to find a free urb in the list */
13253f542974SPaul B Schroeder 	urb = NULL;
13263f542974SPaul B Schroeder 
13270de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
13283f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
13290de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
13300de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
13313f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
13329c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "URB:%d\n", i);
13333f542974SPaul B Schroeder 			break;
13343f542974SPaul B Schroeder 		}
13353f542974SPaul B Schroeder 	}
13360de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
13373f542974SPaul B Schroeder 
13383f542974SPaul B Schroeder 	if (urb == NULL) {
13399c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
13403f542974SPaul B Schroeder 		goto exit;
13413f542974SPaul B Schroeder 	}
13423f542974SPaul B Schroeder 
13433f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
13443b7c7e52SAlexey Khoroshilov 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
13453b7c7e52SAlexey Khoroshilov 					       GFP_ATOMIC);
134610c642d0SJohan Hovold 		if (!urb->transfer_buffer)
13473f542974SPaul B Schroeder 			goto exit;
13483f542974SPaul B Schroeder 	}
13493f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
13503f542974SPaul B Schroeder 
13513f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
13523f542974SPaul B Schroeder 
13533f542974SPaul B Schroeder 	/* fill urb with data and submit  */
13541143832eSGreg Kroah-Hartman 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1355093ea2d3SDonald Lee 		usb_fill_bulk_urb(urb,
1356093ea2d3SDonald Lee 			serial->dev,
1357093ea2d3SDonald Lee 			usb_sndbulkpipe(serial->dev,
1358093ea2d3SDonald Lee 				(port->bulk_out_endpointAddress) + 2),
1359093ea2d3SDonald Lee 			urb->transfer_buffer,
1360093ea2d3SDonald Lee 			transfer_size,
1361093ea2d3SDonald Lee 			mos7840_bulk_out_data_callback, mos7840_port);
1362093ea2d3SDonald Lee 	} else {
13633f542974SPaul B Schroeder 		usb_fill_bulk_urb(urb,
13643f542974SPaul B Schroeder 			serial->dev,
13653f542974SPaul B Schroeder 			usb_sndbulkpipe(serial->dev,
13663f542974SPaul B Schroeder 				port->bulk_out_endpointAddress),
13673f542974SPaul B Schroeder 			urb->transfer_buffer,
13683f542974SPaul B Schroeder 			transfer_size,
13693f542974SPaul B Schroeder 			mos7840_bulk_out_data_callback, mos7840_port);
1370093ea2d3SDonald Lee 	}
13713f542974SPaul B Schroeder 
13723f542974SPaul B Schroeder 	data1 = urb->transfer_buffer;
13739c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
13743f542974SPaul B Schroeder 
137505cf0decSJohan Hovold 	if (mos7840_port->has_led)
137605cf0decSJohan Hovold 		mos7840_led_activity(port);
13770eafe4deSDonald 
13783f542974SPaul B Schroeder 	/* send it down the pipe */
13793f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
13803f542974SPaul B Schroeder 
13813f542974SPaul B Schroeder 	if (status) {
13820de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
138322a416c4SJohan Hovold 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1384194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
13853f542974SPaul B Schroeder 		bytes_sent = status;
13863f542974SPaul B Schroeder 		goto exit;
13873f542974SPaul B Schroeder 	}
13883f542974SPaul B Schroeder 	bytes_sent = transfer_size;
13898c1a07ffSJohan Hovold 	port->icount.tx += transfer_size;
13908c1a07ffSJohan Hovold 	dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
13913f542974SPaul B Schroeder exit:
13923f542974SPaul B Schroeder 	return bytes_sent;
13933f542974SPaul B Schroeder 
13943f542974SPaul B Schroeder }
13953f542974SPaul B Schroeder 
13963f542974SPaul B Schroeder /*****************************************************************************
13973f542974SPaul B Schroeder  * mos7840_throttle
13983f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
13993f542974SPaul B Schroeder  *	being read from the port.
14003f542974SPaul B Schroeder  *****************************************************************************/
14013f542974SPaul B Schroeder 
140295da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
14033f542974SPaul B Schroeder {
140495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14053f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14063f542974SPaul B Schroeder 	int status;
14073f542974SPaul B Schroeder 
14089c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
14093f542974SPaul B Schroeder 		return;
14103f542974SPaul B Schroeder 
14113f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14123f542974SPaul B Schroeder 
14133f542974SPaul B Schroeder 	if (mos7840_port == NULL)
14143f542974SPaul B Schroeder 		return;
14153f542974SPaul B Schroeder 
14163f542974SPaul B Schroeder 	if (!mos7840_port->open) {
14179c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "port not opened\n");
14183f542974SPaul B Schroeder 		return;
14193f542974SPaul B Schroeder 	}
14203f542974SPaul B Schroeder 
14213f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
14223f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
14233f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
142495da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
142595da310eSAlan Cox 		if (status <= 0)
14263f542974SPaul B Schroeder 			return;
14273f542974SPaul B Schroeder 	}
14283f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
14299db276f8SPeter Hurley 	if (C_CRTSCTS(tty)) {
14303f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
143195da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
14323f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
143395da310eSAlan Cox 		if (status < 0)
14343f542974SPaul B Schroeder 			return;
14353f542974SPaul B Schroeder 	}
14363f542974SPaul B Schroeder }
14373f542974SPaul B Schroeder 
14383f542974SPaul B Schroeder /*****************************************************************************
14393f542974SPaul B Schroeder  * mos7840_unthrottle
1440880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
1441880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
1442880af9dbSAlan Cox  *	called)
14433f542974SPaul B Schroeder  *****************************************************************************/
144495da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
14453f542974SPaul B Schroeder {
144695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14473f542974SPaul B Schroeder 	int status;
14483f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
14493f542974SPaul B Schroeder 
14509c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
14513f542974SPaul B Schroeder 		return;
14523f542974SPaul B Schroeder 
14533f542974SPaul B Schroeder 	if (mos7840_port == NULL)
14543f542974SPaul B Schroeder 		return;
14553f542974SPaul B Schroeder 
14563f542974SPaul B Schroeder 	if (!mos7840_port->open) {
14579c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
14583f542974SPaul B Schroeder 		return;
14593f542974SPaul B Schroeder 	}
14603f542974SPaul B Schroeder 
14613f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
14623f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
14633f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
146495da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
146595da310eSAlan Cox 		if (status <= 0)
14663f542974SPaul B Schroeder 			return;
14673f542974SPaul B Schroeder 	}
14683f542974SPaul B Schroeder 
14693f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
14709db276f8SPeter Hurley 	if (C_CRTSCTS(tty)) {
14713f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
147295da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
14733f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
147495da310eSAlan Cox 		if (status < 0)
14753f542974SPaul B Schroeder 			return;
14763f542974SPaul B Schroeder 	}
14773f542974SPaul B Schroeder }
14783f542974SPaul B Schroeder 
147960b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
14803f542974SPaul B Schroeder {
148195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14823f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14833f542974SPaul B Schroeder 	unsigned int result;
14843f542974SPaul B Schroeder 	__u16 msr;
14853f542974SPaul B Schroeder 	__u16 mcr;
1486880af9dbSAlan Cox 	int status;
14873f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14883f542974SPaul B Schroeder 
14893f542974SPaul B Schroeder 	if (mos7840_port == NULL)
14903f542974SPaul B Schroeder 		return -ENODEV;
14913f542974SPaul B Schroeder 
14923f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1493*cd8db057SJohan Hovold 	if (status < 0)
1494a91ccd26SJohan Hovold 		return -EIO;
14953f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1496*cd8db057SJohan Hovold 	if (status < 0)
1497a91ccd26SJohan Hovold 		return -EIO;
14983f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
14993f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
15003f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
15013f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
15023f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
15033f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
15043f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
15053f542974SPaul B Schroeder 
15069c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
15073f542974SPaul B Schroeder 
15083f542974SPaul B Schroeder 	return result;
15093f542974SPaul B Schroeder }
15103f542974SPaul B Schroeder 
151120b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
15123f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
15133f542974SPaul B Schroeder {
151495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
15153f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15163f542974SPaul B Schroeder 	unsigned int mcr;
151787521c46SRoel Kluin 	int status;
15183f542974SPaul B Schroeder 
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 
1524e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
15253f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
15263f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
15273f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
15283f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
15293f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
15303f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
15313f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
15323f542974SPaul B Schroeder 
15333f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
15343f542974SPaul B Schroeder 		mcr |= MCR_RTS;
15353f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
15363f542974SPaul B Schroeder 		mcr |= MCR_DTR;
15373f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
15383f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
15393f542974SPaul B Schroeder 
15403f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
15413f542974SPaul B Schroeder 
15423f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
15433f542974SPaul B Schroeder 	if (status < 0) {
15449c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
154587521c46SRoel Kluin 		return status;
15463f542974SPaul B Schroeder 	}
15473f542974SPaul B Schroeder 
15483f542974SPaul B Schroeder 	return 0;
15493f542974SPaul B Schroeder }
15503f542974SPaul B Schroeder 
15513f542974SPaul B Schroeder /*****************************************************************************
15523f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
15533f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
15543f542974SPaul B Schroeder  *	baud rate.
15553f542974SPaul B Schroeder  *****************************************************************************/
15569c134a14SGreg Kroah-Hartman static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
15579c134a14SGreg Kroah-Hartman 					  int baudRate, int *divisor,
15583f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
15593f542974SPaul B Schroeder {
15609c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
15613f542974SPaul B Schroeder 
15623f542974SPaul B Schroeder 	if (baudRate <= 115200) {
15633f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
15643f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
15653f542974SPaul B Schroeder 	}
15663f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
15673f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
15683f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
15693f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
15703f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
15713f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
15723f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
15733f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
15743f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
15753f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
15763f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
15773f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
15783f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
15793f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
15803f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
15813f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
15823f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
15833f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
15843f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
15853f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
15863f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
15873f542974SPaul B Schroeder 	}
15883f542974SPaul B Schroeder 	return 0;
15893f542974SPaul B Schroeder }
15903f542974SPaul B Schroeder 
15913f542974SPaul B Schroeder /*****************************************************************************
15923f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
15933f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
15943f542974SPaul B Schroeder  *	specified port.
15953f542974SPaul B Schroeder  *****************************************************************************/
15963f542974SPaul B Schroeder 
15973f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
15983f542974SPaul B Schroeder 					    int baudRate)
15993f542974SPaul B Schroeder {
16003f542974SPaul B Schroeder 	int divisor = 0;
16013f542974SPaul B Schroeder 	int status;
16023f542974SPaul B Schroeder 	__u16 Data;
16033f542974SPaul B Schroeder 	unsigned char number;
16043f542974SPaul B Schroeder 	__u16 clk_sel_val;
16053f542974SPaul B Schroeder 	struct usb_serial_port *port;
16063f542974SPaul B Schroeder 
16073f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16083f542974SPaul B Schroeder 		return -1;
16093f542974SPaul B Schroeder 
16109c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
16119c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
16123f542974SPaul B Schroeder 		return -1;
16133f542974SPaul B Schroeder 
16149c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(port->serial, __func__))
16153f542974SPaul B Schroeder 		return -1;
16163f542974SPaul B Schroeder 
16171143832eSGreg Kroah-Hartman 	number = mos7840_port->port->port_number;
16183f542974SPaul B Schroeder 
16191143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1620880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
16213f542974SPaul B Schroeder 	if (baudRate > 115200) {
16223f542974SPaul B Schroeder #ifdef HW_flow_control
1623880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1624880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
16253f542974SPaul B Schroeder 		Data = 0x2b;
16263f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1627880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1628880af9dbSAlan Cox 									Data);
16293f542974SPaul B Schroeder 		if (status < 0) {
16309c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
16313f542974SPaul B Schroeder 			return -1;
16323f542974SPaul B Schroeder 		}
16333f542974SPaul B Schroeder #endif
16343f542974SPaul B Schroeder 
16353f542974SPaul B Schroeder 	} else {
16363f542974SPaul B Schroeder #ifdef HW_flow_control
1637880af9dbSAlan Cox 		/* setting h/w flow control bit to 0 */
16383f542974SPaul B Schroeder 		Data = 0xb;
16393f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1640880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1641880af9dbSAlan Cox 									Data);
16423f542974SPaul B Schroeder 		if (status < 0) {
16439c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
16443f542974SPaul B Schroeder 			return -1;
16453f542974SPaul B Schroeder 		}
16463f542974SPaul B Schroeder #endif
16473f542974SPaul B Schroeder 
16483f542974SPaul B Schroeder 	}
16493f542974SPaul B Schroeder 
1650880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
16513f542974SPaul B Schroeder 		clk_sel_val = 0x0;
16523f542974SPaul B Schroeder 		Data = 0x0;
16539c134a14SGreg Kroah-Hartman 		status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
16543f542974SPaul B Schroeder 						   &clk_sel_val);
1655880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
16563f542974SPaul B Schroeder 								 &Data);
16573f542974SPaul B Schroeder 		if (status < 0) {
16589c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
16593f542974SPaul B Schroeder 			return -1;
16603f542974SPaul B Schroeder 		}
16613f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1662880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1663880af9dbSAlan Cox 								Data);
16643f542974SPaul B Schroeder 		if (status < 0) {
16659c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
16663f542974SPaul B Schroeder 			return -1;
16673f542974SPaul B Schroeder 		}
16683f542974SPaul B Schroeder 		/* Calculate the Divisor */
16693f542974SPaul B Schroeder 
16703f542974SPaul B Schroeder 		if (status) {
1671194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
16723f542974SPaul B Schroeder 			return status;
16733f542974SPaul B Schroeder 		}
16743f542974SPaul B Schroeder 		/* Enable access to divisor latch */
16753f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
16763f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
16773f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
16783f542974SPaul B Schroeder 
16793f542974SPaul B Schroeder 		/* Write the divisor */
16803f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
16819c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
16823f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
16833f542974SPaul B Schroeder 
16843f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
16859c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
16863f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
16873f542974SPaul B Schroeder 
16883f542974SPaul B Schroeder 		/* Disable access to divisor latch */
16893f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
16903f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
16913f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
16923f542974SPaul B Schroeder 
16933f542974SPaul B Schroeder 	}
16943f542974SPaul B Schroeder 	return status;
16953f542974SPaul B Schroeder }
16963f542974SPaul B Schroeder 
16973f542974SPaul B Schroeder /*****************************************************************************
16983f542974SPaul B Schroeder  * mos7840_change_port_settings
16993f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
17003f542974SPaul B Schroeder  *      the specified new settings.
17013f542974SPaul B Schroeder  *****************************************************************************/
17023f542974SPaul B Schroeder 
170395da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
170495da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
17053f542974SPaul B Schroeder {
17063f542974SPaul B Schroeder 	int baud;
17073f542974SPaul B Schroeder 	unsigned cflag;
17083f542974SPaul B Schroeder 	unsigned iflag;
17093f542974SPaul B Schroeder 	__u8 lData;
17103f542974SPaul B Schroeder 	__u8 lParity;
17113f542974SPaul B Schroeder 	__u8 lStop;
17123f542974SPaul B Schroeder 	int status;
17133f542974SPaul B Schroeder 	__u16 Data;
17143f542974SPaul B Schroeder 	struct usb_serial_port *port;
17153f542974SPaul B Schroeder 	struct usb_serial *serial;
17163f542974SPaul B Schroeder 
17173f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17183f542974SPaul B Schroeder 		return;
17193f542974SPaul B Schroeder 
17209c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
17213f542974SPaul B Schroeder 
17229c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
17233f542974SPaul B Schroeder 		return;
17243f542974SPaul B Schroeder 
17259c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(port->serial, __func__))
17263f542974SPaul B Schroeder 		return;
17273f542974SPaul B Schroeder 
17283f542974SPaul B Schroeder 	serial = port->serial;
17293f542974SPaul B Schroeder 
17303f542974SPaul B Schroeder 	if (!mos7840_port->open) {
17319c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
17323f542974SPaul B Schroeder 		return;
17333f542974SPaul B Schroeder 	}
17343f542974SPaul B Schroeder 
17353f542974SPaul B Schroeder 	lData = LCR_BITS_8;
17363f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
17373f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
17383f542974SPaul B Schroeder 
1739adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
1740adc8d746SAlan Cox 	iflag = tty->termios.c_iflag;
17413f542974SPaul B Schroeder 
17423f542974SPaul B Schroeder 	/* Change the number of bits */
17433f542974SPaul B Schroeder 	switch (cflag & CSIZE) {
17443f542974SPaul B Schroeder 	case CS5:
17453f542974SPaul B Schroeder 		lData = LCR_BITS_5;
17463f542974SPaul B Schroeder 		break;
17473f542974SPaul B Schroeder 
17483f542974SPaul B Schroeder 	case CS6:
17493f542974SPaul B Schroeder 		lData = LCR_BITS_6;
17503f542974SPaul B Schroeder 		break;
17513f542974SPaul B Schroeder 
17523f542974SPaul B Schroeder 	case CS7:
17533f542974SPaul B Schroeder 		lData = LCR_BITS_7;
17543f542974SPaul B Schroeder 		break;
175578692cc3SColin Leitner 
17563f542974SPaul B Schroeder 	default:
17573f542974SPaul B Schroeder 	case CS8:
17583f542974SPaul B Schroeder 		lData = LCR_BITS_8;
17593f542974SPaul B Schroeder 		break;
17603f542974SPaul B Schroeder 	}
176178692cc3SColin Leitner 
17623f542974SPaul B Schroeder 	/* Change the Parity bit */
17633f542974SPaul B Schroeder 	if (cflag & PARENB) {
17643f542974SPaul B Schroeder 		if (cflag & PARODD) {
17653f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
17669c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
17673f542974SPaul B Schroeder 		} else {
17683f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
17699c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
17703f542974SPaul B Schroeder 		}
17713f542974SPaul B Schroeder 
17723f542974SPaul B Schroeder 	} else {
17739c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
17743f542974SPaul B Schroeder 	}
17753f542974SPaul B Schroeder 
1776880af9dbSAlan Cox 	if (cflag & CMSPAR)
17773f542974SPaul B Schroeder 		lParity = lParity | 0x20;
17783f542974SPaul B Schroeder 
17793f542974SPaul B Schroeder 	/* Change the Stop bit */
17803f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
17813f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
17829c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
17833f542974SPaul B Schroeder 	} else {
17843f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
17859c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
17863f542974SPaul B Schroeder 	}
17873f542974SPaul B Schroeder 
17883f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
17893f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
17903f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
17913f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
17923f542974SPaul B Schroeder 
17939c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
17943f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
17953f542974SPaul B Schroeder 	/* Disable Interrupts */
17963f542974SPaul B Schroeder 	Data = 0x00;
17973f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
17983f542974SPaul B Schroeder 
17993f542974SPaul B Schroeder 	Data = 0x00;
18003f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
18013f542974SPaul B Schroeder 
18023f542974SPaul B Schroeder 	Data = 0xcf;
18033f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
18043f542974SPaul B Schroeder 
18053f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
18063f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
18073f542974SPaul B Schroeder 
18083f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
18093f542974SPaul B Schroeder 
18103f542974SPaul B Schroeder 	Data = 0x00b;
18113f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
18123f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
18133f542974SPaul B Schroeder 	Data = 0x00b;
18143f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
18153f542974SPaul B Schroeder 
18163f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
18173f542974SPaul B Schroeder 
18183f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
1819880af9dbSAlan Cox 	if (cflag & CBAUD)
18203f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
18213f542974SPaul B Schroeder 
1822880af9dbSAlan Cox 	if (cflag & CRTSCTS)
18233f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
1824880af9dbSAlan Cox 	else
18253f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
18263f542974SPaul B Schroeder 
18273f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
18283f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
18293f542974SPaul B Schroeder 
18303f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
18313f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
18323f542974SPaul B Schroeder 
18333f542974SPaul B Schroeder 	if (!baud) {
18343f542974SPaul B Schroeder 		/* pick a default, any default... */
18359c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "Picked default baud...\n");
18363f542974SPaul B Schroeder 		baud = 9600;
18373f542974SPaul B Schroeder 	}
18383f542974SPaul B Schroeder 
18399c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
18403f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
18413f542974SPaul B Schroeder 
18423f542974SPaul B Schroeder 	/* Enable Interrupts */
18433f542974SPaul B Schroeder 	Data = 0x0c;
18443f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
18453f542974SPaul B Schroeder 
1846ce9d8562SMathieu OTHACEHE 	if (!mos7840_port->read_urb_busy) {
184750de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
18489d380190SJohan Hovold 		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
18493f542974SPaul B Schroeder 		if (status) {
18509c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
18513f542974SPaul B Schroeder 			    status);
185250de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
18533f542974SPaul B Schroeder 		}
18543f542974SPaul B Schroeder 	}
18559c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
18563f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
18573f542974SPaul B Schroeder }
18583f542974SPaul B Schroeder 
18593f542974SPaul B Schroeder /*****************************************************************************
18603f542974SPaul B Schroeder  * mos7840_set_termios
18613f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
18623f542974SPaul B Schroeder  *	the termios structure
18633f542974SPaul B Schroeder  *****************************************************************************/
18643f542974SPaul B Schroeder 
186595da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
186695da310eSAlan Cox 				struct usb_serial_port *port,
1867606d099cSAlan Cox 				struct ktermios *old_termios)
18683f542974SPaul B Schroeder {
18693f542974SPaul B Schroeder 	int status;
18703f542974SPaul B Schroeder 	unsigned int cflag;
18713f542974SPaul B Schroeder 	struct usb_serial *serial;
18723f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
18733363155bSGreg Kroah-Hartman 
18749c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
18753f542974SPaul B Schroeder 		return;
18763f542974SPaul B Schroeder 
18773f542974SPaul B Schroeder 	serial = port->serial;
18783f542974SPaul B Schroeder 
18799c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
18803f542974SPaul B Schroeder 		return;
18813f542974SPaul B Schroeder 
18823f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
18833f542974SPaul B Schroeder 
18843f542974SPaul B Schroeder 	if (mos7840_port == NULL)
18853f542974SPaul B Schroeder 		return;
18863f542974SPaul B Schroeder 
18873f542974SPaul B Schroeder 	if (!mos7840_port->open) {
18889c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
18893f542974SPaul B Schroeder 		return;
18903f542974SPaul B Schroeder 	}
18913f542974SPaul B Schroeder 
18929c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s", "setting termios - \n");
18933f542974SPaul B Schroeder 
1894adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
18953f542974SPaul B Schroeder 
18969c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
1897adc8d746SAlan Cox 		tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
18989c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
18993f542974SPaul B Schroeder 		old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
19003f542974SPaul B Schroeder 
19013f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
19023f542974SPaul B Schroeder 
190395da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
19043f542974SPaul B Schroeder 
19053f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
19069c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
19073f542974SPaul B Schroeder 		return;
19083f542974SPaul B Schroeder 	}
19093f542974SPaul B Schroeder 
1910ce9d8562SMathieu OTHACEHE 	if (!mos7840_port->read_urb_busy) {
191150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
19129d380190SJohan Hovold 		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
19133f542974SPaul B Schroeder 		if (status) {
19149c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
19153f542974SPaul B Schroeder 			    status);
191650de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
19173f542974SPaul B Schroeder 		}
19183f542974SPaul B Schroeder 	}
19193f542974SPaul B Schroeder }
19203f542974SPaul B Schroeder 
19213f542974SPaul B Schroeder /*****************************************************************************
19223f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
19233f542974SPaul B Schroeder  *
19243f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
19253f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
19263f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
19273f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
19283f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
19293f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
19303f542974SPaul B Schroeder  *****************************************************************************/
19313f542974SPaul B Schroeder 
193295da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
193397c4965dSAl Viro 				unsigned int __user *value)
19343f542974SPaul B Schroeder {
19353f542974SPaul B Schroeder 	int count;
19363f542974SPaul B Schroeder 	unsigned int result = 0;
19373f542974SPaul B Schroeder 
193895da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
19399c134a14SGreg Kroah-Hartman 	if (count == 0)
19403f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
19413f542974SPaul B Schroeder 
19423f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
19433f542974SPaul B Schroeder 		return -EFAULT;
19443f542974SPaul B Schroeder 	return 0;
19453f542974SPaul B Schroeder }
19463f542974SPaul B Schroeder 
19473f542974SPaul B Schroeder /*****************************************************************************
19483f542974SPaul B Schroeder  * mos7840_get_serial_info
19493f542974SPaul B Schroeder  *      function to get information about serial port
19503f542974SPaul B Schroeder  *****************************************************************************/
19513f542974SPaul B Schroeder 
19523f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
195397c4965dSAl Viro 				   struct serial_struct __user *retinfo)
19543f542974SPaul B Schroeder {
19553f542974SPaul B Schroeder 	struct serial_struct tmp;
19563f542974SPaul B Schroeder 
19573f542974SPaul B Schroeder 	if (mos7840_port == NULL)
19583f542974SPaul B Schroeder 		return -1;
19593f542974SPaul B Schroeder 
19603f542974SPaul B Schroeder 	memset(&tmp, 0, sizeof(tmp));
19613f542974SPaul B Schroeder 
19623f542974SPaul B Schroeder 	tmp.type = PORT_16550A;
1963e5b1e206SGreg Kroah-Hartman 	tmp.line = mos7840_port->port->minor;
19641143832eSGreg Kroah-Hartman 	tmp.port = mos7840_port->port->port_number;
19653f542974SPaul B Schroeder 	tmp.irq = 0;
19663f542974SPaul B Schroeder 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
19673f542974SPaul B Schroeder 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
19683f542974SPaul B Schroeder 	tmp.baud_base = 9600;
19693f542974SPaul B Schroeder 	tmp.close_delay = 5 * HZ;
19703f542974SPaul B Schroeder 	tmp.closing_wait = 30 * HZ;
19713f542974SPaul B Schroeder 
19723f542974SPaul B Schroeder 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
19733f542974SPaul B Schroeder 		return -EFAULT;
19743f542974SPaul B Schroeder 	return 0;
19753f542974SPaul B Schroeder }
19763f542974SPaul B Schroeder 
19773f542974SPaul B Schroeder /*****************************************************************************
19783f542974SPaul B Schroeder  * SerialIoctl
19793f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
19803f542974SPaul B Schroeder  *****************************************************************************/
19813f542974SPaul B Schroeder 
198200a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
19833f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
19843f542974SPaul B Schroeder {
198595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
198697c4965dSAl Viro 	void __user *argp = (void __user *)arg;
19873f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
19883f542974SPaul B Schroeder 
19899c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
19903f542974SPaul B Schroeder 		return -1;
19913f542974SPaul B Schroeder 
19923f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
19933f542974SPaul B Schroeder 
19943f542974SPaul B Schroeder 	if (mos7840_port == NULL)
19953f542974SPaul B Schroeder 		return -1;
19963f542974SPaul B Schroeder 
19973f542974SPaul B Schroeder 	switch (cmd) {
19983f542974SPaul B Schroeder 		/* return number of bytes available */
19993f542974SPaul B Schroeder 
20003f542974SPaul B Schroeder 	case TIOCSERGETLSR:
20019c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
200295da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
20033f542974SPaul B Schroeder 
20043f542974SPaul B Schroeder 	case TIOCGSERIAL:
20059c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
200697c4965dSAl Viro 		return mos7840_get_serial_info(mos7840_port, argp);
20073f542974SPaul B Schroeder 
20083f542974SPaul B Schroeder 	case TIOCSSERIAL:
20099c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
20103f542974SPaul B Schroeder 		break;
20113f542974SPaul B Schroeder 	default:
20123f542974SPaul B Schroeder 		break;
20133f542974SPaul B Schroeder 	}
20143f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
20153f542974SPaul B Schroeder }
20163f542974SPaul B Schroeder 
20170eafe4deSDonald static int mos7810_check(struct usb_serial *serial)
20183f542974SPaul B Schroeder {
20190eafe4deSDonald 	int i, pass_count = 0;
202015ee89c3SJohan Hovold 	u8 *buf;
20210eafe4deSDonald 	__u16 data = 0, mcr_data = 0;
20220eafe4deSDonald 	__u16 test_pattern = 0x55AA;
202315ee89c3SJohan Hovold 	int res;
202415ee89c3SJohan Hovold 
202515ee89c3SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
202615ee89c3SJohan Hovold 	if (!buf)
202715ee89c3SJohan Hovold 		return 0;	/* failed to identify 7810 */
20283f542974SPaul B Schroeder 
20290eafe4deSDonald 	/* Store MCR setting */
203015ee89c3SJohan Hovold 	res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
20310eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
203215ee89c3SJohan Hovold 		buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
203315ee89c3SJohan Hovold 	if (res == VENDOR_READ_LENGTH)
203415ee89c3SJohan Hovold 		mcr_data = *buf;
20350eafe4deSDonald 
20360eafe4deSDonald 	for (i = 0; i < 16; i++) {
20370eafe4deSDonald 		/* Send the 1-bit test pattern out to MCS7810 test pin */
20380eafe4deSDonald 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
20390eafe4deSDonald 			MCS_WRREQ, MCS_WR_RTYPE,
20400eafe4deSDonald 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
20410eafe4deSDonald 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
20420eafe4deSDonald 
20430eafe4deSDonald 		/* Read the test pattern back */
204415ee89c3SJohan Hovold 		res = usb_control_msg(serial->dev,
204515ee89c3SJohan Hovold 				usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
204615ee89c3SJohan Hovold 				MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2047093ea2d3SDonald Lee 				VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
204815ee89c3SJohan Hovold 		if (res == VENDOR_READ_LENGTH)
204915ee89c3SJohan Hovold 			data = *buf;
2050093ea2d3SDonald Lee 
20510eafe4deSDonald 		/* If this is a MCS7810 device, both test patterns must match */
20520eafe4deSDonald 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
20530eafe4deSDonald 			break;
20540eafe4deSDonald 
20550eafe4deSDonald 		pass_count++;
20563f542974SPaul B Schroeder 	}
2057093ea2d3SDonald Lee 
20580eafe4deSDonald 	/* Restore MCR setting */
20590eafe4deSDonald 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
20600eafe4deSDonald 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
20610eafe4deSDonald 		0, MOS_WDR_TIMEOUT);
20620eafe4deSDonald 
206315ee89c3SJohan Hovold 	kfree(buf);
206415ee89c3SJohan Hovold 
20650eafe4deSDonald 	if (pass_count == 16)
20660eafe4deSDonald 		return 1;
20670eafe4deSDonald 
20680eafe4deSDonald 	return 0;
20690eafe4deSDonald }
20700eafe4deSDonald 
207140c24f28SJohan Hovold static int mos7840_probe(struct usb_serial *serial,
207240c24f28SJohan Hovold 				const struct usb_device_id *id)
20730eafe4deSDonald {
2074d551ec9bSJohan Hovold 	u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
207515ee89c3SJohan Hovold 	u8 *buf;
207640c24f28SJohan Hovold 	int device_type;
207740c24f28SJohan Hovold 
207840c24f28SJohan Hovold 	if (product == MOSCHIP_DEVICE_ID_7810 ||
207940c24f28SJohan Hovold 		product == MOSCHIP_DEVICE_ID_7820) {
208040c24f28SJohan Hovold 		device_type = product;
208140c24f28SJohan Hovold 		goto out;
208240c24f28SJohan Hovold 	}
20830eafe4deSDonald 
208415ee89c3SJohan Hovold 	buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
208540c24f28SJohan Hovold 	if (!buf)
208640c24f28SJohan Hovold 		return -ENOMEM;
208740c24f28SJohan Hovold 
20880eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
208915ee89c3SJohan Hovold 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
20900eafe4deSDonald 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
20910eafe4deSDonald 
20920eafe4deSDonald 	/* For a MCS7840 device GPIO0 must be set to 1 */
209340c24f28SJohan Hovold 	if (buf[0] & 0x01)
20940eafe4deSDonald 		device_type = MOSCHIP_DEVICE_ID_7840;
20950eafe4deSDonald 	else if (mos7810_check(serial))
20960eafe4deSDonald 		device_type = MOSCHIP_DEVICE_ID_7810;
20970eafe4deSDonald 	else
20980eafe4deSDonald 		device_type = MOSCHIP_DEVICE_ID_7820;
209940c24f28SJohan Hovold 
210040c24f28SJohan Hovold 	kfree(buf);
210140c24f28SJohan Hovold out:
2102683a0e4dSJohan Hovold 	usb_set_serial_data(serial, (void *)(unsigned long)device_type);
210340c24f28SJohan Hovold 
210440c24f28SJohan Hovold 	return 0;
21050eafe4deSDonald }
21060eafe4deSDonald 
210740c24f28SJohan Hovold static int mos7840_calc_num_ports(struct usb_serial *serial)
210840c24f28SJohan Hovold {
2109683a0e4dSJohan Hovold 	int device_type = (unsigned long)usb_get_serial_data(serial);
211040c24f28SJohan Hovold 	int mos7840_num_ports;
211140c24f28SJohan Hovold 
21120eafe4deSDonald 	mos7840_num_ports = (device_type >> 4) & 0x000F;
21130eafe4deSDonald 
21143f542974SPaul B Schroeder 	return mos7840_num_ports;
21153f542974SPaul B Schroeder }
21163f542974SPaul B Schroeder 
21175c75633eSJohan Hovold static int mos7840_attach(struct usb_serial *serial)
21185c75633eSJohan Hovold {
21195c75633eSJohan Hovold 	if (serial->num_bulk_in < serial->num_ports ||
21205c75633eSJohan Hovold 			serial->num_bulk_out < serial->num_ports) {
21215c75633eSJohan Hovold 		dev_err(&serial->interface->dev, "missing endpoints\n");
21225c75633eSJohan Hovold 		return -ENODEV;
21235c75633eSJohan Hovold 	}
21245c75633eSJohan Hovold 
21255c75633eSJohan Hovold 	return 0;
21265c75633eSJohan Hovold }
21275c75633eSJohan Hovold 
212880c00750SJohan Hovold static int mos7840_port_probe(struct usb_serial_port *port)
21293f542974SPaul B Schroeder {
213080c00750SJohan Hovold 	struct usb_serial *serial = port->serial;
2131683a0e4dSJohan Hovold 	int device_type = (unsigned long)usb_get_serial_data(serial);
21323f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
213380c00750SJohan Hovold 	int status;
213480c00750SJohan Hovold 	int pnum;
21353f542974SPaul B Schroeder 	__u16 Data;
21363f542974SPaul B Schroeder 
21373f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
21383f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
21393f542974SPaul B Schroeder 
21401143832eSGreg Kroah-Hartman 	pnum = port->port_number;
214180c00750SJohan Hovold 
2142ae685effSJohan Hovold 	dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
21437ac9da10SBurman Yan 	mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
214410c642d0SJohan Hovold 	if (!mos7840_port)
214580c00750SJohan Hovold 		return -ENOMEM;
21463f542974SPaul B Schroeder 
2147880af9dbSAlan Cox 	/* Initialize all port interrupt end point to port 0 int
2148880af9dbSAlan Cox 	 * endpoint. Our device has only one interrupt end point
2149880af9dbSAlan Cox 	 * common to all port */
21503f542974SPaul B Schroeder 
215180c00750SJohan Hovold 	mos7840_port->port = port;
215280c00750SJohan Hovold 	mos7840_set_port_private(port, mos7840_port);
21530de9a702SOliver Neukum 	spin_lock_init(&mos7840_port->pool_lock);
21543f542974SPaul B Schroeder 
215537768adfSTony Cook 	/* minor is not initialised until later by
215637768adfSTony Cook 	 * usb-serial.c:get_free_serial() and cannot therefore be used
215737768adfSTony Cook 	 * to index device instances */
215880c00750SJohan Hovold 	mos7840_port->port_num = pnum + 1;
2159e5b1e206SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
216080c00750SJohan Hovold 	dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
21613f542974SPaul B Schroeder 
21623f542974SPaul B Schroeder 	if (mos7840_port->port_num == 1) {
21633f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0x0;
21643f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0x1;
21653f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x4;
2166ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
21673f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0x8;
21683f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0x9;
21693f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x16;
2170ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
21713f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0xa;
21723f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0xb;
21733f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x19;
2174ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
21753f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0xa;
21763f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0xb;
21773f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x19;
2178ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
21793f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0xc;
21803f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0xd;
21813f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x1c;
21823f542974SPaul B Schroeder 	}
218380c00750SJohan Hovold 	mos7840_dump_serial_port(port, mos7840_port);
218480c00750SJohan Hovold 	mos7840_set_port_private(port, mos7840_port);
21853f542974SPaul B Schroeder 
2186880af9dbSAlan Cox 	/* enable rx_disable bit in control register */
218780c00750SJohan Hovold 	status = mos7840_get_reg_sync(port,
21883f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, &Data);
21893f542974SPaul B Schroeder 	if (status < 0) {
219080c00750SJohan Hovold 		dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2191ae685effSJohan Hovold 		goto out;
21923f542974SPaul B Schroeder 	} else
219380c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2194880af9dbSAlan Cox 	Data |= 0x08;	/* setting driver done bit */
2195880af9dbSAlan Cox 	Data |= 0x04;	/* sp1_bit to have cts change reflect in
2196880af9dbSAlan Cox 			   modem status reg */
21973f542974SPaul B Schroeder 
2198880af9dbSAlan Cox 	/* Data |= 0x20; //rx_disable bit */
219980c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
22003f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, Data);
22013f542974SPaul B Schroeder 	if (status < 0) {
220280c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2203ae685effSJohan Hovold 		goto out;
22043f542974SPaul B Schroeder 	} else
220580c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
22063f542974SPaul B Schroeder 
2207880af9dbSAlan Cox 	/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2208880af9dbSAlan Cox 	   and 0x24 in DCR3 */
22093f542974SPaul B Schroeder 	Data = 0x01;
221080c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
2211880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 0), Data);
22123f542974SPaul B Schroeder 	if (status < 0) {
221380c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2214ae685effSJohan Hovold 		goto out;
22153f542974SPaul B Schroeder 	} else
221680c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
22173f542974SPaul B Schroeder 
22183f542974SPaul B Schroeder 	Data = 0x05;
221980c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
2220880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 1), Data);
22213f542974SPaul B Schroeder 	if (status < 0) {
222280c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2223ae685effSJohan Hovold 		goto out;
22243f542974SPaul B Schroeder 	} else
222580c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
22263f542974SPaul B Schroeder 
22273f542974SPaul B Schroeder 	Data = 0x24;
222880c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
2229880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 2), Data);
22303f542974SPaul B Schroeder 	if (status < 0) {
223180c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2232ae685effSJohan Hovold 		goto out;
22333f542974SPaul B Schroeder 	} else
223480c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
22353f542974SPaul B Schroeder 
2236880af9dbSAlan Cox 	/* write values in clkstart0x0 and clkmulti 0x20 */
22373f542974SPaul B Schroeder 	Data = 0x0;
2238ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
22393f542974SPaul B Schroeder 	if (status < 0) {
224080c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2241ae685effSJohan Hovold 		goto out;
22423f542974SPaul B Schroeder 	} else
224380c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
22443f542974SPaul B Schroeder 
22453f542974SPaul B Schroeder 	Data = 0x20;
2246ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
22473f542974SPaul B Schroeder 	if (status < 0) {
224880c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
22490de9a702SOliver Neukum 		goto error;
22503f542974SPaul B Schroeder 	} else
225180c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
22523f542974SPaul B Schroeder 
2253880af9dbSAlan Cox 	/* write value 0x0 to scratchpad register */
22543f542974SPaul B Schroeder 	Data = 0x00;
2255ae685effSJohan Hovold 	status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
22563f542974SPaul B Schroeder 	if (status < 0) {
225780c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2258ae685effSJohan Hovold 		goto out;
22593f542974SPaul B Schroeder 	} else
226080c00750SJohan Hovold 		dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
22613f542974SPaul B Schroeder 
2262880af9dbSAlan Cox 	/* Zero Length flag register */
2263ae685effSJohan Hovold 	if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
22643f542974SPaul B Schroeder 		Data = 0xff;
226580c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
22663f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
2267880af9dbSAlan Cox 					((__u16)mos7840_port->port_num)), Data);
226880c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
22699c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
22703f542974SPaul B Schroeder 		if (status < 0) {
227180c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2272ae685effSJohan Hovold 			goto out;
22733f542974SPaul B Schroeder 		} else
227480c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
22753f542974SPaul B Schroeder 	} else {
22763f542974SPaul B Schroeder 		Data = 0xff;
227780c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
22783f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
2279880af9dbSAlan Cox 					((__u16)mos7840_port->port_num) - 0x1), Data);
228080c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
22819c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
22823f542974SPaul B Schroeder 		if (status < 0) {
228380c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2284ae685effSJohan Hovold 			goto out;
22853f542974SPaul B Schroeder 		} else
228680c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
22873f542974SPaul B Schroeder 
22883f542974SPaul B Schroeder 	}
22890de9a702SOliver Neukum 	mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
22903f542974SPaul B Schroeder 	mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2291880af9dbSAlan Cox 	mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2292880af9dbSAlan Cox 			GFP_KERNEL);
2293880af9dbSAlan Cox 	if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2294880af9dbSAlan Cox 			!mos7840_port->dr) {
22950de9a702SOliver Neukum 		status = -ENOMEM;
22960de9a702SOliver Neukum 		goto error;
22970de9a702SOliver Neukum 	}
22980eafe4deSDonald 
22990eafe4deSDonald 	mos7840_port->has_led = false;
23000eafe4deSDonald 
23010eafe4deSDonald 	/* Initialize LED timers */
23020eafe4deSDonald 	if (device_type == MOSCHIP_DEVICE_ID_7810) {
23030eafe4deSDonald 		mos7840_port->has_led = true;
23040eafe4deSDonald 
230505cf0decSJohan Hovold 		mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
230605cf0decSJohan Hovold 		mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
230705cf0decSJohan Hovold 								GFP_KERNEL);
230805cf0decSJohan Hovold 		if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
230905cf0decSJohan Hovold 			status = -ENOMEM;
231005cf0decSJohan Hovold 			goto error;
231105cf0decSJohan Hovold 		}
231205cf0decSJohan Hovold 
2313f05b7cb6SVaishali Thakkar 		setup_timer(&mos7840_port->led_timer1, mos7840_led_off,
2314f05b7cb6SVaishali Thakkar 			    (unsigned long)mos7840_port);
23150eafe4deSDonald 		mos7840_port->led_timer1.expires =
23160eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_ON_MS);
2317f05b7cb6SVaishali Thakkar 		setup_timer(&mos7840_port->led_timer2, mos7840_led_flag_off,
2318f05b7cb6SVaishali Thakkar 			    (unsigned long)mos7840_port);
23190eafe4deSDonald 		mos7840_port->led_timer2.expires =
23200eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_OFF_MS);
23210eafe4deSDonald 
23220eafe4deSDonald 		/* Turn off LED */
2323ae685effSJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
23240eafe4deSDonald 	}
2325ae685effSJohan Hovold out:
232680c00750SJohan Hovold 	if (pnum == serial->num_ports - 1) {
2327880af9dbSAlan Cox 		/* Zero Length flag enable */
23283f542974SPaul B Schroeder 		Data = 0x0f;
23293f542974SPaul B Schroeder 		status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
23303f542974SPaul B Schroeder 		if (status < 0) {
233180c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
23327ced46c3SRoel Kluin 			goto error;
23333f542974SPaul B Schroeder 		} else
233480c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
23353f542974SPaul B Schroeder 
23363f542974SPaul B Schroeder 		/* setting configuration feature to one */
23373f542974SPaul B Schroeder 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
233880c00750SJohan Hovold 				0x03, 0x00, 0x01, 0x00, NULL, 0x00,
233980c00750SJohan Hovold 				MOS_WDR_TIMEOUT);
234080c00750SJohan Hovold 	}
23413f542974SPaul B Schroeder 	return 0;
23420de9a702SOliver Neukum error:
234305cf0decSJohan Hovold 	kfree(mos7840_port->led_dr);
234405cf0decSJohan Hovold 	usb_free_urb(mos7840_port->led_urb);
23450de9a702SOliver Neukum 	kfree(mos7840_port->dr);
23460de9a702SOliver Neukum 	kfree(mos7840_port->ctrl_buf);
23470de9a702SOliver Neukum 	usb_free_urb(mos7840_port->control_urb);
23480de9a702SOliver Neukum 	kfree(mos7840_port);
234980c00750SJohan Hovold 
23500de9a702SOliver Neukum 	return status;
23513f542974SPaul B Schroeder }
23523f542974SPaul B Schroeder 
235380c00750SJohan Hovold static int mos7840_port_remove(struct usb_serial_port *port)
23543f542974SPaul B Schroeder {
23553f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
23563f542974SPaul B Schroeder 
235780c00750SJohan Hovold 	mos7840_port = mos7840_get_port_private(port);
23583f542974SPaul B Schroeder 
23590eafe4deSDonald 	if (mos7840_port->has_led) {
23600eafe4deSDonald 		/* Turn off LED */
236180c00750SJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
23620eafe4deSDonald 
23630eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer1);
23640eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer2);
236505cf0decSJohan Hovold 
236605cf0decSJohan Hovold 		usb_kill_urb(mos7840_port->led_urb);
236705cf0decSJohan Hovold 		usb_free_urb(mos7840_port->led_urb);
236805cf0decSJohan Hovold 		kfree(mos7840_port->led_dr);
23690eafe4deSDonald 	}
237080c00750SJohan Hovold 	usb_kill_urb(mos7840_port->control_urb);
237165a4cdbbSJohan Hovold 	usb_free_urb(mos7840_port->control_urb);
23720de9a702SOliver Neukum 	kfree(mos7840_port->ctrl_buf);
23730de9a702SOliver Neukum 	kfree(mos7840_port->dr);
23743f542974SPaul B Schroeder 	kfree(mos7840_port);
237580c00750SJohan Hovold 
237680c00750SJohan Hovold 	return 0;
23773f542974SPaul B Schroeder }
23783f542974SPaul B Schroeder 
23793f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
23803f542974SPaul B Schroeder 	.driver = {
23813f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
23823f542974SPaul B Schroeder 		   .name = "mos7840",
23833f542974SPaul B Schroeder 		   },
23843f542974SPaul B Schroeder 	.description = DRIVER_DESC,
238568e24113SGreg Kroah-Hartman 	.id_table = id_table,
23863f542974SPaul B Schroeder 	.num_ports = 4,
23873f542974SPaul B Schroeder 	.open = mos7840_open,
23883f542974SPaul B Schroeder 	.close = mos7840_close,
23893f542974SPaul B Schroeder 	.write = mos7840_write,
23903f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
23913f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
23923f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
23933f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
23943f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
239540c24f28SJohan Hovold 	.probe = mos7840_probe,
23963f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
23973f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
23983f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
23993f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
24003f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
24010c613371SJohan Hovold 	.tiocmiwait = usb_serial_generic_tiocmiwait,
24028c1a07ffSJohan Hovold 	.get_icount = usb_serial_generic_get_icount,
24035c75633eSJohan Hovold 	.attach = mos7840_attach,
240480c00750SJohan Hovold 	.port_probe = mos7840_port_probe,
240580c00750SJohan Hovold 	.port_remove = mos7840_port_remove,
24063f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
24073f542974SPaul B Schroeder 	.read_int_callback = mos7840_interrupt_callback,
24083f542974SPaul B Schroeder };
24093f542974SPaul B Schroeder 
24104d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
24114d2a7affSAlan Stern 	&moschip7840_4port_device, NULL
24124d2a7affSAlan Stern };
24134d2a7affSAlan Stern 
241468e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
24153f542974SPaul B Schroeder 
24163f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
24173f542974SPaul B Schroeder MODULE_LICENSE("GPL");
2418