blob: f0b4e5c01e13ab4b1914499971266b6d0f110fed [file] [log] [blame]
Paul B Schroeder3f542972006-08-31 19:41:47 -05001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * Clean ups from Moschip version and a few ioctl implementations by:
17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 *
23 */
24
25#include <linux/kernel.h>
26#include <linux/errno.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/tty.h>
30#include <linux/tty_driver.h>
31#include <linux/tty_flip.h>
32#include <linux/module.h>
33#include <linux/serial.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
Alan Cox880af9d2008-07-22 11:16:12 +010036#include <linux/uaccess.h>
Paul B Schroeder3f542972006-08-31 19:41:47 -050037
Paul B Schroeder3f542972006-08-31 19:41:47 -050038#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
39
40/*
41 * 16C50 UART register defines
42 */
43
44#define LCR_BITS_5 0x00 /* 5 bits/char */
45#define LCR_BITS_6 0x01 /* 6 bits/char */
46#define LCR_BITS_7 0x02 /* 7 bits/char */
47#define LCR_BITS_8 0x03 /* 8 bits/char */
48#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
49
50#define LCR_STOP_1 0x00 /* 1 stop bit */
51#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
52#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
53#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
54
55#define LCR_PAR_NONE 0x00 /* No parity */
56#define LCR_PAR_ODD 0x08 /* Odd parity */
57#define LCR_PAR_EVEN 0x18 /* Even parity */
58#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
59#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
60#define LCR_PAR_MASK 0x38 /* Mask for parity field */
61
62#define LCR_SET_BREAK 0x40 /* Set Break condition */
63#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
64
65#define MCR_DTR 0x01 /* Assert DTR */
66#define MCR_RTS 0x02 /* Assert RTS */
67#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
68#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
69#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
70#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
71
72#define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
73#define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
74#define MOS7840_MSR_RI 0x40 /* Current state of RI */
75#define MOS7840_MSR_CD 0x80 /* Current state of CD */
76
77/*
78 * Defines used for sending commands to port
79 */
80
Mark Ferrell1e658482012-07-24 13:38:31 -050081#define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
Paul B Schroeder3f542972006-08-31 19:41:47 -050082
83#define MOS_PORT1 0x0200
84#define MOS_PORT2 0x0300
85#define MOS_VENREG 0x0000
86#define MOS_MAX_PORT 0x02
87#define MOS_WRITE 0x0E
88#define MOS_READ 0x0D
89
90/* Requests */
91#define MCS_RD_RTYPE 0xC0
92#define MCS_WR_RTYPE 0x40
93#define MCS_RDREQ 0x0D
94#define MCS_WRREQ 0x0E
95#define MCS_CTRL_TIMEOUT 500
96#define VENDOR_READ_LENGTH (0x01)
97
98#define MAX_NAME_LEN 64
99
Alan Cox880af9d2008-07-22 11:16:12 +0100100#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
101#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500102
103/* For higher baud Rates use TIOCEXBAUD */
104#define TIOCEXBAUD 0x5462
105
106/* vendor id and device id defines */
107
David Ludlow11e1abb2008-02-25 17:30:52 -0500108/* The native mos7840/7820 component */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500109#define USB_VENDOR_ID_MOSCHIP 0x9710
110#define MOSCHIP_DEVICE_ID_7840 0x7840
111#define MOSCHIP_DEVICE_ID_7820 0x7820
Donald0eafe4d2012-04-19 15:00:45 +0800112#define MOSCHIP_DEVICE_ID_7810 0x7810
David Ludlow11e1abb2008-02-25 17:30:52 -0500113/* The native component can have its vendor/device id's overridden
114 * in vendor-specific implementations. Such devices can be handled
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700115 * by making a change here, in id_table.
David Ludlow11e1abb2008-02-25 17:30:52 -0500116 */
Dave Ludlow870408c2010-09-01 12:33:30 -0400117#define USB_VENDOR_ID_BANDB 0x0856
118#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
119#define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
120#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
121#define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
122#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
123#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
124#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
125#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
126#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
127#define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
128#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
129#define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
130#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
Paul B Schroeder3f542972006-08-31 19:41:47 -0500131
Russell Lang9d498be2009-07-17 19:29:20 +1000132/* This driver also supports
133 * ATEN UC2324 device using Moschip MCS7840
134 * ATEN UC2322 device using Moschip MCS7820
135 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930136#define USB_VENDOR_ID_ATENINTL 0x0557
137#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000138#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930139
David Ludlow11e1abb2008-02-25 17:30:52 -0500140/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500141
142#define SERIAL_IIR_RLS 0x06
143#define SERIAL_IIR_MS 0x00
144
145/*
146 * Emulation of the bit mask on the LINE STATUS REGISTER.
147 */
148#define SERIAL_LSR_DR 0x0001
149#define SERIAL_LSR_OE 0x0002
150#define SERIAL_LSR_PE 0x0004
151#define SERIAL_LSR_FE 0x0008
152#define SERIAL_LSR_BI 0x0010
153
154#define MOS_MSR_DELTA_CTS 0x10
155#define MOS_MSR_DELTA_DSR 0x20
156#define MOS_MSR_DELTA_RI 0x40
157#define MOS_MSR_DELTA_CD 0x80
158
Alan Cox880af9d2008-07-22 11:16:12 +0100159/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500160#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
161#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
162#define LINE_CONTROL_REGISTER ((__u16)(0x03))
163#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
164#define LINE_STATUS_REGISTER ((__u16)(0x05))
165#define MODEM_STATUS_REGISTER ((__u16)(0x06))
166#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
167#define DIVISOR_LATCH_LSB ((__u16)(0x00))
168#define DIVISOR_LATCH_MSB ((__u16)(0x01))
169
170#define CLK_MULTI_REGISTER ((__u16)(0x02))
171#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
Donald Lee093ea2d2012-03-14 15:26:33 +0800172#define GPIO_REGISTER ((__u16)(0x07))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500173
174#define SERIAL_LCR_DLAB ((__u16)(0x0080))
175
176/*
177 * URB POOL related defines
178 */
179#define NUM_URBS 16 /* URB Count */
180#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
181
Donald0eafe4d2012-04-19 15:00:45 +0800182/* LED on/off milliseconds*/
183#define LED_ON_MS 500
184#define LED_OFF_MS 500
185
186static int device_type;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500187
Tony Zelenoffb9c87662012-06-05 17:58:04 +0400188static const struct usb_device_id id_table[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
190 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Donald0eafe4d2012-04-19 15:00:45 +0800191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930205 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000206 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500207 {} /* terminating entry */
208};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700209MODULE_DEVICE_TABLE(usb, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500210
211/* This structure holds all of the local port information */
212
213struct moschip_port {
214 int port_num; /*Actual port number in the device(1,2,etc) */
215 struct urb *write_urb; /* write URB for this port */
216 struct urb *read_urb; /* read URB for this port */
217 __u8 shadowLCR; /* last LCR value received */
218 __u8 shadowMCR; /* last MCR value received */
219 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100220 char open_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500221 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500222 struct usb_serial_port *port; /* loop back to the owner of this object */
223
Alan Cox880af9d2008-07-22 11:16:12 +0100224 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500225 __u8 SpRegOffset;
226 __u8 ControlRegOffset;
227 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100228 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500229 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100230 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500231 char *ctrl_buf;
232 int MsrLsr;
233
Oliver Neukum0de9a702007-03-16 20:28:28 +0100234 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500235 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100236 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800237 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500238
Donald0eafe4d2012-04-19 15:00:45 +0800239 /* For device(s) with LED indicator */
240 bool has_led;
241 bool led_flag;
242 struct timer_list led_timer1; /* Timer for LED on */
243 struct timer_list led_timer2; /* Timer for LED off */
244};
Paul B Schroeder3f542972006-08-31 19:41:47 -0500245
Paul B Schroeder3f542972006-08-31 19:41:47 -0500246/*
247 * mos7840_set_reg_sync
248 * To set the Control register by calling usb_fill_control_urb function
249 * by passing usb_sndctrlpipe function as parameter.
250 */
251
252static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
253 __u16 val)
254{
255 struct usb_device *dev = port->serial->dev;
256 val = val & 0x00ff;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700257 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500258
259 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
260 MCS_WR_RTYPE, val, reg, NULL, 0,
261 MOS_WDR_TIMEOUT);
262}
263
264/*
265 * mos7840_get_reg_sync
266 * To set the Uart register by calling usb_fill_control_urb function by
267 * passing usb_rcvctrlpipe function as parameter.
268 */
269
270static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100271 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500272{
273 struct usb_device *dev = port->serial->dev;
274 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100275 u8 *buf;
276
277 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
278 if (!buf)
279 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500280
281 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100282 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500283 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100284 *val = buf[0];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700285 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100286
287 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500288 return ret;
289}
290
291/*
292 * mos7840_set_uart_reg
293 * To set the Uart register by calling usb_fill_control_urb function by
294 * passing usb_sndctrlpipe function as parameter.
295 */
296
297static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
298 __u16 val)
299{
300
301 struct usb_device *dev = port->serial->dev;
302 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100303 /* For the UART control registers, the application number need
304 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100305 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100306 val |= (((__u16) port->number -
307 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500308 } else {
309 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100310 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500311 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500312 } else {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700313 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500314 (__u16) (port->serial->minor)) + 2) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500315 }
316 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700317 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500318 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
319 MCS_WR_RTYPE, val, reg, NULL, 0,
320 MOS_WDR_TIMEOUT);
321
322}
323
324/*
325 * mos7840_get_uart_reg
326 * To set the Control register by calling usb_fill_control_urb function
327 * by passing usb_rcvctrlpipe function as parameter.
328 */
329static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100330 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500331{
332 struct usb_device *dev = port->serial->dev;
333 int ret = 0;
334 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100335 u8 *buf;
336
337 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
338 if (!buf)
339 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500340
Alan Cox880af9d2008-07-22 11:16:12 +0100341 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100342 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500343 Wval =
344 (((__u16) port->number - (__u16) (port->serial->minor)) +
345 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500346 } else {
347 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100348 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500349 (__u16) (port->serial->minor)) + 1) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500350 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100351 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500352 (__u16) (port->serial->minor)) + 2) << 8;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500353 }
354 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700355 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500356 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100357 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500358 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100359 *val = buf[0];
360
361 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500362 return ret;
363}
364
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700365static void mos7840_dump_serial_port(struct usb_serial_port *port,
366 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500367{
368
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700369 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
370 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
371 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500372
373}
374
375/************************************************************************/
376/************************************************************************/
377/* I N T E R F A C E F U N C T I O N S */
378/* I N T E R F A C E F U N C T I O N S */
379/************************************************************************/
380/************************************************************************/
381
382static inline void mos7840_set_port_private(struct usb_serial_port *port,
383 struct moschip_port *data)
384{
385 usb_set_serial_port_data(port, (void *)data);
386}
387
388static inline struct moschip_port *mos7840_get_port_private(struct
389 usb_serial_port
390 *port)
391{
392 return (struct moschip_port *)usb_get_serial_port_data(port);
393}
394
Oliver Neukum0de9a702007-03-16 20:28:28 +0100395static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500396{
397 struct moschip_port *mos7840_port;
398 struct async_icount *icount;
399 mos7840_port = port;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500400 if (new_msr &
401 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
402 MOS_MSR_DELTA_CD)) {
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100403 icount = &mos7840_port->port->icount;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500404
405 /* update input line counters */
Johan Hovoldc9fac852013-03-21 12:37:15 +0100406 if (new_msr & MOS_MSR_DELTA_CTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500407 icount->cts++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100408 if (new_msr & MOS_MSR_DELTA_DSR)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500409 icount->dsr++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100410 if (new_msr & MOS_MSR_DELTA_CD)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500411 icount->dcd++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100412 if (new_msr & MOS_MSR_DELTA_RI)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500413 icount->rng++;
Johan Hovolde670c6a2013-03-19 09:21:19 +0100414
Johan Hovold0c6133712013-03-21 12:37:17 +0100415 wake_up_interruptible(&port->port->port.delta_msr_wait);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500416 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500417}
418
Oliver Neukum0de9a702007-03-16 20:28:28 +0100419static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500420{
421 struct async_icount *icount;
422
Paul B Schroeder3f542972006-08-31 19:41:47 -0500423 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100424 /*
425 * Parity and Framing errors only count if they
426 * occur exclusive of a break being
427 * received.
428 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500429 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
430 }
431
432 /* update input line counters */
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100433 icount = &port->port->icount;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100434 if (new_lsr & SERIAL_LSR_BI)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500435 icount->brk++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100436 if (new_lsr & SERIAL_LSR_OE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500437 icount->overrun++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100438 if (new_lsr & SERIAL_LSR_PE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500439 icount->parity++;
Johan Hovoldc9fac852013-03-21 12:37:15 +0100440 if (new_lsr & SERIAL_LSR_FE)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500441 icount->frame++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500442}
443
444/************************************************************************/
445/************************************************************************/
446/* U S B C A L L B A C K F U N C T I O N S */
447/* U S B C A L L B A C K F U N C T I O N S */
448/************************************************************************/
449/************************************************************************/
450
David Howells7d12e782006-10-05 14:55:46 +0100451static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500452{
453 unsigned char *data;
454 struct moschip_port *mos7840_port;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700455 struct device *dev = &urb->dev->dev;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500456 __u8 regval = 0x0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700457 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500458
Ming Leicdc97792008-02-24 18:41:47 +0800459 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100460
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700461 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500462 case 0:
463 /* success */
464 break;
465 case -ECONNRESET:
466 case -ENOENT:
467 case -ESHUTDOWN:
468 /* this urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700469 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500470 return;
471 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700472 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Johan Hovold28c3ae92012-10-25 18:56:32 +0200473 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500474 }
475
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700476 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
477 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
478 mos7840_port->MsrLsr, mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500479 data = urb->transfer_buffer;
480 regval = (__u8) data[0];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700481 dev_dbg(dev, "%s data is %x\n", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500482 if (mos7840_port->MsrLsr == 0)
483 mos7840_handle_new_msr(mos7840_port, regval);
484 else if (mos7840_port->MsrLsr == 1)
485 mos7840_handle_new_lsr(mos7840_port, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500486}
487
488static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100489 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500490{
491 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100492 struct usb_ctrlrequest *dr = mcs->dr;
493 unsigned char *buffer = mcs->ctrl_buf;
494 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500495
Paul B Schroeder3f542972006-08-31 19:41:47 -0500496 dr->bRequestType = MCS_RD_RTYPE;
497 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100498 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500499 dr->wIndex = cpu_to_le16(reg);
500 dr->wLength = cpu_to_le16(2);
501
502 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
503 (unsigned char *)dr, buffer, 2,
504 mos7840_control_callback, mcs);
505 mcs->control_urb->transfer_buffer_length = 2;
506 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
507 return ret;
508}
509
Donald0eafe4d2012-04-19 15:00:45 +0800510static void mos7840_set_led_callback(struct urb *urb)
511{
512 switch (urb->status) {
513 case 0:
514 /* Success */
515 break;
516 case -ECONNRESET:
517 case -ENOENT:
518 case -ESHUTDOWN:
519 /* This urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700520 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
521 __func__, urb->status);
Donald0eafe4d2012-04-19 15:00:45 +0800522 break;
523 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700524 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
525 __func__, urb->status);
Donald0eafe4d2012-04-19 15:00:45 +0800526 }
527}
528
529static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
530 __u16 reg)
531{
532 struct usb_device *dev = mcs->port->serial->dev;
533 struct usb_ctrlrequest *dr = mcs->dr;
534
535 dr->bRequestType = MCS_WR_RTYPE;
536 dr->bRequest = MCS_WRREQ;
537 dr->wValue = cpu_to_le16(wval);
538 dr->wIndex = cpu_to_le16(reg);
539 dr->wLength = cpu_to_le16(0);
540
541 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
542 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
543
544 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
545}
546
547static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
548 __u16 val)
549{
550 struct usb_device *dev = port->serial->dev;
551
552 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
553 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
554}
555
556static void mos7840_led_off(unsigned long arg)
557{
558 struct moschip_port *mcs = (struct moschip_port *) arg;
559
560 /* Turn off LED */
561 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
562 mod_timer(&mcs->led_timer2,
563 jiffies + msecs_to_jiffies(LED_OFF_MS));
564}
565
566static void mos7840_led_flag_off(unsigned long arg)
567{
568 struct moschip_port *mcs = (struct moschip_port *) arg;
569
570 mcs->led_flag = false;
571}
572
Paul B Schroeder3f542972006-08-31 19:41:47 -0500573/*****************************************************************************
574 * mos7840_interrupt_callback
575 * this is the callback function for when we have received data on the
576 * interrupt endpoint.
577 *****************************************************************************/
578
David Howells7d12e782006-10-05 14:55:46 +0100579static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500580{
581 int result;
582 int length;
583 struct moschip_port *mos7840_port;
584 struct usb_serial *serial;
585 __u16 Data;
586 unsigned char *data;
587 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100588 int i, rv = 0;
589 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700590 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500591
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700592 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500593 case 0:
594 /* success */
595 break;
596 case -ECONNRESET:
597 case -ENOENT:
598 case -ESHUTDOWN:
599 /* this urb is terminated, clean up */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700600 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
601 __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500602 return;
603 default:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700604 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
605 __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500606 goto exit;
607 }
608
609 length = urb->actual_length;
610 data = urb->transfer_buffer;
611
Ming Leicdc97792008-02-24 18:41:47 +0800612 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500613
614 /* Moschip get 5 bytes
615 * Byte 1 IIR Port 1 (port.number is 0)
616 * Byte 2 IIR Port 2 (port.number is 1)
617 * Byte 3 IIR Port 3 (port.number is 2)
618 * Byte 4 IIR Port 4 (port.number is 3)
619 * Byte 5 FIFO status for both */
620
621 if (length && length > 5) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700622 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500623 return;
624 }
625
626 sp[0] = (__u8) data[0];
627 sp[1] = (__u8) data[1];
628 sp[2] = (__u8) data[2];
629 sp[3] = (__u8) data[3];
630 st = (__u8) data[4];
631
632 for (i = 0; i < serial->num_ports; i++) {
633 mos7840_port = mos7840_get_port_private(serial->port[i]);
634 wval =
635 (((__u16) serial->port[i]->number -
636 (__u16) (serial->minor)) + 1) << 8;
637 if (mos7840_port->open) {
638 if (sp[i] & 0x01) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700639 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500640 } else {
641 switch (sp[i] & 0x0f) {
642 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700643 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
644 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500645 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100646 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500647 break;
648 case SERIAL_IIR_MS:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700649 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500650 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100651 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500652 break;
653 }
Johan Hovolde681b662012-10-25 18:56:33 +0200654 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500655 }
656 }
657 }
Alan Cox880af9d2008-07-22 11:16:12 +0100658 if (!(rv < 0))
659 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100660 return;
661exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500662 result = usb_submit_urb(urb, GFP_ATOMIC);
663 if (result) {
664 dev_err(&urb->dev->dev,
665 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800666 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500667 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500668}
669
670static int mos7840_port_paranoia_check(struct usb_serial_port *port,
671 const char *function)
672{
673 if (!port) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700674 pr_debug("%s - port == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500675 return -1;
676 }
677 if (!port->serial) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700678 pr_debug("%s - port->serial == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500679 return -1;
680 }
681
682 return 0;
683}
684
685/* Inline functions to check the sanity of a pointer that is passed to us */
686static int mos7840_serial_paranoia_check(struct usb_serial *serial,
687 const char *function)
688{
689 if (!serial) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700690 pr_debug("%s - serial == NULL\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500691 return -1;
692 }
693 if (!serial->type) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700694 pr_debug("%s - serial->type == NULL!\n", function);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500695 return -1;
696 }
697
698 return 0;
699}
700
701static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
702 const char *function)
703{
704 /* if no port was specified, or it fails a paranoia check */
705 if (!port ||
706 mos7840_port_paranoia_check(port, function) ||
707 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100708 /* then say that we don't have a valid usb_serial thing,
709 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500710 return NULL;
711 }
712
713 return port->serial;
714}
715
716/*****************************************************************************
717 * mos7840_bulk_in_callback
718 * this is the callback function for when we have received data on the
719 * bulk in endpoint.
720 *****************************************************************************/
721
David Howells7d12e782006-10-05 14:55:46 +0100722static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500723{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700724 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500725 unsigned char *data;
726 struct usb_serial *serial;
727 struct usb_serial_port *port;
728 struct moschip_port *mos7840_port;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700729 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500730
Ming Leicdc97792008-02-24 18:41:47 +0800731 mos7840_port = urb->context;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700732 if (!mos7840_port)
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800733 return;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800734
735 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700736 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800737 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500738 return;
739 }
740
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700741 port = mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800742 if (mos7840_port_paranoia_check(port, __func__)) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800743 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500744 return;
745 }
746
Harvey Harrison441b62c2008-03-03 16:08:34 -0800747 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500748 if (!serial) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800749 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500750 return;
751 }
752
Paul B Schroeder3f542972006-08-31 19:41:47 -0500753 data = urb->transfer_buffer;
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100754 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500755
Paul B Schroeder3f542972006-08-31 19:41:47 -0500756 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100757 struct tty_port *tport = &mos7840_port->port->port;
Jiri Slaby2e124b42013-01-03 15:53:06 +0100758 tty_insert_flip_string(tport, data, urb->actual_length);
759 tty_flip_buffer_push(tport);
Johan Hovold8c1a07f2013-03-21 12:37:16 +0100760 port->icount.rx += urb->actual_length;
761 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500762 }
763
764 if (!mos7840_port->read_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700765 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800766 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500767 return;
768 }
769
Donald0eafe4d2012-04-19 15:00:45 +0800770 /* Turn on LED */
771 if (mos7840_port->has_led && !mos7840_port->led_flag) {
772 mos7840_port->led_flag = true;
773 mos7840_set_led_async(mos7840_port, 0x0301,
774 MODEM_CONTROL_REGISTER);
775 mod_timer(&mos7840_port->led_timer1,
776 jiffies + msecs_to_jiffies(LED_ON_MS));
777 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500778
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800779 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700780 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100781
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700782 if (retval) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700783 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800784 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500785 }
786}
787
788/*****************************************************************************
789 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100790 * this is the callback function for when we have finished sending
791 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500792 *****************************************************************************/
793
David Howells7d12e782006-10-05 14:55:46 +0100794static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500795{
796 struct moschip_port *mos7840_port;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700797 struct usb_serial_port *port;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500798 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700799 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100800 int i;
801
Ming Leicdc97792008-02-24 18:41:47 +0800802 mos7840_port = urb->context;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700803 port = mos7840_port->port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100804 spin_lock(&mos7840_port->pool_lock);
805 for (i = 0; i < NUM_URBS; i++) {
806 if (urb == mos7840_port->write_urb_pool[i]) {
807 mos7840_port->busy[i] = 0;
808 break;
809 }
810 }
811 spin_unlock(&mos7840_port->pool_lock);
812
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700813 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700814 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500815 return;
816 }
817
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700818 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500819 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500820
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700821 tty = tty_port_tty_get(&port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800822 if (tty && mos7840_port->open)
823 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100824 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500825
826}
827
828/************************************************************************/
829/* 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 */
830/************************************************************************/
831#ifdef MCSSerialProbe
832static int mos7840_serial_probe(struct usb_serial *serial,
833 const struct usb_device_id *id)
834{
835
836 /*need to implement the mode_reg reading and updating\
837 structures usb_serial_ device_type\
838 (i.e num_ports, num_bulkin,bulkout etc) */
839 /* Also we can update the changes attach */
840 return 1;
841}
842#endif
843
844/*****************************************************************************
845 * mos7840_open
846 * this function is called by the tty driver when a port is opened
847 * If successful, we return 0
848 * Otherwise we return a negative error number.
849 *****************************************************************************/
850
Alan Coxa509a7e2009-09-19 13:13:26 -0700851static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500852{
853 int response;
854 int j;
855 struct usb_serial *serial;
856 struct urb *urb;
857 __u16 Data;
858 int status;
859 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100860 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500861
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700862 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500863 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500864
Paul B Schroeder3f542972006-08-31 19:41:47 -0500865 serial = port->serial;
866
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700867 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500868 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500869
870 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100871 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500872
Oliver Neukum0de9a702007-03-16 20:28:28 +0100873 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500874 return -ENODEV;
875
876 usb_clear_halt(serial->dev, port->write_urb->pipe);
877 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100878 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500879
880 /* Initialising the write urb pool */
881 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100882 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500883 mos7840_port->write_urb_pool[j] = urb;
884
885 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700886 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500887 continue;
888 }
889
Alan Cox880af9d2008-07-22 11:16:12 +0100890 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
891 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500892 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100893 usb_free_urb(urb);
894 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700895 dev_err(&port->dev,
896 "%s-out of memory for urb buffers.\n",
897 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500898 continue;
899 }
900 }
901
902/*****************************************************************************
903 * Initialize MCS7840 -- Write Init values to corresponding Registers
904 *
905 * Register Index
906 * 1 : IER
907 * 2 : FCR
908 * 3 : LCR
909 * 4 : MCR
910 *
911 * 0x08 : SP1/2 Control Reg
912 *****************************************************************************/
913
Alan Cox880af9d2008-07-22 11:16:12 +0100914 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500915
Paul B Schroeder3f542972006-08-31 19:41:47 -0500916 Data = 0x0;
917 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
918 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700919 dev_dbg(&port->dev, "Reading Spreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500920 return -1;
921 }
922 Data |= 0x80;
923 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
924 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700925 dev_dbg(&port->dev, "writing Spreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500926 return -1;
927 }
928
929 Data &= ~0x80;
930 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
931 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700932 dev_dbg(&port->dev, "writing Spreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500933 return -1;
934 }
Alan Cox880af9d2008-07-22 11:16:12 +0100935 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500936
Paul B Schroeder3f542972006-08-31 19:41:47 -0500937 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100938 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
939 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500940 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700941 dev_dbg(&port->dev, "Reading Controlreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500942 return -1;
943 }
Alan Cox880af9d2008-07-22 11:16:12 +0100944 Data |= 0x08; /* Driver done bit */
945 Data |= 0x20; /* rx_disable */
946 status = mos7840_set_reg_sync(port,
947 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500948 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700949 dev_dbg(&port->dev, "writing Controlreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500950 return -1;
951 }
Alan Cox880af9d2008-07-22 11:16:12 +0100952 /* do register settings here */
953 /* Set all regs to the device default values. */
954 /***********************************
955 * First Disable all interrupts.
956 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500957 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500958 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
959 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700960 dev_dbg(&port->dev, "disabling interrupts failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500961 return -1;
962 }
Alan Cox880af9d2008-07-22 11:16:12 +0100963 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500964 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500965 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
966 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700967 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500968 return -1;
969 }
970
971 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500972 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
973 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700974 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500975 return -1;
976 }
977
978 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500979 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
980 mos7840_port->shadowLCR = Data;
981
982 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500983 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
984 mos7840_port->shadowMCR = Data;
985
986 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500987 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
988 mos7840_port->shadowLCR = Data;
989
Alan Cox880af9d2008-07-22 11:16:12 +0100990 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500991 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
992
993 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500994 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
995
996 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500997 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
998
999 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001000 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1001
1002 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001003 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1004 mos7840_port->shadowLCR = Data;
1005
Alan Cox880af9d2008-07-22 11:16:12 +01001006 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001007 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001008 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1009
1010 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001011 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1012
1013 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001014 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001015 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001016 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001017 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1018
Alan Cox880af9d2008-07-22 11:16:12 +01001019 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001020 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001021 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1022 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001023 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001024 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1025 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001026
Alan Cox880af9d2008-07-22 11:16:12 +01001027 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001028 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001029 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1030 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001031 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001032 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1033 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001034
Alan Cox880af9d2008-07-22 11:16:12 +01001035 /* Check to see if we've set up our endpoint info yet *
1036 * (can't set it up in mos7840_startup as the structures *
1037 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001038 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001039 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001040 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001041 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001042 serial->dev,
1043 usb_rcvintpipe(serial->dev,
1044 serial->port[0]->interrupt_in_endpointAddress),
1045 serial->port[0]->interrupt_in_buffer,
1046 serial->port[0]->interrupt_in_urb->
1047 transfer_buffer_length,
1048 mos7840_interrupt_callback,
1049 serial,
1050 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001051
1052 /* start interrupt read for mos7840 *
1053 * will continue as long as mos7840 is connected */
1054
1055 response =
1056 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1057 GFP_KERNEL);
1058 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001059 dev_err(&port->dev, "%s - Error %d submitting "
1060 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001061 }
1062
1063 }
1064
1065 }
1066
1067 /* see if we've set up our endpoint info yet *
1068 * (can't set it up in mos7840_startup as the *
1069 * structures were not set up at that time.) */
1070
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001071 dev_dbg(&port->dev, "port number is %d\n", port->number);
1072 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1073 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1074 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1075 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1076 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 -05001077 mos7840_port->read_urb = port->read_urb;
1078
1079 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001080 if ((serial->num_ports == 2)
1081 && ((((__u16)port->number -
1082 (__u16)(port->serial->minor)) % 2) != 0)) {
1083 usb_fill_bulk_urb(mos7840_port->read_urb,
1084 serial->dev,
1085 usb_rcvbulkpipe(serial->dev,
1086 (port->bulk_in_endpointAddress) + 2),
1087 port->bulk_in_buffer,
1088 mos7840_port->read_urb->transfer_buffer_length,
1089 mos7840_bulk_in_callback, mos7840_port);
1090 } else {
1091 usb_fill_bulk_urb(mos7840_port->read_urb,
1092 serial->dev,
1093 usb_rcvbulkpipe(serial->dev,
1094 port->bulk_in_endpointAddress),
1095 port->bulk_in_buffer,
1096 mos7840_port->read_urb->transfer_buffer_length,
1097 mos7840_bulk_in_callback, mos7840_port);
1098 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001099
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001100 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001101 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001102 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1103 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001104 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1105 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001106 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001107 }
1108
1109 /* initialize our wait queues */
1110 init_waitqueue_head(&mos7840_port->wait_chase);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001111
Paul B Schroeder3f542972006-08-31 19:41:47 -05001112 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001113 /* Must set to enable ints! */
1114 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001115 /* send a open port command */
1116 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001117 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001118
Paul B Schroeder3f542972006-08-31 19:41:47 -05001119 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001120}
1121
1122/*****************************************************************************
1123 * mos7840_chars_in_buffer
1124 * this function is called by the tty driver when it wants to know how many
1125 * bytes of data we currently have outstanding in the port (data that has
1126 * been written, but hasn't made it out the port yet)
1127 * If successful, we return the number of bytes left to be written in the
1128 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001129 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001130 *****************************************************************************/
1131
Alan Cox95da3102008-07-22 11:09:07 +01001132static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001133{
Alan Cox95da3102008-07-22 11:09:07 +01001134 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001135 int i;
1136 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001137 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001138 struct moschip_port *mos7840_port;
1139
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001140 if (mos7840_port_paranoia_check(port, __func__))
Alan Cox95da3102008-07-22 11:09:07 +01001141 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001142
1143 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001144 if (mos7840_port == NULL)
Alan Cox95da3102008-07-22 11:09:07 +01001145 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001146
Alan Cox880af9d2008-07-22 11:16:12 +01001147 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell5c263b92012-07-24 14:15:13 -05001148 for (i = 0; i < NUM_URBS; ++i) {
1149 if (mos7840_port->busy[i]) {
1150 struct urb *urb = mos7840_port->write_urb_pool[i];
1151 chars += urb->transfer_buffer_length;
1152 }
1153 }
Alan Cox880af9d2008-07-22 11:16:12 +01001154 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001155 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001156 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001157
1158}
1159
Paul B Schroeder3f542972006-08-31 19:41:47 -05001160/*****************************************************************************
1161 * mos7840_close
1162 * this function is called by the tty driver when a port is closed
1163 *****************************************************************************/
1164
Alan Cox335f8512009-06-11 12:26:29 +01001165static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001166{
1167 struct usb_serial *serial;
1168 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001169 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001170 int j;
1171 __u16 Data;
1172
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001173 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001174 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001175
Harvey Harrison441b62c2008-03-03 16:08:34 -08001176 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001177 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001178 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001179
1180 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001181 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001182
Oliver Neukum0de9a702007-03-16 20:28:28 +01001183 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001184 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001185
1186 for (j = 0; j < NUM_URBS; ++j)
1187 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1188
1189 /* Freeing Write URBs */
1190 for (j = 0; j < NUM_URBS; ++j) {
1191 if (mos7840_port->write_urb_pool[j]) {
1192 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1193 kfree(mos7840_port->write_urb_pool[j]->
1194 transfer_buffer);
1195
1196 usb_free_urb(mos7840_port->write_urb_pool[j]);
1197 }
1198 }
1199
Johan Hovoldbb3529c2013-03-21 12:36:35 +01001200 usb_kill_urb(mos7840_port->write_urb);
1201 usb_kill_urb(mos7840_port->read_urb);
1202 mos7840_port->read_urb_busy = false;
1203
Oliver Neukum0de9a702007-03-16 20:28:28 +01001204 port0->open_ports--;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001205 dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001206 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001207 if (serial->port[0]->interrupt_in_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001208 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001209 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001210 }
1211 }
1212
1213 if (mos7840_port->write_urb) {
1214 /* if this urb had a transfer buffer already (old tx) free it */
Syam Sidhardhanae8d4872013-03-07 01:51:12 +05301215 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001216 usb_free_urb(mos7840_port->write_urb);
1217 }
1218
1219 Data = 0x0;
1220 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1221
1222 Data = 0x00;
1223 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1224
1225 mos7840_port->open = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001226}
1227
1228/************************************************************************
1229 *
1230 * mos7840_block_until_chase_response
1231 *
1232 * This function will block the close until one of the following:
1233 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001234 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001235 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1236 *
1237 ************************************************************************/
1238
Alan Cox95da3102008-07-22 11:09:07 +01001239static void mos7840_block_until_chase_response(struct tty_struct *tty,
1240 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001241{
Mark Ferrell1e658482012-07-24 13:38:31 -05001242 int timeout = msecs_to_jiffies(1000);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001243 int wait = 10;
1244 int count;
1245
1246 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001247 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001248
1249 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001250 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001251 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001252
1253 /* Block the thread for a while */
1254 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1255 timeout);
1256 /* No activity.. count down section */
1257 wait--;
1258 if (wait == 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001259 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001260 return;
1261 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001262 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001263 wait = 10;
1264 }
1265 }
1266
1267}
1268
1269/*****************************************************************************
1270 * mos7840_break
1271 * this function sends a break to the port
1272 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001273static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001274{
Alan Cox95da3102008-07-22 11:09:07 +01001275 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001276 unsigned char data;
1277 struct usb_serial *serial;
1278 struct moschip_port *mos7840_port;
1279
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001280 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001281 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001282
Harvey Harrison441b62c2008-03-03 16:08:34 -08001283 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001284 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001285 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001286
1287 mos7840_port = mos7840_get_port_private(port);
1288
Alan Cox880af9d2008-07-22 11:16:12 +01001289 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001290 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001291
Johan Hovoldbb3529c2013-03-21 12:36:35 +01001292 /* flush and block until tx is empty */
1293 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001294
Alan Cox95da3102008-07-22 11:09:07 +01001295 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001296 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001297 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001298 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001299
Alan Cox6b447f042009-01-02 13:48:56 +00001300 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001301 mos7840_port->shadowLCR = data;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001302 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001303 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1304 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001305}
1306
1307/*****************************************************************************
1308 * mos7840_write_room
1309 * this function is called by the tty driver when it wants to know how many
1310 * bytes of data we can accept for a specific port.
1311 * If successful, we return the amount of room that we have for this port
1312 * Otherwise we return a negative error number.
1313 *****************************************************************************/
1314
Alan Cox95da3102008-07-22 11:09:07 +01001315static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001316{
Alan Cox95da3102008-07-22 11:09:07 +01001317 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001318 int i;
1319 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001320 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001321 struct moschip_port *mos7840_port;
1322
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001323 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001324 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001325
1326 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001327 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001328 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001329
Oliver Neukum0de9a702007-03-16 20:28:28 +01001330 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001331 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001332 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001333 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001334 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001335 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001336
Oliver Neukum0de9a702007-03-16 20:28:28 +01001337 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001338 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001339 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001340
1341}
1342
1343/*****************************************************************************
1344 * mos7840_write
1345 * this function is called by the tty driver when data should be written to
1346 * the port.
1347 * If successful, we return the number of bytes written, otherwise we
1348 * return a negative error number.
1349 *****************************************************************************/
1350
Alan Cox95da3102008-07-22 11:09:07 +01001351static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001352 const unsigned char *data, int count)
1353{
1354 int status;
1355 int i;
1356 int bytes_sent = 0;
1357 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001358 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001359
1360 struct moschip_port *mos7840_port;
1361 struct usb_serial *serial;
1362 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001363 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001364 const unsigned char *current_position = data;
1365 unsigned char *data1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001366
1367#ifdef NOTMOS7840
1368 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001369 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1370 mos7840_port->shadowLCR = Data;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001371 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1372 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001373
Alan Cox880af9d2008-07-22 11:16:12 +01001374 /* Data = 0x03; */
1375 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1376 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001377
Alan Cox880af9d2008-07-22 11:16:12 +01001378 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001379 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1380
Alan Cox880af9d2008-07-22 11:16:12 +01001381 /* Data = 0x0c; */
1382 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001383 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001384 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001385 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001386
1387 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001388 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001389 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001390
1391 Data = Data & ~SERIAL_LCR_DLAB;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001392 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001393 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1394#endif
1395
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001396 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001397 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001398
1399 serial = port->serial;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001400 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001401 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001402
1403 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001404 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001405 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001406
1407 /* try to find a free urb in the list */
1408 urb = NULL;
1409
Oliver Neukum0de9a702007-03-16 20:28:28 +01001410 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001411 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001412 if (!mos7840_port->busy[i]) {
1413 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001414 urb = mos7840_port->write_urb_pool[i];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001415 dev_dbg(&port->dev, "URB:%d\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001416 break;
1417 }
1418 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001419 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001420
1421 if (urb == NULL) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001422 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001423 goto exit;
1424 }
1425
1426 if (urb->transfer_buffer == NULL) {
1427 urb->transfer_buffer =
1428 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1429
1430 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001431 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001432 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001433 goto exit;
1434 }
1435 }
1436 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1437
Al Viro97c49652006-10-09 20:29:03 +01001438 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001439
1440 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001441 if ((serial->num_ports == 2)
1442 && ((((__u16)port->number -
1443 (__u16)(port->serial->minor)) % 2) != 0)) {
1444 usb_fill_bulk_urb(urb,
1445 serial->dev,
1446 usb_sndbulkpipe(serial->dev,
1447 (port->bulk_out_endpointAddress) + 2),
1448 urb->transfer_buffer,
1449 transfer_size,
1450 mos7840_bulk_out_data_callback, mos7840_port);
1451 } else {
1452 usb_fill_bulk_urb(urb,
1453 serial->dev,
1454 usb_sndbulkpipe(serial->dev,
1455 port->bulk_out_endpointAddress),
1456 urb->transfer_buffer,
1457 transfer_size,
1458 mos7840_bulk_out_data_callback, mos7840_port);
1459 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001460
1461 data1 = urb->transfer_buffer;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001462 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001463
Donald0eafe4d2012-04-19 15:00:45 +08001464 /* Turn on LED */
1465 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1466 mos7840_port->led_flag = true;
1467 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1468 mod_timer(&mos7840_port->led_timer1,
1469 jiffies + msecs_to_jiffies(LED_ON_MS));
1470 }
1471
Paul B Schroeder3f542972006-08-31 19:41:47 -05001472 /* send it down the pipe */
1473 status = usb_submit_urb(urb, GFP_ATOMIC);
1474
1475 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001476 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001477 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001478 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001479 bytes_sent = status;
1480 goto exit;
1481 }
1482 bytes_sent = transfer_size;
Johan Hovold8c1a07f2013-03-21 12:37:16 +01001483 port->icount.tx += transfer_size;
1484 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001485exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001486 return bytes_sent;
1487
1488}
1489
1490/*****************************************************************************
1491 * mos7840_throttle
1492 * this function is called by the tty driver when it wants to stop the data
1493 * being read from the port.
1494 *****************************************************************************/
1495
Alan Cox95da3102008-07-22 11:09:07 +01001496static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001497{
Alan Cox95da3102008-07-22 11:09:07 +01001498 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001499 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001500 int status;
1501
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001502 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001503 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001504
1505 mos7840_port = mos7840_get_port_private(port);
1506
1507 if (mos7840_port == NULL)
1508 return;
1509
1510 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001511 dev_dbg(&port->dev, "%s", "port not opened\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001512 return;
1513 }
1514
Paul B Schroeder3f542972006-08-31 19:41:47 -05001515 /* if we are implementing XON/XOFF, send the stop character */
1516 if (I_IXOFF(tty)) {
1517 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001518 status = mos7840_write(tty, port, &stop_char, 1);
1519 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001520 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001521 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001522 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001523 if (tty->termios.c_cflag & CRTSCTS) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001524 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001525 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001526 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001527 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001528 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001529 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001530}
1531
1532/*****************************************************************************
1533 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001534 * this function is called by the tty driver when it wants to resume
1535 * the data being read from the port (called after mos7840_throttle is
1536 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001537 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001538static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001539{
Alan Cox95da3102008-07-22 11:09:07 +01001540 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001541 int status;
1542 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1543
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001544 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001545 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001546
1547 if (mos7840_port == NULL)
1548 return;
1549
1550 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001551 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001552 return;
1553 }
1554
Paul B Schroeder3f542972006-08-31 19:41:47 -05001555 /* if we are implementing XON/XOFF, send the start character */
1556 if (I_IXOFF(tty)) {
1557 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001558 status = mos7840_write(tty, port, &start_char, 1);
1559 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001560 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001561 }
1562
1563 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001564 if (tty->termios.c_cflag & CRTSCTS) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001565 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001566 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001567 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001568 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001569 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001570 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001571}
1572
Alan Cox60b33c12011-02-14 16:26:14 +00001573static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001574{
Alan Cox95da3102008-07-22 11:09:07 +01001575 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001576 struct moschip_port *mos7840_port;
1577 unsigned int result;
1578 __u16 msr;
1579 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001580 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001581 mos7840_port = mos7840_get_port_private(port);
1582
Paul B Schroeder3f542972006-08-31 19:41:47 -05001583 if (mos7840_port == NULL)
1584 return -ENODEV;
1585
1586 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1587 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1588 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1589 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1590 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1591 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1592 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1593 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1594 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1595
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001596 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001597
1598 return result;
1599}
1600
Alan Cox20b9d172011-02-14 16:26:50 +00001601static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001602 unsigned int set, unsigned int clear)
1603{
Alan Cox95da3102008-07-22 11:09:07 +01001604 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001605 struct moschip_port *mos7840_port;
1606 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001607 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001608
Paul B Schroeder3f542972006-08-31 19:41:47 -05001609 mos7840_port = mos7840_get_port_private(port);
1610
1611 if (mos7840_port == NULL)
1612 return -ENODEV;
1613
Alan Coxe2984492008-02-20 20:51:45 +00001614 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001615 mcr = mos7840_port->shadowMCR;
1616 if (clear & TIOCM_RTS)
1617 mcr &= ~MCR_RTS;
1618 if (clear & TIOCM_DTR)
1619 mcr &= ~MCR_DTR;
1620 if (clear & TIOCM_LOOP)
1621 mcr &= ~MCR_LOOPBACK;
1622
1623 if (set & TIOCM_RTS)
1624 mcr |= MCR_RTS;
1625 if (set & TIOCM_DTR)
1626 mcr |= MCR_DTR;
1627 if (set & TIOCM_LOOP)
1628 mcr |= MCR_LOOPBACK;
1629
1630 mos7840_port->shadowMCR = mcr;
1631
Paul B Schroeder3f542972006-08-31 19:41:47 -05001632 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1633 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001634 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
Roel Kluin87521c42008-04-17 06:16:24 +02001635 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001636 }
1637
1638 return 0;
1639}
1640
1641/*****************************************************************************
1642 * mos7840_calc_baud_rate_divisor
1643 * this function calculates the proper baud rate divisor for the specified
1644 * baud rate.
1645 *****************************************************************************/
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001646static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1647 int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001648 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001649{
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001650 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001651
1652 if (baudRate <= 115200) {
1653 *divisor = 115200 / baudRate;
1654 *clk_sel_val = 0x0;
1655 }
1656 if ((baudRate > 115200) && (baudRate <= 230400)) {
1657 *divisor = 230400 / baudRate;
1658 *clk_sel_val = 0x10;
1659 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1660 *divisor = 403200 / baudRate;
1661 *clk_sel_val = 0x20;
1662 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1663 *divisor = 460800 / baudRate;
1664 *clk_sel_val = 0x30;
1665 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1666 *divisor = 806400 / baudRate;
1667 *clk_sel_val = 0x40;
1668 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1669 *divisor = 921600 / baudRate;
1670 *clk_sel_val = 0x50;
1671 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1672 *divisor = 1572864 / baudRate;
1673 *clk_sel_val = 0x60;
1674 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1675 *divisor = 3145728 / baudRate;
1676 *clk_sel_val = 0x70;
1677 }
1678 return 0;
1679
1680#ifdef NOTMCS7840
1681
1682 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1683 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1684 *divisor = mos7840_divisor_table[i].Divisor;
1685 return 0;
1686 }
1687 }
1688
1689 /* After trying for all the standard baud rates *
1690 * Try calculating the divisor for this baud rate */
1691
1692 if (baudrate > 75 && baudrate < 230400) {
1693 /* get the divisor */
1694 custom = (__u16) (230400L / baudrate);
1695
1696 /* Check for round off */
1697 round1 = (__u16) (2304000L / baudrate);
1698 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001699 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001700 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001701 *divisor = custom;
1702
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001703 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001704 return 0;
1705 }
1706
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001707 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001708 return -1;
1709#endif
1710}
1711
1712/*****************************************************************************
1713 * mos7840_send_cmd_write_baud_rate
1714 * this function sends the proper command to change the baud rate of the
1715 * specified port.
1716 *****************************************************************************/
1717
1718static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1719 int baudRate)
1720{
1721 int divisor = 0;
1722 int status;
1723 __u16 Data;
1724 unsigned char number;
1725 __u16 clk_sel_val;
1726 struct usb_serial_port *port;
1727
1728 if (mos7840_port == NULL)
1729 return -1;
1730
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001731 port = mos7840_port->port;
1732 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001733 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001734
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001735 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001736 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001737
Paul B Schroeder3f542972006-08-31 19:41:47 -05001738 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1739
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001740 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1741 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001742 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001743 if (baudRate > 115200) {
1744#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001745 /* NOTE: need to see the pther register to modify */
1746 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001747 Data = 0x2b;
1748 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001749 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1750 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001751 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001752 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001753 return -1;
1754 }
1755#endif
1756
1757 } else {
1758#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001759 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001760 Data = 0xb;
1761 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001762 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1763 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001764 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001765 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001766 return -1;
1767 }
1768#endif
1769
1770 }
1771
Alan Cox880af9d2008-07-22 11:16:12 +01001772 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001773 clk_sel_val = 0x0;
1774 Data = 0x0;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001775 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001776 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001777 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1778 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001779 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001780 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001781 return -1;
1782 }
1783 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001784 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1785 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001786 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001787 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001788 return -1;
1789 }
1790 /* Calculate the Divisor */
1791
1792 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001793 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001794 return status;
1795 }
1796 /* Enable access to divisor latch */
1797 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1798 mos7840_port->shadowLCR = Data;
1799 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1800
1801 /* Write the divisor */
1802 Data = (unsigned char)(divisor & 0xff);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001803 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001804 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1805
1806 Data = (unsigned char)((divisor & 0xff00) >> 8);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001807 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001808 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1809
1810 /* Disable access to divisor latch */
1811 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1812 mos7840_port->shadowLCR = Data;
1813 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1814
1815 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001816 return status;
1817}
1818
1819/*****************************************************************************
1820 * mos7840_change_port_settings
1821 * This routine is called to set the UART on the device to match
1822 * the specified new settings.
1823 *****************************************************************************/
1824
Alan Cox95da3102008-07-22 11:09:07 +01001825static void mos7840_change_port_settings(struct tty_struct *tty,
1826 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001827{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001828 int baud;
1829 unsigned cflag;
1830 unsigned iflag;
1831 __u8 lData;
1832 __u8 lParity;
1833 __u8 lStop;
1834 int status;
1835 __u16 Data;
1836 struct usb_serial_port *port;
1837 struct usb_serial *serial;
1838
1839 if (mos7840_port == NULL)
1840 return;
1841
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001842 port = mos7840_port->port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001843
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001844 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001845 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001846
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001847 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001848 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001849
1850 serial = port->serial;
1851
Paul B Schroeder3f542972006-08-31 19:41:47 -05001852 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001853 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001854 return;
1855 }
1856
Paul B Schroeder3f542972006-08-31 19:41:47 -05001857 lData = LCR_BITS_8;
1858 lStop = LCR_STOP_1;
1859 lParity = LCR_PAR_NONE;
1860
Alan Coxadc8d742012-07-14 15:31:47 +01001861 cflag = tty->termios.c_cflag;
1862 iflag = tty->termios.c_iflag;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001863
1864 /* Change the number of bits */
1865 if (cflag & CSIZE) {
1866 switch (cflag & CSIZE) {
1867 case CS5:
1868 lData = LCR_BITS_5;
1869 break;
1870
1871 case CS6:
1872 lData = LCR_BITS_6;
1873 break;
1874
1875 case CS7:
1876 lData = LCR_BITS_7;
1877 break;
1878 default:
1879 case CS8:
1880 lData = LCR_BITS_8;
1881 break;
1882 }
1883 }
1884 /* Change the Parity bit */
1885 if (cflag & PARENB) {
1886 if (cflag & PARODD) {
1887 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001888 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001889 } else {
1890 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001891 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001892 }
1893
1894 } else {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001895 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001896 }
1897
Alan Cox880af9d2008-07-22 11:16:12 +01001898 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001899 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001900
1901 /* Change the Stop bit */
1902 if (cflag & CSTOPB) {
1903 lStop = LCR_STOP_2;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001904 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001905 } else {
1906 lStop = LCR_STOP_1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001907 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001908 }
1909
1910 /* Update the LCR with the correct value */
1911 mos7840_port->shadowLCR &=
1912 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1913 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1914
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001915 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1916 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001917 /* Disable Interrupts */
1918 Data = 0x00;
1919 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1920
1921 Data = 0x00;
1922 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1923
1924 Data = 0xcf;
1925 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1926
1927 /* Send the updated LCR value to the mos7840 */
1928 Data = mos7840_port->shadowLCR;
1929
1930 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1931
1932 Data = 0x00b;
1933 mos7840_port->shadowMCR = Data;
1934 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1935 Data = 0x00b;
1936 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1937
1938 /* set up the MCR register and send it to the mos7840 */
1939
1940 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01001941 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001942 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001943
Alan Cox880af9d2008-07-22 11:16:12 +01001944 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001945 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01001946 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001947 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001948
1949 Data = mos7840_port->shadowMCR;
1950 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1951
1952 /* Determine divisor based on baud rate */
1953 baud = tty_get_baud_rate(tty);
1954
1955 if (!baud) {
1956 /* pick a default, any default... */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001957 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001958 baud = 9600;
1959 }
1960
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001961 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001962 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1963
1964 /* Enable Interrupts */
1965 Data = 0x0c;
1966 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1967
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001968 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001969 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001970 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001971 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001972 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001973 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001974 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001975 }
1976 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001977 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1978 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001979}
1980
1981/*****************************************************************************
1982 * mos7840_set_termios
1983 * this function is called by the tty driver when it wants to change
1984 * the termios structure
1985 *****************************************************************************/
1986
Alan Cox95da3102008-07-22 11:09:07 +01001987static void mos7840_set_termios(struct tty_struct *tty,
1988 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001989 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001990{
1991 int status;
1992 unsigned int cflag;
1993 struct usb_serial *serial;
1994 struct moschip_port *mos7840_port;
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001995
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001996 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001997 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001998
1999 serial = port->serial;
2000
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002001 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002002 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002003
2004 mos7840_port = mos7840_get_port_private(port);
2005
2006 if (mos7840_port == NULL)
2007 return;
2008
Paul B Schroeder3f542972006-08-31 19:41:47 -05002009 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002010 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002011 return;
2012 }
2013
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002014 dev_dbg(&port->dev, "%s", "setting termios - \n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002015
Alan Coxadc8d742012-07-14 15:31:47 +01002016 cflag = tty->termios.c_cflag;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002017
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002018 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07002019 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002020 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2021 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2022 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002023
2024 /* change the port settings to the new ones specified */
2025
Alan Cox95da3102008-07-22 11:09:07 +01002026 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002027
2028 if (!mos7840_port->read_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002029 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002030 return;
2031 }
2032
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002033 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002034 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002035 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2036 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002037 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002038 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002039 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002040 }
2041 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002042}
2043
2044/*****************************************************************************
2045 * mos7840_get_lsr_info - get line status register info
2046 *
2047 * Purpose: Let user call ioctl() to get info when the UART physically
2048 * is emptied. On bus types like RS485, the transmitter must
2049 * release the bus after transmitting. This must be done when
2050 * the transmit shift register is empty, not be done when the
2051 * transmit holding register is empty. This functionality
2052 * allows an RS485 driver to be written in user space.
2053 *****************************************************************************/
2054
Alan Cox95da3102008-07-22 11:09:07 +01002055static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002056 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002057{
2058 int count;
2059 unsigned int result = 0;
2060
Alan Cox95da3102008-07-22 11:09:07 +01002061 count = mos7840_chars_in_buffer(tty);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002062 if (count == 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002063 result = TIOCSER_TEMT;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002064
2065 if (copy_to_user(value, &result, sizeof(int)))
2066 return -EFAULT;
2067 return 0;
2068}
2069
2070/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002071 * mos7840_get_serial_info
2072 * function to get information about serial port
2073 *****************************************************************************/
2074
2075static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002076 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002077{
2078 struct serial_struct tmp;
2079
2080 if (mos7840_port == NULL)
2081 return -1;
2082
2083 if (!retinfo)
2084 return -EFAULT;
2085
2086 memset(&tmp, 0, sizeof(tmp));
2087
2088 tmp.type = PORT_16550A;
2089 tmp.line = mos7840_port->port->serial->minor;
2090 tmp.port = mos7840_port->port->number;
2091 tmp.irq = 0;
2092 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2093 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2094 tmp.baud_base = 9600;
2095 tmp.close_delay = 5 * HZ;
2096 tmp.closing_wait = 30 * HZ;
2097
2098 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2099 return -EFAULT;
2100 return 0;
2101}
2102
2103/*****************************************************************************
2104 * SerialIoctl
2105 * this function handles any ioctl calls to the driver
2106 *****************************************************************************/
2107
Alan Cox00a0d0d2011-02-14 16:27:06 +00002108static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002109 unsigned int cmd, unsigned long arg)
2110{
Alan Cox95da3102008-07-22 11:09:07 +01002111 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002112 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002113 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002114
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002115 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002116 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002117
2118 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002119
2120 if (mos7840_port == NULL)
2121 return -1;
2122
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002123 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002124
2125 switch (cmd) {
2126 /* return number of bytes available */
2127
Paul B Schroeder3f542972006-08-31 19:41:47 -05002128 case TIOCSERGETLSR:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002129 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01002130 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002131
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132 case TIOCGSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002133 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Al Viro97c49652006-10-09 20:29:03 +01002134 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002135
2136 case TIOCSSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002137 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002138 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002139 default:
2140 break;
2141 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002142 return -ENOIOCTLCMD;
2143}
2144
Donald0eafe4d2012-04-19 15:00:45 +08002145static int mos7810_check(struct usb_serial *serial)
2146{
2147 int i, pass_count = 0;
2148 __u16 data = 0, mcr_data = 0;
2149 __u16 test_pattern = 0x55AA;
2150
2151 /* Store MCR setting */
2152 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2153 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2154 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2155
2156 for (i = 0; i < 16; i++) {
2157 /* Send the 1-bit test pattern out to MCS7810 test pin */
2158 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2159 MCS_WRREQ, MCS_WR_RTYPE,
2160 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2161 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2162
2163 /* Read the test pattern back */
2164 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2165 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2166 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2167
2168 /* If this is a MCS7810 device, both test patterns must match */
2169 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2170 break;
2171
2172 pass_count++;
2173 }
2174
2175 /* Restore MCR setting */
2176 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2177 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2178 0, MOS_WDR_TIMEOUT);
2179
2180 if (pass_count == 16)
2181 return 1;
2182
2183 return 0;
2184}
2185
Paul B Schroeder3f542972006-08-31 19:41:47 -05002186static int mos7840_calc_num_ports(struct usb_serial *serial)
2187{
Donald0eafe4d2012-04-19 15:00:45 +08002188 __u16 data = 0x00;
Donald Lee093ea2d2012-03-14 15:26:33 +08002189 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002190
Donald0eafe4d2012-04-19 15:00:45 +08002191 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2192 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
Donald Lee093ea2d2012-03-14 15:26:33 +08002193 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2194
Donald0eafe4d2012-04-19 15:00:45 +08002195 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2196 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2197 device_type = serial->dev->descriptor.idProduct;
Donald Lee093ea2d2012-03-14 15:26:33 +08002198 } else {
Donald0eafe4d2012-04-19 15:00:45 +08002199 /* For a MCS7840 device GPIO0 must be set to 1 */
2200 if ((data & 0x01) == 1)
2201 device_type = MOSCHIP_DEVICE_ID_7840;
2202 else if (mos7810_check(serial))
2203 device_type = MOSCHIP_DEVICE_ID_7810;
2204 else
2205 device_type = MOSCHIP_DEVICE_ID_7820;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002206 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002207
Donald0eafe4d2012-04-19 15:00:45 +08002208 mos7840_num_ports = (device_type >> 4) & 0x000F;
2209 serial->num_bulk_in = mos7840_num_ports;
2210 serial->num_bulk_out = mos7840_num_ports;
2211 serial->num_ports = mos7840_num_ports;
2212
Paul B Schroeder3f542972006-08-31 19:41:47 -05002213 return mos7840_num_ports;
2214}
2215
Johan Hovold80c00752012-10-25 18:56:34 +02002216static int mos7840_port_probe(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002217{
Johan Hovold80c00752012-10-25 18:56:34 +02002218 struct usb_serial *serial = port->serial;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002219 struct moschip_port *mos7840_port;
Johan Hovold80c00752012-10-25 18:56:34 +02002220 int status;
2221 int pnum;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002222 __u16 Data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002223
Paul B Schroeder3f542972006-08-31 19:41:47 -05002224 /* we set up the pointers to the endpoints in the mos7840_open *
2225 * function, as the structures aren't created yet. */
2226
Johan Hovold80c00752012-10-25 18:56:34 +02002227 pnum = port->number - serial->minor;
2228
Johan Hovoldae685ef2012-10-25 18:56:35 +02002229 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2230 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2231 if (mos7840_port == NULL) {
2232 dev_err(&port->dev, "%s - Out of memory\n", __func__);
2233 return -ENOMEM;
2234 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002235
Johan Hovoldae685ef2012-10-25 18:56:35 +02002236 /* Initialize all port interrupt end point to port 0 int
2237 * endpoint. Our device has only one interrupt end point
2238 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002239
Johan Hovoldae685ef2012-10-25 18:56:35 +02002240 mos7840_port->port = port;
2241 mos7840_set_port_private(port, mos7840_port);
2242 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002243
Johan Hovoldae685ef2012-10-25 18:56:35 +02002244 /* minor is not initialised until later by
2245 * usb-serial.c:get_free_serial() and cannot therefore be used
2246 * to index device instances */
2247 mos7840_port->port_num = pnum + 1;
2248 dev_dbg(&port->dev, "port->number = %d\n", port->number);
2249 dev_dbg(&port->dev, "port->serial->minor = %d\n", port->serial->minor);
2250 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2251 dev_dbg(&port->dev, "serial->minor = %d\n", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002252
Johan Hovoldae685ef2012-10-25 18:56:35 +02002253 if (mos7840_port->port_num == 1) {
2254 mos7840_port->SpRegOffset = 0x0;
2255 mos7840_port->ControlRegOffset = 0x1;
2256 mos7840_port->DcrRegOffset = 0x4;
2257 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2258 mos7840_port->SpRegOffset = 0x8;
2259 mos7840_port->ControlRegOffset = 0x9;
2260 mos7840_port->DcrRegOffset = 0x16;
2261 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2262 mos7840_port->SpRegOffset = 0xa;
2263 mos7840_port->ControlRegOffset = 0xb;
2264 mos7840_port->DcrRegOffset = 0x19;
2265 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2266 mos7840_port->SpRegOffset = 0xa;
2267 mos7840_port->ControlRegOffset = 0xb;
2268 mos7840_port->DcrRegOffset = 0x19;
2269 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2270 mos7840_port->SpRegOffset = 0xc;
2271 mos7840_port->ControlRegOffset = 0xd;
2272 mos7840_port->DcrRegOffset = 0x1c;
2273 }
2274 mos7840_dump_serial_port(port, mos7840_port);
2275 mos7840_set_port_private(port, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002276
Johan Hovoldae685ef2012-10-25 18:56:35 +02002277 /* enable rx_disable bit in control register */
2278 status = mos7840_get_reg_sync(port,
2279 mos7840_port->ControlRegOffset, &Data);
2280 if (status < 0) {
2281 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2282 goto out;
2283 } else
2284 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2285 Data |= 0x08; /* setting driver done bit */
2286 Data |= 0x04; /* sp1_bit to have cts change reflect in
2287 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002288
Johan Hovoldae685ef2012-10-25 18:56:35 +02002289 /* Data |= 0x20; //rx_disable bit */
2290 status = mos7840_set_reg_sync(port,
2291 mos7840_port->ControlRegOffset, Data);
2292 if (status < 0) {
2293 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2294 goto out;
2295 } else
2296 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2297
2298 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2299 and 0x24 in DCR3 */
2300 Data = 0x01;
2301 status = mos7840_set_reg_sync(port,
2302 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2303 if (status < 0) {
2304 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2305 goto out;
2306 } else
2307 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2308
2309 Data = 0x05;
2310 status = mos7840_set_reg_sync(port,
2311 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2312 if (status < 0) {
2313 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2314 goto out;
2315 } else
2316 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2317
2318 Data = 0x24;
2319 status = mos7840_set_reg_sync(port,
2320 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2321 if (status < 0) {
2322 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2323 goto out;
2324 } else
2325 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2326
2327 /* write values in clkstart0x0 and clkmulti 0x20 */
2328 Data = 0x0;
2329 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2330 if (status < 0) {
2331 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2332 goto out;
2333 } else
2334 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2335
2336 Data = 0x20;
2337 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2338 if (status < 0) {
2339 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2340 goto error;
2341 } else
2342 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2343
2344 /* write value 0x0 to scratchpad register */
2345 Data = 0x00;
2346 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2347 if (status < 0) {
2348 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2349 goto out;
2350 } else
2351 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2352
2353 /* Zero Length flag register */
2354 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2355 Data = 0xff;
Johan Hovold80c00752012-10-25 18:56:34 +02002356 status = mos7840_set_reg_sync(port,
Johan Hovoldae685ef2012-10-25 18:56:35 +02002357 (__u16) (ZLP_REG1 +
2358 ((__u16)mos7840_port->port_num)), Data);
2359 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002360 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002361 if (status < 0) {
2362 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2363 goto out;
2364 } else
2365 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2366 } else {
2367 Data = 0xff;
2368 status = mos7840_set_reg_sync(port,
2369 (__u16) (ZLP_REG1 +
2370 ((__u16)mos7840_port->port_num) - 0x1), Data);
2371 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002372 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002373 if (status < 0) {
2374 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2375 goto out;
2376 } else
2377 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002378
Johan Hovoldae685ef2012-10-25 18:56:35 +02002379 }
2380 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2381 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2382 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2383 GFP_KERNEL);
2384 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2385 !mos7840_port->dr) {
2386 status = -ENOMEM;
2387 goto error;
2388 }
Donald0eafe4d2012-04-19 15:00:45 +08002389
Johan Hovoldae685ef2012-10-25 18:56:35 +02002390 mos7840_port->has_led = false;
Donald0eafe4d2012-04-19 15:00:45 +08002391
Johan Hovoldae685ef2012-10-25 18:56:35 +02002392 /* Initialize LED timers */
2393 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2394 mos7840_port->has_led = true;
Donald0eafe4d2012-04-19 15:00:45 +08002395
Johan Hovoldae685ef2012-10-25 18:56:35 +02002396 init_timer(&mos7840_port->led_timer1);
2397 mos7840_port->led_timer1.function = mos7840_led_off;
2398 mos7840_port->led_timer1.expires =
2399 jiffies + msecs_to_jiffies(LED_ON_MS);
2400 mos7840_port->led_timer1.data = (unsigned long)mos7840_port;
Donald0eafe4d2012-04-19 15:00:45 +08002401
Johan Hovoldae685ef2012-10-25 18:56:35 +02002402 init_timer(&mos7840_port->led_timer2);
2403 mos7840_port->led_timer2.function = mos7840_led_flag_off;
2404 mos7840_port->led_timer2.expires =
2405 jiffies + msecs_to_jiffies(LED_OFF_MS);
2406 mos7840_port->led_timer2.data = (unsigned long)mos7840_port;
Donald0eafe4d2012-04-19 15:00:45 +08002407
Johan Hovoldae685ef2012-10-25 18:56:35 +02002408 mos7840_port->led_flag = false;
Donald0eafe4d2012-04-19 15:00:45 +08002409
Johan Hovoldae685ef2012-10-25 18:56:35 +02002410 /* Turn off LED */
2411 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2412 }
2413out:
Johan Hovold80c00752012-10-25 18:56:34 +02002414 if (pnum == serial->num_ports - 1) {
2415 /* Zero Length flag enable */
2416 Data = 0x0f;
2417 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2418 if (status < 0) {
2419 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2420 goto error;
2421 } else
2422 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2423
2424 /* setting configuration feature to one */
2425 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2426 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2427 MOS_WDR_TIMEOUT);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002428 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002429 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002430error:
Johan Hovold80c00752012-10-25 18:56:34 +02002431 kfree(mos7840_port->dr);
2432 kfree(mos7840_port->ctrl_buf);
2433 usb_free_urb(mos7840_port->control_urb);
2434 kfree(mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002435
Oliver Neukum0de9a702007-03-16 20:28:28 +01002436 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002437}
2438
Johan Hovold80c00752012-10-25 18:56:34 +02002439static int mos7840_port_remove(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002440{
Paul B Schroeder3f542972006-08-31 19:41:47 -05002441 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002442
Johan Hovold80c00752012-10-25 18:56:34 +02002443 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002444
Johan Hovold80c00752012-10-25 18:56:34 +02002445 if (mos7840_port->has_led) {
2446 /* Turn off LED */
2447 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002448
Johan Hovold80c00752012-10-25 18:56:34 +02002449 del_timer_sync(&mos7840_port->led_timer1);
2450 del_timer_sync(&mos7840_port->led_timer2);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002451 }
Johan Hovold80c00752012-10-25 18:56:34 +02002452 usb_kill_urb(mos7840_port->control_urb);
2453 usb_free_urb(mos7840_port->control_urb);
2454 kfree(mos7840_port->ctrl_buf);
2455 kfree(mos7840_port->dr);
2456 kfree(mos7840_port);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002457
Johan Hovold80c00752012-10-25 18:56:34 +02002458 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002459}
2460
2461static struct usb_serial_driver moschip7840_4port_device = {
2462 .driver = {
2463 .owner = THIS_MODULE,
2464 .name = "mos7840",
2465 },
2466 .description = DRIVER_DESC,
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002467 .id_table = id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002468 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002469 .open = mos7840_open,
2470 .close = mos7840_close,
2471 .write = mos7840_write,
2472 .write_room = mos7840_write_room,
2473 .chars_in_buffer = mos7840_chars_in_buffer,
2474 .throttle = mos7840_throttle,
2475 .unthrottle = mos7840_unthrottle,
2476 .calc_num_ports = mos7840_calc_num_ports,
2477#ifdef MCSSerialProbe
2478 .probe = mos7840_serial_probe,
2479#endif
2480 .ioctl = mos7840_ioctl,
2481 .set_termios = mos7840_set_termios,
2482 .break_ctl = mos7840_break,
2483 .tiocmget = mos7840_tiocmget,
2484 .tiocmset = mos7840_tiocmset,
Johan Hovold0c6133712013-03-21 12:37:17 +01002485 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovold8c1a07f2013-03-21 12:37:16 +01002486 .get_icount = usb_serial_generic_get_icount,
Johan Hovold80c00752012-10-25 18:56:34 +02002487 .port_probe = mos7840_port_probe,
2488 .port_remove = mos7840_port_remove,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002489 .read_bulk_callback = mos7840_bulk_in_callback,
2490 .read_int_callback = mos7840_interrupt_callback,
2491};
2492
Alan Stern4d2a7af2012-02-23 14:57:09 -05002493static struct usb_serial_driver * const serial_drivers[] = {
2494 &moschip7840_4port_device, NULL
2495};
2496
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002497module_usb_serial_driver(serial_drivers, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002498
Paul B Schroeder3f542972006-08-31 19:41:47 -05002499MODULE_DESCRIPTION(DRIVER_DESC);
2500MODULE_LICENSE("GPL");