xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision 3363155b66c8d223845b928f87bc8aa482e5f91d)
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
1170eafe4deSDonald #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 
1880eafe4deSDonald /* LED on/off milliseconds*/
1890eafe4deSDonald #define LED_ON_MS	500
1900eafe4deSDonald #define LED_OFF_MS	500
1910eafe4deSDonald 
1920eafe4deSDonald 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)},
1970eafe4deSDonald 	{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)},
2190eafe4deSDonald 	{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 
2730eafe4deSDonald 	/* For device(s) with LED indicator */
2740eafe4deSDonald 	bool has_led;
2750eafe4deSDonald 	bool led_flag;
2760eafe4deSDonald 	struct timer_list led_timer1;	/* Timer for LED on */
2770eafe4deSDonald 	struct timer_list led_timer2;	/* Timer for LED off */
2780eafe4deSDonald };
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 
5880eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb)
5890eafe4deSDonald {
5900eafe4deSDonald 	switch (urb->status) {
5910eafe4deSDonald 	case 0:
5920eafe4deSDonald 		/* Success */
5930eafe4deSDonald 		break;
5940eafe4deSDonald 	case -ECONNRESET:
5950eafe4deSDonald 	case -ENOENT:
5960eafe4deSDonald 	case -ESHUTDOWN:
5970eafe4deSDonald 		/* This urb is terminated, clean up */
5980eafe4deSDonald 		dbg("%s - urb shutting down with status: %d", __func__,
5990eafe4deSDonald 			urb->status);
6000eafe4deSDonald 		break;
6010eafe4deSDonald 	default:
6020eafe4deSDonald 		dbg("%s - nonzero urb status received: %d", __func__,
6030eafe4deSDonald 			urb->status);
6040eafe4deSDonald 	}
6050eafe4deSDonald }
6060eafe4deSDonald 
6070eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
6080eafe4deSDonald 				__u16 reg)
6090eafe4deSDonald {
6100eafe4deSDonald 	struct usb_device *dev = mcs->port->serial->dev;
6110eafe4deSDonald 	struct usb_ctrlrequest *dr = mcs->dr;
6120eafe4deSDonald 
6130eafe4deSDonald 	dr->bRequestType = MCS_WR_RTYPE;
6140eafe4deSDonald 	dr->bRequest = MCS_WRREQ;
6150eafe4deSDonald 	dr->wValue = cpu_to_le16(wval);
6160eafe4deSDonald 	dr->wIndex = cpu_to_le16(reg);
6170eafe4deSDonald 	dr->wLength = cpu_to_le16(0);
6180eafe4deSDonald 
6190eafe4deSDonald 	usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
6200eafe4deSDonald 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
6210eafe4deSDonald 
6220eafe4deSDonald 	usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
6230eafe4deSDonald }
6240eafe4deSDonald 
6250eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
6260eafe4deSDonald 				__u16 val)
6270eafe4deSDonald {
6280eafe4deSDonald 	struct usb_device *dev = port->serial->dev;
6290eafe4deSDonald 
6300eafe4deSDonald 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
6310eafe4deSDonald 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
6320eafe4deSDonald }
6330eafe4deSDonald 
6340eafe4deSDonald static void mos7840_led_off(unsigned long arg)
6350eafe4deSDonald {
6360eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
6370eafe4deSDonald 
6380eafe4deSDonald 	/* Turn off LED */
6390eafe4deSDonald 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
6400eafe4deSDonald 	mod_timer(&mcs->led_timer2,
6410eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_OFF_MS));
6420eafe4deSDonald }
6430eafe4deSDonald 
6440eafe4deSDonald static void mos7840_led_flag_off(unsigned long arg)
6450eafe4deSDonald {
6460eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
6470eafe4deSDonald 
6480eafe4deSDonald 	mcs->led_flag = false;
6490eafe4deSDonald }
6500eafe4deSDonald 
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 
6700643c724SGreg Kroah-Hartman 	switch (status) {
6713f542974SPaul B Schroeder 	case 0:
6723f542974SPaul B Schroeder 		/* success */
6733f542974SPaul B Schroeder 		break;
6743f542974SPaul B Schroeder 	case -ECONNRESET:
6753f542974SPaul B Schroeder 	case -ENOENT:
6763f542974SPaul B Schroeder 	case -ESHUTDOWN:
6773f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
678441b62c1SHarvey Harrison 		dbg("%s - urb shutting down with status: %d", __func__,
6790643c724SGreg Kroah-Hartman 		    status);
6803f542974SPaul B Schroeder 		return;
6813f542974SPaul B Schroeder 	default:
682441b62c1SHarvey Harrison 		dbg("%s - nonzero urb status received: %d", __func__,
6830643c724SGreg Kroah-Hartman 		    status);
6843f542974SPaul B Schroeder 		goto exit;
6853f542974SPaul B Schroeder 	}
6863f542974SPaul B Schroeder 
6873f542974SPaul B Schroeder 	length = urb->actual_length;
6883f542974SPaul B Schroeder 	data = urb->transfer_buffer;
6893f542974SPaul B Schroeder 
690cdc97792SMing Lei 	serial = urb->context;
6913f542974SPaul B Schroeder 
6923f542974SPaul B Schroeder 	/* Moschip get 5 bytes
6933f542974SPaul B Schroeder 	 * Byte 1 IIR Port 1 (port.number is 0)
6943f542974SPaul B Schroeder 	 * Byte 2 IIR Port 2 (port.number is 1)
6953f542974SPaul B Schroeder 	 * Byte 3 IIR Port 3 (port.number is 2)
6963f542974SPaul B Schroeder 	 * Byte 4 IIR Port 4 (port.number is 3)
6973f542974SPaul B Schroeder 	 * Byte 5 FIFO status for both */
6983f542974SPaul B Schroeder 
6993f542974SPaul B Schroeder 	if (length && length > 5) {
70084fe6e79STony Cook 		dbg("%s", "Wrong data !!!");
7013f542974SPaul B Schroeder 		return;
7023f542974SPaul B Schroeder 	}
7033f542974SPaul B Schroeder 
7043f542974SPaul B Schroeder 	sp[0] = (__u8) data[0];
7053f542974SPaul B Schroeder 	sp[1] = (__u8) data[1];
7063f542974SPaul B Schroeder 	sp[2] = (__u8) data[2];
7073f542974SPaul B Schroeder 	sp[3] = (__u8) data[3];
7083f542974SPaul B Schroeder 	st = (__u8) data[4];
7093f542974SPaul B Schroeder 
7103f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; i++) {
7113f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
7123f542974SPaul B Schroeder 		wval =
7133f542974SPaul B Schroeder 		    (((__u16) serial->port[i]->number -
7143f542974SPaul B Schroeder 		      (__u16) (serial->minor)) + 1) << 8;
7153f542974SPaul B Schroeder 		if (mos7840_port->open) {
7163f542974SPaul B Schroeder 			if (sp[i] & 0x01) {
71784fe6e79STony Cook 				dbg("SP%d No Interrupt !!!", i);
7183f542974SPaul B Schroeder 			} else {
7193f542974SPaul B Schroeder 				switch (sp[i] & 0x0f) {
7203f542974SPaul B Schroeder 				case SERIAL_IIR_RLS:
7213f542974SPaul B Schroeder 					dbg("Serial Port %d: Receiver status error or ", i);
72284fe6e79STony Cook 					dbg("address bit detected in 9-bit mode");
7233f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 1;
7240de9a702SOliver Neukum 					wreg = LINE_STATUS_REGISTER;
7253f542974SPaul B Schroeder 					break;
7263f542974SPaul B Schroeder 				case SERIAL_IIR_MS:
72784fe6e79STony Cook 					dbg("Serial Port %d: Modem status change", i);
7283f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 0;
7290de9a702SOliver Neukum 					wreg = MODEM_STATUS_REGISTER;
7303f542974SPaul B Schroeder 					break;
7313f542974SPaul B Schroeder 				}
7320de9a702SOliver Neukum 				spin_lock(&mos7840_port->pool_lock);
7330de9a702SOliver Neukum 				if (!mos7840_port->zombie) {
7340de9a702SOliver Neukum 					rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
7350de9a702SOliver Neukum 				} else {
7360de9a702SOliver Neukum 					spin_unlock(&mos7840_port->pool_lock);
7370de9a702SOliver Neukum 					return;
7380de9a702SOliver Neukum 				}
7390de9a702SOliver Neukum 				spin_unlock(&mos7840_port->pool_lock);
7403f542974SPaul B Schroeder 			}
7413f542974SPaul B Schroeder 		}
7423f542974SPaul B Schroeder 	}
743880af9dbSAlan Cox 	if (!(rv < 0))
744880af9dbSAlan Cox 		/* the completion handler for the control urb will resubmit */
7450de9a702SOliver Neukum 		return;
7463f542974SPaul B Schroeder exit:
7473f542974SPaul B Schroeder 	result = usb_submit_urb(urb, GFP_ATOMIC);
7483f542974SPaul B Schroeder 	if (result) {
7493f542974SPaul B Schroeder 		dev_err(&urb->dev->dev,
7503f542974SPaul B Schroeder 			"%s - Error %d submitting interrupt urb\n",
751441b62c1SHarvey Harrison 			__func__, result);
7523f542974SPaul B Schroeder 	}
7533f542974SPaul B Schroeder }
7543f542974SPaul B Schroeder 
7553f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port,
7563f542974SPaul B Schroeder 				       const char *function)
7573f542974SPaul B Schroeder {
7583f542974SPaul B Schroeder 	if (!port) {
7593f542974SPaul B Schroeder 		dbg("%s - port == NULL", function);
7603f542974SPaul B Schroeder 		return -1;
7613f542974SPaul B Schroeder 	}
7623f542974SPaul B Schroeder 	if (!port->serial) {
7633f542974SPaul B Schroeder 		dbg("%s - port->serial == NULL", function);
7643f542974SPaul B Schroeder 		return -1;
7653f542974SPaul B Schroeder 	}
7663f542974SPaul B Schroeder 
7673f542974SPaul B Schroeder 	return 0;
7683f542974SPaul B Schroeder }
7693f542974SPaul B Schroeder 
7703f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
7713f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
7723f542974SPaul B Schroeder 					 const char *function)
7733f542974SPaul B Schroeder {
7743f542974SPaul B Schroeder 	if (!serial) {
7753f542974SPaul B Schroeder 		dbg("%s - serial == NULL", function);
7763f542974SPaul B Schroeder 		return -1;
7773f542974SPaul B Schroeder 	}
7783f542974SPaul B Schroeder 	if (!serial->type) {
7793f542974SPaul B Schroeder 		dbg("%s - serial->type == NULL!", function);
7803f542974SPaul B Schroeder 		return -1;
7813f542974SPaul B Schroeder 	}
7823f542974SPaul B Schroeder 
7833f542974SPaul B Schroeder 	return 0;
7843f542974SPaul B Schroeder }
7853f542974SPaul B Schroeder 
7863f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
7873f542974SPaul B Schroeder 						 const char *function)
7883f542974SPaul B Schroeder {
7893f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
7903f542974SPaul B Schroeder 	if (!port ||
7913f542974SPaul B Schroeder 	    mos7840_port_paranoia_check(port, function) ||
7923f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
793880af9dbSAlan Cox 		/* then say that we don't have a valid usb_serial thing,
794880af9dbSAlan Cox 		 * which will end up genrating -ENODEV return values */
7953f542974SPaul B Schroeder 		return NULL;
7963f542974SPaul B Schroeder 	}
7973f542974SPaul B Schroeder 
7983f542974SPaul B Schroeder 	return port->serial;
7993f542974SPaul B Schroeder }
8003f542974SPaul B Schroeder 
8013f542974SPaul B Schroeder /*****************************************************************************
8023f542974SPaul B Schroeder  * mos7840_bulk_in_callback
8033f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
8043f542974SPaul B Schroeder  *	bulk in endpoint.
8053f542974SPaul B Schroeder  *****************************************************************************/
8063f542974SPaul B Schroeder 
8077d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
8083f542974SPaul B Schroeder {
8090643c724SGreg Kroah-Hartman 	int retval;
8103f542974SPaul B Schroeder 	unsigned char *data;
8113f542974SPaul B Schroeder 	struct usb_serial *serial;
8123f542974SPaul B Schroeder 	struct usb_serial_port *port;
8133f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8143f542974SPaul B Schroeder 	struct tty_struct *tty;
8150643c724SGreg Kroah-Hartman 	int status = urb->status;
8163f542974SPaul B Schroeder 
817cdc97792SMing Lei 	mos7840_port = urb->context;
8183f542974SPaul B Schroeder 	if (!mos7840_port) {
81984fe6e79STony Cook 		dbg("%s", "NULL mos7840_port pointer");
82050de36f7SGreg Kroah-Hartman 		return;
82150de36f7SGreg Kroah-Hartman 	}
82250de36f7SGreg Kroah-Hartman 
82350de36f7SGreg Kroah-Hartman 	if (status) {
82450de36f7SGreg Kroah-Hartman 		dbg("nonzero read bulk status received: %d", status);
82550de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8263f542974SPaul B Schroeder 		return;
8273f542974SPaul B Schroeder 	}
8283f542974SPaul B Schroeder 
8293f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
830441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
83184fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
83250de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8333f542974SPaul B Schroeder 		return;
8343f542974SPaul B Schroeder 	}
8353f542974SPaul B Schroeder 
836441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
8373f542974SPaul B Schroeder 	if (!serial) {
83884fe6e79STony Cook 		dbg("%s", "Bad serial pointer");
83950de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8403f542974SPaul B Schroeder 		return;
8413f542974SPaul B Schroeder 	}
8423f542974SPaul B Schroeder 
8433f542974SPaul B Schroeder 	data = urb->transfer_buffer;
8443f542974SPaul B Schroeder 
8453f542974SPaul B Schroeder 	if (urb->actual_length) {
8464a90f09bSAlan Cox 		tty = tty_port_tty_get(&mos7840_port->port->port);
8473f542974SPaul B Schroeder 		if (tty) {
8483f542974SPaul B Schroeder 			tty_insert_flip_string(tty, data, urb->actual_length);
84984fe6e79STony Cook 			dbg(" %s ", data);
8503f542974SPaul B Schroeder 			tty_flip_buffer_push(tty);
8514a90f09bSAlan Cox 			tty_kref_put(tty);
8523f542974SPaul B Schroeder 		}
8533f542974SPaul B Schroeder 		mos7840_port->icount.rx += urb->actual_length;
8540de9a702SOliver Neukum 		smp_wmb();
85584fe6e79STony Cook 		dbg("mos7840_port->icount.rx is %d:",
8563f542974SPaul B Schroeder 		    mos7840_port->icount.rx);
8573f542974SPaul B Schroeder 	}
8583f542974SPaul B Schroeder 
8593f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
86084fe6e79STony Cook 		dbg("%s", "URB KILLED !!!");
86150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8623f542974SPaul B Schroeder 		return;
8633f542974SPaul B Schroeder 	}
8643f542974SPaul B Schroeder 
8650eafe4deSDonald 	/* Turn on LED */
8660eafe4deSDonald 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
8670eafe4deSDonald 		mos7840_port->led_flag = true;
8680eafe4deSDonald 		mos7840_set_led_async(mos7840_port, 0x0301,
8690eafe4deSDonald 					MODEM_CONTROL_REGISTER);
8700eafe4deSDonald 		mod_timer(&mos7840_port->led_timer1,
8710eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_ON_MS));
8720eafe4deSDonald 	}
8730de9a702SOliver Neukum 
87450de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
8750643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
8763f542974SPaul B Schroeder 
8770643c724SGreg Kroah-Hartman 	if (retval) {
87850de36f7SGreg Kroah-Hartman 		dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
87950de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8803f542974SPaul B Schroeder 	}
8813f542974SPaul B Schroeder }
8823f542974SPaul B Schroeder 
8833f542974SPaul B Schroeder /*****************************************************************************
8843f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
885880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
886880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
8873f542974SPaul B Schroeder  *****************************************************************************/
8883f542974SPaul B Schroeder 
8897d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
8903f542974SPaul B Schroeder {
8913f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8923f542974SPaul B Schroeder 	struct tty_struct *tty;
8930643c724SGreg Kroah-Hartman 	int status = urb->status;
8940de9a702SOliver Neukum 	int i;
8950de9a702SOliver Neukum 
896cdc97792SMing Lei 	mos7840_port = urb->context;
8970de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
8980de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
8990de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
9000de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
9010de9a702SOliver Neukum 			break;
9020de9a702SOliver Neukum 		}
9030de9a702SOliver Neukum 	}
9040de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
9050de9a702SOliver Neukum 
9060643c724SGreg Kroah-Hartman 	if (status) {
90784fe6e79STony Cook 		dbg("nonzero write bulk status received:%d", status);
9083f542974SPaul B Schroeder 		return;
9093f542974SPaul B Schroeder 	}
9103f542974SPaul B Schroeder 
911441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
91284fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
9133f542974SPaul B Schroeder 		return;
9143f542974SPaul B Schroeder 	}
9153f542974SPaul B Schroeder 
9164a90f09bSAlan Cox 	tty = tty_port_tty_get(&mos7840_port->port->port);
917b963a844SJiri Slaby 	if (tty && mos7840_port->open)
918b963a844SJiri Slaby 		tty_wakeup(tty);
9194a90f09bSAlan Cox 	tty_kref_put(tty);
9203f542974SPaul B Schroeder 
9213f542974SPaul B Schroeder }
9223f542974SPaul B Schroeder 
9233f542974SPaul B Schroeder /************************************************************************/
9243f542974SPaul 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       */
9253f542974SPaul B Schroeder /************************************************************************/
9263f542974SPaul B Schroeder #ifdef MCSSerialProbe
9273f542974SPaul B Schroeder static int mos7840_serial_probe(struct usb_serial *serial,
9283f542974SPaul B Schroeder 				const struct usb_device_id *id)
9293f542974SPaul B Schroeder {
9303f542974SPaul B Schroeder 
9313f542974SPaul B Schroeder 	/*need to implement the mode_reg reading and updating\
9323f542974SPaul B Schroeder 	   structures usb_serial_ device_type\
9333f542974SPaul B Schroeder 	   (i.e num_ports, num_bulkin,bulkout etc) */
9343f542974SPaul B Schroeder 	/* Also we can update the changes  attach */
9353f542974SPaul B Schroeder 	return 1;
9363f542974SPaul B Schroeder }
9373f542974SPaul B Schroeder #endif
9383f542974SPaul B Schroeder 
9393f542974SPaul B Schroeder /*****************************************************************************
9403f542974SPaul B Schroeder  * mos7840_open
9413f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
9423f542974SPaul B Schroeder  *	If successful, we return 0
9433f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
9443f542974SPaul B Schroeder  *****************************************************************************/
9453f542974SPaul B Schroeder 
946a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
9473f542974SPaul B Schroeder {
9483f542974SPaul B Schroeder 	int response;
9493f542974SPaul B Schroeder 	int j;
9503f542974SPaul B Schroeder 	struct usb_serial *serial;
9513f542974SPaul B Schroeder 	struct urb *urb;
9523f542974SPaul B Schroeder 	__u16 Data;
9533f542974SPaul B Schroeder 	int status;
9543f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
9550de9a702SOliver Neukum 	struct moschip_port *port0;
9563f542974SPaul B Schroeder 
957441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
95884fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
9593f542974SPaul B Schroeder 		return -ENODEV;
9603f542974SPaul B Schroeder 	}
9613f542974SPaul B Schroeder 
9623f542974SPaul B Schroeder 	serial = port->serial;
9633f542974SPaul B Schroeder 
964441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
96584fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
9663f542974SPaul B Schroeder 		return -ENODEV;
9673f542974SPaul B Schroeder 	}
9683f542974SPaul B Schroeder 
9693f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
9700de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
9713f542974SPaul B Schroeder 
9720de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
9733f542974SPaul B Schroeder 		return -ENODEV;
9743f542974SPaul B Schroeder 
9753f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
9763f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
9770de9a702SOliver Neukum 	port0->open_ports++;
9783f542974SPaul B Schroeder 
9793f542974SPaul B Schroeder 	/* Initialising the write urb pool */
9803f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
9810de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
9823f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
9833f542974SPaul B Schroeder 
9843f542974SPaul B Schroeder 		if (urb == NULL) {
985194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "No more urbs???\n");
9863f542974SPaul B Schroeder 			continue;
9873f542974SPaul B Schroeder 		}
9883f542974SPaul B Schroeder 
989880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
990880af9dbSAlan Cox 								GFP_KERNEL);
9913f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
9920de9a702SOliver Neukum 			usb_free_urb(urb);
9930de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
994194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
995194343d9SGreg Kroah-Hartman 				"%s-out of memory for urb buffers.\n",
996194343d9SGreg Kroah-Hartman 				__func__);
9973f542974SPaul B Schroeder 			continue;
9983f542974SPaul B Schroeder 		}
9993f542974SPaul B Schroeder 	}
10003f542974SPaul B Schroeder 
10013f542974SPaul B Schroeder /*****************************************************************************
10023f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
10033f542974SPaul B Schroeder  *
10043f542974SPaul B Schroeder  * Register Index
10053f542974SPaul B Schroeder  * 1 : IER
10063f542974SPaul B Schroeder  * 2 : FCR
10073f542974SPaul B Schroeder  * 3 : LCR
10083f542974SPaul B Schroeder  * 4 : MCR
10093f542974SPaul B Schroeder  *
10103f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
10113f542974SPaul B Schroeder  *****************************************************************************/
10123f542974SPaul B Schroeder 
1013880af9dbSAlan Cox 	/* NEED to check the following Block */
10143f542974SPaul B Schroeder 
10153f542974SPaul B Schroeder 	Data = 0x0;
10163f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
10173f542974SPaul B Schroeder 	if (status < 0) {
101884fe6e79STony Cook 		dbg("Reading Spreg failed");
10193f542974SPaul B Schroeder 		return -1;
10203f542974SPaul B Schroeder 	}
10213f542974SPaul B Schroeder 	Data |= 0x80;
10223f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10233f542974SPaul B Schroeder 	if (status < 0) {
102484fe6e79STony Cook 		dbg("writing Spreg failed");
10253f542974SPaul B Schroeder 		return -1;
10263f542974SPaul B Schroeder 	}
10273f542974SPaul B Schroeder 
10283f542974SPaul B Schroeder 	Data &= ~0x80;
10293f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10303f542974SPaul B Schroeder 	if (status < 0) {
103184fe6e79STony Cook 		dbg("writing Spreg failed");
10323f542974SPaul B Schroeder 		return -1;
10333f542974SPaul B Schroeder 	}
1034880af9dbSAlan Cox 	/* End of block to be checked */
10353f542974SPaul B Schroeder 
10363f542974SPaul B Schroeder 	Data = 0x0;
1037880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1038880af9dbSAlan Cox 									&Data);
10393f542974SPaul B Schroeder 	if (status < 0) {
104084fe6e79STony Cook 		dbg("Reading Controlreg failed");
10413f542974SPaul B Schroeder 		return -1;
10423f542974SPaul B Schroeder 	}
1043880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
1044880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
1045880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
1046880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
10473f542974SPaul B Schroeder 	if (status < 0) {
104884fe6e79STony Cook 		dbg("writing Controlreg failed");
10493f542974SPaul B Schroeder 		return -1;
10503f542974SPaul B Schroeder 	}
1051880af9dbSAlan Cox 	/* do register settings here */
1052880af9dbSAlan Cox 	/* Set all regs to the device default values. */
1053880af9dbSAlan Cox 	/***********************************
1054880af9dbSAlan Cox 	 * First Disable all interrupts.
1055880af9dbSAlan Cox 	 ***********************************/
10563f542974SPaul B Schroeder 	Data = 0x00;
10573f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
10583f542974SPaul B Schroeder 	if (status < 0) {
105984fe6e79STony Cook 		dbg("disabling interrupts failed");
10603f542974SPaul B Schroeder 		return -1;
10613f542974SPaul B Schroeder 	}
1062880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
10633f542974SPaul B Schroeder 	Data = 0x00;
10643f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
10653f542974SPaul B Schroeder 	if (status < 0) {
106684fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
10673f542974SPaul B Schroeder 		return -1;
10683f542974SPaul B Schroeder 	}
10693f542974SPaul B Schroeder 
10703f542974SPaul B Schroeder 	Data = 0xcf;
10713f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
10723f542974SPaul B Schroeder 	if (status < 0) {
107384fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
10743f542974SPaul B Schroeder 		return -1;
10753f542974SPaul B Schroeder 	}
10763f542974SPaul B Schroeder 
10773f542974SPaul B Schroeder 	Data = 0x03;
10783f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10793f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10803f542974SPaul B Schroeder 
10813f542974SPaul B Schroeder 	Data = 0x0b;
10823f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
10833f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
10843f542974SPaul B Schroeder 
10853f542974SPaul B Schroeder 	Data = 0x00;
10863f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
10873f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10883f542974SPaul B Schroeder 
1089880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
10903f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10913f542974SPaul B Schroeder 
10923f542974SPaul B Schroeder 	Data = 0x0c;
10933f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
10943f542974SPaul B Schroeder 
10953f542974SPaul B Schroeder 	Data = 0x0;
10963f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
10973f542974SPaul B Schroeder 
10983f542974SPaul B Schroeder 	Data = 0x00;
10993f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
11003f542974SPaul B Schroeder 
11013f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
11023f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
11033f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
11043f542974SPaul B Schroeder 
1105880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
11063f542974SPaul B Schroeder 	Data = 0x0;
11073f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
11083f542974SPaul B Schroeder 
11093f542974SPaul B Schroeder 	Data = Data | 0x0c;
11103f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
11113f542974SPaul B Schroeder 
11123f542974SPaul B Schroeder 	Data = Data & ~0x0c;
11133f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1114880af9dbSAlan Cox 	/* Finally enable all interrupts */
11153f542974SPaul B Schroeder 	Data = 0x0c;
11163f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
11173f542974SPaul B Schroeder 
1118880af9dbSAlan Cox 	/* clearing rx_disable */
11193f542974SPaul B Schroeder 	Data = 0x0;
1120880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1121880af9dbSAlan Cox 									&Data);
11223f542974SPaul B Schroeder 	Data = Data & ~0x20;
1123880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1124880af9dbSAlan Cox 									Data);
11253f542974SPaul B Schroeder 
1126880af9dbSAlan Cox 	/* rx_negate */
11273f542974SPaul B Schroeder 	Data = 0x0;
1128880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1129880af9dbSAlan Cox 									&Data);
11303f542974SPaul B Schroeder 	Data = Data | 0x10;
1131880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1132880af9dbSAlan Cox 									Data);
11333f542974SPaul B Schroeder 
11343f542974SPaul B Schroeder 	/* Check to see if we've set up our endpoint info yet    *
11353f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the structures *
11363f542974SPaul B Schroeder 	 * were not set up at that time.)                        */
11370de9a702SOliver Neukum 	if (port0->open_ports == 1) {
11383f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_buffer == NULL) {
11393f542974SPaul B Schroeder 			/* set up interrupt urb */
11403f542974SPaul B Schroeder 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
11413f542974SPaul B Schroeder 				serial->dev,
11423f542974SPaul B Schroeder 				usb_rcvintpipe(serial->dev,
1143880af9dbSAlan Cox 				serial->port[0]->interrupt_in_endpointAddress),
11443f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_buffer,
11453f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_urb->
11463f542974SPaul B Schroeder 				transfer_buffer_length,
11473f542974SPaul B Schroeder 				mos7840_interrupt_callback,
11483f542974SPaul B Schroeder 				serial,
1149880af9dbSAlan Cox 				serial->port[0]->interrupt_in_urb->interval);
11503f542974SPaul B Schroeder 
11513f542974SPaul B Schroeder 			/* start interrupt read for mos7840               *
11523f542974SPaul B Schroeder 			 * will continue as long as mos7840 is connected  */
11533f542974SPaul B Schroeder 
11543f542974SPaul B Schroeder 			response =
11553f542974SPaul B Schroeder 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
11563f542974SPaul B Schroeder 					   GFP_KERNEL);
11573f542974SPaul B Schroeder 			if (response) {
1158194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "%s - Error %d submitting "
1159194343d9SGreg Kroah-Hartman 					"interrupt urb\n", __func__, response);
11603f542974SPaul B Schroeder 			}
11613f542974SPaul B Schroeder 
11623f542974SPaul B Schroeder 		}
11633f542974SPaul B Schroeder 
11643f542974SPaul B Schroeder 	}
11653f542974SPaul B Schroeder 
11663f542974SPaul B Schroeder 	/* see if we've set up our endpoint info yet   *
11673f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the  *
11683f542974SPaul B Schroeder 	 * structures were not set up at that time.)   */
11693f542974SPaul B Schroeder 
117084fe6e79STony Cook 	dbg("port number is %d", port->number);
117184fe6e79STony Cook 	dbg("serial number is %d", port->serial->minor);
117284fe6e79STony Cook 	dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
117384fe6e79STony Cook 	dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
117484fe6e79STony Cook 	dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
117584fe6e79STony Cook 	dbg("port's number in the device is %d", mos7840_port->port_num);
11763f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
11773f542974SPaul B Schroeder 
11783f542974SPaul B Schroeder 	/* set up our bulk in urb */
1179093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1180093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1181093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1182093ea2d3SDonald Lee 		usb_fill_bulk_urb(mos7840_port->read_urb,
1183093ea2d3SDonald Lee 			serial->dev,
1184093ea2d3SDonald Lee 			usb_rcvbulkpipe(serial->dev,
1185093ea2d3SDonald Lee 				(port->bulk_in_endpointAddress) + 2),
1186093ea2d3SDonald Lee 			port->bulk_in_buffer,
1187093ea2d3SDonald Lee 			mos7840_port->read_urb->transfer_buffer_length,
1188093ea2d3SDonald Lee 			mos7840_bulk_in_callback, mos7840_port);
1189093ea2d3SDonald Lee 	} else {
11903f542974SPaul B Schroeder 		usb_fill_bulk_urb(mos7840_port->read_urb,
11913f542974SPaul B Schroeder 			serial->dev,
11923f542974SPaul B Schroeder 			usb_rcvbulkpipe(serial->dev,
11933f542974SPaul B Schroeder 				port->bulk_in_endpointAddress),
11943f542974SPaul B Schroeder 			port->bulk_in_buffer,
11953f542974SPaul B Schroeder 			mos7840_port->read_urb->transfer_buffer_length,
11963f542974SPaul B Schroeder 			mos7840_bulk_in_callback, mos7840_port);
1197093ea2d3SDonald Lee 	}
11983f542974SPaul B Schroeder 
119984fe6e79STony Cook 	dbg("mos7840_open: bulkin endpoint is %d",
12003f542974SPaul B Schroeder 	    port->bulk_in_endpointAddress);
120150de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
12023f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
12033f542974SPaul B Schroeder 	if (response) {
1204194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1205194343d9SGreg Kroah-Hartman 			__func__, response);
120650de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
12073f542974SPaul B Schroeder 	}
12083f542974SPaul B Schroeder 
12093f542974SPaul B Schroeder 	/* initialize our wait queues */
12103f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->wait_chase);
12113f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->delta_msr_wait);
12123f542974SPaul B Schroeder 
12133f542974SPaul B Schroeder 	/* initialize our icount structure */
12143f542974SPaul B Schroeder 	memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
12153f542974SPaul B Schroeder 
12163f542974SPaul B Schroeder 	/* initialize our port settings */
1217880af9dbSAlan Cox 	/* Must set to enable ints! */
1218880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
12193f542974SPaul B Schroeder 	/* send a open port command */
12203f542974SPaul B Schroeder 	mos7840_port->open = 1;
1221880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
12223f542974SPaul B Schroeder 	mos7840_port->icount.tx = 0;
12233f542974SPaul B Schroeder 	mos7840_port->icount.rx = 0;
12243f542974SPaul B Schroeder 
122584fe6e79STony Cook 	dbg("usb_serial serial:%p       mos7840_port:%p\n      usb_serial_port port:%p",
1226880af9dbSAlan Cox 				serial, mos7840_port, port);
12273f542974SPaul B Schroeder 
12283f542974SPaul B Schroeder 	return 0;
12293f542974SPaul B Schroeder }
12303f542974SPaul B Schroeder 
12313f542974SPaul B Schroeder /*****************************************************************************
12323f542974SPaul B Schroeder  * mos7840_chars_in_buffer
12333f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
12343f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
12353f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
12363f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
12373f542974SPaul B Schroeder  *	system,
123895da310eSAlan Cox  *	Otherwise we return zero.
12393f542974SPaul B Schroeder  *****************************************************************************/
12403f542974SPaul B Schroeder 
124195da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
12423f542974SPaul B Schroeder {
124395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
12443f542974SPaul B Schroeder 	int i;
12453f542974SPaul B Schroeder 	int chars = 0;
12460de9a702SOliver Neukum 	unsigned long flags;
12473f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12483f542974SPaul B Schroeder 
1249441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
125084fe6e79STony Cook 		dbg("%s", "Invalid port");
125195da310eSAlan Cox 		return 0;
12523f542974SPaul B Schroeder 	}
12533f542974SPaul B Schroeder 
12543f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
1255*3363155bSGreg Kroah-Hartman 	if (mos7840_port == NULL)
125695da310eSAlan Cox 		return 0;
12573f542974SPaul B Schroeder 
12580de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
125995da310eSAlan Cox 	for (i = 0; i < NUM_URBS; ++i)
126095da310eSAlan Cox 		if (mos7840_port->busy[i])
12613f542974SPaul B Schroeder 			chars += URB_TRANSFER_BUFFER_SIZE;
12620de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1263441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, chars);
12640de9a702SOliver Neukum 	return chars;
12653f542974SPaul B Schroeder 
12663f542974SPaul B Schroeder }
12673f542974SPaul B Schroeder 
12683f542974SPaul B Schroeder /*****************************************************************************
12693f542974SPaul B Schroeder  * mos7840_close
12703f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
12713f542974SPaul B Schroeder  *****************************************************************************/
12723f542974SPaul B Schroeder 
1273335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
12743f542974SPaul B Schroeder {
12753f542974SPaul B Schroeder 	struct usb_serial *serial;
12763f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12770de9a702SOliver Neukum 	struct moschip_port *port0;
12783f542974SPaul B Schroeder 	int j;
12793f542974SPaul B Schroeder 	__u16 Data;
12803f542974SPaul B Schroeder 
1281441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
128284fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
12833f542974SPaul B Schroeder 		return;
12843f542974SPaul B Schroeder 	}
12853f542974SPaul B Schroeder 
1286441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
12873f542974SPaul B Schroeder 	if (!serial) {
128884fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
12893f542974SPaul B Schroeder 		return;
12903f542974SPaul B Schroeder 	}
12913f542974SPaul B Schroeder 
12923f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12930de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
12943f542974SPaul B Schroeder 
12950de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
12963f542974SPaul B Schroeder 		return;
12973f542974SPaul B Schroeder 
12983f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
12993f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
13003f542974SPaul B Schroeder 
13013f542974SPaul B Schroeder 	/* Freeing Write URBs */
13023f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
13033f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
13043f542974SPaul B Schroeder 			if (mos7840_port->write_urb_pool[j]->transfer_buffer)
13053f542974SPaul B Schroeder 				kfree(mos7840_port->write_urb_pool[j]->
13063f542974SPaul B Schroeder 				      transfer_buffer);
13073f542974SPaul B Schroeder 
13083f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
13093f542974SPaul B Schroeder 		}
13103f542974SPaul B Schroeder 	}
13113f542974SPaul B Schroeder 
13123f542974SPaul B Schroeder 	/* While closing port, shutdown all bulk read, write  *
13133f542974SPaul B Schroeder 	 * and interrupt read if they exists                  */
13143f542974SPaul B Schroeder 	if (serial->dev) {
13153f542974SPaul B Schroeder 		if (mos7840_port->write_urb) {
131684fe6e79STony Cook 			dbg("%s", "Shutdown bulk write");
13173f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->write_urb);
13183f542974SPaul B Schroeder 		}
13193f542974SPaul B Schroeder 		if (mos7840_port->read_urb) {
132084fe6e79STony Cook 			dbg("%s", "Shutdown bulk read");
13213f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->read_urb);
132250de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
13233f542974SPaul B Schroeder 		}
13243f542974SPaul B Schroeder 		if ((&mos7840_port->control_urb)) {
132584fe6e79STony Cook 			dbg("%s", "Shutdown control read");
1326880af9dbSAlan Cox 			/*/      usb_kill_urb (mos7840_port->control_urb); */
13273f542974SPaul B Schroeder 		}
13283f542974SPaul B Schroeder 	}
1329880af9dbSAlan Cox /*      if(mos7840_port->ctrl_buf != NULL) */
1330880af9dbSAlan Cox /*              kfree(mos7840_port->ctrl_buf); */
13310de9a702SOliver Neukum 	port0->open_ports--;
133284fe6e79STony Cook 	dbg("mos7840_num_open_ports in close%d:in port%d",
13330de9a702SOliver Neukum 	    port0->open_ports, port->number);
13340de9a702SOliver Neukum 	if (port0->open_ports == 0) {
13353f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_urb) {
133684fe6e79STony Cook 			dbg("%s", "Shutdown interrupt_in_urb");
13370de9a702SOliver Neukum 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
13383f542974SPaul B Schroeder 		}
13393f542974SPaul B Schroeder 	}
13403f542974SPaul B Schroeder 
13413f542974SPaul B Schroeder 	if (mos7840_port->write_urb) {
13423f542974SPaul B Schroeder 		/* if this urb had a transfer buffer already (old tx) free it */
1343880af9dbSAlan Cox 		if (mos7840_port->write_urb->transfer_buffer != NULL)
13443f542974SPaul B Schroeder 			kfree(mos7840_port->write_urb->transfer_buffer);
13453f542974SPaul B Schroeder 		usb_free_urb(mos7840_port->write_urb);
13463f542974SPaul B Schroeder 	}
13473f542974SPaul B Schroeder 
13483f542974SPaul B Schroeder 	Data = 0x0;
13493f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
13503f542974SPaul B Schroeder 
13513f542974SPaul B Schroeder 	Data = 0x00;
13523f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
13533f542974SPaul B Schroeder 
13543f542974SPaul B Schroeder 	mos7840_port->open = 0;
13553f542974SPaul B Schroeder }
13563f542974SPaul B Schroeder 
13573f542974SPaul B Schroeder /************************************************************************
13583f542974SPaul B Schroeder  *
13593f542974SPaul B Schroeder  * mos7840_block_until_chase_response
13603f542974SPaul B Schroeder  *
13613f542974SPaul B Schroeder  *	This function will block the close until one of the following:
13623f542974SPaul B Schroeder  *		1. Response to our Chase comes from mos7840
1363dc0d5c1eSJoe Perches  *		2. A timeout of 10 seconds without activity has expired
13643f542974SPaul B Schroeder  *		   (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
13653f542974SPaul B Schroeder  *
13663f542974SPaul B Schroeder  ************************************************************************/
13673f542974SPaul B Schroeder 
136895da310eSAlan Cox static void mos7840_block_until_chase_response(struct tty_struct *tty,
136995da310eSAlan Cox 					struct moschip_port *mos7840_port)
13703f542974SPaul B Schroeder {
13713f542974SPaul B Schroeder 	int timeout = 1 * HZ;
13723f542974SPaul B Schroeder 	int wait = 10;
13733f542974SPaul B Schroeder 	int count;
13743f542974SPaul B Schroeder 
13753f542974SPaul B Schroeder 	while (1) {
137695da310eSAlan Cox 		count = mos7840_chars_in_buffer(tty);
13773f542974SPaul B Schroeder 
13783f542974SPaul B Schroeder 		/* Check for Buffer status */
1379880af9dbSAlan Cox 		if (count <= 0)
13803f542974SPaul B Schroeder 			return;
13813f542974SPaul B Schroeder 
13823f542974SPaul B Schroeder 		/* Block the thread for a while */
13833f542974SPaul B Schroeder 		interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
13843f542974SPaul B Schroeder 					       timeout);
13853f542974SPaul B Schroeder 		/* No activity.. count down section */
13863f542974SPaul B Schroeder 		wait--;
13873f542974SPaul B Schroeder 		if (wait == 0) {
1388441b62c1SHarvey Harrison 			dbg("%s - TIMEOUT", __func__);
13893f542974SPaul B Schroeder 			return;
13903f542974SPaul B Schroeder 		} else {
1391dc0d5c1eSJoe Perches 			/* Reset timeout value back to seconds */
13923f542974SPaul B Schroeder 			wait = 10;
13933f542974SPaul B Schroeder 		}
13943f542974SPaul B Schroeder 	}
13953f542974SPaul B Schroeder 
13963f542974SPaul B Schroeder }
13973f542974SPaul B Schroeder 
13983f542974SPaul B Schroeder /*****************************************************************************
13993f542974SPaul B Schroeder  * mos7840_break
14003f542974SPaul B Schroeder  *	this function sends a break to the port
14013f542974SPaul B Schroeder  *****************************************************************************/
140295da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
14033f542974SPaul B Schroeder {
140495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14053f542974SPaul B Schroeder 	unsigned char data;
14063f542974SPaul B Schroeder 	struct usb_serial *serial;
14073f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14083f542974SPaul B Schroeder 
1409441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
141084fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
14113f542974SPaul B Schroeder 		return;
14123f542974SPaul B Schroeder 	}
14133f542974SPaul B Schroeder 
1414441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
14153f542974SPaul B Schroeder 	if (!serial) {
141684fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
14173f542974SPaul B Schroeder 		return;
14183f542974SPaul B Schroeder 	}
14193f542974SPaul B Schroeder 
14203f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14213f542974SPaul B Schroeder 
1422880af9dbSAlan Cox 	if (mos7840_port == NULL)
14233f542974SPaul B Schroeder 		return;
14243f542974SPaul B Schroeder 
142595da310eSAlan Cox 	if (serial->dev)
14263f542974SPaul B Schroeder 		/* flush and block until tx is empty */
142795da310eSAlan Cox 		mos7840_block_until_chase_response(tty, mos7840_port);
14283f542974SPaul B Schroeder 
142995da310eSAlan Cox 	if (break_state == -1)
14303f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
143195da310eSAlan Cox 	else
14323f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
14333f542974SPaul B Schroeder 
14346b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
14353f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
143684fe6e79STony Cook 	dbg("mcs7840_break mos7840_port->shadowLCR is %x",
14373f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14383f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
14393f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
14403f542974SPaul B Schroeder }
14413f542974SPaul B Schroeder 
14423f542974SPaul B Schroeder /*****************************************************************************
14433f542974SPaul B Schroeder  * mos7840_write_room
14443f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
14453f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
14463f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
14473f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
14483f542974SPaul B Schroeder  *****************************************************************************/
14493f542974SPaul B Schroeder 
145095da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
14513f542974SPaul B Schroeder {
145295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
14533f542974SPaul B Schroeder 	int i;
14543f542974SPaul B Schroeder 	int room = 0;
14550de9a702SOliver Neukum 	unsigned long flags;
14563f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14573f542974SPaul B Schroeder 
1458441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
145984fe6e79STony Cook 		dbg("%s", "Invalid port");
146084fe6e79STony Cook 		dbg("%s", " mos7840_write_room:leaving ...........");
14613f542974SPaul B Schroeder 		return -1;
14623f542974SPaul B Schroeder 	}
14633f542974SPaul B Schroeder 
14643f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14653f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
146684fe6e79STony Cook 		dbg("%s", "mos7840_break:leaving ...........");
14673f542974SPaul B Schroeder 		return -1;
14683f542974SPaul B Schroeder 	}
14693f542974SPaul B Schroeder 
14700de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
14713f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
1472880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
14733f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
14743f542974SPaul B Schroeder 	}
14750de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
14763f542974SPaul B Schroeder 
14770de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1478441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, room);
14790de9a702SOliver Neukum 	return room;
14803f542974SPaul B Schroeder 
14813f542974SPaul B Schroeder }
14823f542974SPaul B Schroeder 
14833f542974SPaul B Schroeder /*****************************************************************************
14843f542974SPaul B Schroeder  * mos7840_write
14853f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
14863f542974SPaul B Schroeder  *	the port.
14873f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
14883f542974SPaul B Schroeder  *      return a negative error number.
14893f542974SPaul B Schroeder  *****************************************************************************/
14903f542974SPaul B Schroeder 
149195da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
14923f542974SPaul B Schroeder 			 const unsigned char *data, int count)
14933f542974SPaul B Schroeder {
14943f542974SPaul B Schroeder 	int status;
14953f542974SPaul B Schroeder 	int i;
14963f542974SPaul B Schroeder 	int bytes_sent = 0;
14973f542974SPaul B Schroeder 	int transfer_size;
14980de9a702SOliver Neukum 	unsigned long flags;
14993f542974SPaul B Schroeder 
15003f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15013f542974SPaul B Schroeder 	struct usb_serial *serial;
15023f542974SPaul B Schroeder 	struct urb *urb;
1503880af9dbSAlan Cox 	/* __u16 Data; */
15043f542974SPaul B Schroeder 	const unsigned char *current_position = data;
15053f542974SPaul B Schroeder 	unsigned char *data1;
15063f542974SPaul B Schroeder 
15073f542974SPaul B Schroeder #ifdef NOTMOS7840
15083f542974SPaul B Schroeder 	Data = 0x00;
15093f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
15103f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
151184fe6e79STony Cook 	dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
151284fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
15133f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
15143f542974SPaul B Schroeder 
1515880af9dbSAlan Cox 	/* Data = 0x03; */
1516880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1517880af9dbSAlan Cox 	/* mos7840_port->shadowLCR=Data;//Need to add later */
15183f542974SPaul B Schroeder 
1519880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
15203f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
15213f542974SPaul B Schroeder 
1522880af9dbSAlan Cox 	/* Data = 0x0c; */
1523880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
15243f542974SPaul B Schroeder 	Data = 0x00;
15253f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
152684fe6e79STony Cook 	dbg("mos7840_write:DLL value is %x", Data);
15273f542974SPaul B Schroeder 
15283f542974SPaul B Schroeder 	Data = 0x0;
15293f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
153084fe6e79STony Cook 	dbg("mos7840_write:DLM value is %x", Data);
15313f542974SPaul B Schroeder 
15323f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
153384fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
15343f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
15353f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
15363f542974SPaul B Schroeder #endif
15373f542974SPaul B Schroeder 
1538441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
153984fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
15403f542974SPaul B Schroeder 		return -1;
15413f542974SPaul B Schroeder 	}
15423f542974SPaul B Schroeder 
15433f542974SPaul B Schroeder 	serial = port->serial;
1544441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
154584fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
15463f542974SPaul B Schroeder 		return -1;
15473f542974SPaul B Schroeder 	}
15483f542974SPaul B Schroeder 
15493f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15503f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
155184fe6e79STony Cook 		dbg("%s", "mos7840_port is NULL");
15523f542974SPaul B Schroeder 		return -1;
15533f542974SPaul B Schroeder 	}
15543f542974SPaul B Schroeder 
15553f542974SPaul B Schroeder 	/* try to find a free urb in the list */
15563f542974SPaul B Schroeder 	urb = NULL;
15573f542974SPaul B Schroeder 
15580de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
15593f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
15600de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
15610de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
15623f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
156384fe6e79STony Cook 			dbg("URB:%d", i);
15643f542974SPaul B Schroeder 			break;
15653f542974SPaul B Schroeder 		}
15663f542974SPaul B Schroeder 	}
15670de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
15683f542974SPaul B Schroeder 
15693f542974SPaul B Schroeder 	if (urb == NULL) {
1570441b62c1SHarvey Harrison 		dbg("%s - no more free urbs", __func__);
15713f542974SPaul B Schroeder 		goto exit;
15723f542974SPaul B Schroeder 	}
15733f542974SPaul B Schroeder 
15743f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
15753f542974SPaul B Schroeder 		urb->transfer_buffer =
15763f542974SPaul B Schroeder 		    kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
15773f542974SPaul B Schroeder 
15783f542974SPaul B Schroeder 		if (urb->transfer_buffer == NULL) {
157922a416c4SJohan Hovold 			dev_err_console(port, "%s no more kernel memory...\n",
1580194343d9SGreg Kroah-Hartman 				__func__);
15813f542974SPaul B Schroeder 			goto exit;
15823f542974SPaul B Schroeder 		}
15833f542974SPaul B Schroeder 	}
15843f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
15853f542974SPaul B Schroeder 
15863f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
15873f542974SPaul B Schroeder 
15883f542974SPaul B Schroeder 	/* fill urb with data and submit  */
1589093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1590093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1591093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1592093ea2d3SDonald Lee 		usb_fill_bulk_urb(urb,
1593093ea2d3SDonald Lee 			serial->dev,
1594093ea2d3SDonald Lee 			usb_sndbulkpipe(serial->dev,
1595093ea2d3SDonald Lee 				(port->bulk_out_endpointAddress) + 2),
1596093ea2d3SDonald Lee 			urb->transfer_buffer,
1597093ea2d3SDonald Lee 			transfer_size,
1598093ea2d3SDonald Lee 			mos7840_bulk_out_data_callback, mos7840_port);
1599093ea2d3SDonald Lee 	} else {
16003f542974SPaul B Schroeder 		usb_fill_bulk_urb(urb,
16013f542974SPaul B Schroeder 			serial->dev,
16023f542974SPaul B Schroeder 			usb_sndbulkpipe(serial->dev,
16033f542974SPaul B Schroeder 				port->bulk_out_endpointAddress),
16043f542974SPaul B Schroeder 			urb->transfer_buffer,
16053f542974SPaul B Schroeder 			transfer_size,
16063f542974SPaul B Schroeder 			mos7840_bulk_out_data_callback, mos7840_port);
1607093ea2d3SDonald Lee 	}
16083f542974SPaul B Schroeder 
16093f542974SPaul B Schroeder 	data1 = urb->transfer_buffer;
161084fe6e79STony Cook 	dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
16113f542974SPaul B Schroeder 
16120eafe4deSDonald 	/* Turn on LED */
16130eafe4deSDonald 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
16140eafe4deSDonald 		mos7840_port->led_flag = true;
16150eafe4deSDonald 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
16160eafe4deSDonald 		mod_timer(&mos7840_port->led_timer1,
16170eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_ON_MS));
16180eafe4deSDonald 	}
16190eafe4deSDonald 
16203f542974SPaul B Schroeder 	/* send it down the pipe */
16213f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
16223f542974SPaul B Schroeder 
16233f542974SPaul B Schroeder 	if (status) {
16240de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
162522a416c4SJohan Hovold 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1626194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
16273f542974SPaul B Schroeder 		bytes_sent = status;
16283f542974SPaul B Schroeder 		goto exit;
16293f542974SPaul B Schroeder 	}
16303f542974SPaul B Schroeder 	bytes_sent = transfer_size;
16313f542974SPaul B Schroeder 	mos7840_port->icount.tx += transfer_size;
16320de9a702SOliver Neukum 	smp_wmb();
163384fe6e79STony Cook 	dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
16343f542974SPaul B Schroeder exit:
16353f542974SPaul B Schroeder 	return bytes_sent;
16363f542974SPaul B Schroeder 
16373f542974SPaul B Schroeder }
16383f542974SPaul B Schroeder 
16393f542974SPaul B Schroeder /*****************************************************************************
16403f542974SPaul B Schroeder  * mos7840_throttle
16413f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
16423f542974SPaul B Schroeder  *	being read from the port.
16433f542974SPaul B Schroeder  *****************************************************************************/
16443f542974SPaul B Schroeder 
164595da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
16463f542974SPaul B Schroeder {
164795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16483f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
16493f542974SPaul B Schroeder 	int status;
16503f542974SPaul B Schroeder 
1651441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
165284fe6e79STony Cook 		dbg("%s", "Invalid port");
16533f542974SPaul B Schroeder 		return;
16543f542974SPaul B Schroeder 	}
16553f542974SPaul B Schroeder 
165684fe6e79STony Cook 	dbg("- port %d", port->number);
16573f542974SPaul B Schroeder 
16583f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
16593f542974SPaul B Schroeder 
16603f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16613f542974SPaul B Schroeder 		return;
16623f542974SPaul B Schroeder 
16633f542974SPaul B Schroeder 	if (!mos7840_port->open) {
166484fe6e79STony Cook 		dbg("%s", "port not opened");
16653f542974SPaul B Schroeder 		return;
16663f542974SPaul B Schroeder 	}
16673f542974SPaul B Schroeder 
16683f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
16693f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
16703f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
167195da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
167295da310eSAlan Cox 		if (status <= 0)
16733f542974SPaul B Schroeder 			return;
16743f542974SPaul B Schroeder 	}
16753f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
16763f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
16773f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
167895da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
16793f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
168095da310eSAlan Cox 		if (status < 0)
16813f542974SPaul B Schroeder 			return;
16823f542974SPaul B Schroeder 	}
16833f542974SPaul B Schroeder }
16843f542974SPaul B Schroeder 
16853f542974SPaul B Schroeder /*****************************************************************************
16863f542974SPaul B Schroeder  * mos7840_unthrottle
1687880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
1688880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
1689880af9dbSAlan Cox  *	called)
16903f542974SPaul B Schroeder  *****************************************************************************/
169195da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
16923f542974SPaul B Schroeder {
169395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16943f542974SPaul B Schroeder 	int status;
16953f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
16963f542974SPaul B Schroeder 
1697441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
169884fe6e79STony Cook 		dbg("%s", "Invalid port");
16993f542974SPaul B Schroeder 		return;
17003f542974SPaul B Schroeder 	}
17013f542974SPaul B Schroeder 
17023f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17033f542974SPaul B Schroeder 		return;
17043f542974SPaul B Schroeder 
17053f542974SPaul B Schroeder 	if (!mos7840_port->open) {
1706441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
17073f542974SPaul B Schroeder 		return;
17083f542974SPaul B Schroeder 	}
17093f542974SPaul B Schroeder 
17103f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
17113f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
17123f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
171395da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
171495da310eSAlan Cox 		if (status <= 0)
17153f542974SPaul B Schroeder 			return;
17163f542974SPaul B Schroeder 	}
17173f542974SPaul B Schroeder 
17183f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
17193f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
17203f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
172195da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
17223f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
172395da310eSAlan Cox 		if (status < 0)
17243f542974SPaul B Schroeder 			return;
17253f542974SPaul B Schroeder 	}
17263f542974SPaul B Schroeder }
17273f542974SPaul B Schroeder 
172860b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
17293f542974SPaul B Schroeder {
173095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
17313f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
17323f542974SPaul B Schroeder 	unsigned int result;
17333f542974SPaul B Schroeder 	__u16 msr;
17343f542974SPaul B Schroeder 	__u16 mcr;
1735880af9dbSAlan Cox 	int status;
17363f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
17373f542974SPaul B Schroeder 
17383f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17393f542974SPaul B Schroeder 		return -ENODEV;
17403f542974SPaul B Schroeder 
17413f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
17423f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
17433f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
17443f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
17453f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
17463f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
17473f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
17483f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
17493f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
17503f542974SPaul B Schroeder 
1751441b62c1SHarvey Harrison 	dbg("%s - 0x%04X", __func__, result);
17523f542974SPaul B Schroeder 
17533f542974SPaul B Schroeder 	return result;
17543f542974SPaul B Schroeder }
17553f542974SPaul B Schroeder 
175620b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
17573f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
17583f542974SPaul B Schroeder {
175995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
17603f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
17613f542974SPaul B Schroeder 	unsigned int mcr;
176287521c46SRoel Kluin 	int status;
17633f542974SPaul B Schroeder 
17643f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
17653f542974SPaul B Schroeder 
17663f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17673f542974SPaul B Schroeder 		return -ENODEV;
17683f542974SPaul B Schroeder 
1769e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
17703f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
17713f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
17723f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
17733f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
17743f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
17753f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
17763f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
17773f542974SPaul B Schroeder 
17783f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
17793f542974SPaul B Schroeder 		mcr |= MCR_RTS;
17803f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
17813f542974SPaul B Schroeder 		mcr |= MCR_DTR;
17823f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
17833f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
17843f542974SPaul B Schroeder 
17853f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
17863f542974SPaul B Schroeder 
17873f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
17883f542974SPaul B Schroeder 	if (status < 0) {
178984fe6e79STony Cook 		dbg("setting MODEM_CONTROL_REGISTER Failed");
179087521c46SRoel Kluin 		return status;
17913f542974SPaul B Schroeder 	}
17923f542974SPaul B Schroeder 
17933f542974SPaul B Schroeder 	return 0;
17943f542974SPaul B Schroeder }
17953f542974SPaul B Schroeder 
17963f542974SPaul B Schroeder /*****************************************************************************
17973f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
17983f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
17993f542974SPaul B Schroeder  *	baud rate.
18003f542974SPaul B Schroeder  *****************************************************************************/
18013f542974SPaul B Schroeder static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
18023f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
18033f542974SPaul B Schroeder {
1804441b62c1SHarvey Harrison 	dbg("%s - %d", __func__, baudRate);
18053f542974SPaul B Schroeder 
18063f542974SPaul B Schroeder 	if (baudRate <= 115200) {
18073f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
18083f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
18093f542974SPaul B Schroeder 	}
18103f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
18113f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
18123f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
18133f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
18143f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
18153f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
18163f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
18173f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
18183f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
18193f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
18203f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
18213f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
18223f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
18233f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
18243f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
18253f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
18263f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
18273f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
18283f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
18293f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
18303f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
18313f542974SPaul B Schroeder 	}
18323f542974SPaul B Schroeder 	return 0;
18333f542974SPaul B Schroeder 
18343f542974SPaul B Schroeder #ifdef NOTMCS7840
18353f542974SPaul B Schroeder 
18363f542974SPaul B Schroeder 	for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
18373f542974SPaul B Schroeder 		if (mos7840_divisor_table[i].BaudRate == baudrate) {
18383f542974SPaul B Schroeder 			*divisor = mos7840_divisor_table[i].Divisor;
18393f542974SPaul B Schroeder 			return 0;
18403f542974SPaul B Schroeder 		}
18413f542974SPaul B Schroeder 	}
18423f542974SPaul B Schroeder 
18433f542974SPaul B Schroeder 	/* After trying for all the standard baud rates    *
18443f542974SPaul B Schroeder 	 * Try calculating the divisor for this baud rate  */
18453f542974SPaul B Schroeder 
18463f542974SPaul B Schroeder 	if (baudrate > 75 && baudrate < 230400) {
18473f542974SPaul B Schroeder 		/* get the divisor */
18483f542974SPaul B Schroeder 		custom = (__u16) (230400L / baudrate);
18493f542974SPaul B Schroeder 
18503f542974SPaul B Schroeder 		/* Check for round off */
18513f542974SPaul B Schroeder 		round1 = (__u16) (2304000L / baudrate);
18523f542974SPaul B Schroeder 		round = (__u16) (round1 - (custom * 10));
1853880af9dbSAlan Cox 		if (round > 4)
18543f542974SPaul B Schroeder 			custom++;
18553f542974SPaul B Schroeder 		*divisor = custom;
18563f542974SPaul B Schroeder 
185784fe6e79STony Cook 		dbg(" Baud %d = %d", baudrate, custom);
18583f542974SPaul B Schroeder 		return 0;
18593f542974SPaul B Schroeder 	}
18603f542974SPaul B Schroeder 
186184fe6e79STony Cook 	dbg("%s", " Baud calculation Failed...");
18623f542974SPaul B Schroeder 	return -1;
18633f542974SPaul B Schroeder #endif
18643f542974SPaul B Schroeder }
18653f542974SPaul B Schroeder 
18663f542974SPaul B Schroeder /*****************************************************************************
18673f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
18683f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
18693f542974SPaul B Schroeder  *	specified port.
18703f542974SPaul B Schroeder  *****************************************************************************/
18713f542974SPaul B Schroeder 
18723f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
18733f542974SPaul B Schroeder 					    int baudRate)
18743f542974SPaul B Schroeder {
18753f542974SPaul B Schroeder 	int divisor = 0;
18763f542974SPaul B Schroeder 	int status;
18773f542974SPaul B Schroeder 	__u16 Data;
18783f542974SPaul B Schroeder 	unsigned char number;
18793f542974SPaul B Schroeder 	__u16 clk_sel_val;
18803f542974SPaul B Schroeder 	struct usb_serial_port *port;
18813f542974SPaul B Schroeder 
18823f542974SPaul B Schroeder 	if (mos7840_port == NULL)
18833f542974SPaul B Schroeder 		return -1;
18843f542974SPaul B Schroeder 
18853f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
1886441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
188784fe6e79STony Cook 		dbg("%s", "Invalid port");
18883f542974SPaul B Schroeder 		return -1;
18893f542974SPaul B Schroeder 	}
18903f542974SPaul B Schroeder 
1891441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
189284fe6e79STony Cook 		dbg("%s", "Invalid Serial");
18933f542974SPaul B Schroeder 		return -1;
18943f542974SPaul B Schroeder 	}
18953f542974SPaul B Schroeder 
18963f542974SPaul B Schroeder 	number = mos7840_port->port->number - mos7840_port->port->serial->minor;
18973f542974SPaul B Schroeder 
1898441b62c1SHarvey Harrison 	dbg("%s - port = %d, baud = %d", __func__,
18993f542974SPaul B Schroeder 	    mos7840_port->port->number, baudRate);
1900880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
19013f542974SPaul B Schroeder 	if (baudRate > 115200) {
19023f542974SPaul B Schroeder #ifdef HW_flow_control
1903880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1904880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
19053f542974SPaul B Schroeder 		Data = 0x2b;
19063f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1907880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1908880af9dbSAlan Cox 									Data);
19093f542974SPaul B Schroeder 		if (status < 0) {
191084fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
19113f542974SPaul B Schroeder 			return -1;
19123f542974SPaul B Schroeder 		}
19133f542974SPaul B Schroeder #endif
19143f542974SPaul B Schroeder 
19153f542974SPaul B Schroeder 	} else {
19163f542974SPaul B Schroeder #ifdef HW_flow_control
1917880af9dbSAlan Cox 		/* setting h/w flow control bit to 0 */
19183f542974SPaul B Schroeder 		Data = 0xb;
19193f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1920880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1921880af9dbSAlan Cox 									Data);
19223f542974SPaul B Schroeder 		if (status < 0) {
192384fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
19243f542974SPaul B Schroeder 			return -1;
19253f542974SPaul B Schroeder 		}
19263f542974SPaul B Schroeder #endif
19273f542974SPaul B Schroeder 
19283f542974SPaul B Schroeder 	}
19293f542974SPaul B Schroeder 
1930880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
19313f542974SPaul B Schroeder 		clk_sel_val = 0x0;
19323f542974SPaul B Schroeder 		Data = 0x0;
1933880af9dbSAlan Cox 		status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
19343f542974SPaul B Schroeder 						   &clk_sel_val);
1935880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
19363f542974SPaul B Schroeder 								 &Data);
19373f542974SPaul B Schroeder 		if (status < 0) {
193884fe6e79STony Cook 			dbg("reading spreg failed in set_serial_baud");
19393f542974SPaul B Schroeder 			return -1;
19403f542974SPaul B Schroeder 		}
19413f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1942880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1943880af9dbSAlan Cox 								Data);
19443f542974SPaul B Schroeder 		if (status < 0) {
194584fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
19463f542974SPaul B Schroeder 			return -1;
19473f542974SPaul B Schroeder 		}
19483f542974SPaul B Schroeder 		/* Calculate the Divisor */
19493f542974SPaul B Schroeder 
19503f542974SPaul B Schroeder 		if (status) {
1951194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
19523f542974SPaul B Schroeder 			return status;
19533f542974SPaul B Schroeder 		}
19543f542974SPaul B Schroeder 		/* Enable access to divisor latch */
19553f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
19563f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
19573f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19583f542974SPaul B Schroeder 
19593f542974SPaul B Schroeder 		/* Write the divisor */
19603f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
196184fe6e79STony Cook 		dbg("set_serial_baud Value to write DLL is %x", Data);
19623f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
19633f542974SPaul B Schroeder 
19643f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
196584fe6e79STony Cook 		dbg("set_serial_baud Value to write DLM is %x", Data);
19663f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
19673f542974SPaul B Schroeder 
19683f542974SPaul B Schroeder 		/* Disable access to divisor latch */
19693f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
19703f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
19713f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19723f542974SPaul B Schroeder 
19733f542974SPaul B Schroeder 	}
19743f542974SPaul B Schroeder 	return status;
19753f542974SPaul B Schroeder }
19763f542974SPaul B Schroeder 
19773f542974SPaul B Schroeder /*****************************************************************************
19783f542974SPaul B Schroeder  * mos7840_change_port_settings
19793f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
19803f542974SPaul B Schroeder  *      the specified new settings.
19813f542974SPaul B Schroeder  *****************************************************************************/
19823f542974SPaul B Schroeder 
198395da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
198495da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
19853f542974SPaul B Schroeder {
19863f542974SPaul B Schroeder 	int baud;
19873f542974SPaul B Schroeder 	unsigned cflag;
19883f542974SPaul B Schroeder 	unsigned iflag;
19893f542974SPaul B Schroeder 	__u8 lData;
19903f542974SPaul B Schroeder 	__u8 lParity;
19913f542974SPaul B Schroeder 	__u8 lStop;
19923f542974SPaul B Schroeder 	int status;
19933f542974SPaul B Schroeder 	__u16 Data;
19943f542974SPaul B Schroeder 	struct usb_serial_port *port;
19953f542974SPaul B Schroeder 	struct usb_serial *serial;
19963f542974SPaul B Schroeder 
19973f542974SPaul B Schroeder 	if (mos7840_port == NULL)
19983f542974SPaul B Schroeder 		return;
19993f542974SPaul B Schroeder 
20003f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
20013f542974SPaul B Schroeder 
2002441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
200384fe6e79STony Cook 		dbg("%s", "Invalid port");
20043f542974SPaul B Schroeder 		return;
20053f542974SPaul B Schroeder 	}
20063f542974SPaul B Schroeder 
2007441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
200884fe6e79STony Cook 		dbg("%s", "Invalid Serial");
20093f542974SPaul B Schroeder 		return;
20103f542974SPaul B Schroeder 	}
20113f542974SPaul B Schroeder 
20123f542974SPaul B Schroeder 	serial = port->serial;
20133f542974SPaul B Schroeder 
2014441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, mos7840_port->port->number);
20153f542974SPaul B Schroeder 
20163f542974SPaul B Schroeder 	if (!mos7840_port->open) {
2017441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
20183f542974SPaul B Schroeder 		return;
20193f542974SPaul B Schroeder 	}
20203f542974SPaul B Schroeder 
20213f542974SPaul B Schroeder 	lData = LCR_BITS_8;
20223f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
20233f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
20243f542974SPaul B Schroeder 
20253f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
20263f542974SPaul B Schroeder 	iflag = tty->termios->c_iflag;
20273f542974SPaul B Schroeder 
20283f542974SPaul B Schroeder 	/* Change the number of bits */
20293f542974SPaul B Schroeder 	if (cflag & CSIZE) {
20303f542974SPaul B Schroeder 		switch (cflag & CSIZE) {
20313f542974SPaul B Schroeder 		case CS5:
20323f542974SPaul B Schroeder 			lData = LCR_BITS_5;
20333f542974SPaul B Schroeder 			break;
20343f542974SPaul B Schroeder 
20353f542974SPaul B Schroeder 		case CS6:
20363f542974SPaul B Schroeder 			lData = LCR_BITS_6;
20373f542974SPaul B Schroeder 			break;
20383f542974SPaul B Schroeder 
20393f542974SPaul B Schroeder 		case CS7:
20403f542974SPaul B Schroeder 			lData = LCR_BITS_7;
20413f542974SPaul B Schroeder 			break;
20423f542974SPaul B Schroeder 		default:
20433f542974SPaul B Schroeder 		case CS8:
20443f542974SPaul B Schroeder 			lData = LCR_BITS_8;
20453f542974SPaul B Schroeder 			break;
20463f542974SPaul B Schroeder 		}
20473f542974SPaul B Schroeder 	}
20483f542974SPaul B Schroeder 	/* Change the Parity bit */
20493f542974SPaul B Schroeder 	if (cflag & PARENB) {
20503f542974SPaul B Schroeder 		if (cflag & PARODD) {
20513f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
2052441b62c1SHarvey Harrison 			dbg("%s - parity = odd", __func__);
20533f542974SPaul B Schroeder 		} else {
20543f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
2055441b62c1SHarvey Harrison 			dbg("%s - parity = even", __func__);
20563f542974SPaul B Schroeder 		}
20573f542974SPaul B Schroeder 
20583f542974SPaul B Schroeder 	} else {
2059441b62c1SHarvey Harrison 		dbg("%s - parity = none", __func__);
20603f542974SPaul B Schroeder 	}
20613f542974SPaul B Schroeder 
2062880af9dbSAlan Cox 	if (cflag & CMSPAR)
20633f542974SPaul B Schroeder 		lParity = lParity | 0x20;
20643f542974SPaul B Schroeder 
20653f542974SPaul B Schroeder 	/* Change the Stop bit */
20663f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
20673f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
2068441b62c1SHarvey Harrison 		dbg("%s - stop bits = 2", __func__);
20693f542974SPaul B Schroeder 	} else {
20703f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
2071441b62c1SHarvey Harrison 		dbg("%s - stop bits = 1", __func__);
20723f542974SPaul B Schroeder 	}
20733f542974SPaul B Schroeder 
20743f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
20753f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
20763f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
20773f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
20783f542974SPaul B Schroeder 
207984fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
20803f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
20813f542974SPaul B Schroeder 	/* Disable Interrupts */
20823f542974SPaul B Schroeder 	Data = 0x00;
20833f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
20843f542974SPaul B Schroeder 
20853f542974SPaul B Schroeder 	Data = 0x00;
20863f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
20873f542974SPaul B Schroeder 
20883f542974SPaul B Schroeder 	Data = 0xcf;
20893f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
20903f542974SPaul B Schroeder 
20913f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
20923f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
20933f542974SPaul B Schroeder 
20943f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
20953f542974SPaul B Schroeder 
20963f542974SPaul B Schroeder 	Data = 0x00b;
20973f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
20983f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20993f542974SPaul B Schroeder 	Data = 0x00b;
21003f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
21013f542974SPaul B Schroeder 
21023f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
21033f542974SPaul B Schroeder 
21043f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
2105880af9dbSAlan Cox 	if (cflag & CBAUD)
21063f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
21073f542974SPaul B Schroeder 
2108880af9dbSAlan Cox 	if (cflag & CRTSCTS)
21093f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
2110880af9dbSAlan Cox 	else
21113f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
21123f542974SPaul B Schroeder 
21133f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
21143f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
21153f542974SPaul B Schroeder 
21163f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
21173f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
21183f542974SPaul B Schroeder 
21193f542974SPaul B Schroeder 	if (!baud) {
21203f542974SPaul B Schroeder 		/* pick a default, any default... */
212184fe6e79STony Cook 		dbg("%s", "Picked default baud...");
21223f542974SPaul B Schroeder 		baud = 9600;
21233f542974SPaul B Schroeder 	}
21243f542974SPaul B Schroeder 
2125441b62c1SHarvey Harrison 	dbg("%s - baud rate = %d", __func__, baud);
21263f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
21273f542974SPaul B Schroeder 
21283f542974SPaul B Schroeder 	/* Enable Interrupts */
21293f542974SPaul B Schroeder 	Data = 0x0c;
21303f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
21313f542974SPaul B Schroeder 
213250de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
213350de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
21343f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
21353f542974SPaul B Schroeder 		if (status) {
21363f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
21373f542974SPaul B Schroeder 			    status);
213850de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
21393f542974SPaul B Schroeder 		}
21403f542974SPaul B Schroeder 	}
21413f542974SPaul B Schroeder 	wake_up(&mos7840_port->delta_msr_wait);
21423f542974SPaul B Schroeder 	mos7840_port->delta_msr_cond = 1;
214384fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
21443f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
21453f542974SPaul B Schroeder }
21463f542974SPaul B Schroeder 
21473f542974SPaul B Schroeder /*****************************************************************************
21483f542974SPaul B Schroeder  * mos7840_set_termios
21493f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
21503f542974SPaul B Schroeder  *	the termios structure
21513f542974SPaul B Schroeder  *****************************************************************************/
21523f542974SPaul B Schroeder 
215395da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
215495da310eSAlan Cox 				struct usb_serial_port *port,
2155606d099cSAlan Cox 				struct ktermios *old_termios)
21563f542974SPaul B Schroeder {
21573f542974SPaul B Schroeder 	int status;
21583f542974SPaul B Schroeder 	unsigned int cflag;
21593f542974SPaul B Schroeder 	struct usb_serial *serial;
21603f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
2161*3363155bSGreg Kroah-Hartman 
2162441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
216384fe6e79STony Cook 		dbg("%s", "Invalid port");
21643f542974SPaul B Schroeder 		return;
21653f542974SPaul B Schroeder 	}
21663f542974SPaul B Schroeder 
21673f542974SPaul B Schroeder 	serial = port->serial;
21683f542974SPaul B Schroeder 
2169441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
217084fe6e79STony Cook 		dbg("%s", "Invalid Serial");
21713f542974SPaul B Schroeder 		return;
21723f542974SPaul B Schroeder 	}
21733f542974SPaul B Schroeder 
21743f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
21753f542974SPaul B Schroeder 
21763f542974SPaul B Schroeder 	if (mos7840_port == NULL)
21773f542974SPaul B Schroeder 		return;
21783f542974SPaul B Schroeder 
21793f542974SPaul B Schroeder 	if (!mos7840_port->open) {
2180441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
21813f542974SPaul B Schroeder 		return;
21823f542974SPaul B Schroeder 	}
21833f542974SPaul B Schroeder 
218484fe6e79STony Cook 	dbg("%s", "setting termios - ");
21853f542974SPaul B Schroeder 
21863f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
21873f542974SPaul B Schroeder 
2188441b62c1SHarvey Harrison 	dbg("%s - clfag %08x iflag %08x", __func__,
21893f542974SPaul B Schroeder 	    tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2190441b62c1SHarvey Harrison 	dbg("%s - old clfag %08x old iflag %08x", __func__,
21913f542974SPaul B Schroeder 	    old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2192441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21933f542974SPaul B Schroeder 
21943f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
21953f542974SPaul B Schroeder 
219695da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
21973f542974SPaul B Schroeder 
21983f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
219984fe6e79STony Cook 		dbg("%s", "URB KILLED !!!!!");
22003f542974SPaul B Schroeder 		return;
22013f542974SPaul B Schroeder 	}
22023f542974SPaul B Schroeder 
220350de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
220450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
22053f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
22063f542974SPaul B Schroeder 		if (status) {
22073f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
22083f542974SPaul B Schroeder 			    status);
220950de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
22103f542974SPaul B Schroeder 		}
22113f542974SPaul B Schroeder 	}
22123f542974SPaul B Schroeder }
22133f542974SPaul B Schroeder 
22143f542974SPaul B Schroeder /*****************************************************************************
22153f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
22163f542974SPaul B Schroeder  *
22173f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
22183f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
22193f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
22203f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
22213f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
22223f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
22233f542974SPaul B Schroeder  *****************************************************************************/
22243f542974SPaul B Schroeder 
222595da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
222697c4965dSAl Viro 				unsigned int __user *value)
22273f542974SPaul B Schroeder {
22283f542974SPaul B Schroeder 	int count;
22293f542974SPaul B Schroeder 	unsigned int result = 0;
22303f542974SPaul B Schroeder 
223195da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
22323f542974SPaul B Schroeder 	if (count == 0) {
2233441b62c1SHarvey Harrison 		dbg("%s -- Empty", __func__);
22343f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
22353f542974SPaul B Schroeder 	}
22363f542974SPaul B Schroeder 
22373f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
22383f542974SPaul B Schroeder 		return -EFAULT;
22393f542974SPaul B Schroeder 	return 0;
22403f542974SPaul B Schroeder }
22413f542974SPaul B Schroeder 
22423f542974SPaul B Schroeder /*****************************************************************************
22433f542974SPaul B Schroeder  * mos7840_get_serial_info
22443f542974SPaul B Schroeder  *      function to get information about serial port
22453f542974SPaul B Schroeder  *****************************************************************************/
22463f542974SPaul B Schroeder 
22473f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
224897c4965dSAl Viro 				   struct serial_struct __user *retinfo)
22493f542974SPaul B Schroeder {
22503f542974SPaul B Schroeder 	struct serial_struct tmp;
22513f542974SPaul B Schroeder 
22523f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22533f542974SPaul B Schroeder 		return -1;
22543f542974SPaul B Schroeder 
22553f542974SPaul B Schroeder 	if (!retinfo)
22563f542974SPaul B Schroeder 		return -EFAULT;
22573f542974SPaul B Schroeder 
22583f542974SPaul B Schroeder 	memset(&tmp, 0, sizeof(tmp));
22593f542974SPaul B Schroeder 
22603f542974SPaul B Schroeder 	tmp.type = PORT_16550A;
22613f542974SPaul B Schroeder 	tmp.line = mos7840_port->port->serial->minor;
22623f542974SPaul B Schroeder 	tmp.port = mos7840_port->port->number;
22633f542974SPaul B Schroeder 	tmp.irq = 0;
22643f542974SPaul B Schroeder 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
22653f542974SPaul B Schroeder 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
22663f542974SPaul B Schroeder 	tmp.baud_base = 9600;
22673f542974SPaul B Schroeder 	tmp.close_delay = 5 * HZ;
22683f542974SPaul B Schroeder 	tmp.closing_wait = 30 * HZ;
22693f542974SPaul B Schroeder 
22703f542974SPaul B Schroeder 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
22713f542974SPaul B Schroeder 		return -EFAULT;
22723f542974SPaul B Schroeder 	return 0;
22733f542974SPaul B Schroeder }
22743f542974SPaul B Schroeder 
22750bca1b91SAlan Cox static int mos7840_get_icount(struct tty_struct *tty,
22760bca1b91SAlan Cox 			struct serial_icounter_struct *icount)
22770bca1b91SAlan Cox {
22780bca1b91SAlan Cox 	struct usb_serial_port *port = tty->driver_data;
22790bca1b91SAlan Cox 	struct moschip_port *mos7840_port;
22800bca1b91SAlan Cox 	struct async_icount cnow;
22810bca1b91SAlan Cox 
22820bca1b91SAlan Cox 	mos7840_port = mos7840_get_port_private(port);
22830bca1b91SAlan Cox 	cnow = mos7840_port->icount;
22840bca1b91SAlan Cox 
22850bca1b91SAlan Cox 	smp_rmb();
22860bca1b91SAlan Cox 	icount->cts = cnow.cts;
22870bca1b91SAlan Cox 	icount->dsr = cnow.dsr;
22880bca1b91SAlan Cox 	icount->rng = cnow.rng;
22890bca1b91SAlan Cox 	icount->dcd = cnow.dcd;
22900bca1b91SAlan Cox 	icount->rx = cnow.rx;
22910bca1b91SAlan Cox 	icount->tx = cnow.tx;
22920bca1b91SAlan Cox 	icount->frame = cnow.frame;
22930bca1b91SAlan Cox 	icount->overrun = cnow.overrun;
22940bca1b91SAlan Cox 	icount->parity = cnow.parity;
22950bca1b91SAlan Cox 	icount->brk = cnow.brk;
22960bca1b91SAlan Cox 	icount->buf_overrun = cnow.buf_overrun;
22970bca1b91SAlan Cox 
22980bca1b91SAlan Cox 	dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
22990bca1b91SAlan Cox 		port->number, icount->rx, icount->tx);
23000bca1b91SAlan Cox 	return 0;
23010bca1b91SAlan Cox }
23020bca1b91SAlan Cox 
23033f542974SPaul B Schroeder /*****************************************************************************
23043f542974SPaul B Schroeder  * SerialIoctl
23053f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
23063f542974SPaul B Schroeder  *****************************************************************************/
23073f542974SPaul B Schroeder 
230800a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
23093f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
23103f542974SPaul B Schroeder {
231195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
231297c4965dSAl Viro 	void __user *argp = (void __user *)arg;
23133f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
23143f542974SPaul B Schroeder 
23153f542974SPaul B Schroeder 	struct async_icount cnow;
23163f542974SPaul B Schroeder 	struct async_icount cprev;
23173f542974SPaul B Schroeder 
2318441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
231984fe6e79STony Cook 		dbg("%s", "Invalid port");
23203f542974SPaul B Schroeder 		return -1;
23213f542974SPaul B Schroeder 	}
23223f542974SPaul B Schroeder 
23233f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
23243f542974SPaul B Schroeder 
23253f542974SPaul B Schroeder 	if (mos7840_port == NULL)
23263f542974SPaul B Schroeder 		return -1;
23273f542974SPaul B Schroeder 
2328441b62c1SHarvey Harrison 	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
23293f542974SPaul B Schroeder 
23303f542974SPaul B Schroeder 	switch (cmd) {
23313f542974SPaul B Schroeder 		/* return number of bytes available */
23323f542974SPaul B Schroeder 
23333f542974SPaul B Schroeder 	case TIOCSERGETLSR:
2334441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
233595da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
23363f542974SPaul B Schroeder 
23373f542974SPaul B Schroeder 	case TIOCGSERIAL:
2338441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
233997c4965dSAl Viro 		return mos7840_get_serial_info(mos7840_port, argp);
23403f542974SPaul B Schroeder 
23413f542974SPaul B Schroeder 	case TIOCSSERIAL:
2342441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
23433f542974SPaul B Schroeder 		break;
23443f542974SPaul B Schroeder 
23453f542974SPaul B Schroeder 	case TIOCMIWAIT:
2346441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
23473f542974SPaul B Schroeder 		cprev = mos7840_port->icount;
23483f542974SPaul B Schroeder 		while (1) {
2349880af9dbSAlan Cox 			/* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
23503f542974SPaul B Schroeder 			mos7840_port->delta_msr_cond = 0;
23513f542974SPaul B Schroeder 			wait_event_interruptible(mos7840_port->delta_msr_wait,
23523f542974SPaul B Schroeder 						 (mos7840_port->
23533f542974SPaul B Schroeder 						  delta_msr_cond == 1));
23543f542974SPaul B Schroeder 
23553f542974SPaul B Schroeder 			/* see if a signal did it */
23563f542974SPaul B Schroeder 			if (signal_pending(current))
23573f542974SPaul B Schroeder 				return -ERESTARTSYS;
23583f542974SPaul B Schroeder 			cnow = mos7840_port->icount;
23590de9a702SOliver Neukum 			smp_rmb();
23603f542974SPaul B Schroeder 			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
23613f542974SPaul B Schroeder 			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
23623f542974SPaul B Schroeder 				return -EIO;	/* no change => error */
23633f542974SPaul B Schroeder 			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
23643f542974SPaul B Schroeder 			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
23653f542974SPaul B Schroeder 			    ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
23663f542974SPaul B Schroeder 			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
23673f542974SPaul B Schroeder 				return 0;
23683f542974SPaul B Schroeder 			}
23693f542974SPaul B Schroeder 			cprev = cnow;
23703f542974SPaul B Schroeder 		}
23713f542974SPaul B Schroeder 		/* NOTREACHED */
23723f542974SPaul B Schroeder 		break;
23733f542974SPaul B Schroeder 
23743f542974SPaul B Schroeder 	default:
23753f542974SPaul B Schroeder 		break;
23763f542974SPaul B Schroeder 	}
23773f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
23783f542974SPaul B Schroeder }
23793f542974SPaul B Schroeder 
23800eafe4deSDonald static int mos7810_check(struct usb_serial *serial)
23813f542974SPaul B Schroeder {
23820eafe4deSDonald 	int i, pass_count = 0;
23830eafe4deSDonald 	__u16 data = 0, mcr_data = 0;
23840eafe4deSDonald 	__u16 test_pattern = 0x55AA;
23853f542974SPaul B Schroeder 
23860eafe4deSDonald 	/* Store MCR setting */
23870eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
23880eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
23890eafe4deSDonald 		&mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
23900eafe4deSDonald 
23910eafe4deSDonald 	for (i = 0; i < 16; i++) {
23920eafe4deSDonald 		/* Send the 1-bit test pattern out to MCS7810 test pin */
23930eafe4deSDonald 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
23940eafe4deSDonald 			MCS_WRREQ, MCS_WR_RTYPE,
23950eafe4deSDonald 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
23960eafe4deSDonald 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
23970eafe4deSDonald 
23980eafe4deSDonald 		/* Read the test pattern back */
23990eafe4deSDonald 		usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
24000eafe4deSDonald 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2401093ea2d3SDonald Lee 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2402093ea2d3SDonald Lee 
24030eafe4deSDonald 		/* If this is a MCS7810 device, both test patterns must match */
24040eafe4deSDonald 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
24050eafe4deSDonald 			break;
24060eafe4deSDonald 
24070eafe4deSDonald 		pass_count++;
24083f542974SPaul B Schroeder 	}
2409093ea2d3SDonald Lee 
24100eafe4deSDonald 	/* Restore MCR setting */
24110eafe4deSDonald 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
24120eafe4deSDonald 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
24130eafe4deSDonald 		0, MOS_WDR_TIMEOUT);
24140eafe4deSDonald 
24150eafe4deSDonald 	if (pass_count == 16)
24160eafe4deSDonald 		return 1;
24170eafe4deSDonald 
24180eafe4deSDonald 	return 0;
24190eafe4deSDonald }
24200eafe4deSDonald 
24210eafe4deSDonald static int mos7840_calc_num_ports(struct usb_serial *serial)
24220eafe4deSDonald {
24230eafe4deSDonald 	__u16 data = 0x00;
24240eafe4deSDonald 	int mos7840_num_ports;
24250eafe4deSDonald 
24260eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
24270eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
24280eafe4deSDonald 		VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
24290eafe4deSDonald 
24300eafe4deSDonald 	if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
24310eafe4deSDonald 		serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
24320eafe4deSDonald 		device_type = serial->dev->descriptor.idProduct;
24330eafe4deSDonald 	} else {
24340eafe4deSDonald 		/* For a MCS7840 device GPIO0 must be set to 1 */
24350eafe4deSDonald 		if ((data & 0x01) == 1)
24360eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7840;
24370eafe4deSDonald 		else if (mos7810_check(serial))
24380eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7810;
24390eafe4deSDonald 		else
24400eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7820;
24410eafe4deSDonald 	}
24420eafe4deSDonald 
24430eafe4deSDonald 	mos7840_num_ports = (device_type >> 4) & 0x000F;
24440eafe4deSDonald 	serial->num_bulk_in = mos7840_num_ports;
24450eafe4deSDonald 	serial->num_bulk_out = mos7840_num_ports;
24460eafe4deSDonald 	serial->num_ports = mos7840_num_ports;
24470eafe4deSDonald 
24483f542974SPaul B Schroeder 	return mos7840_num_ports;
24493f542974SPaul B Schroeder }
24503f542974SPaul B Schroeder 
24513f542974SPaul B Schroeder /****************************************************************************
24523f542974SPaul B Schroeder  * mos7840_startup
24533f542974SPaul B Schroeder  ****************************************************************************/
24543f542974SPaul B Schroeder 
24553f542974SPaul B Schroeder static int mos7840_startup(struct usb_serial *serial)
24563f542974SPaul B Schroeder {
24573f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
24583f542974SPaul B Schroeder 	struct usb_device *dev;
24593f542974SPaul B Schroeder 	int i, status;
24603f542974SPaul B Schroeder 	__u16 Data;
24613f542974SPaul B Schroeder 
24623f542974SPaul B Schroeder 	if (!serial) {
246384fe6e79STony Cook 		dbg("%s", "Invalid Handler");
24643f542974SPaul B Schroeder 		return -1;
24653f542974SPaul B Schroeder 	}
24663f542974SPaul B Schroeder 
24673f542974SPaul B Schroeder 	dev = serial->dev;
24683f542974SPaul B Schroeder 
24693f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
24703f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
24713f542974SPaul B Schroeder 
24723f542974SPaul B Schroeder 	/* set up port private structures */
24733f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
247484fe6e79STony Cook 		dbg ("mos7840_startup: configuring port %d............", i);
24757ac9da10SBurman Yan 		mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
24763f542974SPaul B Schroeder 		if (mos7840_port == NULL) {
2477194343d9SGreg Kroah-Hartman 			dev_err(&dev->dev, "%s - Out of memory\n", __func__);
24780de9a702SOliver Neukum 			status = -ENOMEM;
24790de9a702SOliver Neukum 			i--; /* don't follow NULL pointer cleaning up */
24800de9a702SOliver Neukum 			goto error;
24813f542974SPaul B Schroeder 		}
24823f542974SPaul B Schroeder 
2483880af9dbSAlan Cox 		/* Initialize all port interrupt end point to port 0 int
2484880af9dbSAlan Cox 		 * endpoint. Our device has only one interrupt end point
2485880af9dbSAlan Cox 		 * common to all port */
24863f542974SPaul B Schroeder 
24873f542974SPaul B Schroeder 		mos7840_port->port = serial->port[i];
24883f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
24890de9a702SOliver Neukum 		spin_lock_init(&mos7840_port->pool_lock);
24903f542974SPaul B Schroeder 
249137768adfSTony Cook 		/* minor is not initialised until later by
249237768adfSTony Cook 		 * usb-serial.c:get_free_serial() and cannot therefore be used
249337768adfSTony Cook 		 * to index device instances */
249437768adfSTony Cook 		mos7840_port->port_num = i + 1;
249537768adfSTony Cook 		dbg ("serial->port[i]->number = %d", serial->port[i]->number);
249637768adfSTony Cook 		dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
249737768adfSTony Cook 		dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
249837768adfSTony Cook 		dbg ("serial->minor = %d", serial->minor);
24993f542974SPaul B Schroeder 
25003f542974SPaul B Schroeder 		if (mos7840_port->port_num == 1) {
25013f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x0;
25023f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x1;
25033f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x4;
25043f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
25050de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
25063f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x8;
25073f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x9;
25083f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x16;
25093f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
25100de9a702SOliver Neukum 			   && (serial->num_ports == 2)) {
25113f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
25123f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
25133f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
25143f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 3)
25150de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
25163f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
25173f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
25183f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
25193f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 4)
25200de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
25213f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xc;
25223f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xd;
25233f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x1c;
25243f542974SPaul B Schroeder 		}
25253f542974SPaul B Schroeder 		mos7840_dump_serial_port(mos7840_port);
25263f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
25273f542974SPaul B Schroeder 
2528880af9dbSAlan Cox 		/* enable rx_disable bit in control register */
2529880af9dbSAlan Cox 		status = mos7840_get_reg_sync(serial->port[i],
25303f542974SPaul B Schroeder 				 mos7840_port->ControlRegOffset, &Data);
25313f542974SPaul B Schroeder 		if (status < 0) {
253284fe6e79STony Cook 			dbg("Reading ControlReg failed status-0x%x", status);
25333f542974SPaul B Schroeder 			break;
25343f542974SPaul B Schroeder 		} else
253584fe6e79STony Cook 			dbg("ControlReg Reading success val is %x, status%d",
25363f542974SPaul B Schroeder 			    Data, status);
2537880af9dbSAlan Cox 		Data |= 0x08;	/* setting driver done bit */
2538880af9dbSAlan Cox 		Data |= 0x04;	/* sp1_bit to have cts change reflect in
2539880af9dbSAlan Cox 				   modem status reg */
25403f542974SPaul B Schroeder 
2541880af9dbSAlan Cox 		/* Data |= 0x20; //rx_disable bit */
2542880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
25433f542974SPaul B Schroeder 					 mos7840_port->ControlRegOffset, Data);
25443f542974SPaul B Schroeder 		if (status < 0) {
254584fe6e79STony Cook 			dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
25463f542974SPaul B Schroeder 			break;
25473f542974SPaul B Schroeder 		} else
254884fe6e79STony Cook 			dbg("ControlReg Writing success(rx_disable) status%d",
25493f542974SPaul B Schroeder 			    status);
25503f542974SPaul B Schroeder 
2551880af9dbSAlan Cox 		/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2552880af9dbSAlan Cox 		   and 0x24 in DCR3 */
25533f542974SPaul B Schroeder 		Data = 0x01;
2554880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2555880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
25563f542974SPaul B Schroeder 		if (status < 0) {
255784fe6e79STony Cook 			dbg("Writing DCR0 failed status-0x%x", status);
25583f542974SPaul B Schroeder 			break;
25593f542974SPaul B Schroeder 		} else
256084fe6e79STony Cook 			dbg("DCR0 Writing success status%d", status);
25613f542974SPaul B Schroeder 
25623f542974SPaul B Schroeder 		Data = 0x05;
2563880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2564880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
25653f542974SPaul B Schroeder 		if (status < 0) {
256684fe6e79STony Cook 			dbg("Writing DCR1 failed status-0x%x", status);
25673f542974SPaul B Schroeder 			break;
25683f542974SPaul B Schroeder 		} else
256984fe6e79STony Cook 			dbg("DCR1 Writing success status%d", status);
25703f542974SPaul B Schroeder 
25713f542974SPaul B Schroeder 		Data = 0x24;
2572880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2573880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
25743f542974SPaul B Schroeder 		if (status < 0) {
257584fe6e79STony Cook 			dbg("Writing DCR2 failed status-0x%x", status);
25763f542974SPaul B Schroeder 			break;
25773f542974SPaul B Schroeder 		} else
257884fe6e79STony Cook 			dbg("DCR2 Writing success status%d", status);
25793f542974SPaul B Schroeder 
2580880af9dbSAlan Cox 		/* write values in clkstart0x0 and clkmulti 0x20 */
25813f542974SPaul B Schroeder 		Data = 0x0;
2582880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
25833f542974SPaul B Schroeder 					 CLK_START_VALUE_REGISTER, Data);
25843f542974SPaul B Schroeder 		if (status < 0) {
258584fe6e79STony Cook 			dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
25863f542974SPaul B Schroeder 			break;
25873f542974SPaul B Schroeder 		} else
258884fe6e79STony Cook 			dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
25893f542974SPaul B Schroeder 
25903f542974SPaul B Schroeder 		Data = 0x20;
2591880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2592880af9dbSAlan Cox 					CLK_MULTI_REGISTER, Data);
25933f542974SPaul B Schroeder 		if (status < 0) {
259484fe6e79STony Cook 			dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
25953f542974SPaul B Schroeder 			    status);
25960de9a702SOliver Neukum 			goto error;
25973f542974SPaul B Schroeder 		} else
259884fe6e79STony Cook 			dbg("CLK_MULTI_REGISTER Writing success status%d",
25993f542974SPaul B Schroeder 			    status);
26003f542974SPaul B Schroeder 
2601880af9dbSAlan Cox 		/* write value 0x0 to scratchpad register */
26023f542974SPaul B Schroeder 		Data = 0x00;
2603880af9dbSAlan Cox 		status = mos7840_set_uart_reg(serial->port[i],
2604880af9dbSAlan Cox 						SCRATCH_PAD_REGISTER, Data);
26053f542974SPaul B Schroeder 		if (status < 0) {
260684fe6e79STony Cook 			dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
26073f542974SPaul B Schroeder 			    status);
26083f542974SPaul B Schroeder 			break;
26093f542974SPaul B Schroeder 		} else
261084fe6e79STony Cook 			dbg("SCRATCH_PAD_REGISTER Writing success status%d",
26113f542974SPaul B Schroeder 			    status);
26123f542974SPaul B Schroeder 
2613880af9dbSAlan Cox 		/* Zero Length flag register */
26143f542974SPaul B Schroeder 		if ((mos7840_port->port_num != 1)
26150de9a702SOliver Neukum 		    && (serial->num_ports == 2)) {
26163f542974SPaul B Schroeder 
26173f542974SPaul B Schroeder 			Data = 0xff;
26183f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
26193f542974SPaul B Schroeder 				      (__u16) (ZLP_REG1 +
2620880af9dbSAlan Cox 				      ((__u16)mos7840_port->port_num)), Data);
262184fe6e79STony Cook 			dbg("ZLIP offset %x",
26223f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
26233f542974SPaul B Schroeder 					((__u16) mos7840_port->port_num)));
26243f542974SPaul B Schroeder 			if (status < 0) {
262584fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
26263f542974SPaul B Schroeder 				    i + 2, status);
26273f542974SPaul B Schroeder 				break;
26283f542974SPaul B Schroeder 			} else
262984fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
26303f542974SPaul B Schroeder 				    i + 2, status);
26313f542974SPaul B Schroeder 		} else {
26323f542974SPaul B Schroeder 			Data = 0xff;
26333f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
26343f542974SPaul B Schroeder 			      (__u16) (ZLP_REG1 +
2635880af9dbSAlan Cox 			      ((__u16)mos7840_port->port_num) - 0x1), Data);
263684fe6e79STony Cook 			dbg("ZLIP offset %x",
26373f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
26383f542974SPaul B Schroeder 				     ((__u16) mos7840_port->port_num) - 0x1));
26393f542974SPaul B Schroeder 			if (status < 0) {
264084fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
26413f542974SPaul B Schroeder 				    i + 1, status);
26423f542974SPaul B Schroeder 				break;
26433f542974SPaul B Schroeder 			} else
264484fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
26453f542974SPaul B Schroeder 				    i + 1, status);
26463f542974SPaul B Schroeder 
26473f542974SPaul B Schroeder 		}
26480de9a702SOliver Neukum 		mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
26493f542974SPaul B Schroeder 		mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2650880af9dbSAlan Cox 		mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2651880af9dbSAlan Cox 								GFP_KERNEL);
2652880af9dbSAlan Cox 		if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2653880af9dbSAlan Cox 							!mos7840_port->dr) {
26540de9a702SOliver Neukum 			status = -ENOMEM;
26550de9a702SOliver Neukum 			goto error;
26560de9a702SOliver Neukum 		}
26570eafe4deSDonald 
26580eafe4deSDonald 		mos7840_port->has_led = false;
26590eafe4deSDonald 
26600eafe4deSDonald 		/* Initialize LED timers */
26610eafe4deSDonald 		if (device_type == MOSCHIP_DEVICE_ID_7810) {
26620eafe4deSDonald 			mos7840_port->has_led = true;
26630eafe4deSDonald 
26640eafe4deSDonald 			init_timer(&mos7840_port->led_timer1);
26650eafe4deSDonald 			mos7840_port->led_timer1.function = mos7840_led_off;
26660eafe4deSDonald 			mos7840_port->led_timer1.expires =
26670eafe4deSDonald 					jiffies + msecs_to_jiffies(LED_ON_MS);
26680eafe4deSDonald 			mos7840_port->led_timer1.data =
26690eafe4deSDonald 						(unsigned long)mos7840_port;
26700eafe4deSDonald 
26710eafe4deSDonald 			init_timer(&mos7840_port->led_timer2);
26720eafe4deSDonald 			mos7840_port->led_timer2.function =
26730eafe4deSDonald 						mos7840_led_flag_off;
26740eafe4deSDonald 			mos7840_port->led_timer2.expires =
26750eafe4deSDonald 					jiffies + msecs_to_jiffies(LED_OFF_MS);
26760eafe4deSDonald 			mos7840_port->led_timer2.data =
26770eafe4deSDonald 						(unsigned long)mos7840_port;
26780eafe4deSDonald 
26790eafe4deSDonald 			mos7840_port->led_flag = false;
26800eafe4deSDonald 
26810eafe4deSDonald 			/* Turn off LED */
26820eafe4deSDonald 			mos7840_set_led_sync(serial->port[i],
26830eafe4deSDonald 						MODEM_CONTROL_REGISTER, 0x0300);
26840eafe4deSDonald 		}
26853f542974SPaul B Schroeder 	}
268684fe6e79STony Cook 	dbg ("mos7840_startup: all ports configured...........");
26873f542974SPaul B Schroeder 
2688880af9dbSAlan Cox 	/* Zero Length flag enable */
26893f542974SPaul B Schroeder 	Data = 0x0f;
26903f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
26913f542974SPaul B Schroeder 	if (status < 0) {
269284fe6e79STony Cook 		dbg("Writing ZLP_REG5 failed status-0x%x", status);
26937ced46c3SRoel Kluin 		goto error;
26943f542974SPaul B Schroeder 	} else
269584fe6e79STony Cook 		dbg("ZLP_REG5 Writing success status%d", status);
26963f542974SPaul B Schroeder 
26973f542974SPaul B Schroeder 	/* setting configuration feature to one */
26983f542974SPaul B Schroeder 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
269997c4965dSAl Viro 			(__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
27003f542974SPaul B Schroeder 	return 0;
27010de9a702SOliver Neukum error:
27020de9a702SOliver Neukum 	for (/* nothing */; i >= 0; i--) {
27030de9a702SOliver Neukum 		mos7840_port = mos7840_get_port_private(serial->port[i]);
27040de9a702SOliver Neukum 
27050de9a702SOliver Neukum 		kfree(mos7840_port->dr);
27060de9a702SOliver Neukum 		kfree(mos7840_port->ctrl_buf);
27070de9a702SOliver Neukum 		usb_free_urb(mos7840_port->control_urb);
27080de9a702SOliver Neukum 		kfree(mos7840_port);
27090de9a702SOliver Neukum 		serial->port[i] = NULL;
27100de9a702SOliver Neukum 	}
27110de9a702SOliver Neukum 	return status;
27123f542974SPaul B Schroeder }
27133f542974SPaul B Schroeder 
27143f542974SPaul B Schroeder /****************************************************************************
2715f9c99bb8SAlan Stern  * mos7840_disconnect
27163f542974SPaul B Schroeder  *	This function is called whenever the device is removed from the usb bus.
27173f542974SPaul B Schroeder  ****************************************************************************/
27183f542974SPaul B Schroeder 
2719f9c99bb8SAlan Stern static void mos7840_disconnect(struct usb_serial *serial)
27203f542974SPaul B Schroeder {
27213f542974SPaul B Schroeder 	int i;
27220de9a702SOliver Neukum 	unsigned long flags;
27233f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
27243f542974SPaul B Schroeder 
27253f542974SPaul B Schroeder 	if (!serial) {
272684fe6e79STony Cook 		dbg("%s", "Invalid Handler");
27273f542974SPaul B Schroeder 		return;
27283f542974SPaul B Schroeder 	}
27293f542974SPaul B Schroeder 
27303f542974SPaul B Schroeder 	/* check for the ports to be closed,close the ports and disconnect */
27313f542974SPaul B Schroeder 
27323f542974SPaul B Schroeder 	/* free private structure allocated for serial port  *
27333f542974SPaul B Schroeder 	 * stop reads and writes on all ports                */
27343f542974SPaul B Schroeder 
27353f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
27363f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
273737768adfSTony Cook 		dbg ("mos7840_port %d = %p", i, mos7840_port);
273837768adfSTony Cook 		if (mos7840_port) {
27390de9a702SOliver Neukum 			spin_lock_irqsave(&mos7840_port->pool_lock, flags);
27400de9a702SOliver Neukum 			mos7840_port->zombie = 1;
27410de9a702SOliver Neukum 			spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
27423f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->control_urb);
2743f9c99bb8SAlan Stern 		}
2744f9c99bb8SAlan Stern 	}
2745f9c99bb8SAlan Stern }
2746f9c99bb8SAlan Stern 
2747f9c99bb8SAlan Stern /****************************************************************************
2748f9c99bb8SAlan Stern  * mos7840_release
2749f9c99bb8SAlan Stern  *	This function is called when the usb_serial structure is freed.
2750f9c99bb8SAlan Stern  ****************************************************************************/
2751f9c99bb8SAlan Stern 
2752f9c99bb8SAlan Stern static void mos7840_release(struct usb_serial *serial)
2753f9c99bb8SAlan Stern {
2754f9c99bb8SAlan Stern 	int i;
2755f9c99bb8SAlan Stern 	struct moschip_port *mos7840_port;
2756f9c99bb8SAlan Stern 
2757f9c99bb8SAlan Stern 	if (!serial) {
2758f9c99bb8SAlan Stern 		dbg("%s", "Invalid Handler");
2759f9c99bb8SAlan Stern 		return;
2760f9c99bb8SAlan Stern 	}
2761f9c99bb8SAlan Stern 
2762f9c99bb8SAlan Stern 	/* check for the ports to be closed,close the ports and disconnect */
2763f9c99bb8SAlan Stern 
2764f9c99bb8SAlan Stern 	/* free private structure allocated for serial port  *
2765f9c99bb8SAlan Stern 	 * stop reads and writes on all ports                */
2766f9c99bb8SAlan Stern 
2767f9c99bb8SAlan Stern 	for (i = 0; i < serial->num_ports; ++i) {
2768f9c99bb8SAlan Stern 		mos7840_port = mos7840_get_port_private(serial->port[i]);
2769f9c99bb8SAlan Stern 		dbg("mos7840_port %d = %p", i, mos7840_port);
2770f9c99bb8SAlan Stern 		if (mos7840_port) {
27710eafe4deSDonald 			if (mos7840_port->has_led) {
27720eafe4deSDonald 				/* Turn off LED */
27730eafe4deSDonald 				mos7840_set_led_sync(mos7840_port->port,
27740eafe4deSDonald 						MODEM_CONTROL_REGISTER, 0x0300);
27750eafe4deSDonald 
27760eafe4deSDonald 				del_timer_sync(&mos7840_port->led_timer1);
27770eafe4deSDonald 				del_timer_sync(&mos7840_port->led_timer2);
27780eafe4deSDonald 			}
27790de9a702SOliver Neukum 			kfree(mos7840_port->ctrl_buf);
27800de9a702SOliver Neukum 			kfree(mos7840_port->dr);
27813f542974SPaul B Schroeder 			kfree(mos7840_port);
278237768adfSTony Cook 		}
27833f542974SPaul B Schroeder 	}
27843f542974SPaul B Schroeder }
27853f542974SPaul B Schroeder 
2786d9b1b787SJohannes Hölzl static struct usb_driver io_driver = {
2787d9b1b787SJohannes Hölzl 	.name = "mos7840",
2788d9b1b787SJohannes Hölzl 	.probe = usb_serial_probe,
2789d9b1b787SJohannes Hölzl 	.disconnect = usb_serial_disconnect,
2790d9b1b787SJohannes Hölzl 	.id_table = moschip_id_table_combined,
2791d9b1b787SJohannes Hölzl };
2792d9b1b787SJohannes Hölzl 
27933f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
27943f542974SPaul B Schroeder 	.driver = {
27953f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
27963f542974SPaul B Schroeder 		   .name = "mos7840",
27973f542974SPaul B Schroeder 		   },
27983f542974SPaul B Schroeder 	.description = DRIVER_DESC,
27993f542974SPaul B Schroeder 	.id_table = moschip_port_id_table,
28003f542974SPaul B Schroeder 	.num_ports = 4,
28013f542974SPaul B Schroeder 	.open = mos7840_open,
28023f542974SPaul B Schroeder 	.close = mos7840_close,
28033f542974SPaul B Schroeder 	.write = mos7840_write,
28043f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
28053f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
28063f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
28073f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
28083f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
28093f542974SPaul B Schroeder #ifdef MCSSerialProbe
28103f542974SPaul B Schroeder 	.probe = mos7840_serial_probe,
28113f542974SPaul B Schroeder #endif
28123f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
28133f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
28143f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
28153f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
28163f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
28170bca1b91SAlan Cox 	.get_icount = mos7840_get_icount,
28183f542974SPaul B Schroeder 	.attach = mos7840_startup,
2819f9c99bb8SAlan Stern 	.disconnect = mos7840_disconnect,
2820f9c99bb8SAlan Stern 	.release = mos7840_release,
28213f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
28223f542974SPaul B Schroeder 	.read_int_callback = mos7840_interrupt_callback,
28233f542974SPaul B Schroeder };
28243f542974SPaul B Schroeder 
28254d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
28264d2a7affSAlan Stern 	&moschip7840_4port_device, NULL
28274d2a7affSAlan Stern };
28284d2a7affSAlan Stern 
2829e7414d9aSGreg Kroah-Hartman module_usb_serial_driver(io_driver, serial_drivers);
28303f542974SPaul B Schroeder 
28313f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
28323f542974SPaul B Schroeder MODULE_LICENSE("GPL");
28333f542974SPaul B Schroeder 
28343f542974SPaul B Schroeder module_param(debug, bool, S_IRUGO | S_IWUSR);
28353f542974SPaul B Schroeder MODULE_PARM_DESC(debug, "Debug enabled or not");
2836