xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision 1e658489ba8d87b7c89ca6f734b8319acc534dbc)
13f542974SPaul B Schroeder /*
23f542974SPaul B Schroeder  * This program is free software; you can redistribute it and/or modify
33f542974SPaul B Schroeder  * it under the terms of the GNU General Public License as published by
43f542974SPaul B Schroeder  * the Free Software Foundation; either version 2 of the License, or
53f542974SPaul B Schroeder  * (at your option) any later version.
63f542974SPaul B Schroeder  *
73f542974SPaul B Schroeder  * This program is distributed in the hope that it will be useful,
83f542974SPaul B Schroeder  * but WITHOUT ANY WARRANTY; without even the implied warranty of
93f542974SPaul B Schroeder  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
103f542974SPaul B Schroeder  * GNU General Public License for more details.
113f542974SPaul B Schroeder  *
123f542974SPaul B Schroeder  * You should have received a copy of the GNU General Public License
133f542974SPaul B Schroeder  * along with this program; if not, write to the Free Software
143f542974SPaul B Schroeder  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
153f542974SPaul B Schroeder  *
163f542974SPaul B Schroeder  * Clean ups from Moschip version and a few ioctl implementations by:
173f542974SPaul B Schroeder  *	Paul B Schroeder <pschroeder "at" uplogix "dot" com>
183f542974SPaul B Schroeder  *
193f542974SPaul B Schroeder  * Originally based on drivers/usb/serial/io_edgeport.c which is:
203f542974SPaul B Schroeder  *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
213f542974SPaul B Schroeder  *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
223f542974SPaul B Schroeder  *
233f542974SPaul B Schroeder  */
243f542974SPaul B Schroeder 
253f542974SPaul B Schroeder #include <linux/kernel.h>
263f542974SPaul B Schroeder #include <linux/errno.h>
273f542974SPaul B Schroeder #include <linux/init.h>
283f542974SPaul B Schroeder #include <linux/slab.h>
293f542974SPaul B Schroeder #include <linux/tty.h>
303f542974SPaul B Schroeder #include <linux/tty_driver.h>
313f542974SPaul B Schroeder #include <linux/tty_flip.h>
323f542974SPaul B Schroeder #include <linux/module.h>
333f542974SPaul B Schroeder #include <linux/serial.h>
343f542974SPaul B Schroeder #include <linux/usb.h>
353f542974SPaul B Schroeder #include <linux/usb/serial.h>
36880af9dbSAlan Cox #include <linux/uaccess.h>
373f542974SPaul B Schroeder 
383f542974SPaul B Schroeder /*
393f542974SPaul B Schroeder  * Version Information
403f542974SPaul B Schroeder  */
4137768adfSTony Cook #define DRIVER_VERSION "1.3.2"
423f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
433f542974SPaul B Schroeder 
443f542974SPaul B Schroeder /*
453f542974SPaul B Schroeder  * 16C50 UART register defines
463f542974SPaul B Schroeder  */
473f542974SPaul B Schroeder 
483f542974SPaul B Schroeder #define LCR_BITS_5             0x00	/* 5 bits/char */
493f542974SPaul B Schroeder #define LCR_BITS_6             0x01	/* 6 bits/char */
503f542974SPaul B Schroeder #define LCR_BITS_7             0x02	/* 7 bits/char */
513f542974SPaul B Schroeder #define LCR_BITS_8             0x03	/* 8 bits/char */
523f542974SPaul B Schroeder #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
533f542974SPaul B Schroeder 
543f542974SPaul B Schroeder #define LCR_STOP_1             0x00	/* 1 stop bit */
553f542974SPaul B Schroeder #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
563f542974SPaul B Schroeder #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
573f542974SPaul B Schroeder #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
583f542974SPaul B Schroeder 
593f542974SPaul B Schroeder #define LCR_PAR_NONE           0x00	/* No parity */
603f542974SPaul B Schroeder #define LCR_PAR_ODD            0x08	/* Odd parity */
613f542974SPaul B Schroeder #define LCR_PAR_EVEN           0x18	/* Even parity */
623f542974SPaul B Schroeder #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
633f542974SPaul B Schroeder #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
643f542974SPaul B Schroeder #define LCR_PAR_MASK           0x38	/* Mask for parity field */
653f542974SPaul B Schroeder 
663f542974SPaul B Schroeder #define LCR_SET_BREAK          0x40	/* Set Break condition */
673f542974SPaul B Schroeder #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
683f542974SPaul B Schroeder 
693f542974SPaul B Schroeder #define MCR_DTR                0x01	/* Assert DTR */
703f542974SPaul B Schroeder #define MCR_RTS                0x02	/* Assert RTS */
713f542974SPaul B Schroeder #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
723f542974SPaul B Schroeder #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
733f542974SPaul B Schroeder #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
743f542974SPaul B Schroeder #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
753f542974SPaul B Schroeder 
763f542974SPaul B Schroeder #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
773f542974SPaul B Schroeder #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
783f542974SPaul B Schroeder #define MOS7840_MSR_RI         0x40	/* Current state of RI */
793f542974SPaul B Schroeder #define MOS7840_MSR_CD         0x80	/* Current state of CD */
803f542974SPaul B Schroeder 
813f542974SPaul B Schroeder /*
823f542974SPaul B Schroeder  * Defines used for sending commands to port
833f542974SPaul B Schroeder  */
843f542974SPaul B Schroeder 
85*1e658489SMark Ferrell #define MOS_WDR_TIMEOUT		5000	/* default urb timeout */
863f542974SPaul B Schroeder 
873f542974SPaul B Schroeder #define MOS_PORT1       0x0200
883f542974SPaul B Schroeder #define MOS_PORT2       0x0300
893f542974SPaul B Schroeder #define MOS_VENREG      0x0000
903f542974SPaul B Schroeder #define MOS_MAX_PORT	0x02
913f542974SPaul B Schroeder #define MOS_WRITE       0x0E
923f542974SPaul B Schroeder #define MOS_READ        0x0D
933f542974SPaul B Schroeder 
943f542974SPaul B Schroeder /* Requests */
953f542974SPaul B Schroeder #define MCS_RD_RTYPE    0xC0
963f542974SPaul B Schroeder #define MCS_WR_RTYPE    0x40
973f542974SPaul B Schroeder #define MCS_RDREQ       0x0D
983f542974SPaul B Schroeder #define MCS_WRREQ       0x0E
993f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT        500
1003f542974SPaul B Schroeder #define VENDOR_READ_LENGTH      (0x01)
1013f542974SPaul B Schroeder 
1023f542974SPaul B Schroeder #define MAX_NAME_LEN    64
1033f542974SPaul B Schroeder 
104880af9dbSAlan Cox #define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
105880af9dbSAlan Cox #define ZLP_REG5  0x3E		/* Zero_Flag_Reg5    62 */
1063f542974SPaul B Schroeder 
1073f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */
1083f542974SPaul B Schroeder #define TIOCEXBAUD     0x5462
1093f542974SPaul B Schroeder 
1103f542974SPaul B Schroeder /* vendor id and device id defines */
1113f542974SPaul B Schroeder 
11211e1abb4SDavid Ludlow /* The native mos7840/7820 component */
1133f542974SPaul B Schroeder #define USB_VENDOR_ID_MOSCHIP           0x9710
1143f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7840          0x7840
1153f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7820          0x7820
1160eafe4deSDonald #define MOSCHIP_DEVICE_ID_7810          0x7810
11711e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden
11811e1abb4SDavid Ludlow  * in vendor-specific implementations.  Such devices can be handled
11968e24113SGreg Kroah-Hartman  * by making a change here, in id_table.
12011e1abb4SDavid Ludlow  */
12111e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB              0x0856
122acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
123870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
124acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
125870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
126acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
127acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
128acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
129acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
13011e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
131caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
132870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
133870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
134870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL2_4        0xAC24
1353f542974SPaul B Schroeder 
1369d498beaSRussell Lang /* This driver also supports
1379d498beaSRussell Lang  * ATEN UC2324 device using Moschip MCS7840
1389d498beaSRussell Lang  * ATEN UC2322 device using Moschip MCS7820
1399d498beaSRussell Lang  */
140e9b8cffaSTony Cook #define USB_VENDOR_ID_ATENINTL		0x0557
141e9b8cffaSTony Cook #define ATENINTL_DEVICE_ID_UC2324	0x2011
1429d498beaSRussell Lang #define ATENINTL_DEVICE_ID_UC2322	0x7820
143e9b8cffaSTony Cook 
14411e1abb4SDavid Ludlow /* Interrupt Routine Defines    */
1453f542974SPaul B Schroeder 
1463f542974SPaul B Schroeder #define SERIAL_IIR_RLS      0x06
1473f542974SPaul B Schroeder #define SERIAL_IIR_MS       0x00
1483f542974SPaul B Schroeder 
1493f542974SPaul B Schroeder /*
1503f542974SPaul B Schroeder  *  Emulation of the bit mask on the LINE STATUS REGISTER.
1513f542974SPaul B Schroeder  */
1523f542974SPaul B Schroeder #define SERIAL_LSR_DR       0x0001
1533f542974SPaul B Schroeder #define SERIAL_LSR_OE       0x0002
1543f542974SPaul B Schroeder #define SERIAL_LSR_PE       0x0004
1553f542974SPaul B Schroeder #define SERIAL_LSR_FE       0x0008
1563f542974SPaul B Schroeder #define SERIAL_LSR_BI       0x0010
1573f542974SPaul B Schroeder 
1583f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS   0x10
1593f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR   0x20
1603f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI    0x40
1613f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD    0x80
1623f542974SPaul B Schroeder 
163880af9dbSAlan Cox /* Serial Port register Address */
1643f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
1653f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
1663f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
1673f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
1683f542974SPaul B Schroeder #define LINE_STATUS_REGISTER       ((__u16)(0x05))
1693f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
1703f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
1713f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
1723f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
1733f542974SPaul B Schroeder 
1743f542974SPaul B Schroeder #define CLK_MULTI_REGISTER         ((__u16)(0x02))
1753f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
176093ea2d3SDonald Lee #define GPIO_REGISTER              ((__u16)(0x07))
1773f542974SPaul B Schroeder 
1783f542974SPaul B Schroeder #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
1793f542974SPaul B Schroeder 
1803f542974SPaul B Schroeder /*
1813f542974SPaul B Schroeder  * URB POOL related defines
1823f542974SPaul B Schroeder  */
1833f542974SPaul B Schroeder #define NUM_URBS                        16	/* URB Count */
1843f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
1853f542974SPaul B Schroeder 
1860eafe4deSDonald /* LED on/off milliseconds*/
1870eafe4deSDonald #define LED_ON_MS	500
1880eafe4deSDonald #define LED_OFF_MS	500
1890eafe4deSDonald 
1900eafe4deSDonald static int device_type;
1913f542974SPaul B Schroeder 
192b9c87663STony Zelenoff static const struct usb_device_id id_table[] = {
1933f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
1943f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
1950eafe4deSDonald 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
196acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
197870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
198acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
199870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
200acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
201acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
202acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
203acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
20411e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
205870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
206acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
207870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
20827f1281dSBlaise Gassend 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
209e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
2109d498beaSRussell Lang 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
2113f542974SPaul B Schroeder 	{}			/* terminating entry */
2123f542974SPaul B Schroeder };
21368e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
2143f542974SPaul B Schroeder 
2153f542974SPaul B Schroeder /* This structure holds all of the local port information */
2163f542974SPaul B Schroeder 
2173f542974SPaul B Schroeder struct moschip_port {
2183f542974SPaul B Schroeder 	int port_num;		/*Actual port number in the device(1,2,etc) */
2193f542974SPaul B Schroeder 	struct urb *write_urb;	/* write URB for this port */
2203f542974SPaul B Schroeder 	struct urb *read_urb;	/* read URB for this port */
2210de9a702SOliver Neukum 	struct urb *int_urb;
2223f542974SPaul B Schroeder 	__u8 shadowLCR;		/* last LCR value received */
2233f542974SPaul B Schroeder 	__u8 shadowMCR;		/* last MCR value received */
2243f542974SPaul B Schroeder 	char open;
2250de9a702SOliver Neukum 	char open_ports;
2260de9a702SOliver Neukum 	char zombie;
2273f542974SPaul B Schroeder 	wait_queue_head_t wait_chase;	/* for handling sleeping while waiting for chase to finish */
2283f542974SPaul B Schroeder 	wait_queue_head_t delta_msr_wait;	/* for handling sleeping while waiting for msr change to happen */
2293f542974SPaul B Schroeder 	int delta_msr_cond;
2303f542974SPaul B Schroeder 	struct async_icount icount;
2313f542974SPaul B Schroeder 	struct usb_serial_port *port;	/* loop back to the owner of this object */
2323f542974SPaul B Schroeder 
2333f542974SPaul B Schroeder 	/* Offsets */
2343f542974SPaul B Schroeder 	__u8 SpRegOffset;
2353f542974SPaul B Schroeder 	__u8 ControlRegOffset;
2363f542974SPaul B Schroeder 	__u8 DcrRegOffset;
237880af9dbSAlan Cox 	/* for processing control URBS in interrupt context */
2383f542974SPaul B Schroeder 	struct urb *control_urb;
2390de9a702SOliver Neukum 	struct usb_ctrlrequest *dr;
2403f542974SPaul B Schroeder 	char *ctrl_buf;
2413f542974SPaul B Schroeder 	int MsrLsr;
2423f542974SPaul B Schroeder 
2430de9a702SOliver Neukum 	spinlock_t pool_lock;
2443f542974SPaul B Schroeder 	struct urb *write_urb_pool[NUM_URBS];
2450de9a702SOliver Neukum 	char busy[NUM_URBS];
24650de36f7SGreg Kroah-Hartman 	bool read_urb_busy;
2473f542974SPaul B Schroeder 
2480eafe4deSDonald 	/* For device(s) with LED indicator */
2490eafe4deSDonald 	bool has_led;
2500eafe4deSDonald 	bool led_flag;
2510eafe4deSDonald 	struct timer_list led_timer1;	/* Timer for LED on */
2520eafe4deSDonald 	struct timer_list led_timer2;	/* Timer for LED off */
2530eafe4deSDonald };
2543f542974SPaul B Schroeder 
25590ab5ee9SRusty Russell static bool debug;
2563f542974SPaul B Schroeder 
2573f542974SPaul B Schroeder /*
2583f542974SPaul B Schroeder  * mos7840_set_reg_sync
2593f542974SPaul B Schroeder  * 	To set the Control register by calling usb_fill_control_urb function
2603f542974SPaul B Schroeder  *	by passing usb_sndctrlpipe function as parameter.
2613f542974SPaul B Schroeder  */
2623f542974SPaul B Schroeder 
2633f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2643f542974SPaul B Schroeder 				__u16 val)
2653f542974SPaul B Schroeder {
2663f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2673f542974SPaul B Schroeder 	val = val & 0x00ff;
26884fe6e79STony Cook 	dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
2693f542974SPaul B Schroeder 
2703f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2713f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2723f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2733f542974SPaul B Schroeder }
2743f542974SPaul B Schroeder 
2753f542974SPaul B Schroeder /*
2763f542974SPaul B Schroeder  * mos7840_get_reg_sync
2773f542974SPaul B Schroeder  * 	To set the Uart register by calling usb_fill_control_urb function by
2783f542974SPaul B Schroeder  *	passing usb_rcvctrlpipe function as parameter.
2793f542974SPaul B Schroeder  */
2803f542974SPaul B Schroeder 
2813f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
2823f542974SPaul B Schroeder 				__u16 *val)
2833f542974SPaul B Schroeder {
2843f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2853f542974SPaul B Schroeder 	int ret = 0;
2869e221a35SJohan Hovold 	u8 *buf;
2879e221a35SJohan Hovold 
2889e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2899e221a35SJohan Hovold 	if (!buf)
2909e221a35SJohan Hovold 		return -ENOMEM;
2913f542974SPaul B Schroeder 
2923f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
2939e221a35SJohan Hovold 			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
2943f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
2959e221a35SJohan Hovold 	*val = buf[0];
29684fe6e79STony Cook 	dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
2979e221a35SJohan Hovold 
2989e221a35SJohan Hovold 	kfree(buf);
2993f542974SPaul B Schroeder 	return ret;
3003f542974SPaul B Schroeder }
3013f542974SPaul B Schroeder 
3023f542974SPaul B Schroeder /*
3033f542974SPaul B Schroeder  * mos7840_set_uart_reg
3043f542974SPaul B Schroeder  *	To set the Uart register by calling usb_fill_control_urb function by
3053f542974SPaul B Schroeder  *	passing usb_sndctrlpipe function as parameter.
3063f542974SPaul B Schroeder  */
3073f542974SPaul B Schroeder 
3083f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
3093f542974SPaul B Schroeder 				__u16 val)
3103f542974SPaul B Schroeder {
3113f542974SPaul B Schroeder 
3123f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3133f542974SPaul B Schroeder 	val = val & 0x00ff;
314880af9dbSAlan Cox 	/* For the UART control registers, the application number need
315880af9dbSAlan Cox 	   to be Or'ed */
3160de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
317880af9dbSAlan Cox 		val |= (((__u16) port->number -
318880af9dbSAlan Cox 				(__u16) (port->serial->minor)) + 1) << 8;
31984fe6e79STony Cook 		dbg("mos7840_set_uart_reg application number is %x", val);
3203f542974SPaul B Schroeder 	} else {
3213f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
322880af9dbSAlan Cox 			val |= (((__u16) port->number -
3233f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
32484fe6e79STony Cook 			dbg("mos7840_set_uart_reg application number is %x",
3253f542974SPaul B Schroeder 			    val);
3263f542974SPaul B Schroeder 		} else {
3273f542974SPaul B Schroeder 			val |=
3283f542974SPaul B Schroeder 			    (((__u16) port->number -
3293f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
33084fe6e79STony Cook 			dbg("mos7840_set_uart_reg application number is %x",
3313f542974SPaul B Schroeder 			    val);
3323f542974SPaul B Schroeder 		}
3333f542974SPaul B Schroeder 	}
3343f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
3353f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
3363f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
3373f542974SPaul B Schroeder 
3383f542974SPaul B Schroeder }
3393f542974SPaul B Schroeder 
3403f542974SPaul B Schroeder /*
3413f542974SPaul B Schroeder  * mos7840_get_uart_reg
3423f542974SPaul B Schroeder  *	To set the Control register by calling usb_fill_control_urb function
3433f542974SPaul B Schroeder  *	by passing usb_rcvctrlpipe function as parameter.
3443f542974SPaul B Schroeder  */
3453f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3463f542974SPaul B Schroeder 				__u16 *val)
3473f542974SPaul B Schroeder {
3483f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3493f542974SPaul B Schroeder 	int ret = 0;
3503f542974SPaul B Schroeder 	__u16 Wval;
3519e221a35SJohan Hovold 	u8 *buf;
3529e221a35SJohan Hovold 
3539e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3549e221a35SJohan Hovold 	if (!buf)
3559e221a35SJohan Hovold 		return -ENOMEM;
3563f542974SPaul B Schroeder 
35784fe6e79STony Cook 	/* dbg("application number is %4x",
358880af9dbSAlan Cox 	    (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
3593f542974SPaul B Schroeder 	/* Wval  is same as application number */
3600de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3613f542974SPaul B Schroeder 		Wval =
3623f542974SPaul B Schroeder 		    (((__u16) port->number - (__u16) (port->serial->minor)) +
3633f542974SPaul B Schroeder 		     1) << 8;
36484fe6e79STony Cook 		dbg("mos7840_get_uart_reg application number is %x", Wval);
3653f542974SPaul B Schroeder 	} else {
3663f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
367880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3683f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
36984fe6e79STony Cook 			dbg("mos7840_get_uart_reg application number is %x",
3703f542974SPaul B Schroeder 			    Wval);
3713f542974SPaul B Schroeder 		} else {
372880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3733f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
37484fe6e79STony Cook 			dbg("mos7840_get_uart_reg application number is %x",
3753f542974SPaul B Schroeder 			    Wval);
3763f542974SPaul B Schroeder 		}
3773f542974SPaul B Schroeder 	}
3783f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3799e221a35SJohan Hovold 			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
3803f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
3819e221a35SJohan Hovold 	*val = buf[0];
3829e221a35SJohan Hovold 
3839e221a35SJohan Hovold 	kfree(buf);
3843f542974SPaul B Schroeder 	return ret;
3853f542974SPaul B Schroeder }
3863f542974SPaul B Schroeder 
3873f542974SPaul B Schroeder static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
3883f542974SPaul B Schroeder {
3893f542974SPaul B Schroeder 
39084fe6e79STony Cook 	dbg("***************************************");
39184fe6e79STony Cook 	dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
39284fe6e79STony Cook 	dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
39384fe6e79STony Cook 	dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
39484fe6e79STony Cook 	dbg("***************************************");
3953f542974SPaul B Schroeder 
3963f542974SPaul B Schroeder }
3973f542974SPaul B Schroeder 
3983f542974SPaul B Schroeder /************************************************************************/
3993f542974SPaul B Schroeder /************************************************************************/
4003f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
4013f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
4023f542974SPaul B Schroeder /************************************************************************/
4033f542974SPaul B Schroeder /************************************************************************/
4043f542974SPaul B Schroeder 
4053f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port,
4063f542974SPaul B Schroeder 					    struct moschip_port *data)
4073f542974SPaul B Schroeder {
4083f542974SPaul B Schroeder 	usb_set_serial_port_data(port, (void *)data);
4093f542974SPaul B Schroeder }
4103f542974SPaul B Schroeder 
4113f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct
4123f542974SPaul B Schroeder 							    usb_serial_port
4133f542974SPaul B Schroeder 							    *port)
4143f542974SPaul B Schroeder {
4153f542974SPaul B Schroeder 	return (struct moschip_port *)usb_get_serial_port_data(port);
4163f542974SPaul B Schroeder }
4173f542974SPaul B Schroeder 
4180de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
4193f542974SPaul B Schroeder {
4203f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4213f542974SPaul B Schroeder 	struct async_icount *icount;
4223f542974SPaul B Schroeder 	mos7840_port = port;
4233f542974SPaul B Schroeder 	icount = &mos7840_port->icount;
4243f542974SPaul B Schroeder 	if (new_msr &
4253f542974SPaul B Schroeder 	    (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
4263f542974SPaul B Schroeder 	     MOS_MSR_DELTA_CD)) {
4273f542974SPaul B Schroeder 		icount = &mos7840_port->icount;
4283f542974SPaul B Schroeder 
4293f542974SPaul B Schroeder 		/* update input line counters */
4303f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CTS) {
4313f542974SPaul B Schroeder 			icount->cts++;
4320de9a702SOliver Neukum 			smp_wmb();
4333f542974SPaul B Schroeder 		}
4343f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_DSR) {
4353f542974SPaul B Schroeder 			icount->dsr++;
4360de9a702SOliver Neukum 			smp_wmb();
4373f542974SPaul B Schroeder 		}
4383f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CD) {
4393f542974SPaul B Schroeder 			icount->dcd++;
4400de9a702SOliver Neukum 			smp_wmb();
4413f542974SPaul B Schroeder 		}
4423f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_RI) {
4433f542974SPaul B Schroeder 			icount->rng++;
4440de9a702SOliver Neukum 			smp_wmb();
4450de9a702SOliver Neukum 		}
4463f542974SPaul B Schroeder 	}
4473f542974SPaul B Schroeder }
4483f542974SPaul B Schroeder 
4490de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
4503f542974SPaul B Schroeder {
4513f542974SPaul B Schroeder 	struct async_icount *icount;
4523f542974SPaul B Schroeder 
453441b62c1SHarvey Harrison 	dbg("%s - %02x", __func__, new_lsr);
4543f542974SPaul B Schroeder 
4553f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
456880af9dbSAlan Cox 		/*
457880af9dbSAlan Cox 		 * Parity and Framing errors only count if they
458880af9dbSAlan Cox 		 * occur exclusive of a break being
459880af9dbSAlan Cox 		 * received.
460880af9dbSAlan Cox 		 */
4613f542974SPaul B Schroeder 		new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
4623f542974SPaul B Schroeder 	}
4633f542974SPaul B Schroeder 
4643f542974SPaul B Schroeder 	/* update input line counters */
4653f542974SPaul B Schroeder 	icount = &port->icount;
4663f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
4673f542974SPaul B Schroeder 		icount->brk++;
4680de9a702SOliver Neukum 		smp_wmb();
4693f542974SPaul B Schroeder 	}
4703f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_OE) {
4713f542974SPaul B Schroeder 		icount->overrun++;
4720de9a702SOliver Neukum 		smp_wmb();
4733f542974SPaul B Schroeder 	}
4743f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_PE) {
4753f542974SPaul B Schroeder 		icount->parity++;
4760de9a702SOliver Neukum 		smp_wmb();
4773f542974SPaul B Schroeder 	}
4783f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_FE) {
4793f542974SPaul B Schroeder 		icount->frame++;
4800de9a702SOliver Neukum 		smp_wmb();
4813f542974SPaul B Schroeder 	}
4823f542974SPaul B Schroeder }
4833f542974SPaul B Schroeder 
4843f542974SPaul B Schroeder /************************************************************************/
4853f542974SPaul B Schroeder /************************************************************************/
4863f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4873f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4883f542974SPaul B Schroeder /************************************************************************/
4893f542974SPaul B Schroeder /************************************************************************/
4903f542974SPaul B Schroeder 
4917d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb)
4923f542974SPaul B Schroeder {
4933f542974SPaul B Schroeder 	unsigned char *data;
4943f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4953f542974SPaul B Schroeder 	__u8 regval = 0x0;
4960de9a702SOliver Neukum 	int result = 0;
4970643c724SGreg Kroah-Hartman 	int status = urb->status;
4983f542974SPaul B Schroeder 
499cdc97792SMing Lei 	mos7840_port = urb->context;
5000de9a702SOliver Neukum 
5010643c724SGreg Kroah-Hartman 	switch (status) {
5023f542974SPaul B Schroeder 	case 0:
5033f542974SPaul B Schroeder 		/* success */
5043f542974SPaul B Schroeder 		break;
5053f542974SPaul B Schroeder 	case -ECONNRESET:
5063f542974SPaul B Schroeder 	case -ENOENT:
5073f542974SPaul B Schroeder 	case -ESHUTDOWN:
5083f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
509441b62c1SHarvey Harrison 		dbg("%s - urb shutting down with status: %d", __func__,
5100643c724SGreg Kroah-Hartman 		    status);
5113f542974SPaul B Schroeder 		return;
5123f542974SPaul B Schroeder 	default:
513441b62c1SHarvey Harrison 		dbg("%s - nonzero urb status received: %d", __func__,
5140643c724SGreg Kroah-Hartman 		    status);
5153f542974SPaul B Schroeder 		goto exit;
5163f542974SPaul B Schroeder 	}
5173f542974SPaul B Schroeder 
51884fe6e79STony Cook 	dbg("%s urb buffer size is %d", __func__, urb->actual_length);
51984fe6e79STony Cook 	dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
5203f542974SPaul B Schroeder 	    mos7840_port->MsrLsr, mos7840_port->port_num);
5213f542974SPaul B Schroeder 	data = urb->transfer_buffer;
5223f542974SPaul B Schroeder 	regval = (__u8) data[0];
52384fe6e79STony Cook 	dbg("%s data is %x", __func__, regval);
5243f542974SPaul B Schroeder 	if (mos7840_port->MsrLsr == 0)
5253f542974SPaul B Schroeder 		mos7840_handle_new_msr(mos7840_port, regval);
5263f542974SPaul B Schroeder 	else if (mos7840_port->MsrLsr == 1)
5273f542974SPaul B Schroeder 		mos7840_handle_new_lsr(mos7840_port, regval);
5283f542974SPaul B Schroeder 
5293f542974SPaul B Schroeder exit:
5300de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
5310de9a702SOliver Neukum 	if (!mos7840_port->zombie)
5320de9a702SOliver Neukum 		result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
5330de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
5340de9a702SOliver Neukum 	if (result) {
5350de9a702SOliver Neukum 		dev_err(&urb->dev->dev,
5360de9a702SOliver Neukum 			"%s - Error %d submitting interrupt urb\n",
537441b62c1SHarvey Harrison 			__func__, result);
5380de9a702SOliver Neukum 	}
5393f542974SPaul B Schroeder }
5403f542974SPaul B Schroeder 
5413f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
5423f542974SPaul B Schroeder 			   __u16 *val)
5433f542974SPaul B Schroeder {
5443f542974SPaul B Schroeder 	struct usb_device *dev = mcs->port->serial->dev;
5450de9a702SOliver Neukum 	struct usb_ctrlrequest *dr = mcs->dr;
5460de9a702SOliver Neukum 	unsigned char *buffer = mcs->ctrl_buf;
5470de9a702SOliver Neukum 	int ret;
5483f542974SPaul B Schroeder 
5493f542974SPaul B Schroeder 	dr->bRequestType = MCS_RD_RTYPE;
5503f542974SPaul B Schroeder 	dr->bRequest = MCS_RDREQ;
551880af9dbSAlan Cox 	dr->wValue = cpu_to_le16(Wval);	/* 0 */
5523f542974SPaul B Schroeder 	dr->wIndex = cpu_to_le16(reg);
5533f542974SPaul B Schroeder 	dr->wLength = cpu_to_le16(2);
5543f542974SPaul B Schroeder 
5553f542974SPaul B Schroeder 	usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
5563f542974SPaul B Schroeder 			     (unsigned char *)dr, buffer, 2,
5573f542974SPaul B Schroeder 			     mos7840_control_callback, mcs);
5583f542974SPaul B Schroeder 	mcs->control_urb->transfer_buffer_length = 2;
5593f542974SPaul B Schroeder 	ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
5603f542974SPaul B Schroeder 	return ret;
5613f542974SPaul B Schroeder }
5623f542974SPaul B Schroeder 
5630eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb)
5640eafe4deSDonald {
5650eafe4deSDonald 	switch (urb->status) {
5660eafe4deSDonald 	case 0:
5670eafe4deSDonald 		/* Success */
5680eafe4deSDonald 		break;
5690eafe4deSDonald 	case -ECONNRESET:
5700eafe4deSDonald 	case -ENOENT:
5710eafe4deSDonald 	case -ESHUTDOWN:
5720eafe4deSDonald 		/* This urb is terminated, clean up */
5730eafe4deSDonald 		dbg("%s - urb shutting down with status: %d", __func__,
5740eafe4deSDonald 			urb->status);
5750eafe4deSDonald 		break;
5760eafe4deSDonald 	default:
5770eafe4deSDonald 		dbg("%s - nonzero urb status received: %d", __func__,
5780eafe4deSDonald 			urb->status);
5790eafe4deSDonald 	}
5800eafe4deSDonald }
5810eafe4deSDonald 
5820eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
5830eafe4deSDonald 				__u16 reg)
5840eafe4deSDonald {
5850eafe4deSDonald 	struct usb_device *dev = mcs->port->serial->dev;
5860eafe4deSDonald 	struct usb_ctrlrequest *dr = mcs->dr;
5870eafe4deSDonald 
5880eafe4deSDonald 	dr->bRequestType = MCS_WR_RTYPE;
5890eafe4deSDonald 	dr->bRequest = MCS_WRREQ;
5900eafe4deSDonald 	dr->wValue = cpu_to_le16(wval);
5910eafe4deSDonald 	dr->wIndex = cpu_to_le16(reg);
5920eafe4deSDonald 	dr->wLength = cpu_to_le16(0);
5930eafe4deSDonald 
5940eafe4deSDonald 	usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
5950eafe4deSDonald 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
5960eafe4deSDonald 
5970eafe4deSDonald 	usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
5980eafe4deSDonald }
5990eafe4deSDonald 
6000eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
6010eafe4deSDonald 				__u16 val)
6020eafe4deSDonald {
6030eafe4deSDonald 	struct usb_device *dev = port->serial->dev;
6040eafe4deSDonald 
6050eafe4deSDonald 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
6060eafe4deSDonald 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
6070eafe4deSDonald }
6080eafe4deSDonald 
6090eafe4deSDonald static void mos7840_led_off(unsigned long arg)
6100eafe4deSDonald {
6110eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
6120eafe4deSDonald 
6130eafe4deSDonald 	/* Turn off LED */
6140eafe4deSDonald 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
6150eafe4deSDonald 	mod_timer(&mcs->led_timer2,
6160eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_OFF_MS));
6170eafe4deSDonald }
6180eafe4deSDonald 
6190eafe4deSDonald static void mos7840_led_flag_off(unsigned long arg)
6200eafe4deSDonald {
6210eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
6220eafe4deSDonald 
6230eafe4deSDonald 	mcs->led_flag = false;
6240eafe4deSDonald }
6250eafe4deSDonald 
6263f542974SPaul B Schroeder /*****************************************************************************
6273f542974SPaul B Schroeder  * mos7840_interrupt_callback
6283f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
6293f542974SPaul B Schroeder  *	interrupt endpoint.
6303f542974SPaul B Schroeder  *****************************************************************************/
6313f542974SPaul B Schroeder 
6327d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb)
6333f542974SPaul B Schroeder {
6343f542974SPaul B Schroeder 	int result;
6353f542974SPaul B Schroeder 	int length;
6363f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
6373f542974SPaul B Schroeder 	struct usb_serial *serial;
6383f542974SPaul B Schroeder 	__u16 Data;
6393f542974SPaul B Schroeder 	unsigned char *data;
6403f542974SPaul B Schroeder 	__u8 sp[5], st;
6410de9a702SOliver Neukum 	int i, rv = 0;
6420de9a702SOliver Neukum 	__u16 wval, wreg = 0;
6430643c724SGreg Kroah-Hartman 	int status = urb->status;
6443f542974SPaul B Schroeder 
6450643c724SGreg Kroah-Hartman 	switch (status) {
6463f542974SPaul B Schroeder 	case 0:
6473f542974SPaul B Schroeder 		/* success */
6483f542974SPaul B Schroeder 		break;
6493f542974SPaul B Schroeder 	case -ECONNRESET:
6503f542974SPaul B Schroeder 	case -ENOENT:
6513f542974SPaul B Schroeder 	case -ESHUTDOWN:
6523f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
653441b62c1SHarvey Harrison 		dbg("%s - urb shutting down with status: %d", __func__,
6540643c724SGreg Kroah-Hartman 		    status);
6553f542974SPaul B Schroeder 		return;
6563f542974SPaul B Schroeder 	default:
657441b62c1SHarvey Harrison 		dbg("%s - nonzero urb status received: %d", __func__,
6580643c724SGreg Kroah-Hartman 		    status);
6593f542974SPaul B Schroeder 		goto exit;
6603f542974SPaul B Schroeder 	}
6613f542974SPaul B Schroeder 
6623f542974SPaul B Schroeder 	length = urb->actual_length;
6633f542974SPaul B Schroeder 	data = urb->transfer_buffer;
6643f542974SPaul B Schroeder 
665cdc97792SMing Lei 	serial = urb->context;
6663f542974SPaul B Schroeder 
6673f542974SPaul B Schroeder 	/* Moschip get 5 bytes
6683f542974SPaul B Schroeder 	 * Byte 1 IIR Port 1 (port.number is 0)
6693f542974SPaul B Schroeder 	 * Byte 2 IIR Port 2 (port.number is 1)
6703f542974SPaul B Schroeder 	 * Byte 3 IIR Port 3 (port.number is 2)
6713f542974SPaul B Schroeder 	 * Byte 4 IIR Port 4 (port.number is 3)
6723f542974SPaul B Schroeder 	 * Byte 5 FIFO status for both */
6733f542974SPaul B Schroeder 
6743f542974SPaul B Schroeder 	if (length && length > 5) {
67584fe6e79STony Cook 		dbg("%s", "Wrong data !!!");
6763f542974SPaul B Schroeder 		return;
6773f542974SPaul B Schroeder 	}
6783f542974SPaul B Schroeder 
6793f542974SPaul B Schroeder 	sp[0] = (__u8) data[0];
6803f542974SPaul B Schroeder 	sp[1] = (__u8) data[1];
6813f542974SPaul B Schroeder 	sp[2] = (__u8) data[2];
6823f542974SPaul B Schroeder 	sp[3] = (__u8) data[3];
6833f542974SPaul B Schroeder 	st = (__u8) data[4];
6843f542974SPaul B Schroeder 
6853f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; i++) {
6863f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
6873f542974SPaul B Schroeder 		wval =
6883f542974SPaul B Schroeder 		    (((__u16) serial->port[i]->number -
6893f542974SPaul B Schroeder 		      (__u16) (serial->minor)) + 1) << 8;
6903f542974SPaul B Schroeder 		if (mos7840_port->open) {
6913f542974SPaul B Schroeder 			if (sp[i] & 0x01) {
69284fe6e79STony Cook 				dbg("SP%d No Interrupt !!!", i);
6933f542974SPaul B Schroeder 			} else {
6943f542974SPaul B Schroeder 				switch (sp[i] & 0x0f) {
6953f542974SPaul B Schroeder 				case SERIAL_IIR_RLS:
6963f542974SPaul B Schroeder 					dbg("Serial Port %d: Receiver status error or ", i);
69784fe6e79STony Cook 					dbg("address bit detected in 9-bit mode");
6983f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 1;
6990de9a702SOliver Neukum 					wreg = LINE_STATUS_REGISTER;
7003f542974SPaul B Schroeder 					break;
7013f542974SPaul B Schroeder 				case SERIAL_IIR_MS:
70284fe6e79STony Cook 					dbg("Serial Port %d: Modem status change", i);
7033f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 0;
7040de9a702SOliver Neukum 					wreg = MODEM_STATUS_REGISTER;
7053f542974SPaul B Schroeder 					break;
7063f542974SPaul B Schroeder 				}
7070de9a702SOliver Neukum 				spin_lock(&mos7840_port->pool_lock);
7080de9a702SOliver Neukum 				if (!mos7840_port->zombie) {
7090de9a702SOliver Neukum 					rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
7100de9a702SOliver Neukum 				} else {
7110de9a702SOliver Neukum 					spin_unlock(&mos7840_port->pool_lock);
7120de9a702SOliver Neukum 					return;
7130de9a702SOliver Neukum 				}
7140de9a702SOliver Neukum 				spin_unlock(&mos7840_port->pool_lock);
7153f542974SPaul B Schroeder 			}
7163f542974SPaul B Schroeder 		}
7173f542974SPaul B Schroeder 	}
718880af9dbSAlan Cox 	if (!(rv < 0))
719880af9dbSAlan Cox 		/* the completion handler for the control urb will resubmit */
7200de9a702SOliver Neukum 		return;
7213f542974SPaul B Schroeder exit:
7223f542974SPaul B Schroeder 	result = usb_submit_urb(urb, GFP_ATOMIC);
7233f542974SPaul B Schroeder 	if (result) {
7243f542974SPaul B Schroeder 		dev_err(&urb->dev->dev,
7253f542974SPaul B Schroeder 			"%s - Error %d submitting interrupt urb\n",
726441b62c1SHarvey Harrison 			__func__, result);
7273f542974SPaul B Schroeder 	}
7283f542974SPaul B Schroeder }
7293f542974SPaul B Schroeder 
7303f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port,
7313f542974SPaul B Schroeder 				       const char *function)
7323f542974SPaul B Schroeder {
7333f542974SPaul B Schroeder 	if (!port) {
7343f542974SPaul B Schroeder 		dbg("%s - port == NULL", function);
7353f542974SPaul B Schroeder 		return -1;
7363f542974SPaul B Schroeder 	}
7373f542974SPaul B Schroeder 	if (!port->serial) {
7383f542974SPaul B Schroeder 		dbg("%s - port->serial == NULL", function);
7393f542974SPaul B Schroeder 		return -1;
7403f542974SPaul B Schroeder 	}
7413f542974SPaul B Schroeder 
7423f542974SPaul B Schroeder 	return 0;
7433f542974SPaul B Schroeder }
7443f542974SPaul B Schroeder 
7453f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
7463f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
7473f542974SPaul B Schroeder 					 const char *function)
7483f542974SPaul B Schroeder {
7493f542974SPaul B Schroeder 	if (!serial) {
7503f542974SPaul B Schroeder 		dbg("%s - serial == NULL", function);
7513f542974SPaul B Schroeder 		return -1;
7523f542974SPaul B Schroeder 	}
7533f542974SPaul B Schroeder 	if (!serial->type) {
7543f542974SPaul B Schroeder 		dbg("%s - serial->type == NULL!", function);
7553f542974SPaul B Schroeder 		return -1;
7563f542974SPaul B Schroeder 	}
7573f542974SPaul B Schroeder 
7583f542974SPaul B Schroeder 	return 0;
7593f542974SPaul B Schroeder }
7603f542974SPaul B Schroeder 
7613f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
7623f542974SPaul B Schroeder 						 const char *function)
7633f542974SPaul B Schroeder {
7643f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
7653f542974SPaul B Schroeder 	if (!port ||
7663f542974SPaul B Schroeder 	    mos7840_port_paranoia_check(port, function) ||
7673f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
768880af9dbSAlan Cox 		/* then say that we don't have a valid usb_serial thing,
769880af9dbSAlan Cox 		 * which will end up genrating -ENODEV return values */
7703f542974SPaul B Schroeder 		return NULL;
7713f542974SPaul B Schroeder 	}
7723f542974SPaul B Schroeder 
7733f542974SPaul B Schroeder 	return port->serial;
7743f542974SPaul B Schroeder }
7753f542974SPaul B Schroeder 
7763f542974SPaul B Schroeder /*****************************************************************************
7773f542974SPaul B Schroeder  * mos7840_bulk_in_callback
7783f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
7793f542974SPaul B Schroeder  *	bulk in endpoint.
7803f542974SPaul B Schroeder  *****************************************************************************/
7813f542974SPaul B Schroeder 
7827d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
7833f542974SPaul B Schroeder {
7840643c724SGreg Kroah-Hartman 	int retval;
7853f542974SPaul B Schroeder 	unsigned char *data;
7863f542974SPaul B Schroeder 	struct usb_serial *serial;
7873f542974SPaul B Schroeder 	struct usb_serial_port *port;
7883f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
7893f542974SPaul B Schroeder 	struct tty_struct *tty;
7900643c724SGreg Kroah-Hartman 	int status = urb->status;
7913f542974SPaul B Schroeder 
792cdc97792SMing Lei 	mos7840_port = urb->context;
7933f542974SPaul B Schroeder 	if (!mos7840_port) {
79484fe6e79STony Cook 		dbg("%s", "NULL mos7840_port pointer");
79550de36f7SGreg Kroah-Hartman 		return;
79650de36f7SGreg Kroah-Hartman 	}
79750de36f7SGreg Kroah-Hartman 
79850de36f7SGreg Kroah-Hartman 	if (status) {
79950de36f7SGreg Kroah-Hartman 		dbg("nonzero read bulk status received: %d", status);
80050de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8013f542974SPaul B Schroeder 		return;
8023f542974SPaul B Schroeder 	}
8033f542974SPaul B Schroeder 
8043f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
805441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
80684fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
80750de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8083f542974SPaul B Schroeder 		return;
8093f542974SPaul B Schroeder 	}
8103f542974SPaul B Schroeder 
811441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
8123f542974SPaul B Schroeder 	if (!serial) {
81384fe6e79STony Cook 		dbg("%s", "Bad serial pointer");
81450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8153f542974SPaul B Schroeder 		return;
8163f542974SPaul B Schroeder 	}
8173f542974SPaul B Schroeder 
8183f542974SPaul B Schroeder 	data = urb->transfer_buffer;
8193f542974SPaul B Schroeder 
8203f542974SPaul B Schroeder 	if (urb->actual_length) {
8214a90f09bSAlan Cox 		tty = tty_port_tty_get(&mos7840_port->port->port);
8223f542974SPaul B Schroeder 		if (tty) {
8233f542974SPaul B Schroeder 			tty_insert_flip_string(tty, data, urb->actual_length);
82484fe6e79STony Cook 			dbg(" %s ", data);
8253f542974SPaul B Schroeder 			tty_flip_buffer_push(tty);
8264a90f09bSAlan Cox 			tty_kref_put(tty);
8273f542974SPaul B Schroeder 		}
8283f542974SPaul B Schroeder 		mos7840_port->icount.rx += urb->actual_length;
8290de9a702SOliver Neukum 		smp_wmb();
83084fe6e79STony Cook 		dbg("mos7840_port->icount.rx is %d:",
8313f542974SPaul B Schroeder 		    mos7840_port->icount.rx);
8323f542974SPaul B Schroeder 	}
8333f542974SPaul B Schroeder 
8343f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
83584fe6e79STony Cook 		dbg("%s", "URB KILLED !!!");
83650de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8373f542974SPaul B Schroeder 		return;
8383f542974SPaul B Schroeder 	}
8393f542974SPaul B Schroeder 
8400eafe4deSDonald 	/* Turn on LED */
8410eafe4deSDonald 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
8420eafe4deSDonald 		mos7840_port->led_flag = true;
8430eafe4deSDonald 		mos7840_set_led_async(mos7840_port, 0x0301,
8440eafe4deSDonald 					MODEM_CONTROL_REGISTER);
8450eafe4deSDonald 		mod_timer(&mos7840_port->led_timer1,
8460eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_ON_MS));
8470eafe4deSDonald 	}
8480de9a702SOliver Neukum 
84950de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
8500643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
8513f542974SPaul B Schroeder 
8520643c724SGreg Kroah-Hartman 	if (retval) {
85350de36f7SGreg Kroah-Hartman 		dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
85450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8553f542974SPaul B Schroeder 	}
8563f542974SPaul B Schroeder }
8573f542974SPaul B Schroeder 
8583f542974SPaul B Schroeder /*****************************************************************************
8593f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
860880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
861880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
8623f542974SPaul B Schroeder  *****************************************************************************/
8633f542974SPaul B Schroeder 
8647d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
8653f542974SPaul B Schroeder {
8663f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8673f542974SPaul B Schroeder 	struct tty_struct *tty;
8680643c724SGreg Kroah-Hartman 	int status = urb->status;
8690de9a702SOliver Neukum 	int i;
8700de9a702SOliver Neukum 
871cdc97792SMing Lei 	mos7840_port = urb->context;
8720de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
8730de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
8740de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
8750de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
8760de9a702SOliver Neukum 			break;
8770de9a702SOliver Neukum 		}
8780de9a702SOliver Neukum 	}
8790de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
8800de9a702SOliver Neukum 
8810643c724SGreg Kroah-Hartman 	if (status) {
88284fe6e79STony Cook 		dbg("nonzero write bulk status received:%d", status);
8833f542974SPaul B Schroeder 		return;
8843f542974SPaul B Schroeder 	}
8853f542974SPaul B Schroeder 
886441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
88784fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
8883f542974SPaul B Schroeder 		return;
8893f542974SPaul B Schroeder 	}
8903f542974SPaul B Schroeder 
8914a90f09bSAlan Cox 	tty = tty_port_tty_get(&mos7840_port->port->port);
892b963a844SJiri Slaby 	if (tty && mos7840_port->open)
893b963a844SJiri Slaby 		tty_wakeup(tty);
8944a90f09bSAlan Cox 	tty_kref_put(tty);
8953f542974SPaul B Schroeder 
8963f542974SPaul B Schroeder }
8973f542974SPaul B Schroeder 
8983f542974SPaul B Schroeder /************************************************************************/
8993f542974SPaul 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       */
9003f542974SPaul B Schroeder /************************************************************************/
9013f542974SPaul B Schroeder #ifdef MCSSerialProbe
9023f542974SPaul B Schroeder static int mos7840_serial_probe(struct usb_serial *serial,
9033f542974SPaul B Schroeder 				const struct usb_device_id *id)
9043f542974SPaul B Schroeder {
9053f542974SPaul B Schroeder 
9063f542974SPaul B Schroeder 	/*need to implement the mode_reg reading and updating\
9073f542974SPaul B Schroeder 	   structures usb_serial_ device_type\
9083f542974SPaul B Schroeder 	   (i.e num_ports, num_bulkin,bulkout etc) */
9093f542974SPaul B Schroeder 	/* Also we can update the changes  attach */
9103f542974SPaul B Schroeder 	return 1;
9113f542974SPaul B Schroeder }
9123f542974SPaul B Schroeder #endif
9133f542974SPaul B Schroeder 
9143f542974SPaul B Schroeder /*****************************************************************************
9153f542974SPaul B Schroeder  * mos7840_open
9163f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
9173f542974SPaul B Schroeder  *	If successful, we return 0
9183f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
9193f542974SPaul B Schroeder  *****************************************************************************/
9203f542974SPaul B Schroeder 
921a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
9223f542974SPaul B Schroeder {
9233f542974SPaul B Schroeder 	int response;
9243f542974SPaul B Schroeder 	int j;
9253f542974SPaul B Schroeder 	struct usb_serial *serial;
9263f542974SPaul B Schroeder 	struct urb *urb;
9273f542974SPaul B Schroeder 	__u16 Data;
9283f542974SPaul B Schroeder 	int status;
9293f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
9300de9a702SOliver Neukum 	struct moschip_port *port0;
9313f542974SPaul B Schroeder 
932441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
93384fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
9343f542974SPaul B Schroeder 		return -ENODEV;
9353f542974SPaul B Schroeder 	}
9363f542974SPaul B Schroeder 
9373f542974SPaul B Schroeder 	serial = port->serial;
9383f542974SPaul B Schroeder 
939441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
94084fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
9413f542974SPaul B Schroeder 		return -ENODEV;
9423f542974SPaul B Schroeder 	}
9433f542974SPaul B Schroeder 
9443f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
9450de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
9463f542974SPaul B Schroeder 
9470de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
9483f542974SPaul B Schroeder 		return -ENODEV;
9493f542974SPaul B Schroeder 
9503f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
9513f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
9520de9a702SOliver Neukum 	port0->open_ports++;
9533f542974SPaul B Schroeder 
9543f542974SPaul B Schroeder 	/* Initialising the write urb pool */
9553f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
9560de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
9573f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
9583f542974SPaul B Schroeder 
9593f542974SPaul B Schroeder 		if (urb == NULL) {
960194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "No more urbs???\n");
9613f542974SPaul B Schroeder 			continue;
9623f542974SPaul B Schroeder 		}
9633f542974SPaul B Schroeder 
964880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
965880af9dbSAlan Cox 								GFP_KERNEL);
9663f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
9670de9a702SOliver Neukum 			usb_free_urb(urb);
9680de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
969194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
970194343d9SGreg Kroah-Hartman 				"%s-out of memory for urb buffers.\n",
971194343d9SGreg Kroah-Hartman 				__func__);
9723f542974SPaul B Schroeder 			continue;
9733f542974SPaul B Schroeder 		}
9743f542974SPaul B Schroeder 	}
9753f542974SPaul B Schroeder 
9763f542974SPaul B Schroeder /*****************************************************************************
9773f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
9783f542974SPaul B Schroeder  *
9793f542974SPaul B Schroeder  * Register Index
9803f542974SPaul B Schroeder  * 1 : IER
9813f542974SPaul B Schroeder  * 2 : FCR
9823f542974SPaul B Schroeder  * 3 : LCR
9833f542974SPaul B Schroeder  * 4 : MCR
9843f542974SPaul B Schroeder  *
9853f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
9863f542974SPaul B Schroeder  *****************************************************************************/
9873f542974SPaul B Schroeder 
988880af9dbSAlan Cox 	/* NEED to check the following Block */
9893f542974SPaul B Schroeder 
9903f542974SPaul B Schroeder 	Data = 0x0;
9913f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
9923f542974SPaul B Schroeder 	if (status < 0) {
99384fe6e79STony Cook 		dbg("Reading Spreg failed");
9943f542974SPaul B Schroeder 		return -1;
9953f542974SPaul B Schroeder 	}
9963f542974SPaul B Schroeder 	Data |= 0x80;
9973f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9983f542974SPaul B Schroeder 	if (status < 0) {
99984fe6e79STony Cook 		dbg("writing Spreg failed");
10003f542974SPaul B Schroeder 		return -1;
10013f542974SPaul B Schroeder 	}
10023f542974SPaul B Schroeder 
10033f542974SPaul B Schroeder 	Data &= ~0x80;
10043f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10053f542974SPaul B Schroeder 	if (status < 0) {
100684fe6e79STony Cook 		dbg("writing Spreg failed");
10073f542974SPaul B Schroeder 		return -1;
10083f542974SPaul B Schroeder 	}
1009880af9dbSAlan Cox 	/* End of block to be checked */
10103f542974SPaul B Schroeder 
10113f542974SPaul B Schroeder 	Data = 0x0;
1012880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1013880af9dbSAlan Cox 									&Data);
10143f542974SPaul B Schroeder 	if (status < 0) {
101584fe6e79STony Cook 		dbg("Reading Controlreg failed");
10163f542974SPaul B Schroeder 		return -1;
10173f542974SPaul B Schroeder 	}
1018880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
1019880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
1020880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
1021880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
10223f542974SPaul B Schroeder 	if (status < 0) {
102384fe6e79STony Cook 		dbg("writing Controlreg failed");
10243f542974SPaul B Schroeder 		return -1;
10253f542974SPaul B Schroeder 	}
1026880af9dbSAlan Cox 	/* do register settings here */
1027880af9dbSAlan Cox 	/* Set all regs to the device default values. */
1028880af9dbSAlan Cox 	/***********************************
1029880af9dbSAlan Cox 	 * First Disable all interrupts.
1030880af9dbSAlan Cox 	 ***********************************/
10313f542974SPaul B Schroeder 	Data = 0x00;
10323f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
10333f542974SPaul B Schroeder 	if (status < 0) {
103484fe6e79STony Cook 		dbg("disabling interrupts failed");
10353f542974SPaul B Schroeder 		return -1;
10363f542974SPaul B Schroeder 	}
1037880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
10383f542974SPaul B Schroeder 	Data = 0x00;
10393f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
10403f542974SPaul B Schroeder 	if (status < 0) {
104184fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
10423f542974SPaul B Schroeder 		return -1;
10433f542974SPaul B Schroeder 	}
10443f542974SPaul B Schroeder 
10453f542974SPaul B Schroeder 	Data = 0xcf;
10463f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
10473f542974SPaul B Schroeder 	if (status < 0) {
104884fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
10493f542974SPaul B Schroeder 		return -1;
10503f542974SPaul B Schroeder 	}
10513f542974SPaul B Schroeder 
10523f542974SPaul B Schroeder 	Data = 0x03;
10533f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10543f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10553f542974SPaul B Schroeder 
10563f542974SPaul B Schroeder 	Data = 0x0b;
10573f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
10583f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
10593f542974SPaul B Schroeder 
10603f542974SPaul B Schroeder 	Data = 0x00;
10613f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
10623f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10633f542974SPaul B Schroeder 
1064880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
10653f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10663f542974SPaul B Schroeder 
10673f542974SPaul B Schroeder 	Data = 0x0c;
10683f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
10693f542974SPaul B Schroeder 
10703f542974SPaul B Schroeder 	Data = 0x0;
10713f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
10723f542974SPaul B Schroeder 
10733f542974SPaul B Schroeder 	Data = 0x00;
10743f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
10753f542974SPaul B Schroeder 
10763f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
10773f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10783f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10793f542974SPaul B Schroeder 
1080880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
10813f542974SPaul B Schroeder 	Data = 0x0;
10823f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
10833f542974SPaul B Schroeder 
10843f542974SPaul B Schroeder 	Data = Data | 0x0c;
10853f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10863f542974SPaul B Schroeder 
10873f542974SPaul B Schroeder 	Data = Data & ~0x0c;
10883f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1089880af9dbSAlan Cox 	/* Finally enable all interrupts */
10903f542974SPaul B Schroeder 	Data = 0x0c;
10913f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
10923f542974SPaul B Schroeder 
1093880af9dbSAlan Cox 	/* clearing rx_disable */
10943f542974SPaul B Schroeder 	Data = 0x0;
1095880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1096880af9dbSAlan Cox 									&Data);
10973f542974SPaul B Schroeder 	Data = Data & ~0x20;
1098880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1099880af9dbSAlan Cox 									Data);
11003f542974SPaul B Schroeder 
1101880af9dbSAlan Cox 	/* rx_negate */
11023f542974SPaul B Schroeder 	Data = 0x0;
1103880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1104880af9dbSAlan Cox 									&Data);
11053f542974SPaul B Schroeder 	Data = Data | 0x10;
1106880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1107880af9dbSAlan Cox 									Data);
11083f542974SPaul B Schroeder 
11093f542974SPaul B Schroeder 	/* Check to see if we've set up our endpoint info yet    *
11103f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the structures *
11113f542974SPaul B Schroeder 	 * were not set up at that time.)                        */
11120de9a702SOliver Neukum 	if (port0->open_ports == 1) {
11133f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_buffer == NULL) {
11143f542974SPaul B Schroeder 			/* set up interrupt urb */
11153f542974SPaul B Schroeder 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
11163f542974SPaul B Schroeder 				serial->dev,
11173f542974SPaul B Schroeder 				usb_rcvintpipe(serial->dev,
1118880af9dbSAlan Cox 				serial->port[0]->interrupt_in_endpointAddress),
11193f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_buffer,
11203f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_urb->
11213f542974SPaul B Schroeder 				transfer_buffer_length,
11223f542974SPaul B Schroeder 				mos7840_interrupt_callback,
11233f542974SPaul B Schroeder 				serial,
1124880af9dbSAlan Cox 				serial->port[0]->interrupt_in_urb->interval);
11253f542974SPaul B Schroeder 
11263f542974SPaul B Schroeder 			/* start interrupt read for mos7840               *
11273f542974SPaul B Schroeder 			 * will continue as long as mos7840 is connected  */
11283f542974SPaul B Schroeder 
11293f542974SPaul B Schroeder 			response =
11303f542974SPaul B Schroeder 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
11313f542974SPaul B Schroeder 					   GFP_KERNEL);
11323f542974SPaul B Schroeder 			if (response) {
1133194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "%s - Error %d submitting "
1134194343d9SGreg Kroah-Hartman 					"interrupt urb\n", __func__, response);
11353f542974SPaul B Schroeder 			}
11363f542974SPaul B Schroeder 
11373f542974SPaul B Schroeder 		}
11383f542974SPaul B Schroeder 
11393f542974SPaul B Schroeder 	}
11403f542974SPaul B Schroeder 
11413f542974SPaul B Schroeder 	/* see if we've set up our endpoint info yet   *
11423f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the  *
11433f542974SPaul B Schroeder 	 * structures were not set up at that time.)   */
11443f542974SPaul B Schroeder 
114584fe6e79STony Cook 	dbg("port number is %d", port->number);
114684fe6e79STony Cook 	dbg("serial number is %d", port->serial->minor);
114784fe6e79STony Cook 	dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
114884fe6e79STony Cook 	dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
114984fe6e79STony Cook 	dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
115084fe6e79STony Cook 	dbg("port's number in the device is %d", mos7840_port->port_num);
11513f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
11523f542974SPaul B Schroeder 
11533f542974SPaul B Schroeder 	/* set up our bulk in urb */
1154093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1155093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1156093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1157093ea2d3SDonald Lee 		usb_fill_bulk_urb(mos7840_port->read_urb,
1158093ea2d3SDonald Lee 			serial->dev,
1159093ea2d3SDonald Lee 			usb_rcvbulkpipe(serial->dev,
1160093ea2d3SDonald Lee 				(port->bulk_in_endpointAddress) + 2),
1161093ea2d3SDonald Lee 			port->bulk_in_buffer,
1162093ea2d3SDonald Lee 			mos7840_port->read_urb->transfer_buffer_length,
1163093ea2d3SDonald Lee 			mos7840_bulk_in_callback, mos7840_port);
1164093ea2d3SDonald Lee 	} else {
11653f542974SPaul B Schroeder 		usb_fill_bulk_urb(mos7840_port->read_urb,
11663f542974SPaul B Schroeder 			serial->dev,
11673f542974SPaul B Schroeder 			usb_rcvbulkpipe(serial->dev,
11683f542974SPaul B Schroeder 				port->bulk_in_endpointAddress),
11693f542974SPaul B Schroeder 			port->bulk_in_buffer,
11703f542974SPaul B Schroeder 			mos7840_port->read_urb->transfer_buffer_length,
11713f542974SPaul B Schroeder 			mos7840_bulk_in_callback, mos7840_port);
1172093ea2d3SDonald Lee 	}
11733f542974SPaul B Schroeder 
117484fe6e79STony Cook 	dbg("mos7840_open: bulkin endpoint is %d",
11753f542974SPaul B Schroeder 	    port->bulk_in_endpointAddress);
117650de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
11773f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
11783f542974SPaul B Schroeder 	if (response) {
1179194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1180194343d9SGreg Kroah-Hartman 			__func__, response);
118150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
11823f542974SPaul B Schroeder 	}
11833f542974SPaul B Schroeder 
11843f542974SPaul B Schroeder 	/* initialize our wait queues */
11853f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->wait_chase);
11863f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->delta_msr_wait);
11873f542974SPaul B Schroeder 
11883f542974SPaul B Schroeder 	/* initialize our icount structure */
11893f542974SPaul B Schroeder 	memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
11903f542974SPaul B Schroeder 
11913f542974SPaul B Schroeder 	/* initialize our port settings */
1192880af9dbSAlan Cox 	/* Must set to enable ints! */
1193880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
11943f542974SPaul B Schroeder 	/* send a open port command */
11953f542974SPaul B Schroeder 	mos7840_port->open = 1;
1196880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
11973f542974SPaul B Schroeder 	mos7840_port->icount.tx = 0;
11983f542974SPaul B Schroeder 	mos7840_port->icount.rx = 0;
11993f542974SPaul B Schroeder 
120084fe6e79STony Cook 	dbg("usb_serial serial:%p       mos7840_port:%p\n      usb_serial_port port:%p",
1201880af9dbSAlan Cox 				serial, mos7840_port, port);
12023f542974SPaul B Schroeder 
12033f542974SPaul B Schroeder 	return 0;
12043f542974SPaul B Schroeder }
12053f542974SPaul B Schroeder 
12063f542974SPaul B Schroeder /*****************************************************************************
12073f542974SPaul B Schroeder  * mos7840_chars_in_buffer
12083f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
12093f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
12103f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
12113f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
12123f542974SPaul B Schroeder  *	system,
121395da310eSAlan Cox  *	Otherwise we return zero.
12143f542974SPaul B Schroeder  *****************************************************************************/
12153f542974SPaul B Schroeder 
121695da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
12173f542974SPaul B Schroeder {
121895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
12193f542974SPaul B Schroeder 	int i;
12203f542974SPaul B Schroeder 	int chars = 0;
12210de9a702SOliver Neukum 	unsigned long flags;
12223f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12233f542974SPaul B Schroeder 
1224441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
122584fe6e79STony Cook 		dbg("%s", "Invalid port");
122695da310eSAlan Cox 		return 0;
12273f542974SPaul B Schroeder 	}
12283f542974SPaul B Schroeder 
12293f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12303363155bSGreg Kroah-Hartman 	if (mos7840_port == NULL)
123195da310eSAlan Cox 		return 0;
12323f542974SPaul B Schroeder 
12330de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
12345c263b92SMark Ferrell 	for (i = 0; i < NUM_URBS; ++i) {
12355c263b92SMark Ferrell 		if (mos7840_port->busy[i]) {
12365c263b92SMark Ferrell 			struct urb *urb = mos7840_port->write_urb_pool[i];
12375c263b92SMark Ferrell 			chars += urb->transfer_buffer_length;
12385c263b92SMark Ferrell 		}
12395c263b92SMark Ferrell 	}
12400de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1241441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, chars);
12420de9a702SOliver Neukum 	return chars;
12433f542974SPaul B Schroeder 
12443f542974SPaul B Schroeder }
12453f542974SPaul B Schroeder 
12463f542974SPaul B Schroeder /*****************************************************************************
12473f542974SPaul B Schroeder  * mos7840_close
12483f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
12493f542974SPaul B Schroeder  *****************************************************************************/
12503f542974SPaul B Schroeder 
1251335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
12523f542974SPaul B Schroeder {
12533f542974SPaul B Schroeder 	struct usb_serial *serial;
12543f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12550de9a702SOliver Neukum 	struct moschip_port *port0;
12563f542974SPaul B Schroeder 	int j;
12573f542974SPaul B Schroeder 	__u16 Data;
12583f542974SPaul B Schroeder 
1259441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
126084fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
12613f542974SPaul B Schroeder 		return;
12623f542974SPaul B Schroeder 	}
12633f542974SPaul B Schroeder 
1264441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
12653f542974SPaul B Schroeder 	if (!serial) {
126684fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
12673f542974SPaul B Schroeder 		return;
12683f542974SPaul B Schroeder 	}
12693f542974SPaul B Schroeder 
12703f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12710de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
12723f542974SPaul B Schroeder 
12730de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
12743f542974SPaul B Schroeder 		return;
12753f542974SPaul B Schroeder 
12763f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
12773f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
12783f542974SPaul B Schroeder 
12793f542974SPaul B Schroeder 	/* Freeing Write URBs */
12803f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
12813f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
12823f542974SPaul B Schroeder 			if (mos7840_port->write_urb_pool[j]->transfer_buffer)
12833f542974SPaul B Schroeder 				kfree(mos7840_port->write_urb_pool[j]->
12843f542974SPaul B Schroeder 				      transfer_buffer);
12853f542974SPaul B Schroeder 
12863f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
12873f542974SPaul B Schroeder 		}
12883f542974SPaul B Schroeder 	}
12893f542974SPaul B Schroeder 
12903f542974SPaul B Schroeder 	/* While closing port, shutdown all bulk read, write  *
12913f542974SPaul B Schroeder 	 * and interrupt read if they exists                  */
12923f542974SPaul B Schroeder 	if (serial->dev) {
12933f542974SPaul B Schroeder 		if (mos7840_port->write_urb) {
129484fe6e79STony Cook 			dbg("%s", "Shutdown bulk write");
12953f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->write_urb);
12963f542974SPaul B Schroeder 		}
12973f542974SPaul B Schroeder 		if (mos7840_port->read_urb) {
129884fe6e79STony Cook 			dbg("%s", "Shutdown bulk read");
12993f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->read_urb);
130050de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
13013f542974SPaul B Schroeder 		}
13023f542974SPaul B Schroeder 		if ((&mos7840_port->control_urb)) {
130384fe6e79STony Cook 			dbg("%s", "Shutdown control read");
1304880af9dbSAlan Cox 			/*/      usb_kill_urb (mos7840_port->control_urb); */
13053f542974SPaul B Schroeder 		}
13063f542974SPaul B Schroeder 	}
1307880af9dbSAlan Cox /*      if(mos7840_port->ctrl_buf != NULL) */
1308880af9dbSAlan Cox /*              kfree(mos7840_port->ctrl_buf); */
13090de9a702SOliver Neukum 	port0->open_ports--;
131084fe6e79STony Cook 	dbg("mos7840_num_open_ports in close%d:in port%d",
13110de9a702SOliver Neukum 	    port0->open_ports, port->number);
13120de9a702SOliver Neukum 	if (port0->open_ports == 0) {
13133f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_urb) {
131484fe6e79STony Cook 			dbg("%s", "Shutdown interrupt_in_urb");
13150de9a702SOliver Neukum 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
13163f542974SPaul B Schroeder 		}
13173f542974SPaul B Schroeder 	}
13183f542974SPaul B Schroeder 
13193f542974SPaul B Schroeder 	if (mos7840_port->write_urb) {
13203f542974SPaul B Schroeder 		/* if this urb had a transfer buffer already (old tx) free it */
1321880af9dbSAlan Cox 		if (mos7840_port->write_urb->transfer_buffer != NULL)
13223f542974SPaul B Schroeder 			kfree(mos7840_port->write_urb->transfer_buffer);
13233f542974SPaul B Schroeder 		usb_free_urb(mos7840_port->write_urb);
13243f542974SPaul B Schroeder 	}
13253f542974SPaul B Schroeder 
13263f542974SPaul B Schroeder 	Data = 0x0;
13273f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
13283f542974SPaul B Schroeder 
13293f542974SPaul B Schroeder 	Data = 0x00;
13303f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
13313f542974SPaul B Schroeder 
13323f542974SPaul B Schroeder 	mos7840_port->open = 0;
13333f542974SPaul B Schroeder }
13343f542974SPaul B Schroeder 
13353f542974SPaul B Schroeder /************************************************************************
13363f542974SPaul B Schroeder  *
13373f542974SPaul B Schroeder  * mos7840_block_until_chase_response
13383f542974SPaul B Schroeder  *
13393f542974SPaul B Schroeder  *	This function will block the close until one of the following:
13403f542974SPaul B Schroeder  *		1. Response to our Chase comes from mos7840
1341dc0d5c1eSJoe Perches  *		2. A timeout of 10 seconds without activity has expired
13423f542974SPaul B Schroeder  *		   (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
13433f542974SPaul B Schroeder  *
13443f542974SPaul B Schroeder  ************************************************************************/
13453f542974SPaul B Schroeder 
134695da310eSAlan Cox static void mos7840_block_until_chase_response(struct tty_struct *tty,
134795da310eSAlan Cox 					struct moschip_port *mos7840_port)
13483f542974SPaul B Schroeder {
1349*1e658489SMark Ferrell 	int timeout = msecs_to_jiffies(1000);
13503f542974SPaul B Schroeder 	int wait = 10;
13513f542974SPaul B Schroeder 	int count;
13523f542974SPaul B Schroeder 
13533f542974SPaul B Schroeder 	while (1) {
135495da310eSAlan Cox 		count = mos7840_chars_in_buffer(tty);
13553f542974SPaul B Schroeder 
13563f542974SPaul B Schroeder 		/* Check for Buffer status */
1357880af9dbSAlan Cox 		if (count <= 0)
13583f542974SPaul B Schroeder 			return;
13593f542974SPaul B Schroeder 
13603f542974SPaul B Schroeder 		/* Block the thread for a while */
13613f542974SPaul B Schroeder 		interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
13623f542974SPaul B Schroeder 					       timeout);
13633f542974SPaul B Schroeder 		/* No activity.. count down section */
13643f542974SPaul B Schroeder 		wait--;
13653f542974SPaul B Schroeder 		if (wait == 0) {
1366441b62c1SHarvey Harrison 			dbg("%s - TIMEOUT", __func__);
13673f542974SPaul B Schroeder 			return;
13683f542974SPaul B Schroeder 		} else {
1369dc0d5c1eSJoe Perches 			/* Reset timeout value back to seconds */
13703f542974SPaul B Schroeder 			wait = 10;
13713f542974SPaul B Schroeder 		}
13723f542974SPaul B Schroeder 	}
13733f542974SPaul B Schroeder 
13743f542974SPaul B Schroeder }
13753f542974SPaul B Schroeder 
13763f542974SPaul B Schroeder /*****************************************************************************
13773f542974SPaul B Schroeder  * mos7840_break
13783f542974SPaul B Schroeder  *	this function sends a break to the port
13793f542974SPaul B Schroeder  *****************************************************************************/
138095da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
13813f542974SPaul B Schroeder {
138295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
13833f542974SPaul B Schroeder 	unsigned char data;
13843f542974SPaul B Schroeder 	struct usb_serial *serial;
13853f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13863f542974SPaul B Schroeder 
1387441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
138884fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
13893f542974SPaul B Schroeder 		return;
13903f542974SPaul B Schroeder 	}
13913f542974SPaul B Schroeder 
1392441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
13933f542974SPaul B Schroeder 	if (!serial) {
139484fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
13953f542974SPaul B Schroeder 		return;
13963f542974SPaul B Schroeder 	}
13973f542974SPaul B Schroeder 
13983f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13993f542974SPaul B Schroeder 
1400880af9dbSAlan Cox 	if (mos7840_port == NULL)
14013f542974SPaul B Schroeder 		return;
14023f542974SPaul B Schroeder 
140395da310eSAlan Cox 	if (serial->dev)
14043f542974SPaul B Schroeder 		/* flush and block until tx is empty */
140595da310eSAlan Cox 		mos7840_block_until_chase_response(tty, mos7840_port);
14063f542974SPaul B Schroeder 
140795da310eSAlan Cox 	if (break_state == -1)
14083f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
140995da310eSAlan Cox 	else
14103f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
14113f542974SPaul B Schroeder 
14126b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
14133f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
141484fe6e79STony Cook 	dbg("mcs7840_break mos7840_port->shadowLCR is %x",
14153f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14163f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
14173f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
14183f542974SPaul B Schroeder }
14193f542974SPaul B Schroeder 
14203f542974SPaul B Schroeder /*****************************************************************************
14213f542974SPaul B Schroeder  * mos7840_write_room
14223f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
14233f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
14243f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
14253f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
14263f542974SPaul B Schroeder  *****************************************************************************/
14273f542974SPaul B Schroeder 
142895da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
14293f542974SPaul B Schroeder {
143095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14313f542974SPaul B Schroeder 	int i;
14323f542974SPaul B Schroeder 	int room = 0;
14330de9a702SOliver Neukum 	unsigned long flags;
14343f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14353f542974SPaul B Schroeder 
1436441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
143784fe6e79STony Cook 		dbg("%s", "Invalid port");
143884fe6e79STony Cook 		dbg("%s", " mos7840_write_room:leaving ...........");
14393f542974SPaul B Schroeder 		return -1;
14403f542974SPaul B Schroeder 	}
14413f542974SPaul B Schroeder 
14423f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14433f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
144484fe6e79STony Cook 		dbg("%s", "mos7840_break:leaving ...........");
14453f542974SPaul B Schroeder 		return -1;
14463f542974SPaul B Schroeder 	}
14473f542974SPaul B Schroeder 
14480de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
14493f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
1450880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
14513f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
14523f542974SPaul B Schroeder 	}
14530de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
14543f542974SPaul B Schroeder 
14550de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1456441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, room);
14570de9a702SOliver Neukum 	return room;
14583f542974SPaul B Schroeder 
14593f542974SPaul B Schroeder }
14603f542974SPaul B Schroeder 
14613f542974SPaul B Schroeder /*****************************************************************************
14623f542974SPaul B Schroeder  * mos7840_write
14633f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
14643f542974SPaul B Schroeder  *	the port.
14653f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
14663f542974SPaul B Schroeder  *      return a negative error number.
14673f542974SPaul B Schroeder  *****************************************************************************/
14683f542974SPaul B Schroeder 
146995da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
14703f542974SPaul B Schroeder 			 const unsigned char *data, int count)
14713f542974SPaul B Schroeder {
14723f542974SPaul B Schroeder 	int status;
14733f542974SPaul B Schroeder 	int i;
14743f542974SPaul B Schroeder 	int bytes_sent = 0;
14753f542974SPaul B Schroeder 	int transfer_size;
14760de9a702SOliver Neukum 	unsigned long flags;
14773f542974SPaul B Schroeder 
14783f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14793f542974SPaul B Schroeder 	struct usb_serial *serial;
14803f542974SPaul B Schroeder 	struct urb *urb;
1481880af9dbSAlan Cox 	/* __u16 Data; */
14823f542974SPaul B Schroeder 	const unsigned char *current_position = data;
14833f542974SPaul B Schroeder 	unsigned char *data1;
14843f542974SPaul B Schroeder 
14853f542974SPaul B Schroeder #ifdef NOTMOS7840
14863f542974SPaul B Schroeder 	Data = 0x00;
14873f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
14883f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
148984fe6e79STony Cook 	dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
149084fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
14913f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14923f542974SPaul B Schroeder 
1493880af9dbSAlan Cox 	/* Data = 0x03; */
1494880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1495880af9dbSAlan Cox 	/* mos7840_port->shadowLCR=Data;//Need to add later */
14963f542974SPaul B Schroeder 
1497880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
14983f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14993f542974SPaul B Schroeder 
1500880af9dbSAlan Cox 	/* Data = 0x0c; */
1501880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
15023f542974SPaul B Schroeder 	Data = 0x00;
15033f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
150484fe6e79STony Cook 	dbg("mos7840_write:DLL value is %x", Data);
15053f542974SPaul B Schroeder 
15063f542974SPaul B Schroeder 	Data = 0x0;
15073f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
150884fe6e79STony Cook 	dbg("mos7840_write:DLM value is %x", Data);
15093f542974SPaul B Schroeder 
15103f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
151184fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
15123f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
15133f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
15143f542974SPaul B Schroeder #endif
15153f542974SPaul B Schroeder 
1516441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
151784fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
15183f542974SPaul B Schroeder 		return -1;
15193f542974SPaul B Schroeder 	}
15203f542974SPaul B Schroeder 
15213f542974SPaul B Schroeder 	serial = port->serial;
1522441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
152384fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
15243f542974SPaul B Schroeder 		return -1;
15253f542974SPaul B Schroeder 	}
15263f542974SPaul B Schroeder 
15273f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15283f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
152984fe6e79STony Cook 		dbg("%s", "mos7840_port is NULL");
15303f542974SPaul B Schroeder 		return -1;
15313f542974SPaul B Schroeder 	}
15323f542974SPaul B Schroeder 
15333f542974SPaul B Schroeder 	/* try to find a free urb in the list */
15343f542974SPaul B Schroeder 	urb = NULL;
15353f542974SPaul B Schroeder 
15360de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
15373f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
15380de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
15390de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
15403f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
154184fe6e79STony Cook 			dbg("URB:%d", i);
15423f542974SPaul B Schroeder 			break;
15433f542974SPaul B Schroeder 		}
15443f542974SPaul B Schroeder 	}
15450de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
15463f542974SPaul B Schroeder 
15473f542974SPaul B Schroeder 	if (urb == NULL) {
1548441b62c1SHarvey Harrison 		dbg("%s - no more free urbs", __func__);
15493f542974SPaul B Schroeder 		goto exit;
15503f542974SPaul B Schroeder 	}
15513f542974SPaul B Schroeder 
15523f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
15533f542974SPaul B Schroeder 		urb->transfer_buffer =
15543f542974SPaul B Schroeder 		    kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
15553f542974SPaul B Schroeder 
15563f542974SPaul B Schroeder 		if (urb->transfer_buffer == NULL) {
155722a416c4SJohan Hovold 			dev_err_console(port, "%s no more kernel memory...\n",
1558194343d9SGreg Kroah-Hartman 				__func__);
15593f542974SPaul B Schroeder 			goto exit;
15603f542974SPaul B Schroeder 		}
15613f542974SPaul B Schroeder 	}
15623f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
15633f542974SPaul B Schroeder 
15643f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
15653f542974SPaul B Schroeder 
15663f542974SPaul B Schroeder 	/* fill urb with data and submit  */
1567093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1568093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1569093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1570093ea2d3SDonald Lee 		usb_fill_bulk_urb(urb,
1571093ea2d3SDonald Lee 			serial->dev,
1572093ea2d3SDonald Lee 			usb_sndbulkpipe(serial->dev,
1573093ea2d3SDonald Lee 				(port->bulk_out_endpointAddress) + 2),
1574093ea2d3SDonald Lee 			urb->transfer_buffer,
1575093ea2d3SDonald Lee 			transfer_size,
1576093ea2d3SDonald Lee 			mos7840_bulk_out_data_callback, mos7840_port);
1577093ea2d3SDonald Lee 	} else {
15783f542974SPaul B Schroeder 		usb_fill_bulk_urb(urb,
15793f542974SPaul B Schroeder 			serial->dev,
15803f542974SPaul B Schroeder 			usb_sndbulkpipe(serial->dev,
15813f542974SPaul B Schroeder 				port->bulk_out_endpointAddress),
15823f542974SPaul B Schroeder 			urb->transfer_buffer,
15833f542974SPaul B Schroeder 			transfer_size,
15843f542974SPaul B Schroeder 			mos7840_bulk_out_data_callback, mos7840_port);
1585093ea2d3SDonald Lee 	}
15863f542974SPaul B Schroeder 
15873f542974SPaul B Schroeder 	data1 = urb->transfer_buffer;
158884fe6e79STony Cook 	dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
15893f542974SPaul B Schroeder 
15900eafe4deSDonald 	/* Turn on LED */
15910eafe4deSDonald 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
15920eafe4deSDonald 		mos7840_port->led_flag = true;
15930eafe4deSDonald 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
15940eafe4deSDonald 		mod_timer(&mos7840_port->led_timer1,
15950eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_ON_MS));
15960eafe4deSDonald 	}
15970eafe4deSDonald 
15983f542974SPaul B Schroeder 	/* send it down the pipe */
15993f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
16003f542974SPaul B Schroeder 
16013f542974SPaul B Schroeder 	if (status) {
16020de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
160322a416c4SJohan Hovold 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1604194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
16053f542974SPaul B Schroeder 		bytes_sent = status;
16063f542974SPaul B Schroeder 		goto exit;
16073f542974SPaul B Schroeder 	}
16083f542974SPaul B Schroeder 	bytes_sent = transfer_size;
16093f542974SPaul B Schroeder 	mos7840_port->icount.tx += transfer_size;
16100de9a702SOliver Neukum 	smp_wmb();
161184fe6e79STony Cook 	dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
16123f542974SPaul B Schroeder exit:
16133f542974SPaul B Schroeder 	return bytes_sent;
16143f542974SPaul B Schroeder 
16153f542974SPaul B Schroeder }
16163f542974SPaul B Schroeder 
16173f542974SPaul B Schroeder /*****************************************************************************
16183f542974SPaul B Schroeder  * mos7840_throttle
16193f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
16203f542974SPaul B Schroeder  *	being read from the port.
16213f542974SPaul B Schroeder  *****************************************************************************/
16223f542974SPaul B Schroeder 
162395da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
16243f542974SPaul B Schroeder {
162595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16263f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
16273f542974SPaul B Schroeder 	int status;
16283f542974SPaul B Schroeder 
1629441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
163084fe6e79STony Cook 		dbg("%s", "Invalid port");
16313f542974SPaul B Schroeder 		return;
16323f542974SPaul B Schroeder 	}
16333f542974SPaul B Schroeder 
163484fe6e79STony Cook 	dbg("- port %d", port->number);
16353f542974SPaul B Schroeder 
16363f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
16373f542974SPaul B Schroeder 
16383f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16393f542974SPaul B Schroeder 		return;
16403f542974SPaul B Schroeder 
16413f542974SPaul B Schroeder 	if (!mos7840_port->open) {
164284fe6e79STony Cook 		dbg("%s", "port not opened");
16433f542974SPaul B Schroeder 		return;
16443f542974SPaul B Schroeder 	}
16453f542974SPaul B Schroeder 
16463f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
16473f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
16483f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
164995da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
165095da310eSAlan Cox 		if (status <= 0)
16513f542974SPaul B Schroeder 			return;
16523f542974SPaul B Schroeder 	}
16533f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
16543f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
16553f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
165695da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
16573f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
165895da310eSAlan Cox 		if (status < 0)
16593f542974SPaul B Schroeder 			return;
16603f542974SPaul B Schroeder 	}
16613f542974SPaul B Schroeder }
16623f542974SPaul B Schroeder 
16633f542974SPaul B Schroeder /*****************************************************************************
16643f542974SPaul B Schroeder  * mos7840_unthrottle
1665880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
1666880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
1667880af9dbSAlan Cox  *	called)
16683f542974SPaul B Schroeder  *****************************************************************************/
166995da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
16703f542974SPaul B Schroeder {
167195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16723f542974SPaul B Schroeder 	int status;
16733f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
16743f542974SPaul B Schroeder 
1675441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
167684fe6e79STony Cook 		dbg("%s", "Invalid port");
16773f542974SPaul B Schroeder 		return;
16783f542974SPaul B Schroeder 	}
16793f542974SPaul B Schroeder 
16803f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16813f542974SPaul B Schroeder 		return;
16823f542974SPaul B Schroeder 
16833f542974SPaul B Schroeder 	if (!mos7840_port->open) {
1684441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
16853f542974SPaul B Schroeder 		return;
16863f542974SPaul B Schroeder 	}
16873f542974SPaul B Schroeder 
16883f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
16893f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
16903f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
169195da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
169295da310eSAlan Cox 		if (status <= 0)
16933f542974SPaul B Schroeder 			return;
16943f542974SPaul B Schroeder 	}
16953f542974SPaul B Schroeder 
16963f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
16973f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
16983f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
169995da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
17003f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
170195da310eSAlan Cox 		if (status < 0)
17023f542974SPaul B Schroeder 			return;
17033f542974SPaul B Schroeder 	}
17043f542974SPaul B Schroeder }
17053f542974SPaul B Schroeder 
170660b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
17073f542974SPaul B Schroeder {
170895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
17093f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
17103f542974SPaul B Schroeder 	unsigned int result;
17113f542974SPaul B Schroeder 	__u16 msr;
17123f542974SPaul B Schroeder 	__u16 mcr;
1713880af9dbSAlan Cox 	int status;
17143f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
17153f542974SPaul B Schroeder 
17163f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17173f542974SPaul B Schroeder 		return -ENODEV;
17183f542974SPaul B Schroeder 
17193f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
17203f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
17213f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
17223f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
17233f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
17243f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
17253f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
17263f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
17273f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
17283f542974SPaul B Schroeder 
1729441b62c1SHarvey Harrison 	dbg("%s - 0x%04X", __func__, result);
17303f542974SPaul B Schroeder 
17313f542974SPaul B Schroeder 	return result;
17323f542974SPaul B Schroeder }
17333f542974SPaul B Schroeder 
173420b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
17353f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
17363f542974SPaul B Schroeder {
173795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
17383f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
17393f542974SPaul B Schroeder 	unsigned int mcr;
174087521c46SRoel Kluin 	int status;
17413f542974SPaul B Schroeder 
17423f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
17433f542974SPaul B Schroeder 
17443f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17453f542974SPaul B Schroeder 		return -ENODEV;
17463f542974SPaul B Schroeder 
1747e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
17483f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
17493f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
17503f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
17513f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
17523f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
17533f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
17543f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
17553f542974SPaul B Schroeder 
17563f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
17573f542974SPaul B Schroeder 		mcr |= MCR_RTS;
17583f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
17593f542974SPaul B Schroeder 		mcr |= MCR_DTR;
17603f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
17613f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
17623f542974SPaul B Schroeder 
17633f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
17643f542974SPaul B Schroeder 
17653f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
17663f542974SPaul B Schroeder 	if (status < 0) {
176784fe6e79STony Cook 		dbg("setting MODEM_CONTROL_REGISTER Failed");
176887521c46SRoel Kluin 		return status;
17693f542974SPaul B Schroeder 	}
17703f542974SPaul B Schroeder 
17713f542974SPaul B Schroeder 	return 0;
17723f542974SPaul B Schroeder }
17733f542974SPaul B Schroeder 
17743f542974SPaul B Schroeder /*****************************************************************************
17753f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
17763f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
17773f542974SPaul B Schroeder  *	baud rate.
17783f542974SPaul B Schroeder  *****************************************************************************/
17793f542974SPaul B Schroeder static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
17803f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
17813f542974SPaul B Schroeder {
1782441b62c1SHarvey Harrison 	dbg("%s - %d", __func__, baudRate);
17833f542974SPaul B Schroeder 
17843f542974SPaul B Schroeder 	if (baudRate <= 115200) {
17853f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
17863f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
17873f542974SPaul B Schroeder 	}
17883f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
17893f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
17903f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
17913f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
17923f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
17933f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
17943f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
17953f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
17963f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
17973f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
17983f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
17993f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
18003f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
18013f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
18023f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
18033f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
18043f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
18053f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
18063f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
18073f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
18083f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
18093f542974SPaul B Schroeder 	}
18103f542974SPaul B Schroeder 	return 0;
18113f542974SPaul B Schroeder 
18123f542974SPaul B Schroeder #ifdef NOTMCS7840
18133f542974SPaul B Schroeder 
18143f542974SPaul B Schroeder 	for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
18153f542974SPaul B Schroeder 		if (mos7840_divisor_table[i].BaudRate == baudrate) {
18163f542974SPaul B Schroeder 			*divisor = mos7840_divisor_table[i].Divisor;
18173f542974SPaul B Schroeder 			return 0;
18183f542974SPaul B Schroeder 		}
18193f542974SPaul B Schroeder 	}
18203f542974SPaul B Schroeder 
18213f542974SPaul B Schroeder 	/* After trying for all the standard baud rates    *
18223f542974SPaul B Schroeder 	 * Try calculating the divisor for this baud rate  */
18233f542974SPaul B Schroeder 
18243f542974SPaul B Schroeder 	if (baudrate > 75 && baudrate < 230400) {
18253f542974SPaul B Schroeder 		/* get the divisor */
18263f542974SPaul B Schroeder 		custom = (__u16) (230400L / baudrate);
18273f542974SPaul B Schroeder 
18283f542974SPaul B Schroeder 		/* Check for round off */
18293f542974SPaul B Schroeder 		round1 = (__u16) (2304000L / baudrate);
18303f542974SPaul B Schroeder 		round = (__u16) (round1 - (custom * 10));
1831880af9dbSAlan Cox 		if (round > 4)
18323f542974SPaul B Schroeder 			custom++;
18333f542974SPaul B Schroeder 		*divisor = custom;
18343f542974SPaul B Schroeder 
183584fe6e79STony Cook 		dbg(" Baud %d = %d", baudrate, custom);
18363f542974SPaul B Schroeder 		return 0;
18373f542974SPaul B Schroeder 	}
18383f542974SPaul B Schroeder 
183984fe6e79STony Cook 	dbg("%s", " Baud calculation Failed...");
18403f542974SPaul B Schroeder 	return -1;
18413f542974SPaul B Schroeder #endif
18423f542974SPaul B Schroeder }
18433f542974SPaul B Schroeder 
18443f542974SPaul B Schroeder /*****************************************************************************
18453f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
18463f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
18473f542974SPaul B Schroeder  *	specified port.
18483f542974SPaul B Schroeder  *****************************************************************************/
18493f542974SPaul B Schroeder 
18503f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
18513f542974SPaul B Schroeder 					    int baudRate)
18523f542974SPaul B Schroeder {
18533f542974SPaul B Schroeder 	int divisor = 0;
18543f542974SPaul B Schroeder 	int status;
18553f542974SPaul B Schroeder 	__u16 Data;
18563f542974SPaul B Schroeder 	unsigned char number;
18573f542974SPaul B Schroeder 	__u16 clk_sel_val;
18583f542974SPaul B Schroeder 	struct usb_serial_port *port;
18593f542974SPaul B Schroeder 
18603f542974SPaul B Schroeder 	if (mos7840_port == NULL)
18613f542974SPaul B Schroeder 		return -1;
18623f542974SPaul B Schroeder 
18633f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
1864441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
186584fe6e79STony Cook 		dbg("%s", "Invalid port");
18663f542974SPaul B Schroeder 		return -1;
18673f542974SPaul B Schroeder 	}
18683f542974SPaul B Schroeder 
1869441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
187084fe6e79STony Cook 		dbg("%s", "Invalid Serial");
18713f542974SPaul B Schroeder 		return -1;
18723f542974SPaul B Schroeder 	}
18733f542974SPaul B Schroeder 
18743f542974SPaul B Schroeder 	number = mos7840_port->port->number - mos7840_port->port->serial->minor;
18753f542974SPaul B Schroeder 
1876441b62c1SHarvey Harrison 	dbg("%s - port = %d, baud = %d", __func__,
18773f542974SPaul B Schroeder 	    mos7840_port->port->number, baudRate);
1878880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
18793f542974SPaul B Schroeder 	if (baudRate > 115200) {
18803f542974SPaul B Schroeder #ifdef HW_flow_control
1881880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1882880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
18833f542974SPaul B Schroeder 		Data = 0x2b;
18843f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1885880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1886880af9dbSAlan Cox 									Data);
18873f542974SPaul B Schroeder 		if (status < 0) {
188884fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
18893f542974SPaul B Schroeder 			return -1;
18903f542974SPaul B Schroeder 		}
18913f542974SPaul B Schroeder #endif
18923f542974SPaul B Schroeder 
18933f542974SPaul B Schroeder 	} else {
18943f542974SPaul B Schroeder #ifdef HW_flow_control
1895880af9dbSAlan Cox 		/* setting h/w flow control bit to 0 */
18963f542974SPaul B Schroeder 		Data = 0xb;
18973f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1898880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1899880af9dbSAlan Cox 									Data);
19003f542974SPaul B Schroeder 		if (status < 0) {
190184fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
19023f542974SPaul B Schroeder 			return -1;
19033f542974SPaul B Schroeder 		}
19043f542974SPaul B Schroeder #endif
19053f542974SPaul B Schroeder 
19063f542974SPaul B Schroeder 	}
19073f542974SPaul B Schroeder 
1908880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
19093f542974SPaul B Schroeder 		clk_sel_val = 0x0;
19103f542974SPaul B Schroeder 		Data = 0x0;
1911880af9dbSAlan Cox 		status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
19123f542974SPaul B Schroeder 						   &clk_sel_val);
1913880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
19143f542974SPaul B Schroeder 								 &Data);
19153f542974SPaul B Schroeder 		if (status < 0) {
191684fe6e79STony Cook 			dbg("reading spreg failed in set_serial_baud");
19173f542974SPaul B Schroeder 			return -1;
19183f542974SPaul B Schroeder 		}
19193f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1920880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1921880af9dbSAlan Cox 								Data);
19223f542974SPaul B Schroeder 		if (status < 0) {
192384fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
19243f542974SPaul B Schroeder 			return -1;
19253f542974SPaul B Schroeder 		}
19263f542974SPaul B Schroeder 		/* Calculate the Divisor */
19273f542974SPaul B Schroeder 
19283f542974SPaul B Schroeder 		if (status) {
1929194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
19303f542974SPaul B Schroeder 			return status;
19313f542974SPaul B Schroeder 		}
19323f542974SPaul B Schroeder 		/* Enable access to divisor latch */
19333f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
19343f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
19353f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19363f542974SPaul B Schroeder 
19373f542974SPaul B Schroeder 		/* Write the divisor */
19383f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
193984fe6e79STony Cook 		dbg("set_serial_baud Value to write DLL is %x", Data);
19403f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
19413f542974SPaul B Schroeder 
19423f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
194384fe6e79STony Cook 		dbg("set_serial_baud Value to write DLM is %x", Data);
19443f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
19453f542974SPaul B Schroeder 
19463f542974SPaul B Schroeder 		/* Disable access to divisor latch */
19473f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
19483f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
19493f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19503f542974SPaul B Schroeder 
19513f542974SPaul B Schroeder 	}
19523f542974SPaul B Schroeder 	return status;
19533f542974SPaul B Schroeder }
19543f542974SPaul B Schroeder 
19553f542974SPaul B Schroeder /*****************************************************************************
19563f542974SPaul B Schroeder  * mos7840_change_port_settings
19573f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
19583f542974SPaul B Schroeder  *      the specified new settings.
19593f542974SPaul B Schroeder  *****************************************************************************/
19603f542974SPaul B Schroeder 
196195da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
196295da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
19633f542974SPaul B Schroeder {
19643f542974SPaul B Schroeder 	int baud;
19653f542974SPaul B Schroeder 	unsigned cflag;
19663f542974SPaul B Schroeder 	unsigned iflag;
19673f542974SPaul B Schroeder 	__u8 lData;
19683f542974SPaul B Schroeder 	__u8 lParity;
19693f542974SPaul B Schroeder 	__u8 lStop;
19703f542974SPaul B Schroeder 	int status;
19713f542974SPaul B Schroeder 	__u16 Data;
19723f542974SPaul B Schroeder 	struct usb_serial_port *port;
19733f542974SPaul B Schroeder 	struct usb_serial *serial;
19743f542974SPaul B Schroeder 
19753f542974SPaul B Schroeder 	if (mos7840_port == NULL)
19763f542974SPaul B Schroeder 		return;
19773f542974SPaul B Schroeder 
19783f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
19793f542974SPaul B Schroeder 
1980441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
198184fe6e79STony Cook 		dbg("%s", "Invalid port");
19823f542974SPaul B Schroeder 		return;
19833f542974SPaul B Schroeder 	}
19843f542974SPaul B Schroeder 
1985441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
198684fe6e79STony Cook 		dbg("%s", "Invalid Serial");
19873f542974SPaul B Schroeder 		return;
19883f542974SPaul B Schroeder 	}
19893f542974SPaul B Schroeder 
19903f542974SPaul B Schroeder 	serial = port->serial;
19913f542974SPaul B Schroeder 
1992441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, mos7840_port->port->number);
19933f542974SPaul B Schroeder 
19943f542974SPaul B Schroeder 	if (!mos7840_port->open) {
1995441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
19963f542974SPaul B Schroeder 		return;
19973f542974SPaul B Schroeder 	}
19983f542974SPaul B Schroeder 
19993f542974SPaul B Schroeder 	lData = LCR_BITS_8;
20003f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
20013f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
20023f542974SPaul B Schroeder 
20033f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
20043f542974SPaul B Schroeder 	iflag = tty->termios->c_iflag;
20053f542974SPaul B Schroeder 
20063f542974SPaul B Schroeder 	/* Change the number of bits */
20073f542974SPaul B Schroeder 	if (cflag & CSIZE) {
20083f542974SPaul B Schroeder 		switch (cflag & CSIZE) {
20093f542974SPaul B Schroeder 		case CS5:
20103f542974SPaul B Schroeder 			lData = LCR_BITS_5;
20113f542974SPaul B Schroeder 			break;
20123f542974SPaul B Schroeder 
20133f542974SPaul B Schroeder 		case CS6:
20143f542974SPaul B Schroeder 			lData = LCR_BITS_6;
20153f542974SPaul B Schroeder 			break;
20163f542974SPaul B Schroeder 
20173f542974SPaul B Schroeder 		case CS7:
20183f542974SPaul B Schroeder 			lData = LCR_BITS_7;
20193f542974SPaul B Schroeder 			break;
20203f542974SPaul B Schroeder 		default:
20213f542974SPaul B Schroeder 		case CS8:
20223f542974SPaul B Schroeder 			lData = LCR_BITS_8;
20233f542974SPaul B Schroeder 			break;
20243f542974SPaul B Schroeder 		}
20253f542974SPaul B Schroeder 	}
20263f542974SPaul B Schroeder 	/* Change the Parity bit */
20273f542974SPaul B Schroeder 	if (cflag & PARENB) {
20283f542974SPaul B Schroeder 		if (cflag & PARODD) {
20293f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
2030441b62c1SHarvey Harrison 			dbg("%s - parity = odd", __func__);
20313f542974SPaul B Schroeder 		} else {
20323f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
2033441b62c1SHarvey Harrison 			dbg("%s - parity = even", __func__);
20343f542974SPaul B Schroeder 		}
20353f542974SPaul B Schroeder 
20363f542974SPaul B Schroeder 	} else {
2037441b62c1SHarvey Harrison 		dbg("%s - parity = none", __func__);
20383f542974SPaul B Schroeder 	}
20393f542974SPaul B Schroeder 
2040880af9dbSAlan Cox 	if (cflag & CMSPAR)
20413f542974SPaul B Schroeder 		lParity = lParity | 0x20;
20423f542974SPaul B Schroeder 
20433f542974SPaul B Schroeder 	/* Change the Stop bit */
20443f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
20453f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
2046441b62c1SHarvey Harrison 		dbg("%s - stop bits = 2", __func__);
20473f542974SPaul B Schroeder 	} else {
20483f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
2049441b62c1SHarvey Harrison 		dbg("%s - stop bits = 1", __func__);
20503f542974SPaul B Schroeder 	}
20513f542974SPaul B Schroeder 
20523f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
20533f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
20543f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
20553f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
20563f542974SPaul B Schroeder 
205784fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
20583f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
20593f542974SPaul B Schroeder 	/* Disable Interrupts */
20603f542974SPaul B Schroeder 	Data = 0x00;
20613f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
20623f542974SPaul B Schroeder 
20633f542974SPaul B Schroeder 	Data = 0x00;
20643f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
20653f542974SPaul B Schroeder 
20663f542974SPaul B Schroeder 	Data = 0xcf;
20673f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
20683f542974SPaul B Schroeder 
20693f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
20703f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
20713f542974SPaul B Schroeder 
20723f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
20733f542974SPaul B Schroeder 
20743f542974SPaul B Schroeder 	Data = 0x00b;
20753f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
20763f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20773f542974SPaul B Schroeder 	Data = 0x00b;
20783f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20793f542974SPaul B Schroeder 
20803f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
20813f542974SPaul B Schroeder 
20823f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
2083880af9dbSAlan Cox 	if (cflag & CBAUD)
20843f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
20853f542974SPaul B Schroeder 
2086880af9dbSAlan Cox 	if (cflag & CRTSCTS)
20873f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
2088880af9dbSAlan Cox 	else
20893f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
20903f542974SPaul B Schroeder 
20913f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
20923f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20933f542974SPaul B Schroeder 
20943f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
20953f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
20963f542974SPaul B Schroeder 
20973f542974SPaul B Schroeder 	if (!baud) {
20983f542974SPaul B Schroeder 		/* pick a default, any default... */
209984fe6e79STony Cook 		dbg("%s", "Picked default baud...");
21003f542974SPaul B Schroeder 		baud = 9600;
21013f542974SPaul B Schroeder 	}
21023f542974SPaul B Schroeder 
2103441b62c1SHarvey Harrison 	dbg("%s - baud rate = %d", __func__, baud);
21043f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
21053f542974SPaul B Schroeder 
21063f542974SPaul B Schroeder 	/* Enable Interrupts */
21073f542974SPaul B Schroeder 	Data = 0x0c;
21083f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
21093f542974SPaul B Schroeder 
211050de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
211150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
21123f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
21133f542974SPaul B Schroeder 		if (status) {
21143f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
21153f542974SPaul B Schroeder 			    status);
211650de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
21173f542974SPaul B Schroeder 		}
21183f542974SPaul B Schroeder 	}
21193f542974SPaul B Schroeder 	wake_up(&mos7840_port->delta_msr_wait);
21203f542974SPaul B Schroeder 	mos7840_port->delta_msr_cond = 1;
212184fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
21223f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
21233f542974SPaul B Schroeder }
21243f542974SPaul B Schroeder 
21253f542974SPaul B Schroeder /*****************************************************************************
21263f542974SPaul B Schroeder  * mos7840_set_termios
21273f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
21283f542974SPaul B Schroeder  *	the termios structure
21293f542974SPaul B Schroeder  *****************************************************************************/
21303f542974SPaul B Schroeder 
213195da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
213295da310eSAlan Cox 				struct usb_serial_port *port,
2133606d099cSAlan Cox 				struct ktermios *old_termios)
21343f542974SPaul B Schroeder {
21353f542974SPaul B Schroeder 	int status;
21363f542974SPaul B Schroeder 	unsigned int cflag;
21373f542974SPaul B Schroeder 	struct usb_serial *serial;
21383f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
21393363155bSGreg Kroah-Hartman 
2140441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
214184fe6e79STony Cook 		dbg("%s", "Invalid port");
21423f542974SPaul B Schroeder 		return;
21433f542974SPaul B Schroeder 	}
21443f542974SPaul B Schroeder 
21453f542974SPaul B Schroeder 	serial = port->serial;
21463f542974SPaul B Schroeder 
2147441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
214884fe6e79STony Cook 		dbg("%s", "Invalid Serial");
21493f542974SPaul B Schroeder 		return;
21503f542974SPaul B Schroeder 	}
21513f542974SPaul B Schroeder 
21523f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
21533f542974SPaul B Schroeder 
21543f542974SPaul B Schroeder 	if (mos7840_port == NULL)
21553f542974SPaul B Schroeder 		return;
21563f542974SPaul B Schroeder 
21573f542974SPaul B Schroeder 	if (!mos7840_port->open) {
2158441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
21593f542974SPaul B Schroeder 		return;
21603f542974SPaul B Schroeder 	}
21613f542974SPaul B Schroeder 
216284fe6e79STony Cook 	dbg("%s", "setting termios - ");
21633f542974SPaul B Schroeder 
21643f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
21653f542974SPaul B Schroeder 
2166441b62c1SHarvey Harrison 	dbg("%s - clfag %08x iflag %08x", __func__,
21673f542974SPaul B Schroeder 	    tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2168441b62c1SHarvey Harrison 	dbg("%s - old clfag %08x old iflag %08x", __func__,
21693f542974SPaul B Schroeder 	    old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2170441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21713f542974SPaul B Schroeder 
21723f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
21733f542974SPaul B Schroeder 
217495da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
21753f542974SPaul B Schroeder 
21763f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
217784fe6e79STony Cook 		dbg("%s", "URB KILLED !!!!!");
21783f542974SPaul B Schroeder 		return;
21793f542974SPaul B Schroeder 	}
21803f542974SPaul B Schroeder 
218150de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
218250de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
21833f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
21843f542974SPaul B Schroeder 		if (status) {
21853f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
21863f542974SPaul B Schroeder 			    status);
218750de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
21883f542974SPaul B Schroeder 		}
21893f542974SPaul B Schroeder 	}
21903f542974SPaul B Schroeder }
21913f542974SPaul B Schroeder 
21923f542974SPaul B Schroeder /*****************************************************************************
21933f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
21943f542974SPaul B Schroeder  *
21953f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
21963f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
21973f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
21983f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
21993f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
22003f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
22013f542974SPaul B Schroeder  *****************************************************************************/
22023f542974SPaul B Schroeder 
220395da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
220497c4965dSAl Viro 				unsigned int __user *value)
22053f542974SPaul B Schroeder {
22063f542974SPaul B Schroeder 	int count;
22073f542974SPaul B Schroeder 	unsigned int result = 0;
22083f542974SPaul B Schroeder 
220995da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
22103f542974SPaul B Schroeder 	if (count == 0) {
2211441b62c1SHarvey Harrison 		dbg("%s -- Empty", __func__);
22123f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
22133f542974SPaul B Schroeder 	}
22143f542974SPaul B Schroeder 
22153f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
22163f542974SPaul B Schroeder 		return -EFAULT;
22173f542974SPaul B Schroeder 	return 0;
22183f542974SPaul B Schroeder }
22193f542974SPaul B Schroeder 
22203f542974SPaul B Schroeder /*****************************************************************************
22213f542974SPaul B Schroeder  * mos7840_get_serial_info
22223f542974SPaul B Schroeder  *      function to get information about serial port
22233f542974SPaul B Schroeder  *****************************************************************************/
22243f542974SPaul B Schroeder 
22253f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
222697c4965dSAl Viro 				   struct serial_struct __user *retinfo)
22273f542974SPaul B Schroeder {
22283f542974SPaul B Schroeder 	struct serial_struct tmp;
22293f542974SPaul B Schroeder 
22303f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22313f542974SPaul B Schroeder 		return -1;
22323f542974SPaul B Schroeder 
22333f542974SPaul B Schroeder 	if (!retinfo)
22343f542974SPaul B Schroeder 		return -EFAULT;
22353f542974SPaul B Schroeder 
22363f542974SPaul B Schroeder 	memset(&tmp, 0, sizeof(tmp));
22373f542974SPaul B Schroeder 
22383f542974SPaul B Schroeder 	tmp.type = PORT_16550A;
22393f542974SPaul B Schroeder 	tmp.line = mos7840_port->port->serial->minor;
22403f542974SPaul B Schroeder 	tmp.port = mos7840_port->port->number;
22413f542974SPaul B Schroeder 	tmp.irq = 0;
22423f542974SPaul B Schroeder 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
22433f542974SPaul B Schroeder 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
22443f542974SPaul B Schroeder 	tmp.baud_base = 9600;
22453f542974SPaul B Schroeder 	tmp.close_delay = 5 * HZ;
22463f542974SPaul B Schroeder 	tmp.closing_wait = 30 * HZ;
22473f542974SPaul B Schroeder 
22483f542974SPaul B Schroeder 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
22493f542974SPaul B Schroeder 		return -EFAULT;
22503f542974SPaul B Schroeder 	return 0;
22513f542974SPaul B Schroeder }
22523f542974SPaul B Schroeder 
22530bca1b91SAlan Cox static int mos7840_get_icount(struct tty_struct *tty,
22540bca1b91SAlan Cox 			struct serial_icounter_struct *icount)
22550bca1b91SAlan Cox {
22560bca1b91SAlan Cox 	struct usb_serial_port *port = tty->driver_data;
22570bca1b91SAlan Cox 	struct moschip_port *mos7840_port;
22580bca1b91SAlan Cox 	struct async_icount cnow;
22590bca1b91SAlan Cox 
22600bca1b91SAlan Cox 	mos7840_port = mos7840_get_port_private(port);
22610bca1b91SAlan Cox 	cnow = mos7840_port->icount;
22620bca1b91SAlan Cox 
22630bca1b91SAlan Cox 	smp_rmb();
22640bca1b91SAlan Cox 	icount->cts = cnow.cts;
22650bca1b91SAlan Cox 	icount->dsr = cnow.dsr;
22660bca1b91SAlan Cox 	icount->rng = cnow.rng;
22670bca1b91SAlan Cox 	icount->dcd = cnow.dcd;
22680bca1b91SAlan Cox 	icount->rx = cnow.rx;
22690bca1b91SAlan Cox 	icount->tx = cnow.tx;
22700bca1b91SAlan Cox 	icount->frame = cnow.frame;
22710bca1b91SAlan Cox 	icount->overrun = cnow.overrun;
22720bca1b91SAlan Cox 	icount->parity = cnow.parity;
22730bca1b91SAlan Cox 	icount->brk = cnow.brk;
22740bca1b91SAlan Cox 	icount->buf_overrun = cnow.buf_overrun;
22750bca1b91SAlan Cox 
22760bca1b91SAlan Cox 	dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
22770bca1b91SAlan Cox 		port->number, icount->rx, icount->tx);
22780bca1b91SAlan Cox 	return 0;
22790bca1b91SAlan Cox }
22800bca1b91SAlan Cox 
22813f542974SPaul B Schroeder /*****************************************************************************
22823f542974SPaul B Schroeder  * SerialIoctl
22833f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
22843f542974SPaul B Schroeder  *****************************************************************************/
22853f542974SPaul B Schroeder 
228600a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
22873f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
22883f542974SPaul B Schroeder {
228995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
229097c4965dSAl Viro 	void __user *argp = (void __user *)arg;
22913f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
22923f542974SPaul B Schroeder 
22933f542974SPaul B Schroeder 	struct async_icount cnow;
22943f542974SPaul B Schroeder 	struct async_icount cprev;
22953f542974SPaul B Schroeder 
2296441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
229784fe6e79STony Cook 		dbg("%s", "Invalid port");
22983f542974SPaul B Schroeder 		return -1;
22993f542974SPaul B Schroeder 	}
23003f542974SPaul B Schroeder 
23013f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
23023f542974SPaul B Schroeder 
23033f542974SPaul B Schroeder 	if (mos7840_port == NULL)
23043f542974SPaul B Schroeder 		return -1;
23053f542974SPaul B Schroeder 
2306441b62c1SHarvey Harrison 	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
23073f542974SPaul B Schroeder 
23083f542974SPaul B Schroeder 	switch (cmd) {
23093f542974SPaul B Schroeder 		/* return number of bytes available */
23103f542974SPaul B Schroeder 
23113f542974SPaul B Schroeder 	case TIOCSERGETLSR:
2312441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
231395da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
23143f542974SPaul B Schroeder 
23153f542974SPaul B Schroeder 	case TIOCGSERIAL:
2316441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
231797c4965dSAl Viro 		return mos7840_get_serial_info(mos7840_port, argp);
23183f542974SPaul B Schroeder 
23193f542974SPaul B Schroeder 	case TIOCSSERIAL:
2320441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
23213f542974SPaul B Schroeder 		break;
23223f542974SPaul B Schroeder 
23233f542974SPaul B Schroeder 	case TIOCMIWAIT:
2324441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
23253f542974SPaul B Schroeder 		cprev = mos7840_port->icount;
23263f542974SPaul B Schroeder 		while (1) {
2327880af9dbSAlan Cox 			/* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
23283f542974SPaul B Schroeder 			mos7840_port->delta_msr_cond = 0;
23293f542974SPaul B Schroeder 			wait_event_interruptible(mos7840_port->delta_msr_wait,
23303f542974SPaul B Schroeder 						 (mos7840_port->
23313f542974SPaul B Schroeder 						  delta_msr_cond == 1));
23323f542974SPaul B Schroeder 
23333f542974SPaul B Schroeder 			/* see if a signal did it */
23343f542974SPaul B Schroeder 			if (signal_pending(current))
23353f542974SPaul B Schroeder 				return -ERESTARTSYS;
23363f542974SPaul B Schroeder 			cnow = mos7840_port->icount;
23370de9a702SOliver Neukum 			smp_rmb();
23383f542974SPaul B Schroeder 			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
23393f542974SPaul B Schroeder 			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
23403f542974SPaul B Schroeder 				return -EIO;	/* no change => error */
23413f542974SPaul B Schroeder 			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
23423f542974SPaul B Schroeder 			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
23433f542974SPaul B Schroeder 			    ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
23443f542974SPaul B Schroeder 			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
23453f542974SPaul B Schroeder 				return 0;
23463f542974SPaul B Schroeder 			}
23473f542974SPaul B Schroeder 			cprev = cnow;
23483f542974SPaul B Schroeder 		}
23493f542974SPaul B Schroeder 		/* NOTREACHED */
23503f542974SPaul B Schroeder 		break;
23513f542974SPaul B Schroeder 
23523f542974SPaul B Schroeder 	default:
23533f542974SPaul B Schroeder 		break;
23543f542974SPaul B Schroeder 	}
23553f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
23563f542974SPaul B Schroeder }
23573f542974SPaul B Schroeder 
23580eafe4deSDonald static int mos7810_check(struct usb_serial *serial)
23593f542974SPaul B Schroeder {
23600eafe4deSDonald 	int i, pass_count = 0;
23610eafe4deSDonald 	__u16 data = 0, mcr_data = 0;
23620eafe4deSDonald 	__u16 test_pattern = 0x55AA;
23633f542974SPaul B Schroeder 
23640eafe4deSDonald 	/* Store MCR setting */
23650eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
23660eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
23670eafe4deSDonald 		&mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
23680eafe4deSDonald 
23690eafe4deSDonald 	for (i = 0; i < 16; i++) {
23700eafe4deSDonald 		/* Send the 1-bit test pattern out to MCS7810 test pin */
23710eafe4deSDonald 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
23720eafe4deSDonald 			MCS_WRREQ, MCS_WR_RTYPE,
23730eafe4deSDonald 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
23740eafe4deSDonald 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
23750eafe4deSDonald 
23760eafe4deSDonald 		/* Read the test pattern back */
23770eafe4deSDonald 		usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
23780eafe4deSDonald 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2379093ea2d3SDonald Lee 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2380093ea2d3SDonald Lee 
23810eafe4deSDonald 		/* If this is a MCS7810 device, both test patterns must match */
23820eafe4deSDonald 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
23830eafe4deSDonald 			break;
23840eafe4deSDonald 
23850eafe4deSDonald 		pass_count++;
23863f542974SPaul B Schroeder 	}
2387093ea2d3SDonald Lee 
23880eafe4deSDonald 	/* Restore MCR setting */
23890eafe4deSDonald 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
23900eafe4deSDonald 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
23910eafe4deSDonald 		0, MOS_WDR_TIMEOUT);
23920eafe4deSDonald 
23930eafe4deSDonald 	if (pass_count == 16)
23940eafe4deSDonald 		return 1;
23950eafe4deSDonald 
23960eafe4deSDonald 	return 0;
23970eafe4deSDonald }
23980eafe4deSDonald 
23990eafe4deSDonald static int mos7840_calc_num_ports(struct usb_serial *serial)
24000eafe4deSDonald {
24010eafe4deSDonald 	__u16 data = 0x00;
24020eafe4deSDonald 	int mos7840_num_ports;
24030eafe4deSDonald 
24040eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
24050eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
24060eafe4deSDonald 		VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
24070eafe4deSDonald 
24080eafe4deSDonald 	if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
24090eafe4deSDonald 		serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
24100eafe4deSDonald 		device_type = serial->dev->descriptor.idProduct;
24110eafe4deSDonald 	} else {
24120eafe4deSDonald 		/* For a MCS7840 device GPIO0 must be set to 1 */
24130eafe4deSDonald 		if ((data & 0x01) == 1)
24140eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7840;
24150eafe4deSDonald 		else if (mos7810_check(serial))
24160eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7810;
24170eafe4deSDonald 		else
24180eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7820;
24190eafe4deSDonald 	}
24200eafe4deSDonald 
24210eafe4deSDonald 	mos7840_num_ports = (device_type >> 4) & 0x000F;
24220eafe4deSDonald 	serial->num_bulk_in = mos7840_num_ports;
24230eafe4deSDonald 	serial->num_bulk_out = mos7840_num_ports;
24240eafe4deSDonald 	serial->num_ports = mos7840_num_ports;
24250eafe4deSDonald 
24263f542974SPaul B Schroeder 	return mos7840_num_ports;
24273f542974SPaul B Schroeder }
24283f542974SPaul B Schroeder 
24293f542974SPaul B Schroeder /****************************************************************************
24303f542974SPaul B Schroeder  * mos7840_startup
24313f542974SPaul B Schroeder  ****************************************************************************/
24323f542974SPaul B Schroeder 
24333f542974SPaul B Schroeder static int mos7840_startup(struct usb_serial *serial)
24343f542974SPaul B Schroeder {
24353f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
24363f542974SPaul B Schroeder 	struct usb_device *dev;
24373f542974SPaul B Schroeder 	int i, status;
24383f542974SPaul B Schroeder 	__u16 Data;
24393f542974SPaul B Schroeder 
24403f542974SPaul B Schroeder 	if (!serial) {
244184fe6e79STony Cook 		dbg("%s", "Invalid Handler");
24423f542974SPaul B Schroeder 		return -1;
24433f542974SPaul B Schroeder 	}
24443f542974SPaul B Schroeder 
24453f542974SPaul B Schroeder 	dev = serial->dev;
24463f542974SPaul B Schroeder 
24473f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
24483f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
24493f542974SPaul B Schroeder 
24503f542974SPaul B Schroeder 	/* set up port private structures */
24513f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
245284fe6e79STony Cook 		dbg ("mos7840_startup: configuring port %d............", i);
24537ac9da10SBurman Yan 		mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
24543f542974SPaul B Schroeder 		if (mos7840_port == NULL) {
2455194343d9SGreg Kroah-Hartman 			dev_err(&dev->dev, "%s - Out of memory\n", __func__);
24560de9a702SOliver Neukum 			status = -ENOMEM;
24570de9a702SOliver Neukum 			i--; /* don't follow NULL pointer cleaning up */
24580de9a702SOliver Neukum 			goto error;
24593f542974SPaul B Schroeder 		}
24603f542974SPaul B Schroeder 
2461880af9dbSAlan Cox 		/* Initialize all port interrupt end point to port 0 int
2462880af9dbSAlan Cox 		 * endpoint. Our device has only one interrupt end point
2463880af9dbSAlan Cox 		 * common to all port */
24643f542974SPaul B Schroeder 
24653f542974SPaul B Schroeder 		mos7840_port->port = serial->port[i];
24663f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
24670de9a702SOliver Neukum 		spin_lock_init(&mos7840_port->pool_lock);
24683f542974SPaul B Schroeder 
246937768adfSTony Cook 		/* minor is not initialised until later by
247037768adfSTony Cook 		 * usb-serial.c:get_free_serial() and cannot therefore be used
247137768adfSTony Cook 		 * to index device instances */
247237768adfSTony Cook 		mos7840_port->port_num = i + 1;
247337768adfSTony Cook 		dbg ("serial->port[i]->number = %d", serial->port[i]->number);
247437768adfSTony Cook 		dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
247537768adfSTony Cook 		dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
247637768adfSTony Cook 		dbg ("serial->minor = %d", serial->minor);
24773f542974SPaul B Schroeder 
24783f542974SPaul B Schroeder 		if (mos7840_port->port_num == 1) {
24793f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x0;
24803f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x1;
24813f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x4;
24823f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
24830de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
24843f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x8;
24853f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x9;
24863f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x16;
24873f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
24880de9a702SOliver Neukum 			   && (serial->num_ports == 2)) {
24893f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
24903f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
24913f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
24923f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 3)
24930de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
24943f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
24953f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
24963f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
24973f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 4)
24980de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
24993f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xc;
25003f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xd;
25013f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x1c;
25023f542974SPaul B Schroeder 		}
25033f542974SPaul B Schroeder 		mos7840_dump_serial_port(mos7840_port);
25043f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
25053f542974SPaul B Schroeder 
2506880af9dbSAlan Cox 		/* enable rx_disable bit in control register */
2507880af9dbSAlan Cox 		status = mos7840_get_reg_sync(serial->port[i],
25083f542974SPaul B Schroeder 				 mos7840_port->ControlRegOffset, &Data);
25093f542974SPaul B Schroeder 		if (status < 0) {
251084fe6e79STony Cook 			dbg("Reading ControlReg failed status-0x%x", status);
25113f542974SPaul B Schroeder 			break;
25123f542974SPaul B Schroeder 		} else
251384fe6e79STony Cook 			dbg("ControlReg Reading success val is %x, status%d",
25143f542974SPaul B Schroeder 			    Data, status);
2515880af9dbSAlan Cox 		Data |= 0x08;	/* setting driver done bit */
2516880af9dbSAlan Cox 		Data |= 0x04;	/* sp1_bit to have cts change reflect in
2517880af9dbSAlan Cox 				   modem status reg */
25183f542974SPaul B Schroeder 
2519880af9dbSAlan Cox 		/* Data |= 0x20; //rx_disable bit */
2520880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
25213f542974SPaul B Schroeder 					 mos7840_port->ControlRegOffset, Data);
25223f542974SPaul B Schroeder 		if (status < 0) {
252384fe6e79STony Cook 			dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
25243f542974SPaul B Schroeder 			break;
25253f542974SPaul B Schroeder 		} else
252684fe6e79STony Cook 			dbg("ControlReg Writing success(rx_disable) status%d",
25273f542974SPaul B Schroeder 			    status);
25283f542974SPaul B Schroeder 
2529880af9dbSAlan Cox 		/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2530880af9dbSAlan Cox 		   and 0x24 in DCR3 */
25313f542974SPaul B Schroeder 		Data = 0x01;
2532880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2533880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
25343f542974SPaul B Schroeder 		if (status < 0) {
253584fe6e79STony Cook 			dbg("Writing DCR0 failed status-0x%x", status);
25363f542974SPaul B Schroeder 			break;
25373f542974SPaul B Schroeder 		} else
253884fe6e79STony Cook 			dbg("DCR0 Writing success status%d", status);
25393f542974SPaul B Schroeder 
25403f542974SPaul B Schroeder 		Data = 0x05;
2541880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2542880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
25433f542974SPaul B Schroeder 		if (status < 0) {
254484fe6e79STony Cook 			dbg("Writing DCR1 failed status-0x%x", status);
25453f542974SPaul B Schroeder 			break;
25463f542974SPaul B Schroeder 		} else
254784fe6e79STony Cook 			dbg("DCR1 Writing success status%d", status);
25483f542974SPaul B Schroeder 
25493f542974SPaul B Schroeder 		Data = 0x24;
2550880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2551880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
25523f542974SPaul B Schroeder 		if (status < 0) {
255384fe6e79STony Cook 			dbg("Writing DCR2 failed status-0x%x", status);
25543f542974SPaul B Schroeder 			break;
25553f542974SPaul B Schroeder 		} else
255684fe6e79STony Cook 			dbg("DCR2 Writing success status%d", status);
25573f542974SPaul B Schroeder 
2558880af9dbSAlan Cox 		/* write values in clkstart0x0 and clkmulti 0x20 */
25593f542974SPaul B Schroeder 		Data = 0x0;
2560880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
25613f542974SPaul B Schroeder 					 CLK_START_VALUE_REGISTER, Data);
25623f542974SPaul B Schroeder 		if (status < 0) {
256384fe6e79STony Cook 			dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
25643f542974SPaul B Schroeder 			break;
25653f542974SPaul B Schroeder 		} else
256684fe6e79STony Cook 			dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
25673f542974SPaul B Schroeder 
25683f542974SPaul B Schroeder 		Data = 0x20;
2569880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2570880af9dbSAlan Cox 					CLK_MULTI_REGISTER, Data);
25713f542974SPaul B Schroeder 		if (status < 0) {
257284fe6e79STony Cook 			dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
25733f542974SPaul B Schroeder 			    status);
25740de9a702SOliver Neukum 			goto error;
25753f542974SPaul B Schroeder 		} else
257684fe6e79STony Cook 			dbg("CLK_MULTI_REGISTER Writing success status%d",
25773f542974SPaul B Schroeder 			    status);
25783f542974SPaul B Schroeder 
2579880af9dbSAlan Cox 		/* write value 0x0 to scratchpad register */
25803f542974SPaul B Schroeder 		Data = 0x00;
2581880af9dbSAlan Cox 		status = mos7840_set_uart_reg(serial->port[i],
2582880af9dbSAlan Cox 						SCRATCH_PAD_REGISTER, Data);
25833f542974SPaul B Schroeder 		if (status < 0) {
258484fe6e79STony Cook 			dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
25853f542974SPaul B Schroeder 			    status);
25863f542974SPaul B Schroeder 			break;
25873f542974SPaul B Schroeder 		} else
258884fe6e79STony Cook 			dbg("SCRATCH_PAD_REGISTER Writing success status%d",
25893f542974SPaul B Schroeder 			    status);
25903f542974SPaul B Schroeder 
2591880af9dbSAlan Cox 		/* Zero Length flag register */
25923f542974SPaul B Schroeder 		if ((mos7840_port->port_num != 1)
25930de9a702SOliver Neukum 		    && (serial->num_ports == 2)) {
25943f542974SPaul B Schroeder 
25953f542974SPaul B Schroeder 			Data = 0xff;
25963f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
25973f542974SPaul B Schroeder 				      (__u16) (ZLP_REG1 +
2598880af9dbSAlan Cox 				      ((__u16)mos7840_port->port_num)), Data);
259984fe6e79STony Cook 			dbg("ZLIP offset %x",
26003f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
26013f542974SPaul B Schroeder 					((__u16) mos7840_port->port_num)));
26023f542974SPaul B Schroeder 			if (status < 0) {
260384fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
26043f542974SPaul B Schroeder 				    i + 2, status);
26053f542974SPaul B Schroeder 				break;
26063f542974SPaul B Schroeder 			} else
260784fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
26083f542974SPaul B Schroeder 				    i + 2, status);
26093f542974SPaul B Schroeder 		} else {
26103f542974SPaul B Schroeder 			Data = 0xff;
26113f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
26123f542974SPaul B Schroeder 			      (__u16) (ZLP_REG1 +
2613880af9dbSAlan Cox 			      ((__u16)mos7840_port->port_num) - 0x1), Data);
261484fe6e79STony Cook 			dbg("ZLIP offset %x",
26153f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
26163f542974SPaul B Schroeder 				     ((__u16) mos7840_port->port_num) - 0x1));
26173f542974SPaul B Schroeder 			if (status < 0) {
261884fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
26193f542974SPaul B Schroeder 				    i + 1, status);
26203f542974SPaul B Schroeder 				break;
26213f542974SPaul B Schroeder 			} else
262284fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
26233f542974SPaul B Schroeder 				    i + 1, status);
26243f542974SPaul B Schroeder 
26253f542974SPaul B Schroeder 		}
26260de9a702SOliver Neukum 		mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
26273f542974SPaul B Schroeder 		mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2628880af9dbSAlan Cox 		mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2629880af9dbSAlan Cox 								GFP_KERNEL);
2630880af9dbSAlan Cox 		if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2631880af9dbSAlan Cox 							!mos7840_port->dr) {
26320de9a702SOliver Neukum 			status = -ENOMEM;
26330de9a702SOliver Neukum 			goto error;
26340de9a702SOliver Neukum 		}
26350eafe4deSDonald 
26360eafe4deSDonald 		mos7840_port->has_led = false;
26370eafe4deSDonald 
26380eafe4deSDonald 		/* Initialize LED timers */
26390eafe4deSDonald 		if (device_type == MOSCHIP_DEVICE_ID_7810) {
26400eafe4deSDonald 			mos7840_port->has_led = true;
26410eafe4deSDonald 
26420eafe4deSDonald 			init_timer(&mos7840_port->led_timer1);
26430eafe4deSDonald 			mos7840_port->led_timer1.function = mos7840_led_off;
26440eafe4deSDonald 			mos7840_port->led_timer1.expires =
26450eafe4deSDonald 					jiffies + msecs_to_jiffies(LED_ON_MS);
26460eafe4deSDonald 			mos7840_port->led_timer1.data =
26470eafe4deSDonald 						(unsigned long)mos7840_port;
26480eafe4deSDonald 
26490eafe4deSDonald 			init_timer(&mos7840_port->led_timer2);
26500eafe4deSDonald 			mos7840_port->led_timer2.function =
26510eafe4deSDonald 						mos7840_led_flag_off;
26520eafe4deSDonald 			mos7840_port->led_timer2.expires =
26530eafe4deSDonald 					jiffies + msecs_to_jiffies(LED_OFF_MS);
26540eafe4deSDonald 			mos7840_port->led_timer2.data =
26550eafe4deSDonald 						(unsigned long)mos7840_port;
26560eafe4deSDonald 
26570eafe4deSDonald 			mos7840_port->led_flag = false;
26580eafe4deSDonald 
26590eafe4deSDonald 			/* Turn off LED */
26600eafe4deSDonald 			mos7840_set_led_sync(serial->port[i],
26610eafe4deSDonald 						MODEM_CONTROL_REGISTER, 0x0300);
26620eafe4deSDonald 		}
26633f542974SPaul B Schroeder 	}
266484fe6e79STony Cook 	dbg ("mos7840_startup: all ports configured...........");
26653f542974SPaul B Schroeder 
2666880af9dbSAlan Cox 	/* Zero Length flag enable */
26673f542974SPaul B Schroeder 	Data = 0x0f;
26683f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
26693f542974SPaul B Schroeder 	if (status < 0) {
267084fe6e79STony Cook 		dbg("Writing ZLP_REG5 failed status-0x%x", status);
26717ced46c3SRoel Kluin 		goto error;
26723f542974SPaul B Schroeder 	} else
267384fe6e79STony Cook 		dbg("ZLP_REG5 Writing success status%d", status);
26743f542974SPaul B Schroeder 
26753f542974SPaul B Schroeder 	/* setting configuration feature to one */
26763f542974SPaul B Schroeder 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2677*1e658489SMark Ferrell 			(__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT);
26783f542974SPaul B Schroeder 	return 0;
26790de9a702SOliver Neukum error:
26800de9a702SOliver Neukum 	for (/* nothing */; i >= 0; i--) {
26810de9a702SOliver Neukum 		mos7840_port = mos7840_get_port_private(serial->port[i]);
26820de9a702SOliver Neukum 
26830de9a702SOliver Neukum 		kfree(mos7840_port->dr);
26840de9a702SOliver Neukum 		kfree(mos7840_port->ctrl_buf);
26850de9a702SOliver Neukum 		usb_free_urb(mos7840_port->control_urb);
26860de9a702SOliver Neukum 		kfree(mos7840_port);
26870de9a702SOliver Neukum 		serial->port[i] = NULL;
26880de9a702SOliver Neukum 	}
26890de9a702SOliver Neukum 	return status;
26903f542974SPaul B Schroeder }
26913f542974SPaul B Schroeder 
26923f542974SPaul B Schroeder /****************************************************************************
2693f9c99bb8SAlan Stern  * mos7840_disconnect
26943f542974SPaul B Schroeder  *	This function is called whenever the device is removed from the usb bus.
26953f542974SPaul B Schroeder  ****************************************************************************/
26963f542974SPaul B Schroeder 
2697f9c99bb8SAlan Stern static void mos7840_disconnect(struct usb_serial *serial)
26983f542974SPaul B Schroeder {
26993f542974SPaul B Schroeder 	int i;
27000de9a702SOliver Neukum 	unsigned long flags;
27013f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
27023f542974SPaul B Schroeder 
27033f542974SPaul B Schroeder 	if (!serial) {
270484fe6e79STony Cook 		dbg("%s", "Invalid Handler");
27053f542974SPaul B Schroeder 		return;
27063f542974SPaul B Schroeder 	}
27073f542974SPaul B Schroeder 
27083f542974SPaul B Schroeder 	/* check for the ports to be closed,close the ports and disconnect */
27093f542974SPaul B Schroeder 
27103f542974SPaul B Schroeder 	/* free private structure allocated for serial port  *
27113f542974SPaul B Schroeder 	 * stop reads and writes on all ports                */
27123f542974SPaul B Schroeder 
27133f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
27143f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
271537768adfSTony Cook 		dbg ("mos7840_port %d = %p", i, mos7840_port);
271637768adfSTony Cook 		if (mos7840_port) {
27170de9a702SOliver Neukum 			spin_lock_irqsave(&mos7840_port->pool_lock, flags);
27180de9a702SOliver Neukum 			mos7840_port->zombie = 1;
27190de9a702SOliver Neukum 			spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
27203f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->control_urb);
2721f9c99bb8SAlan Stern 		}
2722f9c99bb8SAlan Stern 	}
2723f9c99bb8SAlan Stern }
2724f9c99bb8SAlan Stern 
2725f9c99bb8SAlan Stern /****************************************************************************
2726f9c99bb8SAlan Stern  * mos7840_release
2727f9c99bb8SAlan Stern  *	This function is called when the usb_serial structure is freed.
2728f9c99bb8SAlan Stern  ****************************************************************************/
2729f9c99bb8SAlan Stern 
2730f9c99bb8SAlan Stern static void mos7840_release(struct usb_serial *serial)
2731f9c99bb8SAlan Stern {
2732f9c99bb8SAlan Stern 	int i;
2733f9c99bb8SAlan Stern 	struct moschip_port *mos7840_port;
2734f9c99bb8SAlan Stern 
2735f9c99bb8SAlan Stern 	if (!serial) {
2736f9c99bb8SAlan Stern 		dbg("%s", "Invalid Handler");
2737f9c99bb8SAlan Stern 		return;
2738f9c99bb8SAlan Stern 	}
2739f9c99bb8SAlan Stern 
2740f9c99bb8SAlan Stern 	/* check for the ports to be closed,close the ports and disconnect */
2741f9c99bb8SAlan Stern 
2742f9c99bb8SAlan Stern 	/* free private structure allocated for serial port  *
2743f9c99bb8SAlan Stern 	 * stop reads and writes on all ports                */
2744f9c99bb8SAlan Stern 
2745f9c99bb8SAlan Stern 	for (i = 0; i < serial->num_ports; ++i) {
2746f9c99bb8SAlan Stern 		mos7840_port = mos7840_get_port_private(serial->port[i]);
2747f9c99bb8SAlan Stern 		dbg("mos7840_port %d = %p", i, mos7840_port);
2748f9c99bb8SAlan Stern 		if (mos7840_port) {
27490eafe4deSDonald 			if (mos7840_port->has_led) {
27500eafe4deSDonald 				/* Turn off LED */
27510eafe4deSDonald 				mos7840_set_led_sync(mos7840_port->port,
27520eafe4deSDonald 						MODEM_CONTROL_REGISTER, 0x0300);
27530eafe4deSDonald 
27540eafe4deSDonald 				del_timer_sync(&mos7840_port->led_timer1);
27550eafe4deSDonald 				del_timer_sync(&mos7840_port->led_timer2);
27560eafe4deSDonald 			}
27570de9a702SOliver Neukum 			kfree(mos7840_port->ctrl_buf);
27580de9a702SOliver Neukum 			kfree(mos7840_port->dr);
27593f542974SPaul B Schroeder 			kfree(mos7840_port);
276037768adfSTony Cook 		}
27613f542974SPaul B Schroeder 	}
27623f542974SPaul B Schroeder }
27633f542974SPaul B Schroeder 
27643f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
27653f542974SPaul B Schroeder 	.driver = {
27663f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
27673f542974SPaul B Schroeder 		   .name = "mos7840",
27683f542974SPaul B Schroeder 		   },
27693f542974SPaul B Schroeder 	.description = DRIVER_DESC,
277068e24113SGreg Kroah-Hartman 	.id_table = id_table,
27713f542974SPaul B Schroeder 	.num_ports = 4,
27723f542974SPaul B Schroeder 	.open = mos7840_open,
27733f542974SPaul B Schroeder 	.close = mos7840_close,
27743f542974SPaul B Schroeder 	.write = mos7840_write,
27753f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
27763f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
27773f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
27783f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
27793f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
27803f542974SPaul B Schroeder #ifdef MCSSerialProbe
27813f542974SPaul B Schroeder 	.probe = mos7840_serial_probe,
27823f542974SPaul B Schroeder #endif
27833f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
27843f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
27853f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
27863f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
27873f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
27880bca1b91SAlan Cox 	.get_icount = mos7840_get_icount,
27893f542974SPaul B Schroeder 	.attach = mos7840_startup,
2790f9c99bb8SAlan Stern 	.disconnect = mos7840_disconnect,
2791f9c99bb8SAlan Stern 	.release = mos7840_release,
27923f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
27933f542974SPaul B Schroeder 	.read_int_callback = mos7840_interrupt_callback,
27943f542974SPaul B Schroeder };
27953f542974SPaul B Schroeder 
27964d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
27974d2a7affSAlan Stern 	&moschip7840_4port_device, NULL
27984d2a7affSAlan Stern };
27994d2a7affSAlan Stern 
280068e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
28013f542974SPaul B Schroeder 
28023f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
28033f542974SPaul B Schroeder MODULE_LICENSE("GPL");
28043f542974SPaul B Schroeder 
28053f542974SPaul B Schroeder module_param(debug, bool, S_IRUGO | S_IWUSR);
28063f542974SPaul B Schroeder MODULE_PARM_DESC(debug, "Debug enabled or not");
2807