blob: d06013033def5f48f1e11f5d60a12b3dfb230551 [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
Johan Hovoldd614a642013-07-26 11:55:17 +0200186enum mos7840_flag {
187 MOS7840_FLAG_CTRL_BUSY,
Johan Hovold62a2cfc2013-07-26 11:55:19 +0200188 MOS7840_FLAG_LED_BUSY,
Johan Hovoldd614a642013-07-26 11:55:17 +0200189};
190
Tony Zelenoffb9c87662012-06-05 17:58:04 +0400191static const struct usb_device_id id_table[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500192 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Donald0eafe4d2012-04-19 15:00:45 +0800194 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930208 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000209 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500210 {} /* terminating entry */
211};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700212MODULE_DEVICE_TABLE(usb, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500213
214/* This structure holds all of the local port information */
215
216struct moschip_port {
217 int port_num; /*Actual port number in the device(1,2,etc) */
218 struct urb *write_urb; /* write URB for this port */
219 struct urb *read_urb; /* read URB for this port */
220 __u8 shadowLCR; /* last LCR value received */
221 __u8 shadowMCR; /* last MCR value received */
222 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100223 char open_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500224 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500225 struct usb_serial_port *port; /* loop back to the owner of this object */
226
Alan Cox880af9d2008-07-22 11:16:12 +0100227 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500228 __u8 SpRegOffset;
229 __u8 ControlRegOffset;
230 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100231 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500232 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100233 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500234 char *ctrl_buf;
235 int MsrLsr;
236
Oliver Neukum0de9a702007-03-16 20:28:28 +0100237 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500238 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100239 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800240 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500241
Donald0eafe4d2012-04-19 15:00:45 +0800242 /* For device(s) with LED indicator */
243 bool has_led;
Donald0eafe4d2012-04-19 15:00:45 +0800244 struct timer_list led_timer1; /* Timer for LED on */
245 struct timer_list led_timer2; /* Timer for LED off */
Johan Hovold62a2cfc2013-07-26 11:55:19 +0200246 struct urb *led_urb;
247 struct usb_ctrlrequest *led_dr;
Johan Hovoldd614a642013-07-26 11:55:17 +0200248
249 unsigned long flags;
Donald0eafe4d2012-04-19 15:00:45 +0800250};
Paul B Schroeder3f542972006-08-31 19:41:47 -0500251
Paul B Schroeder3f542972006-08-31 19:41:47 -0500252/*
253 * mos7840_set_reg_sync
254 * To set the Control register by calling usb_fill_control_urb function
255 * by passing usb_sndctrlpipe function as parameter.
256 */
257
258static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
259 __u16 val)
260{
261 struct usb_device *dev = port->serial->dev;
262 val = val & 0x00ff;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700263 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500264
265 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
266 MCS_WR_RTYPE, val, reg, NULL, 0,
267 MOS_WDR_TIMEOUT);
268}
269
270/*
271 * mos7840_get_reg_sync
272 * To set the Uart register by calling usb_fill_control_urb function by
273 * passing usb_rcvctrlpipe function as parameter.
274 */
275
276static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100277 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500278{
279 struct usb_device *dev = port->serial->dev;
280 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100281 u8 *buf;
282
283 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
284 if (!buf)
285 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500286
287 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100288 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500289 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100290 *val = buf[0];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700291 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100292
293 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500294 return ret;
295}
296
297/*
298 * mos7840_set_uart_reg
299 * To set the Uart register by calling usb_fill_control_urb function by
300 * passing usb_sndctrlpipe function as parameter.
301 */
302
303static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
304 __u16 val)
305{
306
307 struct usb_device *dev = port->serial->dev;
308 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100309 /* For the UART control registers, the application number need
310 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100311 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100312 val |= (((__u16) port->number -
313 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500314 } else {
315 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100316 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500317 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500318 } else {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700319 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500320 (__u16) (port->serial->minor)) + 2) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500321 }
322 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700323 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500324 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
325 MCS_WR_RTYPE, val, reg, NULL, 0,
326 MOS_WDR_TIMEOUT);
327
328}
329
330/*
331 * mos7840_get_uart_reg
332 * To set the Control register by calling usb_fill_control_urb function
333 * by passing usb_rcvctrlpipe function as parameter.
334 */
335static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100336 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500337{
338 struct usb_device *dev = port->serial->dev;
339 int ret = 0;
340 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100341 u8 *buf;
342
343 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
344 if (!buf)
345 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500346
Alan Cox880af9d2008-07-22 11:16:12 +0100347 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100348 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500349 Wval =
350 (((__u16) port->number - (__u16) (port->serial->minor)) +
351 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500352 } else {
353 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100354 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500355 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500356 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100357 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500358 (__u16) (port->serial->minor)) + 2) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500359 }
360 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700361 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500362 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100363 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500364 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100365 *val = buf[0];
366
367 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500368 return ret;
369}
370
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700371static void mos7840_dump_serial_port(struct usb_serial_port *port,
372 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500373{
374
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700375 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
376 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
377 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500378
379}
380
381/************************************************************************/
382/************************************************************************/
383/* I N T E R F A C E F U N C T I O N S */
384/* I N T E R F A C E F U N C T I O N S */
385/************************************************************************/
386/************************************************************************/
387
388static inline void mos7840_set_port_private(struct usb_serial_port *port,
389 struct moschip_port *data)
390{
391 usb_set_serial_port_data(port, (void *)data);
392}
393
394static inline struct moschip_port *mos7840_get_port_private(struct
395 usb_serial_port
396 *port)
397{
398 return (struct moschip_port *)usb_get_serial_port_data(port);
399}
400
Oliver Neukum0de9a702007-03-16 20:28:28 +0100401static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500402{
403 struct moschip_port *mos7840_port;
404 struct async_icount *icount;
405 mos7840_port = port;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500406 if (new_msr &
407 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
408 MOS_MSR_DELTA_CD)) {
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100409 icount = &mos7840_port->port->icount;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500410
411 /* update input line counters */
Johan Hovoldc9fac852013-03-21 12:37:15 +0100412 if (new_msr & MOS_MSR_DELTA_CTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500413 icount->cts++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100414 if (new_msr & MOS_MSR_DELTA_DSR)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500415 icount->dsr++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100416 if (new_msr & MOS_MSR_DELTA_CD)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500417 icount->dcd++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100418 if (new_msr & MOS_MSR_DELTA_RI)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500419 icount->rng++;
Johan Hovolde670c6a2013-03-19 09:21:19 +0100420
Johan Hovold0c6133712013-03-21 12:37:17 +0100421 wake_up_interruptible(&port->port->port.delta_msr_wait);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500422 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500423}
424
Oliver Neukum0de9a702007-03-16 20:28:28 +0100425static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500426{
427 struct async_icount *icount;
428
Paul B Schroeder3f542972006-08-31 19:41:47 -0500429 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100430 /*
431 * Parity and Framing errors only count if they
432 * occur exclusive of a break being
433 * received.
434 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500435 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
436 }
437
438 /* update input line counters */
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100439 icount = &port->port->icount;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100440 if (new_lsr & SERIAL_LSR_BI)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500441 icount->brk++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100442 if (new_lsr & SERIAL_LSR_OE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500443 icount->overrun++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100444 if (new_lsr & SERIAL_LSR_PE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500445 icount->parity++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100446 if (new_lsr & SERIAL_LSR_FE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500447 icount->frame++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500448}
449
450/************************************************************************/
451/************************************************************************/
452/* U S B C A L L B A C K F U N C T I O N S */
453/* U S B C A L L B A C K F U N C T I O N S */
454/************************************************************************/
455/************************************************************************/
456
David Howells7d12e782006-10-05 14:55:46 +0100457static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500458{
459 unsigned char *data;
460 struct moschip_port *mos7840_port;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700461 struct device *dev = &urb->dev->dev;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500462 __u8 regval = 0x0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700463 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500464
Ming Leicdc97792008-02-24 18:41:47 +0800465 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100466
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700467 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500468 case 0:
469 /* success */
470 break;
471 case -ECONNRESET:
472 case -ENOENT:
473 case -ESHUTDOWN:
474 /* this urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700475 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Johan Hovoldd614a642013-07-26 11:55:17 +0200476 goto out;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500477 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700478 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Johan Hovoldd614a642013-07-26 11:55:17 +0200479 goto out;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500480 }
481
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700482 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
483 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
484 mos7840_port->MsrLsr, mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500485 data = urb->transfer_buffer;
486 regval = (__u8) data[0];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700487 dev_dbg(dev, "%s data is %x\n", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500488 if (mos7840_port->MsrLsr == 0)
489 mos7840_handle_new_msr(mos7840_port, regval);
490 else if (mos7840_port->MsrLsr == 1)
491 mos7840_handle_new_lsr(mos7840_port, regval);
Johan Hovoldd614a642013-07-26 11:55:17 +0200492out:
493 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500494}
495
496static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100497 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500498{
499 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100500 struct usb_ctrlrequest *dr = mcs->dr;
501 unsigned char *buffer = mcs->ctrl_buf;
502 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500503
Johan Hovoldd614a642013-07-26 11:55:17 +0200504 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
505 return -EBUSY;
506
Paul B Schroeder3f542972006-08-31 19:41:47 -0500507 dr->bRequestType = MCS_RD_RTYPE;
508 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100509 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500510 dr->wIndex = cpu_to_le16(reg);
511 dr->wLength = cpu_to_le16(2);
512
513 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
514 (unsigned char *)dr, buffer, 2,
515 mos7840_control_callback, mcs);
516 mcs->control_urb->transfer_buffer_length = 2;
517 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
Johan Hovoldd614a642013-07-26 11:55:17 +0200518 if (ret)
519 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
520
Paul B Schroeder3f542972006-08-31 19:41:47 -0500521 return ret;
522}
523
Donald0eafe4d2012-04-19 15:00:45 +0800524static void mos7840_set_led_callback(struct urb *urb)
525{
526 switch (urb->status) {
527 case 0:
528 /* Success */
529 break;
530 case -ECONNRESET:
531 case -ENOENT:
532 case -ESHUTDOWN:
533 /* This urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700534 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
535 __func__, urb->status);
Donald0eafe4d2012-04-19 15:00:45 +0800536 break;
537 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700538 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
539 __func__, urb->status);
Donald0eafe4d2012-04-19 15:00:45 +0800540 }
541}
542
543static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
544 __u16 reg)
545{
546 struct usb_device *dev = mcs->port->serial->dev;
Johan Hovold62a2cfc2013-07-26 11:55:19 +0200547 struct usb_ctrlrequest *dr = mcs->led_dr;
Donald0eafe4d2012-04-19 15:00:45 +0800548
549 dr->bRequestType = MCS_WR_RTYPE;
550 dr->bRequest = MCS_WRREQ;
551 dr->wValue = cpu_to_le16(wval);
552 dr->wIndex = cpu_to_le16(reg);
553 dr->wLength = cpu_to_le16(0);
554
Johan Hovold62a2cfc2013-07-26 11:55:19 +0200555 usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
Donald0eafe4d2012-04-19 15:00:45 +0800556 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
557
Johan Hovold62a2cfc2013-07-26 11:55:19 +0200558 usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
Donald0eafe4d2012-04-19 15:00:45 +0800559}
560
561static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
562 __u16 val)
563{
564 struct usb_device *dev = port->serial->dev;
565
566 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
567 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
568}
569
570static void mos7840_led_off(unsigned long arg)
571{
572 struct moschip_port *mcs = (struct moschip_port *) arg;
573
574 /* Turn off LED */
575 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
576 mod_timer(&mcs->led_timer2,
577 jiffies + msecs_to_jiffies(LED_OFF_MS));
578}
579
580static void mos7840_led_flag_off(unsigned long arg)
581{
582 struct moschip_port *mcs = (struct moschip_port *) arg;
583
Johan Hovold62a2cfc2013-07-26 11:55:19 +0200584 clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
585}
586
587static void mos7840_led_activity(struct usb_serial_port *port)
588{
589 struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
590
591 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
592 return;
593
594 mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
595 mod_timer(&mos7840_port->led_timer1,
596 jiffies + msecs_to_jiffies(LED_ON_MS));
Donald0eafe4d2012-04-19 15:00:45 +0800597}
598
Paul B Schroeder3f542972006-08-31 19:41:47 -0500599/*****************************************************************************
600 * mos7840_interrupt_callback
601 * this is the callback function for when we have received data on the
602 * interrupt endpoint.
603 *****************************************************************************/
604
David Howells7d12e782006-10-05 14:55:46 +0100605static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500606{
607 int result;
608 int length;
609 struct moschip_port *mos7840_port;
610 struct usb_serial *serial;
611 __u16 Data;
612 unsigned char *data;
613 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100614 int i, rv = 0;
615 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700616 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500617
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700618 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500619 case 0:
620 /* success */
621 break;
622 case -ECONNRESET:
623 case -ENOENT:
624 case -ESHUTDOWN:
625 /* this urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700626 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
627 __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500628 return;
629 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700630 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
631 __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500632 goto exit;
633 }
634
635 length = urb->actual_length;
636 data = urb->transfer_buffer;
637
Ming Leicdc97792008-02-24 18:41:47 +0800638 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500639
640 /* Moschip get 5 bytes
641 * Byte 1 IIR Port 1 (port.number is 0)
642 * Byte 2 IIR Port 2 (port.number is 1)
643 * Byte 3 IIR Port 3 (port.number is 2)
644 * Byte 4 IIR Port 4 (port.number is 3)
645 * Byte 5 FIFO status for both */
646
647 if (length && length > 5) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700648 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500649 return;
650 }
651
652 sp[0] = (__u8) data[0];
653 sp[1] = (__u8) data[1];
654 sp[2] = (__u8) data[2];
655 sp[3] = (__u8) data[3];
656 st = (__u8) data[4];
657
658 for (i = 0; i < serial->num_ports; i++) {
659 mos7840_port = mos7840_get_port_private(serial->port[i]);
660 wval =
661 (((__u16) serial->port[i]->number -
662 (__u16) (serial->minor)) + 1) << 8;
663 if (mos7840_port->open) {
664 if (sp[i] & 0x01) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700665 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500666 } else {
667 switch (sp[i] & 0x0f) {
668 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700669 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
670 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500671 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100672 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500673 break;
674 case SERIAL_IIR_MS:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700675 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500676 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100677 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500678 break;
679 }
Johan Hovolde681b662012-10-25 18:56:33 +0200680 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500681 }
682 }
683 }
Alan Cox880af9d2008-07-22 11:16:12 +0100684 if (!(rv < 0))
685 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100686 return;
687exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500688 result = usb_submit_urb(urb, GFP_ATOMIC);
689 if (result) {
690 dev_err(&urb->dev->dev,
691 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800692 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500693 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500694}
695
696static int mos7840_port_paranoia_check(struct usb_serial_port *port,
697 const char *function)
698{
699 if (!port) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700700 pr_debug("%s - port == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500701 return -1;
702 }
703 if (!port->serial) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700704 pr_debug("%s - port->serial == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500705 return -1;
706 }
707
708 return 0;
709}
710
711/* Inline functions to check the sanity of a pointer that is passed to us */
712static int mos7840_serial_paranoia_check(struct usb_serial *serial,
713 const char *function)
714{
715 if (!serial) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700716 pr_debug("%s - serial == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500717 return -1;
718 }
719 if (!serial->type) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700720 pr_debug("%s - serial->type == NULL!\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500721 return -1;
722 }
723
724 return 0;
725}
726
727static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
728 const char *function)
729{
730 /* if no port was specified, or it fails a paranoia check */
731 if (!port ||
732 mos7840_port_paranoia_check(port, function) ||
733 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100734 /* then say that we don't have a valid usb_serial thing,
735 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500736 return NULL;
737 }
738
739 return port->serial;
740}
741
742/*****************************************************************************
743 * mos7840_bulk_in_callback
744 * this is the callback function for when we have received data on the
745 * bulk in endpoint.
746 *****************************************************************************/
747
David Howells7d12e782006-10-05 14:55:46 +0100748static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500749{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700750 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500751 unsigned char *data;
752 struct usb_serial *serial;
753 struct usb_serial_port *port;
754 struct moschip_port *mos7840_port;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700755 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500756
Ming Leicdc97792008-02-24 18:41:47 +0800757 mos7840_port = urb->context;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700758 if (!mos7840_port)
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800759 return;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800760
761 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700762 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800763 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500764 return;
765 }
766
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700767 port = mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800768 if (mos7840_port_paranoia_check(port, __func__)) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800769 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500770 return;
771 }
772
Harvey Harrison441b62c2008-03-03 16:08:34 -0800773 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500774 if (!serial) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800775 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500776 return;
777 }
778
Paul B Schroeder3f542972006-08-31 19:41:47 -0500779 data = urb->transfer_buffer;
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100780 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500781
Paul B Schroeder3f542972006-08-31 19:41:47 -0500782 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100783 struct tty_port *tport = &mos7840_port->port->port;
Jiri Slaby2e124b42013-01-03 15:53:06 +0100784 tty_insert_flip_string(tport, data, urb->actual_length);
785 tty_flip_buffer_push(tport);
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100786 port->icount.rx += urb->actual_length;
787 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500788 }
789
790 if (!mos7840_port->read_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700791 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800792 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500793 return;
794 }
795
Johan Hovold62a2cfc2013-07-26 11:55:19 +0200796 if (mos7840_port->has_led)
797 mos7840_led_activity(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500798
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800799 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700800 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100801
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700802 if (retval) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700803 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800804 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500805 }
806}
807
808/*****************************************************************************
809 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100810 * this is the callback function for when we have finished sending
811 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500812 *****************************************************************************/
813
David Howells7d12e782006-10-05 14:55:46 +0100814static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500815{
816 struct moschip_port *mos7840_port;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700817 struct usb_serial_port *port;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700818 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100819 int i;
820
Ming Leicdc97792008-02-24 18:41:47 +0800821 mos7840_port = urb->context;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700822 port = mos7840_port->port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100823 spin_lock(&mos7840_port->pool_lock);
824 for (i = 0; i < NUM_URBS; i++) {
825 if (urb == mos7840_port->write_urb_pool[i]) {
826 mos7840_port->busy[i] = 0;
827 break;
828 }
829 }
830 spin_unlock(&mos7840_port->pool_lock);
831
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700832 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700833 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500834 return;
835 }
836
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700837 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500838 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500839
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100840 if (mos7840_port->open)
841 tty_port_tty_wakeup(&port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500842
843}
844
845/************************************************************************/
846/* 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 */
847/************************************************************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500848
849/*****************************************************************************
850 * mos7840_open
851 * this function is called by the tty driver when a port is opened
852 * If successful, we return 0
853 * Otherwise we return a negative error number.
854 *****************************************************************************/
855
Alan Coxa509a7e2009-09-19 13:13:26 -0700856static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500857{
858 int response;
859 int j;
860 struct usb_serial *serial;
861 struct urb *urb;
862 __u16 Data;
863 int status;
864 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100865 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500866
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700867 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500868 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500869
Paul B Schroeder3f542972006-08-31 19:41:47 -0500870 serial = port->serial;
871
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700872 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500873 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500874
875 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100876 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500877
Oliver Neukum0de9a702007-03-16 20:28:28 +0100878 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500879 return -ENODEV;
880
881 usb_clear_halt(serial->dev, port->write_urb->pipe);
882 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100883 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500884
885 /* Initialising the write urb pool */
886 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100887 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500888 mos7840_port->write_urb_pool[j] = urb;
889
890 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700891 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500892 continue;
893 }
894
Alan Cox880af9d2008-07-22 11:16:12 +0100895 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
896 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500897 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100898 usb_free_urb(urb);
899 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700900 dev_err(&port->dev,
901 "%s-out of memory for urb buffers.\n",
902 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500903 continue;
904 }
905 }
906
907/*****************************************************************************
908 * Initialize MCS7840 -- Write Init values to corresponding Registers
909 *
910 * Register Index
911 * 1 : IER
912 * 2 : FCR
913 * 3 : LCR
914 * 4 : MCR
915 *
916 * 0x08 : SP1/2 Control Reg
917 *****************************************************************************/
918
Alan Cox880af9d2008-07-22 11:16:12 +0100919 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500920
Paul B Schroeder3f542972006-08-31 19:41:47 -0500921 Data = 0x0;
922 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
923 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700924 dev_dbg(&port->dev, "Reading Spreg failed\n");
Johan Hovoldd3fe9f62013-07-01 14:03:33 +0200925 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500926 }
927 Data |= 0x80;
928 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
929 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700930 dev_dbg(&port->dev, "writing Spreg failed\n");
Johan Hovoldd3fe9f62013-07-01 14:03:33 +0200931 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500932 }
933
934 Data &= ~0x80;
935 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
936 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700937 dev_dbg(&port->dev, "writing Spreg failed\n");
Johan Hovoldd3fe9f62013-07-01 14:03:33 +0200938 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500939 }
Alan Cox880af9d2008-07-22 11:16:12 +0100940 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500941
Paul B Schroeder3f542972006-08-31 19:41:47 -0500942 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100943 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
944 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500945 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700946 dev_dbg(&port->dev, "Reading Controlreg failed\n");
Johan Hovoldd3fe9f62013-07-01 14:03:33 +0200947 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500948 }
Alan Cox880af9d2008-07-22 11:16:12 +0100949 Data |= 0x08; /* Driver done bit */
950 Data |= 0x20; /* rx_disable */
951 status = mos7840_set_reg_sync(port,
952 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500953 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700954 dev_dbg(&port->dev, "writing Controlreg failed\n");
Johan Hovoldd3fe9f62013-07-01 14:03:33 +0200955 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500956 }
Alan Cox880af9d2008-07-22 11:16:12 +0100957 /* do register settings here */
958 /* Set all regs to the device default values. */
959 /***********************************
960 * First Disable all interrupts.
961 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500962 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500963 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
964 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700965 dev_dbg(&port->dev, "disabling interrupts failed\n");
Johan Hovoldd3fe9f62013-07-01 14:03:33 +0200966 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500967 }
Alan Cox880af9d2008-07-22 11:16:12 +0100968 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500969 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500970 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
971 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700972 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Johan Hovoldd3fe9f62013-07-01 14:03:33 +0200973 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500974 }
975
976 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500977 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
978 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700979 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Johan Hovoldd3fe9f62013-07-01 14:03:33 +0200980 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500981 }
982
983 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500984 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
985 mos7840_port->shadowLCR = Data;
986
987 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500988 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
989 mos7840_port->shadowMCR = Data;
990
991 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500992 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
993 mos7840_port->shadowLCR = Data;
994
Alan Cox880af9d2008-07-22 11:16:12 +0100995 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500996 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
997
998 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500999 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1000
1001 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001002 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1003
1004 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001005 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1006
1007 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001008 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1009 mos7840_port->shadowLCR = Data;
1010
Alan Cox880af9d2008-07-22 11:16:12 +01001011 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001012 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001013 status = mos7840_get_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);
1017
1018 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001019 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001020 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001021 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001022 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1023
Alan Cox880af9d2008-07-22 11:16:12 +01001024 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001025 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001026 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1027 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001028 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001029 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1030 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001031
Alan Cox880af9d2008-07-22 11:16:12 +01001032 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001033 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001034 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1035 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001036 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001037 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1038 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001039
Alan Cox880af9d2008-07-22 11:16:12 +01001040 /* Check to see if we've set up our endpoint info yet *
1041 * (can't set it up in mos7840_startup as the structures *
1042 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001043 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001044 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001045 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001046 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001047 serial->dev,
1048 usb_rcvintpipe(serial->dev,
1049 serial->port[0]->interrupt_in_endpointAddress),
1050 serial->port[0]->interrupt_in_buffer,
1051 serial->port[0]->interrupt_in_urb->
1052 transfer_buffer_length,
1053 mos7840_interrupt_callback,
1054 serial,
1055 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001056
1057 /* start interrupt read for mos7840 *
1058 * will continue as long as mos7840 is connected */
1059
1060 response =
1061 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1062 GFP_KERNEL);
1063 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001064 dev_err(&port->dev, "%s - Error %d submitting "
1065 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001066 }
1067
1068 }
1069
1070 }
1071
1072 /* see if we've set up our endpoint info yet *
1073 * (can't set it up in mos7840_startup as the *
1074 * structures were not set up at that time.) */
1075
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001076 dev_dbg(&port->dev, "port number is %d\n", port->number);
1077 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1078 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1079 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1080 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1081 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 -05001082 mos7840_port->read_urb = port->read_urb;
1083
1084 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001085 if ((serial->num_ports == 2)
1086 && ((((__u16)port->number -
1087 (__u16)(port->serial->minor)) % 2) != 0)) {
1088 usb_fill_bulk_urb(mos7840_port->read_urb,
1089 serial->dev,
1090 usb_rcvbulkpipe(serial->dev,
1091 (port->bulk_in_endpointAddress) + 2),
1092 port->bulk_in_buffer,
1093 mos7840_port->read_urb->transfer_buffer_length,
1094 mos7840_bulk_in_callback, mos7840_port);
1095 } else {
1096 usb_fill_bulk_urb(mos7840_port->read_urb,
1097 serial->dev,
1098 usb_rcvbulkpipe(serial->dev,
1099 port->bulk_in_endpointAddress),
1100 port->bulk_in_buffer,
1101 mos7840_port->read_urb->transfer_buffer_length,
1102 mos7840_bulk_in_callback, mos7840_port);
1103 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001104
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001105 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001106 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001107 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1108 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001109 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1110 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001111 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001112 }
1113
1114 /* initialize our wait queues */
1115 init_waitqueue_head(&mos7840_port->wait_chase);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001116
Paul B Schroeder3f542972006-08-31 19:41:47 -05001117 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001118 /* Must set to enable ints! */
1119 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001120 /* send a open port command */
1121 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001122 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001123
Paul B Schroeder3f542972006-08-31 19:41:47 -05001124 return 0;
Johan Hovoldd3fe9f62013-07-01 14:03:33 +02001125err:
1126 for (j = 0; j < NUM_URBS; ++j) {
1127 urb = mos7840_port->write_urb_pool[j];
1128 if (!urb)
1129 continue;
1130 kfree(urb->transfer_buffer);
1131 usb_free_urb(urb);
1132 }
1133 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001134}
1135
1136/*****************************************************************************
1137 * mos7840_chars_in_buffer
1138 * this function is called by the tty driver when it wants to know how many
1139 * bytes of data we currently have outstanding in the port (data that has
1140 * been written, but hasn't made it out the port yet)
1141 * If successful, we return the number of bytes left to be written in the
1142 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001143 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001144 *****************************************************************************/
1145
Alan Cox95da3102008-07-22 11:09:07 +01001146static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001147{
Alan Cox95da3102008-07-22 11:09:07 +01001148 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001149 int i;
1150 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001151 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001152 struct moschip_port *mos7840_port;
1153
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001154 if (mos7840_port_paranoia_check(port, __func__))
Alan Cox95da3102008-07-22 11:09:07 +01001155 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001156
1157 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001158 if (mos7840_port == NULL)
Alan Cox95da3102008-07-22 11:09:07 +01001159 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001160
Alan Cox880af9d2008-07-22 11:16:12 +01001161 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell5c263b92012-07-24 14:15:13 -05001162 for (i = 0; i < NUM_URBS; ++i) {
1163 if (mos7840_port->busy[i]) {
1164 struct urb *urb = mos7840_port->write_urb_pool[i];
1165 chars += urb->transfer_buffer_length;
1166 }
1167 }
Alan Cox880af9d2008-07-22 11:16:12 +01001168 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001169 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001170 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001171
1172}
1173
Paul B Schroeder3f542972006-08-31 19:41:47 -05001174/*****************************************************************************
1175 * mos7840_close
1176 * this function is called by the tty driver when a port is closed
1177 *****************************************************************************/
1178
Alan Cox335f8512009-06-11 12:26:29 +01001179static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001180{
1181 struct usb_serial *serial;
1182 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001183 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001184 int j;
1185 __u16 Data;
1186
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001187 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001188 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001189
Harvey Harrison441b62c2008-03-03 16:08:34 -08001190 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001191 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001192 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001193
1194 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001195 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001196
Oliver Neukum0de9a702007-03-16 20:28:28 +01001197 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001198 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001199
1200 for (j = 0; j < NUM_URBS; ++j)
1201 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1202
1203 /* Freeing Write URBs */
1204 for (j = 0; j < NUM_URBS; ++j) {
1205 if (mos7840_port->write_urb_pool[j]) {
1206 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1207 kfree(mos7840_port->write_urb_pool[j]->
1208 transfer_buffer);
1209
1210 usb_free_urb(mos7840_port->write_urb_pool[j]);
1211 }
1212 }
1213
Johan Hovoldbb3529c2013-03-21 12:36:35 +01001214 usb_kill_urb(mos7840_port->write_urb);
1215 usb_kill_urb(mos7840_port->read_urb);
1216 mos7840_port->read_urb_busy = false;
1217
Oliver Neukum0de9a702007-03-16 20:28:28 +01001218 port0->open_ports--;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001219 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 +01001220 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001221 if (serial->port[0]->interrupt_in_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001222 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001223 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001224 }
1225 }
1226
1227 if (mos7840_port->write_urb) {
1228 /* if this urb had a transfer buffer already (old tx) free it */
Syam Sidhardhanae8d4872013-03-07 01:51:12 +05301229 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001230 usb_free_urb(mos7840_port->write_urb);
1231 }
1232
1233 Data = 0x0;
1234 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1235
1236 Data = 0x00;
1237 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1238
1239 mos7840_port->open = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001240}
1241
1242/************************************************************************
1243 *
1244 * mos7840_block_until_chase_response
1245 *
1246 * This function will block the close until one of the following:
1247 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001248 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001249 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1250 *
1251 ************************************************************************/
1252
Alan Cox95da3102008-07-22 11:09:07 +01001253static void mos7840_block_until_chase_response(struct tty_struct *tty,
1254 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001255{
Mark Ferrell1e658482012-07-24 13:38:31 -05001256 int timeout = msecs_to_jiffies(1000);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001257 int wait = 10;
1258 int count;
1259
1260 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001261 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001262
1263 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001264 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001265 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001266
1267 /* Block the thread for a while */
1268 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1269 timeout);
1270 /* No activity.. count down section */
1271 wait--;
1272 if (wait == 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001273 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001274 return;
1275 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001276 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001277 wait = 10;
1278 }
1279 }
1280
1281}
1282
1283/*****************************************************************************
1284 * mos7840_break
1285 * this function sends a break to the port
1286 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001287static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001288{
Alan Cox95da3102008-07-22 11:09:07 +01001289 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001290 unsigned char data;
1291 struct usb_serial *serial;
1292 struct moschip_port *mos7840_port;
1293
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001294 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001295 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001296
Harvey Harrison441b62c2008-03-03 16:08:34 -08001297 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001298 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001299 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001300
1301 mos7840_port = mos7840_get_port_private(port);
1302
Alan Cox880af9d2008-07-22 11:16:12 +01001303 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001304 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001305
Johan Hovoldbb3529c2013-03-21 12:36:35 +01001306 /* flush and block until tx is empty */
1307 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001308
Alan Cox95da3102008-07-22 11:09:07 +01001309 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001310 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001311 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001312 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001313
Alan Cox6b447f042009-01-02 13:48:56 +00001314 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001315 mos7840_port->shadowLCR = data;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001316 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001317 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1318 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001319}
1320
1321/*****************************************************************************
1322 * mos7840_write_room
1323 * this function is called by the tty driver when it wants to know how many
1324 * bytes of data we can accept for a specific port.
1325 * If successful, we return the amount of room that we have for this port
1326 * Otherwise we return a negative error number.
1327 *****************************************************************************/
1328
Alan Cox95da3102008-07-22 11:09:07 +01001329static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001330{
Alan Cox95da3102008-07-22 11:09:07 +01001331 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001332 int i;
1333 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001334 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001335 struct moschip_port *mos7840_port;
1336
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001337 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001338 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001339
1340 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001341 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001342 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001343
Oliver Neukum0de9a702007-03-16 20:28:28 +01001344 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001345 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001346 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001347 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001348 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001349 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001350
Oliver Neukum0de9a702007-03-16 20:28:28 +01001351 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001352 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001353 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001354
1355}
1356
1357/*****************************************************************************
1358 * mos7840_write
1359 * this function is called by the tty driver when data should be written to
1360 * the port.
1361 * If successful, we return the number of bytes written, otherwise we
1362 * return a negative error number.
1363 *****************************************************************************/
1364
Alan Cox95da3102008-07-22 11:09:07 +01001365static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001366 const unsigned char *data, int count)
1367{
1368 int status;
1369 int i;
1370 int bytes_sent = 0;
1371 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001372 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001373
1374 struct moschip_port *mos7840_port;
1375 struct usb_serial *serial;
1376 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001377 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001378 const unsigned char *current_position = data;
1379 unsigned char *data1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001380
1381#ifdef NOTMOS7840
1382 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001383 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1384 mos7840_port->shadowLCR = Data;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001385 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1386 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001387
Alan Cox880af9d2008-07-22 11:16:12 +01001388 /* Data = 0x03; */
1389 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1390 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001391
Alan Cox880af9d2008-07-22 11:16:12 +01001392 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001393 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1394
Alan Cox880af9d2008-07-22 11:16:12 +01001395 /* Data = 0x0c; */
1396 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001397 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001398 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001399 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001400
1401 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001402 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001403 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001404
1405 Data = Data & ~SERIAL_LCR_DLAB;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001406 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001407 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1408#endif
1409
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001410 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001411 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001412
1413 serial = port->serial;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001414 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001415 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001416
1417 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001418 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001419 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001420
1421 /* try to find a free urb in the list */
1422 urb = NULL;
1423
Oliver Neukum0de9a702007-03-16 20:28:28 +01001424 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001425 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001426 if (!mos7840_port->busy[i]) {
1427 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001428 urb = mos7840_port->write_urb_pool[i];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001429 dev_dbg(&port->dev, "URB:%d\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001430 break;
1431 }
1432 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001433 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001434
1435 if (urb == NULL) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001436 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001437 goto exit;
1438 }
1439
1440 if (urb->transfer_buffer == NULL) {
1441 urb->transfer_buffer =
1442 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1443
1444 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001445 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001446 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001447 goto exit;
1448 }
1449 }
1450 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1451
Al Viro97c49652006-10-09 20:29:03 +01001452 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001453
1454 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001455 if ((serial->num_ports == 2)
1456 && ((((__u16)port->number -
1457 (__u16)(port->serial->minor)) % 2) != 0)) {
1458 usb_fill_bulk_urb(urb,
1459 serial->dev,
1460 usb_sndbulkpipe(serial->dev,
1461 (port->bulk_out_endpointAddress) + 2),
1462 urb->transfer_buffer,
1463 transfer_size,
1464 mos7840_bulk_out_data_callback, mos7840_port);
1465 } else {
1466 usb_fill_bulk_urb(urb,
1467 serial->dev,
1468 usb_sndbulkpipe(serial->dev,
1469 port->bulk_out_endpointAddress),
1470 urb->transfer_buffer,
1471 transfer_size,
1472 mos7840_bulk_out_data_callback, mos7840_port);
1473 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001474
1475 data1 = urb->transfer_buffer;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001476 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001477
Johan Hovold62a2cfc2013-07-26 11:55:19 +02001478 if (mos7840_port->has_led)
1479 mos7840_led_activity(port);
Donald0eafe4d2012-04-19 15:00:45 +08001480
Paul B Schroeder3f542972006-08-31 19:41:47 -05001481 /* send it down the pipe */
1482 status = usb_submit_urb(urb, GFP_ATOMIC);
1483
1484 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001485 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001486 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001487 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001488 bytes_sent = status;
1489 goto exit;
1490 }
1491 bytes_sent = transfer_size;
Johan Hovold8c1a07f2013-03-21 12:37:16 +01001492 port->icount.tx += transfer_size;
1493 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001494exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001495 return bytes_sent;
1496
1497}
1498
1499/*****************************************************************************
1500 * mos7840_throttle
1501 * this function is called by the tty driver when it wants to stop the data
1502 * being read from the port.
1503 *****************************************************************************/
1504
Alan Cox95da3102008-07-22 11:09:07 +01001505static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001506{
Alan Cox95da3102008-07-22 11:09:07 +01001507 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001508 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001509 int status;
1510
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001511 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001512 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001513
1514 mos7840_port = mos7840_get_port_private(port);
1515
1516 if (mos7840_port == NULL)
1517 return;
1518
1519 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001520 dev_dbg(&port->dev, "%s", "port not opened\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001521 return;
1522 }
1523
Paul B Schroeder3f542972006-08-31 19:41:47 -05001524 /* if we are implementing XON/XOFF, send the stop character */
1525 if (I_IXOFF(tty)) {
1526 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001527 status = mos7840_write(tty, port, &stop_char, 1);
1528 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001529 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001530 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001531 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001532 if (tty->termios.c_cflag & CRTSCTS) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001533 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001534 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001535 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001536 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001537 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001538 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001539}
1540
1541/*****************************************************************************
1542 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001543 * this function is called by the tty driver when it wants to resume
1544 * the data being read from the port (called after mos7840_throttle is
1545 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001546 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001547static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001548{
Alan Cox95da3102008-07-22 11:09:07 +01001549 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001550 int status;
1551 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1552
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001553 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001554 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001555
1556 if (mos7840_port == NULL)
1557 return;
1558
1559 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001560 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001561 return;
1562 }
1563
Paul B Schroeder3f542972006-08-31 19:41:47 -05001564 /* if we are implementing XON/XOFF, send the start character */
1565 if (I_IXOFF(tty)) {
1566 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001567 status = mos7840_write(tty, port, &start_char, 1);
1568 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001569 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001570 }
1571
1572 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001573 if (tty->termios.c_cflag & CRTSCTS) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001574 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001575 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001576 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001577 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001578 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001579 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001580}
1581
Alan Cox60b33c12011-02-14 16:26:14 +00001582static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001583{
Alan Cox95da3102008-07-22 11:09:07 +01001584 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001585 struct moschip_port *mos7840_port;
1586 unsigned int result;
1587 __u16 msr;
1588 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001589 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001590 mos7840_port = mos7840_get_port_private(port);
1591
Paul B Schroeder3f542972006-08-31 19:41:47 -05001592 if (mos7840_port == NULL)
1593 return -ENODEV;
1594
1595 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
Johan Hovold7471b5f2013-10-09 17:01:09 +02001596 if (status != 1)
1597 return -EIO;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001598 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
Johan Hovold7471b5f2013-10-09 17:01:09 +02001599 if (status != 1)
1600 return -EIO;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001601 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1602 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1603 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1604 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1605 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1606 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1607 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1608
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001609 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001610
1611 return result;
1612}
1613
Alan Cox20b9d172011-02-14 16:26:50 +00001614static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001615 unsigned int set, unsigned int clear)
1616{
Alan Cox95da3102008-07-22 11:09:07 +01001617 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001618 struct moschip_port *mos7840_port;
1619 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001620 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001621
Paul B Schroeder3f542972006-08-31 19:41:47 -05001622 mos7840_port = mos7840_get_port_private(port);
1623
1624 if (mos7840_port == NULL)
1625 return -ENODEV;
1626
Alan Coxe2984492008-02-20 20:51:45 +00001627 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001628 mcr = mos7840_port->shadowMCR;
1629 if (clear & TIOCM_RTS)
1630 mcr &= ~MCR_RTS;
1631 if (clear & TIOCM_DTR)
1632 mcr &= ~MCR_DTR;
1633 if (clear & TIOCM_LOOP)
1634 mcr &= ~MCR_LOOPBACK;
1635
1636 if (set & TIOCM_RTS)
1637 mcr |= MCR_RTS;
1638 if (set & TIOCM_DTR)
1639 mcr |= MCR_DTR;
1640 if (set & TIOCM_LOOP)
1641 mcr |= MCR_LOOPBACK;
1642
1643 mos7840_port->shadowMCR = mcr;
1644
Paul B Schroeder3f542972006-08-31 19:41:47 -05001645 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1646 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001647 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
Roel Kluin87521c42008-04-17 06:16:24 +02001648 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001649 }
1650
1651 return 0;
1652}
1653
1654/*****************************************************************************
1655 * mos7840_calc_baud_rate_divisor
1656 * this function calculates the proper baud rate divisor for the specified
1657 * baud rate.
1658 *****************************************************************************/
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001659static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1660 int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001661 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001662{
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001663 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001664
1665 if (baudRate <= 115200) {
1666 *divisor = 115200 / baudRate;
1667 *clk_sel_val = 0x0;
1668 }
1669 if ((baudRate > 115200) && (baudRate <= 230400)) {
1670 *divisor = 230400 / baudRate;
1671 *clk_sel_val = 0x10;
1672 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1673 *divisor = 403200 / baudRate;
1674 *clk_sel_val = 0x20;
1675 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1676 *divisor = 460800 / baudRate;
1677 *clk_sel_val = 0x30;
1678 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1679 *divisor = 806400 / baudRate;
1680 *clk_sel_val = 0x40;
1681 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1682 *divisor = 921600 / baudRate;
1683 *clk_sel_val = 0x50;
1684 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1685 *divisor = 1572864 / baudRate;
1686 *clk_sel_val = 0x60;
1687 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1688 *divisor = 3145728 / baudRate;
1689 *clk_sel_val = 0x70;
1690 }
1691 return 0;
1692
1693#ifdef NOTMCS7840
1694
1695 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1696 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1697 *divisor = mos7840_divisor_table[i].Divisor;
1698 return 0;
1699 }
1700 }
1701
1702 /* After trying for all the standard baud rates *
1703 * Try calculating the divisor for this baud rate */
1704
1705 if (baudrate > 75 && baudrate < 230400) {
1706 /* get the divisor */
1707 custom = (__u16) (230400L / baudrate);
1708
1709 /* Check for round off */
1710 round1 = (__u16) (2304000L / baudrate);
1711 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001712 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001713 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001714 *divisor = custom;
1715
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001716 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001717 return 0;
1718 }
1719
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001720 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001721 return -1;
1722#endif
1723}
1724
1725/*****************************************************************************
1726 * mos7840_send_cmd_write_baud_rate
1727 * this function sends the proper command to change the baud rate of the
1728 * specified port.
1729 *****************************************************************************/
1730
1731static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1732 int baudRate)
1733{
1734 int divisor = 0;
1735 int status;
1736 __u16 Data;
1737 unsigned char number;
1738 __u16 clk_sel_val;
1739 struct usb_serial_port *port;
1740
1741 if (mos7840_port == NULL)
1742 return -1;
1743
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001744 port = mos7840_port->port;
1745 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001746 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001747
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001748 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001749 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001750
Paul B Schroeder3f542972006-08-31 19:41:47 -05001751 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1752
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001753 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1754 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001755 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001756 if (baudRate > 115200) {
1757#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001758 /* NOTE: need to see the pther register to modify */
1759 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001760 Data = 0x2b;
1761 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001762 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1763 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001764 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001765 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001766 return -1;
1767 }
1768#endif
1769
1770 } else {
1771#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001772 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001773 Data = 0xb;
1774 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001775 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1776 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001777 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001778 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001779 return -1;
1780 }
1781#endif
1782
1783 }
1784
Alan Cox880af9d2008-07-22 11:16:12 +01001785 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001786 clk_sel_val = 0x0;
1787 Data = 0x0;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001788 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001789 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001790 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1791 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001792 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001793 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001794 return -1;
1795 }
1796 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001797 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1798 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001799 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001800 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001801 return -1;
1802 }
1803 /* Calculate the Divisor */
1804
1805 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001806 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001807 return status;
1808 }
1809 /* Enable access to divisor latch */
1810 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1811 mos7840_port->shadowLCR = Data;
1812 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1813
1814 /* Write the divisor */
1815 Data = (unsigned char)(divisor & 0xff);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001816 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001817 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1818
1819 Data = (unsigned char)((divisor & 0xff00) >> 8);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001820 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001821 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1822
1823 /* Disable access to divisor latch */
1824 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1825 mos7840_port->shadowLCR = Data;
1826 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1827
1828 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001829 return status;
1830}
1831
1832/*****************************************************************************
1833 * mos7840_change_port_settings
1834 * This routine is called to set the UART on the device to match
1835 * the specified new settings.
1836 *****************************************************************************/
1837
Alan Cox95da3102008-07-22 11:09:07 +01001838static void mos7840_change_port_settings(struct tty_struct *tty,
1839 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001840{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001841 int baud;
1842 unsigned cflag;
1843 unsigned iflag;
1844 __u8 lData;
1845 __u8 lParity;
1846 __u8 lStop;
1847 int status;
1848 __u16 Data;
1849 struct usb_serial_port *port;
1850 struct usb_serial *serial;
1851
1852 if (mos7840_port == NULL)
1853 return;
1854
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001855 port = mos7840_port->port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001856
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001857 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001858 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001859
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001860 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001861 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001862
1863 serial = port->serial;
1864
Paul B Schroeder3f542972006-08-31 19:41:47 -05001865 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001866 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001867 return;
1868 }
1869
Paul B Schroeder3f542972006-08-31 19:41:47 -05001870 lData = LCR_BITS_8;
1871 lStop = LCR_STOP_1;
1872 lParity = LCR_PAR_NONE;
1873
Alan Coxadc8d742012-07-14 15:31:47 +01001874 cflag = tty->termios.c_cflag;
1875 iflag = tty->termios.c_iflag;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001876
1877 /* Change the number of bits */
Colin Leitnerd410cf22013-11-08 22:52:34 +01001878 switch (cflag & CSIZE) {
1879 case CS5:
1880 lData = LCR_BITS_5;
1881 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001882
Colin Leitnerd410cf22013-11-08 22:52:34 +01001883 case CS6:
1884 lData = LCR_BITS_6;
1885 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001886
Colin Leitnerd410cf22013-11-08 22:52:34 +01001887 case CS7:
1888 lData = LCR_BITS_7;
1889 break;
1890
1891 default:
1892 case CS8:
1893 lData = LCR_BITS_8;
1894 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001895 }
Colin Leitnerd410cf22013-11-08 22:52:34 +01001896
Paul B Schroeder3f542972006-08-31 19:41:47 -05001897 /* Change the Parity bit */
1898 if (cflag & PARENB) {
1899 if (cflag & PARODD) {
1900 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001901 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001902 } else {
1903 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001904 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001905 }
1906
1907 } else {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001908 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001909 }
1910
Alan Cox880af9d2008-07-22 11:16:12 +01001911 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001912 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001913
1914 /* Change the Stop bit */
1915 if (cflag & CSTOPB) {
1916 lStop = LCR_STOP_2;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001917 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001918 } else {
1919 lStop = LCR_STOP_1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001920 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001921 }
1922
1923 /* Update the LCR with the correct value */
1924 mos7840_port->shadowLCR &=
1925 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1926 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1927
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001928 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1929 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001930 /* Disable Interrupts */
1931 Data = 0x00;
1932 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1933
1934 Data = 0x00;
1935 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1936
1937 Data = 0xcf;
1938 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1939
1940 /* Send the updated LCR value to the mos7840 */
1941 Data = mos7840_port->shadowLCR;
1942
1943 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1944
1945 Data = 0x00b;
1946 mos7840_port->shadowMCR = Data;
1947 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1948 Data = 0x00b;
1949 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1950
1951 /* set up the MCR register and send it to the mos7840 */
1952
1953 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01001954 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001955 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001956
Alan Cox880af9d2008-07-22 11:16:12 +01001957 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001958 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01001959 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001960 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001961
1962 Data = mos7840_port->shadowMCR;
1963 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1964
1965 /* Determine divisor based on baud rate */
1966 baud = tty_get_baud_rate(tty);
1967
1968 if (!baud) {
1969 /* pick a default, any default... */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001970 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001971 baud = 9600;
1972 }
1973
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001974 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001975 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1976
1977 /* Enable Interrupts */
1978 Data = 0x0c;
1979 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1980
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001981 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001982 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001983 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001984 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001985 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001986 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001987 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001988 }
1989 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001990 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1991 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001992}
1993
1994/*****************************************************************************
1995 * mos7840_set_termios
1996 * this function is called by the tty driver when it wants to change
1997 * the termios structure
1998 *****************************************************************************/
1999
Alan Cox95da3102008-07-22 11:09:07 +01002000static void mos7840_set_termios(struct tty_struct *tty,
2001 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002002 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002003{
2004 int status;
2005 unsigned int cflag;
2006 struct usb_serial *serial;
2007 struct moschip_port *mos7840_port;
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07002008
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002009 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002010 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002011
2012 serial = port->serial;
2013
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002014 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002015 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002016
2017 mos7840_port = mos7840_get_port_private(port);
2018
2019 if (mos7840_port == NULL)
2020 return;
2021
Paul B Schroeder3f542972006-08-31 19:41:47 -05002022 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002023 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002024 return;
2025 }
2026
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002027 dev_dbg(&port->dev, "%s", "setting termios - \n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002028
Alan Coxadc8d742012-07-14 15:31:47 +01002029 cflag = tty->termios.c_cflag;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002030
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002031 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07002032 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002033 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2034 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2035 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002036
2037 /* change the port settings to the new ones specified */
2038
Alan Cox95da3102008-07-22 11:09:07 +01002039 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002040
2041 if (!mos7840_port->read_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002042 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002043 return;
2044 }
2045
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002046 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002047 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002048 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2049 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002050 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002051 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002052 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002053 }
2054 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002055}
2056
2057/*****************************************************************************
2058 * mos7840_get_lsr_info - get line status register info
2059 *
2060 * Purpose: Let user call ioctl() to get info when the UART physically
2061 * is emptied. On bus types like RS485, the transmitter must
2062 * release the bus after transmitting. This must be done when
2063 * the transmit shift register is empty, not be done when the
2064 * transmit holding register is empty. This functionality
2065 * allows an RS485 driver to be written in user space.
2066 *****************************************************************************/
2067
Alan Cox95da3102008-07-22 11:09:07 +01002068static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002069 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002070{
2071 int count;
2072 unsigned int result = 0;
2073
Alan Cox95da3102008-07-22 11:09:07 +01002074 count = mos7840_chars_in_buffer(tty);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002075 if (count == 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002076 result = TIOCSER_TEMT;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002077
2078 if (copy_to_user(value, &result, sizeof(int)))
2079 return -EFAULT;
2080 return 0;
2081}
2082
2083/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002084 * mos7840_get_serial_info
2085 * function to get information about serial port
2086 *****************************************************************************/
2087
2088static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002089 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002090{
2091 struct serial_struct tmp;
2092
2093 if (mos7840_port == NULL)
2094 return -1;
2095
2096 if (!retinfo)
2097 return -EFAULT;
2098
2099 memset(&tmp, 0, sizeof(tmp));
2100
2101 tmp.type = PORT_16550A;
2102 tmp.line = mos7840_port->port->serial->minor;
2103 tmp.port = mos7840_port->port->number;
2104 tmp.irq = 0;
2105 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2106 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2107 tmp.baud_base = 9600;
2108 tmp.close_delay = 5 * HZ;
2109 tmp.closing_wait = 30 * HZ;
2110
2111 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2112 return -EFAULT;
2113 return 0;
2114}
2115
2116/*****************************************************************************
2117 * SerialIoctl
2118 * this function handles any ioctl calls to the driver
2119 *****************************************************************************/
2120
Alan Cox00a0d0d2011-02-14 16:27:06 +00002121static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002122 unsigned int cmd, unsigned long arg)
2123{
Alan Cox95da3102008-07-22 11:09:07 +01002124 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002125 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002126 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002127
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002128 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002129 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002130
2131 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132
2133 if (mos7840_port == NULL)
2134 return -1;
2135
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002136 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002137
2138 switch (cmd) {
2139 /* return number of bytes available */
2140
Paul B Schroeder3f542972006-08-31 19:41:47 -05002141 case TIOCSERGETLSR:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002142 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01002143 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002144
Paul B Schroeder3f542972006-08-31 19:41:47 -05002145 case TIOCGSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002146 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Al Viro97c49652006-10-09 20:29:03 +01002147 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002148
2149 case TIOCSSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002150 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002151 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002152 default:
2153 break;
2154 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002155 return -ENOIOCTLCMD;
2156}
2157
Donald0eafe4d2012-04-19 15:00:45 +08002158static int mos7810_check(struct usb_serial *serial)
2159{
2160 int i, pass_count = 0;
Johan Hovold15ee89c332013-05-27 14:44:40 +02002161 u8 *buf;
Donald0eafe4d2012-04-19 15:00:45 +08002162 __u16 data = 0, mcr_data = 0;
2163 __u16 test_pattern = 0x55AA;
Johan Hovold15ee89c332013-05-27 14:44:40 +02002164 int res;
2165
2166 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2167 if (!buf)
2168 return 0; /* failed to identify 7810 */
Donald0eafe4d2012-04-19 15:00:45 +08002169
2170 /* Store MCR setting */
Johan Hovold15ee89c332013-05-27 14:44:40 +02002171 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
Donald0eafe4d2012-04-19 15:00:45 +08002172 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
Johan Hovold15ee89c332013-05-27 14:44:40 +02002173 buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2174 if (res == VENDOR_READ_LENGTH)
2175 mcr_data = *buf;
Donald0eafe4d2012-04-19 15:00:45 +08002176
2177 for (i = 0; i < 16; i++) {
2178 /* Send the 1-bit test pattern out to MCS7810 test pin */
2179 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2180 MCS_WRREQ, MCS_WR_RTYPE,
2181 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2182 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2183
2184 /* Read the test pattern back */
Johan Hovold15ee89c332013-05-27 14:44:40 +02002185 res = usb_control_msg(serial->dev,
2186 usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
2187 MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2188 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2189 if (res == VENDOR_READ_LENGTH)
2190 data = *buf;
Donald0eafe4d2012-04-19 15:00:45 +08002191
2192 /* If this is a MCS7810 device, both test patterns must match */
2193 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2194 break;
2195
2196 pass_count++;
2197 }
2198
2199 /* Restore MCR setting */
2200 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2201 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2202 0, MOS_WDR_TIMEOUT);
2203
Johan Hovold15ee89c332013-05-27 14:44:40 +02002204 kfree(buf);
2205
Donald0eafe4d2012-04-19 15:00:45 +08002206 if (pass_count == 16)
2207 return 1;
2208
2209 return 0;
2210}
2211
Johan Hovolded183fb2013-07-26 11:55:18 +02002212static int mos7840_probe(struct usb_serial *serial,
2213 const struct usb_device_id *id)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002214{
Johan Hovoldd802afc2013-08-11 16:49:20 +02002215 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Johan Hovold15ee89c332013-05-27 14:44:40 +02002216 u8 *buf;
Johan Hovolded183fb2013-07-26 11:55:18 +02002217 int device_type;
2218
2219 if (product == MOSCHIP_DEVICE_ID_7810 ||
2220 product == MOSCHIP_DEVICE_ID_7820) {
2221 device_type = product;
2222 goto out;
2223 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002224
Johan Hovold15ee89c332013-05-27 14:44:40 +02002225 buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
Johan Hovolded183fb2013-07-26 11:55:18 +02002226 if (!buf)
2227 return -ENOMEM;
2228
2229 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
Johan Hovold15ee89c332013-05-27 14:44:40 +02002230 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2231 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
Donald Lee093ea2d2012-03-14 15:26:33 +08002232
Johan Hovolded183fb2013-07-26 11:55:18 +02002233 /* For a MCS7840 device GPIO0 must be set to 1 */
2234 if (buf[0] & 0x01)
2235 device_type = MOSCHIP_DEVICE_ID_7840;
2236 else if (mos7810_check(serial))
2237 device_type = MOSCHIP_DEVICE_ID_7810;
2238 else
2239 device_type = MOSCHIP_DEVICE_ID_7820;
2240
2241 kfree(buf);
2242out:
Johan Hovold499c97f2013-07-27 13:34:42 +02002243 usb_set_serial_data(serial, (void *)(unsigned long)device_type);
Johan Hovolded183fb2013-07-26 11:55:18 +02002244
2245 return 0;
2246}
2247
2248static int mos7840_calc_num_ports(struct usb_serial *serial)
2249{
Johan Hovold499c97f2013-07-27 13:34:42 +02002250 int device_type = (unsigned long)usb_get_serial_data(serial);
Johan Hovolded183fb2013-07-26 11:55:18 +02002251 int mos7840_num_ports;
Donald Lee093ea2d2012-03-14 15:26:33 +08002252
Donald0eafe4d2012-04-19 15:00:45 +08002253 mos7840_num_ports = (device_type >> 4) & 0x000F;
Donald0eafe4d2012-04-19 15:00:45 +08002254
Paul B Schroeder3f542972006-08-31 19:41:47 -05002255 return mos7840_num_ports;
2256}
2257
Johan Hovold80c00752012-10-25 18:56:34 +02002258static int mos7840_port_probe(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002259{
Johan Hovold80c00752012-10-25 18:56:34 +02002260 struct usb_serial *serial = port->serial;
Johan Hovold499c97f2013-07-27 13:34:42 +02002261 int device_type = (unsigned long)usb_get_serial_data(serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002262 struct moschip_port *mos7840_port;
Johan Hovold80c00752012-10-25 18:56:34 +02002263 int status;
2264 int pnum;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002265 __u16 Data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002266
Paul B Schroeder3f542972006-08-31 19:41:47 -05002267 /* we set up the pointers to the endpoints in the mos7840_open *
2268 * function, as the structures aren't created yet. */
2269
Johan Hovold80c00752012-10-25 18:56:34 +02002270 pnum = port->number - serial->minor;
2271
Johan Hovoldae685ef2012-10-25 18:56:35 +02002272 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2273 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2274 if (mos7840_port == NULL) {
2275 dev_err(&port->dev, "%s - Out of memory\n", __func__);
2276 return -ENOMEM;
2277 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002278
Johan Hovoldae685ef2012-10-25 18:56:35 +02002279 /* Initialize all port interrupt end point to port 0 int
2280 * endpoint. Our device has only one interrupt end point
2281 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002282
Johan Hovoldae685ef2012-10-25 18:56:35 +02002283 mos7840_port->port = port;
2284 mos7840_set_port_private(port, mos7840_port);
2285 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002286
Johan Hovoldae685ef2012-10-25 18:56:35 +02002287 /* minor is not initialised until later by
2288 * usb-serial.c:get_free_serial() and cannot therefore be used
2289 * to index device instances */
2290 mos7840_port->port_num = pnum + 1;
2291 dev_dbg(&port->dev, "port->number = %d\n", port->number);
2292 dev_dbg(&port->dev, "port->serial->minor = %d\n", port->serial->minor);
2293 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2294 dev_dbg(&port->dev, "serial->minor = %d\n", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002295
Johan Hovoldae685ef2012-10-25 18:56:35 +02002296 if (mos7840_port->port_num == 1) {
2297 mos7840_port->SpRegOffset = 0x0;
2298 mos7840_port->ControlRegOffset = 0x1;
2299 mos7840_port->DcrRegOffset = 0x4;
2300 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2301 mos7840_port->SpRegOffset = 0x8;
2302 mos7840_port->ControlRegOffset = 0x9;
2303 mos7840_port->DcrRegOffset = 0x16;
2304 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2305 mos7840_port->SpRegOffset = 0xa;
2306 mos7840_port->ControlRegOffset = 0xb;
2307 mos7840_port->DcrRegOffset = 0x19;
2308 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2309 mos7840_port->SpRegOffset = 0xa;
2310 mos7840_port->ControlRegOffset = 0xb;
2311 mos7840_port->DcrRegOffset = 0x19;
2312 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2313 mos7840_port->SpRegOffset = 0xc;
2314 mos7840_port->ControlRegOffset = 0xd;
2315 mos7840_port->DcrRegOffset = 0x1c;
2316 }
2317 mos7840_dump_serial_port(port, mos7840_port);
2318 mos7840_set_port_private(port, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002319
Johan Hovoldae685ef2012-10-25 18:56:35 +02002320 /* enable rx_disable bit in control register */
2321 status = mos7840_get_reg_sync(port,
2322 mos7840_port->ControlRegOffset, &Data);
2323 if (status < 0) {
2324 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2325 goto out;
2326 } else
2327 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2328 Data |= 0x08; /* setting driver done bit */
2329 Data |= 0x04; /* sp1_bit to have cts change reflect in
2330 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002331
Johan Hovoldae685ef2012-10-25 18:56:35 +02002332 /* Data |= 0x20; //rx_disable bit */
2333 status = mos7840_set_reg_sync(port,
2334 mos7840_port->ControlRegOffset, Data);
2335 if (status < 0) {
2336 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2337 goto out;
2338 } else
2339 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2340
2341 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2342 and 0x24 in DCR3 */
2343 Data = 0x01;
2344 status = mos7840_set_reg_sync(port,
2345 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2346 if (status < 0) {
2347 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2348 goto out;
2349 } else
2350 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2351
2352 Data = 0x05;
2353 status = mos7840_set_reg_sync(port,
2354 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2355 if (status < 0) {
2356 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2357 goto out;
2358 } else
2359 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2360
2361 Data = 0x24;
2362 status = mos7840_set_reg_sync(port,
2363 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2364 if (status < 0) {
2365 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2366 goto out;
2367 } else
2368 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2369
2370 /* write values in clkstart0x0 and clkmulti 0x20 */
2371 Data = 0x0;
2372 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2373 if (status < 0) {
2374 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2375 goto out;
2376 } else
2377 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2378
2379 Data = 0x20;
2380 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2381 if (status < 0) {
2382 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2383 goto error;
2384 } else
2385 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2386
2387 /* write value 0x0 to scratchpad register */
2388 Data = 0x00;
2389 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2390 if (status < 0) {
2391 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2392 goto out;
2393 } else
2394 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2395
2396 /* Zero Length flag register */
2397 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2398 Data = 0xff;
Johan Hovold80c00752012-10-25 18:56:34 +02002399 status = mos7840_set_reg_sync(port,
Johan Hovoldae685ef2012-10-25 18:56:35 +02002400 (__u16) (ZLP_REG1 +
2401 ((__u16)mos7840_port->port_num)), Data);
2402 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002403 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002404 if (status < 0) {
2405 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2406 goto out;
2407 } else
2408 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2409 } else {
2410 Data = 0xff;
2411 status = mos7840_set_reg_sync(port,
2412 (__u16) (ZLP_REG1 +
2413 ((__u16)mos7840_port->port_num) - 0x1), Data);
2414 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002415 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002416 if (status < 0) {
2417 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2418 goto out;
2419 } else
2420 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002421
Johan Hovoldae685ef2012-10-25 18:56:35 +02002422 }
2423 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2424 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2425 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2426 GFP_KERNEL);
2427 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2428 !mos7840_port->dr) {
2429 status = -ENOMEM;
2430 goto error;
2431 }
Donald0eafe4d2012-04-19 15:00:45 +08002432
Johan Hovoldae685ef2012-10-25 18:56:35 +02002433 mos7840_port->has_led = false;
Donald0eafe4d2012-04-19 15:00:45 +08002434
Johan Hovoldae685ef2012-10-25 18:56:35 +02002435 /* Initialize LED timers */
2436 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2437 mos7840_port->has_led = true;
Donald0eafe4d2012-04-19 15:00:45 +08002438
Johan Hovold62a2cfc2013-07-26 11:55:19 +02002439 mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
2440 mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
2441 GFP_KERNEL);
2442 if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
2443 status = -ENOMEM;
2444 goto error;
2445 }
2446
Johan Hovoldae685ef2012-10-25 18:56:35 +02002447 init_timer(&mos7840_port->led_timer1);
2448 mos7840_port->led_timer1.function = mos7840_led_off;
2449 mos7840_port->led_timer1.expires =
2450 jiffies + msecs_to_jiffies(LED_ON_MS);
2451 mos7840_port->led_timer1.data = (unsigned long)mos7840_port;
Donald0eafe4d2012-04-19 15:00:45 +08002452
Johan Hovoldae685ef2012-10-25 18:56:35 +02002453 init_timer(&mos7840_port->led_timer2);
2454 mos7840_port->led_timer2.function = mos7840_led_flag_off;
2455 mos7840_port->led_timer2.expires =
2456 jiffies + msecs_to_jiffies(LED_OFF_MS);
2457 mos7840_port->led_timer2.data = (unsigned long)mos7840_port;
Donald0eafe4d2012-04-19 15:00:45 +08002458
Johan Hovoldae685ef2012-10-25 18:56:35 +02002459 /* Turn off LED */
2460 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2461 }
2462out:
Johan Hovold80c00752012-10-25 18:56:34 +02002463 if (pnum == serial->num_ports - 1) {
2464 /* Zero Length flag enable */
2465 Data = 0x0f;
2466 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2467 if (status < 0) {
2468 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2469 goto error;
2470 } else
2471 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2472
2473 /* setting configuration feature to one */
2474 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2475 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2476 MOS_WDR_TIMEOUT);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002477 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002478 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002479error:
Johan Hovold62a2cfc2013-07-26 11:55:19 +02002480 kfree(mos7840_port->led_dr);
2481 usb_free_urb(mos7840_port->led_urb);
Johan Hovold80c00752012-10-25 18:56:34 +02002482 kfree(mos7840_port->dr);
2483 kfree(mos7840_port->ctrl_buf);
2484 usb_free_urb(mos7840_port->control_urb);
2485 kfree(mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002486
Oliver Neukum0de9a702007-03-16 20:28:28 +01002487 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002488}
2489
Johan Hovold80c00752012-10-25 18:56:34 +02002490static int mos7840_port_remove(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002491{
Paul B Schroeder3f542972006-08-31 19:41:47 -05002492 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002493
Johan Hovold80c00752012-10-25 18:56:34 +02002494 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002495
Johan Hovold80c00752012-10-25 18:56:34 +02002496 if (mos7840_port->has_led) {
2497 /* Turn off LED */
2498 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002499
Johan Hovold80c00752012-10-25 18:56:34 +02002500 del_timer_sync(&mos7840_port->led_timer1);
2501 del_timer_sync(&mos7840_port->led_timer2);
Johan Hovold62a2cfc2013-07-26 11:55:19 +02002502
2503 usb_kill_urb(mos7840_port->led_urb);
2504 usb_free_urb(mos7840_port->led_urb);
2505 kfree(mos7840_port->led_dr);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002506 }
Johan Hovold80c00752012-10-25 18:56:34 +02002507 usb_kill_urb(mos7840_port->control_urb);
2508 usb_free_urb(mos7840_port->control_urb);
2509 kfree(mos7840_port->ctrl_buf);
2510 kfree(mos7840_port->dr);
2511 kfree(mos7840_port);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002512
Johan Hovold80c00752012-10-25 18:56:34 +02002513 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002514}
2515
2516static struct usb_serial_driver moschip7840_4port_device = {
2517 .driver = {
2518 .owner = THIS_MODULE,
2519 .name = "mos7840",
2520 },
2521 .description = DRIVER_DESC,
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002522 .id_table = id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002523 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002524 .open = mos7840_open,
2525 .close = mos7840_close,
2526 .write = mos7840_write,
2527 .write_room = mos7840_write_room,
2528 .chars_in_buffer = mos7840_chars_in_buffer,
2529 .throttle = mos7840_throttle,
2530 .unthrottle = mos7840_unthrottle,
2531 .calc_num_ports = mos7840_calc_num_ports,
Johan Hovolded183fb2013-07-26 11:55:18 +02002532 .probe = mos7840_probe,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002533 .ioctl = mos7840_ioctl,
2534 .set_termios = mos7840_set_termios,
2535 .break_ctl = mos7840_break,
2536 .tiocmget = mos7840_tiocmget,
2537 .tiocmset = mos7840_tiocmset,
Johan Hovold0c6133712013-03-21 12:37:17 +01002538 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovold8c1a07f2013-03-21 12:37:16 +01002539 .get_icount = usb_serial_generic_get_icount,
Johan Hovold80c00752012-10-25 18:56:34 +02002540 .port_probe = mos7840_port_probe,
2541 .port_remove = mos7840_port_remove,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002542 .read_bulk_callback = mos7840_bulk_in_callback,
2543 .read_int_callback = mos7840_interrupt_callback,
2544};
2545
Alan Stern4d2a7af2012-02-23 14:57:09 -05002546static struct usb_serial_driver * const serial_drivers[] = {
2547 &moschip7840_4port_device, NULL
2548};
2549
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002550module_usb_serial_driver(serial_drivers, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002551
Paul B Schroeder3f542972006-08-31 19:41:47 -05002552MODULE_DESCRIPTION(DRIVER_DESC);
2553MODULE_LICENSE("GPL");