xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision 0eafe4de1a689b69d3f0ce0b5d4aa5333208fd4a)
13f542974SPaul B Schroeder /*
23f542974SPaul B Schroeder  * This program is free software; you can redistribute it and/or modify
33f542974SPaul B Schroeder  * it under the terms of the GNU General Public License as published by
43f542974SPaul B Schroeder  * the Free Software Foundation; either version 2 of the License, or
53f542974SPaul B Schroeder  * (at your option) any later version.
63f542974SPaul B Schroeder  *
73f542974SPaul B Schroeder  * This program is distributed in the hope that it will be useful,
83f542974SPaul B Schroeder  * but WITHOUT ANY WARRANTY; without even the implied warranty of
93f542974SPaul B Schroeder  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
103f542974SPaul B Schroeder  * GNU General Public License for more details.
113f542974SPaul B Schroeder  *
123f542974SPaul B Schroeder  * You should have received a copy of the GNU General Public License
133f542974SPaul B Schroeder  * along with this program; if not, write to the Free Software
143f542974SPaul B Schroeder  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
153f542974SPaul B Schroeder  *
163f542974SPaul B Schroeder  * Clean ups from Moschip version and a few ioctl implementations by:
173f542974SPaul B Schroeder  *	Paul B Schroeder <pschroeder "at" uplogix "dot" com>
183f542974SPaul B Schroeder  *
193f542974SPaul B Schroeder  * Originally based on drivers/usb/serial/io_edgeport.c which is:
203f542974SPaul B Schroeder  *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
213f542974SPaul B Schroeder  *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
223f542974SPaul B Schroeder  *
233f542974SPaul B Schroeder  */
243f542974SPaul B Schroeder 
253f542974SPaul B Schroeder #include <linux/kernel.h>
263f542974SPaul B Schroeder #include <linux/errno.h>
273f542974SPaul B Schroeder #include <linux/init.h>
283f542974SPaul B Schroeder #include <linux/slab.h>
293f542974SPaul B Schroeder #include <linux/tty.h>
303f542974SPaul B Schroeder #include <linux/tty_driver.h>
313f542974SPaul B Schroeder #include <linux/tty_flip.h>
323f542974SPaul B Schroeder #include <linux/module.h>
333f542974SPaul B Schroeder #include <linux/serial.h>
343f542974SPaul B Schroeder #include <linux/usb.h>
353f542974SPaul B Schroeder #include <linux/usb/serial.h>
36880af9dbSAlan Cox #include <linux/uaccess.h>
373f542974SPaul B Schroeder 
383f542974SPaul B Schroeder /*
393f542974SPaul B Schroeder  * Version Information
403f542974SPaul B Schroeder  */
4137768adfSTony Cook #define DRIVER_VERSION "1.3.2"
423f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
433f542974SPaul B Schroeder 
443f542974SPaul B Schroeder /*
453f542974SPaul B Schroeder  * 16C50 UART register defines
463f542974SPaul B Schroeder  */
473f542974SPaul B Schroeder 
483f542974SPaul B Schroeder #define LCR_BITS_5             0x00	/* 5 bits/char */
493f542974SPaul B Schroeder #define LCR_BITS_6             0x01	/* 6 bits/char */
503f542974SPaul B Schroeder #define LCR_BITS_7             0x02	/* 7 bits/char */
513f542974SPaul B Schroeder #define LCR_BITS_8             0x03	/* 8 bits/char */
523f542974SPaul B Schroeder #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
533f542974SPaul B Schroeder 
543f542974SPaul B Schroeder #define LCR_STOP_1             0x00	/* 1 stop bit */
553f542974SPaul B Schroeder #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
563f542974SPaul B Schroeder #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
573f542974SPaul B Schroeder #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
583f542974SPaul B Schroeder 
593f542974SPaul B Schroeder #define LCR_PAR_NONE           0x00	/* No parity */
603f542974SPaul B Schroeder #define LCR_PAR_ODD            0x08	/* Odd parity */
613f542974SPaul B Schroeder #define LCR_PAR_EVEN           0x18	/* Even parity */
623f542974SPaul B Schroeder #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
633f542974SPaul B Schroeder #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
643f542974SPaul B Schroeder #define LCR_PAR_MASK           0x38	/* Mask for parity field */
653f542974SPaul B Schroeder 
663f542974SPaul B Schroeder #define LCR_SET_BREAK          0x40	/* Set Break condition */
673f542974SPaul B Schroeder #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
683f542974SPaul B Schroeder 
693f542974SPaul B Schroeder #define MCR_DTR                0x01	/* Assert DTR */
703f542974SPaul B Schroeder #define MCR_RTS                0x02	/* Assert RTS */
713f542974SPaul B Schroeder #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
723f542974SPaul B Schroeder #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
733f542974SPaul B Schroeder #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
743f542974SPaul B Schroeder #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
753f542974SPaul B Schroeder 
763f542974SPaul B Schroeder #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
773f542974SPaul B Schroeder #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
783f542974SPaul B Schroeder #define MOS7840_MSR_RI         0x40	/* Current state of RI */
793f542974SPaul B Schroeder #define MOS7840_MSR_CD         0x80	/* Current state of CD */
803f542974SPaul B Schroeder 
813f542974SPaul B Schroeder /*
823f542974SPaul B Schroeder  * Defines used for sending commands to port
833f542974SPaul B Schroeder  */
843f542974SPaul B Schroeder 
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 
105880af9dbSAlan Cox #define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
106880af9dbSAlan Cox #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 
11311e1abb4SDavid 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*0eafe4deSDonald #define MOSCHIP_DEVICE_ID_7810          0x7810
11811e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden
11911e1abb4SDavid Ludlow  * in vendor-specific implementations.  Such devices can be handled
12011e1abb4SDavid Ludlow  * by making a change here, in moschip_port_id_table, and in
12111e1abb4SDavid Ludlow  * moschip_id_table_combined
12211e1abb4SDavid Ludlow  */
12311e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB              0x0856
124acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
125870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
126acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
127870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
128acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
129acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
130acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
131acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
13211e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
133caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
134870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
135870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
136870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL2_4        0xAC24
1373f542974SPaul B Schroeder 
1389d498beaSRussell Lang /* This driver also supports
1399d498beaSRussell Lang  * ATEN UC2324 device using Moschip MCS7840
1409d498beaSRussell Lang  * ATEN UC2322 device using Moschip MCS7820
1419d498beaSRussell Lang  */
142e9b8cffaSTony Cook #define USB_VENDOR_ID_ATENINTL		0x0557
143e9b8cffaSTony Cook #define ATENINTL_DEVICE_ID_UC2324	0x2011
1449d498beaSRussell Lang #define ATENINTL_DEVICE_ID_UC2322	0x7820
145e9b8cffaSTony Cook 
14611e1abb4SDavid Ludlow /* Interrupt Routine Defines    */
1473f542974SPaul B Schroeder 
1483f542974SPaul B Schroeder #define SERIAL_IIR_RLS      0x06
1493f542974SPaul B Schroeder #define SERIAL_IIR_MS       0x00
1503f542974SPaul B Schroeder 
1513f542974SPaul B Schroeder /*
1523f542974SPaul B Schroeder  *  Emulation of the bit mask on the LINE STATUS REGISTER.
1533f542974SPaul B Schroeder  */
1543f542974SPaul B Schroeder #define SERIAL_LSR_DR       0x0001
1553f542974SPaul B Schroeder #define SERIAL_LSR_OE       0x0002
1563f542974SPaul B Schroeder #define SERIAL_LSR_PE       0x0004
1573f542974SPaul B Schroeder #define SERIAL_LSR_FE       0x0008
1583f542974SPaul B Schroeder #define SERIAL_LSR_BI       0x0010
1593f542974SPaul B Schroeder 
1603f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS   0x10
1613f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR   0x20
1623f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI    0x40
1633f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD    0x80
1643f542974SPaul B Schroeder 
165880af9dbSAlan Cox /* Serial Port register Address */
1663f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
1673f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
1683f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
1693f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
1703f542974SPaul B Schroeder #define LINE_STATUS_REGISTER       ((__u16)(0x05))
1713f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
1723f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
1733f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
1743f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
1753f542974SPaul B Schroeder 
1763f542974SPaul B Schroeder #define CLK_MULTI_REGISTER         ((__u16)(0x02))
1773f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
178093ea2d3SDonald Lee #define GPIO_REGISTER              ((__u16)(0x07))
1793f542974SPaul B Schroeder 
1803f542974SPaul B Schroeder #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
1813f542974SPaul B Schroeder 
1823f542974SPaul B Schroeder /*
1833f542974SPaul B Schroeder  * URB POOL related defines
1843f542974SPaul B Schroeder  */
1853f542974SPaul B Schroeder #define NUM_URBS                        16	/* URB Count */
1863f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
1873f542974SPaul B Schroeder 
188*0eafe4deSDonald /* LED on/off milliseconds*/
189*0eafe4deSDonald #define LED_ON_MS	500
190*0eafe4deSDonald #define LED_OFF_MS	500
191*0eafe4deSDonald 
192*0eafe4deSDonald static int device_type;
1933f542974SPaul B Schroeder 
1947d40d7e8SNémeth Márton static const struct usb_device_id moschip_port_id_table[] = {
1953f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
1963f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
197*0eafe4deSDonald 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
198acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
199870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
200acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
201870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
202acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
203acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
204acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
205acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
20611e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
207870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
208acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
209870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
21027f1281dSBlaise Gassend 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
211e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
2129d498beaSRussell Lang 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
2133f542974SPaul B Schroeder 	{}			/* terminating entry */
2143f542974SPaul B Schroeder };
2153f542974SPaul B Schroeder 
2167d40d7e8SNémeth Márton static const struct usb_device_id moschip_id_table_combined[] __devinitconst = {
2173f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
2183f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
219*0eafe4deSDonald 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
220acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
221870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
222acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
223870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
224acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
225acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
226acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
227acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
22811e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
229870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
230acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
231870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
23227f1281dSBlaise Gassend 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
233e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
2349d498beaSRussell Lang 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
2353f542974SPaul B Schroeder 	{}			/* terminating entry */
2363f542974SPaul B Schroeder };
2373f542974SPaul B Schroeder 
2383f542974SPaul B Schroeder MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
2393f542974SPaul B Schroeder 
2403f542974SPaul B Schroeder /* This structure holds all of the local port information */
2413f542974SPaul B Schroeder 
2423f542974SPaul B Schroeder struct moschip_port {
2433f542974SPaul B Schroeder 	int port_num;		/*Actual port number in the device(1,2,etc) */
2443f542974SPaul B Schroeder 	struct urb *write_urb;	/* write URB for this port */
2453f542974SPaul B Schroeder 	struct urb *read_urb;	/* read URB for this port */
2460de9a702SOliver Neukum 	struct urb *int_urb;
2473f542974SPaul B Schroeder 	__u8 shadowLCR;		/* last LCR value received */
2483f542974SPaul B Schroeder 	__u8 shadowMCR;		/* last MCR value received */
2493f542974SPaul B Schroeder 	char open;
2500de9a702SOliver Neukum 	char open_ports;
2510de9a702SOliver Neukum 	char zombie;
2523f542974SPaul B Schroeder 	wait_queue_head_t wait_chase;	/* for handling sleeping while waiting for chase to finish */
2533f542974SPaul B Schroeder 	wait_queue_head_t delta_msr_wait;	/* for handling sleeping while waiting for msr change to happen */
2543f542974SPaul B Schroeder 	int delta_msr_cond;
2553f542974SPaul B Schroeder 	struct async_icount icount;
2563f542974SPaul B Schroeder 	struct usb_serial_port *port;	/* loop back to the owner of this object */
2573f542974SPaul B Schroeder 
2583f542974SPaul B Schroeder 	/* Offsets */
2593f542974SPaul B Schroeder 	__u8 SpRegOffset;
2603f542974SPaul B Schroeder 	__u8 ControlRegOffset;
2613f542974SPaul B Schroeder 	__u8 DcrRegOffset;
262880af9dbSAlan Cox 	/* for processing control URBS in interrupt context */
2633f542974SPaul B Schroeder 	struct urb *control_urb;
2640de9a702SOliver Neukum 	struct usb_ctrlrequest *dr;
2653f542974SPaul B Schroeder 	char *ctrl_buf;
2663f542974SPaul B Schroeder 	int MsrLsr;
2673f542974SPaul B Schroeder 
2680de9a702SOliver Neukum 	spinlock_t pool_lock;
2693f542974SPaul B Schroeder 	struct urb *write_urb_pool[NUM_URBS];
2700de9a702SOliver Neukum 	char busy[NUM_URBS];
27150de36f7SGreg Kroah-Hartman 	bool read_urb_busy;
2723f542974SPaul B Schroeder 
273*0eafe4deSDonald 	/* For device(s) with LED indicator */
274*0eafe4deSDonald 	bool has_led;
275*0eafe4deSDonald 	bool led_flag;
276*0eafe4deSDonald 	struct timer_list led_timer1;	/* Timer for LED on */
277*0eafe4deSDonald 	struct timer_list led_timer2;	/* Timer for LED off */
278*0eafe4deSDonald };
2793f542974SPaul B Schroeder 
28090ab5ee9SRusty Russell static bool debug;
2813f542974SPaul B Schroeder 
2823f542974SPaul B Schroeder /*
2833f542974SPaul B Schroeder  * mos7840_set_reg_sync
2843f542974SPaul B Schroeder  * 	To set the Control register by calling usb_fill_control_urb function
2853f542974SPaul B Schroeder  *	by passing usb_sndctrlpipe function as parameter.
2863f542974SPaul B Schroeder  */
2873f542974SPaul B Schroeder 
2883f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2893f542974SPaul B Schroeder 				__u16 val)
2903f542974SPaul B Schroeder {
2913f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2923f542974SPaul B Schroeder 	val = val & 0x00ff;
29384fe6e79STony Cook 	dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
2943f542974SPaul B Schroeder 
2953f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2963f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2973f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2983f542974SPaul B Schroeder }
2993f542974SPaul B Schroeder 
3003f542974SPaul B Schroeder /*
3013f542974SPaul B Schroeder  * mos7840_get_reg_sync
3023f542974SPaul B Schroeder  * 	To set the Uart register by calling usb_fill_control_urb function by
3033f542974SPaul B Schroeder  *	passing usb_rcvctrlpipe function as parameter.
3043f542974SPaul B Schroeder  */
3053f542974SPaul B Schroeder 
3063f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
3073f542974SPaul B Schroeder 				__u16 *val)
3083f542974SPaul B Schroeder {
3093f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3103f542974SPaul B Schroeder 	int ret = 0;
3119e221a35SJohan Hovold 	u8 *buf;
3129e221a35SJohan Hovold 
3139e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3149e221a35SJohan Hovold 	if (!buf)
3159e221a35SJohan Hovold 		return -ENOMEM;
3163f542974SPaul B Schroeder 
3173f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3189e221a35SJohan Hovold 			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
3193f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
3209e221a35SJohan Hovold 	*val = buf[0];
32184fe6e79STony Cook 	dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
3229e221a35SJohan Hovold 
3239e221a35SJohan Hovold 	kfree(buf);
3243f542974SPaul B Schroeder 	return ret;
3253f542974SPaul B Schroeder }
3263f542974SPaul B Schroeder 
3273f542974SPaul B Schroeder /*
3283f542974SPaul B Schroeder  * mos7840_set_uart_reg
3293f542974SPaul B Schroeder  *	To set the Uart register by calling usb_fill_control_urb function by
3303f542974SPaul B Schroeder  *	passing usb_sndctrlpipe function as parameter.
3313f542974SPaul B Schroeder  */
3323f542974SPaul B Schroeder 
3333f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
3343f542974SPaul B Schroeder 				__u16 val)
3353f542974SPaul B Schroeder {
3363f542974SPaul B Schroeder 
3373f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3383f542974SPaul B Schroeder 	val = val & 0x00ff;
339880af9dbSAlan Cox 	/* For the UART control registers, the application number need
340880af9dbSAlan Cox 	   to be Or'ed */
3410de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
342880af9dbSAlan Cox 		val |= (((__u16) port->number -
343880af9dbSAlan Cox 				(__u16) (port->serial->minor)) + 1) << 8;
34484fe6e79STony Cook 		dbg("mos7840_set_uart_reg application number is %x", val);
3453f542974SPaul B Schroeder 	} else {
3463f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
347880af9dbSAlan Cox 			val |= (((__u16) port->number -
3483f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
34984fe6e79STony Cook 			dbg("mos7840_set_uart_reg application number is %x",
3503f542974SPaul B Schroeder 			    val);
3513f542974SPaul B Schroeder 		} else {
3523f542974SPaul B Schroeder 			val |=
3533f542974SPaul B Schroeder 			    (((__u16) port->number -
3543f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
35584fe6e79STony Cook 			dbg("mos7840_set_uart_reg application number is %x",
3563f542974SPaul B Schroeder 			    val);
3573f542974SPaul B Schroeder 		}
3583f542974SPaul B Schroeder 	}
3593f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
3603f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
3613f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
3623f542974SPaul B Schroeder 
3633f542974SPaul B Schroeder }
3643f542974SPaul B Schroeder 
3653f542974SPaul B Schroeder /*
3663f542974SPaul B Schroeder  * mos7840_get_uart_reg
3673f542974SPaul B Schroeder  *	To set the Control register by calling usb_fill_control_urb function
3683f542974SPaul B Schroeder  *	by passing usb_rcvctrlpipe function as parameter.
3693f542974SPaul B Schroeder  */
3703f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3713f542974SPaul B Schroeder 				__u16 *val)
3723f542974SPaul B Schroeder {
3733f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3743f542974SPaul B Schroeder 	int ret = 0;
3753f542974SPaul B Schroeder 	__u16 Wval;
3769e221a35SJohan Hovold 	u8 *buf;
3779e221a35SJohan Hovold 
3789e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3799e221a35SJohan Hovold 	if (!buf)
3809e221a35SJohan Hovold 		return -ENOMEM;
3813f542974SPaul B Schroeder 
38284fe6e79STony Cook 	/* dbg("application number is %4x",
383880af9dbSAlan Cox 	    (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
3843f542974SPaul B Schroeder 	/* Wval  is same as application number */
3850de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3863f542974SPaul B Schroeder 		Wval =
3873f542974SPaul B Schroeder 		    (((__u16) port->number - (__u16) (port->serial->minor)) +
3883f542974SPaul B Schroeder 		     1) << 8;
38984fe6e79STony Cook 		dbg("mos7840_get_uart_reg application number is %x", Wval);
3903f542974SPaul B Schroeder 	} else {
3913f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
392880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3933f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
39484fe6e79STony Cook 			dbg("mos7840_get_uart_reg application number is %x",
3953f542974SPaul B Schroeder 			    Wval);
3963f542974SPaul B Schroeder 		} else {
397880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3983f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
39984fe6e79STony Cook 			dbg("mos7840_get_uart_reg application number is %x",
4003f542974SPaul B Schroeder 			    Wval);
4013f542974SPaul B Schroeder 		}
4023f542974SPaul B Schroeder 	}
4033f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
4049e221a35SJohan Hovold 			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
4053f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
4069e221a35SJohan Hovold 	*val = buf[0];
4079e221a35SJohan Hovold 
4089e221a35SJohan Hovold 	kfree(buf);
4093f542974SPaul B Schroeder 	return ret;
4103f542974SPaul B Schroeder }
4113f542974SPaul B Schroeder 
4123f542974SPaul B Schroeder static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
4133f542974SPaul B Schroeder {
4143f542974SPaul B Schroeder 
41584fe6e79STony Cook 	dbg("***************************************");
41684fe6e79STony Cook 	dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
41784fe6e79STony Cook 	dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
41884fe6e79STony Cook 	dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
41984fe6e79STony Cook 	dbg("***************************************");
4203f542974SPaul B Schroeder 
4213f542974SPaul B Schroeder }
4223f542974SPaul B Schroeder 
4233f542974SPaul B Schroeder /************************************************************************/
4243f542974SPaul B Schroeder /************************************************************************/
4253f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
4263f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
4273f542974SPaul B Schroeder /************************************************************************/
4283f542974SPaul B Schroeder /************************************************************************/
4293f542974SPaul B Schroeder 
4303f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port,
4313f542974SPaul B Schroeder 					    struct moschip_port *data)
4323f542974SPaul B Schroeder {
4333f542974SPaul B Schroeder 	usb_set_serial_port_data(port, (void *)data);
4343f542974SPaul B Schroeder }
4353f542974SPaul B Schroeder 
4363f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct
4373f542974SPaul B Schroeder 							    usb_serial_port
4383f542974SPaul B Schroeder 							    *port)
4393f542974SPaul B Schroeder {
4403f542974SPaul B Schroeder 	return (struct moschip_port *)usb_get_serial_port_data(port);
4413f542974SPaul B Schroeder }
4423f542974SPaul B Schroeder 
4430de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
4443f542974SPaul B Schroeder {
4453f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4463f542974SPaul B Schroeder 	struct async_icount *icount;
4473f542974SPaul B Schroeder 	mos7840_port = port;
4483f542974SPaul B Schroeder 	icount = &mos7840_port->icount;
4493f542974SPaul B Schroeder 	if (new_msr &
4503f542974SPaul B Schroeder 	    (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
4513f542974SPaul B Schroeder 	     MOS_MSR_DELTA_CD)) {
4523f542974SPaul B Schroeder 		icount = &mos7840_port->icount;
4533f542974SPaul B Schroeder 
4543f542974SPaul B Schroeder 		/* update input line counters */
4553f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CTS) {
4563f542974SPaul B Schroeder 			icount->cts++;
4570de9a702SOliver Neukum 			smp_wmb();
4583f542974SPaul B Schroeder 		}
4593f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_DSR) {
4603f542974SPaul B Schroeder 			icount->dsr++;
4610de9a702SOliver Neukum 			smp_wmb();
4623f542974SPaul B Schroeder 		}
4633f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CD) {
4643f542974SPaul B Schroeder 			icount->dcd++;
4650de9a702SOliver Neukum 			smp_wmb();
4663f542974SPaul B Schroeder 		}
4673f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_RI) {
4683f542974SPaul B Schroeder 			icount->rng++;
4690de9a702SOliver Neukum 			smp_wmb();
4700de9a702SOliver Neukum 		}
4713f542974SPaul B Schroeder 	}
4723f542974SPaul B Schroeder }
4733f542974SPaul B Schroeder 
4740de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
4753f542974SPaul B Schroeder {
4763f542974SPaul B Schroeder 	struct async_icount *icount;
4773f542974SPaul B Schroeder 
478441b62c1SHarvey Harrison 	dbg("%s - %02x", __func__, new_lsr);
4793f542974SPaul B Schroeder 
4803f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
481880af9dbSAlan Cox 		/*
482880af9dbSAlan Cox 		 * Parity and Framing errors only count if they
483880af9dbSAlan Cox 		 * occur exclusive of a break being
484880af9dbSAlan Cox 		 * received.
485880af9dbSAlan Cox 		 */
4863f542974SPaul B Schroeder 		new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
4873f542974SPaul B Schroeder 	}
4883f542974SPaul B Schroeder 
4893f542974SPaul B Schroeder 	/* update input line counters */
4903f542974SPaul B Schroeder 	icount = &port->icount;
4913f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
4923f542974SPaul B Schroeder 		icount->brk++;
4930de9a702SOliver Neukum 		smp_wmb();
4943f542974SPaul B Schroeder 	}
4953f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_OE) {
4963f542974SPaul B Schroeder 		icount->overrun++;
4970de9a702SOliver Neukum 		smp_wmb();
4983f542974SPaul B Schroeder 	}
4993f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_PE) {
5003f542974SPaul B Schroeder 		icount->parity++;
5010de9a702SOliver Neukum 		smp_wmb();
5023f542974SPaul B Schroeder 	}
5033f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_FE) {
5043f542974SPaul B Schroeder 		icount->frame++;
5050de9a702SOliver Neukum 		smp_wmb();
5063f542974SPaul B Schroeder 	}
5073f542974SPaul B Schroeder }
5083f542974SPaul B Schroeder 
5093f542974SPaul B Schroeder /************************************************************************/
5103f542974SPaul B Schroeder /************************************************************************/
5113f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
5123f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
5133f542974SPaul B Schroeder /************************************************************************/
5143f542974SPaul B Schroeder /************************************************************************/
5153f542974SPaul B Schroeder 
5167d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb)
5173f542974SPaul B Schroeder {
5183f542974SPaul B Schroeder 	unsigned char *data;
5193f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
5203f542974SPaul B Schroeder 	__u8 regval = 0x0;
5210de9a702SOliver Neukum 	int result = 0;
5220643c724SGreg Kroah-Hartman 	int status = urb->status;
5233f542974SPaul B Schroeder 
524cdc97792SMing Lei 	mos7840_port = urb->context;
5250de9a702SOliver Neukum 
5260643c724SGreg Kroah-Hartman 	switch (status) {
5273f542974SPaul B Schroeder 	case 0:
5283f542974SPaul B Schroeder 		/* success */
5293f542974SPaul B Schroeder 		break;
5303f542974SPaul B Schroeder 	case -ECONNRESET:
5313f542974SPaul B Schroeder 	case -ENOENT:
5323f542974SPaul B Schroeder 	case -ESHUTDOWN:
5333f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
534441b62c1SHarvey Harrison 		dbg("%s - urb shutting down with status: %d", __func__,
5350643c724SGreg Kroah-Hartman 		    status);
5363f542974SPaul B Schroeder 		return;
5373f542974SPaul B Schroeder 	default:
538441b62c1SHarvey Harrison 		dbg("%s - nonzero urb status received: %d", __func__,
5390643c724SGreg Kroah-Hartman 		    status);
5403f542974SPaul B Schroeder 		goto exit;
5413f542974SPaul B Schroeder 	}
5423f542974SPaul B Schroeder 
54384fe6e79STony Cook 	dbg("%s urb buffer size is %d", __func__, urb->actual_length);
54484fe6e79STony Cook 	dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
5453f542974SPaul B Schroeder 	    mos7840_port->MsrLsr, mos7840_port->port_num);
5463f542974SPaul B Schroeder 	data = urb->transfer_buffer;
5473f542974SPaul B Schroeder 	regval = (__u8) data[0];
54884fe6e79STony Cook 	dbg("%s data is %x", __func__, regval);
5493f542974SPaul B Schroeder 	if (mos7840_port->MsrLsr == 0)
5503f542974SPaul B Schroeder 		mos7840_handle_new_msr(mos7840_port, regval);
5513f542974SPaul B Schroeder 	else if (mos7840_port->MsrLsr == 1)
5523f542974SPaul B Schroeder 		mos7840_handle_new_lsr(mos7840_port, regval);
5533f542974SPaul B Schroeder 
5543f542974SPaul B Schroeder exit:
5550de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
5560de9a702SOliver Neukum 	if (!mos7840_port->zombie)
5570de9a702SOliver Neukum 		result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
5580de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
5590de9a702SOliver Neukum 	if (result) {
5600de9a702SOliver Neukum 		dev_err(&urb->dev->dev,
5610de9a702SOliver Neukum 			"%s - Error %d submitting interrupt urb\n",
562441b62c1SHarvey Harrison 			__func__, result);
5630de9a702SOliver Neukum 	}
5643f542974SPaul B Schroeder }
5653f542974SPaul B Schroeder 
5663f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
5673f542974SPaul B Schroeder 			   __u16 *val)
5683f542974SPaul B Schroeder {
5693f542974SPaul B Schroeder 	struct usb_device *dev = mcs->port->serial->dev;
5700de9a702SOliver Neukum 	struct usb_ctrlrequest *dr = mcs->dr;
5710de9a702SOliver Neukum 	unsigned char *buffer = mcs->ctrl_buf;
5720de9a702SOliver Neukum 	int ret;
5733f542974SPaul B Schroeder 
5743f542974SPaul B Schroeder 	dr->bRequestType = MCS_RD_RTYPE;
5753f542974SPaul B Schroeder 	dr->bRequest = MCS_RDREQ;
576880af9dbSAlan Cox 	dr->wValue = cpu_to_le16(Wval);	/* 0 */
5773f542974SPaul B Schroeder 	dr->wIndex = cpu_to_le16(reg);
5783f542974SPaul B Schroeder 	dr->wLength = cpu_to_le16(2);
5793f542974SPaul B Schroeder 
5803f542974SPaul B Schroeder 	usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
5813f542974SPaul B Schroeder 			     (unsigned char *)dr, buffer, 2,
5823f542974SPaul B Schroeder 			     mos7840_control_callback, mcs);
5833f542974SPaul B Schroeder 	mcs->control_urb->transfer_buffer_length = 2;
5843f542974SPaul B Schroeder 	ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
5853f542974SPaul B Schroeder 	return ret;
5863f542974SPaul B Schroeder }
5873f542974SPaul B Schroeder 
588*0eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb)
589*0eafe4deSDonald {
590*0eafe4deSDonald 	switch (urb->status) {
591*0eafe4deSDonald 	case 0:
592*0eafe4deSDonald 		/* Success */
593*0eafe4deSDonald 		break;
594*0eafe4deSDonald 	case -ECONNRESET:
595*0eafe4deSDonald 	case -ENOENT:
596*0eafe4deSDonald 	case -ESHUTDOWN:
597*0eafe4deSDonald 		/* This urb is terminated, clean up */
598*0eafe4deSDonald 		dbg("%s - urb shutting down with status: %d", __func__,
599*0eafe4deSDonald 			urb->status);
600*0eafe4deSDonald 		break;
601*0eafe4deSDonald 	default:
602*0eafe4deSDonald 		dbg("%s - nonzero urb status received: %d", __func__,
603*0eafe4deSDonald 			urb->status);
604*0eafe4deSDonald 	}
605*0eafe4deSDonald }
606*0eafe4deSDonald 
607*0eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
608*0eafe4deSDonald 				__u16 reg)
609*0eafe4deSDonald {
610*0eafe4deSDonald 	struct usb_device *dev = mcs->port->serial->dev;
611*0eafe4deSDonald 	struct usb_ctrlrequest *dr = mcs->dr;
612*0eafe4deSDonald 
613*0eafe4deSDonald 	dr->bRequestType = MCS_WR_RTYPE;
614*0eafe4deSDonald 	dr->bRequest = MCS_WRREQ;
615*0eafe4deSDonald 	dr->wValue = cpu_to_le16(wval);
616*0eafe4deSDonald 	dr->wIndex = cpu_to_le16(reg);
617*0eafe4deSDonald 	dr->wLength = cpu_to_le16(0);
618*0eafe4deSDonald 
619*0eafe4deSDonald 	usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
620*0eafe4deSDonald 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
621*0eafe4deSDonald 
622*0eafe4deSDonald 	usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
623*0eafe4deSDonald }
624*0eafe4deSDonald 
625*0eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
626*0eafe4deSDonald 				__u16 val)
627*0eafe4deSDonald {
628*0eafe4deSDonald 	struct usb_device *dev = port->serial->dev;
629*0eafe4deSDonald 
630*0eafe4deSDonald 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
631*0eafe4deSDonald 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
632*0eafe4deSDonald }
633*0eafe4deSDonald 
634*0eafe4deSDonald static void mos7840_led_off(unsigned long arg)
635*0eafe4deSDonald {
636*0eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
637*0eafe4deSDonald 
638*0eafe4deSDonald 	/* Turn off LED */
639*0eafe4deSDonald 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
640*0eafe4deSDonald 	mod_timer(&mcs->led_timer2,
641*0eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_OFF_MS));
642*0eafe4deSDonald }
643*0eafe4deSDonald 
644*0eafe4deSDonald static void mos7840_led_flag_off(unsigned long arg)
645*0eafe4deSDonald {
646*0eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
647*0eafe4deSDonald 
648*0eafe4deSDonald 	mcs->led_flag = false;
649*0eafe4deSDonald }
650*0eafe4deSDonald 
6513f542974SPaul B Schroeder /*****************************************************************************
6523f542974SPaul B Schroeder  * mos7840_interrupt_callback
6533f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
6543f542974SPaul B Schroeder  *	interrupt endpoint.
6553f542974SPaul B Schroeder  *****************************************************************************/
6563f542974SPaul B Schroeder 
6577d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb)
6583f542974SPaul B Schroeder {
6593f542974SPaul B Schroeder 	int result;
6603f542974SPaul B Schroeder 	int length;
6613f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
6623f542974SPaul B Schroeder 	struct usb_serial *serial;
6633f542974SPaul B Schroeder 	__u16 Data;
6643f542974SPaul B Schroeder 	unsigned char *data;
6653f542974SPaul B Schroeder 	__u8 sp[5], st;
6660de9a702SOliver Neukum 	int i, rv = 0;
6670de9a702SOliver Neukum 	__u16 wval, wreg = 0;
6680643c724SGreg Kroah-Hartman 	int status = urb->status;
6693f542974SPaul B Schroeder 
67084fe6e79STony Cook 	dbg("%s", " : Entering");
6713f542974SPaul B Schroeder 
6720643c724SGreg Kroah-Hartman 	switch (status) {
6733f542974SPaul B Schroeder 	case 0:
6743f542974SPaul B Schroeder 		/* success */
6753f542974SPaul B Schroeder 		break;
6763f542974SPaul B Schroeder 	case -ECONNRESET:
6773f542974SPaul B Schroeder 	case -ENOENT:
6783f542974SPaul B Schroeder 	case -ESHUTDOWN:
6793f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
680441b62c1SHarvey Harrison 		dbg("%s - urb shutting down with status: %d", __func__,
6810643c724SGreg Kroah-Hartman 		    status);
6823f542974SPaul B Schroeder 		return;
6833f542974SPaul B Schroeder 	default:
684441b62c1SHarvey Harrison 		dbg("%s - nonzero urb status received: %d", __func__,
6850643c724SGreg Kroah-Hartman 		    status);
6863f542974SPaul B Schroeder 		goto exit;
6873f542974SPaul B Schroeder 	}
6883f542974SPaul B Schroeder 
6893f542974SPaul B Schroeder 	length = urb->actual_length;
6903f542974SPaul B Schroeder 	data = urb->transfer_buffer;
6913f542974SPaul B Schroeder 
692cdc97792SMing Lei 	serial = urb->context;
6933f542974SPaul B Schroeder 
6943f542974SPaul B Schroeder 	/* Moschip get 5 bytes
6953f542974SPaul B Schroeder 	 * Byte 1 IIR Port 1 (port.number is 0)
6963f542974SPaul B Schroeder 	 * Byte 2 IIR Port 2 (port.number is 1)
6973f542974SPaul B Schroeder 	 * Byte 3 IIR Port 3 (port.number is 2)
6983f542974SPaul B Schroeder 	 * Byte 4 IIR Port 4 (port.number is 3)
6993f542974SPaul B Schroeder 	 * Byte 5 FIFO status for both */
7003f542974SPaul B Schroeder 
7013f542974SPaul B Schroeder 	if (length && length > 5) {
70284fe6e79STony Cook 		dbg("%s", "Wrong data !!!");
7033f542974SPaul B Schroeder 		return;
7043f542974SPaul B Schroeder 	}
7053f542974SPaul B Schroeder 
7063f542974SPaul B Schroeder 	sp[0] = (__u8) data[0];
7073f542974SPaul B Schroeder 	sp[1] = (__u8) data[1];
7083f542974SPaul B Schroeder 	sp[2] = (__u8) data[2];
7093f542974SPaul B Schroeder 	sp[3] = (__u8) data[3];
7103f542974SPaul B Schroeder 	st = (__u8) data[4];
7113f542974SPaul B Schroeder 
7123f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; i++) {
7133f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
7143f542974SPaul B Schroeder 		wval =
7153f542974SPaul B Schroeder 		    (((__u16) serial->port[i]->number -
7163f542974SPaul B Schroeder 		      (__u16) (serial->minor)) + 1) << 8;
7173f542974SPaul B Schroeder 		if (mos7840_port->open) {
7183f542974SPaul B Schroeder 			if (sp[i] & 0x01) {
71984fe6e79STony Cook 				dbg("SP%d No Interrupt !!!", i);
7203f542974SPaul B Schroeder 			} else {
7213f542974SPaul B Schroeder 				switch (sp[i] & 0x0f) {
7223f542974SPaul B Schroeder 				case SERIAL_IIR_RLS:
7233f542974SPaul B Schroeder 					dbg("Serial Port %d: Receiver status error or ", i);
72484fe6e79STony Cook 					dbg("address bit detected in 9-bit mode");
7253f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 1;
7260de9a702SOliver Neukum 					wreg = LINE_STATUS_REGISTER;
7273f542974SPaul B Schroeder 					break;
7283f542974SPaul B Schroeder 				case SERIAL_IIR_MS:
72984fe6e79STony Cook 					dbg("Serial Port %d: Modem status change", i);
7303f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 0;
7310de9a702SOliver Neukum 					wreg = MODEM_STATUS_REGISTER;
7323f542974SPaul B Schroeder 					break;
7333f542974SPaul B Schroeder 				}
7340de9a702SOliver Neukum 				spin_lock(&mos7840_port->pool_lock);
7350de9a702SOliver Neukum 				if (!mos7840_port->zombie) {
7360de9a702SOliver Neukum 					rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
7370de9a702SOliver Neukum 				} else {
7380de9a702SOliver Neukum 					spin_unlock(&mos7840_port->pool_lock);
7390de9a702SOliver Neukum 					return;
7400de9a702SOliver Neukum 				}
7410de9a702SOliver Neukum 				spin_unlock(&mos7840_port->pool_lock);
7423f542974SPaul B Schroeder 			}
7433f542974SPaul B Schroeder 		}
7443f542974SPaul B Schroeder 	}
745880af9dbSAlan Cox 	if (!(rv < 0))
746880af9dbSAlan Cox 		/* the completion handler for the control urb will resubmit */
7470de9a702SOliver Neukum 		return;
7483f542974SPaul B Schroeder exit:
7493f542974SPaul B Schroeder 	result = usb_submit_urb(urb, GFP_ATOMIC);
7503f542974SPaul B Schroeder 	if (result) {
7513f542974SPaul B Schroeder 		dev_err(&urb->dev->dev,
7523f542974SPaul B Schroeder 			"%s - Error %d submitting interrupt urb\n",
753441b62c1SHarvey Harrison 			__func__, result);
7543f542974SPaul B Schroeder 	}
7553f542974SPaul B Schroeder }
7563f542974SPaul B Schroeder 
7573f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port,
7583f542974SPaul B Schroeder 				       const char *function)
7593f542974SPaul B Schroeder {
7603f542974SPaul B Schroeder 	if (!port) {
7613f542974SPaul B Schroeder 		dbg("%s - port == NULL", function);
7623f542974SPaul B Schroeder 		return -1;
7633f542974SPaul B Schroeder 	}
7643f542974SPaul B Schroeder 	if (!port->serial) {
7653f542974SPaul B Schroeder 		dbg("%s - port->serial == NULL", function);
7663f542974SPaul B Schroeder 		return -1;
7673f542974SPaul B Schroeder 	}
7683f542974SPaul B Schroeder 
7693f542974SPaul B Schroeder 	return 0;
7703f542974SPaul B Schroeder }
7713f542974SPaul B Schroeder 
7723f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
7733f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
7743f542974SPaul B Schroeder 					 const char *function)
7753f542974SPaul B Schroeder {
7763f542974SPaul B Schroeder 	if (!serial) {
7773f542974SPaul B Schroeder 		dbg("%s - serial == NULL", function);
7783f542974SPaul B Schroeder 		return -1;
7793f542974SPaul B Schroeder 	}
7803f542974SPaul B Schroeder 	if (!serial->type) {
7813f542974SPaul B Schroeder 		dbg("%s - serial->type == NULL!", function);
7823f542974SPaul B Schroeder 		return -1;
7833f542974SPaul B Schroeder 	}
7843f542974SPaul B Schroeder 
7853f542974SPaul B Schroeder 	return 0;
7863f542974SPaul B Schroeder }
7873f542974SPaul B Schroeder 
7883f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
7893f542974SPaul B Schroeder 						 const char *function)
7903f542974SPaul B Schroeder {
7913f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
7923f542974SPaul B Schroeder 	if (!port ||
7933f542974SPaul B Schroeder 	    mos7840_port_paranoia_check(port, function) ||
7943f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
795880af9dbSAlan Cox 		/* then say that we don't have a valid usb_serial thing,
796880af9dbSAlan Cox 		 * which will end up genrating -ENODEV return values */
7973f542974SPaul B Schroeder 		return NULL;
7983f542974SPaul B Schroeder 	}
7993f542974SPaul B Schroeder 
8003f542974SPaul B Schroeder 	return port->serial;
8013f542974SPaul B Schroeder }
8023f542974SPaul B Schroeder 
8033f542974SPaul B Schroeder /*****************************************************************************
8043f542974SPaul B Schroeder  * mos7840_bulk_in_callback
8053f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
8063f542974SPaul B Schroeder  *	bulk in endpoint.
8073f542974SPaul B Schroeder  *****************************************************************************/
8083f542974SPaul B Schroeder 
8097d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
8103f542974SPaul B Schroeder {
8110643c724SGreg Kroah-Hartman 	int retval;
8123f542974SPaul B Schroeder 	unsigned char *data;
8133f542974SPaul B Schroeder 	struct usb_serial *serial;
8143f542974SPaul B Schroeder 	struct usb_serial_port *port;
8153f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8163f542974SPaul B Schroeder 	struct tty_struct *tty;
8170643c724SGreg Kroah-Hartman 	int status = urb->status;
8183f542974SPaul B Schroeder 
819cdc97792SMing Lei 	mos7840_port = urb->context;
8203f542974SPaul B Schroeder 	if (!mos7840_port) {
82184fe6e79STony Cook 		dbg("%s", "NULL mos7840_port pointer");
82250de36f7SGreg Kroah-Hartman 		return;
82350de36f7SGreg Kroah-Hartman 	}
82450de36f7SGreg Kroah-Hartman 
82550de36f7SGreg Kroah-Hartman 	if (status) {
82650de36f7SGreg Kroah-Hartman 		dbg("nonzero read bulk status received: %d", status);
82750de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8283f542974SPaul B Schroeder 		return;
8293f542974SPaul B Schroeder 	}
8303f542974SPaul B Schroeder 
8313f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
832441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
83384fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
83450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8353f542974SPaul B Schroeder 		return;
8363f542974SPaul B Schroeder 	}
8373f542974SPaul B Schroeder 
838441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
8393f542974SPaul B Schroeder 	if (!serial) {
84084fe6e79STony Cook 		dbg("%s", "Bad serial pointer");
84150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8423f542974SPaul B Schroeder 		return;
8433f542974SPaul B Schroeder 	}
8443f542974SPaul B Schroeder 
84584fe6e79STony Cook 	dbg("%s", "Entering... ");
8463f542974SPaul B Schroeder 
8473f542974SPaul B Schroeder 	data = urb->transfer_buffer;
8483f542974SPaul B Schroeder 
84984fe6e79STony Cook 	dbg("%s", "Entering ...........");
8503f542974SPaul B Schroeder 
8513f542974SPaul B Schroeder 	if (urb->actual_length) {
8524a90f09bSAlan Cox 		tty = tty_port_tty_get(&mos7840_port->port->port);
8533f542974SPaul B Schroeder 		if (tty) {
8543f542974SPaul B Schroeder 			tty_insert_flip_string(tty, data, urb->actual_length);
85584fe6e79STony Cook 			dbg(" %s ", data);
8563f542974SPaul B Schroeder 			tty_flip_buffer_push(tty);
8574a90f09bSAlan Cox 			tty_kref_put(tty);
8583f542974SPaul B Schroeder 		}
8593f542974SPaul B Schroeder 		mos7840_port->icount.rx += urb->actual_length;
8600de9a702SOliver Neukum 		smp_wmb();
86184fe6e79STony Cook 		dbg("mos7840_port->icount.rx is %d:",
8623f542974SPaul B Schroeder 		    mos7840_port->icount.rx);
8633f542974SPaul B Schroeder 	}
8643f542974SPaul B Schroeder 
8653f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
86684fe6e79STony Cook 		dbg("%s", "URB KILLED !!!");
86750de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8683f542974SPaul B Schroeder 		return;
8693f542974SPaul B Schroeder 	}
8703f542974SPaul B Schroeder 
871*0eafe4deSDonald 	/* Turn on LED */
872*0eafe4deSDonald 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
873*0eafe4deSDonald 		mos7840_port->led_flag = true;
874*0eafe4deSDonald 		mos7840_set_led_async(mos7840_port, 0x0301,
875*0eafe4deSDonald 					MODEM_CONTROL_REGISTER);
876*0eafe4deSDonald 		mod_timer(&mos7840_port->led_timer1,
877*0eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_ON_MS));
878*0eafe4deSDonald 	}
8790de9a702SOliver Neukum 
88050de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
8810643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
8823f542974SPaul B Schroeder 
8830643c724SGreg Kroah-Hartman 	if (retval) {
88450de36f7SGreg Kroah-Hartman 		dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
88550de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8863f542974SPaul B Schroeder 	}
8873f542974SPaul B Schroeder }
8883f542974SPaul B Schroeder 
8893f542974SPaul B Schroeder /*****************************************************************************
8903f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
891880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
892880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
8933f542974SPaul B Schroeder  *****************************************************************************/
8943f542974SPaul B Schroeder 
8957d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
8963f542974SPaul B Schroeder {
8973f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8983f542974SPaul B Schroeder 	struct tty_struct *tty;
8990643c724SGreg Kroah-Hartman 	int status = urb->status;
9000de9a702SOliver Neukum 	int i;
9010de9a702SOliver Neukum 
902cdc97792SMing Lei 	mos7840_port = urb->context;
9030de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
9040de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
9050de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
9060de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
9070de9a702SOliver Neukum 			break;
9080de9a702SOliver Neukum 		}
9090de9a702SOliver Neukum 	}
9100de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
9110de9a702SOliver Neukum 
9120643c724SGreg Kroah-Hartman 	if (status) {
91384fe6e79STony Cook 		dbg("nonzero write bulk status received:%d", status);
9143f542974SPaul B Schroeder 		return;
9153f542974SPaul B Schroeder 	}
9163f542974SPaul B Schroeder 
917441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
91884fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
9193f542974SPaul B Schroeder 		return;
9203f542974SPaul B Schroeder 	}
9213f542974SPaul B Schroeder 
92284fe6e79STony Cook 	dbg("%s", "Entering .........");
9233f542974SPaul B Schroeder 
9244a90f09bSAlan Cox 	tty = tty_port_tty_get(&mos7840_port->port->port);
925b963a844SJiri Slaby 	if (tty && mos7840_port->open)
926b963a844SJiri Slaby 		tty_wakeup(tty);
9274a90f09bSAlan Cox 	tty_kref_put(tty);
9283f542974SPaul B Schroeder 
9293f542974SPaul B Schroeder }
9303f542974SPaul B Schroeder 
9313f542974SPaul B Schroeder /************************************************************************/
9323f542974SPaul 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       */
9333f542974SPaul B Schroeder /************************************************************************/
9343f542974SPaul B Schroeder #ifdef MCSSerialProbe
9353f542974SPaul B Schroeder static int mos7840_serial_probe(struct usb_serial *serial,
9363f542974SPaul B Schroeder 				const struct usb_device_id *id)
9373f542974SPaul B Schroeder {
9383f542974SPaul B Schroeder 
9393f542974SPaul B Schroeder 	/*need to implement the mode_reg reading and updating\
9403f542974SPaul B Schroeder 	   structures usb_serial_ device_type\
9413f542974SPaul B Schroeder 	   (i.e num_ports, num_bulkin,bulkout etc) */
9423f542974SPaul B Schroeder 	/* Also we can update the changes  attach */
9433f542974SPaul B Schroeder 	return 1;
9443f542974SPaul B Schroeder }
9453f542974SPaul B Schroeder #endif
9463f542974SPaul B Schroeder 
9473f542974SPaul B Schroeder /*****************************************************************************
9483f542974SPaul B Schroeder  * mos7840_open
9493f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
9503f542974SPaul B Schroeder  *	If successful, we return 0
9513f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
9523f542974SPaul B Schroeder  *****************************************************************************/
9533f542974SPaul B Schroeder 
954a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
9553f542974SPaul B Schroeder {
9563f542974SPaul B Schroeder 	int response;
9573f542974SPaul B Schroeder 	int j;
9583f542974SPaul B Schroeder 	struct usb_serial *serial;
9593f542974SPaul B Schroeder 	struct urb *urb;
9603f542974SPaul B Schroeder 	__u16 Data;
9613f542974SPaul B Schroeder 	int status;
9623f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
9630de9a702SOliver Neukum 	struct moschip_port *port0;
9643f542974SPaul B Schroeder 
96584fe6e79STony Cook 	dbg ("%s enter", __func__);
96684fe6e79STony Cook 
967441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
96884fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
9693f542974SPaul B Schroeder 		return -ENODEV;
9703f542974SPaul B Schroeder 	}
9713f542974SPaul B Schroeder 
9723f542974SPaul B Schroeder 	serial = port->serial;
9733f542974SPaul B Schroeder 
974441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
97584fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
9763f542974SPaul B Schroeder 		return -ENODEV;
9773f542974SPaul B Schroeder 	}
9783f542974SPaul B Schroeder 
9793f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
9800de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
9813f542974SPaul B Schroeder 
9820de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
9833f542974SPaul B Schroeder 		return -ENODEV;
9843f542974SPaul B Schroeder 
9853f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
9863f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
9870de9a702SOliver Neukum 	port0->open_ports++;
9883f542974SPaul B Schroeder 
9893f542974SPaul B Schroeder 	/* Initialising the write urb pool */
9903f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
9910de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
9923f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
9933f542974SPaul B Schroeder 
9943f542974SPaul B Schroeder 		if (urb == NULL) {
995194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "No more urbs???\n");
9963f542974SPaul B Schroeder 			continue;
9973f542974SPaul B Schroeder 		}
9983f542974SPaul B Schroeder 
999880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1000880af9dbSAlan Cox 								GFP_KERNEL);
10013f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
10020de9a702SOliver Neukum 			usb_free_urb(urb);
10030de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
1004194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
1005194343d9SGreg Kroah-Hartman 				"%s-out of memory for urb buffers.\n",
1006194343d9SGreg Kroah-Hartman 				__func__);
10073f542974SPaul B Schroeder 			continue;
10083f542974SPaul B Schroeder 		}
10093f542974SPaul B Schroeder 	}
10103f542974SPaul B Schroeder 
10113f542974SPaul B Schroeder /*****************************************************************************
10123f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
10133f542974SPaul B Schroeder  *
10143f542974SPaul B Schroeder  * Register Index
10153f542974SPaul B Schroeder  * 1 : IER
10163f542974SPaul B Schroeder  * 2 : FCR
10173f542974SPaul B Schroeder  * 3 : LCR
10183f542974SPaul B Schroeder  * 4 : MCR
10193f542974SPaul B Schroeder  *
10203f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
10213f542974SPaul B Schroeder  *****************************************************************************/
10223f542974SPaul B Schroeder 
1023880af9dbSAlan Cox 	/* NEED to check the following Block */
10243f542974SPaul B Schroeder 
10253f542974SPaul B Schroeder 	Data = 0x0;
10263f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
10273f542974SPaul B Schroeder 	if (status < 0) {
102884fe6e79STony Cook 		dbg("Reading Spreg failed");
10293f542974SPaul B Schroeder 		return -1;
10303f542974SPaul B Schroeder 	}
10313f542974SPaul B Schroeder 	Data |= 0x80;
10323f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10333f542974SPaul B Schroeder 	if (status < 0) {
103484fe6e79STony Cook 		dbg("writing Spreg failed");
10353f542974SPaul B Schroeder 		return -1;
10363f542974SPaul B Schroeder 	}
10373f542974SPaul B Schroeder 
10383f542974SPaul B Schroeder 	Data &= ~0x80;
10393f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10403f542974SPaul B Schroeder 	if (status < 0) {
104184fe6e79STony Cook 		dbg("writing Spreg failed");
10423f542974SPaul B Schroeder 		return -1;
10433f542974SPaul B Schroeder 	}
1044880af9dbSAlan Cox 	/* End of block to be checked */
10453f542974SPaul B Schroeder 
10463f542974SPaul B Schroeder 	Data = 0x0;
1047880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1048880af9dbSAlan Cox 									&Data);
10493f542974SPaul B Schroeder 	if (status < 0) {
105084fe6e79STony Cook 		dbg("Reading Controlreg failed");
10513f542974SPaul B Schroeder 		return -1;
10523f542974SPaul B Schroeder 	}
1053880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
1054880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
1055880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
1056880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
10573f542974SPaul B Schroeder 	if (status < 0) {
105884fe6e79STony Cook 		dbg("writing Controlreg failed");
10593f542974SPaul B Schroeder 		return -1;
10603f542974SPaul B Schroeder 	}
1061880af9dbSAlan Cox 	/* do register settings here */
1062880af9dbSAlan Cox 	/* Set all regs to the device default values. */
1063880af9dbSAlan Cox 	/***********************************
1064880af9dbSAlan Cox 	 * First Disable all interrupts.
1065880af9dbSAlan Cox 	 ***********************************/
10663f542974SPaul B Schroeder 	Data = 0x00;
10673f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
10683f542974SPaul B Schroeder 	if (status < 0) {
106984fe6e79STony Cook 		dbg("disabling interrupts failed");
10703f542974SPaul B Schroeder 		return -1;
10713f542974SPaul B Schroeder 	}
1072880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
10733f542974SPaul B Schroeder 	Data = 0x00;
10743f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
10753f542974SPaul B Schroeder 	if (status < 0) {
107684fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
10773f542974SPaul B Schroeder 		return -1;
10783f542974SPaul B Schroeder 	}
10793f542974SPaul B Schroeder 
10803f542974SPaul B Schroeder 	Data = 0xcf;
10813f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
10823f542974SPaul B Schroeder 	if (status < 0) {
108384fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
10843f542974SPaul B Schroeder 		return -1;
10853f542974SPaul B Schroeder 	}
10863f542974SPaul B Schroeder 
10873f542974SPaul B Schroeder 	Data = 0x03;
10883f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10893f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10903f542974SPaul B Schroeder 
10913f542974SPaul B Schroeder 	Data = 0x0b;
10923f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
10933f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
10943f542974SPaul B Schroeder 
10953f542974SPaul B Schroeder 	Data = 0x00;
10963f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
10973f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10983f542974SPaul B Schroeder 
1099880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
11003f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
11013f542974SPaul B Schroeder 
11023f542974SPaul B Schroeder 	Data = 0x0c;
11033f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
11043f542974SPaul B Schroeder 
11053f542974SPaul B Schroeder 	Data = 0x0;
11063f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
11073f542974SPaul B Schroeder 
11083f542974SPaul B Schroeder 	Data = 0x00;
11093f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
11103f542974SPaul B Schroeder 
11113f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
11123f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
11133f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
11143f542974SPaul B Schroeder 
1115880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
11163f542974SPaul B Schroeder 	Data = 0x0;
11173f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
11183f542974SPaul B Schroeder 
11193f542974SPaul B Schroeder 	Data = Data | 0x0c;
11203f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
11213f542974SPaul B Schroeder 
11223f542974SPaul B Schroeder 	Data = Data & ~0x0c;
11233f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1124880af9dbSAlan Cox 	/* Finally enable all interrupts */
11253f542974SPaul B Schroeder 	Data = 0x0c;
11263f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
11273f542974SPaul B Schroeder 
1128880af9dbSAlan Cox 	/* clearing rx_disable */
11293f542974SPaul B Schroeder 	Data = 0x0;
1130880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1131880af9dbSAlan Cox 									&Data);
11323f542974SPaul B Schroeder 	Data = Data & ~0x20;
1133880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1134880af9dbSAlan Cox 									Data);
11353f542974SPaul B Schroeder 
1136880af9dbSAlan Cox 	/* rx_negate */
11373f542974SPaul B Schroeder 	Data = 0x0;
1138880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1139880af9dbSAlan Cox 									&Data);
11403f542974SPaul B Schroeder 	Data = Data | 0x10;
1141880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1142880af9dbSAlan Cox 									Data);
11433f542974SPaul B Schroeder 
11443f542974SPaul B Schroeder 	/* Check to see if we've set up our endpoint info yet    *
11453f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the structures *
11463f542974SPaul B Schroeder 	 * were not set up at that time.)                        */
11470de9a702SOliver Neukum 	if (port0->open_ports == 1) {
11483f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_buffer == NULL) {
11493f542974SPaul B Schroeder 			/* set up interrupt urb */
11503f542974SPaul B Schroeder 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
11513f542974SPaul B Schroeder 				serial->dev,
11523f542974SPaul B Schroeder 				usb_rcvintpipe(serial->dev,
1153880af9dbSAlan Cox 				serial->port[0]->interrupt_in_endpointAddress),
11543f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_buffer,
11553f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_urb->
11563f542974SPaul B Schroeder 				transfer_buffer_length,
11573f542974SPaul B Schroeder 				mos7840_interrupt_callback,
11583f542974SPaul B Schroeder 				serial,
1159880af9dbSAlan Cox 				serial->port[0]->interrupt_in_urb->interval);
11603f542974SPaul B Schroeder 
11613f542974SPaul B Schroeder 			/* start interrupt read for mos7840               *
11623f542974SPaul B Schroeder 			 * will continue as long as mos7840 is connected  */
11633f542974SPaul B Schroeder 
11643f542974SPaul B Schroeder 			response =
11653f542974SPaul B Schroeder 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
11663f542974SPaul B Schroeder 					   GFP_KERNEL);
11673f542974SPaul B Schroeder 			if (response) {
1168194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "%s - Error %d submitting "
1169194343d9SGreg Kroah-Hartman 					"interrupt urb\n", __func__, response);
11703f542974SPaul B Schroeder 			}
11713f542974SPaul B Schroeder 
11723f542974SPaul B Schroeder 		}
11733f542974SPaul B Schroeder 
11743f542974SPaul B Schroeder 	}
11753f542974SPaul B Schroeder 
11763f542974SPaul B Schroeder 	/* see if we've set up our endpoint info yet   *
11773f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the  *
11783f542974SPaul B Schroeder 	 * structures were not set up at that time.)   */
11793f542974SPaul B Schroeder 
118084fe6e79STony Cook 	dbg("port number is %d", port->number);
118184fe6e79STony Cook 	dbg("serial number is %d", port->serial->minor);
118284fe6e79STony Cook 	dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
118384fe6e79STony Cook 	dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
118484fe6e79STony Cook 	dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
118584fe6e79STony Cook 	dbg("port's number in the device is %d", mos7840_port->port_num);
11863f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
11873f542974SPaul B Schroeder 
11883f542974SPaul B Schroeder 	/* set up our bulk in urb */
1189093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1190093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1191093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1192093ea2d3SDonald Lee 		usb_fill_bulk_urb(mos7840_port->read_urb,
1193093ea2d3SDonald Lee 			serial->dev,
1194093ea2d3SDonald Lee 			usb_rcvbulkpipe(serial->dev,
1195093ea2d3SDonald Lee 				(port->bulk_in_endpointAddress) + 2),
1196093ea2d3SDonald Lee 			port->bulk_in_buffer,
1197093ea2d3SDonald Lee 			mos7840_port->read_urb->transfer_buffer_length,
1198093ea2d3SDonald Lee 			mos7840_bulk_in_callback, mos7840_port);
1199093ea2d3SDonald Lee 	} else {
12003f542974SPaul B Schroeder 		usb_fill_bulk_urb(mos7840_port->read_urb,
12013f542974SPaul B Schroeder 			serial->dev,
12023f542974SPaul B Schroeder 			usb_rcvbulkpipe(serial->dev,
12033f542974SPaul B Schroeder 				port->bulk_in_endpointAddress),
12043f542974SPaul B Schroeder 			port->bulk_in_buffer,
12053f542974SPaul B Schroeder 			mos7840_port->read_urb->transfer_buffer_length,
12063f542974SPaul B Schroeder 			mos7840_bulk_in_callback, mos7840_port);
1207093ea2d3SDonald Lee 	}
12083f542974SPaul B Schroeder 
120984fe6e79STony Cook 	dbg("mos7840_open: bulkin endpoint is %d",
12103f542974SPaul B Schroeder 	    port->bulk_in_endpointAddress);
121150de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
12123f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
12133f542974SPaul B Schroeder 	if (response) {
1214194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1215194343d9SGreg Kroah-Hartman 			__func__, response);
121650de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
12173f542974SPaul B Schroeder 	}
12183f542974SPaul B Schroeder 
12193f542974SPaul B Schroeder 	/* initialize our wait queues */
12203f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->wait_chase);
12213f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->delta_msr_wait);
12223f542974SPaul B Schroeder 
12233f542974SPaul B Schroeder 	/* initialize our icount structure */
12243f542974SPaul B Schroeder 	memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
12253f542974SPaul B Schroeder 
12263f542974SPaul B Schroeder 	/* initialize our port settings */
1227880af9dbSAlan Cox 	/* Must set to enable ints! */
1228880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
12293f542974SPaul B Schroeder 	/* send a open port command */
12303f542974SPaul B Schroeder 	mos7840_port->open = 1;
1231880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
12323f542974SPaul B Schroeder 	mos7840_port->icount.tx = 0;
12333f542974SPaul B Schroeder 	mos7840_port->icount.rx = 0;
12343f542974SPaul B Schroeder 
123584fe6e79STony Cook 	dbg("usb_serial serial:%p       mos7840_port:%p\n      usb_serial_port port:%p",
1236880af9dbSAlan Cox 				serial, mos7840_port, port);
12373f542974SPaul B Schroeder 
123884fe6e79STony Cook 	dbg ("%s leave", __func__);
123984fe6e79STony Cook 
12403f542974SPaul B Schroeder 	return 0;
12413f542974SPaul B Schroeder 
12423f542974SPaul B Schroeder }
12433f542974SPaul B Schroeder 
12443f542974SPaul B Schroeder /*****************************************************************************
12453f542974SPaul B Schroeder  * mos7840_chars_in_buffer
12463f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
12473f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
12483f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
12493f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
12503f542974SPaul B Schroeder  *	system,
125195da310eSAlan Cox  *	Otherwise we return zero.
12523f542974SPaul B Schroeder  *****************************************************************************/
12533f542974SPaul B Schroeder 
125495da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
12553f542974SPaul B Schroeder {
125695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
12573f542974SPaul B Schroeder 	int i;
12583f542974SPaul B Schroeder 	int chars = 0;
12590de9a702SOliver Neukum 	unsigned long flags;
12603f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12613f542974SPaul B Schroeder 
126284fe6e79STony Cook 	dbg("%s", " mos7840_chars_in_buffer:entering ...........");
12633f542974SPaul B Schroeder 
1264441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
126584fe6e79STony Cook 		dbg("%s", "Invalid port");
126695da310eSAlan Cox 		return 0;
12673f542974SPaul B Schroeder 	}
12683f542974SPaul B Schroeder 
12693f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12703f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
127184fe6e79STony Cook 		dbg("%s", "mos7840_break:leaving ...........");
127295da310eSAlan Cox 		return 0;
12733f542974SPaul B Schroeder 	}
12743f542974SPaul B Schroeder 
12750de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
127695da310eSAlan Cox 	for (i = 0; i < NUM_URBS; ++i)
127795da310eSAlan Cox 		if (mos7840_port->busy[i])
12783f542974SPaul B Schroeder 			chars += URB_TRANSFER_BUFFER_SIZE;
12790de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1280441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, chars);
12810de9a702SOliver Neukum 	return chars;
12823f542974SPaul B Schroeder 
12833f542974SPaul B Schroeder }
12843f542974SPaul B Schroeder 
12853f542974SPaul B Schroeder /*****************************************************************************
12863f542974SPaul B Schroeder  * mos7840_close
12873f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
12883f542974SPaul B Schroeder  *****************************************************************************/
12893f542974SPaul B Schroeder 
1290335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
12913f542974SPaul B Schroeder {
12923f542974SPaul B Schroeder 	struct usb_serial *serial;
12933f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12940de9a702SOliver Neukum 	struct moschip_port *port0;
12953f542974SPaul B Schroeder 	int j;
12963f542974SPaul B Schroeder 	__u16 Data;
12973f542974SPaul B Schroeder 
129884fe6e79STony Cook 	dbg("%s", "mos7840_close:entering...");
12993f542974SPaul B Schroeder 
1300441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
130184fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
13023f542974SPaul B Schroeder 		return;
13033f542974SPaul B Schroeder 	}
13043f542974SPaul B Schroeder 
1305441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
13063f542974SPaul B Schroeder 	if (!serial) {
130784fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
13083f542974SPaul B Schroeder 		return;
13093f542974SPaul B Schroeder 	}
13103f542974SPaul B Schroeder 
13113f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13120de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
13133f542974SPaul B Schroeder 
13140de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
13153f542974SPaul B Schroeder 		return;
13163f542974SPaul B Schroeder 
13173f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
13183f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
13193f542974SPaul B Schroeder 
13203f542974SPaul B Schroeder 	/* Freeing Write URBs */
13213f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
13223f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
13233f542974SPaul B Schroeder 			if (mos7840_port->write_urb_pool[j]->transfer_buffer)
13243f542974SPaul B Schroeder 				kfree(mos7840_port->write_urb_pool[j]->
13253f542974SPaul B Schroeder 				      transfer_buffer);
13263f542974SPaul B Schroeder 
13273f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
13283f542974SPaul B Schroeder 		}
13293f542974SPaul B Schroeder 	}
13303f542974SPaul B Schroeder 
13313f542974SPaul B Schroeder 	/* While closing port, shutdown all bulk read, write  *
13323f542974SPaul B Schroeder 	 * and interrupt read if they exists                  */
13333f542974SPaul B Schroeder 	if (serial->dev) {
13343f542974SPaul B Schroeder 		if (mos7840_port->write_urb) {
133584fe6e79STony Cook 			dbg("%s", "Shutdown bulk write");
13363f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->write_urb);
13373f542974SPaul B Schroeder 		}
13383f542974SPaul B Schroeder 		if (mos7840_port->read_urb) {
133984fe6e79STony Cook 			dbg("%s", "Shutdown bulk read");
13403f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->read_urb);
134150de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
13423f542974SPaul B Schroeder 		}
13433f542974SPaul B Schroeder 		if ((&mos7840_port->control_urb)) {
134484fe6e79STony Cook 			dbg("%s", "Shutdown control read");
1345880af9dbSAlan Cox 			/*/      usb_kill_urb (mos7840_port->control_urb); */
13463f542974SPaul B Schroeder 		}
13473f542974SPaul B Schroeder 	}
1348880af9dbSAlan Cox /*      if(mos7840_port->ctrl_buf != NULL) */
1349880af9dbSAlan Cox /*              kfree(mos7840_port->ctrl_buf); */
13500de9a702SOliver Neukum 	port0->open_ports--;
135184fe6e79STony Cook 	dbg("mos7840_num_open_ports in close%d:in port%d",
13520de9a702SOliver Neukum 	    port0->open_ports, port->number);
13530de9a702SOliver Neukum 	if (port0->open_ports == 0) {
13543f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_urb) {
135584fe6e79STony Cook 			dbg("%s", "Shutdown interrupt_in_urb");
13560de9a702SOliver Neukum 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
13573f542974SPaul B Schroeder 		}
13583f542974SPaul B Schroeder 	}
13593f542974SPaul B Schroeder 
13603f542974SPaul B Schroeder 	if (mos7840_port->write_urb) {
13613f542974SPaul B Schroeder 		/* if this urb had a transfer buffer already (old tx) free it */
1362880af9dbSAlan Cox 		if (mos7840_port->write_urb->transfer_buffer != NULL)
13633f542974SPaul B Schroeder 			kfree(mos7840_port->write_urb->transfer_buffer);
13643f542974SPaul B Schroeder 		usb_free_urb(mos7840_port->write_urb);
13653f542974SPaul B Schroeder 	}
13663f542974SPaul B Schroeder 
13673f542974SPaul B Schroeder 	Data = 0x0;
13683f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
13693f542974SPaul B Schroeder 
13703f542974SPaul B Schroeder 	Data = 0x00;
13713f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
13723f542974SPaul B Schroeder 
13733f542974SPaul B Schroeder 	mos7840_port->open = 0;
13743f542974SPaul B Schroeder 
137584fe6e79STony Cook 	dbg("%s", "Leaving ............");
13763f542974SPaul B Schroeder }
13773f542974SPaul B Schroeder 
13783f542974SPaul B Schroeder /************************************************************************
13793f542974SPaul B Schroeder  *
13803f542974SPaul B Schroeder  * mos7840_block_until_chase_response
13813f542974SPaul B Schroeder  *
13823f542974SPaul B Schroeder  *	This function will block the close until one of the following:
13833f542974SPaul B Schroeder  *		1. Response to our Chase comes from mos7840
1384dc0d5c1eSJoe Perches  *		2. A timeout of 10 seconds without activity has expired
13853f542974SPaul B Schroeder  *		   (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
13863f542974SPaul B Schroeder  *
13873f542974SPaul B Schroeder  ************************************************************************/
13883f542974SPaul B Schroeder 
138995da310eSAlan Cox static void mos7840_block_until_chase_response(struct tty_struct *tty,
139095da310eSAlan Cox 					struct moschip_port *mos7840_port)
13913f542974SPaul B Schroeder {
13923f542974SPaul B Schroeder 	int timeout = 1 * HZ;
13933f542974SPaul B Schroeder 	int wait = 10;
13943f542974SPaul B Schroeder 	int count;
13953f542974SPaul B Schroeder 
13963f542974SPaul B Schroeder 	while (1) {
139795da310eSAlan Cox 		count = mos7840_chars_in_buffer(tty);
13983f542974SPaul B Schroeder 
13993f542974SPaul B Schroeder 		/* Check for Buffer status */
1400880af9dbSAlan Cox 		if (count <= 0)
14013f542974SPaul B Schroeder 			return;
14023f542974SPaul B Schroeder 
14033f542974SPaul B Schroeder 		/* Block the thread for a while */
14043f542974SPaul B Schroeder 		interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
14053f542974SPaul B Schroeder 					       timeout);
14063f542974SPaul B Schroeder 		/* No activity.. count down section */
14073f542974SPaul B Schroeder 		wait--;
14083f542974SPaul B Schroeder 		if (wait == 0) {
1409441b62c1SHarvey Harrison 			dbg("%s - TIMEOUT", __func__);
14103f542974SPaul B Schroeder 			return;
14113f542974SPaul B Schroeder 		} else {
1412dc0d5c1eSJoe Perches 			/* Reset timeout value back to seconds */
14133f542974SPaul B Schroeder 			wait = 10;
14143f542974SPaul B Schroeder 		}
14153f542974SPaul B Schroeder 	}
14163f542974SPaul B Schroeder 
14173f542974SPaul B Schroeder }
14183f542974SPaul B Schroeder 
14193f542974SPaul B Schroeder /*****************************************************************************
14203f542974SPaul B Schroeder  * mos7840_break
14213f542974SPaul B Schroeder  *	this function sends a break to the port
14223f542974SPaul B Schroeder  *****************************************************************************/
142395da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
14243f542974SPaul B Schroeder {
142595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14263f542974SPaul B Schroeder 	unsigned char data;
14273f542974SPaul B Schroeder 	struct usb_serial *serial;
14283f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14293f542974SPaul B Schroeder 
143084fe6e79STony Cook 	dbg("%s", "Entering ...........");
143184fe6e79STony Cook 	dbg("mos7840_break: Start");
14323f542974SPaul B Schroeder 
1433441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
143484fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
14353f542974SPaul B Schroeder 		return;
14363f542974SPaul B Schroeder 	}
14373f542974SPaul B Schroeder 
1438441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
14393f542974SPaul B Schroeder 	if (!serial) {
144084fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
14413f542974SPaul B Schroeder 		return;
14423f542974SPaul B Schroeder 	}
14433f542974SPaul B Schroeder 
14443f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14453f542974SPaul B Schroeder 
1446880af9dbSAlan Cox 	if (mos7840_port == NULL)
14473f542974SPaul B Schroeder 		return;
14483f542974SPaul B Schroeder 
144995da310eSAlan Cox 	if (serial->dev)
14503f542974SPaul B Schroeder 		/* flush and block until tx is empty */
145195da310eSAlan Cox 		mos7840_block_until_chase_response(tty, mos7840_port);
14523f542974SPaul B Schroeder 
145395da310eSAlan Cox 	if (break_state == -1)
14543f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
145595da310eSAlan Cox 	else
14563f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
14573f542974SPaul B Schroeder 
14586b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
14593f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
146084fe6e79STony Cook 	dbg("mcs7840_break mos7840_port->shadowLCR is %x",
14613f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14623f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
14633f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
14643f542974SPaul B Schroeder }
14653f542974SPaul B Schroeder 
14663f542974SPaul B Schroeder /*****************************************************************************
14673f542974SPaul B Schroeder  * mos7840_write_room
14683f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
14693f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
14703f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
14713f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
14723f542974SPaul B Schroeder  *****************************************************************************/
14733f542974SPaul B Schroeder 
147495da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
14753f542974SPaul B Schroeder {
147695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14773f542974SPaul B Schroeder 	int i;
14783f542974SPaul B Schroeder 	int room = 0;
14790de9a702SOliver Neukum 	unsigned long flags;
14803f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14813f542974SPaul B Schroeder 
148284fe6e79STony Cook 	dbg("%s", " mos7840_write_room:entering ...........");
14833f542974SPaul B Schroeder 
1484441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
148584fe6e79STony Cook 		dbg("%s", "Invalid port");
148684fe6e79STony Cook 		dbg("%s", " mos7840_write_room:leaving ...........");
14873f542974SPaul B Schroeder 		return -1;
14883f542974SPaul B Schroeder 	}
14893f542974SPaul B Schroeder 
14903f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14913f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
149284fe6e79STony Cook 		dbg("%s", "mos7840_break:leaving ...........");
14933f542974SPaul B Schroeder 		return -1;
14943f542974SPaul B Schroeder 	}
14953f542974SPaul B Schroeder 
14960de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
14973f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
1498880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
14993f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
15003f542974SPaul B Schroeder 	}
15010de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
15023f542974SPaul B Schroeder 
15030de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1504441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, room);
15050de9a702SOliver Neukum 	return room;
15063f542974SPaul B Schroeder 
15073f542974SPaul B Schroeder }
15083f542974SPaul B Schroeder 
15093f542974SPaul B Schroeder /*****************************************************************************
15103f542974SPaul B Schroeder  * mos7840_write
15113f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
15123f542974SPaul B Schroeder  *	the port.
15133f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
15143f542974SPaul B Schroeder  *      return a negative error number.
15153f542974SPaul B Schroeder  *****************************************************************************/
15163f542974SPaul B Schroeder 
151795da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
15183f542974SPaul B Schroeder 			 const unsigned char *data, int count)
15193f542974SPaul B Schroeder {
15203f542974SPaul B Schroeder 	int status;
15213f542974SPaul B Schroeder 	int i;
15223f542974SPaul B Schroeder 	int bytes_sent = 0;
15233f542974SPaul B Schroeder 	int transfer_size;
15240de9a702SOliver Neukum 	unsigned long flags;
15253f542974SPaul B Schroeder 
15263f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15273f542974SPaul B Schroeder 	struct usb_serial *serial;
15283f542974SPaul B Schroeder 	struct urb *urb;
1529880af9dbSAlan Cox 	/* __u16 Data; */
15303f542974SPaul B Schroeder 	const unsigned char *current_position = data;
15313f542974SPaul B Schroeder 	unsigned char *data1;
153284fe6e79STony Cook 	dbg("%s", "entering ...........");
153384fe6e79STony Cook 	/* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1534880af9dbSAlan Cox 					mos7840_port->shadowLCR); */
15353f542974SPaul B Schroeder 
15363f542974SPaul B Schroeder #ifdef NOTMOS7840
15373f542974SPaul B Schroeder 	Data = 0x00;
15383f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
15393f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
154084fe6e79STony Cook 	dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
154184fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
15423f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
15433f542974SPaul B Schroeder 
1544880af9dbSAlan Cox 	/* Data = 0x03; */
1545880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1546880af9dbSAlan Cox 	/* mos7840_port->shadowLCR=Data;//Need to add later */
15473f542974SPaul B Schroeder 
1548880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
15493f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
15503f542974SPaul B Schroeder 
1551880af9dbSAlan Cox 	/* Data = 0x0c; */
1552880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
15533f542974SPaul B Schroeder 	Data = 0x00;
15543f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
155584fe6e79STony Cook 	dbg("mos7840_write:DLL value is %x", Data);
15563f542974SPaul B Schroeder 
15573f542974SPaul B Schroeder 	Data = 0x0;
15583f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
155984fe6e79STony Cook 	dbg("mos7840_write:DLM value is %x", Data);
15603f542974SPaul B Schroeder 
15613f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
156284fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
15633f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
15643f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
15653f542974SPaul B Schroeder #endif
15663f542974SPaul B Schroeder 
1567441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
156884fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
15693f542974SPaul B Schroeder 		return -1;
15703f542974SPaul B Schroeder 	}
15713f542974SPaul B Schroeder 
15723f542974SPaul B Schroeder 	serial = port->serial;
1573441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
157484fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
15753f542974SPaul B Schroeder 		return -1;
15763f542974SPaul B Schroeder 	}
15773f542974SPaul B Schroeder 
15783f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15793f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
158084fe6e79STony Cook 		dbg("%s", "mos7840_port is NULL");
15813f542974SPaul B Schroeder 		return -1;
15823f542974SPaul B Schroeder 	}
15833f542974SPaul B Schroeder 
15843f542974SPaul B Schroeder 	/* try to find a free urb in the list */
15853f542974SPaul B Schroeder 	urb = NULL;
15863f542974SPaul B Schroeder 
15870de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
15883f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
15890de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
15900de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
15913f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
159284fe6e79STony Cook 			dbg("URB:%d", i);
15933f542974SPaul B Schroeder 			break;
15943f542974SPaul B Schroeder 		}
15953f542974SPaul B Schroeder 	}
15960de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
15973f542974SPaul B Schroeder 
15983f542974SPaul B Schroeder 	if (urb == NULL) {
1599441b62c1SHarvey Harrison 		dbg("%s - no more free urbs", __func__);
16003f542974SPaul B Schroeder 		goto exit;
16013f542974SPaul B Schroeder 	}
16023f542974SPaul B Schroeder 
16033f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
16043f542974SPaul B Schroeder 		urb->transfer_buffer =
16053f542974SPaul B Schroeder 		    kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
16063f542974SPaul B Schroeder 
16073f542974SPaul B Schroeder 		if (urb->transfer_buffer == NULL) {
160822a416c4SJohan Hovold 			dev_err_console(port, "%s no more kernel memory...\n",
1609194343d9SGreg Kroah-Hartman 				__func__);
16103f542974SPaul B Schroeder 			goto exit;
16113f542974SPaul B Schroeder 		}
16123f542974SPaul B Schroeder 	}
16133f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
16143f542974SPaul B Schroeder 
16153f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
16163f542974SPaul B Schroeder 
16173f542974SPaul B Schroeder 	/* fill urb with data and submit  */
1618093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1619093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1620093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1621093ea2d3SDonald Lee 		usb_fill_bulk_urb(urb,
1622093ea2d3SDonald Lee 			serial->dev,
1623093ea2d3SDonald Lee 			usb_sndbulkpipe(serial->dev,
1624093ea2d3SDonald Lee 				(port->bulk_out_endpointAddress) + 2),
1625093ea2d3SDonald Lee 			urb->transfer_buffer,
1626093ea2d3SDonald Lee 			transfer_size,
1627093ea2d3SDonald Lee 			mos7840_bulk_out_data_callback, mos7840_port);
1628093ea2d3SDonald Lee 	} else {
16293f542974SPaul B Schroeder 		usb_fill_bulk_urb(urb,
16303f542974SPaul B Schroeder 			serial->dev,
16313f542974SPaul B Schroeder 			usb_sndbulkpipe(serial->dev,
16323f542974SPaul B Schroeder 				port->bulk_out_endpointAddress),
16333f542974SPaul B Schroeder 			urb->transfer_buffer,
16343f542974SPaul B Schroeder 			transfer_size,
16353f542974SPaul B Schroeder 			mos7840_bulk_out_data_callback, mos7840_port);
1636093ea2d3SDonald Lee 	}
16373f542974SPaul B Schroeder 
16383f542974SPaul B Schroeder 	data1 = urb->transfer_buffer;
163984fe6e79STony Cook 	dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
16403f542974SPaul B Schroeder 
1641*0eafe4deSDonald 	/* Turn on LED */
1642*0eafe4deSDonald 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
1643*0eafe4deSDonald 		mos7840_port->led_flag = true;
1644*0eafe4deSDonald 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1645*0eafe4deSDonald 		mod_timer(&mos7840_port->led_timer1,
1646*0eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_ON_MS));
1647*0eafe4deSDonald 	}
1648*0eafe4deSDonald 
16493f542974SPaul B Schroeder 	/* send it down the pipe */
16503f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
16513f542974SPaul B Schroeder 
16523f542974SPaul B Schroeder 	if (status) {
16530de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
165422a416c4SJohan Hovold 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1655194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
16563f542974SPaul B Schroeder 		bytes_sent = status;
16573f542974SPaul B Schroeder 		goto exit;
16583f542974SPaul B Schroeder 	}
16593f542974SPaul B Schroeder 	bytes_sent = transfer_size;
16603f542974SPaul B Schroeder 	mos7840_port->icount.tx += transfer_size;
16610de9a702SOliver Neukum 	smp_wmb();
166284fe6e79STony Cook 	dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
16633f542974SPaul B Schroeder exit:
16643f542974SPaul B Schroeder 	return bytes_sent;
16653f542974SPaul B Schroeder 
16663f542974SPaul B Schroeder }
16673f542974SPaul B Schroeder 
16683f542974SPaul B Schroeder /*****************************************************************************
16693f542974SPaul B Schroeder  * mos7840_throttle
16703f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
16713f542974SPaul B Schroeder  *	being read from the port.
16723f542974SPaul B Schroeder  *****************************************************************************/
16733f542974SPaul B Schroeder 
167495da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
16753f542974SPaul B Schroeder {
167695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16773f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
16783f542974SPaul B Schroeder 	int status;
16793f542974SPaul B Schroeder 
1680441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
168184fe6e79STony Cook 		dbg("%s", "Invalid port");
16823f542974SPaul B Schroeder 		return;
16833f542974SPaul B Schroeder 	}
16843f542974SPaul B Schroeder 
168584fe6e79STony Cook 	dbg("- port %d", port->number);
16863f542974SPaul B Schroeder 
16873f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
16883f542974SPaul B Schroeder 
16893f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16903f542974SPaul B Schroeder 		return;
16913f542974SPaul B Schroeder 
16923f542974SPaul B Schroeder 	if (!mos7840_port->open) {
169384fe6e79STony Cook 		dbg("%s", "port not opened");
16943f542974SPaul B Schroeder 		return;
16953f542974SPaul B Schroeder 	}
16963f542974SPaul B Schroeder 
169784fe6e79STony Cook 	dbg("%s", "Entering ..........");
16983f542974SPaul B Schroeder 
16993f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
17003f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
17013f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
170295da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
170395da310eSAlan Cox 		if (status <= 0)
17043f542974SPaul B Schroeder 			return;
17053f542974SPaul B Schroeder 	}
17063f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
17073f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
17083f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
170995da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
17103f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
171195da310eSAlan Cox 		if (status < 0)
17123f542974SPaul B Schroeder 			return;
17133f542974SPaul B Schroeder 	}
17143f542974SPaul B Schroeder }
17153f542974SPaul B Schroeder 
17163f542974SPaul B Schroeder /*****************************************************************************
17173f542974SPaul B Schroeder  * mos7840_unthrottle
1718880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
1719880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
1720880af9dbSAlan Cox  *	called)
17213f542974SPaul B Schroeder  *****************************************************************************/
172295da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
17233f542974SPaul B Schroeder {
172495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
17253f542974SPaul B Schroeder 	int status;
17263f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
17273f542974SPaul B Schroeder 
1728441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
172984fe6e79STony Cook 		dbg("%s", "Invalid port");
17303f542974SPaul B Schroeder 		return;
17313f542974SPaul B Schroeder 	}
17323f542974SPaul B Schroeder 
17333f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17343f542974SPaul B Schroeder 		return;
17353f542974SPaul B Schroeder 
17363f542974SPaul B Schroeder 	if (!mos7840_port->open) {
1737441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
17383f542974SPaul B Schroeder 		return;
17393f542974SPaul B Schroeder 	}
17403f542974SPaul B Schroeder 
174184fe6e79STony Cook 	dbg("%s", "Entering ..........");
17423f542974SPaul B Schroeder 
17433f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
17443f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
17453f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
174695da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
174795da310eSAlan Cox 		if (status <= 0)
17483f542974SPaul B Schroeder 			return;
17493f542974SPaul B Schroeder 	}
17503f542974SPaul B Schroeder 
17513f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
17523f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
17533f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
175495da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
17553f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
175695da310eSAlan Cox 		if (status < 0)
17573f542974SPaul B Schroeder 			return;
17583f542974SPaul B Schroeder 	}
17593f542974SPaul B Schroeder }
17603f542974SPaul B Schroeder 
176160b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
17623f542974SPaul B Schroeder {
176395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
17643f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
17653f542974SPaul B Schroeder 	unsigned int result;
17663f542974SPaul B Schroeder 	__u16 msr;
17673f542974SPaul B Schroeder 	__u16 mcr;
1768880af9dbSAlan Cox 	int status;
17693f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
17703f542974SPaul B Schroeder 
1771441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
17723f542974SPaul B Schroeder 
17733f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17743f542974SPaul B Schroeder 		return -ENODEV;
17753f542974SPaul B Schroeder 
17763f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
17773f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
17783f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
17793f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
17803f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
17813f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
17823f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
17833f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
17843f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
17853f542974SPaul B Schroeder 
1786441b62c1SHarvey Harrison 	dbg("%s - 0x%04X", __func__, result);
17873f542974SPaul B Schroeder 
17883f542974SPaul B Schroeder 	return result;
17893f542974SPaul B Schroeder }
17903f542974SPaul B Schroeder 
179120b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
17923f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
17933f542974SPaul B Schroeder {
179495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
17953f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
17963f542974SPaul B Schroeder 	unsigned int mcr;
179787521c46SRoel Kluin 	int status;
17983f542974SPaul B Schroeder 
1799441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
18003f542974SPaul B Schroeder 
18013f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
18023f542974SPaul B Schroeder 
18033f542974SPaul B Schroeder 	if (mos7840_port == NULL)
18043f542974SPaul B Schroeder 		return -ENODEV;
18053f542974SPaul B Schroeder 
1806e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
18073f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
18083f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
18093f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
18103f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
18113f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
18123f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
18133f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
18143f542974SPaul B Schroeder 
18153f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
18163f542974SPaul B Schroeder 		mcr |= MCR_RTS;
18173f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
18183f542974SPaul B Schroeder 		mcr |= MCR_DTR;
18193f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
18203f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
18213f542974SPaul B Schroeder 
18223f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
18233f542974SPaul B Schroeder 
18243f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
18253f542974SPaul B Schroeder 	if (status < 0) {
182684fe6e79STony Cook 		dbg("setting MODEM_CONTROL_REGISTER Failed");
182787521c46SRoel Kluin 		return status;
18283f542974SPaul B Schroeder 	}
18293f542974SPaul B Schroeder 
18303f542974SPaul B Schroeder 	return 0;
18313f542974SPaul B Schroeder }
18323f542974SPaul B Schroeder 
18333f542974SPaul B Schroeder /*****************************************************************************
18343f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
18353f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
18363f542974SPaul B Schroeder  *	baud rate.
18373f542974SPaul B Schroeder  *****************************************************************************/
18383f542974SPaul B Schroeder static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
18393f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
18403f542974SPaul B Schroeder {
18413f542974SPaul B Schroeder 
1842441b62c1SHarvey Harrison 	dbg("%s - %d", __func__, baudRate);
18433f542974SPaul B Schroeder 
18443f542974SPaul B Schroeder 	if (baudRate <= 115200) {
18453f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
18463f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
18473f542974SPaul B Schroeder 	}
18483f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
18493f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
18503f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
18513f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
18523f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
18533f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
18543f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
18553f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
18563f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
18573f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
18583f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
18593f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
18603f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
18613f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
18623f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
18633f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
18643f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
18653f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
18663f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
18673f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
18683f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
18693f542974SPaul B Schroeder 	}
18703f542974SPaul B Schroeder 	return 0;
18713f542974SPaul B Schroeder 
18723f542974SPaul B Schroeder #ifdef NOTMCS7840
18733f542974SPaul B Schroeder 
18743f542974SPaul B Schroeder 	for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
18753f542974SPaul B Schroeder 		if (mos7840_divisor_table[i].BaudRate == baudrate) {
18763f542974SPaul B Schroeder 			*divisor = mos7840_divisor_table[i].Divisor;
18773f542974SPaul B Schroeder 			return 0;
18783f542974SPaul B Schroeder 		}
18793f542974SPaul B Schroeder 	}
18803f542974SPaul B Schroeder 
18813f542974SPaul B Schroeder 	/* After trying for all the standard baud rates    *
18823f542974SPaul B Schroeder 	 * Try calculating the divisor for this baud rate  */
18833f542974SPaul B Schroeder 
18843f542974SPaul B Schroeder 	if (baudrate > 75 && baudrate < 230400) {
18853f542974SPaul B Schroeder 		/* get the divisor */
18863f542974SPaul B Schroeder 		custom = (__u16) (230400L / baudrate);
18873f542974SPaul B Schroeder 
18883f542974SPaul B Schroeder 		/* Check for round off */
18893f542974SPaul B Schroeder 		round1 = (__u16) (2304000L / baudrate);
18903f542974SPaul B Schroeder 		round = (__u16) (round1 - (custom * 10));
1891880af9dbSAlan Cox 		if (round > 4)
18923f542974SPaul B Schroeder 			custom++;
18933f542974SPaul B Schroeder 		*divisor = custom;
18943f542974SPaul B Schroeder 
189584fe6e79STony Cook 		dbg(" Baud %d = %d", baudrate, custom);
18963f542974SPaul B Schroeder 		return 0;
18973f542974SPaul B Schroeder 	}
18983f542974SPaul B Schroeder 
189984fe6e79STony Cook 	dbg("%s", " Baud calculation Failed...");
19003f542974SPaul B Schroeder 	return -1;
19013f542974SPaul B Schroeder #endif
19023f542974SPaul B Schroeder }
19033f542974SPaul B Schroeder 
19043f542974SPaul B Schroeder /*****************************************************************************
19053f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
19063f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
19073f542974SPaul B Schroeder  *	specified port.
19083f542974SPaul B Schroeder  *****************************************************************************/
19093f542974SPaul B Schroeder 
19103f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
19113f542974SPaul B Schroeder 					    int baudRate)
19123f542974SPaul B Schroeder {
19133f542974SPaul B Schroeder 	int divisor = 0;
19143f542974SPaul B Schroeder 	int status;
19153f542974SPaul B Schroeder 	__u16 Data;
19163f542974SPaul B Schroeder 	unsigned char number;
19173f542974SPaul B Schroeder 	__u16 clk_sel_val;
19183f542974SPaul B Schroeder 	struct usb_serial_port *port;
19193f542974SPaul B Schroeder 
19203f542974SPaul B Schroeder 	if (mos7840_port == NULL)
19213f542974SPaul B Schroeder 		return -1;
19223f542974SPaul B Schroeder 
19233f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
1924441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
192584fe6e79STony Cook 		dbg("%s", "Invalid port");
19263f542974SPaul B Schroeder 		return -1;
19273f542974SPaul B Schroeder 	}
19283f542974SPaul B Schroeder 
1929441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
193084fe6e79STony Cook 		dbg("%s", "Invalid Serial");
19313f542974SPaul B Schroeder 		return -1;
19323f542974SPaul B Schroeder 	}
19333f542974SPaul B Schroeder 
193484fe6e79STony Cook 	dbg("%s", "Entering ..........");
19353f542974SPaul B Schroeder 
19363f542974SPaul B Schroeder 	number = mos7840_port->port->number - mos7840_port->port->serial->minor;
19373f542974SPaul B Schroeder 
1938441b62c1SHarvey Harrison 	dbg("%s - port = %d, baud = %d", __func__,
19393f542974SPaul B Schroeder 	    mos7840_port->port->number, baudRate);
1940880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
19413f542974SPaul B Schroeder 	if (baudRate > 115200) {
19423f542974SPaul B Schroeder #ifdef HW_flow_control
1943880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1944880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
19453f542974SPaul B Schroeder 		Data = 0x2b;
19463f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1947880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1948880af9dbSAlan Cox 									Data);
19493f542974SPaul B Schroeder 		if (status < 0) {
195084fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
19513f542974SPaul B Schroeder 			return -1;
19523f542974SPaul B Schroeder 		}
19533f542974SPaul B Schroeder #endif
19543f542974SPaul B Schroeder 
19553f542974SPaul B Schroeder 	} else {
19563f542974SPaul B Schroeder #ifdef HW_flow_control
1957880af9dbSAlan Cox 		/* setting h/w flow control bit to 0 */
19583f542974SPaul B Schroeder 		Data = 0xb;
19593f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1960880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1961880af9dbSAlan Cox 									Data);
19623f542974SPaul B Schroeder 		if (status < 0) {
196384fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
19643f542974SPaul B Schroeder 			return -1;
19653f542974SPaul B Schroeder 		}
19663f542974SPaul B Schroeder #endif
19673f542974SPaul B Schroeder 
19683f542974SPaul B Schroeder 	}
19693f542974SPaul B Schroeder 
1970880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
19713f542974SPaul B Schroeder 		clk_sel_val = 0x0;
19723f542974SPaul B Schroeder 		Data = 0x0;
1973880af9dbSAlan Cox 		status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
19743f542974SPaul B Schroeder 						   &clk_sel_val);
1975880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
19763f542974SPaul B Schroeder 								 &Data);
19773f542974SPaul B Schroeder 		if (status < 0) {
197884fe6e79STony Cook 			dbg("reading spreg failed in set_serial_baud");
19793f542974SPaul B Schroeder 			return -1;
19803f542974SPaul B Schroeder 		}
19813f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1982880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1983880af9dbSAlan Cox 								Data);
19843f542974SPaul B Schroeder 		if (status < 0) {
198584fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
19863f542974SPaul B Schroeder 			return -1;
19873f542974SPaul B Schroeder 		}
19883f542974SPaul B Schroeder 		/* Calculate the Divisor */
19893f542974SPaul B Schroeder 
19903f542974SPaul B Schroeder 		if (status) {
1991194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
19923f542974SPaul B Schroeder 			return status;
19933f542974SPaul B Schroeder 		}
19943f542974SPaul B Schroeder 		/* Enable access to divisor latch */
19953f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
19963f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
19973f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19983f542974SPaul B Schroeder 
19993f542974SPaul B Schroeder 		/* Write the divisor */
20003f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
200184fe6e79STony Cook 		dbg("set_serial_baud Value to write DLL is %x", Data);
20023f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
20033f542974SPaul B Schroeder 
20043f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
200584fe6e79STony Cook 		dbg("set_serial_baud Value to write DLM is %x", Data);
20063f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
20073f542974SPaul B Schroeder 
20083f542974SPaul B Schroeder 		/* Disable access to divisor latch */
20093f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
20103f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
20113f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
20123f542974SPaul B Schroeder 
20133f542974SPaul B Schroeder 	}
20143f542974SPaul B Schroeder 	return status;
20153f542974SPaul B Schroeder }
20163f542974SPaul B Schroeder 
20173f542974SPaul B Schroeder /*****************************************************************************
20183f542974SPaul B Schroeder  * mos7840_change_port_settings
20193f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
20203f542974SPaul B Schroeder  *      the specified new settings.
20213f542974SPaul B Schroeder  *****************************************************************************/
20223f542974SPaul B Schroeder 
202395da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
202495da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
20253f542974SPaul B Schroeder {
20263f542974SPaul B Schroeder 	int baud;
20273f542974SPaul B Schroeder 	unsigned cflag;
20283f542974SPaul B Schroeder 	unsigned iflag;
20293f542974SPaul B Schroeder 	__u8 lData;
20303f542974SPaul B Schroeder 	__u8 lParity;
20313f542974SPaul B Schroeder 	__u8 lStop;
20323f542974SPaul B Schroeder 	int status;
20333f542974SPaul B Schroeder 	__u16 Data;
20343f542974SPaul B Schroeder 	struct usb_serial_port *port;
20353f542974SPaul B Schroeder 	struct usb_serial *serial;
20363f542974SPaul B Schroeder 
20373f542974SPaul B Schroeder 	if (mos7840_port == NULL)
20383f542974SPaul B Schroeder 		return;
20393f542974SPaul B Schroeder 
20403f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
20413f542974SPaul B Schroeder 
2042441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
204384fe6e79STony Cook 		dbg("%s", "Invalid port");
20443f542974SPaul B Schroeder 		return;
20453f542974SPaul B Schroeder 	}
20463f542974SPaul B Schroeder 
2047441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
204884fe6e79STony Cook 		dbg("%s", "Invalid Serial");
20493f542974SPaul B Schroeder 		return;
20503f542974SPaul B Schroeder 	}
20513f542974SPaul B Schroeder 
20523f542974SPaul B Schroeder 	serial = port->serial;
20533f542974SPaul B Schroeder 
2054441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, mos7840_port->port->number);
20553f542974SPaul B Schroeder 
20563f542974SPaul B Schroeder 	if (!mos7840_port->open) {
2057441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
20583f542974SPaul B Schroeder 		return;
20593f542974SPaul B Schroeder 	}
20603f542974SPaul B Schroeder 
206184fe6e79STony Cook 	dbg("%s", "Entering ..........");
20623f542974SPaul B Schroeder 
20633f542974SPaul B Schroeder 	lData = LCR_BITS_8;
20643f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
20653f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
20663f542974SPaul B Schroeder 
20673f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
20683f542974SPaul B Schroeder 	iflag = tty->termios->c_iflag;
20693f542974SPaul B Schroeder 
20703f542974SPaul B Schroeder 	/* Change the number of bits */
20713f542974SPaul B Schroeder 	if (cflag & CSIZE) {
20723f542974SPaul B Schroeder 		switch (cflag & CSIZE) {
20733f542974SPaul B Schroeder 		case CS5:
20743f542974SPaul B Schroeder 			lData = LCR_BITS_5;
20753f542974SPaul B Schroeder 			break;
20763f542974SPaul B Schroeder 
20773f542974SPaul B Schroeder 		case CS6:
20783f542974SPaul B Schroeder 			lData = LCR_BITS_6;
20793f542974SPaul B Schroeder 			break;
20803f542974SPaul B Schroeder 
20813f542974SPaul B Schroeder 		case CS7:
20823f542974SPaul B Schroeder 			lData = LCR_BITS_7;
20833f542974SPaul B Schroeder 			break;
20843f542974SPaul B Schroeder 		default:
20853f542974SPaul B Schroeder 		case CS8:
20863f542974SPaul B Schroeder 			lData = LCR_BITS_8;
20873f542974SPaul B Schroeder 			break;
20883f542974SPaul B Schroeder 		}
20893f542974SPaul B Schroeder 	}
20903f542974SPaul B Schroeder 	/* Change the Parity bit */
20913f542974SPaul B Schroeder 	if (cflag & PARENB) {
20923f542974SPaul B Schroeder 		if (cflag & PARODD) {
20933f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
2094441b62c1SHarvey Harrison 			dbg("%s - parity = odd", __func__);
20953f542974SPaul B Schroeder 		} else {
20963f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
2097441b62c1SHarvey Harrison 			dbg("%s - parity = even", __func__);
20983f542974SPaul B Schroeder 		}
20993f542974SPaul B Schroeder 
21003f542974SPaul B Schroeder 	} else {
2101441b62c1SHarvey Harrison 		dbg("%s - parity = none", __func__);
21023f542974SPaul B Schroeder 	}
21033f542974SPaul B Schroeder 
2104880af9dbSAlan Cox 	if (cflag & CMSPAR)
21053f542974SPaul B Schroeder 		lParity = lParity | 0x20;
21063f542974SPaul B Schroeder 
21073f542974SPaul B Schroeder 	/* Change the Stop bit */
21083f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
21093f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
2110441b62c1SHarvey Harrison 		dbg("%s - stop bits = 2", __func__);
21113f542974SPaul B Schroeder 	} else {
21123f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
2113441b62c1SHarvey Harrison 		dbg("%s - stop bits = 1", __func__);
21143f542974SPaul B Schroeder 	}
21153f542974SPaul B Schroeder 
21163f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
21173f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
21183f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
21193f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
21203f542974SPaul B Schroeder 
212184fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
21223f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
21233f542974SPaul B Schroeder 	/* Disable Interrupts */
21243f542974SPaul B Schroeder 	Data = 0x00;
21253f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
21263f542974SPaul B Schroeder 
21273f542974SPaul B Schroeder 	Data = 0x00;
21283f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
21293f542974SPaul B Schroeder 
21303f542974SPaul B Schroeder 	Data = 0xcf;
21313f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
21323f542974SPaul B Schroeder 
21333f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
21343f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
21353f542974SPaul B Schroeder 
21363f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
21373f542974SPaul B Schroeder 
21383f542974SPaul B Schroeder 	Data = 0x00b;
21393f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
21403f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
21413f542974SPaul B Schroeder 	Data = 0x00b;
21423f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
21433f542974SPaul B Schroeder 
21443f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
21453f542974SPaul B Schroeder 
21463f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
2147880af9dbSAlan Cox 	if (cflag & CBAUD)
21483f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
21493f542974SPaul B Schroeder 
2150880af9dbSAlan Cox 	if (cflag & CRTSCTS)
21513f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
2152880af9dbSAlan Cox 	else
21533f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
21543f542974SPaul B Schroeder 
21553f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
21563f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
21573f542974SPaul B Schroeder 
21583f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
21593f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
21603f542974SPaul B Schroeder 
21613f542974SPaul B Schroeder 	if (!baud) {
21623f542974SPaul B Schroeder 		/* pick a default, any default... */
216384fe6e79STony Cook 		dbg("%s", "Picked default baud...");
21643f542974SPaul B Schroeder 		baud = 9600;
21653f542974SPaul B Schroeder 	}
21663f542974SPaul B Schroeder 
2167441b62c1SHarvey Harrison 	dbg("%s - baud rate = %d", __func__, baud);
21683f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
21693f542974SPaul B Schroeder 
21703f542974SPaul B Schroeder 	/* Enable Interrupts */
21713f542974SPaul B Schroeder 	Data = 0x0c;
21723f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
21733f542974SPaul B Schroeder 
217450de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
217550de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
21763f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
21773f542974SPaul B Schroeder 		if (status) {
21783f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
21793f542974SPaul B Schroeder 			    status);
218050de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
21813f542974SPaul B Schroeder 		}
21823f542974SPaul B Schroeder 	}
21833f542974SPaul B Schroeder 	wake_up(&mos7840_port->delta_msr_wait);
21843f542974SPaul B Schroeder 	mos7840_port->delta_msr_cond = 1;
218584fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
21863f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
21873f542974SPaul B Schroeder }
21883f542974SPaul B Schroeder 
21893f542974SPaul B Schroeder /*****************************************************************************
21903f542974SPaul B Schroeder  * mos7840_set_termios
21913f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
21923f542974SPaul B Schroeder  *	the termios structure
21933f542974SPaul B Schroeder  *****************************************************************************/
21943f542974SPaul B Schroeder 
219595da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
219695da310eSAlan Cox 				struct usb_serial_port *port,
2197606d099cSAlan Cox 				struct ktermios *old_termios)
21983f542974SPaul B Schroeder {
21993f542974SPaul B Schroeder 	int status;
22003f542974SPaul B Schroeder 	unsigned int cflag;
22013f542974SPaul B Schroeder 	struct usb_serial *serial;
22023f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
220384fe6e79STony Cook 	dbg("mos7840_set_termios: START");
2204441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
220584fe6e79STony Cook 		dbg("%s", "Invalid port");
22063f542974SPaul B Schroeder 		return;
22073f542974SPaul B Schroeder 	}
22083f542974SPaul B Schroeder 
22093f542974SPaul B Schroeder 	serial = port->serial;
22103f542974SPaul B Schroeder 
2211441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
221284fe6e79STony Cook 		dbg("%s", "Invalid Serial");
22133f542974SPaul B Schroeder 		return;
22143f542974SPaul B Schroeder 	}
22153f542974SPaul B Schroeder 
22163f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
22173f542974SPaul B Schroeder 
22183f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22193f542974SPaul B Schroeder 		return;
22203f542974SPaul B Schroeder 
22213f542974SPaul B Schroeder 	if (!mos7840_port->open) {
2222441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
22233f542974SPaul B Schroeder 		return;
22243f542974SPaul B Schroeder 	}
22253f542974SPaul B Schroeder 
222684fe6e79STony Cook 	dbg("%s", "setting termios - ");
22273f542974SPaul B Schroeder 
22283f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
22293f542974SPaul B Schroeder 
2230441b62c1SHarvey Harrison 	dbg("%s - clfag %08x iflag %08x", __func__,
22313f542974SPaul B Schroeder 	    tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2232441b62c1SHarvey Harrison 	dbg("%s - old clfag %08x old iflag %08x", __func__,
22333f542974SPaul B Schroeder 	    old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2234441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
22353f542974SPaul B Schroeder 
22363f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
22373f542974SPaul B Schroeder 
223895da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
22393f542974SPaul B Schroeder 
22403f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
224184fe6e79STony Cook 		dbg("%s", "URB KILLED !!!!!");
22423f542974SPaul B Schroeder 		return;
22433f542974SPaul B Schroeder 	}
22443f542974SPaul B Schroeder 
224550de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
224650de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
22473f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
22483f542974SPaul B Schroeder 		if (status) {
22493f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
22503f542974SPaul B Schroeder 			    status);
225150de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
22523f542974SPaul B Schroeder 		}
22533f542974SPaul B Schroeder 	}
22543f542974SPaul B Schroeder }
22553f542974SPaul B Schroeder 
22563f542974SPaul B Schroeder /*****************************************************************************
22573f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
22583f542974SPaul B Schroeder  *
22593f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
22603f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
22613f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
22623f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
22633f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
22643f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
22653f542974SPaul B Schroeder  *****************************************************************************/
22663f542974SPaul B Schroeder 
226795da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
226897c4965dSAl Viro 				unsigned int __user *value)
22693f542974SPaul B Schroeder {
22703f542974SPaul B Schroeder 	int count;
22713f542974SPaul B Schroeder 	unsigned int result = 0;
22723f542974SPaul B Schroeder 
227395da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
22743f542974SPaul B Schroeder 	if (count == 0) {
2275441b62c1SHarvey Harrison 		dbg("%s -- Empty", __func__);
22763f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
22773f542974SPaul B Schroeder 	}
22783f542974SPaul B Schroeder 
22793f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
22803f542974SPaul B Schroeder 		return -EFAULT;
22813f542974SPaul B Schroeder 	return 0;
22823f542974SPaul B Schroeder }
22833f542974SPaul B Schroeder 
22843f542974SPaul B Schroeder /*****************************************************************************
22853f542974SPaul B Schroeder  * mos7840_get_serial_info
22863f542974SPaul B Schroeder  *      function to get information about serial port
22873f542974SPaul B Schroeder  *****************************************************************************/
22883f542974SPaul B Schroeder 
22893f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
229097c4965dSAl Viro 				   struct serial_struct __user *retinfo)
22913f542974SPaul B Schroeder {
22923f542974SPaul B Schroeder 	struct serial_struct tmp;
22933f542974SPaul B Schroeder 
22943f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22953f542974SPaul B Schroeder 		return -1;
22963f542974SPaul B Schroeder 
22973f542974SPaul B Schroeder 	if (!retinfo)
22983f542974SPaul B Schroeder 		return -EFAULT;
22993f542974SPaul B Schroeder 
23003f542974SPaul B Schroeder 	memset(&tmp, 0, sizeof(tmp));
23013f542974SPaul B Schroeder 
23023f542974SPaul B Schroeder 	tmp.type = PORT_16550A;
23033f542974SPaul B Schroeder 	tmp.line = mos7840_port->port->serial->minor;
23043f542974SPaul B Schroeder 	tmp.port = mos7840_port->port->number;
23053f542974SPaul B Schroeder 	tmp.irq = 0;
23063f542974SPaul B Schroeder 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
23073f542974SPaul B Schroeder 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
23083f542974SPaul B Schroeder 	tmp.baud_base = 9600;
23093f542974SPaul B Schroeder 	tmp.close_delay = 5 * HZ;
23103f542974SPaul B Schroeder 	tmp.closing_wait = 30 * HZ;
23113f542974SPaul B Schroeder 
23123f542974SPaul B Schroeder 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
23133f542974SPaul B Schroeder 		return -EFAULT;
23143f542974SPaul B Schroeder 	return 0;
23153f542974SPaul B Schroeder }
23163f542974SPaul B Schroeder 
23170bca1b91SAlan Cox static int mos7840_get_icount(struct tty_struct *tty,
23180bca1b91SAlan Cox 			struct serial_icounter_struct *icount)
23190bca1b91SAlan Cox {
23200bca1b91SAlan Cox 	struct usb_serial_port *port = tty->driver_data;
23210bca1b91SAlan Cox 	struct moschip_port *mos7840_port;
23220bca1b91SAlan Cox 	struct async_icount cnow;
23230bca1b91SAlan Cox 
23240bca1b91SAlan Cox 	mos7840_port = mos7840_get_port_private(port);
23250bca1b91SAlan Cox 	cnow = mos7840_port->icount;
23260bca1b91SAlan Cox 
23270bca1b91SAlan Cox 	smp_rmb();
23280bca1b91SAlan Cox 	icount->cts = cnow.cts;
23290bca1b91SAlan Cox 	icount->dsr = cnow.dsr;
23300bca1b91SAlan Cox 	icount->rng = cnow.rng;
23310bca1b91SAlan Cox 	icount->dcd = cnow.dcd;
23320bca1b91SAlan Cox 	icount->rx = cnow.rx;
23330bca1b91SAlan Cox 	icount->tx = cnow.tx;
23340bca1b91SAlan Cox 	icount->frame = cnow.frame;
23350bca1b91SAlan Cox 	icount->overrun = cnow.overrun;
23360bca1b91SAlan Cox 	icount->parity = cnow.parity;
23370bca1b91SAlan Cox 	icount->brk = cnow.brk;
23380bca1b91SAlan Cox 	icount->buf_overrun = cnow.buf_overrun;
23390bca1b91SAlan Cox 
23400bca1b91SAlan Cox 	dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
23410bca1b91SAlan Cox 		port->number, icount->rx, icount->tx);
23420bca1b91SAlan Cox 	return 0;
23430bca1b91SAlan Cox }
23440bca1b91SAlan Cox 
23453f542974SPaul B Schroeder /*****************************************************************************
23463f542974SPaul B Schroeder  * SerialIoctl
23473f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
23483f542974SPaul B Schroeder  *****************************************************************************/
23493f542974SPaul B Schroeder 
235000a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
23513f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
23523f542974SPaul B Schroeder {
235395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
235497c4965dSAl Viro 	void __user *argp = (void __user *)arg;
23553f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
23563f542974SPaul B Schroeder 
23573f542974SPaul B Schroeder 	struct async_icount cnow;
23583f542974SPaul B Schroeder 	struct async_icount cprev;
23593f542974SPaul B Schroeder 
2360441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
236184fe6e79STony Cook 		dbg("%s", "Invalid port");
23623f542974SPaul B Schroeder 		return -1;
23633f542974SPaul B Schroeder 	}
23643f542974SPaul B Schroeder 
23653f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
23663f542974SPaul B Schroeder 
23673f542974SPaul B Schroeder 	if (mos7840_port == NULL)
23683f542974SPaul B Schroeder 		return -1;
23693f542974SPaul B Schroeder 
2370441b62c1SHarvey Harrison 	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
23713f542974SPaul B Schroeder 
23723f542974SPaul B Schroeder 	switch (cmd) {
23733f542974SPaul B Schroeder 		/* return number of bytes available */
23743f542974SPaul B Schroeder 
23753f542974SPaul B Schroeder 	case TIOCSERGETLSR:
2376441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
237795da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
23783f542974SPaul B Schroeder 
23793f542974SPaul B Schroeder 	case TIOCGSERIAL:
2380441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
238197c4965dSAl Viro 		return mos7840_get_serial_info(mos7840_port, argp);
23823f542974SPaul B Schroeder 
23833f542974SPaul B Schroeder 	case TIOCSSERIAL:
2384441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
23853f542974SPaul B Schroeder 		break;
23863f542974SPaul B Schroeder 
23873f542974SPaul B Schroeder 	case TIOCMIWAIT:
2388441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
23893f542974SPaul B Schroeder 		cprev = mos7840_port->icount;
23903f542974SPaul B Schroeder 		while (1) {
2391880af9dbSAlan Cox 			/* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
23923f542974SPaul B Schroeder 			mos7840_port->delta_msr_cond = 0;
23933f542974SPaul B Schroeder 			wait_event_interruptible(mos7840_port->delta_msr_wait,
23943f542974SPaul B Schroeder 						 (mos7840_port->
23953f542974SPaul B Schroeder 						  delta_msr_cond == 1));
23963f542974SPaul B Schroeder 
23973f542974SPaul B Schroeder 			/* see if a signal did it */
23983f542974SPaul B Schroeder 			if (signal_pending(current))
23993f542974SPaul B Schroeder 				return -ERESTARTSYS;
24003f542974SPaul B Schroeder 			cnow = mos7840_port->icount;
24010de9a702SOliver Neukum 			smp_rmb();
24023f542974SPaul B Schroeder 			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
24033f542974SPaul B Schroeder 			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
24043f542974SPaul B Schroeder 				return -EIO;	/* no change => error */
24053f542974SPaul B Schroeder 			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
24063f542974SPaul B Schroeder 			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
24073f542974SPaul B Schroeder 			    ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
24083f542974SPaul B Schroeder 			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
24093f542974SPaul B Schroeder 				return 0;
24103f542974SPaul B Schroeder 			}
24113f542974SPaul B Schroeder 			cprev = cnow;
24123f542974SPaul B Schroeder 		}
24133f542974SPaul B Schroeder 		/* NOTREACHED */
24143f542974SPaul B Schroeder 		break;
24153f542974SPaul B Schroeder 
24163f542974SPaul B Schroeder 	default:
24173f542974SPaul B Schroeder 		break;
24183f542974SPaul B Schroeder 	}
24193f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
24203f542974SPaul B Schroeder }
24213f542974SPaul B Schroeder 
2422*0eafe4deSDonald static int mos7810_check(struct usb_serial *serial)
24233f542974SPaul B Schroeder {
2424*0eafe4deSDonald 	int i, pass_count = 0;
2425*0eafe4deSDonald 	__u16 data = 0, mcr_data = 0;
2426*0eafe4deSDonald 	__u16 test_pattern = 0x55AA;
24273f542974SPaul B Schroeder 
2428*0eafe4deSDonald 	/* Store MCR setting */
2429*0eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2430*0eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2431*0eafe4deSDonald 		&mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2432*0eafe4deSDonald 
2433*0eafe4deSDonald 	for (i = 0; i < 16; i++) {
2434*0eafe4deSDonald 		/* Send the 1-bit test pattern out to MCS7810 test pin */
2435*0eafe4deSDonald 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2436*0eafe4deSDonald 			MCS_WRREQ, MCS_WR_RTYPE,
2437*0eafe4deSDonald 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2438*0eafe4deSDonald 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2439*0eafe4deSDonald 
2440*0eafe4deSDonald 		/* Read the test pattern back */
2441*0eafe4deSDonald 		usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2442*0eafe4deSDonald 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2443093ea2d3SDonald Lee 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2444093ea2d3SDonald Lee 
2445*0eafe4deSDonald 		/* If this is a MCS7810 device, both test patterns must match */
2446*0eafe4deSDonald 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2447*0eafe4deSDonald 			break;
2448*0eafe4deSDonald 
2449*0eafe4deSDonald 		pass_count++;
24503f542974SPaul B Schroeder 	}
2451093ea2d3SDonald Lee 
2452*0eafe4deSDonald 	/* Restore MCR setting */
2453*0eafe4deSDonald 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2454*0eafe4deSDonald 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2455*0eafe4deSDonald 		0, MOS_WDR_TIMEOUT);
2456*0eafe4deSDonald 
2457*0eafe4deSDonald 	if (pass_count == 16)
2458*0eafe4deSDonald 		return 1;
2459*0eafe4deSDonald 
2460*0eafe4deSDonald 	return 0;
2461*0eafe4deSDonald }
2462*0eafe4deSDonald 
2463*0eafe4deSDonald static int mos7840_calc_num_ports(struct usb_serial *serial)
2464*0eafe4deSDonald {
2465*0eafe4deSDonald 	__u16 data = 0x00;
2466*0eafe4deSDonald 	int mos7840_num_ports;
2467*0eafe4deSDonald 
2468*0eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2469*0eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2470*0eafe4deSDonald 		VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2471*0eafe4deSDonald 
2472*0eafe4deSDonald 	if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2473*0eafe4deSDonald 		serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2474*0eafe4deSDonald 		device_type = serial->dev->descriptor.idProduct;
2475*0eafe4deSDonald 	} else {
2476*0eafe4deSDonald 		/* For a MCS7840 device GPIO0 must be set to 1 */
2477*0eafe4deSDonald 		if ((data & 0x01) == 1)
2478*0eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7840;
2479*0eafe4deSDonald 		else if (mos7810_check(serial))
2480*0eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7810;
2481*0eafe4deSDonald 		else
2482*0eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7820;
2483*0eafe4deSDonald 	}
2484*0eafe4deSDonald 
2485*0eafe4deSDonald 	mos7840_num_ports = (device_type >> 4) & 0x000F;
2486*0eafe4deSDonald 	serial->num_bulk_in = mos7840_num_ports;
2487*0eafe4deSDonald 	serial->num_bulk_out = mos7840_num_ports;
2488*0eafe4deSDonald 	serial->num_ports = mos7840_num_ports;
2489*0eafe4deSDonald 
24903f542974SPaul B Schroeder 	return mos7840_num_ports;
24913f542974SPaul B Schroeder }
24923f542974SPaul B Schroeder 
24933f542974SPaul B Schroeder /****************************************************************************
24943f542974SPaul B Schroeder  * mos7840_startup
24953f542974SPaul B Schroeder  ****************************************************************************/
24963f542974SPaul B Schroeder 
24973f542974SPaul B Schroeder static int mos7840_startup(struct usb_serial *serial)
24983f542974SPaul B Schroeder {
24993f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
25003f542974SPaul B Schroeder 	struct usb_device *dev;
25013f542974SPaul B Schroeder 	int i, status;
25023f542974SPaul B Schroeder 
25033f542974SPaul B Schroeder 	__u16 Data;
250484fe6e79STony Cook 	dbg("%s", "mos7840_startup :Entering..........");
25053f542974SPaul B Schroeder 
25063f542974SPaul B Schroeder 	if (!serial) {
250784fe6e79STony Cook 		dbg("%s", "Invalid Handler");
25083f542974SPaul B Schroeder 		return -1;
25093f542974SPaul B Schroeder 	}
25103f542974SPaul B Schroeder 
25113f542974SPaul B Schroeder 	dev = serial->dev;
25123f542974SPaul B Schroeder 
251384fe6e79STony Cook 	dbg("%s", "Entering...");
251484fe6e79STony Cook 	dbg ("mos7840_startup: serial = %p", serial);
25153f542974SPaul B Schroeder 
25163f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
25173f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
25183f542974SPaul B Schroeder 
25193f542974SPaul B Schroeder 	/* set up port private structures */
25203f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
252184fe6e79STony Cook 		dbg ("mos7840_startup: configuring port %d............", i);
25227ac9da10SBurman Yan 		mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
25233f542974SPaul B Schroeder 		if (mos7840_port == NULL) {
2524194343d9SGreg Kroah-Hartman 			dev_err(&dev->dev, "%s - Out of memory\n", __func__);
25250de9a702SOliver Neukum 			status = -ENOMEM;
25260de9a702SOliver Neukum 			i--; /* don't follow NULL pointer cleaning up */
25270de9a702SOliver Neukum 			goto error;
25283f542974SPaul B Schroeder 		}
25293f542974SPaul B Schroeder 
2530880af9dbSAlan Cox 		/* Initialize all port interrupt end point to port 0 int
2531880af9dbSAlan Cox 		 * endpoint. Our device has only one interrupt end point
2532880af9dbSAlan Cox 		 * common to all port */
25333f542974SPaul B Schroeder 
25343f542974SPaul B Schroeder 		mos7840_port->port = serial->port[i];
25353f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
25360de9a702SOliver Neukum 		spin_lock_init(&mos7840_port->pool_lock);
25373f542974SPaul B Schroeder 
253837768adfSTony Cook 		/* minor is not initialised until later by
253937768adfSTony Cook 		 * usb-serial.c:get_free_serial() and cannot therefore be used
254037768adfSTony Cook 		 * to index device instances */
254137768adfSTony Cook 		mos7840_port->port_num = i + 1;
254237768adfSTony Cook 		dbg ("serial->port[i]->number = %d", serial->port[i]->number);
254337768adfSTony Cook 		dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
254437768adfSTony Cook 		dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
254537768adfSTony Cook 		dbg ("serial->minor = %d", serial->minor);
25463f542974SPaul B Schroeder 
25473f542974SPaul B Schroeder 		if (mos7840_port->port_num == 1) {
25483f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x0;
25493f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x1;
25503f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x4;
25513f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
25520de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
25533f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x8;
25543f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x9;
25553f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x16;
25563f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
25570de9a702SOliver Neukum 			   && (serial->num_ports == 2)) {
25583f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
25593f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
25603f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
25613f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 3)
25620de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
25633f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
25643f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
25653f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
25663f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 4)
25670de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
25683f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xc;
25693f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xd;
25703f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x1c;
25713f542974SPaul B Schroeder 		}
25723f542974SPaul B Schroeder 		mos7840_dump_serial_port(mos7840_port);
25733f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
25743f542974SPaul B Schroeder 
2575880af9dbSAlan Cox 		/* enable rx_disable bit in control register */
2576880af9dbSAlan Cox 		status = mos7840_get_reg_sync(serial->port[i],
25773f542974SPaul B Schroeder 				 mos7840_port->ControlRegOffset, &Data);
25783f542974SPaul B Schroeder 		if (status < 0) {
257984fe6e79STony Cook 			dbg("Reading ControlReg failed status-0x%x", status);
25803f542974SPaul B Schroeder 			break;
25813f542974SPaul B Schroeder 		} else
258284fe6e79STony Cook 			dbg("ControlReg Reading success val is %x, status%d",
25833f542974SPaul B Schroeder 			    Data, status);
2584880af9dbSAlan Cox 		Data |= 0x08;	/* setting driver done bit */
2585880af9dbSAlan Cox 		Data |= 0x04;	/* sp1_bit to have cts change reflect in
2586880af9dbSAlan Cox 				   modem status reg */
25873f542974SPaul B Schroeder 
2588880af9dbSAlan Cox 		/* Data |= 0x20; //rx_disable bit */
2589880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
25903f542974SPaul B Schroeder 					 mos7840_port->ControlRegOffset, Data);
25913f542974SPaul B Schroeder 		if (status < 0) {
259284fe6e79STony Cook 			dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
25933f542974SPaul B Schroeder 			break;
25943f542974SPaul B Schroeder 		} else
259584fe6e79STony Cook 			dbg("ControlReg Writing success(rx_disable) status%d",
25963f542974SPaul B Schroeder 			    status);
25973f542974SPaul B Schroeder 
2598880af9dbSAlan Cox 		/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2599880af9dbSAlan Cox 		   and 0x24 in DCR3 */
26003f542974SPaul B Schroeder 		Data = 0x01;
2601880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2602880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
26033f542974SPaul B Schroeder 		if (status < 0) {
260484fe6e79STony Cook 			dbg("Writing DCR0 failed status-0x%x", status);
26053f542974SPaul B Schroeder 			break;
26063f542974SPaul B Schroeder 		} else
260784fe6e79STony Cook 			dbg("DCR0 Writing success status%d", status);
26083f542974SPaul B Schroeder 
26093f542974SPaul B Schroeder 		Data = 0x05;
2610880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2611880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
26123f542974SPaul B Schroeder 		if (status < 0) {
261384fe6e79STony Cook 			dbg("Writing DCR1 failed status-0x%x", status);
26143f542974SPaul B Schroeder 			break;
26153f542974SPaul B Schroeder 		} else
261684fe6e79STony Cook 			dbg("DCR1 Writing success status%d", status);
26173f542974SPaul B Schroeder 
26183f542974SPaul B Schroeder 		Data = 0x24;
2619880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2620880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
26213f542974SPaul B Schroeder 		if (status < 0) {
262284fe6e79STony Cook 			dbg("Writing DCR2 failed status-0x%x", status);
26233f542974SPaul B Schroeder 			break;
26243f542974SPaul B Schroeder 		} else
262584fe6e79STony Cook 			dbg("DCR2 Writing success status%d", status);
26263f542974SPaul B Schroeder 
2627880af9dbSAlan Cox 		/* write values in clkstart0x0 and clkmulti 0x20 */
26283f542974SPaul B Schroeder 		Data = 0x0;
2629880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
26303f542974SPaul B Schroeder 					 CLK_START_VALUE_REGISTER, Data);
26313f542974SPaul B Schroeder 		if (status < 0) {
263284fe6e79STony Cook 			dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
26333f542974SPaul B Schroeder 			break;
26343f542974SPaul B Schroeder 		} else
263584fe6e79STony Cook 			dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
26363f542974SPaul B Schroeder 
26373f542974SPaul B Schroeder 		Data = 0x20;
2638880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2639880af9dbSAlan Cox 					CLK_MULTI_REGISTER, Data);
26403f542974SPaul B Schroeder 		if (status < 0) {
264184fe6e79STony Cook 			dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
26423f542974SPaul B Schroeder 			    status);
26430de9a702SOliver Neukum 			goto error;
26443f542974SPaul B Schroeder 		} else
264584fe6e79STony Cook 			dbg("CLK_MULTI_REGISTER Writing success status%d",
26463f542974SPaul B Schroeder 			    status);
26473f542974SPaul B Schroeder 
2648880af9dbSAlan Cox 		/* write value 0x0 to scratchpad register */
26493f542974SPaul B Schroeder 		Data = 0x00;
2650880af9dbSAlan Cox 		status = mos7840_set_uart_reg(serial->port[i],
2651880af9dbSAlan Cox 						SCRATCH_PAD_REGISTER, Data);
26523f542974SPaul B Schroeder 		if (status < 0) {
265384fe6e79STony Cook 			dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
26543f542974SPaul B Schroeder 			    status);
26553f542974SPaul B Schroeder 			break;
26563f542974SPaul B Schroeder 		} else
265784fe6e79STony Cook 			dbg("SCRATCH_PAD_REGISTER Writing success status%d",
26583f542974SPaul B Schroeder 			    status);
26593f542974SPaul B Schroeder 
2660880af9dbSAlan Cox 		/* Zero Length flag register */
26613f542974SPaul B Schroeder 		if ((mos7840_port->port_num != 1)
26620de9a702SOliver Neukum 		    && (serial->num_ports == 2)) {
26633f542974SPaul B Schroeder 
26643f542974SPaul B Schroeder 			Data = 0xff;
26653f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
26663f542974SPaul B Schroeder 				      (__u16) (ZLP_REG1 +
2667880af9dbSAlan Cox 				      ((__u16)mos7840_port->port_num)), Data);
266884fe6e79STony Cook 			dbg("ZLIP offset %x",
26693f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
26703f542974SPaul B Schroeder 					((__u16) mos7840_port->port_num)));
26713f542974SPaul B Schroeder 			if (status < 0) {
267284fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
26733f542974SPaul B Schroeder 				    i + 2, status);
26743f542974SPaul B Schroeder 				break;
26753f542974SPaul B Schroeder 			} else
267684fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
26773f542974SPaul B Schroeder 				    i + 2, status);
26783f542974SPaul B Schroeder 		} else {
26793f542974SPaul B Schroeder 			Data = 0xff;
26803f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
26813f542974SPaul B Schroeder 			      (__u16) (ZLP_REG1 +
2682880af9dbSAlan Cox 			      ((__u16)mos7840_port->port_num) - 0x1), Data);
268384fe6e79STony Cook 			dbg("ZLIP offset %x",
26843f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
26853f542974SPaul B Schroeder 				     ((__u16) mos7840_port->port_num) - 0x1));
26863f542974SPaul B Schroeder 			if (status < 0) {
268784fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
26883f542974SPaul B Schroeder 				    i + 1, status);
26893f542974SPaul B Schroeder 				break;
26903f542974SPaul B Schroeder 			} else
269184fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
26923f542974SPaul B Schroeder 				    i + 1, status);
26933f542974SPaul B Schroeder 
26943f542974SPaul B Schroeder 		}
26950de9a702SOliver Neukum 		mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
26963f542974SPaul B Schroeder 		mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2697880af9dbSAlan Cox 		mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2698880af9dbSAlan Cox 								GFP_KERNEL);
2699880af9dbSAlan Cox 		if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2700880af9dbSAlan Cox 							!mos7840_port->dr) {
27010de9a702SOliver Neukum 			status = -ENOMEM;
27020de9a702SOliver Neukum 			goto error;
27030de9a702SOliver Neukum 		}
2704*0eafe4deSDonald 
2705*0eafe4deSDonald 		mos7840_port->has_led = false;
2706*0eafe4deSDonald 
2707*0eafe4deSDonald 		/* Initialize LED timers */
2708*0eafe4deSDonald 		if (device_type == MOSCHIP_DEVICE_ID_7810) {
2709*0eafe4deSDonald 			mos7840_port->has_led = true;
2710*0eafe4deSDonald 
2711*0eafe4deSDonald 			init_timer(&mos7840_port->led_timer1);
2712*0eafe4deSDonald 			mos7840_port->led_timer1.function = mos7840_led_off;
2713*0eafe4deSDonald 			mos7840_port->led_timer1.expires =
2714*0eafe4deSDonald 					jiffies + msecs_to_jiffies(LED_ON_MS);
2715*0eafe4deSDonald 			mos7840_port->led_timer1.data =
2716*0eafe4deSDonald 						(unsigned long)mos7840_port;
2717*0eafe4deSDonald 
2718*0eafe4deSDonald 			init_timer(&mos7840_port->led_timer2);
2719*0eafe4deSDonald 			mos7840_port->led_timer2.function =
2720*0eafe4deSDonald 						mos7840_led_flag_off;
2721*0eafe4deSDonald 			mos7840_port->led_timer2.expires =
2722*0eafe4deSDonald 					jiffies + msecs_to_jiffies(LED_OFF_MS);
2723*0eafe4deSDonald 			mos7840_port->led_timer2.data =
2724*0eafe4deSDonald 						(unsigned long)mos7840_port;
2725*0eafe4deSDonald 
2726*0eafe4deSDonald 			mos7840_port->led_flag = false;
2727*0eafe4deSDonald 
2728*0eafe4deSDonald 			/* Turn off LED */
2729*0eafe4deSDonald 			mos7840_set_led_sync(serial->port[i],
2730*0eafe4deSDonald 						MODEM_CONTROL_REGISTER, 0x0300);
2731*0eafe4deSDonald 		}
27323f542974SPaul B Schroeder 	}
273384fe6e79STony Cook 	dbg ("mos7840_startup: all ports configured...........");
27343f542974SPaul B Schroeder 
2735880af9dbSAlan Cox 	/* Zero Length flag enable */
27363f542974SPaul B Schroeder 	Data = 0x0f;
27373f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
27383f542974SPaul B Schroeder 	if (status < 0) {
273984fe6e79STony Cook 		dbg("Writing ZLP_REG5 failed status-0x%x", status);
27407ced46c3SRoel Kluin 		goto error;
27413f542974SPaul B Schroeder 	} else
274284fe6e79STony Cook 		dbg("ZLP_REG5 Writing success status%d", status);
27433f542974SPaul B Schroeder 
27443f542974SPaul B Schroeder 	/* setting configuration feature to one */
27453f542974SPaul B Schroeder 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
274697c4965dSAl Viro 			(__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
27473f542974SPaul B Schroeder 	return 0;
27480de9a702SOliver Neukum error:
27490de9a702SOliver Neukum 	for (/* nothing */; i >= 0; i--) {
27500de9a702SOliver Neukum 		mos7840_port = mos7840_get_port_private(serial->port[i]);
27510de9a702SOliver Neukum 
27520de9a702SOliver Neukum 		kfree(mos7840_port->dr);
27530de9a702SOliver Neukum 		kfree(mos7840_port->ctrl_buf);
27540de9a702SOliver Neukum 		usb_free_urb(mos7840_port->control_urb);
27550de9a702SOliver Neukum 		kfree(mos7840_port);
27560de9a702SOliver Neukum 		serial->port[i] = NULL;
27570de9a702SOliver Neukum 	}
27580de9a702SOliver Neukum 	return status;
27593f542974SPaul B Schroeder }
27603f542974SPaul B Schroeder 
27613f542974SPaul B Schroeder /****************************************************************************
2762f9c99bb8SAlan Stern  * mos7840_disconnect
27633f542974SPaul B Schroeder  *	This function is called whenever the device is removed from the usb bus.
27643f542974SPaul B Schroeder  ****************************************************************************/
27653f542974SPaul B Schroeder 
2766f9c99bb8SAlan Stern static void mos7840_disconnect(struct usb_serial *serial)
27673f542974SPaul B Schroeder {
27683f542974SPaul B Schroeder 	int i;
27690de9a702SOliver Neukum 	unsigned long flags;
27703f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
2771f9c99bb8SAlan Stern 	dbg("%s", " disconnect :entering..........");
27723f542974SPaul B Schroeder 
27733f542974SPaul B Schroeder 	if (!serial) {
277484fe6e79STony Cook 		dbg("%s", "Invalid Handler");
27753f542974SPaul B Schroeder 		return;
27763f542974SPaul B Schroeder 	}
27773f542974SPaul B Schroeder 
27783f542974SPaul B Schroeder 	/* check for the ports to be closed,close the ports and disconnect */
27793f542974SPaul B Schroeder 
27803f542974SPaul B Schroeder 	/* free private structure allocated for serial port  *
27813f542974SPaul B Schroeder 	 * stop reads and writes on all ports                */
27823f542974SPaul B Schroeder 
27833f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
27843f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
278537768adfSTony Cook 		dbg ("mos7840_port %d = %p", i, mos7840_port);
278637768adfSTony Cook 		if (mos7840_port) {
27870de9a702SOliver Neukum 			spin_lock_irqsave(&mos7840_port->pool_lock, flags);
27880de9a702SOliver Neukum 			mos7840_port->zombie = 1;
27890de9a702SOliver Neukum 			spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
27903f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->control_urb);
2791f9c99bb8SAlan Stern 		}
2792f9c99bb8SAlan Stern 	}
2793f9c99bb8SAlan Stern 
2794f9c99bb8SAlan Stern 	dbg("%s", "Thank u :: ");
2795f9c99bb8SAlan Stern 
2796f9c99bb8SAlan Stern }
2797f9c99bb8SAlan Stern 
2798f9c99bb8SAlan Stern /****************************************************************************
2799f9c99bb8SAlan Stern  * mos7840_release
2800f9c99bb8SAlan Stern  *	This function is called when the usb_serial structure is freed.
2801f9c99bb8SAlan Stern  ****************************************************************************/
2802f9c99bb8SAlan Stern 
2803f9c99bb8SAlan Stern static void mos7840_release(struct usb_serial *serial)
2804f9c99bb8SAlan Stern {
2805f9c99bb8SAlan Stern 	int i;
2806f9c99bb8SAlan Stern 	struct moschip_port *mos7840_port;
2807f9c99bb8SAlan Stern 	dbg("%s", " release :entering..........");
2808f9c99bb8SAlan Stern 
2809f9c99bb8SAlan Stern 	if (!serial) {
2810f9c99bb8SAlan Stern 		dbg("%s", "Invalid Handler");
2811f9c99bb8SAlan Stern 		return;
2812f9c99bb8SAlan Stern 	}
2813f9c99bb8SAlan Stern 
2814f9c99bb8SAlan Stern 	/* check for the ports to be closed,close the ports and disconnect */
2815f9c99bb8SAlan Stern 
2816f9c99bb8SAlan Stern 	/* free private structure allocated for serial port  *
2817f9c99bb8SAlan Stern 	 * stop reads and writes on all ports                */
2818f9c99bb8SAlan Stern 
2819f9c99bb8SAlan Stern 	for (i = 0; i < serial->num_ports; ++i) {
2820f9c99bb8SAlan Stern 		mos7840_port = mos7840_get_port_private(serial->port[i]);
2821f9c99bb8SAlan Stern 		dbg("mos7840_port %d = %p", i, mos7840_port);
2822f9c99bb8SAlan Stern 		if (mos7840_port) {
2823*0eafe4deSDonald 			if (mos7840_port->has_led) {
2824*0eafe4deSDonald 				/* Turn off LED */
2825*0eafe4deSDonald 				mos7840_set_led_sync(mos7840_port->port,
2826*0eafe4deSDonald 						MODEM_CONTROL_REGISTER, 0x0300);
2827*0eafe4deSDonald 
2828*0eafe4deSDonald 				del_timer_sync(&mos7840_port->led_timer1);
2829*0eafe4deSDonald 				del_timer_sync(&mos7840_port->led_timer2);
2830*0eafe4deSDonald 			}
28310de9a702SOliver Neukum 			kfree(mos7840_port->ctrl_buf);
28320de9a702SOliver Neukum 			kfree(mos7840_port->dr);
28333f542974SPaul B Schroeder 			kfree(mos7840_port);
283437768adfSTony Cook 		}
28353f542974SPaul B Schroeder 	}
28363f542974SPaul B Schroeder 
283784fe6e79STony Cook 	dbg("%s", "Thank u :: ");
28383f542974SPaul B Schroeder 
28393f542974SPaul B Schroeder }
28403f542974SPaul B Schroeder 
2841d9b1b787SJohannes Hölzl static struct usb_driver io_driver = {
2842d9b1b787SJohannes Hölzl 	.name = "mos7840",
2843d9b1b787SJohannes Hölzl 	.probe = usb_serial_probe,
2844d9b1b787SJohannes Hölzl 	.disconnect = usb_serial_disconnect,
2845d9b1b787SJohannes Hölzl 	.id_table = moschip_id_table_combined,
2846d9b1b787SJohannes Hölzl };
2847d9b1b787SJohannes Hölzl 
28483f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
28493f542974SPaul B Schroeder 	.driver = {
28503f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
28513f542974SPaul B Schroeder 		   .name = "mos7840",
28523f542974SPaul B Schroeder 		   },
28533f542974SPaul B Schroeder 	.description = DRIVER_DESC,
28543f542974SPaul B Schroeder 	.id_table = moschip_port_id_table,
28553f542974SPaul B Schroeder 	.num_ports = 4,
28563f542974SPaul B Schroeder 	.open = mos7840_open,
28573f542974SPaul B Schroeder 	.close = mos7840_close,
28583f542974SPaul B Schroeder 	.write = mos7840_write,
28593f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
28603f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
28613f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
28623f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
28633f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
28643f542974SPaul B Schroeder #ifdef MCSSerialProbe
28653f542974SPaul B Schroeder 	.probe = mos7840_serial_probe,
28663f542974SPaul B Schroeder #endif
28673f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
28683f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
28693f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
28703f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
28713f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
28720bca1b91SAlan Cox 	.get_icount = mos7840_get_icount,
28733f542974SPaul B Schroeder 	.attach = mos7840_startup,
2874f9c99bb8SAlan Stern 	.disconnect = mos7840_disconnect,
2875f9c99bb8SAlan Stern 	.release = mos7840_release,
28763f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
28773f542974SPaul B Schroeder 	.read_int_callback = mos7840_interrupt_callback,
28783f542974SPaul B Schroeder };
28793f542974SPaul B Schroeder 
28804d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
28814d2a7affSAlan Stern 	&moschip7840_4port_device, NULL
28824d2a7affSAlan Stern };
28834d2a7affSAlan Stern 
2884e7414d9aSGreg Kroah-Hartman module_usb_serial_driver(io_driver, serial_drivers);
28853f542974SPaul B Schroeder 
28863f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
28873f542974SPaul B Schroeder MODULE_LICENSE("GPL");
28883f542974SPaul B Schroeder 
28893f542974SPaul B Schroeder module_param(debug, bool, S_IRUGO | S_IWUSR);
28903f542974SPaul B Schroeder MODULE_PARM_DESC(debug, "Debug enabled or not");
2891