blob: b580b4c7fa488bbf80f1b5628e4e46bb8e28807b [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Paul B Schroeder3f542972006-08-31 19:41:47 -05002/*
Paul B Schroeder3f542972006-08-31 19:41:47 -05003 * Clean ups from Moschip version and a few ioctl implementations by:
4 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
5 *
6 * Originally based on drivers/usb/serial/io_edgeport.c which is:
7 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
8 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
9 *
10 */
11
12#include <linux/kernel.h>
13#include <linux/errno.h>
Paul B Schroeder3f542972006-08-31 19:41:47 -050014#include <linux/slab.h>
15#include <linux/tty.h>
16#include <linux/tty_driver.h>
17#include <linux/tty_flip.h>
18#include <linux/module.h>
19#include <linux/serial.h>
20#include <linux/usb.h>
21#include <linux/usb/serial.h>
Alan Cox880af9d2008-07-22 11:16:12 +010022#include <linux/uaccess.h>
Paul B Schroeder3f542972006-08-31 19:41:47 -050023
Paul B Schroeder3f542972006-08-31 19:41:47 -050024#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
25
26/*
27 * 16C50 UART register defines
28 */
29
30#define LCR_BITS_5 0x00 /* 5 bits/char */
31#define LCR_BITS_6 0x01 /* 6 bits/char */
32#define LCR_BITS_7 0x02 /* 7 bits/char */
33#define LCR_BITS_8 0x03 /* 8 bits/char */
34#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
35
36#define LCR_STOP_1 0x00 /* 1 stop bit */
37#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
38#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
39#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
40
41#define LCR_PAR_NONE 0x00 /* No parity */
42#define LCR_PAR_ODD 0x08 /* Odd parity */
43#define LCR_PAR_EVEN 0x18 /* Even parity */
44#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
45#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
46#define LCR_PAR_MASK 0x38 /* Mask for parity field */
47
48#define LCR_SET_BREAK 0x40 /* Set Break condition */
49#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
50
51#define MCR_DTR 0x01 /* Assert DTR */
52#define MCR_RTS 0x02 /* Assert RTS */
53#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
54#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
55#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
56#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
57
58#define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
59#define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
60#define MOS7840_MSR_RI 0x40 /* Current state of RI */
61#define MOS7840_MSR_CD 0x80 /* Current state of CD */
62
63/*
64 * Defines used for sending commands to port
65 */
66
Mark Ferrell1e658482012-07-24 13:38:31 -050067#define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
Paul B Schroeder3f542972006-08-31 19:41:47 -050068
69#define MOS_PORT1 0x0200
70#define MOS_PORT2 0x0300
71#define MOS_VENREG 0x0000
72#define MOS_MAX_PORT 0x02
73#define MOS_WRITE 0x0E
74#define MOS_READ 0x0D
75
76/* Requests */
77#define MCS_RD_RTYPE 0xC0
78#define MCS_WR_RTYPE 0x40
79#define MCS_RDREQ 0x0D
80#define MCS_WRREQ 0x0E
81#define MCS_CTRL_TIMEOUT 500
82#define VENDOR_READ_LENGTH (0x01)
83
84#define MAX_NAME_LEN 64
85
Alan Cox880af9d2008-07-22 11:16:12 +010086#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
87#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
Paul B Schroeder3f542972006-08-31 19:41:47 -050088
89/* For higher baud Rates use TIOCEXBAUD */
90#define TIOCEXBAUD 0x5462
91
92/* vendor id and device id defines */
93
David Ludlow11e1abb2008-02-25 17:30:52 -050094/* The native mos7840/7820 component */
Paul B Schroeder3f542972006-08-31 19:41:47 -050095#define USB_VENDOR_ID_MOSCHIP 0x9710
96#define MOSCHIP_DEVICE_ID_7840 0x7840
97#define MOSCHIP_DEVICE_ID_7820 0x7820
Donald0eafe4d2012-04-19 15:00:45 +080098#define MOSCHIP_DEVICE_ID_7810 0x7810
David Ludlow11e1abb2008-02-25 17:30:52 -050099/* The native component can have its vendor/device id's overridden
100 * in vendor-specific implementations. Such devices can be handled
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700101 * by making a change here, in id_table.
David Ludlow11e1abb2008-02-25 17:30:52 -0500102 */
Dave Ludlow870408c2010-09-01 12:33:30 -0400103#define USB_VENDOR_ID_BANDB 0x0856
104#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
105#define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
106#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
107#define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
108#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
109#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
110#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
111#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
112#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
113#define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
114#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
115#define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
116#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
Paul B Schroeder3f542972006-08-31 19:41:47 -0500117
Russell Lang9d498be2009-07-17 19:29:20 +1000118/* This driver also supports
119 * ATEN UC2324 device using Moschip MCS7840
120 * ATEN UC2322 device using Moschip MCS7820
121 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930122#define USB_VENDOR_ID_ATENINTL 0x0557
123#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000124#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930125
David Ludlow11e1abb2008-02-25 17:30:52 -0500126/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500127
128#define SERIAL_IIR_RLS 0x06
129#define SERIAL_IIR_MS 0x00
130
131/*
132 * Emulation of the bit mask on the LINE STATUS REGISTER.
133 */
134#define SERIAL_LSR_DR 0x0001
135#define SERIAL_LSR_OE 0x0002
136#define SERIAL_LSR_PE 0x0004
137#define SERIAL_LSR_FE 0x0008
138#define SERIAL_LSR_BI 0x0010
139
140#define MOS_MSR_DELTA_CTS 0x10
141#define MOS_MSR_DELTA_DSR 0x20
142#define MOS_MSR_DELTA_RI 0x40
143#define MOS_MSR_DELTA_CD 0x80
144
Alan Cox880af9d2008-07-22 11:16:12 +0100145/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500146#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
147#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
148#define LINE_CONTROL_REGISTER ((__u16)(0x03))
149#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
150#define LINE_STATUS_REGISTER ((__u16)(0x05))
151#define MODEM_STATUS_REGISTER ((__u16)(0x06))
152#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
153#define DIVISOR_LATCH_LSB ((__u16)(0x00))
154#define DIVISOR_LATCH_MSB ((__u16)(0x01))
155
156#define CLK_MULTI_REGISTER ((__u16)(0x02))
157#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
Donald Lee093ea2d2012-03-14 15:26:33 +0800158#define GPIO_REGISTER ((__u16)(0x07))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500159
160#define SERIAL_LCR_DLAB ((__u16)(0x0080))
161
162/*
163 * URB POOL related defines
164 */
165#define NUM_URBS 16 /* URB Count */
166#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
167
Donald0eafe4d2012-04-19 15:00:45 +0800168/* LED on/off milliseconds*/
169#define LED_ON_MS 500
170#define LED_OFF_MS 500
171
Johan Hovoldd8a083c2013-07-26 11:55:17 +0200172enum mos7840_flag {
173 MOS7840_FLAG_CTRL_BUSY,
Johan Hovold05cf0de2013-07-26 11:55:19 +0200174 MOS7840_FLAG_LED_BUSY,
Johan Hovoldd8a083c2013-07-26 11:55:17 +0200175};
Paul B Schroeder3f542972006-08-31 19:41:47 -0500176
Tony Zelenoffb9c87662012-06-05 17:58:04 +0400177static const struct usb_device_id id_table[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
179 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Donald0eafe4d2012-04-19 15:00:45 +0800180 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500181 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400182 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500183 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400184 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500185 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
186 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
187 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
188 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500189 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400190 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930194 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000195 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500196 {} /* terminating entry */
197};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700198MODULE_DEVICE_TABLE(usb, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500199
200/* This structure holds all of the local port information */
201
202struct moschip_port {
203 int port_num; /*Actual port number in the device(1,2,etc) */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500204 struct urb *read_urb; /* read URB for this port */
205 __u8 shadowLCR; /* last LCR value received */
206 __u8 shadowMCR; /* last MCR value received */
207 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100208 char open_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500209 struct usb_serial_port *port; /* loop back to the owner of this object */
210
Alan Cox880af9d2008-07-22 11:16:12 +0100211 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500212 __u8 SpRegOffset;
213 __u8 ControlRegOffset;
214 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100215 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500216 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100217 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500218 char *ctrl_buf;
219 int MsrLsr;
220
Oliver Neukum0de9a702007-03-16 20:28:28 +0100221 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500222 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100223 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800224 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500225
Donald0eafe4d2012-04-19 15:00:45 +0800226 /* For device(s) with LED indicator */
227 bool has_led;
Donald0eafe4d2012-04-19 15:00:45 +0800228 struct timer_list led_timer1; /* Timer for LED on */
229 struct timer_list led_timer2; /* Timer for LED off */
Johan Hovold05cf0de2013-07-26 11:55:19 +0200230 struct urb *led_urb;
231 struct usb_ctrlrequest *led_dr;
Johan Hovoldd8a083c2013-07-26 11:55:17 +0200232
233 unsigned long flags;
Donald0eafe4d2012-04-19 15:00:45 +0800234};
Paul B Schroeder3f542972006-08-31 19:41:47 -0500235
Paul B Schroeder3f542972006-08-31 19:41:47 -0500236/*
237 * mos7840_set_reg_sync
238 * To set the Control register by calling usb_fill_control_urb function
239 * by passing usb_sndctrlpipe function as parameter.
240 */
241
242static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
243 __u16 val)
244{
245 struct usb_device *dev = port->serial->dev;
246 val = val & 0x00ff;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700247 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500248
249 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
250 MCS_WR_RTYPE, val, reg, NULL, 0,
251 MOS_WDR_TIMEOUT);
252}
253
254/*
255 * mos7840_get_reg_sync
256 * To set the Uart register by calling usb_fill_control_urb function by
257 * passing usb_rcvctrlpipe function as parameter.
258 */
259
260static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100261 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500262{
263 struct usb_device *dev = port->serial->dev;
264 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100265 u8 *buf;
266
267 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
268 if (!buf)
269 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500270
271 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100272 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500273 MOS_WDR_TIMEOUT);
Johan Hovoldcd8db052017-01-12 14:56:18 +0100274 if (ret < VENDOR_READ_LENGTH) {
275 if (ret >= 0)
276 ret = -EIO;
277 goto out;
278 }
279
Johan Hovold9e221a32009-12-28 23:01:55 +0100280 *val = buf[0];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700281 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
Johan Hovoldcd8db052017-01-12 14:56:18 +0100282out:
Johan Hovold9e221a32009-12-28 23:01:55 +0100283 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500284 return ret;
285}
286
287/*
288 * mos7840_set_uart_reg
289 * To set the Uart register by calling usb_fill_control_urb function by
290 * passing usb_sndctrlpipe function as parameter.
291 */
292
293static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
294 __u16 val)
295{
296
297 struct usb_device *dev = port->serial->dev;
298 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100299 /* For the UART control registers, the application number need
300 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100301 if (port->serial->num_ports == 4) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700302 val |= ((__u16)port->port_number + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500303 } else {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700304 if (port->port_number == 0) {
305 val |= ((__u16)port->port_number + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500306 } else {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700307 val |= ((__u16)port->port_number + 2) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500308 }
309 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700310 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500311 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
312 MCS_WR_RTYPE, val, reg, NULL, 0,
313 MOS_WDR_TIMEOUT);
314
315}
316
317/*
318 * mos7840_get_uart_reg
319 * To set the Control register by calling usb_fill_control_urb function
320 * by passing usb_rcvctrlpipe function as parameter.
321 */
322static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100323 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500324{
325 struct usb_device *dev = port->serial->dev;
326 int ret = 0;
327 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100328 u8 *buf;
329
330 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
331 if (!buf)
332 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500333
Alan Cox880af9d2008-07-22 11:16:12 +0100334 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100335 if (port->serial->num_ports == 4) {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700336 Wval = ((__u16)port->port_number + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500337 } else {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700338 if (port->port_number == 0) {
339 Wval = ((__u16)port->port_number + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500340 } else {
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700341 Wval = ((__u16)port->port_number + 2) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500342 }
343 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700344 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500345 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100346 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500347 MOS_WDR_TIMEOUT);
Johan Hovoldcd8db052017-01-12 14:56:18 +0100348 if (ret < VENDOR_READ_LENGTH) {
349 if (ret >= 0)
350 ret = -EIO;
351 goto out;
352 }
Johan Hovold9e221a32009-12-28 23:01:55 +0100353 *val = buf[0];
Johan Hovoldcd8db052017-01-12 14:56:18 +0100354out:
Johan Hovold9e221a32009-12-28 23:01:55 +0100355 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500356 return ret;
357}
358
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700359static void mos7840_dump_serial_port(struct usb_serial_port *port,
360 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500361{
362
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700363 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
364 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
365 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500366
367}
368
369/************************************************************************/
370/************************************************************************/
371/* I N T E R F A C E F U N C T I O N S */
372/* I N T E R F A C E F U N C T I O N S */
373/************************************************************************/
374/************************************************************************/
375
376static inline void mos7840_set_port_private(struct usb_serial_port *port,
377 struct moschip_port *data)
378{
379 usb_set_serial_port_data(port, (void *)data);
380}
381
382static inline struct moschip_port *mos7840_get_port_private(struct
383 usb_serial_port
384 *port)
385{
386 return (struct moschip_port *)usb_get_serial_port_data(port);
387}
388
Oliver Neukum0de9a702007-03-16 20:28:28 +0100389static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500390{
391 struct moschip_port *mos7840_port;
392 struct async_icount *icount;
393 mos7840_port = port;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500394 if (new_msr &
395 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
396 MOS_MSR_DELTA_CD)) {
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100397 icount = &mos7840_port->port->icount;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500398
399 /* update input line counters */
Johan Hovoldc9fac852013-03-21 12:37:15 +0100400 if (new_msr & MOS_MSR_DELTA_CTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500401 icount->cts++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100402 if (new_msr & MOS_MSR_DELTA_DSR)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500403 icount->dsr++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100404 if (new_msr & MOS_MSR_DELTA_CD)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500405 icount->dcd++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100406 if (new_msr & MOS_MSR_DELTA_RI)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500407 icount->rng++;
Johan Hovolde670c6a2013-03-19 09:21:19 +0100408
Johan Hovold0c6133712013-03-21 12:37:17 +0100409 wake_up_interruptible(&port->port->port.delta_msr_wait);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500410 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500411}
412
Oliver Neukum0de9a702007-03-16 20:28:28 +0100413static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500414{
415 struct async_icount *icount;
416
Paul B Schroeder3f542972006-08-31 19:41:47 -0500417 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100418 /*
419 * Parity and Framing errors only count if they
420 * occur exclusive of a break being
421 * received.
422 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500423 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
424 }
425
426 /* update input line counters */
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100427 icount = &port->port->icount;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100428 if (new_lsr & SERIAL_LSR_BI)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500429 icount->brk++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100430 if (new_lsr & SERIAL_LSR_OE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500431 icount->overrun++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100432 if (new_lsr & SERIAL_LSR_PE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500433 icount->parity++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100434 if (new_lsr & SERIAL_LSR_FE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500435 icount->frame++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500436}
437
438/************************************************************************/
439/************************************************************************/
440/* U S B C A L L B A C K F U N C T I O N S */
441/* U S B C A L L B A C K F U N C T I O N S */
442/************************************************************************/
443/************************************************************************/
444
David Howells7d12e782006-10-05 14:55:46 +0100445static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500446{
447 unsigned char *data;
448 struct moschip_port *mos7840_port;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700449 struct device *dev = &urb->dev->dev;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500450 __u8 regval = 0x0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700451 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500452
Ming Leicdc97792008-02-24 18:41:47 +0800453 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100454
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700455 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500456 case 0:
457 /* success */
458 break;
459 case -ECONNRESET:
460 case -ENOENT:
461 case -ESHUTDOWN:
462 /* this urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700463 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Johan Hovoldd8a083c2013-07-26 11:55:17 +0200464 goto out;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500465 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700466 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Johan Hovoldd8a083c2013-07-26 11:55:17 +0200467 goto out;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500468 }
469
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700470 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
Johan Hovold794744a2018-07-04 17:02:17 +0200471 if (urb->actual_length < 1)
472 goto out;
473
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700474 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
475 mos7840_port->MsrLsr, mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500476 data = urb->transfer_buffer;
477 regval = (__u8) data[0];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700478 dev_dbg(dev, "%s data is %x\n", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500479 if (mos7840_port->MsrLsr == 0)
480 mos7840_handle_new_msr(mos7840_port, regval);
481 else if (mos7840_port->MsrLsr == 1)
482 mos7840_handle_new_lsr(mos7840_port, regval);
Johan Hovoldd8a083c2013-07-26 11:55:17 +0200483out:
484 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500485}
486
487static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100488 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500489{
490 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100491 struct usb_ctrlrequest *dr = mcs->dr;
492 unsigned char *buffer = mcs->ctrl_buf;
493 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500494
Johan Hovoldd8a083c2013-07-26 11:55:17 +0200495 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
496 return -EBUSY;
497
Paul B Schroeder3f542972006-08-31 19:41:47 -0500498 dr->bRequestType = MCS_RD_RTYPE;
499 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100500 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500501 dr->wIndex = cpu_to_le16(reg);
502 dr->wLength = cpu_to_le16(2);
503
504 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
505 (unsigned char *)dr, buffer, 2,
506 mos7840_control_callback, mcs);
507 mcs->control_urb->transfer_buffer_length = 2;
508 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
Johan Hovoldd8a083c2013-07-26 11:55:17 +0200509 if (ret)
510 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
511
Paul B Schroeder3f542972006-08-31 19:41:47 -0500512 return ret;
513}
514
Donald0eafe4d2012-04-19 15:00:45 +0800515static void mos7840_set_led_callback(struct urb *urb)
516{
517 switch (urb->status) {
518 case 0:
519 /* Success */
520 break;
521 case -ECONNRESET:
522 case -ENOENT:
523 case -ESHUTDOWN:
524 /* This urb is terminated, clean up */
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100525 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700526 __func__, urb->status);
Donald0eafe4d2012-04-19 15:00:45 +0800527 break;
528 default:
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100529 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700530 __func__, urb->status);
Donald0eafe4d2012-04-19 15:00:45 +0800531 }
532}
533
534static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
535 __u16 reg)
536{
537 struct usb_device *dev = mcs->port->serial->dev;
Johan Hovold05cf0de2013-07-26 11:55:19 +0200538 struct usb_ctrlrequest *dr = mcs->led_dr;
Donald0eafe4d2012-04-19 15:00:45 +0800539
540 dr->bRequestType = MCS_WR_RTYPE;
541 dr->bRequest = MCS_WRREQ;
542 dr->wValue = cpu_to_le16(wval);
543 dr->wIndex = cpu_to_le16(reg);
544 dr->wLength = cpu_to_le16(0);
545
Johan Hovold05cf0de2013-07-26 11:55:19 +0200546 usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
Donald0eafe4d2012-04-19 15:00:45 +0800547 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
548
Johan Hovold05cf0de2013-07-26 11:55:19 +0200549 usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
Donald0eafe4d2012-04-19 15:00:45 +0800550}
551
552static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
553 __u16 val)
554{
555 struct usb_device *dev = port->serial->dev;
556
557 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
558 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
559}
560
Kees Cooke99e88a2017-10-16 14:43:17 -0700561static void mos7840_led_off(struct timer_list *t)
Donald0eafe4d2012-04-19 15:00:45 +0800562{
Kees Cooke99e88a2017-10-16 14:43:17 -0700563 struct moschip_port *mcs = from_timer(mcs, t, led_timer1);
Donald0eafe4d2012-04-19 15:00:45 +0800564
565 /* Turn off LED */
566 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
567 mod_timer(&mcs->led_timer2,
568 jiffies + msecs_to_jiffies(LED_OFF_MS));
569}
570
Kees Cooke99e88a2017-10-16 14:43:17 -0700571static void mos7840_led_flag_off(struct timer_list *t)
Donald0eafe4d2012-04-19 15:00:45 +0800572{
Kees Cooke99e88a2017-10-16 14:43:17 -0700573 struct moschip_port *mcs = from_timer(mcs, t, led_timer2);
Donald0eafe4d2012-04-19 15:00:45 +0800574
Johan Hovold05cf0de2013-07-26 11:55:19 +0200575 clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
576}
577
578static void mos7840_led_activity(struct usb_serial_port *port)
579{
580 struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
581
582 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
583 return;
584
585 mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
586 mod_timer(&mos7840_port->led_timer1,
587 jiffies + msecs_to_jiffies(LED_ON_MS));
Donald0eafe4d2012-04-19 15:00:45 +0800588}
589
Paul B Schroeder3f542972006-08-31 19:41:47 -0500590/*****************************************************************************
591 * mos7840_interrupt_callback
592 * this is the callback function for when we have received data on the
593 * interrupt endpoint.
594 *****************************************************************************/
595
David Howells7d12e782006-10-05 14:55:46 +0100596static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500597{
598 int result;
599 int length;
600 struct moschip_port *mos7840_port;
601 struct usb_serial *serial;
602 __u16 Data;
603 unsigned char *data;
604 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100605 int i, rv = 0;
606 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700607 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500608
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700609 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500610 case 0:
611 /* success */
612 break;
613 case -ECONNRESET:
614 case -ENOENT:
615 case -ESHUTDOWN:
616 /* this urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700617 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
618 __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500619 return;
620 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700621 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
622 __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500623 goto exit;
624 }
625
626 length = urb->actual_length;
627 data = urb->transfer_buffer;
628
Ming Leicdc97792008-02-24 18:41:47 +0800629 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500630
631 /* Moschip get 5 bytes
632 * Byte 1 IIR Port 1 (port.number is 0)
633 * Byte 2 IIR Port 2 (port.number is 1)
634 * Byte 3 IIR Port 3 (port.number is 2)
635 * Byte 4 IIR Port 4 (port.number is 3)
636 * Byte 5 FIFO status for both */
637
Geyslan G. Bem365a0442015-12-11 06:46:42 -0300638 if (length > 5) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700639 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500640 return;
641 }
642
643 sp[0] = (__u8) data[0];
644 sp[1] = (__u8) data[1];
645 sp[2] = (__u8) data[2];
646 sp[3] = (__u8) data[3];
647 st = (__u8) data[4];
648
649 for (i = 0; i < serial->num_ports; i++) {
650 mos7840_port = mos7840_get_port_private(serial->port[i]);
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700651 wval = ((__u16)serial->port[i]->port_number + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500652 if (mos7840_port->open) {
653 if (sp[i] & 0x01) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700654 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500655 } else {
656 switch (sp[i] & 0x0f) {
657 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700658 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
659 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500660 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100661 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500662 break;
663 case SERIAL_IIR_MS:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700664 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500665 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100666 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500667 break;
668 }
Johan Hovolde681b662012-10-25 18:56:33 +0200669 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500670 }
671 }
672 }
Alan Cox880af9d2008-07-22 11:16:12 +0100673 if (!(rv < 0))
674 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100675 return;
676exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500677 result = usb_submit_urb(urb, GFP_ATOMIC);
678 if (result) {
679 dev_err(&urb->dev->dev,
680 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800681 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500682 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500683}
684
685static int mos7840_port_paranoia_check(struct usb_serial_port *port,
686 const char *function)
687{
688 if (!port) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700689 pr_debug("%s - port == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500690 return -1;
691 }
692 if (!port->serial) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700693 pr_debug("%s - port->serial == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500694 return -1;
695 }
696
697 return 0;
698}
699
700/* Inline functions to check the sanity of a pointer that is passed to us */
701static int mos7840_serial_paranoia_check(struct usb_serial *serial,
702 const char *function)
703{
704 if (!serial) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700705 pr_debug("%s - serial == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500706 return -1;
707 }
708 if (!serial->type) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700709 pr_debug("%s - serial->type == NULL!\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500710 return -1;
711 }
712
713 return 0;
714}
715
716static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
717 const char *function)
718{
719 /* if no port was specified, or it fails a paranoia check */
720 if (!port ||
721 mos7840_port_paranoia_check(port, function) ||
722 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100723 /* then say that we don't have a valid usb_serial thing,
724 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500725 return NULL;
726 }
727
728 return port->serial;
729}
730
731/*****************************************************************************
732 * mos7840_bulk_in_callback
733 * this is the callback function for when we have received data on the
734 * bulk in endpoint.
735 *****************************************************************************/
736
David Howells7d12e782006-10-05 14:55:46 +0100737static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500738{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700739 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500740 unsigned char *data;
741 struct usb_serial *serial;
742 struct usb_serial_port *port;
743 struct moschip_port *mos7840_port;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700744 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500745
Ming Leicdc97792008-02-24 18:41:47 +0800746 mos7840_port = urb->context;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700747 if (!mos7840_port)
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800748 return;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800749
750 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700751 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800752 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500753 return;
754 }
755
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700756 port = mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800757 if (mos7840_port_paranoia_check(port, __func__)) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800758 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500759 return;
760 }
761
Harvey Harrison441b62c2008-03-03 16:08:34 -0800762 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500763 if (!serial) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800764 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500765 return;
766 }
767
Paul B Schroeder3f542972006-08-31 19:41:47 -0500768 data = urb->transfer_buffer;
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100769 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500770
Paul B Schroeder3f542972006-08-31 19:41:47 -0500771 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100772 struct tty_port *tport = &mos7840_port->port->port;
Jiri Slaby2e124b42013-01-03 15:53:06 +0100773 tty_insert_flip_string(tport, data, urb->actual_length);
774 tty_flip_buffer_push(tport);
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100775 port->icount.rx += urb->actual_length;
776 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500777 }
778
779 if (!mos7840_port->read_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700780 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800781 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500782 return;
783 }
784
Johan Hovold05cf0de2013-07-26 11:55:19 +0200785 if (mos7840_port->has_led)
786 mos7840_led_activity(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500787
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800788 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700789 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100790
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700791 if (retval) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700792 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800793 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500794 }
795}
796
797/*****************************************************************************
798 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100799 * this is the callback function for when we have finished sending
800 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500801 *****************************************************************************/
802
David Howells7d12e782006-10-05 14:55:46 +0100803static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500804{
805 struct moschip_port *mos7840_port;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700806 struct usb_serial_port *port;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700807 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100808 int i;
809
Ming Leicdc97792008-02-24 18:41:47 +0800810 mos7840_port = urb->context;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700811 port = mos7840_port->port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100812 spin_lock(&mos7840_port->pool_lock);
813 for (i = 0; i < NUM_URBS; i++) {
814 if (urb == mos7840_port->write_urb_pool[i]) {
815 mos7840_port->busy[i] = 0;
816 break;
817 }
818 }
819 spin_unlock(&mos7840_port->pool_lock);
820
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700821 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700822 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500823 return;
824 }
825
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700826 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500827 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500828
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100829 if (mos7840_port->open)
830 tty_port_tty_wakeup(&port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500831
832}
833
834/************************************************************************/
835/* 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 */
836/************************************************************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500837
838/*****************************************************************************
839 * mos7840_open
840 * this function is called by the tty driver when a port is opened
841 * If successful, we return 0
842 * Otherwise we return a negative error number.
843 *****************************************************************************/
844
Alan Coxa509a7e2009-09-19 13:13:26 -0700845static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500846{
847 int response;
848 int j;
849 struct usb_serial *serial;
850 struct urb *urb;
851 __u16 Data;
852 int status;
853 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100854 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500855
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700856 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500857 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500858
Paul B Schroeder3f542972006-08-31 19:41:47 -0500859 serial = port->serial;
860
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700861 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500862 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500863
864 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100865 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500866
Oliver Neukum0de9a702007-03-16 20:28:28 +0100867 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500868 return -ENODEV;
869
870 usb_clear_halt(serial->dev, port->write_urb->pipe);
871 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100872 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500873
874 /* Initialising the write urb pool */
875 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100876 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500877 mos7840_port->write_urb_pool[j] = urb;
Johan Hovold10c642d2013-12-29 19:22:56 +0100878 if (!urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500879 continue;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500880
Alan Cox880af9d2008-07-22 11:16:12 +0100881 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
882 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500883 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100884 usb_free_urb(urb);
885 mos7840_port->write_urb_pool[j] = NULL;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500886 continue;
887 }
888 }
889
890/*****************************************************************************
891 * Initialize MCS7840 -- Write Init values to corresponding Registers
892 *
893 * Register Index
894 * 1 : IER
895 * 2 : FCR
896 * 3 : LCR
897 * 4 : MCR
898 *
899 * 0x08 : SP1/2 Control Reg
900 *****************************************************************************/
901
Alan Cox880af9d2008-07-22 11:16:12 +0100902 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500903
Paul B Schroeder3f542972006-08-31 19:41:47 -0500904 Data = 0x0;
905 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
906 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700907 dev_dbg(&port->dev, "Reading Spreg failed\n");
Johan Hovold5f8a2e62013-07-01 14:03:33 +0200908 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500909 }
910 Data |= 0x80;
911 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
912 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700913 dev_dbg(&port->dev, "writing Spreg failed\n");
Johan Hovold5f8a2e62013-07-01 14:03:33 +0200914 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500915 }
916
917 Data &= ~0x80;
918 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
919 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700920 dev_dbg(&port->dev, "writing Spreg failed\n");
Johan Hovold5f8a2e62013-07-01 14:03:33 +0200921 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500922 }
Alan Cox880af9d2008-07-22 11:16:12 +0100923 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500924
Paul B Schroeder3f542972006-08-31 19:41:47 -0500925 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100926 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
927 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500928 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700929 dev_dbg(&port->dev, "Reading Controlreg failed\n");
Johan Hovold5f8a2e62013-07-01 14:03:33 +0200930 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500931 }
Alan Cox880af9d2008-07-22 11:16:12 +0100932 Data |= 0x08; /* Driver done bit */
933 Data |= 0x20; /* rx_disable */
934 status = mos7840_set_reg_sync(port,
935 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500936 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700937 dev_dbg(&port->dev, "writing Controlreg failed\n");
Johan Hovold5f8a2e62013-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 /* do register settings here */
941 /* Set all regs to the device default values. */
942 /***********************************
943 * First Disable all interrupts.
944 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500945 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500946 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
947 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700948 dev_dbg(&port->dev, "disabling interrupts failed\n");
Johan Hovold5f8a2e62013-07-01 14:03:33 +0200949 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500950 }
Alan Cox880af9d2008-07-22 11:16:12 +0100951 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500952 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500953 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
954 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700955 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Johan Hovold5f8a2e62013-07-01 14:03:33 +0200956 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500957 }
958
959 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500960 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
961 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700962 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Johan Hovold5f8a2e62013-07-01 14:03:33 +0200963 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500964 }
965
966 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500967 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
968 mos7840_port->shadowLCR = Data;
969
970 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500971 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
972 mos7840_port->shadowMCR = Data;
973
974 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500975 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
976 mos7840_port->shadowLCR = Data;
977
Alan Cox880af9d2008-07-22 11:16:12 +0100978 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500979 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
980
981 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500982 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
983
984 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500985 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
986
987 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500988 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
989
990 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500991 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
992 mos7840_port->shadowLCR = Data;
993
Alan Cox880af9d2008-07-22 11:16:12 +0100994 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500995 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500996 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
997
998 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500999 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1000
1001 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001002 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001003 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001004 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001005 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1006
Alan Cox880af9d2008-07-22 11:16:12 +01001007 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001008 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001009 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1010 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001011 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001012 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1013 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001014
Alan Cox880af9d2008-07-22 11:16:12 +01001015 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001016 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001017 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1018 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001019 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001020 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1021 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001022
Alan Cox880af9d2008-07-22 11:16:12 +01001023 /* Check to see if we've set up our endpoint info yet *
1024 * (can't set it up in mos7840_startup as the structures *
1025 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001026 if (port0->open_ports == 1) {
Johan Hovold5182c2c2017-02-09 12:11:41 +01001027 /* FIXME: Buffer never NULL, so URB is not submitted. */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001028 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001029 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001030 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001031 serial->dev,
1032 usb_rcvintpipe(serial->dev,
1033 serial->port[0]->interrupt_in_endpointAddress),
1034 serial->port[0]->interrupt_in_buffer,
1035 serial->port[0]->interrupt_in_urb->
1036 transfer_buffer_length,
1037 mos7840_interrupt_callback,
1038 serial,
1039 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001040
Johan Hovold472d7e52017-01-03 16:39:57 +01001041 /* start interrupt read for mos7840 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001042 response =
1043 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1044 GFP_KERNEL);
1045 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001046 dev_err(&port->dev, "%s - Error %d submitting "
1047 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001048 }
1049
1050 }
1051
1052 }
1053
1054 /* see if we've set up our endpoint info yet *
1055 * (can't set it up in mos7840_startup as the *
1056 * structures were not set up at that time.) */
1057
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001058 dev_dbg(&port->dev, "port number is %d\n", port->port_number);
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07001059 dev_dbg(&port->dev, "minor number is %d\n", port->minor);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001060 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1061 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1062 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1063 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 -05001064 mos7840_port->read_urb = port->read_urb;
1065
1066 /* set up our bulk in urb */
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001067 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
Donald Lee093ea2d2012-03-14 15:26:33 +08001068 usb_fill_bulk_urb(mos7840_port->read_urb,
1069 serial->dev,
1070 usb_rcvbulkpipe(serial->dev,
1071 (port->bulk_in_endpointAddress) + 2),
1072 port->bulk_in_buffer,
1073 mos7840_port->read_urb->transfer_buffer_length,
1074 mos7840_bulk_in_callback, mos7840_port);
1075 } else {
1076 usb_fill_bulk_urb(mos7840_port->read_urb,
1077 serial->dev,
1078 usb_rcvbulkpipe(serial->dev,
1079 port->bulk_in_endpointAddress),
1080 port->bulk_in_buffer,
1081 mos7840_port->read_urb->transfer_buffer_length,
1082 mos7840_bulk_in_callback, mos7840_port);
1083 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001084
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001085 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001086 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001087 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1088 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001089 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1090 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001091 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001092 }
1093
Paul B Schroeder3f542972006-08-31 19:41:47 -05001094 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001095 /* Must set to enable ints! */
1096 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001097 /* send a open port command */
1098 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001099 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001100
Paul B Schroeder3f542972006-08-31 19:41:47 -05001101 return 0;
Johan Hovold5f8a2e62013-07-01 14:03:33 +02001102err:
1103 for (j = 0; j < NUM_URBS; ++j) {
1104 urb = mos7840_port->write_urb_pool[j];
1105 if (!urb)
1106 continue;
1107 kfree(urb->transfer_buffer);
1108 usb_free_urb(urb);
1109 }
1110 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001111}
1112
1113/*****************************************************************************
1114 * mos7840_chars_in_buffer
1115 * this function is called by the tty driver when it wants to know how many
1116 * bytes of data we currently have outstanding in the port (data that has
1117 * been written, but hasn't made it out the port yet)
1118 * If successful, we return the number of bytes left to be written in the
1119 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001120 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001121 *****************************************************************************/
1122
Alan Cox95da3102008-07-22 11:09:07 +01001123static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001124{
Alan Cox95da3102008-07-22 11:09:07 +01001125 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001126 int i;
1127 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001128 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001129 struct moschip_port *mos7840_port;
1130
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001131 if (mos7840_port_paranoia_check(port, __func__))
Alan Cox95da3102008-07-22 11:09:07 +01001132 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001133
1134 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001135 if (mos7840_port == NULL)
Alan Cox95da3102008-07-22 11:09:07 +01001136 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001137
Alan Cox880af9d2008-07-22 11:16:12 +01001138 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell5c263b92012-07-24 14:15:13 -05001139 for (i = 0; i < NUM_URBS; ++i) {
1140 if (mos7840_port->busy[i]) {
1141 struct urb *urb = mos7840_port->write_urb_pool[i];
1142 chars += urb->transfer_buffer_length;
1143 }
1144 }
Alan Cox880af9d2008-07-22 11:16:12 +01001145 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001146 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001147 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001148
1149}
1150
Paul B Schroeder3f542972006-08-31 19:41:47 -05001151/*****************************************************************************
1152 * mos7840_close
1153 * this function is called by the tty driver when a port is closed
1154 *****************************************************************************/
1155
Alan Cox335f8512009-06-11 12:26:29 +01001156static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001157{
1158 struct usb_serial *serial;
1159 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001160 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001161 int j;
1162 __u16 Data;
1163
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001164 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001165 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001166
Harvey Harrison441b62c2008-03-03 16:08:34 -08001167 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001168 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001169 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001170
1171 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001172 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001173
Oliver Neukum0de9a702007-03-16 20:28:28 +01001174 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001175 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001176
1177 for (j = 0; j < NUM_URBS; ++j)
1178 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1179
1180 /* Freeing Write URBs */
1181 for (j = 0; j < NUM_URBS; ++j) {
1182 if (mos7840_port->write_urb_pool[j]) {
Fabian Frederick91c72df2014-06-28 14:44:09 +02001183 kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001184 usb_free_urb(mos7840_port->write_urb_pool[j]);
1185 }
1186 }
1187
Johan Hovoldbb3529c2013-03-21 12:36:35 +01001188 usb_kill_urb(mos7840_port->read_urb);
1189 mos7840_port->read_urb_busy = false;
1190
Oliver Neukum0de9a702007-03-16 20:28:28 +01001191 port0->open_ports--;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001192 dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001193 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001194 if (serial->port[0]->interrupt_in_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001195 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001196 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001197 }
1198 }
1199
Paul B Schroeder3f542972006-08-31 19:41:47 -05001200 Data = 0x0;
1201 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1202
1203 Data = 0x00;
1204 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1205
1206 mos7840_port->open = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001207}
1208
Paul B Schroeder3f542972006-08-31 19:41:47 -05001209/*****************************************************************************
1210 * mos7840_break
1211 * this function sends a break to the port
1212 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001213static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001214{
Alan Cox95da3102008-07-22 11:09:07 +01001215 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001216 unsigned char data;
1217 struct usb_serial *serial;
1218 struct moschip_port *mos7840_port;
1219
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001220 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001221 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001222
Harvey Harrison441b62c2008-03-03 16:08:34 -08001223 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001224 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001225 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001226
1227 mos7840_port = mos7840_get_port_private(port);
1228
Alan Cox880af9d2008-07-22 11:16:12 +01001229 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001230 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001231
Alan Cox95da3102008-07-22 11:09:07 +01001232 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001233 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001234 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001235 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001236
Alan Cox6b447f042009-01-02 13:48:56 +00001237 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001238 mos7840_port->shadowLCR = data;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001239 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001240 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1241 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001242}
1243
1244/*****************************************************************************
1245 * mos7840_write_room
1246 * this function is called by the tty driver when it wants to know how many
1247 * bytes of data we can accept for a specific port.
1248 * If successful, we return the amount of room that we have for this port
1249 * Otherwise we return a negative error number.
1250 *****************************************************************************/
1251
Alan Cox95da3102008-07-22 11:09:07 +01001252static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001253{
Alan Cox95da3102008-07-22 11:09:07 +01001254 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001255 int i;
1256 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001257 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001258 struct moschip_port *mos7840_port;
1259
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001260 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001261 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001262
1263 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001264 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001265 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001266
Oliver Neukum0de9a702007-03-16 20:28:28 +01001267 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001268 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001269 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001270 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001271 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001272 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001273
Oliver Neukum0de9a702007-03-16 20:28:28 +01001274 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001275 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001276 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001277
1278}
1279
1280/*****************************************************************************
1281 * mos7840_write
1282 * this function is called by the tty driver when data should be written to
1283 * the port.
1284 * If successful, we return the number of bytes written, otherwise we
1285 * return a negative error number.
1286 *****************************************************************************/
1287
Alan Cox95da3102008-07-22 11:09:07 +01001288static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001289 const unsigned char *data, int count)
1290{
1291 int status;
1292 int i;
1293 int bytes_sent = 0;
1294 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001295 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001296
1297 struct moschip_port *mos7840_port;
1298 struct usb_serial *serial;
1299 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001300 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001301 const unsigned char *current_position = data;
1302 unsigned char *data1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001303
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001304 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001305 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001306
1307 serial = port->serial;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001308 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001309 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001310
1311 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001312 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001313 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001314
1315 /* try to find a free urb in the list */
1316 urb = NULL;
1317
Oliver Neukum0de9a702007-03-16 20:28:28 +01001318 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001319 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001320 if (!mos7840_port->busy[i]) {
1321 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001322 urb = mos7840_port->write_urb_pool[i];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001323 dev_dbg(&port->dev, "URB:%d\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001324 break;
1325 }
1326 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001327 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001328
1329 if (urb == NULL) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001330 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001331 goto exit;
1332 }
1333
1334 if (urb->transfer_buffer == NULL) {
Alexey Khoroshilov3b7c7e52016-08-12 01:05:09 +03001335 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1336 GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +01001337 if (!urb->transfer_buffer)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001338 goto exit;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001339 }
1340 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1341
Al Viro97c49652006-10-09 20:29:03 +01001342 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001343
1344 /* fill urb with data and submit */
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001345 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
Donald Lee093ea2d2012-03-14 15:26:33 +08001346 usb_fill_bulk_urb(urb,
1347 serial->dev,
1348 usb_sndbulkpipe(serial->dev,
1349 (port->bulk_out_endpointAddress) + 2),
1350 urb->transfer_buffer,
1351 transfer_size,
1352 mos7840_bulk_out_data_callback, mos7840_port);
1353 } else {
1354 usb_fill_bulk_urb(urb,
1355 serial->dev,
1356 usb_sndbulkpipe(serial->dev,
1357 port->bulk_out_endpointAddress),
1358 urb->transfer_buffer,
1359 transfer_size,
1360 mos7840_bulk_out_data_callback, mos7840_port);
1361 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001362
1363 data1 = urb->transfer_buffer;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001364 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001365
Johan Hovold05cf0de2013-07-26 11:55:19 +02001366 if (mos7840_port->has_led)
1367 mos7840_led_activity(port);
Donald0eafe4d2012-04-19 15:00:45 +08001368
Paul B Schroeder3f542972006-08-31 19:41:47 -05001369 /* send it down the pipe */
1370 status = usb_submit_urb(urb, GFP_ATOMIC);
1371
1372 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001373 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001374 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001375 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001376 bytes_sent = status;
1377 goto exit;
1378 }
1379 bytes_sent = transfer_size;
Johan Hovold8c1a07f2013-03-21 12:37:16 +01001380 port->icount.tx += transfer_size;
1381 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001382exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001383 return bytes_sent;
1384
1385}
1386
1387/*****************************************************************************
1388 * mos7840_throttle
1389 * this function is called by the tty driver when it wants to stop the data
1390 * being read from the port.
1391 *****************************************************************************/
1392
Alan Cox95da3102008-07-22 11:09:07 +01001393static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001394{
Alan Cox95da3102008-07-22 11:09:07 +01001395 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001396 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001397 int status;
1398
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001399 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001400 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001401
1402 mos7840_port = mos7840_get_port_private(port);
1403
1404 if (mos7840_port == NULL)
1405 return;
1406
1407 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001408 dev_dbg(&port->dev, "%s", "port not opened\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001409 return;
1410 }
1411
Paul B Schroeder3f542972006-08-31 19:41:47 -05001412 /* if we are implementing XON/XOFF, send the stop character */
1413 if (I_IXOFF(tty)) {
1414 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001415 status = mos7840_write(tty, port, &stop_char, 1);
1416 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001417 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001418 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001419 /* if we are implementing RTS/CTS, toggle that line */
Peter Hurley9db276f2016-01-10 20:36:15 -08001420 if (C_CRTSCTS(tty)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001421 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001422 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001423 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001424 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001425 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001426 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001427}
1428
1429/*****************************************************************************
1430 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001431 * this function is called by the tty driver when it wants to resume
1432 * the data being read from the port (called after mos7840_throttle is
1433 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001434 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001435static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001436{
Alan Cox95da3102008-07-22 11:09:07 +01001437 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001438 int status;
1439 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1440
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001441 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001442 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001443
1444 if (mos7840_port == NULL)
1445 return;
1446
1447 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001448 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001449 return;
1450 }
1451
Paul B Schroeder3f542972006-08-31 19:41:47 -05001452 /* if we are implementing XON/XOFF, send the start character */
1453 if (I_IXOFF(tty)) {
1454 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001455 status = mos7840_write(tty, port, &start_char, 1);
1456 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001457 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001458 }
1459
1460 /* if we are implementing RTS/CTS, toggle that line */
Peter Hurley9db276f2016-01-10 20:36:15 -08001461 if (C_CRTSCTS(tty)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001462 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001463 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001464 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001465 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001466 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001467 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001468}
1469
Alan Cox60b33c12011-02-14 16:26:14 +00001470static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001471{
Alan Cox95da3102008-07-22 11:09:07 +01001472 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001473 struct moschip_port *mos7840_port;
1474 unsigned int result;
1475 __u16 msr;
1476 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001477 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001478 mos7840_port = mos7840_get_port_private(port);
1479
Paul B Schroeder3f542972006-08-31 19:41:47 -05001480 if (mos7840_port == NULL)
1481 return -ENODEV;
1482
1483 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
Johan Hovoldcd8db052017-01-12 14:56:18 +01001484 if (status < 0)
Johan Hovolda91ccd22013-10-09 17:01:09 +02001485 return -EIO;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001486 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
Johan Hovoldcd8db052017-01-12 14:56:18 +01001487 if (status < 0)
Johan Hovolda91ccd22013-10-09 17:01:09 +02001488 return -EIO;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001489 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1490 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1491 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1492 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1493 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1494 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1495 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1496
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001497 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001498
1499 return result;
1500}
1501
Alan Cox20b9d172011-02-14 16:26:50 +00001502static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001503 unsigned int set, unsigned int clear)
1504{
Alan Cox95da3102008-07-22 11:09:07 +01001505 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001506 struct moschip_port *mos7840_port;
1507 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001508 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001509
Paul B Schroeder3f542972006-08-31 19:41:47 -05001510 mos7840_port = mos7840_get_port_private(port);
1511
1512 if (mos7840_port == NULL)
1513 return -ENODEV;
1514
Alan Coxe2984492008-02-20 20:51:45 +00001515 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001516 mcr = mos7840_port->shadowMCR;
1517 if (clear & TIOCM_RTS)
1518 mcr &= ~MCR_RTS;
1519 if (clear & TIOCM_DTR)
1520 mcr &= ~MCR_DTR;
1521 if (clear & TIOCM_LOOP)
1522 mcr &= ~MCR_LOOPBACK;
1523
1524 if (set & TIOCM_RTS)
1525 mcr |= MCR_RTS;
1526 if (set & TIOCM_DTR)
1527 mcr |= MCR_DTR;
1528 if (set & TIOCM_LOOP)
1529 mcr |= MCR_LOOPBACK;
1530
1531 mos7840_port->shadowMCR = mcr;
1532
Paul B Schroeder3f542972006-08-31 19:41:47 -05001533 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1534 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001535 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
Roel Kluin87521c42008-04-17 06:16:24 +02001536 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001537 }
1538
1539 return 0;
1540}
1541
1542/*****************************************************************************
1543 * mos7840_calc_baud_rate_divisor
1544 * this function calculates the proper baud rate divisor for the specified
1545 * baud rate.
1546 *****************************************************************************/
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001547static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1548 int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001549 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001550{
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001551 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001552
1553 if (baudRate <= 115200) {
1554 *divisor = 115200 / baudRate;
1555 *clk_sel_val = 0x0;
1556 }
1557 if ((baudRate > 115200) && (baudRate <= 230400)) {
1558 *divisor = 230400 / baudRate;
1559 *clk_sel_val = 0x10;
1560 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1561 *divisor = 403200 / baudRate;
1562 *clk_sel_val = 0x20;
1563 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1564 *divisor = 460800 / baudRate;
1565 *clk_sel_val = 0x30;
1566 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1567 *divisor = 806400 / baudRate;
1568 *clk_sel_val = 0x40;
1569 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1570 *divisor = 921600 / baudRate;
1571 *clk_sel_val = 0x50;
1572 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1573 *divisor = 1572864 / baudRate;
1574 *clk_sel_val = 0x60;
1575 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1576 *divisor = 3145728 / baudRate;
1577 *clk_sel_val = 0x70;
1578 }
1579 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001580}
1581
1582/*****************************************************************************
1583 * mos7840_send_cmd_write_baud_rate
1584 * this function sends the proper command to change the baud rate of the
1585 * specified port.
1586 *****************************************************************************/
1587
1588static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1589 int baudRate)
1590{
1591 int divisor = 0;
1592 int status;
1593 __u16 Data;
1594 unsigned char number;
1595 __u16 clk_sel_val;
1596 struct usb_serial_port *port;
1597
1598 if (mos7840_port == NULL)
1599 return -1;
1600
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001601 port = mos7840_port->port;
1602 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001603 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001604
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001605 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001606 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001607
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001608 number = mos7840_port->port->port_number;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001609
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001610 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001611 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001612 if (baudRate > 115200) {
1613#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001614 /* NOTE: need to see the pther register to modify */
1615 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001616 Data = 0x2b;
1617 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001618 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1619 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001620 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001621 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001622 return -1;
1623 }
1624#endif
1625
1626 } else {
1627#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001628 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001629 Data = 0xb;
1630 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001631 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1632 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001633 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001634 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001635 return -1;
1636 }
1637#endif
1638
1639 }
1640
Alan Cox880af9d2008-07-22 11:16:12 +01001641 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001642 clk_sel_val = 0x0;
1643 Data = 0x0;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001644 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001645 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001646 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1647 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001648 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001649 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001650 return -1;
1651 }
1652 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001653 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1654 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001655 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001656 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001657 return -1;
1658 }
1659 /* Calculate the Divisor */
1660
1661 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001662 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001663 return status;
1664 }
1665 /* Enable access to divisor latch */
1666 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1667 mos7840_port->shadowLCR = Data;
1668 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1669
1670 /* Write the divisor */
1671 Data = (unsigned char)(divisor & 0xff);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001672 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001673 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1674
1675 Data = (unsigned char)((divisor & 0xff00) >> 8);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001676 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001677 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1678
1679 /* Disable access to divisor latch */
1680 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1681 mos7840_port->shadowLCR = Data;
1682 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1683
1684 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001685 return status;
1686}
1687
1688/*****************************************************************************
1689 * mos7840_change_port_settings
1690 * This routine is called to set the UART on the device to match
1691 * the specified new settings.
1692 *****************************************************************************/
1693
Alan Cox95da3102008-07-22 11:09:07 +01001694static void mos7840_change_port_settings(struct tty_struct *tty,
1695 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001696{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001697 int baud;
1698 unsigned cflag;
1699 unsigned iflag;
1700 __u8 lData;
1701 __u8 lParity;
1702 __u8 lStop;
1703 int status;
1704 __u16 Data;
1705 struct usb_serial_port *port;
1706 struct usb_serial *serial;
1707
1708 if (mos7840_port == NULL)
1709 return;
1710
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001711 port = mos7840_port->port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001712
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001713 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001714 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001715
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001716 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001717 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001718
1719 serial = port->serial;
1720
Paul B Schroeder3f542972006-08-31 19:41:47 -05001721 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001722 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001723 return;
1724 }
1725
Paul B Schroeder3f542972006-08-31 19:41:47 -05001726 lData = LCR_BITS_8;
1727 lStop = LCR_STOP_1;
1728 lParity = LCR_PAR_NONE;
1729
Alan Coxadc8d742012-07-14 15:31:47 +01001730 cflag = tty->termios.c_cflag;
1731 iflag = tty->termios.c_iflag;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001732
1733 /* Change the number of bits */
Colin Leitner78692cc2013-11-08 22:52:34 +01001734 switch (cflag & CSIZE) {
1735 case CS5:
1736 lData = LCR_BITS_5;
1737 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001738
Colin Leitner78692cc2013-11-08 22:52:34 +01001739 case CS6:
1740 lData = LCR_BITS_6;
1741 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001742
Colin Leitner78692cc2013-11-08 22:52:34 +01001743 case CS7:
1744 lData = LCR_BITS_7;
1745 break;
1746
1747 default:
1748 case CS8:
1749 lData = LCR_BITS_8;
1750 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001751 }
Colin Leitner78692cc2013-11-08 22:52:34 +01001752
Paul B Schroeder3f542972006-08-31 19:41:47 -05001753 /* Change the Parity bit */
1754 if (cflag & PARENB) {
1755 if (cflag & PARODD) {
1756 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001757 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001758 } else {
1759 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001760 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001761 }
1762
1763 } else {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001764 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001765 }
1766
Alan Cox880af9d2008-07-22 11:16:12 +01001767 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001768 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001769
1770 /* Change the Stop bit */
1771 if (cflag & CSTOPB) {
1772 lStop = LCR_STOP_2;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001773 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001774 } else {
1775 lStop = LCR_STOP_1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001776 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001777 }
1778
1779 /* Update the LCR with the correct value */
1780 mos7840_port->shadowLCR &=
1781 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1782 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1783
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001784 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1785 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001786 /* Disable Interrupts */
1787 Data = 0x00;
1788 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1789
1790 Data = 0x00;
1791 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1792
1793 Data = 0xcf;
1794 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1795
1796 /* Send the updated LCR value to the mos7840 */
1797 Data = mos7840_port->shadowLCR;
1798
1799 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1800
1801 Data = 0x00b;
1802 mos7840_port->shadowMCR = Data;
1803 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1804 Data = 0x00b;
1805 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1806
1807 /* set up the MCR register and send it to the mos7840 */
1808
1809 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01001810 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001811 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001812
Alan Cox880af9d2008-07-22 11:16:12 +01001813 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001814 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01001815 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001816 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001817
1818 Data = mos7840_port->shadowMCR;
1819 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1820
1821 /* Determine divisor based on baud rate */
1822 baud = tty_get_baud_rate(tty);
1823
1824 if (!baud) {
1825 /* pick a default, any default... */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001826 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001827 baud = 9600;
1828 }
1829
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001830 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001831 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1832
1833 /* Enable Interrupts */
1834 Data = 0x0c;
1835 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1836
Mathieu OTHACEHEce9d8562016-02-04 19:01:29 +01001837 if (!mos7840_port->read_urb_busy) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001838 mos7840_port->read_urb_busy = true;
Johan Hovold9d380192014-10-29 09:07:34 +01001839 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001840 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001841 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001842 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001843 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001844 }
1845 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001846 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1847 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001848}
1849
1850/*****************************************************************************
1851 * mos7840_set_termios
1852 * this function is called by the tty driver when it wants to change
1853 * the termios structure
1854 *****************************************************************************/
1855
Alan Cox95da3102008-07-22 11:09:07 +01001856static void mos7840_set_termios(struct tty_struct *tty,
1857 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001858 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001859{
1860 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001861 struct usb_serial *serial;
1862 struct moschip_port *mos7840_port;
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001863
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001864 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001865 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001866
1867 serial = port->serial;
1868
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001869 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001870 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001871
1872 mos7840_port = mos7840_get_port_private(port);
1873
1874 if (mos7840_port == NULL)
1875 return;
1876
Paul B Schroeder3f542972006-08-31 19:41:47 -05001877 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001878 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001879 return;
1880 }
1881
Paul B Schroeder3f542972006-08-31 19:41:47 -05001882 /* change the port settings to the new ones specified */
1883
Alan Cox95da3102008-07-22 11:09:07 +01001884 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001885
1886 if (!mos7840_port->read_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001887 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001888 return;
1889 }
1890
Mathieu OTHACEHEce9d8562016-02-04 19:01:29 +01001891 if (!mos7840_port->read_urb_busy) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001892 mos7840_port->read_urb_busy = true;
Johan Hovold9d380192014-10-29 09:07:34 +01001893 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001894 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001895 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001896 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001897 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001898 }
1899 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001900}
1901
1902/*****************************************************************************
1903 * mos7840_get_lsr_info - get line status register info
1904 *
1905 * Purpose: Let user call ioctl() to get info when the UART physically
1906 * is emptied. On bus types like RS485, the transmitter must
1907 * release the bus after transmitting. This must be done when
1908 * the transmit shift register is empty, not be done when the
1909 * transmit holding register is empty. This functionality
1910 * allows an RS485 driver to be written in user space.
1911 *****************************************************************************/
1912
Alan Cox95da3102008-07-22 11:09:07 +01001913static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01001914 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001915{
1916 int count;
1917 unsigned int result = 0;
1918
Alan Cox95da3102008-07-22 11:09:07 +01001919 count = mos7840_chars_in_buffer(tty);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001920 if (count == 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001921 result = TIOCSER_TEMT;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001922
1923 if (copy_to_user(value, &result, sizeof(int)))
1924 return -EFAULT;
1925 return 0;
1926}
1927
1928/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05001929 * mos7840_get_serial_info
1930 * function to get information about serial port
1931 *****************************************************************************/
1932
1933static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01001934 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001935{
1936 struct serial_struct tmp;
1937
1938 if (mos7840_port == NULL)
1939 return -1;
1940
Paul B Schroeder3f542972006-08-31 19:41:47 -05001941 memset(&tmp, 0, sizeof(tmp));
1942
1943 tmp.type = PORT_16550A;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07001944 tmp.line = mos7840_port->port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001945 tmp.port = mos7840_port->port->port_number;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001946 tmp.irq = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001947 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1948 tmp.baud_base = 9600;
1949 tmp.close_delay = 5 * HZ;
1950 tmp.closing_wait = 30 * HZ;
1951
1952 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1953 return -EFAULT;
1954 return 0;
1955}
1956
1957/*****************************************************************************
1958 * SerialIoctl
1959 * this function handles any ioctl calls to the driver
1960 *****************************************************************************/
1961
Alan Cox00a0d0d2011-02-14 16:27:06 +00001962static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001963 unsigned int cmd, unsigned long arg)
1964{
Alan Cox95da3102008-07-22 11:09:07 +01001965 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01001966 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001967 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001968
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001969 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001970 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001971
1972 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001973
1974 if (mos7840_port == NULL)
1975 return -1;
1976
Paul B Schroeder3f542972006-08-31 19:41:47 -05001977 switch (cmd) {
1978 /* return number of bytes available */
1979
Paul B Schroeder3f542972006-08-31 19:41:47 -05001980 case TIOCSERGETLSR:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001981 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01001982 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001983
Paul B Schroeder3f542972006-08-31 19:41:47 -05001984 case TIOCGSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001985 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Al Viro97c49652006-10-09 20:29:03 +01001986 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001987
1988 case TIOCSSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001989 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001990 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001991 default:
1992 break;
1993 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001994 return -ENOIOCTLCMD;
1995}
1996
Donald0eafe4d2012-04-19 15:00:45 +08001997static int mos7810_check(struct usb_serial *serial)
1998{
1999 int i, pass_count = 0;
Johan Hovold15ee89c332013-05-27 14:44:40 +02002000 u8 *buf;
Donald0eafe4d2012-04-19 15:00:45 +08002001 __u16 data = 0, mcr_data = 0;
2002 __u16 test_pattern = 0x55AA;
Johan Hovold15ee89c332013-05-27 14:44:40 +02002003 int res;
2004
2005 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2006 if (!buf)
2007 return 0; /* failed to identify 7810 */
Donald0eafe4d2012-04-19 15:00:45 +08002008
2009 /* Store MCR setting */
Johan Hovold15ee89c332013-05-27 14:44:40 +02002010 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
Donald0eafe4d2012-04-19 15:00:45 +08002011 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
Johan Hovold15ee89c332013-05-27 14:44:40 +02002012 buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2013 if (res == VENDOR_READ_LENGTH)
2014 mcr_data = *buf;
Donald0eafe4d2012-04-19 15:00:45 +08002015
2016 for (i = 0; i < 16; i++) {
2017 /* Send the 1-bit test pattern out to MCS7810 test pin */
2018 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2019 MCS_WRREQ, MCS_WR_RTYPE,
2020 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2021 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2022
2023 /* Read the test pattern back */
Johan Hovold15ee89c332013-05-27 14:44:40 +02002024 res = usb_control_msg(serial->dev,
2025 usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
2026 MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2027 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2028 if (res == VENDOR_READ_LENGTH)
2029 data = *buf;
Donald0eafe4d2012-04-19 15:00:45 +08002030
2031 /* If this is a MCS7810 device, both test patterns must match */
2032 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2033 break;
2034
2035 pass_count++;
2036 }
2037
2038 /* Restore MCR setting */
2039 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2040 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2041 0, MOS_WDR_TIMEOUT);
2042
Johan Hovold15ee89c332013-05-27 14:44:40 +02002043 kfree(buf);
2044
Donald0eafe4d2012-04-19 15:00:45 +08002045 if (pass_count == 16)
2046 return 1;
2047
2048 return 0;
2049}
2050
Johan Hovold40c24f22013-07-26 11:55:18 +02002051static int mos7840_probe(struct usb_serial *serial,
2052 const struct usb_device_id *id)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002053{
Johan Hovoldd551ec92013-08-11 16:49:20 +02002054 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Johan Hovold15ee89c332013-05-27 14:44:40 +02002055 u8 *buf;
Johan Hovold40c24f22013-07-26 11:55:18 +02002056 int device_type;
2057
2058 if (product == MOSCHIP_DEVICE_ID_7810 ||
2059 product == MOSCHIP_DEVICE_ID_7820) {
2060 device_type = product;
2061 goto out;
2062 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002063
Johan Hovold15ee89c332013-05-27 14:44:40 +02002064 buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
Johan Hovold40c24f22013-07-26 11:55:18 +02002065 if (!buf)
2066 return -ENOMEM;
2067
2068 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
Johan Hovold15ee89c332013-05-27 14:44:40 +02002069 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2070 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
Donald Lee093ea2d2012-03-14 15:26:33 +08002071
Johan Hovold40c24f22013-07-26 11:55:18 +02002072 /* For a MCS7840 device GPIO0 must be set to 1 */
2073 if (buf[0] & 0x01)
2074 device_type = MOSCHIP_DEVICE_ID_7840;
2075 else if (mos7810_check(serial))
2076 device_type = MOSCHIP_DEVICE_ID_7810;
2077 else
2078 device_type = MOSCHIP_DEVICE_ID_7820;
2079
2080 kfree(buf);
2081out:
Johan Hovold683a0e42013-07-27 13:34:42 +02002082 usb_set_serial_data(serial, (void *)(unsigned long)device_type);
Johan Hovold40c24f22013-07-26 11:55:18 +02002083
2084 return 0;
2085}
2086
Johan Hovold07814242017-03-16 17:13:30 +01002087static int mos7840_calc_num_ports(struct usb_serial *serial,
2088 struct usb_serial_endpoints *epds)
Johan Hovold40c24f22013-07-26 11:55:18 +02002089{
Johan Hovold683a0e42013-07-27 13:34:42 +02002090 int device_type = (unsigned long)usb_get_serial_data(serial);
Johan Hovold95254022017-03-16 17:13:47 +01002091 int num_ports;
Donald Lee093ea2d2012-03-14 15:26:33 +08002092
Johan Hovold95254022017-03-16 17:13:47 +01002093 num_ports = (device_type >> 4) & 0x000F;
Donald0eafe4d2012-04-19 15:00:45 +08002094
Johan Hovold95254022017-03-16 17:13:47 +01002095 /*
2096 * num_ports is currently never zero as device_type is one of
2097 * MOSCHIP_DEVICE_ID_78{1,2,4}0.
2098 */
2099 if (num_ports == 0)
2100 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002101
Johan Hovold95254022017-03-16 17:13:47 +01002102 if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
Johan Hovold5c756332017-01-03 16:39:55 +01002103 dev_err(&serial->interface->dev, "missing endpoints\n");
2104 return -ENODEV;
2105 }
2106
Johan Hovold95254022017-03-16 17:13:47 +01002107 return num_ports;
Johan Hovold5c756332017-01-03 16:39:55 +01002108}
2109
Johan Hovold80c00752012-10-25 18:56:34 +02002110static int mos7840_port_probe(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002111{
Johan Hovold80c00752012-10-25 18:56:34 +02002112 struct usb_serial *serial = port->serial;
Johan Hovold683a0e42013-07-27 13:34:42 +02002113 int device_type = (unsigned long)usb_get_serial_data(serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002114 struct moschip_port *mos7840_port;
Johan Hovold80c00752012-10-25 18:56:34 +02002115 int status;
2116 int pnum;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002117 __u16 Data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002118
Paul B Schroeder3f542972006-08-31 19:41:47 -05002119 /* we set up the pointers to the endpoints in the mos7840_open *
2120 * function, as the structures aren't created yet. */
2121
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07002122 pnum = port->port_number;
Johan Hovold80c00752012-10-25 18:56:34 +02002123
Johan Hovoldae685ef2012-10-25 18:56:35 +02002124 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2125 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01002126 if (!mos7840_port)
Johan Hovoldae685ef2012-10-25 18:56:35 +02002127 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002128
Johan Hovoldae685ef2012-10-25 18:56:35 +02002129 /* Initialize all port interrupt end point to port 0 int
2130 * endpoint. Our device has only one interrupt end point
2131 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132
Johan Hovoldae685ef2012-10-25 18:56:35 +02002133 mos7840_port->port = port;
2134 mos7840_set_port_private(port, mos7840_port);
2135 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002136
Johan Hovoldae685ef2012-10-25 18:56:35 +02002137 /* minor is not initialised until later by
2138 * usb-serial.c:get_free_serial() and cannot therefore be used
2139 * to index device instances */
2140 mos7840_port->port_num = pnum + 1;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07002141 dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
Johan Hovoldae685ef2012-10-25 18:56:35 +02002142 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002143
Johan Hovoldae685ef2012-10-25 18:56:35 +02002144 if (mos7840_port->port_num == 1) {
2145 mos7840_port->SpRegOffset = 0x0;
2146 mos7840_port->ControlRegOffset = 0x1;
2147 mos7840_port->DcrRegOffset = 0x4;
2148 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2149 mos7840_port->SpRegOffset = 0x8;
2150 mos7840_port->ControlRegOffset = 0x9;
2151 mos7840_port->DcrRegOffset = 0x16;
2152 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2153 mos7840_port->SpRegOffset = 0xa;
2154 mos7840_port->ControlRegOffset = 0xb;
2155 mos7840_port->DcrRegOffset = 0x19;
2156 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2157 mos7840_port->SpRegOffset = 0xa;
2158 mos7840_port->ControlRegOffset = 0xb;
2159 mos7840_port->DcrRegOffset = 0x19;
2160 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2161 mos7840_port->SpRegOffset = 0xc;
2162 mos7840_port->ControlRegOffset = 0xd;
2163 mos7840_port->DcrRegOffset = 0x1c;
2164 }
2165 mos7840_dump_serial_port(port, mos7840_port);
2166 mos7840_set_port_private(port, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002167
Johan Hovoldae685ef2012-10-25 18:56:35 +02002168 /* enable rx_disable bit in control register */
2169 status = mos7840_get_reg_sync(port,
2170 mos7840_port->ControlRegOffset, &Data);
2171 if (status < 0) {
2172 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2173 goto out;
2174 } else
2175 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2176 Data |= 0x08; /* setting driver done bit */
2177 Data |= 0x04; /* sp1_bit to have cts change reflect in
2178 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002179
Johan Hovoldae685ef2012-10-25 18:56:35 +02002180 /* Data |= 0x20; //rx_disable bit */
2181 status = mos7840_set_reg_sync(port,
2182 mos7840_port->ControlRegOffset, Data);
2183 if (status < 0) {
2184 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2185 goto out;
2186 } else
2187 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2188
2189 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2190 and 0x24 in DCR3 */
2191 Data = 0x01;
2192 status = mos7840_set_reg_sync(port,
2193 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2194 if (status < 0) {
2195 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2196 goto out;
2197 } else
2198 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2199
2200 Data = 0x05;
2201 status = mos7840_set_reg_sync(port,
2202 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2203 if (status < 0) {
2204 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2205 goto out;
2206 } else
2207 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2208
2209 Data = 0x24;
2210 status = mos7840_set_reg_sync(port,
2211 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2212 if (status < 0) {
2213 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2214 goto out;
2215 } else
2216 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2217
2218 /* write values in clkstart0x0 and clkmulti 0x20 */
2219 Data = 0x0;
2220 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2221 if (status < 0) {
2222 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2223 goto out;
2224 } else
2225 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2226
2227 Data = 0x20;
2228 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2229 if (status < 0) {
2230 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2231 goto error;
2232 } else
2233 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2234
2235 /* write value 0x0 to scratchpad register */
2236 Data = 0x00;
2237 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2238 if (status < 0) {
2239 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2240 goto out;
2241 } else
2242 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2243
2244 /* Zero Length flag register */
2245 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2246 Data = 0xff;
Johan Hovold80c00752012-10-25 18:56:34 +02002247 status = mos7840_set_reg_sync(port,
Johan Hovoldae685ef2012-10-25 18:56:35 +02002248 (__u16) (ZLP_REG1 +
2249 ((__u16)mos7840_port->port_num)), Data);
2250 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002251 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002252 if (status < 0) {
2253 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2254 goto out;
2255 } else
2256 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2257 } else {
2258 Data = 0xff;
2259 status = mos7840_set_reg_sync(port,
2260 (__u16) (ZLP_REG1 +
2261 ((__u16)mos7840_port->port_num) - 0x1), Data);
2262 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002263 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002264 if (status < 0) {
2265 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2266 goto out;
2267 } else
2268 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002269
Johan Hovoldae685ef2012-10-25 18:56:35 +02002270 }
2271 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2272 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2273 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2274 GFP_KERNEL);
2275 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2276 !mos7840_port->dr) {
2277 status = -ENOMEM;
2278 goto error;
2279 }
Donald0eafe4d2012-04-19 15:00:45 +08002280
Johan Hovoldae685ef2012-10-25 18:56:35 +02002281 mos7840_port->has_led = false;
Donald0eafe4d2012-04-19 15:00:45 +08002282
Johan Hovoldae685ef2012-10-25 18:56:35 +02002283 /* Initialize LED timers */
2284 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2285 mos7840_port->has_led = true;
Donald0eafe4d2012-04-19 15:00:45 +08002286
Johan Hovold05cf0de2013-07-26 11:55:19 +02002287 mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
2288 mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
2289 GFP_KERNEL);
2290 if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
2291 status = -ENOMEM;
2292 goto error;
2293 }
2294
Kees Cooke99e88a2017-10-16 14:43:17 -07002295 timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
Johan Hovoldae685ef2012-10-25 18:56:35 +02002296 mos7840_port->led_timer1.expires =
2297 jiffies + msecs_to_jiffies(LED_ON_MS);
Kees Cooke99e88a2017-10-16 14:43:17 -07002298 timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
2299 0);
Johan Hovoldae685ef2012-10-25 18:56:35 +02002300 mos7840_port->led_timer2.expires =
2301 jiffies + msecs_to_jiffies(LED_OFF_MS);
Donald0eafe4d2012-04-19 15:00:45 +08002302
Johan Hovoldae685ef2012-10-25 18:56:35 +02002303 /* Turn off LED */
2304 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2305 }
2306out:
Johan Hovold80c00752012-10-25 18:56:34 +02002307 if (pnum == serial->num_ports - 1) {
2308 /* Zero Length flag enable */
2309 Data = 0x0f;
2310 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2311 if (status < 0) {
2312 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2313 goto error;
2314 } else
2315 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2316
2317 /* setting configuration feature to one */
2318 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2319 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2320 MOS_WDR_TIMEOUT);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002321 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002322 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002323error:
Johan Hovold05cf0de2013-07-26 11:55:19 +02002324 kfree(mos7840_port->led_dr);
2325 usb_free_urb(mos7840_port->led_urb);
Johan Hovold80c00752012-10-25 18:56:34 +02002326 kfree(mos7840_port->dr);
2327 kfree(mos7840_port->ctrl_buf);
2328 usb_free_urb(mos7840_port->control_urb);
2329 kfree(mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002330
Oliver Neukum0de9a702007-03-16 20:28:28 +01002331 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002332}
2333
Johan Hovold80c00752012-10-25 18:56:34 +02002334static int mos7840_port_remove(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002335{
Paul B Schroeder3f542972006-08-31 19:41:47 -05002336 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002337
Johan Hovold80c00752012-10-25 18:56:34 +02002338 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002339
Johan Hovold80c00752012-10-25 18:56:34 +02002340 if (mos7840_port->has_led) {
2341 /* Turn off LED */
2342 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002343
Johan Hovold80c00752012-10-25 18:56:34 +02002344 del_timer_sync(&mos7840_port->led_timer1);
2345 del_timer_sync(&mos7840_port->led_timer2);
Johan Hovold05cf0de2013-07-26 11:55:19 +02002346
2347 usb_kill_urb(mos7840_port->led_urb);
2348 usb_free_urb(mos7840_port->led_urb);
2349 kfree(mos7840_port->led_dr);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002350 }
Johan Hovold80c00752012-10-25 18:56:34 +02002351 usb_kill_urb(mos7840_port->control_urb);
2352 usb_free_urb(mos7840_port->control_urb);
2353 kfree(mos7840_port->ctrl_buf);
2354 kfree(mos7840_port->dr);
2355 kfree(mos7840_port);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002356
Johan Hovold80c00752012-10-25 18:56:34 +02002357 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002358}
2359
2360static struct usb_serial_driver moschip7840_4port_device = {
2361 .driver = {
2362 .owner = THIS_MODULE,
2363 .name = "mos7840",
2364 },
2365 .description = DRIVER_DESC,
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002366 .id_table = id_table,
Johan Hovold95254022017-03-16 17:13:47 +01002367 .num_interrupt_in = 1,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002368 .open = mos7840_open,
2369 .close = mos7840_close,
2370 .write = mos7840_write,
2371 .write_room = mos7840_write_room,
2372 .chars_in_buffer = mos7840_chars_in_buffer,
2373 .throttle = mos7840_throttle,
2374 .unthrottle = mos7840_unthrottle,
2375 .calc_num_ports = mos7840_calc_num_ports,
Johan Hovold40c24f22013-07-26 11:55:18 +02002376 .probe = mos7840_probe,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002377 .ioctl = mos7840_ioctl,
2378 .set_termios = mos7840_set_termios,
2379 .break_ctl = mos7840_break,
2380 .tiocmget = mos7840_tiocmget,
2381 .tiocmset = mos7840_tiocmset,
Johan Hovold0c6133712013-03-21 12:37:17 +01002382 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovold8c1a07f2013-03-21 12:37:16 +01002383 .get_icount = usb_serial_generic_get_icount,
Johan Hovold80c00752012-10-25 18:56:34 +02002384 .port_probe = mos7840_port_probe,
2385 .port_remove = mos7840_port_remove,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002386 .read_bulk_callback = mos7840_bulk_in_callback,
2387 .read_int_callback = mos7840_interrupt_callback,
2388};
2389
Alan Stern4d2a7af2012-02-23 14:57:09 -05002390static struct usb_serial_driver * const serial_drivers[] = {
2391 &moschip7840_4port_device, NULL
2392};
2393
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002394module_usb_serial_driver(serial_drivers, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002395
Paul B Schroeder3f542972006-08-31 19:41:47 -05002396MODULE_DESCRIPTION(DRIVER_DESC);
2397MODULE_LICENSE("GPL");