xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision 7b2faede671adc0a7edbb5f6b1e228aa8b62f7c0)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
23f542974SPaul B Schroeder /*
33f542974SPaul B Schroeder  * Clean ups from Moschip version and a few ioctl implementations by:
43f542974SPaul B Schroeder  *	Paul B Schroeder <pschroeder "at" uplogix "dot" com>
53f542974SPaul B Schroeder  *
63f542974SPaul B Schroeder  * Originally based on drivers/usb/serial/io_edgeport.c which is:
73f542974SPaul B Schroeder  *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
83f542974SPaul B Schroeder  *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
93f542974SPaul B Schroeder  *
103f542974SPaul B Schroeder  */
113f542974SPaul B Schroeder 
123f542974SPaul B Schroeder #include <linux/kernel.h>
133f542974SPaul B Schroeder #include <linux/errno.h>
143f542974SPaul B Schroeder #include <linux/slab.h>
153f542974SPaul B Schroeder #include <linux/tty.h>
163f542974SPaul B Schroeder #include <linux/tty_driver.h>
173f542974SPaul B Schroeder #include <linux/tty_flip.h>
183f542974SPaul B Schroeder #include <linux/module.h>
193f542974SPaul B Schroeder #include <linux/serial.h>
203f542974SPaul B Schroeder #include <linux/usb.h>
213f542974SPaul B Schroeder #include <linux/usb/serial.h>
22880af9dbSAlan Cox #include <linux/uaccess.h>
233f542974SPaul B Schroeder 
243f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
253f542974SPaul B Schroeder 
263f542974SPaul B Schroeder /*
273f542974SPaul B Schroeder  * 16C50 UART register defines
283f542974SPaul B Schroeder  */
293f542974SPaul B Schroeder 
303f542974SPaul B Schroeder #define LCR_BITS_5             0x00	/* 5 bits/char */
313f542974SPaul B Schroeder #define LCR_BITS_6             0x01	/* 6 bits/char */
323f542974SPaul B Schroeder #define LCR_BITS_7             0x02	/* 7 bits/char */
333f542974SPaul B Schroeder #define LCR_BITS_8             0x03	/* 8 bits/char */
343f542974SPaul B Schroeder #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
353f542974SPaul B Schroeder 
363f542974SPaul B Schroeder #define LCR_STOP_1             0x00	/* 1 stop bit */
373f542974SPaul B Schroeder #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
383f542974SPaul B Schroeder #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
393f542974SPaul B Schroeder #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
403f542974SPaul B Schroeder 
413f542974SPaul B Schroeder #define LCR_PAR_NONE           0x00	/* No parity */
423f542974SPaul B Schroeder #define LCR_PAR_ODD            0x08	/* Odd parity */
433f542974SPaul B Schroeder #define LCR_PAR_EVEN           0x18	/* Even parity */
443f542974SPaul B Schroeder #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
453f542974SPaul B Schroeder #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
463f542974SPaul B Schroeder #define LCR_PAR_MASK           0x38	/* Mask for parity field */
473f542974SPaul B Schroeder 
483f542974SPaul B Schroeder #define LCR_SET_BREAK          0x40	/* Set Break condition */
493f542974SPaul B Schroeder #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
503f542974SPaul B Schroeder 
513f542974SPaul B Schroeder #define MCR_DTR                0x01	/* Assert DTR */
523f542974SPaul B Schroeder #define MCR_RTS                0x02	/* Assert RTS */
533f542974SPaul B Schroeder #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
543f542974SPaul B Schroeder #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
553f542974SPaul B Schroeder #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
563f542974SPaul B Schroeder #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
573f542974SPaul B Schroeder 
583f542974SPaul B Schroeder #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
593f542974SPaul B Schroeder #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
603f542974SPaul B Schroeder #define MOS7840_MSR_RI         0x40	/* Current state of RI */
613f542974SPaul B Schroeder #define MOS7840_MSR_CD         0x80	/* Current state of CD */
623f542974SPaul B Schroeder 
633f542974SPaul B Schroeder /*
643f542974SPaul B Schroeder  * Defines used for sending commands to port
653f542974SPaul B Schroeder  */
663f542974SPaul B Schroeder 
671e658489SMark Ferrell #define MOS_WDR_TIMEOUT		5000	/* default urb timeout */
683f542974SPaul B Schroeder 
693f542974SPaul B Schroeder #define MOS_PORT1       0x0200
703f542974SPaul B Schroeder #define MOS_PORT2       0x0300
713f542974SPaul B Schroeder #define MOS_VENREG      0x0000
723f542974SPaul B Schroeder #define MOS_MAX_PORT	0x02
733f542974SPaul B Schroeder #define MOS_WRITE       0x0E
743f542974SPaul B Schroeder #define MOS_READ        0x0D
753f542974SPaul B Schroeder 
763f542974SPaul B Schroeder /* Requests */
773f542974SPaul B Schroeder #define MCS_RD_RTYPE    0xC0
783f542974SPaul B Schroeder #define MCS_WR_RTYPE    0x40
793f542974SPaul B Schroeder #define MCS_RDREQ       0x0D
803f542974SPaul B Schroeder #define MCS_WRREQ       0x0E
813f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT        500
823f542974SPaul B Schroeder #define VENDOR_READ_LENGTH      (0x01)
833f542974SPaul B Schroeder 
843f542974SPaul B Schroeder #define MAX_NAME_LEN    64
853f542974SPaul B Schroeder 
86880af9dbSAlan Cox #define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
87880af9dbSAlan Cox #define ZLP_REG5  0x3E		/* Zero_Flag_Reg5    62 */
883f542974SPaul B Schroeder 
893f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */
903f542974SPaul B Schroeder #define TIOCEXBAUD     0x5462
913f542974SPaul B Schroeder 
92375cb533SJohan Hovold /*
93375cb533SJohan Hovold  * Vendor id and device id defines
94375cb533SJohan Hovold  *
95375cb533SJohan Hovold  * NOTE: Do not add new defines, add entries directly to the id_table instead.
9611e1abb4SDavid Ludlow  */
9711e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB              0x0856
98acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
99870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
100acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
101870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
102acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
103acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
104acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
105acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
10611e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
107caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
108870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
109870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
110870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL2_4        0xAC24
1113f542974SPaul B Schroeder 
11211e1abb4SDavid Ludlow /* Interrupt Routine Defines    */
1133f542974SPaul B Schroeder 
1143f542974SPaul B Schroeder #define SERIAL_IIR_RLS      0x06
1153f542974SPaul B Schroeder #define SERIAL_IIR_MS       0x00
1163f542974SPaul B Schroeder 
1173f542974SPaul B Schroeder /*
1183f542974SPaul B Schroeder  *  Emulation of the bit mask on the LINE STATUS REGISTER.
1193f542974SPaul B Schroeder  */
1203f542974SPaul B Schroeder #define SERIAL_LSR_DR       0x0001
1213f542974SPaul B Schroeder #define SERIAL_LSR_OE       0x0002
1223f542974SPaul B Schroeder #define SERIAL_LSR_PE       0x0004
1233f542974SPaul B Schroeder #define SERIAL_LSR_FE       0x0008
1243f542974SPaul B Schroeder #define SERIAL_LSR_BI       0x0010
1253f542974SPaul B Schroeder 
1263f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS   0x10
1273f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR   0x20
1283f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI    0x40
1293f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD    0x80
1303f542974SPaul B Schroeder 
131880af9dbSAlan Cox /* Serial Port register Address */
1323f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
1333f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
1343f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
1353f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
1363f542974SPaul B Schroeder #define LINE_STATUS_REGISTER       ((__u16)(0x05))
1373f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
1383f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
1393f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
1403f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
1413f542974SPaul B Schroeder 
1423f542974SPaul B Schroeder #define CLK_MULTI_REGISTER         ((__u16)(0x02))
1433f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
144093ea2d3SDonald Lee #define GPIO_REGISTER              ((__u16)(0x07))
1453f542974SPaul B Schroeder 
1463f542974SPaul B Schroeder #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
1473f542974SPaul B Schroeder 
1483f542974SPaul B Schroeder /*
1493f542974SPaul B Schroeder  * URB POOL related defines
1503f542974SPaul B Schroeder  */
1513f542974SPaul B Schroeder #define NUM_URBS                        16	/* URB Count */
1523f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
1533f542974SPaul B Schroeder 
1540eafe4deSDonald /* LED on/off milliseconds*/
1550eafe4deSDonald #define LED_ON_MS	500
1560eafe4deSDonald #define LED_OFF_MS	500
1570eafe4deSDonald 
158d8a083ccSJohan Hovold enum mos7840_flag {
15905cf0decSJohan Hovold 	MOS7840_FLAG_LED_BUSY,
160d8a083ccSJohan Hovold };
1613f542974SPaul B Schroeder 
162375cb533SJohan Hovold #define MCS_PORT_MASK	GENMASK(2, 0)
163375cb533SJohan Hovold #define MCS_PORTS(nr)	((nr) & MCS_PORT_MASK)
164375cb533SJohan Hovold #define MCS_LED		BIT(3)
165375cb533SJohan Hovold 
166375cb533SJohan Hovold #define MCS_DEVICE(vid, pid, flags) \
167375cb533SJohan Hovold 		USB_DEVICE((vid), (pid)), .driver_info = (flags)
168375cb533SJohan Hovold 
169b9c87663STony Zelenoff static const struct usb_device_id id_table[] = {
170375cb533SJohan Hovold 	{ MCS_DEVICE(0x0557, 0x2011, MCS_PORTS(4)) },	/* ATEN UC2324 */
171375cb533SJohan Hovold 	{ MCS_DEVICE(0x0557, 0x7820, MCS_PORTS(2)) },	/* ATEN UC2322 */
172375cb533SJohan Hovold 	{ MCS_DEVICE(0x110a, 0x2210, MCS_PORTS(2)) },	/* Moxa UPort 2210 */
173375cb533SJohan Hovold 	{ MCS_DEVICE(0x9710, 0x7810, MCS_PORTS(1) | MCS_LED) }, /* ASIX MCS7810 */
174375cb533SJohan Hovold 	{ MCS_DEVICE(0x9710, 0x7820, MCS_PORTS(2)) },	/* MosChip MCS7820 */
175375cb533SJohan Hovold 	{ MCS_DEVICE(0x9710, 0x7840, MCS_PORTS(4)) },	/* MosChip MCS7840 */
176375cb533SJohan Hovold 	{ MCS_DEVICE(0x9710, 0x7843, MCS_PORTS(3)) },	/* ASIX MCS7840 3 port */
177acf509aeSCliff Brake 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2) },
178870408c8SDave Ludlow 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P) },
179acf509aeSCliff Brake 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4) },
180870408c8SDave Ludlow 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P) },
181acf509aeSCliff Brake 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2) },
182acf509aeSCliff Brake 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4) },
183acf509aeSCliff Brake 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2) },
184acf509aeSCliff Brake 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4) },
18511e1abb4SDavid Ludlow 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2) },
186870408c8SDave Ludlow 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P) },
187acf509aeSCliff Brake 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4) },
188870408c8SDave Ludlow 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P) },
18927f1281dSBlaise Gassend 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4) },
1903f542974SPaul B Schroeder 	{}			/* terminating entry */
1913f542974SPaul B Schroeder };
19268e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
1933f542974SPaul B Schroeder 
1943f542974SPaul B Schroeder /* This structure holds all of the local port information */
1953f542974SPaul B Schroeder 
1963f542974SPaul B Schroeder struct moschip_port {
1973f542974SPaul B Schroeder 	int port_num;		/*Actual port number in the device(1,2,etc) */
1983f542974SPaul B Schroeder 	struct urb *read_urb;	/* read URB for this port */
1993f542974SPaul B Schroeder 	__u8 shadowLCR;		/* last LCR value received */
2003f542974SPaul B Schroeder 	__u8 shadowMCR;		/* last MCR value received */
2013f542974SPaul B Schroeder 	char open;
2023f542974SPaul B Schroeder 	struct usb_serial_port *port;	/* loop back to the owner of this object */
2033f542974SPaul B Schroeder 
2043f542974SPaul B Schroeder 	/* Offsets */
2053f542974SPaul B Schroeder 	__u8 SpRegOffset;
2063f542974SPaul B Schroeder 	__u8 ControlRegOffset;
2073f542974SPaul B Schroeder 	__u8 DcrRegOffset;
2083f542974SPaul B Schroeder 
2090de9a702SOliver Neukum 	spinlock_t pool_lock;
2103f542974SPaul B Schroeder 	struct urb *write_urb_pool[NUM_URBS];
2110de9a702SOliver Neukum 	char busy[NUM_URBS];
21250de36f7SGreg Kroah-Hartman 	bool read_urb_busy;
2133f542974SPaul B Schroeder 
2140eafe4deSDonald 	/* For device(s) with LED indicator */
2150eafe4deSDonald 	bool has_led;
2160eafe4deSDonald 	struct timer_list led_timer1;	/* Timer for LED on */
2170eafe4deSDonald 	struct timer_list led_timer2;	/* Timer for LED off */
21805cf0decSJohan Hovold 	struct urb *led_urb;
21905cf0decSJohan Hovold 	struct usb_ctrlrequest *led_dr;
220d8a083ccSJohan Hovold 
221d8a083ccSJohan Hovold 	unsigned long flags;
2220eafe4deSDonald };
2233f542974SPaul B Schroeder 
2243f542974SPaul B Schroeder /*
2253f542974SPaul B Schroeder  * mos7840_set_reg_sync
2263f542974SPaul B Schroeder  * 	To set the Control register by calling usb_fill_control_urb function
2273f542974SPaul B Schroeder  *	by passing usb_sndctrlpipe function as parameter.
2283f542974SPaul B Schroeder  */
2293f542974SPaul B Schroeder 
2303f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2313f542974SPaul B Schroeder 				__u16 val)
2323f542974SPaul B Schroeder {
2333f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2343f542974SPaul B Schroeder 	val = val & 0x00ff;
2359c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
2363f542974SPaul B Schroeder 
2373f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2383f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2393f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2403f542974SPaul B Schroeder }
2413f542974SPaul B Schroeder 
2423f542974SPaul B Schroeder /*
2433f542974SPaul B Schroeder  * mos7840_get_reg_sync
2443f542974SPaul B Schroeder  * 	To set the Uart register by calling usb_fill_control_urb function by
2453f542974SPaul B Schroeder  *	passing usb_rcvctrlpipe function as parameter.
2463f542974SPaul B Schroeder  */
2473f542974SPaul B Schroeder 
2483f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
2493f542974SPaul B Schroeder 				__u16 *val)
2503f542974SPaul B Schroeder {
2513f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2523f542974SPaul B Schroeder 	int ret = 0;
2539e221a35SJohan Hovold 	u8 *buf;
2549e221a35SJohan Hovold 
2559e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2569e221a35SJohan Hovold 	if (!buf)
2579e221a35SJohan Hovold 		return -ENOMEM;
2583f542974SPaul B Schroeder 
2593f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
2609e221a35SJohan Hovold 			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
2613f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
262cd8db057SJohan Hovold 	if (ret < VENDOR_READ_LENGTH) {
263cd8db057SJohan Hovold 		if (ret >= 0)
264cd8db057SJohan Hovold 			ret = -EIO;
265cd8db057SJohan Hovold 		goto out;
266cd8db057SJohan Hovold 	}
267cd8db057SJohan Hovold 
2689e221a35SJohan Hovold 	*val = buf[0];
2699c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
270cd8db057SJohan Hovold out:
2719e221a35SJohan Hovold 	kfree(buf);
2723f542974SPaul B Schroeder 	return ret;
2733f542974SPaul B Schroeder }
2743f542974SPaul B Schroeder 
2753f542974SPaul B Schroeder /*
2763f542974SPaul B Schroeder  * mos7840_set_uart_reg
2773f542974SPaul B Schroeder  *	To set the Uart register by calling usb_fill_control_urb function by
2783f542974SPaul B Schroeder  *	passing usb_sndctrlpipe function as parameter.
2793f542974SPaul B Schroeder  */
2803f542974SPaul B Schroeder 
2813f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
2823f542974SPaul B Schroeder 				__u16 val)
2833f542974SPaul B Schroeder {
2843f542974SPaul B Schroeder 
2853f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2863f542974SPaul B Schroeder 	val = val & 0x00ff;
287880af9dbSAlan Cox 	/* For the UART control registers, the application number need
288880af9dbSAlan Cox 	   to be Or'ed */
289e8603076SJackyChou 	if (port->serial->num_ports == 2 && port->port_number != 0)
2901143832eSGreg Kroah-Hartman 		val |= ((__u16)port->port_number + 2) << 8;
291e8603076SJackyChou 	else
292e8603076SJackyChou 		val |= ((__u16)port->port_number + 1) << 8;
2939c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
2943f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2953f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2963f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2973f542974SPaul B Schroeder 
2983f542974SPaul B Schroeder }
2993f542974SPaul B Schroeder 
3003f542974SPaul B Schroeder /*
3013f542974SPaul B Schroeder  * mos7840_get_uart_reg
3023f542974SPaul B Schroeder  *	To set the Control register by calling usb_fill_control_urb function
3033f542974SPaul B Schroeder  *	by passing usb_rcvctrlpipe function as parameter.
3043f542974SPaul B Schroeder  */
3053f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3063f542974SPaul B Schroeder 				__u16 *val)
3073f542974SPaul B Schroeder {
3083f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3093f542974SPaul B Schroeder 	int ret = 0;
3103f542974SPaul B Schroeder 	__u16 Wval;
3119e221a35SJohan Hovold 	u8 *buf;
3129e221a35SJohan Hovold 
3139e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3149e221a35SJohan Hovold 	if (!buf)
3159e221a35SJohan Hovold 		return -ENOMEM;
3163f542974SPaul B Schroeder 
3173f542974SPaul B Schroeder 	/* Wval  is same as application number */
318e8603076SJackyChou 	if (port->serial->num_ports == 2 && port->port_number != 0)
3191143832eSGreg Kroah-Hartman 		Wval = ((__u16)port->port_number + 2) << 8;
320e8603076SJackyChou 	else
321e8603076SJackyChou 		Wval = ((__u16)port->port_number + 1) << 8;
3229c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
3233f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3249e221a35SJohan Hovold 			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
3253f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
326cd8db057SJohan Hovold 	if (ret < VENDOR_READ_LENGTH) {
327cd8db057SJohan Hovold 		if (ret >= 0)
328cd8db057SJohan Hovold 			ret = -EIO;
329cd8db057SJohan Hovold 		goto out;
330cd8db057SJohan Hovold 	}
3319e221a35SJohan Hovold 	*val = buf[0];
332cd8db057SJohan Hovold out:
3339e221a35SJohan Hovold 	kfree(buf);
3343f542974SPaul B Schroeder 	return ret;
3353f542974SPaul B Schroeder }
3363f542974SPaul B Schroeder 
3379c134a14SGreg Kroah-Hartman static void mos7840_dump_serial_port(struct usb_serial_port *port,
3389c134a14SGreg Kroah-Hartman 				     struct moschip_port *mos7840_port)
3393f542974SPaul B Schroeder {
3403f542974SPaul B Schroeder 
3419c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
3429c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
3439c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
3443f542974SPaul B Schroeder 
3453f542974SPaul B Schroeder }
3463f542974SPaul B Schroeder 
3473f542974SPaul B Schroeder /************************************************************************/
3483f542974SPaul B Schroeder /************************************************************************/
3493f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
3503f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
3513f542974SPaul B Schroeder /************************************************************************/
3523f542974SPaul B Schroeder /************************************************************************/
3533f542974SPaul B Schroeder 
3540eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb)
3550eafe4deSDonald {
3560eafe4deSDonald 	switch (urb->status) {
3570eafe4deSDonald 	case 0:
3580eafe4deSDonald 		/* Success */
3590eafe4deSDonald 		break;
3600eafe4deSDonald 	case -ECONNRESET:
3610eafe4deSDonald 	case -ENOENT:
3620eafe4deSDonald 	case -ESHUTDOWN:
3630eafe4deSDonald 		/* This urb is terminated, clean up */
364d9a38a87SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
3659c134a14SGreg Kroah-Hartman 			__func__, urb->status);
3660eafe4deSDonald 		break;
3670eafe4deSDonald 	default:
368d9a38a87SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
3699c134a14SGreg Kroah-Hartman 			__func__, urb->status);
3700eafe4deSDonald 	}
3710eafe4deSDonald }
3720eafe4deSDonald 
3730eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
3740eafe4deSDonald 				__u16 reg)
3750eafe4deSDonald {
3760eafe4deSDonald 	struct usb_device *dev = mcs->port->serial->dev;
37705cf0decSJohan Hovold 	struct usb_ctrlrequest *dr = mcs->led_dr;
3780eafe4deSDonald 
3790eafe4deSDonald 	dr->bRequestType = MCS_WR_RTYPE;
3800eafe4deSDonald 	dr->bRequest = MCS_WRREQ;
3810eafe4deSDonald 	dr->wValue = cpu_to_le16(wval);
3820eafe4deSDonald 	dr->wIndex = cpu_to_le16(reg);
3830eafe4deSDonald 	dr->wLength = cpu_to_le16(0);
3840eafe4deSDonald 
38505cf0decSJohan Hovold 	usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
3860eafe4deSDonald 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
3870eafe4deSDonald 
38805cf0decSJohan Hovold 	usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
3890eafe4deSDonald }
3900eafe4deSDonald 
3910eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
3920eafe4deSDonald 				__u16 val)
3930eafe4deSDonald {
3940eafe4deSDonald 	struct usb_device *dev = port->serial->dev;
3950eafe4deSDonald 
3960eafe4deSDonald 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
3970eafe4deSDonald 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
3980eafe4deSDonald }
3990eafe4deSDonald 
400e99e88a9SKees Cook static void mos7840_led_off(struct timer_list *t)
4010eafe4deSDonald {
402e99e88a9SKees Cook 	struct moschip_port *mcs = from_timer(mcs, t, led_timer1);
4030eafe4deSDonald 
4040eafe4deSDonald 	/* Turn off LED */
4050eafe4deSDonald 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
4060eafe4deSDonald 	mod_timer(&mcs->led_timer2,
4070eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_OFF_MS));
4080eafe4deSDonald }
4090eafe4deSDonald 
410e99e88a9SKees Cook static void mos7840_led_flag_off(struct timer_list *t)
4110eafe4deSDonald {
412e99e88a9SKees Cook 	struct moschip_port *mcs = from_timer(mcs, t, led_timer2);
4130eafe4deSDonald 
41405cf0decSJohan Hovold 	clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
41505cf0decSJohan Hovold }
41605cf0decSJohan Hovold 
41705cf0decSJohan Hovold static void mos7840_led_activity(struct usb_serial_port *port)
41805cf0decSJohan Hovold {
41905cf0decSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
42005cf0decSJohan Hovold 
42105cf0decSJohan Hovold 	if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
42205cf0decSJohan Hovold 		return;
42305cf0decSJohan Hovold 
42405cf0decSJohan Hovold 	mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
42505cf0decSJohan Hovold 	mod_timer(&mos7840_port->led_timer1,
42605cf0decSJohan Hovold 				jiffies + msecs_to_jiffies(LED_ON_MS));
4270eafe4deSDonald }
4280eafe4deSDonald 
4293f542974SPaul B Schroeder /*****************************************************************************
4303f542974SPaul B Schroeder  * mos7840_bulk_in_callback
4313f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
4323f542974SPaul B Schroeder  *	bulk in endpoint.
4333f542974SPaul B Schroeder  *****************************************************************************/
4343f542974SPaul B Schroeder 
4357d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
4363f542974SPaul B Schroeder {
4373ec9fb6fSJohan Hovold 	struct moschip_port *mos7840_port = urb->context;
438ce039bd4SJohan Hovold 	struct usb_serial_port *port = mos7840_port->port;
4390643c724SGreg Kroah-Hartman 	int retval;
4403f542974SPaul B Schroeder 	unsigned char *data;
4410643c724SGreg Kroah-Hartman 	int status = urb->status;
4423f542974SPaul B Schroeder 
44350de36f7SGreg Kroah-Hartman 	if (status) {
4449c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
44550de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
4463f542974SPaul B Schroeder 		return;
4473f542974SPaul B Schroeder 	}
4483f542974SPaul B Schroeder 
4493f542974SPaul B Schroeder 	data = urb->transfer_buffer;
45059d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
4513f542974SPaul B Schroeder 
4523f542974SPaul B Schroeder 	if (urb->actual_length) {
45305c7cd39SJiri Slaby 		struct tty_port *tport = &mos7840_port->port->port;
45405c7cd39SJiri Slaby 		tty_insert_flip_string(tport, data, urb->actual_length);
4552e124b4aSJiri Slaby 		tty_flip_buffer_push(tport);
4568c1a07ffSJohan Hovold 		port->icount.rx += urb->actual_length;
4578c1a07ffSJohan Hovold 		dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
4583f542974SPaul B Schroeder 	}
4593f542974SPaul B Schroeder 
4603f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
4619c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
46250de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
4633f542974SPaul B Schroeder 		return;
4643f542974SPaul B Schroeder 	}
4653f542974SPaul B Schroeder 
46605cf0decSJohan Hovold 	if (mos7840_port->has_led)
46705cf0decSJohan Hovold 		mos7840_led_activity(port);
4680de9a702SOliver Neukum 
46950de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
4700643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
4713f542974SPaul B Schroeder 
4720643c724SGreg Kroah-Hartman 	if (retval) {
4739c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
47450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
4753f542974SPaul B Schroeder 	}
4763f542974SPaul B Schroeder }
4773f542974SPaul B Schroeder 
4783f542974SPaul B Schroeder /*****************************************************************************
4793f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
480880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
481880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
4823f542974SPaul B Schroeder  *****************************************************************************/
4833f542974SPaul B Schroeder 
4847d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
4853f542974SPaul B Schroeder {
486ce039bd4SJohan Hovold 	struct moschip_port *mos7840_port = urb->context;
487ce039bd4SJohan Hovold 	struct usb_serial_port *port = mos7840_port->port;
4880643c724SGreg Kroah-Hartman 	int status = urb->status;
48919bfbf46SJohn Ogness 	unsigned long flags;
4900de9a702SOliver Neukum 	int i;
4910de9a702SOliver Neukum 
49219bfbf46SJohn Ogness 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
4930de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
4940de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
4950de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
4960de9a702SOliver Neukum 			break;
4970de9a702SOliver Neukum 		}
4980de9a702SOliver Neukum 	}
49919bfbf46SJohn Ogness 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
5000de9a702SOliver Neukum 
5010643c724SGreg Kroah-Hartman 	if (status) {
5029c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
5033f542974SPaul B Schroeder 		return;
5043f542974SPaul B Schroeder 	}
5053f542974SPaul B Schroeder 
5066aad04f2SJiri Slaby 	if (mos7840_port->open)
5076aad04f2SJiri Slaby 		tty_port_tty_wakeup(&port->port);
5083f542974SPaul B Schroeder 
5093f542974SPaul B Schroeder }
5103f542974SPaul B Schroeder 
5113f542974SPaul B Schroeder /************************************************************************/
5123f542974SPaul 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       */
5133f542974SPaul B Schroeder /************************************************************************/
5143f542974SPaul B Schroeder 
5153f542974SPaul B Schroeder /*****************************************************************************
5163f542974SPaul B Schroeder  * mos7840_open
5173f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
5183f542974SPaul B Schroeder  *	If successful, we return 0
5193f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
5203f542974SPaul B Schroeder  *****************************************************************************/
5213f542974SPaul B Schroeder 
522a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
5233f542974SPaul B Schroeder {
524*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
525ce039bd4SJohan Hovold 	struct usb_serial *serial = port->serial;
5263f542974SPaul B Schroeder 	int response;
5273f542974SPaul B Schroeder 	int j;
5283f542974SPaul B Schroeder 	struct urb *urb;
5293f542974SPaul B Schroeder 	__u16 Data;
5303f542974SPaul B Schroeder 	int status;
5313f542974SPaul B Schroeder 
5323f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
5333f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
5343f542974SPaul B Schroeder 
5353f542974SPaul B Schroeder 	/* Initialising the write urb pool */
5363f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
5370de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
5383f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
53910c642d0SJohan Hovold 		if (!urb)
5403f542974SPaul B Schroeder 			continue;
5413f542974SPaul B Schroeder 
542880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
543880af9dbSAlan Cox 								GFP_KERNEL);
5443f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
5450de9a702SOliver Neukum 			usb_free_urb(urb);
5460de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
5473f542974SPaul B Schroeder 			continue;
5483f542974SPaul B Schroeder 		}
5493f542974SPaul B Schroeder 	}
5503f542974SPaul B Schroeder 
5513f542974SPaul B Schroeder /*****************************************************************************
5523f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
5533f542974SPaul B Schroeder  *
5543f542974SPaul B Schroeder  * Register Index
5553f542974SPaul B Schroeder  * 1 : IER
5563f542974SPaul B Schroeder  * 2 : FCR
5573f542974SPaul B Schroeder  * 3 : LCR
5583f542974SPaul B Schroeder  * 4 : MCR
5593f542974SPaul B Schroeder  *
5603f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
5613f542974SPaul B Schroeder  *****************************************************************************/
5623f542974SPaul B Schroeder 
563880af9dbSAlan Cox 	/* NEED to check the following Block */
5643f542974SPaul B Schroeder 
5653f542974SPaul B Schroeder 	Data = 0x0;
5663f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
5673f542974SPaul B Schroeder 	if (status < 0) {
5689c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Spreg failed\n");
5695f8a2e68SJohan Hovold 		goto err;
5703f542974SPaul B Schroeder 	}
5713f542974SPaul B Schroeder 	Data |= 0x80;
5723f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
5733f542974SPaul B Schroeder 	if (status < 0) {
5749c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
5755f8a2e68SJohan Hovold 		goto err;
5763f542974SPaul B Schroeder 	}
5773f542974SPaul B Schroeder 
5783f542974SPaul B Schroeder 	Data &= ~0x80;
5793f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
5803f542974SPaul B Schroeder 	if (status < 0) {
5819c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
5825f8a2e68SJohan Hovold 		goto err;
5833f542974SPaul B Schroeder 	}
584880af9dbSAlan Cox 	/* End of block to be checked */
5853f542974SPaul B Schroeder 
5863f542974SPaul B Schroeder 	Data = 0x0;
587880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
588880af9dbSAlan Cox 									&Data);
5893f542974SPaul B Schroeder 	if (status < 0) {
5909c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Controlreg failed\n");
5915f8a2e68SJohan Hovold 		goto err;
5923f542974SPaul B Schroeder 	}
593880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
594880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
595880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
596880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
5973f542974SPaul B Schroeder 	if (status < 0) {
5989c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Controlreg failed\n");
5995f8a2e68SJohan Hovold 		goto err;
6003f542974SPaul B Schroeder 	}
601880af9dbSAlan Cox 	/* do register settings here */
602880af9dbSAlan Cox 	/* Set all regs to the device default values. */
603880af9dbSAlan Cox 	/***********************************
604880af9dbSAlan Cox 	 * First Disable all interrupts.
605880af9dbSAlan Cox 	 ***********************************/
6063f542974SPaul B Schroeder 	Data = 0x00;
6073f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
6083f542974SPaul B Schroeder 	if (status < 0) {
6099c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "disabling interrupts failed\n");
6105f8a2e68SJohan Hovold 		goto err;
6113f542974SPaul B Schroeder 	}
612880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
6133f542974SPaul B Schroeder 	Data = 0x00;
6143f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
6153f542974SPaul B Schroeder 	if (status < 0) {
6169c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
6175f8a2e68SJohan Hovold 		goto err;
6183f542974SPaul B Schroeder 	}
6193f542974SPaul B Schroeder 
6203f542974SPaul B Schroeder 	Data = 0xcf;
6213f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
6223f542974SPaul B Schroeder 	if (status < 0) {
6239c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
6245f8a2e68SJohan Hovold 		goto err;
6253f542974SPaul B Schroeder 	}
6263f542974SPaul B Schroeder 
6273f542974SPaul B Schroeder 	Data = 0x03;
6283f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
6293f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
6303f542974SPaul B Schroeder 
6313f542974SPaul B Schroeder 	Data = 0x0b;
6323f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
6333f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
6343f542974SPaul B Schroeder 
6353f542974SPaul B Schroeder 	Data = 0x00;
6363f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
6373f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
6383f542974SPaul B Schroeder 
639880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
6403f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
6413f542974SPaul B Schroeder 
6423f542974SPaul B Schroeder 	Data = 0x0c;
6433f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
6443f542974SPaul B Schroeder 
6453f542974SPaul B Schroeder 	Data = 0x0;
6463f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
6473f542974SPaul B Schroeder 
6483f542974SPaul B Schroeder 	Data = 0x00;
6493f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
6503f542974SPaul B Schroeder 
6513f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
6523f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
6533f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
6543f542974SPaul B Schroeder 
655880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
6563f542974SPaul B Schroeder 	Data = 0x0;
6573f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
6583f542974SPaul B Schroeder 
6593f542974SPaul B Schroeder 	Data = Data | 0x0c;
6603f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
6613f542974SPaul B Schroeder 
6623f542974SPaul B Schroeder 	Data = Data & ~0x0c;
6633f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
664880af9dbSAlan Cox 	/* Finally enable all interrupts */
6653f542974SPaul B Schroeder 	Data = 0x0c;
6663f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
6673f542974SPaul B Schroeder 
668880af9dbSAlan Cox 	/* clearing rx_disable */
6693f542974SPaul B Schroeder 	Data = 0x0;
670880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
671880af9dbSAlan Cox 									&Data);
6723f542974SPaul B Schroeder 	Data = Data & ~0x20;
673880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
674880af9dbSAlan Cox 									Data);
6753f542974SPaul B Schroeder 
676880af9dbSAlan Cox 	/* rx_negate */
6773f542974SPaul B Schroeder 	Data = 0x0;
678880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
679880af9dbSAlan Cox 									&Data);
6803f542974SPaul B Schroeder 	Data = Data | 0x10;
681880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
682880af9dbSAlan Cox 									Data);
6833f542974SPaul B Schroeder 
6841143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "port number is %d\n", port->port_number);
685e5b1e206SGreg Kroah-Hartman 	dev_dbg(&port->dev, "minor number is %d\n", port->minor);
6869c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
6879c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
6889c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
6899c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
6903f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
6913f542974SPaul B Schroeder 
6923f542974SPaul B Schroeder 	/* set up our bulk in urb */
6931143832eSGreg Kroah-Hartman 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
694093ea2d3SDonald Lee 		usb_fill_bulk_urb(mos7840_port->read_urb,
695093ea2d3SDonald Lee 			serial->dev,
696093ea2d3SDonald Lee 			usb_rcvbulkpipe(serial->dev,
697093ea2d3SDonald Lee 				(port->bulk_in_endpointAddress) + 2),
698093ea2d3SDonald Lee 			port->bulk_in_buffer,
699093ea2d3SDonald Lee 			mos7840_port->read_urb->transfer_buffer_length,
700093ea2d3SDonald Lee 			mos7840_bulk_in_callback, mos7840_port);
701093ea2d3SDonald Lee 	} else {
7023f542974SPaul B Schroeder 		usb_fill_bulk_urb(mos7840_port->read_urb,
7033f542974SPaul B Schroeder 			serial->dev,
7043f542974SPaul B Schroeder 			usb_rcvbulkpipe(serial->dev,
7053f542974SPaul B Schroeder 				port->bulk_in_endpointAddress),
7063f542974SPaul B Schroeder 			port->bulk_in_buffer,
7073f542974SPaul B Schroeder 			mos7840_port->read_urb->transfer_buffer_length,
7083f542974SPaul B Schroeder 			mos7840_bulk_in_callback, mos7840_port);
709093ea2d3SDonald Lee 	}
7103f542974SPaul B Schroeder 
7119c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
71250de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
7133f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
7143f542974SPaul B Schroeder 	if (response) {
715194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
716194343d9SGreg Kroah-Hartman 			__func__, response);
71750de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7183f542974SPaul B Schroeder 	}
7193f542974SPaul B Schroeder 
7203f542974SPaul B Schroeder 	/* initialize our port settings */
721880af9dbSAlan Cox 	/* Must set to enable ints! */
722880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
7233f542974SPaul B Schroeder 	/* send a open port command */
7243f542974SPaul B Schroeder 	mos7840_port->open = 1;
725880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
7263f542974SPaul B Schroeder 
7273f542974SPaul B Schroeder 	return 0;
7285f8a2e68SJohan Hovold err:
7295f8a2e68SJohan Hovold 	for (j = 0; j < NUM_URBS; ++j) {
7305f8a2e68SJohan Hovold 		urb = mos7840_port->write_urb_pool[j];
7315f8a2e68SJohan Hovold 		if (!urb)
7325f8a2e68SJohan Hovold 			continue;
7335f8a2e68SJohan Hovold 		kfree(urb->transfer_buffer);
7345f8a2e68SJohan Hovold 		usb_free_urb(urb);
7355f8a2e68SJohan Hovold 	}
7365f8a2e68SJohan Hovold 	return status;
7373f542974SPaul B Schroeder }
7383f542974SPaul B Schroeder 
7393f542974SPaul B Schroeder /*****************************************************************************
7403f542974SPaul B Schroeder  * mos7840_chars_in_buffer
7413f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
7423f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
7433f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
7443f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
7453f542974SPaul B Schroeder  *	system,
74695da310eSAlan Cox  *	Otherwise we return zero.
7473f542974SPaul B Schroeder  *****************************************************************************/
7483f542974SPaul B Schroeder 
74995da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
7503f542974SPaul B Schroeder {
75195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
752*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
7533f542974SPaul B Schroeder 	int i;
7543f542974SPaul B Schroeder 	int chars = 0;
7550de9a702SOliver Neukum 	unsigned long flags;
7563f542974SPaul B Schroeder 
7570de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
7585c263b92SMark Ferrell 	for (i = 0; i < NUM_URBS; ++i) {
7595c263b92SMark Ferrell 		if (mos7840_port->busy[i]) {
7605c263b92SMark Ferrell 			struct urb *urb = mos7840_port->write_urb_pool[i];
7615c263b92SMark Ferrell 			chars += urb->transfer_buffer_length;
7625c263b92SMark Ferrell 		}
7635c263b92SMark Ferrell 	}
7640de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
7659c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
7660de9a702SOliver Neukum 	return chars;
7673f542974SPaul B Schroeder 
7683f542974SPaul B Schroeder }
7693f542974SPaul B Schroeder 
7703f542974SPaul B Schroeder /*****************************************************************************
7713f542974SPaul B Schroeder  * mos7840_close
7723f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
7733f542974SPaul B Schroeder  *****************************************************************************/
7743f542974SPaul B Schroeder 
775335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
7763f542974SPaul B Schroeder {
777*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
7783f542974SPaul B Schroeder 	int j;
7793f542974SPaul B Schroeder 	__u16 Data;
7803f542974SPaul B Schroeder 
7813f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
7823f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
7833f542974SPaul B Schroeder 
7843f542974SPaul B Schroeder 	/* Freeing Write URBs */
7853f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
7863f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
78791c72df1SFabian Frederick 			kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
7883f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
7893f542974SPaul B Schroeder 		}
7903f542974SPaul B Schroeder 	}
7913f542974SPaul B Schroeder 
7923f542974SPaul B Schroeder 	usb_kill_urb(mos7840_port->read_urb);
79350de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = false;
794bb3529c6SJohan Hovold 
7953f542974SPaul B Schroeder 	Data = 0x0;
7963f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
7973f542974SPaul B Schroeder 
7983f542974SPaul B Schroeder 	Data = 0x00;
7993f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
8003f542974SPaul B Schroeder 
8013f542974SPaul B Schroeder 	mos7840_port->open = 0;
8023f542974SPaul B Schroeder }
8033f542974SPaul B Schroeder 
8043f542974SPaul B Schroeder /*****************************************************************************
8053f542974SPaul B Schroeder  * mos7840_break
8063f542974SPaul B Schroeder  *	this function sends a break to the port
8073f542974SPaul B Schroeder  *****************************************************************************/
80895da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
8093f542974SPaul B Schroeder {
81095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
811*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
8123f542974SPaul B Schroeder 	unsigned char data;
8133f542974SPaul B Schroeder 
81495da310eSAlan Cox 	if (break_state == -1)
8153f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
81695da310eSAlan Cox 	else
8173f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
8183f542974SPaul B Schroeder 
8196b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
8203f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
8219c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
8223f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
8233f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
8243f542974SPaul B Schroeder }
8253f542974SPaul B Schroeder 
8263f542974SPaul B Schroeder /*****************************************************************************
8273f542974SPaul B Schroeder  * mos7840_write_room
8283f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
8293f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
8303f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
8313f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
8323f542974SPaul B Schroeder  *****************************************************************************/
8333f542974SPaul B Schroeder 
83495da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
8353f542974SPaul B Schroeder {
83695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
837*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
8383f542974SPaul B Schroeder 	int i;
8393f542974SPaul B Schroeder 	int room = 0;
8400de9a702SOliver Neukum 	unsigned long flags;
8413f542974SPaul B Schroeder 
8420de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
8433f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
844880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
8453f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
8463f542974SPaul B Schroeder 	}
8470de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
8483f542974SPaul B Schroeder 
8490de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
8509c134a14SGreg Kroah-Hartman 	dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
8510de9a702SOliver Neukum 	return room;
8523f542974SPaul B Schroeder 
8533f542974SPaul B Schroeder }
8543f542974SPaul B Schroeder 
8553f542974SPaul B Schroeder /*****************************************************************************
8563f542974SPaul B Schroeder  * mos7840_write
8573f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
8583f542974SPaul B Schroeder  *	the port.
8593f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
8603f542974SPaul B Schroeder  *      return a negative error number.
8613f542974SPaul B Schroeder  *****************************************************************************/
8623f542974SPaul B Schroeder 
86395da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
8643f542974SPaul B Schroeder 			 const unsigned char *data, int count)
8653f542974SPaul B Schroeder {
866*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
867ce039bd4SJohan Hovold 	struct usb_serial *serial = port->serial;
8683f542974SPaul B Schroeder 	int status;
8693f542974SPaul B Schroeder 	int i;
8703f542974SPaul B Schroeder 	int bytes_sent = 0;
8713f542974SPaul B Schroeder 	int transfer_size;
8720de9a702SOliver Neukum 	unsigned long flags;
8733f542974SPaul B Schroeder 	struct urb *urb;
874880af9dbSAlan Cox 	/* __u16 Data; */
8753f542974SPaul B Schroeder 	const unsigned char *current_position = data;
8763f542974SPaul B Schroeder 
8773f542974SPaul B Schroeder 	/* try to find a free urb in the list */
8783f542974SPaul B Schroeder 	urb = NULL;
8793f542974SPaul B Schroeder 
8800de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
8813f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
8820de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
8830de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
8843f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
8859c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "URB:%d\n", i);
8863f542974SPaul B Schroeder 			break;
8873f542974SPaul B Schroeder 		}
8883f542974SPaul B Schroeder 	}
8890de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
8903f542974SPaul B Schroeder 
8913f542974SPaul B Schroeder 	if (urb == NULL) {
8929c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
8933f542974SPaul B Schroeder 		goto exit;
8943f542974SPaul B Schroeder 	}
8953f542974SPaul B Schroeder 
8963f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
8973b7c7e52SAlexey Khoroshilov 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
8983b7c7e52SAlexey Khoroshilov 					       GFP_ATOMIC);
89910c642d0SJohan Hovold 		if (!urb->transfer_buffer)
9003f542974SPaul B Schroeder 			goto exit;
9013f542974SPaul B Schroeder 	}
9023f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
9033f542974SPaul B Schroeder 
9043f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
9053f542974SPaul B Schroeder 
9063f542974SPaul B Schroeder 	/* fill urb with data and submit  */
9071143832eSGreg Kroah-Hartman 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
908093ea2d3SDonald Lee 		usb_fill_bulk_urb(urb,
909093ea2d3SDonald Lee 			serial->dev,
910093ea2d3SDonald Lee 			usb_sndbulkpipe(serial->dev,
911093ea2d3SDonald Lee 				(port->bulk_out_endpointAddress) + 2),
912093ea2d3SDonald Lee 			urb->transfer_buffer,
913093ea2d3SDonald Lee 			transfer_size,
914093ea2d3SDonald Lee 			mos7840_bulk_out_data_callback, mos7840_port);
915093ea2d3SDonald Lee 	} else {
9163f542974SPaul B Schroeder 		usb_fill_bulk_urb(urb,
9173f542974SPaul B Schroeder 			serial->dev,
9183f542974SPaul B Schroeder 			usb_sndbulkpipe(serial->dev,
9193f542974SPaul B Schroeder 				port->bulk_out_endpointAddress),
9203f542974SPaul B Schroeder 			urb->transfer_buffer,
9213f542974SPaul B Schroeder 			transfer_size,
9223f542974SPaul B Schroeder 			mos7840_bulk_out_data_callback, mos7840_port);
923093ea2d3SDonald Lee 	}
9243f542974SPaul B Schroeder 
9259c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
9263f542974SPaul B Schroeder 
92705cf0decSJohan Hovold 	if (mos7840_port->has_led)
92805cf0decSJohan Hovold 		mos7840_led_activity(port);
9290eafe4deSDonald 
9303f542974SPaul B Schroeder 	/* send it down the pipe */
9313f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
9323f542974SPaul B Schroeder 
9333f542974SPaul B Schroeder 	if (status) {
9340de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
93522a416c4SJohan Hovold 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
936194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
9373f542974SPaul B Schroeder 		bytes_sent = status;
9383f542974SPaul B Schroeder 		goto exit;
9393f542974SPaul B Schroeder 	}
9403f542974SPaul B Schroeder 	bytes_sent = transfer_size;
9418c1a07ffSJohan Hovold 	port->icount.tx += transfer_size;
9428c1a07ffSJohan Hovold 	dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
9433f542974SPaul B Schroeder exit:
9443f542974SPaul B Schroeder 	return bytes_sent;
9453f542974SPaul B Schroeder 
9463f542974SPaul B Schroeder }
9473f542974SPaul B Schroeder 
9483f542974SPaul B Schroeder /*****************************************************************************
9493f542974SPaul B Schroeder  * mos7840_throttle
9503f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
9513f542974SPaul B Schroeder  *	being read from the port.
9523f542974SPaul B Schroeder  *****************************************************************************/
9533f542974SPaul B Schroeder 
95495da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
9553f542974SPaul B Schroeder {
95695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
957*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
9583f542974SPaul B Schroeder 	int status;
9593f542974SPaul B Schroeder 
9603f542974SPaul B Schroeder 	if (!mos7840_port->open) {
9619c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "port not opened\n");
9623f542974SPaul B Schroeder 		return;
9633f542974SPaul B Schroeder 	}
9643f542974SPaul B Schroeder 
9653f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
9663f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
9673f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
96895da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
96995da310eSAlan Cox 		if (status <= 0)
9703f542974SPaul B Schroeder 			return;
9713f542974SPaul B Schroeder 	}
9723f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
9739db276f8SPeter Hurley 	if (C_CRTSCTS(tty)) {
9743f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
97595da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
9763f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
97795da310eSAlan Cox 		if (status < 0)
9783f542974SPaul B Schroeder 			return;
9793f542974SPaul B Schroeder 	}
9803f542974SPaul B Schroeder }
9813f542974SPaul B Schroeder 
9823f542974SPaul B Schroeder /*****************************************************************************
9833f542974SPaul B Schroeder  * mos7840_unthrottle
984880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
985880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
986880af9dbSAlan Cox  *	called)
9873f542974SPaul B Schroeder  *****************************************************************************/
98895da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
9893f542974SPaul B Schroeder {
99095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
991*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
9923f542974SPaul B Schroeder 	int status;
9933f542974SPaul B Schroeder 
9943f542974SPaul B Schroeder 	if (!mos7840_port->open) {
9959c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
9963f542974SPaul B Schroeder 		return;
9973f542974SPaul B Schroeder 	}
9983f542974SPaul B Schroeder 
9993f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
10003f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
10013f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
100295da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
100395da310eSAlan Cox 		if (status <= 0)
10043f542974SPaul B Schroeder 			return;
10053f542974SPaul B Schroeder 	}
10063f542974SPaul B Schroeder 
10073f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
10089db276f8SPeter Hurley 	if (C_CRTSCTS(tty)) {
10093f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
101095da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
10113f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
101295da310eSAlan Cox 		if (status < 0)
10133f542974SPaul B Schroeder 			return;
10143f542974SPaul B Schroeder 	}
10153f542974SPaul B Schroeder }
10163f542974SPaul B Schroeder 
101760b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
10183f542974SPaul B Schroeder {
101995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
10203f542974SPaul B Schroeder 	unsigned int result;
10213f542974SPaul B Schroeder 	__u16 msr;
10223f542974SPaul B Schroeder 	__u16 mcr;
1023880af9dbSAlan Cox 	int status;
10243f542974SPaul B Schroeder 
10253f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1026cd8db057SJohan Hovold 	if (status < 0)
1027a91ccd26SJohan Hovold 		return -EIO;
10283f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1029cd8db057SJohan Hovold 	if (status < 0)
1030a91ccd26SJohan Hovold 		return -EIO;
10313f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
10323f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
10333f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
10343f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
10353f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
10363f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
10373f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
10383f542974SPaul B Schroeder 
10399c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
10403f542974SPaul B Schroeder 
10413f542974SPaul B Schroeder 	return result;
10423f542974SPaul B Schroeder }
10433f542974SPaul B Schroeder 
104420b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
10453f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
10463f542974SPaul B Schroeder {
104795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
1048*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
10493f542974SPaul B Schroeder 	unsigned int mcr;
105087521c46SRoel Kluin 	int status;
10513f542974SPaul B Schroeder 
1052e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
10533f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
10543f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
10553f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
10563f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
10573f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
10583f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
10593f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
10603f542974SPaul B Schroeder 
10613f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
10623f542974SPaul B Schroeder 		mcr |= MCR_RTS;
10633f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
10643f542974SPaul B Schroeder 		mcr |= MCR_DTR;
10653f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
10663f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
10673f542974SPaul B Schroeder 
10683f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
10693f542974SPaul B Schroeder 
10703f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
10713f542974SPaul B Schroeder 	if (status < 0) {
10729c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
107387521c46SRoel Kluin 		return status;
10743f542974SPaul B Schroeder 	}
10753f542974SPaul B Schroeder 
10763f542974SPaul B Schroeder 	return 0;
10773f542974SPaul B Schroeder }
10783f542974SPaul B Schroeder 
10793f542974SPaul B Schroeder /*****************************************************************************
10803f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
10813f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
10823f542974SPaul B Schroeder  *	baud rate.
10833f542974SPaul B Schroeder  *****************************************************************************/
10849c134a14SGreg Kroah-Hartman static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
10859c134a14SGreg Kroah-Hartman 					  int baudRate, int *divisor,
10863f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
10873f542974SPaul B Schroeder {
10889c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
10893f542974SPaul B Schroeder 
10903f542974SPaul B Schroeder 	if (baudRate <= 115200) {
10913f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
10923f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
10933f542974SPaul B Schroeder 	}
10943f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
10953f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
10963f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
10973f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
10983f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
10993f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
11003f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
11013f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
11023f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
11033f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
11043f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
11053f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
11063f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
11073f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
11083f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
11093f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
11103f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
11113f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
11123f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
11133f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
11143f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
11153f542974SPaul B Schroeder 	}
11163f542974SPaul B Schroeder 	return 0;
11173f542974SPaul B Schroeder }
11183f542974SPaul B Schroeder 
11193f542974SPaul B Schroeder /*****************************************************************************
11203f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
11213f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
11223f542974SPaul B Schroeder  *	specified port.
11233f542974SPaul B Schroeder  *****************************************************************************/
11243f542974SPaul B Schroeder 
11253f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
11263f542974SPaul B Schroeder 					    int baudRate)
11273f542974SPaul B Schroeder {
1128ce039bd4SJohan Hovold 	struct usb_serial_port *port = mos7840_port->port;
11293f542974SPaul B Schroeder 	int divisor = 0;
11303f542974SPaul B Schroeder 	int status;
11313f542974SPaul B Schroeder 	__u16 Data;
11323f542974SPaul B Schroeder 	__u16 clk_sel_val;
11333f542974SPaul B Schroeder 
11341143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1135880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
11363f542974SPaul B Schroeder 	if (baudRate > 115200) {
11373f542974SPaul B Schroeder #ifdef HW_flow_control
1138880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1139880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
11403f542974SPaul B Schroeder 		Data = 0x2b;
11413f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1142880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1143880af9dbSAlan Cox 									Data);
11443f542974SPaul B Schroeder 		if (status < 0) {
11459c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
11463f542974SPaul B Schroeder 			return -1;
11473f542974SPaul B Schroeder 		}
11483f542974SPaul B Schroeder #endif
11493f542974SPaul B Schroeder 
11503f542974SPaul B Schroeder 	} else {
11513f542974SPaul B Schroeder #ifdef HW_flow_control
1152880af9dbSAlan Cox 		/* setting h/w flow control bit to 0 */
11533f542974SPaul B Schroeder 		Data = 0xb;
11543f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1155880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1156880af9dbSAlan Cox 									Data);
11573f542974SPaul B Schroeder 		if (status < 0) {
11589c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
11593f542974SPaul B Schroeder 			return -1;
11603f542974SPaul B Schroeder 		}
11613f542974SPaul B Schroeder #endif
11623f542974SPaul B Schroeder 
11633f542974SPaul B Schroeder 	}
11643f542974SPaul B Schroeder 
1165880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
11663f542974SPaul B Schroeder 		clk_sel_val = 0x0;
11673f542974SPaul B Schroeder 		Data = 0x0;
11689c134a14SGreg Kroah-Hartman 		status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
11693f542974SPaul B Schroeder 						   &clk_sel_val);
1170880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
11713f542974SPaul B Schroeder 								 &Data);
11723f542974SPaul B Schroeder 		if (status < 0) {
11739c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
11743f542974SPaul B Schroeder 			return -1;
11753f542974SPaul B Schroeder 		}
11763f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1177880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1178880af9dbSAlan Cox 								Data);
11793f542974SPaul B Schroeder 		if (status < 0) {
11809c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
11813f542974SPaul B Schroeder 			return -1;
11823f542974SPaul B Schroeder 		}
11833f542974SPaul B Schroeder 		/* Calculate the Divisor */
11843f542974SPaul B Schroeder 
11853f542974SPaul B Schroeder 		if (status) {
1186194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
11873f542974SPaul B Schroeder 			return status;
11883f542974SPaul B Schroeder 		}
11893f542974SPaul B Schroeder 		/* Enable access to divisor latch */
11903f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
11913f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
11923f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
11933f542974SPaul B Schroeder 
11943f542974SPaul B Schroeder 		/* Write the divisor */
11953f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
11969c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
11973f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
11983f542974SPaul B Schroeder 
11993f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
12009c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
12013f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
12023f542974SPaul B Schroeder 
12033f542974SPaul B Schroeder 		/* Disable access to divisor latch */
12043f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
12053f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
12063f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
12073f542974SPaul B Schroeder 
12083f542974SPaul B Schroeder 	}
12093f542974SPaul B Schroeder 	return status;
12103f542974SPaul B Schroeder }
12113f542974SPaul B Schroeder 
12123f542974SPaul B Schroeder /*****************************************************************************
12133f542974SPaul B Schroeder  * mos7840_change_port_settings
12143f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
12153f542974SPaul B Schroeder  *      the specified new settings.
12163f542974SPaul B Schroeder  *****************************************************************************/
12173f542974SPaul B Schroeder 
121895da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
121995da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
12203f542974SPaul B Schroeder {
1221ce039bd4SJohan Hovold 	struct usb_serial_port *port = mos7840_port->port;
12223f542974SPaul B Schroeder 	int baud;
12233f542974SPaul B Schroeder 	unsigned cflag;
12243f542974SPaul B Schroeder 	__u8 lData;
12253f542974SPaul B Schroeder 	__u8 lParity;
12263f542974SPaul B Schroeder 	__u8 lStop;
12273f542974SPaul B Schroeder 	int status;
12283f542974SPaul B Schroeder 	__u16 Data;
12293f542974SPaul B Schroeder 
12303f542974SPaul B Schroeder 	if (!mos7840_port->open) {
12319c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
12323f542974SPaul B Schroeder 		return;
12333f542974SPaul B Schroeder 	}
12343f542974SPaul B Schroeder 
12353f542974SPaul B Schroeder 	lData = LCR_BITS_8;
12363f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
12373f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
12383f542974SPaul B Schroeder 
1239adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
12403f542974SPaul B Schroeder 
12413f542974SPaul B Schroeder 	/* Change the number of bits */
12423f542974SPaul B Schroeder 	switch (cflag & CSIZE) {
12433f542974SPaul B Schroeder 	case CS5:
12443f542974SPaul B Schroeder 		lData = LCR_BITS_5;
12453f542974SPaul B Schroeder 		break;
12463f542974SPaul B Schroeder 
12473f542974SPaul B Schroeder 	case CS6:
12483f542974SPaul B Schroeder 		lData = LCR_BITS_6;
12493f542974SPaul B Schroeder 		break;
12503f542974SPaul B Schroeder 
12513f542974SPaul B Schroeder 	case CS7:
12523f542974SPaul B Schroeder 		lData = LCR_BITS_7;
12533f542974SPaul B Schroeder 		break;
125478692cc3SColin Leitner 
12553f542974SPaul B Schroeder 	default:
12563f542974SPaul B Schroeder 	case CS8:
12573f542974SPaul B Schroeder 		lData = LCR_BITS_8;
12583f542974SPaul B Schroeder 		break;
12593f542974SPaul B Schroeder 	}
126078692cc3SColin Leitner 
12613f542974SPaul B Schroeder 	/* Change the Parity bit */
12623f542974SPaul B Schroeder 	if (cflag & PARENB) {
12633f542974SPaul B Schroeder 		if (cflag & PARODD) {
12643f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
12659c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
12663f542974SPaul B Schroeder 		} else {
12673f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
12689c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
12693f542974SPaul B Schroeder 		}
12703f542974SPaul B Schroeder 
12713f542974SPaul B Schroeder 	} else {
12729c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
12733f542974SPaul B Schroeder 	}
12743f542974SPaul B Schroeder 
1275880af9dbSAlan Cox 	if (cflag & CMSPAR)
12763f542974SPaul B Schroeder 		lParity = lParity | 0x20;
12773f542974SPaul B Schroeder 
12783f542974SPaul B Schroeder 	/* Change the Stop bit */
12793f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
12803f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
12819c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
12823f542974SPaul B Schroeder 	} else {
12833f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
12849c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
12853f542974SPaul B Schroeder 	}
12863f542974SPaul B Schroeder 
12873f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
12883f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
12893f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
12903f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
12913f542974SPaul B Schroeder 
12929c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
12933f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
12943f542974SPaul B Schroeder 	/* Disable Interrupts */
12953f542974SPaul B Schroeder 	Data = 0x00;
12963f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
12973f542974SPaul B Schroeder 
12983f542974SPaul B Schroeder 	Data = 0x00;
12993f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
13003f542974SPaul B Schroeder 
13013f542974SPaul B Schroeder 	Data = 0xcf;
13023f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
13033f542974SPaul B Schroeder 
13043f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
13053f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
13063f542974SPaul B Schroeder 
13073f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
13083f542974SPaul B Schroeder 
13093f542974SPaul B Schroeder 	Data = 0x00b;
13103f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
13113f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
13123f542974SPaul B Schroeder 	Data = 0x00b;
13133f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
13143f542974SPaul B Schroeder 
13153f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
13163f542974SPaul B Schroeder 
13173f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
1318880af9dbSAlan Cox 	if (cflag & CBAUD)
13193f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
13203f542974SPaul B Schroeder 
1321880af9dbSAlan Cox 	if (cflag & CRTSCTS)
13223f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
1323880af9dbSAlan Cox 	else
13243f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
13253f542974SPaul B Schroeder 
13263f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
13273f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
13283f542974SPaul B Schroeder 
13293f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
13303f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
13313f542974SPaul B Schroeder 
13323f542974SPaul B Schroeder 	if (!baud) {
13333f542974SPaul B Schroeder 		/* pick a default, any default... */
13349c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "Picked default baud...\n");
13353f542974SPaul B Schroeder 		baud = 9600;
13363f542974SPaul B Schroeder 	}
13373f542974SPaul B Schroeder 
13389c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
13393f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
13403f542974SPaul B Schroeder 
13413f542974SPaul B Schroeder 	/* Enable Interrupts */
13423f542974SPaul B Schroeder 	Data = 0x0c;
13433f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
13443f542974SPaul B Schroeder 
1345ce9d8562SMathieu OTHACEHE 	if (!mos7840_port->read_urb_busy) {
134650de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
13479d380190SJohan Hovold 		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
13483f542974SPaul B Schroeder 		if (status) {
13499c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
13503f542974SPaul B Schroeder 			    status);
135150de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
13523f542974SPaul B Schroeder 		}
13533f542974SPaul B Schroeder 	}
13549c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
13553f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
13563f542974SPaul B Schroeder }
13573f542974SPaul B Schroeder 
13583f542974SPaul B Schroeder /*****************************************************************************
13593f542974SPaul B Schroeder  * mos7840_set_termios
13603f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
13613f542974SPaul B Schroeder  *	the termios structure
13623f542974SPaul B Schroeder  *****************************************************************************/
13633f542974SPaul B Schroeder 
136495da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
136595da310eSAlan Cox 				struct usb_serial_port *port,
1366606d099cSAlan Cox 				struct ktermios *old_termios)
13673f542974SPaul B Schroeder {
1368*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
13693f542974SPaul B Schroeder 	int status;
13703f542974SPaul B Schroeder 
13713f542974SPaul B Schroeder 	if (!mos7840_port->open) {
13729c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
13733f542974SPaul B Schroeder 		return;
13743f542974SPaul B Schroeder 	}
13753f542974SPaul B Schroeder 
13763f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
13773f542974SPaul B Schroeder 
137895da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
13793f542974SPaul B Schroeder 
13803f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
13819c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
13823f542974SPaul B Schroeder 		return;
13833f542974SPaul B Schroeder 	}
13843f542974SPaul B Schroeder 
1385ce9d8562SMathieu OTHACEHE 	if (!mos7840_port->read_urb_busy) {
138650de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
13879d380190SJohan Hovold 		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
13883f542974SPaul B Schroeder 		if (status) {
13899c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
13903f542974SPaul B Schroeder 			    status);
139150de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
13923f542974SPaul B Schroeder 		}
13933f542974SPaul B Schroeder 	}
13943f542974SPaul B Schroeder }
13953f542974SPaul B Schroeder 
13963f542974SPaul B Schroeder /*****************************************************************************
13973f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
13983f542974SPaul B Schroeder  *
13993f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
14003f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
14013f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
14023f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
14033f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
14043f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
14053f542974SPaul B Schroeder  *****************************************************************************/
14063f542974SPaul B Schroeder 
140795da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
140897c4965dSAl Viro 				unsigned int __user *value)
14093f542974SPaul B Schroeder {
14103f542974SPaul B Schroeder 	int count;
14113f542974SPaul B Schroeder 	unsigned int result = 0;
14123f542974SPaul B Schroeder 
141395da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
14149c134a14SGreg Kroah-Hartman 	if (count == 0)
14153f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
14163f542974SPaul B Schroeder 
14173f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
14183f542974SPaul B Schroeder 		return -EFAULT;
14193f542974SPaul B Schroeder 	return 0;
14203f542974SPaul B Schroeder }
14213f542974SPaul B Schroeder 
14223f542974SPaul B Schroeder /*****************************************************************************
14233f542974SPaul B Schroeder  * mos7840_get_serial_info
14243f542974SPaul B Schroeder  *      function to get information about serial port
14253f542974SPaul B Schroeder  *****************************************************************************/
14263f542974SPaul B Schroeder 
1427b27ef409SAl Viro static int mos7840_get_serial_info(struct tty_struct *tty,
1428b27ef409SAl Viro 				   struct serial_struct *ss)
14293f542974SPaul B Schroeder {
1430b27ef409SAl Viro 	struct usb_serial_port *port = tty->driver_data;
1431*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
14323f542974SPaul B Schroeder 
1433b27ef409SAl Viro 	ss->type = PORT_16550A;
1434b27ef409SAl Viro 	ss->line = mos7840_port->port->minor;
1435b27ef409SAl Viro 	ss->port = mos7840_port->port->port_number;
1436b27ef409SAl Viro 	ss->irq = 0;
1437b27ef409SAl Viro 	ss->xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1438b27ef409SAl Viro 	ss->baud_base = 9600;
1439b27ef409SAl Viro 	ss->close_delay = 5 * HZ;
1440b27ef409SAl Viro 	ss->closing_wait = 30 * HZ;
14413f542974SPaul B Schroeder 	return 0;
14423f542974SPaul B Schroeder }
14433f542974SPaul B Schroeder 
14443f542974SPaul B Schroeder /*****************************************************************************
14453f542974SPaul B Schroeder  * SerialIoctl
14463f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
14473f542974SPaul B Schroeder  *****************************************************************************/
14483f542974SPaul B Schroeder 
144900a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
14503f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
14513f542974SPaul B Schroeder {
145295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
145397c4965dSAl Viro 	void __user *argp = (void __user *)arg;
14543f542974SPaul B Schroeder 
14553f542974SPaul B Schroeder 	switch (cmd) {
14563f542974SPaul B Schroeder 		/* return number of bytes available */
14573f542974SPaul B Schroeder 
14583f542974SPaul B Schroeder 	case TIOCSERGETLSR:
14599c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
146095da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
14613f542974SPaul B Schroeder 
14623f542974SPaul B Schroeder 	default:
14633f542974SPaul B Schroeder 		break;
14643f542974SPaul B Schroeder 	}
14653f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
14663f542974SPaul B Schroeder }
14673f542974SPaul B Schroeder 
14681c333550SJohan Hovold /*
14691c333550SJohan Hovold  * Check if GPO (pin 42) is connected to GPI (pin 33) as recommended by ASIX
14701c333550SJohan Hovold  * for MCS7810 by bit-banging a 16-bit word.
14711c333550SJohan Hovold  *
14721c333550SJohan Hovold  * Note that GPO is really RTS of the third port so this will toggle RTS of
14731c333550SJohan Hovold  * port two or three on two- and four-port devices.
14741c333550SJohan Hovold  */
14750eafe4deSDonald static int mos7810_check(struct usb_serial *serial)
14763f542974SPaul B Schroeder {
14770eafe4deSDonald 	int i, pass_count = 0;
147815ee89c3SJohan Hovold 	u8 *buf;
14790eafe4deSDonald 	__u16 data = 0, mcr_data = 0;
14800eafe4deSDonald 	__u16 test_pattern = 0x55AA;
148115ee89c3SJohan Hovold 	int res;
148215ee89c3SJohan Hovold 
148315ee89c3SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
148415ee89c3SJohan Hovold 	if (!buf)
148515ee89c3SJohan Hovold 		return 0;	/* failed to identify 7810 */
14863f542974SPaul B Schroeder 
14870eafe4deSDonald 	/* Store MCR setting */
148815ee89c3SJohan Hovold 	res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
14890eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
149015ee89c3SJohan Hovold 		buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
149115ee89c3SJohan Hovold 	if (res == VENDOR_READ_LENGTH)
149215ee89c3SJohan Hovold 		mcr_data = *buf;
14930eafe4deSDonald 
14940eafe4deSDonald 	for (i = 0; i < 16; i++) {
14950eafe4deSDonald 		/* Send the 1-bit test pattern out to MCS7810 test pin */
14960eafe4deSDonald 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
14970eafe4deSDonald 			MCS_WRREQ, MCS_WR_RTYPE,
14980eafe4deSDonald 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
14990eafe4deSDonald 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
15000eafe4deSDonald 
15010eafe4deSDonald 		/* Read the test pattern back */
150215ee89c3SJohan Hovold 		res = usb_control_msg(serial->dev,
150315ee89c3SJohan Hovold 				usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
150415ee89c3SJohan Hovold 				MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
1505093ea2d3SDonald Lee 				VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
150615ee89c3SJohan Hovold 		if (res == VENDOR_READ_LENGTH)
150715ee89c3SJohan Hovold 			data = *buf;
1508093ea2d3SDonald Lee 
15090eafe4deSDonald 		/* If this is a MCS7810 device, both test patterns must match */
15100eafe4deSDonald 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
15110eafe4deSDonald 			break;
15120eafe4deSDonald 
15130eafe4deSDonald 		pass_count++;
15143f542974SPaul B Schroeder 	}
1515093ea2d3SDonald Lee 
15160eafe4deSDonald 	/* Restore MCR setting */
15170eafe4deSDonald 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
15180eafe4deSDonald 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
15190eafe4deSDonald 		0, MOS_WDR_TIMEOUT);
15200eafe4deSDonald 
152115ee89c3SJohan Hovold 	kfree(buf);
152215ee89c3SJohan Hovold 
15230eafe4deSDonald 	if (pass_count == 16)
15240eafe4deSDonald 		return 1;
15250eafe4deSDonald 
15260eafe4deSDonald 	return 0;
15270eafe4deSDonald }
15280eafe4deSDonald 
152940c24f28SJohan Hovold static int mos7840_probe(struct usb_serial *serial,
153040c24f28SJohan Hovold 				const struct usb_device_id *id)
15310eafe4deSDonald {
1532375cb533SJohan Hovold 	unsigned long device_flags = id->driver_info;
153315ee89c3SJohan Hovold 	u8 *buf;
153440c24f28SJohan Hovold 
1535375cb533SJohan Hovold 	/* Skip device-type detection if we already have device flags. */
1536375cb533SJohan Hovold 	if (device_flags)
153740c24f28SJohan Hovold 		goto out;
1538e696d00eSPavel Löbl 
153915ee89c3SJohan Hovold 	buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
154040c24f28SJohan Hovold 	if (!buf)
154140c24f28SJohan Hovold 		return -ENOMEM;
154240c24f28SJohan Hovold 
15430eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
154415ee89c3SJohan Hovold 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
15450eafe4deSDonald 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
15460eafe4deSDonald 
15470eafe4deSDonald 	/* For a MCS7840 device GPIO0 must be set to 1 */
154840c24f28SJohan Hovold 	if (buf[0] & 0x01)
1549375cb533SJohan Hovold 		device_flags = MCS_PORTS(4);
15500eafe4deSDonald 	else if (mos7810_check(serial))
1551375cb533SJohan Hovold 		device_flags = MCS_PORTS(1) | MCS_LED;
15520eafe4deSDonald 	else
1553375cb533SJohan Hovold 		device_flags = MCS_PORTS(2);
155440c24f28SJohan Hovold 
155540c24f28SJohan Hovold 	kfree(buf);
155640c24f28SJohan Hovold out:
1557375cb533SJohan Hovold 	usb_set_serial_data(serial, (void *)device_flags);
155840c24f28SJohan Hovold 
155940c24f28SJohan Hovold 	return 0;
15600eafe4deSDonald }
15610eafe4deSDonald 
156207814246SJohan Hovold static int mos7840_calc_num_ports(struct usb_serial *serial,
156307814246SJohan Hovold 					struct usb_serial_endpoints *epds)
156440c24f28SJohan Hovold {
1565375cb533SJohan Hovold 	unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
1566375cb533SJohan Hovold 	int num_ports = MCS_PORTS(device_flags);
156740c24f28SJohan Hovold 
1568375cb533SJohan Hovold 	if (num_ports == 0 || num_ports > 4)
156995254020SJohan Hovold 		return -ENODEV;
15703f542974SPaul B Schroeder 
157195254020SJohan Hovold 	if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
15725c75633eSJohan Hovold 		dev_err(&serial->interface->dev, "missing endpoints\n");
15735c75633eSJohan Hovold 		return -ENODEV;
15745c75633eSJohan Hovold 	}
15755c75633eSJohan Hovold 
157695254020SJohan Hovold 	return num_ports;
15775c75633eSJohan Hovold }
15785c75633eSJohan Hovold 
1579960fbd1cSJohan Hovold static int mos7840_attach(struct usb_serial *serial)
1580960fbd1cSJohan Hovold {
1581960fbd1cSJohan Hovold 	struct device *dev = &serial->interface->dev;
1582960fbd1cSJohan Hovold 	int status;
1583960fbd1cSJohan Hovold 	u16 val;
1584960fbd1cSJohan Hovold 
1585960fbd1cSJohan Hovold 	/* Zero Length flag enable */
1586960fbd1cSJohan Hovold 	val = 0x0f;
1587960fbd1cSJohan Hovold 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, val);
1588960fbd1cSJohan Hovold 	if (status < 0)
1589960fbd1cSJohan Hovold 		dev_dbg(dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
1590960fbd1cSJohan Hovold 	else
1591960fbd1cSJohan Hovold 		dev_dbg(dev, "ZLP_REG5 Writing success status%d\n", status);
1592960fbd1cSJohan Hovold 
1593960fbd1cSJohan Hovold 	return status;
1594960fbd1cSJohan Hovold }
1595960fbd1cSJohan Hovold 
159680c00750SJohan Hovold static int mos7840_port_probe(struct usb_serial_port *port)
15973f542974SPaul B Schroeder {
159880c00750SJohan Hovold 	struct usb_serial *serial = port->serial;
1599375cb533SJohan Hovold 	unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
16003f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
160180c00750SJohan Hovold 	int status;
160280c00750SJohan Hovold 	int pnum;
16033f542974SPaul B Schroeder 	__u16 Data;
16043f542974SPaul B Schroeder 
16053f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
16063f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
16073f542974SPaul B Schroeder 
16081143832eSGreg Kroah-Hartman 	pnum = port->port_number;
160980c00750SJohan Hovold 
1610ae685effSJohan Hovold 	dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
16117ac9da10SBurman Yan 	mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
161210c642d0SJohan Hovold 	if (!mos7840_port)
161380c00750SJohan Hovold 		return -ENOMEM;
16143f542974SPaul B Schroeder 
1615880af9dbSAlan Cox 	/* Initialize all port interrupt end point to port 0 int
1616880af9dbSAlan Cox 	 * endpoint. Our device has only one interrupt end point
1617880af9dbSAlan Cox 	 * common to all port */
16183f542974SPaul B Schroeder 
161980c00750SJohan Hovold 	mos7840_port->port = port;
16200de9a702SOliver Neukum 	spin_lock_init(&mos7840_port->pool_lock);
16213f542974SPaul B Schroeder 
162237768adfSTony Cook 	/* minor is not initialised until later by
162337768adfSTony Cook 	 * usb-serial.c:get_free_serial() and cannot therefore be used
162437768adfSTony Cook 	 * to index device instances */
162580c00750SJohan Hovold 	mos7840_port->port_num = pnum + 1;
1626e5b1e206SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
162780c00750SJohan Hovold 	dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
16283f542974SPaul B Schroeder 
16293f542974SPaul B Schroeder 	if (mos7840_port->port_num == 1) {
16303f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0x0;
16313f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0x1;
16323f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x4;
1633e8603076SJackyChou 	} else {
1634e8603076SJackyChou 		u8 phy_num = mos7840_port->port_num;
1635e8603076SJackyChou 
1636e8603076SJackyChou 		/* Port 2 in the 2-port case uses registers of port 3 */
1637e8603076SJackyChou 		if (serial->num_ports == 2)
1638e8603076SJackyChou 			phy_num = 3;
1639e8603076SJackyChou 
1640e8603076SJackyChou 		mos7840_port->SpRegOffset = 0x8 + 2 * (phy_num - 2);
1641e8603076SJackyChou 		mos7840_port->ControlRegOffset = 0x9 + 2 * (phy_num - 2);
1642e8603076SJackyChou 		mos7840_port->DcrRegOffset = 0x16 + 3 * (phy_num - 2);
16433f542974SPaul B Schroeder 	}
164480c00750SJohan Hovold 	mos7840_dump_serial_port(port, mos7840_port);
1645*7b2faedeSJohan Hovold 	usb_set_serial_port_data(port, mos7840_port);
16463f542974SPaul B Schroeder 
1647880af9dbSAlan Cox 	/* enable rx_disable bit in control register */
164880c00750SJohan Hovold 	status = mos7840_get_reg_sync(port,
16493f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, &Data);
16503f542974SPaul B Schroeder 	if (status < 0) {
165180c00750SJohan Hovold 		dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
1652960fbd1cSJohan Hovold 		goto error;
16533f542974SPaul B Schroeder 	} else
165480c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
1655880af9dbSAlan Cox 	Data |= 0x08;	/* setting driver done bit */
1656880af9dbSAlan Cox 	Data |= 0x04;	/* sp1_bit to have cts change reflect in
1657880af9dbSAlan Cox 			   modem status reg */
16583f542974SPaul B Schroeder 
1659880af9dbSAlan Cox 	/* Data |= 0x20; //rx_disable bit */
166080c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
16613f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, Data);
16623f542974SPaul B Schroeder 	if (status < 0) {
166380c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
1664960fbd1cSJohan Hovold 		goto error;
16653f542974SPaul B Schroeder 	} else
166680c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
16673f542974SPaul B Schroeder 
1668880af9dbSAlan Cox 	/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
1669880af9dbSAlan Cox 	   and 0x24 in DCR3 */
16703f542974SPaul B Schroeder 	Data = 0x01;
167180c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
1672880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 0), Data);
16733f542974SPaul B Schroeder 	if (status < 0) {
167480c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
1675960fbd1cSJohan Hovold 		goto error;
16763f542974SPaul B Schroeder 	} else
167780c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
16783f542974SPaul B Schroeder 
16793f542974SPaul B Schroeder 	Data = 0x05;
168080c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
1681880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 1), Data);
16823f542974SPaul B Schroeder 	if (status < 0) {
168380c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
1684960fbd1cSJohan Hovold 		goto error;
16853f542974SPaul B Schroeder 	} else
168680c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
16873f542974SPaul B Schroeder 
16883f542974SPaul B Schroeder 	Data = 0x24;
168980c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
1690880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 2), Data);
16913f542974SPaul B Schroeder 	if (status < 0) {
169280c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
1693960fbd1cSJohan Hovold 		goto error;
16943f542974SPaul B Schroeder 	} else
169580c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
16963f542974SPaul B Schroeder 
1697880af9dbSAlan Cox 	/* write values in clkstart0x0 and clkmulti 0x20 */
16983f542974SPaul B Schroeder 	Data = 0x0;
1699ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
17003f542974SPaul B Schroeder 	if (status < 0) {
170180c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
1702960fbd1cSJohan Hovold 		goto error;
17033f542974SPaul B Schroeder 	} else
170480c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
17053f542974SPaul B Schroeder 
17063f542974SPaul B Schroeder 	Data = 0x20;
1707ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
17083f542974SPaul B Schroeder 	if (status < 0) {
170980c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
17100de9a702SOliver Neukum 		goto error;
17113f542974SPaul B Schroeder 	} else
171280c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
17133f542974SPaul B Schroeder 
1714880af9dbSAlan Cox 	/* write value 0x0 to scratchpad register */
17153f542974SPaul B Schroeder 	Data = 0x00;
1716ae685effSJohan Hovold 	status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
17173f542974SPaul B Schroeder 	if (status < 0) {
171880c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
1719960fbd1cSJohan Hovold 		goto error;
17203f542974SPaul B Schroeder 	} else
172180c00750SJohan Hovold 		dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
17223f542974SPaul B Schroeder 
1723880af9dbSAlan Cox 	/* Zero Length flag register */
1724ae685effSJohan Hovold 	if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
17253f542974SPaul B Schroeder 		Data = 0xff;
172680c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
17273f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
1728880af9dbSAlan Cox 					((__u16)mos7840_port->port_num)), Data);
172980c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
17309c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
17313f542974SPaul B Schroeder 		if (status < 0) {
173280c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
1733960fbd1cSJohan Hovold 			goto error;
17343f542974SPaul B Schroeder 		} else
173580c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
17363f542974SPaul B Schroeder 	} else {
17373f542974SPaul B Schroeder 		Data = 0xff;
173880c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
17393f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
1740880af9dbSAlan Cox 					((__u16)mos7840_port->port_num) - 0x1), Data);
174180c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
17429c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
17433f542974SPaul B Schroeder 		if (status < 0) {
174480c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
1745960fbd1cSJohan Hovold 			goto error;
17463f542974SPaul B Schroeder 		} else
174780c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
17483f542974SPaul B Schroeder 
17493f542974SPaul B Schroeder 	}
17500eafe4deSDonald 
1751375cb533SJohan Hovold 	mos7840_port->has_led = device_flags & MCS_LED;
17520eafe4deSDonald 
17530eafe4deSDonald 	/* Initialize LED timers */
1754375cb533SJohan Hovold 	if (mos7840_port->has_led) {
175505cf0decSJohan Hovold 		mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
175605cf0decSJohan Hovold 		mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
175705cf0decSJohan Hovold 								GFP_KERNEL);
175805cf0decSJohan Hovold 		if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
175905cf0decSJohan Hovold 			status = -ENOMEM;
176005cf0decSJohan Hovold 			goto error;
176105cf0decSJohan Hovold 		}
176205cf0decSJohan Hovold 
1763e99e88a9SKees Cook 		timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
17640eafe4deSDonald 		mos7840_port->led_timer1.expires =
17650eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_ON_MS);
1766e99e88a9SKees Cook 		timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
1767e99e88a9SKees Cook 			    0);
17680eafe4deSDonald 		mos7840_port->led_timer2.expires =
17690eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_OFF_MS);
17700eafe4deSDonald 
17710eafe4deSDonald 		/* Turn off LED */
1772ae685effSJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
17730eafe4deSDonald 	}
1774960fbd1cSJohan Hovold 
17753f542974SPaul B Schroeder 	return 0;
17760de9a702SOliver Neukum error:
177705cf0decSJohan Hovold 	kfree(mos7840_port->led_dr);
177805cf0decSJohan Hovold 	usb_free_urb(mos7840_port->led_urb);
17790de9a702SOliver Neukum 	kfree(mos7840_port);
178080c00750SJohan Hovold 
17810de9a702SOliver Neukum 	return status;
17823f542974SPaul B Schroeder }
17833f542974SPaul B Schroeder 
178480c00750SJohan Hovold static int mos7840_port_remove(struct usb_serial_port *port)
17853f542974SPaul B Schroeder {
1786*7b2faedeSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
17873f542974SPaul B Schroeder 
17880eafe4deSDonald 	if (mos7840_port->has_led) {
17890eafe4deSDonald 		/* Turn off LED */
179080c00750SJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
17910eafe4deSDonald 
17920eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer1);
17930eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer2);
179405cf0decSJohan Hovold 
179505cf0decSJohan Hovold 		usb_kill_urb(mos7840_port->led_urb);
179605cf0decSJohan Hovold 		usb_free_urb(mos7840_port->led_urb);
179705cf0decSJohan Hovold 		kfree(mos7840_port->led_dr);
17980eafe4deSDonald 	}
179971831921SJohan Hovold 
18003f542974SPaul B Schroeder 	kfree(mos7840_port);
180180c00750SJohan Hovold 
180280c00750SJohan Hovold 	return 0;
18033f542974SPaul B Schroeder }
18043f542974SPaul B Schroeder 
18053f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
18063f542974SPaul B Schroeder 	.driver = {
18073f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
18083f542974SPaul B Schroeder 		   .name = "mos7840",
18093f542974SPaul B Schroeder 		   },
18103f542974SPaul B Schroeder 	.description = DRIVER_DESC,
181168e24113SGreg Kroah-Hartman 	.id_table = id_table,
181295254020SJohan Hovold 	.num_interrupt_in = 1,
18133f542974SPaul B Schroeder 	.open = mos7840_open,
18143f542974SPaul B Schroeder 	.close = mos7840_close,
18153f542974SPaul B Schroeder 	.write = mos7840_write,
18163f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
18173f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
18183f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
18193f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
18203f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
182140c24f28SJohan Hovold 	.probe = mos7840_probe,
1822960fbd1cSJohan Hovold 	.attach = mos7840_attach,
18233f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
1824b27ef409SAl Viro 	.get_serial = mos7840_get_serial_info,
18253f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
18263f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
18273f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
18283f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
18298c1a07ffSJohan Hovold 	.get_icount = usb_serial_generic_get_icount,
183080c00750SJohan Hovold 	.port_probe = mos7840_port_probe,
183180c00750SJohan Hovold 	.port_remove = mos7840_port_remove,
18323f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
18333f542974SPaul B Schroeder };
18343f542974SPaul B Schroeder 
18354d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
18364d2a7affSAlan Stern 	&moschip7840_4port_device, NULL
18374d2a7affSAlan Stern };
18384d2a7affSAlan Stern 
183968e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
18403f542974SPaul B Schroeder 
18413f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
18423f542974SPaul B Schroeder MODULE_LICENSE("GPL");
1843