blob: aaef523955e0294afc126b175ee50b43a0ed8f55 [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
38/*
39 * Version Information
40 */
Tony Cook37768ad2009-04-18 22:42:18 +093041#define DRIVER_VERSION "1.3.2"
Paul B Schroeder3f542972006-08-31 19:41:47 -050042#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
43
44/*
45 * 16C50 UART register defines
46 */
47
48#define LCR_BITS_5 0x00 /* 5 bits/char */
49#define LCR_BITS_6 0x01 /* 6 bits/char */
50#define LCR_BITS_7 0x02 /* 7 bits/char */
51#define LCR_BITS_8 0x03 /* 8 bits/char */
52#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
53
54#define LCR_STOP_1 0x00 /* 1 stop bit */
55#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
56#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
57#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
58
59#define LCR_PAR_NONE 0x00 /* No parity */
60#define LCR_PAR_ODD 0x08 /* Odd parity */
61#define LCR_PAR_EVEN 0x18 /* Even parity */
62#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
63#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
64#define LCR_PAR_MASK 0x38 /* Mask for parity field */
65
66#define LCR_SET_BREAK 0x40 /* Set Break condition */
67#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
68
69#define MCR_DTR 0x01 /* Assert DTR */
70#define MCR_RTS 0x02 /* Assert RTS */
71#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
72#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
73#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
74#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
75
76#define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
77#define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
78#define MOS7840_MSR_RI 0x40 /* Current state of RI */
79#define MOS7840_MSR_CD 0x80 /* Current state of CD */
80
81/*
82 * Defines used for sending commands to port
83 */
84
Alan Cox880af9d2008-07-22 11:16:12 +010085#define WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
86#define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
Paul B Schroeder3f542972006-08-31 19:41:47 -050087
88#define MOS_PORT1 0x0200
89#define MOS_PORT2 0x0300
90#define MOS_VENREG 0x0000
91#define MOS_MAX_PORT 0x02
92#define MOS_WRITE 0x0E
93#define MOS_READ 0x0D
94
95/* Requests */
96#define MCS_RD_RTYPE 0xC0
97#define MCS_WR_RTYPE 0x40
98#define MCS_RDREQ 0x0D
99#define MCS_WRREQ 0x0E
100#define MCS_CTRL_TIMEOUT 500
101#define VENDOR_READ_LENGTH (0x01)
102
103#define MAX_NAME_LEN 64
104
Alan Cox880af9d2008-07-22 11:16:12 +0100105#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
106#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500107
108/* For higher baud Rates use TIOCEXBAUD */
109#define TIOCEXBAUD 0x5462
110
111/* vendor id and device id defines */
112
David Ludlow11e1abb2008-02-25 17:30:52 -0500113/* The native mos7840/7820 component */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500114#define USB_VENDOR_ID_MOSCHIP 0x9710
115#define MOSCHIP_DEVICE_ID_7840 0x7840
116#define MOSCHIP_DEVICE_ID_7820 0x7820
Donald0eafe4d2012-04-19 15:00:45 +0800117#define MOSCHIP_DEVICE_ID_7810 0x7810
David Ludlow11e1abb2008-02-25 17:30:52 -0500118/* The native component can have its vendor/device id's overridden
119 * in vendor-specific implementations. Such devices can be handled
120 * by making a change here, in moschip_port_id_table, and in
121 * moschip_id_table_combined
122 */
Dave Ludlow870408c2010-09-01 12:33:30 -0400123#define USB_VENDOR_ID_BANDB 0x0856
124#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
125#define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
126#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
127#define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
128#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
129#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
130#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
131#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
132#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
133#define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
134#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
135#define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
136#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
Paul B Schroeder3f542972006-08-31 19:41:47 -0500137
Russell Lang9d498be2009-07-17 19:29:20 +1000138/* This driver also supports
139 * ATEN UC2324 device using Moschip MCS7840
140 * ATEN UC2322 device using Moschip MCS7820
141 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930142#define USB_VENDOR_ID_ATENINTL 0x0557
143#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000144#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930145
David Ludlow11e1abb2008-02-25 17:30:52 -0500146/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500147
148#define SERIAL_IIR_RLS 0x06
149#define SERIAL_IIR_MS 0x00
150
151/*
152 * Emulation of the bit mask on the LINE STATUS REGISTER.
153 */
154#define SERIAL_LSR_DR 0x0001
155#define SERIAL_LSR_OE 0x0002
156#define SERIAL_LSR_PE 0x0004
157#define SERIAL_LSR_FE 0x0008
158#define SERIAL_LSR_BI 0x0010
159
160#define MOS_MSR_DELTA_CTS 0x10
161#define MOS_MSR_DELTA_DSR 0x20
162#define MOS_MSR_DELTA_RI 0x40
163#define MOS_MSR_DELTA_CD 0x80
164
Alan Cox880af9d2008-07-22 11:16:12 +0100165/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500166#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
167#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
168#define LINE_CONTROL_REGISTER ((__u16)(0x03))
169#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
170#define LINE_STATUS_REGISTER ((__u16)(0x05))
171#define MODEM_STATUS_REGISTER ((__u16)(0x06))
172#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
173#define DIVISOR_LATCH_LSB ((__u16)(0x00))
174#define DIVISOR_LATCH_MSB ((__u16)(0x01))
175
176#define CLK_MULTI_REGISTER ((__u16)(0x02))
177#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
Donald Lee093ea2d2012-03-14 15:26:33 +0800178#define GPIO_REGISTER ((__u16)(0x07))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500179
180#define SERIAL_LCR_DLAB ((__u16)(0x0080))
181
182/*
183 * URB POOL related defines
184 */
185#define NUM_URBS 16 /* URB Count */
186#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
187
Donald0eafe4d2012-04-19 15:00:45 +0800188/* LED on/off milliseconds*/
189#define LED_ON_MS 500
190#define LED_OFF_MS 500
191
192static int device_type;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500193
Németh Márton7d40d7e2010-01-10 15:34:24 +0100194static const struct usb_device_id moschip_port_id_table[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500195 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
196 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Donald0eafe4d2012-04-19 15:00:45 +0800197 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400209 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800210 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930211 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000212 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500213 {} /* terminating entry */
214};
215
Németh Márton7d40d7e2010-01-10 15:34:24 +0100216static const struct usb_device_id moschip_id_table_combined[] __devinitconst = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500217 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
218 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Donald0eafe4d2012-04-19 15:00:45 +0800219 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500220 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400221 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500222 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400223 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500224 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
225 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
226 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
227 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500228 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400229 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500230 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400231 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800232 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930233 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000234 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500235 {} /* terminating entry */
236};
237
238MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
239
240/* This structure holds all of the local port information */
241
242struct moschip_port {
243 int port_num; /*Actual port number in the device(1,2,etc) */
244 struct urb *write_urb; /* write URB for this port */
245 struct urb *read_urb; /* read URB for this port */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100246 struct urb *int_urb;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500247 __u8 shadowLCR; /* last LCR value received */
248 __u8 shadowMCR; /* last MCR value received */
249 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100250 char open_ports;
251 char zombie;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500252 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
253 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
254 int delta_msr_cond;
255 struct async_icount icount;
256 struct usb_serial_port *port; /* loop back to the owner of this object */
257
Alan Cox880af9d2008-07-22 11:16:12 +0100258 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500259 __u8 SpRegOffset;
260 __u8 ControlRegOffset;
261 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100262 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500263 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100264 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500265 char *ctrl_buf;
266 int MsrLsr;
267
Oliver Neukum0de9a702007-03-16 20:28:28 +0100268 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500269 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100270 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800271 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500272
Donald0eafe4d2012-04-19 15:00:45 +0800273 /* For device(s) with LED indicator */
274 bool has_led;
275 bool led_flag;
276 struct timer_list led_timer1; /* Timer for LED on */
277 struct timer_list led_timer2; /* Timer for LED off */
278};
Paul B Schroeder3f542972006-08-31 19:41:47 -0500279
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030280static bool debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500281
282/*
283 * mos7840_set_reg_sync
284 * To set the Control register by calling usb_fill_control_urb function
285 * by passing usb_sndctrlpipe function as parameter.
286 */
287
288static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
289 __u16 val)
290{
291 struct usb_device *dev = port->serial->dev;
292 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930293 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500294
295 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
296 MCS_WR_RTYPE, val, reg, NULL, 0,
297 MOS_WDR_TIMEOUT);
298}
299
300/*
301 * mos7840_get_reg_sync
302 * To set the Uart register by calling usb_fill_control_urb function by
303 * passing usb_rcvctrlpipe function as parameter.
304 */
305
306static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100307 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500308{
309 struct usb_device *dev = port->serial->dev;
310 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100311 u8 *buf;
312
313 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
314 if (!buf)
315 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500316
317 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100318 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500319 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100320 *val = buf[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930321 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100322
323 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500324 return ret;
325}
326
327/*
328 * mos7840_set_uart_reg
329 * To set the Uart register by calling usb_fill_control_urb function by
330 * passing usb_sndctrlpipe function as parameter.
331 */
332
333static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
334 __u16 val)
335{
336
337 struct usb_device *dev = port->serial->dev;
338 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100339 /* For the UART control registers, the application number need
340 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100341 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100342 val |= (((__u16) port->number -
343 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930344 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500345 } else {
346 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100347 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500348 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930349 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500350 val);
351 } else {
352 val |=
353 (((__u16) port->number -
354 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930355 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500356 val);
357 }
358 }
359 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
360 MCS_WR_RTYPE, val, reg, NULL, 0,
361 MOS_WDR_TIMEOUT);
362
363}
364
365/*
366 * mos7840_get_uart_reg
367 * To set the Control register by calling usb_fill_control_urb function
368 * by passing usb_rcvctrlpipe function as parameter.
369 */
370static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100371 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500372{
373 struct usb_device *dev = port->serial->dev;
374 int ret = 0;
375 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100376 u8 *buf;
377
378 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
379 if (!buf)
380 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500381
Tony Cook84fe6e72009-04-18 22:55:06 +0930382 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100383 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
384 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100385 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500386 Wval =
387 (((__u16) port->number - (__u16) (port->serial->minor)) +
388 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930389 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500390 } else {
391 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100392 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500393 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930394 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500395 Wval);
396 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100397 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500398 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930399 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500400 Wval);
401 }
402 }
403 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100404 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500405 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100406 *val = buf[0];
407
408 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500409 return ret;
410}
411
412static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
413{
414
Tony Cook84fe6e72009-04-18 22:55:06 +0930415 dbg("***************************************");
416 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
417 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
418 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
419 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500420
421}
422
423/************************************************************************/
424/************************************************************************/
425/* I N T E R F A C E F U N C T I O N S */
426/* I N T E R F A C E F U N C T I O N S */
427/************************************************************************/
428/************************************************************************/
429
430static inline void mos7840_set_port_private(struct usb_serial_port *port,
431 struct moschip_port *data)
432{
433 usb_set_serial_port_data(port, (void *)data);
434}
435
436static inline struct moschip_port *mos7840_get_port_private(struct
437 usb_serial_port
438 *port)
439{
440 return (struct moschip_port *)usb_get_serial_port_data(port);
441}
442
Oliver Neukum0de9a702007-03-16 20:28:28 +0100443static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500444{
445 struct moschip_port *mos7840_port;
446 struct async_icount *icount;
447 mos7840_port = port;
448 icount = &mos7840_port->icount;
449 if (new_msr &
450 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
451 MOS_MSR_DELTA_CD)) {
452 icount = &mos7840_port->icount;
453
454 /* update input line counters */
455 if (new_msr & MOS_MSR_DELTA_CTS) {
456 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100457 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500458 }
459 if (new_msr & MOS_MSR_DELTA_DSR) {
460 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100461 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500462 }
463 if (new_msr & MOS_MSR_DELTA_CD) {
464 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100465 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500466 }
467 if (new_msr & MOS_MSR_DELTA_RI) {
468 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100469 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500470 }
471 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500472}
473
Oliver Neukum0de9a702007-03-16 20:28:28 +0100474static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500475{
476 struct async_icount *icount;
477
Harvey Harrison441b62c2008-03-03 16:08:34 -0800478 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500479
480 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100481 /*
482 * Parity and Framing errors only count if they
483 * occur exclusive of a break being
484 * received.
485 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500486 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
487 }
488
489 /* update input line counters */
490 icount = &port->icount;
491 if (new_lsr & SERIAL_LSR_BI) {
492 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100493 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500494 }
495 if (new_lsr & SERIAL_LSR_OE) {
496 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100497 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500498 }
499 if (new_lsr & SERIAL_LSR_PE) {
500 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100501 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500502 }
503 if (new_lsr & SERIAL_LSR_FE) {
504 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100505 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500506 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500507}
508
509/************************************************************************/
510/************************************************************************/
511/* U S B C A L L B A C K F U N C T I O N S */
512/* U S B C A L L B A C K F U N C T I O N S */
513/************************************************************************/
514/************************************************************************/
515
David Howells7d12e782006-10-05 14:55:46 +0100516static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500517{
518 unsigned char *data;
519 struct moschip_port *mos7840_port;
520 __u8 regval = 0x0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100521 int result = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700522 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500523
Ming Leicdc97792008-02-24 18:41:47 +0800524 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100525
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700526 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500527 case 0:
528 /* success */
529 break;
530 case -ECONNRESET:
531 case -ENOENT:
532 case -ESHUTDOWN:
533 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800534 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700535 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500536 return;
537 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800538 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700539 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500540 goto exit;
541 }
542
Tony Cook84fe6e72009-04-18 22:55:06 +0930543 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
544 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500545 mos7840_port->MsrLsr, mos7840_port->port_num);
546 data = urb->transfer_buffer;
547 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930548 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500549 if (mos7840_port->MsrLsr == 0)
550 mos7840_handle_new_msr(mos7840_port, regval);
551 else if (mos7840_port->MsrLsr == 1)
552 mos7840_handle_new_lsr(mos7840_port, regval);
553
Oliver Neukum0de9a702007-03-16 20:28:28 +0100554exit:
555 spin_lock(&mos7840_port->pool_lock);
556 if (!mos7840_port->zombie)
557 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
558 spin_unlock(&mos7840_port->pool_lock);
559 if (result) {
560 dev_err(&urb->dev->dev,
561 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800562 __func__, result);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100563 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500564}
565
566static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100567 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500568{
569 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100570 struct usb_ctrlrequest *dr = mcs->dr;
571 unsigned char *buffer = mcs->ctrl_buf;
572 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500573
Paul B Schroeder3f542972006-08-31 19:41:47 -0500574 dr->bRequestType = MCS_RD_RTYPE;
575 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100576 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500577 dr->wIndex = cpu_to_le16(reg);
578 dr->wLength = cpu_to_le16(2);
579
580 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
581 (unsigned char *)dr, buffer, 2,
582 mos7840_control_callback, mcs);
583 mcs->control_urb->transfer_buffer_length = 2;
584 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
585 return ret;
586}
587
Donald0eafe4d2012-04-19 15:00:45 +0800588static void mos7840_set_led_callback(struct urb *urb)
589{
590 switch (urb->status) {
591 case 0:
592 /* Success */
593 break;
594 case -ECONNRESET:
595 case -ENOENT:
596 case -ESHUTDOWN:
597 /* This urb is terminated, clean up */
598 dbg("%s - urb shutting down with status: %d", __func__,
599 urb->status);
600 break;
601 default:
602 dbg("%s - nonzero urb status received: %d", __func__,
603 urb->status);
604 }
605}
606
607static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
608 __u16 reg)
609{
610 struct usb_device *dev = mcs->port->serial->dev;
611 struct usb_ctrlrequest *dr = mcs->dr;
612
613 dr->bRequestType = MCS_WR_RTYPE;
614 dr->bRequest = MCS_WRREQ;
615 dr->wValue = cpu_to_le16(wval);
616 dr->wIndex = cpu_to_le16(reg);
617 dr->wLength = cpu_to_le16(0);
618
619 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
620 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
621
622 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
623}
624
625static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
626 __u16 val)
627{
628 struct usb_device *dev = port->serial->dev;
629
630 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
631 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
632}
633
634static void mos7840_led_off(unsigned long arg)
635{
636 struct moschip_port *mcs = (struct moschip_port *) arg;
637
638 /* Turn off LED */
639 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
640 mod_timer(&mcs->led_timer2,
641 jiffies + msecs_to_jiffies(LED_OFF_MS));
642}
643
644static void mos7840_led_flag_off(unsigned long arg)
645{
646 struct moschip_port *mcs = (struct moschip_port *) arg;
647
648 mcs->led_flag = false;
649}
650
Paul B Schroeder3f542972006-08-31 19:41:47 -0500651/*****************************************************************************
652 * mos7840_interrupt_callback
653 * this is the callback function for when we have received data on the
654 * interrupt endpoint.
655 *****************************************************************************/
656
David Howells7d12e782006-10-05 14:55:46 +0100657static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500658{
659 int result;
660 int length;
661 struct moschip_port *mos7840_port;
662 struct usb_serial *serial;
663 __u16 Data;
664 unsigned char *data;
665 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100666 int i, rv = 0;
667 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700668 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500669
Tony Cook84fe6e72009-04-18 22:55:06 +0930670 dbg("%s", " : Entering");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500671
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700672 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500673 case 0:
674 /* success */
675 break;
676 case -ECONNRESET:
677 case -ENOENT:
678 case -ESHUTDOWN:
679 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800680 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700681 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500682 return;
683 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800684 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700685 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500686 goto exit;
687 }
688
689 length = urb->actual_length;
690 data = urb->transfer_buffer;
691
Ming Leicdc97792008-02-24 18:41:47 +0800692 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500693
694 /* Moschip get 5 bytes
695 * Byte 1 IIR Port 1 (port.number is 0)
696 * Byte 2 IIR Port 2 (port.number is 1)
697 * Byte 3 IIR Port 3 (port.number is 2)
698 * Byte 4 IIR Port 4 (port.number is 3)
699 * Byte 5 FIFO status for both */
700
701 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930702 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500703 return;
704 }
705
706 sp[0] = (__u8) data[0];
707 sp[1] = (__u8) data[1];
708 sp[2] = (__u8) data[2];
709 sp[3] = (__u8) data[3];
710 st = (__u8) data[4];
711
712 for (i = 0; i < serial->num_ports; i++) {
713 mos7840_port = mos7840_get_port_private(serial->port[i]);
714 wval =
715 (((__u16) serial->port[i]->number -
716 (__u16) (serial->minor)) + 1) << 8;
717 if (mos7840_port->open) {
718 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930719 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500720 } else {
721 switch (sp[i] & 0x0f) {
722 case SERIAL_IIR_RLS:
723 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930724 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500725 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100726 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500727 break;
728 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930729 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500730 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100731 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500732 break;
733 }
Oliver Neukum0de9a702007-03-16 20:28:28 +0100734 spin_lock(&mos7840_port->pool_lock);
735 if (!mos7840_port->zombie) {
736 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
737 } else {
738 spin_unlock(&mos7840_port->pool_lock);
739 return;
740 }
741 spin_unlock(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500742 }
743 }
744 }
Alan Cox880af9d2008-07-22 11:16:12 +0100745 if (!(rv < 0))
746 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100747 return;
748exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500749 result = usb_submit_urb(urb, GFP_ATOMIC);
750 if (result) {
751 dev_err(&urb->dev->dev,
752 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800753 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500754 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500755}
756
757static int mos7840_port_paranoia_check(struct usb_serial_port *port,
758 const char *function)
759{
760 if (!port) {
761 dbg("%s - port == NULL", function);
762 return -1;
763 }
764 if (!port->serial) {
765 dbg("%s - port->serial == NULL", function);
766 return -1;
767 }
768
769 return 0;
770}
771
772/* Inline functions to check the sanity of a pointer that is passed to us */
773static int mos7840_serial_paranoia_check(struct usb_serial *serial,
774 const char *function)
775{
776 if (!serial) {
777 dbg("%s - serial == NULL", function);
778 return -1;
779 }
780 if (!serial->type) {
781 dbg("%s - serial->type == NULL!", function);
782 return -1;
783 }
784
785 return 0;
786}
787
788static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
789 const char *function)
790{
791 /* if no port was specified, or it fails a paranoia check */
792 if (!port ||
793 mos7840_port_paranoia_check(port, function) ||
794 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100795 /* then say that we don't have a valid usb_serial thing,
796 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500797 return NULL;
798 }
799
800 return port->serial;
801}
802
803/*****************************************************************************
804 * mos7840_bulk_in_callback
805 * this is the callback function for when we have received data on the
806 * bulk in endpoint.
807 *****************************************************************************/
808
David Howells7d12e782006-10-05 14:55:46 +0100809static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500810{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700811 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500812 unsigned char *data;
813 struct usb_serial *serial;
814 struct usb_serial_port *port;
815 struct moschip_port *mos7840_port;
816 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700817 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500818
Ming Leicdc97792008-02-24 18:41:47 +0800819 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500820 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930821 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800822 return;
823 }
824
825 if (status) {
826 dbg("nonzero read bulk status received: %d", status);
827 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500828 return;
829 }
830
831 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800832 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930833 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800834 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500835 return;
836 }
837
Harvey Harrison441b62c2008-03-03 16:08:34 -0800838 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500839 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930840 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800841 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500842 return;
843 }
844
Tony Cook84fe6e72009-04-18 22:55:06 +0930845 dbg("%s", "Entering... ");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500846
847 data = urb->transfer_buffer;
848
Tony Cook84fe6e72009-04-18 22:55:06 +0930849 dbg("%s", "Entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500850
851 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100852 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500853 if (tty) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500854 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930855 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500856 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100857 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500858 }
859 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100860 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930861 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500862 mos7840_port->icount.rx);
863 }
864
865 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930866 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800867 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500868 return;
869 }
870
Donald0eafe4d2012-04-19 15:00:45 +0800871 /* Turn on LED */
872 if (mos7840_port->has_led && !mos7840_port->led_flag) {
873 mos7840_port->led_flag = true;
874 mos7840_set_led_async(mos7840_port, 0x0301,
875 MODEM_CONTROL_REGISTER);
876 mod_timer(&mos7840_port->led_timer1,
877 jiffies + msecs_to_jiffies(LED_ON_MS));
878 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500879
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800880 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700881 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100882
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700883 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800884 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
885 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500886 }
887}
888
889/*****************************************************************************
890 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100891 * this is the callback function for when we have finished sending
892 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500893 *****************************************************************************/
894
David Howells7d12e782006-10-05 14:55:46 +0100895static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500896{
897 struct moschip_port *mos7840_port;
898 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700899 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100900 int i;
901
Ming Leicdc97792008-02-24 18:41:47 +0800902 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100903 spin_lock(&mos7840_port->pool_lock);
904 for (i = 0; i < NUM_URBS; i++) {
905 if (urb == mos7840_port->write_urb_pool[i]) {
906 mos7840_port->busy[i] = 0;
907 break;
908 }
909 }
910 spin_unlock(&mos7840_port->pool_lock);
911
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700912 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930913 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500914 return;
915 }
916
Harvey Harrison441b62c2008-03-03 16:08:34 -0800917 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930918 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500919 return;
920 }
921
Tony Cook84fe6e72009-04-18 22:55:06 +0930922 dbg("%s", "Entering .........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500923
Alan Cox4a90f092008-10-13 10:39:46 +0100924 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800925 if (tty && mos7840_port->open)
926 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100927 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500928
929}
930
931/************************************************************************/
932/* 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 */
933/************************************************************************/
934#ifdef MCSSerialProbe
935static int mos7840_serial_probe(struct usb_serial *serial,
936 const struct usb_device_id *id)
937{
938
939 /*need to implement the mode_reg reading and updating\
940 structures usb_serial_ device_type\
941 (i.e num_ports, num_bulkin,bulkout etc) */
942 /* Also we can update the changes attach */
943 return 1;
944}
945#endif
946
947/*****************************************************************************
948 * mos7840_open
949 * this function is called by the tty driver when a port is opened
950 * If successful, we return 0
951 * Otherwise we return a negative error number.
952 *****************************************************************************/
953
Alan Coxa509a7e2009-09-19 13:13:26 -0700954static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500955{
956 int response;
957 int j;
958 struct usb_serial *serial;
959 struct urb *urb;
960 __u16 Data;
961 int status;
962 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100963 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500964
Tony Cook84fe6e72009-04-18 22:55:06 +0930965 dbg ("%s enter", __func__);
966
Harvey Harrison441b62c2008-03-03 16:08:34 -0800967 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930968 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500969 return -ENODEV;
970 }
971
Paul B Schroeder3f542972006-08-31 19:41:47 -0500972 serial = port->serial;
973
Harvey Harrison441b62c2008-03-03 16:08:34 -0800974 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930975 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500976 return -ENODEV;
977 }
978
979 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100980 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500981
Oliver Neukum0de9a702007-03-16 20:28:28 +0100982 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500983 return -ENODEV;
984
985 usb_clear_halt(serial->dev, port->write_urb->pipe);
986 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100987 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500988
989 /* Initialising the write urb pool */
990 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100991 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500992 mos7840_port->write_urb_pool[j] = urb;
993
994 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700995 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500996 continue;
997 }
998
Alan Cox880af9d2008-07-22 11:16:12 +0100999 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1000 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001001 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001002 usb_free_urb(urb);
1003 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001004 dev_err(&port->dev,
1005 "%s-out of memory for urb buffers.\n",
1006 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001007 continue;
1008 }
1009 }
1010
1011/*****************************************************************************
1012 * Initialize MCS7840 -- Write Init values to corresponding Registers
1013 *
1014 * Register Index
1015 * 1 : IER
1016 * 2 : FCR
1017 * 3 : LCR
1018 * 4 : MCR
1019 *
1020 * 0x08 : SP1/2 Control Reg
1021 *****************************************************************************/
1022
Alan Cox880af9d2008-07-22 11:16:12 +01001023 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001024
Paul B Schroeder3f542972006-08-31 19:41:47 -05001025 Data = 0x0;
1026 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1027 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301028 dbg("Reading Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001029 return -1;
1030 }
1031 Data |= 0x80;
1032 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1033 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301034 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001035 return -1;
1036 }
1037
1038 Data &= ~0x80;
1039 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1040 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301041 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001042 return -1;
1043 }
Alan Cox880af9d2008-07-22 11:16:12 +01001044 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001045
Paul B Schroeder3f542972006-08-31 19:41:47 -05001046 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001047 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1048 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001049 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301050 dbg("Reading Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001051 return -1;
1052 }
Alan Cox880af9d2008-07-22 11:16:12 +01001053 Data |= 0x08; /* Driver done bit */
1054 Data |= 0x20; /* rx_disable */
1055 status = mos7840_set_reg_sync(port,
1056 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001057 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301058 dbg("writing Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001059 return -1;
1060 }
Alan Cox880af9d2008-07-22 11:16:12 +01001061 /* do register settings here */
1062 /* Set all regs to the device default values. */
1063 /***********************************
1064 * First Disable all interrupts.
1065 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -05001066 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001067 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1068 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301069 dbg("disabling interrupts failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001070 return -1;
1071 }
Alan Cox880af9d2008-07-22 11:16:12 +01001072 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001073 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001074 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1075 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301076 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001077 return -1;
1078 }
1079
1080 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001081 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1082 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301083 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001084 return -1;
1085 }
1086
1087 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001088 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1089 mos7840_port->shadowLCR = Data;
1090
1091 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001092 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1093 mos7840_port->shadowMCR = Data;
1094
1095 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001096 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1097 mos7840_port->shadowLCR = Data;
1098
Alan Cox880af9d2008-07-22 11:16:12 +01001099 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001100 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1101
1102 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001103 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1104
1105 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001106 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1107
1108 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001109 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1110
1111 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001112 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1113 mos7840_port->shadowLCR = Data;
1114
Alan Cox880af9d2008-07-22 11:16:12 +01001115 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001116 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001117 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1118
1119 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001120 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1121
1122 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001123 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001124 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001125 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001126 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1127
Alan Cox880af9d2008-07-22 11:16:12 +01001128 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001129 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001130 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1131 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001132 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001133 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1134 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001135
Alan Cox880af9d2008-07-22 11:16:12 +01001136 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001137 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001138 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1139 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001140 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001141 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1142 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001143
Alan Cox880af9d2008-07-22 11:16:12 +01001144 /* Check to see if we've set up our endpoint info yet *
1145 * (can't set it up in mos7840_startup as the structures *
1146 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001147 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001148 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001149 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001150 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001151 serial->dev,
1152 usb_rcvintpipe(serial->dev,
1153 serial->port[0]->interrupt_in_endpointAddress),
1154 serial->port[0]->interrupt_in_buffer,
1155 serial->port[0]->interrupt_in_urb->
1156 transfer_buffer_length,
1157 mos7840_interrupt_callback,
1158 serial,
1159 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001160
1161 /* start interrupt read for mos7840 *
1162 * will continue as long as mos7840 is connected */
1163
1164 response =
1165 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1166 GFP_KERNEL);
1167 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001168 dev_err(&port->dev, "%s - Error %d submitting "
1169 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001170 }
1171
1172 }
1173
1174 }
1175
1176 /* see if we've set up our endpoint info yet *
1177 * (can't set it up in mos7840_startup as the *
1178 * structures were not set up at that time.) */
1179
Tony Cook84fe6e72009-04-18 22:55:06 +09301180 dbg("port number is %d", port->number);
1181 dbg("serial number is %d", port->serial->minor);
1182 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1183 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1184 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1185 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001186 mos7840_port->read_urb = port->read_urb;
1187
1188 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001189 if ((serial->num_ports == 2)
1190 && ((((__u16)port->number -
1191 (__u16)(port->serial->minor)) % 2) != 0)) {
1192 usb_fill_bulk_urb(mos7840_port->read_urb,
1193 serial->dev,
1194 usb_rcvbulkpipe(serial->dev,
1195 (port->bulk_in_endpointAddress) + 2),
1196 port->bulk_in_buffer,
1197 mos7840_port->read_urb->transfer_buffer_length,
1198 mos7840_bulk_in_callback, mos7840_port);
1199 } else {
1200 usb_fill_bulk_urb(mos7840_port->read_urb,
1201 serial->dev,
1202 usb_rcvbulkpipe(serial->dev,
1203 port->bulk_in_endpointAddress),
1204 port->bulk_in_buffer,
1205 mos7840_port->read_urb->transfer_buffer_length,
1206 mos7840_bulk_in_callback, mos7840_port);
1207 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001208
Tony Cook84fe6e72009-04-18 22:55:06 +09301209 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001210 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001211 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001212 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1213 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001214 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1215 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001216 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001217 }
1218
1219 /* initialize our wait queues */
1220 init_waitqueue_head(&mos7840_port->wait_chase);
1221 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1222
1223 /* initialize our icount structure */
1224 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1225
1226 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001227 /* Must set to enable ints! */
1228 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001229 /* send a open port command */
1230 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001231 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001232 mos7840_port->icount.tx = 0;
1233 mos7840_port->icount.rx = 0;
1234
Tony Cook84fe6e72009-04-18 22:55:06 +09301235 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001236 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001237
Tony Cook84fe6e72009-04-18 22:55:06 +09301238 dbg ("%s leave", __func__);
1239
Paul B Schroeder3f542972006-08-31 19:41:47 -05001240 return 0;
1241
1242}
1243
1244/*****************************************************************************
1245 * mos7840_chars_in_buffer
1246 * this function is called by the tty driver when it wants to know how many
1247 * bytes of data we currently have outstanding in the port (data that has
1248 * been written, but hasn't made it out the port yet)
1249 * If successful, we return the number of bytes left to be written in the
1250 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001251 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001252 *****************************************************************************/
1253
Alan Cox95da3102008-07-22 11:09:07 +01001254static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001255{
Alan Cox95da3102008-07-22 11:09:07 +01001256 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001257 int i;
1258 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001259 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001260 struct moschip_port *mos7840_port;
1261
Tony Cook84fe6e72009-04-18 22:55:06 +09301262 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001263
Harvey Harrison441b62c2008-03-03 16:08:34 -08001264 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301265 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001266 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001267 }
1268
1269 mos7840_port = mos7840_get_port_private(port);
1270 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301271 dbg("%s", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001272 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001273 }
1274
Alan Cox880af9d2008-07-22 11:16:12 +01001275 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +01001276 for (i = 0; i < NUM_URBS; ++i)
1277 if (mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001278 chars += URB_TRANSFER_BUFFER_SIZE;
Alan Cox880af9d2008-07-22 11:16:12 +01001279 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001280 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001281 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001282
1283}
1284
Paul B Schroeder3f542972006-08-31 19:41:47 -05001285/*****************************************************************************
1286 * mos7840_close
1287 * this function is called by the tty driver when a port is closed
1288 *****************************************************************************/
1289
Alan Cox335f8512009-06-11 12:26:29 +01001290static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001291{
1292 struct usb_serial *serial;
1293 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001294 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001295 int j;
1296 __u16 Data;
1297
Tony Cook84fe6e72009-04-18 22:55:06 +09301298 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001299
Harvey Harrison441b62c2008-03-03 16:08:34 -08001300 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301301 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001302 return;
1303 }
1304
Harvey Harrison441b62c2008-03-03 16:08:34 -08001305 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001306 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301307 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001308 return;
1309 }
1310
1311 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001312 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001313
Oliver Neukum0de9a702007-03-16 20:28:28 +01001314 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001315 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001316
1317 for (j = 0; j < NUM_URBS; ++j)
1318 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1319
1320 /* Freeing Write URBs */
1321 for (j = 0; j < NUM_URBS; ++j) {
1322 if (mos7840_port->write_urb_pool[j]) {
1323 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1324 kfree(mos7840_port->write_urb_pool[j]->
1325 transfer_buffer);
1326
1327 usb_free_urb(mos7840_port->write_urb_pool[j]);
1328 }
1329 }
1330
Paul B Schroeder3f542972006-08-31 19:41:47 -05001331 /* While closing port, shutdown all bulk read, write *
1332 * and interrupt read if they exists */
1333 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001334 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301335 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001336 usb_kill_urb(mos7840_port->write_urb);
1337 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001338 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301339 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001340 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001341 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001342 }
1343 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301344 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001345 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001346 }
1347 }
Alan Cox880af9d2008-07-22 11:16:12 +01001348/* if(mos7840_port->ctrl_buf != NULL) */
1349/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001350 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301351 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001352 port0->open_ports, port->number);
1353 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001354 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301355 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001356 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001357 }
1358 }
1359
1360 if (mos7840_port->write_urb) {
1361 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001362 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001363 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001364 usb_free_urb(mos7840_port->write_urb);
1365 }
1366
1367 Data = 0x0;
1368 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1369
1370 Data = 0x00;
1371 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1372
1373 mos7840_port->open = 0;
1374
Tony Cook84fe6e72009-04-18 22:55:06 +09301375 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001376}
1377
1378/************************************************************************
1379 *
1380 * mos7840_block_until_chase_response
1381 *
1382 * This function will block the close until one of the following:
1383 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001384 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001385 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1386 *
1387 ************************************************************************/
1388
Alan Cox95da3102008-07-22 11:09:07 +01001389static void mos7840_block_until_chase_response(struct tty_struct *tty,
1390 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001391{
1392 int timeout = 1 * HZ;
1393 int wait = 10;
1394 int count;
1395
1396 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001397 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001398
1399 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001400 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001401 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001402
1403 /* Block the thread for a while */
1404 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1405 timeout);
1406 /* No activity.. count down section */
1407 wait--;
1408 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001409 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001410 return;
1411 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001412 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001413 wait = 10;
1414 }
1415 }
1416
1417}
1418
1419/*****************************************************************************
1420 * mos7840_break
1421 * this function sends a break to the port
1422 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001423static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001424{
Alan Cox95da3102008-07-22 11:09:07 +01001425 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001426 unsigned char data;
1427 struct usb_serial *serial;
1428 struct moschip_port *mos7840_port;
1429
Tony Cook84fe6e72009-04-18 22:55:06 +09301430 dbg("%s", "Entering ...........");
1431 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001432
Harvey Harrison441b62c2008-03-03 16:08:34 -08001433 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301434 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001435 return;
1436 }
1437
Harvey Harrison441b62c2008-03-03 16:08:34 -08001438 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001439 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301440 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001441 return;
1442 }
1443
1444 mos7840_port = mos7840_get_port_private(port);
1445
Alan Cox880af9d2008-07-22 11:16:12 +01001446 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001447 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001448
Alan Cox95da3102008-07-22 11:09:07 +01001449 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001450 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001451 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001452
Alan Cox95da3102008-07-22 11:09:07 +01001453 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001454 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001455 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001456 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001457
Alan Cox6b447f042009-01-02 13:48:56 +00001458 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001459 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301460 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001461 mos7840_port->shadowLCR);
1462 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1463 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001464}
1465
1466/*****************************************************************************
1467 * mos7840_write_room
1468 * this function is called by the tty driver when it wants to know how many
1469 * bytes of data we can accept for a specific port.
1470 * If successful, we return the amount of room that we have for this port
1471 * Otherwise we return a negative error number.
1472 *****************************************************************************/
1473
Alan Cox95da3102008-07-22 11:09:07 +01001474static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001475{
Alan Cox95da3102008-07-22 11:09:07 +01001476 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001477 int i;
1478 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001479 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001480 struct moschip_port *mos7840_port;
1481
Tony Cook84fe6e72009-04-18 22:55:06 +09301482 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001483
Harvey Harrison441b62c2008-03-03 16:08:34 -08001484 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301485 dbg("%s", "Invalid port");
1486 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001487 return -1;
1488 }
1489
1490 mos7840_port = mos7840_get_port_private(port);
1491 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301492 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001493 return -1;
1494 }
1495
Oliver Neukum0de9a702007-03-16 20:28:28 +01001496 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001497 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001498 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001499 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001500 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001501 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001502
Oliver Neukum0de9a702007-03-16 20:28:28 +01001503 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001504 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001505 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001506
1507}
1508
1509/*****************************************************************************
1510 * mos7840_write
1511 * this function is called by the tty driver when data should be written to
1512 * the port.
1513 * If successful, we return the number of bytes written, otherwise we
1514 * return a negative error number.
1515 *****************************************************************************/
1516
Alan Cox95da3102008-07-22 11:09:07 +01001517static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001518 const unsigned char *data, int count)
1519{
1520 int status;
1521 int i;
1522 int bytes_sent = 0;
1523 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001524 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001525
1526 struct moschip_port *mos7840_port;
1527 struct usb_serial *serial;
1528 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001529 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001530 const unsigned char *current_position = data;
1531 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301532 dbg("%s", "entering ...........");
1533 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001534 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001535
1536#ifdef NOTMOS7840
1537 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001538 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1539 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301540 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1541 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001542 mos7840_port->shadowLCR);
1543
Alan Cox880af9d2008-07-22 11:16:12 +01001544 /* Data = 0x03; */
1545 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1546 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001547
Alan Cox880af9d2008-07-22 11:16:12 +01001548 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001549 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1550
Alan Cox880af9d2008-07-22 11:16:12 +01001551 /* Data = 0x0c; */
1552 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001553 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001554 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301555 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001556
1557 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001558 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301559 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001560
1561 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301562 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001563 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001564 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1565#endif
1566
Harvey Harrison441b62c2008-03-03 16:08:34 -08001567 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301568 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001569 return -1;
1570 }
1571
1572 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001573 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301574 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001575 return -1;
1576 }
1577
1578 mos7840_port = mos7840_get_port_private(port);
1579 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301580 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001581 return -1;
1582 }
1583
1584 /* try to find a free urb in the list */
1585 urb = NULL;
1586
Oliver Neukum0de9a702007-03-16 20:28:28 +01001587 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001588 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001589 if (!mos7840_port->busy[i]) {
1590 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001591 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301592 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001593 break;
1594 }
1595 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001596 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001597
1598 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001599 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001600 goto exit;
1601 }
1602
1603 if (urb->transfer_buffer == NULL) {
1604 urb->transfer_buffer =
1605 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1606
1607 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001608 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001609 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001610 goto exit;
1611 }
1612 }
1613 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1614
Al Viro97c49652006-10-09 20:29:03 +01001615 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001616
1617 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001618 if ((serial->num_ports == 2)
1619 && ((((__u16)port->number -
1620 (__u16)(port->serial->minor)) % 2) != 0)) {
1621 usb_fill_bulk_urb(urb,
1622 serial->dev,
1623 usb_sndbulkpipe(serial->dev,
1624 (port->bulk_out_endpointAddress) + 2),
1625 urb->transfer_buffer,
1626 transfer_size,
1627 mos7840_bulk_out_data_callback, mos7840_port);
1628 } else {
1629 usb_fill_bulk_urb(urb,
1630 serial->dev,
1631 usb_sndbulkpipe(serial->dev,
1632 port->bulk_out_endpointAddress),
1633 urb->transfer_buffer,
1634 transfer_size,
1635 mos7840_bulk_out_data_callback, mos7840_port);
1636 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001637
1638 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301639 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001640
Donald0eafe4d2012-04-19 15:00:45 +08001641 /* Turn on LED */
1642 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1643 mos7840_port->led_flag = true;
1644 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1645 mod_timer(&mos7840_port->led_timer1,
1646 jiffies + msecs_to_jiffies(LED_ON_MS));
1647 }
1648
Paul B Schroeder3f542972006-08-31 19:41:47 -05001649 /* send it down the pipe */
1650 status = usb_submit_urb(urb, GFP_ATOMIC);
1651
1652 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001653 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001654 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001655 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001656 bytes_sent = status;
1657 goto exit;
1658 }
1659 bytes_sent = transfer_size;
1660 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001661 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301662 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001663exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001664 return bytes_sent;
1665
1666}
1667
1668/*****************************************************************************
1669 * mos7840_throttle
1670 * this function is called by the tty driver when it wants to stop the data
1671 * being read from the port.
1672 *****************************************************************************/
1673
Alan Cox95da3102008-07-22 11:09:07 +01001674static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001675{
Alan Cox95da3102008-07-22 11:09:07 +01001676 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001677 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001678 int status;
1679
Harvey Harrison441b62c2008-03-03 16:08:34 -08001680 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301681 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001682 return;
1683 }
1684
Tony Cook84fe6e72009-04-18 22:55:06 +09301685 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001686
1687 mos7840_port = mos7840_get_port_private(port);
1688
1689 if (mos7840_port == NULL)
1690 return;
1691
1692 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301693 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001694 return;
1695 }
1696
Tony Cook84fe6e72009-04-18 22:55:06 +09301697 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001698
Paul B Schroeder3f542972006-08-31 19:41:47 -05001699 /* if we are implementing XON/XOFF, send the stop character */
1700 if (I_IXOFF(tty)) {
1701 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001702 status = mos7840_write(tty, port, &stop_char, 1);
1703 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001704 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001705 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001706 /* if we are implementing RTS/CTS, toggle that line */
1707 if (tty->termios->c_cflag & CRTSCTS) {
1708 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001709 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001710 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001711 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001712 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001713 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001714}
1715
1716/*****************************************************************************
1717 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001718 * this function is called by the tty driver when it wants to resume
1719 * the data being read from the port (called after mos7840_throttle is
1720 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001721 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001722static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001723{
Alan Cox95da3102008-07-22 11:09:07 +01001724 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001725 int status;
1726 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1727
Harvey Harrison441b62c2008-03-03 16:08:34 -08001728 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301729 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001730 return;
1731 }
1732
1733 if (mos7840_port == NULL)
1734 return;
1735
1736 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001737 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001738 return;
1739 }
1740
Tony Cook84fe6e72009-04-18 22:55:06 +09301741 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001742
Paul B Schroeder3f542972006-08-31 19:41:47 -05001743 /* if we are implementing XON/XOFF, send the start character */
1744 if (I_IXOFF(tty)) {
1745 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001746 status = mos7840_write(tty, port, &start_char, 1);
1747 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001748 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001749 }
1750
1751 /* if we are implementing RTS/CTS, toggle that line */
1752 if (tty->termios->c_cflag & CRTSCTS) {
1753 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001754 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001755 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001756 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001757 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001758 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001759}
1760
Alan Cox60b33c12011-02-14 16:26:14 +00001761static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001762{
Alan Cox95da3102008-07-22 11:09:07 +01001763 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001764 struct moschip_port *mos7840_port;
1765 unsigned int result;
1766 __u16 msr;
1767 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001768 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001769 mos7840_port = mos7840_get_port_private(port);
1770
Harvey Harrison441b62c2008-03-03 16:08:34 -08001771 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001772
1773 if (mos7840_port == NULL)
1774 return -ENODEV;
1775
1776 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1777 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1778 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1779 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1780 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1781 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1782 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1783 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1784 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1785
Harvey Harrison441b62c2008-03-03 16:08:34 -08001786 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001787
1788 return result;
1789}
1790
Alan Cox20b9d172011-02-14 16:26:50 +00001791static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001792 unsigned int set, unsigned int clear)
1793{
Alan Cox95da3102008-07-22 11:09:07 +01001794 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001795 struct moschip_port *mos7840_port;
1796 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001797 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001798
Harvey Harrison441b62c2008-03-03 16:08:34 -08001799 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001800
1801 mos7840_port = mos7840_get_port_private(port);
1802
1803 if (mos7840_port == NULL)
1804 return -ENODEV;
1805
Alan Coxe2984492008-02-20 20:51:45 +00001806 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001807 mcr = mos7840_port->shadowMCR;
1808 if (clear & TIOCM_RTS)
1809 mcr &= ~MCR_RTS;
1810 if (clear & TIOCM_DTR)
1811 mcr &= ~MCR_DTR;
1812 if (clear & TIOCM_LOOP)
1813 mcr &= ~MCR_LOOPBACK;
1814
1815 if (set & TIOCM_RTS)
1816 mcr |= MCR_RTS;
1817 if (set & TIOCM_DTR)
1818 mcr |= MCR_DTR;
1819 if (set & TIOCM_LOOP)
1820 mcr |= MCR_LOOPBACK;
1821
1822 mos7840_port->shadowMCR = mcr;
1823
Paul B Schroeder3f542972006-08-31 19:41:47 -05001824 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1825 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301826 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001827 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001828 }
1829
1830 return 0;
1831}
1832
1833/*****************************************************************************
1834 * mos7840_calc_baud_rate_divisor
1835 * this function calculates the proper baud rate divisor for the specified
1836 * baud rate.
1837 *****************************************************************************/
1838static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001839 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001840{
1841
Harvey Harrison441b62c2008-03-03 16:08:34 -08001842 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001843
1844 if (baudRate <= 115200) {
1845 *divisor = 115200 / baudRate;
1846 *clk_sel_val = 0x0;
1847 }
1848 if ((baudRate > 115200) && (baudRate <= 230400)) {
1849 *divisor = 230400 / baudRate;
1850 *clk_sel_val = 0x10;
1851 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1852 *divisor = 403200 / baudRate;
1853 *clk_sel_val = 0x20;
1854 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1855 *divisor = 460800 / baudRate;
1856 *clk_sel_val = 0x30;
1857 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1858 *divisor = 806400 / baudRate;
1859 *clk_sel_val = 0x40;
1860 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1861 *divisor = 921600 / baudRate;
1862 *clk_sel_val = 0x50;
1863 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1864 *divisor = 1572864 / baudRate;
1865 *clk_sel_val = 0x60;
1866 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1867 *divisor = 3145728 / baudRate;
1868 *clk_sel_val = 0x70;
1869 }
1870 return 0;
1871
1872#ifdef NOTMCS7840
1873
1874 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1875 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1876 *divisor = mos7840_divisor_table[i].Divisor;
1877 return 0;
1878 }
1879 }
1880
1881 /* After trying for all the standard baud rates *
1882 * Try calculating the divisor for this baud rate */
1883
1884 if (baudrate > 75 && baudrate < 230400) {
1885 /* get the divisor */
1886 custom = (__u16) (230400L / baudrate);
1887
1888 /* Check for round off */
1889 round1 = (__u16) (2304000L / baudrate);
1890 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001891 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001892 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001893 *divisor = custom;
1894
Tony Cook84fe6e72009-04-18 22:55:06 +09301895 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001896 return 0;
1897 }
1898
Tony Cook84fe6e72009-04-18 22:55:06 +09301899 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001900 return -1;
1901#endif
1902}
1903
1904/*****************************************************************************
1905 * mos7840_send_cmd_write_baud_rate
1906 * this function sends the proper command to change the baud rate of the
1907 * specified port.
1908 *****************************************************************************/
1909
1910static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1911 int baudRate)
1912{
1913 int divisor = 0;
1914 int status;
1915 __u16 Data;
1916 unsigned char number;
1917 __u16 clk_sel_val;
1918 struct usb_serial_port *port;
1919
1920 if (mos7840_port == NULL)
1921 return -1;
1922
1923 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001924 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301925 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001926 return -1;
1927 }
1928
Harvey Harrison441b62c2008-03-03 16:08:34 -08001929 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301930 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001931 return -1;
1932 }
1933
Tony Cook84fe6e72009-04-18 22:55:06 +09301934 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001935
1936 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1937
Harvey Harrison441b62c2008-03-03 16:08:34 -08001938 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001939 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001940 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001941 if (baudRate > 115200) {
1942#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001943 /* NOTE: need to see the pther register to modify */
1944 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001945 Data = 0x2b;
1946 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001947 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1948 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001949 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301950 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001951 return -1;
1952 }
1953#endif
1954
1955 } else {
1956#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001957 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001958 Data = 0xb;
1959 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001960 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1961 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001962 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301963 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001964 return -1;
1965 }
1966#endif
1967
1968 }
1969
Alan Cox880af9d2008-07-22 11:16:12 +01001970 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001971 clk_sel_val = 0x0;
1972 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001973 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001974 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001975 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1976 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001977 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301978 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001979 return -1;
1980 }
1981 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001982 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1983 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001984 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301985 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001986 return -1;
1987 }
1988 /* Calculate the Divisor */
1989
1990 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001991 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001992 return status;
1993 }
1994 /* Enable access to divisor latch */
1995 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1996 mos7840_port->shadowLCR = Data;
1997 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1998
1999 /* Write the divisor */
2000 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09302001 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002002 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
2003
2004 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09302005 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002006 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
2007
2008 /* Disable access to divisor latch */
2009 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
2010 mos7840_port->shadowLCR = Data;
2011 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2012
2013 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002014 return status;
2015}
2016
2017/*****************************************************************************
2018 * mos7840_change_port_settings
2019 * This routine is called to set the UART on the device to match
2020 * the specified new settings.
2021 *****************************************************************************/
2022
Alan Cox95da3102008-07-22 11:09:07 +01002023static void mos7840_change_port_settings(struct tty_struct *tty,
2024 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002025{
Paul B Schroeder3f542972006-08-31 19:41:47 -05002026 int baud;
2027 unsigned cflag;
2028 unsigned iflag;
2029 __u8 lData;
2030 __u8 lParity;
2031 __u8 lStop;
2032 int status;
2033 __u16 Data;
2034 struct usb_serial_port *port;
2035 struct usb_serial *serial;
2036
2037 if (mos7840_port == NULL)
2038 return;
2039
2040 port = (struct usb_serial_port *)mos7840_port->port;
2041
Harvey Harrison441b62c2008-03-03 16:08:34 -08002042 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302043 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002044 return;
2045 }
2046
Harvey Harrison441b62c2008-03-03 16:08:34 -08002047 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302048 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002049 return;
2050 }
2051
2052 serial = port->serial;
2053
Harvey Harrison441b62c2008-03-03 16:08:34 -08002054 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002055
2056 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002057 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002058 return;
2059 }
2060
Tony Cook84fe6e72009-04-18 22:55:06 +09302061 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002062
2063 lData = LCR_BITS_8;
2064 lStop = LCR_STOP_1;
2065 lParity = LCR_PAR_NONE;
2066
2067 cflag = tty->termios->c_cflag;
2068 iflag = tty->termios->c_iflag;
2069
2070 /* Change the number of bits */
2071 if (cflag & CSIZE) {
2072 switch (cflag & CSIZE) {
2073 case CS5:
2074 lData = LCR_BITS_5;
2075 break;
2076
2077 case CS6:
2078 lData = LCR_BITS_6;
2079 break;
2080
2081 case CS7:
2082 lData = LCR_BITS_7;
2083 break;
2084 default:
2085 case CS8:
2086 lData = LCR_BITS_8;
2087 break;
2088 }
2089 }
2090 /* Change the Parity bit */
2091 if (cflag & PARENB) {
2092 if (cflag & PARODD) {
2093 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002094 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002095 } else {
2096 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002097 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002098 }
2099
2100 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002101 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002102 }
2103
Alan Cox880af9d2008-07-22 11:16:12 +01002104 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002105 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002106
2107 /* Change the Stop bit */
2108 if (cflag & CSTOPB) {
2109 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002110 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002111 } else {
2112 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002113 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002114 }
2115
2116 /* Update the LCR with the correct value */
2117 mos7840_port->shadowLCR &=
2118 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2119 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2120
Tony Cook84fe6e72009-04-18 22:55:06 +09302121 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002122 mos7840_port->shadowLCR);
2123 /* Disable Interrupts */
2124 Data = 0x00;
2125 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2126
2127 Data = 0x00;
2128 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2129
2130 Data = 0xcf;
2131 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2132
2133 /* Send the updated LCR value to the mos7840 */
2134 Data = mos7840_port->shadowLCR;
2135
2136 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2137
2138 Data = 0x00b;
2139 mos7840_port->shadowMCR = Data;
2140 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2141 Data = 0x00b;
2142 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2143
2144 /* set up the MCR register and send it to the mos7840 */
2145
2146 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002147 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002148 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002149
Alan Cox880af9d2008-07-22 11:16:12 +01002150 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002151 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002152 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002153 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002154
2155 Data = mos7840_port->shadowMCR;
2156 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2157
2158 /* Determine divisor based on baud rate */
2159 baud = tty_get_baud_rate(tty);
2160
2161 if (!baud) {
2162 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302163 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002164 baud = 9600;
2165 }
2166
Harvey Harrison441b62c2008-03-03 16:08:34 -08002167 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002168 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2169
2170 /* Enable Interrupts */
2171 Data = 0x0c;
2172 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2173
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002174 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002175 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002176 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002177 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002178 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002179 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002180 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002181 }
2182 }
2183 wake_up(&mos7840_port->delta_msr_wait);
2184 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302185 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002186 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002187}
2188
2189/*****************************************************************************
2190 * mos7840_set_termios
2191 * this function is called by the tty driver when it wants to change
2192 * the termios structure
2193 *****************************************************************************/
2194
Alan Cox95da3102008-07-22 11:09:07 +01002195static void mos7840_set_termios(struct tty_struct *tty,
2196 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002197 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002198{
2199 int status;
2200 unsigned int cflag;
2201 struct usb_serial *serial;
2202 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302203 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002204 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302205 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002206 return;
2207 }
2208
2209 serial = port->serial;
2210
Harvey Harrison441b62c2008-03-03 16:08:34 -08002211 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302212 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002213 return;
2214 }
2215
2216 mos7840_port = mos7840_get_port_private(port);
2217
2218 if (mos7840_port == NULL)
2219 return;
2220
Paul B Schroeder3f542972006-08-31 19:41:47 -05002221 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002222 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002223 return;
2224 }
2225
Tony Cook84fe6e72009-04-18 22:55:06 +09302226 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002227
2228 cflag = tty->termios->c_cflag;
2229
Harvey Harrison441b62c2008-03-03 16:08:34 -08002230 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002231 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002232 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002233 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002234 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002235
2236 /* change the port settings to the new ones specified */
2237
Alan Cox95da3102008-07-22 11:09:07 +01002238 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002239
2240 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302241 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002242 return;
2243 }
2244
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002245 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002246 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002247 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2248 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002249 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002250 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002251 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002252 }
2253 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002254}
2255
2256/*****************************************************************************
2257 * mos7840_get_lsr_info - get line status register info
2258 *
2259 * Purpose: Let user call ioctl() to get info when the UART physically
2260 * is emptied. On bus types like RS485, the transmitter must
2261 * release the bus after transmitting. This must be done when
2262 * the transmit shift register is empty, not be done when the
2263 * transmit holding register is empty. This functionality
2264 * allows an RS485 driver to be written in user space.
2265 *****************************************************************************/
2266
Alan Cox95da3102008-07-22 11:09:07 +01002267static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002268 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002269{
2270 int count;
2271 unsigned int result = 0;
2272
Alan Cox95da3102008-07-22 11:09:07 +01002273 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002274 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002275 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002276 result = TIOCSER_TEMT;
2277 }
2278
2279 if (copy_to_user(value, &result, sizeof(int)))
2280 return -EFAULT;
2281 return 0;
2282}
2283
2284/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002285 * mos7840_get_serial_info
2286 * function to get information about serial port
2287 *****************************************************************************/
2288
2289static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002290 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002291{
2292 struct serial_struct tmp;
2293
2294 if (mos7840_port == NULL)
2295 return -1;
2296
2297 if (!retinfo)
2298 return -EFAULT;
2299
2300 memset(&tmp, 0, sizeof(tmp));
2301
2302 tmp.type = PORT_16550A;
2303 tmp.line = mos7840_port->port->serial->minor;
2304 tmp.port = mos7840_port->port->number;
2305 tmp.irq = 0;
2306 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2307 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2308 tmp.baud_base = 9600;
2309 tmp.close_delay = 5 * HZ;
2310 tmp.closing_wait = 30 * HZ;
2311
2312 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2313 return -EFAULT;
2314 return 0;
2315}
2316
Alan Cox0bca1b92010-09-16 18:21:40 +01002317static int mos7840_get_icount(struct tty_struct *tty,
2318 struct serial_icounter_struct *icount)
2319{
2320 struct usb_serial_port *port = tty->driver_data;
2321 struct moschip_port *mos7840_port;
2322 struct async_icount cnow;
2323
2324 mos7840_port = mos7840_get_port_private(port);
2325 cnow = mos7840_port->icount;
2326
2327 smp_rmb();
2328 icount->cts = cnow.cts;
2329 icount->dsr = cnow.dsr;
2330 icount->rng = cnow.rng;
2331 icount->dcd = cnow.dcd;
2332 icount->rx = cnow.rx;
2333 icount->tx = cnow.tx;
2334 icount->frame = cnow.frame;
2335 icount->overrun = cnow.overrun;
2336 icount->parity = cnow.parity;
2337 icount->brk = cnow.brk;
2338 icount->buf_overrun = cnow.buf_overrun;
2339
2340 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2341 port->number, icount->rx, icount->tx);
2342 return 0;
2343}
2344
Paul B Schroeder3f542972006-08-31 19:41:47 -05002345/*****************************************************************************
2346 * SerialIoctl
2347 * this function handles any ioctl calls to the driver
2348 *****************************************************************************/
2349
Alan Cox00a0d0d2011-02-14 16:27:06 +00002350static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002351 unsigned int cmd, unsigned long arg)
2352{
Alan Cox95da3102008-07-22 11:09:07 +01002353 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002354 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002355 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002356
2357 struct async_icount cnow;
2358 struct async_icount cprev;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002359
Harvey Harrison441b62c2008-03-03 16:08:34 -08002360 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302361 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002362 return -1;
2363 }
2364
2365 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002366
2367 if (mos7840_port == NULL)
2368 return -1;
2369
Harvey Harrison441b62c2008-03-03 16:08:34 -08002370 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002371
2372 switch (cmd) {
2373 /* return number of bytes available */
2374
Paul B Schroeder3f542972006-08-31 19:41:47 -05002375 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002376 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002377 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002378
Paul B Schroeder3f542972006-08-31 19:41:47 -05002379 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002380 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002381 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002382
2383 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002384 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002385 break;
2386
2387 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002388 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002389 cprev = mos7840_port->icount;
2390 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002391 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002392 mos7840_port->delta_msr_cond = 0;
2393 wait_event_interruptible(mos7840_port->delta_msr_wait,
2394 (mos7840_port->
2395 delta_msr_cond == 1));
2396
2397 /* see if a signal did it */
2398 if (signal_pending(current))
2399 return -ERESTARTSYS;
2400 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002401 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002402 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2403 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2404 return -EIO; /* no change => error */
2405 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2406 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2407 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2408 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2409 return 0;
2410 }
2411 cprev = cnow;
2412 }
2413 /* NOTREACHED */
2414 break;
2415
Paul B Schroeder3f542972006-08-31 19:41:47 -05002416 default:
2417 break;
2418 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002419 return -ENOIOCTLCMD;
2420}
2421
Donald0eafe4d2012-04-19 15:00:45 +08002422static int mos7810_check(struct usb_serial *serial)
2423{
2424 int i, pass_count = 0;
2425 __u16 data = 0, mcr_data = 0;
2426 __u16 test_pattern = 0x55AA;
2427
2428 /* Store MCR setting */
2429 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2430 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2431 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2432
2433 for (i = 0; i < 16; i++) {
2434 /* Send the 1-bit test pattern out to MCS7810 test pin */
2435 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2436 MCS_WRREQ, MCS_WR_RTYPE,
2437 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2438 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2439
2440 /* Read the test pattern back */
2441 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2442 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2443 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2444
2445 /* If this is a MCS7810 device, both test patterns must match */
2446 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2447 break;
2448
2449 pass_count++;
2450 }
2451
2452 /* Restore MCR setting */
2453 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2454 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2455 0, MOS_WDR_TIMEOUT);
2456
2457 if (pass_count == 16)
2458 return 1;
2459
2460 return 0;
2461}
2462
Paul B Schroeder3f542972006-08-31 19:41:47 -05002463static int mos7840_calc_num_ports(struct usb_serial *serial)
2464{
Donald0eafe4d2012-04-19 15:00:45 +08002465 __u16 data = 0x00;
Donald Lee093ea2d2012-03-14 15:26:33 +08002466 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002467
Donald0eafe4d2012-04-19 15:00:45 +08002468 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2469 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
Donald Lee093ea2d2012-03-14 15:26:33 +08002470 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2471
Donald0eafe4d2012-04-19 15:00:45 +08002472 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2473 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2474 device_type = serial->dev->descriptor.idProduct;
Donald Lee093ea2d2012-03-14 15:26:33 +08002475 } else {
Donald0eafe4d2012-04-19 15:00:45 +08002476 /* For a MCS7840 device GPIO0 must be set to 1 */
2477 if ((data & 0x01) == 1)
2478 device_type = MOSCHIP_DEVICE_ID_7840;
2479 else if (mos7810_check(serial))
2480 device_type = MOSCHIP_DEVICE_ID_7810;
2481 else
2482 device_type = MOSCHIP_DEVICE_ID_7820;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002483 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002484
Donald0eafe4d2012-04-19 15:00:45 +08002485 mos7840_num_ports = (device_type >> 4) & 0x000F;
2486 serial->num_bulk_in = mos7840_num_ports;
2487 serial->num_bulk_out = mos7840_num_ports;
2488 serial->num_ports = mos7840_num_ports;
2489
Paul B Schroeder3f542972006-08-31 19:41:47 -05002490 return mos7840_num_ports;
2491}
2492
2493/****************************************************************************
2494 * mos7840_startup
2495 ****************************************************************************/
2496
2497static int mos7840_startup(struct usb_serial *serial)
2498{
2499 struct moschip_port *mos7840_port;
2500 struct usb_device *dev;
2501 int i, status;
2502
2503 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302504 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002505
2506 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302507 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002508 return -1;
2509 }
2510
2511 dev = serial->dev;
2512
Tony Cook84fe6e72009-04-18 22:55:06 +09302513 dbg("%s", "Entering...");
2514 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002515
2516 /* we set up the pointers to the endpoints in the mos7840_open *
2517 * function, as the structures aren't created yet. */
2518
2519 /* set up port private structures */
2520 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302521 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002522 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002523 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002524 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002525 status = -ENOMEM;
2526 i--; /* don't follow NULL pointer cleaning up */
2527 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002528 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002529
Alan Cox880af9d2008-07-22 11:16:12 +01002530 /* Initialize all port interrupt end point to port 0 int
2531 * endpoint. Our device has only one interrupt end point
2532 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002533
2534 mos7840_port->port = serial->port[i];
2535 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002536 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002537
Tony Cook37768ad2009-04-18 22:42:18 +09302538 /* minor is not initialised until later by
2539 * usb-serial.c:get_free_serial() and cannot therefore be used
2540 * to index device instances */
2541 mos7840_port->port_num = i + 1;
2542 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2543 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2544 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2545 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002546
2547 if (mos7840_port->port_num == 1) {
2548 mos7840_port->SpRegOffset = 0x0;
2549 mos7840_port->ControlRegOffset = 0x1;
2550 mos7840_port->DcrRegOffset = 0x4;
2551 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002552 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002553 mos7840_port->SpRegOffset = 0x8;
2554 mos7840_port->ControlRegOffset = 0x9;
2555 mos7840_port->DcrRegOffset = 0x16;
2556 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002557 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002558 mos7840_port->SpRegOffset = 0xa;
2559 mos7840_port->ControlRegOffset = 0xb;
2560 mos7840_port->DcrRegOffset = 0x19;
2561 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002562 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002563 mos7840_port->SpRegOffset = 0xa;
2564 mos7840_port->ControlRegOffset = 0xb;
2565 mos7840_port->DcrRegOffset = 0x19;
2566 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002567 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002568 mos7840_port->SpRegOffset = 0xc;
2569 mos7840_port->ControlRegOffset = 0xd;
2570 mos7840_port->DcrRegOffset = 0x1c;
2571 }
2572 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002573 mos7840_set_port_private(serial->port[i], mos7840_port);
2574
Alan Cox880af9d2008-07-22 11:16:12 +01002575 /* enable rx_disable bit in control register */
2576 status = mos7840_get_reg_sync(serial->port[i],
2577 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002578 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302579 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002580 break;
2581 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302582 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002583 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002584 Data |= 0x08; /* setting driver done bit */
2585 Data |= 0x04; /* sp1_bit to have cts change reflect in
2586 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002587
Alan Cox880af9d2008-07-22 11:16:12 +01002588 /* Data |= 0x20; //rx_disable bit */
2589 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002590 mos7840_port->ControlRegOffset, Data);
2591 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302592 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002593 break;
2594 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302595 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002596 status);
2597
Alan Cox880af9d2008-07-22 11:16:12 +01002598 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2599 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002600 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002601 status = mos7840_set_reg_sync(serial->port[i],
2602 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002603 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302604 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002605 break;
2606 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302607 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002608
2609 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002610 status = mos7840_set_reg_sync(serial->port[i],
2611 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002612 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302613 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002614 break;
2615 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302616 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002617
2618 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002619 status = mos7840_set_reg_sync(serial->port[i],
2620 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002621 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302622 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002623 break;
2624 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302625 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002626
Alan Cox880af9d2008-07-22 11:16:12 +01002627 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002628 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002629 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002630 CLK_START_VALUE_REGISTER, Data);
2631 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302632 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002633 break;
2634 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302635 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002636
2637 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002638 status = mos7840_set_reg_sync(serial->port[i],
2639 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002640 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302641 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002642 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002643 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002644 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302645 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002646 status);
2647
Alan Cox880af9d2008-07-22 11:16:12 +01002648 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002649 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002650 status = mos7840_set_uart_reg(serial->port[i],
2651 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002652 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302653 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002654 status);
2655 break;
2656 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302657 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002658 status);
2659
Alan Cox880af9d2008-07-22 11:16:12 +01002660 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002661 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002662 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002663
2664 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002665 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002666 (__u16) (ZLP_REG1 +
2667 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302668 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002669 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002670 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002671 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302672 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002673 i + 2, status);
2674 break;
2675 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302676 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002677 i + 2, status);
2678 } else {
2679 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002680 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002681 (__u16) (ZLP_REG1 +
2682 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302683 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002684 (__u16) (ZLP_REG1 +
2685 ((__u16) mos7840_port->port_num) - 0x1));
2686 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302687 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002688 i + 1, status);
2689 break;
2690 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302691 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002692 i + 1, status);
2693
2694 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002695 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002696 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002697 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2698 GFP_KERNEL);
2699 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2700 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002701 status = -ENOMEM;
2702 goto error;
2703 }
Donald0eafe4d2012-04-19 15:00:45 +08002704
2705 mos7840_port->has_led = false;
2706
2707 /* Initialize LED timers */
2708 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2709 mos7840_port->has_led = true;
2710
2711 init_timer(&mos7840_port->led_timer1);
2712 mos7840_port->led_timer1.function = mos7840_led_off;
2713 mos7840_port->led_timer1.expires =
2714 jiffies + msecs_to_jiffies(LED_ON_MS);
2715 mos7840_port->led_timer1.data =
2716 (unsigned long)mos7840_port;
2717
2718 init_timer(&mos7840_port->led_timer2);
2719 mos7840_port->led_timer2.function =
2720 mos7840_led_flag_off;
2721 mos7840_port->led_timer2.expires =
2722 jiffies + msecs_to_jiffies(LED_OFF_MS);
2723 mos7840_port->led_timer2.data =
2724 (unsigned long)mos7840_port;
2725
2726 mos7840_port->led_flag = false;
2727
2728 /* Turn off LED */
2729 mos7840_set_led_sync(serial->port[i],
2730 MODEM_CONTROL_REGISTER, 0x0300);
2731 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002732 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302733 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002734
Alan Cox880af9d2008-07-22 11:16:12 +01002735 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002736 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002737 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2738 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302739 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002740 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002741 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302742 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002743
2744 /* setting configuration feature to one */
2745 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002746 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002747 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002748error:
2749 for (/* nothing */; i >= 0; i--) {
2750 mos7840_port = mos7840_get_port_private(serial->port[i]);
2751
2752 kfree(mos7840_port->dr);
2753 kfree(mos7840_port->ctrl_buf);
2754 usb_free_urb(mos7840_port->control_urb);
2755 kfree(mos7840_port);
2756 serial->port[i] = NULL;
2757 }
2758 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002759}
2760
2761/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002762 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002763 * This function is called whenever the device is removed from the usb bus.
2764 ****************************************************************************/
2765
Alan Sternf9c99bb2009-06-02 11:53:55 -04002766static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002767{
2768 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002769 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002770 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002771 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002772
2773 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302774 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002775 return;
2776 }
2777
Alan Cox880af9d2008-07-22 11:16:12 +01002778 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002779
2780 /* free private structure allocated for serial port *
2781 * stop reads and writes on all ports */
2782
2783 for (i = 0; i < serial->num_ports; ++i) {
2784 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302785 dbg ("mos7840_port %d = %p", i, mos7840_port);
2786 if (mos7840_port) {
2787 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2788 mos7840_port->zombie = 1;
2789 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2790 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002791 }
2792 }
2793
2794 dbg("%s", "Thank u :: ");
2795
2796}
2797
2798/****************************************************************************
2799 * mos7840_release
2800 * This function is called when the usb_serial structure is freed.
2801 ****************************************************************************/
2802
2803static void mos7840_release(struct usb_serial *serial)
2804{
2805 int i;
2806 struct moschip_port *mos7840_port;
2807 dbg("%s", " release :entering..........");
2808
2809 if (!serial) {
2810 dbg("%s", "Invalid Handler");
2811 return;
2812 }
2813
2814 /* check for the ports to be closed,close the ports and disconnect */
2815
2816 /* free private structure allocated for serial port *
2817 * stop reads and writes on all ports */
2818
2819 for (i = 0; i < serial->num_ports; ++i) {
2820 mos7840_port = mos7840_get_port_private(serial->port[i]);
2821 dbg("mos7840_port %d = %p", i, mos7840_port);
2822 if (mos7840_port) {
Donald0eafe4d2012-04-19 15:00:45 +08002823 if (mos7840_port->has_led) {
2824 /* Turn off LED */
2825 mos7840_set_led_sync(mos7840_port->port,
2826 MODEM_CONTROL_REGISTER, 0x0300);
2827
2828 del_timer_sync(&mos7840_port->led_timer1);
2829 del_timer_sync(&mos7840_port->led_timer2);
2830 }
Tony Cook37768ad2009-04-18 22:42:18 +09302831 kfree(mos7840_port->ctrl_buf);
2832 kfree(mos7840_port->dr);
2833 kfree(mos7840_port);
2834 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002835 }
2836
Tony Cook84fe6e72009-04-18 22:55:06 +09302837 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002838
2839}
2840
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002841static struct usb_driver io_driver = {
2842 .name = "mos7840",
2843 .probe = usb_serial_probe,
2844 .disconnect = usb_serial_disconnect,
2845 .id_table = moschip_id_table_combined,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002846};
2847
Paul B Schroeder3f542972006-08-31 19:41:47 -05002848static struct usb_serial_driver moschip7840_4port_device = {
2849 .driver = {
2850 .owner = THIS_MODULE,
2851 .name = "mos7840",
2852 },
2853 .description = DRIVER_DESC,
2854 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002855 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002856 .open = mos7840_open,
2857 .close = mos7840_close,
2858 .write = mos7840_write,
2859 .write_room = mos7840_write_room,
2860 .chars_in_buffer = mos7840_chars_in_buffer,
2861 .throttle = mos7840_throttle,
2862 .unthrottle = mos7840_unthrottle,
2863 .calc_num_ports = mos7840_calc_num_ports,
2864#ifdef MCSSerialProbe
2865 .probe = mos7840_serial_probe,
2866#endif
2867 .ioctl = mos7840_ioctl,
2868 .set_termios = mos7840_set_termios,
2869 .break_ctl = mos7840_break,
2870 .tiocmget = mos7840_tiocmget,
2871 .tiocmset = mos7840_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002872 .get_icount = mos7840_get_icount,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002873 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002874 .disconnect = mos7840_disconnect,
2875 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002876 .read_bulk_callback = mos7840_bulk_in_callback,
2877 .read_int_callback = mos7840_interrupt_callback,
2878};
2879
Alan Stern4d2a7af2012-02-23 14:57:09 -05002880static struct usb_serial_driver * const serial_drivers[] = {
2881 &moschip7840_4port_device, NULL
2882};
2883
Greg Kroah-Hartmane7414d92012-02-28 13:12:10 -08002884module_usb_serial_driver(io_driver, serial_drivers);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002885
Paul B Schroeder3f542972006-08-31 19:41:47 -05002886MODULE_DESCRIPTION(DRIVER_DESC);
2887MODULE_LICENSE("GPL");
2888
2889module_param(debug, bool, S_IRUGO | S_IWUSR);
2890MODULE_PARM_DESC(debug, "Debug enabled or not");