blob: 2f6da1e89bfa5606772103ce70f70087a9bfab42 [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
Mark Ferrell1e658482012-07-24 13:38:31 -050085#define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
Paul B Schroeder3f542972006-08-31 19:41:47 -050086
87#define MOS_PORT1 0x0200
88#define MOS_PORT2 0x0300
89#define MOS_VENREG 0x0000
90#define MOS_MAX_PORT 0x02
91#define MOS_WRITE 0x0E
92#define MOS_READ 0x0D
93
94/* Requests */
95#define MCS_RD_RTYPE 0xC0
96#define MCS_WR_RTYPE 0x40
97#define MCS_RDREQ 0x0D
98#define MCS_WRREQ 0x0E
99#define MCS_CTRL_TIMEOUT 500
100#define VENDOR_READ_LENGTH (0x01)
101
102#define MAX_NAME_LEN 64
103
Alan Cox880af9d2008-07-22 11:16:12 +0100104#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
105#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500106
107/* For higher baud Rates use TIOCEXBAUD */
108#define TIOCEXBAUD 0x5462
109
110/* vendor id and device id defines */
111
David Ludlow11e1abb2008-02-25 17:30:52 -0500112/* The native mos7840/7820 component */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500113#define USB_VENDOR_ID_MOSCHIP 0x9710
114#define MOSCHIP_DEVICE_ID_7840 0x7840
115#define MOSCHIP_DEVICE_ID_7820 0x7820
Donald0eafe4d2012-04-19 15:00:45 +0800116#define MOSCHIP_DEVICE_ID_7810 0x7810
David Ludlow11e1abb2008-02-25 17:30:52 -0500117/* The native component can have its vendor/device id's overridden
118 * in vendor-specific implementations. Such devices can be handled
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700119 * by making a change here, in id_table.
David Ludlow11e1abb2008-02-25 17:30:52 -0500120 */
Dave Ludlow870408c2010-09-01 12:33:30 -0400121#define USB_VENDOR_ID_BANDB 0x0856
122#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
123#define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
124#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
125#define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
126#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
127#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
128#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
129#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
130#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
131#define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
132#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
133#define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
134#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
Paul B Schroeder3f542972006-08-31 19:41:47 -0500135
Russell Lang9d498be2009-07-17 19:29:20 +1000136/* This driver also supports
137 * ATEN UC2324 device using Moschip MCS7840
138 * ATEN UC2322 device using Moschip MCS7820
139 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930140#define USB_VENDOR_ID_ATENINTL 0x0557
141#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000142#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930143
David Ludlow11e1abb2008-02-25 17:30:52 -0500144/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500145
146#define SERIAL_IIR_RLS 0x06
147#define SERIAL_IIR_MS 0x00
148
149/*
150 * Emulation of the bit mask on the LINE STATUS REGISTER.
151 */
152#define SERIAL_LSR_DR 0x0001
153#define SERIAL_LSR_OE 0x0002
154#define SERIAL_LSR_PE 0x0004
155#define SERIAL_LSR_FE 0x0008
156#define SERIAL_LSR_BI 0x0010
157
158#define MOS_MSR_DELTA_CTS 0x10
159#define MOS_MSR_DELTA_DSR 0x20
160#define MOS_MSR_DELTA_RI 0x40
161#define MOS_MSR_DELTA_CD 0x80
162
Alan Cox880af9d2008-07-22 11:16:12 +0100163/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500164#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
165#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
166#define LINE_CONTROL_REGISTER ((__u16)(0x03))
167#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
168#define LINE_STATUS_REGISTER ((__u16)(0x05))
169#define MODEM_STATUS_REGISTER ((__u16)(0x06))
170#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
171#define DIVISOR_LATCH_LSB ((__u16)(0x00))
172#define DIVISOR_LATCH_MSB ((__u16)(0x01))
173
174#define CLK_MULTI_REGISTER ((__u16)(0x02))
175#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
Donald Lee093ea2d2012-03-14 15:26:33 +0800176#define GPIO_REGISTER ((__u16)(0x07))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500177
178#define SERIAL_LCR_DLAB ((__u16)(0x0080))
179
180/*
181 * URB POOL related defines
182 */
183#define NUM_URBS 16 /* URB Count */
184#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
185
Donald0eafe4d2012-04-19 15:00:45 +0800186/* LED on/off milliseconds*/
187#define LED_ON_MS 500
188#define LED_OFF_MS 500
189
190static int device_type;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500191
Tony Zelenoffb9c87662012-06-05 17:58:04 +0400192static const struct usb_device_id id_table[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
194 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Donald0eafe4d2012-04-19 15:00:45 +0800195 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930209 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000210 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500211 {} /* terminating entry */
212};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700213MODULE_DEVICE_TABLE(usb, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500214
215/* This structure holds all of the local port information */
216
217struct moschip_port {
218 int port_num; /*Actual port number in the device(1,2,etc) */
219 struct urb *write_urb; /* write URB for this port */
220 struct urb *read_urb; /* read URB for this port */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100221 struct urb *int_urb;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500222 __u8 shadowLCR; /* last LCR value received */
223 __u8 shadowMCR; /* last MCR value received */
224 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100225 char open_ports;
226 char zombie;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500227 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
228 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
229 int delta_msr_cond;
230 struct async_icount icount;
231 struct usb_serial_port *port; /* loop back to the owner of this object */
232
Alan Cox880af9d2008-07-22 11:16:12 +0100233 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500234 __u8 SpRegOffset;
235 __u8 ControlRegOffset;
236 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100237 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500238 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100239 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500240 char *ctrl_buf;
241 int MsrLsr;
242
Oliver Neukum0de9a702007-03-16 20:28:28 +0100243 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500244 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100245 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800246 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500247
Donald0eafe4d2012-04-19 15:00:45 +0800248 /* For device(s) with LED indicator */
249 bool has_led;
250 bool led_flag;
251 struct timer_list led_timer1; /* Timer for LED on */
252 struct timer_list led_timer2; /* Timer for LED off */
253};
Paul B Schroeder3f542972006-08-31 19:41:47 -0500254
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030255static bool debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500256
257/*
258 * mos7840_set_reg_sync
259 * To set the Control register by calling usb_fill_control_urb function
260 * by passing usb_sndctrlpipe function as parameter.
261 */
262
263static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
264 __u16 val)
265{
266 struct usb_device *dev = port->serial->dev;
267 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930268 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500269
270 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
271 MCS_WR_RTYPE, val, reg, NULL, 0,
272 MOS_WDR_TIMEOUT);
273}
274
275/*
276 * mos7840_get_reg_sync
277 * To set the Uart register by calling usb_fill_control_urb function by
278 * passing usb_rcvctrlpipe function as parameter.
279 */
280
281static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100282 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500283{
284 struct usb_device *dev = port->serial->dev;
285 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100286 u8 *buf;
287
288 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
289 if (!buf)
290 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500291
292 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100293 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500294 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100295 *val = buf[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930296 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100297
298 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500299 return ret;
300}
301
302/*
303 * mos7840_set_uart_reg
304 * To set the Uart register by calling usb_fill_control_urb function by
305 * passing usb_sndctrlpipe function as parameter.
306 */
307
308static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
309 __u16 val)
310{
311
312 struct usb_device *dev = port->serial->dev;
313 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100314 /* For the UART control registers, the application number need
315 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100316 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100317 val |= (((__u16) port->number -
318 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930319 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500320 } else {
321 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100322 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500323 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930324 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500325 val);
326 } else {
327 val |=
328 (((__u16) port->number -
329 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930330 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500331 val);
332 }
333 }
334 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
335 MCS_WR_RTYPE, val, reg, NULL, 0,
336 MOS_WDR_TIMEOUT);
337
338}
339
340/*
341 * mos7840_get_uart_reg
342 * To set the Control register by calling usb_fill_control_urb function
343 * by passing usb_rcvctrlpipe function as parameter.
344 */
345static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100346 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500347{
348 struct usb_device *dev = port->serial->dev;
349 int ret = 0;
350 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100351 u8 *buf;
352
353 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
354 if (!buf)
355 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500356
Tony Cook84fe6e72009-04-18 22:55:06 +0930357 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100358 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
359 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100360 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500361 Wval =
362 (((__u16) port->number - (__u16) (port->serial->minor)) +
363 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930364 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500365 } else {
366 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100367 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500368 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930369 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500370 Wval);
371 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100372 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500373 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930374 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500375 Wval);
376 }
377 }
378 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100379 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500380 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100381 *val = buf[0];
382
383 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500384 return ret;
385}
386
387static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
388{
389
Tony Cook84fe6e72009-04-18 22:55:06 +0930390 dbg("***************************************");
391 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
392 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
393 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
394 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500395
396}
397
398/************************************************************************/
399/************************************************************************/
400/* I N T E R F A C E F U N C T I O N S */
401/* I N T E R F A C E F U N C T I O N S */
402/************************************************************************/
403/************************************************************************/
404
405static inline void mos7840_set_port_private(struct usb_serial_port *port,
406 struct moschip_port *data)
407{
408 usb_set_serial_port_data(port, (void *)data);
409}
410
411static inline struct moschip_port *mos7840_get_port_private(struct
412 usb_serial_port
413 *port)
414{
415 return (struct moschip_port *)usb_get_serial_port_data(port);
416}
417
Oliver Neukum0de9a702007-03-16 20:28:28 +0100418static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500419{
420 struct moschip_port *mos7840_port;
421 struct async_icount *icount;
422 mos7840_port = port;
423 icount = &mos7840_port->icount;
424 if (new_msr &
425 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
426 MOS_MSR_DELTA_CD)) {
427 icount = &mos7840_port->icount;
428
429 /* update input line counters */
430 if (new_msr & MOS_MSR_DELTA_CTS) {
431 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100432 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500433 }
434 if (new_msr & MOS_MSR_DELTA_DSR) {
435 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100436 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500437 }
438 if (new_msr & MOS_MSR_DELTA_CD) {
439 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100440 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500441 }
442 if (new_msr & MOS_MSR_DELTA_RI) {
443 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100444 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500445 }
446 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500447}
448
Oliver Neukum0de9a702007-03-16 20:28:28 +0100449static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500450{
451 struct async_icount *icount;
452
Harvey Harrison441b62c2008-03-03 16:08:34 -0800453 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500454
455 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100456 /*
457 * Parity and Framing errors only count if they
458 * occur exclusive of a break being
459 * received.
460 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500461 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
462 }
463
464 /* update input line counters */
465 icount = &port->icount;
466 if (new_lsr & SERIAL_LSR_BI) {
467 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100468 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500469 }
470 if (new_lsr & SERIAL_LSR_OE) {
471 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100472 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500473 }
474 if (new_lsr & SERIAL_LSR_PE) {
475 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100476 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500477 }
478 if (new_lsr & SERIAL_LSR_FE) {
479 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100480 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500481 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500482}
483
484/************************************************************************/
485/************************************************************************/
486/* U S B C A L L B A C K F U N C T I O N S */
487/* U S B C A L L B A C K F U N C T I O N S */
488/************************************************************************/
489/************************************************************************/
490
David Howells7d12e782006-10-05 14:55:46 +0100491static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500492{
493 unsigned char *data;
494 struct moschip_port *mos7840_port;
495 __u8 regval = 0x0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100496 int result = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700497 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500498
Ming Leicdc97792008-02-24 18:41:47 +0800499 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100500
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700501 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500502 case 0:
503 /* success */
504 break;
505 case -ECONNRESET:
506 case -ENOENT:
507 case -ESHUTDOWN:
508 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800509 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700510 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500511 return;
512 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800513 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700514 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500515 goto exit;
516 }
517
Tony Cook84fe6e72009-04-18 22:55:06 +0930518 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
519 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500520 mos7840_port->MsrLsr, mos7840_port->port_num);
521 data = urb->transfer_buffer;
522 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930523 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500524 if (mos7840_port->MsrLsr == 0)
525 mos7840_handle_new_msr(mos7840_port, regval);
526 else if (mos7840_port->MsrLsr == 1)
527 mos7840_handle_new_lsr(mos7840_port, regval);
528
Oliver Neukum0de9a702007-03-16 20:28:28 +0100529exit:
530 spin_lock(&mos7840_port->pool_lock);
531 if (!mos7840_port->zombie)
532 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
533 spin_unlock(&mos7840_port->pool_lock);
534 if (result) {
535 dev_err(&urb->dev->dev,
536 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800537 __func__, result);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100538 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500539}
540
541static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100542 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500543{
544 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100545 struct usb_ctrlrequest *dr = mcs->dr;
546 unsigned char *buffer = mcs->ctrl_buf;
547 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500548
Paul B Schroeder3f542972006-08-31 19:41:47 -0500549 dr->bRequestType = MCS_RD_RTYPE;
550 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100551 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500552 dr->wIndex = cpu_to_le16(reg);
553 dr->wLength = cpu_to_le16(2);
554
555 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
556 (unsigned char *)dr, buffer, 2,
557 mos7840_control_callback, mcs);
558 mcs->control_urb->transfer_buffer_length = 2;
559 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
560 return ret;
561}
562
Donald0eafe4d2012-04-19 15:00:45 +0800563static void mos7840_set_led_callback(struct urb *urb)
564{
565 switch (urb->status) {
566 case 0:
567 /* Success */
568 break;
569 case -ECONNRESET:
570 case -ENOENT:
571 case -ESHUTDOWN:
572 /* This urb is terminated, clean up */
573 dbg("%s - urb shutting down with status: %d", __func__,
574 urb->status);
575 break;
576 default:
577 dbg("%s - nonzero urb status received: %d", __func__,
578 urb->status);
579 }
580}
581
582static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
583 __u16 reg)
584{
585 struct usb_device *dev = mcs->port->serial->dev;
586 struct usb_ctrlrequest *dr = mcs->dr;
587
588 dr->bRequestType = MCS_WR_RTYPE;
589 dr->bRequest = MCS_WRREQ;
590 dr->wValue = cpu_to_le16(wval);
591 dr->wIndex = cpu_to_le16(reg);
592 dr->wLength = cpu_to_le16(0);
593
594 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
595 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
596
597 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
598}
599
600static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
601 __u16 val)
602{
603 struct usb_device *dev = port->serial->dev;
604
605 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
606 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
607}
608
609static void mos7840_led_off(unsigned long arg)
610{
611 struct moschip_port *mcs = (struct moschip_port *) arg;
612
613 /* Turn off LED */
614 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
615 mod_timer(&mcs->led_timer2,
616 jiffies + msecs_to_jiffies(LED_OFF_MS));
617}
618
619static void mos7840_led_flag_off(unsigned long arg)
620{
621 struct moschip_port *mcs = (struct moschip_port *) arg;
622
623 mcs->led_flag = false;
624}
625
Paul B Schroeder3f542972006-08-31 19:41:47 -0500626/*****************************************************************************
627 * mos7840_interrupt_callback
628 * this is the callback function for when we have received data on the
629 * interrupt endpoint.
630 *****************************************************************************/
631
David Howells7d12e782006-10-05 14:55:46 +0100632static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500633{
634 int result;
635 int length;
636 struct moschip_port *mos7840_port;
637 struct usb_serial *serial;
638 __u16 Data;
639 unsigned char *data;
640 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100641 int i, rv = 0;
642 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700643 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500644
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700645 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500646 case 0:
647 /* success */
648 break;
649 case -ECONNRESET:
650 case -ENOENT:
651 case -ESHUTDOWN:
652 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800653 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700654 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500655 return;
656 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800657 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700658 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500659 goto exit;
660 }
661
662 length = urb->actual_length;
663 data = urb->transfer_buffer;
664
Ming Leicdc97792008-02-24 18:41:47 +0800665 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500666
667 /* Moschip get 5 bytes
668 * Byte 1 IIR Port 1 (port.number is 0)
669 * Byte 2 IIR Port 2 (port.number is 1)
670 * Byte 3 IIR Port 3 (port.number is 2)
671 * Byte 4 IIR Port 4 (port.number is 3)
672 * Byte 5 FIFO status for both */
673
674 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930675 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500676 return;
677 }
678
679 sp[0] = (__u8) data[0];
680 sp[1] = (__u8) data[1];
681 sp[2] = (__u8) data[2];
682 sp[3] = (__u8) data[3];
683 st = (__u8) data[4];
684
685 for (i = 0; i < serial->num_ports; i++) {
686 mos7840_port = mos7840_get_port_private(serial->port[i]);
687 wval =
688 (((__u16) serial->port[i]->number -
689 (__u16) (serial->minor)) + 1) << 8;
690 if (mos7840_port->open) {
691 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930692 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500693 } else {
694 switch (sp[i] & 0x0f) {
695 case SERIAL_IIR_RLS:
696 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930697 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500698 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100699 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500700 break;
701 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930702 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500703 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100704 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500705 break;
706 }
Oliver Neukum0de9a702007-03-16 20:28:28 +0100707 spin_lock(&mos7840_port->pool_lock);
708 if (!mos7840_port->zombie) {
709 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
710 } else {
711 spin_unlock(&mos7840_port->pool_lock);
712 return;
713 }
714 spin_unlock(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500715 }
716 }
717 }
Alan Cox880af9d2008-07-22 11:16:12 +0100718 if (!(rv < 0))
719 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100720 return;
721exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500722 result = usb_submit_urb(urb, GFP_ATOMIC);
723 if (result) {
724 dev_err(&urb->dev->dev,
725 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800726 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500727 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500728}
729
730static int mos7840_port_paranoia_check(struct usb_serial_port *port,
731 const char *function)
732{
733 if (!port) {
734 dbg("%s - port == NULL", function);
735 return -1;
736 }
737 if (!port->serial) {
738 dbg("%s - port->serial == NULL", function);
739 return -1;
740 }
741
742 return 0;
743}
744
745/* Inline functions to check the sanity of a pointer that is passed to us */
746static int mos7840_serial_paranoia_check(struct usb_serial *serial,
747 const char *function)
748{
749 if (!serial) {
750 dbg("%s - serial == NULL", function);
751 return -1;
752 }
753 if (!serial->type) {
754 dbg("%s - serial->type == NULL!", function);
755 return -1;
756 }
757
758 return 0;
759}
760
761static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
762 const char *function)
763{
764 /* if no port was specified, or it fails a paranoia check */
765 if (!port ||
766 mos7840_port_paranoia_check(port, function) ||
767 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100768 /* then say that we don't have a valid usb_serial thing,
769 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500770 return NULL;
771 }
772
773 return port->serial;
774}
775
776/*****************************************************************************
777 * mos7840_bulk_in_callback
778 * this is the callback function for when we have received data on the
779 * bulk in endpoint.
780 *****************************************************************************/
781
David Howells7d12e782006-10-05 14:55:46 +0100782static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500783{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700784 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500785 unsigned char *data;
786 struct usb_serial *serial;
787 struct usb_serial_port *port;
788 struct moschip_port *mos7840_port;
789 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700790 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500791
Ming Leicdc97792008-02-24 18:41:47 +0800792 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500793 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930794 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800795 return;
796 }
797
798 if (status) {
799 dbg("nonzero read bulk status received: %d", status);
800 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500801 return;
802 }
803
804 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800805 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930806 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800807 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500808 return;
809 }
810
Harvey Harrison441b62c2008-03-03 16:08:34 -0800811 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500812 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930813 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800814 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500815 return;
816 }
817
Paul B Schroeder3f542972006-08-31 19:41:47 -0500818 data = urb->transfer_buffer;
819
Paul B Schroeder3f542972006-08-31 19:41:47 -0500820 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100821 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500822 if (tty) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500823 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930824 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500825 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100826 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500827 }
828 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100829 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930830 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500831 mos7840_port->icount.rx);
832 }
833
834 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930835 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800836 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500837 return;
838 }
839
Donald0eafe4d2012-04-19 15:00:45 +0800840 /* Turn on LED */
841 if (mos7840_port->has_led && !mos7840_port->led_flag) {
842 mos7840_port->led_flag = true;
843 mos7840_set_led_async(mos7840_port, 0x0301,
844 MODEM_CONTROL_REGISTER);
845 mod_timer(&mos7840_port->led_timer1,
846 jiffies + msecs_to_jiffies(LED_ON_MS));
847 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500848
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800849 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700850 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100851
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700852 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800853 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
854 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500855 }
856}
857
858/*****************************************************************************
859 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100860 * this is the callback function for when we have finished sending
861 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500862 *****************************************************************************/
863
David Howells7d12e782006-10-05 14:55:46 +0100864static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500865{
866 struct moschip_port *mos7840_port;
867 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700868 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100869 int i;
870
Ming Leicdc97792008-02-24 18:41:47 +0800871 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100872 spin_lock(&mos7840_port->pool_lock);
873 for (i = 0; i < NUM_URBS; i++) {
874 if (urb == mos7840_port->write_urb_pool[i]) {
875 mos7840_port->busy[i] = 0;
876 break;
877 }
878 }
879 spin_unlock(&mos7840_port->pool_lock);
880
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700881 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930882 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500883 return;
884 }
885
Harvey Harrison441b62c2008-03-03 16:08:34 -0800886 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930887 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500888 return;
889 }
890
Alan Cox4a90f092008-10-13 10:39:46 +0100891 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800892 if (tty && mos7840_port->open)
893 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100894 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500895
896}
897
898/************************************************************************/
899/* 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 */
900/************************************************************************/
901#ifdef MCSSerialProbe
902static int mos7840_serial_probe(struct usb_serial *serial,
903 const struct usb_device_id *id)
904{
905
906 /*need to implement the mode_reg reading and updating\
907 structures usb_serial_ device_type\
908 (i.e num_ports, num_bulkin,bulkout etc) */
909 /* Also we can update the changes attach */
910 return 1;
911}
912#endif
913
914/*****************************************************************************
915 * mos7840_open
916 * this function is called by the tty driver when a port is opened
917 * If successful, we return 0
918 * Otherwise we return a negative error number.
919 *****************************************************************************/
920
Alan Coxa509a7e2009-09-19 13:13:26 -0700921static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500922{
923 int response;
924 int j;
925 struct usb_serial *serial;
926 struct urb *urb;
927 __u16 Data;
928 int status;
929 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100930 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500931
Harvey Harrison441b62c2008-03-03 16:08:34 -0800932 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930933 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500934 return -ENODEV;
935 }
936
Paul B Schroeder3f542972006-08-31 19:41:47 -0500937 serial = port->serial;
938
Harvey Harrison441b62c2008-03-03 16:08:34 -0800939 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930940 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500941 return -ENODEV;
942 }
943
944 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100945 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500946
Oliver Neukum0de9a702007-03-16 20:28:28 +0100947 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500948 return -ENODEV;
949
950 usb_clear_halt(serial->dev, port->write_urb->pipe);
951 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100952 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500953
954 /* Initialising the write urb pool */
955 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100956 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500957 mos7840_port->write_urb_pool[j] = urb;
958
959 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700960 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500961 continue;
962 }
963
Alan Cox880af9d2008-07-22 11:16:12 +0100964 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
965 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500966 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100967 usb_free_urb(urb);
968 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700969 dev_err(&port->dev,
970 "%s-out of memory for urb buffers.\n",
971 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500972 continue;
973 }
974 }
975
976/*****************************************************************************
977 * Initialize MCS7840 -- Write Init values to corresponding Registers
978 *
979 * Register Index
980 * 1 : IER
981 * 2 : FCR
982 * 3 : LCR
983 * 4 : MCR
984 *
985 * 0x08 : SP1/2 Control Reg
986 *****************************************************************************/
987
Alan Cox880af9d2008-07-22 11:16:12 +0100988 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500989
Paul B Schroeder3f542972006-08-31 19:41:47 -0500990 Data = 0x0;
991 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
992 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930993 dbg("Reading Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500994 return -1;
995 }
996 Data |= 0x80;
997 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
998 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930999 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001000 return -1;
1001 }
1002
1003 Data &= ~0x80;
1004 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1005 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301006 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001007 return -1;
1008 }
Alan Cox880af9d2008-07-22 11:16:12 +01001009 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001010
Paul B Schroeder3f542972006-08-31 19:41:47 -05001011 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001012 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1013 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001014 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301015 dbg("Reading Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001016 return -1;
1017 }
Alan Cox880af9d2008-07-22 11:16:12 +01001018 Data |= 0x08; /* Driver done bit */
1019 Data |= 0x20; /* rx_disable */
1020 status = mos7840_set_reg_sync(port,
1021 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001022 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301023 dbg("writing Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001024 return -1;
1025 }
Alan Cox880af9d2008-07-22 11:16:12 +01001026 /* do register settings here */
1027 /* Set all regs to the device default values. */
1028 /***********************************
1029 * First Disable all interrupts.
1030 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -05001031 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001032 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1033 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301034 dbg("disabling interrupts failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001035 return -1;
1036 }
Alan Cox880af9d2008-07-22 11:16:12 +01001037 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001038 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001039 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1040 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301041 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001042 return -1;
1043 }
1044
1045 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001046 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1047 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301048 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001049 return -1;
1050 }
1051
1052 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001053 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1054 mos7840_port->shadowLCR = Data;
1055
1056 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001057 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1058 mos7840_port->shadowMCR = Data;
1059
1060 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001061 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1062 mos7840_port->shadowLCR = Data;
1063
Alan Cox880af9d2008-07-22 11:16:12 +01001064 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001065 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1066
1067 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001068 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1069
1070 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001071 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1072
1073 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001074 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1075
1076 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001077 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1078 mos7840_port->shadowLCR = Data;
1079
Alan Cox880af9d2008-07-22 11:16:12 +01001080 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001081 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001082 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1083
1084 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001085 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1086
1087 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001088 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001089 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001090 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001091 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1092
Alan Cox880af9d2008-07-22 11:16:12 +01001093 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001094 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001095 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1096 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001097 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001098 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1099 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001100
Alan Cox880af9d2008-07-22 11:16:12 +01001101 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001102 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001103 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1104 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001105 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001106 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1107 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001108
Alan Cox880af9d2008-07-22 11:16:12 +01001109 /* Check to see if we've set up our endpoint info yet *
1110 * (can't set it up in mos7840_startup as the structures *
1111 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001112 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001113 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001114 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001115 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001116 serial->dev,
1117 usb_rcvintpipe(serial->dev,
1118 serial->port[0]->interrupt_in_endpointAddress),
1119 serial->port[0]->interrupt_in_buffer,
1120 serial->port[0]->interrupt_in_urb->
1121 transfer_buffer_length,
1122 mos7840_interrupt_callback,
1123 serial,
1124 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001125
1126 /* start interrupt read for mos7840 *
1127 * will continue as long as mos7840 is connected */
1128
1129 response =
1130 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1131 GFP_KERNEL);
1132 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001133 dev_err(&port->dev, "%s - Error %d submitting "
1134 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001135 }
1136
1137 }
1138
1139 }
1140
1141 /* see if we've set up our endpoint info yet *
1142 * (can't set it up in mos7840_startup as the *
1143 * structures were not set up at that time.) */
1144
Tony Cook84fe6e72009-04-18 22:55:06 +09301145 dbg("port number is %d", port->number);
1146 dbg("serial number is %d", port->serial->minor);
1147 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1148 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1149 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1150 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001151 mos7840_port->read_urb = port->read_urb;
1152
1153 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001154 if ((serial->num_ports == 2)
1155 && ((((__u16)port->number -
1156 (__u16)(port->serial->minor)) % 2) != 0)) {
1157 usb_fill_bulk_urb(mos7840_port->read_urb,
1158 serial->dev,
1159 usb_rcvbulkpipe(serial->dev,
1160 (port->bulk_in_endpointAddress) + 2),
1161 port->bulk_in_buffer,
1162 mos7840_port->read_urb->transfer_buffer_length,
1163 mos7840_bulk_in_callback, mos7840_port);
1164 } else {
1165 usb_fill_bulk_urb(mos7840_port->read_urb,
1166 serial->dev,
1167 usb_rcvbulkpipe(serial->dev,
1168 port->bulk_in_endpointAddress),
1169 port->bulk_in_buffer,
1170 mos7840_port->read_urb->transfer_buffer_length,
1171 mos7840_bulk_in_callback, mos7840_port);
1172 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001173
Tony Cook84fe6e72009-04-18 22:55:06 +09301174 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001175 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001176 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001177 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1178 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001179 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1180 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001181 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001182 }
1183
1184 /* initialize our wait queues */
1185 init_waitqueue_head(&mos7840_port->wait_chase);
1186 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1187
1188 /* initialize our icount structure */
1189 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1190
1191 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001192 /* Must set to enable ints! */
1193 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001194 /* send a open port command */
1195 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001196 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001197 mos7840_port->icount.tx = 0;
1198 mos7840_port->icount.rx = 0;
1199
Tony Cook84fe6e72009-04-18 22:55:06 +09301200 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001201 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001202
1203 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001204}
1205
1206/*****************************************************************************
1207 * mos7840_chars_in_buffer
1208 * this function is called by the tty driver when it wants to know how many
1209 * bytes of data we currently have outstanding in the port (data that has
1210 * been written, but hasn't made it out the port yet)
1211 * If successful, we return the number of bytes left to be written in the
1212 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001213 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001214 *****************************************************************************/
1215
Alan Cox95da3102008-07-22 11:09:07 +01001216static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001217{
Alan Cox95da3102008-07-22 11:09:07 +01001218 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001219 int i;
1220 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001221 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001222 struct moschip_port *mos7840_port;
1223
Harvey Harrison441b62c2008-03-03 16:08:34 -08001224 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301225 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001226 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001227 }
1228
1229 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001230 if (mos7840_port == NULL)
Alan Cox95da3102008-07-22 11:09:07 +01001231 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001232
Alan Cox880af9d2008-07-22 11:16:12 +01001233 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell5c263b92012-07-24 14:15:13 -05001234 for (i = 0; i < NUM_URBS; ++i) {
1235 if (mos7840_port->busy[i]) {
1236 struct urb *urb = mos7840_port->write_urb_pool[i];
1237 chars += urb->transfer_buffer_length;
1238 }
1239 }
Alan Cox880af9d2008-07-22 11:16:12 +01001240 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001241 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001242 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001243
1244}
1245
Paul B Schroeder3f542972006-08-31 19:41:47 -05001246/*****************************************************************************
1247 * mos7840_close
1248 * this function is called by the tty driver when a port is closed
1249 *****************************************************************************/
1250
Alan Cox335f8512009-06-11 12:26:29 +01001251static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001252{
1253 struct usb_serial *serial;
1254 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001255 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001256 int j;
1257 __u16 Data;
1258
Harvey Harrison441b62c2008-03-03 16:08:34 -08001259 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301260 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001261 return;
1262 }
1263
Harvey Harrison441b62c2008-03-03 16:08:34 -08001264 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001265 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301266 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001267 return;
1268 }
1269
1270 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001271 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001272
Oliver Neukum0de9a702007-03-16 20:28:28 +01001273 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001274 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001275
1276 for (j = 0; j < NUM_URBS; ++j)
1277 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1278
1279 /* Freeing Write URBs */
1280 for (j = 0; j < NUM_URBS; ++j) {
1281 if (mos7840_port->write_urb_pool[j]) {
1282 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1283 kfree(mos7840_port->write_urb_pool[j]->
1284 transfer_buffer);
1285
1286 usb_free_urb(mos7840_port->write_urb_pool[j]);
1287 }
1288 }
1289
Paul B Schroeder3f542972006-08-31 19:41:47 -05001290 /* While closing port, shutdown all bulk read, write *
1291 * and interrupt read if they exists */
1292 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001293 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301294 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001295 usb_kill_urb(mos7840_port->write_urb);
1296 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001297 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301298 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001299 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001300 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001301 }
1302 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301303 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001304 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001305 }
1306 }
Alan Cox880af9d2008-07-22 11:16:12 +01001307/* if(mos7840_port->ctrl_buf != NULL) */
1308/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001309 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301310 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001311 port0->open_ports, port->number);
1312 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001313 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301314 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001315 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001316 }
1317 }
1318
1319 if (mos7840_port->write_urb) {
1320 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001321 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001322 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001323 usb_free_urb(mos7840_port->write_urb);
1324 }
1325
1326 Data = 0x0;
1327 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1328
1329 Data = 0x00;
1330 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1331
1332 mos7840_port->open = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001333}
1334
1335/************************************************************************
1336 *
1337 * mos7840_block_until_chase_response
1338 *
1339 * This function will block the close until one of the following:
1340 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001341 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001342 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1343 *
1344 ************************************************************************/
1345
Alan Cox95da3102008-07-22 11:09:07 +01001346static void mos7840_block_until_chase_response(struct tty_struct *tty,
1347 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001348{
Mark Ferrell1e658482012-07-24 13:38:31 -05001349 int timeout = msecs_to_jiffies(1000);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001350 int wait = 10;
1351 int count;
1352
1353 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001354 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001355
1356 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001357 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001358 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001359
1360 /* Block the thread for a while */
1361 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1362 timeout);
1363 /* No activity.. count down section */
1364 wait--;
1365 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001366 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001367 return;
1368 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001369 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001370 wait = 10;
1371 }
1372 }
1373
1374}
1375
1376/*****************************************************************************
1377 * mos7840_break
1378 * this function sends a break to the port
1379 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001380static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001381{
Alan Cox95da3102008-07-22 11:09:07 +01001382 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001383 unsigned char data;
1384 struct usb_serial *serial;
1385 struct moschip_port *mos7840_port;
1386
Harvey Harrison441b62c2008-03-03 16:08:34 -08001387 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301388 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001389 return;
1390 }
1391
Harvey Harrison441b62c2008-03-03 16:08:34 -08001392 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001393 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301394 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001395 return;
1396 }
1397
1398 mos7840_port = mos7840_get_port_private(port);
1399
Alan Cox880af9d2008-07-22 11:16:12 +01001400 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001401 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001402
Alan Cox95da3102008-07-22 11:09:07 +01001403 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001404 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001405 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001406
Alan Cox95da3102008-07-22 11:09:07 +01001407 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001408 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001409 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001410 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001411
Alan Cox6b447f042009-01-02 13:48:56 +00001412 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001413 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301414 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001415 mos7840_port->shadowLCR);
1416 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1417 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001418}
1419
1420/*****************************************************************************
1421 * mos7840_write_room
1422 * this function is called by the tty driver when it wants to know how many
1423 * bytes of data we can accept for a specific port.
1424 * If successful, we return the amount of room that we have for this port
1425 * Otherwise we return a negative error number.
1426 *****************************************************************************/
1427
Alan Cox95da3102008-07-22 11:09:07 +01001428static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001429{
Alan Cox95da3102008-07-22 11:09:07 +01001430 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001431 int i;
1432 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001433 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001434 struct moschip_port *mos7840_port;
1435
Harvey Harrison441b62c2008-03-03 16:08:34 -08001436 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301437 dbg("%s", "Invalid port");
1438 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001439 return -1;
1440 }
1441
1442 mos7840_port = mos7840_get_port_private(port);
1443 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301444 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001445 return -1;
1446 }
1447
Oliver Neukum0de9a702007-03-16 20:28:28 +01001448 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001449 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001450 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001451 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001452 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001453 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001454
Oliver Neukum0de9a702007-03-16 20:28:28 +01001455 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001456 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001457 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001458
1459}
1460
1461/*****************************************************************************
1462 * mos7840_write
1463 * this function is called by the tty driver when data should be written to
1464 * the port.
1465 * If successful, we return the number of bytes written, otherwise we
1466 * return a negative error number.
1467 *****************************************************************************/
1468
Alan Cox95da3102008-07-22 11:09:07 +01001469static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001470 const unsigned char *data, int count)
1471{
1472 int status;
1473 int i;
1474 int bytes_sent = 0;
1475 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001476 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001477
1478 struct moschip_port *mos7840_port;
1479 struct usb_serial *serial;
1480 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001481 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001482 const unsigned char *current_position = data;
1483 unsigned char *data1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001484
1485#ifdef NOTMOS7840
1486 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001487 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1488 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301489 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1490 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001491 mos7840_port->shadowLCR);
1492
Alan Cox880af9d2008-07-22 11:16:12 +01001493 /* Data = 0x03; */
1494 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1495 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001496
Alan Cox880af9d2008-07-22 11:16:12 +01001497 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001498 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1499
Alan Cox880af9d2008-07-22 11:16:12 +01001500 /* Data = 0x0c; */
1501 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001502 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001503 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301504 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001505
1506 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001507 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301508 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001509
1510 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301511 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001512 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001513 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1514#endif
1515
Harvey Harrison441b62c2008-03-03 16:08:34 -08001516 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301517 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001518 return -1;
1519 }
1520
1521 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001522 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301523 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001524 return -1;
1525 }
1526
1527 mos7840_port = mos7840_get_port_private(port);
1528 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301529 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001530 return -1;
1531 }
1532
1533 /* try to find a free urb in the list */
1534 urb = NULL;
1535
Oliver Neukum0de9a702007-03-16 20:28:28 +01001536 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001537 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001538 if (!mos7840_port->busy[i]) {
1539 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001540 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301541 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001542 break;
1543 }
1544 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001545 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001546
1547 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001548 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001549 goto exit;
1550 }
1551
1552 if (urb->transfer_buffer == NULL) {
1553 urb->transfer_buffer =
1554 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1555
1556 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001557 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001558 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001559 goto exit;
1560 }
1561 }
1562 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1563
Al Viro97c49652006-10-09 20:29:03 +01001564 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001565
1566 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001567 if ((serial->num_ports == 2)
1568 && ((((__u16)port->number -
1569 (__u16)(port->serial->minor)) % 2) != 0)) {
1570 usb_fill_bulk_urb(urb,
1571 serial->dev,
1572 usb_sndbulkpipe(serial->dev,
1573 (port->bulk_out_endpointAddress) + 2),
1574 urb->transfer_buffer,
1575 transfer_size,
1576 mos7840_bulk_out_data_callback, mos7840_port);
1577 } else {
1578 usb_fill_bulk_urb(urb,
1579 serial->dev,
1580 usb_sndbulkpipe(serial->dev,
1581 port->bulk_out_endpointAddress),
1582 urb->transfer_buffer,
1583 transfer_size,
1584 mos7840_bulk_out_data_callback, mos7840_port);
1585 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001586
1587 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301588 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001589
Donald0eafe4d2012-04-19 15:00:45 +08001590 /* Turn on LED */
1591 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1592 mos7840_port->led_flag = true;
1593 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1594 mod_timer(&mos7840_port->led_timer1,
1595 jiffies + msecs_to_jiffies(LED_ON_MS));
1596 }
1597
Paul B Schroeder3f542972006-08-31 19:41:47 -05001598 /* send it down the pipe */
1599 status = usb_submit_urb(urb, GFP_ATOMIC);
1600
1601 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001602 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001603 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001604 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001605 bytes_sent = status;
1606 goto exit;
1607 }
1608 bytes_sent = transfer_size;
1609 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001610 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301611 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001612exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001613 return bytes_sent;
1614
1615}
1616
1617/*****************************************************************************
1618 * mos7840_throttle
1619 * this function is called by the tty driver when it wants to stop the data
1620 * being read from the port.
1621 *****************************************************************************/
1622
Alan Cox95da3102008-07-22 11:09:07 +01001623static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001624{
Alan Cox95da3102008-07-22 11:09:07 +01001625 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001626 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001627 int status;
1628
Harvey Harrison441b62c2008-03-03 16:08:34 -08001629 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301630 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001631 return;
1632 }
1633
Tony Cook84fe6e72009-04-18 22:55:06 +09301634 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001635
1636 mos7840_port = mos7840_get_port_private(port);
1637
1638 if (mos7840_port == NULL)
1639 return;
1640
1641 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301642 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001643 return;
1644 }
1645
Paul B Schroeder3f542972006-08-31 19:41:47 -05001646 /* if we are implementing XON/XOFF, send the stop character */
1647 if (I_IXOFF(tty)) {
1648 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001649 status = mos7840_write(tty, port, &stop_char, 1);
1650 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001651 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001652 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001653 /* if we are implementing RTS/CTS, toggle that line */
1654 if (tty->termios->c_cflag & CRTSCTS) {
1655 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001656 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001657 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001658 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001659 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001660 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001661}
1662
1663/*****************************************************************************
1664 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001665 * this function is called by the tty driver when it wants to resume
1666 * the data being read from the port (called after mos7840_throttle is
1667 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001668 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001669static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001670{
Alan Cox95da3102008-07-22 11:09:07 +01001671 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001672 int status;
1673 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1674
Harvey Harrison441b62c2008-03-03 16:08:34 -08001675 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301676 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001677 return;
1678 }
1679
1680 if (mos7840_port == NULL)
1681 return;
1682
1683 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001684 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001685 return;
1686 }
1687
Paul B Schroeder3f542972006-08-31 19:41:47 -05001688 /* if we are implementing XON/XOFF, send the start character */
1689 if (I_IXOFF(tty)) {
1690 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001691 status = mos7840_write(tty, port, &start_char, 1);
1692 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001693 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001694 }
1695
1696 /* if we are implementing RTS/CTS, toggle that line */
1697 if (tty->termios->c_cflag & CRTSCTS) {
1698 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001699 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001700 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001701 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001702 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001703 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001704}
1705
Alan Cox60b33c12011-02-14 16:26:14 +00001706static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001707{
Alan Cox95da3102008-07-22 11:09:07 +01001708 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001709 struct moschip_port *mos7840_port;
1710 unsigned int result;
1711 __u16 msr;
1712 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001713 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001714 mos7840_port = mos7840_get_port_private(port);
1715
Paul B Schroeder3f542972006-08-31 19:41:47 -05001716 if (mos7840_port == NULL)
1717 return -ENODEV;
1718
1719 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1720 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1721 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1722 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1723 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1724 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1725 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1726 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1727 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1728
Harvey Harrison441b62c2008-03-03 16:08:34 -08001729 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001730
1731 return result;
1732}
1733
Alan Cox20b9d172011-02-14 16:26:50 +00001734static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001735 unsigned int set, unsigned int clear)
1736{
Alan Cox95da3102008-07-22 11:09:07 +01001737 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001738 struct moschip_port *mos7840_port;
1739 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001740 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001741
Paul B Schroeder3f542972006-08-31 19:41:47 -05001742 mos7840_port = mos7840_get_port_private(port);
1743
1744 if (mos7840_port == NULL)
1745 return -ENODEV;
1746
Alan Coxe2984492008-02-20 20:51:45 +00001747 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001748 mcr = mos7840_port->shadowMCR;
1749 if (clear & TIOCM_RTS)
1750 mcr &= ~MCR_RTS;
1751 if (clear & TIOCM_DTR)
1752 mcr &= ~MCR_DTR;
1753 if (clear & TIOCM_LOOP)
1754 mcr &= ~MCR_LOOPBACK;
1755
1756 if (set & TIOCM_RTS)
1757 mcr |= MCR_RTS;
1758 if (set & TIOCM_DTR)
1759 mcr |= MCR_DTR;
1760 if (set & TIOCM_LOOP)
1761 mcr |= MCR_LOOPBACK;
1762
1763 mos7840_port->shadowMCR = mcr;
1764
Paul B Schroeder3f542972006-08-31 19:41:47 -05001765 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1766 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301767 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001768 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001769 }
1770
1771 return 0;
1772}
1773
1774/*****************************************************************************
1775 * mos7840_calc_baud_rate_divisor
1776 * this function calculates the proper baud rate divisor for the specified
1777 * baud rate.
1778 *****************************************************************************/
1779static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001780 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001781{
Harvey Harrison441b62c2008-03-03 16:08:34 -08001782 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001783
1784 if (baudRate <= 115200) {
1785 *divisor = 115200 / baudRate;
1786 *clk_sel_val = 0x0;
1787 }
1788 if ((baudRate > 115200) && (baudRate <= 230400)) {
1789 *divisor = 230400 / baudRate;
1790 *clk_sel_val = 0x10;
1791 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1792 *divisor = 403200 / baudRate;
1793 *clk_sel_val = 0x20;
1794 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1795 *divisor = 460800 / baudRate;
1796 *clk_sel_val = 0x30;
1797 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1798 *divisor = 806400 / baudRate;
1799 *clk_sel_val = 0x40;
1800 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1801 *divisor = 921600 / baudRate;
1802 *clk_sel_val = 0x50;
1803 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1804 *divisor = 1572864 / baudRate;
1805 *clk_sel_val = 0x60;
1806 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1807 *divisor = 3145728 / baudRate;
1808 *clk_sel_val = 0x70;
1809 }
1810 return 0;
1811
1812#ifdef NOTMCS7840
1813
1814 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1815 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1816 *divisor = mos7840_divisor_table[i].Divisor;
1817 return 0;
1818 }
1819 }
1820
1821 /* After trying for all the standard baud rates *
1822 * Try calculating the divisor for this baud rate */
1823
1824 if (baudrate > 75 && baudrate < 230400) {
1825 /* get the divisor */
1826 custom = (__u16) (230400L / baudrate);
1827
1828 /* Check for round off */
1829 round1 = (__u16) (2304000L / baudrate);
1830 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001831 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001832 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001833 *divisor = custom;
1834
Tony Cook84fe6e72009-04-18 22:55:06 +09301835 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001836 return 0;
1837 }
1838
Tony Cook84fe6e72009-04-18 22:55:06 +09301839 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001840 return -1;
1841#endif
1842}
1843
1844/*****************************************************************************
1845 * mos7840_send_cmd_write_baud_rate
1846 * this function sends the proper command to change the baud rate of the
1847 * specified port.
1848 *****************************************************************************/
1849
1850static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1851 int baudRate)
1852{
1853 int divisor = 0;
1854 int status;
1855 __u16 Data;
1856 unsigned char number;
1857 __u16 clk_sel_val;
1858 struct usb_serial_port *port;
1859
1860 if (mos7840_port == NULL)
1861 return -1;
1862
1863 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001864 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301865 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001866 return -1;
1867 }
1868
Harvey Harrison441b62c2008-03-03 16:08:34 -08001869 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301870 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001871 return -1;
1872 }
1873
Paul B Schroeder3f542972006-08-31 19:41:47 -05001874 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1875
Harvey Harrison441b62c2008-03-03 16:08:34 -08001876 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001877 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001878 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001879 if (baudRate > 115200) {
1880#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001881 /* NOTE: need to see the pther register to modify */
1882 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001883 Data = 0x2b;
1884 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001885 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1886 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001887 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301888 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001889 return -1;
1890 }
1891#endif
1892
1893 } else {
1894#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001895 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001896 Data = 0xb;
1897 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001898 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1899 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001900 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301901 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001902 return -1;
1903 }
1904#endif
1905
1906 }
1907
Alan Cox880af9d2008-07-22 11:16:12 +01001908 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001909 clk_sel_val = 0x0;
1910 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001911 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001912 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001913 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1914 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001915 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301916 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001917 return -1;
1918 }
1919 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001920 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
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 /* Calculate the Divisor */
1927
1928 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001929 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001930 return status;
1931 }
1932 /* Enable access to divisor latch */
1933 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1934 mos7840_port->shadowLCR = Data;
1935 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1936
1937 /* Write the divisor */
1938 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301939 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001940 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1941
1942 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301943 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001944 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1945
1946 /* Disable access to divisor latch */
1947 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1948 mos7840_port->shadowLCR = Data;
1949 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1950
1951 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001952 return status;
1953}
1954
1955/*****************************************************************************
1956 * mos7840_change_port_settings
1957 * This routine is called to set the UART on the device to match
1958 * the specified new settings.
1959 *****************************************************************************/
1960
Alan Cox95da3102008-07-22 11:09:07 +01001961static void mos7840_change_port_settings(struct tty_struct *tty,
1962 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001963{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001964 int baud;
1965 unsigned cflag;
1966 unsigned iflag;
1967 __u8 lData;
1968 __u8 lParity;
1969 __u8 lStop;
1970 int status;
1971 __u16 Data;
1972 struct usb_serial_port *port;
1973 struct usb_serial *serial;
1974
1975 if (mos7840_port == NULL)
1976 return;
1977
1978 port = (struct usb_serial_port *)mos7840_port->port;
1979
Harvey Harrison441b62c2008-03-03 16:08:34 -08001980 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301981 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001982 return;
1983 }
1984
Harvey Harrison441b62c2008-03-03 16:08:34 -08001985 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301986 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001987 return;
1988 }
1989
1990 serial = port->serial;
1991
Harvey Harrison441b62c2008-03-03 16:08:34 -08001992 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001993
1994 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001995 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001996 return;
1997 }
1998
Paul B Schroeder3f542972006-08-31 19:41:47 -05001999 lData = LCR_BITS_8;
2000 lStop = LCR_STOP_1;
2001 lParity = LCR_PAR_NONE;
2002
2003 cflag = tty->termios->c_cflag;
2004 iflag = tty->termios->c_iflag;
2005
2006 /* Change the number of bits */
2007 if (cflag & CSIZE) {
2008 switch (cflag & CSIZE) {
2009 case CS5:
2010 lData = LCR_BITS_5;
2011 break;
2012
2013 case CS6:
2014 lData = LCR_BITS_6;
2015 break;
2016
2017 case CS7:
2018 lData = LCR_BITS_7;
2019 break;
2020 default:
2021 case CS8:
2022 lData = LCR_BITS_8;
2023 break;
2024 }
2025 }
2026 /* Change the Parity bit */
2027 if (cflag & PARENB) {
2028 if (cflag & PARODD) {
2029 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002030 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002031 } else {
2032 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002033 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002034 }
2035
2036 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002037 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002038 }
2039
Alan Cox880af9d2008-07-22 11:16:12 +01002040 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002041 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002042
2043 /* Change the Stop bit */
2044 if (cflag & CSTOPB) {
2045 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002046 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002047 } else {
2048 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002049 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002050 }
2051
2052 /* Update the LCR with the correct value */
2053 mos7840_port->shadowLCR &=
2054 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2055 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2056
Tony Cook84fe6e72009-04-18 22:55:06 +09302057 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002058 mos7840_port->shadowLCR);
2059 /* Disable Interrupts */
2060 Data = 0x00;
2061 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2062
2063 Data = 0x00;
2064 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2065
2066 Data = 0xcf;
2067 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2068
2069 /* Send the updated LCR value to the mos7840 */
2070 Data = mos7840_port->shadowLCR;
2071
2072 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2073
2074 Data = 0x00b;
2075 mos7840_port->shadowMCR = Data;
2076 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2077 Data = 0x00b;
2078 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2079
2080 /* set up the MCR register and send it to the mos7840 */
2081
2082 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002083 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002084 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002085
Alan Cox880af9d2008-07-22 11:16:12 +01002086 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002087 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002088 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002089 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002090
2091 Data = mos7840_port->shadowMCR;
2092 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2093
2094 /* Determine divisor based on baud rate */
2095 baud = tty_get_baud_rate(tty);
2096
2097 if (!baud) {
2098 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302099 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002100 baud = 9600;
2101 }
2102
Harvey Harrison441b62c2008-03-03 16:08:34 -08002103 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002104 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2105
2106 /* Enable Interrupts */
2107 Data = 0x0c;
2108 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2109
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002110 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002111 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002112 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002113 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002114 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002115 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002116 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002117 }
2118 }
2119 wake_up(&mos7840_port->delta_msr_wait);
2120 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302121 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002122 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002123}
2124
2125/*****************************************************************************
2126 * mos7840_set_termios
2127 * this function is called by the tty driver when it wants to change
2128 * the termios structure
2129 *****************************************************************************/
2130
Alan Cox95da3102008-07-22 11:09:07 +01002131static void mos7840_set_termios(struct tty_struct *tty,
2132 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002133 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002134{
2135 int status;
2136 unsigned int cflag;
2137 struct usb_serial *serial;
2138 struct moschip_port *mos7840_port;
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07002139
Harvey Harrison441b62c2008-03-03 16:08:34 -08002140 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302141 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002142 return;
2143 }
2144
2145 serial = port->serial;
2146
Harvey Harrison441b62c2008-03-03 16:08:34 -08002147 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302148 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002149 return;
2150 }
2151
2152 mos7840_port = mos7840_get_port_private(port);
2153
2154 if (mos7840_port == NULL)
2155 return;
2156
Paul B Schroeder3f542972006-08-31 19:41:47 -05002157 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002158 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002159 return;
2160 }
2161
Tony Cook84fe6e72009-04-18 22:55:06 +09302162 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002163
2164 cflag = tty->termios->c_cflag;
2165
Harvey Harrison441b62c2008-03-03 16:08:34 -08002166 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002167 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002168 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002169 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002170 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002171
2172 /* change the port settings to the new ones specified */
2173
Alan Cox95da3102008-07-22 11:09:07 +01002174 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002175
2176 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302177 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002178 return;
2179 }
2180
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002181 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002182 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002183 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2184 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002185 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002186 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002187 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002188 }
2189 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002190}
2191
2192/*****************************************************************************
2193 * mos7840_get_lsr_info - get line status register info
2194 *
2195 * Purpose: Let user call ioctl() to get info when the UART physically
2196 * is emptied. On bus types like RS485, the transmitter must
2197 * release the bus after transmitting. This must be done when
2198 * the transmit shift register is empty, not be done when the
2199 * transmit holding register is empty. This functionality
2200 * allows an RS485 driver to be written in user space.
2201 *****************************************************************************/
2202
Alan Cox95da3102008-07-22 11:09:07 +01002203static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002204 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002205{
2206 int count;
2207 unsigned int result = 0;
2208
Alan Cox95da3102008-07-22 11:09:07 +01002209 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002210 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002211 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002212 result = TIOCSER_TEMT;
2213 }
2214
2215 if (copy_to_user(value, &result, sizeof(int)))
2216 return -EFAULT;
2217 return 0;
2218}
2219
2220/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002221 * mos7840_get_serial_info
2222 * function to get information about serial port
2223 *****************************************************************************/
2224
2225static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002226 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002227{
2228 struct serial_struct tmp;
2229
2230 if (mos7840_port == NULL)
2231 return -1;
2232
2233 if (!retinfo)
2234 return -EFAULT;
2235
2236 memset(&tmp, 0, sizeof(tmp));
2237
2238 tmp.type = PORT_16550A;
2239 tmp.line = mos7840_port->port->serial->minor;
2240 tmp.port = mos7840_port->port->number;
2241 tmp.irq = 0;
2242 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2243 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2244 tmp.baud_base = 9600;
2245 tmp.close_delay = 5 * HZ;
2246 tmp.closing_wait = 30 * HZ;
2247
2248 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2249 return -EFAULT;
2250 return 0;
2251}
2252
Alan Cox0bca1b92010-09-16 18:21:40 +01002253static int mos7840_get_icount(struct tty_struct *tty,
2254 struct serial_icounter_struct *icount)
2255{
2256 struct usb_serial_port *port = tty->driver_data;
2257 struct moschip_port *mos7840_port;
2258 struct async_icount cnow;
2259
2260 mos7840_port = mos7840_get_port_private(port);
2261 cnow = mos7840_port->icount;
2262
2263 smp_rmb();
2264 icount->cts = cnow.cts;
2265 icount->dsr = cnow.dsr;
2266 icount->rng = cnow.rng;
2267 icount->dcd = cnow.dcd;
2268 icount->rx = cnow.rx;
2269 icount->tx = cnow.tx;
2270 icount->frame = cnow.frame;
2271 icount->overrun = cnow.overrun;
2272 icount->parity = cnow.parity;
2273 icount->brk = cnow.brk;
2274 icount->buf_overrun = cnow.buf_overrun;
2275
2276 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2277 port->number, icount->rx, icount->tx);
2278 return 0;
2279}
2280
Paul B Schroeder3f542972006-08-31 19:41:47 -05002281/*****************************************************************************
2282 * SerialIoctl
2283 * this function handles any ioctl calls to the driver
2284 *****************************************************************************/
2285
Alan Cox00a0d0d2011-02-14 16:27:06 +00002286static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002287 unsigned int cmd, unsigned long arg)
2288{
Alan Cox95da3102008-07-22 11:09:07 +01002289 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002290 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002291 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002292
2293 struct async_icount cnow;
2294 struct async_icount cprev;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002295
Harvey Harrison441b62c2008-03-03 16:08:34 -08002296 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302297 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002298 return -1;
2299 }
2300
2301 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002302
2303 if (mos7840_port == NULL)
2304 return -1;
2305
Harvey Harrison441b62c2008-03-03 16:08:34 -08002306 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002307
2308 switch (cmd) {
2309 /* return number of bytes available */
2310
Paul B Schroeder3f542972006-08-31 19:41:47 -05002311 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002312 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002313 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002314
Paul B Schroeder3f542972006-08-31 19:41:47 -05002315 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002316 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002317 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002318
2319 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002320 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002321 break;
2322
2323 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002324 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002325 cprev = mos7840_port->icount;
2326 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002327 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002328 mos7840_port->delta_msr_cond = 0;
2329 wait_event_interruptible(mos7840_port->delta_msr_wait,
2330 (mos7840_port->
2331 delta_msr_cond == 1));
2332
2333 /* see if a signal did it */
2334 if (signal_pending(current))
2335 return -ERESTARTSYS;
2336 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002337 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002338 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2339 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2340 return -EIO; /* no change => error */
2341 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2342 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2343 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2344 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2345 return 0;
2346 }
2347 cprev = cnow;
2348 }
2349 /* NOTREACHED */
2350 break;
2351
Paul B Schroeder3f542972006-08-31 19:41:47 -05002352 default:
2353 break;
2354 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002355 return -ENOIOCTLCMD;
2356}
2357
Donald0eafe4d2012-04-19 15:00:45 +08002358static int mos7810_check(struct usb_serial *serial)
2359{
2360 int i, pass_count = 0;
2361 __u16 data = 0, mcr_data = 0;
2362 __u16 test_pattern = 0x55AA;
2363
2364 /* Store MCR setting */
2365 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2366 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2367 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2368
2369 for (i = 0; i < 16; i++) {
2370 /* Send the 1-bit test pattern out to MCS7810 test pin */
2371 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2372 MCS_WRREQ, MCS_WR_RTYPE,
2373 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2374 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2375
2376 /* Read the test pattern back */
2377 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2378 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2379 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2380
2381 /* If this is a MCS7810 device, both test patterns must match */
2382 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2383 break;
2384
2385 pass_count++;
2386 }
2387
2388 /* Restore MCR setting */
2389 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2390 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2391 0, MOS_WDR_TIMEOUT);
2392
2393 if (pass_count == 16)
2394 return 1;
2395
2396 return 0;
2397}
2398
Paul B Schroeder3f542972006-08-31 19:41:47 -05002399static int mos7840_calc_num_ports(struct usb_serial *serial)
2400{
Donald0eafe4d2012-04-19 15:00:45 +08002401 __u16 data = 0x00;
Donald Lee093ea2d2012-03-14 15:26:33 +08002402 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002403
Donald0eafe4d2012-04-19 15:00:45 +08002404 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2405 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
Donald Lee093ea2d2012-03-14 15:26:33 +08002406 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2407
Donald0eafe4d2012-04-19 15:00:45 +08002408 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2409 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2410 device_type = serial->dev->descriptor.idProduct;
Donald Lee093ea2d2012-03-14 15:26:33 +08002411 } else {
Donald0eafe4d2012-04-19 15:00:45 +08002412 /* For a MCS7840 device GPIO0 must be set to 1 */
2413 if ((data & 0x01) == 1)
2414 device_type = MOSCHIP_DEVICE_ID_7840;
2415 else if (mos7810_check(serial))
2416 device_type = MOSCHIP_DEVICE_ID_7810;
2417 else
2418 device_type = MOSCHIP_DEVICE_ID_7820;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002419 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002420
Donald0eafe4d2012-04-19 15:00:45 +08002421 mos7840_num_ports = (device_type >> 4) & 0x000F;
2422 serial->num_bulk_in = mos7840_num_ports;
2423 serial->num_bulk_out = mos7840_num_ports;
2424 serial->num_ports = mos7840_num_ports;
2425
Paul B Schroeder3f542972006-08-31 19:41:47 -05002426 return mos7840_num_ports;
2427}
2428
2429/****************************************************************************
2430 * mos7840_startup
2431 ****************************************************************************/
2432
2433static int mos7840_startup(struct usb_serial *serial)
2434{
2435 struct moschip_port *mos7840_port;
2436 struct usb_device *dev;
2437 int i, status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002438 __u16 Data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002439
2440 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302441 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002442 return -1;
2443 }
2444
2445 dev = serial->dev;
2446
Paul B Schroeder3f542972006-08-31 19:41:47 -05002447 /* we set up the pointers to the endpoints in the mos7840_open *
2448 * function, as the structures aren't created yet. */
2449
2450 /* set up port private structures */
2451 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302452 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002453 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002454 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002455 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002456 status = -ENOMEM;
2457 i--; /* don't follow NULL pointer cleaning up */
2458 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002459 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002460
Alan Cox880af9d2008-07-22 11:16:12 +01002461 /* Initialize all port interrupt end point to port 0 int
2462 * endpoint. Our device has only one interrupt end point
2463 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002464
2465 mos7840_port->port = serial->port[i];
2466 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002467 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002468
Tony Cook37768ad2009-04-18 22:42:18 +09302469 /* minor is not initialised until later by
2470 * usb-serial.c:get_free_serial() and cannot therefore be used
2471 * to index device instances */
2472 mos7840_port->port_num = i + 1;
2473 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2474 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2475 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2476 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002477
2478 if (mos7840_port->port_num == 1) {
2479 mos7840_port->SpRegOffset = 0x0;
2480 mos7840_port->ControlRegOffset = 0x1;
2481 mos7840_port->DcrRegOffset = 0x4;
2482 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002483 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002484 mos7840_port->SpRegOffset = 0x8;
2485 mos7840_port->ControlRegOffset = 0x9;
2486 mos7840_port->DcrRegOffset = 0x16;
2487 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002488 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002489 mos7840_port->SpRegOffset = 0xa;
2490 mos7840_port->ControlRegOffset = 0xb;
2491 mos7840_port->DcrRegOffset = 0x19;
2492 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002493 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002494 mos7840_port->SpRegOffset = 0xa;
2495 mos7840_port->ControlRegOffset = 0xb;
2496 mos7840_port->DcrRegOffset = 0x19;
2497 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002498 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002499 mos7840_port->SpRegOffset = 0xc;
2500 mos7840_port->ControlRegOffset = 0xd;
2501 mos7840_port->DcrRegOffset = 0x1c;
2502 }
2503 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002504 mos7840_set_port_private(serial->port[i], mos7840_port);
2505
Alan Cox880af9d2008-07-22 11:16:12 +01002506 /* enable rx_disable bit in control register */
2507 status = mos7840_get_reg_sync(serial->port[i],
2508 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002509 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302510 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002511 break;
2512 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302513 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002514 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002515 Data |= 0x08; /* setting driver done bit */
2516 Data |= 0x04; /* sp1_bit to have cts change reflect in
2517 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002518
Alan Cox880af9d2008-07-22 11:16:12 +01002519 /* Data |= 0x20; //rx_disable bit */
2520 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002521 mos7840_port->ControlRegOffset, Data);
2522 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302523 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002524 break;
2525 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302526 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002527 status);
2528
Alan Cox880af9d2008-07-22 11:16:12 +01002529 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2530 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002531 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002532 status = mos7840_set_reg_sync(serial->port[i],
2533 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002534 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302535 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002536 break;
2537 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302538 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002539
2540 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002541 status = mos7840_set_reg_sync(serial->port[i],
2542 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002543 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302544 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002545 break;
2546 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302547 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002548
2549 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002550 status = mos7840_set_reg_sync(serial->port[i],
2551 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002552 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302553 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002554 break;
2555 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302556 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002557
Alan Cox880af9d2008-07-22 11:16:12 +01002558 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002559 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002560 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002561 CLK_START_VALUE_REGISTER, Data);
2562 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302563 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002564 break;
2565 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302566 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002567
2568 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002569 status = mos7840_set_reg_sync(serial->port[i],
2570 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002571 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302572 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002573 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002574 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002575 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302576 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002577 status);
2578
Alan Cox880af9d2008-07-22 11:16:12 +01002579 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002580 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002581 status = mos7840_set_uart_reg(serial->port[i],
2582 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002583 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302584 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002585 status);
2586 break;
2587 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302588 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002589 status);
2590
Alan Cox880af9d2008-07-22 11:16:12 +01002591 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002592 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002593 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002594
2595 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002596 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002597 (__u16) (ZLP_REG1 +
2598 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302599 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002600 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002601 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002602 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302603 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002604 i + 2, status);
2605 break;
2606 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302607 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002608 i + 2, status);
2609 } else {
2610 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002611 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002612 (__u16) (ZLP_REG1 +
2613 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302614 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002615 (__u16) (ZLP_REG1 +
2616 ((__u16) mos7840_port->port_num) - 0x1));
2617 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302618 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002619 i + 1, status);
2620 break;
2621 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302622 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002623 i + 1, status);
2624
2625 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002626 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002627 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002628 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2629 GFP_KERNEL);
2630 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2631 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002632 status = -ENOMEM;
2633 goto error;
2634 }
Donald0eafe4d2012-04-19 15:00:45 +08002635
2636 mos7840_port->has_led = false;
2637
2638 /* Initialize LED timers */
2639 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2640 mos7840_port->has_led = true;
2641
2642 init_timer(&mos7840_port->led_timer1);
2643 mos7840_port->led_timer1.function = mos7840_led_off;
2644 mos7840_port->led_timer1.expires =
2645 jiffies + msecs_to_jiffies(LED_ON_MS);
2646 mos7840_port->led_timer1.data =
2647 (unsigned long)mos7840_port;
2648
2649 init_timer(&mos7840_port->led_timer2);
2650 mos7840_port->led_timer2.function =
2651 mos7840_led_flag_off;
2652 mos7840_port->led_timer2.expires =
2653 jiffies + msecs_to_jiffies(LED_OFF_MS);
2654 mos7840_port->led_timer2.data =
2655 (unsigned long)mos7840_port;
2656
2657 mos7840_port->led_flag = false;
2658
2659 /* Turn off LED */
2660 mos7840_set_led_sync(serial->port[i],
2661 MODEM_CONTROL_REGISTER, 0x0300);
2662 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002663 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302664 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002665
Alan Cox880af9d2008-07-22 11:16:12 +01002666 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002667 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002668 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2669 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302670 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002671 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002672 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302673 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002674
2675 /* setting configuration feature to one */
2676 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Mark Ferrell1e658482012-07-24 13:38:31 -05002677 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002678 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002679error:
2680 for (/* nothing */; i >= 0; i--) {
2681 mos7840_port = mos7840_get_port_private(serial->port[i]);
2682
2683 kfree(mos7840_port->dr);
2684 kfree(mos7840_port->ctrl_buf);
2685 usb_free_urb(mos7840_port->control_urb);
2686 kfree(mos7840_port);
2687 serial->port[i] = NULL;
2688 }
2689 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002690}
2691
2692/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002693 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002694 * This function is called whenever the device is removed from the usb bus.
2695 ****************************************************************************/
2696
Alan Sternf9c99bb2009-06-02 11:53:55 -04002697static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002698{
2699 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002700 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002701 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002702
2703 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302704 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002705 return;
2706 }
2707
Alan Cox880af9d2008-07-22 11:16:12 +01002708 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002709
2710 /* free private structure allocated for serial port *
2711 * stop reads and writes on all ports */
2712
2713 for (i = 0; i < serial->num_ports; ++i) {
2714 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302715 dbg ("mos7840_port %d = %p", i, mos7840_port);
2716 if (mos7840_port) {
2717 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2718 mos7840_port->zombie = 1;
2719 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2720 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002721 }
2722 }
Alan Sternf9c99bb2009-06-02 11:53:55 -04002723}
2724
2725/****************************************************************************
2726 * mos7840_release
2727 * This function is called when the usb_serial structure is freed.
2728 ****************************************************************************/
2729
2730static void mos7840_release(struct usb_serial *serial)
2731{
2732 int i;
2733 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002734
2735 if (!serial) {
2736 dbg("%s", "Invalid Handler");
2737 return;
2738 }
2739
2740 /* check for the ports to be closed,close the ports and disconnect */
2741
2742 /* free private structure allocated for serial port *
2743 * stop reads and writes on all ports */
2744
2745 for (i = 0; i < serial->num_ports; ++i) {
2746 mos7840_port = mos7840_get_port_private(serial->port[i]);
2747 dbg("mos7840_port %d = %p", i, mos7840_port);
2748 if (mos7840_port) {
Donald0eafe4d2012-04-19 15:00:45 +08002749 if (mos7840_port->has_led) {
2750 /* Turn off LED */
2751 mos7840_set_led_sync(mos7840_port->port,
2752 MODEM_CONTROL_REGISTER, 0x0300);
2753
2754 del_timer_sync(&mos7840_port->led_timer1);
2755 del_timer_sync(&mos7840_port->led_timer2);
2756 }
Tony Cook37768ad2009-04-18 22:42:18 +09302757 kfree(mos7840_port->ctrl_buf);
2758 kfree(mos7840_port->dr);
2759 kfree(mos7840_port);
2760 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002761 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002762}
2763
2764static struct usb_serial_driver moschip7840_4port_device = {
2765 .driver = {
2766 .owner = THIS_MODULE,
2767 .name = "mos7840",
2768 },
2769 .description = DRIVER_DESC,
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002770 .id_table = id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002771 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002772 .open = mos7840_open,
2773 .close = mos7840_close,
2774 .write = mos7840_write,
2775 .write_room = mos7840_write_room,
2776 .chars_in_buffer = mos7840_chars_in_buffer,
2777 .throttle = mos7840_throttle,
2778 .unthrottle = mos7840_unthrottle,
2779 .calc_num_ports = mos7840_calc_num_ports,
2780#ifdef MCSSerialProbe
2781 .probe = mos7840_serial_probe,
2782#endif
2783 .ioctl = mos7840_ioctl,
2784 .set_termios = mos7840_set_termios,
2785 .break_ctl = mos7840_break,
2786 .tiocmget = mos7840_tiocmget,
2787 .tiocmset = mos7840_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002788 .get_icount = mos7840_get_icount,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002789 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002790 .disconnect = mos7840_disconnect,
2791 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002792 .read_bulk_callback = mos7840_bulk_in_callback,
2793 .read_int_callback = mos7840_interrupt_callback,
2794};
2795
Alan Stern4d2a7af2012-02-23 14:57:09 -05002796static struct usb_serial_driver * const serial_drivers[] = {
2797 &moschip7840_4port_device, NULL
2798};
2799
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002800module_usb_serial_driver(serial_drivers, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002801
Paul B Schroeder3f542972006-08-31 19:41:47 -05002802MODULE_DESCRIPTION(DRIVER_DESC);
2803MODULE_LICENSE("GPL");
2804
2805module_param(debug, bool, S_IRUGO | S_IWUSR);
2806MODULE_PARM_DESC(debug, "Debug enabled or not");