xref: /openbmc/linux/drivers/usb/serial/mos7840.c (revision 05c7cd39907184328f48d3e7899f9cdd653ad336)
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 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
393f542974SPaul B Schroeder 
403f542974SPaul B Schroeder /*
413f542974SPaul B Schroeder  * 16C50 UART register defines
423f542974SPaul B Schroeder  */
433f542974SPaul B Schroeder 
443f542974SPaul B Schroeder #define LCR_BITS_5             0x00	/* 5 bits/char */
453f542974SPaul B Schroeder #define LCR_BITS_6             0x01	/* 6 bits/char */
463f542974SPaul B Schroeder #define LCR_BITS_7             0x02	/* 7 bits/char */
473f542974SPaul B Schroeder #define LCR_BITS_8             0x03	/* 8 bits/char */
483f542974SPaul B Schroeder #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
493f542974SPaul B Schroeder 
503f542974SPaul B Schroeder #define LCR_STOP_1             0x00	/* 1 stop bit */
513f542974SPaul B Schroeder #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
523f542974SPaul B Schroeder #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
533f542974SPaul B Schroeder #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
543f542974SPaul B Schroeder 
553f542974SPaul B Schroeder #define LCR_PAR_NONE           0x00	/* No parity */
563f542974SPaul B Schroeder #define LCR_PAR_ODD            0x08	/* Odd parity */
573f542974SPaul B Schroeder #define LCR_PAR_EVEN           0x18	/* Even parity */
583f542974SPaul B Schroeder #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
593f542974SPaul B Schroeder #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
603f542974SPaul B Schroeder #define LCR_PAR_MASK           0x38	/* Mask for parity field */
613f542974SPaul B Schroeder 
623f542974SPaul B Schroeder #define LCR_SET_BREAK          0x40	/* Set Break condition */
633f542974SPaul B Schroeder #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
643f542974SPaul B Schroeder 
653f542974SPaul B Schroeder #define MCR_DTR                0x01	/* Assert DTR */
663f542974SPaul B Schroeder #define MCR_RTS                0x02	/* Assert RTS */
673f542974SPaul B Schroeder #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
683f542974SPaul B Schroeder #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
693f542974SPaul B Schroeder #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
703f542974SPaul B Schroeder #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
713f542974SPaul B Schroeder 
723f542974SPaul B Schroeder #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
733f542974SPaul B Schroeder #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
743f542974SPaul B Schroeder #define MOS7840_MSR_RI         0x40	/* Current state of RI */
753f542974SPaul B Schroeder #define MOS7840_MSR_CD         0x80	/* Current state of CD */
763f542974SPaul B Schroeder 
773f542974SPaul B Schroeder /*
783f542974SPaul B Schroeder  * Defines used for sending commands to port
793f542974SPaul B Schroeder  */
803f542974SPaul B Schroeder 
811e658489SMark Ferrell #define MOS_WDR_TIMEOUT		5000	/* default urb timeout */
823f542974SPaul B Schroeder 
833f542974SPaul B Schroeder #define MOS_PORT1       0x0200
843f542974SPaul B Schroeder #define MOS_PORT2       0x0300
853f542974SPaul B Schroeder #define MOS_VENREG      0x0000
863f542974SPaul B Schroeder #define MOS_MAX_PORT	0x02
873f542974SPaul B Schroeder #define MOS_WRITE       0x0E
883f542974SPaul B Schroeder #define MOS_READ        0x0D
893f542974SPaul B Schroeder 
903f542974SPaul B Schroeder /* Requests */
913f542974SPaul B Schroeder #define MCS_RD_RTYPE    0xC0
923f542974SPaul B Schroeder #define MCS_WR_RTYPE    0x40
933f542974SPaul B Schroeder #define MCS_RDREQ       0x0D
943f542974SPaul B Schroeder #define MCS_WRREQ       0x0E
953f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT        500
963f542974SPaul B Schroeder #define VENDOR_READ_LENGTH      (0x01)
973f542974SPaul B Schroeder 
983f542974SPaul B Schroeder #define MAX_NAME_LEN    64
993f542974SPaul B Schroeder 
100880af9dbSAlan Cox #define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
101880af9dbSAlan Cox #define ZLP_REG5  0x3E		/* Zero_Flag_Reg5    62 */
1023f542974SPaul B Schroeder 
1033f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */
1043f542974SPaul B Schroeder #define TIOCEXBAUD     0x5462
1053f542974SPaul B Schroeder 
1063f542974SPaul B Schroeder /* vendor id and device id defines */
1073f542974SPaul B Schroeder 
10811e1abb4SDavid Ludlow /* The native mos7840/7820 component */
1093f542974SPaul B Schroeder #define USB_VENDOR_ID_MOSCHIP           0x9710
1103f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7840          0x7840
1113f542974SPaul B Schroeder #define MOSCHIP_DEVICE_ID_7820          0x7820
1120eafe4deSDonald #define MOSCHIP_DEVICE_ID_7810          0x7810
11311e1abb4SDavid Ludlow /* The native component can have its vendor/device id's overridden
11411e1abb4SDavid Ludlow  * in vendor-specific implementations.  Such devices can be handled
11568e24113SGreg Kroah-Hartman  * by making a change here, in id_table.
11611e1abb4SDavid Ludlow  */
11711e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB              0x0856
118acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
119870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
120acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
121870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
122acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
123acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
124acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
125acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
12611e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
127caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
128870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
129870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
130870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL2_4        0xAC24
1313f542974SPaul B Schroeder 
1329d498beaSRussell Lang /* This driver also supports
1339d498beaSRussell Lang  * ATEN UC2324 device using Moschip MCS7840
1349d498beaSRussell Lang  * ATEN UC2322 device using Moschip MCS7820
1359d498beaSRussell Lang  */
136e9b8cffaSTony Cook #define USB_VENDOR_ID_ATENINTL		0x0557
137e9b8cffaSTony Cook #define ATENINTL_DEVICE_ID_UC2324	0x2011
1389d498beaSRussell Lang #define ATENINTL_DEVICE_ID_UC2322	0x7820
139e9b8cffaSTony Cook 
14011e1abb4SDavid Ludlow /* Interrupt Routine Defines    */
1413f542974SPaul B Schroeder 
1423f542974SPaul B Schroeder #define SERIAL_IIR_RLS      0x06
1433f542974SPaul B Schroeder #define SERIAL_IIR_MS       0x00
1443f542974SPaul B Schroeder 
1453f542974SPaul B Schroeder /*
1463f542974SPaul B Schroeder  *  Emulation of the bit mask on the LINE STATUS REGISTER.
1473f542974SPaul B Schroeder  */
1483f542974SPaul B Schroeder #define SERIAL_LSR_DR       0x0001
1493f542974SPaul B Schroeder #define SERIAL_LSR_OE       0x0002
1503f542974SPaul B Schroeder #define SERIAL_LSR_PE       0x0004
1513f542974SPaul B Schroeder #define SERIAL_LSR_FE       0x0008
1523f542974SPaul B Schroeder #define SERIAL_LSR_BI       0x0010
1533f542974SPaul B Schroeder 
1543f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS   0x10
1553f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR   0x20
1563f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI    0x40
1573f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD    0x80
1583f542974SPaul B Schroeder 
159880af9dbSAlan Cox /* Serial Port register Address */
1603f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
1613f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
1623f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
1633f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
1643f542974SPaul B Schroeder #define LINE_STATUS_REGISTER       ((__u16)(0x05))
1653f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
1663f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
1673f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
1683f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
1693f542974SPaul B Schroeder 
1703f542974SPaul B Schroeder #define CLK_MULTI_REGISTER         ((__u16)(0x02))
1713f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
172093ea2d3SDonald Lee #define GPIO_REGISTER              ((__u16)(0x07))
1733f542974SPaul B Schroeder 
1743f542974SPaul B Schroeder #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
1753f542974SPaul B Schroeder 
1763f542974SPaul B Schroeder /*
1773f542974SPaul B Schroeder  * URB POOL related defines
1783f542974SPaul B Schroeder  */
1793f542974SPaul B Schroeder #define NUM_URBS                        16	/* URB Count */
1803f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
1813f542974SPaul B Schroeder 
1820eafe4deSDonald /* LED on/off milliseconds*/
1830eafe4deSDonald #define LED_ON_MS	500
1840eafe4deSDonald #define LED_OFF_MS	500
1850eafe4deSDonald 
1860eafe4deSDonald static int device_type;
1873f542974SPaul B Schroeder 
188b9c87663STony Zelenoff static const struct usb_device_id 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)},
1910eafe4deSDonald 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
192acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
193870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
194acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
195870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
196acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
197acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
198acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
199acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
20011e1abb4SDavid Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
201870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
202acf509aeSCliff Brake 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
203870408c8SDave Ludlow 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
20427f1281dSBlaise Gassend 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
205e9b8cffaSTony Cook 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
2069d498beaSRussell Lang 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
2073f542974SPaul B Schroeder 	{}			/* terminating entry */
2083f542974SPaul B Schroeder };
20968e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
2103f542974SPaul B Schroeder 
2113f542974SPaul B Schroeder /* This structure holds all of the local port information */
2123f542974SPaul B Schroeder 
2133f542974SPaul B Schroeder struct moschip_port {
2143f542974SPaul B Schroeder 	int port_num;		/*Actual port number in the device(1,2,etc) */
2153f542974SPaul B Schroeder 	struct urb *write_urb;	/* write URB for this port */
2163f542974SPaul B Schroeder 	struct urb *read_urb;	/* read URB for this port */
2173f542974SPaul B Schroeder 	__u8 shadowLCR;		/* last LCR value received */
2183f542974SPaul B Schroeder 	__u8 shadowMCR;		/* last MCR value received */
2193f542974SPaul B Schroeder 	char open;
2200de9a702SOliver Neukum 	char open_ports;
2213f542974SPaul B Schroeder 	wait_queue_head_t wait_chase;	/* for handling sleeping while waiting for chase to finish */
2223f542974SPaul B Schroeder 	wait_queue_head_t delta_msr_wait;	/* for handling sleeping while waiting for msr change to happen */
2233f542974SPaul B Schroeder 	int delta_msr_cond;
2243f542974SPaul B Schroeder 	struct async_icount icount;
2253f542974SPaul B Schroeder 	struct usb_serial_port *port;	/* loop back to the owner of this object */
2263f542974SPaul B Schroeder 
2273f542974SPaul B Schroeder 	/* Offsets */
2283f542974SPaul B Schroeder 	__u8 SpRegOffset;
2293f542974SPaul B Schroeder 	__u8 ControlRegOffset;
2303f542974SPaul B Schroeder 	__u8 DcrRegOffset;
231880af9dbSAlan Cox 	/* for processing control URBS in interrupt context */
2323f542974SPaul B Schroeder 	struct urb *control_urb;
2330de9a702SOliver Neukum 	struct usb_ctrlrequest *dr;
2343f542974SPaul B Schroeder 	char *ctrl_buf;
2353f542974SPaul B Schroeder 	int MsrLsr;
2363f542974SPaul B Schroeder 
2370de9a702SOliver Neukum 	spinlock_t pool_lock;
2383f542974SPaul B Schroeder 	struct urb *write_urb_pool[NUM_URBS];
2390de9a702SOliver Neukum 	char busy[NUM_URBS];
24050de36f7SGreg Kroah-Hartman 	bool read_urb_busy;
2413f542974SPaul B Schroeder 
2420eafe4deSDonald 	/* For device(s) with LED indicator */
2430eafe4deSDonald 	bool has_led;
2440eafe4deSDonald 	bool led_flag;
2450eafe4deSDonald 	struct timer_list led_timer1;	/* Timer for LED on */
2460eafe4deSDonald 	struct timer_list led_timer2;	/* Timer for LED off */
2470eafe4deSDonald };
2483f542974SPaul B Schroeder 
2493f542974SPaul B Schroeder /*
2503f542974SPaul B Schroeder  * mos7840_set_reg_sync
2513f542974SPaul B Schroeder  * 	To set the Control register by calling usb_fill_control_urb function
2523f542974SPaul B Schroeder  *	by passing usb_sndctrlpipe function as parameter.
2533f542974SPaul B Schroeder  */
2543f542974SPaul B Schroeder 
2553f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2563f542974SPaul B Schroeder 				__u16 val)
2573f542974SPaul B Schroeder {
2583f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2593f542974SPaul B Schroeder 	val = val & 0x00ff;
2609c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
2613f542974SPaul B Schroeder 
2623f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2633f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
2643f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
2653f542974SPaul B Schroeder }
2663f542974SPaul B Schroeder 
2673f542974SPaul B Schroeder /*
2683f542974SPaul B Schroeder  * mos7840_get_reg_sync
2693f542974SPaul B Schroeder  * 	To set the Uart register by calling usb_fill_control_urb function by
2703f542974SPaul B Schroeder  *	passing usb_rcvctrlpipe function as parameter.
2713f542974SPaul B Schroeder  */
2723f542974SPaul B Schroeder 
2733f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
2743f542974SPaul B Schroeder 				__u16 *val)
2753f542974SPaul B Schroeder {
2763f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
2773f542974SPaul B Schroeder 	int ret = 0;
2789e221a35SJohan Hovold 	u8 *buf;
2799e221a35SJohan Hovold 
2809e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2819e221a35SJohan Hovold 	if (!buf)
2829e221a35SJohan Hovold 		return -ENOMEM;
2833f542974SPaul B Schroeder 
2843f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
2859e221a35SJohan Hovold 			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
2863f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
2879e221a35SJohan Hovold 	*val = buf[0];
2889c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
2899e221a35SJohan Hovold 
2909e221a35SJohan Hovold 	kfree(buf);
2913f542974SPaul B Schroeder 	return ret;
2923f542974SPaul B Schroeder }
2933f542974SPaul B Schroeder 
2943f542974SPaul B Schroeder /*
2953f542974SPaul B Schroeder  * mos7840_set_uart_reg
2963f542974SPaul B Schroeder  *	To set the Uart register by calling usb_fill_control_urb function by
2973f542974SPaul B Schroeder  *	passing usb_sndctrlpipe function as parameter.
2983f542974SPaul B Schroeder  */
2993f542974SPaul B Schroeder 
3003f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
3013f542974SPaul B Schroeder 				__u16 val)
3023f542974SPaul B Schroeder {
3033f542974SPaul B Schroeder 
3043f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3053f542974SPaul B Schroeder 	val = val & 0x00ff;
306880af9dbSAlan Cox 	/* For the UART control registers, the application number need
307880af9dbSAlan Cox 	   to be Or'ed */
3080de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
309880af9dbSAlan Cox 		val |= (((__u16) port->number -
310880af9dbSAlan Cox 				(__u16) (port->serial->minor)) + 1) << 8;
3113f542974SPaul B Schroeder 	} else {
3123f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
313880af9dbSAlan Cox 			val |= (((__u16) port->number -
3143f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
3153f542974SPaul B Schroeder 		} else {
3169c134a14SGreg Kroah-Hartman 			val |= (((__u16) port->number -
3173f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
3183f542974SPaul B Schroeder 		}
3193f542974SPaul B Schroeder 	}
3209c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
3213f542974SPaul B Schroeder 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
3223f542974SPaul B Schroeder 			       MCS_WR_RTYPE, val, reg, NULL, 0,
3233f542974SPaul B Schroeder 			       MOS_WDR_TIMEOUT);
3243f542974SPaul B Schroeder 
3253f542974SPaul B Schroeder }
3263f542974SPaul B Schroeder 
3273f542974SPaul B Schroeder /*
3283f542974SPaul B Schroeder  * mos7840_get_uart_reg
3293f542974SPaul B Schroeder  *	To set the Control register by calling usb_fill_control_urb function
3303f542974SPaul B Schroeder  *	by passing usb_rcvctrlpipe function as parameter.
3313f542974SPaul B Schroeder  */
3323f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3333f542974SPaul B Schroeder 				__u16 *val)
3343f542974SPaul B Schroeder {
3353f542974SPaul B Schroeder 	struct usb_device *dev = port->serial->dev;
3363f542974SPaul B Schroeder 	int ret = 0;
3373f542974SPaul B Schroeder 	__u16 Wval;
3389e221a35SJohan Hovold 	u8 *buf;
3399e221a35SJohan Hovold 
3409e221a35SJohan Hovold 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3419e221a35SJohan Hovold 	if (!buf)
3429e221a35SJohan Hovold 		return -ENOMEM;
3433f542974SPaul B Schroeder 
3443f542974SPaul B Schroeder 	/* Wval  is same as application number */
3450de9a702SOliver Neukum 	if (port->serial->num_ports == 4) {
3463f542974SPaul B Schroeder 		Wval =
3473f542974SPaul B Schroeder 		    (((__u16) port->number - (__u16) (port->serial->minor)) +
3483f542974SPaul B Schroeder 		     1) << 8;
3493f542974SPaul B Schroeder 	} else {
3503f542974SPaul B Schroeder 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
351880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3523f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 1) << 8;
3533f542974SPaul B Schroeder 		} else {
354880af9dbSAlan Cox 			Wval = (((__u16) port->number -
3553f542974SPaul B Schroeder 			      (__u16) (port->serial->minor)) + 2) << 8;
3563f542974SPaul B Schroeder 		}
3573f542974SPaul B Schroeder 	}
3589c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
3593f542974SPaul B Schroeder 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3609e221a35SJohan Hovold 			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
3613f542974SPaul B Schroeder 			      MOS_WDR_TIMEOUT);
3629e221a35SJohan Hovold 	*val = buf[0];
3639e221a35SJohan Hovold 
3649e221a35SJohan Hovold 	kfree(buf);
3653f542974SPaul B Schroeder 	return ret;
3663f542974SPaul B Schroeder }
3673f542974SPaul B Schroeder 
3689c134a14SGreg Kroah-Hartman static void mos7840_dump_serial_port(struct usb_serial_port *port,
3699c134a14SGreg Kroah-Hartman 				     struct moschip_port *mos7840_port)
3703f542974SPaul B Schroeder {
3713f542974SPaul B Schroeder 
3729c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
3739c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
3749c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
3753f542974SPaul B Schroeder 
3763f542974SPaul B Schroeder }
3773f542974SPaul B Schroeder 
3783f542974SPaul B Schroeder /************************************************************************/
3793f542974SPaul B Schroeder /************************************************************************/
3803f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3813f542974SPaul B Schroeder /*             I N T E R F A C E   F U N C T I O N S			*/
3823f542974SPaul B Schroeder /************************************************************************/
3833f542974SPaul B Schroeder /************************************************************************/
3843f542974SPaul B Schroeder 
3853f542974SPaul B Schroeder static inline void mos7840_set_port_private(struct usb_serial_port *port,
3863f542974SPaul B Schroeder 					    struct moschip_port *data)
3873f542974SPaul B Schroeder {
3883f542974SPaul B Schroeder 	usb_set_serial_port_data(port, (void *)data);
3893f542974SPaul B Schroeder }
3903f542974SPaul B Schroeder 
3913f542974SPaul B Schroeder static inline struct moschip_port *mos7840_get_port_private(struct
3923f542974SPaul B Schroeder 							    usb_serial_port
3933f542974SPaul B Schroeder 							    *port)
3943f542974SPaul B Schroeder {
3953f542974SPaul B Schroeder 	return (struct moschip_port *)usb_get_serial_port_data(port);
3963f542974SPaul B Schroeder }
3973f542974SPaul B Schroeder 
3980de9a702SOliver Neukum static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
3993f542974SPaul B Schroeder {
4003f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4013f542974SPaul B Schroeder 	struct async_icount *icount;
4023f542974SPaul B Schroeder 	mos7840_port = port;
4033f542974SPaul B Schroeder 	icount = &mos7840_port->icount;
4043f542974SPaul B Schroeder 	if (new_msr &
4053f542974SPaul B Schroeder 	    (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
4063f542974SPaul B Schroeder 	     MOS_MSR_DELTA_CD)) {
4073f542974SPaul B Schroeder 		icount = &mos7840_port->icount;
4083f542974SPaul B Schroeder 
4093f542974SPaul B Schroeder 		/* update input line counters */
4103f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CTS) {
4113f542974SPaul B Schroeder 			icount->cts++;
4120de9a702SOliver Neukum 			smp_wmb();
4133f542974SPaul B Schroeder 		}
4143f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_DSR) {
4153f542974SPaul B Schroeder 			icount->dsr++;
4160de9a702SOliver Neukum 			smp_wmb();
4173f542974SPaul B Schroeder 		}
4183f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_CD) {
4193f542974SPaul B Schroeder 			icount->dcd++;
4200de9a702SOliver Neukum 			smp_wmb();
4213f542974SPaul B Schroeder 		}
4223f542974SPaul B Schroeder 		if (new_msr & MOS_MSR_DELTA_RI) {
4233f542974SPaul B Schroeder 			icount->rng++;
4240de9a702SOliver Neukum 			smp_wmb();
4250de9a702SOliver Neukum 		}
4263f542974SPaul B Schroeder 	}
4273f542974SPaul B Schroeder }
4283f542974SPaul B Schroeder 
4290de9a702SOliver Neukum static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
4303f542974SPaul B Schroeder {
4313f542974SPaul B Schroeder 	struct async_icount *icount;
4323f542974SPaul B Schroeder 
4333f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
434880af9dbSAlan Cox 		/*
435880af9dbSAlan Cox 		 * Parity and Framing errors only count if they
436880af9dbSAlan Cox 		 * occur exclusive of a break being
437880af9dbSAlan Cox 		 * received.
438880af9dbSAlan Cox 		 */
4393f542974SPaul B Schroeder 		new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
4403f542974SPaul B Schroeder 	}
4413f542974SPaul B Schroeder 
4423f542974SPaul B Schroeder 	/* update input line counters */
4433f542974SPaul B Schroeder 	icount = &port->icount;
4443f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_BI) {
4453f542974SPaul B Schroeder 		icount->brk++;
4460de9a702SOliver Neukum 		smp_wmb();
4473f542974SPaul B Schroeder 	}
4483f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_OE) {
4493f542974SPaul B Schroeder 		icount->overrun++;
4500de9a702SOliver Neukum 		smp_wmb();
4513f542974SPaul B Schroeder 	}
4523f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_PE) {
4533f542974SPaul B Schroeder 		icount->parity++;
4540de9a702SOliver Neukum 		smp_wmb();
4553f542974SPaul B Schroeder 	}
4563f542974SPaul B Schroeder 	if (new_lsr & SERIAL_LSR_FE) {
4573f542974SPaul B Schroeder 		icount->frame++;
4580de9a702SOliver Neukum 		smp_wmb();
4593f542974SPaul B Schroeder 	}
4603f542974SPaul B Schroeder }
4613f542974SPaul B Schroeder 
4623f542974SPaul B Schroeder /************************************************************************/
4633f542974SPaul B Schroeder /************************************************************************/
4643f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4653f542974SPaul B Schroeder /*            U S B  C A L L B A C K   F U N C T I O N S                */
4663f542974SPaul B Schroeder /************************************************************************/
4673f542974SPaul B Schroeder /************************************************************************/
4683f542974SPaul B Schroeder 
4697d12e780SDavid Howells static void mos7840_control_callback(struct urb *urb)
4703f542974SPaul B Schroeder {
4713f542974SPaul B Schroeder 	unsigned char *data;
4723f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
4739c134a14SGreg Kroah-Hartman 	struct device *dev = &urb->dev->dev;
4743f542974SPaul B Schroeder 	__u8 regval = 0x0;
4750643c724SGreg Kroah-Hartman 	int status = urb->status;
4763f542974SPaul B Schroeder 
477cdc97792SMing Lei 	mos7840_port = urb->context;
4780de9a702SOliver Neukum 
4790643c724SGreg Kroah-Hartman 	switch (status) {
4803f542974SPaul B Schroeder 	case 0:
4813f542974SPaul B Schroeder 		/* success */
4823f542974SPaul B Schroeder 		break;
4833f542974SPaul B Schroeder 	case -ECONNRESET:
4843f542974SPaul B Schroeder 	case -ENOENT:
4853f542974SPaul B Schroeder 	case -ESHUTDOWN:
4863f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
4879c134a14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
4883f542974SPaul B Schroeder 		return;
4893f542974SPaul B Schroeder 	default:
4909c134a14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
49128c3ae9aSJohan Hovold 		return;
4923f542974SPaul B Schroeder 	}
4933f542974SPaul B Schroeder 
4949c134a14SGreg Kroah-Hartman 	dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
4959c134a14SGreg Kroah-Hartman 	dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
4963f542974SPaul B Schroeder 		mos7840_port->MsrLsr, mos7840_port->port_num);
4973f542974SPaul B Schroeder 	data = urb->transfer_buffer;
4983f542974SPaul B Schroeder 	regval = (__u8) data[0];
4999c134a14SGreg Kroah-Hartman 	dev_dbg(dev, "%s data is %x\n", __func__, regval);
5003f542974SPaul B Schroeder 	if (mos7840_port->MsrLsr == 0)
5013f542974SPaul B Schroeder 		mos7840_handle_new_msr(mos7840_port, regval);
5023f542974SPaul B Schroeder 	else if (mos7840_port->MsrLsr == 1)
5033f542974SPaul B Schroeder 		mos7840_handle_new_lsr(mos7840_port, regval);
5043f542974SPaul B Schroeder }
5053f542974SPaul B Schroeder 
5063f542974SPaul B Schroeder static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
5073f542974SPaul B Schroeder 			   __u16 *val)
5083f542974SPaul B Schroeder {
5093f542974SPaul B Schroeder 	struct usb_device *dev = mcs->port->serial->dev;
5100de9a702SOliver Neukum 	struct usb_ctrlrequest *dr = mcs->dr;
5110de9a702SOliver Neukum 	unsigned char *buffer = mcs->ctrl_buf;
5120de9a702SOliver Neukum 	int ret;
5133f542974SPaul B Schroeder 
5143f542974SPaul B Schroeder 	dr->bRequestType = MCS_RD_RTYPE;
5153f542974SPaul B Schroeder 	dr->bRequest = MCS_RDREQ;
516880af9dbSAlan Cox 	dr->wValue = cpu_to_le16(Wval);	/* 0 */
5173f542974SPaul B Schroeder 	dr->wIndex = cpu_to_le16(reg);
5183f542974SPaul B Schroeder 	dr->wLength = cpu_to_le16(2);
5193f542974SPaul B Schroeder 
5203f542974SPaul B Schroeder 	usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
5213f542974SPaul B Schroeder 			     (unsigned char *)dr, buffer, 2,
5223f542974SPaul B Schroeder 			     mos7840_control_callback, mcs);
5233f542974SPaul B Schroeder 	mcs->control_urb->transfer_buffer_length = 2;
5243f542974SPaul B Schroeder 	ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
5253f542974SPaul B Schroeder 	return ret;
5263f542974SPaul B Schroeder }
5273f542974SPaul B Schroeder 
5280eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb)
5290eafe4deSDonald {
5300eafe4deSDonald 	switch (urb->status) {
5310eafe4deSDonald 	case 0:
5320eafe4deSDonald 		/* Success */
5330eafe4deSDonald 		break;
5340eafe4deSDonald 	case -ECONNRESET:
5350eafe4deSDonald 	case -ENOENT:
5360eafe4deSDonald 	case -ESHUTDOWN:
5370eafe4deSDonald 		/* This urb is terminated, clean up */
5389c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
5399c134a14SGreg Kroah-Hartman 			__func__, urb->status);
5400eafe4deSDonald 		break;
5410eafe4deSDonald 	default:
5429c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
5439c134a14SGreg Kroah-Hartman 			__func__, urb->status);
5440eafe4deSDonald 	}
5450eafe4deSDonald }
5460eafe4deSDonald 
5470eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
5480eafe4deSDonald 				__u16 reg)
5490eafe4deSDonald {
5500eafe4deSDonald 	struct usb_device *dev = mcs->port->serial->dev;
5510eafe4deSDonald 	struct usb_ctrlrequest *dr = mcs->dr;
5520eafe4deSDonald 
5530eafe4deSDonald 	dr->bRequestType = MCS_WR_RTYPE;
5540eafe4deSDonald 	dr->bRequest = MCS_WRREQ;
5550eafe4deSDonald 	dr->wValue = cpu_to_le16(wval);
5560eafe4deSDonald 	dr->wIndex = cpu_to_le16(reg);
5570eafe4deSDonald 	dr->wLength = cpu_to_le16(0);
5580eafe4deSDonald 
5590eafe4deSDonald 	usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
5600eafe4deSDonald 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
5610eafe4deSDonald 
5620eafe4deSDonald 	usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
5630eafe4deSDonald }
5640eafe4deSDonald 
5650eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
5660eafe4deSDonald 				__u16 val)
5670eafe4deSDonald {
5680eafe4deSDonald 	struct usb_device *dev = port->serial->dev;
5690eafe4deSDonald 
5700eafe4deSDonald 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
5710eafe4deSDonald 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
5720eafe4deSDonald }
5730eafe4deSDonald 
5740eafe4deSDonald static void mos7840_led_off(unsigned long arg)
5750eafe4deSDonald {
5760eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
5770eafe4deSDonald 
5780eafe4deSDonald 	/* Turn off LED */
5790eafe4deSDonald 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
5800eafe4deSDonald 	mod_timer(&mcs->led_timer2,
5810eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_OFF_MS));
5820eafe4deSDonald }
5830eafe4deSDonald 
5840eafe4deSDonald static void mos7840_led_flag_off(unsigned long arg)
5850eafe4deSDonald {
5860eafe4deSDonald 	struct moschip_port *mcs = (struct moschip_port *) arg;
5870eafe4deSDonald 
5880eafe4deSDonald 	mcs->led_flag = false;
5890eafe4deSDonald }
5900eafe4deSDonald 
5913f542974SPaul B Schroeder /*****************************************************************************
5923f542974SPaul B Schroeder  * mos7840_interrupt_callback
5933f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
5943f542974SPaul B Schroeder  *	interrupt endpoint.
5953f542974SPaul B Schroeder  *****************************************************************************/
5963f542974SPaul B Schroeder 
5977d12e780SDavid Howells static void mos7840_interrupt_callback(struct urb *urb)
5983f542974SPaul B Schroeder {
5993f542974SPaul B Schroeder 	int result;
6003f542974SPaul B Schroeder 	int length;
6013f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
6023f542974SPaul B Schroeder 	struct usb_serial *serial;
6033f542974SPaul B Schroeder 	__u16 Data;
6043f542974SPaul B Schroeder 	unsigned char *data;
6053f542974SPaul B Schroeder 	__u8 sp[5], st;
6060de9a702SOliver Neukum 	int i, rv = 0;
6070de9a702SOliver Neukum 	__u16 wval, wreg = 0;
6080643c724SGreg Kroah-Hartman 	int status = urb->status;
6093f542974SPaul B Schroeder 
6100643c724SGreg Kroah-Hartman 	switch (status) {
6113f542974SPaul B Schroeder 	case 0:
6123f542974SPaul B Schroeder 		/* success */
6133f542974SPaul B Schroeder 		break;
6143f542974SPaul B Schroeder 	case -ECONNRESET:
6153f542974SPaul B Schroeder 	case -ENOENT:
6163f542974SPaul B Schroeder 	case -ESHUTDOWN:
6173f542974SPaul B Schroeder 		/* this urb is terminated, clean up */
6189c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
6199c134a14SGreg Kroah-Hartman 			__func__, status);
6203f542974SPaul B Schroeder 		return;
6213f542974SPaul B Schroeder 	default:
6229c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
6239c134a14SGreg Kroah-Hartman 			__func__, status);
6243f542974SPaul B Schroeder 		goto exit;
6253f542974SPaul B Schroeder 	}
6263f542974SPaul B Schroeder 
6273f542974SPaul B Schroeder 	length = urb->actual_length;
6283f542974SPaul B Schroeder 	data = urb->transfer_buffer;
6293f542974SPaul B Schroeder 
630cdc97792SMing Lei 	serial = urb->context;
6313f542974SPaul B Schroeder 
6323f542974SPaul B Schroeder 	/* Moschip get 5 bytes
6333f542974SPaul B Schroeder 	 * Byte 1 IIR Port 1 (port.number is 0)
6343f542974SPaul B Schroeder 	 * Byte 2 IIR Port 2 (port.number is 1)
6353f542974SPaul B Schroeder 	 * Byte 3 IIR Port 3 (port.number is 2)
6363f542974SPaul B Schroeder 	 * Byte 4 IIR Port 4 (port.number is 3)
6373f542974SPaul B Schroeder 	 * Byte 5 FIFO status for both */
6383f542974SPaul B Schroeder 
6393f542974SPaul B Schroeder 	if (length && length > 5) {
6409c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
6413f542974SPaul B Schroeder 		return;
6423f542974SPaul B Schroeder 	}
6433f542974SPaul B Schroeder 
6443f542974SPaul B Schroeder 	sp[0] = (__u8) data[0];
6453f542974SPaul B Schroeder 	sp[1] = (__u8) data[1];
6463f542974SPaul B Schroeder 	sp[2] = (__u8) data[2];
6473f542974SPaul B Schroeder 	sp[3] = (__u8) data[3];
6483f542974SPaul B Schroeder 	st = (__u8) data[4];
6493f542974SPaul B Schroeder 
6503f542974SPaul B Schroeder 	for (i = 0; i < serial->num_ports; i++) {
6513f542974SPaul B Schroeder 		mos7840_port = mos7840_get_port_private(serial->port[i]);
6523f542974SPaul B Schroeder 		wval =
6533f542974SPaul B Schroeder 		    (((__u16) serial->port[i]->number -
6543f542974SPaul B Schroeder 		      (__u16) (serial->minor)) + 1) << 8;
6553f542974SPaul B Schroeder 		if (mos7840_port->open) {
6563f542974SPaul B Schroeder 			if (sp[i] & 0x01) {
6579c134a14SGreg Kroah-Hartman 				dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
6583f542974SPaul B Schroeder 			} else {
6593f542974SPaul B Schroeder 				switch (sp[i] & 0x0f) {
6603f542974SPaul B Schroeder 				case SERIAL_IIR_RLS:
6619c134a14SGreg Kroah-Hartman 					dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
6629c134a14SGreg Kroah-Hartman 					dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
6633f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 1;
6640de9a702SOliver Neukum 					wreg = LINE_STATUS_REGISTER;
6653f542974SPaul B Schroeder 					break;
6663f542974SPaul B Schroeder 				case SERIAL_IIR_MS:
6679c134a14SGreg Kroah-Hartman 					dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
6683f542974SPaul B Schroeder 					mos7840_port->MsrLsr = 0;
6690de9a702SOliver Neukum 					wreg = MODEM_STATUS_REGISTER;
6703f542974SPaul B Schroeder 					break;
6713f542974SPaul B Schroeder 				}
6720de9a702SOliver Neukum 				rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
6733f542974SPaul B Schroeder 			}
6743f542974SPaul B Schroeder 		}
6753f542974SPaul B Schroeder 	}
676880af9dbSAlan Cox 	if (!(rv < 0))
677880af9dbSAlan Cox 		/* the completion handler for the control urb will resubmit */
6780de9a702SOliver Neukum 		return;
6793f542974SPaul B Schroeder exit:
6803f542974SPaul B Schroeder 	result = usb_submit_urb(urb, GFP_ATOMIC);
6813f542974SPaul B Schroeder 	if (result) {
6823f542974SPaul B Schroeder 		dev_err(&urb->dev->dev,
6833f542974SPaul B Schroeder 			"%s - Error %d submitting interrupt urb\n",
684441b62c1SHarvey Harrison 			__func__, result);
6853f542974SPaul B Schroeder 	}
6863f542974SPaul B Schroeder }
6873f542974SPaul B Schroeder 
6883f542974SPaul B Schroeder static int mos7840_port_paranoia_check(struct usb_serial_port *port,
6893f542974SPaul B Schroeder 				       const char *function)
6903f542974SPaul B Schroeder {
6913f542974SPaul B Schroeder 	if (!port) {
6929c134a14SGreg Kroah-Hartman 		pr_debug("%s - port == NULL\n", function);
6933f542974SPaul B Schroeder 		return -1;
6943f542974SPaul B Schroeder 	}
6953f542974SPaul B Schroeder 	if (!port->serial) {
6969c134a14SGreg Kroah-Hartman 		pr_debug("%s - port->serial == NULL\n", function);
6973f542974SPaul B Schroeder 		return -1;
6983f542974SPaul B Schroeder 	}
6993f542974SPaul B Schroeder 
7003f542974SPaul B Schroeder 	return 0;
7013f542974SPaul B Schroeder }
7023f542974SPaul B Schroeder 
7033f542974SPaul B Schroeder /* Inline functions to check the sanity of a pointer that is passed to us */
7043f542974SPaul B Schroeder static int mos7840_serial_paranoia_check(struct usb_serial *serial,
7053f542974SPaul B Schroeder 					 const char *function)
7063f542974SPaul B Schroeder {
7073f542974SPaul B Schroeder 	if (!serial) {
7089c134a14SGreg Kroah-Hartman 		pr_debug("%s - serial == NULL\n", function);
7093f542974SPaul B Schroeder 		return -1;
7103f542974SPaul B Schroeder 	}
7113f542974SPaul B Schroeder 	if (!serial->type) {
7129c134a14SGreg Kroah-Hartman 		pr_debug("%s - serial->type == NULL!\n", function);
7133f542974SPaul B Schroeder 		return -1;
7143f542974SPaul B Schroeder 	}
7153f542974SPaul B Schroeder 
7163f542974SPaul B Schroeder 	return 0;
7173f542974SPaul B Schroeder }
7183f542974SPaul B Schroeder 
7193f542974SPaul B Schroeder static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
7203f542974SPaul B Schroeder 						 const char *function)
7213f542974SPaul B Schroeder {
7223f542974SPaul B Schroeder 	/* if no port was specified, or it fails a paranoia check */
7233f542974SPaul B Schroeder 	if (!port ||
7243f542974SPaul B Schroeder 	    mos7840_port_paranoia_check(port, function) ||
7253f542974SPaul B Schroeder 	    mos7840_serial_paranoia_check(port->serial, function)) {
726880af9dbSAlan Cox 		/* then say that we don't have a valid usb_serial thing,
727880af9dbSAlan Cox 		 * which will end up genrating -ENODEV return values */
7283f542974SPaul B Schroeder 		return NULL;
7293f542974SPaul B Schroeder 	}
7303f542974SPaul B Schroeder 
7313f542974SPaul B Schroeder 	return port->serial;
7323f542974SPaul B Schroeder }
7333f542974SPaul B Schroeder 
7343f542974SPaul B Schroeder /*****************************************************************************
7353f542974SPaul B Schroeder  * mos7840_bulk_in_callback
7363f542974SPaul B Schroeder  *	this is the callback function for when we have received data on the
7373f542974SPaul B Schroeder  *	bulk in endpoint.
7383f542974SPaul B Schroeder  *****************************************************************************/
7393f542974SPaul B Schroeder 
7407d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
7413f542974SPaul B Schroeder {
7420643c724SGreg Kroah-Hartman 	int retval;
7433f542974SPaul B Schroeder 	unsigned char *data;
7443f542974SPaul B Schroeder 	struct usb_serial *serial;
7453f542974SPaul B Schroeder 	struct usb_serial_port *port;
7463f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
7473f542974SPaul B Schroeder 	struct tty_struct *tty;
7480643c724SGreg Kroah-Hartman 	int status = urb->status;
7493f542974SPaul B Schroeder 
750cdc97792SMing Lei 	mos7840_port = urb->context;
7519c134a14SGreg Kroah-Hartman 	if (!mos7840_port)
75250de36f7SGreg Kroah-Hartman 		return;
75350de36f7SGreg Kroah-Hartman 
75450de36f7SGreg Kroah-Hartman 	if (status) {
7559c134a14SGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
75650de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7573f542974SPaul B Schroeder 		return;
7583f542974SPaul B Schroeder 	}
7593f542974SPaul B Schroeder 
7609c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
761441b62c1SHarvey Harrison 	if (mos7840_port_paranoia_check(port, __func__)) {
76250de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7633f542974SPaul B Schroeder 		return;
7643f542974SPaul B Schroeder 	}
7653f542974SPaul B Schroeder 
766441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
7673f542974SPaul B Schroeder 	if (!serial) {
76850de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7693f542974SPaul B Schroeder 		return;
7703f542974SPaul B Schroeder 	}
7713f542974SPaul B Schroeder 
7723f542974SPaul B Schroeder 	data = urb->transfer_buffer;
77359d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
7743f542974SPaul B Schroeder 
7753f542974SPaul B Schroeder 	if (urb->actual_length) {
776*05c7cd39SJiri Slaby 		struct tty_port *tport = &mos7840_port->port->port;
777*05c7cd39SJiri Slaby 		tty = tty_port_tty_get(tport);
7783f542974SPaul B Schroeder 		if (tty) {
779*05c7cd39SJiri Slaby 			tty_insert_flip_string(tport, data, urb->actual_length);
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();
7859c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx);
7863f542974SPaul B Schroeder 	}
7873f542974SPaul B Schroeder 
7883f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
7899c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
79050de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
7913f542974SPaul B Schroeder 		return;
7923f542974SPaul B Schroeder 	}
7933f542974SPaul B Schroeder 
7940eafe4deSDonald 	/* Turn on LED */
7950eafe4deSDonald 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
7960eafe4deSDonald 		mos7840_port->led_flag = true;
7970eafe4deSDonald 		mos7840_set_led_async(mos7840_port, 0x0301,
7980eafe4deSDonald 					MODEM_CONTROL_REGISTER);
7990eafe4deSDonald 		mod_timer(&mos7840_port->led_timer1,
8000eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_ON_MS));
8010eafe4deSDonald 	}
8020de9a702SOliver Neukum 
80350de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
8040643c724SGreg Kroah-Hartman 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
8053f542974SPaul B Schroeder 
8060643c724SGreg Kroah-Hartman 	if (retval) {
8079c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
80850de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
8093f542974SPaul B Schroeder 	}
8103f542974SPaul B Schroeder }
8113f542974SPaul B Schroeder 
8123f542974SPaul B Schroeder /*****************************************************************************
8133f542974SPaul B Schroeder  * mos7840_bulk_out_data_callback
814880af9dbSAlan Cox  *	this is the callback function for when we have finished sending
815880af9dbSAlan Cox  *	serial data on the bulk out endpoint.
8163f542974SPaul B Schroeder  *****************************************************************************/
8173f542974SPaul B Schroeder 
8187d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
8193f542974SPaul B Schroeder {
8203f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8219c134a14SGreg Kroah-Hartman 	struct usb_serial_port *port;
8223f542974SPaul B Schroeder 	struct tty_struct *tty;
8230643c724SGreg Kroah-Hartman 	int status = urb->status;
8240de9a702SOliver Neukum 	int i;
8250de9a702SOliver Neukum 
826cdc97792SMing Lei 	mos7840_port = urb->context;
8279c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
8280de9a702SOliver Neukum 	spin_lock(&mos7840_port->pool_lock);
8290de9a702SOliver Neukum 	for (i = 0; i < NUM_URBS; i++) {
8300de9a702SOliver Neukum 		if (urb == mos7840_port->write_urb_pool[i]) {
8310de9a702SOliver Neukum 			mos7840_port->busy[i] = 0;
8320de9a702SOliver Neukum 			break;
8330de9a702SOliver Neukum 		}
8340de9a702SOliver Neukum 	}
8350de9a702SOliver Neukum 	spin_unlock(&mos7840_port->pool_lock);
8360de9a702SOliver Neukum 
8370643c724SGreg Kroah-Hartman 	if (status) {
8389c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
8393f542974SPaul B Schroeder 		return;
8403f542974SPaul B Schroeder 	}
8413f542974SPaul B Schroeder 
8429c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
8433f542974SPaul B Schroeder 		return;
8443f542974SPaul B Schroeder 
8459c134a14SGreg Kroah-Hartman 	tty = tty_port_tty_get(&port->port);
846b963a844SJiri Slaby 	if (tty && mos7840_port->open)
847b963a844SJiri Slaby 		tty_wakeup(tty);
8484a90f09bSAlan Cox 	tty_kref_put(tty);
8493f542974SPaul B Schroeder 
8503f542974SPaul B Schroeder }
8513f542974SPaul B Schroeder 
8523f542974SPaul B Schroeder /************************************************************************/
8533f542974SPaul 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       */
8543f542974SPaul B Schroeder /************************************************************************/
8553f542974SPaul B Schroeder #ifdef MCSSerialProbe
8563f542974SPaul B Schroeder static int mos7840_serial_probe(struct usb_serial *serial,
8573f542974SPaul B Schroeder 				const struct usb_device_id *id)
8583f542974SPaul B Schroeder {
8593f542974SPaul B Schroeder 
8603f542974SPaul B Schroeder 	/*need to implement the mode_reg reading and updating\
8613f542974SPaul B Schroeder 	   structures usb_serial_ device_type\
8623f542974SPaul B Schroeder 	   (i.e num_ports, num_bulkin,bulkout etc) */
8633f542974SPaul B Schroeder 	/* Also we can update the changes  attach */
8643f542974SPaul B Schroeder 	return 1;
8653f542974SPaul B Schroeder }
8663f542974SPaul B Schroeder #endif
8673f542974SPaul B Schroeder 
8683f542974SPaul B Schroeder /*****************************************************************************
8693f542974SPaul B Schroeder  * mos7840_open
8703f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is opened
8713f542974SPaul B Schroeder  *	If successful, we return 0
8723f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
8733f542974SPaul B Schroeder  *****************************************************************************/
8743f542974SPaul B Schroeder 
875a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
8763f542974SPaul B Schroeder {
8773f542974SPaul B Schroeder 	int response;
8783f542974SPaul B Schroeder 	int j;
8793f542974SPaul B Schroeder 	struct usb_serial *serial;
8803f542974SPaul B Schroeder 	struct urb *urb;
8813f542974SPaul B Schroeder 	__u16 Data;
8823f542974SPaul B Schroeder 	int status;
8833f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
8840de9a702SOliver Neukum 	struct moschip_port *port0;
8853f542974SPaul B Schroeder 
8869c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
8873f542974SPaul B Schroeder 		return -ENODEV;
8883f542974SPaul B Schroeder 
8893f542974SPaul B Schroeder 	serial = port->serial;
8903f542974SPaul B Schroeder 
8919c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
8923f542974SPaul B Schroeder 		return -ENODEV;
8933f542974SPaul B Schroeder 
8943f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
8950de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
8963f542974SPaul B Schroeder 
8970de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
8983f542974SPaul B Schroeder 		return -ENODEV;
8993f542974SPaul B Schroeder 
9003f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->write_urb->pipe);
9013f542974SPaul B Schroeder 	usb_clear_halt(serial->dev, port->read_urb->pipe);
9020de9a702SOliver Neukum 	port0->open_ports++;
9033f542974SPaul B Schroeder 
9043f542974SPaul B Schroeder 	/* Initialising the write urb pool */
9053f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
9060de9a702SOliver Neukum 		urb = usb_alloc_urb(0, GFP_KERNEL);
9073f542974SPaul B Schroeder 		mos7840_port->write_urb_pool[j] = urb;
9083f542974SPaul B Schroeder 
9093f542974SPaul B Schroeder 		if (urb == NULL) {
910194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "No more urbs???\n");
9113f542974SPaul B Schroeder 			continue;
9123f542974SPaul B Schroeder 		}
9133f542974SPaul B Schroeder 
914880af9dbSAlan Cox 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
915880af9dbSAlan Cox 								GFP_KERNEL);
9163f542974SPaul B Schroeder 		if (!urb->transfer_buffer) {
9170de9a702SOliver Neukum 			usb_free_urb(urb);
9180de9a702SOliver Neukum 			mos7840_port->write_urb_pool[j] = NULL;
919194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
920194343d9SGreg Kroah-Hartman 				"%s-out of memory for urb buffers.\n",
921194343d9SGreg Kroah-Hartman 				__func__);
9223f542974SPaul B Schroeder 			continue;
9233f542974SPaul B Schroeder 		}
9243f542974SPaul B Schroeder 	}
9253f542974SPaul B Schroeder 
9263f542974SPaul B Schroeder /*****************************************************************************
9273f542974SPaul B Schroeder  * Initialize MCS7840 -- Write Init values to corresponding Registers
9283f542974SPaul B Schroeder  *
9293f542974SPaul B Schroeder  * Register Index
9303f542974SPaul B Schroeder  * 1 : IER
9313f542974SPaul B Schroeder  * 2 : FCR
9323f542974SPaul B Schroeder  * 3 : LCR
9333f542974SPaul B Schroeder  * 4 : MCR
9343f542974SPaul B Schroeder  *
9353f542974SPaul B Schroeder  * 0x08 : SP1/2 Control Reg
9363f542974SPaul B Schroeder  *****************************************************************************/
9373f542974SPaul B Schroeder 
938880af9dbSAlan Cox 	/* NEED to check the following Block */
9393f542974SPaul B Schroeder 
9403f542974SPaul B Schroeder 	Data = 0x0;
9413f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
9423f542974SPaul B Schroeder 	if (status < 0) {
9439c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Spreg failed\n");
9443f542974SPaul B Schroeder 		return -1;
9453f542974SPaul B Schroeder 	}
9463f542974SPaul B Schroeder 	Data |= 0x80;
9473f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9483f542974SPaul B Schroeder 	if (status < 0) {
9499c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
9503f542974SPaul B Schroeder 		return -1;
9513f542974SPaul B Schroeder 	}
9523f542974SPaul B Schroeder 
9533f542974SPaul B Schroeder 	Data &= ~0x80;
9543f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
9553f542974SPaul B Schroeder 	if (status < 0) {
9569c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Spreg failed\n");
9573f542974SPaul B Schroeder 		return -1;
9583f542974SPaul B Schroeder 	}
959880af9dbSAlan Cox 	/* End of block to be checked */
9603f542974SPaul B Schroeder 
9613f542974SPaul B Schroeder 	Data = 0x0;
962880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
963880af9dbSAlan Cox 									&Data);
9643f542974SPaul B Schroeder 	if (status < 0) {
9659c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Reading Controlreg failed\n");
9663f542974SPaul B Schroeder 		return -1;
9673f542974SPaul B Schroeder 	}
968880af9dbSAlan Cox 	Data |= 0x08;		/* Driver done bit */
969880af9dbSAlan Cox 	Data |= 0x20;		/* rx_disable */
970880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port,
971880af9dbSAlan Cox 				mos7840_port->ControlRegOffset, Data);
9723f542974SPaul B Schroeder 	if (status < 0) {
9739c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "writing Controlreg failed\n");
9743f542974SPaul B Schroeder 		return -1;
9753f542974SPaul B Schroeder 	}
976880af9dbSAlan Cox 	/* do register settings here */
977880af9dbSAlan Cox 	/* Set all regs to the device default values. */
978880af9dbSAlan Cox 	/***********************************
979880af9dbSAlan Cox 	 * First Disable all interrupts.
980880af9dbSAlan Cox 	 ***********************************/
9813f542974SPaul B Schroeder 	Data = 0x00;
9823f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
9833f542974SPaul B Schroeder 	if (status < 0) {
9849c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "disabling interrupts failed\n");
9853f542974SPaul B Schroeder 		return -1;
9863f542974SPaul B Schroeder 	}
987880af9dbSAlan Cox 	/* Set FIFO_CONTROL_REGISTER to the default value */
9883f542974SPaul B Schroeder 	Data = 0x00;
9893f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9903f542974SPaul B Schroeder 	if (status < 0) {
9919c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
9923f542974SPaul B Schroeder 		return -1;
9933f542974SPaul B Schroeder 	}
9943f542974SPaul B Schroeder 
9953f542974SPaul B Schroeder 	Data = 0xcf;
9963f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
9973f542974SPaul B Schroeder 	if (status < 0) {
9989c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
9993f542974SPaul B Schroeder 		return -1;
10003f542974SPaul B Schroeder 	}
10013f542974SPaul B Schroeder 
10023f542974SPaul B Schroeder 	Data = 0x03;
10033f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10043f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10053f542974SPaul B Schroeder 
10063f542974SPaul B Schroeder 	Data = 0x0b;
10073f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
10083f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
10093f542974SPaul B Schroeder 
10103f542974SPaul B Schroeder 	Data = 0x00;
10113f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
10123f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10133f542974SPaul B Schroeder 
1014880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
10153f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10163f542974SPaul B Schroeder 
10173f542974SPaul B Schroeder 	Data = 0x0c;
10183f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
10193f542974SPaul B Schroeder 
10203f542974SPaul B Schroeder 	Data = 0x0;
10213f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
10223f542974SPaul B Schroeder 
10233f542974SPaul B Schroeder 	Data = 0x00;
10243f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
10253f542974SPaul B Schroeder 
10263f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
10273f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
10283f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
10293f542974SPaul B Schroeder 
1030880af9dbSAlan Cox 	/* clearing Bulkin and Bulkout Fifo */
10313f542974SPaul B Schroeder 	Data = 0x0;
10323f542974SPaul B Schroeder 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
10333f542974SPaul B Schroeder 
10343f542974SPaul B Schroeder 	Data = Data | 0x0c;
10353f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
10363f542974SPaul B Schroeder 
10373f542974SPaul B Schroeder 	Data = Data & ~0x0c;
10383f542974SPaul B Schroeder 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1039880af9dbSAlan Cox 	/* Finally enable all interrupts */
10403f542974SPaul B Schroeder 	Data = 0x0c;
10413f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
10423f542974SPaul B Schroeder 
1043880af9dbSAlan Cox 	/* clearing rx_disable */
10443f542974SPaul B Schroeder 	Data = 0x0;
1045880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1046880af9dbSAlan Cox 									&Data);
10473f542974SPaul B Schroeder 	Data = Data & ~0x20;
1048880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1049880af9dbSAlan Cox 									Data);
10503f542974SPaul B Schroeder 
1051880af9dbSAlan Cox 	/* rx_negate */
10523f542974SPaul B Schroeder 	Data = 0x0;
1053880af9dbSAlan Cox 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1054880af9dbSAlan Cox 									&Data);
10553f542974SPaul B Schroeder 	Data = Data | 0x10;
1056880af9dbSAlan Cox 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1057880af9dbSAlan Cox 									Data);
10583f542974SPaul B Schroeder 
10593f542974SPaul B Schroeder 	/* Check to see if we've set up our endpoint info yet    *
10603f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the structures *
10613f542974SPaul B Schroeder 	 * were not set up at that time.)                        */
10620de9a702SOliver Neukum 	if (port0->open_ports == 1) {
10633f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_buffer == NULL) {
10643f542974SPaul B Schroeder 			/* set up interrupt urb */
10653f542974SPaul B Schroeder 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
10663f542974SPaul B Schroeder 				serial->dev,
10673f542974SPaul B Schroeder 				usb_rcvintpipe(serial->dev,
1068880af9dbSAlan Cox 				serial->port[0]->interrupt_in_endpointAddress),
10693f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_buffer,
10703f542974SPaul B Schroeder 				serial->port[0]->interrupt_in_urb->
10713f542974SPaul B Schroeder 				transfer_buffer_length,
10723f542974SPaul B Schroeder 				mos7840_interrupt_callback,
10733f542974SPaul B Schroeder 				serial,
1074880af9dbSAlan Cox 				serial->port[0]->interrupt_in_urb->interval);
10753f542974SPaul B Schroeder 
10763f542974SPaul B Schroeder 			/* start interrupt read for mos7840               *
10773f542974SPaul B Schroeder 			 * will continue as long as mos7840 is connected  */
10783f542974SPaul B Schroeder 
10793f542974SPaul B Schroeder 			response =
10803f542974SPaul B Schroeder 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
10813f542974SPaul B Schroeder 					   GFP_KERNEL);
10823f542974SPaul B Schroeder 			if (response) {
1083194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "%s - Error %d submitting "
1084194343d9SGreg Kroah-Hartman 					"interrupt urb\n", __func__, response);
10853f542974SPaul B Schroeder 			}
10863f542974SPaul B Schroeder 
10873f542974SPaul B Schroeder 		}
10883f542974SPaul B Schroeder 
10893f542974SPaul B Schroeder 	}
10903f542974SPaul B Schroeder 
10913f542974SPaul B Schroeder 	/* see if we've set up our endpoint info yet   *
10923f542974SPaul B Schroeder 	 * (can't set it up in mos7840_startup as the  *
10933f542974SPaul B Schroeder 	 * structures were not set up at that time.)   */
10943f542974SPaul B Schroeder 
10959c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port number is %d\n", port->number);
10969c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
10979c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
10989c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
10999c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
11009c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
11013f542974SPaul B Schroeder 	mos7840_port->read_urb = port->read_urb;
11023f542974SPaul B Schroeder 
11033f542974SPaul B Schroeder 	/* set up our bulk in urb */
1104093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1105093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1106093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1107093ea2d3SDonald Lee 		usb_fill_bulk_urb(mos7840_port->read_urb,
1108093ea2d3SDonald Lee 			serial->dev,
1109093ea2d3SDonald Lee 			usb_rcvbulkpipe(serial->dev,
1110093ea2d3SDonald Lee 				(port->bulk_in_endpointAddress) + 2),
1111093ea2d3SDonald Lee 			port->bulk_in_buffer,
1112093ea2d3SDonald Lee 			mos7840_port->read_urb->transfer_buffer_length,
1113093ea2d3SDonald Lee 			mos7840_bulk_in_callback, mos7840_port);
1114093ea2d3SDonald Lee 	} else {
11153f542974SPaul B Schroeder 		usb_fill_bulk_urb(mos7840_port->read_urb,
11163f542974SPaul B Schroeder 			serial->dev,
11173f542974SPaul B Schroeder 			usb_rcvbulkpipe(serial->dev,
11183f542974SPaul B Schroeder 				port->bulk_in_endpointAddress),
11193f542974SPaul B Schroeder 			port->bulk_in_buffer,
11203f542974SPaul B Schroeder 			mos7840_port->read_urb->transfer_buffer_length,
11213f542974SPaul B Schroeder 			mos7840_bulk_in_callback, mos7840_port);
1122093ea2d3SDonald Lee 	}
11233f542974SPaul B Schroeder 
11249c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
112550de36f7SGreg Kroah-Hartman 	mos7840_port->read_urb_busy = true;
11263f542974SPaul B Schroeder 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
11273f542974SPaul B Schroeder 	if (response) {
1128194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1129194343d9SGreg Kroah-Hartman 			__func__, response);
113050de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = false;
11313f542974SPaul B Schroeder 	}
11323f542974SPaul B Schroeder 
11333f542974SPaul B Schroeder 	/* initialize our wait queues */
11343f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->wait_chase);
11353f542974SPaul B Schroeder 	init_waitqueue_head(&mos7840_port->delta_msr_wait);
11363f542974SPaul B Schroeder 
11373f542974SPaul B Schroeder 	/* initialize our icount structure */
11383f542974SPaul B Schroeder 	memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
11393f542974SPaul B Schroeder 
11403f542974SPaul B Schroeder 	/* initialize our port settings */
1141880af9dbSAlan Cox 	/* Must set to enable ints! */
1142880af9dbSAlan Cox 	mos7840_port->shadowMCR = MCR_MASTER_IE;
11433f542974SPaul B Schroeder 	/* send a open port command */
11443f542974SPaul B Schroeder 	mos7840_port->open = 1;
1145880af9dbSAlan Cox 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
11463f542974SPaul B Schroeder 	mos7840_port->icount.tx = 0;
11473f542974SPaul B Schroeder 	mos7840_port->icount.rx = 0;
11483f542974SPaul B Schroeder 
11493f542974SPaul B Schroeder 	return 0;
11503f542974SPaul B Schroeder }
11513f542974SPaul B Schroeder 
11523f542974SPaul B Schroeder /*****************************************************************************
11533f542974SPaul B Schroeder  * mos7840_chars_in_buffer
11543f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
11553f542974SPaul B Schroeder  *	bytes of data we currently have outstanding in the port (data that has
11563f542974SPaul B Schroeder  *	been written, but hasn't made it out the port yet)
11573f542974SPaul B Schroeder  *	If successful, we return the number of bytes left to be written in the
11583f542974SPaul B Schroeder  *	system,
115995da310eSAlan Cox  *	Otherwise we return zero.
11603f542974SPaul B Schroeder  *****************************************************************************/
11613f542974SPaul B Schroeder 
116295da310eSAlan Cox static int mos7840_chars_in_buffer(struct tty_struct *tty)
11633f542974SPaul B Schroeder {
116495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11653f542974SPaul B Schroeder 	int i;
11663f542974SPaul B Schroeder 	int chars = 0;
11670de9a702SOliver Neukum 	unsigned long flags;
11683f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11693f542974SPaul B Schroeder 
11709c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
117195da310eSAlan Cox 		return 0;
11723f542974SPaul B Schroeder 
11733f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
11743363155bSGreg Kroah-Hartman 	if (mos7840_port == NULL)
117595da310eSAlan Cox 		return 0;
11763f542974SPaul B Schroeder 
11770de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
11785c263b92SMark Ferrell 	for (i = 0; i < NUM_URBS; ++i) {
11795c263b92SMark Ferrell 		if (mos7840_port->busy[i]) {
11805c263b92SMark Ferrell 			struct urb *urb = mos7840_port->write_urb_pool[i];
11815c263b92SMark Ferrell 			chars += urb->transfer_buffer_length;
11825c263b92SMark Ferrell 		}
11835c263b92SMark Ferrell 	}
11840de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
11859c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
11860de9a702SOliver Neukum 	return chars;
11873f542974SPaul B Schroeder 
11883f542974SPaul B Schroeder }
11893f542974SPaul B Schroeder 
11903f542974SPaul B Schroeder /*****************************************************************************
11913f542974SPaul B Schroeder  * mos7840_close
11923f542974SPaul B Schroeder  *	this function is called by the tty driver when a port is closed
11933f542974SPaul B Schroeder  *****************************************************************************/
11943f542974SPaul B Schroeder 
1195335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
11963f542974SPaul B Schroeder {
11973f542974SPaul B Schroeder 	struct usb_serial *serial;
11983f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
11990de9a702SOliver Neukum 	struct moschip_port *port0;
12003f542974SPaul B Schroeder 	int j;
12013f542974SPaul B Schroeder 	__u16 Data;
12023f542974SPaul B Schroeder 
12039c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
12043f542974SPaul B Schroeder 		return;
12053f542974SPaul B Schroeder 
1206441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
12079c134a14SGreg Kroah-Hartman 	if (!serial)
12083f542974SPaul B Schroeder 		return;
12093f542974SPaul B Schroeder 
12103f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
12110de9a702SOliver Neukum 	port0 = mos7840_get_port_private(serial->port[0]);
12123f542974SPaul B Schroeder 
12130de9a702SOliver Neukum 	if (mos7840_port == NULL || port0 == NULL)
12143f542974SPaul B Schroeder 		return;
12153f542974SPaul B Schroeder 
12163f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j)
12173f542974SPaul B Schroeder 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
12183f542974SPaul B Schroeder 
12193f542974SPaul B Schroeder 	/* Freeing Write URBs */
12203f542974SPaul B Schroeder 	for (j = 0; j < NUM_URBS; ++j) {
12213f542974SPaul B Schroeder 		if (mos7840_port->write_urb_pool[j]) {
12223f542974SPaul B Schroeder 			if (mos7840_port->write_urb_pool[j]->transfer_buffer)
12233f542974SPaul B Schroeder 				kfree(mos7840_port->write_urb_pool[j]->
12243f542974SPaul B Schroeder 				      transfer_buffer);
12253f542974SPaul B Schroeder 
12263f542974SPaul B Schroeder 			usb_free_urb(mos7840_port->write_urb_pool[j]);
12273f542974SPaul B Schroeder 		}
12283f542974SPaul B Schroeder 	}
12293f542974SPaul B Schroeder 
12303f542974SPaul B Schroeder 	/* While closing port, shutdown all bulk read, write  *
12313f542974SPaul B Schroeder 	 * and interrupt read if they exists                  */
12323f542974SPaul B Schroeder 	if (serial->dev) {
12333f542974SPaul B Schroeder 		if (mos7840_port->write_urb) {
12349c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s", "Shutdown bulk write\n");
12353f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->write_urb);
12363f542974SPaul B Schroeder 		}
12373f542974SPaul B Schroeder 		if (mos7840_port->read_urb) {
12389c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s", "Shutdown bulk read\n");
12393f542974SPaul B Schroeder 			usb_kill_urb(mos7840_port->read_urb);
124050de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
12413f542974SPaul B Schroeder 		}
12423f542974SPaul B Schroeder 		if ((&mos7840_port->control_urb)) {
12439c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s", "Shutdown control read\n");
1244880af9dbSAlan Cox 			/*/      usb_kill_urb (mos7840_port->control_urb); */
12453f542974SPaul B Schroeder 		}
12463f542974SPaul B Schroeder 	}
1247880af9dbSAlan Cox /*      if(mos7840_port->ctrl_buf != NULL) */
1248880af9dbSAlan Cox /*              kfree(mos7840_port->ctrl_buf); */
12490de9a702SOliver Neukum 	port0->open_ports--;
12509c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
12510de9a702SOliver Neukum 	if (port0->open_ports == 0) {
12523f542974SPaul B Schroeder 		if (serial->port[0]->interrupt_in_urb) {
12539c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
12540de9a702SOliver Neukum 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
12553f542974SPaul B Schroeder 		}
12563f542974SPaul B Schroeder 	}
12573f542974SPaul B Schroeder 
12583f542974SPaul B Schroeder 	if (mos7840_port->write_urb) {
12593f542974SPaul B Schroeder 		/* if this urb had a transfer buffer already (old tx) free it */
1260880af9dbSAlan Cox 		if (mos7840_port->write_urb->transfer_buffer != NULL)
12613f542974SPaul B Schroeder 			kfree(mos7840_port->write_urb->transfer_buffer);
12623f542974SPaul B Schroeder 		usb_free_urb(mos7840_port->write_urb);
12633f542974SPaul B Schroeder 	}
12643f542974SPaul B Schroeder 
12653f542974SPaul B Schroeder 	Data = 0x0;
12663f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
12673f542974SPaul B Schroeder 
12683f542974SPaul B Schroeder 	Data = 0x00;
12693f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
12703f542974SPaul B Schroeder 
12713f542974SPaul B Schroeder 	mos7840_port->open = 0;
12723f542974SPaul B Schroeder }
12733f542974SPaul B Schroeder 
12743f542974SPaul B Schroeder /************************************************************************
12753f542974SPaul B Schroeder  *
12763f542974SPaul B Schroeder  * mos7840_block_until_chase_response
12773f542974SPaul B Schroeder  *
12783f542974SPaul B Schroeder  *	This function will block the close until one of the following:
12793f542974SPaul B Schroeder  *		1. Response to our Chase comes from mos7840
1280dc0d5c1eSJoe Perches  *		2. A timeout of 10 seconds without activity has expired
12813f542974SPaul B Schroeder  *		   (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
12823f542974SPaul B Schroeder  *
12833f542974SPaul B Schroeder  ************************************************************************/
12843f542974SPaul B Schroeder 
128595da310eSAlan Cox static void mos7840_block_until_chase_response(struct tty_struct *tty,
128695da310eSAlan Cox 					struct moschip_port *mos7840_port)
12873f542974SPaul B Schroeder {
12881e658489SMark Ferrell 	int timeout = msecs_to_jiffies(1000);
12893f542974SPaul B Schroeder 	int wait = 10;
12903f542974SPaul B Schroeder 	int count;
12913f542974SPaul B Schroeder 
12923f542974SPaul B Schroeder 	while (1) {
129395da310eSAlan Cox 		count = mos7840_chars_in_buffer(tty);
12943f542974SPaul B Schroeder 
12953f542974SPaul B Schroeder 		/* Check for Buffer status */
1296880af9dbSAlan Cox 		if (count <= 0)
12973f542974SPaul B Schroeder 			return;
12983f542974SPaul B Schroeder 
12993f542974SPaul B Schroeder 		/* Block the thread for a while */
13003f542974SPaul B Schroeder 		interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
13013f542974SPaul B Schroeder 					       timeout);
13023f542974SPaul B Schroeder 		/* No activity.. count down section */
13033f542974SPaul B Schroeder 		wait--;
13043f542974SPaul B Schroeder 		if (wait == 0) {
13059c134a14SGreg Kroah-Hartman 			dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
13063f542974SPaul B Schroeder 			return;
13073f542974SPaul B Schroeder 		} else {
1308dc0d5c1eSJoe Perches 			/* Reset timeout value back to seconds */
13093f542974SPaul B Schroeder 			wait = 10;
13103f542974SPaul B Schroeder 		}
13113f542974SPaul B Schroeder 	}
13123f542974SPaul B Schroeder 
13133f542974SPaul B Schroeder }
13143f542974SPaul B Schroeder 
13153f542974SPaul B Schroeder /*****************************************************************************
13163f542974SPaul B Schroeder  * mos7840_break
13173f542974SPaul B Schroeder  *	this function sends a break to the port
13183f542974SPaul B Schroeder  *****************************************************************************/
131995da310eSAlan Cox static void mos7840_break(struct tty_struct *tty, int break_state)
13203f542974SPaul B Schroeder {
132195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
13223f542974SPaul B Schroeder 	unsigned char data;
13233f542974SPaul B Schroeder 	struct usb_serial *serial;
13243f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13253f542974SPaul B Schroeder 
13269c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
13273f542974SPaul B Schroeder 		return;
13283f542974SPaul B Schroeder 
1329441b62c1SHarvey Harrison 	serial = mos7840_get_usb_serial(port, __func__);
13309c134a14SGreg Kroah-Hartman 	if (!serial)
13313f542974SPaul B Schroeder 		return;
13323f542974SPaul B Schroeder 
13333f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13343f542974SPaul B Schroeder 
1335880af9dbSAlan Cox 	if (mos7840_port == NULL)
13363f542974SPaul B Schroeder 		return;
13373f542974SPaul B Schroeder 
133895da310eSAlan Cox 	if (serial->dev)
13393f542974SPaul B Schroeder 		/* flush and block until tx is empty */
134095da310eSAlan Cox 		mos7840_block_until_chase_response(tty, mos7840_port);
13413f542974SPaul B Schroeder 
134295da310eSAlan Cox 	if (break_state == -1)
13433f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
134495da310eSAlan Cox 	else
13453f542974SPaul B Schroeder 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
13463f542974SPaul B Schroeder 
13476b447f04SAlan Cox 	/* FIXME: no locking on shadowLCR anywhere in driver */
13483f542974SPaul B Schroeder 	mos7840_port->shadowLCR = data;
13499c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
13503f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
13513f542974SPaul B Schroeder 			     mos7840_port->shadowLCR);
13523f542974SPaul B Schroeder }
13533f542974SPaul B Schroeder 
13543f542974SPaul B Schroeder /*****************************************************************************
13553f542974SPaul B Schroeder  * mos7840_write_room
13563f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to know how many
13573f542974SPaul B Schroeder  *	bytes of data we can accept for a specific port.
13583f542974SPaul B Schroeder  *	If successful, we return the amount of room that we have for this port
13593f542974SPaul B Schroeder  *	Otherwise we return a negative error number.
13603f542974SPaul B Schroeder  *****************************************************************************/
13613f542974SPaul B Schroeder 
136295da310eSAlan Cox static int mos7840_write_room(struct tty_struct *tty)
13633f542974SPaul B Schroeder {
136495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
13653f542974SPaul B Schroeder 	int i;
13663f542974SPaul B Schroeder 	int room = 0;
13670de9a702SOliver Neukum 	unsigned long flags;
13683f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
13693f542974SPaul B Schroeder 
13709c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
13713f542974SPaul B Schroeder 		return -1;
13723f542974SPaul B Schroeder 
13733f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
13749c134a14SGreg Kroah-Hartman 	if (mos7840_port == NULL)
13753f542974SPaul B Schroeder 		return -1;
13763f542974SPaul B Schroeder 
13770de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
13783f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
1379880af9dbSAlan Cox 		if (!mos7840_port->busy[i])
13803f542974SPaul B Schroeder 			room += URB_TRANSFER_BUFFER_SIZE;
13813f542974SPaul B Schroeder 	}
13820de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
13833f542974SPaul B Schroeder 
13840de9a702SOliver Neukum 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
13859c134a14SGreg Kroah-Hartman 	dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
13860de9a702SOliver Neukum 	return room;
13873f542974SPaul B Schroeder 
13883f542974SPaul B Schroeder }
13893f542974SPaul B Schroeder 
13903f542974SPaul B Schroeder /*****************************************************************************
13913f542974SPaul B Schroeder  * mos7840_write
13923f542974SPaul B Schroeder  *	this function is called by the tty driver when data should be written to
13933f542974SPaul B Schroeder  *	the port.
13943f542974SPaul B Schroeder  *	If successful, we return the number of bytes written, otherwise we
13953f542974SPaul B Schroeder  *      return a negative error number.
13963f542974SPaul B Schroeder  *****************************************************************************/
13973f542974SPaul B Schroeder 
139895da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
13993f542974SPaul B Schroeder 			 const unsigned char *data, int count)
14003f542974SPaul B Schroeder {
14013f542974SPaul B Schroeder 	int status;
14023f542974SPaul B Schroeder 	int i;
14033f542974SPaul B Schroeder 	int bytes_sent = 0;
14043f542974SPaul B Schroeder 	int transfer_size;
14050de9a702SOliver Neukum 	unsigned long flags;
14063f542974SPaul B Schroeder 
14073f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
14083f542974SPaul B Schroeder 	struct usb_serial *serial;
14093f542974SPaul B Schroeder 	struct urb *urb;
1410880af9dbSAlan Cox 	/* __u16 Data; */
14113f542974SPaul B Schroeder 	const unsigned char *current_position = data;
14123f542974SPaul B Schroeder 	unsigned char *data1;
14133f542974SPaul B Schroeder 
14143f542974SPaul B Schroeder #ifdef NOTMOS7840
14153f542974SPaul B Schroeder 	Data = 0x00;
14163f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
14173f542974SPaul B Schroeder 	mos7840_port->shadowLCR = Data;
14189c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
14199c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
14203f542974SPaul B Schroeder 
1421880af9dbSAlan Cox 	/* Data = 0x03; */
1422880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1423880af9dbSAlan Cox 	/* mos7840_port->shadowLCR=Data;//Need to add later */
14243f542974SPaul B Schroeder 
1425880af9dbSAlan Cox 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
14263f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14273f542974SPaul B Schroeder 
1428880af9dbSAlan Cox 	/* Data = 0x0c; */
1429880af9dbSAlan Cox 	/* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
14303f542974SPaul B Schroeder 	Data = 0x00;
14313f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
14329c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
14333f542974SPaul B Schroeder 
14343f542974SPaul B Schroeder 	Data = 0x0;
14353f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
14369c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
14373f542974SPaul B Schroeder 
14383f542974SPaul B Schroeder 	Data = Data & ~SERIAL_LCR_DLAB;
14399c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
14403f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
14413f542974SPaul B Schroeder #endif
14423f542974SPaul B Schroeder 
14439c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
14443f542974SPaul B Schroeder 		return -1;
14453f542974SPaul B Schroeder 
14463f542974SPaul B Schroeder 	serial = port->serial;
14479c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
14483f542974SPaul B Schroeder 		return -1;
14493f542974SPaul B Schroeder 
14503f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
14519c134a14SGreg Kroah-Hartman 	if (mos7840_port == NULL)
14523f542974SPaul B Schroeder 		return -1;
14533f542974SPaul B Schroeder 
14543f542974SPaul B Schroeder 	/* try to find a free urb in the list */
14553f542974SPaul B Schroeder 	urb = NULL;
14563f542974SPaul B Schroeder 
14570de9a702SOliver Neukum 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
14583f542974SPaul B Schroeder 	for (i = 0; i < NUM_URBS; ++i) {
14590de9a702SOliver Neukum 		if (!mos7840_port->busy[i]) {
14600de9a702SOliver Neukum 			mos7840_port->busy[i] = 1;
14613f542974SPaul B Schroeder 			urb = mos7840_port->write_urb_pool[i];
14629c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "URB:%d\n", i);
14633f542974SPaul B Schroeder 			break;
14643f542974SPaul B Schroeder 		}
14653f542974SPaul B Schroeder 	}
14660de9a702SOliver Neukum 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
14673f542974SPaul B Schroeder 
14683f542974SPaul B Schroeder 	if (urb == NULL) {
14699c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
14703f542974SPaul B Schroeder 		goto exit;
14713f542974SPaul B Schroeder 	}
14723f542974SPaul B Schroeder 
14733f542974SPaul B Schroeder 	if (urb->transfer_buffer == NULL) {
14743f542974SPaul B Schroeder 		urb->transfer_buffer =
14753f542974SPaul B Schroeder 		    kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
14763f542974SPaul B Schroeder 
14773f542974SPaul B Schroeder 		if (urb->transfer_buffer == NULL) {
147822a416c4SJohan Hovold 			dev_err_console(port, "%s no more kernel memory...\n",
1479194343d9SGreg Kroah-Hartman 				__func__);
14803f542974SPaul B Schroeder 			goto exit;
14813f542974SPaul B Schroeder 		}
14823f542974SPaul B Schroeder 	}
14833f542974SPaul B Schroeder 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
14843f542974SPaul B Schroeder 
14853f542974SPaul B Schroeder 	memcpy(urb->transfer_buffer, current_position, transfer_size);
14863f542974SPaul B Schroeder 
14873f542974SPaul B Schroeder 	/* fill urb with data and submit  */
1488093ea2d3SDonald Lee 	if ((serial->num_ports == 2)
1489093ea2d3SDonald Lee 		&& ((((__u16)port->number -
1490093ea2d3SDonald Lee 			(__u16)(port->serial->minor)) % 2) != 0)) {
1491093ea2d3SDonald Lee 		usb_fill_bulk_urb(urb,
1492093ea2d3SDonald Lee 			serial->dev,
1493093ea2d3SDonald Lee 			usb_sndbulkpipe(serial->dev,
1494093ea2d3SDonald Lee 				(port->bulk_out_endpointAddress) + 2),
1495093ea2d3SDonald Lee 			urb->transfer_buffer,
1496093ea2d3SDonald Lee 			transfer_size,
1497093ea2d3SDonald Lee 			mos7840_bulk_out_data_callback, mos7840_port);
1498093ea2d3SDonald Lee 	} else {
14993f542974SPaul B Schroeder 		usb_fill_bulk_urb(urb,
15003f542974SPaul B Schroeder 			serial->dev,
15013f542974SPaul B Schroeder 			usb_sndbulkpipe(serial->dev,
15023f542974SPaul B Schroeder 				port->bulk_out_endpointAddress),
15033f542974SPaul B Schroeder 			urb->transfer_buffer,
15043f542974SPaul B Schroeder 			transfer_size,
15053f542974SPaul B Schroeder 			mos7840_bulk_out_data_callback, mos7840_port);
1506093ea2d3SDonald Lee 	}
15073f542974SPaul B Schroeder 
15083f542974SPaul B Schroeder 	data1 = urb->transfer_buffer;
15099c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
15103f542974SPaul B Schroeder 
15110eafe4deSDonald 	/* Turn on LED */
15120eafe4deSDonald 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
15130eafe4deSDonald 		mos7840_port->led_flag = true;
15140eafe4deSDonald 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
15150eafe4deSDonald 		mod_timer(&mos7840_port->led_timer1,
15160eafe4deSDonald 				jiffies + msecs_to_jiffies(LED_ON_MS));
15170eafe4deSDonald 	}
15180eafe4deSDonald 
15193f542974SPaul B Schroeder 	/* send it down the pipe */
15203f542974SPaul B Schroeder 	status = usb_submit_urb(urb, GFP_ATOMIC);
15213f542974SPaul B Schroeder 
15223f542974SPaul B Schroeder 	if (status) {
15230de9a702SOliver Neukum 		mos7840_port->busy[i] = 0;
152422a416c4SJohan Hovold 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1525194343d9SGreg Kroah-Hartman 			"with status = %d\n", __func__, status);
15263f542974SPaul B Schroeder 		bytes_sent = status;
15273f542974SPaul B Schroeder 		goto exit;
15283f542974SPaul B Schroeder 	}
15293f542974SPaul B Schroeder 	bytes_sent = transfer_size;
15303f542974SPaul B Schroeder 	mos7840_port->icount.tx += transfer_size;
15310de9a702SOliver Neukum 	smp_wmb();
15329c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
15333f542974SPaul B Schroeder exit:
15343f542974SPaul B Schroeder 	return bytes_sent;
15353f542974SPaul B Schroeder 
15363f542974SPaul B Schroeder }
15373f542974SPaul B Schroeder 
15383f542974SPaul B Schroeder /*****************************************************************************
15393f542974SPaul B Schroeder  * mos7840_throttle
15403f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to stop the data
15413f542974SPaul B Schroeder  *	being read from the port.
15423f542974SPaul B Schroeder  *****************************************************************************/
15433f542974SPaul B Schroeder 
154495da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
15453f542974SPaul B Schroeder {
154695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
15473f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
15483f542974SPaul B Schroeder 	int status;
15493f542974SPaul B Schroeder 
15509c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
15513f542974SPaul B Schroeder 		return;
15523f542974SPaul B Schroeder 
15533f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
15543f542974SPaul B Schroeder 
15553f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15563f542974SPaul B Schroeder 		return;
15573f542974SPaul B Schroeder 
15583f542974SPaul B Schroeder 	if (!mos7840_port->open) {
15599c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "port not opened\n");
15603f542974SPaul B Schroeder 		return;
15613f542974SPaul B Schroeder 	}
15623f542974SPaul B Schroeder 
15633f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the stop character */
15643f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
15653f542974SPaul B Schroeder 		unsigned char stop_char = STOP_CHAR(tty);
156695da310eSAlan Cox 		status = mos7840_write(tty, port, &stop_char, 1);
156795da310eSAlan Cox 		if (status <= 0)
15683f542974SPaul B Schroeder 			return;
15693f542974SPaul B Schroeder 	}
15703f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
1571adc8d746SAlan Cox 	if (tty->termios.c_cflag & CRTSCTS) {
15723f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~MCR_RTS;
157395da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
15743f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
157595da310eSAlan Cox 		if (status < 0)
15763f542974SPaul B Schroeder 			return;
15773f542974SPaul B Schroeder 	}
15783f542974SPaul B Schroeder }
15793f542974SPaul B Schroeder 
15803f542974SPaul B Schroeder /*****************************************************************************
15813f542974SPaul B Schroeder  * mos7840_unthrottle
1582880af9dbSAlan Cox  *	this function is called by the tty driver when it wants to resume
1583880af9dbSAlan Cox  *	the data being read from the port (called after mos7840_throttle is
1584880af9dbSAlan Cox  *	called)
15853f542974SPaul B Schroeder  *****************************************************************************/
158695da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
15873f542974SPaul B Schroeder {
158895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
15893f542974SPaul B Schroeder 	int status;
15903f542974SPaul B Schroeder 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
15913f542974SPaul B Schroeder 
15929c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
15933f542974SPaul B Schroeder 		return;
15943f542974SPaul B Schroeder 
15953f542974SPaul B Schroeder 	if (mos7840_port == NULL)
15963f542974SPaul B Schroeder 		return;
15973f542974SPaul B Schroeder 
15983f542974SPaul B Schroeder 	if (!mos7840_port->open) {
15999c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
16003f542974SPaul B Schroeder 		return;
16013f542974SPaul B Schroeder 	}
16023f542974SPaul B Schroeder 
16033f542974SPaul B Schroeder 	/* if we are implementing XON/XOFF, send the start character */
16043f542974SPaul B Schroeder 	if (I_IXOFF(tty)) {
16053f542974SPaul B Schroeder 		unsigned char start_char = START_CHAR(tty);
160695da310eSAlan Cox 		status = mos7840_write(tty, port, &start_char, 1);
160795da310eSAlan Cox 		if (status <= 0)
16083f542974SPaul B Schroeder 			return;
16093f542974SPaul B Schroeder 	}
16103f542974SPaul B Schroeder 
16113f542974SPaul B Schroeder 	/* if we are implementing RTS/CTS, toggle that line */
1612adc8d746SAlan Cox 	if (tty->termios.c_cflag & CRTSCTS) {
16133f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= MCR_RTS;
161495da310eSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
16153f542974SPaul B Schroeder 					 mos7840_port->shadowMCR);
161695da310eSAlan Cox 		if (status < 0)
16173f542974SPaul B Schroeder 			return;
16183f542974SPaul B Schroeder 	}
16193f542974SPaul B Schroeder }
16203f542974SPaul B Schroeder 
162160b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
16223f542974SPaul B Schroeder {
162395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16243f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
16253f542974SPaul B Schroeder 	unsigned int result;
16263f542974SPaul B Schroeder 	__u16 msr;
16273f542974SPaul B Schroeder 	__u16 mcr;
1628880af9dbSAlan Cox 	int status;
16293f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
16303f542974SPaul B Schroeder 
16313f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16323f542974SPaul B Schroeder 		return -ENODEV;
16333f542974SPaul B Schroeder 
16343f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
16353f542974SPaul B Schroeder 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
16363f542974SPaul B Schroeder 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
16373f542974SPaul B Schroeder 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
16383f542974SPaul B Schroeder 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
16393f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
16403f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
16413f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
16423f542974SPaul B Schroeder 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
16433f542974SPaul B Schroeder 
16449c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
16453f542974SPaul B Schroeder 
16463f542974SPaul B Schroeder 	return result;
16473f542974SPaul B Schroeder }
16483f542974SPaul B Schroeder 
164920b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
16503f542974SPaul B Schroeder 			    unsigned int set, unsigned int clear)
16513f542974SPaul B Schroeder {
165295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
16533f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
16543f542974SPaul B Schroeder 	unsigned int mcr;
165587521c46SRoel Kluin 	int status;
16563f542974SPaul B Schroeder 
16573f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
16583f542974SPaul B Schroeder 
16593f542974SPaul B Schroeder 	if (mos7840_port == NULL)
16603f542974SPaul B Schroeder 		return -ENODEV;
16613f542974SPaul B Schroeder 
1662e2984494SAlan Cox 	/* FIXME: What locks the port registers ? */
16633f542974SPaul B Schroeder 	mcr = mos7840_port->shadowMCR;
16643f542974SPaul B Schroeder 	if (clear & TIOCM_RTS)
16653f542974SPaul B Schroeder 		mcr &= ~MCR_RTS;
16663f542974SPaul B Schroeder 	if (clear & TIOCM_DTR)
16673f542974SPaul B Schroeder 		mcr &= ~MCR_DTR;
16683f542974SPaul B Schroeder 	if (clear & TIOCM_LOOP)
16693f542974SPaul B Schroeder 		mcr &= ~MCR_LOOPBACK;
16703f542974SPaul B Schroeder 
16713f542974SPaul B Schroeder 	if (set & TIOCM_RTS)
16723f542974SPaul B Schroeder 		mcr |= MCR_RTS;
16733f542974SPaul B Schroeder 	if (set & TIOCM_DTR)
16743f542974SPaul B Schroeder 		mcr |= MCR_DTR;
16753f542974SPaul B Schroeder 	if (set & TIOCM_LOOP)
16763f542974SPaul B Schroeder 		mcr |= MCR_LOOPBACK;
16773f542974SPaul B Schroeder 
16783f542974SPaul B Schroeder 	mos7840_port->shadowMCR = mcr;
16793f542974SPaul B Schroeder 
16803f542974SPaul B Schroeder 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
16813f542974SPaul B Schroeder 	if (status < 0) {
16829c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
168387521c46SRoel Kluin 		return status;
16843f542974SPaul B Schroeder 	}
16853f542974SPaul B Schroeder 
16863f542974SPaul B Schroeder 	return 0;
16873f542974SPaul B Schroeder }
16883f542974SPaul B Schroeder 
16893f542974SPaul B Schroeder /*****************************************************************************
16903f542974SPaul B Schroeder  * mos7840_calc_baud_rate_divisor
16913f542974SPaul B Schroeder  *	this function calculates the proper baud rate divisor for the specified
16923f542974SPaul B Schroeder  *	baud rate.
16933f542974SPaul B Schroeder  *****************************************************************************/
16949c134a14SGreg Kroah-Hartman static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
16959c134a14SGreg Kroah-Hartman 					  int baudRate, int *divisor,
16963f542974SPaul B Schroeder 					  __u16 *clk_sel_val)
16973f542974SPaul B Schroeder {
16989c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
16993f542974SPaul B Schroeder 
17003f542974SPaul B Schroeder 	if (baudRate <= 115200) {
17013f542974SPaul B Schroeder 		*divisor = 115200 / baudRate;
17023f542974SPaul B Schroeder 		*clk_sel_val = 0x0;
17033f542974SPaul B Schroeder 	}
17043f542974SPaul B Schroeder 	if ((baudRate > 115200) && (baudRate <= 230400)) {
17053f542974SPaul B Schroeder 		*divisor = 230400 / baudRate;
17063f542974SPaul B Schroeder 		*clk_sel_val = 0x10;
17073f542974SPaul B Schroeder 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
17083f542974SPaul B Schroeder 		*divisor = 403200 / baudRate;
17093f542974SPaul B Schroeder 		*clk_sel_val = 0x20;
17103f542974SPaul B Schroeder 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
17113f542974SPaul B Schroeder 		*divisor = 460800 / baudRate;
17123f542974SPaul B Schroeder 		*clk_sel_val = 0x30;
17133f542974SPaul B Schroeder 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
17143f542974SPaul B Schroeder 		*divisor = 806400 / baudRate;
17153f542974SPaul B Schroeder 		*clk_sel_val = 0x40;
17163f542974SPaul B Schroeder 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
17173f542974SPaul B Schroeder 		*divisor = 921600 / baudRate;
17183f542974SPaul B Schroeder 		*clk_sel_val = 0x50;
17193f542974SPaul B Schroeder 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
17203f542974SPaul B Schroeder 		*divisor = 1572864 / baudRate;
17213f542974SPaul B Schroeder 		*clk_sel_val = 0x60;
17223f542974SPaul B Schroeder 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
17233f542974SPaul B Schroeder 		*divisor = 3145728 / baudRate;
17243f542974SPaul B Schroeder 		*clk_sel_val = 0x70;
17253f542974SPaul B Schroeder 	}
17263f542974SPaul B Schroeder 	return 0;
17273f542974SPaul B Schroeder 
17283f542974SPaul B Schroeder #ifdef NOTMCS7840
17293f542974SPaul B Schroeder 
17303f542974SPaul B Schroeder 	for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
17313f542974SPaul B Schroeder 		if (mos7840_divisor_table[i].BaudRate == baudrate) {
17323f542974SPaul B Schroeder 			*divisor = mos7840_divisor_table[i].Divisor;
17333f542974SPaul B Schroeder 			return 0;
17343f542974SPaul B Schroeder 		}
17353f542974SPaul B Schroeder 	}
17363f542974SPaul B Schroeder 
17373f542974SPaul B Schroeder 	/* After trying for all the standard baud rates    *
17383f542974SPaul B Schroeder 	 * Try calculating the divisor for this baud rate  */
17393f542974SPaul B Schroeder 
17403f542974SPaul B Schroeder 	if (baudrate > 75 && baudrate < 230400) {
17413f542974SPaul B Schroeder 		/* get the divisor */
17423f542974SPaul B Schroeder 		custom = (__u16) (230400L / baudrate);
17433f542974SPaul B Schroeder 
17443f542974SPaul B Schroeder 		/* Check for round off */
17453f542974SPaul B Schroeder 		round1 = (__u16) (2304000L / baudrate);
17463f542974SPaul B Schroeder 		round = (__u16) (round1 - (custom * 10));
1747880af9dbSAlan Cox 		if (round > 4)
17483f542974SPaul B Schroeder 			custom++;
17493f542974SPaul B Schroeder 		*divisor = custom;
17503f542974SPaul B Schroeder 
17519c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
17523f542974SPaul B Schroeder 		return 0;
17533f542974SPaul B Schroeder 	}
17543f542974SPaul B Schroeder 
17559c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
17563f542974SPaul B Schroeder 	return -1;
17573f542974SPaul B Schroeder #endif
17583f542974SPaul B Schroeder }
17593f542974SPaul B Schroeder 
17603f542974SPaul B Schroeder /*****************************************************************************
17613f542974SPaul B Schroeder  * mos7840_send_cmd_write_baud_rate
17623f542974SPaul B Schroeder  *	this function sends the proper command to change the baud rate of the
17633f542974SPaul B Schroeder  *	specified port.
17643f542974SPaul B Schroeder  *****************************************************************************/
17653f542974SPaul B Schroeder 
17663f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
17673f542974SPaul B Schroeder 					    int baudRate)
17683f542974SPaul B Schroeder {
17693f542974SPaul B Schroeder 	int divisor = 0;
17703f542974SPaul B Schroeder 	int status;
17713f542974SPaul B Schroeder 	__u16 Data;
17723f542974SPaul B Schroeder 	unsigned char number;
17733f542974SPaul B Schroeder 	__u16 clk_sel_val;
17743f542974SPaul B Schroeder 	struct usb_serial_port *port;
17753f542974SPaul B Schroeder 
17763f542974SPaul B Schroeder 	if (mos7840_port == NULL)
17773f542974SPaul B Schroeder 		return -1;
17783f542974SPaul B Schroeder 
17799c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
17809c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
17813f542974SPaul B Schroeder 		return -1;
17823f542974SPaul B Schroeder 
17839c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(port->serial, __func__))
17843f542974SPaul B Schroeder 		return -1;
17853f542974SPaul B Schroeder 
17863f542974SPaul B Schroeder 	number = mos7840_port->port->number - mos7840_port->port->serial->minor;
17873f542974SPaul B Schroeder 
17889c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
17893f542974SPaul B Schroeder 		mos7840_port->port->number, baudRate);
1790880af9dbSAlan Cox 	/* reset clk_uart_sel in spregOffset */
17913f542974SPaul B Schroeder 	if (baudRate > 115200) {
17923f542974SPaul B Schroeder #ifdef HW_flow_control
1793880af9dbSAlan Cox 		/* NOTE: need to see the pther register to modify */
1794880af9dbSAlan Cox 		/* setting h/w flow control bit to 1 */
17953f542974SPaul B Schroeder 		Data = 0x2b;
17963f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1797880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1798880af9dbSAlan Cox 									Data);
17993f542974SPaul B Schroeder 		if (status < 0) {
18009c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
18013f542974SPaul B Schroeder 			return -1;
18023f542974SPaul B Schroeder 		}
18033f542974SPaul B Schroeder #endif
18043f542974SPaul B Schroeder 
18053f542974SPaul B Schroeder 	} else {
18063f542974SPaul B Schroeder #ifdef HW_flow_control
1807880af9dbSAlan Cox 		/* setting h/w flow control bit to 0 */
18083f542974SPaul B Schroeder 		Data = 0xb;
18093f542974SPaul B Schroeder 		mos7840_port->shadowMCR = Data;
1810880af9dbSAlan Cox 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1811880af9dbSAlan Cox 									Data);
18123f542974SPaul B Schroeder 		if (status < 0) {
18139c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
18143f542974SPaul B Schroeder 			return -1;
18153f542974SPaul B Schroeder 		}
18163f542974SPaul B Schroeder #endif
18173f542974SPaul B Schroeder 
18183f542974SPaul B Schroeder 	}
18193f542974SPaul B Schroeder 
1820880af9dbSAlan Cox 	if (1) {		/* baudRate <= 115200) */
18213f542974SPaul B Schroeder 		clk_sel_val = 0x0;
18223f542974SPaul B Schroeder 		Data = 0x0;
18239c134a14SGreg Kroah-Hartman 		status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
18243f542974SPaul B Schroeder 						   &clk_sel_val);
1825880af9dbSAlan Cox 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
18263f542974SPaul B Schroeder 								 &Data);
18273f542974SPaul B Schroeder 		if (status < 0) {
18289c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
18293f542974SPaul B Schroeder 			return -1;
18303f542974SPaul B Schroeder 		}
18313f542974SPaul B Schroeder 		Data = (Data & 0x8f) | clk_sel_val;
1832880af9dbSAlan Cox 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1833880af9dbSAlan Cox 								Data);
18343f542974SPaul B Schroeder 		if (status < 0) {
18359c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
18363f542974SPaul B Schroeder 			return -1;
18373f542974SPaul B Schroeder 		}
18383f542974SPaul B Schroeder 		/* Calculate the Divisor */
18393f542974SPaul B Schroeder 
18403f542974SPaul B Schroeder 		if (status) {
1841194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
18423f542974SPaul B Schroeder 			return status;
18433f542974SPaul B Schroeder 		}
18443f542974SPaul B Schroeder 		/* Enable access to divisor latch */
18453f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
18463f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
18473f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
18483f542974SPaul B Schroeder 
18493f542974SPaul B Schroeder 		/* Write the divisor */
18503f542974SPaul B Schroeder 		Data = (unsigned char)(divisor & 0xff);
18519c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
18523f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
18533f542974SPaul B Schroeder 
18543f542974SPaul B Schroeder 		Data = (unsigned char)((divisor & 0xff00) >> 8);
18559c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
18563f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
18573f542974SPaul B Schroeder 
18583f542974SPaul B Schroeder 		/* Disable access to divisor latch */
18593f542974SPaul B Schroeder 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
18603f542974SPaul B Schroeder 		mos7840_port->shadowLCR = Data;
18613f542974SPaul B Schroeder 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
18623f542974SPaul B Schroeder 
18633f542974SPaul B Schroeder 	}
18643f542974SPaul B Schroeder 	return status;
18653f542974SPaul B Schroeder }
18663f542974SPaul B Schroeder 
18673f542974SPaul B Schroeder /*****************************************************************************
18683f542974SPaul B Schroeder  * mos7840_change_port_settings
18693f542974SPaul B Schroeder  *	This routine is called to set the UART on the device to match
18703f542974SPaul B Schroeder  *      the specified new settings.
18713f542974SPaul B Schroeder  *****************************************************************************/
18723f542974SPaul B Schroeder 
187395da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
187495da310eSAlan Cox 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
18753f542974SPaul B Schroeder {
18763f542974SPaul B Schroeder 	int baud;
18773f542974SPaul B Schroeder 	unsigned cflag;
18783f542974SPaul B Schroeder 	unsigned iflag;
18793f542974SPaul B Schroeder 	__u8 lData;
18803f542974SPaul B Schroeder 	__u8 lParity;
18813f542974SPaul B Schroeder 	__u8 lStop;
18823f542974SPaul B Schroeder 	int status;
18833f542974SPaul B Schroeder 	__u16 Data;
18843f542974SPaul B Schroeder 	struct usb_serial_port *port;
18853f542974SPaul B Schroeder 	struct usb_serial *serial;
18863f542974SPaul B Schroeder 
18873f542974SPaul B Schroeder 	if (mos7840_port == NULL)
18883f542974SPaul B Schroeder 		return;
18893f542974SPaul B Schroeder 
18909c134a14SGreg Kroah-Hartman 	port = mos7840_port->port;
18913f542974SPaul B Schroeder 
18929c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
18933f542974SPaul B Schroeder 		return;
18943f542974SPaul B Schroeder 
18959c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(port->serial, __func__))
18963f542974SPaul B Schroeder 		return;
18973f542974SPaul B Schroeder 
18983f542974SPaul B Schroeder 	serial = port->serial;
18993f542974SPaul B Schroeder 
19003f542974SPaul B Schroeder 	if (!mos7840_port->open) {
19019c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
19023f542974SPaul B Schroeder 		return;
19033f542974SPaul B Schroeder 	}
19043f542974SPaul B Schroeder 
19053f542974SPaul B Schroeder 	lData = LCR_BITS_8;
19063f542974SPaul B Schroeder 	lStop = LCR_STOP_1;
19073f542974SPaul B Schroeder 	lParity = LCR_PAR_NONE;
19083f542974SPaul B Schroeder 
1909adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
1910adc8d746SAlan Cox 	iflag = tty->termios.c_iflag;
19113f542974SPaul B Schroeder 
19123f542974SPaul B Schroeder 	/* Change the number of bits */
19133f542974SPaul B Schroeder 	if (cflag & CSIZE) {
19143f542974SPaul B Schroeder 		switch (cflag & CSIZE) {
19153f542974SPaul B Schroeder 		case CS5:
19163f542974SPaul B Schroeder 			lData = LCR_BITS_5;
19173f542974SPaul B Schroeder 			break;
19183f542974SPaul B Schroeder 
19193f542974SPaul B Schroeder 		case CS6:
19203f542974SPaul B Schroeder 			lData = LCR_BITS_6;
19213f542974SPaul B Schroeder 			break;
19223f542974SPaul B Schroeder 
19233f542974SPaul B Schroeder 		case CS7:
19243f542974SPaul B Schroeder 			lData = LCR_BITS_7;
19253f542974SPaul B Schroeder 			break;
19263f542974SPaul B Schroeder 		default:
19273f542974SPaul B Schroeder 		case CS8:
19283f542974SPaul B Schroeder 			lData = LCR_BITS_8;
19293f542974SPaul B Schroeder 			break;
19303f542974SPaul B Schroeder 		}
19313f542974SPaul B Schroeder 	}
19323f542974SPaul B Schroeder 	/* Change the Parity bit */
19333f542974SPaul B Schroeder 	if (cflag & PARENB) {
19343f542974SPaul B Schroeder 		if (cflag & PARODD) {
19353f542974SPaul B Schroeder 			lParity = LCR_PAR_ODD;
19369c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
19373f542974SPaul B Schroeder 		} else {
19383f542974SPaul B Schroeder 			lParity = LCR_PAR_EVEN;
19399c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
19403f542974SPaul B Schroeder 		}
19413f542974SPaul B Schroeder 
19423f542974SPaul B Schroeder 	} else {
19439c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
19443f542974SPaul B Schroeder 	}
19453f542974SPaul B Schroeder 
1946880af9dbSAlan Cox 	if (cflag & CMSPAR)
19473f542974SPaul B Schroeder 		lParity = lParity | 0x20;
19483f542974SPaul B Schroeder 
19493f542974SPaul B Schroeder 	/* Change the Stop bit */
19503f542974SPaul B Schroeder 	if (cflag & CSTOPB) {
19513f542974SPaul B Schroeder 		lStop = LCR_STOP_2;
19529c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
19533f542974SPaul B Schroeder 	} else {
19543f542974SPaul B Schroeder 		lStop = LCR_STOP_1;
19559c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
19563f542974SPaul B Schroeder 	}
19573f542974SPaul B Schroeder 
19583f542974SPaul B Schroeder 	/* Update the LCR with the correct value */
19593f542974SPaul B Schroeder 	mos7840_port->shadowLCR &=
19603f542974SPaul B Schroeder 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
19613f542974SPaul B Schroeder 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
19623f542974SPaul B Schroeder 
19639c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
19643f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
19653f542974SPaul B Schroeder 	/* Disable Interrupts */
19663f542974SPaul B Schroeder 	Data = 0x00;
19673f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
19683f542974SPaul B Schroeder 
19693f542974SPaul B Schroeder 	Data = 0x00;
19703f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
19713f542974SPaul B Schroeder 
19723f542974SPaul B Schroeder 	Data = 0xcf;
19733f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
19743f542974SPaul B Schroeder 
19753f542974SPaul B Schroeder 	/* Send the updated LCR value to the mos7840 */
19763f542974SPaul B Schroeder 	Data = mos7840_port->shadowLCR;
19773f542974SPaul B Schroeder 
19783f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
19793f542974SPaul B Schroeder 
19803f542974SPaul B Schroeder 	Data = 0x00b;
19813f542974SPaul B Schroeder 	mos7840_port->shadowMCR = Data;
19823f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
19833f542974SPaul B Schroeder 	Data = 0x00b;
19843f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
19853f542974SPaul B Schroeder 
19863f542974SPaul B Schroeder 	/* set up the MCR register and send it to the mos7840 */
19873f542974SPaul B Schroeder 
19883f542974SPaul B Schroeder 	mos7840_port->shadowMCR = MCR_MASTER_IE;
1989880af9dbSAlan Cox 	if (cflag & CBAUD)
19903f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
19913f542974SPaul B Schroeder 
1992880af9dbSAlan Cox 	if (cflag & CRTSCTS)
19933f542974SPaul B Schroeder 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
1994880af9dbSAlan Cox 	else
19953f542974SPaul B Schroeder 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
19963f542974SPaul B Schroeder 
19973f542974SPaul B Schroeder 	Data = mos7840_port->shadowMCR;
19983f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
19993f542974SPaul B Schroeder 
20003f542974SPaul B Schroeder 	/* Determine divisor based on baud rate */
20013f542974SPaul B Schroeder 	baud = tty_get_baud_rate(tty);
20023f542974SPaul B Schroeder 
20033f542974SPaul B Schroeder 	if (!baud) {
20043f542974SPaul B Schroeder 		/* pick a default, any default... */
20059c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "Picked default baud...\n");
20063f542974SPaul B Schroeder 		baud = 9600;
20073f542974SPaul B Schroeder 	}
20083f542974SPaul B Schroeder 
20099c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
20103f542974SPaul B Schroeder 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
20113f542974SPaul B Schroeder 
20123f542974SPaul B Schroeder 	/* Enable Interrupts */
20133f542974SPaul B Schroeder 	Data = 0x0c;
20143f542974SPaul B Schroeder 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
20153f542974SPaul B Schroeder 
201650de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
201750de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
20183f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
20193f542974SPaul B Schroeder 		if (status) {
20209c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
20213f542974SPaul B Schroeder 			    status);
202250de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
20233f542974SPaul B Schroeder 		}
20243f542974SPaul B Schroeder 	}
20253f542974SPaul B Schroeder 	wake_up(&mos7840_port->delta_msr_wait);
20263f542974SPaul B Schroeder 	mos7840_port->delta_msr_cond = 1;
20279c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
20283f542974SPaul B Schroeder 		mos7840_port->shadowLCR);
20293f542974SPaul B Schroeder }
20303f542974SPaul B Schroeder 
20313f542974SPaul B Schroeder /*****************************************************************************
20323f542974SPaul B Schroeder  * mos7840_set_termios
20333f542974SPaul B Schroeder  *	this function is called by the tty driver when it wants to change
20343f542974SPaul B Schroeder  *	the termios structure
20353f542974SPaul B Schroeder  *****************************************************************************/
20363f542974SPaul B Schroeder 
203795da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
203895da310eSAlan Cox 				struct usb_serial_port *port,
2039606d099cSAlan Cox 				struct ktermios *old_termios)
20403f542974SPaul B Schroeder {
20413f542974SPaul B Schroeder 	int status;
20423f542974SPaul B Schroeder 	unsigned int cflag;
20433f542974SPaul B Schroeder 	struct usb_serial *serial;
20443f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
20453363155bSGreg Kroah-Hartman 
20469c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
20473f542974SPaul B Schroeder 		return;
20483f542974SPaul B Schroeder 
20493f542974SPaul B Schroeder 	serial = port->serial;
20503f542974SPaul B Schroeder 
20519c134a14SGreg Kroah-Hartman 	if (mos7840_serial_paranoia_check(serial, __func__))
20523f542974SPaul B Schroeder 		return;
20533f542974SPaul B Schroeder 
20543f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
20553f542974SPaul B Schroeder 
20563f542974SPaul B Schroeder 	if (mos7840_port == NULL)
20573f542974SPaul B Schroeder 		return;
20583f542974SPaul B Schroeder 
20593f542974SPaul B Schroeder 	if (!mos7840_port->open) {
20609c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
20613f542974SPaul B Schroeder 		return;
20623f542974SPaul B Schroeder 	}
20633f542974SPaul B Schroeder 
20649c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s", "setting termios - \n");
20653f542974SPaul B Schroeder 
2066adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
20673f542974SPaul B Schroeder 
20689c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2069adc8d746SAlan Cox 		tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
20709c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
20713f542974SPaul B Schroeder 		old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
20729c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
20733f542974SPaul B Schroeder 
20743f542974SPaul B Schroeder 	/* change the port settings to the new ones specified */
20753f542974SPaul B Schroeder 
207695da310eSAlan Cox 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
20773f542974SPaul B Schroeder 
20783f542974SPaul B Schroeder 	if (!mos7840_port->read_urb) {
20799c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
20803f542974SPaul B Schroeder 		return;
20813f542974SPaul B Schroeder 	}
20823f542974SPaul B Schroeder 
208350de36f7SGreg Kroah-Hartman 	if (mos7840_port->read_urb_busy == false) {
208450de36f7SGreg Kroah-Hartman 		mos7840_port->read_urb_busy = true;
20853f542974SPaul B Schroeder 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
20863f542974SPaul B Schroeder 		if (status) {
20879c134a14SGreg Kroah-Hartman 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
20883f542974SPaul B Schroeder 			    status);
208950de36f7SGreg Kroah-Hartman 			mos7840_port->read_urb_busy = false;
20903f542974SPaul B Schroeder 		}
20913f542974SPaul B Schroeder 	}
20923f542974SPaul B Schroeder }
20933f542974SPaul B Schroeder 
20943f542974SPaul B Schroeder /*****************************************************************************
20953f542974SPaul B Schroeder  * mos7840_get_lsr_info - get line status register info
20963f542974SPaul B Schroeder  *
20973f542974SPaul B Schroeder  * Purpose: Let user call ioctl() to get info when the UART physically
20983f542974SPaul B Schroeder  * 	    is emptied.  On bus types like RS485, the transmitter must
20993f542974SPaul B Schroeder  * 	    release the bus after transmitting. This must be done when
21003f542974SPaul B Schroeder  * 	    the transmit shift register is empty, not be done when the
21013f542974SPaul B Schroeder  * 	    transmit holding register is empty.  This functionality
21023f542974SPaul B Schroeder  * 	    allows an RS485 driver to be written in user space.
21033f542974SPaul B Schroeder  *****************************************************************************/
21043f542974SPaul B Schroeder 
210595da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
210697c4965dSAl Viro 				unsigned int __user *value)
21073f542974SPaul B Schroeder {
21083f542974SPaul B Schroeder 	int count;
21093f542974SPaul B Schroeder 	unsigned int result = 0;
21103f542974SPaul B Schroeder 
211195da310eSAlan Cox 	count = mos7840_chars_in_buffer(tty);
21129c134a14SGreg Kroah-Hartman 	if (count == 0)
21133f542974SPaul B Schroeder 		result = TIOCSER_TEMT;
21143f542974SPaul B Schroeder 
21153f542974SPaul B Schroeder 	if (copy_to_user(value, &result, sizeof(int)))
21163f542974SPaul B Schroeder 		return -EFAULT;
21173f542974SPaul B Schroeder 	return 0;
21183f542974SPaul B Schroeder }
21193f542974SPaul B Schroeder 
21203f542974SPaul B Schroeder /*****************************************************************************
21213f542974SPaul B Schroeder  * mos7840_get_serial_info
21223f542974SPaul B Schroeder  *      function to get information about serial port
21233f542974SPaul B Schroeder  *****************************************************************************/
21243f542974SPaul B Schroeder 
21253f542974SPaul B Schroeder static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
212697c4965dSAl Viro 				   struct serial_struct __user *retinfo)
21273f542974SPaul B Schroeder {
21283f542974SPaul B Schroeder 	struct serial_struct tmp;
21293f542974SPaul B Schroeder 
21303f542974SPaul B Schroeder 	if (mos7840_port == NULL)
21313f542974SPaul B Schroeder 		return -1;
21323f542974SPaul B Schroeder 
21333f542974SPaul B Schroeder 	if (!retinfo)
21343f542974SPaul B Schroeder 		return -EFAULT;
21353f542974SPaul B Schroeder 
21363f542974SPaul B Schroeder 	memset(&tmp, 0, sizeof(tmp));
21373f542974SPaul B Schroeder 
21383f542974SPaul B Schroeder 	tmp.type = PORT_16550A;
21393f542974SPaul B Schroeder 	tmp.line = mos7840_port->port->serial->minor;
21403f542974SPaul B Schroeder 	tmp.port = mos7840_port->port->number;
21413f542974SPaul B Schroeder 	tmp.irq = 0;
21423f542974SPaul B Schroeder 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
21433f542974SPaul B Schroeder 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
21443f542974SPaul B Schroeder 	tmp.baud_base = 9600;
21453f542974SPaul B Schroeder 	tmp.close_delay = 5 * HZ;
21463f542974SPaul B Schroeder 	tmp.closing_wait = 30 * HZ;
21473f542974SPaul B Schroeder 
21483f542974SPaul B Schroeder 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
21493f542974SPaul B Schroeder 		return -EFAULT;
21503f542974SPaul B Schroeder 	return 0;
21513f542974SPaul B Schroeder }
21523f542974SPaul B Schroeder 
21530bca1b91SAlan Cox static int mos7840_get_icount(struct tty_struct *tty,
21540bca1b91SAlan Cox 			struct serial_icounter_struct *icount)
21550bca1b91SAlan Cox {
21560bca1b91SAlan Cox 	struct usb_serial_port *port = tty->driver_data;
21570bca1b91SAlan Cox 	struct moschip_port *mos7840_port;
21580bca1b91SAlan Cox 	struct async_icount cnow;
21590bca1b91SAlan Cox 
21600bca1b91SAlan Cox 	mos7840_port = mos7840_get_port_private(port);
21610bca1b91SAlan Cox 	cnow = mos7840_port->icount;
21620bca1b91SAlan Cox 
21630bca1b91SAlan Cox 	smp_rmb();
21640bca1b91SAlan Cox 	icount->cts = cnow.cts;
21650bca1b91SAlan Cox 	icount->dsr = cnow.dsr;
21660bca1b91SAlan Cox 	icount->rng = cnow.rng;
21670bca1b91SAlan Cox 	icount->dcd = cnow.dcd;
21680bca1b91SAlan Cox 	icount->rx = cnow.rx;
21690bca1b91SAlan Cox 	icount->tx = cnow.tx;
21700bca1b91SAlan Cox 	icount->frame = cnow.frame;
21710bca1b91SAlan Cox 	icount->overrun = cnow.overrun;
21720bca1b91SAlan Cox 	icount->parity = cnow.parity;
21730bca1b91SAlan Cox 	icount->brk = cnow.brk;
21740bca1b91SAlan Cox 	icount->buf_overrun = cnow.buf_overrun;
21750bca1b91SAlan Cox 
21769c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
21779c134a14SGreg Kroah-Hartman 		icount->rx, icount->tx);
21780bca1b91SAlan Cox 	return 0;
21790bca1b91SAlan Cox }
21800bca1b91SAlan Cox 
21813f542974SPaul B Schroeder /*****************************************************************************
21823f542974SPaul B Schroeder  * SerialIoctl
21833f542974SPaul B Schroeder  *	this function handles any ioctl calls to the driver
21843f542974SPaul B Schroeder  *****************************************************************************/
21853f542974SPaul B Schroeder 
218600a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
21873f542974SPaul B Schroeder 			 unsigned int cmd, unsigned long arg)
21883f542974SPaul B Schroeder {
218995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
219097c4965dSAl Viro 	void __user *argp = (void __user *)arg;
21913f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
21923f542974SPaul B Schroeder 
21933f542974SPaul B Schroeder 	struct async_icount cnow;
21943f542974SPaul B Schroeder 	struct async_icount cprev;
21953f542974SPaul B Schroeder 
21969c134a14SGreg Kroah-Hartman 	if (mos7840_port_paranoia_check(port, __func__))
21973f542974SPaul B Schroeder 		return -1;
21983f542974SPaul B Schroeder 
21993f542974SPaul B Schroeder 	mos7840_port = mos7840_get_port_private(port);
22003f542974SPaul B Schroeder 
22013f542974SPaul B Schroeder 	if (mos7840_port == NULL)
22023f542974SPaul B Schroeder 		return -1;
22033f542974SPaul B Schroeder 
22049c134a14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
22053f542974SPaul B Schroeder 
22063f542974SPaul B Schroeder 	switch (cmd) {
22073f542974SPaul B Schroeder 		/* return number of bytes available */
22083f542974SPaul B Schroeder 
22093f542974SPaul B Schroeder 	case TIOCSERGETLSR:
22109c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
221195da310eSAlan Cox 		return mos7840_get_lsr_info(tty, argp);
22123f542974SPaul B Schroeder 
22133f542974SPaul B Schroeder 	case TIOCGSERIAL:
22149c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
221597c4965dSAl Viro 		return mos7840_get_serial_info(mos7840_port, argp);
22163f542974SPaul B Schroeder 
22173f542974SPaul B Schroeder 	case TIOCSSERIAL:
22189c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
22193f542974SPaul B Schroeder 		break;
22203f542974SPaul B Schroeder 
22213f542974SPaul B Schroeder 	case TIOCMIWAIT:
22229c134a14SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s  TIOCMIWAIT\n", __func__);
22233f542974SPaul B Schroeder 		cprev = mos7840_port->icount;
22243f542974SPaul B Schroeder 		while (1) {
2225880af9dbSAlan Cox 			/* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
22263f542974SPaul B Schroeder 			mos7840_port->delta_msr_cond = 0;
22273f542974SPaul B Schroeder 			wait_event_interruptible(mos7840_port->delta_msr_wait,
22283f542974SPaul B Schroeder 						 (mos7840_port->
22293f542974SPaul B Schroeder 						  delta_msr_cond == 1));
22303f542974SPaul B Schroeder 
22313f542974SPaul B Schroeder 			/* see if a signal did it */
22323f542974SPaul B Schroeder 			if (signal_pending(current))
22333f542974SPaul B Schroeder 				return -ERESTARTSYS;
22343f542974SPaul B Schroeder 			cnow = mos7840_port->icount;
22350de9a702SOliver Neukum 			smp_rmb();
22363f542974SPaul B Schroeder 			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
22373f542974SPaul B Schroeder 			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
22383f542974SPaul B Schroeder 				return -EIO;	/* no change => error */
22393f542974SPaul B Schroeder 			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
22403f542974SPaul B Schroeder 			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
22413f542974SPaul B Schroeder 			    ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
22423f542974SPaul B Schroeder 			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
22433f542974SPaul B Schroeder 				return 0;
22443f542974SPaul B Schroeder 			}
22453f542974SPaul B Schroeder 			cprev = cnow;
22463f542974SPaul B Schroeder 		}
22473f542974SPaul B Schroeder 		/* NOTREACHED */
22483f542974SPaul B Schroeder 		break;
22493f542974SPaul B Schroeder 
22503f542974SPaul B Schroeder 	default:
22513f542974SPaul B Schroeder 		break;
22523f542974SPaul B Schroeder 	}
22533f542974SPaul B Schroeder 	return -ENOIOCTLCMD;
22543f542974SPaul B Schroeder }
22553f542974SPaul B Schroeder 
22560eafe4deSDonald static int mos7810_check(struct usb_serial *serial)
22573f542974SPaul B Schroeder {
22580eafe4deSDonald 	int i, pass_count = 0;
22590eafe4deSDonald 	__u16 data = 0, mcr_data = 0;
22600eafe4deSDonald 	__u16 test_pattern = 0x55AA;
22613f542974SPaul B Schroeder 
22620eafe4deSDonald 	/* Store MCR setting */
22630eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
22640eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
22650eafe4deSDonald 		&mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
22660eafe4deSDonald 
22670eafe4deSDonald 	for (i = 0; i < 16; i++) {
22680eafe4deSDonald 		/* Send the 1-bit test pattern out to MCS7810 test pin */
22690eafe4deSDonald 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
22700eafe4deSDonald 			MCS_WRREQ, MCS_WR_RTYPE,
22710eafe4deSDonald 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
22720eafe4deSDonald 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
22730eafe4deSDonald 
22740eafe4deSDonald 		/* Read the test pattern back */
22750eafe4deSDonald 		usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
22760eafe4deSDonald 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2277093ea2d3SDonald Lee 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2278093ea2d3SDonald Lee 
22790eafe4deSDonald 		/* If this is a MCS7810 device, both test patterns must match */
22800eafe4deSDonald 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
22810eafe4deSDonald 			break;
22820eafe4deSDonald 
22830eafe4deSDonald 		pass_count++;
22843f542974SPaul B Schroeder 	}
2285093ea2d3SDonald Lee 
22860eafe4deSDonald 	/* Restore MCR setting */
22870eafe4deSDonald 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
22880eafe4deSDonald 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
22890eafe4deSDonald 		0, MOS_WDR_TIMEOUT);
22900eafe4deSDonald 
22910eafe4deSDonald 	if (pass_count == 16)
22920eafe4deSDonald 		return 1;
22930eafe4deSDonald 
22940eafe4deSDonald 	return 0;
22950eafe4deSDonald }
22960eafe4deSDonald 
22970eafe4deSDonald static int mos7840_calc_num_ports(struct usb_serial *serial)
22980eafe4deSDonald {
22990eafe4deSDonald 	__u16 data = 0x00;
23000eafe4deSDonald 	int mos7840_num_ports;
23010eafe4deSDonald 
23020eafe4deSDonald 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
23030eafe4deSDonald 		MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
23040eafe4deSDonald 		VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
23050eafe4deSDonald 
23060eafe4deSDonald 	if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
23070eafe4deSDonald 		serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
23080eafe4deSDonald 		device_type = serial->dev->descriptor.idProduct;
23090eafe4deSDonald 	} else {
23100eafe4deSDonald 		/* For a MCS7840 device GPIO0 must be set to 1 */
23110eafe4deSDonald 		if ((data & 0x01) == 1)
23120eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7840;
23130eafe4deSDonald 		else if (mos7810_check(serial))
23140eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7810;
23150eafe4deSDonald 		else
23160eafe4deSDonald 			device_type = MOSCHIP_DEVICE_ID_7820;
23170eafe4deSDonald 	}
23180eafe4deSDonald 
23190eafe4deSDonald 	mos7840_num_ports = (device_type >> 4) & 0x000F;
23200eafe4deSDonald 	serial->num_bulk_in = mos7840_num_ports;
23210eafe4deSDonald 	serial->num_bulk_out = mos7840_num_ports;
23220eafe4deSDonald 	serial->num_ports = mos7840_num_ports;
23230eafe4deSDonald 
23243f542974SPaul B Schroeder 	return mos7840_num_ports;
23253f542974SPaul B Schroeder }
23263f542974SPaul B Schroeder 
232780c00750SJohan Hovold static int mos7840_port_probe(struct usb_serial_port *port)
23283f542974SPaul B Schroeder {
232980c00750SJohan Hovold 	struct usb_serial *serial = port->serial;
23303f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
233180c00750SJohan Hovold 	int status;
233280c00750SJohan Hovold 	int pnum;
23333f542974SPaul B Schroeder 	__u16 Data;
23343f542974SPaul B Schroeder 
23353f542974SPaul B Schroeder 	/* we set up the pointers to the endpoints in the mos7840_open *
23363f542974SPaul B Schroeder 	 * function, as the structures aren't created yet.             */
23373f542974SPaul B Schroeder 
233880c00750SJohan Hovold 	pnum = port->number - serial->minor;
233980c00750SJohan Hovold 
2340ae685effSJohan Hovold 	dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
23417ac9da10SBurman Yan 	mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
23423f542974SPaul B Schroeder 	if (mos7840_port == NULL) {
234380c00750SJohan Hovold 		dev_err(&port->dev, "%s - Out of memory\n", __func__);
234480c00750SJohan Hovold 		return -ENOMEM;
23453f542974SPaul B Schroeder 	}
23463f542974SPaul B Schroeder 
2347880af9dbSAlan Cox 	/* Initialize all port interrupt end point to port 0 int
2348880af9dbSAlan Cox 	 * endpoint. Our device has only one interrupt end point
2349880af9dbSAlan Cox 	 * common to all port */
23503f542974SPaul B Schroeder 
235180c00750SJohan Hovold 	mos7840_port->port = port;
235280c00750SJohan Hovold 	mos7840_set_port_private(port, mos7840_port);
23530de9a702SOliver Neukum 	spin_lock_init(&mos7840_port->pool_lock);
23543f542974SPaul B Schroeder 
235537768adfSTony Cook 	/* minor is not initialised until later by
235637768adfSTony Cook 	 * usb-serial.c:get_free_serial() and cannot therefore be used
235737768adfSTony Cook 	 * to index device instances */
235880c00750SJohan Hovold 	mos7840_port->port_num = pnum + 1;
235980c00750SJohan Hovold 	dev_dbg(&port->dev, "port->number = %d\n", port->number);
236080c00750SJohan Hovold 	dev_dbg(&port->dev, "port->serial->minor = %d\n", port->serial->minor);
236180c00750SJohan Hovold 	dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
236280c00750SJohan Hovold 	dev_dbg(&port->dev, "serial->minor = %d\n", serial->minor);
23633f542974SPaul B Schroeder 
23643f542974SPaul B Schroeder 	if (mos7840_port->port_num == 1) {
23653f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0x0;
23663f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0x1;
23673f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x4;
2368ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
23693f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0x8;
23703f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0x9;
23713f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x16;
2372ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
23733f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0xa;
23743f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0xb;
23753f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x19;
2376ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
23773f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0xa;
23783f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0xb;
23793f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x19;
2380ae685effSJohan Hovold 	} else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
23813f542974SPaul B Schroeder 		mos7840_port->SpRegOffset = 0xc;
23823f542974SPaul B Schroeder 		mos7840_port->ControlRegOffset = 0xd;
23833f542974SPaul B Schroeder 		mos7840_port->DcrRegOffset = 0x1c;
23843f542974SPaul B Schroeder 	}
238580c00750SJohan Hovold 	mos7840_dump_serial_port(port, mos7840_port);
238680c00750SJohan Hovold 	mos7840_set_port_private(port, mos7840_port);
23873f542974SPaul B Schroeder 
2388880af9dbSAlan Cox 	/* enable rx_disable bit in control register */
238980c00750SJohan Hovold 	status = mos7840_get_reg_sync(port,
23903f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, &Data);
23913f542974SPaul B Schroeder 	if (status < 0) {
239280c00750SJohan Hovold 		dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2393ae685effSJohan Hovold 		goto out;
23943f542974SPaul B Schroeder 	} else
239580c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2396880af9dbSAlan Cox 	Data |= 0x08;	/* setting driver done bit */
2397880af9dbSAlan Cox 	Data |= 0x04;	/* sp1_bit to have cts change reflect in
2398880af9dbSAlan Cox 			   modem status reg */
23993f542974SPaul B Schroeder 
2400880af9dbSAlan Cox 	/* Data |= 0x20; //rx_disable bit */
240180c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
24023f542974SPaul B Schroeder 			mos7840_port->ControlRegOffset, Data);
24033f542974SPaul B Schroeder 	if (status < 0) {
240480c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2405ae685effSJohan Hovold 		goto out;
24063f542974SPaul B Schroeder 	} else
240780c00750SJohan Hovold 		dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
24083f542974SPaul B Schroeder 
2409880af9dbSAlan Cox 	/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2410880af9dbSAlan Cox 	   and 0x24 in DCR3 */
24113f542974SPaul B Schroeder 	Data = 0x01;
241280c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
2413880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 0), Data);
24143f542974SPaul B Schroeder 	if (status < 0) {
241580c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2416ae685effSJohan Hovold 		goto out;
24173f542974SPaul B Schroeder 	} else
241880c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
24193f542974SPaul B Schroeder 
24203f542974SPaul B Schroeder 	Data = 0x05;
242180c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
2422880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 1), Data);
24233f542974SPaul B Schroeder 	if (status < 0) {
242480c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2425ae685effSJohan Hovold 		goto out;
24263f542974SPaul B Schroeder 	} else
242780c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
24283f542974SPaul B Schroeder 
24293f542974SPaul B Schroeder 	Data = 0x24;
243080c00750SJohan Hovold 	status = mos7840_set_reg_sync(port,
2431880af9dbSAlan Cox 			(__u16) (mos7840_port->DcrRegOffset + 2), Data);
24323f542974SPaul B Schroeder 	if (status < 0) {
243380c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2434ae685effSJohan Hovold 		goto out;
24353f542974SPaul B Schroeder 	} else
243680c00750SJohan Hovold 		dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
24373f542974SPaul B Schroeder 
2438880af9dbSAlan Cox 	/* write values in clkstart0x0 and clkmulti 0x20 */
24393f542974SPaul B Schroeder 	Data = 0x0;
2440ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
24413f542974SPaul B Schroeder 	if (status < 0) {
244280c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2443ae685effSJohan Hovold 		goto out;
24443f542974SPaul B Schroeder 	} else
244580c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
24463f542974SPaul B Schroeder 
24473f542974SPaul B Schroeder 	Data = 0x20;
2448ae685effSJohan Hovold 	status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
24493f542974SPaul B Schroeder 	if (status < 0) {
245080c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
24510de9a702SOliver Neukum 		goto error;
24523f542974SPaul B Schroeder 	} else
245380c00750SJohan Hovold 		dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
24543f542974SPaul B Schroeder 
2455880af9dbSAlan Cox 	/* write value 0x0 to scratchpad register */
24563f542974SPaul B Schroeder 	Data = 0x00;
2457ae685effSJohan Hovold 	status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
24583f542974SPaul B Schroeder 	if (status < 0) {
245980c00750SJohan Hovold 		dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2460ae685effSJohan Hovold 		goto out;
24613f542974SPaul B Schroeder 	} else
246280c00750SJohan Hovold 		dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
24633f542974SPaul B Schroeder 
2464880af9dbSAlan Cox 	/* Zero Length flag register */
2465ae685effSJohan Hovold 	if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
24663f542974SPaul B Schroeder 		Data = 0xff;
246780c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
24683f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
2469880af9dbSAlan Cox 					((__u16)mos7840_port->port_num)), Data);
247080c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
24719c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
24723f542974SPaul B Schroeder 		if (status < 0) {
247380c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2474ae685effSJohan Hovold 			goto out;
24753f542974SPaul B Schroeder 		} else
247680c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
24773f542974SPaul B Schroeder 	} else {
24783f542974SPaul B Schroeder 		Data = 0xff;
247980c00750SJohan Hovold 		status = mos7840_set_reg_sync(port,
24803f542974SPaul B Schroeder 				(__u16) (ZLP_REG1 +
2481880af9dbSAlan Cox 					((__u16)mos7840_port->port_num) - 0x1), Data);
248280c00750SJohan Hovold 		dev_dbg(&port->dev, "ZLIP offset %x\n",
24839c134a14SGreg Kroah-Hartman 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
24843f542974SPaul B Schroeder 		if (status < 0) {
248580c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2486ae685effSJohan Hovold 			goto out;
24873f542974SPaul B Schroeder 		} else
248880c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
24893f542974SPaul B Schroeder 
24903f542974SPaul B Schroeder 	}
24910de9a702SOliver Neukum 	mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
24923f542974SPaul B Schroeder 	mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2493880af9dbSAlan Cox 	mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2494880af9dbSAlan Cox 			GFP_KERNEL);
2495880af9dbSAlan Cox 	if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2496880af9dbSAlan Cox 			!mos7840_port->dr) {
24970de9a702SOliver Neukum 		status = -ENOMEM;
24980de9a702SOliver Neukum 		goto error;
24990de9a702SOliver Neukum 	}
25000eafe4deSDonald 
25010eafe4deSDonald 	mos7840_port->has_led = false;
25020eafe4deSDonald 
25030eafe4deSDonald 	/* Initialize LED timers */
25040eafe4deSDonald 	if (device_type == MOSCHIP_DEVICE_ID_7810) {
25050eafe4deSDonald 		mos7840_port->has_led = true;
25060eafe4deSDonald 
25070eafe4deSDonald 		init_timer(&mos7840_port->led_timer1);
25080eafe4deSDonald 		mos7840_port->led_timer1.function = mos7840_led_off;
25090eafe4deSDonald 		mos7840_port->led_timer1.expires =
25100eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_ON_MS);
2511ae685effSJohan Hovold 		mos7840_port->led_timer1.data = (unsigned long)mos7840_port;
25120eafe4deSDonald 
25130eafe4deSDonald 		init_timer(&mos7840_port->led_timer2);
2514ae685effSJohan Hovold 		mos7840_port->led_timer2.function = mos7840_led_flag_off;
25150eafe4deSDonald 		mos7840_port->led_timer2.expires =
25160eafe4deSDonald 			jiffies + msecs_to_jiffies(LED_OFF_MS);
2517ae685effSJohan Hovold 		mos7840_port->led_timer2.data = (unsigned long)mos7840_port;
25180eafe4deSDonald 
25190eafe4deSDonald 		mos7840_port->led_flag = false;
25200eafe4deSDonald 
25210eafe4deSDonald 		/* Turn off LED */
2522ae685effSJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
25230eafe4deSDonald 	}
2524ae685effSJohan Hovold out:
252580c00750SJohan Hovold 	if (pnum == serial->num_ports - 1) {
2526880af9dbSAlan Cox 		/* Zero Length flag enable */
25273f542974SPaul B Schroeder 		Data = 0x0f;
25283f542974SPaul B Schroeder 		status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
25293f542974SPaul B Schroeder 		if (status < 0) {
253080c00750SJohan Hovold 			dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
25317ced46c3SRoel Kluin 			goto error;
25323f542974SPaul B Schroeder 		} else
253380c00750SJohan Hovold 			dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
25343f542974SPaul B Schroeder 
25353f542974SPaul B Schroeder 		/* setting configuration feature to one */
25363f542974SPaul B Schroeder 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
253780c00750SJohan Hovold 				0x03, 0x00, 0x01, 0x00, NULL, 0x00,
253880c00750SJohan Hovold 				MOS_WDR_TIMEOUT);
253980c00750SJohan Hovold 	}
25403f542974SPaul B Schroeder 	return 0;
25410de9a702SOliver Neukum error:
25420de9a702SOliver Neukum 	kfree(mos7840_port->dr);
25430de9a702SOliver Neukum 	kfree(mos7840_port->ctrl_buf);
25440de9a702SOliver Neukum 	usb_free_urb(mos7840_port->control_urb);
25450de9a702SOliver Neukum 	kfree(mos7840_port);
254680c00750SJohan Hovold 
25470de9a702SOliver Neukum 	return status;
25483f542974SPaul B Schroeder }
25493f542974SPaul B Schroeder 
255080c00750SJohan Hovold static int mos7840_port_remove(struct usb_serial_port *port)
25513f542974SPaul B Schroeder {
25523f542974SPaul B Schroeder 	struct moschip_port *mos7840_port;
25533f542974SPaul B Schroeder 
255480c00750SJohan Hovold 	mos7840_port = mos7840_get_port_private(port);
25553f542974SPaul B Schroeder 
25560eafe4deSDonald 	if (mos7840_port->has_led) {
25570eafe4deSDonald 		/* Turn off LED */
255880c00750SJohan Hovold 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
25590eafe4deSDonald 
25600eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer1);
25610eafe4deSDonald 		del_timer_sync(&mos7840_port->led_timer2);
25620eafe4deSDonald 	}
256380c00750SJohan Hovold 	usb_kill_urb(mos7840_port->control_urb);
256465a4cdbbSJohan Hovold 	usb_free_urb(mos7840_port->control_urb);
25650de9a702SOliver Neukum 	kfree(mos7840_port->ctrl_buf);
25660de9a702SOliver Neukum 	kfree(mos7840_port->dr);
25673f542974SPaul B Schroeder 	kfree(mos7840_port);
256880c00750SJohan Hovold 
256980c00750SJohan Hovold 	return 0;
25703f542974SPaul B Schroeder }
25713f542974SPaul B Schroeder 
25723f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
25733f542974SPaul B Schroeder 	.driver = {
25743f542974SPaul B Schroeder 		   .owner = THIS_MODULE,
25753f542974SPaul B Schroeder 		   .name = "mos7840",
25763f542974SPaul B Schroeder 		   },
25773f542974SPaul B Schroeder 	.description = DRIVER_DESC,
257868e24113SGreg Kroah-Hartman 	.id_table = id_table,
25793f542974SPaul B Schroeder 	.num_ports = 4,
25803f542974SPaul B Schroeder 	.open = mos7840_open,
25813f542974SPaul B Schroeder 	.close = mos7840_close,
25823f542974SPaul B Schroeder 	.write = mos7840_write,
25833f542974SPaul B Schroeder 	.write_room = mos7840_write_room,
25843f542974SPaul B Schroeder 	.chars_in_buffer = mos7840_chars_in_buffer,
25853f542974SPaul B Schroeder 	.throttle = mos7840_throttle,
25863f542974SPaul B Schroeder 	.unthrottle = mos7840_unthrottle,
25873f542974SPaul B Schroeder 	.calc_num_ports = mos7840_calc_num_ports,
25883f542974SPaul B Schroeder #ifdef MCSSerialProbe
25893f542974SPaul B Schroeder 	.probe = mos7840_serial_probe,
25903f542974SPaul B Schroeder #endif
25913f542974SPaul B Schroeder 	.ioctl = mos7840_ioctl,
25923f542974SPaul B Schroeder 	.set_termios = mos7840_set_termios,
25933f542974SPaul B Schroeder 	.break_ctl = mos7840_break,
25943f542974SPaul B Schroeder 	.tiocmget = mos7840_tiocmget,
25953f542974SPaul B Schroeder 	.tiocmset = mos7840_tiocmset,
25960bca1b91SAlan Cox 	.get_icount = mos7840_get_icount,
259780c00750SJohan Hovold 	.port_probe = mos7840_port_probe,
259880c00750SJohan Hovold 	.port_remove = mos7840_port_remove,
25993f542974SPaul B Schroeder 	.read_bulk_callback = mos7840_bulk_in_callback,
26003f542974SPaul B Schroeder 	.read_int_callback = mos7840_interrupt_callback,
26013f542974SPaul B Schroeder };
26023f542974SPaul B Schroeder 
26034d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
26044d2a7affSAlan Stern 	&moschip7840_4port_device, NULL
26054d2a7affSAlan Stern };
26064d2a7affSAlan Stern 
260768e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
26083f542974SPaul B Schroeder 
26093f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
26103f542974SPaul B Schroeder MODULE_LICENSE("GPL");
2611