blob: 7e998081e1cd9b42651143e7f01e63286410393d [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 Hovold0c613372013-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;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700798 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100799 int i;
800
Ming Leicdc97792008-02-24 18:41:47 +0800801 mos7840_port = urb->context;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700802 port = mos7840_port->port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100803 spin_lock(&mos7840_port->pool_lock);
804 for (i = 0; i < NUM_URBS; i++) {
805 if (urb == mos7840_port->write_urb_pool[i]) {
806 mos7840_port->busy[i] = 0;
807 break;
808 }
809 }
810 spin_unlock(&mos7840_port->pool_lock);
811
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700812 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700813 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500814 return;
815 }
816
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700817 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500818 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500819
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100820 if (mos7840_port->open)
821 tty_port_tty_wakeup(&port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500822
823}
824
825/************************************************************************/
826/* 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 */
827/************************************************************************/
828#ifdef MCSSerialProbe
829static int mos7840_serial_probe(struct usb_serial *serial,
830 const struct usb_device_id *id)
831{
832
833 /*need to implement the mode_reg reading and updating\
834 structures usb_serial_ device_type\
835 (i.e num_ports, num_bulkin,bulkout etc) */
836 /* Also we can update the changes attach */
837 return 1;
838}
839#endif
840
841/*****************************************************************************
842 * mos7840_open
843 * this function is called by the tty driver when a port is opened
844 * If successful, we return 0
845 * Otherwise we return a negative error number.
846 *****************************************************************************/
847
Alan Coxa509a7e2009-09-19 13:13:26 -0700848static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500849{
850 int response;
851 int j;
852 struct usb_serial *serial;
853 struct urb *urb;
854 __u16 Data;
855 int status;
856 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100857 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500858
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700859 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500860 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500861
Paul B Schroeder3f542972006-08-31 19:41:47 -0500862 serial = port->serial;
863
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700864 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500865 return -ENODEV;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500866
867 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100868 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500869
Oliver Neukum0de9a702007-03-16 20:28:28 +0100870 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500871 return -ENODEV;
872
873 usb_clear_halt(serial->dev, port->write_urb->pipe);
874 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100875 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500876
877 /* Initialising the write urb pool */
878 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100879 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500880 mos7840_port->write_urb_pool[j] = urb;
881
882 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700883 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500884 continue;
885 }
886
Alan Cox880af9d2008-07-22 11:16:12 +0100887 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
888 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500889 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100890 usb_free_urb(urb);
891 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700892 dev_err(&port->dev,
893 "%s-out of memory for urb buffers.\n",
894 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500895 continue;
896 }
897 }
898
899/*****************************************************************************
900 * Initialize MCS7840 -- Write Init values to corresponding Registers
901 *
902 * Register Index
903 * 1 : IER
904 * 2 : FCR
905 * 3 : LCR
906 * 4 : MCR
907 *
908 * 0x08 : SP1/2 Control Reg
909 *****************************************************************************/
910
Alan Cox880af9d2008-07-22 11:16:12 +0100911 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500912
Paul B Schroeder3f542972006-08-31 19:41:47 -0500913 Data = 0x0;
914 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
915 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700916 dev_dbg(&port->dev, "Reading Spreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500917 return -1;
918 }
919 Data |= 0x80;
920 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
921 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700922 dev_dbg(&port->dev, "writing Spreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500923 return -1;
924 }
925
926 Data &= ~0x80;
927 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
928 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700929 dev_dbg(&port->dev, "writing Spreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500930 return -1;
931 }
Alan Cox880af9d2008-07-22 11:16:12 +0100932 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500933
Paul B Schroeder3f542972006-08-31 19:41:47 -0500934 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100935 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
936 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500937 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700938 dev_dbg(&port->dev, "Reading Controlreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500939 return -1;
940 }
Alan Cox880af9d2008-07-22 11:16:12 +0100941 Data |= 0x08; /* Driver done bit */
942 Data |= 0x20; /* rx_disable */
943 status = mos7840_set_reg_sync(port,
944 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500945 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700946 dev_dbg(&port->dev, "writing Controlreg failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500947 return -1;
948 }
Alan Cox880af9d2008-07-22 11:16:12 +0100949 /* do register settings here */
950 /* Set all regs to the device default values. */
951 /***********************************
952 * First Disable all interrupts.
953 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500954 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500955 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
956 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700957 dev_dbg(&port->dev, "disabling interrupts failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500958 return -1;
959 }
Alan Cox880af9d2008-07-22 11:16:12 +0100960 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500961 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500962 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
963 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700964 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500965 return -1;
966 }
967
968 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500969 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
970 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -0700971 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500972 return -1;
973 }
974
975 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500976 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
977 mos7840_port->shadowLCR = Data;
978
979 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500980 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
981 mos7840_port->shadowMCR = Data;
982
983 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500984 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
985 mos7840_port->shadowLCR = Data;
986
Alan Cox880af9d2008-07-22 11:16:12 +0100987 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500988 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
989
990 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500991 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
992
993 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500994 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
995
996 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500997 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
998
999 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001000 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1001 mos7840_port->shadowLCR = Data;
1002
Alan Cox880af9d2008-07-22 11:16:12 +01001003 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001004 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001005 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1006
1007 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001008 status = mos7840_set_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);
Alan Cox880af9d2008-07-22 11:16:12 +01001012 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001013 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001014 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1015
Alan Cox880af9d2008-07-22 11:16:12 +01001016 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001017 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001018 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1019 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001020 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001021 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1022 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001023
Alan Cox880af9d2008-07-22 11:16:12 +01001024 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001025 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001026 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1027 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001028 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001029 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1030 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001031
Alan Cox880af9d2008-07-22 11:16:12 +01001032 /* Check to see if we've set up our endpoint info yet *
1033 * (can't set it up in mos7840_startup as the structures *
1034 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001035 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001036 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001037 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001038 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001039 serial->dev,
1040 usb_rcvintpipe(serial->dev,
1041 serial->port[0]->interrupt_in_endpointAddress),
1042 serial->port[0]->interrupt_in_buffer,
1043 serial->port[0]->interrupt_in_urb->
1044 transfer_buffer_length,
1045 mos7840_interrupt_callback,
1046 serial,
1047 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001048
1049 /* start interrupt read for mos7840 *
1050 * will continue as long as mos7840 is connected */
1051
1052 response =
1053 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1054 GFP_KERNEL);
1055 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001056 dev_err(&port->dev, "%s - Error %d submitting "
1057 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001058 }
1059
1060 }
1061
1062 }
1063
1064 /* see if we've set up our endpoint info yet *
1065 * (can't set it up in mos7840_startup as the *
1066 * structures were not set up at that time.) */
1067
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001068 dev_dbg(&port->dev, "port number is %d\n", port->number);
1069 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1070 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1071 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1072 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1073 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 -05001074 mos7840_port->read_urb = port->read_urb;
1075
1076 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001077 if ((serial->num_ports == 2)
1078 && ((((__u16)port->number -
1079 (__u16)(port->serial->minor)) % 2) != 0)) {
1080 usb_fill_bulk_urb(mos7840_port->read_urb,
1081 serial->dev,
1082 usb_rcvbulkpipe(serial->dev,
1083 (port->bulk_in_endpointAddress) + 2),
1084 port->bulk_in_buffer,
1085 mos7840_port->read_urb->transfer_buffer_length,
1086 mos7840_bulk_in_callback, mos7840_port);
1087 } else {
1088 usb_fill_bulk_urb(mos7840_port->read_urb,
1089 serial->dev,
1090 usb_rcvbulkpipe(serial->dev,
1091 port->bulk_in_endpointAddress),
1092 port->bulk_in_buffer,
1093 mos7840_port->read_urb->transfer_buffer_length,
1094 mos7840_bulk_in_callback, mos7840_port);
1095 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001096
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001097 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001098 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001099 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1100 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001101 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1102 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001103 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001104 }
1105
1106 /* initialize our wait queues */
1107 init_waitqueue_head(&mos7840_port->wait_chase);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001108
Paul B Schroeder3f542972006-08-31 19:41:47 -05001109 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001110 /* Must set to enable ints! */
1111 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001112 /* send a open port command */
1113 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001114 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001115
Paul B Schroeder3f542972006-08-31 19:41:47 -05001116 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001117}
1118
1119/*****************************************************************************
1120 * mos7840_chars_in_buffer
1121 * this function is called by the tty driver when it wants to know how many
1122 * bytes of data we currently have outstanding in the port (data that has
1123 * been written, but hasn't made it out the port yet)
1124 * If successful, we return the number of bytes left to be written in the
1125 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001126 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001127 *****************************************************************************/
1128
Alan Cox95da3102008-07-22 11:09:07 +01001129static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001130{
Alan Cox95da3102008-07-22 11:09:07 +01001131 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001132 int i;
1133 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001134 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001135 struct moschip_port *mos7840_port;
1136
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001137 if (mos7840_port_paranoia_check(port, __func__))
Alan Cox95da3102008-07-22 11:09:07 +01001138 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001139
1140 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001141 if (mos7840_port == NULL)
Alan Cox95da3102008-07-22 11:09:07 +01001142 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001143
Alan Cox880af9d2008-07-22 11:16:12 +01001144 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell5c263b92012-07-24 14:15:13 -05001145 for (i = 0; i < NUM_URBS; ++i) {
1146 if (mos7840_port->busy[i]) {
1147 struct urb *urb = mos7840_port->write_urb_pool[i];
1148 chars += urb->transfer_buffer_length;
1149 }
1150 }
Alan Cox880af9d2008-07-22 11:16:12 +01001151 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001152 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001153 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001154
1155}
1156
Paul B Schroeder3f542972006-08-31 19:41:47 -05001157/*****************************************************************************
1158 * mos7840_close
1159 * this function is called by the tty driver when a port is closed
1160 *****************************************************************************/
1161
Alan Cox335f8512009-06-11 12:26:29 +01001162static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001163{
1164 struct usb_serial *serial;
1165 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001166 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001167 int j;
1168 __u16 Data;
1169
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001170 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001171 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001172
Harvey Harrison441b62c2008-03-03 16:08:34 -08001173 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001174 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001175 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001176
1177 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001178 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001179
Oliver Neukum0de9a702007-03-16 20:28:28 +01001180 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001181 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001182
1183 for (j = 0; j < NUM_URBS; ++j)
1184 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1185
1186 /* Freeing Write URBs */
1187 for (j = 0; j < NUM_URBS; ++j) {
1188 if (mos7840_port->write_urb_pool[j]) {
1189 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1190 kfree(mos7840_port->write_urb_pool[j]->
1191 transfer_buffer);
1192
1193 usb_free_urb(mos7840_port->write_urb_pool[j]);
1194 }
1195 }
1196
Johan Hovoldbb3529c2013-03-21 12:36:35 +01001197 usb_kill_urb(mos7840_port->write_urb);
1198 usb_kill_urb(mos7840_port->read_urb);
1199 mos7840_port->read_urb_busy = false;
1200
Oliver Neukum0de9a702007-03-16 20:28:28 +01001201 port0->open_ports--;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001202 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 +01001203 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001204 if (serial->port[0]->interrupt_in_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001205 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001206 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001207 }
1208 }
1209
1210 if (mos7840_port->write_urb) {
1211 /* if this urb had a transfer buffer already (old tx) free it */
Syam Sidhardhanae8d4872013-03-07 01:51:12 +05301212 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001213 usb_free_urb(mos7840_port->write_urb);
1214 }
1215
1216 Data = 0x0;
1217 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1218
1219 Data = 0x00;
1220 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1221
1222 mos7840_port->open = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001223}
1224
1225/************************************************************************
1226 *
1227 * mos7840_block_until_chase_response
1228 *
1229 * This function will block the close until one of the following:
1230 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001231 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001232 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1233 *
1234 ************************************************************************/
1235
Alan Cox95da3102008-07-22 11:09:07 +01001236static void mos7840_block_until_chase_response(struct tty_struct *tty,
1237 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001238{
Mark Ferrell1e658482012-07-24 13:38:31 -05001239 int timeout = msecs_to_jiffies(1000);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001240 int wait = 10;
1241 int count;
1242
1243 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001244 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001245
1246 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001247 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001248 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001249
1250 /* Block the thread for a while */
1251 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1252 timeout);
1253 /* No activity.. count down section */
1254 wait--;
1255 if (wait == 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001256 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001257 return;
1258 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001259 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001260 wait = 10;
1261 }
1262 }
1263
1264}
1265
1266/*****************************************************************************
1267 * mos7840_break
1268 * this function sends a break to the port
1269 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001270static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001271{
Alan Cox95da3102008-07-22 11:09:07 +01001272 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001273 unsigned char data;
1274 struct usb_serial *serial;
1275 struct moschip_port *mos7840_port;
1276
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001277 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001278 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001279
Harvey Harrison441b62c2008-03-03 16:08:34 -08001280 serial = mos7840_get_usb_serial(port, __func__);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001281 if (!serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001282 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001283
1284 mos7840_port = mos7840_get_port_private(port);
1285
Alan Cox880af9d2008-07-22 11:16:12 +01001286 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001287 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001288
Johan Hovoldbb3529c2013-03-21 12:36:35 +01001289 /* flush and block until tx is empty */
1290 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001291
Alan Cox95da3102008-07-22 11:09:07 +01001292 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001293 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001294 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001295 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001296
Alan Cox6b447f042009-01-02 13:48:56 +00001297 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001298 mos7840_port->shadowLCR = data;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001299 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001300 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1301 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001302}
1303
1304/*****************************************************************************
1305 * mos7840_write_room
1306 * this function is called by the tty driver when it wants to know how many
1307 * bytes of data we can accept for a specific port.
1308 * If successful, we return the amount of room that we have for this port
1309 * Otherwise we return a negative error number.
1310 *****************************************************************************/
1311
Alan Cox95da3102008-07-22 11:09:07 +01001312static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001313{
Alan Cox95da3102008-07-22 11:09:07 +01001314 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001315 int i;
1316 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001317 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001318 struct moschip_port *mos7840_port;
1319
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001320 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001321 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001322
1323 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001324 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001325 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001326
Oliver Neukum0de9a702007-03-16 20:28:28 +01001327 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001328 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001329 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001330 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001331 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001332 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001333
Oliver Neukum0de9a702007-03-16 20:28:28 +01001334 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001335 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001336 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001337
1338}
1339
1340/*****************************************************************************
1341 * mos7840_write
1342 * this function is called by the tty driver when data should be written to
1343 * the port.
1344 * If successful, we return the number of bytes written, otherwise we
1345 * return a negative error number.
1346 *****************************************************************************/
1347
Alan Cox95da3102008-07-22 11:09:07 +01001348static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001349 const unsigned char *data, int count)
1350{
1351 int status;
1352 int i;
1353 int bytes_sent = 0;
1354 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001355 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001356
1357 struct moschip_port *mos7840_port;
1358 struct usb_serial *serial;
1359 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001360 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001361 const unsigned char *current_position = data;
1362 unsigned char *data1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001363
1364#ifdef NOTMOS7840
1365 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001366 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1367 mos7840_port->shadowLCR = Data;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001368 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1369 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001370
Alan Cox880af9d2008-07-22 11:16:12 +01001371 /* Data = 0x03; */
1372 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1373 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001374
Alan Cox880af9d2008-07-22 11:16:12 +01001375 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001376 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1377
Alan Cox880af9d2008-07-22 11:16:12 +01001378 /* Data = 0x0c; */
1379 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001380 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001381 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001382 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001383
1384 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001385 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001386 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001387
1388 Data = Data & ~SERIAL_LCR_DLAB;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001389 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001390 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1391#endif
1392
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001393 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001394 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001395
1396 serial = port->serial;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001397 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001398 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001399
1400 mos7840_port = mos7840_get_port_private(port);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001401 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001402 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001403
1404 /* try to find a free urb in the list */
1405 urb = NULL;
1406
Oliver Neukum0de9a702007-03-16 20:28:28 +01001407 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001408 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001409 if (!mos7840_port->busy[i]) {
1410 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001411 urb = mos7840_port->write_urb_pool[i];
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001412 dev_dbg(&port->dev, "URB:%d\n", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001413 break;
1414 }
1415 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001416 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001417
1418 if (urb == NULL) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001419 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001420 goto exit;
1421 }
1422
1423 if (urb->transfer_buffer == NULL) {
1424 urb->transfer_buffer =
1425 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1426
1427 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001428 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001429 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001430 goto exit;
1431 }
1432 }
1433 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1434
Al Viro97c49652006-10-09 20:29:03 +01001435 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001436
1437 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001438 if ((serial->num_ports == 2)
1439 && ((((__u16)port->number -
1440 (__u16)(port->serial->minor)) % 2) != 0)) {
1441 usb_fill_bulk_urb(urb,
1442 serial->dev,
1443 usb_sndbulkpipe(serial->dev,
1444 (port->bulk_out_endpointAddress) + 2),
1445 urb->transfer_buffer,
1446 transfer_size,
1447 mos7840_bulk_out_data_callback, mos7840_port);
1448 } else {
1449 usb_fill_bulk_urb(urb,
1450 serial->dev,
1451 usb_sndbulkpipe(serial->dev,
1452 port->bulk_out_endpointAddress),
1453 urb->transfer_buffer,
1454 transfer_size,
1455 mos7840_bulk_out_data_callback, mos7840_port);
1456 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001457
1458 data1 = urb->transfer_buffer;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001459 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001460
Donald0eafe4d2012-04-19 15:00:45 +08001461 /* Turn on LED */
1462 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1463 mos7840_port->led_flag = true;
1464 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1465 mod_timer(&mos7840_port->led_timer1,
1466 jiffies + msecs_to_jiffies(LED_ON_MS));
1467 }
1468
Paul B Schroeder3f542972006-08-31 19:41:47 -05001469 /* send it down the pipe */
1470 status = usb_submit_urb(urb, GFP_ATOMIC);
1471
1472 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001473 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001474 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001475 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001476 bytes_sent = status;
1477 goto exit;
1478 }
1479 bytes_sent = transfer_size;
Johan Hovold8c1a07f2013-03-21 12:37:16 +01001480 port->icount.tx += transfer_size;
1481 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001482exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001483 return bytes_sent;
1484
1485}
1486
1487/*****************************************************************************
1488 * mos7840_throttle
1489 * this function is called by the tty driver when it wants to stop the data
1490 * being read from the port.
1491 *****************************************************************************/
1492
Alan Cox95da3102008-07-22 11:09:07 +01001493static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001494{
Alan Cox95da3102008-07-22 11:09:07 +01001495 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001496 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001497 int status;
1498
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001499 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001500 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001501
1502 mos7840_port = mos7840_get_port_private(port);
1503
1504 if (mos7840_port == NULL)
1505 return;
1506
1507 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001508 dev_dbg(&port->dev, "%s", "port not opened\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001509 return;
1510 }
1511
Paul B Schroeder3f542972006-08-31 19:41:47 -05001512 /* if we are implementing XON/XOFF, send the stop character */
1513 if (I_IXOFF(tty)) {
1514 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001515 status = mos7840_write(tty, port, &stop_char, 1);
1516 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001517 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001518 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001519 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001520 if (tty->termios.c_cflag & CRTSCTS) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001521 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001522 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001523 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001524 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001525 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001526 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001527}
1528
1529/*****************************************************************************
1530 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001531 * this function is called by the tty driver when it wants to resume
1532 * the data being read from the port (called after mos7840_throttle is
1533 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001534 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001535static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001536{
Alan Cox95da3102008-07-22 11:09:07 +01001537 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001538 int status;
1539 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1540
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001541 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001542 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001543
1544 if (mos7840_port == NULL)
1545 return;
1546
1547 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001548 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001549 return;
1550 }
1551
Paul B Schroeder3f542972006-08-31 19:41:47 -05001552 /* if we are implementing XON/XOFF, send the start character */
1553 if (I_IXOFF(tty)) {
1554 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001555 status = mos7840_write(tty, port, &start_char, 1);
1556 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001557 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001558 }
1559
1560 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001561 if (tty->termios.c_cflag & CRTSCTS) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001562 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001563 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001564 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001565 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001566 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001567 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001568}
1569
Alan Cox60b33c12011-02-14 16:26:14 +00001570static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001571{
Alan Cox95da3102008-07-22 11:09:07 +01001572 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001573 struct moschip_port *mos7840_port;
1574 unsigned int result;
1575 __u16 msr;
1576 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001577 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001578 mos7840_port = mos7840_get_port_private(port);
1579
Paul B Schroeder3f542972006-08-31 19:41:47 -05001580 if (mos7840_port == NULL)
1581 return -ENODEV;
1582
1583 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1584 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1585 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1586 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1587 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1588 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1589 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1590 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1591 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1592
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001593 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001594
1595 return result;
1596}
1597
Alan Cox20b9d172011-02-14 16:26:50 +00001598static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001599 unsigned int set, unsigned int clear)
1600{
Alan Cox95da3102008-07-22 11:09:07 +01001601 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001602 struct moschip_port *mos7840_port;
1603 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001604 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001605
Paul B Schroeder3f542972006-08-31 19:41:47 -05001606 mos7840_port = mos7840_get_port_private(port);
1607
1608 if (mos7840_port == NULL)
1609 return -ENODEV;
1610
Alan Coxe2984492008-02-20 20:51:45 +00001611 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001612 mcr = mos7840_port->shadowMCR;
1613 if (clear & TIOCM_RTS)
1614 mcr &= ~MCR_RTS;
1615 if (clear & TIOCM_DTR)
1616 mcr &= ~MCR_DTR;
1617 if (clear & TIOCM_LOOP)
1618 mcr &= ~MCR_LOOPBACK;
1619
1620 if (set & TIOCM_RTS)
1621 mcr |= MCR_RTS;
1622 if (set & TIOCM_DTR)
1623 mcr |= MCR_DTR;
1624 if (set & TIOCM_LOOP)
1625 mcr |= MCR_LOOPBACK;
1626
1627 mos7840_port->shadowMCR = mcr;
1628
Paul B Schroeder3f542972006-08-31 19:41:47 -05001629 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1630 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001631 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
Roel Kluin87521c42008-04-17 06:16:24 +02001632 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001633 }
1634
1635 return 0;
1636}
1637
1638/*****************************************************************************
1639 * mos7840_calc_baud_rate_divisor
1640 * this function calculates the proper baud rate divisor for the specified
1641 * baud rate.
1642 *****************************************************************************/
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001643static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1644 int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001645 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001646{
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001647 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001648
1649 if (baudRate <= 115200) {
1650 *divisor = 115200 / baudRate;
1651 *clk_sel_val = 0x0;
1652 }
1653 if ((baudRate > 115200) && (baudRate <= 230400)) {
1654 *divisor = 230400 / baudRate;
1655 *clk_sel_val = 0x10;
1656 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1657 *divisor = 403200 / baudRate;
1658 *clk_sel_val = 0x20;
1659 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1660 *divisor = 460800 / baudRate;
1661 *clk_sel_val = 0x30;
1662 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1663 *divisor = 806400 / baudRate;
1664 *clk_sel_val = 0x40;
1665 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1666 *divisor = 921600 / baudRate;
1667 *clk_sel_val = 0x50;
1668 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1669 *divisor = 1572864 / baudRate;
1670 *clk_sel_val = 0x60;
1671 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1672 *divisor = 3145728 / baudRate;
1673 *clk_sel_val = 0x70;
1674 }
1675 return 0;
1676
1677#ifdef NOTMCS7840
1678
1679 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1680 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1681 *divisor = mos7840_divisor_table[i].Divisor;
1682 return 0;
1683 }
1684 }
1685
1686 /* After trying for all the standard baud rates *
1687 * Try calculating the divisor for this baud rate */
1688
1689 if (baudrate > 75 && baudrate < 230400) {
1690 /* get the divisor */
1691 custom = (__u16) (230400L / baudrate);
1692
1693 /* Check for round off */
1694 round1 = (__u16) (2304000L / baudrate);
1695 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001696 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001697 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001698 *divisor = custom;
1699
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001700 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001701 return 0;
1702 }
1703
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001704 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001705 return -1;
1706#endif
1707}
1708
1709/*****************************************************************************
1710 * mos7840_send_cmd_write_baud_rate
1711 * this function sends the proper command to change the baud rate of the
1712 * specified port.
1713 *****************************************************************************/
1714
1715static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1716 int baudRate)
1717{
1718 int divisor = 0;
1719 int status;
1720 __u16 Data;
1721 unsigned char number;
1722 __u16 clk_sel_val;
1723 struct usb_serial_port *port;
1724
1725 if (mos7840_port == NULL)
1726 return -1;
1727
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001728 port = mos7840_port->port;
1729 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001730 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001731
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001732 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001733 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001734
Paul B Schroeder3f542972006-08-31 19:41:47 -05001735 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1736
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001737 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1738 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001739 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001740 if (baudRate > 115200) {
1741#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001742 /* NOTE: need to see the pther register to modify */
1743 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001744 Data = 0x2b;
1745 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001746 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1747 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001748 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001749 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001750 return -1;
1751 }
1752#endif
1753
1754 } else {
1755#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001756 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001757 Data = 0xb;
1758 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001759 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1760 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001761 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001762 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001763 return -1;
1764 }
1765#endif
1766
1767 }
1768
Alan Cox880af9d2008-07-22 11:16:12 +01001769 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001770 clk_sel_val = 0x0;
1771 Data = 0x0;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001772 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001773 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001774 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1775 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001776 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001777 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001778 return -1;
1779 }
1780 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001781 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1782 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001783 if (status < 0) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001784 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001785 return -1;
1786 }
1787 /* Calculate the Divisor */
1788
1789 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001790 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001791 return status;
1792 }
1793 /* Enable access to divisor latch */
1794 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1795 mos7840_port->shadowLCR = Data;
1796 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1797
1798 /* Write the divisor */
1799 Data = (unsigned char)(divisor & 0xff);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001800 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001801 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1802
1803 Data = (unsigned char)((divisor & 0xff00) >> 8);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001804 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001805 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1806
1807 /* Disable access to divisor latch */
1808 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1809 mos7840_port->shadowLCR = Data;
1810 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1811
1812 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001813 return status;
1814}
1815
1816/*****************************************************************************
1817 * mos7840_change_port_settings
1818 * This routine is called to set the UART on the device to match
1819 * the specified new settings.
1820 *****************************************************************************/
1821
Alan Cox95da3102008-07-22 11:09:07 +01001822static void mos7840_change_port_settings(struct tty_struct *tty,
1823 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001824{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001825 int baud;
1826 unsigned cflag;
1827 unsigned iflag;
1828 __u8 lData;
1829 __u8 lParity;
1830 __u8 lStop;
1831 int status;
1832 __u16 Data;
1833 struct usb_serial_port *port;
1834 struct usb_serial *serial;
1835
1836 if (mos7840_port == NULL)
1837 return;
1838
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001839 port = mos7840_port->port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001840
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001841 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001842 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001843
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001844 if (mos7840_serial_paranoia_check(port->serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001845 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001846
1847 serial = port->serial;
1848
Paul B Schroeder3f542972006-08-31 19:41:47 -05001849 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001850 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001851 return;
1852 }
1853
Paul B Schroeder3f542972006-08-31 19:41:47 -05001854 lData = LCR_BITS_8;
1855 lStop = LCR_STOP_1;
1856 lParity = LCR_PAR_NONE;
1857
Alan Coxadc8d742012-07-14 15:31:47 +01001858 cflag = tty->termios.c_cflag;
1859 iflag = tty->termios.c_iflag;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001860
1861 /* Change the number of bits */
1862 if (cflag & CSIZE) {
1863 switch (cflag & CSIZE) {
1864 case CS5:
1865 lData = LCR_BITS_5;
1866 break;
1867
1868 case CS6:
1869 lData = LCR_BITS_6;
1870 break;
1871
1872 case CS7:
1873 lData = LCR_BITS_7;
1874 break;
1875 default:
1876 case CS8:
1877 lData = LCR_BITS_8;
1878 break;
1879 }
1880 }
1881 /* Change the Parity bit */
1882 if (cflag & PARENB) {
1883 if (cflag & PARODD) {
1884 lParity = LCR_PAR_ODD;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001885 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001886 } else {
1887 lParity = LCR_PAR_EVEN;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001888 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001889 }
1890
1891 } else {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001892 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001893 }
1894
Alan Cox880af9d2008-07-22 11:16:12 +01001895 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001896 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001897
1898 /* Change the Stop bit */
1899 if (cflag & CSTOPB) {
1900 lStop = LCR_STOP_2;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001901 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001902 } else {
1903 lStop = LCR_STOP_1;
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001904 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001905 }
1906
1907 /* Update the LCR with the correct value */
1908 mos7840_port->shadowLCR &=
1909 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1910 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1911
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001912 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1913 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001914 /* Disable Interrupts */
1915 Data = 0x00;
1916 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1917
1918 Data = 0x00;
1919 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1920
1921 Data = 0xcf;
1922 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1923
1924 /* Send the updated LCR value to the mos7840 */
1925 Data = mos7840_port->shadowLCR;
1926
1927 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1928
1929 Data = 0x00b;
1930 mos7840_port->shadowMCR = Data;
1931 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1932 Data = 0x00b;
1933 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1934
1935 /* set up the MCR register and send it to the mos7840 */
1936
1937 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01001938 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001939 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001940
Alan Cox880af9d2008-07-22 11:16:12 +01001941 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001942 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01001943 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001944 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001945
1946 Data = mos7840_port->shadowMCR;
1947 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1948
1949 /* Determine divisor based on baud rate */
1950 baud = tty_get_baud_rate(tty);
1951
1952 if (!baud) {
1953 /* pick a default, any default... */
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001954 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001955 baud = 9600;
1956 }
1957
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001958 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001959 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1960
1961 /* Enable Interrupts */
1962 Data = 0x0c;
1963 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1964
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001965 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001966 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001967 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001968 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001969 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001970 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001971 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001972 }
1973 }
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001974 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1975 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001976}
1977
1978/*****************************************************************************
1979 * mos7840_set_termios
1980 * this function is called by the tty driver when it wants to change
1981 * the termios structure
1982 *****************************************************************************/
1983
Alan Cox95da3102008-07-22 11:09:07 +01001984static void mos7840_set_termios(struct tty_struct *tty,
1985 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08001986 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001987{
1988 int status;
1989 unsigned int cflag;
1990 struct usb_serial *serial;
1991 struct moschip_port *mos7840_port;
Greg Kroah-Hartman33631552012-05-03 16:44:33 -07001992
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001993 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001994 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001995
1996 serial = port->serial;
1997
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07001998 if (mos7840_serial_paranoia_check(serial, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05001999 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002000
2001 mos7840_port = mos7840_get_port_private(port);
2002
2003 if (mos7840_port == NULL)
2004 return;
2005
Paul B Schroeder3f542972006-08-31 19:41:47 -05002006 if (!mos7840_port->open) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002007 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002008 return;
2009 }
2010
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002011 dev_dbg(&port->dev, "%s", "setting termios - \n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002012
Alan Coxadc8d742012-07-14 15:31:47 +01002013 cflag = tty->termios.c_cflag;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002014
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002015 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07002016 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002017 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2018 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2019 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002020
2021 /* change the port settings to the new ones specified */
2022
Alan Cox95da3102008-07-22 11:09:07 +01002023 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002024
2025 if (!mos7840_port->read_urb) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002026 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002027 return;
2028 }
2029
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002030 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002031 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002032 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2033 if (status) {
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002034 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002035 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002036 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002037 }
2038 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002039}
2040
2041/*****************************************************************************
2042 * mos7840_get_lsr_info - get line status register info
2043 *
2044 * Purpose: Let user call ioctl() to get info when the UART physically
2045 * is emptied. On bus types like RS485, the transmitter must
2046 * release the bus after transmitting. This must be done when
2047 * the transmit shift register is empty, not be done when the
2048 * transmit holding register is empty. This functionality
2049 * allows an RS485 driver to be written in user space.
2050 *****************************************************************************/
2051
Alan Cox95da3102008-07-22 11:09:07 +01002052static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002053 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002054{
2055 int count;
2056 unsigned int result = 0;
2057
Alan Cox95da3102008-07-22 11:09:07 +01002058 count = mos7840_chars_in_buffer(tty);
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002059 if (count == 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002060 result = TIOCSER_TEMT;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002061
2062 if (copy_to_user(value, &result, sizeof(int)))
2063 return -EFAULT;
2064 return 0;
2065}
2066
2067/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002068 * mos7840_get_serial_info
2069 * function to get information about serial port
2070 *****************************************************************************/
2071
2072static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002073 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002074{
2075 struct serial_struct tmp;
2076
2077 if (mos7840_port == NULL)
2078 return -1;
2079
2080 if (!retinfo)
2081 return -EFAULT;
2082
2083 memset(&tmp, 0, sizeof(tmp));
2084
2085 tmp.type = PORT_16550A;
2086 tmp.line = mos7840_port->port->serial->minor;
2087 tmp.port = mos7840_port->port->number;
2088 tmp.irq = 0;
2089 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2090 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2091 tmp.baud_base = 9600;
2092 tmp.close_delay = 5 * HZ;
2093 tmp.closing_wait = 30 * HZ;
2094
2095 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2096 return -EFAULT;
2097 return 0;
2098}
2099
2100/*****************************************************************************
2101 * SerialIoctl
2102 * this function handles any ioctl calls to the driver
2103 *****************************************************************************/
2104
Alan Cox00a0d0d2011-02-14 16:27:06 +00002105static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002106 unsigned int cmd, unsigned long arg)
2107{
Alan Cox95da3102008-07-22 11:09:07 +01002108 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002109 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002110 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002111
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002112 if (mos7840_port_paranoia_check(port, __func__))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002113 return -1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002114
2115 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002116
2117 if (mos7840_port == NULL)
2118 return -1;
2119
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002120 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002121
2122 switch (cmd) {
2123 /* return number of bytes available */
2124
Paul B Schroeder3f542972006-08-31 19:41:47 -05002125 case TIOCSERGETLSR:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002126 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01002127 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002128
Paul B Schroeder3f542972006-08-31 19:41:47 -05002129 case TIOCGSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002130 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Al Viro97c49652006-10-09 20:29:03 +01002131 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132
2133 case TIOCSSERIAL:
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002134 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002135 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002136 default:
2137 break;
2138 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002139 return -ENOIOCTLCMD;
2140}
2141
Donald0eafe4d2012-04-19 15:00:45 +08002142static int mos7810_check(struct usb_serial *serial)
2143{
2144 int i, pass_count = 0;
Johan Hovold15ee89c332013-05-27 14:44:40 +02002145 u8 *buf;
Donald0eafe4d2012-04-19 15:00:45 +08002146 __u16 data = 0, mcr_data = 0;
2147 __u16 test_pattern = 0x55AA;
Johan Hovold15ee89c332013-05-27 14:44:40 +02002148 int res;
2149
2150 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2151 if (!buf)
2152 return 0; /* failed to identify 7810 */
Donald0eafe4d2012-04-19 15:00:45 +08002153
2154 /* Store MCR setting */
Johan Hovold15ee89c332013-05-27 14:44:40 +02002155 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
Donald0eafe4d2012-04-19 15:00:45 +08002156 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
Johan Hovold15ee89c332013-05-27 14:44:40 +02002157 buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2158 if (res == VENDOR_READ_LENGTH)
2159 mcr_data = *buf;
Donald0eafe4d2012-04-19 15:00:45 +08002160
2161 for (i = 0; i < 16; i++) {
2162 /* Send the 1-bit test pattern out to MCS7810 test pin */
2163 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2164 MCS_WRREQ, MCS_WR_RTYPE,
2165 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2166 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2167
2168 /* Read the test pattern back */
Johan Hovold15ee89c332013-05-27 14:44:40 +02002169 res = usb_control_msg(serial->dev,
2170 usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
2171 MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2172 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2173 if (res == VENDOR_READ_LENGTH)
2174 data = *buf;
Donald0eafe4d2012-04-19 15:00:45 +08002175
2176 /* If this is a MCS7810 device, both test patterns must match */
2177 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2178 break;
2179
2180 pass_count++;
2181 }
2182
2183 /* Restore MCR setting */
2184 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2185 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2186 0, MOS_WDR_TIMEOUT);
2187
Johan Hovold15ee89c332013-05-27 14:44:40 +02002188 kfree(buf);
2189
Donald0eafe4d2012-04-19 15:00:45 +08002190 if (pass_count == 16)
2191 return 1;
2192
2193 return 0;
2194}
2195
Paul B Schroeder3f542972006-08-31 19:41:47 -05002196static int mos7840_calc_num_ports(struct usb_serial *serial)
2197{
Donald0eafe4d2012-04-19 15:00:45 +08002198 __u16 data = 0x00;
Johan Hovold15ee89c332013-05-27 14:44:40 +02002199 u8 *buf;
Donald Lee093ea2d2012-03-14 15:26:33 +08002200 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002201
Johan Hovold15ee89c332013-05-27 14:44:40 +02002202 buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2203 if (buf) {
2204 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2205 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2206 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2207 data = *buf;
2208 kfree(buf);
2209 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002210
Donald0eafe4d2012-04-19 15:00:45 +08002211 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2212 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2213 device_type = serial->dev->descriptor.idProduct;
Donald Lee093ea2d2012-03-14 15:26:33 +08002214 } else {
Donald0eafe4d2012-04-19 15:00:45 +08002215 /* For a MCS7840 device GPIO0 must be set to 1 */
2216 if ((data & 0x01) == 1)
2217 device_type = MOSCHIP_DEVICE_ID_7840;
2218 else if (mos7810_check(serial))
2219 device_type = MOSCHIP_DEVICE_ID_7810;
2220 else
2221 device_type = MOSCHIP_DEVICE_ID_7820;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002222 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002223
Donald0eafe4d2012-04-19 15:00:45 +08002224 mos7840_num_ports = (device_type >> 4) & 0x000F;
2225 serial->num_bulk_in = mos7840_num_ports;
2226 serial->num_bulk_out = mos7840_num_ports;
2227 serial->num_ports = mos7840_num_ports;
2228
Paul B Schroeder3f542972006-08-31 19:41:47 -05002229 return mos7840_num_ports;
2230}
2231
Johan Hovold80c00752012-10-25 18:56:34 +02002232static int mos7840_port_probe(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002233{
Johan Hovold80c00752012-10-25 18:56:34 +02002234 struct usb_serial *serial = port->serial;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002235 struct moschip_port *mos7840_port;
Johan Hovold80c00752012-10-25 18:56:34 +02002236 int status;
2237 int pnum;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002238 __u16 Data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002239
Paul B Schroeder3f542972006-08-31 19:41:47 -05002240 /* we set up the pointers to the endpoints in the mos7840_open *
2241 * function, as the structures aren't created yet. */
2242
Johan Hovold80c00752012-10-25 18:56:34 +02002243 pnum = port->number - serial->minor;
2244
Johan Hovoldae685ef2012-10-25 18:56:35 +02002245 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2246 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2247 if (mos7840_port == NULL) {
2248 dev_err(&port->dev, "%s - Out of memory\n", __func__);
2249 return -ENOMEM;
2250 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002251
Johan Hovoldae685ef2012-10-25 18:56:35 +02002252 /* Initialize all port interrupt end point to port 0 int
2253 * endpoint. Our device has only one interrupt end point
2254 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002255
Johan Hovoldae685ef2012-10-25 18:56:35 +02002256 mos7840_port->port = port;
2257 mos7840_set_port_private(port, mos7840_port);
2258 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002259
Johan Hovoldae685ef2012-10-25 18:56:35 +02002260 /* minor is not initialised until later by
2261 * usb-serial.c:get_free_serial() and cannot therefore be used
2262 * to index device instances */
2263 mos7840_port->port_num = pnum + 1;
2264 dev_dbg(&port->dev, "port->number = %d\n", port->number);
2265 dev_dbg(&port->dev, "port->serial->minor = %d\n", port->serial->minor);
2266 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2267 dev_dbg(&port->dev, "serial->minor = %d\n", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002268
Johan Hovoldae685ef2012-10-25 18:56:35 +02002269 if (mos7840_port->port_num == 1) {
2270 mos7840_port->SpRegOffset = 0x0;
2271 mos7840_port->ControlRegOffset = 0x1;
2272 mos7840_port->DcrRegOffset = 0x4;
2273 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2274 mos7840_port->SpRegOffset = 0x8;
2275 mos7840_port->ControlRegOffset = 0x9;
2276 mos7840_port->DcrRegOffset = 0x16;
2277 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2278 mos7840_port->SpRegOffset = 0xa;
2279 mos7840_port->ControlRegOffset = 0xb;
2280 mos7840_port->DcrRegOffset = 0x19;
2281 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2282 mos7840_port->SpRegOffset = 0xa;
2283 mos7840_port->ControlRegOffset = 0xb;
2284 mos7840_port->DcrRegOffset = 0x19;
2285 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2286 mos7840_port->SpRegOffset = 0xc;
2287 mos7840_port->ControlRegOffset = 0xd;
2288 mos7840_port->DcrRegOffset = 0x1c;
2289 }
2290 mos7840_dump_serial_port(port, mos7840_port);
2291 mos7840_set_port_private(port, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002292
Johan Hovoldae685ef2012-10-25 18:56:35 +02002293 /* enable rx_disable bit in control register */
2294 status = mos7840_get_reg_sync(port,
2295 mos7840_port->ControlRegOffset, &Data);
2296 if (status < 0) {
2297 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2298 goto out;
2299 } else
2300 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2301 Data |= 0x08; /* setting driver done bit */
2302 Data |= 0x04; /* sp1_bit to have cts change reflect in
2303 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002304
Johan Hovoldae685ef2012-10-25 18:56:35 +02002305 /* Data |= 0x20; //rx_disable bit */
2306 status = mos7840_set_reg_sync(port,
2307 mos7840_port->ControlRegOffset, Data);
2308 if (status < 0) {
2309 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2310 goto out;
2311 } else
2312 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2313
2314 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2315 and 0x24 in DCR3 */
2316 Data = 0x01;
2317 status = mos7840_set_reg_sync(port,
2318 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2319 if (status < 0) {
2320 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2321 goto out;
2322 } else
2323 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2324
2325 Data = 0x05;
2326 status = mos7840_set_reg_sync(port,
2327 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2328 if (status < 0) {
2329 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2330 goto out;
2331 } else
2332 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2333
2334 Data = 0x24;
2335 status = mos7840_set_reg_sync(port,
2336 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2337 if (status < 0) {
2338 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2339 goto out;
2340 } else
2341 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2342
2343 /* write values in clkstart0x0 and clkmulti 0x20 */
2344 Data = 0x0;
2345 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2346 if (status < 0) {
2347 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2348 goto out;
2349 } else
2350 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2351
2352 Data = 0x20;
2353 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2354 if (status < 0) {
2355 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2356 goto error;
2357 } else
2358 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2359
2360 /* write value 0x0 to scratchpad register */
2361 Data = 0x00;
2362 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2363 if (status < 0) {
2364 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2365 goto out;
2366 } else
2367 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2368
2369 /* Zero Length flag register */
2370 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2371 Data = 0xff;
Johan Hovold80c00752012-10-25 18:56:34 +02002372 status = mos7840_set_reg_sync(port,
Johan Hovoldae685ef2012-10-25 18:56:35 +02002373 (__u16) (ZLP_REG1 +
2374 ((__u16)mos7840_port->port_num)), Data);
2375 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002376 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002377 if (status < 0) {
2378 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2379 goto out;
2380 } else
2381 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2382 } else {
2383 Data = 0xff;
2384 status = mos7840_set_reg_sync(port,
2385 (__u16) (ZLP_REG1 +
2386 ((__u16)mos7840_port->port_num) - 0x1), Data);
2387 dev_dbg(&port->dev, "ZLIP offset %x\n",
Greg Kroah-Hartman9c134a12012-09-14 16:06:04 -07002388 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
Johan Hovoldae685ef2012-10-25 18:56:35 +02002389 if (status < 0) {
2390 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2391 goto out;
2392 } else
2393 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002394
Johan Hovoldae685ef2012-10-25 18:56:35 +02002395 }
2396 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2397 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2398 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2399 GFP_KERNEL);
2400 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2401 !mos7840_port->dr) {
2402 status = -ENOMEM;
2403 goto error;
2404 }
Donald0eafe4d2012-04-19 15:00:45 +08002405
Johan Hovoldae685ef2012-10-25 18:56:35 +02002406 mos7840_port->has_led = false;
Donald0eafe4d2012-04-19 15:00:45 +08002407
Johan Hovoldae685ef2012-10-25 18:56:35 +02002408 /* Initialize LED timers */
2409 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2410 mos7840_port->has_led = true;
Donald0eafe4d2012-04-19 15:00:45 +08002411
Johan Hovoldae685ef2012-10-25 18:56:35 +02002412 init_timer(&mos7840_port->led_timer1);
2413 mos7840_port->led_timer1.function = mos7840_led_off;
2414 mos7840_port->led_timer1.expires =
2415 jiffies + msecs_to_jiffies(LED_ON_MS);
2416 mos7840_port->led_timer1.data = (unsigned long)mos7840_port;
Donald0eafe4d2012-04-19 15:00:45 +08002417
Johan Hovoldae685ef2012-10-25 18:56:35 +02002418 init_timer(&mos7840_port->led_timer2);
2419 mos7840_port->led_timer2.function = mos7840_led_flag_off;
2420 mos7840_port->led_timer2.expires =
2421 jiffies + msecs_to_jiffies(LED_OFF_MS);
2422 mos7840_port->led_timer2.data = (unsigned long)mos7840_port;
Donald0eafe4d2012-04-19 15:00:45 +08002423
Johan Hovoldae685ef2012-10-25 18:56:35 +02002424 mos7840_port->led_flag = false;
Donald0eafe4d2012-04-19 15:00:45 +08002425
Johan Hovoldae685ef2012-10-25 18:56:35 +02002426 /* Turn off LED */
2427 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2428 }
2429out:
Johan Hovold80c00752012-10-25 18:56:34 +02002430 if (pnum == serial->num_ports - 1) {
2431 /* Zero Length flag enable */
2432 Data = 0x0f;
2433 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2434 if (status < 0) {
2435 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2436 goto error;
2437 } else
2438 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2439
2440 /* setting configuration feature to one */
2441 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2442 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2443 MOS_WDR_TIMEOUT);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002444 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002445 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002446error:
Johan Hovold80c00752012-10-25 18:56:34 +02002447 kfree(mos7840_port->dr);
2448 kfree(mos7840_port->ctrl_buf);
2449 usb_free_urb(mos7840_port->control_urb);
2450 kfree(mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002451
Oliver Neukum0de9a702007-03-16 20:28:28 +01002452 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002453}
2454
Johan Hovold80c00752012-10-25 18:56:34 +02002455static int mos7840_port_remove(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002456{
Paul B Schroeder3f542972006-08-31 19:41:47 -05002457 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002458
Johan Hovold80c00752012-10-25 18:56:34 +02002459 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002460
Johan Hovold80c00752012-10-25 18:56:34 +02002461 if (mos7840_port->has_led) {
2462 /* Turn off LED */
2463 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002464
Johan Hovold80c00752012-10-25 18:56:34 +02002465 del_timer_sync(&mos7840_port->led_timer1);
2466 del_timer_sync(&mos7840_port->led_timer2);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002467 }
Johan Hovold80c00752012-10-25 18:56:34 +02002468 usb_kill_urb(mos7840_port->control_urb);
2469 usb_free_urb(mos7840_port->control_urb);
2470 kfree(mos7840_port->ctrl_buf);
2471 kfree(mos7840_port->dr);
2472 kfree(mos7840_port);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002473
Johan Hovold80c00752012-10-25 18:56:34 +02002474 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002475}
2476
2477static struct usb_serial_driver moschip7840_4port_device = {
2478 .driver = {
2479 .owner = THIS_MODULE,
2480 .name = "mos7840",
2481 },
2482 .description = DRIVER_DESC,
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002483 .id_table = id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002484 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002485 .open = mos7840_open,
2486 .close = mos7840_close,
2487 .write = mos7840_write,
2488 .write_room = mos7840_write_room,
2489 .chars_in_buffer = mos7840_chars_in_buffer,
2490 .throttle = mos7840_throttle,
2491 .unthrottle = mos7840_unthrottle,
2492 .calc_num_ports = mos7840_calc_num_ports,
2493#ifdef MCSSerialProbe
2494 .probe = mos7840_serial_probe,
2495#endif
2496 .ioctl = mos7840_ioctl,
2497 .set_termios = mos7840_set_termios,
2498 .break_ctl = mos7840_break,
2499 .tiocmget = mos7840_tiocmget,
2500 .tiocmset = mos7840_tiocmset,
Johan Hovold0c613372013-03-21 12:37:17 +01002501 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovold8c1a07f2013-03-21 12:37:16 +01002502 .get_icount = usb_serial_generic_get_icount,
Johan Hovold80c00752012-10-25 18:56:34 +02002503 .port_probe = mos7840_port_probe,
2504 .port_remove = mos7840_port_remove,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002505 .read_bulk_callback = mos7840_bulk_in_callback,
2506 .read_int_callback = mos7840_interrupt_callback,
2507};
2508
Alan Stern4d2a7af2012-02-23 14:57:09 -05002509static struct usb_serial_driver * const serial_drivers[] = {
2510 &moschip7840_4port_device, NULL
2511};
2512
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002513module_usb_serial_driver(serial_drivers, id_table);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002514
Paul B Schroeder3f542972006-08-31 19:41:47 -05002515MODULE_DESCRIPTION(DRIVER_DESC);
2516MODULE_LICENSE("GPL");