blob: 92feae6e88512cdefcf4cf3ffa353d9414a34344 [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
Paul B Schroeder3f542972006-08-31 19:41:47 -050038#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
39
40/*
41 * 16C50 UART register defines
42 */
43
44#define LCR_BITS_5 0x00 /* 5 bits/char */
45#define LCR_BITS_6 0x01 /* 6 bits/char */
46#define LCR_BITS_7 0x02 /* 7 bits/char */
47#define LCR_BITS_8 0x03 /* 8 bits/char */
48#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
49
50#define LCR_STOP_1 0x00 /* 1 stop bit */
51#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
52#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
53#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
54
55#define LCR_PAR_NONE 0x00 /* No parity */
56#define LCR_PAR_ODD 0x08 /* Odd parity */
57#define LCR_PAR_EVEN 0x18 /* Even parity */
58#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
59#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
60#define LCR_PAR_MASK 0x38 /* Mask for parity field */
61
62#define LCR_SET_BREAK 0x40 /* Set Break condition */
63#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
64
65#define MCR_DTR 0x01 /* Assert DTR */
66#define MCR_RTS 0x02 /* Assert RTS */
67#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
68#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
69#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
70#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
71
72#define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
73#define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
74#define MOS7840_MSR_RI 0x40 /* Current state of RI */
75#define MOS7840_MSR_CD 0x80 /* Current state of CD */
76
77/*
78 * Defines used for sending commands to port
79 */
80
Mark Ferrell1e658482012-07-24 13:38:31 -050081#define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
Paul B Schroeder3f542972006-08-31 19:41:47 -050082
83#define MOS_PORT1 0x0200
84#define MOS_PORT2 0x0300
85#define MOS_VENREG 0x0000
86#define MOS_MAX_PORT 0x02
87#define MOS_WRITE 0x0E
88#define MOS_READ 0x0D
89
90/* Requests */
91#define MCS_RD_RTYPE 0xC0
92#define MCS_WR_RTYPE 0x40
93#define MCS_RDREQ 0x0D
94#define MCS_WRREQ 0x0E
95#define MCS_CTRL_TIMEOUT 500
96#define VENDOR_READ_LENGTH (0x01)
97
98#define MAX_NAME_LEN 64
99
Alan Cox880af9d2008-07-22 11:16:12 +0100100#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
101#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500102
103/* For higher baud Rates use TIOCEXBAUD */
104#define TIOCEXBAUD 0x5462
105
106/* vendor id and device id defines */
107
David Ludlow11e1abb2008-02-25 17:30:52 -0500108/* The native mos7840/7820 component */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500109#define USB_VENDOR_ID_MOSCHIP 0x9710
110#define MOSCHIP_DEVICE_ID_7840 0x7840
111#define MOSCHIP_DEVICE_ID_7820 0x7820
Donald0eafe4d2012-04-19 15:00:45 +0800112#define MOSCHIP_DEVICE_ID_7810 0x7810
David Ludlow11e1abb2008-02-25 17:30:52 -0500113/* The native component can have its vendor/device id's overridden
114 * in vendor-specific implementations. Such devices can be handled
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700115 * by making a change here, in id_table.
David Ludlow11e1abb2008-02-25 17:30:52 -0500116 */
Dave Ludlow870408c2010-09-01 12:33:30 -0400117#define USB_VENDOR_ID_BANDB 0x0856
118#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
119#define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
120#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
121#define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
122#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
123#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
124#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
125#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
126#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
127#define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
128#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
129#define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
130#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
Paul B Schroeder3f542972006-08-31 19:41:47 -0500131
Russell Lang9d498be2009-07-17 19:29:20 +1000132/* This driver also supports
133 * ATEN UC2324 device using Moschip MCS7840
134 * ATEN UC2322 device using Moschip MCS7820
135 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930136#define USB_VENDOR_ID_ATENINTL 0x0557
137#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000138#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930139
David Ludlow11e1abb2008-02-25 17:30:52 -0500140/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500141
142#define SERIAL_IIR_RLS 0x06
143#define SERIAL_IIR_MS 0x00
144
145/*
146 * Emulation of the bit mask on the LINE STATUS REGISTER.
147 */
148#define SERIAL_LSR_DR 0x0001
149#define SERIAL_LSR_OE 0x0002
150#define SERIAL_LSR_PE 0x0004
151#define SERIAL_LSR_FE 0x0008
152#define SERIAL_LSR_BI 0x0010
153
154#define MOS_MSR_DELTA_CTS 0x10
155#define MOS_MSR_DELTA_DSR 0x20
156#define MOS_MSR_DELTA_RI 0x40
157#define MOS_MSR_DELTA_CD 0x80
158
Alan Cox880af9d2008-07-22 11:16:12 +0100159/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500160#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
161#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
162#define LINE_CONTROL_REGISTER ((__u16)(0x03))
163#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
164#define LINE_STATUS_REGISTER ((__u16)(0x05))
165#define MODEM_STATUS_REGISTER ((__u16)(0x06))
166#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
167#define DIVISOR_LATCH_LSB ((__u16)(0x00))
168#define DIVISOR_LATCH_MSB ((__u16)(0x01))
169
170#define CLK_MULTI_REGISTER ((__u16)(0x02))
171#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
Donald Lee093ea2d2012-03-14 15:26:33 +0800172#define GPIO_REGISTER ((__u16)(0x07))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500173
174#define SERIAL_LCR_DLAB ((__u16)(0x0080))
175
176/*
177 * URB POOL related defines
178 */
179#define NUM_URBS 16 /* URB Count */
180#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
181
Donald0eafe4d2012-04-19 15:00:45 +0800182/* LED on/off milliseconds*/
183#define LED_ON_MS 500
184#define LED_OFF_MS 500
185
186static int device_type;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500187
Tony Zelenoffb9c87662012-06-05 17:58:04 +0400188static const struct usb_device_id id_table[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
190 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Donald0eafe4d2012-04-19 15:00:45 +0800191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930205 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000206 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500207 {} /* terminating entry */
208};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700209MODULE_DEVICE_TABLE(usb, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500210
211/* This structure holds all of the local port information */
212
213struct moschip_port {
214 int port_num; /*Actual port number in the device(1,2,etc) */
215 struct urb *write_urb; /* write URB for this port */
216 struct urb *read_urb; /* read URB for this port */
217 __u8 shadowLCR; /* last LCR value received */
218 __u8 shadowMCR; /* last MCR value received */
219 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100220 char open_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500221 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500222 int delta_msr_cond;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500223 struct usb_serial_port *port; /* loop back to the owner of this object */
224
Alan Cox880af9d2008-07-22 11:16:12 +0100225 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500226 __u8 SpRegOffset;
227 __u8 ControlRegOffset;
228 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100229 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500230 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100231 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500232 char *ctrl_buf;
233 int MsrLsr;
234
Oliver Neukum0de9a702007-03-16 20:28:28 +0100235 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500236 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100237 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800238 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500239
Donald0eafe4d2012-04-19 15:00:45 +0800240 /* For device(s) with LED indicator */
241 bool has_led;
242 bool led_flag;
243 struct timer_list led_timer1; /* Timer for LED on */
244 struct timer_list led_timer2; /* Timer for LED off */
245};
Paul B Schroeder3f542972006-08-31 19:41:47 -0500246
Paul B Schroeder3f542972006-08-31 19:41:47 -0500247/*
248 * mos7840_set_reg_sync
249 * To set the Control register by calling usb_fill_control_urb function
250 * by passing usb_sndctrlpipe function as parameter.
251 */
252
253static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
254 __u16 val)
255{
256 struct usb_device *dev = port->serial->dev;
257 val = val & 0x00ff;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700258 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500259
260 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
261 MCS_WR_RTYPE, val, reg, NULL, 0,
262 MOS_WDR_TIMEOUT);
263}
264
265/*
266 * mos7840_get_reg_sync
267 * To set the Uart register by calling usb_fill_control_urb function by
268 * passing usb_rcvctrlpipe function as parameter.
269 */
270
271static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100272 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500273{
274 struct usb_device *dev = port->serial->dev;
275 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100276 u8 *buf;
277
278 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
279 if (!buf)
280 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500281
282 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100283 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500284 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100285 *val = buf[0];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700286 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100287
288 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500289 return ret;
290}
291
292/*
293 * mos7840_set_uart_reg
294 * To set the Uart register by calling usb_fill_control_urb function by
295 * passing usb_sndctrlpipe function as parameter.
296 */
297
298static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
299 __u16 val)
300{
301
302 struct usb_device *dev = port->serial->dev;
303 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100304 /* For the UART control registers, the application number need
305 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100306 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100307 val |= (((__u16) port->number -
308 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500309 } else {
310 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100311 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500312 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500313 } else {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700314 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500315 (__u16) (port->serial->minor)) + 2) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500316 }
317 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700318 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500319 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
320 MCS_WR_RTYPE, val, reg, NULL, 0,
321 MOS_WDR_TIMEOUT);
322
323}
324
325/*
326 * mos7840_get_uart_reg
327 * To set the Control register by calling usb_fill_control_urb function
328 * by passing usb_rcvctrlpipe function as parameter.
329 */
330static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100331 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500332{
333 struct usb_device *dev = port->serial->dev;
334 int ret = 0;
335 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100336 u8 *buf;
337
338 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
339 if (!buf)
340 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500341
Alan Cox880af9d2008-07-22 11:16:12 +0100342 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100343 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500344 Wval =
345 (((__u16) port->number - (__u16) (port->serial->minor)) +
346 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500347 } else {
348 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100349 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500350 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500351 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100352 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500353 (__u16) (port->serial->minor)) + 2) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500354 }
355 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700356 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500357 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100358 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500359 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100360 *val = buf[0];
361
362 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500363 return ret;
364}
365
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700366static void mos7840_dump_serial_port(struct usb_serial_port *port,
367 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500368{
369
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700370 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
371 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
372 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500373
374}
375
376/************************************************************************/
377/************************************************************************/
378/* I N T E R F A C E F U N C T I O N S */
379/* I N T E R F A C E F U N C T I O N S */
380/************************************************************************/
381/************************************************************************/
382
383static inline void mos7840_set_port_private(struct usb_serial_port *port,
384 struct moschip_port *data)
385{
386 usb_set_serial_port_data(port, (void *)data);
387}
388
389static inline struct moschip_port *mos7840_get_port_private(struct
390 usb_serial_port
391 *port)
392{
393 return (struct moschip_port *)usb_get_serial_port_data(port);
394}
395
Oliver Neukum0de9a702007-03-16 20:28:28 +0100396static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500397{
398 struct moschip_port *mos7840_port;
399 struct async_icount *icount;
400 mos7840_port = port;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500401 if (new_msr &
402 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
403 MOS_MSR_DELTA_CD)) {
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100404 icount = &mos7840_port->port->icount;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500405
406 /* update input line counters */
Johan Hovoldc9fac852013-03-21 12:37:15 +0100407 if (new_msr & MOS_MSR_DELTA_CTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500408 icount->cts++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100409 if (new_msr & MOS_MSR_DELTA_DSR)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500410 icount->dsr++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100411 if (new_msr & MOS_MSR_DELTA_CD)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500412 icount->dcd++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100413 if (new_msr & MOS_MSR_DELTA_RI)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500414 icount->rng++;
Johan Hovolde670c6a2013-03-19 09:21:19 +0100415
416 mos7840_port->delta_msr_cond = 1;
Johan Hovolda14430d2013-03-19 09:21:20 +0100417 wake_up_interruptible(&port->port->delta_msr_wait);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500418 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500419}
420
Oliver Neukum0de9a702007-03-16 20:28:28 +0100421static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500422{
423 struct async_icount *icount;
424
Paul B Schroeder3f542972006-08-31 19:41:47 -0500425 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100426 /*
427 * Parity and Framing errors only count if they
428 * occur exclusive of a break being
429 * received.
430 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500431 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
432 }
433
434 /* update input line counters */
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100435 icount = &port->port->icount;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100436 if (new_lsr & SERIAL_LSR_BI)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500437 icount->brk++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100438 if (new_lsr & SERIAL_LSR_OE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500439 icount->overrun++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100440 if (new_lsr & SERIAL_LSR_PE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500441 icount->parity++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100442 if (new_lsr & SERIAL_LSR_FE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500443 icount->frame++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500444}
445
446/************************************************************************/
447/************************************************************************/
448/* U S B C A L L B A C K F U N C T I O N S */
449/* U S B C A L L B A C K F U N C T I O N S */
450/************************************************************************/
451/************************************************************************/
452
David Howells7d12e782006-10-05 14:55:46 +0100453static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500454{
455 unsigned char *data;
456 struct moschip_port *mos7840_port;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700457 struct device *dev = &urb->dev->dev;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500458 __u8 regval = 0x0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700459 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500460
Ming Leicdc97792008-02-24 18:41:47 +0800461 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100462
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700463 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500464 case 0:
465 /* success */
466 break;
467 case -ECONNRESET:
468 case -ENOENT:
469 case -ESHUTDOWN:
470 /* this urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700471 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500472 return;
473 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700474 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Johan Hovold28c3ae92012-10-25 18:56:32 +0200475 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500476 }
477
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700478 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
479 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
480 mos7840_port->MsrLsr, mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500481 data = urb->transfer_buffer;
482 regval = (__u8) data[0];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700483 dev_dbg(dev, "%s data is %x\n", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500484 if (mos7840_port->MsrLsr == 0)
485 mos7840_handle_new_msr(mos7840_port, regval);
486 else if (mos7840_port->MsrLsr == 1)
487 mos7840_handle_new_lsr(mos7840_port, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500488}
489
490static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100491 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500492{
493 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100494 struct usb_ctrlrequest *dr = mcs->dr;
495 unsigned char *buffer = mcs->ctrl_buf;
496 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500497
Paul B Schroeder3f542972006-08-31 19:41:47 -0500498 dr->bRequestType = MCS_RD_RTYPE;
499 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100500 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500501 dr->wIndex = cpu_to_le16(reg);
502 dr->wLength = cpu_to_le16(2);
503
504 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
505 (unsigned char *)dr, buffer, 2,
506 mos7840_control_callback, mcs);
507 mcs->control_urb->transfer_buffer_length = 2;
508 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
509 return ret;
510}
511
Donald0eafe4d2012-04-19 15:00:45 +0800512static void mos7840_set_led_callback(struct urb *urb)
513{
514 switch (urb->status) {
515 case 0:
516 /* Success */
517 break;
518 case -ECONNRESET:
519 case -ENOENT:
520 case -ESHUTDOWN:
521 /* This urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700522 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
523 __func__, urb->status);
Donald0eafe4d2012-04-19 15:00:45 +0800524 break;
525 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700526 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
527 __func__, urb->status);
Donald0eafe4d2012-04-19 15:00:45 +0800528 }
529}
530
531static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
532 __u16 reg)
533{
534 struct usb_device *dev = mcs->port->serial->dev;
535 struct usb_ctrlrequest *dr = mcs->dr;
536
537 dr->bRequestType = MCS_WR_RTYPE;
538 dr->bRequest = MCS_WRREQ;
539 dr->wValue = cpu_to_le16(wval);
540 dr->wIndex = cpu_to_le16(reg);
541 dr->wLength = cpu_to_le16(0);
542
543 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
544 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
545
546 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
547}
548
549static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
550 __u16 val)
551{
552 struct usb_device *dev = port->serial->dev;
553
554 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
555 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
556}
557
558static void mos7840_led_off(unsigned long arg)
559{
560 struct moschip_port *mcs = (struct moschip_port *) arg;
561
562 /* Turn off LED */
563 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
564 mod_timer(&mcs->led_timer2,
565 jiffies + msecs_to_jiffies(LED_OFF_MS));
566}
567
568static void mos7840_led_flag_off(unsigned long arg)
569{
570 struct moschip_port *mcs = (struct moschip_port *) arg;
571
572 mcs->led_flag = false;
573}
574
Paul B Schroeder3f542972006-08-31 19:41:47 -0500575/*****************************************************************************
576 * mos7840_interrupt_callback
577 * this is the callback function for when we have received data on the
578 * interrupt endpoint.
579 *****************************************************************************/
580
David Howells7d12e782006-10-05 14:55:46 +0100581static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500582{
583 int result;
584 int length;
585 struct moschip_port *mos7840_port;
586 struct usb_serial *serial;
587 __u16 Data;
588 unsigned char *data;
589 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100590 int i, rv = 0;
591 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700592 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500593
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700594 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500595 case 0:
596 /* success */
597 break;
598 case -ECONNRESET:
599 case -ENOENT:
600 case -ESHUTDOWN:
601 /* this urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700602 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
603 __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500604 return;
605 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700606 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
607 __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500608 goto exit;
609 }
610
611 length = urb->actual_length;
612 data = urb->transfer_buffer;
613
Ming Leicdc97792008-02-24 18:41:47 +0800614 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500615
616 /* Moschip get 5 bytes
617 * Byte 1 IIR Port 1 (port.number is 0)
618 * Byte 2 IIR Port 2 (port.number is 1)
619 * Byte 3 IIR Port 3 (port.number is 2)
620 * Byte 4 IIR Port 4 (port.number is 3)
621 * Byte 5 FIFO status for both */
622
623 if (length && length > 5) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700624 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500625 return;
626 }
627
628 sp[0] = (__u8) data[0];
629 sp[1] = (__u8) data[1];
630 sp[2] = (__u8) data[2];
631 sp[3] = (__u8) data[3];
632 st = (__u8) data[4];
633
634 for (i = 0; i < serial->num_ports; i++) {
635 mos7840_port = mos7840_get_port_private(serial->port[i]);
636 wval =
637 (((__u16) serial->port[i]->number -
638 (__u16) (serial->minor)) + 1) << 8;
639 if (mos7840_port->open) {
640 if (sp[i] & 0x01) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700641 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500642 } else {
643 switch (sp[i] & 0x0f) {
644 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700645 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
646 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500647 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100648 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500649 break;
650 case SERIAL_IIR_MS:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700651 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500652 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100653 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500654 break;
655 }
Johan Hovolde681b662012-10-25 18:56:33 +0200656 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500657 }
658 }
659 }
Alan Cox880af9d2008-07-22 11:16:12 +0100660 if (!(rv < 0))
661 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100662 return;
663exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500664 result = usb_submit_urb(urb, GFP_ATOMIC);
665 if (result) {
666 dev_err(&urb->dev->dev,
667 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800668 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500669 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500670}
671
672static int mos7840_port_paranoia_check(struct usb_serial_port *port,
673 const char *function)
674{
675 if (!port) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700676 pr_debug("%s - port == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500677 return -1;
678 }
679 if (!port->serial) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700680 pr_debug("%s - port->serial == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500681 return -1;
682 }
683
684 return 0;
685}
686
687/* Inline functions to check the sanity of a pointer that is passed to us */
688static int mos7840_serial_paranoia_check(struct usb_serial *serial,
689 const char *function)
690{
691 if (!serial) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700692 pr_debug("%s - serial == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500693 return -1;
694 }
695 if (!serial->type) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700696 pr_debug("%s - serial->type == NULL!\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500697 return -1;
698 }
699
700 return 0;
701}
702
703static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
704 const char *function)
705{
706 /* if no port was specified, or it fails a paranoia check */
707 if (!port ||
708 mos7840_port_paranoia_check(port, function) ||
709 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100710 /* then say that we don't have a valid usb_serial thing,
711 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500712 return NULL;
713 }
714
715 return port->serial;
716}
717
718/*****************************************************************************
719 * mos7840_bulk_in_callback
720 * this is the callback function for when we have received data on the
721 * bulk in endpoint.
722 *****************************************************************************/
723
David Howells7d12e782006-10-05 14:55:46 +0100724static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500725{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700726 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500727 unsigned char *data;
728 struct usb_serial *serial;
729 struct usb_serial_port *port;
730 struct moschip_port *mos7840_port;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700731 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500732
Ming Leicdc97792008-02-24 18:41:47 +0800733 mos7840_port = urb->context;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700734 if (!mos7840_port)
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800735 return;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800736
737 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700738 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800739 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500740 return;
741 }
742
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700743 port = mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800744 if (mos7840_port_paranoia_check(port, __func__)) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800745 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500746 return;
747 }
748
Harvey Harrison441b62c2008-03-03 16:08:34 -0800749 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500750 if (!serial) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800751 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500752 return;
753 }
754
Paul B Schroeder3f542972006-08-31 19:41:47 -0500755 data = urb->transfer_buffer;
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100756 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500757
Paul B Schroeder3f542972006-08-31 19:41:47 -0500758 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100759 struct tty_port *tport = &mos7840_port->port->port;
Jiri Slaby2e124b42013-01-03 15:53:06 +0100760 tty_insert_flip_string(tport, data, urb->actual_length);
761 tty_flip_buffer_push(tport);
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100762 port->icount.rx += urb->actual_length;
763 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500764 }
765
766 if (!mos7840_port->read_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700767 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800768 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500769 return;
770 }
771
Donald0eafe4d2012-04-19 15:00:45 +0800772 /* Turn on LED */
773 if (mos7840_port->has_led && !mos7840_port->led_flag) {
774 mos7840_port->led_flag = true;
775 mos7840_set_led_async(mos7840_port, 0x0301,
776 MODEM_CONTROL_REGISTER);
777 mod_timer(&mos7840_port->led_timer1,
778 jiffies + msecs_to_jiffies(LED_ON_MS));
779 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500780
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800781 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700782 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100783
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700784 if (retval) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700785 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800786 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500787 }
788}
789
790/*****************************************************************************
791 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100792 * this is the callback function for when we have finished sending
793 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500794 *****************************************************************************/
795
David Howells7d12e782006-10-05 14:55:46 +0100796static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500797{
798 struct moschip_port *mos7840_port;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700799 struct usb_serial_port *port;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500800 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700801 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100802 int i;
803
Ming Leicdc97792008-02-24 18:41:47 +0800804 mos7840_port = urb->context;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700805 port = mos7840_port->port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100806 spin_lock(&mos7840_port->pool_lock);
807 for (i = 0; i < NUM_URBS; i++) {
808 if (urb == mos7840_port->write_urb_pool[i]) {
809 mos7840_port->busy[i] = 0;
810 break;
811 }
812 }
813 spin_unlock(&mos7840_port->pool_lock);
814
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700815 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700816 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500817 return;
818 }
819
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700820 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500821 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500822
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700823 tty = tty_port_tty_get(&port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800824 if (tty && mos7840_port->open)
825 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100826 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500827
828}
829
830/************************************************************************/
831/* 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 */
832/************************************************************************/
833#ifdef MCSSerialProbe
834static int mos7840_serial_probe(struct usb_serial *serial,
835 const struct usb_device_id *id)
836{
837
838 /*need to implement the mode_reg reading and updating\
839 structures usb_serial_ device_type\
840 (i.e num_ports, num_bulkin,bulkout etc) */
841 /* Also we can update the changes attach */
842 return 1;
843}
844#endif
845
846/*****************************************************************************
847 * mos7840_open
848 * this function is called by the tty driver when a port is opened
849 * If successful, we return 0
850 * Otherwise we return a negative error number.
851 *****************************************************************************/
852
Alan Coxa509a7e2009-09-19 13:13:26 -0700853static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500854{
855 int response;
856 int j;
857 struct usb_serial *serial;
858 struct urb *urb;
859 __u16 Data;
860 int status;
861 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100862 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500863
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700864 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500865 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500866
Paul B Schroeder3f542972006-08-31 19:41:47 -0500867 serial = port->serial;
868
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700869 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500870 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500871
872 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100873 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500874
Oliver Neukum0de9a702007-03-16 20:28:28 +0100875 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500876 return -ENODEV;
877
878 usb_clear_halt(serial->dev, port->write_urb->pipe);
879 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100880 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500881
882 /* Initialising the write urb pool */
883 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100884 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500885 mos7840_port->write_urb_pool[j] = urb;
886
887 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700888 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500889 continue;
890 }
891
Alan Cox880af9d2008-07-22 11:16:12 +0100892 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
893 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500894 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100895 usb_free_urb(urb);
896 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700897 dev_err(&port->dev,
898 "%s-out of memory for urb buffers.\n",
899 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500900 continue;
901 }
902 }
903
904/*****************************************************************************
905 * Initialize MCS7840 -- Write Init values to corresponding Registers
906 *
907 * Register Index
908 * 1 : IER
909 * 2 : FCR
910 * 3 : LCR
911 * 4 : MCR
912 *
913 * 0x08 : SP1/2 Control Reg
914 *****************************************************************************/
915
Alan Cox880af9d2008-07-22 11:16:12 +0100916 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500917
Paul B Schroeder3f542972006-08-31 19:41:47 -0500918 Data = 0x0;
919 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
920 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700921 dev_dbg(&port->dev, "Reading Spreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500922 return -1;
923 }
924 Data |= 0x80;
925 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
926 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700927 dev_dbg(&port->dev, "writing Spreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500928 return -1;
929 }
930
931 Data &= ~0x80;
932 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
933 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700934 dev_dbg(&port->dev, "writing Spreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500935 return -1;
936 }
Alan Cox880af9d2008-07-22 11:16:12 +0100937 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500938
Paul B Schroeder3f542972006-08-31 19:41:47 -0500939 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100940 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
941 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500942 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700943 dev_dbg(&port->dev, "Reading Controlreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500944 return -1;
945 }
Alan Cox880af9d2008-07-22 11:16:12 +0100946 Data |= 0x08; /* Driver done bit */
947 Data |= 0x20; /* rx_disable */
948 status = mos7840_set_reg_sync(port,
949 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500950 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700951 dev_dbg(&port->dev, "writing Controlreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500952 return -1;
953 }
Alan Cox880af9d2008-07-22 11:16:12 +0100954 /* do register settings here */
955 /* Set all regs to the device default values. */
956 /***********************************
957 * First Disable all interrupts.
958 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500959 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500960 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
961 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700962 dev_dbg(&port->dev, "disabling interrupts failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500963 return -1;
964 }
Alan Cox880af9d2008-07-22 11:16:12 +0100965 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500966 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500967 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
968 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700969 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500970 return -1;
971 }
972
973 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500974 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
975 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700976 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500977 return -1;
978 }
979
980 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500981 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
982 mos7840_port->shadowLCR = Data;
983
984 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500985 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
986 mos7840_port->shadowMCR = Data;
987
988 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500989 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
990 mos7840_port->shadowLCR = Data;
991
Alan Cox880af9d2008-07-22 11:16:12 +0100992 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500993 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
994
995 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500996 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
997
998 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500999 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1000
1001 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001002 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1003
1004 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001005 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1006 mos7840_port->shadowLCR = Data;
1007
Alan Cox880af9d2008-07-22 11:16:12 +01001008 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001009 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001010 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1011
1012 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001013 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1014
1015 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001016 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001017 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001018 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001019 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1020
Alan Cox880af9d2008-07-22 11:16:12 +01001021 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001022 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001023 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1024 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001025 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001026 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1027 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001028
Alan Cox880af9d2008-07-22 11:16:12 +01001029 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001030 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001031 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1032 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001033 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001034 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1035 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001036
Alan Cox880af9d2008-07-22 11:16:12 +01001037 /* Check to see if we've set up our endpoint info yet *
1038 * (can't set it up in mos7840_startup as the structures *
1039 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001040 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001041 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001042 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001043 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001044 serial->dev,
1045 usb_rcvintpipe(serial->dev,
1046 serial->port[0]->interrupt_in_endpointAddress),
1047 serial->port[0]->interrupt_in_buffer,
1048 serial->port[0]->interrupt_in_urb->
1049 transfer_buffer_length,
1050 mos7840_interrupt_callback,
1051 serial,
1052 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001053
1054 /* start interrupt read for mos7840 *
1055 * will continue as long as mos7840 is connected */
1056
1057 response =
1058 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1059 GFP_KERNEL);
1060 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001061 dev_err(&port->dev, "%s - Error %d submitting "
1062 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001063 }
1064
1065 }
1066
1067 }
1068
1069 /* see if we've set up our endpoint info yet *
1070 * (can't set it up in mos7840_startup as the *
1071 * structures were not set up at that time.) */
1072
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001073 dev_dbg(&port->dev, "port number is %d\n", port->number);
1074 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1075 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1076 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1077 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1078 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001079 mos7840_port->read_urb = port->read_urb;
1080
1081 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001082 if ((serial->num_ports == 2)
1083 && ((((__u16)port->number -
1084 (__u16)(port->serial->minor)) % 2) != 0)) {
1085 usb_fill_bulk_urb(mos7840_port->read_urb,
1086 serial->dev,
1087 usb_rcvbulkpipe(serial->dev,
1088 (port->bulk_in_endpointAddress) + 2),
1089 port->bulk_in_buffer,
1090 mos7840_port->read_urb->transfer_buffer_length,
1091 mos7840_bulk_in_callback, mos7840_port);
1092 } else {
1093 usb_fill_bulk_urb(mos7840_port->read_urb,
1094 serial->dev,
1095 usb_rcvbulkpipe(serial->dev,
1096 port->bulk_in_endpointAddress),
1097 port->bulk_in_buffer,
1098 mos7840_port->read_urb->transfer_buffer_length,
1099 mos7840_bulk_in_callback, mos7840_port);
1100 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001101
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001102 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001103 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001104 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1105 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001106 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1107 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001108 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001109 }
1110
1111 /* initialize our wait queues */
1112 init_waitqueue_head(&mos7840_port->wait_chase);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001113
Paul B Schroeder3f542972006-08-31 19:41:47 -05001114 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001115 /* Must set to enable ints! */
1116 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001117 /* send a open port command */
1118 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001119 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001120
Paul B Schroeder3f542972006-08-31 19:41:47 -05001121 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001122}
1123
1124/*****************************************************************************
1125 * mos7840_chars_in_buffer
1126 * this function is called by the tty driver when it wants to know how many
1127 * bytes of data we currently have outstanding in the port (data that has
1128 * been written, but hasn't made it out the port yet)
1129 * If successful, we return the number of bytes left to be written in the
1130 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001131 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001132 *****************************************************************************/
1133
Alan Cox95da3102008-07-22 11:09:07 +01001134static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001135{
Alan Cox95da3102008-07-22 11:09:07 +01001136 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001137 int i;
1138 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001139 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001140 struct moschip_port *mos7840_port;
1141
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001142 if (mos7840_port_paranoia_check(port, __func__))
Alan Cox95da3102008-07-22 11:09:07 +01001143 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001144
1145 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001146 if (mos7840_port == NULL)
Alan Cox95da3102008-07-22 11:09:07 +01001147 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001148
Alan Cox880af9d2008-07-22 11:16:12 +01001149 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell5c263b92012-07-24 14:15:13 -05001150 for (i = 0; i < NUM_URBS; ++i) {
1151 if (mos7840_port->busy[i]) {
1152 struct urb *urb = mos7840_port->write_urb_pool[i];
1153 chars += urb->transfer_buffer_length;
1154 }
1155 }
Alan Cox880af9d2008-07-22 11:16:12 +01001156 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001157 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001158 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001159
1160}
1161
Paul B Schroeder3f542972006-08-31 19:41:47 -05001162/*****************************************************************************
1163 * mos7840_close
1164 * this function is called by the tty driver when a port is closed
1165 *****************************************************************************/
1166
Alan Cox335f8512009-06-11 12:26:29 +01001167static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001168{
1169 struct usb_serial *serial;
1170 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001171 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001172 int j;
1173 __u16 Data;
1174
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001175 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001176 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001177
Harvey Harrison441b62c2008-03-03 16:08:34 -08001178 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001179 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001180 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001181
1182 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001183 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001184
Oliver Neukum0de9a702007-03-16 20:28:28 +01001185 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001186 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001187
1188 for (j = 0; j < NUM_URBS; ++j)
1189 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1190
1191 /* Freeing Write URBs */
1192 for (j = 0; j < NUM_URBS; ++j) {
1193 if (mos7840_port->write_urb_pool[j]) {
1194 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1195 kfree(mos7840_port->write_urb_pool[j]->
1196 transfer_buffer);
1197
1198 usb_free_urb(mos7840_port->write_urb_pool[j]);
1199 }
1200 }
1201
Johan Hovoldbb3529c2013-03-21 12:36:35 +01001202 usb_kill_urb(mos7840_port->write_urb);
1203 usb_kill_urb(mos7840_port->read_urb);
1204 mos7840_port->read_urb_busy = false;
1205
Oliver Neukum0de9a702007-03-16 20:28:28 +01001206 port0->open_ports--;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001207 dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001208 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001209 if (serial->port[0]->interrupt_in_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001210 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001211 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001212 }
1213 }
1214
1215 if (mos7840_port->write_urb) {
1216 /* if this urb had a transfer buffer already (old tx) free it */
Syam Sidhardhanae8d4872013-03-07 01:51:12 +05301217 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001218 usb_free_urb(mos7840_port->write_urb);
1219 }
1220
1221 Data = 0x0;
1222 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1223
1224 Data = 0x00;
1225 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1226
1227 mos7840_port->open = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001228}
1229
1230/************************************************************************
1231 *
1232 * mos7840_block_until_chase_response
1233 *
1234 * This function will block the close until one of the following:
1235 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001236 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001237 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1238 *
1239 ************************************************************************/
1240
Alan Cox95da3102008-07-22 11:09:07 +01001241static void mos7840_block_until_chase_response(struct tty_struct *tty,
1242 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001243{
Mark Ferrell1e658482012-07-24 13:38:31 -05001244 int timeout = msecs_to_jiffies(1000);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001245 int wait = 10;
1246 int count;
1247
1248 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001249 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001250
1251 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001252 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001253 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001254
1255 /* Block the thread for a while */
1256 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1257 timeout);
1258 /* No activity.. count down section */
1259 wait--;
1260 if (wait == 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001261 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001262 return;
1263 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001264 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001265 wait = 10;
1266 }
1267 }
1268
1269}
1270
1271/*****************************************************************************
1272 * mos7840_break
1273 * this function sends a break to the port
1274 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001275static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001276{
Alan Cox95da3102008-07-22 11:09:07 +01001277 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001278 unsigned char data;
1279 struct usb_serial *serial;
1280 struct moschip_port *mos7840_port;
1281
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001282 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001283 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001284
Harvey Harrison441b62c2008-03-03 16:08:34 -08001285 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001286 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001287 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001288
1289 mos7840_port = mos7840_get_port_private(port);
1290
Alan Cox880af9d2008-07-22 11:16:12 +01001291 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001292 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001293
Johan Hovoldbb3529c2013-03-21 12:36:35 +01001294 /* flush and block until tx is empty */
1295 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001296
Alan Cox95da3102008-07-22 11:09:07 +01001297 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001298 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001299 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001300 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001301
Alan Cox6b447f042009-01-02 13:48:56 +00001302 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001303 mos7840_port->shadowLCR = data;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001304 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001305 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1306 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001307}
1308
1309/*****************************************************************************
1310 * mos7840_write_room
1311 * this function is called by the tty driver when it wants to know how many
1312 * bytes of data we can accept for a specific port.
1313 * If successful, we return the amount of room that we have for this port
1314 * Otherwise we return a negative error number.
1315 *****************************************************************************/
1316
Alan Cox95da3102008-07-22 11:09:07 +01001317static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001318{
Alan Cox95da3102008-07-22 11:09:07 +01001319 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001320 int i;
1321 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001322 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001323 struct moschip_port *mos7840_port;
1324
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001325 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001326 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001327
1328 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001329 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001330 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001331
Oliver Neukum0de9a702007-03-16 20:28:28 +01001332 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001333 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001334 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001335 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001336 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001337 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001338
Oliver Neukum0de9a702007-03-16 20:28:28 +01001339 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001340 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001341 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001342
1343}
1344
1345/*****************************************************************************
1346 * mos7840_write
1347 * this function is called by the tty driver when data should be written to
1348 * the port.
1349 * If successful, we return the number of bytes written, otherwise we
1350 * return a negative error number.
1351 *****************************************************************************/
1352
Alan Cox95da3102008-07-22 11:09:07 +01001353static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001354 const unsigned char *data, int count)
1355{
1356 int status;
1357 int i;
1358 int bytes_sent = 0;
1359 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001360 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001361
1362 struct moschip_port *mos7840_port;
1363 struct usb_serial *serial;
1364 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001365 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001366 const unsigned char *current_position = data;
1367 unsigned char *data1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001368
1369#ifdef NOTMOS7840
1370 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001371 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1372 mos7840_port->shadowLCR = Data;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001373 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1374 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001375
Alan Cox880af9d2008-07-22 11:16:12 +01001376 /* Data = 0x03; */
1377 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1378 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001379
Alan Cox880af9d2008-07-22 11:16:12 +01001380 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001381 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1382
Alan Cox880af9d2008-07-22 11:16:12 +01001383 /* Data = 0x0c; */
1384 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001385 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001386 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001387 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001388
1389 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001390 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001391 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001392
1393 Data = Data & ~SERIAL_LCR_DLAB;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001394 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001395 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1396#endif
1397
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001398 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001399 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001400
1401 serial = port->serial;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001402 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001403 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001404
1405 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001406 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001407 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001408
1409 /* try to find a free urb in the list */
1410 urb = NULL;
1411
Oliver Neukum0de9a702007-03-16 20:28:28 +01001412 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001413 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001414 if (!mos7840_port->busy[i]) {
1415 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001416 urb = mos7840_port->write_urb_pool[i];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001417 dev_dbg(&port->dev, "URB:%d\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001418 break;
1419 }
1420 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001421 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001422
1423 if (urb == NULL) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001424 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001425 goto exit;
1426 }
1427
1428 if (urb->transfer_buffer == NULL) {
1429 urb->transfer_buffer =
1430 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1431
1432 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001433 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001434 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001435 goto exit;
1436 }
1437 }
1438 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1439
Al Viro97c49652006-10-09 20:29:03 +01001440 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001441
1442 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001443 if ((serial->num_ports == 2)
1444 && ((((__u16)port->number -
1445 (__u16)(port->serial->minor)) % 2) != 0)) {
1446 usb_fill_bulk_urb(urb,
1447 serial->dev,
1448 usb_sndbulkpipe(serial->dev,
1449 (port->bulk_out_endpointAddress) + 2),
1450 urb->transfer_buffer,
1451 transfer_size,
1452 mos7840_bulk_out_data_callback, mos7840_port);
1453 } else {
1454 usb_fill_bulk_urb(urb,
1455 serial->dev,
1456 usb_sndbulkpipe(serial->dev,
1457 port->bulk_out_endpointAddress),
1458 urb->transfer_buffer,
1459 transfer_size,
1460 mos7840_bulk_out_data_callback, mos7840_port);
1461 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001462
1463 data1 = urb->transfer_buffer;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001464 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001465
Donald0eafe4d2012-04-19 15:00:45 +08001466 /* Turn on LED */
1467 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1468 mos7840_port->led_flag = true;
1469 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1470 mod_timer(&mos7840_port->led_timer1,
1471 jiffies + msecs_to_jiffies(LED_ON_MS));
1472 }
1473
Paul B Schroeder3f542972006-08-31 19:41:47 -05001474 /* send it down the pipe */
1475 status = usb_submit_urb(urb, GFP_ATOMIC);
1476
1477 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001478 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001479 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001480 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001481 bytes_sent = status;
1482 goto exit;
1483 }
1484 bytes_sent = transfer_size;
Johan Hovold8c1a07f2013-03-21 12:37:16 +01001485 port->icount.tx += transfer_size;
1486 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001487exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001488 return bytes_sent;
1489
1490}
1491
1492/*****************************************************************************
1493 * mos7840_throttle
1494 * this function is called by the tty driver when it wants to stop the data
1495 * being read from the port.
1496 *****************************************************************************/
1497
Alan Cox95da3102008-07-22 11:09:07 +01001498static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001499{
Alan Cox95da3102008-07-22 11:09:07 +01001500 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001501 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001502 int status;
1503
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001504 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001505 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001506
1507 mos7840_port = mos7840_get_port_private(port);
1508
1509 if (mos7840_port == NULL)
1510 return;
1511
1512 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001513 dev_dbg(&port->dev, "%s", "port not opened\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001514 return;
1515 }
1516
Paul B Schroeder3f542972006-08-31 19:41:47 -05001517 /* if we are implementing XON/XOFF, send the stop character */
1518 if (I_IXOFF(tty)) {
1519 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001520 status = mos7840_write(tty, port, &stop_char, 1);
1521 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001522 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001523 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001524 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001525 if (tty->termios.c_cflag & CRTSCTS) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001526 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001527 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001528 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001529 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001530 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001531 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001532}
1533
1534/*****************************************************************************
1535 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001536 * this function is called by the tty driver when it wants to resume
1537 * the data being read from the port (called after mos7840_throttle is
1538 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001539 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001540static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001541{
Alan Cox95da3102008-07-22 11:09:07 +01001542 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001543 int status;
1544 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1545
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001546 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001547 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001548
1549 if (mos7840_port == NULL)
1550 return;
1551
1552 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001553 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001554 return;
1555 }
1556
Paul B Schroeder3f542972006-08-31 19:41:47 -05001557 /* if we are implementing XON/XOFF, send the start character */
1558 if (I_IXOFF(tty)) {
1559 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001560 status = mos7840_write(tty, port, &start_char, 1);
1561 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001562 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001563 }
1564
1565 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001566 if (tty->termios.c_cflag & CRTSCTS) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001567 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001568 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001569 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001570 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001571 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001572 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001573}
1574
Alan Cox60b33c12011-02-14 16:26:14 +00001575static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001576{
Alan Cox95da3102008-07-22 11:09:07 +01001577 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001578 struct moschip_port *mos7840_port;
1579 unsigned int result;
1580 __u16 msr;
1581 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001582 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001583 mos7840_port = mos7840_get_port_private(port);
1584
Paul B Schroeder3f542972006-08-31 19:41:47 -05001585 if (mos7840_port == NULL)
1586 return -ENODEV;
1587
1588 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1589 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1590 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1591 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1592 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1593 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1594 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1595 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1596 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1597
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001598 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001599
1600 return result;
1601}
1602
Alan Cox20b9d172011-02-14 16:26:50 +00001603static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001604 unsigned int set, unsigned int clear)
1605{
Alan Cox95da3102008-07-22 11:09:07 +01001606 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001607 struct moschip_port *mos7840_port;
1608 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001609 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001610
Paul B Schroeder3f542972006-08-31 19:41:47 -05001611 mos7840_port = mos7840_get_port_private(port);
1612
1613 if (mos7840_port == NULL)
1614 return -ENODEV;
1615
Alan Coxe2984492008-02-20 20:51:45 +00001616 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001617 mcr = mos7840_port->shadowMCR;
1618 if (clear & TIOCM_RTS)
1619 mcr &= ~MCR_RTS;
1620 if (clear & TIOCM_DTR)
1621 mcr &= ~MCR_DTR;
1622 if (clear & TIOCM_LOOP)
1623 mcr &= ~MCR_LOOPBACK;
1624
1625 if (set & TIOCM_RTS)
1626 mcr |= MCR_RTS;
1627 if (set & TIOCM_DTR)
1628 mcr |= MCR_DTR;
1629 if (set & TIOCM_LOOP)
1630 mcr |= MCR_LOOPBACK;
1631
1632 mos7840_port->shadowMCR = mcr;
1633
Paul B Schroeder3f542972006-08-31 19:41:47 -05001634 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1635 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001636 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
Roel Kluin87521c42008-04-17 06:16:24 +02001637 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001638 }
1639
1640 return 0;
1641}
1642
1643/*****************************************************************************
1644 * mos7840_calc_baud_rate_divisor
1645 * this function calculates the proper baud rate divisor for the specified
1646 * baud rate.
1647 *****************************************************************************/
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001648static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1649 int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001650 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001651{
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001652 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001653
1654 if (baudRate <= 115200) {
1655 *divisor = 115200 / baudRate;
1656 *clk_sel_val = 0x0;
1657 }
1658 if ((baudRate > 115200) && (baudRate <= 230400)) {
1659 *divisor = 230400 / baudRate;
1660 *clk_sel_val = 0x10;
1661 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1662 *divisor = 403200 / baudRate;
1663 *clk_sel_val = 0x20;
1664 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1665 *divisor = 460800 / baudRate;
1666 *clk_sel_val = 0x30;
1667 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1668 *divisor = 806400 / baudRate;
1669 *clk_sel_val = 0x40;
1670 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1671 *divisor = 921600 / baudRate;
1672 *clk_sel_val = 0x50;
1673 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1674 *divisor = 1572864 / baudRate;
1675 *clk_sel_val = 0x60;
1676 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1677 *divisor = 3145728 / baudRate;
1678 *clk_sel_val = 0x70;
1679 }
1680 return 0;
1681
1682#ifdef NOTMCS7840
1683
1684 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1685 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1686 *divisor = mos7840_divisor_table[i].Divisor;
1687 return 0;
1688 }
1689 }
1690
1691 /* After trying for all the standard baud rates *
1692 * Try calculating the divisor for this baud rate */
1693
1694 if (baudrate > 75 && baudrate < 230400) {
1695 /* get the divisor */
1696 custom = (__u16) (230400L / baudrate);
1697
1698 /* Check for round off */
1699 round1 = (__u16) (2304000L / baudrate);
1700 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001701 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001702 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001703 *divisor = custom;
1704
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001705 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001706 return 0;
1707 }
1708
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001709 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001710 return -1;
1711#endif
1712}
1713
1714/*****************************************************************************
1715 * mos7840_send_cmd_write_baud_rate
1716 * this function sends the proper command to change the baud rate of the
1717 * specified port.
1718 *****************************************************************************/
1719
1720static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1721 int baudRate)
1722{
1723 int divisor = 0;
1724 int status;
1725 __u16 Data;
1726 unsigned char number;
1727 __u16 clk_sel_val;
1728 struct usb_serial_port *port;
1729
1730 if (mos7840_port == NULL)
1731 return -1;
1732
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001733 port = mos7840_port->port;
1734 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001735 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001736
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001737 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001738 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001739
Paul B Schroeder3f542972006-08-31 19:41:47 -05001740 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1741
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001742 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1743 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001744 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001745 if (baudRate > 115200) {
1746#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001747 /* NOTE: need to see the pther register to modify */
1748 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001749 Data = 0x2b;
1750 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001751 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1752 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001753 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001754 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001755 return -1;
1756 }
1757#endif
1758
1759 } else {
1760#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001761 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001762 Data = 0xb;
1763 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001764 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1765 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001766 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001767 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001768 return -1;
1769 }
1770#endif
1771
1772 }
1773
Alan Cox880af9d2008-07-22 11:16:12 +01001774 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001775 clk_sel_val = 0x0;
1776 Data = 0x0;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001777 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001778 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001779 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1780 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001781 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001782 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001783 return -1;
1784 }
1785 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001786 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1787 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001788 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001789 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001790 return -1;
1791 }
1792 /* Calculate the Divisor */
1793
1794 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001795 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001796 return status;
1797 }
1798 /* Enable access to divisor latch */
1799 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1800 mos7840_port->shadowLCR = Data;
1801 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1802
1803 /* Write the divisor */
1804 Data = (unsigned char)(divisor & 0xff);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001805 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001806 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1807
1808 Data = (unsigned char)((divisor & 0xff00) >> 8);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001809 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001810 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1811
1812 /* Disable access to divisor latch */
1813 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1814 mos7840_port->shadowLCR = Data;
1815 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1816
1817 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001818 return status;
1819}
1820
1821/*****************************************************************************
1822 * mos7840_change_port_settings
1823 * This routine is called to set the UART on the device to match
1824 * the specified new settings.
1825 *****************************************************************************/
1826
Alan Cox95da3102008-07-22 11:09:07 +01001827static void mos7840_change_port_settings(struct tty_struct *tty,
1828 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001829{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001830 int baud;
1831 unsigned cflag;
1832 unsigned iflag;
1833 __u8 lData;
1834 __u8 lParity;
1835 __u8 lStop;
1836 int status;
1837 __u16 Data;
1838 struct usb_serial_port *port;
1839 struct usb_serial *serial;
1840
1841 if (mos7840_port == NULL)
1842 return;
1843
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001844 port = mos7840_port->port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001845
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001846 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001847 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001848
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001849 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001850 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001851
1852 serial = port->serial;
1853
Paul B Schroeder3f542972006-08-31 19:41:47 -05001854 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001855 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001856 return;
1857 }
1858
Paul B Schroeder3f542972006-08-31 19:41:47 -05001859 lData = LCR_BITS_8;
1860 lStop = LCR_STOP_1;
1861 lParity = LCR_PAR_NONE;
1862
Alan Coxadc8d742012-07-14 15:31:47 +01001863 cflag = tty->termios.c_cflag;
1864 iflag = tty->termios.c_iflag;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001865
1866 /* Change the number of bits */
1867 if (cflag & CSIZE) {
1868 switch (cflag & CSIZE) {
1869 case CS5:
1870 lData = LCR_BITS_5;
1871 break;
1872
1873 case CS6:
1874 lData = LCR_BITS_6;
1875 break;
1876
1877 case CS7:
1878 lData = LCR_BITS_7;
1879 break;
1880 default:
1881 case CS8:
1882 lData = LCR_BITS_8;
1883 break;
1884 }
1885 }
1886 /* Change the Parity bit */
1887 if (cflag & PARENB) {
1888 if (cflag & PARODD) {
1889 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001890 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001891 } else {
1892 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001893 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001894 }
1895
1896 } else {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001897 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001898 }
1899
Alan Cox880af9d2008-07-22 11:16:12 +01001900 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001901 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001902
1903 /* Change the Stop bit */
1904 if (cflag & CSTOPB) {
1905 lStop = LCR_STOP_2;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001906 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001907 } else {
1908 lStop = LCR_STOP_1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001909 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001910 }
1911
1912 /* Update the LCR with the correct value */
1913 mos7840_port->shadowLCR &=
1914 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1915 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1916
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001917 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1918 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001919 /* Disable Interrupts */
1920 Data = 0x00;
1921 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1922
1923 Data = 0x00;
1924 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1925
1926 Data = 0xcf;
1927 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1928
1929 /* Send the updated LCR value to the mos7840 */
1930 Data = mos7840_port->shadowLCR;
1931
1932 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1933
1934 Data = 0x00b;
1935 mos7840_port->shadowMCR = Data;
1936 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1937 Data = 0x00b;
1938 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1939
1940 /* set up the MCR register and send it to the mos7840 */
1941
1942 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01001943 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001944 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001945
Alan Cox880af9d2008-07-22 11:16:12 +01001946 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001947 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01001948 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001949 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001950
1951 Data = mos7840_port->shadowMCR;
1952 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1953
1954 /* Determine divisor based on baud rate */
1955 baud = tty_get_baud_rate(tty);
1956
1957 if (!baud) {
1958 /* pick a default, any default... */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001959 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001960 baud = 9600;
1961 }
1962
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001963 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001964 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1965
1966 /* Enable Interrupts */
1967 Data = 0x0c;
1968 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1969
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001970 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001971 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001972 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001973 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001974 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001975 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001976 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001977 }
1978 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001979 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1980 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001981}
1982
1983/*****************************************************************************
1984 * mos7840_set_termios
1985 * this function is called by the tty driver when it wants to change
1986 * the termios structure
1987 *****************************************************************************/
1988
Alan Cox95da3102008-07-22 11:09:07 +01001989static void mos7840_set_termios(struct tty_struct *tty,
1990 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001991 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001992{
1993 int status;
1994 unsigned int cflag;
1995 struct usb_serial *serial;
1996 struct moschip_port *mos7840_port;
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001997
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001998 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001999 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002000
2001 serial = port->serial;
2002
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002003 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002004 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002005
2006 mos7840_port = mos7840_get_port_private(port);
2007
2008 if (mos7840_port == NULL)
2009 return;
2010
Paul B Schroeder3f542972006-08-31 19:41:47 -05002011 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002012 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002013 return;
2014 }
2015
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002016 dev_dbg(&port->dev, "%s", "setting termios - \n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002017
Alan Coxadc8d742012-07-14 15:31:47 +01002018 cflag = tty->termios.c_cflag;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002019
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002020 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07002021 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002022 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2023 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2024 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002025
2026 /* change the port settings to the new ones specified */
2027
Alan Cox95da3102008-07-22 11:09:07 +01002028 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002029
2030 if (!mos7840_port->read_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002031 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002032 return;
2033 }
2034
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002035 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002036 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002037 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2038 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002039 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002040 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002041 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002042 }
2043 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002044}
2045
2046/*****************************************************************************
2047 * mos7840_get_lsr_info - get line status register info
2048 *
2049 * Purpose: Let user call ioctl() to get info when the UART physically
2050 * is emptied. On bus types like RS485, the transmitter must
2051 * release the bus after transmitting. This must be done when
2052 * the transmit shift register is empty, not be done when the
2053 * transmit holding register is empty. This functionality
2054 * allows an RS485 driver to be written in user space.
2055 *****************************************************************************/
2056
Alan Cox95da3102008-07-22 11:09:07 +01002057static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002058 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002059{
2060 int count;
2061 unsigned int result = 0;
2062
Alan Cox95da3102008-07-22 11:09:07 +01002063 count = mos7840_chars_in_buffer(tty);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002064 if (count == 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002065 result = TIOCSER_TEMT;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002066
2067 if (copy_to_user(value, &result, sizeof(int)))
2068 return -EFAULT;
2069 return 0;
2070}
2071
2072/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002073 * mos7840_get_serial_info
2074 * function to get information about serial port
2075 *****************************************************************************/
2076
2077static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002078 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002079{
2080 struct serial_struct tmp;
2081
2082 if (mos7840_port == NULL)
2083 return -1;
2084
2085 if (!retinfo)
2086 return -EFAULT;
2087
2088 memset(&tmp, 0, sizeof(tmp));
2089
2090 tmp.type = PORT_16550A;
2091 tmp.line = mos7840_port->port->serial->minor;
2092 tmp.port = mos7840_port->port->number;
2093 tmp.irq = 0;
2094 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2095 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2096 tmp.baud_base = 9600;
2097 tmp.close_delay = 5 * HZ;
2098 tmp.closing_wait = 30 * HZ;
2099
2100 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2101 return -EFAULT;
2102 return 0;
2103}
2104
2105/*****************************************************************************
2106 * SerialIoctl
2107 * this function handles any ioctl calls to the driver
2108 *****************************************************************************/
2109
Alan Cox00a0d0d2011-02-14 16:27:06 +00002110static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002111 unsigned int cmd, unsigned long arg)
2112{
Alan Cox95da3102008-07-22 11:09:07 +01002113 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002114 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002115 struct moschip_port *mos7840_port;
Johan Hovoldc9fac852013-03-21 12:37:15 +01002116 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002117 struct async_icount cnow;
2118 struct async_icount cprev;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002119
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002120 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002121 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002122
2123 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002124
2125 if (mos7840_port == NULL)
2126 return -1;
2127
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002128 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002129
2130 switch (cmd) {
2131 /* return number of bytes available */
2132
Paul B Schroeder3f542972006-08-31 19:41:47 -05002133 case TIOCSERGETLSR:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002134 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01002135 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002136
Paul B Schroeder3f542972006-08-31 19:41:47 -05002137 case TIOCGSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002138 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Al Viro97c49652006-10-09 20:29:03 +01002139 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002140
2141 case TIOCSSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002142 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002143 break;
2144
2145 case TIOCMIWAIT:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002146 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
Johan Hovoldc9fac852013-03-21 12:37:15 +01002147 spin_lock_irqsave(&port->lock, flags);
Johan Hovold8c1a07f2013-03-21 12:37:16 +01002148 cprev = port->icount;
Johan Hovoldc9fac852013-03-21 12:37:15 +01002149 spin_unlock_irqrestore(&port->lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002150 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002151 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002152 mos7840_port->delta_msr_cond = 0;
Johan Hovolda14430d2013-03-19 09:21:20 +01002153 wait_event_interruptible(port->delta_msr_wait,
2154 (port->serial->disconnected ||
2155 mos7840_port->
Paul B Schroeder3f542972006-08-31 19:41:47 -05002156 delta_msr_cond == 1));
2157
2158 /* see if a signal did it */
2159 if (signal_pending(current))
2160 return -ERESTARTSYS;
Johan Hovolda14430d2013-03-19 09:21:20 +01002161
2162 if (port->serial->disconnected)
2163 return -EIO;
2164
Johan Hovoldc9fac852013-03-21 12:37:15 +01002165 spin_lock_irqsave(&port->lock, flags);
Johan Hovold8c1a07f2013-03-21 12:37:16 +01002166 cnow = port->icount;
Johan Hovoldc9fac852013-03-21 12:37:15 +01002167 spin_unlock_irqrestore(&port->lock, flags);
2168
Paul B Schroeder3f542972006-08-31 19:41:47 -05002169 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2170 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2171 return -EIO; /* no change => error */
Johan Hovoldc9fac852013-03-21 12:37:15 +01002172
Paul B Schroeder3f542972006-08-31 19:41:47 -05002173 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2174 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2175 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2176 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2177 return 0;
2178 }
2179 cprev = cnow;
2180 }
2181 /* NOTREACHED */
2182 break;
2183
Paul B Schroeder3f542972006-08-31 19:41:47 -05002184 default:
2185 break;
2186 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002187 return -ENOIOCTLCMD;
2188}
2189
Donald0eafe4d2012-04-19 15:00:45 +08002190static int mos7810_check(struct usb_serial *serial)
2191{
2192 int i, pass_count = 0;
2193 __u16 data = 0, mcr_data = 0;
2194 __u16 test_pattern = 0x55AA;
2195
2196 /* Store MCR setting */
2197 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2198 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2199 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2200
2201 for (i = 0; i < 16; i++) {
2202 /* Send the 1-bit test pattern out to MCS7810 test pin */
2203 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2204 MCS_WRREQ, MCS_WR_RTYPE,
2205 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2206 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2207
2208 /* Read the test pattern back */
2209 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2210 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2211 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2212
2213 /* If this is a MCS7810 device, both test patterns must match */
2214 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2215 break;
2216
2217 pass_count++;
2218 }
2219
2220 /* Restore MCR setting */
2221 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2222 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2223 0, MOS_WDR_TIMEOUT);
2224
2225 if (pass_count == 16)
2226 return 1;
2227
2228 return 0;
2229}
2230
Paul B Schroeder3f542972006-08-31 19:41:47 -05002231static int mos7840_calc_num_ports(struct usb_serial *serial)
2232{
Donald0eafe4d2012-04-19 15:00:45 +08002233 __u16 data = 0x00;
Donald Lee093ea2d2012-03-14 15:26:33 +08002234 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002235
Donald0eafe4d2012-04-19 15:00:45 +08002236 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2237 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
Donald Lee093ea2d2012-03-14 15:26:33 +08002238 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2239
Donald0eafe4d2012-04-19 15:00:45 +08002240 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2241 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2242 device_type = serial->dev->descriptor.idProduct;
Donald Lee093ea2d2012-03-14 15:26:33 +08002243 } else {
Donald0eafe4d2012-04-19 15:00:45 +08002244 /* For a MCS7840 device GPIO0 must be set to 1 */
2245 if ((data & 0x01) == 1)
2246 device_type = MOSCHIP_DEVICE_ID_7840;
2247 else if (mos7810_check(serial))
2248 device_type = MOSCHIP_DEVICE_ID_7810;
2249 else
2250 device_type = MOSCHIP_DEVICE_ID_7820;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002251 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002252
Donald0eafe4d2012-04-19 15:00:45 +08002253 mos7840_num_ports = (device_type >> 4) & 0x000F;
2254 serial->num_bulk_in = mos7840_num_ports;
2255 serial->num_bulk_out = mos7840_num_ports;
2256 serial->num_ports = mos7840_num_ports;
2257
Paul B Schroeder3f542972006-08-31 19:41:47 -05002258 return mos7840_num_ports;
2259}
2260
Johan Hovold80c00752012-10-25 18:56:34 +02002261static int mos7840_port_probe(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002262{
Johan Hovold80c00752012-10-25 18:56:34 +02002263 struct usb_serial *serial = port->serial;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002264 struct moschip_port *mos7840_port;
Johan Hovold80c00752012-10-25 18:56:34 +02002265 int status;
2266 int pnum;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002267 __u16 Data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002268
Paul B Schroeder3f542972006-08-31 19:41:47 -05002269 /* we set up the pointers to the endpoints in the mos7840_open *
2270 * function, as the structures aren't created yet. */
2271
Johan Hovold80c00752012-10-25 18:56:34 +02002272 pnum = port->number - serial->minor;
2273
Johan Hovoldae685ef2012-10-25 18:56:35 +02002274 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2275 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2276 if (mos7840_port == NULL) {
2277 dev_err(&port->dev, "%s - Out of memory\n", __func__);
2278 return -ENOMEM;
2279 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002280
Johan Hovoldae685ef2012-10-25 18:56:35 +02002281 /* Initialize all port interrupt end point to port 0 int
2282 * endpoint. Our device has only one interrupt end point
2283 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002284
Johan Hovoldae685ef2012-10-25 18:56:35 +02002285 mos7840_port->port = port;
2286 mos7840_set_port_private(port, mos7840_port);
2287 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002288
Johan Hovoldae685ef2012-10-25 18:56:35 +02002289 /* minor is not initialised until later by
2290 * usb-serial.c:get_free_serial() and cannot therefore be used
2291 * to index device instances */
2292 mos7840_port->port_num = pnum + 1;
2293 dev_dbg(&port->dev, "port->number = %d\n", port->number);
2294 dev_dbg(&port->dev, "port->serial->minor = %d\n", port->serial->minor);
2295 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2296 dev_dbg(&port->dev, "serial->minor = %d\n", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002297
Johan Hovoldae685ef2012-10-25 18:56:35 +02002298 if (mos7840_port->port_num == 1) {
2299 mos7840_port->SpRegOffset = 0x0;
2300 mos7840_port->ControlRegOffset = 0x1;
2301 mos7840_port->DcrRegOffset = 0x4;
2302 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2303 mos7840_port->SpRegOffset = 0x8;
2304 mos7840_port->ControlRegOffset = 0x9;
2305 mos7840_port->DcrRegOffset = 0x16;
2306 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2307 mos7840_port->SpRegOffset = 0xa;
2308 mos7840_port->ControlRegOffset = 0xb;
2309 mos7840_port->DcrRegOffset = 0x19;
2310 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2311 mos7840_port->SpRegOffset = 0xa;
2312 mos7840_port->ControlRegOffset = 0xb;
2313 mos7840_port->DcrRegOffset = 0x19;
2314 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2315 mos7840_port->SpRegOffset = 0xc;
2316 mos7840_port->ControlRegOffset = 0xd;
2317 mos7840_port->DcrRegOffset = 0x1c;
2318 }
2319 mos7840_dump_serial_port(port, mos7840_port);
2320 mos7840_set_port_private(port, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002321
Johan Hovoldae685ef2012-10-25 18:56:35 +02002322 /* enable rx_disable bit in control register */
2323 status = mos7840_get_reg_sync(port,
2324 mos7840_port->ControlRegOffset, &Data);
2325 if (status < 0) {
2326 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2327 goto out;
2328 } else
2329 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2330 Data |= 0x08; /* setting driver done bit */
2331 Data |= 0x04; /* sp1_bit to have cts change reflect in
2332 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002333
Johan Hovoldae685ef2012-10-25 18:56:35 +02002334 /* Data |= 0x20; //rx_disable bit */
2335 status = mos7840_set_reg_sync(port,
2336 mos7840_port->ControlRegOffset, Data);
2337 if (status < 0) {
2338 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2339 goto out;
2340 } else
2341 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2342
2343 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2344 and 0x24 in DCR3 */
2345 Data = 0x01;
2346 status = mos7840_set_reg_sync(port,
2347 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2348 if (status < 0) {
2349 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2350 goto out;
2351 } else
2352 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2353
2354 Data = 0x05;
2355 status = mos7840_set_reg_sync(port,
2356 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2357 if (status < 0) {
2358 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2359 goto out;
2360 } else
2361 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2362
2363 Data = 0x24;
2364 status = mos7840_set_reg_sync(port,
2365 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2366 if (status < 0) {
2367 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2368 goto out;
2369 } else
2370 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2371
2372 /* write values in clkstart0x0 and clkmulti 0x20 */
2373 Data = 0x0;
2374 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2375 if (status < 0) {
2376 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2377 goto out;
2378 } else
2379 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2380
2381 Data = 0x20;
2382 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2383 if (status < 0) {
2384 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2385 goto error;
2386 } else
2387 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2388
2389 /* write value 0x0 to scratchpad register */
2390 Data = 0x00;
2391 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2392 if (status < 0) {
2393 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2394 goto out;
2395 } else
2396 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2397
2398 /* Zero Length flag register */
2399 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2400 Data = 0xff;
Johan Hovold80c00752012-10-25 18:56:34 +02002401 status = mos7840_set_reg_sync(port,
Johan Hovoldae685ef2012-10-25 18:56:35 +02002402 (__u16) (ZLP_REG1 +
2403 ((__u16)mos7840_port->port_num)), Data);
2404 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002405 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002406 if (status < 0) {
2407 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2408 goto out;
2409 } else
2410 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2411 } else {
2412 Data = 0xff;
2413 status = mos7840_set_reg_sync(port,
2414 (__u16) (ZLP_REG1 +
2415 ((__u16)mos7840_port->port_num) - 0x1), Data);
2416 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002417 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002418 if (status < 0) {
2419 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2420 goto out;
2421 } else
2422 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002423
Johan Hovoldae685ef2012-10-25 18:56:35 +02002424 }
2425 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2426 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2427 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2428 GFP_KERNEL);
2429 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2430 !mos7840_port->dr) {
2431 status = -ENOMEM;
2432 goto error;
2433 }
Donald0eafe4d2012-04-19 15:00:45 +08002434
Johan Hovoldae685ef2012-10-25 18:56:35 +02002435 mos7840_port->has_led = false;
Donald0eafe4d2012-04-19 15:00:45 +08002436
Johan Hovoldae685ef2012-10-25 18:56:35 +02002437 /* Initialize LED timers */
2438 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2439 mos7840_port->has_led = true;
Donald0eafe4d2012-04-19 15:00:45 +08002440
Johan Hovoldae685ef2012-10-25 18:56:35 +02002441 init_timer(&mos7840_port->led_timer1);
2442 mos7840_port->led_timer1.function = mos7840_led_off;
2443 mos7840_port->led_timer1.expires =
2444 jiffies + msecs_to_jiffies(LED_ON_MS);
2445 mos7840_port->led_timer1.data = (unsigned long)mos7840_port;
Donald0eafe4d2012-04-19 15:00:45 +08002446
Johan Hovoldae685ef2012-10-25 18:56:35 +02002447 init_timer(&mos7840_port->led_timer2);
2448 mos7840_port->led_timer2.function = mos7840_led_flag_off;
2449 mos7840_port->led_timer2.expires =
2450 jiffies + msecs_to_jiffies(LED_OFF_MS);
2451 mos7840_port->led_timer2.data = (unsigned long)mos7840_port;
Donald0eafe4d2012-04-19 15:00:45 +08002452
Johan Hovoldae685ef2012-10-25 18:56:35 +02002453 mos7840_port->led_flag = false;
Donald0eafe4d2012-04-19 15:00:45 +08002454
Johan Hovoldae685ef2012-10-25 18:56:35 +02002455 /* Turn off LED */
2456 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2457 }
2458out:
Johan Hovold80c00752012-10-25 18:56:34 +02002459 if (pnum == serial->num_ports - 1) {
2460 /* Zero Length flag enable */
2461 Data = 0x0f;
2462 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2463 if (status < 0) {
2464 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2465 goto error;
2466 } else
2467 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2468
2469 /* setting configuration feature to one */
2470 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2471 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2472 MOS_WDR_TIMEOUT);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002473 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002474 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002475error:
Johan Hovold80c00752012-10-25 18:56:34 +02002476 kfree(mos7840_port->dr);
2477 kfree(mos7840_port->ctrl_buf);
2478 usb_free_urb(mos7840_port->control_urb);
2479 kfree(mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002480
Oliver Neukum0de9a702007-03-16 20:28:28 +01002481 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002482}
2483
Johan Hovold80c00752012-10-25 18:56:34 +02002484static int mos7840_port_remove(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002485{
Paul B Schroeder3f542972006-08-31 19:41:47 -05002486 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002487
Johan Hovold80c00752012-10-25 18:56:34 +02002488 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002489
Johan Hovold80c00752012-10-25 18:56:34 +02002490 if (mos7840_port->has_led) {
2491 /* Turn off LED */
2492 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002493
Johan Hovold80c00752012-10-25 18:56:34 +02002494 del_timer_sync(&mos7840_port->led_timer1);
2495 del_timer_sync(&mos7840_port->led_timer2);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002496 }
Johan Hovold80c00752012-10-25 18:56:34 +02002497 usb_kill_urb(mos7840_port->control_urb);
2498 usb_free_urb(mos7840_port->control_urb);
2499 kfree(mos7840_port->ctrl_buf);
2500 kfree(mos7840_port->dr);
2501 kfree(mos7840_port);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002502
Johan Hovold80c00752012-10-25 18:56:34 +02002503 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002504}
2505
2506static struct usb_serial_driver moschip7840_4port_device = {
2507 .driver = {
2508 .owner = THIS_MODULE,
2509 .name = "mos7840",
2510 },
2511 .description = DRIVER_DESC,
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002512 .id_table = id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002513 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002514 .open = mos7840_open,
2515 .close = mos7840_close,
2516 .write = mos7840_write,
2517 .write_room = mos7840_write_room,
2518 .chars_in_buffer = mos7840_chars_in_buffer,
2519 .throttle = mos7840_throttle,
2520 .unthrottle = mos7840_unthrottle,
2521 .calc_num_ports = mos7840_calc_num_ports,
2522#ifdef MCSSerialProbe
2523 .probe = mos7840_serial_probe,
2524#endif
2525 .ioctl = mos7840_ioctl,
2526 .set_termios = mos7840_set_termios,
2527 .break_ctl = mos7840_break,
2528 .tiocmget = mos7840_tiocmget,
2529 .tiocmset = mos7840_tiocmset,
Johan Hovold8c1a07f2013-03-21 12:37:16 +01002530 .get_icount = usb_serial_generic_get_icount,
Johan Hovold80c00752012-10-25 18:56:34 +02002531 .port_probe = mos7840_port_probe,
2532 .port_remove = mos7840_port_remove,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002533 .read_bulk_callback = mos7840_bulk_in_callback,
2534 .read_int_callback = mos7840_interrupt_callback,
2535};
2536
Alan Stern4d2a7af2012-02-23 14:57:09 -05002537static struct usb_serial_driver * const serial_drivers[] = {
2538 &moschip7840_4port_device, NULL
2539};
2540
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002541module_usb_serial_driver(serial_drivers, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002542
Paul B Schroeder3f542972006-08-31 19:41:47 -05002543MODULE_DESCRIPTION(DRIVER_DESC);
2544MODULE_LICENSE("GPL");