blob: d9086ee16663e080479663ed3173a4d857ae0c5e [file] [log] [blame]
Paul B Schroeder3f542972006-08-31 19:41:47 -05001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * Clean ups from Moschip version and a few ioctl implementations by:
17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 *
23 */
24
25#include <linux/kernel.h>
26#include <linux/errno.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/tty.h>
30#include <linux/tty_driver.h>
31#include <linux/tty_flip.h>
32#include <linux/module.h>
33#include <linux/serial.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
Alan Cox880af9d2008-07-22 11:16:12 +010036#include <linux/uaccess.h>
Paul B Schroeder3f542972006-08-31 19:41:47 -050037
38/*
39 * Version Information
40 */
Tony Cook37768ad2009-04-18 22:42:18 +093041#define DRIVER_VERSION "1.3.2"
Paul B Schroeder3f542972006-08-31 19:41:47 -050042#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
43
44/*
45 * 16C50 UART register defines
46 */
47
48#define LCR_BITS_5 0x00 /* 5 bits/char */
49#define LCR_BITS_6 0x01 /* 6 bits/char */
50#define LCR_BITS_7 0x02 /* 7 bits/char */
51#define LCR_BITS_8 0x03 /* 8 bits/char */
52#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
53
54#define LCR_STOP_1 0x00 /* 1 stop bit */
55#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
56#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
57#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
58
59#define LCR_PAR_NONE 0x00 /* No parity */
60#define LCR_PAR_ODD 0x08 /* Odd parity */
61#define LCR_PAR_EVEN 0x18 /* Even parity */
62#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
63#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
64#define LCR_PAR_MASK 0x38 /* Mask for parity field */
65
66#define LCR_SET_BREAK 0x40 /* Set Break condition */
67#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
68
69#define MCR_DTR 0x01 /* Assert DTR */
70#define MCR_RTS 0x02 /* Assert RTS */
71#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
72#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
73#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
74#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
75
76#define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
77#define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
78#define MOS7840_MSR_RI 0x40 /* Current state of RI */
79#define MOS7840_MSR_CD 0x80 /* Current state of CD */
80
81/*
82 * Defines used for sending commands to port
83 */
84
Alan Cox880af9d2008-07-22 11:16:12 +010085#define WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
86#define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
Paul B Schroeder3f542972006-08-31 19:41:47 -050087
88#define MOS_PORT1 0x0200
89#define MOS_PORT2 0x0300
90#define MOS_VENREG 0x0000
91#define MOS_MAX_PORT 0x02
92#define MOS_WRITE 0x0E
93#define MOS_READ 0x0D
94
95/* Requests */
96#define MCS_RD_RTYPE 0xC0
97#define MCS_WR_RTYPE 0x40
98#define MCS_RDREQ 0x0D
99#define MCS_WRREQ 0x0E
100#define MCS_CTRL_TIMEOUT 500
101#define VENDOR_READ_LENGTH (0x01)
102
103#define MAX_NAME_LEN 64
104
Alan Cox880af9d2008-07-22 11:16:12 +0100105#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
106#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500107
108/* For higher baud Rates use TIOCEXBAUD */
109#define TIOCEXBAUD 0x5462
110
111/* vendor id and device id defines */
112
David Ludlow11e1abb2008-02-25 17:30:52 -0500113/* The native mos7840/7820 component */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500114#define USB_VENDOR_ID_MOSCHIP 0x9710
115#define MOSCHIP_DEVICE_ID_7840 0x7840
116#define MOSCHIP_DEVICE_ID_7820 0x7820
Donald0eafe4d2012-04-19 15:00:45 +0800117#define MOSCHIP_DEVICE_ID_7810 0x7810
David Ludlow11e1abb2008-02-25 17:30:52 -0500118/* The native component can have its vendor/device id's overridden
119 * in vendor-specific implementations. Such devices can be handled
120 * by making a change here, in moschip_port_id_table, and in
121 * moschip_id_table_combined
122 */
Dave Ludlow870408c2010-09-01 12:33:30 -0400123#define USB_VENDOR_ID_BANDB 0x0856
124#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
125#define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
126#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
127#define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
128#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
129#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
130#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
131#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
132#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
133#define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
134#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
135#define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
136#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
Paul B Schroeder3f542972006-08-31 19:41:47 -0500137
Russell Lang9d498be2009-07-17 19:29:20 +1000138/* This driver also supports
139 * ATEN UC2324 device using Moschip MCS7840
140 * ATEN UC2322 device using Moschip MCS7820
141 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930142#define USB_VENDOR_ID_ATENINTL 0x0557
143#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000144#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930145
David Ludlow11e1abb2008-02-25 17:30:52 -0500146/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500147
148#define SERIAL_IIR_RLS 0x06
149#define SERIAL_IIR_MS 0x00
150
151/*
152 * Emulation of the bit mask on the LINE STATUS REGISTER.
153 */
154#define SERIAL_LSR_DR 0x0001
155#define SERIAL_LSR_OE 0x0002
156#define SERIAL_LSR_PE 0x0004
157#define SERIAL_LSR_FE 0x0008
158#define SERIAL_LSR_BI 0x0010
159
160#define MOS_MSR_DELTA_CTS 0x10
161#define MOS_MSR_DELTA_DSR 0x20
162#define MOS_MSR_DELTA_RI 0x40
163#define MOS_MSR_DELTA_CD 0x80
164
Alan Cox880af9d2008-07-22 11:16:12 +0100165/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500166#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
167#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
168#define LINE_CONTROL_REGISTER ((__u16)(0x03))
169#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
170#define LINE_STATUS_REGISTER ((__u16)(0x05))
171#define MODEM_STATUS_REGISTER ((__u16)(0x06))
172#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
173#define DIVISOR_LATCH_LSB ((__u16)(0x00))
174#define DIVISOR_LATCH_MSB ((__u16)(0x01))
175
176#define CLK_MULTI_REGISTER ((__u16)(0x02))
177#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
Donald Lee093ea2d2012-03-14 15:26:33 +0800178#define GPIO_REGISTER ((__u16)(0x07))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500179
180#define SERIAL_LCR_DLAB ((__u16)(0x0080))
181
182/*
183 * URB POOL related defines
184 */
185#define NUM_URBS 16 /* URB Count */
186#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
187
Donald0eafe4d2012-04-19 15:00:45 +0800188/* LED on/off milliseconds*/
189#define LED_ON_MS 500
190#define LED_OFF_MS 500
191
192static int device_type;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500193
Németh Márton7d40d7e2010-01-10 15:34:24 +0100194static const struct usb_device_id moschip_port_id_table[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500195 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
196 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Donald0eafe4d2012-04-19 15:00:45 +0800197 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400209 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800210 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930211 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000212 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500213 {} /* terminating entry */
214};
215
Németh Márton7d40d7e2010-01-10 15:34:24 +0100216static const struct usb_device_id moschip_id_table_combined[] __devinitconst = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500217 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
218 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Donald0eafe4d2012-04-19 15:00:45 +0800219 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500220 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400221 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500222 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400223 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500224 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
225 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
226 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
227 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500228 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400229 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500230 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400231 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800232 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930233 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000234 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500235 {} /* terminating entry */
236};
237
238MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
239
240/* This structure holds all of the local port information */
241
242struct moschip_port {
243 int port_num; /*Actual port number in the device(1,2,etc) */
244 struct urb *write_urb; /* write URB for this port */
245 struct urb *read_urb; /* read URB for this port */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100246 struct urb *int_urb;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500247 __u8 shadowLCR; /* last LCR value received */
248 __u8 shadowMCR; /* last MCR value received */
249 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100250 char open_ports;
251 char zombie;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500252 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
253 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
254 int delta_msr_cond;
255 struct async_icount icount;
256 struct usb_serial_port *port; /* loop back to the owner of this object */
257
Alan Cox880af9d2008-07-22 11:16:12 +0100258 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500259 __u8 SpRegOffset;
260 __u8 ControlRegOffset;
261 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100262 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500263 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100264 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500265 char *ctrl_buf;
266 int MsrLsr;
267
Oliver Neukum0de9a702007-03-16 20:28:28 +0100268 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500269 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100270 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800271 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500272
Donald0eafe4d2012-04-19 15:00:45 +0800273 /* For device(s) with LED indicator */
274 bool has_led;
275 bool led_flag;
276 struct timer_list led_timer1; /* Timer for LED on */
277 struct timer_list led_timer2; /* Timer for LED off */
278};
Paul B Schroeder3f542972006-08-31 19:41:47 -0500279
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030280static bool debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500281
282/*
283 * mos7840_set_reg_sync
284 * To set the Control register by calling usb_fill_control_urb function
285 * by passing usb_sndctrlpipe function as parameter.
286 */
287
288static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
289 __u16 val)
290{
291 struct usb_device *dev = port->serial->dev;
292 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930293 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500294
295 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
296 MCS_WR_RTYPE, val, reg, NULL, 0,
297 MOS_WDR_TIMEOUT);
298}
299
300/*
301 * mos7840_get_reg_sync
302 * To set the Uart register by calling usb_fill_control_urb function by
303 * passing usb_rcvctrlpipe function as parameter.
304 */
305
306static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100307 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500308{
309 struct usb_device *dev = port->serial->dev;
310 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100311 u8 *buf;
312
313 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
314 if (!buf)
315 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500316
317 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100318 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500319 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100320 *val = buf[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930321 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100322
323 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500324 return ret;
325}
326
327/*
328 * mos7840_set_uart_reg
329 * To set the Uart register by calling usb_fill_control_urb function by
330 * passing usb_sndctrlpipe function as parameter.
331 */
332
333static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
334 __u16 val)
335{
336
337 struct usb_device *dev = port->serial->dev;
338 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100339 /* For the UART control registers, the application number need
340 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100341 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100342 val |= (((__u16) port->number -
343 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930344 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500345 } else {
346 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100347 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500348 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930349 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500350 val);
351 } else {
352 val |=
353 (((__u16) port->number -
354 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930355 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500356 val);
357 }
358 }
359 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
360 MCS_WR_RTYPE, val, reg, NULL, 0,
361 MOS_WDR_TIMEOUT);
362
363}
364
365/*
366 * mos7840_get_uart_reg
367 * To set the Control register by calling usb_fill_control_urb function
368 * by passing usb_rcvctrlpipe function as parameter.
369 */
370static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100371 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500372{
373 struct usb_device *dev = port->serial->dev;
374 int ret = 0;
375 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100376 u8 *buf;
377
378 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
379 if (!buf)
380 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500381
Tony Cook84fe6e72009-04-18 22:55:06 +0930382 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100383 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
384 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100385 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500386 Wval =
387 (((__u16) port->number - (__u16) (port->serial->minor)) +
388 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930389 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500390 } else {
391 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100392 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500393 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930394 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500395 Wval);
396 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100397 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500398 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930399 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500400 Wval);
401 }
402 }
403 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100404 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500405 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100406 *val = buf[0];
407
408 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500409 return ret;
410}
411
412static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
413{
414
Tony Cook84fe6e72009-04-18 22:55:06 +0930415 dbg("***************************************");
416 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
417 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
418 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
419 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500420
421}
422
423/************************************************************************/
424/************************************************************************/
425/* I N T E R F A C E F U N C T I O N S */
426/* I N T E R F A C E F U N C T I O N S */
427/************************************************************************/
428/************************************************************************/
429
430static inline void mos7840_set_port_private(struct usb_serial_port *port,
431 struct moschip_port *data)
432{
433 usb_set_serial_port_data(port, (void *)data);
434}
435
436static inline struct moschip_port *mos7840_get_port_private(struct
437 usb_serial_port
438 *port)
439{
440 return (struct moschip_port *)usb_get_serial_port_data(port);
441}
442
Oliver Neukum0de9a702007-03-16 20:28:28 +0100443static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500444{
445 struct moschip_port *mos7840_port;
446 struct async_icount *icount;
447 mos7840_port = port;
448 icount = &mos7840_port->icount;
449 if (new_msr &
450 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
451 MOS_MSR_DELTA_CD)) {
452 icount = &mos7840_port->icount;
453
454 /* update input line counters */
455 if (new_msr & MOS_MSR_DELTA_CTS) {
456 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100457 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500458 }
459 if (new_msr & MOS_MSR_DELTA_DSR) {
460 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100461 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500462 }
463 if (new_msr & MOS_MSR_DELTA_CD) {
464 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100465 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500466 }
467 if (new_msr & MOS_MSR_DELTA_RI) {
468 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100469 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500470 }
471 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500472}
473
Oliver Neukum0de9a702007-03-16 20:28:28 +0100474static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500475{
476 struct async_icount *icount;
477
Harvey Harrison441b62c2008-03-03 16:08:34 -0800478 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500479
480 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100481 /*
482 * Parity and Framing errors only count if they
483 * occur exclusive of a break being
484 * received.
485 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500486 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
487 }
488
489 /* update input line counters */
490 icount = &port->icount;
491 if (new_lsr & SERIAL_LSR_BI) {
492 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100493 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500494 }
495 if (new_lsr & SERIAL_LSR_OE) {
496 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100497 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500498 }
499 if (new_lsr & SERIAL_LSR_PE) {
500 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100501 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500502 }
503 if (new_lsr & SERIAL_LSR_FE) {
504 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100505 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500506 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500507}
508
509/************************************************************************/
510/************************************************************************/
511/* U S B C A L L B A C K F U N C T I O N S */
512/* U S B C A L L B A C K F U N C T I O N S */
513/************************************************************************/
514/************************************************************************/
515
David Howells7d12e782006-10-05 14:55:46 +0100516static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500517{
518 unsigned char *data;
519 struct moschip_port *mos7840_port;
520 __u8 regval = 0x0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100521 int result = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700522 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500523
Ming Leicdc97792008-02-24 18:41:47 +0800524 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100525
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700526 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500527 case 0:
528 /* success */
529 break;
530 case -ECONNRESET:
531 case -ENOENT:
532 case -ESHUTDOWN:
533 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800534 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700535 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500536 return;
537 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800538 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700539 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500540 goto exit;
541 }
542
Tony Cook84fe6e72009-04-18 22:55:06 +0930543 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
544 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500545 mos7840_port->MsrLsr, mos7840_port->port_num);
546 data = urb->transfer_buffer;
547 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930548 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500549 if (mos7840_port->MsrLsr == 0)
550 mos7840_handle_new_msr(mos7840_port, regval);
551 else if (mos7840_port->MsrLsr == 1)
552 mos7840_handle_new_lsr(mos7840_port, regval);
553
Oliver Neukum0de9a702007-03-16 20:28:28 +0100554exit:
555 spin_lock(&mos7840_port->pool_lock);
556 if (!mos7840_port->zombie)
557 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
558 spin_unlock(&mos7840_port->pool_lock);
559 if (result) {
560 dev_err(&urb->dev->dev,
561 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800562 __func__, result);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100563 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500564}
565
566static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100567 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500568{
569 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100570 struct usb_ctrlrequest *dr = mcs->dr;
571 unsigned char *buffer = mcs->ctrl_buf;
572 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500573
Paul B Schroeder3f542972006-08-31 19:41:47 -0500574 dr->bRequestType = MCS_RD_RTYPE;
575 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100576 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500577 dr->wIndex = cpu_to_le16(reg);
578 dr->wLength = cpu_to_le16(2);
579
580 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
581 (unsigned char *)dr, buffer, 2,
582 mos7840_control_callback, mcs);
583 mcs->control_urb->transfer_buffer_length = 2;
584 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
585 return ret;
586}
587
Donald0eafe4d2012-04-19 15:00:45 +0800588static void mos7840_set_led_callback(struct urb *urb)
589{
590 switch (urb->status) {
591 case 0:
592 /* Success */
593 break;
594 case -ECONNRESET:
595 case -ENOENT:
596 case -ESHUTDOWN:
597 /* This urb is terminated, clean up */
598 dbg("%s - urb shutting down with status: %d", __func__,
599 urb->status);
600 break;
601 default:
602 dbg("%s - nonzero urb status received: %d", __func__,
603 urb->status);
604 }
605}
606
607static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
608 __u16 reg)
609{
610 struct usb_device *dev = mcs->port->serial->dev;
611 struct usb_ctrlrequest *dr = mcs->dr;
612
613 dr->bRequestType = MCS_WR_RTYPE;
614 dr->bRequest = MCS_WRREQ;
615 dr->wValue = cpu_to_le16(wval);
616 dr->wIndex = cpu_to_le16(reg);
617 dr->wLength = cpu_to_le16(0);
618
619 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
620 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
621
622 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
623}
624
625static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
626 __u16 val)
627{
628 struct usb_device *dev = port->serial->dev;
629
630 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
631 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
632}
633
634static void mos7840_led_off(unsigned long arg)
635{
636 struct moschip_port *mcs = (struct moschip_port *) arg;
637
638 /* Turn off LED */
639 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
640 mod_timer(&mcs->led_timer2,
641 jiffies + msecs_to_jiffies(LED_OFF_MS));
642}
643
644static void mos7840_led_flag_off(unsigned long arg)
645{
646 struct moschip_port *mcs = (struct moschip_port *) arg;
647
648 mcs->led_flag = false;
649}
650
Paul B Schroeder3f542972006-08-31 19:41:47 -0500651/*****************************************************************************
652 * mos7840_interrupt_callback
653 * this is the callback function for when we have received data on the
654 * interrupt endpoint.
655 *****************************************************************************/
656
David Howells7d12e782006-10-05 14:55:46 +0100657static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500658{
659 int result;
660 int length;
661 struct moschip_port *mos7840_port;
662 struct usb_serial *serial;
663 __u16 Data;
664 unsigned char *data;
665 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100666 int i, rv = 0;
667 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700668 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500669
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700670 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500671 case 0:
672 /* success */
673 break;
674 case -ECONNRESET:
675 case -ENOENT:
676 case -ESHUTDOWN:
677 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800678 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700679 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500680 return;
681 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800682 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700683 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500684 goto exit;
685 }
686
687 length = urb->actual_length;
688 data = urb->transfer_buffer;
689
Ming Leicdc97792008-02-24 18:41:47 +0800690 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500691
692 /* Moschip get 5 bytes
693 * Byte 1 IIR Port 1 (port.number is 0)
694 * Byte 2 IIR Port 2 (port.number is 1)
695 * Byte 3 IIR Port 3 (port.number is 2)
696 * Byte 4 IIR Port 4 (port.number is 3)
697 * Byte 5 FIFO status for both */
698
699 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930700 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500701 return;
702 }
703
704 sp[0] = (__u8) data[0];
705 sp[1] = (__u8) data[1];
706 sp[2] = (__u8) data[2];
707 sp[3] = (__u8) data[3];
708 st = (__u8) data[4];
709
710 for (i = 0; i < serial->num_ports; i++) {
711 mos7840_port = mos7840_get_port_private(serial->port[i]);
712 wval =
713 (((__u16) serial->port[i]->number -
714 (__u16) (serial->minor)) + 1) << 8;
715 if (mos7840_port->open) {
716 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930717 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500718 } else {
719 switch (sp[i] & 0x0f) {
720 case SERIAL_IIR_RLS:
721 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930722 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500723 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100724 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500725 break;
726 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930727 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500728 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100729 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500730 break;
731 }
Oliver Neukum0de9a702007-03-16 20:28:28 +0100732 spin_lock(&mos7840_port->pool_lock);
733 if (!mos7840_port->zombie) {
734 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
735 } else {
736 spin_unlock(&mos7840_port->pool_lock);
737 return;
738 }
739 spin_unlock(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500740 }
741 }
742 }
Alan Cox880af9d2008-07-22 11:16:12 +0100743 if (!(rv < 0))
744 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100745 return;
746exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500747 result = usb_submit_urb(urb, GFP_ATOMIC);
748 if (result) {
749 dev_err(&urb->dev->dev,
750 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800751 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500752 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500753}
754
755static int mos7840_port_paranoia_check(struct usb_serial_port *port,
756 const char *function)
757{
758 if (!port) {
759 dbg("%s - port == NULL", function);
760 return -1;
761 }
762 if (!port->serial) {
763 dbg("%s - port->serial == NULL", function);
764 return -1;
765 }
766
767 return 0;
768}
769
770/* Inline functions to check the sanity of a pointer that is passed to us */
771static int mos7840_serial_paranoia_check(struct usb_serial *serial,
772 const char *function)
773{
774 if (!serial) {
775 dbg("%s - serial == NULL", function);
776 return -1;
777 }
778 if (!serial->type) {
779 dbg("%s - serial->type == NULL!", function);
780 return -1;
781 }
782
783 return 0;
784}
785
786static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
787 const char *function)
788{
789 /* if no port was specified, or it fails a paranoia check */
790 if (!port ||
791 mos7840_port_paranoia_check(port, function) ||
792 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100793 /* then say that we don't have a valid usb_serial thing,
794 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500795 return NULL;
796 }
797
798 return port->serial;
799}
800
801/*****************************************************************************
802 * mos7840_bulk_in_callback
803 * this is the callback function for when we have received data on the
804 * bulk in endpoint.
805 *****************************************************************************/
806
David Howells7d12e782006-10-05 14:55:46 +0100807static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500808{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700809 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500810 unsigned char *data;
811 struct usb_serial *serial;
812 struct usb_serial_port *port;
813 struct moschip_port *mos7840_port;
814 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700815 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500816
Ming Leicdc97792008-02-24 18:41:47 +0800817 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500818 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930819 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800820 return;
821 }
822
823 if (status) {
824 dbg("nonzero read bulk status received: %d", status);
825 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500826 return;
827 }
828
829 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800830 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930831 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800832 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500833 return;
834 }
835
Harvey Harrison441b62c2008-03-03 16:08:34 -0800836 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500837 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930838 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800839 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500840 return;
841 }
842
Paul B Schroeder3f542972006-08-31 19:41:47 -0500843 data = urb->transfer_buffer;
844
Paul B Schroeder3f542972006-08-31 19:41:47 -0500845 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100846 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500847 if (tty) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500848 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930849 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500850 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100851 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500852 }
853 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100854 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930855 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500856 mos7840_port->icount.rx);
857 }
858
859 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930860 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800861 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500862 return;
863 }
864
Donald0eafe4d2012-04-19 15:00:45 +0800865 /* Turn on LED */
866 if (mos7840_port->has_led && !mos7840_port->led_flag) {
867 mos7840_port->led_flag = true;
868 mos7840_set_led_async(mos7840_port, 0x0301,
869 MODEM_CONTROL_REGISTER);
870 mod_timer(&mos7840_port->led_timer1,
871 jiffies + msecs_to_jiffies(LED_ON_MS));
872 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500873
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800874 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700875 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100876
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700877 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800878 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
879 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500880 }
881}
882
883/*****************************************************************************
884 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100885 * this is the callback function for when we have finished sending
886 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500887 *****************************************************************************/
888
David Howells7d12e782006-10-05 14:55:46 +0100889static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500890{
891 struct moschip_port *mos7840_port;
892 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700893 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100894 int i;
895
Ming Leicdc97792008-02-24 18:41:47 +0800896 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100897 spin_lock(&mos7840_port->pool_lock);
898 for (i = 0; i < NUM_URBS; i++) {
899 if (urb == mos7840_port->write_urb_pool[i]) {
900 mos7840_port->busy[i] = 0;
901 break;
902 }
903 }
904 spin_unlock(&mos7840_port->pool_lock);
905
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700906 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930907 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500908 return;
909 }
910
Harvey Harrison441b62c2008-03-03 16:08:34 -0800911 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930912 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500913 return;
914 }
915
Alan Cox4a90f092008-10-13 10:39:46 +0100916 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800917 if (tty && mos7840_port->open)
918 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100919 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500920
921}
922
923/************************************************************************/
924/* 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 */
925/************************************************************************/
926#ifdef MCSSerialProbe
927static int mos7840_serial_probe(struct usb_serial *serial,
928 const struct usb_device_id *id)
929{
930
931 /*need to implement the mode_reg reading and updating\
932 structures usb_serial_ device_type\
933 (i.e num_ports, num_bulkin,bulkout etc) */
934 /* Also we can update the changes attach */
935 return 1;
936}
937#endif
938
939/*****************************************************************************
940 * mos7840_open
941 * this function is called by the tty driver when a port is opened
942 * If successful, we return 0
943 * Otherwise we return a negative error number.
944 *****************************************************************************/
945
Alan Coxa509a7e2009-09-19 13:13:26 -0700946static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500947{
948 int response;
949 int j;
950 struct usb_serial *serial;
951 struct urb *urb;
952 __u16 Data;
953 int status;
954 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100955 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500956
Harvey Harrison441b62c2008-03-03 16:08:34 -0800957 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930958 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500959 return -ENODEV;
960 }
961
Paul B Schroeder3f542972006-08-31 19:41:47 -0500962 serial = port->serial;
963
Harvey Harrison441b62c2008-03-03 16:08:34 -0800964 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930965 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500966 return -ENODEV;
967 }
968
969 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100970 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500971
Oliver Neukum0de9a702007-03-16 20:28:28 +0100972 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500973 return -ENODEV;
974
975 usb_clear_halt(serial->dev, port->write_urb->pipe);
976 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100977 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500978
979 /* Initialising the write urb pool */
980 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100981 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500982 mos7840_port->write_urb_pool[j] = urb;
983
984 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700985 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500986 continue;
987 }
988
Alan Cox880af9d2008-07-22 11:16:12 +0100989 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
990 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500991 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100992 usb_free_urb(urb);
993 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700994 dev_err(&port->dev,
995 "%s-out of memory for urb buffers.\n",
996 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500997 continue;
998 }
999 }
1000
1001/*****************************************************************************
1002 * Initialize MCS7840 -- Write Init values to corresponding Registers
1003 *
1004 * Register Index
1005 * 1 : IER
1006 * 2 : FCR
1007 * 3 : LCR
1008 * 4 : MCR
1009 *
1010 * 0x08 : SP1/2 Control Reg
1011 *****************************************************************************/
1012
Alan Cox880af9d2008-07-22 11:16:12 +01001013 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001014
Paul B Schroeder3f542972006-08-31 19:41:47 -05001015 Data = 0x0;
1016 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1017 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301018 dbg("Reading Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001019 return -1;
1020 }
1021 Data |= 0x80;
1022 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1023 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301024 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001025 return -1;
1026 }
1027
1028 Data &= ~0x80;
1029 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1030 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301031 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001032 return -1;
1033 }
Alan Cox880af9d2008-07-22 11:16:12 +01001034 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001035
Paul B Schroeder3f542972006-08-31 19:41:47 -05001036 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001037 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1038 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001039 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301040 dbg("Reading Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001041 return -1;
1042 }
Alan Cox880af9d2008-07-22 11:16:12 +01001043 Data |= 0x08; /* Driver done bit */
1044 Data |= 0x20; /* rx_disable */
1045 status = mos7840_set_reg_sync(port,
1046 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001047 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301048 dbg("writing Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001049 return -1;
1050 }
Alan Cox880af9d2008-07-22 11:16:12 +01001051 /* do register settings here */
1052 /* Set all regs to the device default values. */
1053 /***********************************
1054 * First Disable all interrupts.
1055 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -05001056 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001057 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1058 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301059 dbg("disabling interrupts failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001060 return -1;
1061 }
Alan Cox880af9d2008-07-22 11:16:12 +01001062 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001063 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001064 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1065 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301066 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001067 return -1;
1068 }
1069
1070 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001071 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1072 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301073 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001074 return -1;
1075 }
1076
1077 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001078 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1079 mos7840_port->shadowLCR = Data;
1080
1081 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001082 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1083 mos7840_port->shadowMCR = Data;
1084
1085 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001086 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1087 mos7840_port->shadowLCR = Data;
1088
Alan Cox880af9d2008-07-22 11:16:12 +01001089 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001090 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1091
1092 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001093 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1094
1095 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001096 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1097
1098 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001099 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1100
1101 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001102 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1103 mos7840_port->shadowLCR = Data;
1104
Alan Cox880af9d2008-07-22 11:16:12 +01001105 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001106 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001107 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1108
1109 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001110 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1111
1112 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001113 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001114 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001115 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001116 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1117
Alan Cox880af9d2008-07-22 11:16:12 +01001118 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001119 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001120 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1121 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001122 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001123 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1124 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001125
Alan Cox880af9d2008-07-22 11:16:12 +01001126 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001127 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001128 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1129 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001130 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001131 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1132 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001133
Alan Cox880af9d2008-07-22 11:16:12 +01001134 /* Check to see if we've set up our endpoint info yet *
1135 * (can't set it up in mos7840_startup as the structures *
1136 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001137 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001138 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001139 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001140 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001141 serial->dev,
1142 usb_rcvintpipe(serial->dev,
1143 serial->port[0]->interrupt_in_endpointAddress),
1144 serial->port[0]->interrupt_in_buffer,
1145 serial->port[0]->interrupt_in_urb->
1146 transfer_buffer_length,
1147 mos7840_interrupt_callback,
1148 serial,
1149 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001150
1151 /* start interrupt read for mos7840 *
1152 * will continue as long as mos7840 is connected */
1153
1154 response =
1155 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1156 GFP_KERNEL);
1157 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001158 dev_err(&port->dev, "%s - Error %d submitting "
1159 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001160 }
1161
1162 }
1163
1164 }
1165
1166 /* see if we've set up our endpoint info yet *
1167 * (can't set it up in mos7840_startup as the *
1168 * structures were not set up at that time.) */
1169
Tony Cook84fe6e72009-04-18 22:55:06 +09301170 dbg("port number is %d", port->number);
1171 dbg("serial number is %d", port->serial->minor);
1172 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1173 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1174 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1175 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001176 mos7840_port->read_urb = port->read_urb;
1177
1178 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001179 if ((serial->num_ports == 2)
1180 && ((((__u16)port->number -
1181 (__u16)(port->serial->minor)) % 2) != 0)) {
1182 usb_fill_bulk_urb(mos7840_port->read_urb,
1183 serial->dev,
1184 usb_rcvbulkpipe(serial->dev,
1185 (port->bulk_in_endpointAddress) + 2),
1186 port->bulk_in_buffer,
1187 mos7840_port->read_urb->transfer_buffer_length,
1188 mos7840_bulk_in_callback, mos7840_port);
1189 } else {
1190 usb_fill_bulk_urb(mos7840_port->read_urb,
1191 serial->dev,
1192 usb_rcvbulkpipe(serial->dev,
1193 port->bulk_in_endpointAddress),
1194 port->bulk_in_buffer,
1195 mos7840_port->read_urb->transfer_buffer_length,
1196 mos7840_bulk_in_callback, mos7840_port);
1197 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001198
Tony Cook84fe6e72009-04-18 22:55:06 +09301199 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001200 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001201 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001202 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1203 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001204 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1205 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001206 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001207 }
1208
1209 /* initialize our wait queues */
1210 init_waitqueue_head(&mos7840_port->wait_chase);
1211 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1212
1213 /* initialize our icount structure */
1214 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1215
1216 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001217 /* Must set to enable ints! */
1218 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001219 /* send a open port command */
1220 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001221 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001222 mos7840_port->icount.tx = 0;
1223 mos7840_port->icount.rx = 0;
1224
Tony Cook84fe6e72009-04-18 22:55:06 +09301225 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001226 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001227
1228 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001229}
1230
1231/*****************************************************************************
1232 * mos7840_chars_in_buffer
1233 * this function is called by the tty driver when it wants to know how many
1234 * bytes of data we currently have outstanding in the port (data that has
1235 * been written, but hasn't made it out the port yet)
1236 * If successful, we return the number of bytes left to be written in the
1237 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001238 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001239 *****************************************************************************/
1240
Alan Cox95da3102008-07-22 11:09:07 +01001241static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001242{
Alan Cox95da3102008-07-22 11:09:07 +01001243 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001244 int i;
1245 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001246 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001247 struct moschip_port *mos7840_port;
1248
Harvey Harrison441b62c2008-03-03 16:08:34 -08001249 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301250 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001251 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001252 }
1253
1254 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001255 if (mos7840_port == NULL)
Alan Cox95da3102008-07-22 11:09:07 +01001256 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001257
Alan Cox880af9d2008-07-22 11:16:12 +01001258 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +01001259 for (i = 0; i < NUM_URBS; ++i)
1260 if (mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001261 chars += URB_TRANSFER_BUFFER_SIZE;
Alan Cox880af9d2008-07-22 11:16:12 +01001262 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001263 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001264 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001265
1266}
1267
Paul B Schroeder3f542972006-08-31 19:41:47 -05001268/*****************************************************************************
1269 * mos7840_close
1270 * this function is called by the tty driver when a port is closed
1271 *****************************************************************************/
1272
Alan Cox335f8512009-06-11 12:26:29 +01001273static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001274{
1275 struct usb_serial *serial;
1276 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001277 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001278 int j;
1279 __u16 Data;
1280
Harvey Harrison441b62c2008-03-03 16:08:34 -08001281 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301282 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001283 return;
1284 }
1285
Harvey Harrison441b62c2008-03-03 16:08:34 -08001286 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001287 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301288 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001289 return;
1290 }
1291
1292 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001293 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001294
Oliver Neukum0de9a702007-03-16 20:28:28 +01001295 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001296 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001297
1298 for (j = 0; j < NUM_URBS; ++j)
1299 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1300
1301 /* Freeing Write URBs */
1302 for (j = 0; j < NUM_URBS; ++j) {
1303 if (mos7840_port->write_urb_pool[j]) {
1304 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1305 kfree(mos7840_port->write_urb_pool[j]->
1306 transfer_buffer);
1307
1308 usb_free_urb(mos7840_port->write_urb_pool[j]);
1309 }
1310 }
1311
Paul B Schroeder3f542972006-08-31 19:41:47 -05001312 /* While closing port, shutdown all bulk read, write *
1313 * and interrupt read if they exists */
1314 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001315 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301316 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001317 usb_kill_urb(mos7840_port->write_urb);
1318 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001319 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301320 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001321 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001322 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001323 }
1324 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301325 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001326 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001327 }
1328 }
Alan Cox880af9d2008-07-22 11:16:12 +01001329/* if(mos7840_port->ctrl_buf != NULL) */
1330/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001331 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301332 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001333 port0->open_ports, port->number);
1334 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001335 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301336 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001337 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001338 }
1339 }
1340
1341 if (mos7840_port->write_urb) {
1342 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001343 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001344 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001345 usb_free_urb(mos7840_port->write_urb);
1346 }
1347
1348 Data = 0x0;
1349 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1350
1351 Data = 0x00;
1352 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1353
1354 mos7840_port->open = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001355}
1356
1357/************************************************************************
1358 *
1359 * mos7840_block_until_chase_response
1360 *
1361 * This function will block the close until one of the following:
1362 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001363 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001364 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1365 *
1366 ************************************************************************/
1367
Alan Cox95da3102008-07-22 11:09:07 +01001368static void mos7840_block_until_chase_response(struct tty_struct *tty,
1369 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001370{
1371 int timeout = 1 * HZ;
1372 int wait = 10;
1373 int count;
1374
1375 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001376 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001377
1378 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001379 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001380 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001381
1382 /* Block the thread for a while */
1383 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1384 timeout);
1385 /* No activity.. count down section */
1386 wait--;
1387 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001388 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001389 return;
1390 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001391 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001392 wait = 10;
1393 }
1394 }
1395
1396}
1397
1398/*****************************************************************************
1399 * mos7840_break
1400 * this function sends a break to the port
1401 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001402static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001403{
Alan Cox95da3102008-07-22 11:09:07 +01001404 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001405 unsigned char data;
1406 struct usb_serial *serial;
1407 struct moschip_port *mos7840_port;
1408
Harvey Harrison441b62c2008-03-03 16:08:34 -08001409 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301410 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001411 return;
1412 }
1413
Harvey Harrison441b62c2008-03-03 16:08:34 -08001414 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001415 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301416 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001417 return;
1418 }
1419
1420 mos7840_port = mos7840_get_port_private(port);
1421
Alan Cox880af9d2008-07-22 11:16:12 +01001422 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001423 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001424
Alan Cox95da3102008-07-22 11:09:07 +01001425 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001426 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001427 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001428
Alan Cox95da3102008-07-22 11:09:07 +01001429 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001430 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001431 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001432 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001433
Alan Cox6b447f042009-01-02 13:48:56 +00001434 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001435 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301436 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001437 mos7840_port->shadowLCR);
1438 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1439 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001440}
1441
1442/*****************************************************************************
1443 * mos7840_write_room
1444 * this function is called by the tty driver when it wants to know how many
1445 * bytes of data we can accept for a specific port.
1446 * If successful, we return the amount of room that we have for this port
1447 * Otherwise we return a negative error number.
1448 *****************************************************************************/
1449
Alan Cox95da3102008-07-22 11:09:07 +01001450static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001451{
Alan Cox95da3102008-07-22 11:09:07 +01001452 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001453 int i;
1454 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001455 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001456 struct moschip_port *mos7840_port;
1457
Harvey Harrison441b62c2008-03-03 16:08:34 -08001458 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301459 dbg("%s", "Invalid port");
1460 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001461 return -1;
1462 }
1463
1464 mos7840_port = mos7840_get_port_private(port);
1465 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301466 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001467 return -1;
1468 }
1469
Oliver Neukum0de9a702007-03-16 20:28:28 +01001470 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001471 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001472 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001473 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001474 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001475 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001476
Oliver Neukum0de9a702007-03-16 20:28:28 +01001477 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001478 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001479 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001480
1481}
1482
1483/*****************************************************************************
1484 * mos7840_write
1485 * this function is called by the tty driver when data should be written to
1486 * the port.
1487 * If successful, we return the number of bytes written, otherwise we
1488 * return a negative error number.
1489 *****************************************************************************/
1490
Alan Cox95da3102008-07-22 11:09:07 +01001491static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001492 const unsigned char *data, int count)
1493{
1494 int status;
1495 int i;
1496 int bytes_sent = 0;
1497 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001498 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001499
1500 struct moschip_port *mos7840_port;
1501 struct usb_serial *serial;
1502 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001503 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001504 const unsigned char *current_position = data;
1505 unsigned char *data1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001506
1507#ifdef NOTMOS7840
1508 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001509 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1510 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301511 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1512 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001513 mos7840_port->shadowLCR);
1514
Alan Cox880af9d2008-07-22 11:16:12 +01001515 /* Data = 0x03; */
1516 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1517 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001518
Alan Cox880af9d2008-07-22 11:16:12 +01001519 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001520 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1521
Alan Cox880af9d2008-07-22 11:16:12 +01001522 /* Data = 0x0c; */
1523 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001524 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001525 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301526 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001527
1528 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001529 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301530 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001531
1532 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301533 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001534 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001535 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1536#endif
1537
Harvey Harrison441b62c2008-03-03 16:08:34 -08001538 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301539 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001540 return -1;
1541 }
1542
1543 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001544 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301545 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001546 return -1;
1547 }
1548
1549 mos7840_port = mos7840_get_port_private(port);
1550 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301551 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001552 return -1;
1553 }
1554
1555 /* try to find a free urb in the list */
1556 urb = NULL;
1557
Oliver Neukum0de9a702007-03-16 20:28:28 +01001558 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001559 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001560 if (!mos7840_port->busy[i]) {
1561 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001562 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301563 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001564 break;
1565 }
1566 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001567 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001568
1569 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001570 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001571 goto exit;
1572 }
1573
1574 if (urb->transfer_buffer == NULL) {
1575 urb->transfer_buffer =
1576 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1577
1578 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001579 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001580 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001581 goto exit;
1582 }
1583 }
1584 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1585
Al Viro97c49652006-10-09 20:29:03 +01001586 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001587
1588 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001589 if ((serial->num_ports == 2)
1590 && ((((__u16)port->number -
1591 (__u16)(port->serial->minor)) % 2) != 0)) {
1592 usb_fill_bulk_urb(urb,
1593 serial->dev,
1594 usb_sndbulkpipe(serial->dev,
1595 (port->bulk_out_endpointAddress) + 2),
1596 urb->transfer_buffer,
1597 transfer_size,
1598 mos7840_bulk_out_data_callback, mos7840_port);
1599 } else {
1600 usb_fill_bulk_urb(urb,
1601 serial->dev,
1602 usb_sndbulkpipe(serial->dev,
1603 port->bulk_out_endpointAddress),
1604 urb->transfer_buffer,
1605 transfer_size,
1606 mos7840_bulk_out_data_callback, mos7840_port);
1607 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001608
1609 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301610 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001611
Donald0eafe4d2012-04-19 15:00:45 +08001612 /* Turn on LED */
1613 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1614 mos7840_port->led_flag = true;
1615 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1616 mod_timer(&mos7840_port->led_timer1,
1617 jiffies + msecs_to_jiffies(LED_ON_MS));
1618 }
1619
Paul B Schroeder3f542972006-08-31 19:41:47 -05001620 /* send it down the pipe */
1621 status = usb_submit_urb(urb, GFP_ATOMIC);
1622
1623 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001624 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001625 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001626 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001627 bytes_sent = status;
1628 goto exit;
1629 }
1630 bytes_sent = transfer_size;
1631 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001632 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301633 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001634exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001635 return bytes_sent;
1636
1637}
1638
1639/*****************************************************************************
1640 * mos7840_throttle
1641 * this function is called by the tty driver when it wants to stop the data
1642 * being read from the port.
1643 *****************************************************************************/
1644
Alan Cox95da3102008-07-22 11:09:07 +01001645static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001646{
Alan Cox95da3102008-07-22 11:09:07 +01001647 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001648 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001649 int status;
1650
Harvey Harrison441b62c2008-03-03 16:08:34 -08001651 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301652 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001653 return;
1654 }
1655
Tony Cook84fe6e72009-04-18 22:55:06 +09301656 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001657
1658 mos7840_port = mos7840_get_port_private(port);
1659
1660 if (mos7840_port == NULL)
1661 return;
1662
1663 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301664 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001665 return;
1666 }
1667
Paul B Schroeder3f542972006-08-31 19:41:47 -05001668 /* if we are implementing XON/XOFF, send the stop character */
1669 if (I_IXOFF(tty)) {
1670 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001671 status = mos7840_write(tty, port, &stop_char, 1);
1672 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001673 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001674 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001675 /* if we are implementing RTS/CTS, toggle that line */
1676 if (tty->termios->c_cflag & CRTSCTS) {
1677 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001678 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001679 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001680 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001681 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001682 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001683}
1684
1685/*****************************************************************************
1686 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001687 * this function is called by the tty driver when it wants to resume
1688 * the data being read from the port (called after mos7840_throttle is
1689 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001690 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001691static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001692{
Alan Cox95da3102008-07-22 11:09:07 +01001693 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001694 int status;
1695 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1696
Harvey Harrison441b62c2008-03-03 16:08:34 -08001697 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301698 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001699 return;
1700 }
1701
1702 if (mos7840_port == NULL)
1703 return;
1704
1705 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001706 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001707 return;
1708 }
1709
Paul B Schroeder3f542972006-08-31 19:41:47 -05001710 /* if we are implementing XON/XOFF, send the start character */
1711 if (I_IXOFF(tty)) {
1712 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001713 status = mos7840_write(tty, port, &start_char, 1);
1714 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001715 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001716 }
1717
1718 /* if we are implementing RTS/CTS, toggle that line */
1719 if (tty->termios->c_cflag & CRTSCTS) {
1720 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001721 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001722 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001723 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001724 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001725 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001726}
1727
Alan Cox60b33c12011-02-14 16:26:14 +00001728static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001729{
Alan Cox95da3102008-07-22 11:09:07 +01001730 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001731 struct moschip_port *mos7840_port;
1732 unsigned int result;
1733 __u16 msr;
1734 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001735 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001736 mos7840_port = mos7840_get_port_private(port);
1737
Paul B Schroeder3f542972006-08-31 19:41:47 -05001738 if (mos7840_port == NULL)
1739 return -ENODEV;
1740
1741 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1742 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1743 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1744 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1745 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1746 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1747 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1748 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1749 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1750
Harvey Harrison441b62c2008-03-03 16:08:34 -08001751 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001752
1753 return result;
1754}
1755
Alan Cox20b9d172011-02-14 16:26:50 +00001756static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001757 unsigned int set, unsigned int clear)
1758{
Alan Cox95da3102008-07-22 11:09:07 +01001759 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001760 struct moschip_port *mos7840_port;
1761 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001762 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001763
Paul B Schroeder3f542972006-08-31 19:41:47 -05001764 mos7840_port = mos7840_get_port_private(port);
1765
1766 if (mos7840_port == NULL)
1767 return -ENODEV;
1768
Alan Coxe2984492008-02-20 20:51:45 +00001769 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001770 mcr = mos7840_port->shadowMCR;
1771 if (clear & TIOCM_RTS)
1772 mcr &= ~MCR_RTS;
1773 if (clear & TIOCM_DTR)
1774 mcr &= ~MCR_DTR;
1775 if (clear & TIOCM_LOOP)
1776 mcr &= ~MCR_LOOPBACK;
1777
1778 if (set & TIOCM_RTS)
1779 mcr |= MCR_RTS;
1780 if (set & TIOCM_DTR)
1781 mcr |= MCR_DTR;
1782 if (set & TIOCM_LOOP)
1783 mcr |= MCR_LOOPBACK;
1784
1785 mos7840_port->shadowMCR = mcr;
1786
Paul B Schroeder3f542972006-08-31 19:41:47 -05001787 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1788 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301789 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001790 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001791 }
1792
1793 return 0;
1794}
1795
1796/*****************************************************************************
1797 * mos7840_calc_baud_rate_divisor
1798 * this function calculates the proper baud rate divisor for the specified
1799 * baud rate.
1800 *****************************************************************************/
1801static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001802 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001803{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001804 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001805
1806 if (baudRate <= 115200) {
1807 *divisor = 115200 / baudRate;
1808 *clk_sel_val = 0x0;
1809 }
1810 if ((baudRate > 115200) && (baudRate <= 230400)) {
1811 *divisor = 230400 / baudRate;
1812 *clk_sel_val = 0x10;
1813 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1814 *divisor = 403200 / baudRate;
1815 *clk_sel_val = 0x20;
1816 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1817 *divisor = 460800 / baudRate;
1818 *clk_sel_val = 0x30;
1819 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1820 *divisor = 806400 / baudRate;
1821 *clk_sel_val = 0x40;
1822 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1823 *divisor = 921600 / baudRate;
1824 *clk_sel_val = 0x50;
1825 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1826 *divisor = 1572864 / baudRate;
1827 *clk_sel_val = 0x60;
1828 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1829 *divisor = 3145728 / baudRate;
1830 *clk_sel_val = 0x70;
1831 }
1832 return 0;
1833
1834#ifdef NOTMCS7840
1835
1836 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1837 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1838 *divisor = mos7840_divisor_table[i].Divisor;
1839 return 0;
1840 }
1841 }
1842
1843 /* After trying for all the standard baud rates *
1844 * Try calculating the divisor for this baud rate */
1845
1846 if (baudrate > 75 && baudrate < 230400) {
1847 /* get the divisor */
1848 custom = (__u16) (230400L / baudrate);
1849
1850 /* Check for round off */
1851 round1 = (__u16) (2304000L / baudrate);
1852 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001853 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001854 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001855 *divisor = custom;
1856
Tony Cook84fe6e72009-04-18 22:55:06 +09301857 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001858 return 0;
1859 }
1860
Tony Cook84fe6e72009-04-18 22:55:06 +09301861 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001862 return -1;
1863#endif
1864}
1865
1866/*****************************************************************************
1867 * mos7840_send_cmd_write_baud_rate
1868 * this function sends the proper command to change the baud rate of the
1869 * specified port.
1870 *****************************************************************************/
1871
1872static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1873 int baudRate)
1874{
1875 int divisor = 0;
1876 int status;
1877 __u16 Data;
1878 unsigned char number;
1879 __u16 clk_sel_val;
1880 struct usb_serial_port *port;
1881
1882 if (mos7840_port == NULL)
1883 return -1;
1884
1885 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001886 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301887 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001888 return -1;
1889 }
1890
Harvey Harrison441b62c2008-03-03 16:08:34 -08001891 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301892 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001893 return -1;
1894 }
1895
Paul B Schroeder3f542972006-08-31 19:41:47 -05001896 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1897
Harvey Harrison441b62c2008-03-03 16:08:34 -08001898 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001899 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001900 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001901 if (baudRate > 115200) {
1902#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001903 /* NOTE: need to see the pther register to modify */
1904 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001905 Data = 0x2b;
1906 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001907 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1908 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001909 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301910 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001911 return -1;
1912 }
1913#endif
1914
1915 } else {
1916#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001917 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001918 Data = 0xb;
1919 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001920 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1921 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001922 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301923 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001924 return -1;
1925 }
1926#endif
1927
1928 }
1929
Alan Cox880af9d2008-07-22 11:16:12 +01001930 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001931 clk_sel_val = 0x0;
1932 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001933 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001934 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001935 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1936 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001937 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301938 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001939 return -1;
1940 }
1941 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001942 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1943 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001944 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301945 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001946 return -1;
1947 }
1948 /* Calculate the Divisor */
1949
1950 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001951 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001952 return status;
1953 }
1954 /* Enable access to divisor latch */
1955 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1956 mos7840_port->shadowLCR = Data;
1957 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1958
1959 /* Write the divisor */
1960 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301961 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001962 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1963
1964 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301965 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001966 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1967
1968 /* Disable access to divisor latch */
1969 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1970 mos7840_port->shadowLCR = Data;
1971 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1972
1973 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001974 return status;
1975}
1976
1977/*****************************************************************************
1978 * mos7840_change_port_settings
1979 * This routine is called to set the UART on the device to match
1980 * the specified new settings.
1981 *****************************************************************************/
1982
Alan Cox95da3102008-07-22 11:09:07 +01001983static void mos7840_change_port_settings(struct tty_struct *tty,
1984 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001985{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001986 int baud;
1987 unsigned cflag;
1988 unsigned iflag;
1989 __u8 lData;
1990 __u8 lParity;
1991 __u8 lStop;
1992 int status;
1993 __u16 Data;
1994 struct usb_serial_port *port;
1995 struct usb_serial *serial;
1996
1997 if (mos7840_port == NULL)
1998 return;
1999
2000 port = (struct usb_serial_port *)mos7840_port->port;
2001
Harvey Harrison441b62c2008-03-03 16:08:34 -08002002 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302003 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002004 return;
2005 }
2006
Harvey Harrison441b62c2008-03-03 16:08:34 -08002007 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302008 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002009 return;
2010 }
2011
2012 serial = port->serial;
2013
Harvey Harrison441b62c2008-03-03 16:08:34 -08002014 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002015
2016 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002017 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002018 return;
2019 }
2020
Paul B Schroeder3f542972006-08-31 19:41:47 -05002021 lData = LCR_BITS_8;
2022 lStop = LCR_STOP_1;
2023 lParity = LCR_PAR_NONE;
2024
2025 cflag = tty->termios->c_cflag;
2026 iflag = tty->termios->c_iflag;
2027
2028 /* Change the number of bits */
2029 if (cflag & CSIZE) {
2030 switch (cflag & CSIZE) {
2031 case CS5:
2032 lData = LCR_BITS_5;
2033 break;
2034
2035 case CS6:
2036 lData = LCR_BITS_6;
2037 break;
2038
2039 case CS7:
2040 lData = LCR_BITS_7;
2041 break;
2042 default:
2043 case CS8:
2044 lData = LCR_BITS_8;
2045 break;
2046 }
2047 }
2048 /* Change the Parity bit */
2049 if (cflag & PARENB) {
2050 if (cflag & PARODD) {
2051 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002052 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002053 } else {
2054 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002055 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002056 }
2057
2058 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002059 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002060 }
2061
Alan Cox880af9d2008-07-22 11:16:12 +01002062 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002063 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002064
2065 /* Change the Stop bit */
2066 if (cflag & CSTOPB) {
2067 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002068 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002069 } else {
2070 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002071 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002072 }
2073
2074 /* Update the LCR with the correct value */
2075 mos7840_port->shadowLCR &=
2076 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2077 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2078
Tony Cook84fe6e72009-04-18 22:55:06 +09302079 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002080 mos7840_port->shadowLCR);
2081 /* Disable Interrupts */
2082 Data = 0x00;
2083 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2084
2085 Data = 0x00;
2086 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2087
2088 Data = 0xcf;
2089 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2090
2091 /* Send the updated LCR value to the mos7840 */
2092 Data = mos7840_port->shadowLCR;
2093
2094 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2095
2096 Data = 0x00b;
2097 mos7840_port->shadowMCR = Data;
2098 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2099 Data = 0x00b;
2100 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2101
2102 /* set up the MCR register and send it to the mos7840 */
2103
2104 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002105 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002106 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002107
Alan Cox880af9d2008-07-22 11:16:12 +01002108 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002109 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002110 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002111 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002112
2113 Data = mos7840_port->shadowMCR;
2114 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2115
2116 /* Determine divisor based on baud rate */
2117 baud = tty_get_baud_rate(tty);
2118
2119 if (!baud) {
2120 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302121 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002122 baud = 9600;
2123 }
2124
Harvey Harrison441b62c2008-03-03 16:08:34 -08002125 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002126 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2127
2128 /* Enable Interrupts */
2129 Data = 0x0c;
2130 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2131
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002132 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002133 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002134 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002135 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002136 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002137 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002138 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002139 }
2140 }
2141 wake_up(&mos7840_port->delta_msr_wait);
2142 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302143 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002144 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002145}
2146
2147/*****************************************************************************
2148 * mos7840_set_termios
2149 * this function is called by the tty driver when it wants to change
2150 * the termios structure
2151 *****************************************************************************/
2152
Alan Cox95da3102008-07-22 11:09:07 +01002153static void mos7840_set_termios(struct tty_struct *tty,
2154 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002155 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002156{
2157 int status;
2158 unsigned int cflag;
2159 struct usb_serial *serial;
2160 struct moschip_port *mos7840_port;
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07002161
Harvey Harrison441b62c2008-03-03 16:08:34 -08002162 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302163 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002164 return;
2165 }
2166
2167 serial = port->serial;
2168
Harvey Harrison441b62c2008-03-03 16:08:34 -08002169 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302170 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002171 return;
2172 }
2173
2174 mos7840_port = mos7840_get_port_private(port);
2175
2176 if (mos7840_port == NULL)
2177 return;
2178
Paul B Schroeder3f542972006-08-31 19:41:47 -05002179 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002180 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002181 return;
2182 }
2183
Tony Cook84fe6e72009-04-18 22:55:06 +09302184 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002185
2186 cflag = tty->termios->c_cflag;
2187
Harvey Harrison441b62c2008-03-03 16:08:34 -08002188 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002189 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002190 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002191 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002192 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002193
2194 /* change the port settings to the new ones specified */
2195
Alan Cox95da3102008-07-22 11:09:07 +01002196 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002197
2198 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302199 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002200 return;
2201 }
2202
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002203 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002204 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002205 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2206 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002207 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002208 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002209 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002210 }
2211 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002212}
2213
2214/*****************************************************************************
2215 * mos7840_get_lsr_info - get line status register info
2216 *
2217 * Purpose: Let user call ioctl() to get info when the UART physically
2218 * is emptied. On bus types like RS485, the transmitter must
2219 * release the bus after transmitting. This must be done when
2220 * the transmit shift register is empty, not be done when the
2221 * transmit holding register is empty. This functionality
2222 * allows an RS485 driver to be written in user space.
2223 *****************************************************************************/
2224
Alan Cox95da3102008-07-22 11:09:07 +01002225static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002226 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002227{
2228 int count;
2229 unsigned int result = 0;
2230
Alan Cox95da3102008-07-22 11:09:07 +01002231 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002232 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002233 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002234 result = TIOCSER_TEMT;
2235 }
2236
2237 if (copy_to_user(value, &result, sizeof(int)))
2238 return -EFAULT;
2239 return 0;
2240}
2241
2242/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002243 * mos7840_get_serial_info
2244 * function to get information about serial port
2245 *****************************************************************************/
2246
2247static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002248 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002249{
2250 struct serial_struct tmp;
2251
2252 if (mos7840_port == NULL)
2253 return -1;
2254
2255 if (!retinfo)
2256 return -EFAULT;
2257
2258 memset(&tmp, 0, sizeof(tmp));
2259
2260 tmp.type = PORT_16550A;
2261 tmp.line = mos7840_port->port->serial->minor;
2262 tmp.port = mos7840_port->port->number;
2263 tmp.irq = 0;
2264 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2265 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2266 tmp.baud_base = 9600;
2267 tmp.close_delay = 5 * HZ;
2268 tmp.closing_wait = 30 * HZ;
2269
2270 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2271 return -EFAULT;
2272 return 0;
2273}
2274
Alan Cox0bca1b92010-09-16 18:21:40 +01002275static int mos7840_get_icount(struct tty_struct *tty,
2276 struct serial_icounter_struct *icount)
2277{
2278 struct usb_serial_port *port = tty->driver_data;
2279 struct moschip_port *mos7840_port;
2280 struct async_icount cnow;
2281
2282 mos7840_port = mos7840_get_port_private(port);
2283 cnow = mos7840_port->icount;
2284
2285 smp_rmb();
2286 icount->cts = cnow.cts;
2287 icount->dsr = cnow.dsr;
2288 icount->rng = cnow.rng;
2289 icount->dcd = cnow.dcd;
2290 icount->rx = cnow.rx;
2291 icount->tx = cnow.tx;
2292 icount->frame = cnow.frame;
2293 icount->overrun = cnow.overrun;
2294 icount->parity = cnow.parity;
2295 icount->brk = cnow.brk;
2296 icount->buf_overrun = cnow.buf_overrun;
2297
2298 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2299 port->number, icount->rx, icount->tx);
2300 return 0;
2301}
2302
Paul B Schroeder3f542972006-08-31 19:41:47 -05002303/*****************************************************************************
2304 * SerialIoctl
2305 * this function handles any ioctl calls to the driver
2306 *****************************************************************************/
2307
Alan Cox00a0d0d2011-02-14 16:27:06 +00002308static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002309 unsigned int cmd, unsigned long arg)
2310{
Alan Cox95da3102008-07-22 11:09:07 +01002311 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002312 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002313 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002314
2315 struct async_icount cnow;
2316 struct async_icount cprev;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002317
Harvey Harrison441b62c2008-03-03 16:08:34 -08002318 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302319 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002320 return -1;
2321 }
2322
2323 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002324
2325 if (mos7840_port == NULL)
2326 return -1;
2327
Harvey Harrison441b62c2008-03-03 16:08:34 -08002328 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002329
2330 switch (cmd) {
2331 /* return number of bytes available */
2332
Paul B Schroeder3f542972006-08-31 19:41:47 -05002333 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002334 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002335 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002336
Paul B Schroeder3f542972006-08-31 19:41:47 -05002337 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002338 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002339 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002340
2341 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002342 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002343 break;
2344
2345 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002346 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002347 cprev = mos7840_port->icount;
2348 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002349 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002350 mos7840_port->delta_msr_cond = 0;
2351 wait_event_interruptible(mos7840_port->delta_msr_wait,
2352 (mos7840_port->
2353 delta_msr_cond == 1));
2354
2355 /* see if a signal did it */
2356 if (signal_pending(current))
2357 return -ERESTARTSYS;
2358 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002359 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002360 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2361 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2362 return -EIO; /* no change => error */
2363 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2364 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2365 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2366 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2367 return 0;
2368 }
2369 cprev = cnow;
2370 }
2371 /* NOTREACHED */
2372 break;
2373
Paul B Schroeder3f542972006-08-31 19:41:47 -05002374 default:
2375 break;
2376 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002377 return -ENOIOCTLCMD;
2378}
2379
Donald0eafe4d2012-04-19 15:00:45 +08002380static int mos7810_check(struct usb_serial *serial)
2381{
2382 int i, pass_count = 0;
2383 __u16 data = 0, mcr_data = 0;
2384 __u16 test_pattern = 0x55AA;
2385
2386 /* Store MCR setting */
2387 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2388 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2389 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2390
2391 for (i = 0; i < 16; i++) {
2392 /* Send the 1-bit test pattern out to MCS7810 test pin */
2393 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2394 MCS_WRREQ, MCS_WR_RTYPE,
2395 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2396 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2397
2398 /* Read the test pattern back */
2399 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2400 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2401 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2402
2403 /* If this is a MCS7810 device, both test patterns must match */
2404 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2405 break;
2406
2407 pass_count++;
2408 }
2409
2410 /* Restore MCR setting */
2411 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2412 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2413 0, MOS_WDR_TIMEOUT);
2414
2415 if (pass_count == 16)
2416 return 1;
2417
2418 return 0;
2419}
2420
Paul B Schroeder3f542972006-08-31 19:41:47 -05002421static int mos7840_calc_num_ports(struct usb_serial *serial)
2422{
Donald0eafe4d2012-04-19 15:00:45 +08002423 __u16 data = 0x00;
Donald Lee093ea2d2012-03-14 15:26:33 +08002424 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002425
Donald0eafe4d2012-04-19 15:00:45 +08002426 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2427 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
Donald Lee093ea2d2012-03-14 15:26:33 +08002428 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2429
Donald0eafe4d2012-04-19 15:00:45 +08002430 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2431 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2432 device_type = serial->dev->descriptor.idProduct;
Donald Lee093ea2d2012-03-14 15:26:33 +08002433 } else {
Donald0eafe4d2012-04-19 15:00:45 +08002434 /* For a MCS7840 device GPIO0 must be set to 1 */
2435 if ((data & 0x01) == 1)
2436 device_type = MOSCHIP_DEVICE_ID_7840;
2437 else if (mos7810_check(serial))
2438 device_type = MOSCHIP_DEVICE_ID_7810;
2439 else
2440 device_type = MOSCHIP_DEVICE_ID_7820;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002441 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002442
Donald0eafe4d2012-04-19 15:00:45 +08002443 mos7840_num_ports = (device_type >> 4) & 0x000F;
2444 serial->num_bulk_in = mos7840_num_ports;
2445 serial->num_bulk_out = mos7840_num_ports;
2446 serial->num_ports = mos7840_num_ports;
2447
Paul B Schroeder3f542972006-08-31 19:41:47 -05002448 return mos7840_num_ports;
2449}
2450
2451/****************************************************************************
2452 * mos7840_startup
2453 ****************************************************************************/
2454
2455static int mos7840_startup(struct usb_serial *serial)
2456{
2457 struct moschip_port *mos7840_port;
2458 struct usb_device *dev;
2459 int i, status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002460 __u16 Data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002461
2462 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302463 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002464 return -1;
2465 }
2466
2467 dev = serial->dev;
2468
Paul B Schroeder3f542972006-08-31 19:41:47 -05002469 /* we set up the pointers to the endpoints in the mos7840_open *
2470 * function, as the structures aren't created yet. */
2471
2472 /* set up port private structures */
2473 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302474 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002475 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002476 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002477 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002478 status = -ENOMEM;
2479 i--; /* don't follow NULL pointer cleaning up */
2480 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002481 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002482
Alan Cox880af9d2008-07-22 11:16:12 +01002483 /* Initialize all port interrupt end point to port 0 int
2484 * endpoint. Our device has only one interrupt end point
2485 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002486
2487 mos7840_port->port = serial->port[i];
2488 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002489 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002490
Tony Cook37768ad2009-04-18 22:42:18 +09302491 /* minor is not initialised until later by
2492 * usb-serial.c:get_free_serial() and cannot therefore be used
2493 * to index device instances */
2494 mos7840_port->port_num = i + 1;
2495 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2496 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2497 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2498 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002499
2500 if (mos7840_port->port_num == 1) {
2501 mos7840_port->SpRegOffset = 0x0;
2502 mos7840_port->ControlRegOffset = 0x1;
2503 mos7840_port->DcrRegOffset = 0x4;
2504 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002505 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002506 mos7840_port->SpRegOffset = 0x8;
2507 mos7840_port->ControlRegOffset = 0x9;
2508 mos7840_port->DcrRegOffset = 0x16;
2509 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002510 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002511 mos7840_port->SpRegOffset = 0xa;
2512 mos7840_port->ControlRegOffset = 0xb;
2513 mos7840_port->DcrRegOffset = 0x19;
2514 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002515 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002516 mos7840_port->SpRegOffset = 0xa;
2517 mos7840_port->ControlRegOffset = 0xb;
2518 mos7840_port->DcrRegOffset = 0x19;
2519 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002520 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002521 mos7840_port->SpRegOffset = 0xc;
2522 mos7840_port->ControlRegOffset = 0xd;
2523 mos7840_port->DcrRegOffset = 0x1c;
2524 }
2525 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002526 mos7840_set_port_private(serial->port[i], mos7840_port);
2527
Alan Cox880af9d2008-07-22 11:16:12 +01002528 /* enable rx_disable bit in control register */
2529 status = mos7840_get_reg_sync(serial->port[i],
2530 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002531 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302532 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002533 break;
2534 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302535 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002536 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002537 Data |= 0x08; /* setting driver done bit */
2538 Data |= 0x04; /* sp1_bit to have cts change reflect in
2539 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002540
Alan Cox880af9d2008-07-22 11:16:12 +01002541 /* Data |= 0x20; //rx_disable bit */
2542 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002543 mos7840_port->ControlRegOffset, Data);
2544 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302545 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002546 break;
2547 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302548 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002549 status);
2550
Alan Cox880af9d2008-07-22 11:16:12 +01002551 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2552 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002553 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002554 status = mos7840_set_reg_sync(serial->port[i],
2555 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002556 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302557 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002558 break;
2559 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302560 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002561
2562 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002563 status = mos7840_set_reg_sync(serial->port[i],
2564 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002565 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302566 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002567 break;
2568 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302569 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002570
2571 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002572 status = mos7840_set_reg_sync(serial->port[i],
2573 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002574 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302575 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002576 break;
2577 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302578 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002579
Alan Cox880af9d2008-07-22 11:16:12 +01002580 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002581 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002582 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002583 CLK_START_VALUE_REGISTER, Data);
2584 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302585 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002586 break;
2587 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302588 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002589
2590 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002591 status = mos7840_set_reg_sync(serial->port[i],
2592 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002593 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302594 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002595 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002596 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002597 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302598 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002599 status);
2600
Alan Cox880af9d2008-07-22 11:16:12 +01002601 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002602 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002603 status = mos7840_set_uart_reg(serial->port[i],
2604 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002605 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302606 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002607 status);
2608 break;
2609 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302610 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002611 status);
2612
Alan Cox880af9d2008-07-22 11:16:12 +01002613 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002614 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002615 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002616
2617 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002618 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002619 (__u16) (ZLP_REG1 +
2620 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302621 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002622 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002623 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002624 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302625 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002626 i + 2, status);
2627 break;
2628 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302629 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002630 i + 2, status);
2631 } else {
2632 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002633 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002634 (__u16) (ZLP_REG1 +
2635 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302636 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002637 (__u16) (ZLP_REG1 +
2638 ((__u16) mos7840_port->port_num) - 0x1));
2639 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302640 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002641 i + 1, status);
2642 break;
2643 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302644 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002645 i + 1, status);
2646
2647 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002648 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002649 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002650 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2651 GFP_KERNEL);
2652 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2653 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002654 status = -ENOMEM;
2655 goto error;
2656 }
Donald0eafe4d2012-04-19 15:00:45 +08002657
2658 mos7840_port->has_led = false;
2659
2660 /* Initialize LED timers */
2661 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2662 mos7840_port->has_led = true;
2663
2664 init_timer(&mos7840_port->led_timer1);
2665 mos7840_port->led_timer1.function = mos7840_led_off;
2666 mos7840_port->led_timer1.expires =
2667 jiffies + msecs_to_jiffies(LED_ON_MS);
2668 mos7840_port->led_timer1.data =
2669 (unsigned long)mos7840_port;
2670
2671 init_timer(&mos7840_port->led_timer2);
2672 mos7840_port->led_timer2.function =
2673 mos7840_led_flag_off;
2674 mos7840_port->led_timer2.expires =
2675 jiffies + msecs_to_jiffies(LED_OFF_MS);
2676 mos7840_port->led_timer2.data =
2677 (unsigned long)mos7840_port;
2678
2679 mos7840_port->led_flag = false;
2680
2681 /* Turn off LED */
2682 mos7840_set_led_sync(serial->port[i],
2683 MODEM_CONTROL_REGISTER, 0x0300);
2684 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002685 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302686 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002687
Alan Cox880af9d2008-07-22 11:16:12 +01002688 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002689 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002690 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2691 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302692 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002693 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002694 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302695 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002696
2697 /* setting configuration feature to one */
2698 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002699 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002700 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002701error:
2702 for (/* nothing */; i >= 0; i--) {
2703 mos7840_port = mos7840_get_port_private(serial->port[i]);
2704
2705 kfree(mos7840_port->dr);
2706 kfree(mos7840_port->ctrl_buf);
2707 usb_free_urb(mos7840_port->control_urb);
2708 kfree(mos7840_port);
2709 serial->port[i] = NULL;
2710 }
2711 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002712}
2713
2714/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002715 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002716 * This function is called whenever the device is removed from the usb bus.
2717 ****************************************************************************/
2718
Alan Sternf9c99bb2009-06-02 11:53:55 -04002719static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002720{
2721 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002722 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002723 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002724
2725 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302726 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002727 return;
2728 }
2729
Alan Cox880af9d2008-07-22 11:16:12 +01002730 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002731
2732 /* free private structure allocated for serial port *
2733 * stop reads and writes on all ports */
2734
2735 for (i = 0; i < serial->num_ports; ++i) {
2736 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302737 dbg ("mos7840_port %d = %p", i, mos7840_port);
2738 if (mos7840_port) {
2739 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2740 mos7840_port->zombie = 1;
2741 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2742 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002743 }
2744 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04002745}
2746
2747/****************************************************************************
2748 * mos7840_release
2749 * This function is called when the usb_serial structure is freed.
2750 ****************************************************************************/
2751
2752static void mos7840_release(struct usb_serial *serial)
2753{
2754 int i;
2755 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002756
2757 if (!serial) {
2758 dbg("%s", "Invalid Handler");
2759 return;
2760 }
2761
2762 /* check for the ports to be closed,close the ports and disconnect */
2763
2764 /* free private structure allocated for serial port *
2765 * stop reads and writes on all ports */
2766
2767 for (i = 0; i < serial->num_ports; ++i) {
2768 mos7840_port = mos7840_get_port_private(serial->port[i]);
2769 dbg("mos7840_port %d = %p", i, mos7840_port);
2770 if (mos7840_port) {
Donald0eafe4d2012-04-19 15:00:45 +08002771 if (mos7840_port->has_led) {
2772 /* Turn off LED */
2773 mos7840_set_led_sync(mos7840_port->port,
2774 MODEM_CONTROL_REGISTER, 0x0300);
2775
2776 del_timer_sync(&mos7840_port->led_timer1);
2777 del_timer_sync(&mos7840_port->led_timer2);
2778 }
Tony Cook37768ad2009-04-18 22:42:18 +09302779 kfree(mos7840_port->ctrl_buf);
2780 kfree(mos7840_port->dr);
2781 kfree(mos7840_port);
2782 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002783 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002784}
2785
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002786static struct usb_driver io_driver = {
2787 .name = "mos7840",
2788 .probe = usb_serial_probe,
2789 .disconnect = usb_serial_disconnect,
2790 .id_table = moschip_id_table_combined,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002791};
2792
Paul B Schroeder3f542972006-08-31 19:41:47 -05002793static struct usb_serial_driver moschip7840_4port_device = {
2794 .driver = {
2795 .owner = THIS_MODULE,
2796 .name = "mos7840",
2797 },
2798 .description = DRIVER_DESC,
2799 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002800 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002801 .open = mos7840_open,
2802 .close = mos7840_close,
2803 .write = mos7840_write,
2804 .write_room = mos7840_write_room,
2805 .chars_in_buffer = mos7840_chars_in_buffer,
2806 .throttle = mos7840_throttle,
2807 .unthrottle = mos7840_unthrottle,
2808 .calc_num_ports = mos7840_calc_num_ports,
2809#ifdef MCSSerialProbe
2810 .probe = mos7840_serial_probe,
2811#endif
2812 .ioctl = mos7840_ioctl,
2813 .set_termios = mos7840_set_termios,
2814 .break_ctl = mos7840_break,
2815 .tiocmget = mos7840_tiocmget,
2816 .tiocmset = mos7840_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002817 .get_icount = mos7840_get_icount,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002818 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002819 .disconnect = mos7840_disconnect,
2820 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002821 .read_bulk_callback = mos7840_bulk_in_callback,
2822 .read_int_callback = mos7840_interrupt_callback,
2823};
2824
Alan Stern4d2a7af2012-02-23 14:57:09 -05002825static struct usb_serial_driver * const serial_drivers[] = {
2826 &moschip7840_4port_device, NULL
2827};
2828
Greg Kroah-Hartmane7414d92012-02-28 13:12:10 -08002829module_usb_serial_driver(io_driver, serial_drivers);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002830
Paul B Schroeder3f542972006-08-31 19:41:47 -05002831MODULE_DESCRIPTION(DRIVER_DESC);
2832MODULE_LICENSE("GPL");
2833
2834module_param(debug, bool, S_IRUGO | S_IWUSR);
2835MODULE_PARM_DESC(debug, "Debug enabled or not");