xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision ce039bd4b21fdd158e6dbe8baeb577e6347ea7aa)
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 /*             I N T E R F A C E   F U N C T I O N S			*/
3503f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3513f542974SPaul B Schroeder /************************************************************************/
3523f542974SPaul B Schroeder /************************************************************************/
3533f542974SPaul B Schroeder 
3543f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port,
3553f542974SPaul B Schroeder 					    struct moschip_port *data)
3563f542974SPaul B Schroeder {
3573f542974SPaul B Schroeder 	usb_set_serial_port_data(port, (void *)data);
3583f542974SPaul B Schroeder }
3593f542974SPaul B Schroeder 
3603f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct
3613f542974SPaul B Schroeder 							    usb_serial_port
3623f542974SPaul B Schroeder 							    *port)
3633f542974SPaul B Schroeder {
3643f542974SPaul B Schroeder 	return (struct moschip_port *)usb_get_serial_port_data(port);
3653f542974SPaul B Schroeder }
3663f542974SPaul B Schroeder 
3673f542974SPaul B Schroeder /************************************************************************/
3683f542974SPaul B Schroeder /************************************************************************/
3693f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
3703f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
3713f542974SPaul B Schroeder /************************************************************************/
3723f542974SPaul B Schroeder /************************************************************************/
3733f542974SPaul B Schroeder 
3740eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb)
3750eafe4deSDonald {
3760eafe4deSDonald 	switch (urb->status) {
3770eafe4deSDonald 	case 0:
3780eafe4deSDonald 		/* Success */
3790eafe4deSDonald 		break;
3800eafe4deSDonald 	case -ECONNRESET:
3810eafe4deSDonald 	case -ENOENT:
3820eafe4deSDonald 	case -ESHUTDOWN:
3830eafe4deSDonald 		/* This urb is terminated, clean up */
384d9a38a87SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
3859c134a14SGreg Kroah-Hartman 			__func__, urb->status);
3860eafe4deSDonald 		break;
3870eafe4deSDonald 	default:
388d9a38a87SJohan Hovold 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
3899c134a14SGreg Kroah-Hartman 			__func__, urb->status);
3900eafe4deSDonald 	}
3910eafe4deSDonald }
3920eafe4deSDonald 
3930eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
3940eafe4deSDonald 				__u16 reg)
3950eafe4deSDonald {
3960eafe4deSDonald 	struct usb_device *dev = mcs->port->serial->dev;
39705cf0decSJohan Hovold 	struct usb_ctrlrequest *dr = mcs->led_dr;
3980eafe4deSDonald 
3990eafe4deSDonald 	dr->bRequestType = MCS_WR_RTYPE;
4000eafe4deSDonald 	dr->bRequest = MCS_WRREQ;
4010eafe4deSDonald 	dr->wValue = cpu_to_le16(wval);
4020eafe4deSDonald 	dr->wIndex = cpu_to_le16(reg);
4030eafe4deSDonald 	dr->wLength = cpu_to_le16(0);
4040eafe4deSDonald 
40505cf0decSJohan Hovold 	usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
4060eafe4deSDonald 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
4070eafe4deSDonald 
40805cf0decSJohan Hovold 	usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
4090eafe4deSDonald }
4100eafe4deSDonald 
4110eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
4120eafe4deSDonald 				__u16 val)
4130eafe4deSDonald {
4140eafe4deSDonald 	struct usb_device *dev = port->serial->dev;
4150eafe4deSDonald 
4160eafe4deSDonald 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
4170eafe4deSDonald 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
4180eafe4deSDonald }
4190eafe4deSDonald 
420e99e88a9SKees Cook static void mos7840_led_off(struct timer_list *t)
4210eafe4deSDonald {
422e99e88a9SKees Cook 	struct moschip_port *mcs = from_timer(mcs, t, led_timer1);
4230eafe4deSDonald 
4240eafe4deSDonald 	/* Turn off LED */
4250eafe4deSDonald 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
4260eafe4deSDonald 	mod_timer(&mcs->led_timer2,
4270eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_OFF_MS));
4280eafe4deSDonald }
4290eafe4deSDonald 
430e99e88a9SKees Cook static void mos7840_led_flag_off(struct timer_list *t)
4310eafe4deSDonald {
432e99e88a9SKees Cook 	struct moschip_port *mcs = from_timer(mcs, t, led_timer2);
4330eafe4deSDonald 
43405cf0decSJohan Hovold 	clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
43505cf0decSJohan Hovold }
43605cf0decSJohan Hovold 
43705cf0decSJohan Hovold static void mos7840_led_activity(struct usb_serial_port *port)
43805cf0decSJohan Hovold {
43905cf0decSJohan Hovold 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
44005cf0decSJohan Hovold 
44105cf0decSJohan Hovold 	if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
44205cf0decSJohan Hovold 		return;
44305cf0decSJohan Hovold 
44405cf0decSJohan Hovold 	mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
44505cf0decSJohan Hovold 	mod_timer(&mos7840_port->led_timer1,
44605cf0decSJohan Hovold 				jiffies + msecs_to_jiffies(LED_ON_MS));
4470eafe4deSDonald }
4480eafe4deSDonald 
4493f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
4503f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
4513f542974SPaul B Schroeder 					 const char *function)
4523f542974SPaul B Schroeder {
4533f542974SPaul B Schroeder 	if (!serial) {
4549c134a14SGreg Kroah-Hartman 		pr_debug("%s - serial == NULL\n", function);
4553f542974SPaul B Schroeder 		return -1;
4563f542974SPaul B Schroeder 	}
4573f542974SPaul B Schroeder 	if (!serial->type) {
4589c134a14SGreg Kroah-Hartman 		pr_debug("%s - serial->type == NULL!\n", function);
4593f542974SPaul B Schroeder 		return -1;
4603f542974SPaul B Schroeder 	}
4613f542974SPaul B Schroeder 
4623f542974SPaul B Schroeder 	return 0;
4633f542974SPaul B Schroeder }
4643f542974SPaul B Schroeder 
4653f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
4663f542974SPaul B Schroeder 						 const char *function)
4673f542974SPaul B Schroeder {
4683f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
4693f542974SPaul B Schroeder 	if (!port ||
4703f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
471880af9dbSAlan Cox 		/* then say that we don't have a valid usb_serial thing,
472880af9dbSAlan Cox 		 * which will end up genrating -ENODEV return values */
4733f542974SPaul B Schroeder 		return NULL;
4743f542974SPaul B Schroeder 	}
4753f542974SPaul B Schroeder 
4763f542974SPaul B Schroeder 	return port->serial;
4773f542974SPaul B Schroeder }
4783f542974SPaul B Schroeder 
4793f542974SPaul B Schroeder /*****************************************************************************
4803f542974SPaul B Schroeder  * mos7840_bulk_in_callback
4813f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
4823f542974SPaul B Schroeder  *	bulk in endpoint.
4833f542974SPaul B Schroeder  *****************************************************************************/
4843f542974SPaul B Schroeder 
4857d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
4863f542974SPaul B Schroeder {
4873ec9fb6fSJohan Hovold 	struct moschip_port *mos7840_port = urb->context;
488*ce039bd4SJohan Hovold 	struct usb_serial_port *port = mos7840_port->port;
4890643c724SGreg Kroah-Hartman 	int retval;
4903f542974SPaul B Schroeder 	unsigned char *data;
4913f542974SPaul B Schroeder 	struct usb_serial *serial;
4920643c724SGreg Kroah-Hartman 	int status = urb->status;
4933f542974SPaul B Schroeder 
49450de36f7SGreg Kroah-Hartman 	if (status) {
4959c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
49650de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
4973f542974SPaul B Schroeder 		return;
4983f542974SPaul B Schroeder 	}
4993f542974SPaul B Schroeder 
500441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
5013f542974SPaul B Schroeder 	if (!serial) {
50250de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
5033f542974SPaul B Schroeder 		return;
5043f542974SPaul B Schroeder 	}
5053f542974SPaul B Schroeder 
5063f542974SPaul B Schroeder 	data = urb->transfer_buffer;
50759d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
5083f542974SPaul B Schroeder 
5093f542974SPaul B Schroeder 	if (urb->actual_length) {
51005c7cd39SJiri Slaby 		struct tty_port *tport = &mos7840_port->port->port;
51105c7cd39SJiri Slaby 		tty_insert_flip_string(tport, data, urb->actual_length);
5122e124b4aSJiri Slaby 		tty_flip_buffer_push(tport);
5138c1a07ffSJohan Hovold 		port->icount.rx += urb->actual_length;
5148c1a07ffSJohan Hovold 		dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
5153f542974SPaul B Schroeder 	}
5163f542974SPaul B Schroeder 
5173f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
5189c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
51950de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
5203f542974SPaul B Schroeder 		return;
5213f542974SPaul B Schroeder 	}
5223f542974SPaul B Schroeder 
52305cf0decSJohan Hovold 	if (mos7840_port->has_led)
52405cf0decSJohan Hovold 		mos7840_led_activity(port);
5250de9a702SOliver Neukum 
52650de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
5270643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
5283f542974SPaul B Schroeder 
5290643c724SGreg Kroah-Hartman 	if (retval) {
5309c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
53150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
5323f542974SPaul B Schroeder 	}
5333f542974SPaul B Schroeder }
5343f542974SPaul B Schroeder 
5353f542974SPaul B Schroeder /*****************************************************************************
5363f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
537880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
538880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
5393f542974SPaul B Schroeder  *****************************************************************************/
5403f542974SPaul B Schroeder 
5417d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
5423f542974SPaul B Schroeder {
543*ce039bd4SJohan Hovold 	struct moschip_port *mos7840_port = urb->context;
544*ce039bd4SJohan Hovold 	struct usb_serial_port *port = mos7840_port->port;
5450643c724SGreg Kroah-Hartman 	int status = urb->status;
54619bfbf46SJohn Ogness 	unsigned long flags;
5470de9a702SOliver Neukum 	int i;
5480de9a702SOliver Neukum 
54919bfbf46SJohn Ogness 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
5500de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
5510de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
5520de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
5530de9a702SOliver Neukum 			break;
5540de9a702SOliver Neukum 		}
5550de9a702SOliver Neukum 	}
55619bfbf46SJohn Ogness 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
5570de9a702SOliver Neukum 
5580643c724SGreg Kroah-Hartman 	if (status) {
5599c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
5603f542974SPaul B Schroeder 		return;
5613f542974SPaul B Schroeder 	}
5623f542974SPaul B Schroeder 
5636aad04f2SJiri Slaby 	if (mos7840_port->open)
5646aad04f2SJiri Slaby 		tty_port_tty_wakeup(&port->port);
5653f542974SPaul B Schroeder 
5663f542974SPaul B Schroeder }
5673f542974SPaul B Schroeder 
5683f542974SPaul B Schroeder /************************************************************************/
5693f542974SPaul 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       */
5703f542974SPaul B Schroeder /************************************************************************/
5713f542974SPaul B Schroeder 
5723f542974SPaul B Schroeder /*****************************************************************************
5733f542974SPaul B Schroeder  * mos7840_open
5743f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
5753f542974SPaul B Schroeder  *	If successful, we return 0
5763f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
5773f542974SPaul B Schroeder  *****************************************************************************/
5783f542974SPaul B Schroeder 
579a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
5803f542974SPaul B Schroeder {
581*ce039bd4SJohan Hovold 	struct usb_serial *serial = port->serial;
5823f542974SPaul B Schroeder 	int response;
5833f542974SPaul B Schroeder 	int j;
5843f542974SPaul B Schroeder 	struct urb *urb;
5853f542974SPaul B Schroeder 	__u16 Data;
5863f542974SPaul B Schroeder 	int status;
5873f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
5883f542974SPaul B Schroeder 
5899c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
5903f542974SPaul B Schroeder 		return -ENODEV;
5913f542974SPaul B Schroeder 
5923f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
59371831921SJohan Hovold 	if (mos7840_port == NULL)
5943f542974SPaul B Schroeder 		return -ENODEV;
5953f542974SPaul B Schroeder 
5963f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
5973f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
5983f542974SPaul B Schroeder 
5993f542974SPaul B Schroeder 	/* Initialising the write urb pool */
6003f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
6010de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
6023f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
60310c642d0SJohan Hovold 		if (!urb)
6043f542974SPaul B Schroeder 			continue;
6053f542974SPaul B Schroeder 
606880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
607880af9dbSAlan Cox 								GFP_KERNEL);
6083f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
6090de9a702SOliver Neukum 			usb_free_urb(urb);
6100de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
6113f542974SPaul B Schroeder 			continue;
6123f542974SPaul B Schroeder 		}
6133f542974SPaul B Schroeder 	}
6143f542974SPaul B Schroeder 
6153f542974SPaul B Schroeder /*****************************************************************************
6163f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
6173f542974SPaul B Schroeder  *
6183f542974SPaul B Schroeder  * Register Index
6193f542974SPaul B Schroeder  * 1 : IER
6203f542974SPaul B Schroeder  * 2 : FCR
6213f542974SPaul B Schroeder  * 3 : LCR
6223f542974SPaul B Schroeder  * 4 : MCR
6233f542974SPaul B Schroeder  *
6243f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
6253f542974SPaul B Schroeder  *****************************************************************************/
6263f542974SPaul B Schroeder 
627880af9dbSAlan Cox 	/* NEED to check the following Block */
6283f542974SPaul B Schroeder 
6293f542974SPaul B Schroeder 	Data = 0x0;
6303f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
6313f542974SPaul B Schroeder 	if (status < 0) {
6329c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Spreg failed\n");
6335f8a2e68SJohan Hovold 		goto err;
6343f542974SPaul B Schroeder 	}
6353f542974SPaul B Schroeder 	Data |= 0x80;
6363f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
6373f542974SPaul B Schroeder 	if (status < 0) {
6389c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
6395f8a2e68SJohan Hovold 		goto err;
6403f542974SPaul B Schroeder 	}
6413f542974SPaul B Schroeder 
6423f542974SPaul B Schroeder 	Data &= ~0x80;
6433f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
6443f542974SPaul B Schroeder 	if (status < 0) {
6459c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
6465f8a2e68SJohan Hovold 		goto err;
6473f542974SPaul B Schroeder 	}
648880af9dbSAlan Cox 	/* End of block to be checked */
6493f542974SPaul B Schroeder 
6503f542974SPaul B Schroeder 	Data = 0x0;
651880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
652880af9dbSAlan Cox 									&Data);
6533f542974SPaul B Schroeder 	if (status < 0) {
6549c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Controlreg failed\n");
6555f8a2e68SJohan Hovold 		goto err;
6563f542974SPaul B Schroeder 	}
657880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
658880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
659880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
660880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
6613f542974SPaul B Schroeder 	if (status < 0) {
6629c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Controlreg failed\n");
6635f8a2e68SJohan Hovold 		goto err;
6643f542974SPaul B Schroeder 	}
665880af9dbSAlan Cox 	/* do register settings here */
666880af9dbSAlan Cox 	/* Set all regs to the device default values. */
667880af9dbSAlan Cox 	/***********************************
668880af9dbSAlan Cox 	 * First Disable all interrupts.
669880af9dbSAlan Cox 	 ***********************************/
6703f542974SPaul B Schroeder 	Data = 0x00;
6713f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
6723f542974SPaul B Schroeder 	if (status < 0) {
6739c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "disabling interrupts failed\n");
6745f8a2e68SJohan Hovold 		goto err;
6753f542974SPaul B Schroeder 	}
676880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
6773f542974SPaul B Schroeder 	Data = 0x00;
6783f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
6793f542974SPaul B Schroeder 	if (status < 0) {
6809c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
6815f8a2e68SJohan Hovold 		goto err;
6823f542974SPaul B Schroeder 	}
6833f542974SPaul B Schroeder 
6843f542974SPaul B Schroeder 	Data = 0xcf;
6853f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
6863f542974SPaul B Schroeder 	if (status < 0) {
6879c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
6885f8a2e68SJohan Hovold 		goto err;
6893f542974SPaul B Schroeder 	}
6903f542974SPaul B Schroeder 
6913f542974SPaul B Schroeder 	Data = 0x03;
6923f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
6933f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
6943f542974SPaul B Schroeder 
6953f542974SPaul B Schroeder 	Data = 0x0b;
6963f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
6973f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
6983f542974SPaul B Schroeder 
6993f542974SPaul B Schroeder 	Data = 0x00;
7003f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
7013f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
7023f542974SPaul B Schroeder 
703880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
7043f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
7053f542974SPaul B Schroeder 
7063f542974SPaul B Schroeder 	Data = 0x0c;
7073f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
7083f542974SPaul B Schroeder 
7093f542974SPaul B Schroeder 	Data = 0x0;
7103f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
7113f542974SPaul B Schroeder 
7123f542974SPaul B Schroeder 	Data = 0x00;
7133f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
7143f542974SPaul B Schroeder 
7153f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
7163f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
7173f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
7183f542974SPaul B Schroeder 
719880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
7203f542974SPaul B Schroeder 	Data = 0x0;
7213f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
7223f542974SPaul B Schroeder 
7233f542974SPaul B Schroeder 	Data = Data | 0x0c;
7243f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
7253f542974SPaul B Schroeder 
7263f542974SPaul B Schroeder 	Data = Data & ~0x0c;
7273f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
728880af9dbSAlan Cox 	/* Finally enable all interrupts */
7293f542974SPaul B Schroeder 	Data = 0x0c;
7303f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
7313f542974SPaul B Schroeder 
732880af9dbSAlan Cox 	/* clearing rx_disable */
7333f542974SPaul B Schroeder 	Data = 0x0;
734880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
735880af9dbSAlan Cox 									&Data);
7363f542974SPaul B Schroeder 	Data = Data & ~0x20;
737880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
738880af9dbSAlan Cox 									Data);
7393f542974SPaul B Schroeder 
740880af9dbSAlan Cox 	/* rx_negate */
7413f542974SPaul B Schroeder 	Data = 0x0;
742880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
743880af9dbSAlan Cox 									&Data);
7443f542974SPaul B Schroeder 	Data = Data | 0x10;
745880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
746880af9dbSAlan Cox 									Data);
7473f542974SPaul B Schroeder 
7481143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "port number is %d\n", port->port_number);
749e5b1e206SGreg Kroah-Hartman 	dev_dbg(&port->dev, "minor number is %d\n", port->minor);
7509c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
7519c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
7529c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
7539c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
7543f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
7553f542974SPaul B Schroeder 
7563f542974SPaul B Schroeder 	/* set up our bulk in urb */
7571143832eSGreg Kroah-Hartman 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
758093ea2d3SDonald Lee 		usb_fill_bulk_urb(mos7840_port->read_urb,
759093ea2d3SDonald Lee 			serial->dev,
760093ea2d3SDonald Lee 			usb_rcvbulkpipe(serial->dev,
761093ea2d3SDonald Lee 				(port->bulk_in_endpointAddress) + 2),
762093ea2d3SDonald Lee 			port->bulk_in_buffer,
763093ea2d3SDonald Lee 			mos7840_port->read_urb->transfer_buffer_length,
764093ea2d3SDonald Lee 			mos7840_bulk_in_callback, mos7840_port);
765093ea2d3SDonald Lee 	} else {
7663f542974SPaul B Schroeder 		usb_fill_bulk_urb(mos7840_port->read_urb,
7673f542974SPaul B Schroeder 			serial->dev,
7683f542974SPaul B Schroeder 			usb_rcvbulkpipe(serial->dev,
7693f542974SPaul B Schroeder 				port->bulk_in_endpointAddress),
7703f542974SPaul B Schroeder 			port->bulk_in_buffer,
7713f542974SPaul B Schroeder 			mos7840_port->read_urb->transfer_buffer_length,
7723f542974SPaul B Schroeder 			mos7840_bulk_in_callback, mos7840_port);
773093ea2d3SDonald Lee 	}
7743f542974SPaul B Schroeder 
7759c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
77650de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
7773f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
7783f542974SPaul B Schroeder 	if (response) {
779194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
780194343d9SGreg Kroah-Hartman 			__func__, response);
78150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7823f542974SPaul B Schroeder 	}
7833f542974SPaul B Schroeder 
7843f542974SPaul B Schroeder 	/* initialize our port settings */
785880af9dbSAlan Cox 	/* Must set to enable ints! */
786880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
7873f542974SPaul B Schroeder 	/* send a open port command */
7883f542974SPaul B Schroeder 	mos7840_port->open = 1;
789880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
7903f542974SPaul B Schroeder 
7913f542974SPaul B Schroeder 	return 0;
7925f8a2e68SJohan Hovold err:
7935f8a2e68SJohan Hovold 	for (j = 0; j < NUM_URBS; ++j) {
7945f8a2e68SJohan Hovold 		urb = mos7840_port->write_urb_pool[j];
7955f8a2e68SJohan Hovold 		if (!urb)
7965f8a2e68SJohan Hovold 			continue;
7975f8a2e68SJohan Hovold 		kfree(urb->transfer_buffer);
7985f8a2e68SJohan Hovold 		usb_free_urb(urb);
7995f8a2e68SJohan Hovold 	}
8005f8a2e68SJohan Hovold 	return status;
8013f542974SPaul B Schroeder }
8023f542974SPaul B Schroeder 
8033f542974SPaul B Schroeder /*****************************************************************************
8043f542974SPaul B Schroeder  * mos7840_chars_in_buffer
8053f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
8063f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
8073f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
8083f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
8093f542974SPaul B Schroeder  *	system,
81095da310eSAlan Cox  *	Otherwise we return zero.
8113f542974SPaul B Schroeder  *****************************************************************************/
8123f542974SPaul B Schroeder 
81395da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
8143f542974SPaul B Schroeder {
81595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8163f542974SPaul B Schroeder 	int i;
8173f542974SPaul B Schroeder 	int chars = 0;
8180de9a702SOliver Neukum 	unsigned long flags;
8193f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8203f542974SPaul B Schroeder 
8213f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
8223363155bSGreg Kroah-Hartman 	if (mos7840_port == NULL)
82395da310eSAlan Cox 		return 0;
8243f542974SPaul B Schroeder 
8250de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
8265c263b92SMark Ferrell 	for (i = 0; i < NUM_URBS; ++i) {
8275c263b92SMark Ferrell 		if (mos7840_port->busy[i]) {
8285c263b92SMark Ferrell 			struct urb *urb = mos7840_port->write_urb_pool[i];
8295c263b92SMark Ferrell 			chars += urb->transfer_buffer_length;
8305c263b92SMark Ferrell 		}
8315c263b92SMark Ferrell 	}
8320de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
8339c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
8340de9a702SOliver Neukum 	return chars;
8353f542974SPaul B Schroeder 
8363f542974SPaul B Schroeder }
8373f542974SPaul B Schroeder 
8383f542974SPaul B Schroeder /*****************************************************************************
8393f542974SPaul B Schroeder  * mos7840_close
8403f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
8413f542974SPaul B Schroeder  *****************************************************************************/
8423f542974SPaul B Schroeder 
843335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
8443f542974SPaul B Schroeder {
8453f542974SPaul B Schroeder 	struct usb_serial *serial;
8463f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8473f542974SPaul B Schroeder 	int j;
8483f542974SPaul B Schroeder 	__u16 Data;
8493f542974SPaul B Schroeder 
850441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
8519c134a14SGreg Kroah-Hartman 	if (!serial)
8523f542974SPaul B Schroeder 		return;
8533f542974SPaul B Schroeder 
8543f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
85571831921SJohan Hovold 	if (mos7840_port == NULL)
8563f542974SPaul B Schroeder 		return;
8573f542974SPaul B Schroeder 
8583f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
8593f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
8603f542974SPaul B Schroeder 
8613f542974SPaul B Schroeder 	/* Freeing Write URBs */
8623f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
8633f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
86491c72df1SFabian Frederick 			kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
8653f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
8663f542974SPaul B Schroeder 		}
8673f542974SPaul B Schroeder 	}
8683f542974SPaul B Schroeder 
8693f542974SPaul B Schroeder 	usb_kill_urb(mos7840_port->read_urb);
87050de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = false;
871bb3529c6SJohan Hovold 
8723f542974SPaul B Schroeder 	Data = 0x0;
8733f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
8743f542974SPaul B Schroeder 
8753f542974SPaul B Schroeder 	Data = 0x00;
8763f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
8773f542974SPaul B Schroeder 
8783f542974SPaul B Schroeder 	mos7840_port->open = 0;
8793f542974SPaul B Schroeder }
8803f542974SPaul B Schroeder 
8813f542974SPaul B Schroeder /*****************************************************************************
8823f542974SPaul B Schroeder  * mos7840_break
8833f542974SPaul B Schroeder  *	this function sends a break to the port
8843f542974SPaul B Schroeder  *****************************************************************************/
88595da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
8863f542974SPaul B Schroeder {
88795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8883f542974SPaul B Schroeder 	unsigned char data;
8893f542974SPaul B Schroeder 	struct usb_serial *serial;
8903f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8913f542974SPaul B Schroeder 
892441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
8939c134a14SGreg Kroah-Hartman 	if (!serial)
8943f542974SPaul B Schroeder 		return;
8953f542974SPaul B Schroeder 
8963f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
8973f542974SPaul B Schroeder 
898880af9dbSAlan Cox 	if (mos7840_port == NULL)
8993f542974SPaul B Schroeder 		return;
9003f542974SPaul B Schroeder 
90195da310eSAlan Cox 	if (break_state == -1)
9023f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
90395da310eSAlan Cox 	else
9043f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
9053f542974SPaul B Schroeder 
9066b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
9073f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
9089c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
9093f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
9103f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
9113f542974SPaul B Schroeder }
9123f542974SPaul B Schroeder 
9133f542974SPaul B Schroeder /*****************************************************************************
9143f542974SPaul B Schroeder  * mos7840_write_room
9153f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
9163f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
9173f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
9183f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
9193f542974SPaul B Schroeder  *****************************************************************************/
9203f542974SPaul B Schroeder 
92195da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
9223f542974SPaul B Schroeder {
92395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
9243f542974SPaul B Schroeder 	int i;
9253f542974SPaul B Schroeder 	int room = 0;
9260de9a702SOliver Neukum 	unsigned long flags;
9273f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
9283f542974SPaul B Schroeder 
9293f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
9309c134a14SGreg Kroah-Hartman 	if (mos7840_port == NULL)
9313f542974SPaul B Schroeder 		return -1;
9323f542974SPaul B Schroeder 
9330de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
9343f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
935880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
9363f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
9373f542974SPaul B Schroeder 	}
9380de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
9393f542974SPaul B Schroeder 
9400de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
9419c134a14SGreg Kroah-Hartman 	dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
9420de9a702SOliver Neukum 	return room;
9433f542974SPaul B Schroeder 
9443f542974SPaul B Schroeder }
9453f542974SPaul B Schroeder 
9463f542974SPaul B Schroeder /*****************************************************************************
9473f542974SPaul B Schroeder  * mos7840_write
9483f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
9493f542974SPaul B Schroeder  *	the port.
9503f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
9513f542974SPaul B Schroeder  *      return a negative error number.
9523f542974SPaul B Schroeder  *****************************************************************************/
9533f542974SPaul B Schroeder 
95495da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
9553f542974SPaul B Schroeder 			 const unsigned char *data, int count)
9563f542974SPaul B Schroeder {
957*ce039bd4SJohan Hovold 	struct usb_serial *serial = port->serial;
9583f542974SPaul B Schroeder 	int status;
9593f542974SPaul B Schroeder 	int i;
9603f542974SPaul B Schroeder 	int bytes_sent = 0;
9613f542974SPaul B Schroeder 	int transfer_size;
9620de9a702SOliver Neukum 	unsigned long flags;
9633f542974SPaul B Schroeder 
9643f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
9653f542974SPaul B Schroeder 	struct urb *urb;
966880af9dbSAlan Cox 	/* __u16 Data; */
9673f542974SPaul B Schroeder 	const unsigned char *current_position = data;
9683f542974SPaul B Schroeder 
9699c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
9703f542974SPaul B Schroeder 		return -1;
9713f542974SPaul B Schroeder 
9723f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
9739c134a14SGreg Kroah-Hartman 	if (mos7840_port == NULL)
9743f542974SPaul B Schroeder 		return -1;
9753f542974SPaul B Schroeder 
9763f542974SPaul B Schroeder 	/* try to find a free urb in the list */
9773f542974SPaul B Schroeder 	urb = NULL;
9783f542974SPaul B Schroeder 
9790de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
9803f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
9810de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
9820de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
9833f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
9849c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "URB:%d\n", i);
9853f542974SPaul B Schroeder 			break;
9863f542974SPaul B Schroeder 		}
9873f542974SPaul B Schroeder 	}
9880de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
9893f542974SPaul B Schroeder 
9903f542974SPaul B Schroeder 	if (urb == NULL) {
9919c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
9923f542974SPaul B Schroeder 		goto exit;
9933f542974SPaul B Schroeder 	}
9943f542974SPaul B Schroeder 
9953f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
9963b7c7e52SAlexey Khoroshilov 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
9973b7c7e52SAlexey Khoroshilov 					       GFP_ATOMIC);
99810c642d0SJohan Hovold 		if (!urb->transfer_buffer)
9993f542974SPaul B Schroeder 			goto exit;
10003f542974SPaul B Schroeder 	}
10013f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
10023f542974SPaul B Schroeder 
10033f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
10043f542974SPaul B Schroeder 
10053f542974SPaul B Schroeder 	/* fill urb with data and submit  */
10061143832eSGreg Kroah-Hartman 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1007093ea2d3SDonald Lee 		usb_fill_bulk_urb(urb,
1008093ea2d3SDonald Lee 			serial->dev,
1009093ea2d3SDonald Lee 			usb_sndbulkpipe(serial->dev,
1010093ea2d3SDonald Lee 				(port->bulk_out_endpointAddress) + 2),
1011093ea2d3SDonald Lee 			urb->transfer_buffer,
1012093ea2d3SDonald Lee 			transfer_size,
1013093ea2d3SDonald Lee 			mos7840_bulk_out_data_callback, mos7840_port);
1014093ea2d3SDonald Lee 	} else {
10153f542974SPaul B Schroeder 		usb_fill_bulk_urb(urb,
10163f542974SPaul B Schroeder 			serial->dev,
10173f542974SPaul B Schroeder 			usb_sndbulkpipe(serial->dev,
10183f542974SPaul B Schroeder 				port->bulk_out_endpointAddress),
10193f542974SPaul B Schroeder 			urb->transfer_buffer,
10203f542974SPaul B Schroeder 			transfer_size,
10213f542974SPaul B Schroeder 			mos7840_bulk_out_data_callback, mos7840_port);
1022093ea2d3SDonald Lee 	}
10233f542974SPaul B Schroeder 
10249c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
10253f542974SPaul B Schroeder 
102605cf0decSJohan Hovold 	if (mos7840_port->has_led)
102705cf0decSJohan Hovold 		mos7840_led_activity(port);
10280eafe4deSDonald 
10293f542974SPaul B Schroeder 	/* send it down the pipe */
10303f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
10313f542974SPaul B Schroeder 
10323f542974SPaul B Schroeder 	if (status) {
10330de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
103422a416c4SJohan Hovold 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1035194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
10363f542974SPaul B Schroeder 		bytes_sent = status;
10373f542974SPaul B Schroeder 		goto exit;
10383f542974SPaul B Schroeder 	}
10393f542974SPaul B Schroeder 	bytes_sent = transfer_size;
10408c1a07ffSJohan Hovold 	port->icount.tx += transfer_size;
10418c1a07ffSJohan Hovold 	dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
10423f542974SPaul B Schroeder exit:
10433f542974SPaul B Schroeder 	return bytes_sent;
10443f542974SPaul B Schroeder 
10453f542974SPaul B Schroeder }
10463f542974SPaul B Schroeder 
10473f542974SPaul B Schroeder /*****************************************************************************
10483f542974SPaul B Schroeder  * mos7840_throttle
10493f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
10503f542974SPaul B Schroeder  *	being read from the port.
10513f542974SPaul B Schroeder  *****************************************************************************/
10523f542974SPaul B Schroeder 
105395da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
10543f542974SPaul B Schroeder {
105595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
10563f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
10573f542974SPaul B Schroeder 	int status;
10583f542974SPaul B Schroeder 
10593f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
10603f542974SPaul B Schroeder 
10613f542974SPaul B Schroeder 	if (mos7840_port == NULL)
10623f542974SPaul B Schroeder 		return;
10633f542974SPaul B Schroeder 
10643f542974SPaul B Schroeder 	if (!mos7840_port->open) {
10659c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "port not opened\n");
10663f542974SPaul B Schroeder 		return;
10673f542974SPaul B Schroeder 	}
10683f542974SPaul B Schroeder 
10693f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
10703f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
10713f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
107295da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
107395da310eSAlan Cox 		if (status <= 0)
10743f542974SPaul B Schroeder 			return;
10753f542974SPaul B Schroeder 	}
10763f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
10779db276f8SPeter Hurley 	if (C_CRTSCTS(tty)) {
10783f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
107995da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
10803f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
108195da310eSAlan Cox 		if (status < 0)
10823f542974SPaul B Schroeder 			return;
10833f542974SPaul B Schroeder 	}
10843f542974SPaul B Schroeder }
10853f542974SPaul B Schroeder 
10863f542974SPaul B Schroeder /*****************************************************************************
10873f542974SPaul B Schroeder  * mos7840_unthrottle
1088880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
1089880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
1090880af9dbSAlan Cox  *	called)
10913f542974SPaul B Schroeder  *****************************************************************************/
109295da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
10933f542974SPaul B Schroeder {
109495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
10953f542974SPaul B Schroeder 	int status;
10963f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
10973f542974SPaul B Schroeder 
10983f542974SPaul B Schroeder 	if (mos7840_port == NULL)
10993f542974SPaul B Schroeder 		return;
11003f542974SPaul B Schroeder 
11013f542974SPaul B Schroeder 	if (!mos7840_port->open) {
11029c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
11033f542974SPaul B Schroeder 		return;
11043f542974SPaul B Schroeder 	}
11053f542974SPaul B Schroeder 
11063f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
11073f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
11083f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
110995da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
111095da310eSAlan Cox 		if (status <= 0)
11113f542974SPaul B Schroeder 			return;
11123f542974SPaul B Schroeder 	}
11133f542974SPaul B Schroeder 
11143f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
11159db276f8SPeter Hurley 	if (C_CRTSCTS(tty)) {
11163f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
111795da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
11183f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
111995da310eSAlan Cox 		if (status < 0)
11203f542974SPaul B Schroeder 			return;
11213f542974SPaul B Schroeder 	}
11223f542974SPaul B Schroeder }
11233f542974SPaul B Schroeder 
112460b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
11253f542974SPaul B Schroeder {
112695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11273f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11283f542974SPaul B Schroeder 	unsigned int result;
11293f542974SPaul B Schroeder 	__u16 msr;
11303f542974SPaul B Schroeder 	__u16 mcr;
1131880af9dbSAlan Cox 	int status;
11323f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11333f542974SPaul B Schroeder 
11343f542974SPaul B Schroeder 	if (mos7840_port == NULL)
11353f542974SPaul B Schroeder 		return -ENODEV;
11363f542974SPaul B Schroeder 
11373f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1138cd8db057SJohan Hovold 	if (status < 0)
1139a91ccd26SJohan Hovold 		return -EIO;
11403f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1141cd8db057SJohan Hovold 	if (status < 0)
1142a91ccd26SJohan Hovold 		return -EIO;
11433f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
11443f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
11453f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
11463f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
11473f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
11483f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
11493f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
11503f542974SPaul B Schroeder 
11519c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
11523f542974SPaul B Schroeder 
11533f542974SPaul B Schroeder 	return result;
11543f542974SPaul B Schroeder }
11553f542974SPaul B Schroeder 
115620b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
11573f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
11583f542974SPaul B Schroeder {
115995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11603f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11613f542974SPaul B Schroeder 	unsigned int mcr;
116287521c46SRoel Kluin 	int status;
11633f542974SPaul B Schroeder 
11643f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11653f542974SPaul B Schroeder 
11663f542974SPaul B Schroeder 	if (mos7840_port == NULL)
11673f542974SPaul B Schroeder 		return -ENODEV;
11683f542974SPaul B Schroeder 
1169e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
11703f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
11713f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
11723f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
11733f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
11743f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
11753f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
11763f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
11773f542974SPaul B Schroeder 
11783f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
11793f542974SPaul B Schroeder 		mcr |= MCR_RTS;
11803f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
11813f542974SPaul B Schroeder 		mcr |= MCR_DTR;
11823f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
11833f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
11843f542974SPaul B Schroeder 
11853f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
11863f542974SPaul B Schroeder 
11873f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
11883f542974SPaul B Schroeder 	if (status < 0) {
11899c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
119087521c46SRoel Kluin 		return status;
11913f542974SPaul B Schroeder 	}
11923f542974SPaul B Schroeder 
11933f542974SPaul B Schroeder 	return 0;
11943f542974SPaul B Schroeder }
11953f542974SPaul B Schroeder 
11963f542974SPaul B Schroeder /*****************************************************************************
11973f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
11983f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
11993f542974SPaul B Schroeder  *	baud rate.
12003f542974SPaul B Schroeder  *****************************************************************************/
12019c134a14SGreg Kroah-Hartman static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
12029c134a14SGreg Kroah-Hartman 					  int baudRate, int *divisor,
12033f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
12043f542974SPaul B Schroeder {
12059c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
12063f542974SPaul B Schroeder 
12073f542974SPaul B Schroeder 	if (baudRate <= 115200) {
12083f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
12093f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
12103f542974SPaul B Schroeder 	}
12113f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
12123f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
12133f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
12143f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
12153f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
12163f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
12173f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
12183f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
12193f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
12203f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
12213f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
12223f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
12233f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
12243f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
12253f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
12263f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
12273f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
12283f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
12293f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
12303f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
12313f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
12323f542974SPaul B Schroeder 	}
12333f542974SPaul B Schroeder 	return 0;
12343f542974SPaul B Schroeder }
12353f542974SPaul B Schroeder 
12363f542974SPaul B Schroeder /*****************************************************************************
12373f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
12383f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
12393f542974SPaul B Schroeder  *	specified port.
12403f542974SPaul B Schroeder  *****************************************************************************/
12413f542974SPaul B Schroeder 
12423f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
12433f542974SPaul B Schroeder 					    int baudRate)
12443f542974SPaul B Schroeder {
1245*ce039bd4SJohan Hovold 	struct usb_serial_port *port = mos7840_port->port;
12463f542974SPaul B Schroeder 	int divisor = 0;
12473f542974SPaul B Schroeder 	int status;
12483f542974SPaul B Schroeder 	__u16 Data;
12493f542974SPaul B Schroeder 	__u16 clk_sel_val;
12503f542974SPaul B Schroeder 
12519c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(port->serial, __func__))
12523f542974SPaul B Schroeder 		return -1;
12533f542974SPaul B Schroeder 
12541143832eSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1255880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
12563f542974SPaul B Schroeder 	if (baudRate > 115200) {
12573f542974SPaul B Schroeder #ifdef HW_flow_control
1258880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1259880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
12603f542974SPaul B Schroeder 		Data = 0x2b;
12613f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1262880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1263880af9dbSAlan Cox 									Data);
12643f542974SPaul B Schroeder 		if (status < 0) {
12659c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
12663f542974SPaul B Schroeder 			return -1;
12673f542974SPaul B Schroeder 		}
12683f542974SPaul B Schroeder #endif
12693f542974SPaul B Schroeder 
12703f542974SPaul B Schroeder 	} else {
12713f542974SPaul B Schroeder #ifdef HW_flow_control
1272880af9dbSAlan Cox 		/* setting h/w flow control bit to 0 */
12733f542974SPaul B Schroeder 		Data = 0xb;
12743f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1275880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1276880af9dbSAlan Cox 									Data);
12773f542974SPaul B Schroeder 		if (status < 0) {
12789c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
12793f542974SPaul B Schroeder 			return -1;
12803f542974SPaul B Schroeder 		}
12813f542974SPaul B Schroeder #endif
12823f542974SPaul B Schroeder 
12833f542974SPaul B Schroeder 	}
12843f542974SPaul B Schroeder 
1285880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
12863f542974SPaul B Schroeder 		clk_sel_val = 0x0;
12873f542974SPaul B Schroeder 		Data = 0x0;
12889c134a14SGreg Kroah-Hartman 		status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
12893f542974SPaul B Schroeder 						   &clk_sel_val);
1290880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
12913f542974SPaul B Schroeder 								 &Data);
12923f542974SPaul B Schroeder 		if (status < 0) {
12939c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
12943f542974SPaul B Schroeder 			return -1;
12953f542974SPaul B Schroeder 		}
12963f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1297880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1298880af9dbSAlan Cox 								Data);
12993f542974SPaul B Schroeder 		if (status < 0) {
13009c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
13013f542974SPaul B Schroeder 			return -1;
13023f542974SPaul B Schroeder 		}
13033f542974SPaul B Schroeder 		/* Calculate the Divisor */
13043f542974SPaul B Schroeder 
13053f542974SPaul B Schroeder 		if (status) {
1306194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
13073f542974SPaul B Schroeder 			return status;
13083f542974SPaul B Schroeder 		}
13093f542974SPaul B Schroeder 		/* Enable access to divisor latch */
13103f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
13113f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
13123f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
13133f542974SPaul B Schroeder 
13143f542974SPaul B Schroeder 		/* Write the divisor */
13153f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
13169c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
13173f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
13183f542974SPaul B Schroeder 
13193f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
13209c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
13213f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
13223f542974SPaul B Schroeder 
13233f542974SPaul B Schroeder 		/* Disable access to divisor latch */
13243f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
13253f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
13263f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
13273f542974SPaul B Schroeder 
13283f542974SPaul B Schroeder 	}
13293f542974SPaul B Schroeder 	return status;
13303f542974SPaul B Schroeder }
13313f542974SPaul B Schroeder 
13323f542974SPaul B Schroeder /*****************************************************************************
13333f542974SPaul B Schroeder  * mos7840_change_port_settings
13343f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
13353f542974SPaul B Schroeder  *      the specified new settings.
13363f542974SPaul B Schroeder  *****************************************************************************/
13373f542974SPaul B Schroeder 
133895da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
133995da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
13403f542974SPaul B Schroeder {
1341*ce039bd4SJohan Hovold 	struct usb_serial_port *port = mos7840_port->port;
13423f542974SPaul B Schroeder 	int baud;
13433f542974SPaul B Schroeder 	unsigned cflag;
13443f542974SPaul B Schroeder 	__u8 lData;
13453f542974SPaul B Schroeder 	__u8 lParity;
13463f542974SPaul B Schroeder 	__u8 lStop;
13473f542974SPaul B Schroeder 	int status;
13483f542974SPaul B Schroeder 	__u16 Data;
13493f542974SPaul B Schroeder 
13509c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(port->serial, __func__))
13513f542974SPaul B Schroeder 		return;
13523f542974SPaul B Schroeder 
13533f542974SPaul B Schroeder 	if (!mos7840_port->open) {
13549c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
13553f542974SPaul B Schroeder 		return;
13563f542974SPaul B Schroeder 	}
13573f542974SPaul B Schroeder 
13583f542974SPaul B Schroeder 	lData = LCR_BITS_8;
13593f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
13603f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
13613f542974SPaul B Schroeder 
1362adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
13633f542974SPaul B Schroeder 
13643f542974SPaul B Schroeder 	/* Change the number of bits */
13653f542974SPaul B Schroeder 	switch (cflag & CSIZE) {
13663f542974SPaul B Schroeder 	case CS5:
13673f542974SPaul B Schroeder 		lData = LCR_BITS_5;
13683f542974SPaul B Schroeder 		break;
13693f542974SPaul B Schroeder 
13703f542974SPaul B Schroeder 	case CS6:
13713f542974SPaul B Schroeder 		lData = LCR_BITS_6;
13723f542974SPaul B Schroeder 		break;
13733f542974SPaul B Schroeder 
13743f542974SPaul B Schroeder 	case CS7:
13753f542974SPaul B Schroeder 		lData = LCR_BITS_7;
13763f542974SPaul B Schroeder 		break;
137778692cc3SColin Leitner 
13783f542974SPaul B Schroeder 	default:
13793f542974SPaul B Schroeder 	case CS8:
13803f542974SPaul B Schroeder 		lData = LCR_BITS_8;
13813f542974SPaul B Schroeder 		break;
13823f542974SPaul B Schroeder 	}
138378692cc3SColin Leitner 
13843f542974SPaul B Schroeder 	/* Change the Parity bit */
13853f542974SPaul B Schroeder 	if (cflag & PARENB) {
13863f542974SPaul B Schroeder 		if (cflag & PARODD) {
13873f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
13889c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
13893f542974SPaul B Schroeder 		} else {
13903f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
13919c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
13923f542974SPaul B Schroeder 		}
13933f542974SPaul B Schroeder 
13943f542974SPaul B Schroeder 	} else {
13959c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
13963f542974SPaul B Schroeder 	}
13973f542974SPaul B Schroeder 
1398880af9dbSAlan Cox 	if (cflag & CMSPAR)
13993f542974SPaul B Schroeder 		lParity = lParity | 0x20;
14003f542974SPaul B Schroeder 
14013f542974SPaul B Schroeder 	/* Change the Stop bit */
14023f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
14033f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
14049c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
14053f542974SPaul B Schroeder 	} else {
14063f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
14079c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
14083f542974SPaul B Schroeder 	}
14093f542974SPaul B Schroeder 
14103f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
14113f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
14123f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
14133f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
14143f542974SPaul B Schroeder 
14159c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
14163f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
14173f542974SPaul B Schroeder 	/* Disable Interrupts */
14183f542974SPaul B Schroeder 	Data = 0x00;
14193f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
14203f542974SPaul B Schroeder 
14213f542974SPaul B Schroeder 	Data = 0x00;
14223f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
14233f542974SPaul B Schroeder 
14243f542974SPaul B Schroeder 	Data = 0xcf;
14253f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
14263f542974SPaul B Schroeder 
14273f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
14283f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
14293f542974SPaul B Schroeder 
14303f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14313f542974SPaul B Schroeder 
14323f542974SPaul B Schroeder 	Data = 0x00b;
14333f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
14343f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
14353f542974SPaul B Schroeder 	Data = 0x00b;
14363f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
14373f542974SPaul B Schroeder 
14383f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
14393f542974SPaul B Schroeder 
14403f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
1441880af9dbSAlan Cox 	if (cflag & CBAUD)
14423f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
14433f542974SPaul B Schroeder 
1444880af9dbSAlan Cox 	if (cflag & CRTSCTS)
14453f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
1446880af9dbSAlan Cox 	else
14473f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
14483f542974SPaul B Schroeder 
14493f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
14503f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
14513f542974SPaul B Schroeder 
14523f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
14533f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
14543f542974SPaul B Schroeder 
14553f542974SPaul B Schroeder 	if (!baud) {
14563f542974SPaul B Schroeder 		/* pick a default, any default... */
14579c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "Picked default baud...\n");
14583f542974SPaul B Schroeder 		baud = 9600;
14593f542974SPaul B Schroeder 	}
14603f542974SPaul B Schroeder 
14619c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
14623f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
14633f542974SPaul B Schroeder 
14643f542974SPaul B Schroeder 	/* Enable Interrupts */
14653f542974SPaul B Schroeder 	Data = 0x0c;
14663f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
14673f542974SPaul B Schroeder 
1468ce9d8562SMathieu OTHACEHE 	if (!mos7840_port->read_urb_busy) {
146950de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
14709d380190SJohan Hovold 		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
14713f542974SPaul B Schroeder 		if (status) {
14729c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
14733f542974SPaul B Schroeder 			    status);
147450de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
14753f542974SPaul B Schroeder 		}
14763f542974SPaul B Schroeder 	}
14779c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
14783f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
14793f542974SPaul B Schroeder }
14803f542974SPaul B Schroeder 
14813f542974SPaul B Schroeder /*****************************************************************************
14823f542974SPaul B Schroeder  * mos7840_set_termios
14833f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
14843f542974SPaul B Schroeder  *	the termios structure
14853f542974SPaul B Schroeder  *****************************************************************************/
14863f542974SPaul B Schroeder 
148795da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
148895da310eSAlan Cox 				struct usb_serial_port *port,
1489606d099cSAlan Cox 				struct ktermios *old_termios)
14903f542974SPaul B Schroeder {
1491*ce039bd4SJohan Hovold 	struct usb_serial *serial = port->serial;
14923f542974SPaul B Schroeder 	int status;
14933f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14943363155bSGreg Kroah-Hartman 
14953f542974SPaul B Schroeder 
14969c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
14973f542974SPaul B Schroeder 		return;
14983f542974SPaul B Schroeder 
14993f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15003f542974SPaul B Schroeder 
15013f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15023f542974SPaul B Schroeder 		return;
15033f542974SPaul B Schroeder 
15043f542974SPaul B Schroeder 	if (!mos7840_port->open) {
15059c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
15063f542974SPaul B Schroeder 		return;
15073f542974SPaul B Schroeder 	}
15083f542974SPaul B Schroeder 
15093f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
15103f542974SPaul B Schroeder 
151195da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
15123f542974SPaul B Schroeder 
15133f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
15149c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
15153f542974SPaul B Schroeder 		return;
15163f542974SPaul B Schroeder 	}
15173f542974SPaul B Schroeder 
1518ce9d8562SMathieu OTHACEHE 	if (!mos7840_port->read_urb_busy) {
151950de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
15209d380190SJohan Hovold 		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
15213f542974SPaul B Schroeder 		if (status) {
15229c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
15233f542974SPaul B Schroeder 			    status);
152450de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
15253f542974SPaul B Schroeder 		}
15263f542974SPaul B Schroeder 	}
15273f542974SPaul B Schroeder }
15283f542974SPaul B Schroeder 
15293f542974SPaul B Schroeder /*****************************************************************************
15303f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
15313f542974SPaul B Schroeder  *
15323f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
15333f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
15343f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
15353f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
15363f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
15373f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
15383f542974SPaul B Schroeder  *****************************************************************************/
15393f542974SPaul B Schroeder 
154095da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
154197c4965dSAl Viro 				unsigned int __user *value)
15423f542974SPaul B Schroeder {
15433f542974SPaul B Schroeder 	int count;
15443f542974SPaul B Schroeder 	unsigned int result = 0;
15453f542974SPaul B Schroeder 
154695da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
15479c134a14SGreg Kroah-Hartman 	if (count == 0)
15483f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
15493f542974SPaul B Schroeder 
15503f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
15513f542974SPaul B Schroeder 		return -EFAULT;
15523f542974SPaul B Schroeder 	return 0;
15533f542974SPaul B Schroeder }
15543f542974SPaul B Schroeder 
15553f542974SPaul B Schroeder /*****************************************************************************
15563f542974SPaul B Schroeder  * mos7840_get_serial_info
15573f542974SPaul B Schroeder  *      function to get information about serial port
15583f542974SPaul B Schroeder  *****************************************************************************/
15593f542974SPaul B Schroeder 
1560b27ef409SAl Viro static int mos7840_get_serial_info(struct tty_struct *tty,
1561b27ef409SAl Viro 				   struct serial_struct *ss)
15623f542974SPaul B Schroeder {
1563b27ef409SAl Viro 	struct usb_serial_port *port = tty->driver_data;
1564b27ef409SAl Viro 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
15653f542974SPaul B Schroeder 
1566b27ef409SAl Viro 	ss->type = PORT_16550A;
1567b27ef409SAl Viro 	ss->line = mos7840_port->port->minor;
1568b27ef409SAl Viro 	ss->port = mos7840_port->port->port_number;
1569b27ef409SAl Viro 	ss->irq = 0;
1570b27ef409SAl Viro 	ss->xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1571b27ef409SAl Viro 	ss->baud_base = 9600;
1572b27ef409SAl Viro 	ss->close_delay = 5 * HZ;
1573b27ef409SAl Viro 	ss->closing_wait = 30 * HZ;
15743f542974SPaul B Schroeder 	return 0;
15753f542974SPaul B Schroeder }
15763f542974SPaul B Schroeder 
15773f542974SPaul B Schroeder /*****************************************************************************
15783f542974SPaul B Schroeder  * SerialIoctl
15793f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
15803f542974SPaul B Schroeder  *****************************************************************************/
15813f542974SPaul B Schroeder 
158200a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
15833f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
15843f542974SPaul B Schroeder {
158595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
158697c4965dSAl Viro 	void __user *argp = (void __user *)arg;
15873f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15883f542974SPaul B Schroeder 
15893f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15903f542974SPaul B Schroeder 
15913f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15923f542974SPaul B Schroeder 		return -1;
15933f542974SPaul B Schroeder 
15943f542974SPaul B Schroeder 	switch (cmd) {
15953f542974SPaul B Schroeder 		/* return number of bytes available */
15963f542974SPaul B Schroeder 
15973f542974SPaul B Schroeder 	case TIOCSERGETLSR:
15989c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
159995da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
16003f542974SPaul B Schroeder 
16013f542974SPaul B Schroeder 	default:
16023f542974SPaul B Schroeder 		break;
16033f542974SPaul B Schroeder 	}
16043f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
16053f542974SPaul B Schroeder }
16063f542974SPaul B Schroeder 
16071c333550SJohan Hovold /*
16081c333550SJohan Hovold  * Check if GPO (pin 42) is connected to GPI (pin 33) as recommended by ASIX
16091c333550SJohan Hovold  * for MCS7810 by bit-banging a 16-bit word.
16101c333550SJohan Hovold  *
16111c333550SJohan Hovold  * Note that GPO is really RTS of the third port so this will toggle RTS of
16121c333550SJohan Hovold  * port two or three on two- and four-port devices.
16131c333550SJohan Hovold  */
16140eafe4deSDonald static int mos7810_check(struct usb_serial *serial)
16153f542974SPaul B Schroeder {
16160eafe4deSDonald 	int i, pass_count = 0;
161715ee89c3SJohan Hovold 	u8 *buf;
16180eafe4deSDonald 	__u16 data = 0, mcr_data = 0;
16190eafe4deSDonald 	__u16 test_pattern = 0x55AA;
162015ee89c3SJohan Hovold 	int res;
162115ee89c3SJohan Hovold 
162215ee89c3SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
162315ee89c3SJohan Hovold 	if (!buf)
162415ee89c3SJohan Hovold 		return 0;	/* failed to identify 7810 */
16253f542974SPaul B Schroeder 
16260eafe4deSDonald 	/* Store MCR setting */
162715ee89c3SJohan Hovold 	res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
16280eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
162915ee89c3SJohan Hovold 		buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
163015ee89c3SJohan Hovold 	if (res == VENDOR_READ_LENGTH)
163115ee89c3SJohan Hovold 		mcr_data = *buf;
16320eafe4deSDonald 
16330eafe4deSDonald 	for (i = 0; i < 16; i++) {
16340eafe4deSDonald 		/* Send the 1-bit test pattern out to MCS7810 test pin */
16350eafe4deSDonald 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
16360eafe4deSDonald 			MCS_WRREQ, MCS_WR_RTYPE,
16370eafe4deSDonald 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
16380eafe4deSDonald 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
16390eafe4deSDonald 
16400eafe4deSDonald 		/* Read the test pattern back */
164115ee89c3SJohan Hovold 		res = usb_control_msg(serial->dev,
164215ee89c3SJohan Hovold 				usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
164315ee89c3SJohan Hovold 				MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
1644093ea2d3SDonald Lee 				VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
164515ee89c3SJohan Hovold 		if (res == VENDOR_READ_LENGTH)
164615ee89c3SJohan Hovold 			data = *buf;
1647093ea2d3SDonald Lee 
16480eafe4deSDonald 		/* If this is a MCS7810 device, both test patterns must match */
16490eafe4deSDonald 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
16500eafe4deSDonald 			break;
16510eafe4deSDonald 
16520eafe4deSDonald 		pass_count++;
16533f542974SPaul B Schroeder 	}
1654093ea2d3SDonald Lee 
16550eafe4deSDonald 	/* Restore MCR setting */
16560eafe4deSDonald 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
16570eafe4deSDonald 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
16580eafe4deSDonald 		0, MOS_WDR_TIMEOUT);
16590eafe4deSDonald 
166015ee89c3SJohan Hovold 	kfree(buf);
166115ee89c3SJohan Hovold 
16620eafe4deSDonald 	if (pass_count == 16)
16630eafe4deSDonald 		return 1;
16640eafe4deSDonald 
16650eafe4deSDonald 	return 0;
16660eafe4deSDonald }
16670eafe4deSDonald 
166840c24f28SJohan Hovold static int mos7840_probe(struct usb_serial *serial,
166940c24f28SJohan Hovold 				const struct usb_device_id *id)
16700eafe4deSDonald {
1671375cb533SJohan Hovold 	unsigned long device_flags = id->driver_info;
167215ee89c3SJohan Hovold 	u8 *buf;
167340c24f28SJohan Hovold 
1674375cb533SJohan Hovold 	/* Skip device-type detection if we already have device flags. */
1675375cb533SJohan Hovold 	if (device_flags)
167640c24f28SJohan Hovold 		goto out;
1677e696d00eSPavel Löbl 
167815ee89c3SJohan Hovold 	buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
167940c24f28SJohan Hovold 	if (!buf)
168040c24f28SJohan Hovold 		return -ENOMEM;
168140c24f28SJohan Hovold 
16820eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
168315ee89c3SJohan Hovold 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
16840eafe4deSDonald 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
16850eafe4deSDonald 
16860eafe4deSDonald 	/* For a MCS7840 device GPIO0 must be set to 1 */
168740c24f28SJohan Hovold 	if (buf[0] & 0x01)
1688375cb533SJohan Hovold 		device_flags = MCS_PORTS(4);
16890eafe4deSDonald 	else if (mos7810_check(serial))
1690375cb533SJohan Hovold 		device_flags = MCS_PORTS(1) | MCS_LED;
16910eafe4deSDonald 	else
1692375cb533SJohan Hovold 		device_flags = MCS_PORTS(2);
169340c24f28SJohan Hovold 
169440c24f28SJohan Hovold 	kfree(buf);
169540c24f28SJohan Hovold out:
1696375cb533SJohan Hovold 	usb_set_serial_data(serial, (void *)device_flags);
169740c24f28SJohan Hovold 
169840c24f28SJohan Hovold 	return 0;
16990eafe4deSDonald }
17000eafe4deSDonald 
170107814246SJohan Hovold static int mos7840_calc_num_ports(struct usb_serial *serial,
170207814246SJohan Hovold 					struct usb_serial_endpoints *epds)
170340c24f28SJohan Hovold {
1704375cb533SJohan Hovold 	unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
1705375cb533SJohan Hovold 	int num_ports = MCS_PORTS(device_flags);
170640c24f28SJohan Hovold 
1707375cb533SJohan Hovold 	if (num_ports == 0 || num_ports > 4)
170895254020SJohan Hovold 		return -ENODEV;
17093f542974SPaul B Schroeder 
171095254020SJohan Hovold 	if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
17115c75633eSJohan Hovold 		dev_err(&serial->interface->dev, "missing endpoints\n");
17125c75633eSJohan Hovold 		return -ENODEV;
17135c75633eSJohan Hovold 	}
17145c75633eSJohan Hovold 
171595254020SJohan Hovold 	return num_ports;
17165c75633eSJohan Hovold }
17175c75633eSJohan Hovold 
1718960fbd1cSJohan Hovold static int mos7840_attach(struct usb_serial *serial)
1719960fbd1cSJohan Hovold {
1720960fbd1cSJohan Hovold 	struct device *dev = &serial->interface->dev;
1721960fbd1cSJohan Hovold 	int status;
1722960fbd1cSJohan Hovold 	u16 val;
1723960fbd1cSJohan Hovold 
1724960fbd1cSJohan Hovold 	/* Zero Length flag enable */
1725960fbd1cSJohan Hovold 	val = 0x0f;
1726960fbd1cSJohan Hovold 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, val);
1727960fbd1cSJohan Hovold 	if (status < 0)
1728960fbd1cSJohan Hovold 		dev_dbg(dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
1729960fbd1cSJohan Hovold 	else
1730960fbd1cSJohan Hovold 		dev_dbg(dev, "ZLP_REG5 Writing success status%d\n", status);
1731960fbd1cSJohan Hovold 
1732960fbd1cSJohan Hovold 	return status;
1733960fbd1cSJohan Hovold }
1734960fbd1cSJohan Hovold 
173580c00750SJohan Hovold static int mos7840_port_probe(struct usb_serial_port *port)
17363f542974SPaul B Schroeder {
173780c00750SJohan Hovold 	struct usb_serial *serial = port->serial;
1738375cb533SJohan Hovold 	unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
17393f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
174080c00750SJohan Hovold 	int status;
174180c00750SJohan Hovold 	int pnum;
17423f542974SPaul B Schroeder 	__u16 Data;
17433f542974SPaul B Schroeder 
17443f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
17453f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
17463f542974SPaul B Schroeder 
17471143832eSGreg Kroah-Hartman 	pnum = port->port_number;
174880c00750SJohan Hovold 
1749ae685effSJohan Hovold 	dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
17507ac9da10SBurman Yan 	mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
175110c642d0SJohan Hovold 	if (!mos7840_port)
175280c00750SJohan Hovold 		return -ENOMEM;
17533f542974SPaul B Schroeder 
1754880af9dbSAlan Cox 	/* Initialize all port interrupt end point to port 0 int
1755880af9dbSAlan Cox 	 * endpoint. Our device has only one interrupt end point
1756880af9dbSAlan Cox 	 * common to all port */
17573f542974SPaul B Schroeder 
175880c00750SJohan Hovold 	mos7840_port->port = port;
175980c00750SJohan Hovold 	mos7840_set_port_private(port, mos7840_port);
17600de9a702SOliver Neukum 	spin_lock_init(&mos7840_port->pool_lock);
17613f542974SPaul B Schroeder 
176237768adfSTony Cook 	/* minor is not initialised until later by
176337768adfSTony Cook 	 * usb-serial.c:get_free_serial() and cannot therefore be used
176437768adfSTony Cook 	 * to index device instances */
176580c00750SJohan Hovold 	mos7840_port->port_num = pnum + 1;
1766e5b1e206SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
176780c00750SJohan Hovold 	dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
17683f542974SPaul B Schroeder 
17693f542974SPaul B Schroeder 	if (mos7840_port->port_num == 1) {
17703f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0x0;
17713f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0x1;
17723f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x4;
1773e8603076SJackyChou 	} else {
1774e8603076SJackyChou 		u8 phy_num = mos7840_port->port_num;
1775e8603076SJackyChou 
1776e8603076SJackyChou 		/* Port 2 in the 2-port case uses registers of port 3 */
1777e8603076SJackyChou 		if (serial->num_ports == 2)
1778e8603076SJackyChou 			phy_num = 3;
1779e8603076SJackyChou 
1780e8603076SJackyChou 		mos7840_port->SpRegOffset = 0x8 + 2 * (phy_num - 2);
1781e8603076SJackyChou 		mos7840_port->ControlRegOffset = 0x9 + 2 * (phy_num - 2);
1782e8603076SJackyChou 		mos7840_port->DcrRegOffset = 0x16 + 3 * (phy_num - 2);
17833f542974SPaul B Schroeder 	}
178480c00750SJohan Hovold 	mos7840_dump_serial_port(port, mos7840_port);
178580c00750SJohan Hovold 	mos7840_set_port_private(port, mos7840_port);
17863f542974SPaul B Schroeder 
1787880af9dbSAlan Cox 	/* enable rx_disable bit in control register */
178880c00750SJohan Hovold 	status = mos7840_get_reg_sync(port,
17893f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, &Data);
17903f542974SPaul B Schroeder 	if (status < 0) {
179180c00750SJohan Hovold 		dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
1792960fbd1cSJohan Hovold 		goto error;
17933f542974SPaul B Schroeder 	} else
179480c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
1795880af9dbSAlan Cox 	Data |= 0x08;	/* setting driver done bit */
1796880af9dbSAlan Cox 	Data |= 0x04;	/* sp1_bit to have cts change reflect in
1797880af9dbSAlan Cox 			   modem status reg */
17983f542974SPaul B Schroeder 
1799880af9dbSAlan Cox 	/* Data |= 0x20; //rx_disable bit */
180080c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
18013f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, Data);
18023f542974SPaul B Schroeder 	if (status < 0) {
180380c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
1804960fbd1cSJohan Hovold 		goto error;
18053f542974SPaul B Schroeder 	} else
180680c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
18073f542974SPaul B Schroeder 
1808880af9dbSAlan Cox 	/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
1809880af9dbSAlan Cox 	   and 0x24 in DCR3 */
18103f542974SPaul B Schroeder 	Data = 0x01;
181180c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
1812880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 0), Data);
18133f542974SPaul B Schroeder 	if (status < 0) {
181480c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
1815960fbd1cSJohan Hovold 		goto error;
18163f542974SPaul B Schroeder 	} else
181780c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
18183f542974SPaul B Schroeder 
18193f542974SPaul B Schroeder 	Data = 0x05;
182080c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
1821880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 1), Data);
18223f542974SPaul B Schroeder 	if (status < 0) {
182380c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
1824960fbd1cSJohan Hovold 		goto error;
18253f542974SPaul B Schroeder 	} else
182680c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
18273f542974SPaul B Schroeder 
18283f542974SPaul B Schroeder 	Data = 0x24;
182980c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
1830880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 2), Data);
18313f542974SPaul B Schroeder 	if (status < 0) {
183280c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
1833960fbd1cSJohan Hovold 		goto error;
18343f542974SPaul B Schroeder 	} else
183580c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
18363f542974SPaul B Schroeder 
1837880af9dbSAlan Cox 	/* write values in clkstart0x0 and clkmulti 0x20 */
18383f542974SPaul B Schroeder 	Data = 0x0;
1839ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
18403f542974SPaul B Schroeder 	if (status < 0) {
184180c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
1842960fbd1cSJohan Hovold 		goto error;
18433f542974SPaul B Schroeder 	} else
184480c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
18453f542974SPaul B Schroeder 
18463f542974SPaul B Schroeder 	Data = 0x20;
1847ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
18483f542974SPaul B Schroeder 	if (status < 0) {
184980c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
18500de9a702SOliver Neukum 		goto error;
18513f542974SPaul B Schroeder 	} else
185280c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
18533f542974SPaul B Schroeder 
1854880af9dbSAlan Cox 	/* write value 0x0 to scratchpad register */
18553f542974SPaul B Schroeder 	Data = 0x00;
1856ae685effSJohan Hovold 	status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
18573f542974SPaul B Schroeder 	if (status < 0) {
185880c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
1859960fbd1cSJohan Hovold 		goto error;
18603f542974SPaul B Schroeder 	} else
186180c00750SJohan Hovold 		dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
18623f542974SPaul B Schroeder 
1863880af9dbSAlan Cox 	/* Zero Length flag register */
1864ae685effSJohan Hovold 	if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
18653f542974SPaul B Schroeder 		Data = 0xff;
186680c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
18673f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
1868880af9dbSAlan Cox 					((__u16)mos7840_port->port_num)), Data);
186980c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
18709c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
18713f542974SPaul B Schroeder 		if (status < 0) {
187280c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
1873960fbd1cSJohan Hovold 			goto error;
18743f542974SPaul B Schroeder 		} else
187580c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
18763f542974SPaul B Schroeder 	} else {
18773f542974SPaul B Schroeder 		Data = 0xff;
187880c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
18793f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
1880880af9dbSAlan Cox 					((__u16)mos7840_port->port_num) - 0x1), Data);
188180c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
18829c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
18833f542974SPaul B Schroeder 		if (status < 0) {
188480c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
1885960fbd1cSJohan Hovold 			goto error;
18863f542974SPaul B Schroeder 		} else
188780c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
18883f542974SPaul B Schroeder 
18893f542974SPaul B Schroeder 	}
18900eafe4deSDonald 
1891375cb533SJohan Hovold 	mos7840_port->has_led = device_flags & MCS_LED;
18920eafe4deSDonald 
18930eafe4deSDonald 	/* Initialize LED timers */
1894375cb533SJohan Hovold 	if (mos7840_port->has_led) {
189505cf0decSJohan Hovold 		mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
189605cf0decSJohan Hovold 		mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
189705cf0decSJohan Hovold 								GFP_KERNEL);
189805cf0decSJohan Hovold 		if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
189905cf0decSJohan Hovold 			status = -ENOMEM;
190005cf0decSJohan Hovold 			goto error;
190105cf0decSJohan Hovold 		}
190205cf0decSJohan Hovold 
1903e99e88a9SKees Cook 		timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
19040eafe4deSDonald 		mos7840_port->led_timer1.expires =
19050eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_ON_MS);
1906e99e88a9SKees Cook 		timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
1907e99e88a9SKees Cook 			    0);
19080eafe4deSDonald 		mos7840_port->led_timer2.expires =
19090eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_OFF_MS);
19100eafe4deSDonald 
19110eafe4deSDonald 		/* Turn off LED */
1912ae685effSJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
19130eafe4deSDonald 	}
1914960fbd1cSJohan Hovold 
19153f542974SPaul B Schroeder 	return 0;
19160de9a702SOliver Neukum error:
191705cf0decSJohan Hovold 	kfree(mos7840_port->led_dr);
191805cf0decSJohan Hovold 	usb_free_urb(mos7840_port->led_urb);
19190de9a702SOliver Neukum 	kfree(mos7840_port);
192080c00750SJohan Hovold 
19210de9a702SOliver Neukum 	return status;
19223f542974SPaul B Schroeder }
19233f542974SPaul B Schroeder 
192480c00750SJohan Hovold static int mos7840_port_remove(struct usb_serial_port *port)
19253f542974SPaul B Schroeder {
19263f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
19273f542974SPaul B Schroeder 
192880c00750SJohan Hovold 	mos7840_port = mos7840_get_port_private(port);
19293f542974SPaul B Schroeder 
19300eafe4deSDonald 	if (mos7840_port->has_led) {
19310eafe4deSDonald 		/* Turn off LED */
193280c00750SJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
19330eafe4deSDonald 
19340eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer1);
19350eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer2);
193605cf0decSJohan Hovold 
193705cf0decSJohan Hovold 		usb_kill_urb(mos7840_port->led_urb);
193805cf0decSJohan Hovold 		usb_free_urb(mos7840_port->led_urb);
193905cf0decSJohan Hovold 		kfree(mos7840_port->led_dr);
19400eafe4deSDonald 	}
194171831921SJohan Hovold 
19423f542974SPaul B Schroeder 	kfree(mos7840_port);
194380c00750SJohan Hovold 
194480c00750SJohan Hovold 	return 0;
19453f542974SPaul B Schroeder }
19463f542974SPaul B Schroeder 
19473f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
19483f542974SPaul B Schroeder 	.driver = {
19493f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
19503f542974SPaul B Schroeder 		   .name = "mos7840",
19513f542974SPaul B Schroeder 		   },
19523f542974SPaul B Schroeder 	.description = DRIVER_DESC,
195368e24113SGreg Kroah-Hartman 	.id_table = id_table,
195495254020SJohan Hovold 	.num_interrupt_in = 1,
19553f542974SPaul B Schroeder 	.open = mos7840_open,
19563f542974SPaul B Schroeder 	.close = mos7840_close,
19573f542974SPaul B Schroeder 	.write = mos7840_write,
19583f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
19593f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
19603f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
19613f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
19623f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
196340c24f28SJohan Hovold 	.probe = mos7840_probe,
1964960fbd1cSJohan Hovold 	.attach = mos7840_attach,
19653f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
1966b27ef409SAl Viro 	.get_serial = mos7840_get_serial_info,
19673f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
19683f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
19693f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
19703f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
19718c1a07ffSJohan Hovold 	.get_icount = usb_serial_generic_get_icount,
197280c00750SJohan Hovold 	.port_probe = mos7840_port_probe,
197380c00750SJohan Hovold 	.port_remove = mos7840_port_remove,
19743f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
19753f542974SPaul B Schroeder };
19763f542974SPaul B Schroeder 
19774d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
19784d2a7affSAlan Stern 	&moschip7840_4port_device, NULL
19794d2a7affSAlan Stern };
19804d2a7affSAlan Stern 
198168e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
19823f542974SPaul B Schroeder 
19833f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
19843f542974SPaul B Schroeder MODULE_LICENSE("GPL");
1985