xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision 405f55712dfe464b3240d7816cc4fe4174831be2)
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>
29*405f5571SAlexey Dobriyan #include <linux/smp_lock.h>
303f542974SPaul B Schroeder #include <linux/tty.h>
313f542974SPaul B Schroeder #include <linux/tty_driver.h>
323f542974SPaul B Schroeder #include <linux/tty_flip.h>
333f542974SPaul B Schroeder #include <linux/module.h>
343f542974SPaul B Schroeder #include <linux/serial.h>
353f542974SPaul B Schroeder #include <linux/usb.h>
363f542974SPaul B Schroeder #include <linux/usb/serial.h>
37880af9dbSAlan Cox #include <linux/uaccess.h>
383f542974SPaul B Schroeder 
393f542974SPaul B Schroeder /*
403f542974SPaul B Schroeder  * Version Information
413f542974SPaul B Schroeder  */
4237768adfSTony Cook #define DRIVER_VERSION "1.3.2"
433f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
443f542974SPaul B Schroeder 
453f542974SPaul B Schroeder /*
463f542974SPaul B Schroeder  * 16C50 UART register defines
473f542974SPaul B Schroeder  */
483f542974SPaul B Schroeder 
493f542974SPaul B Schroeder #define LCR_BITS_5             0x00	/* 5 bits/char */
503f542974SPaul B Schroeder #define LCR_BITS_6             0x01	/* 6 bits/char */
513f542974SPaul B Schroeder #define LCR_BITS_7             0x02	/* 7 bits/char */
523f542974SPaul B Schroeder #define LCR_BITS_8             0x03	/* 8 bits/char */
533f542974SPaul B Schroeder #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
543f542974SPaul B Schroeder 
553f542974SPaul B Schroeder #define LCR_STOP_1             0x00	/* 1 stop bit */
563f542974SPaul B Schroeder #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
573f542974SPaul B Schroeder #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
583f542974SPaul B Schroeder #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
593f542974SPaul B Schroeder 
603f542974SPaul B Schroeder #define LCR_PAR_NONE           0x00	/* No parity */
613f542974SPaul B Schroeder #define LCR_PAR_ODD            0x08	/* Odd parity */
623f542974SPaul B Schroeder #define LCR_PAR_EVEN           0x18	/* Even parity */
633f542974SPaul B Schroeder #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
643f542974SPaul B Schroeder #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
653f542974SPaul B Schroeder #define LCR_PAR_MASK           0x38	/* Mask for parity field */
663f542974SPaul B Schroeder 
673f542974SPaul B Schroeder #define LCR_SET_BREAK          0x40	/* Set Break condition */
683f542974SPaul B Schroeder #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
693f542974SPaul B Schroeder 
703f542974SPaul B Schroeder #define MCR_DTR                0x01	/* Assert DTR */
713f542974SPaul B Schroeder #define MCR_RTS                0x02	/* Assert RTS */
723f542974SPaul B Schroeder #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
733f542974SPaul B Schroeder #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
743f542974SPaul B Schroeder #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
753f542974SPaul B Schroeder #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
763f542974SPaul B Schroeder 
773f542974SPaul B Schroeder #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
783f542974SPaul B Schroeder #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
793f542974SPaul B Schroeder #define MOS7840_MSR_RI         0x40	/* Current state of RI */
803f542974SPaul B Schroeder #define MOS7840_MSR_CD         0x80	/* Current state of CD */
813f542974SPaul B Schroeder 
823f542974SPaul B Schroeder /*
833f542974SPaul B Schroeder  * Defines used for sending commands to port
843f542974SPaul B Schroeder  */
853f542974SPaul B Schroeder 
863f542974SPaul B Schroeder #define WAIT_FOR_EVER   (HZ * 0)	/* timeout urb is wait for ever */
873f542974SPaul B Schroeder #define MOS_WDR_TIMEOUT (HZ * 5)	/* default urb timeout */
883f542974SPaul B Schroeder 
893f542974SPaul B Schroeder #define MOS_PORT1       0x0200
903f542974SPaul B Schroeder #define MOS_PORT2       0x0300
913f542974SPaul B Schroeder #define MOS_VENREG      0x0000
923f542974SPaul B Schroeder #define MOS_MAX_PORT	0x02
933f542974SPaul B Schroeder #define MOS_WRITE       0x0E
943f542974SPaul B Schroeder #define MOS_READ        0x0D
953f542974SPaul B Schroeder 
963f542974SPaul B Schroeder /* Requests */
973f542974SPaul B Schroeder #define MCS_RD_RTYPE    0xC0
983f542974SPaul B Schroeder #define MCS_WR_RTYPE    0x40
993f542974SPaul B Schroeder #define MCS_RDREQ       0x0D
1003f542974SPaul B Schroeder #define MCS_WRREQ       0x0E
1013f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT        500
1023f542974SPaul B Schroeder #define VENDOR_READ_LENGTH      (0x01)
1033f542974SPaul B Schroeder 
1043f542974SPaul B Schroeder #define MAX_NAME_LEN    64
1053f542974SPaul B Schroeder 
106880af9dbSAlan Cox #define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
107880af9dbSAlan Cox #define ZLP_REG5  0x3E		/* Zero_Flag_Reg5    62 */
1083f542974SPaul B Schroeder 
1093f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */
1103f542974SPaul B Schroeder #define TIOCEXBAUD     0x5462
1113f542974SPaul B Schroeder 
1123f542974SPaul B Schroeder /* vendor id and device id defines */
1133f542974SPaul B Schroeder 
11411e1abb4SDavid Ludlow /* The native mos7840/7820 component */
1153f542974SPaul B Schroeder #define USB_VENDOR_ID_MOSCHIP           0x9710
1163f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7840          0x7840
1173f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7820          0x7820
11811e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden
11911e1abb4SDavid Ludlow  * in vendor-specific implementations.  Such devices can be handled
12011e1abb4SDavid Ludlow  * by making a change here, in moschip_port_id_table, and in
12111e1abb4SDavid Ludlow  * moschip_id_table_combined
12211e1abb4SDavid Ludlow  */
12311e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB             0x0856
12411e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_4       0xAC44
12511e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2       0xAC42
1263f542974SPaul B Schroeder 
127e9b8cffaSTony Cook /* This driver also supports the ATEN UC2324 device since it is mos7840 based
128e9b8cffaSTony Cook  *  - if I knew the device id it would also support the ATEN UC2322 */
129e9b8cffaSTony Cook #define USB_VENDOR_ID_ATENINTL		0x0557
130e9b8cffaSTony Cook #define ATENINTL_DEVICE_ID_UC2324	0x2011
131e9b8cffaSTony Cook 
13211e1abb4SDavid Ludlow /* Interrupt Routine Defines    */
1333f542974SPaul B Schroeder 
1343f542974SPaul B Schroeder #define SERIAL_IIR_RLS      0x06
1353f542974SPaul B Schroeder #define SERIAL_IIR_MS       0x00
1363f542974SPaul B Schroeder 
1373f542974SPaul B Schroeder /*
1383f542974SPaul B Schroeder  *  Emulation of the bit mask on the LINE STATUS REGISTER.
1393f542974SPaul B Schroeder  */
1403f542974SPaul B Schroeder #define SERIAL_LSR_DR       0x0001
1413f542974SPaul B Schroeder #define SERIAL_LSR_OE       0x0002
1423f542974SPaul B Schroeder #define SERIAL_LSR_PE       0x0004
1433f542974SPaul B Schroeder #define SERIAL_LSR_FE       0x0008
1443f542974SPaul B Schroeder #define SERIAL_LSR_BI       0x0010
1453f542974SPaul B Schroeder 
1463f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS   0x10
1473f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR   0x20
1483f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI    0x40
1493f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD    0x80
1503f542974SPaul B Schroeder 
151880af9dbSAlan Cox /* Serial Port register Address */
1523f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
1533f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
1543f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
1553f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
1563f542974SPaul B Schroeder #define LINE_STATUS_REGISTER       ((__u16)(0x05))
1573f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
1583f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
1593f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
1603f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
1613f542974SPaul B Schroeder 
1623f542974SPaul B Schroeder #define CLK_MULTI_REGISTER         ((__u16)(0x02))
1633f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
1643f542974SPaul B Schroeder 
1653f542974SPaul B Schroeder #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
1663f542974SPaul B Schroeder 
1673f542974SPaul B Schroeder /*
1683f542974SPaul B Schroeder  * URB POOL related defines
1693f542974SPaul B Schroeder  */
1703f542974SPaul B Schroeder #define NUM_URBS                        16	/* URB Count */
1713f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
1723f542974SPaul B Schroeder 
1733f542974SPaul B Schroeder 
1743f542974SPaul B Schroeder static struct usb_device_id moschip_port_id_table[] = {
1753f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
1763f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
17711e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
17811e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
179e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
1803f542974SPaul B Schroeder 	{}			/* terminating entry */
1813f542974SPaul B Schroeder };
1823f542974SPaul B Schroeder 
1833f542974SPaul B Schroeder static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
1843f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
1853f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
18611e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
18711e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
188e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
1893f542974SPaul B Schroeder 	{}			/* terminating entry */
1903f542974SPaul B Schroeder };
1913f542974SPaul B Schroeder 
1923f542974SPaul B Schroeder MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
1933f542974SPaul B Schroeder 
1943f542974SPaul B Schroeder /* This structure holds all of the local port information */
1953f542974SPaul B Schroeder 
1963f542974SPaul B Schroeder struct moschip_port {
1973f542974SPaul B Schroeder 	int port_num;		/*Actual port number in the device(1,2,etc) */
1983f542974SPaul B Schroeder 	struct urb *write_urb;	/* write URB for this port */
1993f542974SPaul B Schroeder 	struct urb *read_urb;	/* read URB for this port */
2000de9a702SOliver Neukum 	struct urb *int_urb;
2013f542974SPaul B Schroeder 	__u8 shadowLCR;		/* last LCR value received */
2023f542974SPaul B Schroeder 	__u8 shadowMCR;		/* last MCR value received */
2033f542974SPaul B Schroeder 	char open;
2040de9a702SOliver Neukum 	char open_ports;
2050de9a702SOliver Neukum 	char zombie;
2063f542974SPaul B Schroeder 	wait_queue_head_t wait_chase;	/* for handling sleeping while waiting for chase to finish */
2073f542974SPaul B Schroeder 	wait_queue_head_t delta_msr_wait;	/* for handling sleeping while waiting for msr change to happen */
2083f542974SPaul B Schroeder 	int delta_msr_cond;
2093f542974SPaul B Schroeder 	struct async_icount icount;
2103f542974SPaul B Schroeder 	struct usb_serial_port *port;	/* loop back to the owner of this object */
2113f542974SPaul B Schroeder 
2123f542974SPaul B Schroeder 	/* Offsets */
2133f542974SPaul B Schroeder 	__u8 SpRegOffset;
2143f542974SPaul B Schroeder 	__u8 ControlRegOffset;
2153f542974SPaul B Schroeder 	__u8 DcrRegOffset;
216880af9dbSAlan Cox 	/* for processing control URBS in interrupt context */
2173f542974SPaul B Schroeder 	struct urb *control_urb;
2180de9a702SOliver Neukum 	struct usb_ctrlrequest *dr;
2193f542974SPaul B Schroeder 	char *ctrl_buf;
2203f542974SPaul B Schroeder 	int MsrLsr;
2213f542974SPaul B Schroeder 
2220de9a702SOliver Neukum 	spinlock_t pool_lock;
2233f542974SPaul B Schroeder 	struct urb *write_urb_pool[NUM_URBS];
2240de9a702SOliver Neukum 	char busy[NUM_URBS];
22550de36f7SGreg Kroah-Hartman 	bool read_urb_busy;
2263f542974SPaul B Schroeder };
2273f542974SPaul B Schroeder 
2283f542974SPaul B Schroeder 
2293f542974SPaul B Schroeder static int debug;
2303f542974SPaul B Schroeder 
2313f542974SPaul B Schroeder /*
2323f542974SPaul B Schroeder  * mos7840_set_reg_sync
2333f542974SPaul B Schroeder  * 	To set the Control register by calling usb_fill_control_urb function
2343f542974SPaul B Schroeder  *	by passing usb_sndctrlpipe function as parameter.
2353f542974SPaul B Schroeder  */
2363f542974SPaul B Schroeder 
2373f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2383f542974SPaul B Schroeder 				__u16 val)
2393f542974SPaul B Schroeder {
2403f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2413f542974SPaul B Schroeder 	val = val & 0x00ff;
24284fe6e79STony Cook 	dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
2433f542974SPaul B Schroeder 
2443f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2453f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2463f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2473f542974SPaul B Schroeder }
2483f542974SPaul B Schroeder 
2493f542974SPaul B Schroeder /*
2503f542974SPaul B Schroeder  * mos7840_get_reg_sync
2513f542974SPaul B Schroeder  * 	To set the Uart register by calling usb_fill_control_urb function by
2523f542974SPaul B Schroeder  *	passing usb_rcvctrlpipe function as parameter.
2533f542974SPaul B Schroeder  */
2543f542974SPaul B Schroeder 
2553f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
2563f542974SPaul B Schroeder 				__u16 *val)
2573f542974SPaul B Schroeder {
2583f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2593f542974SPaul B Schroeder 	int ret = 0;
2603f542974SPaul B Schroeder 
2613f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
2623f542974SPaul B Schroeder 			      MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
2633f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
26484fe6e79STony Cook 	dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
2653f542974SPaul B Schroeder 	*val = (*val) & 0x00ff;
2663f542974SPaul B Schroeder 	return ret;
2673f542974SPaul B Schroeder }
2683f542974SPaul B Schroeder 
2693f542974SPaul B Schroeder /*
2703f542974SPaul B Schroeder  * mos7840_set_uart_reg
2713f542974SPaul B Schroeder  *	To set the Uart register by calling usb_fill_control_urb function by
2723f542974SPaul B Schroeder  *	passing usb_sndctrlpipe function as parameter.
2733f542974SPaul B Schroeder  */
2743f542974SPaul B Schroeder 
2753f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
2763f542974SPaul B Schroeder 				__u16 val)
2773f542974SPaul B Schroeder {
2783f542974SPaul B Schroeder 
2793f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2803f542974SPaul B Schroeder 	val = val & 0x00ff;
281880af9dbSAlan Cox 	/* For the UART control registers, the application number need
282880af9dbSAlan Cox 	   to be Or'ed */
2830de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
284880af9dbSAlan Cox 		val |= (((__u16) port->number -
285880af9dbSAlan Cox 				(__u16) (port->serial->minor)) + 1) << 8;
28684fe6e79STony Cook 		dbg("mos7840_set_uart_reg application number is %x", val);
2873f542974SPaul B Schroeder 	} else {
2883f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
289880af9dbSAlan Cox 			val |= (((__u16) port->number -
2903f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
29184fe6e79STony Cook 			dbg("mos7840_set_uart_reg application number is %x",
2923f542974SPaul B Schroeder 			    val);
2933f542974SPaul B Schroeder 		} else {
2943f542974SPaul B Schroeder 			val |=
2953f542974SPaul B Schroeder 			    (((__u16) port->number -
2963f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
29784fe6e79STony Cook 			dbg("mos7840_set_uart_reg application number is %x",
2983f542974SPaul B Schroeder 			    val);
2993f542974SPaul B Schroeder 		}
3003f542974SPaul B Schroeder 	}
3013f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
3023f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
3033f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
3043f542974SPaul B Schroeder 
3053f542974SPaul B Schroeder }
3063f542974SPaul B Schroeder 
3073f542974SPaul B Schroeder /*
3083f542974SPaul B Schroeder  * mos7840_get_uart_reg
3093f542974SPaul B Schroeder  *	To set the Control register by calling usb_fill_control_urb function
3103f542974SPaul B Schroeder  *	by passing usb_rcvctrlpipe function as parameter.
3113f542974SPaul B Schroeder  */
3123f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3133f542974SPaul B Schroeder 				__u16 *val)
3143f542974SPaul B Schroeder {
3153f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3163f542974SPaul B Schroeder 	int ret = 0;
3173f542974SPaul B Schroeder 	__u16 Wval;
3183f542974SPaul B Schroeder 
31984fe6e79STony Cook 	/* dbg("application number is %4x",
320880af9dbSAlan Cox 	    (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
3213f542974SPaul B Schroeder 	/* Wval  is same as application number */
3220de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3233f542974SPaul B Schroeder 		Wval =
3243f542974SPaul B Schroeder 		    (((__u16) port->number - (__u16) (port->serial->minor)) +
3253f542974SPaul B Schroeder 		     1) << 8;
32684fe6e79STony Cook 		dbg("mos7840_get_uart_reg application number is %x", Wval);
3273f542974SPaul B Schroeder 	} else {
3283f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
329880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3303f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
33184fe6e79STony Cook 			dbg("mos7840_get_uart_reg application number is %x",
3323f542974SPaul B Schroeder 			    Wval);
3333f542974SPaul B Schroeder 		} else {
334880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3353f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
33684fe6e79STony Cook 			dbg("mos7840_get_uart_reg application number is %x",
3373f542974SPaul B Schroeder 			    Wval);
3383f542974SPaul B Schroeder 		}
3393f542974SPaul B Schroeder 	}
3403f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3413f542974SPaul B Schroeder 			      MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
3423f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
3433f542974SPaul B Schroeder 	*val = (*val) & 0x00ff;
3443f542974SPaul B Schroeder 	return ret;
3453f542974SPaul B Schroeder }
3463f542974SPaul B Schroeder 
3473f542974SPaul B Schroeder static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
3483f542974SPaul B Schroeder {
3493f542974SPaul B Schroeder 
35084fe6e79STony Cook 	dbg("***************************************");
35184fe6e79STony Cook 	dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
35284fe6e79STony Cook 	dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
35384fe6e79STony Cook 	dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
35484fe6e79STony Cook 	dbg("***************************************");
3553f542974SPaul B Schroeder 
3563f542974SPaul B Schroeder }
3573f542974SPaul B Schroeder 
3583f542974SPaul B Schroeder /************************************************************************/
3593f542974SPaul B Schroeder /************************************************************************/
3603f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3613f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3623f542974SPaul B Schroeder /************************************************************************/
3633f542974SPaul B Schroeder /************************************************************************/
3643f542974SPaul B Schroeder 
3653f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port,
3663f542974SPaul B Schroeder 					    struct moschip_port *data)
3673f542974SPaul B Schroeder {
3683f542974SPaul B Schroeder 	usb_set_serial_port_data(port, (void *)data);
3693f542974SPaul B Schroeder }
3703f542974SPaul B Schroeder 
3713f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct
3723f542974SPaul B Schroeder 							    usb_serial_port
3733f542974SPaul B Schroeder 							    *port)
3743f542974SPaul B Schroeder {
3753f542974SPaul B Schroeder 	return (struct moschip_port *)usb_get_serial_port_data(port);
3763f542974SPaul B Schroeder }
3773f542974SPaul B Schroeder 
3780de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
3793f542974SPaul B Schroeder {
3803f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
3813f542974SPaul B Schroeder 	struct async_icount *icount;
3823f542974SPaul B Schroeder 	mos7840_port = port;
3833f542974SPaul B Schroeder 	icount = &mos7840_port->icount;
3843f542974SPaul B Schroeder 	if (new_msr &
3853f542974SPaul B Schroeder 	    (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
3863f542974SPaul B Schroeder 	     MOS_MSR_DELTA_CD)) {
3873f542974SPaul B Schroeder 		icount = &mos7840_port->icount;
3883f542974SPaul B Schroeder 
3893f542974SPaul B Schroeder 		/* update input line counters */
3903f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CTS) {
3913f542974SPaul B Schroeder 			icount->cts++;
3920de9a702SOliver Neukum 			smp_wmb();
3933f542974SPaul B Schroeder 		}
3943f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_DSR) {
3953f542974SPaul B Schroeder 			icount->dsr++;
3960de9a702SOliver Neukum 			smp_wmb();
3973f542974SPaul B Schroeder 		}
3983f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CD) {
3993f542974SPaul B Schroeder 			icount->dcd++;
4000de9a702SOliver Neukum 			smp_wmb();
4013f542974SPaul B Schroeder 		}
4023f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_RI) {
4033f542974SPaul B Schroeder 			icount->rng++;
4040de9a702SOliver Neukum 			smp_wmb();
4050de9a702SOliver Neukum 		}
4063f542974SPaul B Schroeder 	}
4073f542974SPaul B Schroeder }
4083f542974SPaul B Schroeder 
4090de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
4103f542974SPaul B Schroeder {
4113f542974SPaul B Schroeder 	struct async_icount *icount;
4123f542974SPaul B Schroeder 
413441b62c1SHarvey Harrison 	dbg("%s - %02x", __func__, new_lsr);
4143f542974SPaul B Schroeder 
4153f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
416880af9dbSAlan Cox 		/*
417880af9dbSAlan Cox 		 * Parity and Framing errors only count if they
418880af9dbSAlan Cox 		 * occur exclusive of a break being
419880af9dbSAlan Cox 		 * received.
420880af9dbSAlan Cox 		 */
4213f542974SPaul B Schroeder 		new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
4223f542974SPaul B Schroeder 	}
4233f542974SPaul B Schroeder 
4243f542974SPaul B Schroeder 	/* update input line counters */
4253f542974SPaul B Schroeder 	icount = &port->icount;
4263f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
4273f542974SPaul B Schroeder 		icount->brk++;
4280de9a702SOliver Neukum 		smp_wmb();
4293f542974SPaul B Schroeder 	}
4303f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_OE) {
4313f542974SPaul B Schroeder 		icount->overrun++;
4320de9a702SOliver Neukum 		smp_wmb();
4333f542974SPaul B Schroeder 	}
4343f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_PE) {
4353f542974SPaul B Schroeder 		icount->parity++;
4360de9a702SOliver Neukum 		smp_wmb();
4373f542974SPaul B Schroeder 	}
4383f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_FE) {
4393f542974SPaul B Schroeder 		icount->frame++;
4400de9a702SOliver Neukum 		smp_wmb();
4413f542974SPaul B Schroeder 	}
4423f542974SPaul B Schroeder }
4433f542974SPaul B Schroeder 
4443f542974SPaul B Schroeder /************************************************************************/
4453f542974SPaul B Schroeder /************************************************************************/
4463f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4473f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4483f542974SPaul B Schroeder /************************************************************************/
4493f542974SPaul B Schroeder /************************************************************************/
4503f542974SPaul B Schroeder 
4517d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb)
4523f542974SPaul B Schroeder {
4533f542974SPaul B Schroeder 	unsigned char *data;
4543f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4553f542974SPaul B Schroeder 	__u8 regval = 0x0;
4560de9a702SOliver Neukum 	int result = 0;
4570643c724SGreg Kroah-Hartman 	int status = urb->status;
4583f542974SPaul B Schroeder 
459cdc97792SMing Lei 	mos7840_port = urb->context;
4600de9a702SOliver Neukum 
4610643c724SGreg Kroah-Hartman 	switch (status) {
4623f542974SPaul B Schroeder 	case 0:
4633f542974SPaul B Schroeder 		/* success */
4643f542974SPaul B Schroeder 		break;
4653f542974SPaul B Schroeder 	case -ECONNRESET:
4663f542974SPaul B Schroeder 	case -ENOENT:
4673f542974SPaul B Schroeder 	case -ESHUTDOWN:
4683f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
469441b62c1SHarvey Harrison 		dbg("%s - urb shutting down with status: %d", __func__,
4700643c724SGreg Kroah-Hartman 		    status);
4713f542974SPaul B Schroeder 		return;
4723f542974SPaul B Schroeder 	default:
473441b62c1SHarvey Harrison 		dbg("%s - nonzero urb status received: %d", __func__,
4740643c724SGreg Kroah-Hartman 		    status);
4753f542974SPaul B Schroeder 		goto exit;
4763f542974SPaul B Schroeder 	}
4773f542974SPaul B Schroeder 
47884fe6e79STony Cook 	dbg("%s urb buffer size is %d", __func__, urb->actual_length);
47984fe6e79STony Cook 	dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
4803f542974SPaul B Schroeder 	    mos7840_port->MsrLsr, mos7840_port->port_num);
4813f542974SPaul B Schroeder 	data = urb->transfer_buffer;
4823f542974SPaul B Schroeder 	regval = (__u8) data[0];
48384fe6e79STony Cook 	dbg("%s data is %x", __func__, regval);
4843f542974SPaul B Schroeder 	if (mos7840_port->MsrLsr == 0)
4853f542974SPaul B Schroeder 		mos7840_handle_new_msr(mos7840_port, regval);
4863f542974SPaul B Schroeder 	else if (mos7840_port->MsrLsr == 1)
4873f542974SPaul B Schroeder 		mos7840_handle_new_lsr(mos7840_port, regval);
4883f542974SPaul B Schroeder 
4893f542974SPaul B Schroeder exit:
4900de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
4910de9a702SOliver Neukum 	if (!mos7840_port->zombie)
4920de9a702SOliver Neukum 		result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
4930de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
4940de9a702SOliver Neukum 	if (result) {
4950de9a702SOliver Neukum 		dev_err(&urb->dev->dev,
4960de9a702SOliver Neukum 			"%s - Error %d submitting interrupt urb\n",
497441b62c1SHarvey Harrison 			__func__, result);
4980de9a702SOliver Neukum 	}
4993f542974SPaul B Schroeder }
5003f542974SPaul B Schroeder 
5013f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
5023f542974SPaul B Schroeder 			   __u16 *val)
5033f542974SPaul B Schroeder {
5043f542974SPaul B Schroeder 	struct usb_device *dev = mcs->port->serial->dev;
5050de9a702SOliver Neukum 	struct usb_ctrlrequest *dr = mcs->dr;
5060de9a702SOliver Neukum 	unsigned char *buffer = mcs->ctrl_buf;
5070de9a702SOliver Neukum 	int ret;
5083f542974SPaul B Schroeder 
5093f542974SPaul B Schroeder 	dr->bRequestType = MCS_RD_RTYPE;
5103f542974SPaul B Schroeder 	dr->bRequest = MCS_RDREQ;
511880af9dbSAlan Cox 	dr->wValue = cpu_to_le16(Wval);	/* 0 */
5123f542974SPaul B Schroeder 	dr->wIndex = cpu_to_le16(reg);
5133f542974SPaul B Schroeder 	dr->wLength = cpu_to_le16(2);
5143f542974SPaul B Schroeder 
5153f542974SPaul B Schroeder 	usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
5163f542974SPaul B Schroeder 			     (unsigned char *)dr, buffer, 2,
5173f542974SPaul B Schroeder 			     mos7840_control_callback, mcs);
5183f542974SPaul B Schroeder 	mcs->control_urb->transfer_buffer_length = 2;
5193f542974SPaul B Schroeder 	ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
5203f542974SPaul B Schroeder 	return ret;
5213f542974SPaul B Schroeder }
5223f542974SPaul B Schroeder 
5233f542974SPaul B Schroeder /*****************************************************************************
5243f542974SPaul B Schroeder  * mos7840_interrupt_callback
5253f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
5263f542974SPaul B Schroeder  *	interrupt endpoint.
5273f542974SPaul B Schroeder  *****************************************************************************/
5283f542974SPaul B Schroeder 
5297d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb)
5303f542974SPaul B Schroeder {
5313f542974SPaul B Schroeder 	int result;
5323f542974SPaul B Schroeder 	int length;
5333f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
5343f542974SPaul B Schroeder 	struct usb_serial *serial;
5353f542974SPaul B Schroeder 	__u16 Data;
5363f542974SPaul B Schroeder 	unsigned char *data;
5373f542974SPaul B Schroeder 	__u8 sp[5], st;
5380de9a702SOliver Neukum 	int i, rv = 0;
5390de9a702SOliver Neukum 	__u16 wval, wreg = 0;
5400643c724SGreg Kroah-Hartman 	int status = urb->status;
5413f542974SPaul B Schroeder 
54284fe6e79STony Cook 	dbg("%s", " : Entering");
5433f542974SPaul B Schroeder 
5440643c724SGreg Kroah-Hartman 	switch (status) {
5453f542974SPaul B Schroeder 	case 0:
5463f542974SPaul B Schroeder 		/* success */
5473f542974SPaul B Schroeder 		break;
5483f542974SPaul B Schroeder 	case -ECONNRESET:
5493f542974SPaul B Schroeder 	case -ENOENT:
5503f542974SPaul B Schroeder 	case -ESHUTDOWN:
5513f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
552441b62c1SHarvey Harrison 		dbg("%s - urb shutting down with status: %d", __func__,
5530643c724SGreg Kroah-Hartman 		    status);
5543f542974SPaul B Schroeder 		return;
5553f542974SPaul B Schroeder 	default:
556441b62c1SHarvey Harrison 		dbg("%s - nonzero urb status received: %d", __func__,
5570643c724SGreg Kroah-Hartman 		    status);
5583f542974SPaul B Schroeder 		goto exit;
5593f542974SPaul B Schroeder 	}
5603f542974SPaul B Schroeder 
5613f542974SPaul B Schroeder 	length = urb->actual_length;
5623f542974SPaul B Schroeder 	data = urb->transfer_buffer;
5633f542974SPaul B Schroeder 
564cdc97792SMing Lei 	serial = urb->context;
5653f542974SPaul B Schroeder 
5663f542974SPaul B Schroeder 	/* Moschip get 5 bytes
5673f542974SPaul B Schroeder 	 * Byte 1 IIR Port 1 (port.number is 0)
5683f542974SPaul B Schroeder 	 * Byte 2 IIR Port 2 (port.number is 1)
5693f542974SPaul B Schroeder 	 * Byte 3 IIR Port 3 (port.number is 2)
5703f542974SPaul B Schroeder 	 * Byte 4 IIR Port 4 (port.number is 3)
5713f542974SPaul B Schroeder 	 * Byte 5 FIFO status for both */
5723f542974SPaul B Schroeder 
5733f542974SPaul B Schroeder 	if (length && length > 5) {
57484fe6e79STony Cook 		dbg("%s", "Wrong data !!!");
5753f542974SPaul B Schroeder 		return;
5763f542974SPaul B Schroeder 	}
5773f542974SPaul B Schroeder 
5783f542974SPaul B Schroeder 	sp[0] = (__u8) data[0];
5793f542974SPaul B Schroeder 	sp[1] = (__u8) data[1];
5803f542974SPaul B Schroeder 	sp[2] = (__u8) data[2];
5813f542974SPaul B Schroeder 	sp[3] = (__u8) data[3];
5823f542974SPaul B Schroeder 	st = (__u8) data[4];
5833f542974SPaul B Schroeder 
5843f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; i++) {
5853f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
5863f542974SPaul B Schroeder 		wval =
5873f542974SPaul B Schroeder 		    (((__u16) serial->port[i]->number -
5883f542974SPaul B Schroeder 		      (__u16) (serial->minor)) + 1) << 8;
5893f542974SPaul B Schroeder 		if (mos7840_port->open) {
5903f542974SPaul B Schroeder 			if (sp[i] & 0x01) {
59184fe6e79STony Cook 				dbg("SP%d No Interrupt !!!", i);
5923f542974SPaul B Schroeder 			} else {
5933f542974SPaul B Schroeder 				switch (sp[i] & 0x0f) {
5943f542974SPaul B Schroeder 				case SERIAL_IIR_RLS:
5953f542974SPaul B Schroeder 					dbg("Serial Port %d: Receiver status error or ", i);
59684fe6e79STony Cook 					dbg("address bit detected in 9-bit mode");
5973f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 1;
5980de9a702SOliver Neukum 					wreg = LINE_STATUS_REGISTER;
5993f542974SPaul B Schroeder 					break;
6003f542974SPaul B Schroeder 				case SERIAL_IIR_MS:
60184fe6e79STony Cook 					dbg("Serial Port %d: Modem status change", i);
6023f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 0;
6030de9a702SOliver Neukum 					wreg = MODEM_STATUS_REGISTER;
6043f542974SPaul B Schroeder 					break;
6053f542974SPaul B Schroeder 				}
6060de9a702SOliver Neukum 				spin_lock(&mos7840_port->pool_lock);
6070de9a702SOliver Neukum 				if (!mos7840_port->zombie) {
6080de9a702SOliver Neukum 					rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
6090de9a702SOliver Neukum 				} else {
6100de9a702SOliver Neukum 					spin_unlock(&mos7840_port->pool_lock);
6110de9a702SOliver Neukum 					return;
6120de9a702SOliver Neukum 				}
6130de9a702SOliver Neukum 				spin_unlock(&mos7840_port->pool_lock);
6143f542974SPaul B Schroeder 			}
6153f542974SPaul B Schroeder 		}
6163f542974SPaul B Schroeder 	}
617880af9dbSAlan Cox 	if (!(rv < 0))
618880af9dbSAlan Cox 		/* the completion handler for the control urb will resubmit */
6190de9a702SOliver Neukum 		return;
6203f542974SPaul B Schroeder exit:
6213f542974SPaul B Schroeder 	result = usb_submit_urb(urb, GFP_ATOMIC);
6223f542974SPaul B Schroeder 	if (result) {
6233f542974SPaul B Schroeder 		dev_err(&urb->dev->dev,
6243f542974SPaul B Schroeder 			"%s - Error %d submitting interrupt urb\n",
625441b62c1SHarvey Harrison 			__func__, result);
6263f542974SPaul B Schroeder 	}
6273f542974SPaul B Schroeder }
6283f542974SPaul B Schroeder 
6293f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port,
6303f542974SPaul B Schroeder 				       const char *function)
6313f542974SPaul B Schroeder {
6323f542974SPaul B Schroeder 	if (!port) {
6333f542974SPaul B Schroeder 		dbg("%s - port == NULL", function);
6343f542974SPaul B Schroeder 		return -1;
6353f542974SPaul B Schroeder 	}
6363f542974SPaul B Schroeder 	if (!port->serial) {
6373f542974SPaul B Schroeder 		dbg("%s - port->serial == NULL", function);
6383f542974SPaul B Schroeder 		return -1;
6393f542974SPaul B Schroeder 	}
6403f542974SPaul B Schroeder 
6413f542974SPaul B Schroeder 	return 0;
6423f542974SPaul B Schroeder }
6433f542974SPaul B Schroeder 
6443f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
6453f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
6463f542974SPaul B Schroeder 					 const char *function)
6473f542974SPaul B Schroeder {
6483f542974SPaul B Schroeder 	if (!serial) {
6493f542974SPaul B Schroeder 		dbg("%s - serial == NULL", function);
6503f542974SPaul B Schroeder 		return -1;
6513f542974SPaul B Schroeder 	}
6523f542974SPaul B Schroeder 	if (!serial->type) {
6533f542974SPaul B Schroeder 		dbg("%s - serial->type == NULL!", function);
6543f542974SPaul B Schroeder 		return -1;
6553f542974SPaul B Schroeder 	}
6563f542974SPaul B Schroeder 
6573f542974SPaul B Schroeder 	return 0;
6583f542974SPaul B Schroeder }
6593f542974SPaul B Schroeder 
6603f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
6613f542974SPaul B Schroeder 						 const char *function)
6623f542974SPaul B Schroeder {
6633f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
6643f542974SPaul B Schroeder 	if (!port ||
6653f542974SPaul B Schroeder 	    mos7840_port_paranoia_check(port, function) ||
6663f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
667880af9dbSAlan Cox 		/* then say that we don't have a valid usb_serial thing,
668880af9dbSAlan Cox 		 * which will end up genrating -ENODEV return values */
6693f542974SPaul B Schroeder 		return NULL;
6703f542974SPaul B Schroeder 	}
6713f542974SPaul B Schroeder 
6723f542974SPaul B Schroeder 	return port->serial;
6733f542974SPaul B Schroeder }
6743f542974SPaul B Schroeder 
6753f542974SPaul B Schroeder /*****************************************************************************
6763f542974SPaul B Schroeder  * mos7840_bulk_in_callback
6773f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
6783f542974SPaul B Schroeder  *	bulk in endpoint.
6793f542974SPaul B Schroeder  *****************************************************************************/
6803f542974SPaul B Schroeder 
6817d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
6823f542974SPaul B Schroeder {
6830643c724SGreg Kroah-Hartman 	int retval;
6843f542974SPaul B Schroeder 	unsigned char *data;
6853f542974SPaul B Schroeder 	struct usb_serial *serial;
6863f542974SPaul B Schroeder 	struct usb_serial_port *port;
6873f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
6883f542974SPaul B Schroeder 	struct tty_struct *tty;
6890643c724SGreg Kroah-Hartman 	int status = urb->status;
6903f542974SPaul B Schroeder 
691cdc97792SMing Lei 	mos7840_port = urb->context;
6923f542974SPaul B Schroeder 	if (!mos7840_port) {
69384fe6e79STony Cook 		dbg("%s", "NULL mos7840_port pointer");
69450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
69550de36f7SGreg Kroah-Hartman 		return;
69650de36f7SGreg Kroah-Hartman 	}
69750de36f7SGreg Kroah-Hartman 
69850de36f7SGreg Kroah-Hartman 	if (status) {
69950de36f7SGreg Kroah-Hartman 		dbg("nonzero read bulk status received: %d", status);
70050de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7013f542974SPaul B Schroeder 		return;
7023f542974SPaul B Schroeder 	}
7033f542974SPaul B Schroeder 
7043f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
705441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
70684fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
70750de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7083f542974SPaul B Schroeder 		return;
7093f542974SPaul B Schroeder 	}
7103f542974SPaul B Schroeder 
711441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
7123f542974SPaul B Schroeder 	if (!serial) {
71384fe6e79STony Cook 		dbg("%s", "Bad serial pointer");
71450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7153f542974SPaul B Schroeder 		return;
7163f542974SPaul B Schroeder 	}
7173f542974SPaul B Schroeder 
71884fe6e79STony Cook 	dbg("%s", "Entering... ");
7193f542974SPaul B Schroeder 
7203f542974SPaul B Schroeder 	data = urb->transfer_buffer;
7213f542974SPaul B Schroeder 
72284fe6e79STony Cook 	dbg("%s", "Entering ...........");
7233f542974SPaul B Schroeder 
7243f542974SPaul B Schroeder 	if (urb->actual_length) {
7254a90f09bSAlan Cox 		tty = tty_port_tty_get(&mos7840_port->port->port);
7263f542974SPaul B Schroeder 		if (tty) {
7273f542974SPaul B Schroeder 			tty_buffer_request_room(tty, urb->actual_length);
7283f542974SPaul B Schroeder 			tty_insert_flip_string(tty, data, urb->actual_length);
72984fe6e79STony Cook 			dbg(" %s ", data);
7303f542974SPaul B Schroeder 			tty_flip_buffer_push(tty);
7314a90f09bSAlan Cox 			tty_kref_put(tty);
7323f542974SPaul B Schroeder 		}
7333f542974SPaul B Schroeder 		mos7840_port->icount.rx += urb->actual_length;
7340de9a702SOliver Neukum 		smp_wmb();
73584fe6e79STony Cook 		dbg("mos7840_port->icount.rx is %d:",
7363f542974SPaul B Schroeder 		    mos7840_port->icount.rx);
7373f542974SPaul B Schroeder 	}
7383f542974SPaul B Schroeder 
7393f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
74084fe6e79STony Cook 		dbg("%s", "URB KILLED !!!");
74150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7423f542974SPaul B Schroeder 		return;
7433f542974SPaul B Schroeder 	}
7443f542974SPaul B Schroeder 
7450de9a702SOliver Neukum 
7463f542974SPaul B Schroeder 	mos7840_port->read_urb->dev = serial->dev;
7473f542974SPaul B Schroeder 
74850de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
7490643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
7503f542974SPaul B Schroeder 
7510643c724SGreg Kroah-Hartman 	if (retval) {
75250de36f7SGreg Kroah-Hartman 		dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
75350de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7543f542974SPaul B Schroeder 	}
7553f542974SPaul B Schroeder }
7563f542974SPaul B Schroeder 
7573f542974SPaul B Schroeder /*****************************************************************************
7583f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
759880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
760880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
7613f542974SPaul B Schroeder  *****************************************************************************/
7623f542974SPaul B Schroeder 
7637d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
7643f542974SPaul B Schroeder {
7653f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
7663f542974SPaul B Schroeder 	struct tty_struct *tty;
7670643c724SGreg Kroah-Hartman 	int status = urb->status;
7680de9a702SOliver Neukum 	int i;
7690de9a702SOliver Neukum 
770cdc97792SMing Lei 	mos7840_port = urb->context;
7710de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
7720de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
7730de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
7740de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
7750de9a702SOliver Neukum 			break;
7760de9a702SOliver Neukum 		}
7770de9a702SOliver Neukum 	}
7780de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
7790de9a702SOliver Neukum 
7800643c724SGreg Kroah-Hartman 	if (status) {
78184fe6e79STony Cook 		dbg("nonzero write bulk status received:%d", status);
7823f542974SPaul B Schroeder 		return;
7833f542974SPaul B Schroeder 	}
7843f542974SPaul B Schroeder 
785441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
78684fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
7873f542974SPaul B Schroeder 		return;
7883f542974SPaul B Schroeder 	}
7893f542974SPaul B Schroeder 
79084fe6e79STony Cook 	dbg("%s", "Entering .........");
7913f542974SPaul B Schroeder 
7924a90f09bSAlan Cox 	tty = tty_port_tty_get(&mos7840_port->port->port);
793b963a844SJiri Slaby 	if (tty && mos7840_port->open)
794b963a844SJiri Slaby 		tty_wakeup(tty);
7954a90f09bSAlan Cox 	tty_kref_put(tty);
7963f542974SPaul B Schroeder 
7973f542974SPaul B Schroeder }
7983f542974SPaul B Schroeder 
7993f542974SPaul B Schroeder /************************************************************************/
8003f542974SPaul 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       */
8013f542974SPaul B Schroeder /************************************************************************/
8023f542974SPaul B Schroeder #ifdef MCSSerialProbe
8033f542974SPaul B Schroeder static int mos7840_serial_probe(struct usb_serial *serial,
8043f542974SPaul B Schroeder 				const struct usb_device_id *id)
8053f542974SPaul B Schroeder {
8063f542974SPaul B Schroeder 
8073f542974SPaul B Schroeder 	/*need to implement the mode_reg reading and updating\
8083f542974SPaul B Schroeder 	   structures usb_serial_ device_type\
8093f542974SPaul B Schroeder 	   (i.e num_ports, num_bulkin,bulkout etc) */
8103f542974SPaul B Schroeder 	/* Also we can update the changes  attach */
8113f542974SPaul B Schroeder 	return 1;
8123f542974SPaul B Schroeder }
8133f542974SPaul B Schroeder #endif
8143f542974SPaul B Schroeder 
8153f542974SPaul B Schroeder /*****************************************************************************
8163f542974SPaul B Schroeder  * mos7840_open
8173f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
8183f542974SPaul B Schroeder  *	If successful, we return 0
8193f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
8203f542974SPaul B Schroeder  *****************************************************************************/
8213f542974SPaul B Schroeder 
82295da310eSAlan Cox static int mos7840_open(struct tty_struct *tty,
82395da310eSAlan Cox 			struct usb_serial_port *port, struct file *filp)
8243f542974SPaul B Schroeder {
8253f542974SPaul B Schroeder 	int response;
8263f542974SPaul B Schroeder 	int j;
8273f542974SPaul B Schroeder 	struct usb_serial *serial;
8283f542974SPaul B Schroeder 	struct urb *urb;
8293f542974SPaul B Schroeder 	__u16 Data;
8303f542974SPaul B Schroeder 	int status;
8313f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8320de9a702SOliver Neukum 	struct moschip_port *port0;
8333f542974SPaul B Schroeder 
83484fe6e79STony Cook 	dbg ("%s enter", __func__);
83584fe6e79STony Cook 
836441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
83784fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
8383f542974SPaul B Schroeder 		return -ENODEV;
8393f542974SPaul B Schroeder 	}
8403f542974SPaul B Schroeder 
8413f542974SPaul B Schroeder 	serial = port->serial;
8423f542974SPaul B Schroeder 
843441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
84484fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
8453f542974SPaul B Schroeder 		return -ENODEV;
8463f542974SPaul B Schroeder 	}
8473f542974SPaul B Schroeder 
8483f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
8490de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
8503f542974SPaul B Schroeder 
8510de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
8523f542974SPaul B Schroeder 		return -ENODEV;
8533f542974SPaul B Schroeder 
8543f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
8553f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
8560de9a702SOliver Neukum 	port0->open_ports++;
8573f542974SPaul B Schroeder 
8583f542974SPaul B Schroeder 	/* Initialising the write urb pool */
8593f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
8600de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
8613f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
8623f542974SPaul B Schroeder 
8633f542974SPaul B Schroeder 		if (urb == NULL) {
864194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "No more urbs???\n");
8653f542974SPaul B Schroeder 			continue;
8663f542974SPaul B Schroeder 		}
8673f542974SPaul B Schroeder 
868880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
869880af9dbSAlan Cox 								GFP_KERNEL);
8703f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
8710de9a702SOliver Neukum 			usb_free_urb(urb);
8720de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
873194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
874194343d9SGreg Kroah-Hartman 				"%s-out of memory for urb buffers.\n",
875194343d9SGreg Kroah-Hartman 				__func__);
8763f542974SPaul B Schroeder 			continue;
8773f542974SPaul B Schroeder 		}
8783f542974SPaul B Schroeder 	}
8793f542974SPaul B Schroeder 
8803f542974SPaul B Schroeder /*****************************************************************************
8813f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
8823f542974SPaul B Schroeder  *
8833f542974SPaul B Schroeder  * Register Index
8843f542974SPaul B Schroeder  * 1 : IER
8853f542974SPaul B Schroeder  * 2 : FCR
8863f542974SPaul B Schroeder  * 3 : LCR
8873f542974SPaul B Schroeder  * 4 : MCR
8883f542974SPaul B Schroeder  *
8893f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
8903f542974SPaul B Schroeder  *****************************************************************************/
8913f542974SPaul B Schroeder 
892880af9dbSAlan Cox 	/* NEED to check the following Block */
8933f542974SPaul B Schroeder 
8943f542974SPaul B Schroeder 	Data = 0x0;
8953f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
8963f542974SPaul B Schroeder 	if (status < 0) {
89784fe6e79STony Cook 		dbg("Reading Spreg failed");
8983f542974SPaul B Schroeder 		return -1;
8993f542974SPaul B Schroeder 	}
9003f542974SPaul B Schroeder 	Data |= 0x80;
9013f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9023f542974SPaul B Schroeder 	if (status < 0) {
90384fe6e79STony Cook 		dbg("writing Spreg failed");
9043f542974SPaul B Schroeder 		return -1;
9053f542974SPaul B Schroeder 	}
9063f542974SPaul B Schroeder 
9073f542974SPaul B Schroeder 	Data &= ~0x80;
9083f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9093f542974SPaul B Schroeder 	if (status < 0) {
91084fe6e79STony Cook 		dbg("writing Spreg failed");
9113f542974SPaul B Schroeder 		return -1;
9123f542974SPaul B Schroeder 	}
913880af9dbSAlan Cox 	/* End of block to be checked */
9143f542974SPaul B Schroeder 
9153f542974SPaul B Schroeder 	Data = 0x0;
916880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
917880af9dbSAlan Cox 									&Data);
9183f542974SPaul B Schroeder 	if (status < 0) {
91984fe6e79STony Cook 		dbg("Reading Controlreg failed");
9203f542974SPaul B Schroeder 		return -1;
9213f542974SPaul B Schroeder 	}
922880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
923880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
924880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
925880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
9263f542974SPaul B Schroeder 	if (status < 0) {
92784fe6e79STony Cook 		dbg("writing Controlreg failed");
9283f542974SPaul B Schroeder 		return -1;
9293f542974SPaul B Schroeder 	}
930880af9dbSAlan Cox 	/* do register settings here */
931880af9dbSAlan Cox 	/* Set all regs to the device default values. */
932880af9dbSAlan Cox 	/***********************************
933880af9dbSAlan Cox 	 * First Disable all interrupts.
934880af9dbSAlan Cox 	 ***********************************/
9353f542974SPaul B Schroeder 	Data = 0x00;
9363f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
9373f542974SPaul B Schroeder 	if (status < 0) {
93884fe6e79STony Cook 		dbg("disabling interrupts failed");
9393f542974SPaul B Schroeder 		return -1;
9403f542974SPaul B Schroeder 	}
941880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
9423f542974SPaul B Schroeder 	Data = 0x00;
9433f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9443f542974SPaul B Schroeder 	if (status < 0) {
94584fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
9463f542974SPaul B Schroeder 		return -1;
9473f542974SPaul B Schroeder 	}
9483f542974SPaul B Schroeder 
9493f542974SPaul B Schroeder 	Data = 0xcf;
9503f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9513f542974SPaul B Schroeder 	if (status < 0) {
95284fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
9533f542974SPaul B Schroeder 		return -1;
9543f542974SPaul B Schroeder 	}
9553f542974SPaul B Schroeder 
9563f542974SPaul B Schroeder 	Data = 0x03;
9573f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9583f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9593f542974SPaul B Schroeder 
9603f542974SPaul B Schroeder 	Data = 0x0b;
9613f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
9623f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
9633f542974SPaul B Schroeder 
9643f542974SPaul B Schroeder 	Data = 0x00;
9653f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
9663f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9673f542974SPaul B Schroeder 
968880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
9693f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9703f542974SPaul B Schroeder 
9713f542974SPaul B Schroeder 	Data = 0x0c;
9723f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
9733f542974SPaul B Schroeder 
9743f542974SPaul B Schroeder 	Data = 0x0;
9753f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
9763f542974SPaul B Schroeder 
9773f542974SPaul B Schroeder 	Data = 0x00;
9783f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
9793f542974SPaul B Schroeder 
9803f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
9813f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9823f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9833f542974SPaul B Schroeder 
984880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
9853f542974SPaul B Schroeder 	Data = 0x0;
9863f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
9873f542974SPaul B Schroeder 
9883f542974SPaul B Schroeder 	Data = Data | 0x0c;
9893f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9903f542974SPaul B Schroeder 
9913f542974SPaul B Schroeder 	Data = Data & ~0x0c;
9923f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
993880af9dbSAlan Cox 	/* Finally enable all interrupts */
9943f542974SPaul B Schroeder 	Data = 0x0c;
9953f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
9963f542974SPaul B Schroeder 
997880af9dbSAlan Cox 	/* clearing rx_disable */
9983f542974SPaul B Schroeder 	Data = 0x0;
999880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1000880af9dbSAlan Cox 									&Data);
10013f542974SPaul B Schroeder 	Data = Data & ~0x20;
1002880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1003880af9dbSAlan Cox 									Data);
10043f542974SPaul B Schroeder 
1005880af9dbSAlan Cox 	/* rx_negate */
10063f542974SPaul B Schroeder 	Data = 0x0;
1007880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1008880af9dbSAlan Cox 									&Data);
10093f542974SPaul B Schroeder 	Data = Data | 0x10;
1010880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1011880af9dbSAlan Cox 									Data);
10123f542974SPaul B Schroeder 
10133f542974SPaul B Schroeder 	/* Check to see if we've set up our endpoint info yet    *
10143f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the structures *
10153f542974SPaul B Schroeder 	 * were not set up at that time.)                        */
10160de9a702SOliver Neukum 	if (port0->open_ports == 1) {
10173f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_buffer == NULL) {
10183f542974SPaul B Schroeder 			/* set up interrupt urb */
10193f542974SPaul B Schroeder 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
10203f542974SPaul B Schroeder 				serial->dev,
10213f542974SPaul B Schroeder 				usb_rcvintpipe(serial->dev,
1022880af9dbSAlan Cox 				serial->port[0]->interrupt_in_endpointAddress),
10233f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_buffer,
10243f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_urb->
10253f542974SPaul B Schroeder 				transfer_buffer_length,
10263f542974SPaul B Schroeder 				mos7840_interrupt_callback,
10273f542974SPaul B Schroeder 				serial,
1028880af9dbSAlan Cox 				serial->port[0]->interrupt_in_urb->interval);
10293f542974SPaul B Schroeder 
10303f542974SPaul B Schroeder 			/* start interrupt read for mos7840               *
10313f542974SPaul B Schroeder 			 * will continue as long as mos7840 is connected  */
10323f542974SPaul B Schroeder 
10333f542974SPaul B Schroeder 			response =
10343f542974SPaul B Schroeder 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
10353f542974SPaul B Schroeder 					   GFP_KERNEL);
10363f542974SPaul B Schroeder 			if (response) {
1037194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "%s - Error %d submitting "
1038194343d9SGreg Kroah-Hartman 					"interrupt urb\n", __func__, response);
10393f542974SPaul B Schroeder 			}
10403f542974SPaul B Schroeder 
10413f542974SPaul B Schroeder 		}
10423f542974SPaul B Schroeder 
10433f542974SPaul B Schroeder 	}
10443f542974SPaul B Schroeder 
10453f542974SPaul B Schroeder 	/* see if we've set up our endpoint info yet   *
10463f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the  *
10473f542974SPaul B Schroeder 	 * structures were not set up at that time.)   */
10483f542974SPaul B Schroeder 
104984fe6e79STony Cook 	dbg("port number is %d", port->number);
105084fe6e79STony Cook 	dbg("serial number is %d", port->serial->minor);
105184fe6e79STony Cook 	dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
105284fe6e79STony Cook 	dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
105384fe6e79STony Cook 	dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
105484fe6e79STony Cook 	dbg("port's number in the device is %d", mos7840_port->port_num);
10553f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
10563f542974SPaul B Schroeder 
10573f542974SPaul B Schroeder 	/* set up our bulk in urb */
10583f542974SPaul B Schroeder 
10593f542974SPaul B Schroeder 	usb_fill_bulk_urb(mos7840_port->read_urb,
10603f542974SPaul B Schroeder 			  serial->dev,
10613f542974SPaul B Schroeder 			  usb_rcvbulkpipe(serial->dev,
10623f542974SPaul B Schroeder 					  port->bulk_in_endpointAddress),
10633f542974SPaul B Schroeder 			  port->bulk_in_buffer,
10643f542974SPaul B Schroeder 			  mos7840_port->read_urb->transfer_buffer_length,
10653f542974SPaul B Schroeder 			  mos7840_bulk_in_callback, mos7840_port);
10663f542974SPaul B Schroeder 
106784fe6e79STony Cook 	dbg("mos7840_open: bulkin endpoint is %d",
10683f542974SPaul B Schroeder 	    port->bulk_in_endpointAddress);
106950de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
10703f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
10713f542974SPaul B Schroeder 	if (response) {
1072194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1073194343d9SGreg Kroah-Hartman 			__func__, response);
107450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
10753f542974SPaul B Schroeder 	}
10763f542974SPaul B Schroeder 
10773f542974SPaul B Schroeder 	/* initialize our wait queues */
10783f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->wait_chase);
10793f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->delta_msr_wait);
10803f542974SPaul B Schroeder 
10813f542974SPaul B Schroeder 	/* initialize our icount structure */
10823f542974SPaul B Schroeder 	memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
10833f542974SPaul B Schroeder 
10843f542974SPaul B Schroeder 	/* initialize our port settings */
1085880af9dbSAlan Cox 	/* Must set to enable ints! */
1086880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
10873f542974SPaul B Schroeder 	/* send a open port command */
10883f542974SPaul B Schroeder 	mos7840_port->open = 1;
1089880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
10903f542974SPaul B Schroeder 	mos7840_port->icount.tx = 0;
10913f542974SPaul B Schroeder 	mos7840_port->icount.rx = 0;
10923f542974SPaul B Schroeder 
109384fe6e79STony Cook 	dbg("usb_serial serial:%p       mos7840_port:%p\n      usb_serial_port port:%p",
1094880af9dbSAlan Cox 				serial, mos7840_port, port);
10953f542974SPaul B Schroeder 
109684fe6e79STony Cook 	dbg ("%s leave", __func__);
109784fe6e79STony Cook 
10983f542974SPaul B Schroeder 	return 0;
10993f542974SPaul B Schroeder 
11003f542974SPaul B Schroeder }
11013f542974SPaul B Schroeder 
11023f542974SPaul B Schroeder /*****************************************************************************
11033f542974SPaul B Schroeder  * mos7840_chars_in_buffer
11043f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
11053f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
11063f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
11073f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
11083f542974SPaul B Schroeder  *	system,
110995da310eSAlan Cox  *	Otherwise we return zero.
11103f542974SPaul B Schroeder  *****************************************************************************/
11113f542974SPaul B Schroeder 
111295da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
11133f542974SPaul B Schroeder {
111495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11153f542974SPaul B Schroeder 	int i;
11163f542974SPaul B Schroeder 	int chars = 0;
11170de9a702SOliver Neukum 	unsigned long flags;
11183f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11193f542974SPaul B Schroeder 
112084fe6e79STony Cook 	dbg("%s", " mos7840_chars_in_buffer:entering ...........");
11213f542974SPaul B Schroeder 
1122441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
112384fe6e79STony Cook 		dbg("%s", "Invalid port");
112495da310eSAlan Cox 		return 0;
11253f542974SPaul B Schroeder 	}
11263f542974SPaul B Schroeder 
11273f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11283f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
112984fe6e79STony Cook 		dbg("%s", "mos7840_break:leaving ...........");
113095da310eSAlan Cox 		return 0;
11313f542974SPaul B Schroeder 	}
11323f542974SPaul B Schroeder 
11330de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
113495da310eSAlan Cox 	for (i = 0; i < NUM_URBS; ++i)
113595da310eSAlan Cox 		if (mos7840_port->busy[i])
11363f542974SPaul B Schroeder 			chars += URB_TRANSFER_BUFFER_SIZE;
11370de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1138441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, chars);
11390de9a702SOliver Neukum 	return chars;
11403f542974SPaul B Schroeder 
11413f542974SPaul B Schroeder }
11423f542974SPaul B Schroeder 
11433f542974SPaul B Schroeder /*****************************************************************************
11443f542974SPaul B Schroeder  * mos7840_close
11453f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
11463f542974SPaul B Schroeder  *****************************************************************************/
11473f542974SPaul B Schroeder 
1148335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
11493f542974SPaul B Schroeder {
11503f542974SPaul B Schroeder 	struct usb_serial *serial;
11513f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11520de9a702SOliver Neukum 	struct moschip_port *port0;
11533f542974SPaul B Schroeder 	int j;
11543f542974SPaul B Schroeder 	__u16 Data;
11553f542974SPaul B Schroeder 
115684fe6e79STony Cook 	dbg("%s", "mos7840_close:entering...");
11573f542974SPaul B Schroeder 
1158441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
115984fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
11603f542974SPaul B Schroeder 		return;
11613f542974SPaul B Schroeder 	}
11623f542974SPaul B Schroeder 
1163441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
11643f542974SPaul B Schroeder 	if (!serial) {
116584fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
11663f542974SPaul B Schroeder 		return;
11673f542974SPaul B Schroeder 	}
11683f542974SPaul B Schroeder 
11693f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11700de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
11713f542974SPaul B Schroeder 
11720de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
11733f542974SPaul B Schroeder 		return;
11743f542974SPaul B Schroeder 
11753f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
11763f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
11773f542974SPaul B Schroeder 
11783f542974SPaul B Schroeder 	/* Freeing Write URBs */
11793f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
11803f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
11813f542974SPaul B Schroeder 			if (mos7840_port->write_urb_pool[j]->transfer_buffer)
11823f542974SPaul B Schroeder 				kfree(mos7840_port->write_urb_pool[j]->
11833f542974SPaul B Schroeder 				      transfer_buffer);
11843f542974SPaul B Schroeder 
11853f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
11863f542974SPaul B Schroeder 		}
11873f542974SPaul B Schroeder 	}
11883f542974SPaul B Schroeder 
11893f542974SPaul B Schroeder 	/* While closing port, shutdown all bulk read, write  *
11903f542974SPaul B Schroeder 	 * and interrupt read if they exists                  */
11913f542974SPaul B Schroeder 	if (serial->dev) {
11923f542974SPaul B Schroeder 		if (mos7840_port->write_urb) {
119384fe6e79STony Cook 			dbg("%s", "Shutdown bulk write");
11943f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->write_urb);
11953f542974SPaul B Schroeder 		}
11963f542974SPaul B Schroeder 		if (mos7840_port->read_urb) {
119784fe6e79STony Cook 			dbg("%s", "Shutdown bulk read");
11983f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->read_urb);
119950de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
12003f542974SPaul B Schroeder 		}
12013f542974SPaul B Schroeder 		if ((&mos7840_port->control_urb)) {
120284fe6e79STony Cook 			dbg("%s", "Shutdown control read");
1203880af9dbSAlan Cox 			/*/      usb_kill_urb (mos7840_port->control_urb); */
12043f542974SPaul B Schroeder 		}
12053f542974SPaul B Schroeder 	}
1206880af9dbSAlan Cox /*      if(mos7840_port->ctrl_buf != NULL) */
1207880af9dbSAlan Cox /*              kfree(mos7840_port->ctrl_buf); */
12080de9a702SOliver Neukum 	port0->open_ports--;
120984fe6e79STony Cook 	dbg("mos7840_num_open_ports in close%d:in port%d",
12100de9a702SOliver Neukum 	    port0->open_ports, port->number);
12110de9a702SOliver Neukum 	if (port0->open_ports == 0) {
12123f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_urb) {
121384fe6e79STony Cook 			dbg("%s", "Shutdown interrupt_in_urb");
12140de9a702SOliver Neukum 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
12153f542974SPaul B Schroeder 		}
12163f542974SPaul B Schroeder 	}
12173f542974SPaul B Schroeder 
12183f542974SPaul B Schroeder 	if (mos7840_port->write_urb) {
12193f542974SPaul B Schroeder 		/* if this urb had a transfer buffer already (old tx) free it */
1220880af9dbSAlan Cox 		if (mos7840_port->write_urb->transfer_buffer != NULL)
12213f542974SPaul B Schroeder 			kfree(mos7840_port->write_urb->transfer_buffer);
12223f542974SPaul B Schroeder 		usb_free_urb(mos7840_port->write_urb);
12233f542974SPaul B Schroeder 	}
12243f542974SPaul B Schroeder 
12253f542974SPaul B Schroeder 	Data = 0x0;
12263f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
12273f542974SPaul B Schroeder 
12283f542974SPaul B Schroeder 	Data = 0x00;
12293f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
12303f542974SPaul B Schroeder 
12313f542974SPaul B Schroeder 	mos7840_port->open = 0;
12323f542974SPaul B Schroeder 
123384fe6e79STony Cook 	dbg("%s", "Leaving ............");
12343f542974SPaul B Schroeder }
12353f542974SPaul B Schroeder 
12363f542974SPaul B Schroeder /************************************************************************
12373f542974SPaul B Schroeder  *
12383f542974SPaul B Schroeder  * mos7840_block_until_chase_response
12393f542974SPaul B Schroeder  *
12403f542974SPaul B Schroeder  *	This function will block the close until one of the following:
12413f542974SPaul B Schroeder  *		1. Response to our Chase comes from mos7840
1242dc0d5c1eSJoe Perches  *		2. A timeout of 10 seconds without activity has expired
12433f542974SPaul B Schroeder  *		   (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
12443f542974SPaul B Schroeder  *
12453f542974SPaul B Schroeder  ************************************************************************/
12463f542974SPaul B Schroeder 
124795da310eSAlan Cox static void mos7840_block_until_chase_response(struct tty_struct *tty,
124895da310eSAlan Cox 					struct moschip_port *mos7840_port)
12493f542974SPaul B Schroeder {
12503f542974SPaul B Schroeder 	int timeout = 1 * HZ;
12513f542974SPaul B Schroeder 	int wait = 10;
12523f542974SPaul B Schroeder 	int count;
12533f542974SPaul B Schroeder 
12543f542974SPaul B Schroeder 	while (1) {
125595da310eSAlan Cox 		count = mos7840_chars_in_buffer(tty);
12563f542974SPaul B Schroeder 
12573f542974SPaul B Schroeder 		/* Check for Buffer status */
1258880af9dbSAlan Cox 		if (count <= 0)
12593f542974SPaul B Schroeder 			return;
12603f542974SPaul B Schroeder 
12613f542974SPaul B Schroeder 		/* Block the thread for a while */
12623f542974SPaul B Schroeder 		interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
12633f542974SPaul B Schroeder 					       timeout);
12643f542974SPaul B Schroeder 		/* No activity.. count down section */
12653f542974SPaul B Schroeder 		wait--;
12663f542974SPaul B Schroeder 		if (wait == 0) {
1267441b62c1SHarvey Harrison 			dbg("%s - TIMEOUT", __func__);
12683f542974SPaul B Schroeder 			return;
12693f542974SPaul B Schroeder 		} else {
1270dc0d5c1eSJoe Perches 			/* Reset timeout value back to seconds */
12713f542974SPaul B Schroeder 			wait = 10;
12723f542974SPaul B Schroeder 		}
12733f542974SPaul B Schroeder 	}
12743f542974SPaul B Schroeder 
12753f542974SPaul B Schroeder }
12763f542974SPaul B Schroeder 
12773f542974SPaul B Schroeder /*****************************************************************************
12783f542974SPaul B Schroeder  * mos7840_break
12793f542974SPaul B Schroeder  *	this function sends a break to the port
12803f542974SPaul B Schroeder  *****************************************************************************/
128195da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
12823f542974SPaul B Schroeder {
128395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
12843f542974SPaul B Schroeder 	unsigned char data;
12853f542974SPaul B Schroeder 	struct usb_serial *serial;
12863f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12873f542974SPaul B Schroeder 
128884fe6e79STony Cook 	dbg("%s", "Entering ...........");
128984fe6e79STony Cook 	dbg("mos7840_break: Start");
12903f542974SPaul B Schroeder 
1291441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
129284fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
12933f542974SPaul B Schroeder 		return;
12943f542974SPaul B Schroeder 	}
12953f542974SPaul B Schroeder 
1296441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
12973f542974SPaul B Schroeder 	if (!serial) {
129884fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
12993f542974SPaul B Schroeder 		return;
13003f542974SPaul B Schroeder 	}
13013f542974SPaul B Schroeder 
13023f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13033f542974SPaul B Schroeder 
1304880af9dbSAlan Cox 	if (mos7840_port == NULL)
13053f542974SPaul B Schroeder 		return;
13063f542974SPaul B Schroeder 
130795da310eSAlan Cox 	if (serial->dev)
13083f542974SPaul B Schroeder 		/* flush and block until tx is empty */
130995da310eSAlan Cox 		mos7840_block_until_chase_response(tty, mos7840_port);
13103f542974SPaul B Schroeder 
131195da310eSAlan Cox 	if (break_state == -1)
13123f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
131395da310eSAlan Cox 	else
13143f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
13153f542974SPaul B Schroeder 
13166b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
13173f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
131884fe6e79STony Cook 	dbg("mcs7840_break mos7840_port->shadowLCR is %x",
13193f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
13203f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
13213f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
13223f542974SPaul B Schroeder 
13233f542974SPaul B Schroeder 	return;
13243f542974SPaul B Schroeder }
13253f542974SPaul B Schroeder 
13263f542974SPaul B Schroeder /*****************************************************************************
13273f542974SPaul B Schroeder  * mos7840_write_room
13283f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
13293f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
13303f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
13313f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
13323f542974SPaul B Schroeder  *****************************************************************************/
13333f542974SPaul B Schroeder 
133495da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
13353f542974SPaul B Schroeder {
133695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
13373f542974SPaul B Schroeder 	int i;
13383f542974SPaul B Schroeder 	int room = 0;
13390de9a702SOliver Neukum 	unsigned long flags;
13403f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13413f542974SPaul B Schroeder 
134284fe6e79STony Cook 	dbg("%s", " mos7840_write_room:entering ...........");
13433f542974SPaul B Schroeder 
1344441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
134584fe6e79STony Cook 		dbg("%s", "Invalid port");
134684fe6e79STony Cook 		dbg("%s", " mos7840_write_room:leaving ...........");
13473f542974SPaul B Schroeder 		return -1;
13483f542974SPaul B Schroeder 	}
13493f542974SPaul B Schroeder 
13503f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13513f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
135284fe6e79STony Cook 		dbg("%s", "mos7840_break:leaving ...........");
13533f542974SPaul B Schroeder 		return -1;
13543f542974SPaul B Schroeder 	}
13553f542974SPaul B Schroeder 
13560de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
13573f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
1358880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
13593f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
13603f542974SPaul B Schroeder 	}
13610de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
13623f542974SPaul B Schroeder 
13630de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1364441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, room);
13650de9a702SOliver Neukum 	return room;
13663f542974SPaul B Schroeder 
13673f542974SPaul B Schroeder }
13683f542974SPaul B Schroeder 
13693f542974SPaul B Schroeder /*****************************************************************************
13703f542974SPaul B Schroeder  * mos7840_write
13713f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
13723f542974SPaul B Schroeder  *	the port.
13733f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
13743f542974SPaul B Schroeder  *      return a negative error number.
13753f542974SPaul B Schroeder  *****************************************************************************/
13763f542974SPaul B Schroeder 
137795da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
13783f542974SPaul B Schroeder 			 const unsigned char *data, int count)
13793f542974SPaul B Schroeder {
13803f542974SPaul B Schroeder 	int status;
13813f542974SPaul B Schroeder 	int i;
13823f542974SPaul B Schroeder 	int bytes_sent = 0;
13833f542974SPaul B Schroeder 	int transfer_size;
13840de9a702SOliver Neukum 	unsigned long flags;
13853f542974SPaul B Schroeder 
13863f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13873f542974SPaul B Schroeder 	struct usb_serial *serial;
13883f542974SPaul B Schroeder 	struct urb *urb;
1389880af9dbSAlan Cox 	/* __u16 Data; */
13903f542974SPaul B Schroeder 	const unsigned char *current_position = data;
13913f542974SPaul B Schroeder 	unsigned char *data1;
139284fe6e79STony Cook 	dbg("%s", "entering ...........");
139384fe6e79STony Cook 	/* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1394880af9dbSAlan Cox 					mos7840_port->shadowLCR); */
13953f542974SPaul B Schroeder 
13963f542974SPaul B Schroeder #ifdef NOTMOS7840
13973f542974SPaul B Schroeder 	Data = 0x00;
13983f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
13993f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
140084fe6e79STony Cook 	dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
140184fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
14023f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14033f542974SPaul B Schroeder 
1404880af9dbSAlan Cox 	/* Data = 0x03; */
1405880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1406880af9dbSAlan Cox 	/* mos7840_port->shadowLCR=Data;//Need to add later */
14073f542974SPaul B Schroeder 
1408880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
14093f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14103f542974SPaul B Schroeder 
1411880af9dbSAlan Cox 	/* Data = 0x0c; */
1412880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
14133f542974SPaul B Schroeder 	Data = 0x00;
14143f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
141584fe6e79STony Cook 	dbg("mos7840_write:DLL value is %x", Data);
14163f542974SPaul B Schroeder 
14173f542974SPaul B Schroeder 	Data = 0x0;
14183f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
141984fe6e79STony Cook 	dbg("mos7840_write:DLM value is %x", Data);
14203f542974SPaul B Schroeder 
14213f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
142284fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
14233f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14243f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14253f542974SPaul B Schroeder #endif
14263f542974SPaul B Schroeder 
1427441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
142884fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
14293f542974SPaul B Schroeder 		return -1;
14303f542974SPaul B Schroeder 	}
14313f542974SPaul B Schroeder 
14323f542974SPaul B Schroeder 	serial = port->serial;
1433441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
143484fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
14353f542974SPaul B Schroeder 		return -1;
14363f542974SPaul B Schroeder 	}
14373f542974SPaul B Schroeder 
14383f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14393f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
144084fe6e79STony Cook 		dbg("%s", "mos7840_port is NULL");
14413f542974SPaul B Schroeder 		return -1;
14423f542974SPaul B Schroeder 	}
14433f542974SPaul B Schroeder 
14443f542974SPaul B Schroeder 	/* try to find a free urb in the list */
14453f542974SPaul B Schroeder 	urb = NULL;
14463f542974SPaul B Schroeder 
14470de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
14483f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
14490de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
14500de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
14513f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
145284fe6e79STony Cook 			dbg("URB:%d", i);
14533f542974SPaul B Schroeder 			break;
14543f542974SPaul B Schroeder 		}
14553f542974SPaul B Schroeder 	}
14560de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
14573f542974SPaul B Schroeder 
14583f542974SPaul B Schroeder 	if (urb == NULL) {
1459441b62c1SHarvey Harrison 		dbg("%s - no more free urbs", __func__);
14603f542974SPaul B Schroeder 		goto exit;
14613f542974SPaul B Schroeder 	}
14623f542974SPaul B Schroeder 
14633f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
14643f542974SPaul B Schroeder 		urb->transfer_buffer =
14653f542974SPaul B Schroeder 		    kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
14663f542974SPaul B Schroeder 
14673f542974SPaul B Schroeder 		if (urb->transfer_buffer == NULL) {
1468194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s no more kernel memory...\n",
1469194343d9SGreg Kroah-Hartman 				__func__);
14703f542974SPaul B Schroeder 			goto exit;
14713f542974SPaul B Schroeder 		}
14723f542974SPaul B Schroeder 	}
14733f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
14743f542974SPaul B Schroeder 
14753f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
14763f542974SPaul B Schroeder 
14773f542974SPaul B Schroeder 	/* fill urb with data and submit  */
14783f542974SPaul B Schroeder 	usb_fill_bulk_urb(urb,
14793f542974SPaul B Schroeder 			  serial->dev,
14803f542974SPaul B Schroeder 			  usb_sndbulkpipe(serial->dev,
14813f542974SPaul B Schroeder 					  port->bulk_out_endpointAddress),
14823f542974SPaul B Schroeder 			  urb->transfer_buffer,
14833f542974SPaul B Schroeder 			  transfer_size,
14843f542974SPaul B Schroeder 			  mos7840_bulk_out_data_callback, mos7840_port);
14853f542974SPaul B Schroeder 
14863f542974SPaul B Schroeder 	data1 = urb->transfer_buffer;
148784fe6e79STony Cook 	dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
14883f542974SPaul B Schroeder 
14893f542974SPaul B Schroeder 	/* send it down the pipe */
14903f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
14913f542974SPaul B Schroeder 
14923f542974SPaul B Schroeder 	if (status) {
14930de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
1494194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1495194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
14963f542974SPaul B Schroeder 		bytes_sent = status;
14973f542974SPaul B Schroeder 		goto exit;
14983f542974SPaul B Schroeder 	}
14993f542974SPaul B Schroeder 	bytes_sent = transfer_size;
15003f542974SPaul B Schroeder 	mos7840_port->icount.tx += transfer_size;
15010de9a702SOliver Neukum 	smp_wmb();
150284fe6e79STony Cook 	dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
15033f542974SPaul B Schroeder exit:
15043f542974SPaul B Schroeder 	return bytes_sent;
15053f542974SPaul B Schroeder 
15063f542974SPaul B Schroeder }
15073f542974SPaul B Schroeder 
15083f542974SPaul B Schroeder /*****************************************************************************
15093f542974SPaul B Schroeder  * mos7840_throttle
15103f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
15113f542974SPaul B Schroeder  *	being read from the port.
15123f542974SPaul B Schroeder  *****************************************************************************/
15133f542974SPaul B Schroeder 
151495da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
15153f542974SPaul B Schroeder {
151695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
15173f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15183f542974SPaul B Schroeder 	int status;
15193f542974SPaul B Schroeder 
1520441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
152184fe6e79STony Cook 		dbg("%s", "Invalid port");
15223f542974SPaul B Schroeder 		return;
15233f542974SPaul B Schroeder 	}
15243f542974SPaul B Schroeder 
152584fe6e79STony Cook 	dbg("- port %d", port->number);
15263f542974SPaul B Schroeder 
15273f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15283f542974SPaul B Schroeder 
15293f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15303f542974SPaul B Schroeder 		return;
15313f542974SPaul B Schroeder 
15323f542974SPaul B Schroeder 	if (!mos7840_port->open) {
153384fe6e79STony Cook 		dbg("%s", "port not opened");
15343f542974SPaul B Schroeder 		return;
15353f542974SPaul B Schroeder 	}
15363f542974SPaul B Schroeder 
153784fe6e79STony Cook 	dbg("%s", "Entering ..........");
15383f542974SPaul B Schroeder 
15393f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
15403f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
15413f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
154295da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
154395da310eSAlan Cox 		if (status <= 0)
15443f542974SPaul B Schroeder 			return;
15453f542974SPaul B Schroeder 	}
15463f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
15473f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
15483f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
154995da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
15503f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
155195da310eSAlan Cox 		if (status < 0)
15523f542974SPaul B Schroeder 			return;
15533f542974SPaul B Schroeder 	}
15543f542974SPaul B Schroeder 
15553f542974SPaul B Schroeder 	return;
15563f542974SPaul B Schroeder }
15573f542974SPaul B Schroeder 
15583f542974SPaul B Schroeder /*****************************************************************************
15593f542974SPaul B Schroeder  * mos7840_unthrottle
1560880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
1561880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
1562880af9dbSAlan Cox  *	called)
15633f542974SPaul B Schroeder  *****************************************************************************/
156495da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
15653f542974SPaul B Schroeder {
156695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
15673f542974SPaul B Schroeder 	int status;
15683f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
15693f542974SPaul B Schroeder 
1570441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
157184fe6e79STony Cook 		dbg("%s", "Invalid port");
15723f542974SPaul B Schroeder 		return;
15733f542974SPaul B Schroeder 	}
15743f542974SPaul B Schroeder 
15753f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15763f542974SPaul B Schroeder 		return;
15773f542974SPaul B Schroeder 
15783f542974SPaul B Schroeder 	if (!mos7840_port->open) {
1579441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
15803f542974SPaul B Schroeder 		return;
15813f542974SPaul B Schroeder 	}
15823f542974SPaul B Schroeder 
158384fe6e79STony Cook 	dbg("%s", "Entering ..........");
15843f542974SPaul B Schroeder 
15853f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
15863f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
15873f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
158895da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
158995da310eSAlan Cox 		if (status <= 0)
15903f542974SPaul B Schroeder 			return;
15913f542974SPaul B Schroeder 	}
15923f542974SPaul B Schroeder 
15933f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
15943f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
15953f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
159695da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
15973f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
159895da310eSAlan Cox 		if (status < 0)
15993f542974SPaul B Schroeder 			return;
16003f542974SPaul B Schroeder 	}
16013f542974SPaul B Schroeder }
16023f542974SPaul B Schroeder 
160395da310eSAlan Cox static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
16043f542974SPaul B Schroeder {
160595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16063f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
16073f542974SPaul B Schroeder 	unsigned int result;
16083f542974SPaul B Schroeder 	__u16 msr;
16093f542974SPaul B Schroeder 	__u16 mcr;
1610880af9dbSAlan Cox 	int status;
16113f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
16123f542974SPaul B Schroeder 
1613441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
16143f542974SPaul B Schroeder 
16153f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16163f542974SPaul B Schroeder 		return -ENODEV;
16173f542974SPaul B Schroeder 
16183f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
16193f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
16203f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
16213f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
16223f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
16233f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
16243f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
16253f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
16263f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
16273f542974SPaul B Schroeder 
1628441b62c1SHarvey Harrison 	dbg("%s - 0x%04X", __func__, result);
16293f542974SPaul B Schroeder 
16303f542974SPaul B Schroeder 	return result;
16313f542974SPaul B Schroeder }
16323f542974SPaul B Schroeder 
163395da310eSAlan Cox static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
16343f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
16353f542974SPaul B Schroeder {
163695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16373f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
16383f542974SPaul B Schroeder 	unsigned int mcr;
163987521c46SRoel Kluin 	int status;
16403f542974SPaul B Schroeder 
1641441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
16423f542974SPaul B Schroeder 
16433f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
16443f542974SPaul B Schroeder 
16453f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16463f542974SPaul B Schroeder 		return -ENODEV;
16473f542974SPaul B Schroeder 
1648e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
16493f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
16503f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
16513f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
16523f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
16533f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
16543f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
16553f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
16563f542974SPaul B Schroeder 
16573f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
16583f542974SPaul B Schroeder 		mcr |= MCR_RTS;
16593f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
16603f542974SPaul B Schroeder 		mcr |= MCR_DTR;
16613f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
16623f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
16633f542974SPaul B Schroeder 
16643f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
16653f542974SPaul B Schroeder 
16663f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
16673f542974SPaul B Schroeder 	if (status < 0) {
166884fe6e79STony Cook 		dbg("setting MODEM_CONTROL_REGISTER Failed");
166987521c46SRoel Kluin 		return status;
16703f542974SPaul B Schroeder 	}
16713f542974SPaul B Schroeder 
16723f542974SPaul B Schroeder 	return 0;
16733f542974SPaul B Schroeder }
16743f542974SPaul B Schroeder 
16753f542974SPaul B Schroeder /*****************************************************************************
16763f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
16773f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
16783f542974SPaul B Schroeder  *	baud rate.
16793f542974SPaul B Schroeder  *****************************************************************************/
16803f542974SPaul B Schroeder static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
16813f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
16823f542974SPaul B Schroeder {
16833f542974SPaul B Schroeder 
1684441b62c1SHarvey Harrison 	dbg("%s - %d", __func__, baudRate);
16853f542974SPaul B Schroeder 
16863f542974SPaul B Schroeder 	if (baudRate <= 115200) {
16873f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
16883f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
16893f542974SPaul B Schroeder 	}
16903f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
16913f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
16923f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
16933f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
16943f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
16953f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
16963f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
16973f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
16983f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
16993f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
17003f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
17013f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
17023f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
17033f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
17043f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
17053f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
17063f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
17073f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
17083f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
17093f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
17103f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
17113f542974SPaul B Schroeder 	}
17123f542974SPaul B Schroeder 	return 0;
17133f542974SPaul B Schroeder 
17143f542974SPaul B Schroeder #ifdef NOTMCS7840
17153f542974SPaul B Schroeder 
17163f542974SPaul B Schroeder 	for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
17173f542974SPaul B Schroeder 		if (mos7840_divisor_table[i].BaudRate == baudrate) {
17183f542974SPaul B Schroeder 			*divisor = mos7840_divisor_table[i].Divisor;
17193f542974SPaul B Schroeder 			return 0;
17203f542974SPaul B Schroeder 		}
17213f542974SPaul B Schroeder 	}
17223f542974SPaul B Schroeder 
17233f542974SPaul B Schroeder 	/* After trying for all the standard baud rates    *
17243f542974SPaul B Schroeder 	 * Try calculating the divisor for this baud rate  */
17253f542974SPaul B Schroeder 
17263f542974SPaul B Schroeder 	if (baudrate > 75 && baudrate < 230400) {
17273f542974SPaul B Schroeder 		/* get the divisor */
17283f542974SPaul B Schroeder 		custom = (__u16) (230400L / baudrate);
17293f542974SPaul B Schroeder 
17303f542974SPaul B Schroeder 		/* Check for round off */
17313f542974SPaul B Schroeder 		round1 = (__u16) (2304000L / baudrate);
17323f542974SPaul B Schroeder 		round = (__u16) (round1 - (custom * 10));
1733880af9dbSAlan Cox 		if (round > 4)
17343f542974SPaul B Schroeder 			custom++;
17353f542974SPaul B Schroeder 		*divisor = custom;
17363f542974SPaul B Schroeder 
173784fe6e79STony Cook 		dbg(" Baud %d = %d", baudrate, custom);
17383f542974SPaul B Schroeder 		return 0;
17393f542974SPaul B Schroeder 	}
17403f542974SPaul B Schroeder 
174184fe6e79STony Cook 	dbg("%s", " Baud calculation Failed...");
17423f542974SPaul B Schroeder 	return -1;
17433f542974SPaul B Schroeder #endif
17443f542974SPaul B Schroeder }
17453f542974SPaul B Schroeder 
17463f542974SPaul B Schroeder /*****************************************************************************
17473f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
17483f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
17493f542974SPaul B Schroeder  *	specified port.
17503f542974SPaul B Schroeder  *****************************************************************************/
17513f542974SPaul B Schroeder 
17523f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
17533f542974SPaul B Schroeder 					    int baudRate)
17543f542974SPaul B Schroeder {
17553f542974SPaul B Schroeder 	int divisor = 0;
17563f542974SPaul B Schroeder 	int status;
17573f542974SPaul B Schroeder 	__u16 Data;
17583f542974SPaul B Schroeder 	unsigned char number;
17593f542974SPaul B Schroeder 	__u16 clk_sel_val;
17603f542974SPaul B Schroeder 	struct usb_serial_port *port;
17613f542974SPaul B Schroeder 
17623f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17633f542974SPaul B Schroeder 		return -1;
17643f542974SPaul B Schroeder 
17653f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
1766441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
176784fe6e79STony Cook 		dbg("%s", "Invalid port");
17683f542974SPaul B Schroeder 		return -1;
17693f542974SPaul B Schroeder 	}
17703f542974SPaul B Schroeder 
1771441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
177284fe6e79STony Cook 		dbg("%s", "Invalid Serial");
17733f542974SPaul B Schroeder 		return -1;
17743f542974SPaul B Schroeder 	}
17753f542974SPaul B Schroeder 
177684fe6e79STony Cook 	dbg("%s", "Entering ..........");
17773f542974SPaul B Schroeder 
17783f542974SPaul B Schroeder 	number = mos7840_port->port->number - mos7840_port->port->serial->minor;
17793f542974SPaul B Schroeder 
1780441b62c1SHarvey Harrison 	dbg("%s - port = %d, baud = %d", __func__,
17813f542974SPaul B Schroeder 	    mos7840_port->port->number, baudRate);
1782880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
17833f542974SPaul B Schroeder 	if (baudRate > 115200) {
17843f542974SPaul B Schroeder #ifdef HW_flow_control
1785880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1786880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
17873f542974SPaul B Schroeder 		Data = 0x2b;
17883f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1789880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1790880af9dbSAlan Cox 									Data);
17913f542974SPaul B Schroeder 		if (status < 0) {
179284fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
17933f542974SPaul B Schroeder 			return -1;
17943f542974SPaul B Schroeder 		}
17953f542974SPaul B Schroeder #endif
17963f542974SPaul B Schroeder 
17973f542974SPaul B Schroeder 	} else {
17983f542974SPaul B Schroeder #ifdef HW_flow_control
1799880af9dbSAlan Cox 		/ *setting h/w flow control bit to 0 */
18003f542974SPaul B Schroeder 		Data = 0xb;
18013f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1802880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1803880af9dbSAlan Cox 									Data);
18043f542974SPaul B Schroeder 		if (status < 0) {
180584fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
18063f542974SPaul B Schroeder 			return -1;
18073f542974SPaul B Schroeder 		}
18083f542974SPaul B Schroeder #endif
18093f542974SPaul B Schroeder 
18103f542974SPaul B Schroeder 	}
18113f542974SPaul B Schroeder 
1812880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
18133f542974SPaul B Schroeder 		clk_sel_val = 0x0;
18143f542974SPaul B Schroeder 		Data = 0x0;
1815880af9dbSAlan Cox 		status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
18163f542974SPaul B Schroeder 						   &clk_sel_val);
1817880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
18183f542974SPaul B Schroeder 								 &Data);
18193f542974SPaul B Schroeder 		if (status < 0) {
182084fe6e79STony Cook 			dbg("reading spreg failed in set_serial_baud");
18213f542974SPaul B Schroeder 			return -1;
18223f542974SPaul B Schroeder 		}
18233f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1824880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1825880af9dbSAlan Cox 								Data);
18263f542974SPaul B Schroeder 		if (status < 0) {
182784fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
18283f542974SPaul B Schroeder 			return -1;
18293f542974SPaul B Schroeder 		}
18303f542974SPaul B Schroeder 		/* Calculate the Divisor */
18313f542974SPaul B Schroeder 
18323f542974SPaul B Schroeder 		if (status) {
1833194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
18343f542974SPaul B Schroeder 			return status;
18353f542974SPaul B Schroeder 		}
18363f542974SPaul B Schroeder 		/* Enable access to divisor latch */
18373f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
18383f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
18393f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
18403f542974SPaul B Schroeder 
18413f542974SPaul B Schroeder 		/* Write the divisor */
18423f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
184384fe6e79STony Cook 		dbg("set_serial_baud Value to write DLL is %x", Data);
18443f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
18453f542974SPaul B Schroeder 
18463f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
184784fe6e79STony Cook 		dbg("set_serial_baud Value to write DLM is %x", Data);
18483f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
18493f542974SPaul B Schroeder 
18503f542974SPaul B Schroeder 		/* Disable access to divisor latch */
18513f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
18523f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
18533f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
18543f542974SPaul B Schroeder 
18553f542974SPaul B Schroeder 	}
18563f542974SPaul B Schroeder 	return status;
18573f542974SPaul B Schroeder }
18583f542974SPaul B Schroeder 
18593f542974SPaul B Schroeder /*****************************************************************************
18603f542974SPaul B Schroeder  * mos7840_change_port_settings
18613f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
18623f542974SPaul B Schroeder  *      the specified new settings.
18633f542974SPaul B Schroeder  *****************************************************************************/
18643f542974SPaul B Schroeder 
186595da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
186695da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
18673f542974SPaul B Schroeder {
18683f542974SPaul B Schroeder 	int baud;
18693f542974SPaul B Schroeder 	unsigned cflag;
18703f542974SPaul B Schroeder 	unsigned iflag;
18713f542974SPaul B Schroeder 	__u8 lData;
18723f542974SPaul B Schroeder 	__u8 lParity;
18733f542974SPaul B Schroeder 	__u8 lStop;
18743f542974SPaul B Schroeder 	int status;
18753f542974SPaul B Schroeder 	__u16 Data;
18763f542974SPaul B Schroeder 	struct usb_serial_port *port;
18773f542974SPaul B Schroeder 	struct usb_serial *serial;
18783f542974SPaul B Schroeder 
18793f542974SPaul B Schroeder 	if (mos7840_port == NULL)
18803f542974SPaul B Schroeder 		return;
18813f542974SPaul B Schroeder 
18823f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
18833f542974SPaul B Schroeder 
1884441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
188584fe6e79STony Cook 		dbg("%s", "Invalid port");
18863f542974SPaul B Schroeder 		return;
18873f542974SPaul B Schroeder 	}
18883f542974SPaul B Schroeder 
1889441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
189084fe6e79STony Cook 		dbg("%s", "Invalid Serial");
18913f542974SPaul B Schroeder 		return;
18923f542974SPaul B Schroeder 	}
18933f542974SPaul B Schroeder 
18943f542974SPaul B Schroeder 	serial = port->serial;
18953f542974SPaul B Schroeder 
1896441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, mos7840_port->port->number);
18973f542974SPaul B Schroeder 
18983f542974SPaul B Schroeder 	if (!mos7840_port->open) {
1899441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
19003f542974SPaul B Schroeder 		return;
19013f542974SPaul B Schroeder 	}
19023f542974SPaul B Schroeder 
190384fe6e79STony Cook 	dbg("%s", "Entering ..........");
19043f542974SPaul B Schroeder 
19053f542974SPaul B Schroeder 	lData = LCR_BITS_8;
19063f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
19073f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
19083f542974SPaul B Schroeder 
19093f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
19103f542974SPaul B Schroeder 	iflag = tty->termios->c_iflag;
19113f542974SPaul B Schroeder 
19123f542974SPaul B Schroeder 	/* Change the number of bits */
19133f542974SPaul B Schroeder 	if (cflag & CSIZE) {
19143f542974SPaul B Schroeder 		switch (cflag & CSIZE) {
19153f542974SPaul B Schroeder 		case CS5:
19163f542974SPaul B Schroeder 			lData = LCR_BITS_5;
19173f542974SPaul B Schroeder 			break;
19183f542974SPaul B Schroeder 
19193f542974SPaul B Schroeder 		case CS6:
19203f542974SPaul B Schroeder 			lData = LCR_BITS_6;
19213f542974SPaul B Schroeder 			break;
19223f542974SPaul B Schroeder 
19233f542974SPaul B Schroeder 		case CS7:
19243f542974SPaul B Schroeder 			lData = LCR_BITS_7;
19253f542974SPaul B Schroeder 			break;
19263f542974SPaul B Schroeder 		default:
19273f542974SPaul B Schroeder 		case CS8:
19283f542974SPaul B Schroeder 			lData = LCR_BITS_8;
19293f542974SPaul B Schroeder 			break;
19303f542974SPaul B Schroeder 		}
19313f542974SPaul B Schroeder 	}
19323f542974SPaul B Schroeder 	/* Change the Parity bit */
19333f542974SPaul B Schroeder 	if (cflag & PARENB) {
19343f542974SPaul B Schroeder 		if (cflag & PARODD) {
19353f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
1936441b62c1SHarvey Harrison 			dbg("%s - parity = odd", __func__);
19373f542974SPaul B Schroeder 		} else {
19383f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
1939441b62c1SHarvey Harrison 			dbg("%s - parity = even", __func__);
19403f542974SPaul B Schroeder 		}
19413f542974SPaul B Schroeder 
19423f542974SPaul B Schroeder 	} else {
1943441b62c1SHarvey Harrison 		dbg("%s - parity = none", __func__);
19443f542974SPaul B Schroeder 	}
19453f542974SPaul B Schroeder 
1946880af9dbSAlan Cox 	if (cflag & CMSPAR)
19473f542974SPaul B Schroeder 		lParity = lParity | 0x20;
19483f542974SPaul B Schroeder 
19493f542974SPaul B Schroeder 	/* Change the Stop bit */
19503f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
19513f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
1952441b62c1SHarvey Harrison 		dbg("%s - stop bits = 2", __func__);
19533f542974SPaul B Schroeder 	} else {
19543f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
1955441b62c1SHarvey Harrison 		dbg("%s - stop bits = 1", __func__);
19563f542974SPaul B Schroeder 	}
19573f542974SPaul B Schroeder 
19583f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
19593f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
19603f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
19613f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
19623f542974SPaul B Schroeder 
196384fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
19643f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
19653f542974SPaul B Schroeder 	/* Disable Interrupts */
19663f542974SPaul B Schroeder 	Data = 0x00;
19673f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
19683f542974SPaul B Schroeder 
19693f542974SPaul B Schroeder 	Data = 0x00;
19703f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
19713f542974SPaul B Schroeder 
19723f542974SPaul B Schroeder 	Data = 0xcf;
19733f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
19743f542974SPaul B Schroeder 
19753f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
19763f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
19773f542974SPaul B Schroeder 
19783f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19793f542974SPaul B Schroeder 
19803f542974SPaul B Schroeder 	Data = 0x00b;
19813f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
19823f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
19833f542974SPaul B Schroeder 	Data = 0x00b;
19843f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
19853f542974SPaul B Schroeder 
19863f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
19873f542974SPaul B Schroeder 
19883f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
1989880af9dbSAlan Cox 	if (cflag & CBAUD)
19903f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
19913f542974SPaul B Schroeder 
1992880af9dbSAlan Cox 	if (cflag & CRTSCTS)
19933f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
1994880af9dbSAlan Cox 	else
19953f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
19963f542974SPaul B Schroeder 
19973f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
19983f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
19993f542974SPaul B Schroeder 
20003f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
20013f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
20023f542974SPaul B Schroeder 
20033f542974SPaul B Schroeder 	if (!baud) {
20043f542974SPaul B Schroeder 		/* pick a default, any default... */
200584fe6e79STony Cook 		dbg("%s", "Picked default baud...");
20063f542974SPaul B Schroeder 		baud = 9600;
20073f542974SPaul B Schroeder 	}
20083f542974SPaul B Schroeder 
2009441b62c1SHarvey Harrison 	dbg("%s - baud rate = %d", __func__, baud);
20103f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
20113f542974SPaul B Schroeder 
20123f542974SPaul B Schroeder 	/* Enable Interrupts */
20133f542974SPaul B Schroeder 	Data = 0x0c;
20143f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
20153f542974SPaul B Schroeder 
201650de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
20173f542974SPaul B Schroeder 		mos7840_port->read_urb->dev = serial->dev;
201850de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
20193f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
20203f542974SPaul B Schroeder 		if (status) {
20213f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
20223f542974SPaul B Schroeder 			    status);
202350de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
20243f542974SPaul B Schroeder 		}
20253f542974SPaul B Schroeder 	}
20263f542974SPaul B Schroeder 	wake_up(&mos7840_port->delta_msr_wait);
20273f542974SPaul B Schroeder 	mos7840_port->delta_msr_cond = 1;
202884fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
20293f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
20303f542974SPaul B Schroeder 
20313f542974SPaul B Schroeder 	return;
20323f542974SPaul B Schroeder }
20333f542974SPaul B Schroeder 
20343f542974SPaul B Schroeder /*****************************************************************************
20353f542974SPaul B Schroeder  * mos7840_set_termios
20363f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
20373f542974SPaul B Schroeder  *	the termios structure
20383f542974SPaul B Schroeder  *****************************************************************************/
20393f542974SPaul B Schroeder 
204095da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
204195da310eSAlan Cox 				struct usb_serial_port *port,
2042606d099cSAlan Cox 				struct ktermios *old_termios)
20433f542974SPaul B Schroeder {
20443f542974SPaul B Schroeder 	int status;
20453f542974SPaul B Schroeder 	unsigned int cflag;
20463f542974SPaul B Schroeder 	struct usb_serial *serial;
20473f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
204884fe6e79STony Cook 	dbg("mos7840_set_termios: START");
2049441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
205084fe6e79STony Cook 		dbg("%s", "Invalid port");
20513f542974SPaul B Schroeder 		return;
20523f542974SPaul B Schroeder 	}
20533f542974SPaul B Schroeder 
20543f542974SPaul B Schroeder 	serial = port->serial;
20553f542974SPaul B Schroeder 
2056441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
205784fe6e79STony Cook 		dbg("%s", "Invalid Serial");
20583f542974SPaul B Schroeder 		return;
20593f542974SPaul B Schroeder 	}
20603f542974SPaul B Schroeder 
20613f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
20623f542974SPaul B Schroeder 
20633f542974SPaul B Schroeder 	if (mos7840_port == NULL)
20643f542974SPaul B Schroeder 		return;
20653f542974SPaul B Schroeder 
20663f542974SPaul B Schroeder 	if (!mos7840_port->open) {
2067441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
20683f542974SPaul B Schroeder 		return;
20693f542974SPaul B Schroeder 	}
20703f542974SPaul B Schroeder 
207184fe6e79STony Cook 	dbg("%s", "setting termios - ");
20723f542974SPaul B Schroeder 
20733f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
20743f542974SPaul B Schroeder 
2075441b62c1SHarvey Harrison 	dbg("%s - clfag %08x iflag %08x", __func__,
20763f542974SPaul B Schroeder 	    tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2077441b62c1SHarvey Harrison 	dbg("%s - old clfag %08x old iflag %08x", __func__,
20783f542974SPaul B Schroeder 	    old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2079441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
20803f542974SPaul B Schroeder 
20813f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
20823f542974SPaul B Schroeder 
208395da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
20843f542974SPaul B Schroeder 
20853f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
208684fe6e79STony Cook 		dbg("%s", "URB KILLED !!!!!");
20873f542974SPaul B Schroeder 		return;
20883f542974SPaul B Schroeder 	}
20893f542974SPaul B Schroeder 
209050de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
20913f542974SPaul B Schroeder 		mos7840_port->read_urb->dev = serial->dev;
209250de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
20933f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
20943f542974SPaul B Schroeder 		if (status) {
20953f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
20963f542974SPaul B Schroeder 			    status);
209750de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
20983f542974SPaul B Schroeder 		}
20993f542974SPaul B Schroeder 	}
21003f542974SPaul B Schroeder 	return;
21013f542974SPaul B Schroeder }
21023f542974SPaul B Schroeder 
21033f542974SPaul B Schroeder /*****************************************************************************
21043f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
21053f542974SPaul B Schroeder  *
21063f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
21073f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
21083f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
21093f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
21103f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
21113f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
21123f542974SPaul B Schroeder  *****************************************************************************/
21133f542974SPaul B Schroeder 
211495da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
211597c4965dSAl Viro 				unsigned int __user *value)
21163f542974SPaul B Schroeder {
21173f542974SPaul B Schroeder 	int count;
21183f542974SPaul B Schroeder 	unsigned int result = 0;
21193f542974SPaul B Schroeder 
212095da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
21213f542974SPaul B Schroeder 	if (count == 0) {
2122441b62c1SHarvey Harrison 		dbg("%s -- Empty", __func__);
21233f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
21243f542974SPaul B Schroeder 	}
21253f542974SPaul B Schroeder 
21263f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
21273f542974SPaul B Schroeder 		return -EFAULT;
21283f542974SPaul B Schroeder 	return 0;
21293f542974SPaul B Schroeder }
21303f542974SPaul B Schroeder 
21313f542974SPaul B Schroeder /*****************************************************************************
21323f542974SPaul B Schroeder  * mos7840_set_modem_info
21333f542974SPaul B Schroeder  *      function to set modem info
21343f542974SPaul B Schroeder  *****************************************************************************/
21353f542974SPaul B Schroeder 
213695da310eSAlan Cox /* FIXME: Should be using the model control hooks */
213795da310eSAlan Cox 
21383f542974SPaul B Schroeder static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
213997c4965dSAl Viro 				  unsigned int cmd, unsigned int __user *value)
21403f542974SPaul B Schroeder {
21413f542974SPaul B Schroeder 	unsigned int mcr;
21423f542974SPaul B Schroeder 	unsigned int arg;
21433f542974SPaul B Schroeder 	__u16 Data;
21443f542974SPaul B Schroeder 	int status;
21453f542974SPaul B Schroeder 	struct usb_serial_port *port;
21463f542974SPaul B Schroeder 
21473f542974SPaul B Schroeder 	if (mos7840_port == NULL)
21483f542974SPaul B Schroeder 		return -1;
21493f542974SPaul B Schroeder 
21503f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
2151441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
215284fe6e79STony Cook 		dbg("%s", "Invalid port");
21533f542974SPaul B Schroeder 		return -1;
21543f542974SPaul B Schroeder 	}
21553f542974SPaul B Schroeder 
21563f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
21573f542974SPaul B Schroeder 
21583f542974SPaul B Schroeder 	if (copy_from_user(&arg, value, sizeof(int)))
21593f542974SPaul B Schroeder 		return -EFAULT;
21603f542974SPaul B Schroeder 
21613f542974SPaul B Schroeder 	switch (cmd) {
21623f542974SPaul B Schroeder 	case TIOCMBIS:
21633f542974SPaul B Schroeder 		if (arg & TIOCM_RTS)
21643f542974SPaul B Schroeder 			mcr |= MCR_RTS;
21653f542974SPaul B Schroeder 		if (arg & TIOCM_DTR)
21663f542974SPaul B Schroeder 			mcr |= MCR_RTS;
21673f542974SPaul B Schroeder 		if (arg & TIOCM_LOOP)
21683f542974SPaul B Schroeder 			mcr |= MCR_LOOPBACK;
21693f542974SPaul B Schroeder 		break;
21703f542974SPaul B Schroeder 
21713f542974SPaul B Schroeder 	case TIOCMBIC:
21723f542974SPaul B Schroeder 		if (arg & TIOCM_RTS)
21733f542974SPaul B Schroeder 			mcr &= ~MCR_RTS;
21743f542974SPaul B Schroeder 		if (arg & TIOCM_DTR)
21753f542974SPaul B Schroeder 			mcr &= ~MCR_RTS;
21763f542974SPaul B Schroeder 		if (arg & TIOCM_LOOP)
21773f542974SPaul B Schroeder 			mcr &= ~MCR_LOOPBACK;
21783f542974SPaul B Schroeder 		break;
21793f542974SPaul B Schroeder 
21803f542974SPaul B Schroeder 	case TIOCMSET:
21813f542974SPaul B Schroeder 		/* turn off the RTS and DTR and LOOPBACK
21823f542974SPaul B Schroeder 		 * and then only turn on what was asked to */
21833f542974SPaul B Schroeder 		mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
21843f542974SPaul B Schroeder 		mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
21853f542974SPaul B Schroeder 		mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
21863f542974SPaul B Schroeder 		mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
21873f542974SPaul B Schroeder 		break;
21883f542974SPaul B Schroeder 	}
21893f542974SPaul B Schroeder 
21906b447f04SAlan Cox 	lock_kernel();
21913f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
21923f542974SPaul B Schroeder 
21933f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
21943f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
21956b447f04SAlan Cox 	unlock_kernel();
21963f542974SPaul B Schroeder 	if (status < 0) {
219784fe6e79STony Cook 		dbg("setting MODEM_CONTROL_REGISTER Failed");
21983f542974SPaul B Schroeder 		return -1;
21993f542974SPaul B Schroeder 	}
22003f542974SPaul B Schroeder 
22013f542974SPaul B Schroeder 	return 0;
22023f542974SPaul B Schroeder }
22033f542974SPaul B Schroeder 
22043f542974SPaul B Schroeder /*****************************************************************************
22053f542974SPaul B Schroeder  * mos7840_get_modem_info
22063f542974SPaul B Schroeder  *      function to get modem info
22073f542974SPaul B Schroeder  *****************************************************************************/
22083f542974SPaul B Schroeder 
22093f542974SPaul B Schroeder static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
221097c4965dSAl Viro 				  unsigned int __user *value)
22113f542974SPaul B Schroeder {
22123f542974SPaul B Schroeder 	unsigned int result = 0;
22133f542974SPaul B Schroeder 	__u16 msr;
22143f542974SPaul B Schroeder 	unsigned int mcr = mos7840_port->shadowMCR;
2215880af9dbSAlan Cox         mos7840_get_uart_reg(mos7840_port->port,
221695da310eSAlan Cox 						MODEM_STATUS_REGISTER, &msr);
22173f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)	/* 0x002 */
22183f542974SPaul B Schroeder 	    |((mcr & MCR_RTS) ? TIOCM_RTS : 0)	/* 0x004 */
22193f542974SPaul B Schroeder 	    |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)	/* 0x020 */
22203f542974SPaul B Schroeder 	    |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)	/* 0x040 */
22213f542974SPaul B Schroeder 	    |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)	/* 0x080 */
22223f542974SPaul B Schroeder 	    |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);	/* 0x100 */
22233f542974SPaul B Schroeder 
2224441b62c1SHarvey Harrison 	dbg("%s -- %x", __func__, result);
22253f542974SPaul B Schroeder 
22263f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
22273f542974SPaul B Schroeder 		return -EFAULT;
22283f542974SPaul B Schroeder 	return 0;
22293f542974SPaul B Schroeder }
22303f542974SPaul B Schroeder 
22313f542974SPaul B Schroeder /*****************************************************************************
22323f542974SPaul B Schroeder  * mos7840_get_serial_info
22333f542974SPaul B Schroeder  *      function to get information about serial port
22343f542974SPaul B Schroeder  *****************************************************************************/
22353f542974SPaul B Schroeder 
22363f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
223797c4965dSAl Viro 				   struct serial_struct __user *retinfo)
22383f542974SPaul B Schroeder {
22393f542974SPaul B Schroeder 	struct serial_struct tmp;
22403f542974SPaul B Schroeder 
22413f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22423f542974SPaul B Schroeder 		return -1;
22433f542974SPaul B Schroeder 
22443f542974SPaul B Schroeder 	if (!retinfo)
22453f542974SPaul B Schroeder 		return -EFAULT;
22463f542974SPaul B Schroeder 
22473f542974SPaul B Schroeder 	memset(&tmp, 0, sizeof(tmp));
22483f542974SPaul B Schroeder 
22493f542974SPaul B Schroeder 	tmp.type = PORT_16550A;
22503f542974SPaul B Schroeder 	tmp.line = mos7840_port->port->serial->minor;
22513f542974SPaul B Schroeder 	tmp.port = mos7840_port->port->number;
22523f542974SPaul B Schroeder 	tmp.irq = 0;
22533f542974SPaul B Schroeder 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
22543f542974SPaul B Schroeder 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
22553f542974SPaul B Schroeder 	tmp.baud_base = 9600;
22563f542974SPaul B Schroeder 	tmp.close_delay = 5 * HZ;
22573f542974SPaul B Schroeder 	tmp.closing_wait = 30 * HZ;
22583f542974SPaul B Schroeder 
22593f542974SPaul B Schroeder 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
22603f542974SPaul B Schroeder 		return -EFAULT;
22613f542974SPaul B Schroeder 	return 0;
22623f542974SPaul B Schroeder }
22633f542974SPaul B Schroeder 
22643f542974SPaul B Schroeder /*****************************************************************************
22653f542974SPaul B Schroeder  * SerialIoctl
22663f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
22673f542974SPaul B Schroeder  *****************************************************************************/
22683f542974SPaul B Schroeder 
226995da310eSAlan Cox static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
22703f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
22713f542974SPaul B Schroeder {
227295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
227397c4965dSAl Viro 	void __user *argp = (void __user *)arg;
22743f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
22753f542974SPaul B Schroeder 
22763f542974SPaul B Schroeder 	struct async_icount cnow;
22773f542974SPaul B Schroeder 	struct async_icount cprev;
22783f542974SPaul B Schroeder 	struct serial_icounter_struct icount;
22793f542974SPaul B Schroeder 	int mosret = 0;
22803f542974SPaul B Schroeder 
2281441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
228284fe6e79STony Cook 		dbg("%s", "Invalid port");
22833f542974SPaul B Schroeder 		return -1;
22843f542974SPaul B Schroeder 	}
22853f542974SPaul B Schroeder 
22863f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
22873f542974SPaul B Schroeder 
22883f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22893f542974SPaul B Schroeder 		return -1;
22903f542974SPaul B Schroeder 
2291441b62c1SHarvey Harrison 	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
22923f542974SPaul B Schroeder 
22933f542974SPaul B Schroeder 	switch (cmd) {
22943f542974SPaul B Schroeder 		/* return number of bytes available */
22953f542974SPaul B Schroeder 
22963f542974SPaul B Schroeder 	case TIOCSERGETLSR:
2297441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
229895da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
22993f542974SPaul B Schroeder 		return 0;
23003f542974SPaul B Schroeder 
230195da310eSAlan Cox 	/* FIXME: use the modem hooks and remove this */
23023f542974SPaul B Schroeder 	case TIOCMBIS:
23033f542974SPaul B Schroeder 	case TIOCMBIC:
23043f542974SPaul B Schroeder 	case TIOCMSET:
2305441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
23063f542974SPaul B Schroeder 		    port->number);
23073f542974SPaul B Schroeder 		mosret =
230897c4965dSAl Viro 		    mos7840_set_modem_info(mos7840_port, cmd, argp);
23093f542974SPaul B Schroeder 		return mosret;
23103f542974SPaul B Schroeder 
23113f542974SPaul B Schroeder 	case TIOCMGET:
2312441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCMGET", __func__, port->number);
231397c4965dSAl Viro 		return mos7840_get_modem_info(mos7840_port, 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 	case TIOCGICOUNT:
23533f542974SPaul B Schroeder 		cnow = mos7840_port->icount;
23540de9a702SOliver Neukum 		smp_rmb();
23553f542974SPaul B Schroeder 		icount.cts = cnow.cts;
23563f542974SPaul B Schroeder 		icount.dsr = cnow.dsr;
23573f542974SPaul B Schroeder 		icount.rng = cnow.rng;
23583f542974SPaul B Schroeder 		icount.dcd = cnow.dcd;
23593f542974SPaul B Schroeder 		icount.rx = cnow.rx;
23603f542974SPaul B Schroeder 		icount.tx = cnow.tx;
23613f542974SPaul B Schroeder 		icount.frame = cnow.frame;
23623f542974SPaul B Schroeder 		icount.overrun = cnow.overrun;
23633f542974SPaul B Schroeder 		icount.parity = cnow.parity;
23643f542974SPaul B Schroeder 		icount.brk = cnow.brk;
23653f542974SPaul B Schroeder 		icount.buf_overrun = cnow.buf_overrun;
23663f542974SPaul B Schroeder 
2367441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
23683f542974SPaul B Schroeder 		    port->number, icount.rx, icount.tx);
236997c4965dSAl Viro 		if (copy_to_user(argp, &icount, sizeof(icount)))
23703f542974SPaul B Schroeder 			return -EFAULT;
23713f542974SPaul B Schroeder 		return 0;
23723f542974SPaul B Schroeder 	default:
23733f542974SPaul B Schroeder 		break;
23743f542974SPaul B Schroeder 	}
23753f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
23763f542974SPaul B Schroeder }
23773f542974SPaul B Schroeder 
23783f542974SPaul B Schroeder static int mos7840_calc_num_ports(struct usb_serial *serial)
23793f542974SPaul B Schroeder {
23800de9a702SOliver Neukum 	int mos7840_num_ports = 0;
23813f542974SPaul B Schroeder 
238284fe6e79STony Cook 	dbg("numberofendpoints: cur %d, alt %d",
238384fe6e79STony Cook 	    (int)serial->interface->cur_altsetting->desc.bNumEndpoints,
23843f542974SPaul B Schroeder 	    (int)serial->interface->altsetting->desc.bNumEndpoints);
23853f542974SPaul B Schroeder 	if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
23860de9a702SOliver Neukum 		mos7840_num_ports = serial->num_ports = 2;
23873f542974SPaul B Schroeder 	} else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
23880de9a702SOliver Neukum 		serial->num_bulk_in = 4;
23890de9a702SOliver Neukum 		serial->num_bulk_out = 4;
23900de9a702SOliver Neukum 		mos7840_num_ports = serial->num_ports = 4;
23913f542974SPaul B Schroeder 	}
239284fe6e79STony Cook 	dbg ("mos7840_num_ports = %d", mos7840_num_ports);
23933f542974SPaul B Schroeder 	return mos7840_num_ports;
23943f542974SPaul B Schroeder }
23953f542974SPaul B Schroeder 
23963f542974SPaul B Schroeder /****************************************************************************
23973f542974SPaul B Schroeder  * mos7840_startup
23983f542974SPaul B Schroeder  ****************************************************************************/
23993f542974SPaul B Schroeder 
24003f542974SPaul B Schroeder static int mos7840_startup(struct usb_serial *serial)
24013f542974SPaul B Schroeder {
24023f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
24033f542974SPaul B Schroeder 	struct usb_device *dev;
24043f542974SPaul B Schroeder 	int i, status;
24053f542974SPaul B Schroeder 
24063f542974SPaul B Schroeder 	__u16 Data;
240784fe6e79STony Cook 	dbg("%s", "mos7840_startup :Entering..........");
24083f542974SPaul B Schroeder 
24093f542974SPaul B Schroeder 	if (!serial) {
241084fe6e79STony Cook 		dbg("%s", "Invalid Handler");
24113f542974SPaul B Schroeder 		return -1;
24123f542974SPaul B Schroeder 	}
24133f542974SPaul B Schroeder 
24143f542974SPaul B Schroeder 	dev = serial->dev;
24153f542974SPaul B Schroeder 
241684fe6e79STony Cook 	dbg("%s", "Entering...");
241784fe6e79STony Cook 	dbg ("mos7840_startup: serial = %p", serial);
24183f542974SPaul B Schroeder 
24193f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
24203f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
24213f542974SPaul B Schroeder 
24223f542974SPaul B Schroeder 	/* set up port private structures */
24233f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
242484fe6e79STony Cook 		dbg ("mos7840_startup: configuring port %d............", i);
24257ac9da10SBurman Yan 		mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
24263f542974SPaul B Schroeder 		if (mos7840_port == NULL) {
2427194343d9SGreg Kroah-Hartman 			dev_err(&dev->dev, "%s - Out of memory\n", __func__);
24280de9a702SOliver Neukum 			status = -ENOMEM;
24290de9a702SOliver Neukum 			i--; /* don't follow NULL pointer cleaning up */
24300de9a702SOliver Neukum 			goto error;
24313f542974SPaul B Schroeder 		}
24323f542974SPaul B Schroeder 
2433880af9dbSAlan Cox 		/* Initialize all port interrupt end point to port 0 int
2434880af9dbSAlan Cox 		 * endpoint. Our device has only one interrupt end point
2435880af9dbSAlan Cox 		 * common to all port */
24363f542974SPaul B Schroeder 
24373f542974SPaul B Schroeder 		mos7840_port->port = serial->port[i];
24383f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
24390de9a702SOliver Neukum 		spin_lock_init(&mos7840_port->pool_lock);
24403f542974SPaul B Schroeder 
244137768adfSTony Cook 		/* minor is not initialised until later by
244237768adfSTony Cook 		 * usb-serial.c:get_free_serial() and cannot therefore be used
244337768adfSTony Cook 		 * to index device instances */
244437768adfSTony Cook 		mos7840_port->port_num = i + 1;
244537768adfSTony Cook 		dbg ("serial->port[i]->number = %d", serial->port[i]->number);
244637768adfSTony Cook 		dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
244737768adfSTony Cook 		dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
244837768adfSTony Cook 		dbg ("serial->minor = %d", serial->minor);
24493f542974SPaul B Schroeder 
24503f542974SPaul B Schroeder 		if (mos7840_port->port_num == 1) {
24513f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x0;
24523f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x1;
24533f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x4;
24543f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
24550de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
24563f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x8;
24573f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x9;
24583f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x16;
24593f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
24600de9a702SOliver Neukum 			   && (serial->num_ports == 2)) {
24613f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
24623f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
24633f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
24643f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 3)
24650de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
24663f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
24673f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
24683f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
24693f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 4)
24700de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
24713f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xc;
24723f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xd;
24733f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x1c;
24743f542974SPaul B Schroeder 		}
24753f542974SPaul B Schroeder 		mos7840_dump_serial_port(mos7840_port);
24763f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
24773f542974SPaul B Schroeder 
2478880af9dbSAlan Cox 		/* enable rx_disable bit in control register */
2479880af9dbSAlan Cox 		status = mos7840_get_reg_sync(serial->port[i],
24803f542974SPaul B Schroeder 				 mos7840_port->ControlRegOffset, &Data);
24813f542974SPaul B Schroeder 		if (status < 0) {
248284fe6e79STony Cook 			dbg("Reading ControlReg failed status-0x%x", status);
24833f542974SPaul B Schroeder 			break;
24843f542974SPaul B Schroeder 		} else
248584fe6e79STony Cook 			dbg("ControlReg Reading success val is %x, status%d",
24863f542974SPaul B Schroeder 			    Data, status);
2487880af9dbSAlan Cox 		Data |= 0x08;	/* setting driver done bit */
2488880af9dbSAlan Cox 		Data |= 0x04;	/* sp1_bit to have cts change reflect in
2489880af9dbSAlan Cox 				   modem status reg */
24903f542974SPaul B Schroeder 
2491880af9dbSAlan Cox 		/* Data |= 0x20; //rx_disable bit */
2492880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
24933f542974SPaul B Schroeder 					 mos7840_port->ControlRegOffset, Data);
24943f542974SPaul B Schroeder 		if (status < 0) {
249584fe6e79STony Cook 			dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
24963f542974SPaul B Schroeder 			break;
24973f542974SPaul B Schroeder 		} else
249884fe6e79STony Cook 			dbg("ControlReg Writing success(rx_disable) status%d",
24993f542974SPaul B Schroeder 			    status);
25003f542974SPaul B Schroeder 
2501880af9dbSAlan Cox 		/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2502880af9dbSAlan Cox 		   and 0x24 in DCR3 */
25033f542974SPaul B Schroeder 		Data = 0x01;
2504880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2505880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
25063f542974SPaul B Schroeder 		if (status < 0) {
250784fe6e79STony Cook 			dbg("Writing DCR0 failed status-0x%x", status);
25083f542974SPaul B Schroeder 			break;
25093f542974SPaul B Schroeder 		} else
251084fe6e79STony Cook 			dbg("DCR0 Writing success status%d", status);
25113f542974SPaul B Schroeder 
25123f542974SPaul B Schroeder 		Data = 0x05;
2513880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2514880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
25153f542974SPaul B Schroeder 		if (status < 0) {
251684fe6e79STony Cook 			dbg("Writing DCR1 failed status-0x%x", status);
25173f542974SPaul B Schroeder 			break;
25183f542974SPaul B Schroeder 		} else
251984fe6e79STony Cook 			dbg("DCR1 Writing success status%d", status);
25203f542974SPaul B Schroeder 
25213f542974SPaul B Schroeder 		Data = 0x24;
2522880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2523880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
25243f542974SPaul B Schroeder 		if (status < 0) {
252584fe6e79STony Cook 			dbg("Writing DCR2 failed status-0x%x", status);
25263f542974SPaul B Schroeder 			break;
25273f542974SPaul B Schroeder 		} else
252884fe6e79STony Cook 			dbg("DCR2 Writing success status%d", status);
25293f542974SPaul B Schroeder 
2530880af9dbSAlan Cox 		/* write values in clkstart0x0 and clkmulti 0x20 */
25313f542974SPaul B Schroeder 		Data = 0x0;
2532880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
25333f542974SPaul B Schroeder 					 CLK_START_VALUE_REGISTER, Data);
25343f542974SPaul B Schroeder 		if (status < 0) {
253584fe6e79STony Cook 			dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
25363f542974SPaul B Schroeder 			break;
25373f542974SPaul B Schroeder 		} else
253884fe6e79STony Cook 			dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
25393f542974SPaul B Schroeder 
25403f542974SPaul B Schroeder 		Data = 0x20;
2541880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2542880af9dbSAlan Cox 					CLK_MULTI_REGISTER, Data);
25433f542974SPaul B Schroeder 		if (status < 0) {
254484fe6e79STony Cook 			dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
25453f542974SPaul B Schroeder 			    status);
25460de9a702SOliver Neukum 			goto error;
25473f542974SPaul B Schroeder 		} else
254884fe6e79STony Cook 			dbg("CLK_MULTI_REGISTER Writing success status%d",
25493f542974SPaul B Schroeder 			    status);
25503f542974SPaul B Schroeder 
2551880af9dbSAlan Cox 		/* write value 0x0 to scratchpad register */
25523f542974SPaul B Schroeder 		Data = 0x00;
2553880af9dbSAlan Cox 		status = mos7840_set_uart_reg(serial->port[i],
2554880af9dbSAlan Cox 						SCRATCH_PAD_REGISTER, Data);
25553f542974SPaul B Schroeder 		if (status < 0) {
255684fe6e79STony Cook 			dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
25573f542974SPaul B Schroeder 			    status);
25583f542974SPaul B Schroeder 			break;
25593f542974SPaul B Schroeder 		} else
256084fe6e79STony Cook 			dbg("SCRATCH_PAD_REGISTER Writing success status%d",
25613f542974SPaul B Schroeder 			    status);
25623f542974SPaul B Schroeder 
2563880af9dbSAlan Cox 		/* Zero Length flag register */
25643f542974SPaul B Schroeder 		if ((mos7840_port->port_num != 1)
25650de9a702SOliver Neukum 		    && (serial->num_ports == 2)) {
25663f542974SPaul B Schroeder 
25673f542974SPaul B Schroeder 			Data = 0xff;
25683f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
25693f542974SPaul B Schroeder 				      (__u16) (ZLP_REG1 +
2570880af9dbSAlan Cox 				      ((__u16)mos7840_port->port_num)), Data);
257184fe6e79STony Cook 			dbg("ZLIP offset %x",
25723f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
25733f542974SPaul B Schroeder 					((__u16) mos7840_port->port_num)));
25743f542974SPaul B Schroeder 			if (status < 0) {
257584fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
25763f542974SPaul B Schroeder 				    i + 2, status);
25773f542974SPaul B Schroeder 				break;
25783f542974SPaul B Schroeder 			} else
257984fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
25803f542974SPaul B Schroeder 				    i + 2, status);
25813f542974SPaul B Schroeder 		} else {
25823f542974SPaul B Schroeder 			Data = 0xff;
25833f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
25843f542974SPaul B Schroeder 			      (__u16) (ZLP_REG1 +
2585880af9dbSAlan Cox 			      ((__u16)mos7840_port->port_num) - 0x1), Data);
258684fe6e79STony Cook 			dbg("ZLIP offset %x",
25873f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
25883f542974SPaul B Schroeder 				     ((__u16) mos7840_port->port_num) - 0x1));
25893f542974SPaul B Schroeder 			if (status < 0) {
259084fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
25913f542974SPaul B Schroeder 				    i + 1, status);
25923f542974SPaul B Schroeder 				break;
25933f542974SPaul B Schroeder 			} else
259484fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
25953f542974SPaul B Schroeder 				    i + 1, status);
25963f542974SPaul B Schroeder 
25973f542974SPaul B Schroeder 		}
25980de9a702SOliver Neukum 		mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
25993f542974SPaul B Schroeder 		mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2600880af9dbSAlan Cox 		mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2601880af9dbSAlan Cox 								GFP_KERNEL);
2602880af9dbSAlan Cox 		if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2603880af9dbSAlan Cox 							!mos7840_port->dr) {
26040de9a702SOliver Neukum 			status = -ENOMEM;
26050de9a702SOliver Neukum 			goto error;
26060de9a702SOliver Neukum 		}
26073f542974SPaul B Schroeder 	}
260884fe6e79STony Cook 	dbg ("mos7840_startup: all ports configured...........");
26093f542974SPaul B Schroeder 
2610880af9dbSAlan Cox 	/* Zero Length flag enable */
26113f542974SPaul B Schroeder 	Data = 0x0f;
26123f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
26133f542974SPaul B Schroeder 	if (status < 0) {
261484fe6e79STony Cook 		dbg("Writing ZLP_REG5 failed status-0x%x", status);
26157ced46c3SRoel Kluin 		goto error;
26163f542974SPaul B Schroeder 	} else
261784fe6e79STony Cook 		dbg("ZLP_REG5 Writing success status%d", status);
26183f542974SPaul B Schroeder 
26193f542974SPaul B Schroeder 	/* setting configuration feature to one */
26203f542974SPaul B Schroeder 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
262197c4965dSAl Viro 			(__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
26223f542974SPaul B Schroeder 	return 0;
26230de9a702SOliver Neukum error:
26240de9a702SOliver Neukum 	for (/* nothing */; i >= 0; i--) {
26250de9a702SOliver Neukum 		mos7840_port = mos7840_get_port_private(serial->port[i]);
26260de9a702SOliver Neukum 
26270de9a702SOliver Neukum 		kfree(mos7840_port->dr);
26280de9a702SOliver Neukum 		kfree(mos7840_port->ctrl_buf);
26290de9a702SOliver Neukum 		usb_free_urb(mos7840_port->control_urb);
26300de9a702SOliver Neukum 		kfree(mos7840_port);
26310de9a702SOliver Neukum 		serial->port[i] = NULL;
26320de9a702SOliver Neukum 	}
26330de9a702SOliver Neukum 	return status;
26343f542974SPaul B Schroeder }
26353f542974SPaul B Schroeder 
26363f542974SPaul B Schroeder /****************************************************************************
2637f9c99bb8SAlan Stern  * mos7840_disconnect
26383f542974SPaul B Schroeder  *	This function is called whenever the device is removed from the usb bus.
26393f542974SPaul B Schroeder  ****************************************************************************/
26403f542974SPaul B Schroeder 
2641f9c99bb8SAlan Stern static void mos7840_disconnect(struct usb_serial *serial)
26423f542974SPaul B Schroeder {
26433f542974SPaul B Schroeder 	int i;
26440de9a702SOliver Neukum 	unsigned long flags;
26453f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
2646f9c99bb8SAlan Stern 	dbg("%s", " disconnect :entering..........");
26473f542974SPaul B Schroeder 
26483f542974SPaul B Schroeder 	if (!serial) {
264984fe6e79STony Cook 		dbg("%s", "Invalid Handler");
26503f542974SPaul B Schroeder 		return;
26513f542974SPaul B Schroeder 	}
26523f542974SPaul B Schroeder 
26533f542974SPaul B Schroeder 	/* check for the ports to be closed,close the ports and disconnect */
26543f542974SPaul B Schroeder 
26553f542974SPaul B Schroeder 	/* free private structure allocated for serial port  *
26563f542974SPaul B Schroeder 	 * stop reads and writes on all ports                */
26573f542974SPaul B Schroeder 
26583f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
26593f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
266037768adfSTony Cook 		dbg ("mos7840_port %d = %p", i, mos7840_port);
266137768adfSTony Cook 		if (mos7840_port) {
26620de9a702SOliver Neukum 			spin_lock_irqsave(&mos7840_port->pool_lock, flags);
26630de9a702SOliver Neukum 			mos7840_port->zombie = 1;
26640de9a702SOliver Neukum 			spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
26653f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->control_urb);
2666f9c99bb8SAlan Stern 		}
2667f9c99bb8SAlan Stern 	}
2668f9c99bb8SAlan Stern 
2669f9c99bb8SAlan Stern 	dbg("%s", "Thank u :: ");
2670f9c99bb8SAlan Stern 
2671f9c99bb8SAlan Stern }
2672f9c99bb8SAlan Stern 
2673f9c99bb8SAlan Stern /****************************************************************************
2674f9c99bb8SAlan Stern  * mos7840_release
2675f9c99bb8SAlan Stern  *	This function is called when the usb_serial structure is freed.
2676f9c99bb8SAlan Stern  ****************************************************************************/
2677f9c99bb8SAlan Stern 
2678f9c99bb8SAlan Stern static void mos7840_release(struct usb_serial *serial)
2679f9c99bb8SAlan Stern {
2680f9c99bb8SAlan Stern 	int i;
2681f9c99bb8SAlan Stern 	struct moschip_port *mos7840_port;
2682f9c99bb8SAlan Stern 	dbg("%s", " release :entering..........");
2683f9c99bb8SAlan Stern 
2684f9c99bb8SAlan Stern 	if (!serial) {
2685f9c99bb8SAlan Stern 		dbg("%s", "Invalid Handler");
2686f9c99bb8SAlan Stern 		return;
2687f9c99bb8SAlan Stern 	}
2688f9c99bb8SAlan Stern 
2689f9c99bb8SAlan Stern 	/* check for the ports to be closed,close the ports and disconnect */
2690f9c99bb8SAlan Stern 
2691f9c99bb8SAlan Stern 	/* free private structure allocated for serial port  *
2692f9c99bb8SAlan Stern 	 * stop reads and writes on all ports                */
2693f9c99bb8SAlan Stern 
2694f9c99bb8SAlan Stern 	for (i = 0; i < serial->num_ports; ++i) {
2695f9c99bb8SAlan Stern 		mos7840_port = mos7840_get_port_private(serial->port[i]);
2696f9c99bb8SAlan Stern 		dbg("mos7840_port %d = %p", i, mos7840_port);
2697f9c99bb8SAlan Stern 		if (mos7840_port) {
26980de9a702SOliver Neukum 			kfree(mos7840_port->ctrl_buf);
26990de9a702SOliver Neukum 			kfree(mos7840_port->dr);
27003f542974SPaul B Schroeder 			kfree(mos7840_port);
270137768adfSTony Cook 		}
27023f542974SPaul B Schroeder 	}
27033f542974SPaul B Schroeder 
270484fe6e79STony Cook 	dbg("%s", "Thank u :: ");
27053f542974SPaul B Schroeder 
27063f542974SPaul B Schroeder }
27073f542974SPaul B Schroeder 
2708d9b1b787SJohannes Hölzl static struct usb_driver io_driver = {
2709d9b1b787SJohannes Hölzl 	.name = "mos7840",
2710d9b1b787SJohannes Hölzl 	.probe = usb_serial_probe,
2711d9b1b787SJohannes Hölzl 	.disconnect = usb_serial_disconnect,
2712d9b1b787SJohannes Hölzl 	.id_table = moschip_id_table_combined,
2713d9b1b787SJohannes Hölzl 	.no_dynamic_id = 1,
2714d9b1b787SJohannes Hölzl };
2715d9b1b787SJohannes Hölzl 
27163f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
27173f542974SPaul B Schroeder 	.driver = {
27183f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
27193f542974SPaul B Schroeder 		   .name = "mos7840",
27203f542974SPaul B Schroeder 		   },
27213f542974SPaul B Schroeder 	.description = DRIVER_DESC,
2722d9b1b787SJohannes Hölzl 	.usb_driver = &io_driver,
27233f542974SPaul B Schroeder 	.id_table = moschip_port_id_table,
27243f542974SPaul B Schroeder 	.num_ports = 4,
27253f542974SPaul B Schroeder 	.open = mos7840_open,
27263f542974SPaul B Schroeder 	.close = mos7840_close,
27273f542974SPaul B Schroeder 	.write = mos7840_write,
27283f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
27293f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
27303f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
27313f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
27323f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
27333f542974SPaul B Schroeder #ifdef MCSSerialProbe
27343f542974SPaul B Schroeder 	.probe = mos7840_serial_probe,
27353f542974SPaul B Schroeder #endif
27363f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
27373f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
27383f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
27393f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
27403f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
27413f542974SPaul B Schroeder 	.attach = mos7840_startup,
2742f9c99bb8SAlan Stern 	.disconnect = mos7840_disconnect,
2743f9c99bb8SAlan Stern 	.release = mos7840_release,
27443f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
27453f542974SPaul B Schroeder 	.read_int_callback = mos7840_interrupt_callback,
27463f542974SPaul B Schroeder };
27473f542974SPaul B Schroeder 
27483f542974SPaul B Schroeder /****************************************************************************
27493f542974SPaul B Schroeder  * moschip7840_init
27503f542974SPaul B Schroeder  *	This is called by the module subsystem, or on startup to initialize us
27513f542974SPaul B Schroeder  ****************************************************************************/
27523f542974SPaul B Schroeder static int __init moschip7840_init(void)
27533f542974SPaul B Schroeder {
27543f542974SPaul B Schroeder 	int retval;
27553f542974SPaul B Schroeder 
275684fe6e79STony Cook 	dbg("%s", " mos7840_init :entering..........");
27573f542974SPaul B Schroeder 
27583f542974SPaul B Schroeder 	/* Register with the usb serial */
27593f542974SPaul B Schroeder 	retval = usb_serial_register(&moschip7840_4port_device);
27603f542974SPaul B Schroeder 
27613f542974SPaul B Schroeder 	if (retval)
27623f542974SPaul B Schroeder 		goto failed_port_device_register;
27633f542974SPaul B Schroeder 
276484fe6e79STony Cook 	dbg("%s", "Entering...");
2765c197a8dbSGreg Kroah-Hartman 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2766c197a8dbSGreg Kroah-Hartman 	       DRIVER_DESC "\n");
27673f542974SPaul B Schroeder 
27683f542974SPaul B Schroeder 	/* Register with the usb */
27693f542974SPaul B Schroeder 	retval = usb_register(&io_driver);
27703f542974SPaul B Schroeder 	if (retval == 0) {
277184fe6e79STony Cook 		dbg("%s", "Leaving...");
27723f542974SPaul B Schroeder 		return 0;
27733f542974SPaul B Schroeder 	}
27743f542974SPaul B Schroeder 	usb_serial_deregister(&moschip7840_4port_device);
27753f542974SPaul B Schroeder failed_port_device_register:
27763f542974SPaul B Schroeder 	return retval;
27773f542974SPaul B Schroeder }
27783f542974SPaul B Schroeder 
27793f542974SPaul B Schroeder /****************************************************************************
27803f542974SPaul B Schroeder  * moschip7840_exit
27813f542974SPaul B Schroeder  *	Called when the driver is about to be unloaded.
27823f542974SPaul B Schroeder  ****************************************************************************/
27833f542974SPaul B Schroeder static void __exit moschip7840_exit(void)
27843f542974SPaul B Schroeder {
27853f542974SPaul B Schroeder 
278684fe6e79STony Cook 	dbg("%s", " mos7840_exit :entering..........");
27873f542974SPaul B Schroeder 
27883f542974SPaul B Schroeder 	usb_deregister(&io_driver);
27893f542974SPaul B Schroeder 
27903f542974SPaul B Schroeder 	usb_serial_deregister(&moschip7840_4port_device);
27913f542974SPaul B Schroeder 
279284fe6e79STony Cook 	dbg("%s", "Entering...");
27933f542974SPaul B Schroeder }
27943f542974SPaul B Schroeder 
27953f542974SPaul B Schroeder module_init(moschip7840_init);
27963f542974SPaul B Schroeder module_exit(moschip7840_exit);
27973f542974SPaul B Schroeder 
27983f542974SPaul B Schroeder /* Module information */
27993f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
28003f542974SPaul B Schroeder MODULE_LICENSE("GPL");
28013f542974SPaul B Schroeder 
28023f542974SPaul B Schroeder module_param(debug, bool, S_IRUGO | S_IWUSR);
28033f542974SPaul B Schroeder MODULE_PARM_DESC(debug, "Debug enabled or not");
2804