xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision 11e1abb453690a907ea26c07043987165a4f277f)
13f542974SPaul B Schroeder /*
23f542974SPaul B Schroeder  * This program is free software; you can redistribute it and/or modify
33f542974SPaul B Schroeder  * it under the terms of the GNU General Public License as published by
43f542974SPaul B Schroeder  * the Free Software Foundation; either version 2 of the License, or
53f542974SPaul B Schroeder  * (at your option) any later version.
63f542974SPaul B Schroeder  *
73f542974SPaul B Schroeder  * This program is distributed in the hope that it will be useful,
83f542974SPaul B Schroeder  * but WITHOUT ANY WARRANTY; without even the implied warranty of
93f542974SPaul B Schroeder  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
103f542974SPaul B Schroeder  * GNU General Public License for more details.
113f542974SPaul B Schroeder  *
123f542974SPaul B Schroeder  * You should have received a copy of the GNU General Public License
133f542974SPaul B Schroeder  * along with this program; if not, write to the Free Software
143f542974SPaul B Schroeder  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
153f542974SPaul B Schroeder  *
163f542974SPaul B Schroeder  * Clean ups from Moschip version and a few ioctl implementations by:
173f542974SPaul B Schroeder  *	Paul B Schroeder <pschroeder "at" uplogix "dot" com>
183f542974SPaul B Schroeder  *
193f542974SPaul B Schroeder  * Originally based on drivers/usb/serial/io_edgeport.c which is:
203f542974SPaul B Schroeder  *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
213f542974SPaul B Schroeder  *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
223f542974SPaul B Schroeder  *
233f542974SPaul B Schroeder  */
243f542974SPaul B Schroeder 
253f542974SPaul B Schroeder #include <linux/kernel.h>
263f542974SPaul B Schroeder #include <linux/errno.h>
273f542974SPaul B Schroeder #include <linux/init.h>
283f542974SPaul B Schroeder #include <linux/slab.h>
293f542974SPaul B Schroeder #include <linux/tty.h>
303f542974SPaul B Schroeder #include <linux/tty_driver.h>
313f542974SPaul B Schroeder #include <linux/tty_flip.h>
323f542974SPaul B Schroeder #include <linux/module.h>
333f542974SPaul B Schroeder #include <linux/serial.h>
343f542974SPaul B Schroeder #include <linux/usb.h>
353f542974SPaul B Schroeder #include <linux/usb/serial.h>
363f542974SPaul B Schroeder #include <asm/uaccess.h>
373f542974SPaul B Schroeder 
383f542974SPaul B Schroeder /*
393f542974SPaul B Schroeder  * Version Information
403f542974SPaul B Schroeder  */
413f542974SPaul B Schroeder #define DRIVER_VERSION "1.3.1"
423f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
433f542974SPaul B Schroeder 
443f542974SPaul B Schroeder /*
453f542974SPaul B Schroeder  * 16C50 UART register defines
463f542974SPaul B Schroeder  */
473f542974SPaul B Schroeder 
483f542974SPaul B Schroeder #define LCR_BITS_5             0x00	/* 5 bits/char */
493f542974SPaul B Schroeder #define LCR_BITS_6             0x01	/* 6 bits/char */
503f542974SPaul B Schroeder #define LCR_BITS_7             0x02	/* 7 bits/char */
513f542974SPaul B Schroeder #define LCR_BITS_8             0x03	/* 8 bits/char */
523f542974SPaul B Schroeder #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
533f542974SPaul B Schroeder 
543f542974SPaul B Schroeder #define LCR_STOP_1             0x00	/* 1 stop bit */
553f542974SPaul B Schroeder #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
563f542974SPaul B Schroeder #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
573f542974SPaul B Schroeder #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
583f542974SPaul B Schroeder 
593f542974SPaul B Schroeder #define LCR_PAR_NONE           0x00	/* No parity */
603f542974SPaul B Schroeder #define LCR_PAR_ODD            0x08	/* Odd parity */
613f542974SPaul B Schroeder #define LCR_PAR_EVEN           0x18	/* Even parity */
623f542974SPaul B Schroeder #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
633f542974SPaul B Schroeder #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
643f542974SPaul B Schroeder #define LCR_PAR_MASK           0x38	/* Mask for parity field */
653f542974SPaul B Schroeder 
663f542974SPaul B Schroeder #define LCR_SET_BREAK          0x40	/* Set Break condition */
673f542974SPaul B Schroeder #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
683f542974SPaul B Schroeder 
693f542974SPaul B Schroeder #define MCR_DTR                0x01	/* Assert DTR */
703f542974SPaul B Schroeder #define MCR_RTS                0x02	/* Assert RTS */
713f542974SPaul B Schroeder #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
723f542974SPaul B Schroeder #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
733f542974SPaul B Schroeder #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
743f542974SPaul B Schroeder #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
753f542974SPaul B Schroeder 
763f542974SPaul B Schroeder #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
773f542974SPaul B Schroeder #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
783f542974SPaul B Schroeder #define MOS7840_MSR_RI         0x40	/* Current state of RI */
793f542974SPaul B Schroeder #define MOS7840_MSR_CD         0x80	/* Current state of CD */
803f542974SPaul B Schroeder 
813f542974SPaul B Schroeder /*
823f542974SPaul B Schroeder  * Defines used for sending commands to port
833f542974SPaul B Schroeder  */
843f542974SPaul B Schroeder 
853f542974SPaul B Schroeder #define WAIT_FOR_EVER   (HZ * 0 )	/* timeout urb is wait for ever */
863f542974SPaul B Schroeder #define MOS_WDR_TIMEOUT (HZ * 5 )	/* default urb timeout */
873f542974SPaul B Schroeder 
883f542974SPaul B Schroeder #define MOS_PORT1       0x0200
893f542974SPaul B Schroeder #define MOS_PORT2       0x0300
903f542974SPaul B Schroeder #define MOS_VENREG      0x0000
913f542974SPaul B Schroeder #define MOS_MAX_PORT	0x02
923f542974SPaul B Schroeder #define MOS_WRITE       0x0E
933f542974SPaul B Schroeder #define MOS_READ        0x0D
943f542974SPaul B Schroeder 
953f542974SPaul B Schroeder /* Requests */
963f542974SPaul B Schroeder #define MCS_RD_RTYPE    0xC0
973f542974SPaul B Schroeder #define MCS_WR_RTYPE    0x40
983f542974SPaul B Schroeder #define MCS_RDREQ       0x0D
993f542974SPaul B Schroeder #define MCS_WRREQ       0x0E
1003f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT        500
1013f542974SPaul B Schroeder #define VENDOR_READ_LENGTH      (0x01)
1023f542974SPaul B Schroeder 
1033f542974SPaul B Schroeder #define MAX_NAME_LEN    64
1043f542974SPaul B Schroeder 
1053f542974SPaul B Schroeder #define ZLP_REG1  0x3A		//Zero_Flag_Reg1    58
1063f542974SPaul B Schroeder #define ZLP_REG5  0x3E		//Zero_Flag_Reg5    62
1073f542974SPaul B Schroeder 
1083f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */
1093f542974SPaul B Schroeder #define TIOCEXBAUD     0x5462
1103f542974SPaul B Schroeder 
1113f542974SPaul B Schroeder /* vendor id and device id defines */
1123f542974SPaul B Schroeder 
113*11e1abb4SDavid Ludlow /* The native mos7840/7820 component */
1143f542974SPaul B Schroeder #define USB_VENDOR_ID_MOSCHIP           0x9710
1153f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7840          0x7840
1163f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7820          0x7820
117*11e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden
118*11e1abb4SDavid Ludlow  * in vendor-specific implementations.  Such devices can be handled
119*11e1abb4SDavid Ludlow  * by making a change here, in moschip_port_id_table, and in
120*11e1abb4SDavid Ludlow  * moschip_id_table_combined
121*11e1abb4SDavid Ludlow  */
122*11e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB             0x0856
123*11e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_4       0xAC44
124*11e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2       0xAC42
1253f542974SPaul B Schroeder 
126*11e1abb4SDavid Ludlow /* Interrupt Routine Defines    */
1273f542974SPaul B Schroeder 
1283f542974SPaul B Schroeder #define SERIAL_IIR_RLS      0x06
1293f542974SPaul B Schroeder #define SERIAL_IIR_MS       0x00
1303f542974SPaul B Schroeder 
1313f542974SPaul B Schroeder /*
1323f542974SPaul B Schroeder  *  Emulation of the bit mask on the LINE STATUS REGISTER.
1333f542974SPaul B Schroeder  */
1343f542974SPaul B Schroeder #define SERIAL_LSR_DR       0x0001
1353f542974SPaul B Schroeder #define SERIAL_LSR_OE       0x0002
1363f542974SPaul B Schroeder #define SERIAL_LSR_PE       0x0004
1373f542974SPaul B Schroeder #define SERIAL_LSR_FE       0x0008
1383f542974SPaul B Schroeder #define SERIAL_LSR_BI       0x0010
1393f542974SPaul B Schroeder 
1403f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS   0x10
1413f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR   0x20
1423f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI    0x40
1433f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD    0x80
1443f542974SPaul B Schroeder 
1453f542974SPaul B Schroeder // Serial Port register Address
1463f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
1473f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
1483f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
1493f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
1503f542974SPaul B Schroeder #define LINE_STATUS_REGISTER       ((__u16)(0x05))
1513f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
1523f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
1533f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
1543f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
1553f542974SPaul B Schroeder 
1563f542974SPaul B Schroeder #define CLK_MULTI_REGISTER         ((__u16)(0x02))
1573f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
1583f542974SPaul B Schroeder 
1593f542974SPaul B Schroeder #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
1603f542974SPaul B Schroeder 
1613f542974SPaul B Schroeder /*
1623f542974SPaul B Schroeder  * URB POOL related defines
1633f542974SPaul B Schroeder  */
1643f542974SPaul B Schroeder #define NUM_URBS                        16	/* URB Count */
1653f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
1663f542974SPaul B Schroeder 
1673f542974SPaul B Schroeder 
1683f542974SPaul B Schroeder static struct usb_device_id moschip_port_id_table[] = {
1693f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
1703f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
171*11e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
172*11e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
1733f542974SPaul B Schroeder 	{}			/* terminating entry */
1743f542974SPaul B Schroeder };
1753f542974SPaul B Schroeder 
1763f542974SPaul B Schroeder static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
1773f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
1783f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
179*11e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
180*11e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
1813f542974SPaul B Schroeder 	{}			/* terminating entry */
1823f542974SPaul B Schroeder };
1833f542974SPaul B Schroeder 
1843f542974SPaul B Schroeder MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
1853f542974SPaul B Schroeder 
1863f542974SPaul B Schroeder /* This structure holds all of the local port information */
1873f542974SPaul B Schroeder 
1883f542974SPaul B Schroeder struct moschip_port {
1893f542974SPaul B Schroeder 	int port_num;		/*Actual port number in the device(1,2,etc) */
1903f542974SPaul B Schroeder 	struct urb *write_urb;	/* write URB for this port */
1913f542974SPaul B Schroeder 	struct urb *read_urb;	/* read URB for this port */
1920de9a702SOliver Neukum 	struct urb *int_urb;
1933f542974SPaul B Schroeder 	__u8 shadowLCR;		/* last LCR value received */
1943f542974SPaul B Schroeder 	__u8 shadowMCR;		/* last MCR value received */
1953f542974SPaul B Schroeder 	char open;
1960de9a702SOliver Neukum 	char open_ports;
1970de9a702SOliver Neukum 	char zombie;
1983f542974SPaul B Schroeder 	wait_queue_head_t wait_chase;	/* for handling sleeping while waiting for chase to finish */
1993f542974SPaul B Schroeder 	wait_queue_head_t delta_msr_wait;	/* for handling sleeping while waiting for msr change to happen */
2003f542974SPaul B Schroeder 	int delta_msr_cond;
2013f542974SPaul B Schroeder 	struct async_icount icount;
2023f542974SPaul B Schroeder 	struct usb_serial_port *port;	/* loop back to the owner of this object */
2033f542974SPaul B Schroeder 
2043f542974SPaul B Schroeder 	/*Offsets */
2053f542974SPaul B Schroeder 	__u8 SpRegOffset;
2063f542974SPaul B Schroeder 	__u8 ControlRegOffset;
2073f542974SPaul B Schroeder 	__u8 DcrRegOffset;
2083f542974SPaul B Schroeder 	//for processing control URBS in interrupt context
2093f542974SPaul B Schroeder 	struct urb *control_urb;
2100de9a702SOliver Neukum 	struct usb_ctrlrequest *dr;
2113f542974SPaul B Schroeder 	char *ctrl_buf;
2123f542974SPaul B Schroeder 	int MsrLsr;
2133f542974SPaul B Schroeder 
2140de9a702SOliver Neukum 	spinlock_t pool_lock;
2153f542974SPaul B Schroeder 	struct urb *write_urb_pool[NUM_URBS];
2160de9a702SOliver Neukum 	char busy[NUM_URBS];
2173f542974SPaul B Schroeder };
2183f542974SPaul B Schroeder 
2193f542974SPaul B Schroeder 
2203f542974SPaul B Schroeder static int debug;
2213f542974SPaul B Schroeder 
2223f542974SPaul B Schroeder /*
2233f542974SPaul B Schroeder  * mos7840_set_reg_sync
2243f542974SPaul B Schroeder  * 	To set the Control register by calling usb_fill_control_urb function
2253f542974SPaul B Schroeder  *	by passing usb_sndctrlpipe function as parameter.
2263f542974SPaul B Schroeder  */
2273f542974SPaul B Schroeder 
2283f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2293f542974SPaul B Schroeder 				__u16 val)
2303f542974SPaul B Schroeder {
2313f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2323f542974SPaul B Schroeder 	val = val & 0x00ff;
2333f542974SPaul B Schroeder 	dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
2343f542974SPaul B Schroeder 
2353f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2363f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2373f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2383f542974SPaul B Schroeder }
2393f542974SPaul B Schroeder 
2403f542974SPaul B Schroeder /*
2413f542974SPaul B Schroeder  * mos7840_get_reg_sync
2423f542974SPaul B Schroeder  * 	To set the Uart register by calling usb_fill_control_urb function by
2433f542974SPaul B Schroeder  *	passing usb_rcvctrlpipe function as parameter.
2443f542974SPaul B Schroeder  */
2453f542974SPaul B Schroeder 
2463f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
2473f542974SPaul B Schroeder 				__u16 * val)
2483f542974SPaul B Schroeder {
2493f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2503f542974SPaul B Schroeder 	int ret = 0;
2513f542974SPaul B Schroeder 
2523f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
2533f542974SPaul B Schroeder 			      MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
2543f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
2553f542974SPaul B Schroeder 	dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
2563f542974SPaul B Schroeder 	*val = (*val) & 0x00ff;
2573f542974SPaul B Schroeder 	return ret;
2583f542974SPaul B Schroeder }
2593f542974SPaul B Schroeder 
2603f542974SPaul B Schroeder /*
2613f542974SPaul B Schroeder  * mos7840_set_uart_reg
2623f542974SPaul B Schroeder  *	To set the Uart register by calling usb_fill_control_urb function by
2633f542974SPaul B Schroeder  *	passing usb_sndctrlpipe function as parameter.
2643f542974SPaul B Schroeder  */
2653f542974SPaul B Schroeder 
2663f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
2673f542974SPaul B Schroeder 				__u16 val)
2683f542974SPaul B Schroeder {
2693f542974SPaul B Schroeder 
2703f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2713f542974SPaul B Schroeder 	val = val & 0x00ff;
2723f542974SPaul B Schroeder 	// For the UART control registers, the application number need to be Or'ed
2730de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
2743f542974SPaul B Schroeder 		val |=
2753f542974SPaul B Schroeder 		    (((__u16) port->number - (__u16) (port->serial->minor)) +
2763f542974SPaul B Schroeder 		     1) << 8;
2773f542974SPaul B Schroeder 		dbg("mos7840_set_uart_reg application number is %x\n", val);
2783f542974SPaul B Schroeder 	} else {
2793f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
2803f542974SPaul B Schroeder 			val |=
2813f542974SPaul B Schroeder 			    (((__u16) port->number -
2823f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
2833f542974SPaul B Schroeder 			dbg("mos7840_set_uart_reg application number is %x\n",
2843f542974SPaul B Schroeder 			    val);
2853f542974SPaul B Schroeder 		} else {
2863f542974SPaul B Schroeder 			val |=
2873f542974SPaul B Schroeder 			    (((__u16) port->number -
2883f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
2893f542974SPaul B Schroeder 			dbg("mos7840_set_uart_reg application number is %x\n",
2903f542974SPaul B Schroeder 			    val);
2913f542974SPaul B Schroeder 		}
2923f542974SPaul B Schroeder 	}
2933f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2943f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2953f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2963f542974SPaul B Schroeder 
2973f542974SPaul B Schroeder }
2983f542974SPaul B Schroeder 
2993f542974SPaul B Schroeder /*
3003f542974SPaul B Schroeder  * mos7840_get_uart_reg
3013f542974SPaul B Schroeder  *	To set the Control register by calling usb_fill_control_urb function
3023f542974SPaul B Schroeder  *	by passing usb_rcvctrlpipe function as parameter.
3033f542974SPaul B Schroeder  */
3043f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3053f542974SPaul B Schroeder 				__u16 * val)
3063f542974SPaul B Schroeder {
3073f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3083f542974SPaul B Schroeder 	int ret = 0;
3093f542974SPaul B Schroeder 	__u16 Wval;
3103f542974SPaul B Schroeder 
3113f542974SPaul B Schroeder 	//dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
3123f542974SPaul B Schroeder 	/*Wval  is same as application number */
3130de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3143f542974SPaul B Schroeder 		Wval =
3153f542974SPaul B Schroeder 		    (((__u16) port->number - (__u16) (port->serial->minor)) +
3163f542974SPaul B Schroeder 		     1) << 8;
3173f542974SPaul B Schroeder 		dbg("mos7840_get_uart_reg application number is %x\n", Wval);
3183f542974SPaul B Schroeder 	} else {
3193f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
3203f542974SPaul B Schroeder 			Wval =
3213f542974SPaul B Schroeder 			    (((__u16) port->number -
3223f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
3233f542974SPaul B Schroeder 			dbg("mos7840_get_uart_reg application number is %x\n",
3243f542974SPaul B Schroeder 			    Wval);
3253f542974SPaul B Schroeder 		} else {
3263f542974SPaul B Schroeder 			Wval =
3273f542974SPaul B Schroeder 			    (((__u16) port->number -
3283f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
3293f542974SPaul B Schroeder 			dbg("mos7840_get_uart_reg application number is %x\n",
3303f542974SPaul B Schroeder 			    Wval);
3313f542974SPaul B Schroeder 		}
3323f542974SPaul B Schroeder 	}
3333f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3343f542974SPaul B Schroeder 			      MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
3353f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
3363f542974SPaul B Schroeder 	*val = (*val) & 0x00ff;
3373f542974SPaul B Schroeder 	return ret;
3383f542974SPaul B Schroeder }
3393f542974SPaul B Schroeder 
3403f542974SPaul B Schroeder static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
3413f542974SPaul B Schroeder {
3423f542974SPaul B Schroeder 
3433f542974SPaul B Schroeder 	dbg("***************************************\n");
3443f542974SPaul B Schroeder 	dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
3453f542974SPaul B Schroeder 	dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
3463f542974SPaul B Schroeder 	dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
3473f542974SPaul B Schroeder 	dbg("***************************************\n");
3483f542974SPaul B Schroeder 
3493f542974SPaul B Schroeder }
3503f542974SPaul B Schroeder 
3513f542974SPaul B Schroeder /************************************************************************/
3523f542974SPaul B Schroeder /************************************************************************/
3533f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3543f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3553f542974SPaul B Schroeder /************************************************************************/
3563f542974SPaul B Schroeder /************************************************************************/
3573f542974SPaul B Schroeder 
3583f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port,
3593f542974SPaul B Schroeder 					    struct moschip_port *data)
3603f542974SPaul B Schroeder {
3613f542974SPaul B Schroeder 	usb_set_serial_port_data(port, (void *)data);
3623f542974SPaul B Schroeder }
3633f542974SPaul B Schroeder 
3643f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct
3653f542974SPaul B Schroeder 							    usb_serial_port
3663f542974SPaul B Schroeder 							    *port)
3673f542974SPaul B Schroeder {
3683f542974SPaul B Schroeder 	return (struct moschip_port *)usb_get_serial_port_data(port);
3693f542974SPaul B Schroeder }
3703f542974SPaul B Schroeder 
3710de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
3723f542974SPaul B Schroeder {
3733f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
3743f542974SPaul B Schroeder 	struct async_icount *icount;
3753f542974SPaul B Schroeder 	mos7840_port = port;
3763f542974SPaul B Schroeder 	icount = &mos7840_port->icount;
3773f542974SPaul B Schroeder 	if (new_msr &
3783f542974SPaul B Schroeder 	    (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
3793f542974SPaul B Schroeder 	     MOS_MSR_DELTA_CD)) {
3803f542974SPaul B Schroeder 		icount = &mos7840_port->icount;
3813f542974SPaul B Schroeder 
3823f542974SPaul B Schroeder 		/* update input line counters */
3833f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CTS) {
3843f542974SPaul B Schroeder 			icount->cts++;
3850de9a702SOliver Neukum 			smp_wmb();
3863f542974SPaul B Schroeder 		}
3873f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_DSR) {
3883f542974SPaul B Schroeder 			icount->dsr++;
3890de9a702SOliver Neukum 			smp_wmb();
3903f542974SPaul B Schroeder 		}
3913f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CD) {
3923f542974SPaul B Schroeder 			icount->dcd++;
3930de9a702SOliver Neukum 			smp_wmb();
3943f542974SPaul B Schroeder 		}
3953f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_RI) {
3963f542974SPaul B Schroeder 			icount->rng++;
3970de9a702SOliver Neukum 			smp_wmb();
3980de9a702SOliver Neukum 		}
3993f542974SPaul B Schroeder 	}
4003f542974SPaul B Schroeder }
4013f542974SPaul B Schroeder 
4020de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
4033f542974SPaul B Schroeder {
4043f542974SPaul B Schroeder 	struct async_icount *icount;
4053f542974SPaul B Schroeder 
4063f542974SPaul B Schroeder 	dbg("%s - %02x", __FUNCTION__, new_lsr);
4073f542974SPaul B Schroeder 
4083f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
4093f542974SPaul B Schroeder 		//
4103f542974SPaul B Schroeder 		// Parity and Framing errors only count if they
4113f542974SPaul B Schroeder 		// occur exclusive of a break being
4123f542974SPaul B Schroeder 		// received.
4133f542974SPaul B Schroeder 		//
4143f542974SPaul B Schroeder 		new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
4153f542974SPaul B Schroeder 	}
4163f542974SPaul B Schroeder 
4173f542974SPaul B Schroeder 	/* update input line counters */
4183f542974SPaul B Schroeder 	icount = &port->icount;
4193f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
4203f542974SPaul B Schroeder 		icount->brk++;
4210de9a702SOliver Neukum 		smp_wmb();
4223f542974SPaul B Schroeder 	}
4233f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_OE) {
4243f542974SPaul B Schroeder 		icount->overrun++;
4250de9a702SOliver Neukum 		smp_wmb();
4263f542974SPaul B Schroeder 	}
4273f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_PE) {
4283f542974SPaul B Schroeder 		icount->parity++;
4290de9a702SOliver Neukum 		smp_wmb();
4303f542974SPaul B Schroeder 	}
4313f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_FE) {
4323f542974SPaul B Schroeder 		icount->frame++;
4330de9a702SOliver Neukum 		smp_wmb();
4343f542974SPaul B Schroeder 	}
4353f542974SPaul B Schroeder }
4363f542974SPaul B Schroeder 
4373f542974SPaul B Schroeder /************************************************************************/
4383f542974SPaul B Schroeder /************************************************************************/
4393f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4403f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4413f542974SPaul B Schroeder /************************************************************************/
4423f542974SPaul B Schroeder /************************************************************************/
4433f542974SPaul B Schroeder 
4447d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb)
4453f542974SPaul B Schroeder {
4463f542974SPaul B Schroeder 	unsigned char *data;
4473f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4483f542974SPaul B Schroeder 	__u8 regval = 0x0;
4490de9a702SOliver Neukum 	int result = 0;
4500643c724SGreg Kroah-Hartman 	int status = urb->status;
4513f542974SPaul B Schroeder 
4520de9a702SOliver Neukum 	mos7840_port = (struct moschip_port *)urb->context;
4530de9a702SOliver Neukum 
4540643c724SGreg Kroah-Hartman 	switch (status) {
4553f542974SPaul B Schroeder 	case 0:
4563f542974SPaul B Schroeder 		/* success */
4573f542974SPaul B Schroeder 		break;
4583f542974SPaul B Schroeder 	case -ECONNRESET:
4593f542974SPaul B Schroeder 	case -ENOENT:
4603f542974SPaul B Schroeder 	case -ESHUTDOWN:
4613f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
4623f542974SPaul B Schroeder 		dbg("%s - urb shutting down with status: %d", __FUNCTION__,
4630643c724SGreg Kroah-Hartman 		    status);
4643f542974SPaul B Schroeder 		return;
4653f542974SPaul B Schroeder 	default:
4663f542974SPaul B Schroeder 		dbg("%s - nonzero urb status received: %d", __FUNCTION__,
4670643c724SGreg Kroah-Hartman 		    status);
4683f542974SPaul B Schroeder 		goto exit;
4693f542974SPaul B Schroeder 	}
4703f542974SPaul B Schroeder 
4713f542974SPaul B Schroeder 	dbg("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
4723f542974SPaul B Schroeder 	dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__,
4733f542974SPaul B Schroeder 	    mos7840_port->MsrLsr, mos7840_port->port_num);
4743f542974SPaul B Schroeder 	data = urb->transfer_buffer;
4753f542974SPaul B Schroeder 	regval = (__u8) data[0];
4763f542974SPaul B Schroeder 	dbg("%s data is %x\n", __FUNCTION__, regval);
4773f542974SPaul B Schroeder 	if (mos7840_port->MsrLsr == 0)
4783f542974SPaul B Schroeder 		mos7840_handle_new_msr(mos7840_port, regval);
4793f542974SPaul B Schroeder 	else if (mos7840_port->MsrLsr == 1)
4803f542974SPaul B Schroeder 		mos7840_handle_new_lsr(mos7840_port, regval);
4813f542974SPaul B Schroeder 
4823f542974SPaul B Schroeder exit:
4830de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
4840de9a702SOliver Neukum 	if (!mos7840_port->zombie)
4850de9a702SOliver Neukum 		result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
4860de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
4870de9a702SOliver Neukum 	if (result) {
4880de9a702SOliver Neukum 		dev_err(&urb->dev->dev,
4890de9a702SOliver Neukum 			"%s - Error %d submitting interrupt urb\n",
4900de9a702SOliver Neukum 			__FUNCTION__, result);
4910de9a702SOliver Neukum 	}
4923f542974SPaul B Schroeder }
4933f542974SPaul B Schroeder 
4943f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
4953f542974SPaul B Schroeder 			   __u16 * val)
4963f542974SPaul B Schroeder {
4973f542974SPaul B Schroeder 	struct usb_device *dev = mcs->port->serial->dev;
4980de9a702SOliver Neukum 	struct usb_ctrlrequest *dr = mcs->dr;
4990de9a702SOliver Neukum 	unsigned char *buffer = mcs->ctrl_buf;
5000de9a702SOliver Neukum 	int ret;
5013f542974SPaul B Schroeder 
5023f542974SPaul B Schroeder 	dr->bRequestType = MCS_RD_RTYPE;
5033f542974SPaul B Schroeder 	dr->bRequest = MCS_RDREQ;
5043f542974SPaul B Schroeder 	dr->wValue = cpu_to_le16(Wval);	//0;
5053f542974SPaul B Schroeder 	dr->wIndex = cpu_to_le16(reg);
5063f542974SPaul B Schroeder 	dr->wLength = cpu_to_le16(2);
5073f542974SPaul B Schroeder 
5083f542974SPaul B Schroeder 	usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
5093f542974SPaul B Schroeder 			     (unsigned char *)dr, buffer, 2,
5103f542974SPaul B Schroeder 			     mos7840_control_callback, mcs);
5113f542974SPaul B Schroeder 	mcs->control_urb->transfer_buffer_length = 2;
5123f542974SPaul B Schroeder 	ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
5133f542974SPaul B Schroeder 	return ret;
5143f542974SPaul B Schroeder }
5153f542974SPaul B Schroeder 
5163f542974SPaul B Schroeder /*****************************************************************************
5173f542974SPaul B Schroeder  * mos7840_interrupt_callback
5183f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
5193f542974SPaul B Schroeder  *	interrupt endpoint.
5203f542974SPaul B Schroeder  *****************************************************************************/
5213f542974SPaul B Schroeder 
5227d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb)
5233f542974SPaul B Schroeder {
5243f542974SPaul B Schroeder 	int result;
5253f542974SPaul B Schroeder 	int length;
5263f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
5273f542974SPaul B Schroeder 	struct usb_serial *serial;
5283f542974SPaul B Schroeder 	__u16 Data;
5293f542974SPaul B Schroeder 	unsigned char *data;
5303f542974SPaul B Schroeder 	__u8 sp[5], st;
5310de9a702SOliver Neukum 	int i, rv = 0;
5320de9a702SOliver Neukum 	__u16 wval, wreg = 0;
5330643c724SGreg Kroah-Hartman 	int status = urb->status;
5343f542974SPaul B Schroeder 
5353f542974SPaul B Schroeder 	dbg("%s", " : Entering\n");
5363f542974SPaul B Schroeder 
5370643c724SGreg Kroah-Hartman 	switch (status) {
5383f542974SPaul B Schroeder 	case 0:
5393f542974SPaul B Schroeder 		/* success */
5403f542974SPaul B Schroeder 		break;
5413f542974SPaul B Schroeder 	case -ECONNRESET:
5423f542974SPaul B Schroeder 	case -ENOENT:
5433f542974SPaul B Schroeder 	case -ESHUTDOWN:
5443f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
5453f542974SPaul B Schroeder 		dbg("%s - urb shutting down with status: %d", __FUNCTION__,
5460643c724SGreg Kroah-Hartman 		    status);
5473f542974SPaul B Schroeder 		return;
5483f542974SPaul B Schroeder 	default:
5493f542974SPaul B Schroeder 		dbg("%s - nonzero urb status received: %d", __FUNCTION__,
5500643c724SGreg Kroah-Hartman 		    status);
5513f542974SPaul B Schroeder 		goto exit;
5523f542974SPaul B Schroeder 	}
5533f542974SPaul B Schroeder 
5543f542974SPaul B Schroeder 	length = urb->actual_length;
5553f542974SPaul B Schroeder 	data = urb->transfer_buffer;
5563f542974SPaul B Schroeder 
5573f542974SPaul B Schroeder 	serial = (struct usb_serial *)urb->context;
5583f542974SPaul B Schroeder 
5593f542974SPaul B Schroeder 	/* Moschip get 5 bytes
5603f542974SPaul B Schroeder 	 * Byte 1 IIR Port 1 (port.number is 0)
5613f542974SPaul B Schroeder 	 * Byte 2 IIR Port 2 (port.number is 1)
5623f542974SPaul B Schroeder 	 * Byte 3 IIR Port 3 (port.number is 2)
5633f542974SPaul B Schroeder 	 * Byte 4 IIR Port 4 (port.number is 3)
5643f542974SPaul B Schroeder 	 * Byte 5 FIFO status for both */
5653f542974SPaul B Schroeder 
5663f542974SPaul B Schroeder 	if (length && length > 5) {
5673f542974SPaul B Schroeder 		dbg("%s \n", "Wrong data !!!");
5683f542974SPaul B Schroeder 		return;
5693f542974SPaul B Schroeder 	}
5703f542974SPaul B Schroeder 
5713f542974SPaul B Schroeder 	sp[0] = (__u8) data[0];
5723f542974SPaul B Schroeder 	sp[1] = (__u8) data[1];
5733f542974SPaul B Schroeder 	sp[2] = (__u8) data[2];
5743f542974SPaul B Schroeder 	sp[3] = (__u8) data[3];
5753f542974SPaul B Schroeder 	st = (__u8) data[4];
5763f542974SPaul B Schroeder 
5773f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; i++) {
5783f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
5793f542974SPaul B Schroeder 		wval =
5803f542974SPaul B Schroeder 		    (((__u16) serial->port[i]->number -
5813f542974SPaul B Schroeder 		      (__u16) (serial->minor)) + 1) << 8;
5823f542974SPaul B Schroeder 		if (mos7840_port->open) {
5833f542974SPaul B Schroeder 			if (sp[i] & 0x01) {
5843f542974SPaul B Schroeder 				dbg("SP%d No Interrupt !!!\n", i);
5853f542974SPaul B Schroeder 			} else {
5863f542974SPaul B Schroeder 				switch (sp[i] & 0x0f) {
5873f542974SPaul B Schroeder 				case SERIAL_IIR_RLS:
5883f542974SPaul B Schroeder 					dbg("Serial Port %d: Receiver status error or ", i);
5893f542974SPaul B Schroeder 					dbg("address bit detected in 9-bit mode\n");
5903f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 1;
5910de9a702SOliver Neukum 					wreg = LINE_STATUS_REGISTER;
5923f542974SPaul B Schroeder 					break;
5933f542974SPaul B Schroeder 				case SERIAL_IIR_MS:
5943f542974SPaul B Schroeder 					dbg("Serial Port %d: Modem status change\n", i);
5953f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 0;
5960de9a702SOliver Neukum 					wreg = MODEM_STATUS_REGISTER;
5973f542974SPaul B Schroeder 					break;
5983f542974SPaul B Schroeder 				}
5990de9a702SOliver Neukum 				spin_lock(&mos7840_port->pool_lock);
6000de9a702SOliver Neukum 				if (!mos7840_port->zombie) {
6010de9a702SOliver Neukum 					rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
6020de9a702SOliver Neukum 				} else {
6030de9a702SOliver Neukum 					spin_unlock(&mos7840_port->pool_lock);
6040de9a702SOliver Neukum 					return;
6050de9a702SOliver Neukum 				}
6060de9a702SOliver Neukum 				spin_unlock(&mos7840_port->pool_lock);
6073f542974SPaul B Schroeder 			}
6083f542974SPaul B Schroeder 		}
6093f542974SPaul B Schroeder 	}
6100de9a702SOliver Neukum 	if (!(rv < 0)) /* the completion handler for the control urb will resubmit */
6110de9a702SOliver Neukum 		return;
6123f542974SPaul B Schroeder exit:
6133f542974SPaul B Schroeder 	result = usb_submit_urb(urb, GFP_ATOMIC);
6143f542974SPaul B Schroeder 	if (result) {
6153f542974SPaul B Schroeder 		dev_err(&urb->dev->dev,
6163f542974SPaul B Schroeder 			"%s - Error %d submitting interrupt urb\n",
6173f542974SPaul B Schroeder 			__FUNCTION__, result);
6183f542974SPaul B Schroeder 	}
6193f542974SPaul B Schroeder }
6203f542974SPaul B Schroeder 
6213f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port,
6223f542974SPaul B Schroeder 				       const char *function)
6233f542974SPaul B Schroeder {
6243f542974SPaul B Schroeder 	if (!port) {
6253f542974SPaul B Schroeder 		dbg("%s - port == NULL", function);
6263f542974SPaul B Schroeder 		return -1;
6273f542974SPaul B Schroeder 	}
6283f542974SPaul B Schroeder 	if (!port->serial) {
6293f542974SPaul B Schroeder 		dbg("%s - port->serial == NULL", function);
6303f542974SPaul B Schroeder 		return -1;
6313f542974SPaul B Schroeder 	}
6323f542974SPaul B Schroeder 
6333f542974SPaul B Schroeder 	return 0;
6343f542974SPaul B Schroeder }
6353f542974SPaul B Schroeder 
6363f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
6373f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
6383f542974SPaul B Schroeder 					 const char *function)
6393f542974SPaul B Schroeder {
6403f542974SPaul B Schroeder 	if (!serial) {
6413f542974SPaul B Schroeder 		dbg("%s - serial == NULL", function);
6423f542974SPaul B Schroeder 		return -1;
6433f542974SPaul B Schroeder 	}
6443f542974SPaul B Schroeder 	if (!serial->type) {
6453f542974SPaul B Schroeder 		dbg("%s - serial->type == NULL!", function);
6463f542974SPaul B Schroeder 		return -1;
6473f542974SPaul B Schroeder 	}
6483f542974SPaul B Schroeder 
6493f542974SPaul B Schroeder 	return 0;
6503f542974SPaul B Schroeder }
6513f542974SPaul B Schroeder 
6523f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
6533f542974SPaul B Schroeder 						 const char *function)
6543f542974SPaul B Schroeder {
6553f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
6563f542974SPaul B Schroeder 	if (!port ||
6573f542974SPaul B Schroeder 	    mos7840_port_paranoia_check(port, function) ||
6583f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
6590de9a702SOliver Neukum 		/* then say that we don't have a valid usb_serial thing, which will
6600de9a702SOliver Neukum 		 * end up genrating -ENODEV return values */
6613f542974SPaul B Schroeder 		return NULL;
6623f542974SPaul B Schroeder 	}
6633f542974SPaul B Schroeder 
6643f542974SPaul B Schroeder 	return port->serial;
6653f542974SPaul B Schroeder }
6663f542974SPaul B Schroeder 
6673f542974SPaul B Schroeder /*****************************************************************************
6683f542974SPaul B Schroeder  * mos7840_bulk_in_callback
6693f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
6703f542974SPaul B Schroeder  *	bulk in endpoint.
6713f542974SPaul B Schroeder  *****************************************************************************/
6723f542974SPaul B Schroeder 
6737d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
6743f542974SPaul B Schroeder {
6750643c724SGreg Kroah-Hartman 	int retval;
6763f542974SPaul B Schroeder 	unsigned char *data;
6773f542974SPaul B Schroeder 	struct usb_serial *serial;
6783f542974SPaul B Schroeder 	struct usb_serial_port *port;
6793f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
6803f542974SPaul B Schroeder 	struct tty_struct *tty;
6810643c724SGreg Kroah-Hartman 	int status = urb->status;
6823f542974SPaul B Schroeder 
6830643c724SGreg Kroah-Hartman 	if (status) {
6840643c724SGreg Kroah-Hartman 		dbg("nonzero read bulk status received: %d", status);
6853f542974SPaul B Schroeder 		return;
6863f542974SPaul B Schroeder 	}
6873f542974SPaul B Schroeder 
6883f542974SPaul B Schroeder 	mos7840_port = (struct moschip_port *)urb->context;
6893f542974SPaul B Schroeder 	if (!mos7840_port) {
6903f542974SPaul B Schroeder 		dbg("%s", "NULL mos7840_port pointer \n");
6913f542974SPaul B Schroeder 		return;
6923f542974SPaul B Schroeder 	}
6933f542974SPaul B Schroeder 
6943f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
6953f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
6963f542974SPaul B Schroeder 		dbg("%s", "Port Paranoia failed \n");
6973f542974SPaul B Schroeder 		return;
6983f542974SPaul B Schroeder 	}
6993f542974SPaul B Schroeder 
7003f542974SPaul B Schroeder 	serial = mos7840_get_usb_serial(port, __FUNCTION__);
7013f542974SPaul B Schroeder 	if (!serial) {
7023f542974SPaul B Schroeder 		dbg("%s\n", "Bad serial pointer ");
7033f542974SPaul B Schroeder 		return;
7043f542974SPaul B Schroeder 	}
7053f542974SPaul B Schroeder 
7063f542974SPaul B Schroeder 	dbg("%s\n", "Entering... \n");
7073f542974SPaul B Schroeder 
7083f542974SPaul B Schroeder 	data = urb->transfer_buffer;
7093f542974SPaul B Schroeder 
7103f542974SPaul B Schroeder 	dbg("%s", "Entering ........... \n");
7113f542974SPaul B Schroeder 
7123f542974SPaul B Schroeder 	if (urb->actual_length) {
7133f542974SPaul B Schroeder 		tty = mos7840_port->port->tty;
7143f542974SPaul B Schroeder 		if (tty) {
7153f542974SPaul B Schroeder 			tty_buffer_request_room(tty, urb->actual_length);
7163f542974SPaul B Schroeder 			tty_insert_flip_string(tty, data, urb->actual_length);
7173f542974SPaul B Schroeder 			dbg(" %s \n", data);
7183f542974SPaul B Schroeder 			tty_flip_buffer_push(tty);
7193f542974SPaul B Schroeder 		}
7203f542974SPaul B Schroeder 		mos7840_port->icount.rx += urb->actual_length;
7210de9a702SOliver Neukum 		smp_wmb();
7223f542974SPaul B Schroeder 		dbg("mos7840_port->icount.rx is %d:\n",
7233f542974SPaul B Schroeder 		    mos7840_port->icount.rx);
7243f542974SPaul B Schroeder 	}
7253f542974SPaul B Schroeder 
7263f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
7273f542974SPaul B Schroeder 		dbg("%s", "URB KILLED !!!\n");
7283f542974SPaul B Schroeder 		return;
7293f542974SPaul B Schroeder 	}
7303f542974SPaul B Schroeder 
7310de9a702SOliver Neukum 
7323f542974SPaul B Schroeder 	mos7840_port->read_urb->dev = serial->dev;
7333f542974SPaul B Schroeder 
7340643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
7353f542974SPaul B Schroeder 
7360643c724SGreg Kroah-Hartman 	if (retval) {
7370643c724SGreg Kroah-Hartman 		dbg(" usb_submit_urb(read bulk) failed, retval = %d",
7380643c724SGreg Kroah-Hartman 		 retval);
7393f542974SPaul B Schroeder 	}
7403f542974SPaul B Schroeder }
7413f542974SPaul B Schroeder 
7423f542974SPaul B Schroeder /*****************************************************************************
7433f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
7443f542974SPaul B Schroeder  *	this is the callback function for when we have finished sending serial data
7453f542974SPaul B Schroeder  *	on the bulk out endpoint.
7463f542974SPaul B Schroeder  *****************************************************************************/
7473f542974SPaul B Schroeder 
7487d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
7493f542974SPaul B Schroeder {
7503f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
7513f542974SPaul B Schroeder 	struct tty_struct *tty;
7520643c724SGreg Kroah-Hartman 	int status = urb->status;
7530de9a702SOliver Neukum 	int i;
7540de9a702SOliver Neukum 
7550de9a702SOliver Neukum 	mos7840_port = (struct moschip_port *)urb->context;
7560de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
7570de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
7580de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
7590de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
7600de9a702SOliver Neukum 			break;
7610de9a702SOliver Neukum 		}
7620de9a702SOliver Neukum 	}
7630de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
7640de9a702SOliver Neukum 
7650643c724SGreg Kroah-Hartman 	if (status) {
7660643c724SGreg Kroah-Hartman 		dbg("nonzero write bulk status received:%d\n", status);
7673f542974SPaul B Schroeder 		return;
7683f542974SPaul B Schroeder 	}
7693f542974SPaul B Schroeder 
7703f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(mos7840_port->port, __FUNCTION__)) {
7713f542974SPaul B Schroeder 		dbg("%s", "Port Paranoia failed \n");
7723f542974SPaul B Schroeder 		return;
7733f542974SPaul B Schroeder 	}
7743f542974SPaul B Schroeder 
7753f542974SPaul B Schroeder 	dbg("%s \n", "Entering .........");
7763f542974SPaul B Schroeder 
7773f542974SPaul B Schroeder 	tty = mos7840_port->port->tty;
7783f542974SPaul B Schroeder 
779b963a844SJiri Slaby 	if (tty && mos7840_port->open)
780b963a844SJiri Slaby 		tty_wakeup(tty);
7813f542974SPaul B Schroeder 
7823f542974SPaul B Schroeder }
7833f542974SPaul B Schroeder 
7843f542974SPaul B Schroeder /************************************************************************/
7853f542974SPaul 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       */
7863f542974SPaul B Schroeder /************************************************************************/
7873f542974SPaul B Schroeder #ifdef MCSSerialProbe
7883f542974SPaul B Schroeder static int mos7840_serial_probe(struct usb_serial *serial,
7893f542974SPaul B Schroeder 				const struct usb_device_id *id)
7903f542974SPaul B Schroeder {
7913f542974SPaul B Schroeder 
7923f542974SPaul B Schroeder 	/*need to implement the mode_reg reading and updating\
7933f542974SPaul B Schroeder 	   structures usb_serial_ device_type\
7943f542974SPaul B Schroeder 	   (i.e num_ports, num_bulkin,bulkout etc) */
7953f542974SPaul B Schroeder 	/* Also we can update the changes  attach */
7963f542974SPaul B Schroeder 	return 1;
7973f542974SPaul B Schroeder }
7983f542974SPaul B Schroeder #endif
7993f542974SPaul B Schroeder 
8003f542974SPaul B Schroeder /*****************************************************************************
8013f542974SPaul B Schroeder  * mos7840_open
8023f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
8033f542974SPaul B Schroeder  *	If successful, we return 0
8043f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
8053f542974SPaul B Schroeder  *****************************************************************************/
8063f542974SPaul B Schroeder 
8073f542974SPaul B Schroeder static int mos7840_open(struct usb_serial_port *port, struct file *filp)
8083f542974SPaul B Schroeder {
8093f542974SPaul B Schroeder 	int response;
8103f542974SPaul B Schroeder 	int j;
8113f542974SPaul B Schroeder 	struct usb_serial *serial;
8123f542974SPaul B Schroeder 	struct urb *urb;
8133f542974SPaul B Schroeder 	__u16 Data;
8143f542974SPaul B Schroeder 	int status;
8153f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8160de9a702SOliver Neukum 	struct moschip_port *port0;
8173f542974SPaul B Schroeder 
8183f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
8193f542974SPaul B Schroeder 		dbg("%s", "Port Paranoia failed \n");
8203f542974SPaul B Schroeder 		return -ENODEV;
8213f542974SPaul B Schroeder 	}
8223f542974SPaul B Schroeder 
8233f542974SPaul B Schroeder 	serial = port->serial;
8243f542974SPaul B Schroeder 
8253f542974SPaul B Schroeder 	if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
8263f542974SPaul B Schroeder 		dbg("%s", "Serial Paranoia failed \n");
8273f542974SPaul B Schroeder 		return -ENODEV;
8283f542974SPaul B Schroeder 	}
8293f542974SPaul B Schroeder 
8303f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
8310de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
8323f542974SPaul B Schroeder 
8330de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
8343f542974SPaul B Schroeder 		return -ENODEV;
8353f542974SPaul B Schroeder 
8363f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
8373f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
8380de9a702SOliver Neukum 	port0->open_ports++;
8393f542974SPaul B Schroeder 
8403f542974SPaul B Schroeder 	/* Initialising the write urb pool */
8413f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
8420de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
8433f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
8443f542974SPaul B Schroeder 
8453f542974SPaul B Schroeder 		if (urb == NULL) {
8463f542974SPaul B Schroeder 			err("No more urbs???");
8473f542974SPaul B Schroeder 			continue;
8483f542974SPaul B Schroeder 		}
8493f542974SPaul B Schroeder 
8500de9a702SOliver Neukum 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
8513f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
8520de9a702SOliver Neukum 			usb_free_urb(urb);
8530de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
8543f542974SPaul B Schroeder 			err("%s-out of memory for urb buffers.", __FUNCTION__);
8553f542974SPaul B Schroeder 			continue;
8563f542974SPaul B Schroeder 		}
8573f542974SPaul B Schroeder 	}
8583f542974SPaul B Schroeder 
8593f542974SPaul B Schroeder /*****************************************************************************
8603f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
8613f542974SPaul B Schroeder  *
8623f542974SPaul B Schroeder  * Register Index
8633f542974SPaul B Schroeder  * 1 : IER
8643f542974SPaul B Schroeder  * 2 : FCR
8653f542974SPaul B Schroeder  * 3 : LCR
8663f542974SPaul B Schroeder  * 4 : MCR
8673f542974SPaul B Schroeder  *
8683f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
8693f542974SPaul B Schroeder  *****************************************************************************/
8703f542974SPaul B Schroeder 
8713f542974SPaul B Schroeder //NEED to check the following Block
8723f542974SPaul B Schroeder 
8733f542974SPaul B Schroeder 	status = 0;
8743f542974SPaul B Schroeder 	Data = 0x0;
8753f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
8763f542974SPaul B Schroeder 	if (status < 0) {
8773f542974SPaul B Schroeder 		dbg("Reading Spreg failed\n");
8783f542974SPaul B Schroeder 		return -1;
8793f542974SPaul B Schroeder 	}
8803f542974SPaul B Schroeder 	Data |= 0x80;
8813f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
8823f542974SPaul B Schroeder 	if (status < 0) {
8833f542974SPaul B Schroeder 		dbg("writing Spreg failed\n");
8843f542974SPaul B Schroeder 		return -1;
8853f542974SPaul B Schroeder 	}
8863f542974SPaul B Schroeder 
8873f542974SPaul B Schroeder 	Data &= ~0x80;
8883f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
8893f542974SPaul B Schroeder 	if (status < 0) {
8903f542974SPaul B Schroeder 		dbg("writing Spreg failed\n");
8913f542974SPaul B Schroeder 		return -1;
8923f542974SPaul B Schroeder 	}
8933f542974SPaul B Schroeder //End of block to be checked
8943f542974SPaul B Schroeder 
8953f542974SPaul B Schroeder 	status = 0;
8963f542974SPaul B Schroeder 	Data = 0x0;
8973f542974SPaul B Schroeder 	status =
8983f542974SPaul B Schroeder 	    mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
8993f542974SPaul B Schroeder 	if (status < 0) {
9003f542974SPaul B Schroeder 		dbg("Reading Controlreg failed\n");
9013f542974SPaul B Schroeder 		return -1;
9023f542974SPaul B Schroeder 	}
9033f542974SPaul B Schroeder 	Data |= 0x08;		//Driver done bit
9043f542974SPaul B Schroeder 	Data |= 0x20;		//rx_disable
9050de9a702SOliver Neukum 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
9063f542974SPaul B Schroeder 	if (status < 0) {
9073f542974SPaul B Schroeder 		dbg("writing Controlreg failed\n");
9083f542974SPaul B Schroeder 		return -1;
9093f542974SPaul B Schroeder 	}
9103f542974SPaul B Schroeder 	//do register settings here
9113f542974SPaul B Schroeder 	// Set all regs to the device default values.
9123f542974SPaul B Schroeder 	////////////////////////////////////
9133f542974SPaul B Schroeder 	// First Disable all interrupts.
9143f542974SPaul B Schroeder 	////////////////////////////////////
9153f542974SPaul B Schroeder 
9163f542974SPaul B Schroeder 	Data = 0x00;
9173f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
9183f542974SPaul B Schroeder 	if (status < 0) {
9193f542974SPaul B Schroeder 		dbg("disableing interrupts failed\n");
9203f542974SPaul B Schroeder 		return -1;
9213f542974SPaul B Schroeder 	}
9223f542974SPaul B Schroeder 	// Set FIFO_CONTROL_REGISTER to the default value
9233f542974SPaul B Schroeder 	Data = 0x00;
9243f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9253f542974SPaul B Schroeder 	if (status < 0) {
9263f542974SPaul B Schroeder 		dbg("Writing FIFO_CONTROL_REGISTER  failed\n");
9273f542974SPaul B Schroeder 		return -1;
9283f542974SPaul B Schroeder 	}
9293f542974SPaul B Schroeder 
9303f542974SPaul B Schroeder 	Data = 0xcf;
9313f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9323f542974SPaul B Schroeder 	if (status < 0) {
9333f542974SPaul B Schroeder 		dbg("Writing FIFO_CONTROL_REGISTER  failed\n");
9343f542974SPaul B Schroeder 		return -1;
9353f542974SPaul B Schroeder 	}
9363f542974SPaul B Schroeder 
9373f542974SPaul B Schroeder 	Data = 0x03;
9383f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9393f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9403f542974SPaul B Schroeder 
9413f542974SPaul B Schroeder 	Data = 0x0b;
9423f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
9433f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
9443f542974SPaul B Schroeder 
9453f542974SPaul B Schroeder 	Data = 0x00;
9463f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
9473f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9483f542974SPaul B Schroeder 
9493f542974SPaul B Schroeder 	Data |= SERIAL_LCR_DLAB;	//data latch enable in LCR 0x80
9503f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9513f542974SPaul B Schroeder 
9523f542974SPaul B Schroeder 	Data = 0x0c;
9533f542974SPaul B Schroeder 	status = 0;
9543f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
9553f542974SPaul B Schroeder 
9563f542974SPaul B Schroeder 	Data = 0x0;
9573f542974SPaul B Schroeder 	status = 0;
9583f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
9593f542974SPaul B Schroeder 
9603f542974SPaul B Schroeder 	Data = 0x00;
9613f542974SPaul B Schroeder 	status = 0;
9623f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
9633f542974SPaul B Schroeder 
9643f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
9653f542974SPaul B Schroeder 	status = 0;
9663f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
9673f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
9683f542974SPaul B Schroeder 
9693f542974SPaul B Schroeder 	//clearing Bulkin and Bulkout Fifo
9703f542974SPaul B Schroeder 	Data = 0x0;
9713f542974SPaul B Schroeder 	status = 0;
9723f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
9733f542974SPaul B Schroeder 
9743f542974SPaul B Schroeder 	Data = Data | 0x0c;
9753f542974SPaul B Schroeder 	status = 0;
9763f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9773f542974SPaul B Schroeder 
9783f542974SPaul B Schroeder 	Data = Data & ~0x0c;
9793f542974SPaul B Schroeder 	status = 0;
9803f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9813f542974SPaul B Schroeder 	//Finally enable all interrupts
9823f542974SPaul B Schroeder 	Data = 0x0;
9833f542974SPaul B Schroeder 	Data = 0x0c;
9843f542974SPaul B Schroeder 	status = 0;
9853f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
9863f542974SPaul B Schroeder 
9873f542974SPaul B Schroeder 	//clearing rx_disable
9883f542974SPaul B Schroeder 	Data = 0x0;
9893f542974SPaul B Schroeder 	status = 0;
9903f542974SPaul B Schroeder 	status =
9913f542974SPaul B Schroeder 	    mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
9923f542974SPaul B Schroeder 	Data = Data & ~0x20;
9933f542974SPaul B Schroeder 	status = 0;
9943f542974SPaul B Schroeder 	status =
9953f542974SPaul B Schroeder 	    mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
9963f542974SPaul B Schroeder 
9973f542974SPaul B Schroeder 	// rx_negate
9983f542974SPaul B Schroeder 	Data = 0x0;
9993f542974SPaul B Schroeder 	status = 0;
10003f542974SPaul B Schroeder 	status =
10013f542974SPaul B Schroeder 	    mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
10023f542974SPaul B Schroeder 	Data = Data | 0x10;
10033f542974SPaul B Schroeder 	status = 0;
10043f542974SPaul B Schroeder 	status =
10053f542974SPaul B Schroeder 	    mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
10063f542974SPaul B Schroeder 
10073f542974SPaul B Schroeder 	/* force low_latency on so that our tty_push actually forces *
10083f542974SPaul B Schroeder 	 * the data through,otherwise it is scheduled, and with      *
10093f542974SPaul B Schroeder 	 * high data rates (like with OHCI) data can get lost.       */
10103f542974SPaul B Schroeder 
10113f542974SPaul B Schroeder 	if (port->tty)
10123f542974SPaul B Schroeder 		port->tty->low_latency = 1;
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 
10193f542974SPaul B Schroeder 			/* set up interrupt urb */
10203f542974SPaul B Schroeder 
10213f542974SPaul B Schroeder 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
10223f542974SPaul B Schroeder 					 serial->dev,
10233f542974SPaul B Schroeder 					 usb_rcvintpipe(serial->dev,
10243f542974SPaul B Schroeder 							serial->port[0]->
10253f542974SPaul B Schroeder 							interrupt_in_endpointAddress),
10263f542974SPaul B Schroeder 					 serial->port[0]->interrupt_in_buffer,
10273f542974SPaul B Schroeder 					 serial->port[0]->interrupt_in_urb->
10283f542974SPaul B Schroeder 					 transfer_buffer_length,
10293f542974SPaul B Schroeder 					 mos7840_interrupt_callback,
10303f542974SPaul B Schroeder 					 serial,
10313f542974SPaul B Schroeder 					 serial->port[0]->interrupt_in_urb->
10323f542974SPaul B Schroeder 					 interval);
10333f542974SPaul B Schroeder 
10343f542974SPaul B Schroeder 			/* start interrupt read for mos7840               *
10353f542974SPaul B Schroeder 			 * will continue as long as mos7840 is connected  */
10363f542974SPaul B Schroeder 
10373f542974SPaul B Schroeder 			response =
10383f542974SPaul B Schroeder 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
10393f542974SPaul B Schroeder 					   GFP_KERNEL);
10403f542974SPaul B Schroeder 			if (response) {
10413f542974SPaul B Schroeder 				err("%s - Error %d submitting interrupt urb",
10423f542974SPaul B Schroeder 				    __FUNCTION__, response);
10433f542974SPaul B Schroeder 			}
10443f542974SPaul B Schroeder 
10453f542974SPaul B Schroeder 		}
10463f542974SPaul B Schroeder 
10473f542974SPaul B Schroeder 	}
10483f542974SPaul B Schroeder 
10493f542974SPaul B Schroeder 	/* see if we've set up our endpoint info yet   *
10503f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the  *
10513f542974SPaul B Schroeder 	 * structures were not set up at that time.)   */
10523f542974SPaul B Schroeder 
10533f542974SPaul B Schroeder 	dbg("port number is %d \n", port->number);
10543f542974SPaul B Schroeder 	dbg("serial number is %d \n", port->serial->minor);
10553f542974SPaul B Schroeder 	dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
10563f542974SPaul B Schroeder 	dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
10573f542974SPaul B Schroeder 	dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
10583f542974SPaul B Schroeder 	dbg("port's number in the device is %d\n", mos7840_port->port_num);
10593f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
10603f542974SPaul B Schroeder 
10613f542974SPaul B Schroeder 	/* set up our bulk in urb */
10623f542974SPaul B Schroeder 
10633f542974SPaul B Schroeder 	usb_fill_bulk_urb(mos7840_port->read_urb,
10643f542974SPaul B Schroeder 			  serial->dev,
10653f542974SPaul B Schroeder 			  usb_rcvbulkpipe(serial->dev,
10663f542974SPaul B Schroeder 					  port->bulk_in_endpointAddress),
10673f542974SPaul B Schroeder 			  port->bulk_in_buffer,
10683f542974SPaul B Schroeder 			  mos7840_port->read_urb->transfer_buffer_length,
10693f542974SPaul B Schroeder 			  mos7840_bulk_in_callback, mos7840_port);
10703f542974SPaul B Schroeder 
10713f542974SPaul B Schroeder 	dbg("mos7840_open: bulkin endpoint is %d\n",
10723f542974SPaul B Schroeder 	    port->bulk_in_endpointAddress);
10733f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
10743f542974SPaul B Schroeder 	if (response) {
10753f542974SPaul B Schroeder 		err("%s - Error %d submitting control urb", __FUNCTION__,
10763f542974SPaul B Schroeder 		    response);
10773f542974SPaul B Schroeder 	}
10783f542974SPaul B Schroeder 
10793f542974SPaul B Schroeder 	/* initialize our wait queues */
10803f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->wait_chase);
10813f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->delta_msr_wait);
10823f542974SPaul B Schroeder 
10833f542974SPaul B Schroeder 	/* initialize our icount structure */
10843f542974SPaul B Schroeder 	memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
10853f542974SPaul B Schroeder 
10863f542974SPaul B Schroeder 	/* initialize our port settings */
10873f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;	/* Must set to enable ints! */
10883f542974SPaul B Schroeder 	/* send a open port command */
10893f542974SPaul B Schroeder 	mos7840_port->open = 1;
10903f542974SPaul B Schroeder 	//mos7840_change_port_settings(mos7840_port,old_termios);
10913f542974SPaul B Schroeder 	mos7840_port->icount.tx = 0;
10923f542974SPaul B Schroeder 	mos7840_port->icount.rx = 0;
10933f542974SPaul B Schroeder 
109497c4965dSAl Viro 	dbg("\n\nusb_serial serial:%p       mos7840_port:%p\n      usb_serial_port port:%p\n\n", serial, mos7840_port, port);
10953f542974SPaul B Schroeder 
10963f542974SPaul B Schroeder 	return 0;
10973f542974SPaul B Schroeder 
10983f542974SPaul B Schroeder }
10993f542974SPaul B Schroeder 
11003f542974SPaul B Schroeder /*****************************************************************************
11013f542974SPaul B Schroeder  * mos7840_chars_in_buffer
11023f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
11033f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
11043f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
11053f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
11063f542974SPaul B Schroeder  *	system,
11073f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
11083f542974SPaul B Schroeder  *****************************************************************************/
11093f542974SPaul B Schroeder 
11103f542974SPaul B Schroeder static int mos7840_chars_in_buffer(struct usb_serial_port *port)
11113f542974SPaul B Schroeder {
11123f542974SPaul B Schroeder 	int i;
11133f542974SPaul B Schroeder 	int chars = 0;
11140de9a702SOliver Neukum 	unsigned long flags;
11153f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11163f542974SPaul B Schroeder 
11173f542974SPaul B Schroeder 	dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
11183f542974SPaul B Schroeder 
11193f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
11203f542974SPaul B Schroeder 		dbg("%s", "Invalid port \n");
11213f542974SPaul B Schroeder 		return -1;
11223f542974SPaul B Schroeder 	}
11233f542974SPaul B Schroeder 
11243f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11253f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
11263f542974SPaul B Schroeder 		dbg("%s \n", "mos7840_break:leaving ...........");
11273f542974SPaul B Schroeder 		return -1;
11283f542974SPaul B Schroeder 	}
11293f542974SPaul B Schroeder 
11300de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock,flags);
11313f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
11320de9a702SOliver Neukum 		if (mos7840_port->busy[i]) {
11333f542974SPaul B Schroeder 			chars += URB_TRANSFER_BUFFER_SIZE;
11343f542974SPaul B Schroeder 		}
11353f542974SPaul B Schroeder 	}
11360de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock,flags);
11373f542974SPaul B Schroeder 	dbg("%s - returns %d", __FUNCTION__, chars);
11380de9a702SOliver Neukum 	return chars;
11393f542974SPaul B Schroeder 
11403f542974SPaul B Schroeder }
11413f542974SPaul B Schroeder 
11423f542974SPaul B Schroeder /************************************************************************
11433f542974SPaul B Schroeder  *
11443f542974SPaul B Schroeder  * mos7840_block_until_tx_empty
11453f542974SPaul B Schroeder  *
11463f542974SPaul B Schroeder  *	This function will block the close until one of the following:
11473f542974SPaul B Schroeder  *		1. TX count are 0
11483f542974SPaul B Schroeder  *		2. The mos7840 has stopped
1149dc0d5c1eSJoe Perches  *		3. A timeout of 3 seconds without activity has expired
11503f542974SPaul B Schroeder  *
11513f542974SPaul B Schroeder  ************************************************************************/
11523f542974SPaul B Schroeder static void mos7840_block_until_tx_empty(struct moschip_port *mos7840_port)
11533f542974SPaul B Schroeder {
11543f542974SPaul B Schroeder 	int timeout = HZ / 10;
11553f542974SPaul B Schroeder 	int wait = 30;
11563f542974SPaul B Schroeder 	int count;
11573f542974SPaul B Schroeder 
11583f542974SPaul B Schroeder 	while (1) {
11593f542974SPaul B Schroeder 
11603f542974SPaul B Schroeder 		count = mos7840_chars_in_buffer(mos7840_port->port);
11613f542974SPaul B Schroeder 
11623f542974SPaul B Schroeder 		/* Check for Buffer status */
11633f542974SPaul B Schroeder 		if (count <= 0) {
11643f542974SPaul B Schroeder 			return;
11653f542974SPaul B Schroeder 		}
11663f542974SPaul B Schroeder 
11673f542974SPaul B Schroeder 		/* Block the thread for a while */
11683f542974SPaul B Schroeder 		interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
11693f542974SPaul B Schroeder 					       timeout);
11703f542974SPaul B Schroeder 
11713f542974SPaul B Schroeder 		/* No activity.. count down section */
11723f542974SPaul B Schroeder 		wait--;
11733f542974SPaul B Schroeder 		if (wait == 0) {
11743f542974SPaul B Schroeder 			dbg("%s - TIMEOUT", __FUNCTION__);
11753f542974SPaul B Schroeder 			return;
11763f542974SPaul B Schroeder 		} else {
1177dc0d5c1eSJoe Perches 			/* Reset timeout value back to seconds */
11783f542974SPaul B Schroeder 			wait = 30;
11793f542974SPaul B Schroeder 		}
11803f542974SPaul B Schroeder 	}
11813f542974SPaul B Schroeder }
11823f542974SPaul B Schroeder 
11833f542974SPaul B Schroeder /*****************************************************************************
11843f542974SPaul B Schroeder  * mos7840_close
11853f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
11863f542974SPaul B Schroeder  *****************************************************************************/
11873f542974SPaul B Schroeder 
11883f542974SPaul B Schroeder static void mos7840_close(struct usb_serial_port *port, struct file *filp)
11893f542974SPaul B Schroeder {
11903f542974SPaul B Schroeder 	struct usb_serial *serial;
11913f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11920de9a702SOliver Neukum 	struct moschip_port *port0;
11933f542974SPaul B Schroeder 	int j;
11943f542974SPaul B Schroeder 	__u16 Data;
11953f542974SPaul B Schroeder 
11963f542974SPaul B Schroeder 	dbg("%s\n", "mos7840_close:entering...");
11973f542974SPaul B Schroeder 
11983f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
11993f542974SPaul B Schroeder 		dbg("%s", "Port Paranoia failed \n");
12003f542974SPaul B Schroeder 		return;
12013f542974SPaul B Schroeder 	}
12023f542974SPaul B Schroeder 
12033f542974SPaul B Schroeder 	serial = mos7840_get_usb_serial(port, __FUNCTION__);
12043f542974SPaul B Schroeder 	if (!serial) {
12053f542974SPaul B Schroeder 		dbg("%s", "Serial Paranoia failed \n");
12063f542974SPaul B Schroeder 		return;
12073f542974SPaul B Schroeder 	}
12083f542974SPaul B Schroeder 
12093f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12100de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
12113f542974SPaul B Schroeder 
12120de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
12133f542974SPaul B Schroeder 		return;
12143f542974SPaul B Schroeder 
12153f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
12163f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
12173f542974SPaul B Schroeder 
12183f542974SPaul B Schroeder 	/* Freeing Write URBs */
12193f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
12203f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
12213f542974SPaul B Schroeder 			if (mos7840_port->write_urb_pool[j]->transfer_buffer)
12223f542974SPaul B Schroeder 				kfree(mos7840_port->write_urb_pool[j]->
12233f542974SPaul B Schroeder 				      transfer_buffer);
12243f542974SPaul B Schroeder 
12253f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
12263f542974SPaul B Schroeder 		}
12273f542974SPaul B Schroeder 	}
12283f542974SPaul B Schroeder 
12293f542974SPaul B Schroeder 	if (serial->dev) {
12303f542974SPaul B Schroeder 		/* flush and block until tx is empty */
12313f542974SPaul B Schroeder 		mos7840_block_until_tx_empty(mos7840_port);
12323f542974SPaul B Schroeder 	}
12333f542974SPaul B Schroeder 
12343f542974SPaul B Schroeder 	/* While closing port, shutdown all bulk read, write  *
12353f542974SPaul B Schroeder 	 * and interrupt read if they exists                  */
12363f542974SPaul B Schroeder 	if (serial->dev) {
12373f542974SPaul B Schroeder 
12383f542974SPaul B Schroeder 		if (mos7840_port->write_urb) {
12393f542974SPaul B Schroeder 			dbg("%s", "Shutdown bulk write\n");
12403f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->write_urb);
12413f542974SPaul B Schroeder 		}
12423f542974SPaul B Schroeder 
12433f542974SPaul B Schroeder 		if (mos7840_port->read_urb) {
12443f542974SPaul B Schroeder 			dbg("%s", "Shutdown bulk read\n");
12453f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->read_urb);
12463f542974SPaul B Schroeder 		}
12473f542974SPaul B Schroeder 		if ((&mos7840_port->control_urb)) {
12483f542974SPaul B Schroeder 			dbg("%s", "Shutdown control read\n");
12493f542974SPaul B Schroeder 			//      usb_kill_urb (mos7840_port->control_urb);
12503f542974SPaul B Schroeder 
12513f542974SPaul B Schroeder 		}
12523f542974SPaul B Schroeder 	}
12533f542974SPaul B Schroeder //              if(mos7840_port->ctrl_buf != NULL)
12543f542974SPaul B Schroeder //                      kfree(mos7840_port->ctrl_buf);
12550de9a702SOliver Neukum 	port0->open_ports--;
12563f542974SPaul B Schroeder 	dbg("mos7840_num_open_ports in close%d:in port%d\n",
12570de9a702SOliver Neukum 	    port0->open_ports, port->number);
12580de9a702SOliver Neukum 	if (port0->open_ports == 0) {
12593f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_urb) {
12603f542974SPaul B Schroeder 			dbg("%s", "Shutdown interrupt_in_urb\n");
12610de9a702SOliver Neukum 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
12623f542974SPaul B Schroeder 		}
12633f542974SPaul B Schroeder 	}
12643f542974SPaul B Schroeder 
12653f542974SPaul B Schroeder 	if (mos7840_port->write_urb) {
12663f542974SPaul B Schroeder 		/* if this urb had a transfer buffer already (old tx) free it */
12673f542974SPaul B Schroeder 
12683f542974SPaul B Schroeder 		if (mos7840_port->write_urb->transfer_buffer != NULL) {
12693f542974SPaul B Schroeder 			kfree(mos7840_port->write_urb->transfer_buffer);
12703f542974SPaul B Schroeder 		}
12713f542974SPaul B Schroeder 		usb_free_urb(mos7840_port->write_urb);
12723f542974SPaul B Schroeder 	}
12733f542974SPaul B Schroeder 
12743f542974SPaul B Schroeder 	Data = 0x0;
12753f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
12763f542974SPaul B Schroeder 
12773f542974SPaul B Schroeder 	Data = 0x00;
12783f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
12793f542974SPaul B Schroeder 
12803f542974SPaul B Schroeder 	mos7840_port->open = 0;
12813f542974SPaul B Schroeder 
12823f542974SPaul B Schroeder 	dbg("%s \n", "Leaving ............");
12833f542974SPaul B Schroeder }
12843f542974SPaul B Schroeder 
12853f542974SPaul B Schroeder /************************************************************************
12863f542974SPaul B Schroeder  *
12873f542974SPaul B Schroeder  * mos7840_block_until_chase_response
12883f542974SPaul B Schroeder  *
12893f542974SPaul B Schroeder  *	This function will block the close until one of the following:
12903f542974SPaul B Schroeder  *		1. Response to our Chase comes from mos7840
1291dc0d5c1eSJoe Perches  *		2. A timeout of 10 seconds without activity has expired
12923f542974SPaul B Schroeder  *		   (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
12933f542974SPaul B Schroeder  *
12943f542974SPaul B Schroeder  ************************************************************************/
12953f542974SPaul B Schroeder 
12963f542974SPaul B Schroeder static void mos7840_block_until_chase_response(struct moschip_port
12973f542974SPaul B Schroeder 					       *mos7840_port)
12983f542974SPaul B Schroeder {
12993f542974SPaul B Schroeder 	int timeout = 1 * HZ;
13003f542974SPaul B Schroeder 	int wait = 10;
13013f542974SPaul B Schroeder 	int count;
13023f542974SPaul B Schroeder 
13033f542974SPaul B Schroeder 	while (1) {
13043f542974SPaul B Schroeder 		count = mos7840_chars_in_buffer(mos7840_port->port);
13053f542974SPaul B Schroeder 
13063f542974SPaul B Schroeder 		/* Check for Buffer status */
13073f542974SPaul B Schroeder 		if (count <= 0) {
13083f542974SPaul B Schroeder 			return;
13093f542974SPaul B Schroeder 		}
13103f542974SPaul B Schroeder 
13113f542974SPaul B Schroeder 		/* Block the thread for a while */
13123f542974SPaul B Schroeder 		interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
13133f542974SPaul B Schroeder 					       timeout);
13143f542974SPaul B Schroeder 		/* No activity.. count down section */
13153f542974SPaul B Schroeder 		wait--;
13163f542974SPaul B Schroeder 		if (wait == 0) {
13173f542974SPaul B Schroeder 			dbg("%s - TIMEOUT", __FUNCTION__);
13183f542974SPaul B Schroeder 			return;
13193f542974SPaul B Schroeder 		} else {
1320dc0d5c1eSJoe Perches 			/* Reset timeout value back to seconds */
13213f542974SPaul B Schroeder 			wait = 10;
13223f542974SPaul B Schroeder 		}
13233f542974SPaul B Schroeder 	}
13243f542974SPaul B Schroeder 
13253f542974SPaul B Schroeder }
13263f542974SPaul B Schroeder 
13273f542974SPaul B Schroeder /*****************************************************************************
13283f542974SPaul B Schroeder  * mos7840_break
13293f542974SPaul B Schroeder  *	this function sends a break to the port
13303f542974SPaul B Schroeder  *****************************************************************************/
13313f542974SPaul B Schroeder static void mos7840_break(struct usb_serial_port *port, int break_state)
13323f542974SPaul B Schroeder {
13333f542974SPaul B Schroeder 	unsigned char data;
13343f542974SPaul B Schroeder 	struct usb_serial *serial;
13353f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13363f542974SPaul B Schroeder 
13373f542974SPaul B Schroeder 	dbg("%s \n", "Entering ...........");
13383f542974SPaul B Schroeder 	dbg("mos7840_break: Start\n");
13393f542974SPaul B Schroeder 
13403f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
13413f542974SPaul B Schroeder 		dbg("%s", "Port Paranoia failed \n");
13423f542974SPaul B Schroeder 		return;
13433f542974SPaul B Schroeder 	}
13443f542974SPaul B Schroeder 
13453f542974SPaul B Schroeder 	serial = mos7840_get_usb_serial(port, __FUNCTION__);
13463f542974SPaul B Schroeder 	if (!serial) {
13473f542974SPaul B Schroeder 		dbg("%s", "Serial Paranoia failed \n");
13483f542974SPaul B Schroeder 		return;
13493f542974SPaul B Schroeder 	}
13503f542974SPaul B Schroeder 
13513f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13523f542974SPaul B Schroeder 
13533f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
13543f542974SPaul B Schroeder 		return;
13553f542974SPaul B Schroeder 	}
13563f542974SPaul B Schroeder 
13573f542974SPaul B Schroeder 	if (serial->dev) {
13583f542974SPaul B Schroeder 
13593f542974SPaul B Schroeder 		/* flush and block until tx is empty */
13603f542974SPaul B Schroeder 		mos7840_block_until_chase_response(mos7840_port);
13613f542974SPaul B Schroeder 	}
13623f542974SPaul B Schroeder 
13633f542974SPaul B Schroeder 	if (break_state == -1) {
13643f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
13653f542974SPaul B Schroeder 	} else {
13663f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
13673f542974SPaul B Schroeder 	}
13683f542974SPaul B Schroeder 
13693f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
13703f542974SPaul B Schroeder 	dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
13713f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
13723f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
13733f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
13743f542974SPaul B Schroeder 
13753f542974SPaul B Schroeder 	return;
13763f542974SPaul B Schroeder }
13773f542974SPaul B Schroeder 
13783f542974SPaul B Schroeder /*****************************************************************************
13793f542974SPaul B Schroeder  * mos7840_write_room
13803f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
13813f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
13823f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
13833f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
13843f542974SPaul B Schroeder  *****************************************************************************/
13853f542974SPaul B Schroeder 
13863f542974SPaul B Schroeder static int mos7840_write_room(struct usb_serial_port *port)
13873f542974SPaul B Schroeder {
13883f542974SPaul B Schroeder 	int i;
13893f542974SPaul B Schroeder 	int room = 0;
13900de9a702SOliver Neukum 	unsigned long flags;
13913f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13923f542974SPaul B Schroeder 
13933f542974SPaul B Schroeder 	dbg("%s \n", " mos7840_write_room:entering ...........");
13943f542974SPaul B Schroeder 
13953f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
13963f542974SPaul B Schroeder 		dbg("%s", "Invalid port \n");
13973f542974SPaul B Schroeder 		dbg("%s \n", " mos7840_write_room:leaving ...........");
13983f542974SPaul B Schroeder 		return -1;
13993f542974SPaul B Schroeder 	}
14003f542974SPaul B Schroeder 
14013f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14023f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
14033f542974SPaul B Schroeder 		dbg("%s \n", "mos7840_break:leaving ...........");
14043f542974SPaul B Schroeder 		return -1;
14053f542974SPaul B Schroeder 	}
14063f542974SPaul B Schroeder 
14070de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
14083f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
14090de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
14103f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
14113f542974SPaul B Schroeder 		}
14123f542974SPaul B Schroeder 	}
14130de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
14143f542974SPaul B Schroeder 
14150de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
14163f542974SPaul B Schroeder 	dbg("%s - returns %d", __FUNCTION__, room);
14170de9a702SOliver Neukum 	return room;
14183f542974SPaul B Schroeder 
14193f542974SPaul B Schroeder }
14203f542974SPaul B Schroeder 
14213f542974SPaul B Schroeder /*****************************************************************************
14223f542974SPaul B Schroeder  * mos7840_write
14233f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
14243f542974SPaul B Schroeder  *	the port.
14253f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
14263f542974SPaul B Schroeder  *      return a negative error number.
14273f542974SPaul B Schroeder  *****************************************************************************/
14283f542974SPaul B Schroeder 
14293f542974SPaul B Schroeder static int mos7840_write(struct usb_serial_port *port,
14303f542974SPaul B Schroeder 			 const unsigned char *data, int count)
14313f542974SPaul B Schroeder {
14323f542974SPaul B Schroeder 	int status;
14333f542974SPaul B Schroeder 	int i;
14343f542974SPaul B Schroeder 	int bytes_sent = 0;
14353f542974SPaul B Schroeder 	int transfer_size;
14360de9a702SOliver Neukum 	unsigned long flags;
14373f542974SPaul B Schroeder 
14383f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14393f542974SPaul B Schroeder 	struct usb_serial *serial;
14403f542974SPaul B Schroeder 	struct urb *urb;
14413f542974SPaul B Schroeder 	//__u16 Data;
14423f542974SPaul B Schroeder 	const unsigned char *current_position = data;
14433f542974SPaul B Schroeder 	unsigned char *data1;
14443f542974SPaul B Schroeder 	dbg("%s \n", "entering ...........");
14453f542974SPaul B Schroeder 	//dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
14463f542974SPaul B Schroeder 
14473f542974SPaul B Schroeder #ifdef NOTMOS7840
14483f542974SPaul B Schroeder 	Data = 0x00;
14493f542974SPaul B Schroeder 	status = 0;
14503f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
14513f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
14523f542974SPaul B Schroeder 	dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
14533f542974SPaul B Schroeder 	dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
14543f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14553f542974SPaul B Schroeder 
14563f542974SPaul B Schroeder 	//Data = 0x03;
14573f542974SPaul B Schroeder 	//status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
14583f542974SPaul B Schroeder 	//mos7840_port->shadowLCR=Data;//Need to add later
14593f542974SPaul B Schroeder 
14603f542974SPaul B Schroeder 	Data |= SERIAL_LCR_DLAB;	//data latch enable in LCR 0x80
14613f542974SPaul B Schroeder 	status = 0;
14623f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14633f542974SPaul B Schroeder 
14643f542974SPaul B Schroeder 	//Data = 0x0c;
14653f542974SPaul B Schroeder 	//status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
14663f542974SPaul B Schroeder 	Data = 0x00;
14673f542974SPaul B Schroeder 	status = 0;
14683f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
14693f542974SPaul B Schroeder 	dbg("mos7840_write:DLL value is %x\n", Data);
14703f542974SPaul B Schroeder 
14713f542974SPaul B Schroeder 	Data = 0x0;
14723f542974SPaul B Schroeder 	status = 0;
14733f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
14743f542974SPaul B Schroeder 	dbg("mos7840_write:DLM value is %x\n", Data);
14753f542974SPaul B Schroeder 
14763f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
14773f542974SPaul B Schroeder 	dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
14783f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14793f542974SPaul B Schroeder 	status = 0;
14803f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14813f542974SPaul B Schroeder #endif
14823f542974SPaul B Schroeder 
14833f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
14843f542974SPaul B Schroeder 		dbg("%s", "Port Paranoia failed \n");
14853f542974SPaul B Schroeder 		return -1;
14863f542974SPaul B Schroeder 	}
14873f542974SPaul B Schroeder 
14883f542974SPaul B Schroeder 	serial = port->serial;
14893f542974SPaul B Schroeder 	if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
14903f542974SPaul B Schroeder 		dbg("%s", "Serial Paranoia failed \n");
14913f542974SPaul B Schroeder 		return -1;
14923f542974SPaul B Schroeder 	}
14933f542974SPaul B Schroeder 
14943f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14953f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
14963f542974SPaul B Schroeder 		dbg("%s", "mos7840_port is NULL\n");
14973f542974SPaul B Schroeder 		return -1;
14983f542974SPaul B Schroeder 	}
14993f542974SPaul B Schroeder 
15003f542974SPaul B Schroeder 	/* try to find a free urb in the list */
15013f542974SPaul B Schroeder 	urb = NULL;
15023f542974SPaul B Schroeder 
15030de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
15043f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
15050de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
15060de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
15073f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
15083f542974SPaul B Schroeder 			dbg("\nURB:%d", i);
15093f542974SPaul B Schroeder 			break;
15103f542974SPaul B Schroeder 		}
15113f542974SPaul B Schroeder 	}
15120de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
15133f542974SPaul B Schroeder 
15143f542974SPaul B Schroeder 	if (urb == NULL) {
15153f542974SPaul B Schroeder 		dbg("%s - no more free urbs", __FUNCTION__);
15163f542974SPaul B Schroeder 		goto exit;
15173f542974SPaul B Schroeder 	}
15183f542974SPaul B Schroeder 
15193f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
15203f542974SPaul B Schroeder 		urb->transfer_buffer =
15213f542974SPaul B Schroeder 		    kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
15223f542974SPaul B Schroeder 
15233f542974SPaul B Schroeder 		if (urb->transfer_buffer == NULL) {
15243f542974SPaul B Schroeder 			err("%s no more kernel memory...", __FUNCTION__);
15253f542974SPaul B Schroeder 			goto exit;
15263f542974SPaul B Schroeder 		}
15273f542974SPaul B Schroeder 	}
15283f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
15293f542974SPaul B Schroeder 
15303f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
15313f542974SPaul B Schroeder 
15323f542974SPaul B Schroeder 	/* fill urb with data and submit  */
15333f542974SPaul B Schroeder 	usb_fill_bulk_urb(urb,
15343f542974SPaul B Schroeder 			  serial->dev,
15353f542974SPaul B Schroeder 			  usb_sndbulkpipe(serial->dev,
15363f542974SPaul B Schroeder 					  port->bulk_out_endpointAddress),
15373f542974SPaul B Schroeder 			  urb->transfer_buffer,
15383f542974SPaul B Schroeder 			  transfer_size,
15393f542974SPaul B Schroeder 			  mos7840_bulk_out_data_callback, mos7840_port);
15403f542974SPaul B Schroeder 
15413f542974SPaul B Schroeder 	data1 = urb->transfer_buffer;
15423f542974SPaul B Schroeder 	dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
15433f542974SPaul B Schroeder 
15443f542974SPaul B Schroeder 	/* send it down the pipe */
15453f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
15463f542974SPaul B Schroeder 
15473f542974SPaul B Schroeder 	if (status) {
15480de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
15493f542974SPaul B Schroeder 		err("%s - usb_submit_urb(write bulk) failed with status = %d",
15503f542974SPaul B Schroeder 		    __FUNCTION__, status);
15513f542974SPaul B Schroeder 		bytes_sent = status;
15523f542974SPaul B Schroeder 		goto exit;
15533f542974SPaul B Schroeder 	}
15543f542974SPaul B Schroeder 	bytes_sent = transfer_size;
15553f542974SPaul B Schroeder 	mos7840_port->icount.tx += transfer_size;
15560de9a702SOliver Neukum 	smp_wmb();
15573f542974SPaul B Schroeder 	dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
15583f542974SPaul B Schroeder       exit:
15593f542974SPaul B Schroeder 
15603f542974SPaul B Schroeder 	return bytes_sent;
15613f542974SPaul B Schroeder 
15623f542974SPaul B Schroeder }
15633f542974SPaul B Schroeder 
15643f542974SPaul B Schroeder /*****************************************************************************
15653f542974SPaul B Schroeder  * mos7840_throttle
15663f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
15673f542974SPaul B Schroeder  *	being read from the port.
15683f542974SPaul B Schroeder  *****************************************************************************/
15693f542974SPaul B Schroeder 
15703f542974SPaul B Schroeder static void mos7840_throttle(struct usb_serial_port *port)
15713f542974SPaul B Schroeder {
15723f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15733f542974SPaul B Schroeder 	struct tty_struct *tty;
15743f542974SPaul B Schroeder 	int status;
15753f542974SPaul B Schroeder 
15763f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
15773f542974SPaul B Schroeder 		dbg("%s", "Invalid port \n");
15783f542974SPaul B Schroeder 		return;
15793f542974SPaul B Schroeder 	}
15803f542974SPaul B Schroeder 
15813f542974SPaul B Schroeder 	dbg("- port %d\n", port->number);
15823f542974SPaul B Schroeder 
15833f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15843f542974SPaul B Schroeder 
15853f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15863f542974SPaul B Schroeder 		return;
15873f542974SPaul B Schroeder 
15883f542974SPaul B Schroeder 	if (!mos7840_port->open) {
15893f542974SPaul B Schroeder 		dbg("%s\n", "port not opened");
15903f542974SPaul B Schroeder 		return;
15913f542974SPaul B Schroeder 	}
15923f542974SPaul B Schroeder 
15933f542974SPaul B Schroeder 	dbg("%s", "Entering .......... \n");
15943f542974SPaul B Schroeder 
15953f542974SPaul B Schroeder 	tty = port->tty;
15963f542974SPaul B Schroeder 	if (!tty) {
15973f542974SPaul B Schroeder 		dbg("%s - no tty available", __FUNCTION__);
15983f542974SPaul B Schroeder 		return;
15993f542974SPaul B Schroeder 	}
16003f542974SPaul B Schroeder 
16013f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
16023f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
16033f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
16043f542974SPaul B Schroeder 		status = mos7840_write(port, &stop_char, 1);
16053f542974SPaul B Schroeder 		if (status <= 0) {
16063f542974SPaul B Schroeder 			return;
16073f542974SPaul B Schroeder 		}
16083f542974SPaul B Schroeder 	}
16093f542974SPaul B Schroeder 
16103f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
16113f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
16123f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
16133f542974SPaul B Schroeder 		status = 0;
16143f542974SPaul B Schroeder 		status =
16153f542974SPaul B Schroeder 		    mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
16163f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
16173f542974SPaul B Schroeder 
16183f542974SPaul B Schroeder 		if (status < 0) {
16193f542974SPaul B Schroeder 			return;
16203f542974SPaul B Schroeder 		}
16213f542974SPaul B Schroeder 	}
16223f542974SPaul B Schroeder 
16233f542974SPaul B Schroeder 	return;
16243f542974SPaul B Schroeder }
16253f542974SPaul B Schroeder 
16263f542974SPaul B Schroeder /*****************************************************************************
16273f542974SPaul B Schroeder  * mos7840_unthrottle
16283f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to resume the data
16293f542974SPaul B Schroeder  *	being read from the port (called after SerialThrottle is called)
16303f542974SPaul B Schroeder  *****************************************************************************/
16313f542974SPaul B Schroeder static void mos7840_unthrottle(struct usb_serial_port *port)
16323f542974SPaul B Schroeder {
16333f542974SPaul B Schroeder 	struct tty_struct *tty;
16343f542974SPaul B Schroeder 	int status;
16353f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
16363f542974SPaul B Schroeder 
16373f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
16383f542974SPaul B Schroeder 		dbg("%s", "Invalid port \n");
16393f542974SPaul B Schroeder 		return;
16403f542974SPaul B Schroeder 	}
16413f542974SPaul B Schroeder 
16423f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16433f542974SPaul B Schroeder 		return;
16443f542974SPaul B Schroeder 
16453f542974SPaul B Schroeder 	if (!mos7840_port->open) {
16463f542974SPaul B Schroeder 		dbg("%s - port not opened", __FUNCTION__);
16473f542974SPaul B Schroeder 		return;
16483f542974SPaul B Schroeder 	}
16493f542974SPaul B Schroeder 
16503f542974SPaul B Schroeder 	dbg("%s", "Entering .......... \n");
16513f542974SPaul B Schroeder 
16523f542974SPaul B Schroeder 	tty = port->tty;
16533f542974SPaul B Schroeder 	if (!tty) {
16543f542974SPaul B Schroeder 		dbg("%s - no tty available", __FUNCTION__);
16553f542974SPaul B Schroeder 		return;
16563f542974SPaul B Schroeder 	}
16573f542974SPaul B Schroeder 
16583f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
16593f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
16603f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
16613f542974SPaul B Schroeder 		status = mos7840_write(port, &start_char, 1);
16623f542974SPaul B Schroeder 		if (status <= 0) {
16633f542974SPaul B Schroeder 			return;
16643f542974SPaul B Schroeder 		}
16653f542974SPaul B Schroeder 	}
16663f542974SPaul B Schroeder 
16673f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
16683f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
16693f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
16703f542974SPaul B Schroeder 		status = 0;
16713f542974SPaul B Schroeder 		status =
16723f542974SPaul B Schroeder 		    mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
16733f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
16743f542974SPaul B Schroeder 		if (status < 0) {
16753f542974SPaul B Schroeder 			return;
16763f542974SPaul B Schroeder 		}
16773f542974SPaul B Schroeder 	}
16783f542974SPaul B Schroeder 
16793f542974SPaul B Schroeder 	return;
16803f542974SPaul B Schroeder }
16813f542974SPaul B Schroeder 
16823f542974SPaul B Schroeder static int mos7840_tiocmget(struct usb_serial_port *port, struct file *file)
16833f542974SPaul B Schroeder {
16843f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
16853f542974SPaul B Schroeder 	unsigned int result;
16863f542974SPaul B Schroeder 	__u16 msr;
16873f542974SPaul B Schroeder 	__u16 mcr;
16883f542974SPaul B Schroeder 	int status = 0;
16893f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
16903f542974SPaul B Schroeder 
16913f542974SPaul B Schroeder 	dbg("%s - port %d", __FUNCTION__, port->number);
16923f542974SPaul B Schroeder 
16933f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16943f542974SPaul B Schroeder 		return -ENODEV;
16953f542974SPaul B Schroeder 
16963f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
16973f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
16983f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
16993f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
17003f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
17013f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
17023f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
17033f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
17043f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
17053f542974SPaul B Schroeder 
17063f542974SPaul B Schroeder 	dbg("%s - 0x%04X", __FUNCTION__, result);
17073f542974SPaul B Schroeder 
17083f542974SPaul B Schroeder 	return result;
17093f542974SPaul B Schroeder }
17103f542974SPaul B Schroeder 
17113f542974SPaul B Schroeder static int mos7840_tiocmset(struct usb_serial_port *port, struct file *file,
17123f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
17133f542974SPaul B Schroeder {
17143f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
17153f542974SPaul B Schroeder 	unsigned int mcr;
17163f542974SPaul B Schroeder 	unsigned int status;
17173f542974SPaul B Schroeder 
17183f542974SPaul B Schroeder 	dbg("%s - port %d", __FUNCTION__, port->number);
17193f542974SPaul B Schroeder 
17203f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
17213f542974SPaul B Schroeder 
17223f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17233f542974SPaul B Schroeder 		return -ENODEV;
17243f542974SPaul B Schroeder 
17253f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
17263f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
17273f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
17283f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
17293f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
17303f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
17313f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
17323f542974SPaul B Schroeder 
17333f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
17343f542974SPaul B Schroeder 		mcr |= MCR_RTS;
17353f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
17363f542974SPaul B Schroeder 		mcr |= MCR_DTR;
17373f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
17383f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
17393f542974SPaul B Schroeder 
17403f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
17413f542974SPaul B Schroeder 
17423f542974SPaul B Schroeder 	status = 0;
17433f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
17443f542974SPaul B Schroeder 	if (status < 0) {
17453f542974SPaul B Schroeder 		dbg("setting MODEM_CONTROL_REGISTER Failed\n");
17463f542974SPaul B Schroeder 		return -1;
17473f542974SPaul B Schroeder 	}
17483f542974SPaul B Schroeder 
17493f542974SPaul B Schroeder 	return 0;
17503f542974SPaul B Schroeder }
17513f542974SPaul B Schroeder 
17523f542974SPaul B Schroeder /*****************************************************************************
17533f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
17543f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
17553f542974SPaul B Schroeder  *	baud rate.
17563f542974SPaul B Schroeder  *****************************************************************************/
17573f542974SPaul B Schroeder static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
17583f542974SPaul B Schroeder 					  __u16 * clk_sel_val)
17593f542974SPaul B Schroeder {
17603f542974SPaul B Schroeder 
17613f542974SPaul B Schroeder 	dbg("%s - %d", __FUNCTION__, baudRate);
17623f542974SPaul B Schroeder 
17633f542974SPaul B Schroeder 	if (baudRate <= 115200) {
17643f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
17653f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
17663f542974SPaul B Schroeder 	}
17673f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
17683f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
17693f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
17703f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
17713f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
17723f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
17733f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
17743f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
17753f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
17763f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
17773f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
17783f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
17793f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
17803f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
17813f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
17823f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
17833f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
17843f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
17853f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
17863f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
17873f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
17883f542974SPaul B Schroeder 	}
17893f542974SPaul B Schroeder 	return 0;
17903f542974SPaul B Schroeder 
17913f542974SPaul B Schroeder #ifdef NOTMCS7840
17923f542974SPaul B Schroeder 
17933f542974SPaul B Schroeder 	for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
17943f542974SPaul B Schroeder 		if (mos7840_divisor_table[i].BaudRate == baudrate) {
17953f542974SPaul B Schroeder 			*divisor = mos7840_divisor_table[i].Divisor;
17963f542974SPaul B Schroeder 			return 0;
17973f542974SPaul B Schroeder 		}
17983f542974SPaul B Schroeder 	}
17993f542974SPaul B Schroeder 
18003f542974SPaul B Schroeder 	/* After trying for all the standard baud rates    *
18013f542974SPaul B Schroeder 	 * Try calculating the divisor for this baud rate  */
18023f542974SPaul B Schroeder 
18033f542974SPaul B Schroeder 	if (baudrate > 75 && baudrate < 230400) {
18043f542974SPaul B Schroeder 		/* get the divisor */
18053f542974SPaul B Schroeder 		custom = (__u16) (230400L / baudrate);
18063f542974SPaul B Schroeder 
18073f542974SPaul B Schroeder 		/* Check for round off */
18083f542974SPaul B Schroeder 		round1 = (__u16) (2304000L / baudrate);
18093f542974SPaul B Schroeder 		round = (__u16) (round1 - (custom * 10));
18103f542974SPaul B Schroeder 		if (round > 4) {
18113f542974SPaul B Schroeder 			custom++;
18123f542974SPaul B Schroeder 		}
18133f542974SPaul B Schroeder 		*divisor = custom;
18143f542974SPaul B Schroeder 
18153f542974SPaul B Schroeder 		dbg(" Baud %d = %d\n", baudrate, custom);
18163f542974SPaul B Schroeder 		return 0;
18173f542974SPaul B Schroeder 	}
18183f542974SPaul B Schroeder 
18193f542974SPaul B Schroeder 	dbg("%s\n", " Baud calculation Failed...");
18203f542974SPaul B Schroeder 	return -1;
18213f542974SPaul B Schroeder #endif
18223f542974SPaul B Schroeder }
18233f542974SPaul B Schroeder 
18243f542974SPaul B Schroeder /*****************************************************************************
18253f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
18263f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
18273f542974SPaul B Schroeder  *	specified port.
18283f542974SPaul B Schroeder  *****************************************************************************/
18293f542974SPaul B Schroeder 
18303f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
18313f542974SPaul B Schroeder 					    int baudRate)
18323f542974SPaul B Schroeder {
18333f542974SPaul B Schroeder 	int divisor = 0;
18343f542974SPaul B Schroeder 	int status;
18353f542974SPaul B Schroeder 	__u16 Data;
18363f542974SPaul B Schroeder 	unsigned char number;
18373f542974SPaul B Schroeder 	__u16 clk_sel_val;
18383f542974SPaul B Schroeder 	struct usb_serial_port *port;
18393f542974SPaul B Schroeder 
18403f542974SPaul B Schroeder 	if (mos7840_port == NULL)
18413f542974SPaul B Schroeder 		return -1;
18423f542974SPaul B Schroeder 
18433f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
18443f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
18453f542974SPaul B Schroeder 		dbg("%s", "Invalid port \n");
18463f542974SPaul B Schroeder 		return -1;
18473f542974SPaul B Schroeder 	}
18483f542974SPaul B Schroeder 
18493f542974SPaul B Schroeder 	if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
18503f542974SPaul B Schroeder 		dbg("%s", "Invalid Serial \n");
18513f542974SPaul B Schroeder 		return -1;
18523f542974SPaul B Schroeder 	}
18533f542974SPaul B Schroeder 
18543f542974SPaul B Schroeder 	dbg("%s", "Entering .......... \n");
18553f542974SPaul B Schroeder 
18563f542974SPaul B Schroeder 	number = mos7840_port->port->number - mos7840_port->port->serial->minor;
18573f542974SPaul B Schroeder 
18583f542974SPaul B Schroeder 	dbg("%s - port = %d, baud = %d", __FUNCTION__,
18593f542974SPaul B Schroeder 	    mos7840_port->port->number, baudRate);
18603f542974SPaul B Schroeder 	//reset clk_uart_sel in spregOffset
18613f542974SPaul B Schroeder 	if (baudRate > 115200) {
18623f542974SPaul B Schroeder #ifdef HW_flow_control
18633f542974SPaul B Schroeder 		//NOTE: need to see the pther register to modify
18643f542974SPaul B Schroeder 		//setting h/w flow control bit to 1;
18653f542974SPaul B Schroeder 		status = 0;
18663f542974SPaul B Schroeder 		Data = 0x2b;
18673f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
18683f542974SPaul B Schroeder 		status =
18693f542974SPaul B Schroeder 		    mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
18703f542974SPaul B Schroeder 		if (status < 0) {
18713f542974SPaul B Schroeder 			dbg("Writing spreg failed in set_serial_baud\n");
18723f542974SPaul B Schroeder 			return -1;
18733f542974SPaul B Schroeder 		}
18743f542974SPaul B Schroeder #endif
18753f542974SPaul B Schroeder 
18763f542974SPaul B Schroeder 	} else {
18773f542974SPaul B Schroeder #ifdef HW_flow_control
18783f542974SPaul B Schroeder 		//setting h/w flow control bit to 0;
18793f542974SPaul B Schroeder 		status = 0;
18803f542974SPaul B Schroeder 		Data = 0xb;
18813f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
18823f542974SPaul B Schroeder 		status =
18833f542974SPaul B Schroeder 		    mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
18843f542974SPaul B Schroeder 		if (status < 0) {
18853f542974SPaul B Schroeder 			dbg("Writing spreg failed in set_serial_baud\n");
18863f542974SPaul B Schroeder 			return -1;
18873f542974SPaul B Schroeder 		}
18883f542974SPaul B Schroeder #endif
18893f542974SPaul B Schroeder 
18903f542974SPaul B Schroeder 	}
18913f542974SPaul B Schroeder 
18923f542974SPaul B Schroeder 	if (1)			//baudRate <= 115200)
18933f542974SPaul B Schroeder 	{
18943f542974SPaul B Schroeder 		clk_sel_val = 0x0;
18953f542974SPaul B Schroeder 		Data = 0x0;
18963f542974SPaul B Schroeder 		status = 0;
18973f542974SPaul B Schroeder 		status =
18983f542974SPaul B Schroeder 		    mos7840_calc_baud_rate_divisor(baudRate, &divisor,
18993f542974SPaul B Schroeder 						   &clk_sel_val);
19003f542974SPaul B Schroeder 		status =
19013f542974SPaul B Schroeder 		    mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
19023f542974SPaul B Schroeder 					 &Data);
19033f542974SPaul B Schroeder 		if (status < 0) {
19043f542974SPaul B Schroeder 			dbg("reading spreg failed in set_serial_baud\n");
19053f542974SPaul B Schroeder 			return -1;
19063f542974SPaul B Schroeder 		}
19073f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
19083f542974SPaul B Schroeder 		status = 0;
19093f542974SPaul B Schroeder 		status =
19103f542974SPaul B Schroeder 		    mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
19113f542974SPaul B Schroeder 		if (status < 0) {
19123f542974SPaul B Schroeder 			dbg("Writing spreg failed in set_serial_baud\n");
19133f542974SPaul B Schroeder 			return -1;
19143f542974SPaul B Schroeder 		}
19153f542974SPaul B Schroeder 		/* Calculate the Divisor */
19163f542974SPaul B Schroeder 
19173f542974SPaul B Schroeder 		if (status) {
19183f542974SPaul B Schroeder 			err("%s - bad baud rate", __FUNCTION__);
19193f542974SPaul B Schroeder 			dbg("%s\n", "bad baud rate");
19203f542974SPaul B Schroeder 			return status;
19213f542974SPaul B Schroeder 		}
19223f542974SPaul B Schroeder 		/* Enable access to divisor latch */
19233f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
19243f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
19253f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19263f542974SPaul B Schroeder 
19273f542974SPaul B Schroeder 		/* Write the divisor */
19283f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
19293f542974SPaul B Schroeder 		dbg("set_serial_baud Value to write DLL is %x\n", Data);
19303f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
19313f542974SPaul B Schroeder 
19323f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
19333f542974SPaul B Schroeder 		dbg("set_serial_baud Value to write DLM is %x\n", Data);
19343f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
19353f542974SPaul B Schroeder 
19363f542974SPaul B Schroeder 		/* Disable access to divisor latch */
19373f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
19383f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
19393f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19403f542974SPaul B Schroeder 
19413f542974SPaul B Schroeder 	}
19423f542974SPaul B Schroeder 
19433f542974SPaul B Schroeder 	return status;
19443f542974SPaul B Schroeder }
19453f542974SPaul B Schroeder 
19463f542974SPaul B Schroeder /*****************************************************************************
19473f542974SPaul B Schroeder  * mos7840_change_port_settings
19483f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
19493f542974SPaul B Schroeder  *      the specified new settings.
19503f542974SPaul B Schroeder  *****************************************************************************/
19513f542974SPaul B Schroeder 
19523f542974SPaul B Schroeder static void mos7840_change_port_settings(struct moschip_port *mos7840_port,
1953606d099cSAlan Cox 					 struct ktermios *old_termios)
19543f542974SPaul B Schroeder {
19553f542974SPaul B Schroeder 	struct tty_struct *tty;
19563f542974SPaul B Schroeder 	int baud;
19573f542974SPaul B Schroeder 	unsigned cflag;
19583f542974SPaul B Schroeder 	unsigned iflag;
19593f542974SPaul B Schroeder 	__u8 lData;
19603f542974SPaul B Schroeder 	__u8 lParity;
19613f542974SPaul B Schroeder 	__u8 lStop;
19623f542974SPaul B Schroeder 	int status;
19633f542974SPaul B Schroeder 	__u16 Data;
19643f542974SPaul B Schroeder 	struct usb_serial_port *port;
19653f542974SPaul B Schroeder 	struct usb_serial *serial;
19663f542974SPaul B Schroeder 
19673f542974SPaul B Schroeder 	if (mos7840_port == NULL)
19683f542974SPaul B Schroeder 		return;
19693f542974SPaul B Schroeder 
19703f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
19713f542974SPaul B Schroeder 
19723f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
19733f542974SPaul B Schroeder 		dbg("%s", "Invalid port \n");
19743f542974SPaul B Schroeder 		return;
19753f542974SPaul B Schroeder 	}
19763f542974SPaul B Schroeder 
19773f542974SPaul B Schroeder 	if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
19783f542974SPaul B Schroeder 		dbg("%s", "Invalid Serial \n");
19793f542974SPaul B Schroeder 		return;
19803f542974SPaul B Schroeder 	}
19813f542974SPaul B Schroeder 
19823f542974SPaul B Schroeder 	serial = port->serial;
19833f542974SPaul B Schroeder 
19843f542974SPaul B Schroeder 	dbg("%s - port %d", __FUNCTION__, mos7840_port->port->number);
19853f542974SPaul B Schroeder 
19863f542974SPaul B Schroeder 	if (!mos7840_port->open) {
19873f542974SPaul B Schroeder 		dbg("%s - port not opened", __FUNCTION__);
19883f542974SPaul B Schroeder 		return;
19893f542974SPaul B Schroeder 	}
19903f542974SPaul B Schroeder 
19913f542974SPaul B Schroeder 	tty = mos7840_port->port->tty;
19923f542974SPaul B Schroeder 
19933f542974SPaul B Schroeder 	dbg("%s", "Entering .......... \n");
19943f542974SPaul B Schroeder 
19953f542974SPaul B Schroeder 	lData = LCR_BITS_8;
19963f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
19973f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
19983f542974SPaul B Schroeder 
19993f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
20003f542974SPaul B Schroeder 	iflag = tty->termios->c_iflag;
20013f542974SPaul B Schroeder 
20023f542974SPaul B Schroeder 	/* Change the number of bits */
20033f542974SPaul B Schroeder 	if (cflag & CSIZE) {
20043f542974SPaul B Schroeder 		switch (cflag & CSIZE) {
20053f542974SPaul B Schroeder 		case CS5:
20063f542974SPaul B Schroeder 			lData = LCR_BITS_5;
20073f542974SPaul B Schroeder 			break;
20083f542974SPaul B Schroeder 
20093f542974SPaul B Schroeder 		case CS6:
20103f542974SPaul B Schroeder 			lData = LCR_BITS_6;
20113f542974SPaul B Schroeder 			break;
20123f542974SPaul B Schroeder 
20133f542974SPaul B Schroeder 		case CS7:
20143f542974SPaul B Schroeder 			lData = LCR_BITS_7;
20153f542974SPaul B Schroeder 			break;
20163f542974SPaul B Schroeder 		default:
20173f542974SPaul B Schroeder 		case CS8:
20183f542974SPaul B Schroeder 			lData = LCR_BITS_8;
20193f542974SPaul B Schroeder 			break;
20203f542974SPaul B Schroeder 		}
20213f542974SPaul B Schroeder 	}
20223f542974SPaul B Schroeder 	/* Change the Parity bit */
20233f542974SPaul B Schroeder 	if (cflag & PARENB) {
20243f542974SPaul B Schroeder 		if (cflag & PARODD) {
20253f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
20263f542974SPaul B Schroeder 			dbg("%s - parity = odd", __FUNCTION__);
20273f542974SPaul B Schroeder 		} else {
20283f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
20293f542974SPaul B Schroeder 			dbg("%s - parity = even", __FUNCTION__);
20303f542974SPaul B Schroeder 		}
20313f542974SPaul B Schroeder 
20323f542974SPaul B Schroeder 	} else {
20333f542974SPaul B Schroeder 		dbg("%s - parity = none", __FUNCTION__);
20343f542974SPaul B Schroeder 	}
20353f542974SPaul B Schroeder 
20363f542974SPaul B Schroeder 	if (cflag & CMSPAR) {
20373f542974SPaul B Schroeder 		lParity = lParity | 0x20;
20383f542974SPaul B Schroeder 	}
20393f542974SPaul B Schroeder 
20403f542974SPaul B Schroeder 	/* Change the Stop bit */
20413f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
20423f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
20433f542974SPaul B Schroeder 		dbg("%s - stop bits = 2", __FUNCTION__);
20443f542974SPaul B Schroeder 	} else {
20453f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
20463f542974SPaul B Schroeder 		dbg("%s - stop bits = 1", __FUNCTION__);
20473f542974SPaul B Schroeder 	}
20483f542974SPaul B Schroeder 
20493f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
20503f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
20513f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
20523f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
20533f542974SPaul B Schroeder 
20543f542974SPaul B Schroeder 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
20553f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
20563f542974SPaul B Schroeder 	/* Disable Interrupts */
20573f542974SPaul B Schroeder 	Data = 0x00;
20583f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
20593f542974SPaul B Schroeder 
20603f542974SPaul B Schroeder 	Data = 0x00;
20613f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
20623f542974SPaul B Schroeder 
20633f542974SPaul B Schroeder 	Data = 0xcf;
20643f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
20653f542974SPaul B Schroeder 
20663f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
20673f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
20683f542974SPaul B Schroeder 
20693f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
20703f542974SPaul B Schroeder 
20713f542974SPaul B Schroeder 	Data = 0x00b;
20723f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
20733f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20743f542974SPaul B Schroeder 	Data = 0x00b;
20753f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20763f542974SPaul B Schroeder 
20773f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
20783f542974SPaul B Schroeder 
20793f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
20803f542974SPaul B Schroeder 	if (cflag & CBAUD) {
20813f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
20823f542974SPaul B Schroeder 	}
20833f542974SPaul B Schroeder 
20843f542974SPaul B Schroeder 	if (cflag & CRTSCTS) {
20853f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
20863f542974SPaul B Schroeder 
20873f542974SPaul B Schroeder 	} else {
20883f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
20893f542974SPaul B Schroeder 	}
20903f542974SPaul B Schroeder 
20913f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
20923f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20933f542974SPaul B Schroeder 
20943f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
20953f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
20963f542974SPaul B Schroeder 
20973f542974SPaul B Schroeder 	if (!baud) {
20983f542974SPaul B Schroeder 		/* pick a default, any default... */
20993f542974SPaul B Schroeder 		dbg("%s\n", "Picked default baud...");
21003f542974SPaul B Schroeder 		baud = 9600;
21013f542974SPaul B Schroeder 	}
21023f542974SPaul B Schroeder 
21033f542974SPaul B Schroeder 	dbg("%s - baud rate = %d", __FUNCTION__, baud);
21043f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
21053f542974SPaul B Schroeder 
21063f542974SPaul B Schroeder 	/* Enable Interrupts */
21073f542974SPaul B Schroeder 	Data = 0x0c;
21083f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
21093f542974SPaul B Schroeder 
21103f542974SPaul B Schroeder 	if (mos7840_port->read_urb->status != -EINPROGRESS) {
21113f542974SPaul B Schroeder 		mos7840_port->read_urb->dev = serial->dev;
21123f542974SPaul B Schroeder 
21133f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
21143f542974SPaul B Schroeder 
21153f542974SPaul B Schroeder 		if (status) {
21163f542974SPaul B Schroeder 			dbg(" usb_submit_urb(read bulk) failed, status = %d",
21173f542974SPaul B Schroeder 			    status);
21183f542974SPaul B Schroeder 		}
21193f542974SPaul B Schroeder 	}
21203f542974SPaul B Schroeder 	wake_up(&mos7840_port->delta_msr_wait);
21213f542974SPaul B Schroeder 	mos7840_port->delta_msr_cond = 1;
21223f542974SPaul B Schroeder 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
21233f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
21243f542974SPaul B Schroeder 
21253f542974SPaul B Schroeder 	return;
21263f542974SPaul B Schroeder }
21273f542974SPaul B Schroeder 
21283f542974SPaul B Schroeder /*****************************************************************************
21293f542974SPaul B Schroeder  * mos7840_set_termios
21303f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
21313f542974SPaul B Schroeder  *	the termios structure
21323f542974SPaul B Schroeder  *****************************************************************************/
21333f542974SPaul B Schroeder 
21343f542974SPaul B Schroeder static void mos7840_set_termios(struct usb_serial_port *port,
2135606d099cSAlan Cox 				struct ktermios *old_termios)
21363f542974SPaul B Schroeder {
21373f542974SPaul B Schroeder 	int status;
21383f542974SPaul B Schroeder 	unsigned int cflag;
21393f542974SPaul B Schroeder 	struct usb_serial *serial;
21403f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
21413f542974SPaul B Schroeder 	struct tty_struct *tty;
21423f542974SPaul B Schroeder 	dbg("mos7840_set_termios: START\n");
21433f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
21443f542974SPaul B Schroeder 		dbg("%s", "Invalid port \n");
21453f542974SPaul B Schroeder 		return;
21463f542974SPaul B Schroeder 	}
21473f542974SPaul B Schroeder 
21483f542974SPaul B Schroeder 	serial = port->serial;
21493f542974SPaul B Schroeder 
21503f542974SPaul B Schroeder 	if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
21513f542974SPaul B Schroeder 		dbg("%s", "Invalid Serial \n");
21523f542974SPaul B Schroeder 		return;
21533f542974SPaul B Schroeder 	}
21543f542974SPaul B Schroeder 
21553f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
21563f542974SPaul B Schroeder 
21573f542974SPaul B Schroeder 	if (mos7840_port == NULL)
21583f542974SPaul B Schroeder 		return;
21593f542974SPaul B Schroeder 
21603f542974SPaul B Schroeder 	tty = port->tty;
21613f542974SPaul B Schroeder 
21623f542974SPaul B Schroeder 	if (!mos7840_port->open) {
21633f542974SPaul B Schroeder 		dbg("%s - port not opened", __FUNCTION__);
21643f542974SPaul B Schroeder 		return;
21653f542974SPaul B Schroeder 	}
21663f542974SPaul B Schroeder 
21673f542974SPaul B Schroeder 	dbg("%s\n", "setting termios - ");
21683f542974SPaul B Schroeder 
21693f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
21703f542974SPaul B Schroeder 
21713f542974SPaul B Schroeder 	dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
21723f542974SPaul B Schroeder 	    tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
21733f542974SPaul B Schroeder 	dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
21743f542974SPaul B Schroeder 	    old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
21753f542974SPaul B Schroeder 	dbg("%s - port %d", __FUNCTION__, port->number);
21763f542974SPaul B Schroeder 
21773f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
21783f542974SPaul B Schroeder 
21793f542974SPaul B Schroeder 	mos7840_change_port_settings(mos7840_port, old_termios);
21803f542974SPaul B Schroeder 
21813f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
21823f542974SPaul B Schroeder 		dbg("%s", "URB KILLED !!!!!\n");
21833f542974SPaul B Schroeder 		return;
21843f542974SPaul B Schroeder 	}
21853f542974SPaul B Schroeder 
21863f542974SPaul B Schroeder 	if (mos7840_port->read_urb->status != -EINPROGRESS) {
21873f542974SPaul B Schroeder 		mos7840_port->read_urb->dev = serial->dev;
21883f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
21893f542974SPaul B Schroeder 		if (status) {
21903f542974SPaul B Schroeder 			dbg(" usb_submit_urb(read bulk) failed, status = %d",
21913f542974SPaul B Schroeder 			    status);
21923f542974SPaul B Schroeder 		}
21933f542974SPaul B Schroeder 	}
21943f542974SPaul B Schroeder 	return;
21953f542974SPaul B Schroeder }
21963f542974SPaul B Schroeder 
21973f542974SPaul B Schroeder /*****************************************************************************
21983f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
21993f542974SPaul B Schroeder  *
22003f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
22013f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
22023f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
22033f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
22043f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
22053f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
22063f542974SPaul B Schroeder  *****************************************************************************/
22073f542974SPaul B Schroeder 
22083f542974SPaul B Schroeder static int mos7840_get_lsr_info(struct moschip_port *mos7840_port,
220997c4965dSAl Viro 				unsigned int __user *value)
22103f542974SPaul B Schroeder {
22113f542974SPaul B Schroeder 	int count;
22123f542974SPaul B Schroeder 	unsigned int result = 0;
22133f542974SPaul B Schroeder 
22143f542974SPaul B Schroeder 	count = mos7840_chars_in_buffer(mos7840_port->port);
22153f542974SPaul B Schroeder 	if (count == 0) {
22163f542974SPaul B Schroeder 		dbg("%s -- Empty", __FUNCTION__);
22173f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
22183f542974SPaul B Schroeder 	}
22193f542974SPaul B Schroeder 
22203f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
22213f542974SPaul B Schroeder 		return -EFAULT;
22223f542974SPaul B Schroeder 	return 0;
22233f542974SPaul B Schroeder }
22243f542974SPaul B Schroeder 
22253f542974SPaul B Schroeder /*****************************************************************************
22263f542974SPaul B Schroeder  * mos7840_set_modem_info
22273f542974SPaul B Schroeder  *      function to set modem info
22283f542974SPaul B Schroeder  *****************************************************************************/
22293f542974SPaul B Schroeder 
22303f542974SPaul B Schroeder static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
223197c4965dSAl Viro 				  unsigned int cmd, unsigned int __user *value)
22323f542974SPaul B Schroeder {
22333f542974SPaul B Schroeder 	unsigned int mcr;
22343f542974SPaul B Schroeder 	unsigned int arg;
22353f542974SPaul B Schroeder 	__u16 Data;
22363f542974SPaul B Schroeder 	int status;
22373f542974SPaul B Schroeder 	struct usb_serial_port *port;
22383f542974SPaul B Schroeder 
22393f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22403f542974SPaul B Schroeder 		return -1;
22413f542974SPaul B Schroeder 
22423f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
22433f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
22443f542974SPaul B Schroeder 		dbg("%s", "Invalid port \n");
22453f542974SPaul B Schroeder 		return -1;
22463f542974SPaul B Schroeder 	}
22473f542974SPaul B Schroeder 
22483f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
22493f542974SPaul B Schroeder 
22503f542974SPaul B Schroeder 	if (copy_from_user(&arg, value, sizeof(int)))
22513f542974SPaul B Schroeder 		return -EFAULT;
22523f542974SPaul B Schroeder 
22533f542974SPaul B Schroeder 	switch (cmd) {
22543f542974SPaul B Schroeder 	case TIOCMBIS:
22553f542974SPaul B Schroeder 		if (arg & TIOCM_RTS)
22563f542974SPaul B Schroeder 			mcr |= MCR_RTS;
22573f542974SPaul B Schroeder 		if (arg & TIOCM_DTR)
22583f542974SPaul B Schroeder 			mcr |= MCR_RTS;
22593f542974SPaul B Schroeder 		if (arg & TIOCM_LOOP)
22603f542974SPaul B Schroeder 			mcr |= MCR_LOOPBACK;
22613f542974SPaul B Schroeder 		break;
22623f542974SPaul B Schroeder 
22633f542974SPaul B Schroeder 	case TIOCMBIC:
22643f542974SPaul B Schroeder 		if (arg & TIOCM_RTS)
22653f542974SPaul B Schroeder 			mcr &= ~MCR_RTS;
22663f542974SPaul B Schroeder 		if (arg & TIOCM_DTR)
22673f542974SPaul B Schroeder 			mcr &= ~MCR_RTS;
22683f542974SPaul B Schroeder 		if (arg & TIOCM_LOOP)
22693f542974SPaul B Schroeder 			mcr &= ~MCR_LOOPBACK;
22703f542974SPaul B Schroeder 		break;
22713f542974SPaul B Schroeder 
22723f542974SPaul B Schroeder 	case TIOCMSET:
22733f542974SPaul B Schroeder 		/* turn off the RTS and DTR and LOOPBACK
22743f542974SPaul B Schroeder 		 * and then only turn on what was asked to */
22753f542974SPaul B Schroeder 		mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
22763f542974SPaul B Schroeder 		mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
22773f542974SPaul B Schroeder 		mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
22783f542974SPaul B Schroeder 		mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
22793f542974SPaul B Schroeder 		break;
22803f542974SPaul B Schroeder 	}
22813f542974SPaul B Schroeder 
22823f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
22833f542974SPaul B Schroeder 
22843f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
22853f542974SPaul B Schroeder 	status = 0;
22863f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
22873f542974SPaul B Schroeder 	if (status < 0) {
22883f542974SPaul B Schroeder 		dbg("setting MODEM_CONTROL_REGISTER Failed\n");
22893f542974SPaul B Schroeder 		return -1;
22903f542974SPaul B Schroeder 	}
22913f542974SPaul B Schroeder 
22923f542974SPaul B Schroeder 	return 0;
22933f542974SPaul B Schroeder }
22943f542974SPaul B Schroeder 
22953f542974SPaul B Schroeder /*****************************************************************************
22963f542974SPaul B Schroeder  * mos7840_get_modem_info
22973f542974SPaul B Schroeder  *      function to get modem info
22983f542974SPaul B Schroeder  *****************************************************************************/
22993f542974SPaul B Schroeder 
23003f542974SPaul B Schroeder static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
230197c4965dSAl Viro 				  unsigned int __user *value)
23023f542974SPaul B Schroeder {
23033f542974SPaul B Schroeder 	unsigned int result = 0;
23043f542974SPaul B Schroeder 	__u16 msr;
23053f542974SPaul B Schroeder 	unsigned int mcr = mos7840_port->shadowMCR;
23063f542974SPaul B Schroeder 	int status = 0;
23073f542974SPaul B Schroeder 	status =
23083f542974SPaul B Schroeder 	    mos7840_get_uart_reg(mos7840_port->port, MODEM_STATUS_REGISTER,
23093f542974SPaul B Schroeder 				 &msr);
23103f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)	/* 0x002 */
23113f542974SPaul B Schroeder 	    |((mcr & MCR_RTS) ? TIOCM_RTS : 0)	/* 0x004 */
23123f542974SPaul B Schroeder 	    |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)	/* 0x020 */
23133f542974SPaul B Schroeder 	    |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)	/* 0x040 */
23143f542974SPaul B Schroeder 	    |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)	/* 0x080 */
23153f542974SPaul B Schroeder 	    |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);	/* 0x100 */
23163f542974SPaul B Schroeder 
23173f542974SPaul B Schroeder 	dbg("%s -- %x", __FUNCTION__, result);
23183f542974SPaul B Schroeder 
23193f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
23203f542974SPaul B Schroeder 		return -EFAULT;
23213f542974SPaul B Schroeder 	return 0;
23223f542974SPaul B Schroeder }
23233f542974SPaul B Schroeder 
23243f542974SPaul B Schroeder /*****************************************************************************
23253f542974SPaul B Schroeder  * mos7840_get_serial_info
23263f542974SPaul B Schroeder  *      function to get information about serial port
23273f542974SPaul B Schroeder  *****************************************************************************/
23283f542974SPaul B Schroeder 
23293f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
233097c4965dSAl Viro 				   struct serial_struct __user *retinfo)
23313f542974SPaul B Schroeder {
23323f542974SPaul B Schroeder 	struct serial_struct tmp;
23333f542974SPaul B Schroeder 
23343f542974SPaul B Schroeder 	if (mos7840_port == NULL)
23353f542974SPaul B Schroeder 		return -1;
23363f542974SPaul B Schroeder 
23373f542974SPaul B Schroeder 	if (!retinfo)
23383f542974SPaul B Schroeder 		return -EFAULT;
23393f542974SPaul B Schroeder 
23403f542974SPaul B Schroeder 	memset(&tmp, 0, sizeof(tmp));
23413f542974SPaul B Schroeder 
23423f542974SPaul B Schroeder 	tmp.type = PORT_16550A;
23433f542974SPaul B Schroeder 	tmp.line = mos7840_port->port->serial->minor;
23443f542974SPaul B Schroeder 	tmp.port = mos7840_port->port->number;
23453f542974SPaul B Schroeder 	tmp.irq = 0;
23463f542974SPaul B Schroeder 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
23473f542974SPaul B Schroeder 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
23483f542974SPaul B Schroeder 	tmp.baud_base = 9600;
23493f542974SPaul B Schroeder 	tmp.close_delay = 5 * HZ;
23503f542974SPaul B Schroeder 	tmp.closing_wait = 30 * HZ;
23513f542974SPaul B Schroeder 
23523f542974SPaul B Schroeder 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
23533f542974SPaul B Schroeder 		return -EFAULT;
23543f542974SPaul B Schroeder 	return 0;
23553f542974SPaul B Schroeder }
23563f542974SPaul B Schroeder 
23573f542974SPaul B Schroeder /*****************************************************************************
23583f542974SPaul B Schroeder  * SerialIoctl
23593f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
23603f542974SPaul B Schroeder  *****************************************************************************/
23613f542974SPaul B Schroeder 
23623f542974SPaul B Schroeder static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
23633f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
23643f542974SPaul B Schroeder {
236597c4965dSAl Viro 	void __user *argp = (void __user *)arg;
23663f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
23673f542974SPaul B Schroeder 	struct tty_struct *tty;
23683f542974SPaul B Schroeder 
23693f542974SPaul B Schroeder 	struct async_icount cnow;
23703f542974SPaul B Schroeder 	struct async_icount cprev;
23713f542974SPaul B Schroeder 	struct serial_icounter_struct icount;
23723f542974SPaul B Schroeder 	int mosret = 0;
23733f542974SPaul B Schroeder 
23743f542974SPaul B Schroeder 	if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
23753f542974SPaul B Schroeder 		dbg("%s", "Invalid port \n");
23763f542974SPaul B Schroeder 		return -1;
23773f542974SPaul B Schroeder 	}
23783f542974SPaul B Schroeder 
23793f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
23803f542974SPaul B Schroeder 
23813f542974SPaul B Schroeder 	if (mos7840_port == NULL)
23823f542974SPaul B Schroeder 		return -1;
23833f542974SPaul B Schroeder 
2384ad18027fSAdrian Bunk 	tty = mos7840_port->port->tty;
2385ad18027fSAdrian Bunk 
23863f542974SPaul B Schroeder 	dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
23873f542974SPaul B Schroeder 
23883f542974SPaul B Schroeder 	switch (cmd) {
23893f542974SPaul B Schroeder 		/* return number of bytes available */
23903f542974SPaul B Schroeder 
23913f542974SPaul B Schroeder 	case TIOCSERGETLSR:
23923f542974SPaul B Schroeder 		dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
239397c4965dSAl Viro 		return mos7840_get_lsr_info(mos7840_port, argp);
23943f542974SPaul B Schroeder 		return 0;
23953f542974SPaul B Schroeder 
23963f542974SPaul B Schroeder 	case TIOCMBIS:
23973f542974SPaul B Schroeder 	case TIOCMBIC:
23983f542974SPaul B Schroeder 	case TIOCMSET:
23993f542974SPaul B Schroeder 		dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
24003f542974SPaul B Schroeder 		    port->number);
24013f542974SPaul B Schroeder 		mosret =
240297c4965dSAl Viro 		    mos7840_set_modem_info(mos7840_port, cmd, argp);
24033f542974SPaul B Schroeder 		return mosret;
24043f542974SPaul B Schroeder 
24053f542974SPaul B Schroeder 	case TIOCMGET:
24063f542974SPaul B Schroeder 		dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
240797c4965dSAl Viro 		return mos7840_get_modem_info(mos7840_port, argp);
24083f542974SPaul B Schroeder 
24093f542974SPaul B Schroeder 	case TIOCGSERIAL:
24103f542974SPaul B Schroeder 		dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
241197c4965dSAl Viro 		return mos7840_get_serial_info(mos7840_port, argp);
24123f542974SPaul B Schroeder 
24133f542974SPaul B Schroeder 	case TIOCSSERIAL:
24143f542974SPaul B Schroeder 		dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
24153f542974SPaul B Schroeder 		break;
24163f542974SPaul B Schroeder 
24173f542974SPaul B Schroeder 	case TIOCMIWAIT:
24183f542974SPaul B Schroeder 		dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
24193f542974SPaul B Schroeder 		cprev = mos7840_port->icount;
24203f542974SPaul B Schroeder 		while (1) {
24213f542974SPaul B Schroeder 			//interruptible_sleep_on(&mos7840_port->delta_msr_wait);
24223f542974SPaul B Schroeder 			mos7840_port->delta_msr_cond = 0;
24233f542974SPaul B Schroeder 			wait_event_interruptible(mos7840_port->delta_msr_wait,
24243f542974SPaul B Schroeder 						 (mos7840_port->
24253f542974SPaul B Schroeder 						  delta_msr_cond == 1));
24263f542974SPaul B Schroeder 
24273f542974SPaul B Schroeder 			/* see if a signal did it */
24283f542974SPaul B Schroeder 			if (signal_pending(current))
24293f542974SPaul B Schroeder 				return -ERESTARTSYS;
24303f542974SPaul B Schroeder 			cnow = mos7840_port->icount;
24310de9a702SOliver Neukum 			smp_rmb();
24323f542974SPaul B Schroeder 			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
24333f542974SPaul B Schroeder 			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
24343f542974SPaul B Schroeder 				return -EIO;	/* no change => error */
24353f542974SPaul B Schroeder 			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
24363f542974SPaul B Schroeder 			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
24373f542974SPaul B Schroeder 			    ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
24383f542974SPaul B Schroeder 			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
24393f542974SPaul B Schroeder 				return 0;
24403f542974SPaul B Schroeder 			}
24413f542974SPaul B Schroeder 			cprev = cnow;
24423f542974SPaul B Schroeder 		}
24433f542974SPaul B Schroeder 		/* NOTREACHED */
24443f542974SPaul B Schroeder 		break;
24453f542974SPaul B Schroeder 
24463f542974SPaul B Schroeder 	case TIOCGICOUNT:
24473f542974SPaul B Schroeder 		cnow = mos7840_port->icount;
24480de9a702SOliver Neukum 		smp_rmb();
24493f542974SPaul B Schroeder 		icount.cts = cnow.cts;
24503f542974SPaul B Schroeder 		icount.dsr = cnow.dsr;
24513f542974SPaul B Schroeder 		icount.rng = cnow.rng;
24523f542974SPaul B Schroeder 		icount.dcd = cnow.dcd;
24533f542974SPaul B Schroeder 		icount.rx = cnow.rx;
24543f542974SPaul B Schroeder 		icount.tx = cnow.tx;
24553f542974SPaul B Schroeder 		icount.frame = cnow.frame;
24563f542974SPaul B Schroeder 		icount.overrun = cnow.overrun;
24573f542974SPaul B Schroeder 		icount.parity = cnow.parity;
24583f542974SPaul B Schroeder 		icount.brk = cnow.brk;
24593f542974SPaul B Schroeder 		icount.buf_overrun = cnow.buf_overrun;
24603f542974SPaul B Schroeder 
24613f542974SPaul B Schroeder 		dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
24623f542974SPaul B Schroeder 		    port->number, icount.rx, icount.tx);
246397c4965dSAl Viro 		if (copy_to_user(argp, &icount, sizeof(icount)))
24643f542974SPaul B Schroeder 			return -EFAULT;
24653f542974SPaul B Schroeder 		return 0;
24663f542974SPaul B Schroeder 
24673f542974SPaul B Schroeder 	case TIOCEXBAUD:
24683f542974SPaul B Schroeder 		return 0;
24693f542974SPaul B Schroeder 	default:
24703f542974SPaul B Schroeder 		break;
24713f542974SPaul B Schroeder 	}
24723f542974SPaul B Schroeder 
24733f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
24743f542974SPaul B Schroeder }
24753f542974SPaul B Schroeder 
24763f542974SPaul B Schroeder static int mos7840_calc_num_ports(struct usb_serial *serial)
24773f542974SPaul B Schroeder {
24780de9a702SOliver Neukum 	int mos7840_num_ports = 0;
24793f542974SPaul B Schroeder 
24803f542974SPaul B Schroeder 	dbg("numberofendpoints: %d \n",
24813f542974SPaul B Schroeder 	    (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
24823f542974SPaul B Schroeder 	dbg("numberofendpoints: %d \n",
24833f542974SPaul B Schroeder 	    (int)serial->interface->altsetting->desc.bNumEndpoints);
24843f542974SPaul B Schroeder 	if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
24850de9a702SOliver Neukum 		mos7840_num_ports = serial->num_ports = 2;
24863f542974SPaul B Schroeder 	} else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
24870de9a702SOliver Neukum 		serial->num_bulk_in = 4;
24880de9a702SOliver Neukum 		serial->num_bulk_out = 4;
24890de9a702SOliver Neukum 		mos7840_num_ports = serial->num_ports = 4;
24903f542974SPaul B Schroeder 	}
24913f542974SPaul B Schroeder 
24923f542974SPaul B Schroeder 	return mos7840_num_ports;
24933f542974SPaul B Schroeder }
24943f542974SPaul B Schroeder 
24953f542974SPaul B Schroeder /****************************************************************************
24963f542974SPaul B Schroeder  * mos7840_startup
24973f542974SPaul B Schroeder  ****************************************************************************/
24983f542974SPaul B Schroeder 
24993f542974SPaul B Schroeder static int mos7840_startup(struct usb_serial *serial)
25003f542974SPaul B Schroeder {
25013f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
25023f542974SPaul B Schroeder 	struct usb_device *dev;
25033f542974SPaul B Schroeder 	int i, status;
25043f542974SPaul B Schroeder 
25053f542974SPaul B Schroeder 	__u16 Data;
25063f542974SPaul B Schroeder 	dbg("%s \n", " mos7840_startup :entering..........");
25073f542974SPaul B Schroeder 
25083f542974SPaul B Schroeder 	if (!serial) {
25093f542974SPaul B Schroeder 		dbg("%s\n", "Invalid Handler");
25103f542974SPaul B Schroeder 		return -1;
25113f542974SPaul B Schroeder 	}
25123f542974SPaul B Schroeder 
25133f542974SPaul B Schroeder 	dev = serial->dev;
25143f542974SPaul B Schroeder 
25153f542974SPaul B Schroeder 	dbg("%s\n", "Entering...");
25163f542974SPaul B Schroeder 
25173f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
25183f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
25193f542974SPaul B Schroeder 
25203f542974SPaul B Schroeder 	/* set up port private structures */
25213f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
25227ac9da10SBurman Yan 		mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
25233f542974SPaul B Schroeder 		if (mos7840_port == NULL) {
25243f542974SPaul B Schroeder 			err("%s - Out of memory", __FUNCTION__);
25250de9a702SOliver Neukum 			status = -ENOMEM;
25260de9a702SOliver Neukum 			i--; /* don't follow NULL pointer cleaning up */
25270de9a702SOliver Neukum 			goto error;
25283f542974SPaul B Schroeder 		}
25293f542974SPaul B Schroeder 
25303f542974SPaul B Schroeder 		/* Initialize all port interrupt end point to port 0 int endpoint *
25313f542974SPaul B Schroeder 		 * Our device has only one interrupt end point comman to all port */
25323f542974SPaul B Schroeder 
25333f542974SPaul B Schroeder 		mos7840_port->port = serial->port[i];
25343f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
25350de9a702SOliver Neukum 		spin_lock_init(&mos7840_port->pool_lock);
25363f542974SPaul B Schroeder 
25373f542974SPaul B Schroeder 		mos7840_port->port_num = ((serial->port[i]->number -
25383f542974SPaul B Schroeder 					   (serial->port[i]->serial->minor)) +
25393f542974SPaul B Schroeder 					  1);
25403f542974SPaul B Schroeder 
25413f542974SPaul B Schroeder 		if (mos7840_port->port_num == 1) {
25423f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x0;
25433f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x1;
25443f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x4;
25453f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
25460de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
25473f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x8;
25483f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x9;
25493f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x16;
25503f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
25510de9a702SOliver Neukum 			   && (serial->num_ports == 2)) {
25523f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
25533f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
25543f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
25553f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 3)
25560de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
25573f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
25583f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
25593f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
25603f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 4)
25610de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
25623f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xc;
25633f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xd;
25643f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x1c;
25653f542974SPaul B Schroeder 		}
25663f542974SPaul B Schroeder 		mos7840_dump_serial_port(mos7840_port);
25673f542974SPaul B Schroeder 
25683f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
25693f542974SPaul B Schroeder 
25703f542974SPaul B Schroeder 		//enable rx_disable bit in control register
25713f542974SPaul B Schroeder 
25723f542974SPaul B Schroeder 		status =
25733f542974SPaul B Schroeder 		    mos7840_get_reg_sync(serial->port[i],
25743f542974SPaul B Schroeder 					 mos7840_port->ControlRegOffset, &Data);
25753f542974SPaul B Schroeder 		if (status < 0) {
25763f542974SPaul B Schroeder 			dbg("Reading ControlReg failed status-0x%x\n", status);
25773f542974SPaul B Schroeder 			break;
25783f542974SPaul B Schroeder 		} else
25793f542974SPaul B Schroeder 			dbg("ControlReg Reading success val is %x, status%d\n",
25803f542974SPaul B Schroeder 			    Data, status);
25813f542974SPaul B Schroeder 		Data |= 0x08;	//setting driver done bit
25823f542974SPaul B Schroeder 		Data |= 0x04;	//sp1_bit to have cts change reflect in modem status reg
25833f542974SPaul B Schroeder 
25843f542974SPaul B Schroeder 		//Data |= 0x20; //rx_disable bit
25853f542974SPaul B Schroeder 		status = 0;
25863f542974SPaul B Schroeder 		status =
25873f542974SPaul B Schroeder 		    mos7840_set_reg_sync(serial->port[i],
25883f542974SPaul B Schroeder 					 mos7840_port->ControlRegOffset, Data);
25893f542974SPaul B Schroeder 		if (status < 0) {
25903f542974SPaul B Schroeder 			dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
25913f542974SPaul B Schroeder 			break;
25923f542974SPaul B Schroeder 		} else
25933f542974SPaul B Schroeder 			dbg("ControlReg Writing success(rx_disable) status%d\n",
25943f542974SPaul B Schroeder 			    status);
25953f542974SPaul B Schroeder 
25963f542974SPaul B Schroeder 		//Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
25973f542974SPaul B Schroeder 		Data = 0x01;
25983f542974SPaul B Schroeder 		status = 0;
25993f542974SPaul B Schroeder 		status =
26003f542974SPaul B Schroeder 		    mos7840_set_reg_sync(serial->port[i],
26013f542974SPaul B Schroeder 					 (__u16) (mos7840_port->DcrRegOffset +
26023f542974SPaul B Schroeder 						  0), Data);
26033f542974SPaul B Schroeder 		if (status < 0) {
26043f542974SPaul B Schroeder 			dbg("Writing DCR0 failed status-0x%x\n", status);
26053f542974SPaul B Schroeder 			break;
26063f542974SPaul B Schroeder 		} else
26073f542974SPaul B Schroeder 			dbg("DCR0 Writing success status%d\n", status);
26083f542974SPaul B Schroeder 
26093f542974SPaul B Schroeder 		Data = 0x05;
26103f542974SPaul B Schroeder 		status = 0;
26113f542974SPaul B Schroeder 		status =
26123f542974SPaul B Schroeder 		    mos7840_set_reg_sync(serial->port[i],
26133f542974SPaul B Schroeder 					 (__u16) (mos7840_port->DcrRegOffset +
26143f542974SPaul B Schroeder 						  1), Data);
26153f542974SPaul B Schroeder 		if (status < 0) {
26163f542974SPaul B Schroeder 			dbg("Writing DCR1 failed status-0x%x\n", status);
26173f542974SPaul B Schroeder 			break;
26183f542974SPaul B Schroeder 		} else
26193f542974SPaul B Schroeder 			dbg("DCR1 Writing success status%d\n", status);
26203f542974SPaul B Schroeder 
26213f542974SPaul B Schroeder 		Data = 0x24;
26223f542974SPaul B Schroeder 		status = 0;
26233f542974SPaul B Schroeder 		status =
26243f542974SPaul B Schroeder 		    mos7840_set_reg_sync(serial->port[i],
26253f542974SPaul B Schroeder 					 (__u16) (mos7840_port->DcrRegOffset +
26263f542974SPaul B Schroeder 						  2), Data);
26273f542974SPaul B Schroeder 		if (status < 0) {
26283f542974SPaul B Schroeder 			dbg("Writing DCR2 failed status-0x%x\n", status);
26293f542974SPaul B Schroeder 			break;
26303f542974SPaul B Schroeder 		} else
26313f542974SPaul B Schroeder 			dbg("DCR2 Writing success status%d\n", status);
26323f542974SPaul B Schroeder 
26333f542974SPaul B Schroeder 		// write values in clkstart0x0 and clkmulti 0x20
26343f542974SPaul B Schroeder 		Data = 0x0;
26353f542974SPaul B Schroeder 		status = 0;
26363f542974SPaul B Schroeder 		status =
26373f542974SPaul B Schroeder 		    mos7840_set_reg_sync(serial->port[i],
26383f542974SPaul B Schroeder 					 CLK_START_VALUE_REGISTER, Data);
26393f542974SPaul B Schroeder 		if (status < 0) {
26403f542974SPaul B Schroeder 			dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
26413f542974SPaul B Schroeder 			break;
26423f542974SPaul B Schroeder 		} else
26433f542974SPaul B Schroeder 			dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
26443f542974SPaul B Schroeder 
26453f542974SPaul B Schroeder 		Data = 0x20;
26463f542974SPaul B Schroeder 		status =
26473f542974SPaul B Schroeder 		    mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
26483f542974SPaul B Schroeder 					 Data);
26493f542974SPaul B Schroeder 		if (status < 0) {
26503f542974SPaul B Schroeder 			dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
26513f542974SPaul B Schroeder 			    status);
26520de9a702SOliver Neukum 			goto error;
26533f542974SPaul B Schroeder 		} else
26543f542974SPaul B Schroeder 			dbg("CLK_MULTI_REGISTER Writing success status%d\n",
26553f542974SPaul B Schroeder 			    status);
26563f542974SPaul B Schroeder 
26573f542974SPaul B Schroeder 		//write value 0x0 to scratchpad register
26583f542974SPaul B Schroeder 		Data = 0x00;
26593f542974SPaul B Schroeder 		status =
26603f542974SPaul B Schroeder 		    mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
26613f542974SPaul B Schroeder 					 Data);
26623f542974SPaul B Schroeder 		if (status < 0) {
26633f542974SPaul B Schroeder 			dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
26643f542974SPaul B Schroeder 			    status);
26653f542974SPaul B Schroeder 			break;
26663f542974SPaul B Schroeder 		} else
26673f542974SPaul B Schroeder 			dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
26683f542974SPaul B Schroeder 			    status);
26693f542974SPaul B Schroeder 
26703f542974SPaul B Schroeder 		//Zero Length flag register
26713f542974SPaul B Schroeder 		if ((mos7840_port->port_num != 1)
26720de9a702SOliver Neukum 		    && (serial->num_ports == 2)) {
26733f542974SPaul B Schroeder 
26743f542974SPaul B Schroeder 			Data = 0xff;
26753f542974SPaul B Schroeder 			status = 0;
26763f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
26773f542974SPaul B Schroeder 						      (__u16) (ZLP_REG1 +
26783f542974SPaul B Schroeder 							       ((__u16)
26793f542974SPaul B Schroeder 								mos7840_port->
26803f542974SPaul B Schroeder 								port_num)),
26813f542974SPaul B Schroeder 						      Data);
26823f542974SPaul B Schroeder 			dbg("ZLIP offset%x\n",
26833f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
26843f542974SPaul B Schroeder 				     ((__u16) mos7840_port->port_num)));
26853f542974SPaul B Schroeder 			if (status < 0) {
26863f542974SPaul B Schroeder 				dbg("Writing ZLP_REG%d failed status-0x%x\n",
26873f542974SPaul B Schroeder 				    i + 2, status);
26883f542974SPaul B Schroeder 				break;
26893f542974SPaul B Schroeder 			} else
26903f542974SPaul B Schroeder 				dbg("ZLP_REG%d Writing success status%d\n",
26913f542974SPaul B Schroeder 				    i + 2, status);
26923f542974SPaul B Schroeder 		} else {
26933f542974SPaul B Schroeder 			Data = 0xff;
26943f542974SPaul B Schroeder 			status = 0;
26953f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
26963f542974SPaul B Schroeder 						      (__u16) (ZLP_REG1 +
26973f542974SPaul B Schroeder 							       ((__u16)
26983f542974SPaul B Schroeder 								mos7840_port->
26993f542974SPaul B Schroeder 								port_num) -
27003f542974SPaul B Schroeder 							       0x1), Data);
27013f542974SPaul B Schroeder 			dbg("ZLIP offset%x\n",
27023f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
27033f542974SPaul B Schroeder 				     ((__u16) mos7840_port->port_num) - 0x1));
27043f542974SPaul B Schroeder 			if (status < 0) {
27053f542974SPaul B Schroeder 				dbg("Writing ZLP_REG%d failed status-0x%x\n",
27063f542974SPaul B Schroeder 				    i + 1, status);
27073f542974SPaul B Schroeder 				break;
27083f542974SPaul B Schroeder 			} else
27093f542974SPaul B Schroeder 				dbg("ZLP_REG%d Writing success status%d\n",
27103f542974SPaul B Schroeder 				    i + 1, status);
27113f542974SPaul B Schroeder 
27123f542974SPaul B Schroeder 		}
27130de9a702SOliver Neukum 		mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
27143f542974SPaul B Schroeder 		mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
27150de9a702SOliver Neukum 		mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
27160de9a702SOliver Neukum 		if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || !mos7840_port->dr) {
27170de9a702SOliver Neukum 			status = -ENOMEM;
27180de9a702SOliver Neukum 			goto error;
27190de9a702SOliver Neukum 		}
27203f542974SPaul B Schroeder 	}
27213f542974SPaul B Schroeder 
27223f542974SPaul B Schroeder 	//Zero Length flag enable
27233f542974SPaul B Schroeder 	Data = 0x0f;
27243f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
27253f542974SPaul B Schroeder 	if (status < 0) {
27263f542974SPaul B Schroeder 		dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
27277ced46c3SRoel Kluin 		goto error;
27283f542974SPaul B Schroeder 	} else
27293f542974SPaul B Schroeder 		dbg("ZLP_REG5 Writing success status%d\n", status);
27303f542974SPaul B Schroeder 
27313f542974SPaul B Schroeder 	/* setting configuration feature to one */
27323f542974SPaul B Schroeder 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
273397c4965dSAl Viro 			(__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
27343f542974SPaul B Schroeder 	return 0;
27350de9a702SOliver Neukum error:
27360de9a702SOliver Neukum 	for (/* nothing */; i >= 0; i--) {
27370de9a702SOliver Neukum 		mos7840_port = mos7840_get_port_private(serial->port[i]);
27380de9a702SOliver Neukum 
27390de9a702SOliver Neukum 		kfree(mos7840_port->dr);
27400de9a702SOliver Neukum 		kfree(mos7840_port->ctrl_buf);
27410de9a702SOliver Neukum 		usb_free_urb(mos7840_port->control_urb);
27420de9a702SOliver Neukum 		kfree(mos7840_port);
27430de9a702SOliver Neukum 		serial->port[i] = NULL;
27440de9a702SOliver Neukum 	}
27450de9a702SOliver Neukum 	return status;
27463f542974SPaul B Schroeder }
27473f542974SPaul B Schroeder 
27483f542974SPaul B Schroeder /****************************************************************************
27493f542974SPaul B Schroeder  * mos7840_shutdown
27503f542974SPaul B Schroeder  *	This function is called whenever the device is removed from the usb bus.
27513f542974SPaul B Schroeder  ****************************************************************************/
27523f542974SPaul B Schroeder 
27533f542974SPaul B Schroeder static void mos7840_shutdown(struct usb_serial *serial)
27543f542974SPaul B Schroeder {
27553f542974SPaul B Schroeder 	int i;
27560de9a702SOliver Neukum 	unsigned long flags;
27573f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
27583f542974SPaul B Schroeder 	dbg("%s \n", " shutdown :entering..........");
27593f542974SPaul B Schroeder 
27603f542974SPaul B Schroeder 	if (!serial) {
27613f542974SPaul B Schroeder 		dbg("%s", "Invalid Handler \n");
27623f542974SPaul B Schroeder 		return;
27633f542974SPaul B Schroeder 	}
27643f542974SPaul B Schroeder 
27653f542974SPaul B Schroeder 	/*      check for the ports to be closed,close the ports and disconnect         */
27663f542974SPaul B Schroeder 
27673f542974SPaul B Schroeder 	/* free private structure allocated for serial port  *
27683f542974SPaul B Schroeder 	 * stop reads and writes on all ports                */
27693f542974SPaul B Schroeder 
27703f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
27713f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
27720de9a702SOliver Neukum 		spin_lock_irqsave(&mos7840_port->pool_lock, flags);
27730de9a702SOliver Neukum 		mos7840_port->zombie = 1;
27740de9a702SOliver Neukum 		spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
27753f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->control_urb);
27760de9a702SOliver Neukum 		kfree(mos7840_port->ctrl_buf);
27770de9a702SOliver Neukum 		kfree(mos7840_port->dr);
27783f542974SPaul B Schroeder 		kfree(mos7840_port);
27793f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], NULL);
27803f542974SPaul B Schroeder 	}
27813f542974SPaul B Schroeder 
27823f542974SPaul B Schroeder 	dbg("%s\n", "Thank u :: ");
27833f542974SPaul B Schroeder 
27843f542974SPaul B Schroeder }
27853f542974SPaul B Schroeder 
2786d9b1b787SJohannes Hölzl static struct usb_driver io_driver = {
2787d9b1b787SJohannes Hölzl 	.name = "mos7840",
2788d9b1b787SJohannes Hölzl 	.probe = usb_serial_probe,
2789d9b1b787SJohannes Hölzl 	.disconnect = usb_serial_disconnect,
2790d9b1b787SJohannes Hölzl 	.id_table = moschip_id_table_combined,
2791d9b1b787SJohannes Hölzl 	.no_dynamic_id = 1,
2792d9b1b787SJohannes Hölzl };
2793d9b1b787SJohannes Hölzl 
27943f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
27953f542974SPaul B Schroeder 	.driver = {
27963f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
27973f542974SPaul B Schroeder 		   .name = "mos7840",
27983f542974SPaul B Schroeder 		   },
27993f542974SPaul B Schroeder 	.description = DRIVER_DESC,
2800d9b1b787SJohannes Hölzl 	.usb_driver = &io_driver,
28013f542974SPaul B Schroeder 	.id_table = moschip_port_id_table,
28023f542974SPaul B Schroeder 	.num_interrupt_in = 1,	//NUM_DONT_CARE,//1,
28033f542974SPaul B Schroeder #ifdef check
28043f542974SPaul B Schroeder 	.num_bulk_in = 4,
28053f542974SPaul B Schroeder 	.num_bulk_out = 4,
28063f542974SPaul B Schroeder 	.num_ports = 4,
28073f542974SPaul B Schroeder #endif
28083f542974SPaul B Schroeder 	.open = mos7840_open,
28093f542974SPaul B Schroeder 	.close = mos7840_close,
28103f542974SPaul B Schroeder 	.write = mos7840_write,
28113f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
28123f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
28133f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
28143f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
28153f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
28163f542974SPaul B Schroeder #ifdef MCSSerialProbe
28173f542974SPaul B Schroeder 	.probe = mos7840_serial_probe,
28183f542974SPaul B Schroeder #endif
28193f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
28203f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
28213f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
28223f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
28233f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
28243f542974SPaul B Schroeder 	.attach = mos7840_startup,
28253f542974SPaul B Schroeder 	.shutdown = mos7840_shutdown,
28263f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
28273f542974SPaul B Schroeder 	.read_int_callback = mos7840_interrupt_callback,
28283f542974SPaul B Schroeder };
28293f542974SPaul B Schroeder 
28303f542974SPaul B Schroeder /****************************************************************************
28313f542974SPaul B Schroeder  * moschip7840_init
28323f542974SPaul B Schroeder  *	This is called by the module subsystem, or on startup to initialize us
28333f542974SPaul B Schroeder  ****************************************************************************/
28343f542974SPaul B Schroeder static int __init moschip7840_init(void)
28353f542974SPaul B Schroeder {
28363f542974SPaul B Schroeder 	int retval;
28373f542974SPaul B Schroeder 
28383f542974SPaul B Schroeder 	dbg("%s \n", " mos7840_init :entering..........");
28393f542974SPaul B Schroeder 
28403f542974SPaul B Schroeder 	/* Register with the usb serial */
28413f542974SPaul B Schroeder 	retval = usb_serial_register(&moschip7840_4port_device);
28423f542974SPaul B Schroeder 
28433f542974SPaul B Schroeder 	if (retval)
28443f542974SPaul B Schroeder 		goto failed_port_device_register;
28453f542974SPaul B Schroeder 
28463f542974SPaul B Schroeder 	dbg("%s\n", "Entring...");
28473f542974SPaul B Schroeder 	info(DRIVER_DESC " " DRIVER_VERSION);
28483f542974SPaul B Schroeder 
28493f542974SPaul B Schroeder 	/* Register with the usb */
28503f542974SPaul B Schroeder 	retval = usb_register(&io_driver);
28513f542974SPaul B Schroeder 
28523f542974SPaul B Schroeder 	if (retval)
28533f542974SPaul B Schroeder 		goto failed_usb_register;
28543f542974SPaul B Schroeder 
28553f542974SPaul B Schroeder 	if (retval == 0) {
28563f542974SPaul B Schroeder 		dbg("%s\n", "Leaving...");
28573f542974SPaul B Schroeder 		return 0;
28583f542974SPaul B Schroeder 	}
28593f542974SPaul B Schroeder 
28603f542974SPaul B Schroeder       failed_usb_register:
28613f542974SPaul B Schroeder 	usb_serial_deregister(&moschip7840_4port_device);
28623f542974SPaul B Schroeder 
28633f542974SPaul B Schroeder       failed_port_device_register:
28643f542974SPaul B Schroeder 
28653f542974SPaul B Schroeder 	return retval;
28663f542974SPaul B Schroeder }
28673f542974SPaul B Schroeder 
28683f542974SPaul B Schroeder /****************************************************************************
28693f542974SPaul B Schroeder  * moschip7840_exit
28703f542974SPaul B Schroeder  *	Called when the driver is about to be unloaded.
28713f542974SPaul B Schroeder  ****************************************************************************/
28723f542974SPaul B Schroeder static void __exit moschip7840_exit(void)
28733f542974SPaul B Schroeder {
28743f542974SPaul B Schroeder 
28753f542974SPaul B Schroeder 	dbg("%s \n", " mos7840_exit :entering..........");
28763f542974SPaul B Schroeder 
28773f542974SPaul B Schroeder 	usb_deregister(&io_driver);
28783f542974SPaul B Schroeder 
28793f542974SPaul B Schroeder 	usb_serial_deregister(&moschip7840_4port_device);
28803f542974SPaul B Schroeder 
28813f542974SPaul B Schroeder 	dbg("%s\n", "Entring...");
28823f542974SPaul B Schroeder }
28833f542974SPaul B Schroeder 
28843f542974SPaul B Schroeder module_init(moschip7840_init);
28853f542974SPaul B Schroeder module_exit(moschip7840_exit);
28863f542974SPaul B Schroeder 
28873f542974SPaul B Schroeder /* Module information */
28883f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
28893f542974SPaul B Schroeder MODULE_LICENSE("GPL");
28903f542974SPaul B Schroeder 
28913f542974SPaul B Schroeder module_param(debug, bool, S_IRUGO | S_IWUSR);
28923f542974SPaul B Schroeder MODULE_PARM_DESC(debug, "Debug enabled or not");
2893