xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision 093ea2d3a766cb8a4c4de57efec6c0a127a58792)
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
11711e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden
11811e1abb4SDavid Ludlow  * in vendor-specific implementations.  Such devices can be handled
11911e1abb4SDavid Ludlow  * by making a change here, in moschip_port_id_table, and in
12011e1abb4SDavid Ludlow  * moschip_id_table_combined
12111e1abb4SDavid Ludlow  */
12211e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB              0x0856
123acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
124870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
125acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
126870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
127acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
128acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
129acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
130acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
13111e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
132caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
133870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
134870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
135870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL2_4        0xAC24
1363f542974SPaul B Schroeder 
1379d498beaSRussell Lang /* This driver also supports
1389d498beaSRussell Lang  * ATEN UC2324 device using Moschip MCS7840
1399d498beaSRussell Lang  * ATEN UC2322 device using Moschip MCS7820
1409d498beaSRussell Lang  */
141e9b8cffaSTony Cook #define USB_VENDOR_ID_ATENINTL		0x0557
142e9b8cffaSTony Cook #define ATENINTL_DEVICE_ID_UC2324	0x2011
1439d498beaSRussell Lang #define ATENINTL_DEVICE_ID_UC2322	0x7820
144e9b8cffaSTony Cook 
14511e1abb4SDavid Ludlow /* Interrupt Routine Defines    */
1463f542974SPaul B Schroeder 
1473f542974SPaul B Schroeder #define SERIAL_IIR_RLS      0x06
1483f542974SPaul B Schroeder #define SERIAL_IIR_MS       0x00
1493f542974SPaul B Schroeder 
1503f542974SPaul B Schroeder /*
1513f542974SPaul B Schroeder  *  Emulation of the bit mask on the LINE STATUS REGISTER.
1523f542974SPaul B Schroeder  */
1533f542974SPaul B Schroeder #define SERIAL_LSR_DR       0x0001
1543f542974SPaul B Schroeder #define SERIAL_LSR_OE       0x0002
1553f542974SPaul B Schroeder #define SERIAL_LSR_PE       0x0004
1563f542974SPaul B Schroeder #define SERIAL_LSR_FE       0x0008
1573f542974SPaul B Schroeder #define SERIAL_LSR_BI       0x0010
1583f542974SPaul B Schroeder 
1593f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS   0x10
1603f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR   0x20
1613f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI    0x40
1623f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD    0x80
1633f542974SPaul B Schroeder 
164880af9dbSAlan Cox /* Serial Port register Address */
1653f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
1663f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
1673f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
1683f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
1693f542974SPaul B Schroeder #define LINE_STATUS_REGISTER       ((__u16)(0x05))
1703f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
1713f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
1723f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
1733f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
1743f542974SPaul B Schroeder 
1753f542974SPaul B Schroeder #define CLK_MULTI_REGISTER         ((__u16)(0x02))
1763f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
177*093ea2d3SDonald Lee #define GPIO_REGISTER              ((__u16)(0x07))
1783f542974SPaul B Schroeder 
1793f542974SPaul B Schroeder #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
1803f542974SPaul B Schroeder 
1813f542974SPaul B Schroeder /*
1823f542974SPaul B Schroeder  * URB POOL related defines
1833f542974SPaul B Schroeder  */
1843f542974SPaul B Schroeder #define NUM_URBS                        16	/* URB Count */
1853f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
1863f542974SPaul B Schroeder 
1873f542974SPaul B Schroeder 
1887d40d7e8SNémeth Márton static const struct usb_device_id moschip_port_id_table[] = {
1893f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
1903f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
191acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
192870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
193acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
194870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
195acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
196acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
197acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
198acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
19911e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
200870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
201acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
202870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
20327f1281dSBlaise Gassend 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
204e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
2059d498beaSRussell Lang 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
2063f542974SPaul B Schroeder 	{}			/* terminating entry */
2073f542974SPaul B Schroeder };
2083f542974SPaul B Schroeder 
2097d40d7e8SNémeth Márton static const struct usb_device_id moschip_id_table_combined[] __devinitconst = {
2103f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
2113f542974SPaul B Schroeder 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
212acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
213870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
214acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
215870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
216acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
217acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
218acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
219acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
22011e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
221870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
222acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
223870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
22427f1281dSBlaise Gassend 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
225e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
2269d498beaSRussell Lang 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
2273f542974SPaul B Schroeder 	{}			/* terminating entry */
2283f542974SPaul B Schroeder };
2293f542974SPaul B Schroeder 
2303f542974SPaul B Schroeder MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
2313f542974SPaul B Schroeder 
2323f542974SPaul B Schroeder /* This structure holds all of the local port information */
2333f542974SPaul B Schroeder 
2343f542974SPaul B Schroeder struct moschip_port {
2353f542974SPaul B Schroeder 	int port_num;		/*Actual port number in the device(1,2,etc) */
2363f542974SPaul B Schroeder 	struct urb *write_urb;	/* write URB for this port */
2373f542974SPaul B Schroeder 	struct urb *read_urb;	/* read URB for this port */
2380de9a702SOliver Neukum 	struct urb *int_urb;
2393f542974SPaul B Schroeder 	__u8 shadowLCR;		/* last LCR value received */
2403f542974SPaul B Schroeder 	__u8 shadowMCR;		/* last MCR value received */
2413f542974SPaul B Schroeder 	char open;
2420de9a702SOliver Neukum 	char open_ports;
2430de9a702SOliver Neukum 	char zombie;
2443f542974SPaul B Schroeder 	wait_queue_head_t wait_chase;	/* for handling sleeping while waiting for chase to finish */
2453f542974SPaul B Schroeder 	wait_queue_head_t delta_msr_wait;	/* for handling sleeping while waiting for msr change to happen */
2463f542974SPaul B Schroeder 	int delta_msr_cond;
2473f542974SPaul B Schroeder 	struct async_icount icount;
2483f542974SPaul B Schroeder 	struct usb_serial_port *port;	/* loop back to the owner of this object */
2493f542974SPaul B Schroeder 
2503f542974SPaul B Schroeder 	/* Offsets */
2513f542974SPaul B Schroeder 	__u8 SpRegOffset;
2523f542974SPaul B Schroeder 	__u8 ControlRegOffset;
2533f542974SPaul B Schroeder 	__u8 DcrRegOffset;
254880af9dbSAlan Cox 	/* for processing control URBS in interrupt context */
2553f542974SPaul B Schroeder 	struct urb *control_urb;
2560de9a702SOliver Neukum 	struct usb_ctrlrequest *dr;
2573f542974SPaul B Schroeder 	char *ctrl_buf;
2583f542974SPaul B Schroeder 	int MsrLsr;
2593f542974SPaul B Schroeder 
2600de9a702SOliver Neukum 	spinlock_t pool_lock;
2613f542974SPaul B Schroeder 	struct urb *write_urb_pool[NUM_URBS];
2620de9a702SOliver Neukum 	char busy[NUM_URBS];
26350de36f7SGreg Kroah-Hartman 	bool read_urb_busy;
2643f542974SPaul B Schroeder };
2653f542974SPaul B Schroeder 
2663f542974SPaul B Schroeder 
26790ab5ee9SRusty Russell static bool debug;
2683f542974SPaul B Schroeder 
2693f542974SPaul B Schroeder /*
2703f542974SPaul B Schroeder  * mos7840_set_reg_sync
2713f542974SPaul B Schroeder  * 	To set the Control register by calling usb_fill_control_urb function
2723f542974SPaul B Schroeder  *	by passing usb_sndctrlpipe function as parameter.
2733f542974SPaul B Schroeder  */
2743f542974SPaul B Schroeder 
2753f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2763f542974SPaul B Schroeder 				__u16 val)
2773f542974SPaul B Schroeder {
2783f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2793f542974SPaul B Schroeder 	val = val & 0x00ff;
28084fe6e79STony Cook 	dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
2813f542974SPaul B Schroeder 
2823f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2833f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2843f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2853f542974SPaul B Schroeder }
2863f542974SPaul B Schroeder 
2873f542974SPaul B Schroeder /*
2883f542974SPaul B Schroeder  * mos7840_get_reg_sync
2893f542974SPaul B Schroeder  * 	To set the Uart register by calling usb_fill_control_urb function by
2903f542974SPaul B Schroeder  *	passing usb_rcvctrlpipe function as parameter.
2913f542974SPaul B Schroeder  */
2923f542974SPaul B Schroeder 
2933f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
2943f542974SPaul B Schroeder 				__u16 *val)
2953f542974SPaul B Schroeder {
2963f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2973f542974SPaul B Schroeder 	int ret = 0;
2989e221a35SJohan Hovold 	u8 *buf;
2999e221a35SJohan Hovold 
3009e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3019e221a35SJohan Hovold 	if (!buf)
3029e221a35SJohan Hovold 		return -ENOMEM;
3033f542974SPaul B Schroeder 
3043f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3059e221a35SJohan Hovold 			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
3063f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
3079e221a35SJohan Hovold 	*val = buf[0];
30884fe6e79STony Cook 	dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
3099e221a35SJohan Hovold 
3109e221a35SJohan Hovold 	kfree(buf);
3113f542974SPaul B Schroeder 	return ret;
3123f542974SPaul B Schroeder }
3133f542974SPaul B Schroeder 
3143f542974SPaul B Schroeder /*
3153f542974SPaul B Schroeder  * mos7840_set_uart_reg
3163f542974SPaul B Schroeder  *	To set the Uart register by calling usb_fill_control_urb function by
3173f542974SPaul B Schroeder  *	passing usb_sndctrlpipe function as parameter.
3183f542974SPaul B Schroeder  */
3193f542974SPaul B Schroeder 
3203f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
3213f542974SPaul B Schroeder 				__u16 val)
3223f542974SPaul B Schroeder {
3233f542974SPaul B Schroeder 
3243f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3253f542974SPaul B Schroeder 	val = val & 0x00ff;
326880af9dbSAlan Cox 	/* For the UART control registers, the application number need
327880af9dbSAlan Cox 	   to be Or'ed */
3280de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
329880af9dbSAlan Cox 		val |= (((__u16) port->number -
330880af9dbSAlan Cox 				(__u16) (port->serial->minor)) + 1) << 8;
33184fe6e79STony Cook 		dbg("mos7840_set_uart_reg application number is %x", val);
3323f542974SPaul B Schroeder 	} else {
3333f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
334880af9dbSAlan Cox 			val |= (((__u16) port->number -
3353f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
33684fe6e79STony Cook 			dbg("mos7840_set_uart_reg application number is %x",
3373f542974SPaul B Schroeder 			    val);
3383f542974SPaul B Schroeder 		} else {
3393f542974SPaul B Schroeder 			val |=
3403f542974SPaul B Schroeder 			    (((__u16) port->number -
3413f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
34284fe6e79STony Cook 			dbg("mos7840_set_uart_reg application number is %x",
3433f542974SPaul B Schroeder 			    val);
3443f542974SPaul B Schroeder 		}
3453f542974SPaul B Schroeder 	}
3463f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
3473f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
3483f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
3493f542974SPaul B Schroeder 
3503f542974SPaul B Schroeder }
3513f542974SPaul B Schroeder 
3523f542974SPaul B Schroeder /*
3533f542974SPaul B Schroeder  * mos7840_get_uart_reg
3543f542974SPaul B Schroeder  *	To set the Control register by calling usb_fill_control_urb function
3553f542974SPaul B Schroeder  *	by passing usb_rcvctrlpipe function as parameter.
3563f542974SPaul B Schroeder  */
3573f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3583f542974SPaul B Schroeder 				__u16 *val)
3593f542974SPaul B Schroeder {
3603f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3613f542974SPaul B Schroeder 	int ret = 0;
3623f542974SPaul B Schroeder 	__u16 Wval;
3639e221a35SJohan Hovold 	u8 *buf;
3649e221a35SJohan Hovold 
3659e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3669e221a35SJohan Hovold 	if (!buf)
3679e221a35SJohan Hovold 		return -ENOMEM;
3683f542974SPaul B Schroeder 
36984fe6e79STony Cook 	/* dbg("application number is %4x",
370880af9dbSAlan Cox 	    (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
3713f542974SPaul B Schroeder 	/* Wval  is same as application number */
3720de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3733f542974SPaul B Schroeder 		Wval =
3743f542974SPaul B Schroeder 		    (((__u16) port->number - (__u16) (port->serial->minor)) +
3753f542974SPaul B Schroeder 		     1) << 8;
37684fe6e79STony Cook 		dbg("mos7840_get_uart_reg application number is %x", Wval);
3773f542974SPaul B Schroeder 	} else {
3783f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
379880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3803f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
38184fe6e79STony Cook 			dbg("mos7840_get_uart_reg application number is %x",
3823f542974SPaul B Schroeder 			    Wval);
3833f542974SPaul B Schroeder 		} else {
384880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3853f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
38684fe6e79STony Cook 			dbg("mos7840_get_uart_reg application number is %x",
3873f542974SPaul B Schroeder 			    Wval);
3883f542974SPaul B Schroeder 		}
3893f542974SPaul B Schroeder 	}
3903f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3919e221a35SJohan Hovold 			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
3923f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
3939e221a35SJohan Hovold 	*val = buf[0];
3949e221a35SJohan Hovold 
3959e221a35SJohan Hovold 	kfree(buf);
3963f542974SPaul B Schroeder 	return ret;
3973f542974SPaul B Schroeder }
3983f542974SPaul B Schroeder 
3993f542974SPaul B Schroeder static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
4003f542974SPaul B Schroeder {
4013f542974SPaul B Schroeder 
40284fe6e79STony Cook 	dbg("***************************************");
40384fe6e79STony Cook 	dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
40484fe6e79STony Cook 	dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
40584fe6e79STony Cook 	dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
40684fe6e79STony Cook 	dbg("***************************************");
4073f542974SPaul B Schroeder 
4083f542974SPaul B Schroeder }
4093f542974SPaul B Schroeder 
4103f542974SPaul B Schroeder /************************************************************************/
4113f542974SPaul B Schroeder /************************************************************************/
4123f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
4133f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
4143f542974SPaul B Schroeder /************************************************************************/
4153f542974SPaul B Schroeder /************************************************************************/
4163f542974SPaul B Schroeder 
4173f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port,
4183f542974SPaul B Schroeder 					    struct moschip_port *data)
4193f542974SPaul B Schroeder {
4203f542974SPaul B Schroeder 	usb_set_serial_port_data(port, (void *)data);
4213f542974SPaul B Schroeder }
4223f542974SPaul B Schroeder 
4233f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct
4243f542974SPaul B Schroeder 							    usb_serial_port
4253f542974SPaul B Schroeder 							    *port)
4263f542974SPaul B Schroeder {
4273f542974SPaul B Schroeder 	return (struct moschip_port *)usb_get_serial_port_data(port);
4283f542974SPaul B Schroeder }
4293f542974SPaul B Schroeder 
4300de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
4313f542974SPaul B Schroeder {
4323f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4333f542974SPaul B Schroeder 	struct async_icount *icount;
4343f542974SPaul B Schroeder 	mos7840_port = port;
4353f542974SPaul B Schroeder 	icount = &mos7840_port->icount;
4363f542974SPaul B Schroeder 	if (new_msr &
4373f542974SPaul B Schroeder 	    (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
4383f542974SPaul B Schroeder 	     MOS_MSR_DELTA_CD)) {
4393f542974SPaul B Schroeder 		icount = &mos7840_port->icount;
4403f542974SPaul B Schroeder 
4413f542974SPaul B Schroeder 		/* update input line counters */
4423f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CTS) {
4433f542974SPaul B Schroeder 			icount->cts++;
4440de9a702SOliver Neukum 			smp_wmb();
4453f542974SPaul B Schroeder 		}
4463f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_DSR) {
4473f542974SPaul B Schroeder 			icount->dsr++;
4480de9a702SOliver Neukum 			smp_wmb();
4493f542974SPaul B Schroeder 		}
4503f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CD) {
4513f542974SPaul B Schroeder 			icount->dcd++;
4520de9a702SOliver Neukum 			smp_wmb();
4533f542974SPaul B Schroeder 		}
4543f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_RI) {
4553f542974SPaul B Schroeder 			icount->rng++;
4560de9a702SOliver Neukum 			smp_wmb();
4570de9a702SOliver Neukum 		}
4583f542974SPaul B Schroeder 	}
4593f542974SPaul B Schroeder }
4603f542974SPaul B Schroeder 
4610de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
4623f542974SPaul B Schroeder {
4633f542974SPaul B Schroeder 	struct async_icount *icount;
4643f542974SPaul B Schroeder 
465441b62c1SHarvey Harrison 	dbg("%s - %02x", __func__, new_lsr);
4663f542974SPaul B Schroeder 
4673f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
468880af9dbSAlan Cox 		/*
469880af9dbSAlan Cox 		 * Parity and Framing errors only count if they
470880af9dbSAlan Cox 		 * occur exclusive of a break being
471880af9dbSAlan Cox 		 * received.
472880af9dbSAlan Cox 		 */
4733f542974SPaul B Schroeder 		new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
4743f542974SPaul B Schroeder 	}
4753f542974SPaul B Schroeder 
4763f542974SPaul B Schroeder 	/* update input line counters */
4773f542974SPaul B Schroeder 	icount = &port->icount;
4783f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
4793f542974SPaul B Schroeder 		icount->brk++;
4800de9a702SOliver Neukum 		smp_wmb();
4813f542974SPaul B Schroeder 	}
4823f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_OE) {
4833f542974SPaul B Schroeder 		icount->overrun++;
4840de9a702SOliver Neukum 		smp_wmb();
4853f542974SPaul B Schroeder 	}
4863f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_PE) {
4873f542974SPaul B Schroeder 		icount->parity++;
4880de9a702SOliver Neukum 		smp_wmb();
4893f542974SPaul B Schroeder 	}
4903f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_FE) {
4913f542974SPaul B Schroeder 		icount->frame++;
4920de9a702SOliver Neukum 		smp_wmb();
4933f542974SPaul B Schroeder 	}
4943f542974SPaul B Schroeder }
4953f542974SPaul B Schroeder 
4963f542974SPaul B Schroeder /************************************************************************/
4973f542974SPaul B Schroeder /************************************************************************/
4983f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4993f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
5003f542974SPaul B Schroeder /************************************************************************/
5013f542974SPaul B Schroeder /************************************************************************/
5023f542974SPaul B Schroeder 
5037d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb)
5043f542974SPaul B Schroeder {
5053f542974SPaul B Schroeder 	unsigned char *data;
5063f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
5073f542974SPaul B Schroeder 	__u8 regval = 0x0;
5080de9a702SOliver Neukum 	int result = 0;
5090643c724SGreg Kroah-Hartman 	int status = urb->status;
5103f542974SPaul B Schroeder 
511cdc97792SMing Lei 	mos7840_port = urb->context;
5120de9a702SOliver Neukum 
5130643c724SGreg Kroah-Hartman 	switch (status) {
5143f542974SPaul B Schroeder 	case 0:
5153f542974SPaul B Schroeder 		/* success */
5163f542974SPaul B Schroeder 		break;
5173f542974SPaul B Schroeder 	case -ECONNRESET:
5183f542974SPaul B Schroeder 	case -ENOENT:
5193f542974SPaul B Schroeder 	case -ESHUTDOWN:
5203f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
521441b62c1SHarvey Harrison 		dbg("%s - urb shutting down with status: %d", __func__,
5220643c724SGreg Kroah-Hartman 		    status);
5233f542974SPaul B Schroeder 		return;
5243f542974SPaul B Schroeder 	default:
525441b62c1SHarvey Harrison 		dbg("%s - nonzero urb status received: %d", __func__,
5260643c724SGreg Kroah-Hartman 		    status);
5273f542974SPaul B Schroeder 		goto exit;
5283f542974SPaul B Schroeder 	}
5293f542974SPaul B Schroeder 
53084fe6e79STony Cook 	dbg("%s urb buffer size is %d", __func__, urb->actual_length);
53184fe6e79STony Cook 	dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
5323f542974SPaul B Schroeder 	    mos7840_port->MsrLsr, mos7840_port->port_num);
5333f542974SPaul B Schroeder 	data = urb->transfer_buffer;
5343f542974SPaul B Schroeder 	regval = (__u8) data[0];
53584fe6e79STony Cook 	dbg("%s data is %x", __func__, regval);
5363f542974SPaul B Schroeder 	if (mos7840_port->MsrLsr == 0)
5373f542974SPaul B Schroeder 		mos7840_handle_new_msr(mos7840_port, regval);
5383f542974SPaul B Schroeder 	else if (mos7840_port->MsrLsr == 1)
5393f542974SPaul B Schroeder 		mos7840_handle_new_lsr(mos7840_port, regval);
5403f542974SPaul B Schroeder 
5413f542974SPaul B Schroeder exit:
5420de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
5430de9a702SOliver Neukum 	if (!mos7840_port->zombie)
5440de9a702SOliver Neukum 		result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
5450de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
5460de9a702SOliver Neukum 	if (result) {
5470de9a702SOliver Neukum 		dev_err(&urb->dev->dev,
5480de9a702SOliver Neukum 			"%s - Error %d submitting interrupt urb\n",
549441b62c1SHarvey Harrison 			__func__, result);
5500de9a702SOliver Neukum 	}
5513f542974SPaul B Schroeder }
5523f542974SPaul B Schroeder 
5533f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
5543f542974SPaul B Schroeder 			   __u16 *val)
5553f542974SPaul B Schroeder {
5563f542974SPaul B Schroeder 	struct usb_device *dev = mcs->port->serial->dev;
5570de9a702SOliver Neukum 	struct usb_ctrlrequest *dr = mcs->dr;
5580de9a702SOliver Neukum 	unsigned char *buffer = mcs->ctrl_buf;
5590de9a702SOliver Neukum 	int ret;
5603f542974SPaul B Schroeder 
5613f542974SPaul B Schroeder 	dr->bRequestType = MCS_RD_RTYPE;
5623f542974SPaul B Schroeder 	dr->bRequest = MCS_RDREQ;
563880af9dbSAlan Cox 	dr->wValue = cpu_to_le16(Wval);	/* 0 */
5643f542974SPaul B Schroeder 	dr->wIndex = cpu_to_le16(reg);
5653f542974SPaul B Schroeder 	dr->wLength = cpu_to_le16(2);
5663f542974SPaul B Schroeder 
5673f542974SPaul B Schroeder 	usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
5683f542974SPaul B Schroeder 			     (unsigned char *)dr, buffer, 2,
5693f542974SPaul B Schroeder 			     mos7840_control_callback, mcs);
5703f542974SPaul B Schroeder 	mcs->control_urb->transfer_buffer_length = 2;
5713f542974SPaul B Schroeder 	ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
5723f542974SPaul B Schroeder 	return ret;
5733f542974SPaul B Schroeder }
5743f542974SPaul B Schroeder 
5753f542974SPaul B Schroeder /*****************************************************************************
5763f542974SPaul B Schroeder  * mos7840_interrupt_callback
5773f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
5783f542974SPaul B Schroeder  *	interrupt endpoint.
5793f542974SPaul B Schroeder  *****************************************************************************/
5803f542974SPaul B Schroeder 
5817d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb)
5823f542974SPaul B Schroeder {
5833f542974SPaul B Schroeder 	int result;
5843f542974SPaul B Schroeder 	int length;
5853f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
5863f542974SPaul B Schroeder 	struct usb_serial *serial;
5873f542974SPaul B Schroeder 	__u16 Data;
5883f542974SPaul B Schroeder 	unsigned char *data;
5893f542974SPaul B Schroeder 	__u8 sp[5], st;
5900de9a702SOliver Neukum 	int i, rv = 0;
5910de9a702SOliver Neukum 	__u16 wval, wreg = 0;
5920643c724SGreg Kroah-Hartman 	int status = urb->status;
5933f542974SPaul B Schroeder 
59484fe6e79STony Cook 	dbg("%s", " : Entering");
5953f542974SPaul B Schroeder 
5960643c724SGreg Kroah-Hartman 	switch (status) {
5973f542974SPaul B Schroeder 	case 0:
5983f542974SPaul B Schroeder 		/* success */
5993f542974SPaul B Schroeder 		break;
6003f542974SPaul B Schroeder 	case -ECONNRESET:
6013f542974SPaul B Schroeder 	case -ENOENT:
6023f542974SPaul B Schroeder 	case -ESHUTDOWN:
6033f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
604441b62c1SHarvey Harrison 		dbg("%s - urb shutting down with status: %d", __func__,
6050643c724SGreg Kroah-Hartman 		    status);
6063f542974SPaul B Schroeder 		return;
6073f542974SPaul B Schroeder 	default:
608441b62c1SHarvey Harrison 		dbg("%s - nonzero urb status received: %d", __func__,
6090643c724SGreg Kroah-Hartman 		    status);
6103f542974SPaul B Schroeder 		goto exit;
6113f542974SPaul B Schroeder 	}
6123f542974SPaul B Schroeder 
6133f542974SPaul B Schroeder 	length = urb->actual_length;
6143f542974SPaul B Schroeder 	data = urb->transfer_buffer;
6153f542974SPaul B Schroeder 
616cdc97792SMing Lei 	serial = urb->context;
6173f542974SPaul B Schroeder 
6183f542974SPaul B Schroeder 	/* Moschip get 5 bytes
6193f542974SPaul B Schroeder 	 * Byte 1 IIR Port 1 (port.number is 0)
6203f542974SPaul B Schroeder 	 * Byte 2 IIR Port 2 (port.number is 1)
6213f542974SPaul B Schroeder 	 * Byte 3 IIR Port 3 (port.number is 2)
6223f542974SPaul B Schroeder 	 * Byte 4 IIR Port 4 (port.number is 3)
6233f542974SPaul B Schroeder 	 * Byte 5 FIFO status for both */
6243f542974SPaul B Schroeder 
6253f542974SPaul B Schroeder 	if (length && length > 5) {
62684fe6e79STony Cook 		dbg("%s", "Wrong data !!!");
6273f542974SPaul B Schroeder 		return;
6283f542974SPaul B Schroeder 	}
6293f542974SPaul B Schroeder 
6303f542974SPaul B Schroeder 	sp[0] = (__u8) data[0];
6313f542974SPaul B Schroeder 	sp[1] = (__u8) data[1];
6323f542974SPaul B Schroeder 	sp[2] = (__u8) data[2];
6333f542974SPaul B Schroeder 	sp[3] = (__u8) data[3];
6343f542974SPaul B Schroeder 	st = (__u8) data[4];
6353f542974SPaul B Schroeder 
6363f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; i++) {
6373f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
6383f542974SPaul B Schroeder 		wval =
6393f542974SPaul B Schroeder 		    (((__u16) serial->port[i]->number -
6403f542974SPaul B Schroeder 		      (__u16) (serial->minor)) + 1) << 8;
6413f542974SPaul B Schroeder 		if (mos7840_port->open) {
6423f542974SPaul B Schroeder 			if (sp[i] & 0x01) {
64384fe6e79STony Cook 				dbg("SP%d No Interrupt !!!", i);
6443f542974SPaul B Schroeder 			} else {
6453f542974SPaul B Schroeder 				switch (sp[i] & 0x0f) {
6463f542974SPaul B Schroeder 				case SERIAL_IIR_RLS:
6473f542974SPaul B Schroeder 					dbg("Serial Port %d: Receiver status error or ", i);
64884fe6e79STony Cook 					dbg("address bit detected in 9-bit mode");
6493f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 1;
6500de9a702SOliver Neukum 					wreg = LINE_STATUS_REGISTER;
6513f542974SPaul B Schroeder 					break;
6523f542974SPaul B Schroeder 				case SERIAL_IIR_MS:
65384fe6e79STony Cook 					dbg("Serial Port %d: Modem status change", i);
6543f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 0;
6550de9a702SOliver Neukum 					wreg = MODEM_STATUS_REGISTER;
6563f542974SPaul B Schroeder 					break;
6573f542974SPaul B Schroeder 				}
6580de9a702SOliver Neukum 				spin_lock(&mos7840_port->pool_lock);
6590de9a702SOliver Neukum 				if (!mos7840_port->zombie) {
6600de9a702SOliver Neukum 					rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
6610de9a702SOliver Neukum 				} else {
6620de9a702SOliver Neukum 					spin_unlock(&mos7840_port->pool_lock);
6630de9a702SOliver Neukum 					return;
6640de9a702SOliver Neukum 				}
6650de9a702SOliver Neukum 				spin_unlock(&mos7840_port->pool_lock);
6663f542974SPaul B Schroeder 			}
6673f542974SPaul B Schroeder 		}
6683f542974SPaul B Schroeder 	}
669880af9dbSAlan Cox 	if (!(rv < 0))
670880af9dbSAlan Cox 		/* the completion handler for the control urb will resubmit */
6710de9a702SOliver Neukum 		return;
6723f542974SPaul B Schroeder exit:
6733f542974SPaul B Schroeder 	result = usb_submit_urb(urb, GFP_ATOMIC);
6743f542974SPaul B Schroeder 	if (result) {
6753f542974SPaul B Schroeder 		dev_err(&urb->dev->dev,
6763f542974SPaul B Schroeder 			"%s - Error %d submitting interrupt urb\n",
677441b62c1SHarvey Harrison 			__func__, result);
6783f542974SPaul B Schroeder 	}
6793f542974SPaul B Schroeder }
6803f542974SPaul B Schroeder 
6813f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port,
6823f542974SPaul B Schroeder 				       const char *function)
6833f542974SPaul B Schroeder {
6843f542974SPaul B Schroeder 	if (!port) {
6853f542974SPaul B Schroeder 		dbg("%s - port == NULL", function);
6863f542974SPaul B Schroeder 		return -1;
6873f542974SPaul B Schroeder 	}
6883f542974SPaul B Schroeder 	if (!port->serial) {
6893f542974SPaul B Schroeder 		dbg("%s - port->serial == NULL", function);
6903f542974SPaul B Schroeder 		return -1;
6913f542974SPaul B Schroeder 	}
6923f542974SPaul B Schroeder 
6933f542974SPaul B Schroeder 	return 0;
6943f542974SPaul B Schroeder }
6953f542974SPaul B Schroeder 
6963f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
6973f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
6983f542974SPaul B Schroeder 					 const char *function)
6993f542974SPaul B Schroeder {
7003f542974SPaul B Schroeder 	if (!serial) {
7013f542974SPaul B Schroeder 		dbg("%s - serial == NULL", function);
7023f542974SPaul B Schroeder 		return -1;
7033f542974SPaul B Schroeder 	}
7043f542974SPaul B Schroeder 	if (!serial->type) {
7053f542974SPaul B Schroeder 		dbg("%s - serial->type == NULL!", function);
7063f542974SPaul B Schroeder 		return -1;
7073f542974SPaul B Schroeder 	}
7083f542974SPaul B Schroeder 
7093f542974SPaul B Schroeder 	return 0;
7103f542974SPaul B Schroeder }
7113f542974SPaul B Schroeder 
7123f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
7133f542974SPaul B Schroeder 						 const char *function)
7143f542974SPaul B Schroeder {
7153f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
7163f542974SPaul B Schroeder 	if (!port ||
7173f542974SPaul B Schroeder 	    mos7840_port_paranoia_check(port, function) ||
7183f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
719880af9dbSAlan Cox 		/* then say that we don't have a valid usb_serial thing,
720880af9dbSAlan Cox 		 * which will end up genrating -ENODEV return values */
7213f542974SPaul B Schroeder 		return NULL;
7223f542974SPaul B Schroeder 	}
7233f542974SPaul B Schroeder 
7243f542974SPaul B Schroeder 	return port->serial;
7253f542974SPaul B Schroeder }
7263f542974SPaul B Schroeder 
7273f542974SPaul B Schroeder /*****************************************************************************
7283f542974SPaul B Schroeder  * mos7840_bulk_in_callback
7293f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
7303f542974SPaul B Schroeder  *	bulk in endpoint.
7313f542974SPaul B Schroeder  *****************************************************************************/
7323f542974SPaul B Schroeder 
7337d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
7343f542974SPaul B Schroeder {
7350643c724SGreg Kroah-Hartman 	int retval;
7363f542974SPaul B Schroeder 	unsigned char *data;
7373f542974SPaul B Schroeder 	struct usb_serial *serial;
7383f542974SPaul B Schroeder 	struct usb_serial_port *port;
7393f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
7403f542974SPaul B Schroeder 	struct tty_struct *tty;
7410643c724SGreg Kroah-Hartman 	int status = urb->status;
7423f542974SPaul B Schroeder 
743cdc97792SMing Lei 	mos7840_port = urb->context;
7443f542974SPaul B Schroeder 	if (!mos7840_port) {
74584fe6e79STony Cook 		dbg("%s", "NULL mos7840_port pointer");
74650de36f7SGreg Kroah-Hartman 		return;
74750de36f7SGreg Kroah-Hartman 	}
74850de36f7SGreg Kroah-Hartman 
74950de36f7SGreg Kroah-Hartman 	if (status) {
75050de36f7SGreg Kroah-Hartman 		dbg("nonzero read bulk status received: %d", status);
75150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7523f542974SPaul B Schroeder 		return;
7533f542974SPaul B Schroeder 	}
7543f542974SPaul B Schroeder 
7553f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
756441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
75784fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
75850de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7593f542974SPaul B Schroeder 		return;
7603f542974SPaul B Schroeder 	}
7613f542974SPaul B Schroeder 
762441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
7633f542974SPaul B Schroeder 	if (!serial) {
76484fe6e79STony Cook 		dbg("%s", "Bad serial pointer");
76550de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7663f542974SPaul B Schroeder 		return;
7673f542974SPaul B Schroeder 	}
7683f542974SPaul B Schroeder 
76984fe6e79STony Cook 	dbg("%s", "Entering... ");
7703f542974SPaul B Schroeder 
7713f542974SPaul B Schroeder 	data = urb->transfer_buffer;
7723f542974SPaul B Schroeder 
77384fe6e79STony Cook 	dbg("%s", "Entering ...........");
7743f542974SPaul B Schroeder 
7753f542974SPaul B Schroeder 	if (urb->actual_length) {
7764a90f09bSAlan Cox 		tty = tty_port_tty_get(&mos7840_port->port->port);
7773f542974SPaul B Schroeder 		if (tty) {
7783f542974SPaul B Schroeder 			tty_insert_flip_string(tty, data, urb->actual_length);
77984fe6e79STony Cook 			dbg(" %s ", data);
7803f542974SPaul B Schroeder 			tty_flip_buffer_push(tty);
7814a90f09bSAlan Cox 			tty_kref_put(tty);
7823f542974SPaul B Schroeder 		}
7833f542974SPaul B Schroeder 		mos7840_port->icount.rx += urb->actual_length;
7840de9a702SOliver Neukum 		smp_wmb();
78584fe6e79STony Cook 		dbg("mos7840_port->icount.rx is %d:",
7863f542974SPaul B Schroeder 		    mos7840_port->icount.rx);
7873f542974SPaul B Schroeder 	}
7883f542974SPaul B Schroeder 
7893f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
79084fe6e79STony Cook 		dbg("%s", "URB KILLED !!!");
79150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7923f542974SPaul B Schroeder 		return;
7933f542974SPaul B Schroeder 	}
7943f542974SPaul B Schroeder 
7950de9a702SOliver Neukum 
79650de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
7970643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
7983f542974SPaul B Schroeder 
7990643c724SGreg Kroah-Hartman 	if (retval) {
80050de36f7SGreg Kroah-Hartman 		dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
80150de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8023f542974SPaul B Schroeder 	}
8033f542974SPaul B Schroeder }
8043f542974SPaul B Schroeder 
8053f542974SPaul B Schroeder /*****************************************************************************
8063f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
807880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
808880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
8093f542974SPaul B Schroeder  *****************************************************************************/
8103f542974SPaul B Schroeder 
8117d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
8123f542974SPaul B Schroeder {
8133f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8143f542974SPaul B Schroeder 	struct tty_struct *tty;
8150643c724SGreg Kroah-Hartman 	int status = urb->status;
8160de9a702SOliver Neukum 	int i;
8170de9a702SOliver Neukum 
818cdc97792SMing Lei 	mos7840_port = urb->context;
8190de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
8200de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
8210de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
8220de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
8230de9a702SOliver Neukum 			break;
8240de9a702SOliver Neukum 		}
8250de9a702SOliver Neukum 	}
8260de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
8270de9a702SOliver Neukum 
8280643c724SGreg Kroah-Hartman 	if (status) {
82984fe6e79STony Cook 		dbg("nonzero write bulk status received:%d", status);
8303f542974SPaul B Schroeder 		return;
8313f542974SPaul B Schroeder 	}
8323f542974SPaul B Schroeder 
833441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
83484fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
8353f542974SPaul B Schroeder 		return;
8363f542974SPaul B Schroeder 	}
8373f542974SPaul B Schroeder 
83884fe6e79STony Cook 	dbg("%s", "Entering .........");
8393f542974SPaul B Schroeder 
8404a90f09bSAlan Cox 	tty = tty_port_tty_get(&mos7840_port->port->port);
841b963a844SJiri Slaby 	if (tty && mos7840_port->open)
842b963a844SJiri Slaby 		tty_wakeup(tty);
8434a90f09bSAlan Cox 	tty_kref_put(tty);
8443f542974SPaul B Schroeder 
8453f542974SPaul B Schroeder }
8463f542974SPaul B Schroeder 
8473f542974SPaul B Schroeder /************************************************************************/
8483f542974SPaul 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       */
8493f542974SPaul B Schroeder /************************************************************************/
8503f542974SPaul B Schroeder #ifdef MCSSerialProbe
8513f542974SPaul B Schroeder static int mos7840_serial_probe(struct usb_serial *serial,
8523f542974SPaul B Schroeder 				const struct usb_device_id *id)
8533f542974SPaul B Schroeder {
8543f542974SPaul B Schroeder 
8553f542974SPaul B Schroeder 	/*need to implement the mode_reg reading and updating\
8563f542974SPaul B Schroeder 	   structures usb_serial_ device_type\
8573f542974SPaul B Schroeder 	   (i.e num_ports, num_bulkin,bulkout etc) */
8583f542974SPaul B Schroeder 	/* Also we can update the changes  attach */
8593f542974SPaul B Schroeder 	return 1;
8603f542974SPaul B Schroeder }
8613f542974SPaul B Schroeder #endif
8623f542974SPaul B Schroeder 
8633f542974SPaul B Schroeder /*****************************************************************************
8643f542974SPaul B Schroeder  * mos7840_open
8653f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
8663f542974SPaul B Schroeder  *	If successful, we return 0
8673f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
8683f542974SPaul B Schroeder  *****************************************************************************/
8693f542974SPaul B Schroeder 
870a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
8713f542974SPaul B Schroeder {
8723f542974SPaul B Schroeder 	int response;
8733f542974SPaul B Schroeder 	int j;
8743f542974SPaul B Schroeder 	struct usb_serial *serial;
8753f542974SPaul B Schroeder 	struct urb *urb;
8763f542974SPaul B Schroeder 	__u16 Data;
8773f542974SPaul B Schroeder 	int status;
8783f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8790de9a702SOliver Neukum 	struct moschip_port *port0;
8803f542974SPaul B Schroeder 
88184fe6e79STony Cook 	dbg ("%s enter", __func__);
88284fe6e79STony Cook 
883441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
88484fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
8853f542974SPaul B Schroeder 		return -ENODEV;
8863f542974SPaul B Schroeder 	}
8873f542974SPaul B Schroeder 
8883f542974SPaul B Schroeder 	serial = port->serial;
8893f542974SPaul B Schroeder 
890441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
89184fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
8923f542974SPaul B Schroeder 		return -ENODEV;
8933f542974SPaul B Schroeder 	}
8943f542974SPaul B Schroeder 
8953f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
8960de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
8973f542974SPaul B Schroeder 
8980de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
8993f542974SPaul B Schroeder 		return -ENODEV;
9003f542974SPaul B Schroeder 
9013f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
9023f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
9030de9a702SOliver Neukum 	port0->open_ports++;
9043f542974SPaul B Schroeder 
9053f542974SPaul B Schroeder 	/* Initialising the write urb pool */
9063f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
9070de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
9083f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
9093f542974SPaul B Schroeder 
9103f542974SPaul B Schroeder 		if (urb == NULL) {
911194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "No more urbs???\n");
9123f542974SPaul B Schroeder 			continue;
9133f542974SPaul B Schroeder 		}
9143f542974SPaul B Schroeder 
915880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
916880af9dbSAlan Cox 								GFP_KERNEL);
9173f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
9180de9a702SOliver Neukum 			usb_free_urb(urb);
9190de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
920194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
921194343d9SGreg Kroah-Hartman 				"%s-out of memory for urb buffers.\n",
922194343d9SGreg Kroah-Hartman 				__func__);
9233f542974SPaul B Schroeder 			continue;
9243f542974SPaul B Schroeder 		}
9253f542974SPaul B Schroeder 	}
9263f542974SPaul B Schroeder 
9273f542974SPaul B Schroeder /*****************************************************************************
9283f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
9293f542974SPaul B Schroeder  *
9303f542974SPaul B Schroeder  * Register Index
9313f542974SPaul B Schroeder  * 1 : IER
9323f542974SPaul B Schroeder  * 2 : FCR
9333f542974SPaul B Schroeder  * 3 : LCR
9343f542974SPaul B Schroeder  * 4 : MCR
9353f542974SPaul B Schroeder  *
9363f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
9373f542974SPaul B Schroeder  *****************************************************************************/
9383f542974SPaul B Schroeder 
939880af9dbSAlan Cox 	/* NEED to check the following Block */
9403f542974SPaul B Schroeder 
9413f542974SPaul B Schroeder 	Data = 0x0;
9423f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
9433f542974SPaul B Schroeder 	if (status < 0) {
94484fe6e79STony Cook 		dbg("Reading Spreg failed");
9453f542974SPaul B Schroeder 		return -1;
9463f542974SPaul B Schroeder 	}
9473f542974SPaul B Schroeder 	Data |= 0x80;
9483f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9493f542974SPaul B Schroeder 	if (status < 0) {
95084fe6e79STony Cook 		dbg("writing Spreg failed");
9513f542974SPaul B Schroeder 		return -1;
9523f542974SPaul B Schroeder 	}
9533f542974SPaul B Schroeder 
9543f542974SPaul B Schroeder 	Data &= ~0x80;
9553f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9563f542974SPaul B Schroeder 	if (status < 0) {
95784fe6e79STony Cook 		dbg("writing Spreg failed");
9583f542974SPaul B Schroeder 		return -1;
9593f542974SPaul B Schroeder 	}
960880af9dbSAlan Cox 	/* End of block to be checked */
9613f542974SPaul B Schroeder 
9623f542974SPaul B Schroeder 	Data = 0x0;
963880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
964880af9dbSAlan Cox 									&Data);
9653f542974SPaul B Schroeder 	if (status < 0) {
96684fe6e79STony Cook 		dbg("Reading Controlreg failed");
9673f542974SPaul B Schroeder 		return -1;
9683f542974SPaul B Schroeder 	}
969880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
970880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
971880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
972880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
9733f542974SPaul B Schroeder 	if (status < 0) {
97484fe6e79STony Cook 		dbg("writing Controlreg failed");
9753f542974SPaul B Schroeder 		return -1;
9763f542974SPaul B Schroeder 	}
977880af9dbSAlan Cox 	/* do register settings here */
978880af9dbSAlan Cox 	/* Set all regs to the device default values. */
979880af9dbSAlan Cox 	/***********************************
980880af9dbSAlan Cox 	 * First Disable all interrupts.
981880af9dbSAlan Cox 	 ***********************************/
9823f542974SPaul B Schroeder 	Data = 0x00;
9833f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
9843f542974SPaul B Schroeder 	if (status < 0) {
98584fe6e79STony Cook 		dbg("disabling interrupts failed");
9863f542974SPaul B Schroeder 		return -1;
9873f542974SPaul B Schroeder 	}
988880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
9893f542974SPaul B Schroeder 	Data = 0x00;
9903f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9913f542974SPaul B Schroeder 	if (status < 0) {
99284fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
9933f542974SPaul B Schroeder 		return -1;
9943f542974SPaul B Schroeder 	}
9953f542974SPaul B Schroeder 
9963f542974SPaul B Schroeder 	Data = 0xcf;
9973f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9983f542974SPaul B Schroeder 	if (status < 0) {
99984fe6e79STony Cook 		dbg("Writing FIFO_CONTROL_REGISTER  failed");
10003f542974SPaul B Schroeder 		return -1;
10013f542974SPaul B Schroeder 	}
10023f542974SPaul B Schroeder 
10033f542974SPaul B Schroeder 	Data = 0x03;
10043f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10053f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10063f542974SPaul B Schroeder 
10073f542974SPaul B Schroeder 	Data = 0x0b;
10083f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
10093f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
10103f542974SPaul B Schroeder 
10113f542974SPaul B Schroeder 	Data = 0x00;
10123f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
10133f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10143f542974SPaul B Schroeder 
1015880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
10163f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10173f542974SPaul B Schroeder 
10183f542974SPaul B Schroeder 	Data = 0x0c;
10193f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
10203f542974SPaul B Schroeder 
10213f542974SPaul B Schroeder 	Data = 0x0;
10223f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
10233f542974SPaul B Schroeder 
10243f542974SPaul B Schroeder 	Data = 0x00;
10253f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
10263f542974SPaul B Schroeder 
10273f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
10283f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10293f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10303f542974SPaul B Schroeder 
1031880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
10323f542974SPaul B Schroeder 	Data = 0x0;
10333f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
10343f542974SPaul B Schroeder 
10353f542974SPaul B Schroeder 	Data = Data | 0x0c;
10363f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10373f542974SPaul B Schroeder 
10383f542974SPaul B Schroeder 	Data = Data & ~0x0c;
10393f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1040880af9dbSAlan Cox 	/* Finally enable all interrupts */
10413f542974SPaul B Schroeder 	Data = 0x0c;
10423f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
10433f542974SPaul B Schroeder 
1044880af9dbSAlan Cox 	/* clearing rx_disable */
10453f542974SPaul B Schroeder 	Data = 0x0;
1046880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1047880af9dbSAlan Cox 									&Data);
10483f542974SPaul B Schroeder 	Data = Data & ~0x20;
1049880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1050880af9dbSAlan Cox 									Data);
10513f542974SPaul B Schroeder 
1052880af9dbSAlan Cox 	/* rx_negate */
10533f542974SPaul B Schroeder 	Data = 0x0;
1054880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1055880af9dbSAlan Cox 									&Data);
10563f542974SPaul B Schroeder 	Data = Data | 0x10;
1057880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1058880af9dbSAlan Cox 									Data);
10593f542974SPaul B Schroeder 
10603f542974SPaul B Schroeder 	/* Check to see if we've set up our endpoint info yet    *
10613f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the structures *
10623f542974SPaul B Schroeder 	 * were not set up at that time.)                        */
10630de9a702SOliver Neukum 	if (port0->open_ports == 1) {
10643f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_buffer == NULL) {
10653f542974SPaul B Schroeder 			/* set up interrupt urb */
10663f542974SPaul B Schroeder 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
10673f542974SPaul B Schroeder 				serial->dev,
10683f542974SPaul B Schroeder 				usb_rcvintpipe(serial->dev,
1069880af9dbSAlan Cox 				serial->port[0]->interrupt_in_endpointAddress),
10703f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_buffer,
10713f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_urb->
10723f542974SPaul B Schroeder 				transfer_buffer_length,
10733f542974SPaul B Schroeder 				mos7840_interrupt_callback,
10743f542974SPaul B Schroeder 				serial,
1075880af9dbSAlan Cox 				serial->port[0]->interrupt_in_urb->interval);
10763f542974SPaul B Schroeder 
10773f542974SPaul B Schroeder 			/* start interrupt read for mos7840               *
10783f542974SPaul B Schroeder 			 * will continue as long as mos7840 is connected  */
10793f542974SPaul B Schroeder 
10803f542974SPaul B Schroeder 			response =
10813f542974SPaul B Schroeder 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
10823f542974SPaul B Schroeder 					   GFP_KERNEL);
10833f542974SPaul B Schroeder 			if (response) {
1084194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "%s - Error %d submitting "
1085194343d9SGreg Kroah-Hartman 					"interrupt urb\n", __func__, response);
10863f542974SPaul B Schroeder 			}
10873f542974SPaul B Schroeder 
10883f542974SPaul B Schroeder 		}
10893f542974SPaul B Schroeder 
10903f542974SPaul B Schroeder 	}
10913f542974SPaul B Schroeder 
10923f542974SPaul B Schroeder 	/* see if we've set up our endpoint info yet   *
10933f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the  *
10943f542974SPaul B Schroeder 	 * structures were not set up at that time.)   */
10953f542974SPaul B Schroeder 
109684fe6e79STony Cook 	dbg("port number is %d", port->number);
109784fe6e79STony Cook 	dbg("serial number is %d", port->serial->minor);
109884fe6e79STony Cook 	dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
109984fe6e79STony Cook 	dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
110084fe6e79STony Cook 	dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
110184fe6e79STony Cook 	dbg("port's number in the device is %d", mos7840_port->port_num);
11023f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
11033f542974SPaul B Schroeder 
11043f542974SPaul B Schroeder 	/* set up our bulk in urb */
1105*093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1106*093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1107*093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1108*093ea2d3SDonald Lee 		usb_fill_bulk_urb(mos7840_port->read_urb,
1109*093ea2d3SDonald Lee 			serial->dev,
1110*093ea2d3SDonald Lee 			usb_rcvbulkpipe(serial->dev,
1111*093ea2d3SDonald Lee 				(port->bulk_in_endpointAddress) + 2),
1112*093ea2d3SDonald Lee 			port->bulk_in_buffer,
1113*093ea2d3SDonald Lee 			mos7840_port->read_urb->transfer_buffer_length,
1114*093ea2d3SDonald Lee 			mos7840_bulk_in_callback, mos7840_port);
1115*093ea2d3SDonald Lee 	} else {
11163f542974SPaul B Schroeder 		usb_fill_bulk_urb(mos7840_port->read_urb,
11173f542974SPaul B Schroeder 			serial->dev,
11183f542974SPaul B Schroeder 			usb_rcvbulkpipe(serial->dev,
11193f542974SPaul B Schroeder 				port->bulk_in_endpointAddress),
11203f542974SPaul B Schroeder 			port->bulk_in_buffer,
11213f542974SPaul B Schroeder 			mos7840_port->read_urb->transfer_buffer_length,
11223f542974SPaul B Schroeder 			mos7840_bulk_in_callback, mos7840_port);
1123*093ea2d3SDonald Lee 	}
11243f542974SPaul B Schroeder 
112584fe6e79STony Cook 	dbg("mos7840_open: bulkin endpoint is %d",
11263f542974SPaul B Schroeder 	    port->bulk_in_endpointAddress);
112750de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
11283f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
11293f542974SPaul B Schroeder 	if (response) {
1130194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1131194343d9SGreg Kroah-Hartman 			__func__, response);
113250de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
11333f542974SPaul B Schroeder 	}
11343f542974SPaul B Schroeder 
11353f542974SPaul B Schroeder 	/* initialize our wait queues */
11363f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->wait_chase);
11373f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->delta_msr_wait);
11383f542974SPaul B Schroeder 
11393f542974SPaul B Schroeder 	/* initialize our icount structure */
11403f542974SPaul B Schroeder 	memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
11413f542974SPaul B Schroeder 
11423f542974SPaul B Schroeder 	/* initialize our port settings */
1143880af9dbSAlan Cox 	/* Must set to enable ints! */
1144880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
11453f542974SPaul B Schroeder 	/* send a open port command */
11463f542974SPaul B Schroeder 	mos7840_port->open = 1;
1147880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
11483f542974SPaul B Schroeder 	mos7840_port->icount.tx = 0;
11493f542974SPaul B Schroeder 	mos7840_port->icount.rx = 0;
11503f542974SPaul B Schroeder 
115184fe6e79STony Cook 	dbg("usb_serial serial:%p       mos7840_port:%p\n      usb_serial_port port:%p",
1152880af9dbSAlan Cox 				serial, mos7840_port, port);
11533f542974SPaul B Schroeder 
115484fe6e79STony Cook 	dbg ("%s leave", __func__);
115584fe6e79STony Cook 
11563f542974SPaul B Schroeder 	return 0;
11573f542974SPaul B Schroeder 
11583f542974SPaul B Schroeder }
11593f542974SPaul B Schroeder 
11603f542974SPaul B Schroeder /*****************************************************************************
11613f542974SPaul B Schroeder  * mos7840_chars_in_buffer
11623f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
11633f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
11643f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
11653f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
11663f542974SPaul B Schroeder  *	system,
116795da310eSAlan Cox  *	Otherwise we return zero.
11683f542974SPaul B Schroeder  *****************************************************************************/
11693f542974SPaul B Schroeder 
117095da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
11713f542974SPaul B Schroeder {
117295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11733f542974SPaul B Schroeder 	int i;
11743f542974SPaul B Schroeder 	int chars = 0;
11750de9a702SOliver Neukum 	unsigned long flags;
11763f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11773f542974SPaul B Schroeder 
117884fe6e79STony Cook 	dbg("%s", " mos7840_chars_in_buffer:entering ...........");
11793f542974SPaul B Schroeder 
1180441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
118184fe6e79STony Cook 		dbg("%s", "Invalid port");
118295da310eSAlan Cox 		return 0;
11833f542974SPaul B Schroeder 	}
11843f542974SPaul B Schroeder 
11853f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11863f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
118784fe6e79STony Cook 		dbg("%s", "mos7840_break:leaving ...........");
118895da310eSAlan Cox 		return 0;
11893f542974SPaul B Schroeder 	}
11903f542974SPaul B Schroeder 
11910de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
119295da310eSAlan Cox 	for (i = 0; i < NUM_URBS; ++i)
119395da310eSAlan Cox 		if (mos7840_port->busy[i])
11943f542974SPaul B Schroeder 			chars += URB_TRANSFER_BUFFER_SIZE;
11950de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1196441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, chars);
11970de9a702SOliver Neukum 	return chars;
11983f542974SPaul B Schroeder 
11993f542974SPaul B Schroeder }
12003f542974SPaul B Schroeder 
12013f542974SPaul B Schroeder /*****************************************************************************
12023f542974SPaul B Schroeder  * mos7840_close
12033f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
12043f542974SPaul B Schroeder  *****************************************************************************/
12053f542974SPaul B Schroeder 
1206335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
12073f542974SPaul B Schroeder {
12083f542974SPaul B Schroeder 	struct usb_serial *serial;
12093f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
12100de9a702SOliver Neukum 	struct moschip_port *port0;
12113f542974SPaul B Schroeder 	int j;
12123f542974SPaul B Schroeder 	__u16 Data;
12133f542974SPaul B Schroeder 
121484fe6e79STony Cook 	dbg("%s", "mos7840_close:entering...");
12153f542974SPaul B Schroeder 
1216441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
121784fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
12183f542974SPaul B Schroeder 		return;
12193f542974SPaul B Schroeder 	}
12203f542974SPaul B Schroeder 
1221441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
12223f542974SPaul B Schroeder 	if (!serial) {
122384fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
12243f542974SPaul B Schroeder 		return;
12253f542974SPaul B Schroeder 	}
12263f542974SPaul B Schroeder 
12273f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12280de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
12293f542974SPaul B Schroeder 
12300de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
12313f542974SPaul B Schroeder 		return;
12323f542974SPaul B Schroeder 
12333f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
12343f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
12353f542974SPaul B Schroeder 
12363f542974SPaul B Schroeder 	/* Freeing Write URBs */
12373f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
12383f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
12393f542974SPaul B Schroeder 			if (mos7840_port->write_urb_pool[j]->transfer_buffer)
12403f542974SPaul B Schroeder 				kfree(mos7840_port->write_urb_pool[j]->
12413f542974SPaul B Schroeder 				      transfer_buffer);
12423f542974SPaul B Schroeder 
12433f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
12443f542974SPaul B Schroeder 		}
12453f542974SPaul B Schroeder 	}
12463f542974SPaul B Schroeder 
12473f542974SPaul B Schroeder 	/* While closing port, shutdown all bulk read, write  *
12483f542974SPaul B Schroeder 	 * and interrupt read if they exists                  */
12493f542974SPaul B Schroeder 	if (serial->dev) {
12503f542974SPaul B Schroeder 		if (mos7840_port->write_urb) {
125184fe6e79STony Cook 			dbg("%s", "Shutdown bulk write");
12523f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->write_urb);
12533f542974SPaul B Schroeder 		}
12543f542974SPaul B Schroeder 		if (mos7840_port->read_urb) {
125584fe6e79STony Cook 			dbg("%s", "Shutdown bulk read");
12563f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->read_urb);
125750de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
12583f542974SPaul B Schroeder 		}
12593f542974SPaul B Schroeder 		if ((&mos7840_port->control_urb)) {
126084fe6e79STony Cook 			dbg("%s", "Shutdown control read");
1261880af9dbSAlan Cox 			/*/      usb_kill_urb (mos7840_port->control_urb); */
12623f542974SPaul B Schroeder 		}
12633f542974SPaul B Schroeder 	}
1264880af9dbSAlan Cox /*      if(mos7840_port->ctrl_buf != NULL) */
1265880af9dbSAlan Cox /*              kfree(mos7840_port->ctrl_buf); */
12660de9a702SOliver Neukum 	port0->open_ports--;
126784fe6e79STony Cook 	dbg("mos7840_num_open_ports in close%d:in port%d",
12680de9a702SOliver Neukum 	    port0->open_ports, port->number);
12690de9a702SOliver Neukum 	if (port0->open_ports == 0) {
12703f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_urb) {
127184fe6e79STony Cook 			dbg("%s", "Shutdown interrupt_in_urb");
12720de9a702SOliver Neukum 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
12733f542974SPaul B Schroeder 		}
12743f542974SPaul B Schroeder 	}
12753f542974SPaul B Schroeder 
12763f542974SPaul B Schroeder 	if (mos7840_port->write_urb) {
12773f542974SPaul B Schroeder 		/* if this urb had a transfer buffer already (old tx) free it */
1278880af9dbSAlan Cox 		if (mos7840_port->write_urb->transfer_buffer != NULL)
12793f542974SPaul B Schroeder 			kfree(mos7840_port->write_urb->transfer_buffer);
12803f542974SPaul B Schroeder 		usb_free_urb(mos7840_port->write_urb);
12813f542974SPaul B Schroeder 	}
12823f542974SPaul B Schroeder 
12833f542974SPaul B Schroeder 	Data = 0x0;
12843f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
12853f542974SPaul B Schroeder 
12863f542974SPaul B Schroeder 	Data = 0x00;
12873f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
12883f542974SPaul B Schroeder 
12893f542974SPaul B Schroeder 	mos7840_port->open = 0;
12903f542974SPaul B Schroeder 
129184fe6e79STony Cook 	dbg("%s", "Leaving ............");
12923f542974SPaul B Schroeder }
12933f542974SPaul B Schroeder 
12943f542974SPaul B Schroeder /************************************************************************
12953f542974SPaul B Schroeder  *
12963f542974SPaul B Schroeder  * mos7840_block_until_chase_response
12973f542974SPaul B Schroeder  *
12983f542974SPaul B Schroeder  *	This function will block the close until one of the following:
12993f542974SPaul B Schroeder  *		1. Response to our Chase comes from mos7840
1300dc0d5c1eSJoe Perches  *		2. A timeout of 10 seconds without activity has expired
13013f542974SPaul B Schroeder  *		   (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
13023f542974SPaul B Schroeder  *
13033f542974SPaul B Schroeder  ************************************************************************/
13043f542974SPaul B Schroeder 
130595da310eSAlan Cox static void mos7840_block_until_chase_response(struct tty_struct *tty,
130695da310eSAlan Cox 					struct moschip_port *mos7840_port)
13073f542974SPaul B Schroeder {
13083f542974SPaul B Schroeder 	int timeout = 1 * HZ;
13093f542974SPaul B Schroeder 	int wait = 10;
13103f542974SPaul B Schroeder 	int count;
13113f542974SPaul B Schroeder 
13123f542974SPaul B Schroeder 	while (1) {
131395da310eSAlan Cox 		count = mos7840_chars_in_buffer(tty);
13143f542974SPaul B Schroeder 
13153f542974SPaul B Schroeder 		/* Check for Buffer status */
1316880af9dbSAlan Cox 		if (count <= 0)
13173f542974SPaul B Schroeder 			return;
13183f542974SPaul B Schroeder 
13193f542974SPaul B Schroeder 		/* Block the thread for a while */
13203f542974SPaul B Schroeder 		interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
13213f542974SPaul B Schroeder 					       timeout);
13223f542974SPaul B Schroeder 		/* No activity.. count down section */
13233f542974SPaul B Schroeder 		wait--;
13243f542974SPaul B Schroeder 		if (wait == 0) {
1325441b62c1SHarvey Harrison 			dbg("%s - TIMEOUT", __func__);
13263f542974SPaul B Schroeder 			return;
13273f542974SPaul B Schroeder 		} else {
1328dc0d5c1eSJoe Perches 			/* Reset timeout value back to seconds */
13293f542974SPaul B Schroeder 			wait = 10;
13303f542974SPaul B Schroeder 		}
13313f542974SPaul B Schroeder 	}
13323f542974SPaul B Schroeder 
13333f542974SPaul B Schroeder }
13343f542974SPaul B Schroeder 
13353f542974SPaul B Schroeder /*****************************************************************************
13363f542974SPaul B Schroeder  * mos7840_break
13373f542974SPaul B Schroeder  *	this function sends a break to the port
13383f542974SPaul B Schroeder  *****************************************************************************/
133995da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
13403f542974SPaul B Schroeder {
134195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
13423f542974SPaul B Schroeder 	unsigned char data;
13433f542974SPaul B Schroeder 	struct usb_serial *serial;
13443f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13453f542974SPaul B Schroeder 
134684fe6e79STony Cook 	dbg("%s", "Entering ...........");
134784fe6e79STony Cook 	dbg("mos7840_break: Start");
13483f542974SPaul B Schroeder 
1349441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
135084fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
13513f542974SPaul B Schroeder 		return;
13523f542974SPaul B Schroeder 	}
13533f542974SPaul B Schroeder 
1354441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
13553f542974SPaul B Schroeder 	if (!serial) {
135684fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
13573f542974SPaul B Schroeder 		return;
13583f542974SPaul B Schroeder 	}
13593f542974SPaul B Schroeder 
13603f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13613f542974SPaul B Schroeder 
1362880af9dbSAlan Cox 	if (mos7840_port == NULL)
13633f542974SPaul B Schroeder 		return;
13643f542974SPaul B Schroeder 
136595da310eSAlan Cox 	if (serial->dev)
13663f542974SPaul B Schroeder 		/* flush and block until tx is empty */
136795da310eSAlan Cox 		mos7840_block_until_chase_response(tty, mos7840_port);
13683f542974SPaul B Schroeder 
136995da310eSAlan Cox 	if (break_state == -1)
13703f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
137195da310eSAlan Cox 	else
13723f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
13733f542974SPaul B Schroeder 
13746b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
13753f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
137684fe6e79STony Cook 	dbg("mcs7840_break mos7840_port->shadowLCR is %x",
13773f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
13783f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
13793f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
13803f542974SPaul B Schroeder }
13813f542974SPaul B Schroeder 
13823f542974SPaul B Schroeder /*****************************************************************************
13833f542974SPaul B Schroeder  * mos7840_write_room
13843f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
13853f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
13863f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
13873f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
13883f542974SPaul B Schroeder  *****************************************************************************/
13893f542974SPaul B Schroeder 
139095da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
13913f542974SPaul B Schroeder {
139295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
13933f542974SPaul B Schroeder 	int i;
13943f542974SPaul B Schroeder 	int room = 0;
13950de9a702SOliver Neukum 	unsigned long flags;
13963f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13973f542974SPaul B Schroeder 
139884fe6e79STony Cook 	dbg("%s", " mos7840_write_room:entering ...........");
13993f542974SPaul B Schroeder 
1400441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
140184fe6e79STony Cook 		dbg("%s", "Invalid port");
140284fe6e79STony Cook 		dbg("%s", " mos7840_write_room:leaving ...........");
14033f542974SPaul B Schroeder 		return -1;
14043f542974SPaul B Schroeder 	}
14053f542974SPaul B Schroeder 
14063f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14073f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
140884fe6e79STony Cook 		dbg("%s", "mos7840_break:leaving ...........");
14093f542974SPaul B Schroeder 		return -1;
14103f542974SPaul B Schroeder 	}
14113f542974SPaul B Schroeder 
14120de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
14133f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
1414880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
14153f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
14163f542974SPaul B Schroeder 	}
14170de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
14183f542974SPaul B Schroeder 
14190de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1420441b62c1SHarvey Harrison 	dbg("%s - returns %d", __func__, room);
14210de9a702SOliver Neukum 	return room;
14223f542974SPaul B Schroeder 
14233f542974SPaul B Schroeder }
14243f542974SPaul B Schroeder 
14253f542974SPaul B Schroeder /*****************************************************************************
14263f542974SPaul B Schroeder  * mos7840_write
14273f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
14283f542974SPaul B Schroeder  *	the port.
14293f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
14303f542974SPaul B Schroeder  *      return a negative error number.
14313f542974SPaul B Schroeder  *****************************************************************************/
14323f542974SPaul B Schroeder 
143395da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
14343f542974SPaul B Schroeder 			 const unsigned char *data, int count)
14353f542974SPaul B Schroeder {
14363f542974SPaul B Schroeder 	int status;
14373f542974SPaul B Schroeder 	int i;
14383f542974SPaul B Schroeder 	int bytes_sent = 0;
14393f542974SPaul B Schroeder 	int transfer_size;
14400de9a702SOliver Neukum 	unsigned long flags;
14413f542974SPaul B Schroeder 
14423f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14433f542974SPaul B Schroeder 	struct usb_serial *serial;
14443f542974SPaul B Schroeder 	struct urb *urb;
1445880af9dbSAlan Cox 	/* __u16 Data; */
14463f542974SPaul B Schroeder 	const unsigned char *current_position = data;
14473f542974SPaul B Schroeder 	unsigned char *data1;
144884fe6e79STony Cook 	dbg("%s", "entering ...........");
144984fe6e79STony Cook 	/* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1450880af9dbSAlan Cox 					mos7840_port->shadowLCR); */
14513f542974SPaul B Schroeder 
14523f542974SPaul B Schroeder #ifdef NOTMOS7840
14533f542974SPaul B Schroeder 	Data = 0x00;
14543f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
14553f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
145684fe6e79STony Cook 	dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
145784fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
14583f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14593f542974SPaul B Schroeder 
1460880af9dbSAlan Cox 	/* Data = 0x03; */
1461880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1462880af9dbSAlan Cox 	/* mos7840_port->shadowLCR=Data;//Need to add later */
14633f542974SPaul B Schroeder 
1464880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
14653f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14663f542974SPaul B Schroeder 
1467880af9dbSAlan Cox 	/* Data = 0x0c; */
1468880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
14693f542974SPaul B Schroeder 	Data = 0x00;
14703f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
147184fe6e79STony Cook 	dbg("mos7840_write:DLL value is %x", Data);
14723f542974SPaul B Schroeder 
14733f542974SPaul B Schroeder 	Data = 0x0;
14743f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
147584fe6e79STony Cook 	dbg("mos7840_write:DLM value is %x", Data);
14763f542974SPaul B Schroeder 
14773f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
147884fe6e79STony Cook 	dbg("mos7840_write: mos7840_port->shadowLCR is %x",
14793f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
14803f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14813f542974SPaul B Schroeder #endif
14823f542974SPaul B Schroeder 
1483441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
148484fe6e79STony Cook 		dbg("%s", "Port Paranoia failed");
14853f542974SPaul B Schroeder 		return -1;
14863f542974SPaul B Schroeder 	}
14873f542974SPaul B Schroeder 
14883f542974SPaul B Schroeder 	serial = port->serial;
1489441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
149084fe6e79STony Cook 		dbg("%s", "Serial Paranoia failed");
14913f542974SPaul B Schroeder 		return -1;
14923f542974SPaul B Schroeder 	}
14933f542974SPaul B Schroeder 
14943f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14953f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
149684fe6e79STony Cook 		dbg("%s", "mos7840_port is NULL");
14973f542974SPaul B Schroeder 		return -1;
14983f542974SPaul B Schroeder 	}
14993f542974SPaul B Schroeder 
15003f542974SPaul B Schroeder 	/* try to find a free urb in the list */
15013f542974SPaul B Schroeder 	urb = NULL;
15023f542974SPaul B Schroeder 
15030de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
15043f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
15050de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
15060de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
15073f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
150884fe6e79STony Cook 			dbg("URB:%d", i);
15093f542974SPaul B Schroeder 			break;
15103f542974SPaul B Schroeder 		}
15113f542974SPaul B Schroeder 	}
15120de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
15133f542974SPaul B Schroeder 
15143f542974SPaul B Schroeder 	if (urb == NULL) {
1515441b62c1SHarvey Harrison 		dbg("%s - no more free urbs", __func__);
15163f542974SPaul B Schroeder 		goto exit;
15173f542974SPaul B Schroeder 	}
15183f542974SPaul B Schroeder 
15193f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
15203f542974SPaul B Schroeder 		urb->transfer_buffer =
15213f542974SPaul B Schroeder 		    kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
15223f542974SPaul B Schroeder 
15233f542974SPaul B Schroeder 		if (urb->transfer_buffer == NULL) {
152422a416c4SJohan Hovold 			dev_err_console(port, "%s no more kernel memory...\n",
1525194343d9SGreg Kroah-Hartman 				__func__);
15263f542974SPaul B Schroeder 			goto exit;
15273f542974SPaul B Schroeder 		}
15283f542974SPaul B Schroeder 	}
15293f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
15303f542974SPaul B Schroeder 
15313f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
15323f542974SPaul B Schroeder 
15333f542974SPaul B Schroeder 	/* fill urb with data and submit  */
1534*093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1535*093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1536*093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1537*093ea2d3SDonald Lee 		usb_fill_bulk_urb(urb,
1538*093ea2d3SDonald Lee 			serial->dev,
1539*093ea2d3SDonald Lee 			usb_sndbulkpipe(serial->dev,
1540*093ea2d3SDonald Lee 				(port->bulk_out_endpointAddress) + 2),
1541*093ea2d3SDonald Lee 			urb->transfer_buffer,
1542*093ea2d3SDonald Lee 			transfer_size,
1543*093ea2d3SDonald Lee 			mos7840_bulk_out_data_callback, mos7840_port);
1544*093ea2d3SDonald Lee 	} else {
15453f542974SPaul B Schroeder 		usb_fill_bulk_urb(urb,
15463f542974SPaul B Schroeder 			serial->dev,
15473f542974SPaul B Schroeder 			usb_sndbulkpipe(serial->dev,
15483f542974SPaul B Schroeder 				port->bulk_out_endpointAddress),
15493f542974SPaul B Schroeder 			urb->transfer_buffer,
15503f542974SPaul B Schroeder 			transfer_size,
15513f542974SPaul B Schroeder 			mos7840_bulk_out_data_callback, mos7840_port);
1552*093ea2d3SDonald Lee 	}
15533f542974SPaul B Schroeder 
15543f542974SPaul B Schroeder 	data1 = urb->transfer_buffer;
155584fe6e79STony Cook 	dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
15563f542974SPaul B Schroeder 
15573f542974SPaul B Schroeder 	/* send it down the pipe */
15583f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
15593f542974SPaul B Schroeder 
15603f542974SPaul B Schroeder 	if (status) {
15610de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
156222a416c4SJohan Hovold 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1563194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
15643f542974SPaul B Schroeder 		bytes_sent = status;
15653f542974SPaul B Schroeder 		goto exit;
15663f542974SPaul B Schroeder 	}
15673f542974SPaul B Schroeder 	bytes_sent = transfer_size;
15683f542974SPaul B Schroeder 	mos7840_port->icount.tx += transfer_size;
15690de9a702SOliver Neukum 	smp_wmb();
157084fe6e79STony Cook 	dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
15713f542974SPaul B Schroeder exit:
15723f542974SPaul B Schroeder 	return bytes_sent;
15733f542974SPaul B Schroeder 
15743f542974SPaul B Schroeder }
15753f542974SPaul B Schroeder 
15763f542974SPaul B Schroeder /*****************************************************************************
15773f542974SPaul B Schroeder  * mos7840_throttle
15783f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
15793f542974SPaul B Schroeder  *	being read from the port.
15803f542974SPaul B Schroeder  *****************************************************************************/
15813f542974SPaul B Schroeder 
158295da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
15833f542974SPaul B Schroeder {
158495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
15853f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15863f542974SPaul B Schroeder 	int status;
15873f542974SPaul B Schroeder 
1588441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
158984fe6e79STony Cook 		dbg("%s", "Invalid port");
15903f542974SPaul B Schroeder 		return;
15913f542974SPaul B Schroeder 	}
15923f542974SPaul B Schroeder 
159384fe6e79STony Cook 	dbg("- port %d", port->number);
15943f542974SPaul B Schroeder 
15953f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15963f542974SPaul B Schroeder 
15973f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15983f542974SPaul B Schroeder 		return;
15993f542974SPaul B Schroeder 
16003f542974SPaul B Schroeder 	if (!mos7840_port->open) {
160184fe6e79STony Cook 		dbg("%s", "port not opened");
16023f542974SPaul B Schroeder 		return;
16033f542974SPaul B Schroeder 	}
16043f542974SPaul B Schroeder 
160584fe6e79STony Cook 	dbg("%s", "Entering ..........");
16063f542974SPaul B Schroeder 
16073f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
16083f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
16093f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
161095da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
161195da310eSAlan Cox 		if (status <= 0)
16123f542974SPaul B Schroeder 			return;
16133f542974SPaul B Schroeder 	}
16143f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
16153f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
16163f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
161795da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
16183f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
161995da310eSAlan Cox 		if (status < 0)
16203f542974SPaul B Schroeder 			return;
16213f542974SPaul B Schroeder 	}
16223f542974SPaul B Schroeder }
16233f542974SPaul B Schroeder 
16243f542974SPaul B Schroeder /*****************************************************************************
16253f542974SPaul B Schroeder  * mos7840_unthrottle
1626880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
1627880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
1628880af9dbSAlan Cox  *	called)
16293f542974SPaul B Schroeder  *****************************************************************************/
163095da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
16313f542974SPaul B Schroeder {
163295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16333f542974SPaul B Schroeder 	int status;
16343f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
16353f542974SPaul B Schroeder 
1636441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
163784fe6e79STony Cook 		dbg("%s", "Invalid port");
16383f542974SPaul B Schroeder 		return;
16393f542974SPaul B Schroeder 	}
16403f542974SPaul B Schroeder 
16413f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16423f542974SPaul B Schroeder 		return;
16433f542974SPaul B Schroeder 
16443f542974SPaul B Schroeder 	if (!mos7840_port->open) {
1645441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
16463f542974SPaul B Schroeder 		return;
16473f542974SPaul B Schroeder 	}
16483f542974SPaul B Schroeder 
164984fe6e79STony Cook 	dbg("%s", "Entering ..........");
16503f542974SPaul B Schroeder 
16513f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
16523f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
16533f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
165495da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
165595da310eSAlan Cox 		if (status <= 0)
16563f542974SPaul B Schroeder 			return;
16573f542974SPaul B Schroeder 	}
16583f542974SPaul B Schroeder 
16593f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
16603f542974SPaul B Schroeder 	if (tty->termios->c_cflag & CRTSCTS) {
16613f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
166295da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
16633f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
166495da310eSAlan Cox 		if (status < 0)
16653f542974SPaul B Schroeder 			return;
16663f542974SPaul B Schroeder 	}
16673f542974SPaul B Schroeder }
16683f542974SPaul B Schroeder 
166960b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
16703f542974SPaul B Schroeder {
167195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16723f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
16733f542974SPaul B Schroeder 	unsigned int result;
16743f542974SPaul B Schroeder 	__u16 msr;
16753f542974SPaul B Schroeder 	__u16 mcr;
1676880af9dbSAlan Cox 	int status;
16773f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
16783f542974SPaul B Schroeder 
1679441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
16803f542974SPaul B Schroeder 
16813f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16823f542974SPaul B Schroeder 		return -ENODEV;
16833f542974SPaul B Schroeder 
16843f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
16853f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
16863f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
16873f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
16883f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
16893f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
16903f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
16913f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
16923f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
16933f542974SPaul B Schroeder 
1694441b62c1SHarvey Harrison 	dbg("%s - 0x%04X", __func__, result);
16953f542974SPaul B Schroeder 
16963f542974SPaul B Schroeder 	return result;
16973f542974SPaul B Schroeder }
16983f542974SPaul B Schroeder 
169920b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
17003f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
17013f542974SPaul B Schroeder {
170295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
17033f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
17043f542974SPaul B Schroeder 	unsigned int mcr;
170587521c46SRoel Kluin 	int status;
17063f542974SPaul B Schroeder 
1707441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
17083f542974SPaul B Schroeder 
17093f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
17103f542974SPaul B Schroeder 
17113f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17123f542974SPaul B Schroeder 		return -ENODEV;
17133f542974SPaul B Schroeder 
1714e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
17153f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
17163f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
17173f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
17183f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
17193f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
17203f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
17213f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
17223f542974SPaul B Schroeder 
17233f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
17243f542974SPaul B Schroeder 		mcr |= MCR_RTS;
17253f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
17263f542974SPaul B Schroeder 		mcr |= MCR_DTR;
17273f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
17283f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
17293f542974SPaul B Schroeder 
17303f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
17313f542974SPaul B Schroeder 
17323f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
17333f542974SPaul B Schroeder 	if (status < 0) {
173484fe6e79STony Cook 		dbg("setting MODEM_CONTROL_REGISTER Failed");
173587521c46SRoel Kluin 		return status;
17363f542974SPaul B Schroeder 	}
17373f542974SPaul B Schroeder 
17383f542974SPaul B Schroeder 	return 0;
17393f542974SPaul B Schroeder }
17403f542974SPaul B Schroeder 
17413f542974SPaul B Schroeder /*****************************************************************************
17423f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
17433f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
17443f542974SPaul B Schroeder  *	baud rate.
17453f542974SPaul B Schroeder  *****************************************************************************/
17463f542974SPaul B Schroeder static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
17473f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
17483f542974SPaul B Schroeder {
17493f542974SPaul B Schroeder 
1750441b62c1SHarvey Harrison 	dbg("%s - %d", __func__, baudRate);
17513f542974SPaul B Schroeder 
17523f542974SPaul B Schroeder 	if (baudRate <= 115200) {
17533f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
17543f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
17553f542974SPaul B Schroeder 	}
17563f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
17573f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
17583f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
17593f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
17603f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
17613f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
17623f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
17633f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
17643f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
17653f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
17663f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
17673f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
17683f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
17693f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
17703f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
17713f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
17723f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
17733f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
17743f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
17753f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
17763f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
17773f542974SPaul B Schroeder 	}
17783f542974SPaul B Schroeder 	return 0;
17793f542974SPaul B Schroeder 
17803f542974SPaul B Schroeder #ifdef NOTMCS7840
17813f542974SPaul B Schroeder 
17823f542974SPaul B Schroeder 	for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
17833f542974SPaul B Schroeder 		if (mos7840_divisor_table[i].BaudRate == baudrate) {
17843f542974SPaul B Schroeder 			*divisor = mos7840_divisor_table[i].Divisor;
17853f542974SPaul B Schroeder 			return 0;
17863f542974SPaul B Schroeder 		}
17873f542974SPaul B Schroeder 	}
17883f542974SPaul B Schroeder 
17893f542974SPaul B Schroeder 	/* After trying for all the standard baud rates    *
17903f542974SPaul B Schroeder 	 * Try calculating the divisor for this baud rate  */
17913f542974SPaul B Schroeder 
17923f542974SPaul B Schroeder 	if (baudrate > 75 && baudrate < 230400) {
17933f542974SPaul B Schroeder 		/* get the divisor */
17943f542974SPaul B Schroeder 		custom = (__u16) (230400L / baudrate);
17953f542974SPaul B Schroeder 
17963f542974SPaul B Schroeder 		/* Check for round off */
17973f542974SPaul B Schroeder 		round1 = (__u16) (2304000L / baudrate);
17983f542974SPaul B Schroeder 		round = (__u16) (round1 - (custom * 10));
1799880af9dbSAlan Cox 		if (round > 4)
18003f542974SPaul B Schroeder 			custom++;
18013f542974SPaul B Schroeder 		*divisor = custom;
18023f542974SPaul B Schroeder 
180384fe6e79STony Cook 		dbg(" Baud %d = %d", baudrate, custom);
18043f542974SPaul B Schroeder 		return 0;
18053f542974SPaul B Schroeder 	}
18063f542974SPaul B Schroeder 
180784fe6e79STony Cook 	dbg("%s", " Baud calculation Failed...");
18083f542974SPaul B Schroeder 	return -1;
18093f542974SPaul B Schroeder #endif
18103f542974SPaul B Schroeder }
18113f542974SPaul B Schroeder 
18123f542974SPaul B Schroeder /*****************************************************************************
18133f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
18143f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
18153f542974SPaul B Schroeder  *	specified port.
18163f542974SPaul B Schroeder  *****************************************************************************/
18173f542974SPaul B Schroeder 
18183f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
18193f542974SPaul B Schroeder 					    int baudRate)
18203f542974SPaul B Schroeder {
18213f542974SPaul B Schroeder 	int divisor = 0;
18223f542974SPaul B Schroeder 	int status;
18233f542974SPaul B Schroeder 	__u16 Data;
18243f542974SPaul B Schroeder 	unsigned char number;
18253f542974SPaul B Schroeder 	__u16 clk_sel_val;
18263f542974SPaul B Schroeder 	struct usb_serial_port *port;
18273f542974SPaul B Schroeder 
18283f542974SPaul B Schroeder 	if (mos7840_port == NULL)
18293f542974SPaul B Schroeder 		return -1;
18303f542974SPaul B Schroeder 
18313f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
1832441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
183384fe6e79STony Cook 		dbg("%s", "Invalid port");
18343f542974SPaul B Schroeder 		return -1;
18353f542974SPaul B Schroeder 	}
18363f542974SPaul B Schroeder 
1837441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
183884fe6e79STony Cook 		dbg("%s", "Invalid Serial");
18393f542974SPaul B Schroeder 		return -1;
18403f542974SPaul B Schroeder 	}
18413f542974SPaul B Schroeder 
184284fe6e79STony Cook 	dbg("%s", "Entering ..........");
18433f542974SPaul B Schroeder 
18443f542974SPaul B Schroeder 	number = mos7840_port->port->number - mos7840_port->port->serial->minor;
18453f542974SPaul B Schroeder 
1846441b62c1SHarvey Harrison 	dbg("%s - port = %d, baud = %d", __func__,
18473f542974SPaul B Schroeder 	    mos7840_port->port->number, baudRate);
1848880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
18493f542974SPaul B Schroeder 	if (baudRate > 115200) {
18503f542974SPaul B Schroeder #ifdef HW_flow_control
1851880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1852880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
18533f542974SPaul B Schroeder 		Data = 0x2b;
18543f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1855880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1856880af9dbSAlan Cox 									Data);
18573f542974SPaul B Schroeder 		if (status < 0) {
185884fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
18593f542974SPaul B Schroeder 			return -1;
18603f542974SPaul B Schroeder 		}
18613f542974SPaul B Schroeder #endif
18623f542974SPaul B Schroeder 
18633f542974SPaul B Schroeder 	} else {
18643f542974SPaul B Schroeder #ifdef HW_flow_control
1865880af9dbSAlan Cox 		/* setting h/w flow control bit to 0 */
18663f542974SPaul B Schroeder 		Data = 0xb;
18673f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1868880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1869880af9dbSAlan Cox 									Data);
18703f542974SPaul B Schroeder 		if (status < 0) {
187184fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
18723f542974SPaul B Schroeder 			return -1;
18733f542974SPaul B Schroeder 		}
18743f542974SPaul B Schroeder #endif
18753f542974SPaul B Schroeder 
18763f542974SPaul B Schroeder 	}
18773f542974SPaul B Schroeder 
1878880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
18793f542974SPaul B Schroeder 		clk_sel_val = 0x0;
18803f542974SPaul B Schroeder 		Data = 0x0;
1881880af9dbSAlan Cox 		status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
18823f542974SPaul B Schroeder 						   &clk_sel_val);
1883880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
18843f542974SPaul B Schroeder 								 &Data);
18853f542974SPaul B Schroeder 		if (status < 0) {
188684fe6e79STony Cook 			dbg("reading spreg failed in set_serial_baud");
18873f542974SPaul B Schroeder 			return -1;
18883f542974SPaul B Schroeder 		}
18893f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1890880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1891880af9dbSAlan Cox 								Data);
18923f542974SPaul B Schroeder 		if (status < 0) {
189384fe6e79STony Cook 			dbg("Writing spreg failed in set_serial_baud");
18943f542974SPaul B Schroeder 			return -1;
18953f542974SPaul B Schroeder 		}
18963f542974SPaul B Schroeder 		/* Calculate the Divisor */
18973f542974SPaul B Schroeder 
18983f542974SPaul B Schroeder 		if (status) {
1899194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
19003f542974SPaul B Schroeder 			return status;
19013f542974SPaul B Schroeder 		}
19023f542974SPaul B Schroeder 		/* Enable access to divisor latch */
19033f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
19043f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
19053f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19063f542974SPaul B Schroeder 
19073f542974SPaul B Schroeder 		/* Write the divisor */
19083f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
190984fe6e79STony Cook 		dbg("set_serial_baud Value to write DLL is %x", Data);
19103f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
19113f542974SPaul B Schroeder 
19123f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
191384fe6e79STony Cook 		dbg("set_serial_baud Value to write DLM is %x", Data);
19143f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
19153f542974SPaul B Schroeder 
19163f542974SPaul B Schroeder 		/* Disable access to divisor latch */
19173f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
19183f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
19193f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19203f542974SPaul B Schroeder 
19213f542974SPaul B Schroeder 	}
19223f542974SPaul B Schroeder 	return status;
19233f542974SPaul B Schroeder }
19243f542974SPaul B Schroeder 
19253f542974SPaul B Schroeder /*****************************************************************************
19263f542974SPaul B Schroeder  * mos7840_change_port_settings
19273f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
19283f542974SPaul B Schroeder  *      the specified new settings.
19293f542974SPaul B Schroeder  *****************************************************************************/
19303f542974SPaul B Schroeder 
193195da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
193295da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
19333f542974SPaul B Schroeder {
19343f542974SPaul B Schroeder 	int baud;
19353f542974SPaul B Schroeder 	unsigned cflag;
19363f542974SPaul B Schroeder 	unsigned iflag;
19373f542974SPaul B Schroeder 	__u8 lData;
19383f542974SPaul B Schroeder 	__u8 lParity;
19393f542974SPaul B Schroeder 	__u8 lStop;
19403f542974SPaul B Schroeder 	int status;
19413f542974SPaul B Schroeder 	__u16 Data;
19423f542974SPaul B Schroeder 	struct usb_serial_port *port;
19433f542974SPaul B Schroeder 	struct usb_serial *serial;
19443f542974SPaul B Schroeder 
19453f542974SPaul B Schroeder 	if (mos7840_port == NULL)
19463f542974SPaul B Schroeder 		return;
19473f542974SPaul B Schroeder 
19483f542974SPaul B Schroeder 	port = (struct usb_serial_port *)mos7840_port->port;
19493f542974SPaul B Schroeder 
1950441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
195184fe6e79STony Cook 		dbg("%s", "Invalid port");
19523f542974SPaul B Schroeder 		return;
19533f542974SPaul B Schroeder 	}
19543f542974SPaul B Schroeder 
1955441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(port->serial, __func__)) {
195684fe6e79STony Cook 		dbg("%s", "Invalid Serial");
19573f542974SPaul B Schroeder 		return;
19583f542974SPaul B Schroeder 	}
19593f542974SPaul B Schroeder 
19603f542974SPaul B Schroeder 	serial = port->serial;
19613f542974SPaul B Schroeder 
1962441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, mos7840_port->port->number);
19633f542974SPaul B Schroeder 
19643f542974SPaul B Schroeder 	if (!mos7840_port->open) {
1965441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
19663f542974SPaul B Schroeder 		return;
19673f542974SPaul B Schroeder 	}
19683f542974SPaul B Schroeder 
196984fe6e79STony Cook 	dbg("%s", "Entering ..........");
19703f542974SPaul B Schroeder 
19713f542974SPaul B Schroeder 	lData = LCR_BITS_8;
19723f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
19733f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
19743f542974SPaul B Schroeder 
19753f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
19763f542974SPaul B Schroeder 	iflag = tty->termios->c_iflag;
19773f542974SPaul B Schroeder 
19783f542974SPaul B Schroeder 	/* Change the number of bits */
19793f542974SPaul B Schroeder 	if (cflag & CSIZE) {
19803f542974SPaul B Schroeder 		switch (cflag & CSIZE) {
19813f542974SPaul B Schroeder 		case CS5:
19823f542974SPaul B Schroeder 			lData = LCR_BITS_5;
19833f542974SPaul B Schroeder 			break;
19843f542974SPaul B Schroeder 
19853f542974SPaul B Schroeder 		case CS6:
19863f542974SPaul B Schroeder 			lData = LCR_BITS_6;
19873f542974SPaul B Schroeder 			break;
19883f542974SPaul B Schroeder 
19893f542974SPaul B Schroeder 		case CS7:
19903f542974SPaul B Schroeder 			lData = LCR_BITS_7;
19913f542974SPaul B Schroeder 			break;
19923f542974SPaul B Schroeder 		default:
19933f542974SPaul B Schroeder 		case CS8:
19943f542974SPaul B Schroeder 			lData = LCR_BITS_8;
19953f542974SPaul B Schroeder 			break;
19963f542974SPaul B Schroeder 		}
19973f542974SPaul B Schroeder 	}
19983f542974SPaul B Schroeder 	/* Change the Parity bit */
19993f542974SPaul B Schroeder 	if (cflag & PARENB) {
20003f542974SPaul B Schroeder 		if (cflag & PARODD) {
20013f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
2002441b62c1SHarvey Harrison 			dbg("%s - parity = odd", __func__);
20033f542974SPaul B Schroeder 		} else {
20043f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
2005441b62c1SHarvey Harrison 			dbg("%s - parity = even", __func__);
20063f542974SPaul B Schroeder 		}
20073f542974SPaul B Schroeder 
20083f542974SPaul B Schroeder 	} else {
2009441b62c1SHarvey Harrison 		dbg("%s - parity = none", __func__);
20103f542974SPaul B Schroeder 	}
20113f542974SPaul B Schroeder 
2012880af9dbSAlan Cox 	if (cflag & CMSPAR)
20133f542974SPaul B Schroeder 		lParity = lParity | 0x20;
20143f542974SPaul B Schroeder 
20153f542974SPaul B Schroeder 	/* Change the Stop bit */
20163f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
20173f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
2018441b62c1SHarvey Harrison 		dbg("%s - stop bits = 2", __func__);
20193f542974SPaul B Schroeder 	} else {
20203f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
2021441b62c1SHarvey Harrison 		dbg("%s - stop bits = 1", __func__);
20223f542974SPaul B Schroeder 	}
20233f542974SPaul B Schroeder 
20243f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
20253f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
20263f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
20273f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
20283f542974SPaul B Schroeder 
202984fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
20303f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
20313f542974SPaul B Schroeder 	/* Disable Interrupts */
20323f542974SPaul B Schroeder 	Data = 0x00;
20333f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
20343f542974SPaul B Schroeder 
20353f542974SPaul B Schroeder 	Data = 0x00;
20363f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
20373f542974SPaul B Schroeder 
20383f542974SPaul B Schroeder 	Data = 0xcf;
20393f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
20403f542974SPaul B Schroeder 
20413f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
20423f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
20433f542974SPaul B Schroeder 
20443f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
20453f542974SPaul B Schroeder 
20463f542974SPaul B Schroeder 	Data = 0x00b;
20473f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
20483f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20493f542974SPaul B Schroeder 	Data = 0x00b;
20503f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20513f542974SPaul B Schroeder 
20523f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
20533f542974SPaul B Schroeder 
20543f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
2055880af9dbSAlan Cox 	if (cflag & CBAUD)
20563f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
20573f542974SPaul B Schroeder 
2058880af9dbSAlan Cox 	if (cflag & CRTSCTS)
20593f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
2060880af9dbSAlan Cox 	else
20613f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
20623f542974SPaul B Schroeder 
20633f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
20643f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
20653f542974SPaul B Schroeder 
20663f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
20673f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
20683f542974SPaul B Schroeder 
20693f542974SPaul B Schroeder 	if (!baud) {
20703f542974SPaul B Schroeder 		/* pick a default, any default... */
207184fe6e79STony Cook 		dbg("%s", "Picked default baud...");
20723f542974SPaul B Schroeder 		baud = 9600;
20733f542974SPaul B Schroeder 	}
20743f542974SPaul B Schroeder 
2075441b62c1SHarvey Harrison 	dbg("%s - baud rate = %d", __func__, baud);
20763f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
20773f542974SPaul B Schroeder 
20783f542974SPaul B Schroeder 	/* Enable Interrupts */
20793f542974SPaul B Schroeder 	Data = 0x0c;
20803f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
20813f542974SPaul B Schroeder 
208250de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
208350de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
20843f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
20853f542974SPaul B Schroeder 		if (status) {
20863f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
20873f542974SPaul B Schroeder 			    status);
208850de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
20893f542974SPaul B Schroeder 		}
20903f542974SPaul B Schroeder 	}
20913f542974SPaul B Schroeder 	wake_up(&mos7840_port->delta_msr_wait);
20923f542974SPaul B Schroeder 	mos7840_port->delta_msr_cond = 1;
209384fe6e79STony Cook 	dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
20943f542974SPaul B Schroeder 	    mos7840_port->shadowLCR);
20953f542974SPaul B Schroeder }
20963f542974SPaul B Schroeder 
20973f542974SPaul B Schroeder /*****************************************************************************
20983f542974SPaul B Schroeder  * mos7840_set_termios
20993f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
21003f542974SPaul B Schroeder  *	the termios structure
21013f542974SPaul B Schroeder  *****************************************************************************/
21023f542974SPaul B Schroeder 
210395da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
210495da310eSAlan Cox 				struct usb_serial_port *port,
2105606d099cSAlan Cox 				struct ktermios *old_termios)
21063f542974SPaul B Schroeder {
21073f542974SPaul B Schroeder 	int status;
21083f542974SPaul B Schroeder 	unsigned int cflag;
21093f542974SPaul B Schroeder 	struct usb_serial *serial;
21103f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
211184fe6e79STony Cook 	dbg("mos7840_set_termios: START");
2112441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
211384fe6e79STony Cook 		dbg("%s", "Invalid port");
21143f542974SPaul B Schroeder 		return;
21153f542974SPaul B Schroeder 	}
21163f542974SPaul B Schroeder 
21173f542974SPaul B Schroeder 	serial = port->serial;
21183f542974SPaul B Schroeder 
2119441b62c1SHarvey Harrison 	if (mos7840_serial_paranoia_check(serial, __func__)) {
212084fe6e79STony Cook 		dbg("%s", "Invalid Serial");
21213f542974SPaul B Schroeder 		return;
21223f542974SPaul B Schroeder 	}
21233f542974SPaul B Schroeder 
21243f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
21253f542974SPaul B Schroeder 
21263f542974SPaul B Schroeder 	if (mos7840_port == NULL)
21273f542974SPaul B Schroeder 		return;
21283f542974SPaul B Schroeder 
21293f542974SPaul B Schroeder 	if (!mos7840_port->open) {
2130441b62c1SHarvey Harrison 		dbg("%s - port not opened", __func__);
21313f542974SPaul B Schroeder 		return;
21323f542974SPaul B Schroeder 	}
21333f542974SPaul B Schroeder 
213484fe6e79STony Cook 	dbg("%s", "setting termios - ");
21353f542974SPaul B Schroeder 
21363f542974SPaul B Schroeder 	cflag = tty->termios->c_cflag;
21373f542974SPaul B Schroeder 
2138441b62c1SHarvey Harrison 	dbg("%s - clfag %08x iflag %08x", __func__,
21393f542974SPaul B Schroeder 	    tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2140441b62c1SHarvey Harrison 	dbg("%s - old clfag %08x old iflag %08x", __func__,
21413f542974SPaul B Schroeder 	    old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2142441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
21433f542974SPaul B Schroeder 
21443f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
21453f542974SPaul B Schroeder 
214695da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
21473f542974SPaul B Schroeder 
21483f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
214984fe6e79STony Cook 		dbg("%s", "URB KILLED !!!!!");
21503f542974SPaul B Schroeder 		return;
21513f542974SPaul B Schroeder 	}
21523f542974SPaul B Schroeder 
215350de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
215450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
21553f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
21563f542974SPaul B Schroeder 		if (status) {
21573f542974SPaul B Schroeder 			dbg("usb_submit_urb(read bulk) failed, status = %d",
21583f542974SPaul B Schroeder 			    status);
215950de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
21603f542974SPaul B Schroeder 		}
21613f542974SPaul B Schroeder 	}
21623f542974SPaul B Schroeder }
21633f542974SPaul B Schroeder 
21643f542974SPaul B Schroeder /*****************************************************************************
21653f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
21663f542974SPaul B Schroeder  *
21673f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
21683f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
21693f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
21703f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
21713f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
21723f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
21733f542974SPaul B Schroeder  *****************************************************************************/
21743f542974SPaul B Schroeder 
217595da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
217697c4965dSAl Viro 				unsigned int __user *value)
21773f542974SPaul B Schroeder {
21783f542974SPaul B Schroeder 	int count;
21793f542974SPaul B Schroeder 	unsigned int result = 0;
21803f542974SPaul B Schroeder 
218195da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
21823f542974SPaul B Schroeder 	if (count == 0) {
2183441b62c1SHarvey Harrison 		dbg("%s -- Empty", __func__);
21843f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
21853f542974SPaul B Schroeder 	}
21863f542974SPaul B Schroeder 
21873f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
21883f542974SPaul B Schroeder 		return -EFAULT;
21893f542974SPaul B Schroeder 	return 0;
21903f542974SPaul B Schroeder }
21913f542974SPaul B Schroeder 
21923f542974SPaul B Schroeder /*****************************************************************************
21933f542974SPaul B Schroeder  * mos7840_get_serial_info
21943f542974SPaul B Schroeder  *      function to get information about serial port
21953f542974SPaul B Schroeder  *****************************************************************************/
21963f542974SPaul B Schroeder 
21973f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
219897c4965dSAl Viro 				   struct serial_struct __user *retinfo)
21993f542974SPaul B Schroeder {
22003f542974SPaul B Schroeder 	struct serial_struct tmp;
22013f542974SPaul B Schroeder 
22023f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22033f542974SPaul B Schroeder 		return -1;
22043f542974SPaul B Schroeder 
22053f542974SPaul B Schroeder 	if (!retinfo)
22063f542974SPaul B Schroeder 		return -EFAULT;
22073f542974SPaul B Schroeder 
22083f542974SPaul B Schroeder 	memset(&tmp, 0, sizeof(tmp));
22093f542974SPaul B Schroeder 
22103f542974SPaul B Schroeder 	tmp.type = PORT_16550A;
22113f542974SPaul B Schroeder 	tmp.line = mos7840_port->port->serial->minor;
22123f542974SPaul B Schroeder 	tmp.port = mos7840_port->port->number;
22133f542974SPaul B Schroeder 	tmp.irq = 0;
22143f542974SPaul B Schroeder 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
22153f542974SPaul B Schroeder 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
22163f542974SPaul B Schroeder 	tmp.baud_base = 9600;
22173f542974SPaul B Schroeder 	tmp.close_delay = 5 * HZ;
22183f542974SPaul B Schroeder 	tmp.closing_wait = 30 * HZ;
22193f542974SPaul B Schroeder 
22203f542974SPaul B Schroeder 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
22213f542974SPaul B Schroeder 		return -EFAULT;
22223f542974SPaul B Schroeder 	return 0;
22233f542974SPaul B Schroeder }
22243f542974SPaul B Schroeder 
22250bca1b91SAlan Cox static int mos7840_get_icount(struct tty_struct *tty,
22260bca1b91SAlan Cox 			struct serial_icounter_struct *icount)
22270bca1b91SAlan Cox {
22280bca1b91SAlan Cox 	struct usb_serial_port *port = tty->driver_data;
22290bca1b91SAlan Cox 	struct moschip_port *mos7840_port;
22300bca1b91SAlan Cox 	struct async_icount cnow;
22310bca1b91SAlan Cox 
22320bca1b91SAlan Cox 	mos7840_port = mos7840_get_port_private(port);
22330bca1b91SAlan Cox 	cnow = mos7840_port->icount;
22340bca1b91SAlan Cox 
22350bca1b91SAlan Cox 	smp_rmb();
22360bca1b91SAlan Cox 	icount->cts = cnow.cts;
22370bca1b91SAlan Cox 	icount->dsr = cnow.dsr;
22380bca1b91SAlan Cox 	icount->rng = cnow.rng;
22390bca1b91SAlan Cox 	icount->dcd = cnow.dcd;
22400bca1b91SAlan Cox 	icount->rx = cnow.rx;
22410bca1b91SAlan Cox 	icount->tx = cnow.tx;
22420bca1b91SAlan Cox 	icount->frame = cnow.frame;
22430bca1b91SAlan Cox 	icount->overrun = cnow.overrun;
22440bca1b91SAlan Cox 	icount->parity = cnow.parity;
22450bca1b91SAlan Cox 	icount->brk = cnow.brk;
22460bca1b91SAlan Cox 	icount->buf_overrun = cnow.buf_overrun;
22470bca1b91SAlan Cox 
22480bca1b91SAlan Cox 	dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
22490bca1b91SAlan Cox 		port->number, icount->rx, icount->tx);
22500bca1b91SAlan Cox 	return 0;
22510bca1b91SAlan Cox }
22520bca1b91SAlan Cox 
22533f542974SPaul B Schroeder /*****************************************************************************
22543f542974SPaul B Schroeder  * SerialIoctl
22553f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
22563f542974SPaul B Schroeder  *****************************************************************************/
22573f542974SPaul B Schroeder 
225800a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
22593f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
22603f542974SPaul B Schroeder {
226195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
226297c4965dSAl Viro 	void __user *argp = (void __user *)arg;
22633f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
22643f542974SPaul B Schroeder 
22653f542974SPaul B Schroeder 	struct async_icount cnow;
22663f542974SPaul B Schroeder 	struct async_icount cprev;
22673f542974SPaul B Schroeder 
2268441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
226984fe6e79STony Cook 		dbg("%s", "Invalid port");
22703f542974SPaul B Schroeder 		return -1;
22713f542974SPaul B Schroeder 	}
22723f542974SPaul B Schroeder 
22733f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
22743f542974SPaul B Schroeder 
22753f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22763f542974SPaul B Schroeder 		return -1;
22773f542974SPaul B Schroeder 
2278441b62c1SHarvey Harrison 	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
22793f542974SPaul B Schroeder 
22803f542974SPaul B Schroeder 	switch (cmd) {
22813f542974SPaul B Schroeder 		/* return number of bytes available */
22823f542974SPaul B Schroeder 
22833f542974SPaul B Schroeder 	case TIOCSERGETLSR:
2284441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
228595da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
22863f542974SPaul B Schroeder 
22873f542974SPaul B Schroeder 	case TIOCGSERIAL:
2288441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
228997c4965dSAl Viro 		return mos7840_get_serial_info(mos7840_port, argp);
22903f542974SPaul B Schroeder 
22913f542974SPaul B Schroeder 	case TIOCSSERIAL:
2292441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
22933f542974SPaul B Schroeder 		break;
22943f542974SPaul B Schroeder 
22953f542974SPaul B Schroeder 	case TIOCMIWAIT:
2296441b62c1SHarvey Harrison 		dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
22973f542974SPaul B Schroeder 		cprev = mos7840_port->icount;
22983f542974SPaul B Schroeder 		while (1) {
2299880af9dbSAlan Cox 			/* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
23003f542974SPaul B Schroeder 			mos7840_port->delta_msr_cond = 0;
23013f542974SPaul B Schroeder 			wait_event_interruptible(mos7840_port->delta_msr_wait,
23023f542974SPaul B Schroeder 						 (mos7840_port->
23033f542974SPaul B Schroeder 						  delta_msr_cond == 1));
23043f542974SPaul B Schroeder 
23053f542974SPaul B Schroeder 			/* see if a signal did it */
23063f542974SPaul B Schroeder 			if (signal_pending(current))
23073f542974SPaul B Schroeder 				return -ERESTARTSYS;
23083f542974SPaul B Schroeder 			cnow = mos7840_port->icount;
23090de9a702SOliver Neukum 			smp_rmb();
23103f542974SPaul B Schroeder 			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
23113f542974SPaul B Schroeder 			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
23123f542974SPaul B Schroeder 				return -EIO;	/* no change => error */
23133f542974SPaul B Schroeder 			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
23143f542974SPaul B Schroeder 			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
23153f542974SPaul B Schroeder 			    ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
23163f542974SPaul B Schroeder 			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
23173f542974SPaul B Schroeder 				return 0;
23183f542974SPaul B Schroeder 			}
23193f542974SPaul B Schroeder 			cprev = cnow;
23203f542974SPaul B Schroeder 		}
23213f542974SPaul B Schroeder 		/* NOTREACHED */
23223f542974SPaul B Schroeder 		break;
23233f542974SPaul B Schroeder 
23243f542974SPaul B Schroeder 	default:
23253f542974SPaul B Schroeder 		break;
23263f542974SPaul B Schroeder 	}
23273f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
23283f542974SPaul B Schroeder }
23293f542974SPaul B Schroeder 
23303f542974SPaul B Schroeder static int mos7840_calc_num_ports(struct usb_serial *serial)
23313f542974SPaul B Schroeder {
2332*093ea2d3SDonald Lee 	__u16 Data = 0x00;
2333*093ea2d3SDonald Lee 	int ret = 0;
2334*093ea2d3SDonald Lee 	int mos7840_num_ports;
23353f542974SPaul B Schroeder 
2336*093ea2d3SDonald Lee 	ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2337*093ea2d3SDonald Lee 		MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &Data,
2338*093ea2d3SDonald Lee 		VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2339*093ea2d3SDonald Lee 
2340*093ea2d3SDonald Lee 	if ((Data & 0x01) == 0) {
2341*093ea2d3SDonald Lee 		mos7840_num_ports = 2;
2342*093ea2d3SDonald Lee 		serial->num_bulk_in = 2;
2343*093ea2d3SDonald Lee 		serial->num_bulk_out = 2;
2344*093ea2d3SDonald Lee 		serial->num_ports = 2;
2345*093ea2d3SDonald Lee 	} else {
2346*093ea2d3SDonald Lee 		mos7840_num_ports = 4;
23470de9a702SOliver Neukum 		serial->num_bulk_in = 4;
23480de9a702SOliver Neukum 		serial->num_bulk_out = 4;
2349*093ea2d3SDonald Lee 		serial->num_ports = 4;
23503f542974SPaul B Schroeder 	}
2351*093ea2d3SDonald Lee 
23523f542974SPaul B Schroeder 	return mos7840_num_ports;
23533f542974SPaul B Schroeder }
23543f542974SPaul B Schroeder 
23553f542974SPaul B Schroeder /****************************************************************************
23563f542974SPaul B Schroeder  * mos7840_startup
23573f542974SPaul B Schroeder  ****************************************************************************/
23583f542974SPaul B Schroeder 
23593f542974SPaul B Schroeder static int mos7840_startup(struct usb_serial *serial)
23603f542974SPaul B Schroeder {
23613f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
23623f542974SPaul B Schroeder 	struct usb_device *dev;
23633f542974SPaul B Schroeder 	int i, status;
23643f542974SPaul B Schroeder 
23653f542974SPaul B Schroeder 	__u16 Data;
236684fe6e79STony Cook 	dbg("%s", "mos7840_startup :Entering..........");
23673f542974SPaul B Schroeder 
23683f542974SPaul B Schroeder 	if (!serial) {
236984fe6e79STony Cook 		dbg("%s", "Invalid Handler");
23703f542974SPaul B Schroeder 		return -1;
23713f542974SPaul B Schroeder 	}
23723f542974SPaul B Schroeder 
23733f542974SPaul B Schroeder 	dev = serial->dev;
23743f542974SPaul B Schroeder 
237584fe6e79STony Cook 	dbg("%s", "Entering...");
237684fe6e79STony Cook 	dbg ("mos7840_startup: serial = %p", serial);
23773f542974SPaul B Schroeder 
23783f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
23793f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
23803f542974SPaul B Schroeder 
23813f542974SPaul B Schroeder 	/* set up port private structures */
23823f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
238384fe6e79STony Cook 		dbg ("mos7840_startup: configuring port %d............", i);
23847ac9da10SBurman Yan 		mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
23853f542974SPaul B Schroeder 		if (mos7840_port == NULL) {
2386194343d9SGreg Kroah-Hartman 			dev_err(&dev->dev, "%s - Out of memory\n", __func__);
23870de9a702SOliver Neukum 			status = -ENOMEM;
23880de9a702SOliver Neukum 			i--; /* don't follow NULL pointer cleaning up */
23890de9a702SOliver Neukum 			goto error;
23903f542974SPaul B Schroeder 		}
23913f542974SPaul B Schroeder 
2392880af9dbSAlan Cox 		/* Initialize all port interrupt end point to port 0 int
2393880af9dbSAlan Cox 		 * endpoint. Our device has only one interrupt end point
2394880af9dbSAlan Cox 		 * common to all port */
23953f542974SPaul B Schroeder 
23963f542974SPaul B Schroeder 		mos7840_port->port = serial->port[i];
23973f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
23980de9a702SOliver Neukum 		spin_lock_init(&mos7840_port->pool_lock);
23993f542974SPaul B Schroeder 
240037768adfSTony Cook 		/* minor is not initialised until later by
240137768adfSTony Cook 		 * usb-serial.c:get_free_serial() and cannot therefore be used
240237768adfSTony Cook 		 * to index device instances */
240337768adfSTony Cook 		mos7840_port->port_num = i + 1;
240437768adfSTony Cook 		dbg ("serial->port[i]->number = %d", serial->port[i]->number);
240537768adfSTony Cook 		dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
240637768adfSTony Cook 		dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
240737768adfSTony Cook 		dbg ("serial->minor = %d", serial->minor);
24083f542974SPaul B Schroeder 
24093f542974SPaul B Schroeder 		if (mos7840_port->port_num == 1) {
24103f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x0;
24113f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x1;
24123f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x4;
24133f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
24140de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
24153f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0x8;
24163f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0x9;
24173f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x16;
24183f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 2)
24190de9a702SOliver Neukum 			   && (serial->num_ports == 2)) {
24203f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
24213f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
24223f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
24233f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 3)
24240de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
24253f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xa;
24263f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xb;
24273f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x19;
24283f542974SPaul B Schroeder 		} else if ((mos7840_port->port_num == 4)
24290de9a702SOliver Neukum 			   && (serial->num_ports == 4)) {
24303f542974SPaul B Schroeder 			mos7840_port->SpRegOffset = 0xc;
24313f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset = 0xd;
24323f542974SPaul B Schroeder 			mos7840_port->DcrRegOffset = 0x1c;
24333f542974SPaul B Schroeder 		}
24343f542974SPaul B Schroeder 		mos7840_dump_serial_port(mos7840_port);
24353f542974SPaul B Schroeder 		mos7840_set_port_private(serial->port[i], mos7840_port);
24363f542974SPaul B Schroeder 
2437880af9dbSAlan Cox 		/* enable rx_disable bit in control register */
2438880af9dbSAlan Cox 		status = mos7840_get_reg_sync(serial->port[i],
24393f542974SPaul B Schroeder 				 mos7840_port->ControlRegOffset, &Data);
24403f542974SPaul B Schroeder 		if (status < 0) {
244184fe6e79STony Cook 			dbg("Reading ControlReg failed status-0x%x", status);
24423f542974SPaul B Schroeder 			break;
24433f542974SPaul B Schroeder 		} else
244484fe6e79STony Cook 			dbg("ControlReg Reading success val is %x, status%d",
24453f542974SPaul B Schroeder 			    Data, status);
2446880af9dbSAlan Cox 		Data |= 0x08;	/* setting driver done bit */
2447880af9dbSAlan Cox 		Data |= 0x04;	/* sp1_bit to have cts change reflect in
2448880af9dbSAlan Cox 				   modem status reg */
24493f542974SPaul B Schroeder 
2450880af9dbSAlan Cox 		/* Data |= 0x20; //rx_disable bit */
2451880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
24523f542974SPaul B Schroeder 					 mos7840_port->ControlRegOffset, Data);
24533f542974SPaul B Schroeder 		if (status < 0) {
245484fe6e79STony Cook 			dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
24553f542974SPaul B Schroeder 			break;
24563f542974SPaul B Schroeder 		} else
245784fe6e79STony Cook 			dbg("ControlReg Writing success(rx_disable) status%d",
24583f542974SPaul B Schroeder 			    status);
24593f542974SPaul B Schroeder 
2460880af9dbSAlan Cox 		/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2461880af9dbSAlan Cox 		   and 0x24 in DCR3 */
24623f542974SPaul B Schroeder 		Data = 0x01;
2463880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2464880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
24653f542974SPaul B Schroeder 		if (status < 0) {
246684fe6e79STony Cook 			dbg("Writing DCR0 failed status-0x%x", status);
24673f542974SPaul B Schroeder 			break;
24683f542974SPaul B Schroeder 		} else
246984fe6e79STony Cook 			dbg("DCR0 Writing success status%d", status);
24703f542974SPaul B Schroeder 
24713f542974SPaul B Schroeder 		Data = 0x05;
2472880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2473880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
24743f542974SPaul B Schroeder 		if (status < 0) {
247584fe6e79STony Cook 			dbg("Writing DCR1 failed status-0x%x", status);
24763f542974SPaul B Schroeder 			break;
24773f542974SPaul B Schroeder 		} else
247884fe6e79STony Cook 			dbg("DCR1 Writing success status%d", status);
24793f542974SPaul B Schroeder 
24803f542974SPaul B Schroeder 		Data = 0x24;
2481880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2482880af9dbSAlan Cox 			 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
24833f542974SPaul B Schroeder 		if (status < 0) {
248484fe6e79STony Cook 			dbg("Writing DCR2 failed status-0x%x", status);
24853f542974SPaul B Schroeder 			break;
24863f542974SPaul B Schroeder 		} else
248784fe6e79STony Cook 			dbg("DCR2 Writing success status%d", status);
24883f542974SPaul B Schroeder 
2489880af9dbSAlan Cox 		/* write values in clkstart0x0 and clkmulti 0x20 */
24903f542974SPaul B Schroeder 		Data = 0x0;
2491880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
24923f542974SPaul B Schroeder 					 CLK_START_VALUE_REGISTER, Data);
24933f542974SPaul B Schroeder 		if (status < 0) {
249484fe6e79STony Cook 			dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
24953f542974SPaul B Schroeder 			break;
24963f542974SPaul B Schroeder 		} else
249784fe6e79STony Cook 			dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
24983f542974SPaul B Schroeder 
24993f542974SPaul B Schroeder 		Data = 0x20;
2500880af9dbSAlan Cox 		status = mos7840_set_reg_sync(serial->port[i],
2501880af9dbSAlan Cox 					CLK_MULTI_REGISTER, Data);
25023f542974SPaul B Schroeder 		if (status < 0) {
250384fe6e79STony Cook 			dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
25043f542974SPaul B Schroeder 			    status);
25050de9a702SOliver Neukum 			goto error;
25063f542974SPaul B Schroeder 		} else
250784fe6e79STony Cook 			dbg("CLK_MULTI_REGISTER Writing success status%d",
25083f542974SPaul B Schroeder 			    status);
25093f542974SPaul B Schroeder 
2510880af9dbSAlan Cox 		/* write value 0x0 to scratchpad register */
25113f542974SPaul B Schroeder 		Data = 0x00;
2512880af9dbSAlan Cox 		status = mos7840_set_uart_reg(serial->port[i],
2513880af9dbSAlan Cox 						SCRATCH_PAD_REGISTER, Data);
25143f542974SPaul B Schroeder 		if (status < 0) {
251584fe6e79STony Cook 			dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
25163f542974SPaul B Schroeder 			    status);
25173f542974SPaul B Schroeder 			break;
25183f542974SPaul B Schroeder 		} else
251984fe6e79STony Cook 			dbg("SCRATCH_PAD_REGISTER Writing success status%d",
25203f542974SPaul B Schroeder 			    status);
25213f542974SPaul B Schroeder 
2522880af9dbSAlan Cox 		/* Zero Length flag register */
25233f542974SPaul B Schroeder 		if ((mos7840_port->port_num != 1)
25240de9a702SOliver Neukum 		    && (serial->num_ports == 2)) {
25253f542974SPaul B Schroeder 
25263f542974SPaul B Schroeder 			Data = 0xff;
25273f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
25283f542974SPaul B Schroeder 				      (__u16) (ZLP_REG1 +
2529880af9dbSAlan Cox 				      ((__u16)mos7840_port->port_num)), Data);
253084fe6e79STony Cook 			dbg("ZLIP offset %x",
25313f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
25323f542974SPaul B Schroeder 					((__u16) mos7840_port->port_num)));
25333f542974SPaul B Schroeder 			if (status < 0) {
253484fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
25353f542974SPaul B Schroeder 				    i + 2, status);
25363f542974SPaul B Schroeder 				break;
25373f542974SPaul B Schroeder 			} else
253884fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
25393f542974SPaul B Schroeder 				    i + 2, status);
25403f542974SPaul B Schroeder 		} else {
25413f542974SPaul B Schroeder 			Data = 0xff;
25423f542974SPaul B Schroeder 			status = mos7840_set_reg_sync(serial->port[i],
25433f542974SPaul B Schroeder 			      (__u16) (ZLP_REG1 +
2544880af9dbSAlan Cox 			      ((__u16)mos7840_port->port_num) - 0x1), Data);
254584fe6e79STony Cook 			dbg("ZLIP offset %x",
25463f542974SPaul B Schroeder 			    (__u16) (ZLP_REG1 +
25473f542974SPaul B Schroeder 				     ((__u16) mos7840_port->port_num) - 0x1));
25483f542974SPaul B Schroeder 			if (status < 0) {
254984fe6e79STony Cook 				dbg("Writing ZLP_REG%d failed status-0x%x",
25503f542974SPaul B Schroeder 				    i + 1, status);
25513f542974SPaul B Schroeder 				break;
25523f542974SPaul B Schroeder 			} else
255384fe6e79STony Cook 				dbg("ZLP_REG%d Writing success status%d",
25543f542974SPaul B Schroeder 				    i + 1, status);
25553f542974SPaul B Schroeder 
25563f542974SPaul B Schroeder 		}
25570de9a702SOliver Neukum 		mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
25583f542974SPaul B Schroeder 		mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2559880af9dbSAlan Cox 		mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2560880af9dbSAlan Cox 								GFP_KERNEL);
2561880af9dbSAlan Cox 		if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2562880af9dbSAlan Cox 							!mos7840_port->dr) {
25630de9a702SOliver Neukum 			status = -ENOMEM;
25640de9a702SOliver Neukum 			goto error;
25650de9a702SOliver Neukum 		}
25663f542974SPaul B Schroeder 	}
256784fe6e79STony Cook 	dbg ("mos7840_startup: all ports configured...........");
25683f542974SPaul B Schroeder 
2569880af9dbSAlan Cox 	/* Zero Length flag enable */
25703f542974SPaul B Schroeder 	Data = 0x0f;
25713f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
25723f542974SPaul B Schroeder 	if (status < 0) {
257384fe6e79STony Cook 		dbg("Writing ZLP_REG5 failed status-0x%x", status);
25747ced46c3SRoel Kluin 		goto error;
25753f542974SPaul B Schroeder 	} else
257684fe6e79STony Cook 		dbg("ZLP_REG5 Writing success status%d", status);
25773f542974SPaul B Schroeder 
25783f542974SPaul B Schroeder 	/* setting configuration feature to one */
25793f542974SPaul B Schroeder 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
258097c4965dSAl Viro 			(__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
25813f542974SPaul B Schroeder 	return 0;
25820de9a702SOliver Neukum error:
25830de9a702SOliver Neukum 	for (/* nothing */; i >= 0; i--) {
25840de9a702SOliver Neukum 		mos7840_port = mos7840_get_port_private(serial->port[i]);
25850de9a702SOliver Neukum 
25860de9a702SOliver Neukum 		kfree(mos7840_port->dr);
25870de9a702SOliver Neukum 		kfree(mos7840_port->ctrl_buf);
25880de9a702SOliver Neukum 		usb_free_urb(mos7840_port->control_urb);
25890de9a702SOliver Neukum 		kfree(mos7840_port);
25900de9a702SOliver Neukum 		serial->port[i] = NULL;
25910de9a702SOliver Neukum 	}
25920de9a702SOliver Neukum 	return status;
25933f542974SPaul B Schroeder }
25943f542974SPaul B Schroeder 
25953f542974SPaul B Schroeder /****************************************************************************
2596f9c99bb8SAlan Stern  * mos7840_disconnect
25973f542974SPaul B Schroeder  *	This function is called whenever the device is removed from the usb bus.
25983f542974SPaul B Schroeder  ****************************************************************************/
25993f542974SPaul B Schroeder 
2600f9c99bb8SAlan Stern static void mos7840_disconnect(struct usb_serial *serial)
26013f542974SPaul B Schroeder {
26023f542974SPaul B Schroeder 	int i;
26030de9a702SOliver Neukum 	unsigned long flags;
26043f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
2605f9c99bb8SAlan Stern 	dbg("%s", " disconnect :entering..........");
26063f542974SPaul B Schroeder 
26073f542974SPaul B Schroeder 	if (!serial) {
260884fe6e79STony Cook 		dbg("%s", "Invalid Handler");
26093f542974SPaul B Schroeder 		return;
26103f542974SPaul B Schroeder 	}
26113f542974SPaul B Schroeder 
26123f542974SPaul B Schroeder 	/* check for the ports to be closed,close the ports and disconnect */
26133f542974SPaul B Schroeder 
26143f542974SPaul B Schroeder 	/* free private structure allocated for serial port  *
26153f542974SPaul B Schroeder 	 * stop reads and writes on all ports                */
26163f542974SPaul B Schroeder 
26173f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; ++i) {
26183f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
261937768adfSTony Cook 		dbg ("mos7840_port %d = %p", i, mos7840_port);
262037768adfSTony Cook 		if (mos7840_port) {
26210de9a702SOliver Neukum 			spin_lock_irqsave(&mos7840_port->pool_lock, flags);
26220de9a702SOliver Neukum 			mos7840_port->zombie = 1;
26230de9a702SOliver Neukum 			spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
26243f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->control_urb);
2625f9c99bb8SAlan Stern 		}
2626f9c99bb8SAlan Stern 	}
2627f9c99bb8SAlan Stern 
2628f9c99bb8SAlan Stern 	dbg("%s", "Thank u :: ");
2629f9c99bb8SAlan Stern 
2630f9c99bb8SAlan Stern }
2631f9c99bb8SAlan Stern 
2632f9c99bb8SAlan Stern /****************************************************************************
2633f9c99bb8SAlan Stern  * mos7840_release
2634f9c99bb8SAlan Stern  *	This function is called when the usb_serial structure is freed.
2635f9c99bb8SAlan Stern  ****************************************************************************/
2636f9c99bb8SAlan Stern 
2637f9c99bb8SAlan Stern static void mos7840_release(struct usb_serial *serial)
2638f9c99bb8SAlan Stern {
2639f9c99bb8SAlan Stern 	int i;
2640f9c99bb8SAlan Stern 	struct moschip_port *mos7840_port;
2641f9c99bb8SAlan Stern 	dbg("%s", " release :entering..........");
2642f9c99bb8SAlan Stern 
2643f9c99bb8SAlan Stern 	if (!serial) {
2644f9c99bb8SAlan Stern 		dbg("%s", "Invalid Handler");
2645f9c99bb8SAlan Stern 		return;
2646f9c99bb8SAlan Stern 	}
2647f9c99bb8SAlan Stern 
2648f9c99bb8SAlan Stern 	/* check for the ports to be closed,close the ports and disconnect */
2649f9c99bb8SAlan Stern 
2650f9c99bb8SAlan Stern 	/* free private structure allocated for serial port  *
2651f9c99bb8SAlan Stern 	 * stop reads and writes on all ports                */
2652f9c99bb8SAlan Stern 
2653f9c99bb8SAlan Stern 	for (i = 0; i < serial->num_ports; ++i) {
2654f9c99bb8SAlan Stern 		mos7840_port = mos7840_get_port_private(serial->port[i]);
2655f9c99bb8SAlan Stern 		dbg("mos7840_port %d = %p", i, mos7840_port);
2656f9c99bb8SAlan Stern 		if (mos7840_port) {
26570de9a702SOliver Neukum 			kfree(mos7840_port->ctrl_buf);
26580de9a702SOliver Neukum 			kfree(mos7840_port->dr);
26593f542974SPaul B Schroeder 			kfree(mos7840_port);
266037768adfSTony Cook 		}
26613f542974SPaul B Schroeder 	}
26623f542974SPaul B Schroeder 
266384fe6e79STony Cook 	dbg("%s", "Thank u :: ");
26643f542974SPaul B Schroeder 
26653f542974SPaul B Schroeder }
26663f542974SPaul B Schroeder 
2667d9b1b787SJohannes Hölzl static struct usb_driver io_driver = {
2668d9b1b787SJohannes Hölzl 	.name = "mos7840",
2669d9b1b787SJohannes Hölzl 	.probe = usb_serial_probe,
2670d9b1b787SJohannes Hölzl 	.disconnect = usb_serial_disconnect,
2671d9b1b787SJohannes Hölzl 	.id_table = moschip_id_table_combined,
2672d9b1b787SJohannes Hölzl };
2673d9b1b787SJohannes Hölzl 
26743f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
26753f542974SPaul B Schroeder 	.driver = {
26763f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
26773f542974SPaul B Schroeder 		   .name = "mos7840",
26783f542974SPaul B Schroeder 		   },
26793f542974SPaul B Schroeder 	.description = DRIVER_DESC,
26803f542974SPaul B Schroeder 	.id_table = moschip_port_id_table,
26813f542974SPaul B Schroeder 	.num_ports = 4,
26823f542974SPaul B Schroeder 	.open = mos7840_open,
26833f542974SPaul B Schroeder 	.close = mos7840_close,
26843f542974SPaul B Schroeder 	.write = mos7840_write,
26853f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
26863f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
26873f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
26883f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
26893f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
26903f542974SPaul B Schroeder #ifdef MCSSerialProbe
26913f542974SPaul B Schroeder 	.probe = mos7840_serial_probe,
26923f542974SPaul B Schroeder #endif
26933f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
26943f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
26953f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
26963f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
26973f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
26980bca1b91SAlan Cox 	.get_icount = mos7840_get_icount,
26993f542974SPaul B Schroeder 	.attach = mos7840_startup,
2700f9c99bb8SAlan Stern 	.disconnect = mos7840_disconnect,
2701f9c99bb8SAlan Stern 	.release = mos7840_release,
27023f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
27033f542974SPaul B Schroeder 	.read_int_callback = mos7840_interrupt_callback,
27043f542974SPaul B Schroeder };
27053f542974SPaul B Schroeder 
27064d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
27074d2a7affSAlan Stern 	&moschip7840_4port_device, NULL
27084d2a7affSAlan Stern };
27094d2a7affSAlan Stern 
2710e7414d9aSGreg Kroah-Hartman module_usb_serial_driver(io_driver, serial_drivers);
27113f542974SPaul B Schroeder 
27123f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
27133f542974SPaul B Schroeder MODULE_LICENSE("GPL");
27143f542974SPaul B Schroeder 
27153f542974SPaul B Schroeder module_param(debug, bool, S_IRUGO | S_IWUSR);
27163f542974SPaul B Schroeder MODULE_PARM_DESC(debug, "Debug enabled or not");
2717