15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
23f542974SPaul B Schroeder /*
33f542974SPaul B Schroeder * Clean ups from Moschip version and a few ioctl implementations by:
43f542974SPaul B Schroeder * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
53f542974SPaul B Schroeder *
63f542974SPaul B Schroeder * Originally based on drivers/usb/serial/io_edgeport.c which is:
73f542974SPaul B Schroeder * Copyright (C) 2000 Inside Out Networks, All rights reserved.
83f542974SPaul B Schroeder * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
93f542974SPaul B Schroeder *
103f542974SPaul B Schroeder */
113f542974SPaul B Schroeder
123f542974SPaul B Schroeder #include <linux/kernel.h>
133f542974SPaul B Schroeder #include <linux/errno.h>
143f542974SPaul B Schroeder #include <linux/slab.h>
153f542974SPaul B Schroeder #include <linux/tty.h>
163f542974SPaul B Schroeder #include <linux/tty_driver.h>
173f542974SPaul B Schroeder #include <linux/tty_flip.h>
183f542974SPaul B Schroeder #include <linux/module.h>
193f542974SPaul B Schroeder #include <linux/serial.h>
203f542974SPaul B Schroeder #include <linux/usb.h>
213f542974SPaul B Schroeder #include <linux/usb/serial.h>
22880af9dbSAlan Cox #include <linux/uaccess.h>
233f542974SPaul B Schroeder
243f542974SPaul B Schroeder #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
253f542974SPaul B Schroeder
263f542974SPaul B Schroeder /*
273f542974SPaul B Schroeder * 16C50 UART register defines
283f542974SPaul B Schroeder */
293f542974SPaul B Schroeder
303f542974SPaul B Schroeder #define LCR_BITS_5 0x00 /* 5 bits/char */
313f542974SPaul B Schroeder #define LCR_BITS_6 0x01 /* 6 bits/char */
323f542974SPaul B Schroeder #define LCR_BITS_7 0x02 /* 7 bits/char */
333f542974SPaul B Schroeder #define LCR_BITS_8 0x03 /* 8 bits/char */
343f542974SPaul B Schroeder #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
353f542974SPaul B Schroeder
363f542974SPaul B Schroeder #define LCR_STOP_1 0x00 /* 1 stop bit */
373f542974SPaul B Schroeder #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
383f542974SPaul B Schroeder #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
393f542974SPaul B Schroeder #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
403f542974SPaul B Schroeder
413f542974SPaul B Schroeder #define LCR_PAR_NONE 0x00 /* No parity */
423f542974SPaul B Schroeder #define LCR_PAR_ODD 0x08 /* Odd parity */
433f542974SPaul B Schroeder #define LCR_PAR_EVEN 0x18 /* Even parity */
443f542974SPaul B Schroeder #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
453f542974SPaul B Schroeder #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
463f542974SPaul B Schroeder #define LCR_PAR_MASK 0x38 /* Mask for parity field */
473f542974SPaul B Schroeder
483f542974SPaul B Schroeder #define LCR_SET_BREAK 0x40 /* Set Break condition */
493f542974SPaul B Schroeder #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
503f542974SPaul B Schroeder
513f542974SPaul B Schroeder #define MCR_DTR 0x01 /* Assert DTR */
523f542974SPaul B Schroeder #define MCR_RTS 0x02 /* Assert RTS */
533f542974SPaul B Schroeder #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
543f542974SPaul B Schroeder #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
553f542974SPaul B Schroeder #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
563f542974SPaul B Schroeder #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
573f542974SPaul B Schroeder
583f542974SPaul B Schroeder #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
593f542974SPaul B Schroeder #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
603f542974SPaul B Schroeder #define MOS7840_MSR_RI 0x40 /* Current state of RI */
613f542974SPaul B Schroeder #define MOS7840_MSR_CD 0x80 /* Current state of CD */
623f542974SPaul B Schroeder
633f542974SPaul B Schroeder /*
643f542974SPaul B Schroeder * Defines used for sending commands to port
653f542974SPaul B Schroeder */
663f542974SPaul B Schroeder
671e658489SMark Ferrell #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
683f542974SPaul B Schroeder
693f542974SPaul B Schroeder #define MOS_PORT1 0x0200
703f542974SPaul B Schroeder #define MOS_PORT2 0x0300
713f542974SPaul B Schroeder #define MOS_VENREG 0x0000
723f542974SPaul B Schroeder #define MOS_MAX_PORT 0x02
733f542974SPaul B Schroeder #define MOS_WRITE 0x0E
743f542974SPaul B Schroeder #define MOS_READ 0x0D
753f542974SPaul B Schroeder
763f542974SPaul B Schroeder /* Requests */
773f542974SPaul B Schroeder #define MCS_RD_RTYPE 0xC0
783f542974SPaul B Schroeder #define MCS_WR_RTYPE 0x40
793f542974SPaul B Schroeder #define MCS_RDREQ 0x0D
803f542974SPaul B Schroeder #define MCS_WRREQ 0x0E
813f542974SPaul B Schroeder #define MCS_CTRL_TIMEOUT 500
823f542974SPaul B Schroeder #define VENDOR_READ_LENGTH (0x01)
833f542974SPaul B Schroeder
843f542974SPaul B Schroeder #define MAX_NAME_LEN 64
853f542974SPaul B Schroeder
86880af9dbSAlan Cox #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
87880af9dbSAlan Cox #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
883f542974SPaul B Schroeder
893f542974SPaul B Schroeder /* For higher baud Rates use TIOCEXBAUD */
903f542974SPaul B Schroeder #define TIOCEXBAUD 0x5462
913f542974SPaul B Schroeder
92375cb533SJohan Hovold /*
93375cb533SJohan Hovold * Vendor id and device id defines
94375cb533SJohan Hovold *
95375cb533SJohan Hovold * NOTE: Do not add new defines, add entries directly to the id_table instead.
9611e1abb4SDavid Ludlow */
9711e1abb4SDavid Ludlow #define USB_VENDOR_ID_BANDB 0x0856
98acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
99870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
100acf509aeSCliff Brake #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
101870408c8SDave Ludlow #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
102acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
103acf509aeSCliff Brake #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
104acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
105acf509aeSCliff Brake #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
10611e1abb4SDavid Ludlow #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
107caf3a636SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
108870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
109870408c8SDave Ludlow #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
1103f542974SPaul B Schroeder
11111e1abb4SDavid Ludlow /* Interrupt Routine Defines */
1123f542974SPaul B Schroeder
1133f542974SPaul B Schroeder #define SERIAL_IIR_RLS 0x06
1143f542974SPaul B Schroeder #define SERIAL_IIR_MS 0x00
1153f542974SPaul B Schroeder
1163f542974SPaul B Schroeder /*
1173f542974SPaul B Schroeder * Emulation of the bit mask on the LINE STATUS REGISTER.
1183f542974SPaul B Schroeder */
1193f542974SPaul B Schroeder #define SERIAL_LSR_DR 0x0001
1203f542974SPaul B Schroeder #define SERIAL_LSR_OE 0x0002
1213f542974SPaul B Schroeder #define SERIAL_LSR_PE 0x0004
1223f542974SPaul B Schroeder #define SERIAL_LSR_FE 0x0008
1233f542974SPaul B Schroeder #define SERIAL_LSR_BI 0x0010
1243f542974SPaul B Schroeder
1253f542974SPaul B Schroeder #define MOS_MSR_DELTA_CTS 0x10
1263f542974SPaul B Schroeder #define MOS_MSR_DELTA_DSR 0x20
1273f542974SPaul B Schroeder #define MOS_MSR_DELTA_RI 0x40
1283f542974SPaul B Schroeder #define MOS_MSR_DELTA_CD 0x80
1293f542974SPaul B Schroeder
130880af9dbSAlan Cox /* Serial Port register Address */
1313f542974SPaul B Schroeder #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
1323f542974SPaul B Schroeder #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
1333f542974SPaul B Schroeder #define LINE_CONTROL_REGISTER ((__u16)(0x03))
1343f542974SPaul B Schroeder #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
1353f542974SPaul B Schroeder #define LINE_STATUS_REGISTER ((__u16)(0x05))
1363f542974SPaul B Schroeder #define MODEM_STATUS_REGISTER ((__u16)(0x06))
1373f542974SPaul B Schroeder #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
1383f542974SPaul B Schroeder #define DIVISOR_LATCH_LSB ((__u16)(0x00))
1393f542974SPaul B Schroeder #define DIVISOR_LATCH_MSB ((__u16)(0x01))
1403f542974SPaul B Schroeder
1413f542974SPaul B Schroeder #define CLK_MULTI_REGISTER ((__u16)(0x02))
1423f542974SPaul B Schroeder #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
143093ea2d3SDonald Lee #define GPIO_REGISTER ((__u16)(0x07))
1443f542974SPaul B Schroeder
1453f542974SPaul B Schroeder #define SERIAL_LCR_DLAB ((__u16)(0x0080))
1463f542974SPaul B Schroeder
1473f542974SPaul B Schroeder /*
1483f542974SPaul B Schroeder * URB POOL related defines
1493f542974SPaul B Schroeder */
1503f542974SPaul B Schroeder #define NUM_URBS 16 /* URB Count */
1513f542974SPaul B Schroeder #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
1523f542974SPaul B Schroeder
1530eafe4deSDonald /* LED on/off milliseconds*/
1540eafe4deSDonald #define LED_ON_MS 500
1550eafe4deSDonald #define LED_OFF_MS 500
1560eafe4deSDonald
157d8a083ccSJohan Hovold enum mos7840_flag {
15805cf0decSJohan Hovold MOS7840_FLAG_LED_BUSY,
159d8a083ccSJohan Hovold };
1603f542974SPaul B Schroeder
161375cb533SJohan Hovold #define MCS_PORT_MASK GENMASK(2, 0)
162375cb533SJohan Hovold #define MCS_PORTS(nr) ((nr) & MCS_PORT_MASK)
163375cb533SJohan Hovold #define MCS_LED BIT(3)
164375cb533SJohan Hovold
165375cb533SJohan Hovold #define MCS_DEVICE(vid, pid, flags) \
166375cb533SJohan Hovold USB_DEVICE((vid), (pid)), .driver_info = (flags)
167375cb533SJohan Hovold
168b9c87663STony Zelenoff static const struct usb_device_id id_table[] = {
169375cb533SJohan Hovold { MCS_DEVICE(0x0557, 0x2011, MCS_PORTS(4)) }, /* ATEN UC2324 */
170375cb533SJohan Hovold { MCS_DEVICE(0x0557, 0x7820, MCS_PORTS(2)) }, /* ATEN UC2322 */
171375cb533SJohan Hovold { MCS_DEVICE(0x110a, 0x2210, MCS_PORTS(2)) }, /* Moxa UPort 2210 */
172375cb533SJohan Hovold { MCS_DEVICE(0x9710, 0x7810, MCS_PORTS(1) | MCS_LED) }, /* ASIX MCS7810 */
173375cb533SJohan Hovold { MCS_DEVICE(0x9710, 0x7820, MCS_PORTS(2)) }, /* MosChip MCS7820 */
174375cb533SJohan Hovold { MCS_DEVICE(0x9710, 0x7840, MCS_PORTS(4)) }, /* MosChip MCS7840 */
175375cb533SJohan Hovold { MCS_DEVICE(0x9710, 0x7843, MCS_PORTS(3)) }, /* ASIX MCS7840 3 port */
176acf509aeSCliff Brake { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2) },
177870408c8SDave Ludlow { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P) },
178acf509aeSCliff Brake { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4) },
179870408c8SDave Ludlow { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P) },
180acf509aeSCliff Brake { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2) },
181acf509aeSCliff Brake { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4) },
182acf509aeSCliff Brake { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2) },
183acf509aeSCliff Brake { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4) },
18411e1abb4SDavid Ludlow { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2) },
185870408c8SDave Ludlow { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P) },
186acf509aeSCliff Brake { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4) },
187870408c8SDave Ludlow { USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P) },
1883f542974SPaul B Schroeder {} /* terminating entry */
1893f542974SPaul B Schroeder };
19068e24113SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
1913f542974SPaul B Schroeder
1923f542974SPaul B Schroeder /* This structure holds all of the local port information */
1933f542974SPaul B Schroeder
1943f542974SPaul B Schroeder struct moschip_port {
1953f542974SPaul B Schroeder int port_num; /*Actual port number in the device(1,2,etc) */
1963f542974SPaul B Schroeder struct urb *read_urb; /* read URB for this port */
1973f542974SPaul B Schroeder __u8 shadowLCR; /* last LCR value received */
1983f542974SPaul B Schroeder __u8 shadowMCR; /* last MCR value received */
1993f542974SPaul B Schroeder struct usb_serial_port *port; /* loop back to the owner of this object */
2003f542974SPaul B Schroeder
2013f542974SPaul B Schroeder /* Offsets */
2023f542974SPaul B Schroeder __u8 SpRegOffset;
2033f542974SPaul B Schroeder __u8 ControlRegOffset;
2043f542974SPaul B Schroeder __u8 DcrRegOffset;
2053f542974SPaul B Schroeder
2060de9a702SOliver Neukum spinlock_t pool_lock;
2073f542974SPaul B Schroeder struct urb *write_urb_pool[NUM_URBS];
2080de9a702SOliver Neukum char busy[NUM_URBS];
20950de36f7SGreg Kroah-Hartman bool read_urb_busy;
2103f542974SPaul B Schroeder
2110eafe4deSDonald /* For device(s) with LED indicator */
2120eafe4deSDonald bool has_led;
2130eafe4deSDonald struct timer_list led_timer1; /* Timer for LED on */
2140eafe4deSDonald struct timer_list led_timer2; /* Timer for LED off */
21505cf0decSJohan Hovold struct urb *led_urb;
21605cf0decSJohan Hovold struct usb_ctrlrequest *led_dr;
217d8a083ccSJohan Hovold
218d8a083ccSJohan Hovold unsigned long flags;
2190eafe4deSDonald };
2203f542974SPaul B Schroeder
2213f542974SPaul B Schroeder /*
2223f542974SPaul B Schroeder * mos7840_set_reg_sync
2233f542974SPaul B Schroeder * To set the Control register by calling usb_fill_control_urb function
2243f542974SPaul B Schroeder * by passing usb_sndctrlpipe function as parameter.
2253f542974SPaul B Schroeder */
2263f542974SPaul B Schroeder
mos7840_set_reg_sync(struct usb_serial_port * port,__u16 reg,__u16 val)2273f542974SPaul B Schroeder static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
2283f542974SPaul B Schroeder __u16 val)
2293f542974SPaul B Schroeder {
2303f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev;
2313f542974SPaul B Schroeder val = val & 0x00ff;
2329c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
2333f542974SPaul B Schroeder
2343f542974SPaul B Schroeder return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2353f542974SPaul B Schroeder MCS_WR_RTYPE, val, reg, NULL, 0,
2363f542974SPaul B Schroeder MOS_WDR_TIMEOUT);
2373f542974SPaul B Schroeder }
2383f542974SPaul B Schroeder
2393f542974SPaul B Schroeder /*
2403f542974SPaul B Schroeder * mos7840_get_reg_sync
2413f542974SPaul B Schroeder * To set the Uart register by calling usb_fill_control_urb function by
2423f542974SPaul B Schroeder * passing usb_rcvctrlpipe function as parameter.
2433f542974SPaul B Schroeder */
2443f542974SPaul B Schroeder
mos7840_get_reg_sync(struct usb_serial_port * port,__u16 reg,__u16 * val)2453f542974SPaul B Schroeder static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
2463f542974SPaul B Schroeder __u16 *val)
2473f542974SPaul B Schroeder {
2483f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev;
2493f542974SPaul B Schroeder int ret = 0;
2509e221a35SJohan Hovold u8 *buf;
2519e221a35SJohan Hovold
2529e221a35SJohan Hovold buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2539e221a35SJohan Hovold if (!buf)
2549e221a35SJohan Hovold return -ENOMEM;
2553f542974SPaul B Schroeder
2563f542974SPaul B Schroeder ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
2579e221a35SJohan Hovold MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
2583f542974SPaul B Schroeder MOS_WDR_TIMEOUT);
259cd8db057SJohan Hovold if (ret < VENDOR_READ_LENGTH) {
260cd8db057SJohan Hovold if (ret >= 0)
261cd8db057SJohan Hovold ret = -EIO;
262cd8db057SJohan Hovold goto out;
263cd8db057SJohan Hovold }
264cd8db057SJohan Hovold
2659e221a35SJohan Hovold *val = buf[0];
2669c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
267cd8db057SJohan Hovold out:
2689e221a35SJohan Hovold kfree(buf);
2693f542974SPaul B Schroeder return ret;
2703f542974SPaul B Schroeder }
2713f542974SPaul B Schroeder
2723f542974SPaul B Schroeder /*
2733f542974SPaul B Schroeder * mos7840_set_uart_reg
2743f542974SPaul B Schroeder * To set the Uart register by calling usb_fill_control_urb function by
2753f542974SPaul B Schroeder * passing usb_sndctrlpipe function as parameter.
2763f542974SPaul B Schroeder */
2773f542974SPaul B Schroeder
mos7840_set_uart_reg(struct usb_serial_port * port,__u16 reg,__u16 val)2783f542974SPaul B Schroeder static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
2793f542974SPaul B Schroeder __u16 val)
2803f542974SPaul B Schroeder {
2813f542974SPaul B Schroeder
2823f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev;
2833f542974SPaul B Schroeder val = val & 0x00ff;
284880af9dbSAlan Cox /* For the UART control registers, the application number need
285880af9dbSAlan Cox to be Or'ed */
286e8603076SJackyChou if (port->serial->num_ports == 2 && port->port_number != 0)
2871143832eSGreg Kroah-Hartman val |= ((__u16)port->port_number + 2) << 8;
288e8603076SJackyChou else
289e8603076SJackyChou val |= ((__u16)port->port_number + 1) << 8;
2909c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
2913f542974SPaul B Schroeder return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
2923f542974SPaul B Schroeder MCS_WR_RTYPE, val, reg, NULL, 0,
2933f542974SPaul B Schroeder MOS_WDR_TIMEOUT);
2943f542974SPaul B Schroeder
2953f542974SPaul B Schroeder }
2963f542974SPaul B Schroeder
2973f542974SPaul B Schroeder /*
2983f542974SPaul B Schroeder * mos7840_get_uart_reg
2993f542974SPaul B Schroeder * To set the Control register by calling usb_fill_control_urb function
3003f542974SPaul B Schroeder * by passing usb_rcvctrlpipe function as parameter.
3013f542974SPaul B Schroeder */
mos7840_get_uart_reg(struct usb_serial_port * port,__u16 reg,__u16 * val)3023f542974SPaul B Schroeder static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
3033f542974SPaul B Schroeder __u16 *val)
3043f542974SPaul B Schroeder {
3053f542974SPaul B Schroeder struct usb_device *dev = port->serial->dev;
3063f542974SPaul B Schroeder int ret = 0;
3073f542974SPaul B Schroeder __u16 Wval;
3089e221a35SJohan Hovold u8 *buf;
3099e221a35SJohan Hovold
3109e221a35SJohan Hovold buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
3119e221a35SJohan Hovold if (!buf)
3129e221a35SJohan Hovold return -ENOMEM;
3133f542974SPaul B Schroeder
3143f542974SPaul B Schroeder /* Wval is same as application number */
315e8603076SJackyChou if (port->serial->num_ports == 2 && port->port_number != 0)
3161143832eSGreg Kroah-Hartman Wval = ((__u16)port->port_number + 2) << 8;
317e8603076SJackyChou else
318e8603076SJackyChou Wval = ((__u16)port->port_number + 1) << 8;
3199c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
3203f542974SPaul B Schroeder ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
3219e221a35SJohan Hovold MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
3223f542974SPaul B Schroeder MOS_WDR_TIMEOUT);
323cd8db057SJohan Hovold if (ret < VENDOR_READ_LENGTH) {
324cd8db057SJohan Hovold if (ret >= 0)
325cd8db057SJohan Hovold ret = -EIO;
326cd8db057SJohan Hovold goto out;
327cd8db057SJohan Hovold }
3289e221a35SJohan Hovold *val = buf[0];
329cd8db057SJohan Hovold out:
3309e221a35SJohan Hovold kfree(buf);
3313f542974SPaul B Schroeder return ret;
3323f542974SPaul B Schroeder }
3333f542974SPaul B Schroeder
mos7840_dump_serial_port(struct usb_serial_port * port,struct moschip_port * mos7840_port)3349c134a14SGreg Kroah-Hartman static void mos7840_dump_serial_port(struct usb_serial_port *port,
3359c134a14SGreg Kroah-Hartman struct moschip_port *mos7840_port)
3363f542974SPaul B Schroeder {
3373f542974SPaul B Schroeder
3389c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
3399c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
3409c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
3413f542974SPaul B Schroeder
3423f542974SPaul B Schroeder }
3433f542974SPaul B Schroeder
3443f542974SPaul B Schroeder /************************************************************************/
3453f542974SPaul B Schroeder /************************************************************************/
3463f542974SPaul B Schroeder /* U S B C A L L B A C K F U N C T I O N S */
3473f542974SPaul B Schroeder /* U S B C A L L B A C K F U N C T I O N S */
3483f542974SPaul B Schroeder /************************************************************************/
3493f542974SPaul B Schroeder /************************************************************************/
3503f542974SPaul B Schroeder
mos7840_set_led_callback(struct urb * urb)3510eafe4deSDonald static void mos7840_set_led_callback(struct urb *urb)
3520eafe4deSDonald {
3530eafe4deSDonald switch (urb->status) {
3540eafe4deSDonald case 0:
3550eafe4deSDonald /* Success */
3560eafe4deSDonald break;
3570eafe4deSDonald case -ECONNRESET:
3580eafe4deSDonald case -ENOENT:
3590eafe4deSDonald case -ESHUTDOWN:
3600eafe4deSDonald /* This urb is terminated, clean up */
361d9a38a87SJohan Hovold dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
3629c134a14SGreg Kroah-Hartman __func__, urb->status);
3630eafe4deSDonald break;
3640eafe4deSDonald default:
365d9a38a87SJohan Hovold dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
3669c134a14SGreg Kroah-Hartman __func__, urb->status);
3670eafe4deSDonald }
3680eafe4deSDonald }
3690eafe4deSDonald
mos7840_set_led_async(struct moschip_port * mcs,__u16 wval,__u16 reg)3700eafe4deSDonald static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
3710eafe4deSDonald __u16 reg)
3720eafe4deSDonald {
3730eafe4deSDonald struct usb_device *dev = mcs->port->serial->dev;
37405cf0decSJohan Hovold struct usb_ctrlrequest *dr = mcs->led_dr;
3750eafe4deSDonald
3760eafe4deSDonald dr->bRequestType = MCS_WR_RTYPE;
3770eafe4deSDonald dr->bRequest = MCS_WRREQ;
3780eafe4deSDonald dr->wValue = cpu_to_le16(wval);
3790eafe4deSDonald dr->wIndex = cpu_to_le16(reg);
3800eafe4deSDonald dr->wLength = cpu_to_le16(0);
3810eafe4deSDonald
38205cf0decSJohan Hovold usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
3830eafe4deSDonald (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
3840eafe4deSDonald
38505cf0decSJohan Hovold usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
3860eafe4deSDonald }
3870eafe4deSDonald
mos7840_set_led_sync(struct usb_serial_port * port,__u16 reg,__u16 val)3880eafe4deSDonald static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
3890eafe4deSDonald __u16 val)
3900eafe4deSDonald {
3910eafe4deSDonald struct usb_device *dev = port->serial->dev;
3920eafe4deSDonald
3930eafe4deSDonald usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
3940eafe4deSDonald val, reg, NULL, 0, MOS_WDR_TIMEOUT);
3950eafe4deSDonald }
3960eafe4deSDonald
mos7840_led_off(struct timer_list * t)397e99e88a9SKees Cook static void mos7840_led_off(struct timer_list *t)
3980eafe4deSDonald {
399e99e88a9SKees Cook struct moschip_port *mcs = from_timer(mcs, t, led_timer1);
4000eafe4deSDonald
4010eafe4deSDonald /* Turn off LED */
4020eafe4deSDonald mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
4030eafe4deSDonald mod_timer(&mcs->led_timer2,
4040eafe4deSDonald jiffies + msecs_to_jiffies(LED_OFF_MS));
4050eafe4deSDonald }
4060eafe4deSDonald
mos7840_led_flag_off(struct timer_list * t)407e99e88a9SKees Cook static void mos7840_led_flag_off(struct timer_list *t)
4080eafe4deSDonald {
409e99e88a9SKees Cook struct moschip_port *mcs = from_timer(mcs, t, led_timer2);
4100eafe4deSDonald
41105cf0decSJohan Hovold clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
41205cf0decSJohan Hovold }
41305cf0decSJohan Hovold
mos7840_led_activity(struct usb_serial_port * port)41405cf0decSJohan Hovold static void mos7840_led_activity(struct usb_serial_port *port)
41505cf0decSJohan Hovold {
41605cf0decSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
41705cf0decSJohan Hovold
41805cf0decSJohan Hovold if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
41905cf0decSJohan Hovold return;
42005cf0decSJohan Hovold
42105cf0decSJohan Hovold mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
42205cf0decSJohan Hovold mod_timer(&mos7840_port->led_timer1,
42305cf0decSJohan Hovold jiffies + msecs_to_jiffies(LED_ON_MS));
4240eafe4deSDonald }
4250eafe4deSDonald
4263f542974SPaul B Schroeder /*****************************************************************************
4273f542974SPaul B Schroeder * mos7840_bulk_in_callback
4283f542974SPaul B Schroeder * this is the callback function for when we have received data on the
4293f542974SPaul B Schroeder * bulk in endpoint.
4303f542974SPaul B Schroeder *****************************************************************************/
4313f542974SPaul B Schroeder
mos7840_bulk_in_callback(struct urb * urb)4327d12e780SDavid Howells static void mos7840_bulk_in_callback(struct urb *urb)
4333f542974SPaul B Schroeder {
4343ec9fb6fSJohan Hovold struct moschip_port *mos7840_port = urb->context;
435ce039bd4SJohan Hovold struct usb_serial_port *port = mos7840_port->port;
4360643c724SGreg Kroah-Hartman int retval;
4373f542974SPaul B Schroeder unsigned char *data;
4380643c724SGreg Kroah-Hartman int status = urb->status;
4393f542974SPaul B Schroeder
44050de36f7SGreg Kroah-Hartman if (status) {
4419c134a14SGreg Kroah-Hartman dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
44250de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false;
4433f542974SPaul B Schroeder return;
4443f542974SPaul B Schroeder }
4453f542974SPaul B Schroeder
4463f542974SPaul B Schroeder data = urb->transfer_buffer;
44759d33f2fSGreg Kroah-Hartman usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
4483f542974SPaul B Schroeder
4493f542974SPaul B Schroeder if (urb->actual_length) {
45005c7cd39SJiri Slaby struct tty_port *tport = &mos7840_port->port->port;
45105c7cd39SJiri Slaby tty_insert_flip_string(tport, data, urb->actual_length);
4522e124b4aSJiri Slaby tty_flip_buffer_push(tport);
4538c1a07ffSJohan Hovold port->icount.rx += urb->actual_length;
4548c1a07ffSJohan Hovold dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
4553f542974SPaul B Schroeder }
4563f542974SPaul B Schroeder
45705cf0decSJohan Hovold if (mos7840_port->has_led)
45805cf0decSJohan Hovold mos7840_led_activity(port);
4590de9a702SOliver Neukum
46050de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true;
4610643c724SGreg Kroah-Hartman retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
4623f542974SPaul B Schroeder
4630643c724SGreg Kroah-Hartman if (retval) {
4649c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
46550de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false;
4663f542974SPaul B Schroeder }
4673f542974SPaul B Schroeder }
4683f542974SPaul B Schroeder
4693f542974SPaul B Schroeder /*****************************************************************************
4703f542974SPaul B Schroeder * mos7840_bulk_out_data_callback
471880af9dbSAlan Cox * this is the callback function for when we have finished sending
472880af9dbSAlan Cox * serial data on the bulk out endpoint.
4733f542974SPaul B Schroeder *****************************************************************************/
4743f542974SPaul B Schroeder
mos7840_bulk_out_data_callback(struct urb * urb)4757d12e780SDavid Howells static void mos7840_bulk_out_data_callback(struct urb *urb)
4763f542974SPaul B Schroeder {
477ce039bd4SJohan Hovold struct moschip_port *mos7840_port = urb->context;
478ce039bd4SJohan Hovold struct usb_serial_port *port = mos7840_port->port;
4790643c724SGreg Kroah-Hartman int status = urb->status;
48019bfbf46SJohn Ogness unsigned long flags;
4810de9a702SOliver Neukum int i;
4820de9a702SOliver Neukum
48319bfbf46SJohn Ogness spin_lock_irqsave(&mos7840_port->pool_lock, flags);
4840de9a702SOliver Neukum for (i = 0; i < NUM_URBS; i++) {
4850de9a702SOliver Neukum if (urb == mos7840_port->write_urb_pool[i]) {
4860de9a702SOliver Neukum mos7840_port->busy[i] = 0;
4870de9a702SOliver Neukum break;
4880de9a702SOliver Neukum }
4890de9a702SOliver Neukum }
49019bfbf46SJohn Ogness spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
4910de9a702SOliver Neukum
4920643c724SGreg Kroah-Hartman if (status) {
4939c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
4943f542974SPaul B Schroeder return;
4953f542974SPaul B Schroeder }
4963f542974SPaul B Schroeder
4976aad04f2SJiri Slaby tty_port_tty_wakeup(&port->port);
4983f542974SPaul B Schroeder
4993f542974SPaul B Schroeder }
5003f542974SPaul B Schroeder
5013f542974SPaul B Schroeder /************************************************************************/
5023f542974SPaul 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 */
5033f542974SPaul B Schroeder /************************************************************************/
5043f542974SPaul B Schroeder
5053f542974SPaul B Schroeder /*****************************************************************************
5063f542974SPaul B Schroeder * mos7840_open
5073f542974SPaul B Schroeder * this function is called by the tty driver when a port is opened
5083f542974SPaul B Schroeder * If successful, we return 0
5093f542974SPaul B Schroeder * Otherwise we return a negative error number.
5103f542974SPaul B Schroeder *****************************************************************************/
5113f542974SPaul B Schroeder
mos7840_open(struct tty_struct * tty,struct usb_serial_port * port)512a509a7e4SAlan Cox static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
5133f542974SPaul B Schroeder {
5147b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
515ce039bd4SJohan Hovold struct usb_serial *serial = port->serial;
5163f542974SPaul B Schroeder int response;
5173f542974SPaul B Schroeder int j;
5183f542974SPaul B Schroeder struct urb *urb;
5193f542974SPaul B Schroeder __u16 Data;
5203f542974SPaul B Schroeder int status;
5213f542974SPaul B Schroeder
5223f542974SPaul B Schroeder usb_clear_halt(serial->dev, port->write_urb->pipe);
5233f542974SPaul B Schroeder usb_clear_halt(serial->dev, port->read_urb->pipe);
5243f542974SPaul B Schroeder
5253f542974SPaul B Schroeder /* Initialising the write urb pool */
5263f542974SPaul B Schroeder for (j = 0; j < NUM_URBS; ++j) {
5270de9a702SOliver Neukum urb = usb_alloc_urb(0, GFP_KERNEL);
5283f542974SPaul B Schroeder mos7840_port->write_urb_pool[j] = urb;
52910c642d0SJohan Hovold if (!urb)
5303f542974SPaul B Schroeder continue;
5313f542974SPaul B Schroeder
532880af9dbSAlan Cox urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
533880af9dbSAlan Cox GFP_KERNEL);
5343f542974SPaul B Schroeder if (!urb->transfer_buffer) {
5350de9a702SOliver Neukum usb_free_urb(urb);
5360de9a702SOliver Neukum mos7840_port->write_urb_pool[j] = NULL;
5373f542974SPaul B Schroeder continue;
5383f542974SPaul B Schroeder }
5393f542974SPaul B Schroeder }
5403f542974SPaul B Schroeder
5413f542974SPaul B Schroeder /*****************************************************************************
5423f542974SPaul B Schroeder * Initialize MCS7840 -- Write Init values to corresponding Registers
5433f542974SPaul B Schroeder *
5443f542974SPaul B Schroeder * Register Index
5453f542974SPaul B Schroeder * 1 : IER
5463f542974SPaul B Schroeder * 2 : FCR
5473f542974SPaul B Schroeder * 3 : LCR
5483f542974SPaul B Schroeder * 4 : MCR
5493f542974SPaul B Schroeder *
5503f542974SPaul B Schroeder * 0x08 : SP1/2 Control Reg
5513f542974SPaul B Schroeder *****************************************************************************/
5523f542974SPaul B Schroeder
553880af9dbSAlan Cox /* NEED to check the following Block */
5543f542974SPaul B Schroeder
5553f542974SPaul B Schroeder Data = 0x0;
5563f542974SPaul B Schroeder status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
5573f542974SPaul B Schroeder if (status < 0) {
5589c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Reading Spreg failed\n");
5595f8a2e68SJohan Hovold goto err;
5603f542974SPaul B Schroeder }
5613f542974SPaul B Schroeder Data |= 0x80;
5623f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
5633f542974SPaul B Schroeder if (status < 0) {
5649c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "writing Spreg failed\n");
5655f8a2e68SJohan Hovold goto err;
5663f542974SPaul B Schroeder }
5673f542974SPaul B Schroeder
5683f542974SPaul B Schroeder Data &= ~0x80;
5693f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
5703f542974SPaul B Schroeder if (status < 0) {
5719c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "writing Spreg failed\n");
5725f8a2e68SJohan Hovold goto err;
5733f542974SPaul B Schroeder }
574880af9dbSAlan Cox /* End of block to be checked */
5753f542974SPaul B Schroeder
5763f542974SPaul B Schroeder Data = 0x0;
577880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
578880af9dbSAlan Cox &Data);
5793f542974SPaul B Schroeder if (status < 0) {
5809c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Reading Controlreg failed\n");
5815f8a2e68SJohan Hovold goto err;
5823f542974SPaul B Schroeder }
583880af9dbSAlan Cox Data |= 0x08; /* Driver done bit */
584880af9dbSAlan Cox Data |= 0x20; /* rx_disable */
585880af9dbSAlan Cox status = mos7840_set_reg_sync(port,
586880af9dbSAlan Cox mos7840_port->ControlRegOffset, Data);
5873f542974SPaul B Schroeder if (status < 0) {
5889c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "writing Controlreg failed\n");
5895f8a2e68SJohan Hovold goto err;
5903f542974SPaul B Schroeder }
591880af9dbSAlan Cox /* do register settings here */
592880af9dbSAlan Cox /* Set all regs to the device default values. */
593880af9dbSAlan Cox /***********************************
594880af9dbSAlan Cox * First Disable all interrupts.
595880af9dbSAlan Cox ***********************************/
5963f542974SPaul B Schroeder Data = 0x00;
5973f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
5983f542974SPaul B Schroeder if (status < 0) {
5999c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "disabling interrupts failed\n");
6005f8a2e68SJohan Hovold goto err;
6013f542974SPaul B Schroeder }
602880af9dbSAlan Cox /* Set FIFO_CONTROL_REGISTER to the default value */
6033f542974SPaul B Schroeder Data = 0x00;
6043f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
6053f542974SPaul B Schroeder if (status < 0) {
6069c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
6075f8a2e68SJohan Hovold goto err;
6083f542974SPaul B Schroeder }
6093f542974SPaul B Schroeder
6103f542974SPaul B Schroeder Data = 0xcf;
6113f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
6123f542974SPaul B Schroeder if (status < 0) {
6139c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
6145f8a2e68SJohan Hovold goto err;
6153f542974SPaul B Schroeder }
6163f542974SPaul B Schroeder
6173f542974SPaul B Schroeder Data = 0x03;
6183f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
6193f542974SPaul B Schroeder mos7840_port->shadowLCR = Data;
6203f542974SPaul B Schroeder
6213f542974SPaul B Schroeder Data = 0x0b;
6223f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
6233f542974SPaul B Schroeder mos7840_port->shadowMCR = Data;
6243f542974SPaul B Schroeder
6253f542974SPaul B Schroeder Data = 0x00;
6263f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
6273f542974SPaul B Schroeder mos7840_port->shadowLCR = Data;
6283f542974SPaul B Schroeder
629880af9dbSAlan Cox Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
6303f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
6313f542974SPaul B Schroeder
6323f542974SPaul B Schroeder Data = 0x0c;
6333f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
6343f542974SPaul B Schroeder
6353f542974SPaul B Schroeder Data = 0x0;
6363f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
6373f542974SPaul B Schroeder
6383f542974SPaul B Schroeder Data = 0x00;
6393f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
6403f542974SPaul B Schroeder
6413f542974SPaul B Schroeder Data = Data & ~SERIAL_LCR_DLAB;
6423f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
6433f542974SPaul B Schroeder mos7840_port->shadowLCR = Data;
6443f542974SPaul B Schroeder
645880af9dbSAlan Cox /* clearing Bulkin and Bulkout Fifo */
6463f542974SPaul B Schroeder Data = 0x0;
6473f542974SPaul B Schroeder status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
6483f542974SPaul B Schroeder
6493f542974SPaul B Schroeder Data = Data | 0x0c;
6503f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
6513f542974SPaul B Schroeder
6523f542974SPaul B Schroeder Data = Data & ~0x0c;
6533f542974SPaul B Schroeder status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
654880af9dbSAlan Cox /* Finally enable all interrupts */
6553f542974SPaul B Schroeder Data = 0x0c;
6563f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
6573f542974SPaul B Schroeder
658880af9dbSAlan Cox /* clearing rx_disable */
6593f542974SPaul B Schroeder Data = 0x0;
660880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
661880af9dbSAlan Cox &Data);
6623f542974SPaul B Schroeder Data = Data & ~0x20;
663880af9dbSAlan Cox status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
664880af9dbSAlan Cox Data);
6653f542974SPaul B Schroeder
666880af9dbSAlan Cox /* rx_negate */
6673f542974SPaul B Schroeder Data = 0x0;
668880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
669880af9dbSAlan Cox &Data);
6703f542974SPaul B Schroeder Data = Data | 0x10;
671880af9dbSAlan Cox status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
672880af9dbSAlan Cox Data);
6733f542974SPaul B Schroeder
6741143832eSGreg Kroah-Hartman dev_dbg(&port->dev, "port number is %d\n", port->port_number);
675e5b1e206SGreg Kroah-Hartman dev_dbg(&port->dev, "minor number is %d\n", port->minor);
6769c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
6779c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
6789c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
6799c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
6803f542974SPaul B Schroeder mos7840_port->read_urb = port->read_urb;
6813f542974SPaul B Schroeder
6823f542974SPaul B Schroeder /* set up our bulk in urb */
6831143832eSGreg Kroah-Hartman if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
684093ea2d3SDonald Lee usb_fill_bulk_urb(mos7840_port->read_urb,
685093ea2d3SDonald Lee serial->dev,
686093ea2d3SDonald Lee usb_rcvbulkpipe(serial->dev,
687093ea2d3SDonald Lee (port->bulk_in_endpointAddress) + 2),
688093ea2d3SDonald Lee port->bulk_in_buffer,
689093ea2d3SDonald Lee mos7840_port->read_urb->transfer_buffer_length,
690093ea2d3SDonald Lee mos7840_bulk_in_callback, mos7840_port);
691093ea2d3SDonald Lee } else {
6923f542974SPaul B Schroeder usb_fill_bulk_urb(mos7840_port->read_urb,
6933f542974SPaul B Schroeder serial->dev,
6943f542974SPaul B Schroeder usb_rcvbulkpipe(serial->dev,
6953f542974SPaul B Schroeder port->bulk_in_endpointAddress),
6963f542974SPaul B Schroeder port->bulk_in_buffer,
6973f542974SPaul B Schroeder mos7840_port->read_urb->transfer_buffer_length,
6983f542974SPaul B Schroeder mos7840_bulk_in_callback, mos7840_port);
699093ea2d3SDonald Lee }
7003f542974SPaul B Schroeder
7019c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
70250de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true;
7033f542974SPaul B Schroeder response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
7043f542974SPaul B Schroeder if (response) {
705194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s - Error %d submitting control urb\n",
706194343d9SGreg Kroah-Hartman __func__, response);
70750de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false;
7083f542974SPaul B Schroeder }
7093f542974SPaul B Schroeder
7103f542974SPaul B Schroeder /* initialize our port settings */
711880af9dbSAlan Cox /* Must set to enable ints! */
712880af9dbSAlan Cox mos7840_port->shadowMCR = MCR_MASTER_IE;
7133f542974SPaul B Schroeder
7143f542974SPaul B Schroeder return 0;
7155f8a2e68SJohan Hovold err:
7165f8a2e68SJohan Hovold for (j = 0; j < NUM_URBS; ++j) {
7175f8a2e68SJohan Hovold urb = mos7840_port->write_urb_pool[j];
7185f8a2e68SJohan Hovold if (!urb)
7195f8a2e68SJohan Hovold continue;
7205f8a2e68SJohan Hovold kfree(urb->transfer_buffer);
7215f8a2e68SJohan Hovold usb_free_urb(urb);
7225f8a2e68SJohan Hovold }
7235f8a2e68SJohan Hovold return status;
7243f542974SPaul B Schroeder }
7253f542974SPaul B Schroeder
7263f542974SPaul B Schroeder /*****************************************************************************
7273f542974SPaul B Schroeder * mos7840_chars_in_buffer
7283f542974SPaul B Schroeder * this function is called by the tty driver when it wants to know how many
7293f542974SPaul B Schroeder * bytes of data we currently have outstanding in the port (data that has
7303f542974SPaul B Schroeder * been written, but hasn't made it out the port yet)
7313f542974SPaul B Schroeder *****************************************************************************/
7323f542974SPaul B Schroeder
mos7840_chars_in_buffer(struct tty_struct * tty)733155591d3SJiri Slaby static unsigned int mos7840_chars_in_buffer(struct tty_struct *tty)
7343f542974SPaul B Schroeder {
73595da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
7367b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
7373f542974SPaul B Schroeder int i;
738155591d3SJiri Slaby unsigned int chars = 0;
7390de9a702SOliver Neukum unsigned long flags;
7403f542974SPaul B Schroeder
7410de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags);
7425c263b92SMark Ferrell for (i = 0; i < NUM_URBS; ++i) {
7435c263b92SMark Ferrell if (mos7840_port->busy[i]) {
7445c263b92SMark Ferrell struct urb *urb = mos7840_port->write_urb_pool[i];
7455c263b92SMark Ferrell chars += urb->transfer_buffer_length;
7465c263b92SMark Ferrell }
7475c263b92SMark Ferrell }
7480de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
749155591d3SJiri Slaby dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
7500de9a702SOliver Neukum return chars;
7513f542974SPaul B Schroeder
7523f542974SPaul B Schroeder }
7533f542974SPaul B Schroeder
7543f542974SPaul B Schroeder /*****************************************************************************
7553f542974SPaul B Schroeder * mos7840_close
7563f542974SPaul B Schroeder * this function is called by the tty driver when a port is closed
7573f542974SPaul B Schroeder *****************************************************************************/
7583f542974SPaul B Schroeder
mos7840_close(struct usb_serial_port * port)759335f8514SAlan Cox static void mos7840_close(struct usb_serial_port *port)
7603f542974SPaul B Schroeder {
7617b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
7623f542974SPaul B Schroeder int j;
7633f542974SPaul B Schroeder __u16 Data;
7643f542974SPaul B Schroeder
7653f542974SPaul B Schroeder for (j = 0; j < NUM_URBS; ++j)
7663f542974SPaul B Schroeder usb_kill_urb(mos7840_port->write_urb_pool[j]);
7673f542974SPaul B Schroeder
7683f542974SPaul B Schroeder /* Freeing Write URBs */
7693f542974SPaul B Schroeder for (j = 0; j < NUM_URBS; ++j) {
7703f542974SPaul B Schroeder if (mos7840_port->write_urb_pool[j]) {
77191c72df1SFabian Frederick kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
7723f542974SPaul B Schroeder usb_free_urb(mos7840_port->write_urb_pool[j]);
7733f542974SPaul B Schroeder }
7743f542974SPaul B Schroeder }
7753f542974SPaul B Schroeder
7763f542974SPaul B Schroeder usb_kill_urb(mos7840_port->read_urb);
77750de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false;
778bb3529c6SJohan Hovold
7793f542974SPaul B Schroeder Data = 0x0;
7803f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
7813f542974SPaul B Schroeder
7823f542974SPaul B Schroeder Data = 0x00;
7833f542974SPaul B Schroeder mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
7843f542974SPaul B Schroeder }
7853f542974SPaul B Schroeder
7863f542974SPaul B Schroeder /*****************************************************************************
7873f542974SPaul B Schroeder * mos7840_break
7883f542974SPaul B Schroeder * this function sends a break to the port
7893f542974SPaul B Schroeder *****************************************************************************/
mos7840_break(struct tty_struct * tty,int break_state)7906ff58ae1SJohan Hovold static int mos7840_break(struct tty_struct *tty, int break_state)
7913f542974SPaul B Schroeder {
79295da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
7937b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
7943f542974SPaul B Schroeder unsigned char data;
7953f542974SPaul B Schroeder
79695da310eSAlan Cox if (break_state == -1)
7973f542974SPaul B Schroeder data = mos7840_port->shadowLCR | LCR_SET_BREAK;
79895da310eSAlan Cox else
7993f542974SPaul B Schroeder data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
8003f542974SPaul B Schroeder
8016b447f04SAlan Cox /* FIXME: no locking on shadowLCR anywhere in driver */
8023f542974SPaul B Schroeder mos7840_port->shadowLCR = data;
8039c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
8046ff58ae1SJohan Hovold
8056ff58ae1SJohan Hovold return mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
8063f542974SPaul B Schroeder mos7840_port->shadowLCR);
8073f542974SPaul B Schroeder }
8083f542974SPaul B Schroeder
8093f542974SPaul B Schroeder /*****************************************************************************
8103f542974SPaul B Schroeder * mos7840_write_room
8113f542974SPaul B Schroeder * this function is called by the tty driver when it wants to know how many
8123f542974SPaul B Schroeder * bytes of data we can accept for a specific port.
8133f542974SPaul B Schroeder *****************************************************************************/
8143f542974SPaul B Schroeder
mos7840_write_room(struct tty_struct * tty)81594cc7aeaSJiri Slaby static unsigned int mos7840_write_room(struct tty_struct *tty)
8163f542974SPaul B Schroeder {
81795da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
8187b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
8193f542974SPaul B Schroeder int i;
82094cc7aeaSJiri Slaby unsigned int room = 0;
8210de9a702SOliver Neukum unsigned long flags;
8223f542974SPaul B Schroeder
8230de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags);
8243f542974SPaul B Schroeder for (i = 0; i < NUM_URBS; ++i) {
825880af9dbSAlan Cox if (!mos7840_port->busy[i])
8263f542974SPaul B Schroeder room += URB_TRANSFER_BUFFER_SIZE;
8273f542974SPaul B Schroeder }
8280de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
8293f542974SPaul B Schroeder
8300de9a702SOliver Neukum room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
83194cc7aeaSJiri Slaby dev_dbg(&mos7840_port->port->dev, "%s - returns %u\n", __func__, room);
8320de9a702SOliver Neukum return room;
8333f542974SPaul B Schroeder
8343f542974SPaul B Schroeder }
8353f542974SPaul B Schroeder
8363f542974SPaul B Schroeder /*****************************************************************************
8373f542974SPaul B Schroeder * mos7840_write
8383f542974SPaul B Schroeder * this function is called by the tty driver when data should be written to
8393f542974SPaul B Schroeder * the port.
8403f542974SPaul B Schroeder * If successful, we return the number of bytes written, otherwise we
8413f542974SPaul B Schroeder * return a negative error number.
8423f542974SPaul B Schroeder *****************************************************************************/
8433f542974SPaul B Schroeder
mos7840_write(struct tty_struct * tty,struct usb_serial_port * port,const unsigned char * data,int count)84495da310eSAlan Cox static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
8453f542974SPaul B Schroeder const unsigned char *data, int count)
8463f542974SPaul B Schroeder {
8477b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
848ce039bd4SJohan Hovold struct usb_serial *serial = port->serial;
8493f542974SPaul B Schroeder int status;
8503f542974SPaul B Schroeder int i;
8513f542974SPaul B Schroeder int bytes_sent = 0;
8523f542974SPaul B Schroeder int transfer_size;
8530de9a702SOliver Neukum unsigned long flags;
8543f542974SPaul B Schroeder struct urb *urb;
855880af9dbSAlan Cox /* __u16 Data; */
8563f542974SPaul B Schroeder const unsigned char *current_position = data;
8573f542974SPaul B Schroeder
8583f542974SPaul B Schroeder /* try to find a free urb in the list */
8593f542974SPaul B Schroeder urb = NULL;
8603f542974SPaul B Schroeder
8610de9a702SOliver Neukum spin_lock_irqsave(&mos7840_port->pool_lock, flags);
8623f542974SPaul B Schroeder for (i = 0; i < NUM_URBS; ++i) {
8630de9a702SOliver Neukum if (!mos7840_port->busy[i]) {
8640de9a702SOliver Neukum mos7840_port->busy[i] = 1;
8653f542974SPaul B Schroeder urb = mos7840_port->write_urb_pool[i];
8669c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "URB:%d\n", i);
8673f542974SPaul B Schroeder break;
8683f542974SPaul B Schroeder }
8693f542974SPaul B Schroeder }
8700de9a702SOliver Neukum spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
8713f542974SPaul B Schroeder
8723f542974SPaul B Schroeder if (urb == NULL) {
8739c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
8743f542974SPaul B Schroeder goto exit;
8753f542974SPaul B Schroeder }
8763f542974SPaul B Schroeder
8773f542974SPaul B Schroeder if (urb->transfer_buffer == NULL) {
8783b7c7e52SAlexey Khoroshilov urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
8793b7c7e52SAlexey Khoroshilov GFP_ATOMIC);
880a70aa7dcSDan Carpenter if (!urb->transfer_buffer) {
881a70aa7dcSDan Carpenter bytes_sent = -ENOMEM;
8823f542974SPaul B Schroeder goto exit;
8833f542974SPaul B Schroeder }
884a70aa7dcSDan Carpenter }
8853f542974SPaul B Schroeder transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
8863f542974SPaul B Schroeder
8873f542974SPaul B Schroeder memcpy(urb->transfer_buffer, current_position, transfer_size);
8883f542974SPaul B Schroeder
8893f542974SPaul B Schroeder /* fill urb with data and submit */
8901143832eSGreg Kroah-Hartman if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
891093ea2d3SDonald Lee usb_fill_bulk_urb(urb,
892093ea2d3SDonald Lee serial->dev,
893093ea2d3SDonald Lee usb_sndbulkpipe(serial->dev,
894093ea2d3SDonald Lee (port->bulk_out_endpointAddress) + 2),
895093ea2d3SDonald Lee urb->transfer_buffer,
896093ea2d3SDonald Lee transfer_size,
897093ea2d3SDonald Lee mos7840_bulk_out_data_callback, mos7840_port);
898093ea2d3SDonald Lee } else {
8993f542974SPaul B Schroeder usb_fill_bulk_urb(urb,
9003f542974SPaul B Schroeder serial->dev,
9013f542974SPaul B Schroeder usb_sndbulkpipe(serial->dev,
9023f542974SPaul B Schroeder port->bulk_out_endpointAddress),
9033f542974SPaul B Schroeder urb->transfer_buffer,
9043f542974SPaul B Schroeder transfer_size,
9053f542974SPaul B Schroeder mos7840_bulk_out_data_callback, mos7840_port);
906093ea2d3SDonald Lee }
9073f542974SPaul B Schroeder
9089c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
9093f542974SPaul B Schroeder
91005cf0decSJohan Hovold if (mos7840_port->has_led)
91105cf0decSJohan Hovold mos7840_led_activity(port);
9120eafe4deSDonald
9133f542974SPaul B Schroeder /* send it down the pipe */
9143f542974SPaul B Schroeder status = usb_submit_urb(urb, GFP_ATOMIC);
9153f542974SPaul B Schroeder
9163f542974SPaul B Schroeder if (status) {
9170de9a702SOliver Neukum mos7840_port->busy[i] = 0;
91822a416c4SJohan Hovold dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
919194343d9SGreg Kroah-Hartman "with status = %d\n", __func__, status);
9203f542974SPaul B Schroeder bytes_sent = status;
9213f542974SPaul B Schroeder goto exit;
9223f542974SPaul B Schroeder }
9233f542974SPaul B Schroeder bytes_sent = transfer_size;
9248c1a07ffSJohan Hovold port->icount.tx += transfer_size;
9258c1a07ffSJohan Hovold dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
9263f542974SPaul B Schroeder exit:
9273f542974SPaul B Schroeder return bytes_sent;
9283f542974SPaul B Schroeder
9293f542974SPaul B Schroeder }
9303f542974SPaul B Schroeder
9313f542974SPaul B Schroeder /*****************************************************************************
9323f542974SPaul B Schroeder * mos7840_throttle
9333f542974SPaul B Schroeder * this function is called by the tty driver when it wants to stop the data
9343f542974SPaul B Schroeder * being read from the port.
9353f542974SPaul B Schroeder *****************************************************************************/
9363f542974SPaul B Schroeder
mos7840_throttle(struct tty_struct * tty)93795da310eSAlan Cox static void mos7840_throttle(struct tty_struct *tty)
9383f542974SPaul B Schroeder {
93995da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
9407b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
9413f542974SPaul B Schroeder int status;
9423f542974SPaul B Schroeder
9433f542974SPaul B Schroeder /* if we are implementing XON/XOFF, send the stop character */
9443f542974SPaul B Schroeder if (I_IXOFF(tty)) {
9453f542974SPaul B Schroeder unsigned char stop_char = STOP_CHAR(tty);
94695da310eSAlan Cox status = mos7840_write(tty, port, &stop_char, 1);
94795da310eSAlan Cox if (status <= 0)
9483f542974SPaul B Schroeder return;
9493f542974SPaul B Schroeder }
9503f542974SPaul B Schroeder /* if we are implementing RTS/CTS, toggle that line */
9519db276f8SPeter Hurley if (C_CRTSCTS(tty)) {
9523f542974SPaul B Schroeder mos7840_port->shadowMCR &= ~MCR_RTS;
95395da310eSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
9543f542974SPaul B Schroeder mos7840_port->shadowMCR);
95595da310eSAlan Cox if (status < 0)
9563f542974SPaul B Schroeder return;
9573f542974SPaul B Schroeder }
9583f542974SPaul B Schroeder }
9593f542974SPaul B Schroeder
9603f542974SPaul B Schroeder /*****************************************************************************
9613f542974SPaul B Schroeder * mos7840_unthrottle
962880af9dbSAlan Cox * this function is called by the tty driver when it wants to resume
963880af9dbSAlan Cox * the data being read from the port (called after mos7840_throttle is
964880af9dbSAlan Cox * called)
9653f542974SPaul B Schroeder *****************************************************************************/
mos7840_unthrottle(struct tty_struct * tty)96695da310eSAlan Cox static void mos7840_unthrottle(struct tty_struct *tty)
9673f542974SPaul B Schroeder {
96895da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
9697b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
9703f542974SPaul B Schroeder int status;
9713f542974SPaul B Schroeder
9723f542974SPaul B Schroeder /* if we are implementing XON/XOFF, send the start character */
9733f542974SPaul B Schroeder if (I_IXOFF(tty)) {
9743f542974SPaul B Schroeder unsigned char start_char = START_CHAR(tty);
97595da310eSAlan Cox status = mos7840_write(tty, port, &start_char, 1);
97695da310eSAlan Cox if (status <= 0)
9773f542974SPaul B Schroeder return;
9783f542974SPaul B Schroeder }
9793f542974SPaul B Schroeder
9803f542974SPaul B Schroeder /* if we are implementing RTS/CTS, toggle that line */
9819db276f8SPeter Hurley if (C_CRTSCTS(tty)) {
9823f542974SPaul B Schroeder mos7840_port->shadowMCR |= MCR_RTS;
98395da310eSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
9843f542974SPaul B Schroeder mos7840_port->shadowMCR);
98595da310eSAlan Cox if (status < 0)
9863f542974SPaul B Schroeder return;
9873f542974SPaul B Schroeder }
9883f542974SPaul B Schroeder }
9893f542974SPaul B Schroeder
mos7840_tiocmget(struct tty_struct * tty)99060b33c13SAlan Cox static int mos7840_tiocmget(struct tty_struct *tty)
9913f542974SPaul B Schroeder {
99295da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
9933f542974SPaul B Schroeder unsigned int result;
9943f542974SPaul B Schroeder __u16 msr;
9953f542974SPaul B Schroeder __u16 mcr;
996880af9dbSAlan Cox int status;
9973f542974SPaul B Schroeder
9983f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
999cd8db057SJohan Hovold if (status < 0)
1000a91ccd26SJohan Hovold return -EIO;
10013f542974SPaul B Schroeder status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1002cd8db057SJohan Hovold if (status < 0)
1003a91ccd26SJohan Hovold return -EIO;
10043f542974SPaul B Schroeder result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
10053f542974SPaul B Schroeder | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
10063f542974SPaul B Schroeder | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
10073f542974SPaul B Schroeder | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
10083f542974SPaul B Schroeder | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
10093f542974SPaul B Schroeder | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
10103f542974SPaul B Schroeder | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
10113f542974SPaul B Schroeder
10129c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
10133f542974SPaul B Schroeder
10143f542974SPaul B Schroeder return result;
10153f542974SPaul B Schroeder }
10163f542974SPaul B Schroeder
mos7840_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)101720b9d177SAlan Cox static int mos7840_tiocmset(struct tty_struct *tty,
10183f542974SPaul B Schroeder unsigned int set, unsigned int clear)
10193f542974SPaul B Schroeder {
102095da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
10217b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
10223f542974SPaul B Schroeder unsigned int mcr;
102387521c46SRoel Kluin int status;
10243f542974SPaul B Schroeder
1025e2984494SAlan Cox /* FIXME: What locks the port registers ? */
10263f542974SPaul B Schroeder mcr = mos7840_port->shadowMCR;
10273f542974SPaul B Schroeder if (clear & TIOCM_RTS)
10283f542974SPaul B Schroeder mcr &= ~MCR_RTS;
10293f542974SPaul B Schroeder if (clear & TIOCM_DTR)
10303f542974SPaul B Schroeder mcr &= ~MCR_DTR;
10313f542974SPaul B Schroeder if (clear & TIOCM_LOOP)
10323f542974SPaul B Schroeder mcr &= ~MCR_LOOPBACK;
10333f542974SPaul B Schroeder
10343f542974SPaul B Schroeder if (set & TIOCM_RTS)
10353f542974SPaul B Schroeder mcr |= MCR_RTS;
10363f542974SPaul B Schroeder if (set & TIOCM_DTR)
10373f542974SPaul B Schroeder mcr |= MCR_DTR;
10383f542974SPaul B Schroeder if (set & TIOCM_LOOP)
10393f542974SPaul B Schroeder mcr |= MCR_LOOPBACK;
10403f542974SPaul B Schroeder
10413f542974SPaul B Schroeder mos7840_port->shadowMCR = mcr;
10423f542974SPaul B Schroeder
10433f542974SPaul B Schroeder status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
10443f542974SPaul B Schroeder if (status < 0) {
10459c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
104687521c46SRoel Kluin return status;
10473f542974SPaul B Schroeder }
10483f542974SPaul B Schroeder
10493f542974SPaul B Schroeder return 0;
10503f542974SPaul B Schroeder }
10513f542974SPaul B Schroeder
10523f542974SPaul B Schroeder /*****************************************************************************
10533f542974SPaul B Schroeder * mos7840_calc_baud_rate_divisor
10543f542974SPaul B Schroeder * this function calculates the proper baud rate divisor for the specified
10553f542974SPaul B Schroeder * baud rate.
10563f542974SPaul B Schroeder *****************************************************************************/
mos7840_calc_baud_rate_divisor(struct usb_serial_port * port,int baudRate,int * divisor,__u16 * clk_sel_val)10579c134a14SGreg Kroah-Hartman static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
10589c134a14SGreg Kroah-Hartman int baudRate, int *divisor,
10593f542974SPaul B Schroeder __u16 *clk_sel_val)
10603f542974SPaul B Schroeder {
10619c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
10623f542974SPaul B Schroeder
10633f542974SPaul B Schroeder if (baudRate <= 115200) {
10643f542974SPaul B Schroeder *divisor = 115200 / baudRate;
10653f542974SPaul B Schroeder *clk_sel_val = 0x0;
10663f542974SPaul B Schroeder }
10673f542974SPaul B Schroeder if ((baudRate > 115200) && (baudRate <= 230400)) {
10683f542974SPaul B Schroeder *divisor = 230400 / baudRate;
10693f542974SPaul B Schroeder *clk_sel_val = 0x10;
10703f542974SPaul B Schroeder } else if ((baudRate > 230400) && (baudRate <= 403200)) {
10713f542974SPaul B Schroeder *divisor = 403200 / baudRate;
10723f542974SPaul B Schroeder *clk_sel_val = 0x20;
10733f542974SPaul B Schroeder } else if ((baudRate > 403200) && (baudRate <= 460800)) {
10743f542974SPaul B Schroeder *divisor = 460800 / baudRate;
10753f542974SPaul B Schroeder *clk_sel_val = 0x30;
10763f542974SPaul B Schroeder } else if ((baudRate > 460800) && (baudRate <= 806400)) {
10773f542974SPaul B Schroeder *divisor = 806400 / baudRate;
10783f542974SPaul B Schroeder *clk_sel_val = 0x40;
10793f542974SPaul B Schroeder } else if ((baudRate > 806400) && (baudRate <= 921600)) {
10803f542974SPaul B Schroeder *divisor = 921600 / baudRate;
10813f542974SPaul B Schroeder *clk_sel_val = 0x50;
10823f542974SPaul B Schroeder } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
10833f542974SPaul B Schroeder *divisor = 1572864 / baudRate;
10843f542974SPaul B Schroeder *clk_sel_val = 0x60;
10853f542974SPaul B Schroeder } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
10863f542974SPaul B Schroeder *divisor = 3145728 / baudRate;
10873f542974SPaul B Schroeder *clk_sel_val = 0x70;
10883f542974SPaul B Schroeder }
10893f542974SPaul B Schroeder return 0;
10903f542974SPaul B Schroeder }
10913f542974SPaul B Schroeder
10923f542974SPaul B Schroeder /*****************************************************************************
10933f542974SPaul B Schroeder * mos7840_send_cmd_write_baud_rate
10943f542974SPaul B Schroeder * this function sends the proper command to change the baud rate of the
10953f542974SPaul B Schroeder * specified port.
10963f542974SPaul B Schroeder *****************************************************************************/
10973f542974SPaul B Schroeder
mos7840_send_cmd_write_baud_rate(struct moschip_port * mos7840_port,int baudRate)10983f542974SPaul B Schroeder static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
10993f542974SPaul B Schroeder int baudRate)
11003f542974SPaul B Schroeder {
1101ce039bd4SJohan Hovold struct usb_serial_port *port = mos7840_port->port;
11023f542974SPaul B Schroeder int divisor = 0;
11033f542974SPaul B Schroeder int status;
11043f542974SPaul B Schroeder __u16 Data;
11053f542974SPaul B Schroeder __u16 clk_sel_val;
11063f542974SPaul B Schroeder
11071143832eSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1108880af9dbSAlan Cox /* reset clk_uart_sel in spregOffset */
11093f542974SPaul B Schroeder if (baudRate > 115200) {
11103f542974SPaul B Schroeder #ifdef HW_flow_control
1111880af9dbSAlan Cox /* NOTE: need to see the pther register to modify */
1112880af9dbSAlan Cox /* setting h/w flow control bit to 1 */
11133f542974SPaul B Schroeder Data = 0x2b;
11143f542974SPaul B Schroeder mos7840_port->shadowMCR = Data;
1115880af9dbSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1116880af9dbSAlan Cox Data);
11173f542974SPaul B Schroeder if (status < 0) {
11189c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
11193f542974SPaul B Schroeder return -1;
11203f542974SPaul B Schroeder }
11213f542974SPaul B Schroeder #endif
11223f542974SPaul B Schroeder
11233f542974SPaul B Schroeder } else {
11243f542974SPaul B Schroeder #ifdef HW_flow_control
1125880af9dbSAlan Cox /* setting h/w flow control bit to 0 */
11263f542974SPaul B Schroeder Data = 0xb;
11273f542974SPaul B Schroeder mos7840_port->shadowMCR = Data;
1128880af9dbSAlan Cox status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1129880af9dbSAlan Cox Data);
11303f542974SPaul B Schroeder if (status < 0) {
11319c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
11323f542974SPaul B Schroeder return -1;
11333f542974SPaul B Schroeder }
11343f542974SPaul B Schroeder #endif
11353f542974SPaul B Schroeder
11363f542974SPaul B Schroeder }
11373f542974SPaul B Schroeder
1138880af9dbSAlan Cox if (1) { /* baudRate <= 115200) */
11393f542974SPaul B Schroeder clk_sel_val = 0x0;
11403f542974SPaul B Schroeder Data = 0x0;
11419c134a14SGreg Kroah-Hartman status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
11423f542974SPaul B Schroeder &clk_sel_val);
1143880af9dbSAlan Cox status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
11443f542974SPaul B Schroeder &Data);
11453f542974SPaul B Schroeder if (status < 0) {
11469c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
11473f542974SPaul B Schroeder return -1;
11483f542974SPaul B Schroeder }
11493f542974SPaul B Schroeder Data = (Data & 0x8f) | clk_sel_val;
1150880af9dbSAlan Cox status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1151880af9dbSAlan Cox Data);
11523f542974SPaul B Schroeder if (status < 0) {
11539c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
11543f542974SPaul B Schroeder return -1;
11553f542974SPaul B Schroeder }
11563f542974SPaul B Schroeder /* Calculate the Divisor */
11573f542974SPaul B Schroeder
11583f542974SPaul B Schroeder if (status) {
1159194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s - bad baud rate\n", __func__);
11603f542974SPaul B Schroeder return status;
11613f542974SPaul B Schroeder }
11623f542974SPaul B Schroeder /* Enable access to divisor latch */
11633f542974SPaul B Schroeder Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
11643f542974SPaul B Schroeder mos7840_port->shadowLCR = Data;
11653f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
11663f542974SPaul B Schroeder
11673f542974SPaul B Schroeder /* Write the divisor */
11683f542974SPaul B Schroeder Data = (unsigned char)(divisor & 0xff);
11699c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
11703f542974SPaul B Schroeder mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
11713f542974SPaul B Schroeder
11723f542974SPaul B Schroeder Data = (unsigned char)((divisor & 0xff00) >> 8);
11739c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
11743f542974SPaul B Schroeder mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
11753f542974SPaul B Schroeder
11763f542974SPaul B Schroeder /* Disable access to divisor latch */
11773f542974SPaul B Schroeder Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
11783f542974SPaul B Schroeder mos7840_port->shadowLCR = Data;
11793f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
11803f542974SPaul B Schroeder
11813f542974SPaul B Schroeder }
11823f542974SPaul B Schroeder return status;
11833f542974SPaul B Schroeder }
11843f542974SPaul B Schroeder
11853f542974SPaul B Schroeder /*****************************************************************************
11863f542974SPaul B Schroeder * mos7840_change_port_settings
11873f542974SPaul B Schroeder * This routine is called to set the UART on the device to match
11883f542974SPaul B Schroeder * the specified new settings.
11893f542974SPaul B Schroeder *****************************************************************************/
11903f542974SPaul B Schroeder
mos7840_change_port_settings(struct tty_struct * tty,struct moschip_port * mos7840_port,const struct ktermios * old_termios)119195da310eSAlan Cox static void mos7840_change_port_settings(struct tty_struct *tty,
1192f6d47fe5SIlpo Järvinen struct moschip_port *mos7840_port,
1193f6d47fe5SIlpo Järvinen const struct ktermios *old_termios)
11943f542974SPaul B Schroeder {
1195ce039bd4SJohan Hovold struct usb_serial_port *port = mos7840_port->port;
11963f542974SPaul B Schroeder int baud;
11973f542974SPaul B Schroeder unsigned cflag;
11983f542974SPaul B Schroeder __u8 lData;
11993f542974SPaul B Schroeder __u8 lParity;
12003f542974SPaul B Schroeder __u8 lStop;
12013f542974SPaul B Schroeder int status;
12023f542974SPaul B Schroeder __u16 Data;
12033f542974SPaul B Schroeder
12043f542974SPaul B Schroeder lData = LCR_BITS_8;
12053f542974SPaul B Schroeder lStop = LCR_STOP_1;
12063f542974SPaul B Schroeder lParity = LCR_PAR_NONE;
12073f542974SPaul B Schroeder
1208adc8d746SAlan Cox cflag = tty->termios.c_cflag;
12093f542974SPaul B Schroeder
12103f542974SPaul B Schroeder /* Change the number of bits */
12113f542974SPaul B Schroeder switch (cflag & CSIZE) {
12123f542974SPaul B Schroeder case CS5:
12133f542974SPaul B Schroeder lData = LCR_BITS_5;
12143f542974SPaul B Schroeder break;
12153f542974SPaul B Schroeder
12163f542974SPaul B Schroeder case CS6:
12173f542974SPaul B Schroeder lData = LCR_BITS_6;
12183f542974SPaul B Schroeder break;
12193f542974SPaul B Schroeder
12203f542974SPaul B Schroeder case CS7:
12213f542974SPaul B Schroeder lData = LCR_BITS_7;
12223f542974SPaul B Schroeder break;
122378692cc3SColin Leitner
12243f542974SPaul B Schroeder default:
12253f542974SPaul B Schroeder case CS8:
12263f542974SPaul B Schroeder lData = LCR_BITS_8;
12273f542974SPaul B Schroeder break;
12283f542974SPaul B Schroeder }
122978692cc3SColin Leitner
12303f542974SPaul B Schroeder /* Change the Parity bit */
12313f542974SPaul B Schroeder if (cflag & PARENB) {
12323f542974SPaul B Schroeder if (cflag & PARODD) {
12333f542974SPaul B Schroeder lParity = LCR_PAR_ODD;
12349c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
12353f542974SPaul B Schroeder } else {
12363f542974SPaul B Schroeder lParity = LCR_PAR_EVEN;
12379c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - parity = even\n", __func__);
12383f542974SPaul B Schroeder }
12393f542974SPaul B Schroeder
12403f542974SPaul B Schroeder } else {
12419c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - parity = none\n", __func__);
12423f542974SPaul B Schroeder }
12433f542974SPaul B Schroeder
1244880af9dbSAlan Cox if (cflag & CMSPAR)
12453f542974SPaul B Schroeder lParity = lParity | 0x20;
12463f542974SPaul B Schroeder
12473f542974SPaul B Schroeder /* Change the Stop bit */
12483f542974SPaul B Schroeder if (cflag & CSTOPB) {
12493f542974SPaul B Schroeder lStop = LCR_STOP_2;
12509c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
12513f542974SPaul B Schroeder } else {
12523f542974SPaul B Schroeder lStop = LCR_STOP_1;
12539c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
12543f542974SPaul B Schroeder }
12553f542974SPaul B Schroeder
12563f542974SPaul B Schroeder /* Update the LCR with the correct value */
12573f542974SPaul B Schroeder mos7840_port->shadowLCR &=
12583f542974SPaul B Schroeder ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
12593f542974SPaul B Schroeder mos7840_port->shadowLCR |= (lData | lParity | lStop);
12603f542974SPaul B Schroeder
12619c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
12623f542974SPaul B Schroeder mos7840_port->shadowLCR);
12633f542974SPaul B Schroeder /* Disable Interrupts */
12643f542974SPaul B Schroeder Data = 0x00;
12653f542974SPaul B Schroeder mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
12663f542974SPaul B Schroeder
12673f542974SPaul B Schroeder Data = 0x00;
12683f542974SPaul B Schroeder mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
12693f542974SPaul B Schroeder
12703f542974SPaul B Schroeder Data = 0xcf;
12713f542974SPaul B Schroeder mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
12723f542974SPaul B Schroeder
12733f542974SPaul B Schroeder /* Send the updated LCR value to the mos7840 */
12743f542974SPaul B Schroeder Data = mos7840_port->shadowLCR;
12753f542974SPaul B Schroeder
12763f542974SPaul B Schroeder mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
12773f542974SPaul B Schroeder
12783f542974SPaul B Schroeder Data = 0x00b;
12793f542974SPaul B Schroeder mos7840_port->shadowMCR = Data;
12803f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
12813f542974SPaul B Schroeder Data = 0x00b;
12823f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
12833f542974SPaul B Schroeder
12843f542974SPaul B Schroeder /* set up the MCR register and send it to the mos7840 */
12853f542974SPaul B Schroeder
12863f542974SPaul B Schroeder mos7840_port->shadowMCR = MCR_MASTER_IE;
1287880af9dbSAlan Cox if (cflag & CBAUD)
12883f542974SPaul B Schroeder mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
12893f542974SPaul B Schroeder
1290880af9dbSAlan Cox if (cflag & CRTSCTS)
12913f542974SPaul B Schroeder mos7840_port->shadowMCR |= (MCR_XON_ANY);
1292880af9dbSAlan Cox else
12933f542974SPaul B Schroeder mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
12943f542974SPaul B Schroeder
12953f542974SPaul B Schroeder Data = mos7840_port->shadowMCR;
12963f542974SPaul B Schroeder mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
12973f542974SPaul B Schroeder
12983f542974SPaul B Schroeder /* Determine divisor based on baud rate */
12993f542974SPaul B Schroeder baud = tty_get_baud_rate(tty);
13003f542974SPaul B Schroeder
13013f542974SPaul B Schroeder if (!baud) {
13023f542974SPaul B Schroeder /* pick a default, any default... */
13039c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s", "Picked default baud...\n");
13043f542974SPaul B Schroeder baud = 9600;
13053f542974SPaul B Schroeder }
13063f542974SPaul B Schroeder
13079c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
13083f542974SPaul B Schroeder status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
13093f542974SPaul B Schroeder
13103f542974SPaul B Schroeder /* Enable Interrupts */
13113f542974SPaul B Schroeder Data = 0x0c;
13123f542974SPaul B Schroeder mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
13133f542974SPaul B Schroeder
1314ce9d8562SMathieu OTHACEHE if (!mos7840_port->read_urb_busy) {
131550de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true;
13169d380190SJohan Hovold status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
13173f542974SPaul B Schroeder if (status) {
13189c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
13193f542974SPaul B Schroeder status);
132050de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false;
13213f542974SPaul B Schroeder }
13223f542974SPaul B Schroeder }
13239c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
13243f542974SPaul B Schroeder mos7840_port->shadowLCR);
13253f542974SPaul B Schroeder }
13263f542974SPaul B Schroeder
13273f542974SPaul B Schroeder /*****************************************************************************
13283f542974SPaul B Schroeder * mos7840_set_termios
13293f542974SPaul B Schroeder * this function is called by the tty driver when it wants to change
13303f542974SPaul B Schroeder * the termios structure
13313f542974SPaul B Schroeder *****************************************************************************/
13323f542974SPaul B Schroeder
mos7840_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)133395da310eSAlan Cox static void mos7840_set_termios(struct tty_struct *tty,
133495da310eSAlan Cox struct usb_serial_port *port,
1335f6d47fe5SIlpo Järvinen const struct ktermios *old_termios)
13363f542974SPaul B Schroeder {
13377b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
13383f542974SPaul B Schroeder int status;
13393f542974SPaul B Schroeder
13403f542974SPaul B Schroeder /* change the port settings to the new ones specified */
13413f542974SPaul B Schroeder
134295da310eSAlan Cox mos7840_change_port_settings(tty, mos7840_port, old_termios);
13433f542974SPaul B Schroeder
1344ce9d8562SMathieu OTHACEHE if (!mos7840_port->read_urb_busy) {
134550de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = true;
13469d380190SJohan Hovold status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
13473f542974SPaul B Schroeder if (status) {
13489c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
13493f542974SPaul B Schroeder status);
135050de36f7SGreg Kroah-Hartman mos7840_port->read_urb_busy = false;
13513f542974SPaul B Schroeder }
13523f542974SPaul B Schroeder }
13533f542974SPaul B Schroeder }
13543f542974SPaul B Schroeder
13553f542974SPaul B Schroeder /*****************************************************************************
13563f542974SPaul B Schroeder * mos7840_get_lsr_info - get line status register info
13573f542974SPaul B Schroeder *
13583f542974SPaul B Schroeder * Purpose: Let user call ioctl() to get info when the UART physically
13593f542974SPaul B Schroeder * is emptied. On bus types like RS485, the transmitter must
13603f542974SPaul B Schroeder * release the bus after transmitting. This must be done when
13613f542974SPaul B Schroeder * the transmit shift register is empty, not be done when the
13623f542974SPaul B Schroeder * transmit holding register is empty. This functionality
13633f542974SPaul B Schroeder * allows an RS485 driver to be written in user space.
13643f542974SPaul B Schroeder *****************************************************************************/
13653f542974SPaul B Schroeder
mos7840_get_lsr_info(struct tty_struct * tty,unsigned int __user * value)136695da310eSAlan Cox static int mos7840_get_lsr_info(struct tty_struct *tty,
136797c4965dSAl Viro unsigned int __user *value)
13683f542974SPaul B Schroeder {
13693f542974SPaul B Schroeder int count;
13703f542974SPaul B Schroeder unsigned int result = 0;
13713f542974SPaul B Schroeder
137295da310eSAlan Cox count = mos7840_chars_in_buffer(tty);
13739c134a14SGreg Kroah-Hartman if (count == 0)
13743f542974SPaul B Schroeder result = TIOCSER_TEMT;
13753f542974SPaul B Schroeder
13763f542974SPaul B Schroeder if (copy_to_user(value, &result, sizeof(int)))
13773f542974SPaul B Schroeder return -EFAULT;
13783f542974SPaul B Schroeder return 0;
13793f542974SPaul B Schroeder }
13803f542974SPaul B Schroeder
13813f542974SPaul B Schroeder /*****************************************************************************
13823f542974SPaul B Schroeder * SerialIoctl
13833f542974SPaul B Schroeder * this function handles any ioctl calls to the driver
13843f542974SPaul B Schroeder *****************************************************************************/
13853f542974SPaul B Schroeder
mos7840_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)138600a0d0d6SAlan Cox static int mos7840_ioctl(struct tty_struct *tty,
13873f542974SPaul B Schroeder unsigned int cmd, unsigned long arg)
13883f542974SPaul B Schroeder {
138995da310eSAlan Cox struct usb_serial_port *port = tty->driver_data;
139097c4965dSAl Viro void __user *argp = (void __user *)arg;
13913f542974SPaul B Schroeder
13923f542974SPaul B Schroeder switch (cmd) {
13933f542974SPaul B Schroeder /* return number of bytes available */
13943f542974SPaul B Schroeder
13953f542974SPaul B Schroeder case TIOCSERGETLSR:
13969c134a14SGreg Kroah-Hartman dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
139795da310eSAlan Cox return mos7840_get_lsr_info(tty, argp);
13983f542974SPaul B Schroeder
13993f542974SPaul B Schroeder default:
14003f542974SPaul B Schroeder break;
14013f542974SPaul B Schroeder }
14023f542974SPaul B Schroeder return -ENOIOCTLCMD;
14033f542974SPaul B Schroeder }
14043f542974SPaul B Schroeder
14051c333550SJohan Hovold /*
14061c333550SJohan Hovold * Check if GPO (pin 42) is connected to GPI (pin 33) as recommended by ASIX
14071c333550SJohan Hovold * for MCS7810 by bit-banging a 16-bit word.
14081c333550SJohan Hovold *
14091c333550SJohan Hovold * Note that GPO is really RTS of the third port so this will toggle RTS of
14101c333550SJohan Hovold * port two or three on two- and four-port devices.
14111c333550SJohan Hovold */
mos7810_check(struct usb_serial * serial)14120eafe4deSDonald static int mos7810_check(struct usb_serial *serial)
14133f542974SPaul B Schroeder {
14140eafe4deSDonald int i, pass_count = 0;
141515ee89c3SJohan Hovold u8 *buf;
14160eafe4deSDonald __u16 data = 0, mcr_data = 0;
14170eafe4deSDonald __u16 test_pattern = 0x55AA;
141815ee89c3SJohan Hovold int res;
141915ee89c3SJohan Hovold
142015ee89c3SJohan Hovold buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
142115ee89c3SJohan Hovold if (!buf)
142215ee89c3SJohan Hovold return 0; /* failed to identify 7810 */
14233f542974SPaul B Schroeder
14240eafe4deSDonald /* Store MCR setting */
142515ee89c3SJohan Hovold res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
14260eafe4deSDonald MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
142715ee89c3SJohan Hovold buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
142815ee89c3SJohan Hovold if (res == VENDOR_READ_LENGTH)
142915ee89c3SJohan Hovold mcr_data = *buf;
14300eafe4deSDonald
14310eafe4deSDonald for (i = 0; i < 16; i++) {
14320eafe4deSDonald /* Send the 1-bit test pattern out to MCS7810 test pin */
14330eafe4deSDonald usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
14340eafe4deSDonald MCS_WRREQ, MCS_WR_RTYPE,
14350eafe4deSDonald (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
14360eafe4deSDonald MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
14370eafe4deSDonald
14380eafe4deSDonald /* Read the test pattern back */
143915ee89c3SJohan Hovold res = usb_control_msg(serial->dev,
144015ee89c3SJohan Hovold usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
144115ee89c3SJohan Hovold MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
1442093ea2d3SDonald Lee VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
144315ee89c3SJohan Hovold if (res == VENDOR_READ_LENGTH)
144415ee89c3SJohan Hovold data = *buf;
1445093ea2d3SDonald Lee
14460eafe4deSDonald /* If this is a MCS7810 device, both test patterns must match */
14470eafe4deSDonald if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
14480eafe4deSDonald break;
14490eafe4deSDonald
14500eafe4deSDonald pass_count++;
14513f542974SPaul B Schroeder }
1452093ea2d3SDonald Lee
14530eafe4deSDonald /* Restore MCR setting */
14540eafe4deSDonald usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
14550eafe4deSDonald MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
14560eafe4deSDonald 0, MOS_WDR_TIMEOUT);
14570eafe4deSDonald
145815ee89c3SJohan Hovold kfree(buf);
145915ee89c3SJohan Hovold
14600eafe4deSDonald if (pass_count == 16)
14610eafe4deSDonald return 1;
14620eafe4deSDonald
14630eafe4deSDonald return 0;
14640eafe4deSDonald }
14650eafe4deSDonald
mos7840_probe(struct usb_serial * serial,const struct usb_device_id * id)146640c24f28SJohan Hovold static int mos7840_probe(struct usb_serial *serial,
146740c24f28SJohan Hovold const struct usb_device_id *id)
14680eafe4deSDonald {
1469375cb533SJohan Hovold unsigned long device_flags = id->driver_info;
147015ee89c3SJohan Hovold u8 *buf;
147140c24f28SJohan Hovold
1472375cb533SJohan Hovold /* Skip device-type detection if we already have device flags. */
1473375cb533SJohan Hovold if (device_flags)
147440c24f28SJohan Hovold goto out;
1475e696d00eSPavel Löbl
147615ee89c3SJohan Hovold buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
147740c24f28SJohan Hovold if (!buf)
147840c24f28SJohan Hovold return -ENOMEM;
147940c24f28SJohan Hovold
14800eafe4deSDonald usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
148115ee89c3SJohan Hovold MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
14820eafe4deSDonald VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
14830eafe4deSDonald
14840eafe4deSDonald /* For a MCS7840 device GPIO0 must be set to 1 */
148540c24f28SJohan Hovold if (buf[0] & 0x01)
1486375cb533SJohan Hovold device_flags = MCS_PORTS(4);
14870eafe4deSDonald else if (mos7810_check(serial))
1488375cb533SJohan Hovold device_flags = MCS_PORTS(1) | MCS_LED;
14890eafe4deSDonald else
1490375cb533SJohan Hovold device_flags = MCS_PORTS(2);
149140c24f28SJohan Hovold
149240c24f28SJohan Hovold kfree(buf);
149340c24f28SJohan Hovold out:
1494375cb533SJohan Hovold usb_set_serial_data(serial, (void *)device_flags);
149540c24f28SJohan Hovold
149640c24f28SJohan Hovold return 0;
14970eafe4deSDonald }
14980eafe4deSDonald
mos7840_calc_num_ports(struct usb_serial * serial,struct usb_serial_endpoints * epds)149907814246SJohan Hovold static int mos7840_calc_num_ports(struct usb_serial *serial,
150007814246SJohan Hovold struct usb_serial_endpoints *epds)
150140c24f28SJohan Hovold {
1502375cb533SJohan Hovold unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
1503375cb533SJohan Hovold int num_ports = MCS_PORTS(device_flags);
150440c24f28SJohan Hovold
1505375cb533SJohan Hovold if (num_ports == 0 || num_ports > 4)
150695254020SJohan Hovold return -ENODEV;
15073f542974SPaul B Schroeder
150895254020SJohan Hovold if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
15095c75633eSJohan Hovold dev_err(&serial->interface->dev, "missing endpoints\n");
15105c75633eSJohan Hovold return -ENODEV;
15115c75633eSJohan Hovold }
15125c75633eSJohan Hovold
151395254020SJohan Hovold return num_ports;
15145c75633eSJohan Hovold }
15155c75633eSJohan Hovold
mos7840_attach(struct usb_serial * serial)1516960fbd1cSJohan Hovold static int mos7840_attach(struct usb_serial *serial)
1517960fbd1cSJohan Hovold {
1518960fbd1cSJohan Hovold struct device *dev = &serial->interface->dev;
1519960fbd1cSJohan Hovold int status;
1520960fbd1cSJohan Hovold u16 val;
1521960fbd1cSJohan Hovold
1522960fbd1cSJohan Hovold /* Zero Length flag enable */
1523960fbd1cSJohan Hovold val = 0x0f;
1524960fbd1cSJohan Hovold status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, val);
1525960fbd1cSJohan Hovold if (status < 0)
1526960fbd1cSJohan Hovold dev_dbg(dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
1527960fbd1cSJohan Hovold else
1528960fbd1cSJohan Hovold dev_dbg(dev, "ZLP_REG5 Writing success status%d\n", status);
1529960fbd1cSJohan Hovold
1530960fbd1cSJohan Hovold return status;
1531960fbd1cSJohan Hovold }
1532960fbd1cSJohan Hovold
mos7840_port_probe(struct usb_serial_port * port)153380c00750SJohan Hovold static int mos7840_port_probe(struct usb_serial_port *port)
15343f542974SPaul B Schroeder {
153580c00750SJohan Hovold struct usb_serial *serial = port->serial;
1536375cb533SJohan Hovold unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
15373f542974SPaul B Schroeder struct moschip_port *mos7840_port;
153880c00750SJohan Hovold int status;
153980c00750SJohan Hovold int pnum;
15403f542974SPaul B Schroeder __u16 Data;
15413f542974SPaul B Schroeder
15423f542974SPaul B Schroeder /* we set up the pointers to the endpoints in the mos7840_open *
15433f542974SPaul B Schroeder * function, as the structures aren't created yet. */
15443f542974SPaul B Schroeder
15451143832eSGreg Kroah-Hartman pnum = port->port_number;
154680c00750SJohan Hovold
1547ae685effSJohan Hovold dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
15487ac9da10SBurman Yan mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
154910c642d0SJohan Hovold if (!mos7840_port)
155080c00750SJohan Hovold return -ENOMEM;
15513f542974SPaul B Schroeder
1552880af9dbSAlan Cox /* Initialize all port interrupt end point to port 0 int
1553880af9dbSAlan Cox * endpoint. Our device has only one interrupt end point
1554880af9dbSAlan Cox * common to all port */
15553f542974SPaul B Schroeder
155680c00750SJohan Hovold mos7840_port->port = port;
15570de9a702SOliver Neukum spin_lock_init(&mos7840_port->pool_lock);
15583f542974SPaul B Schroeder
155937768adfSTony Cook /* minor is not initialised until later by
156037768adfSTony Cook * usb-serial.c:get_free_serial() and cannot therefore be used
156137768adfSTony Cook * to index device instances */
156280c00750SJohan Hovold mos7840_port->port_num = pnum + 1;
1563e5b1e206SGreg Kroah-Hartman dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
156480c00750SJohan Hovold dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
15653f542974SPaul B Schroeder
15663f542974SPaul B Schroeder if (mos7840_port->port_num == 1) {
15673f542974SPaul B Schroeder mos7840_port->SpRegOffset = 0x0;
15683f542974SPaul B Schroeder mos7840_port->ControlRegOffset = 0x1;
15693f542974SPaul B Schroeder mos7840_port->DcrRegOffset = 0x4;
1570e8603076SJackyChou } else {
1571e8603076SJackyChou u8 phy_num = mos7840_port->port_num;
1572e8603076SJackyChou
1573e8603076SJackyChou /* Port 2 in the 2-port case uses registers of port 3 */
1574e8603076SJackyChou if (serial->num_ports == 2)
1575e8603076SJackyChou phy_num = 3;
1576e8603076SJackyChou
1577e8603076SJackyChou mos7840_port->SpRegOffset = 0x8 + 2 * (phy_num - 2);
1578e8603076SJackyChou mos7840_port->ControlRegOffset = 0x9 + 2 * (phy_num - 2);
1579e8603076SJackyChou mos7840_port->DcrRegOffset = 0x16 + 3 * (phy_num - 2);
15803f542974SPaul B Schroeder }
158180c00750SJohan Hovold mos7840_dump_serial_port(port, mos7840_port);
15827b2faedeSJohan Hovold usb_set_serial_port_data(port, mos7840_port);
15833f542974SPaul B Schroeder
1584880af9dbSAlan Cox /* enable rx_disable bit in control register */
158580c00750SJohan Hovold status = mos7840_get_reg_sync(port,
15863f542974SPaul B Schroeder mos7840_port->ControlRegOffset, &Data);
15873f542974SPaul B Schroeder if (status < 0) {
158880c00750SJohan Hovold dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
1589960fbd1cSJohan Hovold goto error;
15903f542974SPaul B Schroeder } else
159180c00750SJohan Hovold dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
1592880af9dbSAlan Cox Data |= 0x08; /* setting driver done bit */
1593880af9dbSAlan Cox Data |= 0x04; /* sp1_bit to have cts change reflect in
1594880af9dbSAlan Cox modem status reg */
15953f542974SPaul B Schroeder
1596880af9dbSAlan Cox /* Data |= 0x20; //rx_disable bit */
159780c00750SJohan Hovold status = mos7840_set_reg_sync(port,
15983f542974SPaul B Schroeder mos7840_port->ControlRegOffset, Data);
15993f542974SPaul B Schroeder if (status < 0) {
160080c00750SJohan Hovold dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
1601960fbd1cSJohan Hovold goto error;
16023f542974SPaul B Schroeder } else
160380c00750SJohan Hovold dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
16043f542974SPaul B Schroeder
1605880af9dbSAlan Cox /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
1606880af9dbSAlan Cox and 0x24 in DCR3 */
16073f542974SPaul B Schroeder Data = 0x01;
160880c00750SJohan Hovold status = mos7840_set_reg_sync(port,
1609880af9dbSAlan Cox (__u16) (mos7840_port->DcrRegOffset + 0), Data);
16103f542974SPaul B Schroeder if (status < 0) {
161180c00750SJohan Hovold dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
1612960fbd1cSJohan Hovold goto error;
16133f542974SPaul B Schroeder } else
161480c00750SJohan Hovold dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
16153f542974SPaul B Schroeder
16163f542974SPaul B Schroeder Data = 0x05;
161780c00750SJohan Hovold status = mos7840_set_reg_sync(port,
1618880af9dbSAlan Cox (__u16) (mos7840_port->DcrRegOffset + 1), Data);
16193f542974SPaul B Schroeder if (status < 0) {
162080c00750SJohan Hovold dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
1621960fbd1cSJohan Hovold goto error;
16223f542974SPaul B Schroeder } else
162380c00750SJohan Hovold dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
16243f542974SPaul B Schroeder
16253f542974SPaul B Schroeder Data = 0x24;
162680c00750SJohan Hovold status = mos7840_set_reg_sync(port,
1627880af9dbSAlan Cox (__u16) (mos7840_port->DcrRegOffset + 2), Data);
16283f542974SPaul B Schroeder if (status < 0) {
162980c00750SJohan Hovold dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
1630960fbd1cSJohan Hovold goto error;
16313f542974SPaul B Schroeder } else
163280c00750SJohan Hovold dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
16333f542974SPaul B Schroeder
1634880af9dbSAlan Cox /* write values in clkstart0x0 and clkmulti 0x20 */
16353f542974SPaul B Schroeder Data = 0x0;
1636ae685effSJohan Hovold status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
16373f542974SPaul B Schroeder if (status < 0) {
163880c00750SJohan Hovold dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
1639960fbd1cSJohan Hovold goto error;
16403f542974SPaul B Schroeder } else
164180c00750SJohan Hovold dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
16423f542974SPaul B Schroeder
16433f542974SPaul B Schroeder Data = 0x20;
1644ae685effSJohan Hovold status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
16453f542974SPaul B Schroeder if (status < 0) {
164680c00750SJohan Hovold dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
16470de9a702SOliver Neukum goto error;
16483f542974SPaul B Schroeder } else
164980c00750SJohan Hovold dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
16503f542974SPaul B Schroeder
1651880af9dbSAlan Cox /* write value 0x0 to scratchpad register */
16523f542974SPaul B Schroeder Data = 0x00;
1653ae685effSJohan Hovold status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
16543f542974SPaul B Schroeder if (status < 0) {
165580c00750SJohan Hovold dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
1656960fbd1cSJohan Hovold goto error;
16573f542974SPaul B Schroeder } else
165880c00750SJohan Hovold dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
16593f542974SPaul B Schroeder
1660880af9dbSAlan Cox /* Zero Length flag register */
1661ae685effSJohan Hovold if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
16623f542974SPaul B Schroeder Data = 0xff;
166380c00750SJohan Hovold status = mos7840_set_reg_sync(port,
16643f542974SPaul B Schroeder (__u16) (ZLP_REG1 +
1665880af9dbSAlan Cox ((__u16)mos7840_port->port_num)), Data);
166680c00750SJohan Hovold dev_dbg(&port->dev, "ZLIP offset %x\n",
16679c134a14SGreg Kroah-Hartman (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
16683f542974SPaul B Schroeder if (status < 0) {
166980c00750SJohan Hovold dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
1670960fbd1cSJohan Hovold goto error;
16713f542974SPaul B Schroeder } else
167280c00750SJohan Hovold dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
16733f542974SPaul B Schroeder } else {
16743f542974SPaul B Schroeder Data = 0xff;
167580c00750SJohan Hovold status = mos7840_set_reg_sync(port,
16763f542974SPaul B Schroeder (__u16) (ZLP_REG1 +
1677880af9dbSAlan Cox ((__u16)mos7840_port->port_num) - 0x1), Data);
167880c00750SJohan Hovold dev_dbg(&port->dev, "ZLIP offset %x\n",
16799c134a14SGreg Kroah-Hartman (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
16803f542974SPaul B Schroeder if (status < 0) {
168180c00750SJohan Hovold dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
1682960fbd1cSJohan Hovold goto error;
16833f542974SPaul B Schroeder } else
168480c00750SJohan Hovold dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
16853f542974SPaul B Schroeder
16863f542974SPaul B Schroeder }
16870eafe4deSDonald
1688375cb533SJohan Hovold mos7840_port->has_led = device_flags & MCS_LED;
16890eafe4deSDonald
16900eafe4deSDonald /* Initialize LED timers */
1691375cb533SJohan Hovold if (mos7840_port->has_led) {
169205cf0decSJohan Hovold mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
169305cf0decSJohan Hovold mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
169405cf0decSJohan Hovold GFP_KERNEL);
169505cf0decSJohan Hovold if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
169605cf0decSJohan Hovold status = -ENOMEM;
169705cf0decSJohan Hovold goto error;
169805cf0decSJohan Hovold }
169905cf0decSJohan Hovold
1700e99e88a9SKees Cook timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
17010eafe4deSDonald mos7840_port->led_timer1.expires =
17020eafe4deSDonald jiffies + msecs_to_jiffies(LED_ON_MS);
1703e99e88a9SKees Cook timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
1704e99e88a9SKees Cook 0);
17050eafe4deSDonald mos7840_port->led_timer2.expires =
17060eafe4deSDonald jiffies + msecs_to_jiffies(LED_OFF_MS);
17070eafe4deSDonald
17080eafe4deSDonald /* Turn off LED */
1709ae685effSJohan Hovold mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
17100eafe4deSDonald }
1711960fbd1cSJohan Hovold
17123f542974SPaul B Schroeder return 0;
17130de9a702SOliver Neukum error:
171405cf0decSJohan Hovold kfree(mos7840_port->led_dr);
171505cf0decSJohan Hovold usb_free_urb(mos7840_port->led_urb);
17160de9a702SOliver Neukum kfree(mos7840_port);
171780c00750SJohan Hovold
17180de9a702SOliver Neukum return status;
17193f542974SPaul B Schroeder }
17203f542974SPaul B Schroeder
mos7840_port_remove(struct usb_serial_port * port)1721c5d1448fSUwe Kleine-König static void mos7840_port_remove(struct usb_serial_port *port)
17223f542974SPaul B Schroeder {
17237b2faedeSJohan Hovold struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
17243f542974SPaul B Schroeder
17250eafe4deSDonald if (mos7840_port->has_led) {
17260eafe4deSDonald /* Turn off LED */
172780c00750SJohan Hovold mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
17280eafe4deSDonald
1729292a089dSSteven Rostedt (Google) timer_shutdown_sync(&mos7840_port->led_timer1);
1730292a089dSSteven Rostedt (Google) timer_shutdown_sync(&mos7840_port->led_timer2);
173105cf0decSJohan Hovold
173205cf0decSJohan Hovold usb_kill_urb(mos7840_port->led_urb);
173305cf0decSJohan Hovold usb_free_urb(mos7840_port->led_urb);
173405cf0decSJohan Hovold kfree(mos7840_port->led_dr);
17350eafe4deSDonald }
173671831921SJohan Hovold
17373f542974SPaul B Schroeder kfree(mos7840_port);
17383f542974SPaul B Schroeder }
17393f542974SPaul B Schroeder
mos7840_suspend(struct usb_serial * serial,pm_message_t message)1740*5ae6a64fSDmitry Smirnov static int mos7840_suspend(struct usb_serial *serial, pm_message_t message)
1741*5ae6a64fSDmitry Smirnov {
1742*5ae6a64fSDmitry Smirnov struct moschip_port *mos7840_port;
1743*5ae6a64fSDmitry Smirnov struct usb_serial_port *port;
1744*5ae6a64fSDmitry Smirnov int i;
1745*5ae6a64fSDmitry Smirnov
1746*5ae6a64fSDmitry Smirnov for (i = 0; i < serial->num_ports; ++i) {
1747*5ae6a64fSDmitry Smirnov port = serial->port[i];
1748*5ae6a64fSDmitry Smirnov if (!tty_port_initialized(&port->port))
1749*5ae6a64fSDmitry Smirnov continue;
1750*5ae6a64fSDmitry Smirnov
1751*5ae6a64fSDmitry Smirnov mos7840_port = usb_get_serial_port_data(port);
1752*5ae6a64fSDmitry Smirnov
1753*5ae6a64fSDmitry Smirnov usb_kill_urb(mos7840_port->read_urb);
1754*5ae6a64fSDmitry Smirnov mos7840_port->read_urb_busy = false;
1755*5ae6a64fSDmitry Smirnov }
1756*5ae6a64fSDmitry Smirnov
1757*5ae6a64fSDmitry Smirnov return 0;
1758*5ae6a64fSDmitry Smirnov }
1759*5ae6a64fSDmitry Smirnov
mos7840_resume(struct usb_serial * serial)1760*5ae6a64fSDmitry Smirnov static int mos7840_resume(struct usb_serial *serial)
1761*5ae6a64fSDmitry Smirnov {
1762*5ae6a64fSDmitry Smirnov struct moschip_port *mos7840_port;
1763*5ae6a64fSDmitry Smirnov struct usb_serial_port *port;
1764*5ae6a64fSDmitry Smirnov int res;
1765*5ae6a64fSDmitry Smirnov int i;
1766*5ae6a64fSDmitry Smirnov
1767*5ae6a64fSDmitry Smirnov for (i = 0; i < serial->num_ports; ++i) {
1768*5ae6a64fSDmitry Smirnov port = serial->port[i];
1769*5ae6a64fSDmitry Smirnov if (!tty_port_initialized(&port->port))
1770*5ae6a64fSDmitry Smirnov continue;
1771*5ae6a64fSDmitry Smirnov
1772*5ae6a64fSDmitry Smirnov mos7840_port = usb_get_serial_port_data(port);
1773*5ae6a64fSDmitry Smirnov
1774*5ae6a64fSDmitry Smirnov mos7840_port->read_urb_busy = true;
1775*5ae6a64fSDmitry Smirnov res = usb_submit_urb(mos7840_port->read_urb, GFP_NOIO);
1776*5ae6a64fSDmitry Smirnov if (res)
1777*5ae6a64fSDmitry Smirnov mos7840_port->read_urb_busy = false;
1778*5ae6a64fSDmitry Smirnov }
1779*5ae6a64fSDmitry Smirnov
1780*5ae6a64fSDmitry Smirnov return 0;
1781*5ae6a64fSDmitry Smirnov }
1782*5ae6a64fSDmitry Smirnov
17833f542974SPaul B Schroeder static struct usb_serial_driver moschip7840_4port_device = {
17843f542974SPaul B Schroeder .driver = {
17853f542974SPaul B Schroeder .owner = THIS_MODULE,
17863f542974SPaul B Schroeder .name = "mos7840",
17873f542974SPaul B Schroeder },
17883f542974SPaul B Schroeder .description = DRIVER_DESC,
178968e24113SGreg Kroah-Hartman .id_table = id_table,
179095254020SJohan Hovold .num_interrupt_in = 1,
17913f542974SPaul B Schroeder .open = mos7840_open,
17923f542974SPaul B Schroeder .close = mos7840_close,
17933f542974SPaul B Schroeder .write = mos7840_write,
17943f542974SPaul B Schroeder .write_room = mos7840_write_room,
17953f542974SPaul B Schroeder .chars_in_buffer = mos7840_chars_in_buffer,
17963f542974SPaul B Schroeder .throttle = mos7840_throttle,
17973f542974SPaul B Schroeder .unthrottle = mos7840_unthrottle,
17983f542974SPaul B Schroeder .calc_num_ports = mos7840_calc_num_ports,
179940c24f28SJohan Hovold .probe = mos7840_probe,
1800960fbd1cSJohan Hovold .attach = mos7840_attach,
18013f542974SPaul B Schroeder .ioctl = mos7840_ioctl,
18023f542974SPaul B Schroeder .set_termios = mos7840_set_termios,
18033f542974SPaul B Schroeder .break_ctl = mos7840_break,
18043f542974SPaul B Schroeder .tiocmget = mos7840_tiocmget,
18053f542974SPaul B Schroeder .tiocmset = mos7840_tiocmset,
18068c1a07ffSJohan Hovold .get_icount = usb_serial_generic_get_icount,
180780c00750SJohan Hovold .port_probe = mos7840_port_probe,
180880c00750SJohan Hovold .port_remove = mos7840_port_remove,
18093f542974SPaul B Schroeder .read_bulk_callback = mos7840_bulk_in_callback,
1810*5ae6a64fSDmitry Smirnov .suspend = mos7840_suspend,
1811*5ae6a64fSDmitry Smirnov .resume = mos7840_resume,
18123f542974SPaul B Schroeder };
18133f542974SPaul B Schroeder
18144d2a7affSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
18154d2a7affSAlan Stern &moschip7840_4port_device, NULL
18164d2a7affSAlan Stern };
18174d2a7affSAlan Stern
181868e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
18193f542974SPaul B Schroeder
18203f542974SPaul B Schroeder MODULE_DESCRIPTION(DRIVER_DESC);
18213f542974SPaul B Schroeder MODULE_LICENSE("GPL");
1822