blob: c8542356898b4f6cdb81d1789a906a34d2b4152e [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
David Ludlow11e1abb2008-02-25 17:30:52 -0500117/* The native component can have its vendor/device id's overridden
118 * in vendor-specific implementations. Such devices can be handled
119 * by making a change here, in moschip_port_id_table, and in
120 * moschip_id_table_combined
121 */
Dave Ludlow870408c2010-09-01 12:33:30 -0400122#define USB_VENDOR_ID_BANDB 0x0856
123#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
124#define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
125#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
126#define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
127#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
128#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
129#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
130#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
131#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
132#define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
133#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
134#define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
135#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
Paul B Schroeder3f542972006-08-31 19:41:47 -0500136
Russell Lang9d498be2009-07-17 19:29:20 +1000137/* This driver also supports
138 * ATEN UC2324 device using Moschip MCS7840
139 * ATEN UC2322 device using Moschip MCS7820
140 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930141#define USB_VENDOR_ID_ATENINTL 0x0557
142#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000143#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930144
David Ludlow11e1abb2008-02-25 17:30:52 -0500145/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500146
147#define SERIAL_IIR_RLS 0x06
148#define SERIAL_IIR_MS 0x00
149
150/*
151 * Emulation of the bit mask on the LINE STATUS REGISTER.
152 */
153#define SERIAL_LSR_DR 0x0001
154#define SERIAL_LSR_OE 0x0002
155#define SERIAL_LSR_PE 0x0004
156#define SERIAL_LSR_FE 0x0008
157#define SERIAL_LSR_BI 0x0010
158
159#define MOS_MSR_DELTA_CTS 0x10
160#define MOS_MSR_DELTA_DSR 0x20
161#define MOS_MSR_DELTA_RI 0x40
162#define MOS_MSR_DELTA_CD 0x80
163
Alan Cox880af9d2008-07-22 11:16:12 +0100164/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500165#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
166#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
167#define LINE_CONTROL_REGISTER ((__u16)(0x03))
168#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
169#define LINE_STATUS_REGISTER ((__u16)(0x05))
170#define MODEM_STATUS_REGISTER ((__u16)(0x06))
171#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
172#define DIVISOR_LATCH_LSB ((__u16)(0x00))
173#define DIVISOR_LATCH_MSB ((__u16)(0x01))
174
175#define CLK_MULTI_REGISTER ((__u16)(0x02))
176#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
Donald Lee093ea2d2012-03-14 15:26:33 +0800177#define GPIO_REGISTER ((__u16)(0x07))
Paul B Schroeder3f542972006-08-31 19:41:47 -0500178
179#define SERIAL_LCR_DLAB ((__u16)(0x0080))
180
181/*
182 * URB POOL related defines
183 */
184#define NUM_URBS 16 /* URB Count */
185#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
186
187
Németh Márton7d40d7e2010-01-10 15:34:24 +0100188static const struct usb_device_id moschip_port_id_table[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
190 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930204 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000205 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500206 {} /* terminating entry */
207};
208
Tony Zelenoff0fd47a32012-06-05 17:58:04 +0400209static const struct usb_device_id moschip_id_table_combined[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500210 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
211 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500212 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400213 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500214 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400215 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500216 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
217 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
218 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
219 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500220 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400221 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500222 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Dave Ludlow870408c2010-09-01 12:33:30 -0400223 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800224 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930225 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000226 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500227 {} /* terminating entry */
228};
229
230MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
231
232/* This structure holds all of the local port information */
233
234struct moschip_port {
235 int port_num; /*Actual port number in the device(1,2,etc) */
236 struct urb *write_urb; /* write URB for this port */
237 struct urb *read_urb; /* read URB for this port */
238 __u8 shadowLCR; /* last LCR value received */
239 __u8 shadowMCR; /* last MCR value received */
240 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100241 char open_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500242 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
243 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
244 int delta_msr_cond;
245 struct async_icount icount;
246 struct usb_serial_port *port; /* loop back to the owner of this object */
247
Alan Cox880af9d2008-07-22 11:16:12 +0100248 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500249 __u8 SpRegOffset;
250 __u8 ControlRegOffset;
251 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100252 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500253 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100254 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500255 char *ctrl_buf;
256 int MsrLsr;
257
Oliver Neukum0de9a702007-03-16 20:28:28 +0100258 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500259 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100260 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800261 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500262};
263
264
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030265static bool debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500266
267/*
268 * mos7840_set_reg_sync
269 * To set the Control register by calling usb_fill_control_urb function
270 * by passing usb_sndctrlpipe function as parameter.
271 */
272
273static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
274 __u16 val)
275{
276 struct usb_device *dev = port->serial->dev;
277 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930278 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500279
280 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
281 MCS_WR_RTYPE, val, reg, NULL, 0,
282 MOS_WDR_TIMEOUT);
283}
284
285/*
286 * mos7840_get_reg_sync
287 * To set the Uart register by calling usb_fill_control_urb function by
288 * passing usb_rcvctrlpipe function as parameter.
289 */
290
291static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100292 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500293{
294 struct usb_device *dev = port->serial->dev;
295 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100296 u8 *buf;
297
298 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
299 if (!buf)
300 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500301
302 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100303 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500304 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100305 *val = buf[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930306 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100307
308 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500309 return ret;
310}
311
312/*
313 * mos7840_set_uart_reg
314 * To set the Uart register by calling usb_fill_control_urb function by
315 * passing usb_sndctrlpipe function as parameter.
316 */
317
318static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
319 __u16 val)
320{
321
322 struct usb_device *dev = port->serial->dev;
323 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100324 /* For the UART control registers, the application number need
325 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100326 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100327 val |= (((__u16) port->number -
328 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930329 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500330 } else {
331 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100332 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500333 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930334 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500335 val);
336 } else {
337 val |=
338 (((__u16) port->number -
339 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930340 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500341 val);
342 }
343 }
344 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
345 MCS_WR_RTYPE, val, reg, NULL, 0,
346 MOS_WDR_TIMEOUT);
347
348}
349
350/*
351 * mos7840_get_uart_reg
352 * To set the Control register by calling usb_fill_control_urb function
353 * by passing usb_rcvctrlpipe function as parameter.
354 */
355static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100356 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500357{
358 struct usb_device *dev = port->serial->dev;
359 int ret = 0;
360 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100361 u8 *buf;
362
363 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
364 if (!buf)
365 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500366
Tony Cook84fe6e72009-04-18 22:55:06 +0930367 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100368 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
369 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100370 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500371 Wval =
372 (((__u16) port->number - (__u16) (port->serial->minor)) +
373 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930374 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500375 } else {
376 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100377 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500378 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930379 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500380 Wval);
381 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100382 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500383 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930384 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500385 Wval);
386 }
387 }
388 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100389 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500390 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100391 *val = buf[0];
392
393 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500394 return ret;
395}
396
397static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
398{
399
Tony Cook84fe6e72009-04-18 22:55:06 +0930400 dbg("***************************************");
401 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
402 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
403 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
404 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500405
406}
407
408/************************************************************************/
409/************************************************************************/
410/* I N T E R F A C E F U N C T I O N S */
411/* I N T E R F A C E F U N C T I O N S */
412/************************************************************************/
413/************************************************************************/
414
415static inline void mos7840_set_port_private(struct usb_serial_port *port,
416 struct moschip_port *data)
417{
418 usb_set_serial_port_data(port, (void *)data);
419}
420
421static inline struct moschip_port *mos7840_get_port_private(struct
422 usb_serial_port
423 *port)
424{
425 return (struct moschip_port *)usb_get_serial_port_data(port);
426}
427
Oliver Neukum0de9a702007-03-16 20:28:28 +0100428static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500429{
430 struct moschip_port *mos7840_port;
431 struct async_icount *icount;
432 mos7840_port = port;
433 icount = &mos7840_port->icount;
434 if (new_msr &
435 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
436 MOS_MSR_DELTA_CD)) {
437 icount = &mos7840_port->icount;
438
439 /* update input line counters */
440 if (new_msr & MOS_MSR_DELTA_CTS) {
441 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100442 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500443 }
444 if (new_msr & MOS_MSR_DELTA_DSR) {
445 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100446 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500447 }
448 if (new_msr & MOS_MSR_DELTA_CD) {
449 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100450 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500451 }
452 if (new_msr & MOS_MSR_DELTA_RI) {
453 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100454 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500455 }
456 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500457}
458
Oliver Neukum0de9a702007-03-16 20:28:28 +0100459static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500460{
461 struct async_icount *icount;
462
Harvey Harrison441b62c2008-03-03 16:08:34 -0800463 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500464
465 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100466 /*
467 * Parity and Framing errors only count if they
468 * occur exclusive of a break being
469 * received.
470 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500471 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
472 }
473
474 /* update input line counters */
475 icount = &port->icount;
476 if (new_lsr & SERIAL_LSR_BI) {
477 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100478 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500479 }
480 if (new_lsr & SERIAL_LSR_OE) {
481 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100482 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500483 }
484 if (new_lsr & SERIAL_LSR_PE) {
485 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100486 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500487 }
488 if (new_lsr & SERIAL_LSR_FE) {
489 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100490 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500491 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500492}
493
494/************************************************************************/
495/************************************************************************/
496/* U S B C A L L B A C K F U N C T I O N S */
497/* U S B C A L L B A C K F U N C T I O N S */
498/************************************************************************/
499/************************************************************************/
500
David Howells7d12e782006-10-05 14:55:46 +0100501static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500502{
503 unsigned char *data;
504 struct moschip_port *mos7840_port;
505 __u8 regval = 0x0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700506 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500507
Ming Leicdc97792008-02-24 18:41:47 +0800508 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100509
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700510 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500511 case 0:
512 /* success */
513 break;
514 case -ECONNRESET:
515 case -ENOENT:
516 case -ESHUTDOWN:
517 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800518 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700519 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500520 return;
521 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800522 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700523 status);
Johan Hovold380c05e2012-10-25 18:56:32 +0200524 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500525 }
526
Tony Cook84fe6e72009-04-18 22:55:06 +0930527 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
528 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500529 mos7840_port->MsrLsr, mos7840_port->port_num);
530 data = urb->transfer_buffer;
531 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930532 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500533 if (mos7840_port->MsrLsr == 0)
534 mos7840_handle_new_msr(mos7840_port, regval);
535 else if (mos7840_port->MsrLsr == 1)
536 mos7840_handle_new_lsr(mos7840_port, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500537}
538
539static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100540 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500541{
542 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100543 struct usb_ctrlrequest *dr = mcs->dr;
544 unsigned char *buffer = mcs->ctrl_buf;
545 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500546
Paul B Schroeder3f542972006-08-31 19:41:47 -0500547 dr->bRequestType = MCS_RD_RTYPE;
548 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100549 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500550 dr->wIndex = cpu_to_le16(reg);
551 dr->wLength = cpu_to_le16(2);
552
553 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
554 (unsigned char *)dr, buffer, 2,
555 mos7840_control_callback, mcs);
556 mcs->control_urb->transfer_buffer_length = 2;
557 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
558 return ret;
559}
560
561/*****************************************************************************
562 * mos7840_interrupt_callback
563 * this is the callback function for when we have received data on the
564 * interrupt endpoint.
565 *****************************************************************************/
566
David Howells7d12e782006-10-05 14:55:46 +0100567static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500568{
569 int result;
570 int length;
571 struct moschip_port *mos7840_port;
572 struct usb_serial *serial;
573 __u16 Data;
574 unsigned char *data;
575 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100576 int i, rv = 0;
577 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700578 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500579
Tony Cook84fe6e72009-04-18 22:55:06 +0930580 dbg("%s", " : Entering");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500581
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700582 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500583 case 0:
584 /* success */
585 break;
586 case -ECONNRESET:
587 case -ENOENT:
588 case -ESHUTDOWN:
589 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800590 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700591 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500592 return;
593 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800594 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700595 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500596 goto exit;
597 }
598
599 length = urb->actual_length;
600 data = urb->transfer_buffer;
601
Ming Leicdc97792008-02-24 18:41:47 +0800602 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500603
604 /* Moschip get 5 bytes
605 * Byte 1 IIR Port 1 (port.number is 0)
606 * Byte 2 IIR Port 2 (port.number is 1)
607 * Byte 3 IIR Port 3 (port.number is 2)
608 * Byte 4 IIR Port 4 (port.number is 3)
609 * Byte 5 FIFO status for both */
610
611 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930612 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500613 return;
614 }
615
616 sp[0] = (__u8) data[0];
617 sp[1] = (__u8) data[1];
618 sp[2] = (__u8) data[2];
619 sp[3] = (__u8) data[3];
620 st = (__u8) data[4];
621
622 for (i = 0; i < serial->num_ports; i++) {
623 mos7840_port = mos7840_get_port_private(serial->port[i]);
624 wval =
625 (((__u16) serial->port[i]->number -
626 (__u16) (serial->minor)) + 1) << 8;
627 if (mos7840_port->open) {
628 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930629 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500630 } else {
631 switch (sp[i] & 0x0f) {
632 case SERIAL_IIR_RLS:
633 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930634 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500635 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100636 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500637 break;
638 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930639 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500640 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100641 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500642 break;
643 }
Johan Hovold8d649dd2012-10-25 18:56:33 +0200644 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500645 }
646 }
647 }
Alan Cox880af9d2008-07-22 11:16:12 +0100648 if (!(rv < 0))
649 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100650 return;
651exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500652 result = usb_submit_urb(urb, GFP_ATOMIC);
653 if (result) {
654 dev_err(&urb->dev->dev,
655 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800656 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500657 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500658}
659
660static int mos7840_port_paranoia_check(struct usb_serial_port *port,
661 const char *function)
662{
663 if (!port) {
664 dbg("%s - port == NULL", function);
665 return -1;
666 }
667 if (!port->serial) {
668 dbg("%s - port->serial == NULL", function);
669 return -1;
670 }
671
672 return 0;
673}
674
675/* Inline functions to check the sanity of a pointer that is passed to us */
676static int mos7840_serial_paranoia_check(struct usb_serial *serial,
677 const char *function)
678{
679 if (!serial) {
680 dbg("%s - serial == NULL", function);
681 return -1;
682 }
683 if (!serial->type) {
684 dbg("%s - serial->type == NULL!", function);
685 return -1;
686 }
687
688 return 0;
689}
690
691static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
692 const char *function)
693{
694 /* if no port was specified, or it fails a paranoia check */
695 if (!port ||
696 mos7840_port_paranoia_check(port, function) ||
697 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100698 /* then say that we don't have a valid usb_serial thing,
699 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500700 return NULL;
701 }
702
703 return port->serial;
704}
705
706/*****************************************************************************
707 * mos7840_bulk_in_callback
708 * this is the callback function for when we have received data on the
709 * bulk in endpoint.
710 *****************************************************************************/
711
David Howells7d12e782006-10-05 14:55:46 +0100712static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500713{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700714 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500715 unsigned char *data;
716 struct usb_serial *serial;
717 struct usb_serial_port *port;
718 struct moschip_port *mos7840_port;
719 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700720 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500721
Ming Leicdc97792008-02-24 18:41:47 +0800722 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500723 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930724 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800725 return;
726 }
727
728 if (status) {
729 dbg("nonzero read bulk status received: %d", status);
730 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500731 return;
732 }
733
734 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800735 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930736 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800737 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500738 return;
739 }
740
Harvey Harrison441b62c2008-03-03 16:08:34 -0800741 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500742 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930743 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800744 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500745 return;
746 }
747
Tony Cook84fe6e72009-04-18 22:55:06 +0930748 dbg("%s", "Entering... ");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500749
750 data = urb->transfer_buffer;
751
Tony Cook84fe6e72009-04-18 22:55:06 +0930752 dbg("%s", "Entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500753
754 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100755 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500756 if (tty) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500757 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930758 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500759 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100760 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500761 }
762 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100763 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930764 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500765 mos7840_port->icount.rx);
766 }
767
768 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930769 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800770 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500771 return;
772 }
773
Paul B Schroeder3f542972006-08-31 19:41:47 -0500774
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800775 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700776 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100777
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700778 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800779 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
780 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500781 }
782}
783
784/*****************************************************************************
785 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100786 * this is the callback function for when we have finished sending
787 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500788 *****************************************************************************/
789
David Howells7d12e782006-10-05 14:55:46 +0100790static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500791{
792 struct moschip_port *mos7840_port;
793 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700794 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100795 int i;
796
Ming Leicdc97792008-02-24 18:41:47 +0800797 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100798 spin_lock(&mos7840_port->pool_lock);
799 for (i = 0; i < NUM_URBS; i++) {
800 if (urb == mos7840_port->write_urb_pool[i]) {
801 mos7840_port->busy[i] = 0;
802 break;
803 }
804 }
805 spin_unlock(&mos7840_port->pool_lock);
806
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700807 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930808 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500809 return;
810 }
811
Harvey Harrison441b62c2008-03-03 16:08:34 -0800812 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930813 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500814 return;
815 }
816
Tony Cook84fe6e72009-04-18 22:55:06 +0930817 dbg("%s", "Entering .........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500818
Alan Cox4a90f092008-10-13 10:39:46 +0100819 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800820 if (tty && mos7840_port->open)
821 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100822 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500823
824}
825
826/************************************************************************/
827/* 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 */
828/************************************************************************/
829#ifdef MCSSerialProbe
830static int mos7840_serial_probe(struct usb_serial *serial,
831 const struct usb_device_id *id)
832{
833
834 /*need to implement the mode_reg reading and updating\
835 structures usb_serial_ device_type\
836 (i.e num_ports, num_bulkin,bulkout etc) */
837 /* Also we can update the changes attach */
838 return 1;
839}
840#endif
841
842/*****************************************************************************
843 * mos7840_open
844 * this function is called by the tty driver when a port is opened
845 * If successful, we return 0
846 * Otherwise we return a negative error number.
847 *****************************************************************************/
848
Alan Coxa509a7e2009-09-19 13:13:26 -0700849static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500850{
851 int response;
852 int j;
853 struct usb_serial *serial;
854 struct urb *urb;
855 __u16 Data;
856 int status;
857 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100858 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500859
Tony Cook84fe6e72009-04-18 22:55:06 +0930860 dbg ("%s enter", __func__);
861
Harvey Harrison441b62c2008-03-03 16:08:34 -0800862 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930863 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500864 return -ENODEV;
865 }
866
Paul B Schroeder3f542972006-08-31 19:41:47 -0500867 serial = port->serial;
868
Harvey Harrison441b62c2008-03-03 16:08:34 -0800869 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930870 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500871 return -ENODEV;
872 }
873
874 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100875 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500876
Oliver Neukum0de9a702007-03-16 20:28:28 +0100877 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500878 return -ENODEV;
879
880 usb_clear_halt(serial->dev, port->write_urb->pipe);
881 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100882 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500883
884 /* Initialising the write urb pool */
885 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100886 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500887 mos7840_port->write_urb_pool[j] = urb;
888
889 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700890 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500891 continue;
892 }
893
Alan Cox880af9d2008-07-22 11:16:12 +0100894 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
895 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500896 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100897 usb_free_urb(urb);
898 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700899 dev_err(&port->dev,
900 "%s-out of memory for urb buffers.\n",
901 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500902 continue;
903 }
904 }
905
906/*****************************************************************************
907 * Initialize MCS7840 -- Write Init values to corresponding Registers
908 *
909 * Register Index
910 * 1 : IER
911 * 2 : FCR
912 * 3 : LCR
913 * 4 : MCR
914 *
915 * 0x08 : SP1/2 Control Reg
916 *****************************************************************************/
917
Alan Cox880af9d2008-07-22 11:16:12 +0100918 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500919
Paul B Schroeder3f542972006-08-31 19:41:47 -0500920 Data = 0x0;
921 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
922 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930923 dbg("Reading Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500924 return -1;
925 }
926 Data |= 0x80;
927 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
928 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930929 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500930 return -1;
931 }
932
933 Data &= ~0x80;
934 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
935 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930936 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500937 return -1;
938 }
Alan Cox880af9d2008-07-22 11:16:12 +0100939 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500940
Paul B Schroeder3f542972006-08-31 19:41:47 -0500941 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100942 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
943 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500944 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930945 dbg("Reading Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500946 return -1;
947 }
Alan Cox880af9d2008-07-22 11:16:12 +0100948 Data |= 0x08; /* Driver done bit */
949 Data |= 0x20; /* rx_disable */
950 status = mos7840_set_reg_sync(port,
951 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500952 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930953 dbg("writing Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500954 return -1;
955 }
Alan Cox880af9d2008-07-22 11:16:12 +0100956 /* do register settings here */
957 /* Set all regs to the device default values. */
958 /***********************************
959 * First Disable all interrupts.
960 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500961 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500962 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
963 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930964 dbg("disabling interrupts failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500965 return -1;
966 }
Alan Cox880af9d2008-07-22 11:16:12 +0100967 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500968 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500969 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
970 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930971 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500972 return -1;
973 }
974
975 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500976 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
977 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930978 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500979 return -1;
980 }
981
982 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500983 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
984 mos7840_port->shadowLCR = Data;
985
986 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500987 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
988 mos7840_port->shadowMCR = Data;
989
990 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500991 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
992 mos7840_port->shadowLCR = Data;
993
Alan Cox880af9d2008-07-22 11:16:12 +0100994 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500995 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
996
997 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500998 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
999
1000 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001001 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1002
1003 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001004 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1005
1006 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001007 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1008 mos7840_port->shadowLCR = Data;
1009
Alan Cox880af9d2008-07-22 11:16:12 +01001010 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001011 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001012 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1013
1014 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001015 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1016
1017 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001018 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001019 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001020 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001021 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1022
Alan Cox880af9d2008-07-22 11:16:12 +01001023 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001024 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001025 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1026 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001027 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001028 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1029 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001030
Alan Cox880af9d2008-07-22 11:16:12 +01001031 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001032 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001033 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1034 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001035 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001036 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1037 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001038
Alan Cox880af9d2008-07-22 11:16:12 +01001039 /* Check to see if we've set up our endpoint info yet *
1040 * (can't set it up in mos7840_startup as the structures *
1041 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001042 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001043 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001044 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001045 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001046 serial->dev,
1047 usb_rcvintpipe(serial->dev,
1048 serial->port[0]->interrupt_in_endpointAddress),
1049 serial->port[0]->interrupt_in_buffer,
1050 serial->port[0]->interrupt_in_urb->
1051 transfer_buffer_length,
1052 mos7840_interrupt_callback,
1053 serial,
1054 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001055
1056 /* start interrupt read for mos7840 *
1057 * will continue as long as mos7840 is connected */
1058
1059 response =
1060 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1061 GFP_KERNEL);
1062 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001063 dev_err(&port->dev, "%s - Error %d submitting "
1064 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001065 }
1066
1067 }
1068
1069 }
1070
1071 /* see if we've set up our endpoint info yet *
1072 * (can't set it up in mos7840_startup as the *
1073 * structures were not set up at that time.) */
1074
Tony Cook84fe6e72009-04-18 22:55:06 +09301075 dbg("port number is %d", port->number);
1076 dbg("serial number is %d", port->serial->minor);
1077 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1078 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1079 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1080 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001081 mos7840_port->read_urb = port->read_urb;
1082
1083 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001084 if ((serial->num_ports == 2)
1085 && ((((__u16)port->number -
1086 (__u16)(port->serial->minor)) % 2) != 0)) {
1087 usb_fill_bulk_urb(mos7840_port->read_urb,
1088 serial->dev,
1089 usb_rcvbulkpipe(serial->dev,
1090 (port->bulk_in_endpointAddress) + 2),
1091 port->bulk_in_buffer,
1092 mos7840_port->read_urb->transfer_buffer_length,
1093 mos7840_bulk_in_callback, mos7840_port);
1094 } else {
1095 usb_fill_bulk_urb(mos7840_port->read_urb,
1096 serial->dev,
1097 usb_rcvbulkpipe(serial->dev,
1098 port->bulk_in_endpointAddress),
1099 port->bulk_in_buffer,
1100 mos7840_port->read_urb->transfer_buffer_length,
1101 mos7840_bulk_in_callback, mos7840_port);
1102 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001103
Tony Cook84fe6e72009-04-18 22:55:06 +09301104 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001105 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001106 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001107 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1108 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001109 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1110 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001111 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001112 }
1113
1114 /* initialize our wait queues */
1115 init_waitqueue_head(&mos7840_port->wait_chase);
1116 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1117
1118 /* initialize our icount structure */
1119 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1120
1121 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001122 /* Must set to enable ints! */
1123 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001124 /* send a open port command */
1125 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001126 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001127 mos7840_port->icount.tx = 0;
1128 mos7840_port->icount.rx = 0;
1129
Tony Cook84fe6e72009-04-18 22:55:06 +09301130 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001131 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001132
Tony Cook84fe6e72009-04-18 22:55:06 +09301133 dbg ("%s leave", __func__);
1134
Paul B Schroeder3f542972006-08-31 19:41:47 -05001135 return 0;
1136
1137}
1138
1139/*****************************************************************************
1140 * mos7840_chars_in_buffer
1141 * this function is called by the tty driver when it wants to know how many
1142 * bytes of data we currently have outstanding in the port (data that has
1143 * been written, but hasn't made it out the port yet)
1144 * If successful, we return the number of bytes left to be written in the
1145 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001146 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001147 *****************************************************************************/
1148
Alan Cox95da3102008-07-22 11:09:07 +01001149static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001150{
Alan Cox95da3102008-07-22 11:09:07 +01001151 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001152 int i;
1153 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001154 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001155 struct moschip_port *mos7840_port;
1156
Tony Cook84fe6e72009-04-18 22:55:06 +09301157 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001158
Harvey Harrison441b62c2008-03-03 16:08:34 -08001159 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301160 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001161 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001162 }
1163
1164 mos7840_port = mos7840_get_port_private(port);
1165 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301166 dbg("%s", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001167 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001168 }
1169
Alan Cox880af9d2008-07-22 11:16:12 +01001170 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell4893ce52012-07-24 14:15:13 -05001171 for (i = 0; i < NUM_URBS; ++i) {
1172 if (mos7840_port->busy[i]) {
1173 struct urb *urb = mos7840_port->write_urb_pool[i];
1174 chars += urb->transfer_buffer_length;
1175 }
1176 }
Alan Cox880af9d2008-07-22 11:16:12 +01001177 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001178 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001179 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001180
1181}
1182
Paul B Schroeder3f542972006-08-31 19:41:47 -05001183/*****************************************************************************
1184 * mos7840_close
1185 * this function is called by the tty driver when a port is closed
1186 *****************************************************************************/
1187
Alan Cox335f8512009-06-11 12:26:29 +01001188static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001189{
1190 struct usb_serial *serial;
1191 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001192 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001193 int j;
1194 __u16 Data;
1195
Tony Cook84fe6e72009-04-18 22:55:06 +09301196 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001197
Harvey Harrison441b62c2008-03-03 16:08:34 -08001198 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301199 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001200 return;
1201 }
1202
Harvey Harrison441b62c2008-03-03 16:08:34 -08001203 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001204 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301205 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001206 return;
1207 }
1208
1209 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001210 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001211
Oliver Neukum0de9a702007-03-16 20:28:28 +01001212 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001213 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001214
1215 for (j = 0; j < NUM_URBS; ++j)
1216 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1217
1218 /* Freeing Write URBs */
1219 for (j = 0; j < NUM_URBS; ++j) {
1220 if (mos7840_port->write_urb_pool[j]) {
1221 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1222 kfree(mos7840_port->write_urb_pool[j]->
1223 transfer_buffer);
1224
1225 usb_free_urb(mos7840_port->write_urb_pool[j]);
1226 }
1227 }
1228
Paul B Schroeder3f542972006-08-31 19:41:47 -05001229 /* While closing port, shutdown all bulk read, write *
1230 * and interrupt read if they exists */
1231 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001232 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301233 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001234 usb_kill_urb(mos7840_port->write_urb);
1235 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001236 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301237 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001238 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001239 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001240 }
1241 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301242 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001243 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001244 }
1245 }
Alan Cox880af9d2008-07-22 11:16:12 +01001246/* if(mos7840_port->ctrl_buf != NULL) */
1247/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001248 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301249 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001250 port0->open_ports, port->number);
1251 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001252 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301253 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001254 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001255 }
1256 }
1257
1258 if (mos7840_port->write_urb) {
1259 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001260 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001261 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001262 usb_free_urb(mos7840_port->write_urb);
1263 }
1264
1265 Data = 0x0;
1266 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1267
1268 Data = 0x00;
1269 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1270
1271 mos7840_port->open = 0;
1272
Tony Cook84fe6e72009-04-18 22:55:06 +09301273 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001274}
1275
1276/************************************************************************
1277 *
1278 * mos7840_block_until_chase_response
1279 *
1280 * This function will block the close until one of the following:
1281 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001282 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001283 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1284 *
1285 ************************************************************************/
1286
Alan Cox95da3102008-07-22 11:09:07 +01001287static void mos7840_block_until_chase_response(struct tty_struct *tty,
1288 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001289{
1290 int timeout = 1 * HZ;
1291 int wait = 10;
1292 int count;
1293
1294 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001295 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001296
1297 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001298 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001299 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001300
1301 /* Block the thread for a while */
1302 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1303 timeout);
1304 /* No activity.. count down section */
1305 wait--;
1306 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001307 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001308 return;
1309 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001310 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001311 wait = 10;
1312 }
1313 }
1314
1315}
1316
1317/*****************************************************************************
1318 * mos7840_break
1319 * this function sends a break to the port
1320 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001321static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001322{
Alan Cox95da3102008-07-22 11:09:07 +01001323 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001324 unsigned char data;
1325 struct usb_serial *serial;
1326 struct moschip_port *mos7840_port;
1327
Tony Cook84fe6e72009-04-18 22:55:06 +09301328 dbg("%s", "Entering ...........");
1329 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001330
Harvey Harrison441b62c2008-03-03 16:08:34 -08001331 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301332 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001333 return;
1334 }
1335
Harvey Harrison441b62c2008-03-03 16:08:34 -08001336 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001337 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301338 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001339 return;
1340 }
1341
1342 mos7840_port = mos7840_get_port_private(port);
1343
Alan Cox880af9d2008-07-22 11:16:12 +01001344 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001345 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001346
Alan Cox95da3102008-07-22 11:09:07 +01001347 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001348 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001349 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001350
Alan Cox95da3102008-07-22 11:09:07 +01001351 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001352 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001353 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001354 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001355
Alan Cox6b447f042009-01-02 13:48:56 +00001356 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001357 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301358 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001359 mos7840_port->shadowLCR);
1360 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1361 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001362}
1363
1364/*****************************************************************************
1365 * mos7840_write_room
1366 * this function is called by the tty driver when it wants to know how many
1367 * bytes of data we can accept for a specific port.
1368 * If successful, we return the amount of room that we have for this port
1369 * Otherwise we return a negative error number.
1370 *****************************************************************************/
1371
Alan Cox95da3102008-07-22 11:09:07 +01001372static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001373{
Alan Cox95da3102008-07-22 11:09:07 +01001374 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001375 int i;
1376 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001377 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001378 struct moschip_port *mos7840_port;
1379
Tony Cook84fe6e72009-04-18 22:55:06 +09301380 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001381
Harvey Harrison441b62c2008-03-03 16:08:34 -08001382 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301383 dbg("%s", "Invalid port");
1384 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001385 return -1;
1386 }
1387
1388 mos7840_port = mos7840_get_port_private(port);
1389 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301390 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001391 return -1;
1392 }
1393
Oliver Neukum0de9a702007-03-16 20:28:28 +01001394 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001395 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001396 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001397 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001398 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001399 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001400
Oliver Neukum0de9a702007-03-16 20:28:28 +01001401 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001402 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001403 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001404
1405}
1406
1407/*****************************************************************************
1408 * mos7840_write
1409 * this function is called by the tty driver when data should be written to
1410 * the port.
1411 * If successful, we return the number of bytes written, otherwise we
1412 * return a negative error number.
1413 *****************************************************************************/
1414
Alan Cox95da3102008-07-22 11:09:07 +01001415static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001416 const unsigned char *data, int count)
1417{
1418 int status;
1419 int i;
1420 int bytes_sent = 0;
1421 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001422 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001423
1424 struct moschip_port *mos7840_port;
1425 struct usb_serial *serial;
1426 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001427 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001428 const unsigned char *current_position = data;
1429 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301430 dbg("%s", "entering ...........");
1431 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001432 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001433
1434#ifdef NOTMOS7840
1435 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001436 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1437 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301438 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1439 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001440 mos7840_port->shadowLCR);
1441
Alan Cox880af9d2008-07-22 11:16:12 +01001442 /* Data = 0x03; */
1443 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1444 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001445
Alan Cox880af9d2008-07-22 11:16:12 +01001446 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001447 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1448
Alan Cox880af9d2008-07-22 11:16:12 +01001449 /* Data = 0x0c; */
1450 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001451 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001452 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301453 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001454
1455 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001456 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301457 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001458
1459 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301460 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001461 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001462 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1463#endif
1464
Harvey Harrison441b62c2008-03-03 16:08:34 -08001465 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301466 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001467 return -1;
1468 }
1469
1470 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001471 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301472 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001473 return -1;
1474 }
1475
1476 mos7840_port = mos7840_get_port_private(port);
1477 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301478 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001479 return -1;
1480 }
1481
1482 /* try to find a free urb in the list */
1483 urb = NULL;
1484
Oliver Neukum0de9a702007-03-16 20:28:28 +01001485 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001486 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001487 if (!mos7840_port->busy[i]) {
1488 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001489 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301490 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001491 break;
1492 }
1493 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001494 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001495
1496 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001497 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001498 goto exit;
1499 }
1500
1501 if (urb->transfer_buffer == NULL) {
1502 urb->transfer_buffer =
1503 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1504
1505 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001506 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001507 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001508 goto exit;
1509 }
1510 }
1511 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1512
Al Viro97c49652006-10-09 20:29:03 +01001513 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001514
1515 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001516 if ((serial->num_ports == 2)
1517 && ((((__u16)port->number -
1518 (__u16)(port->serial->minor)) % 2) != 0)) {
1519 usb_fill_bulk_urb(urb,
1520 serial->dev,
1521 usb_sndbulkpipe(serial->dev,
1522 (port->bulk_out_endpointAddress) + 2),
1523 urb->transfer_buffer,
1524 transfer_size,
1525 mos7840_bulk_out_data_callback, mos7840_port);
1526 } else {
1527 usb_fill_bulk_urb(urb,
1528 serial->dev,
1529 usb_sndbulkpipe(serial->dev,
1530 port->bulk_out_endpointAddress),
1531 urb->transfer_buffer,
1532 transfer_size,
1533 mos7840_bulk_out_data_callback, mos7840_port);
1534 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001535
1536 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301537 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001538
1539 /* send it down the pipe */
1540 status = usb_submit_urb(urb, GFP_ATOMIC);
1541
1542 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001543 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001544 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001545 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001546 bytes_sent = status;
1547 goto exit;
1548 }
1549 bytes_sent = transfer_size;
1550 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001551 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301552 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001553exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001554 return bytes_sent;
1555
1556}
1557
1558/*****************************************************************************
1559 * mos7840_throttle
1560 * this function is called by the tty driver when it wants to stop the data
1561 * being read from the port.
1562 *****************************************************************************/
1563
Alan Cox95da3102008-07-22 11:09:07 +01001564static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001565{
Alan Cox95da3102008-07-22 11:09:07 +01001566 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001567 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001568 int status;
1569
Harvey Harrison441b62c2008-03-03 16:08:34 -08001570 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301571 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001572 return;
1573 }
1574
Tony Cook84fe6e72009-04-18 22:55:06 +09301575 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001576
1577 mos7840_port = mos7840_get_port_private(port);
1578
1579 if (mos7840_port == NULL)
1580 return;
1581
1582 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301583 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001584 return;
1585 }
1586
Tony Cook84fe6e72009-04-18 22:55:06 +09301587 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001588
Paul B Schroeder3f542972006-08-31 19:41:47 -05001589 /* if we are implementing XON/XOFF, send the stop character */
1590 if (I_IXOFF(tty)) {
1591 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001592 status = mos7840_write(tty, port, &stop_char, 1);
1593 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001594 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001595 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001596 /* if we are implementing RTS/CTS, toggle that line */
1597 if (tty->termios->c_cflag & CRTSCTS) {
1598 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001599 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001600 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001601 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001602 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001603 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001604}
1605
1606/*****************************************************************************
1607 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001608 * this function is called by the tty driver when it wants to resume
1609 * the data being read from the port (called after mos7840_throttle is
1610 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001611 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001612static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001613{
Alan Cox95da3102008-07-22 11:09:07 +01001614 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001615 int status;
1616 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1617
Harvey Harrison441b62c2008-03-03 16:08:34 -08001618 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301619 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001620 return;
1621 }
1622
1623 if (mos7840_port == NULL)
1624 return;
1625
1626 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001627 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001628 return;
1629 }
1630
Tony Cook84fe6e72009-04-18 22:55:06 +09301631 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001632
Paul B Schroeder3f542972006-08-31 19:41:47 -05001633 /* if we are implementing XON/XOFF, send the start character */
1634 if (I_IXOFF(tty)) {
1635 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001636 status = mos7840_write(tty, port, &start_char, 1);
1637 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001638 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001639 }
1640
1641 /* if we are implementing RTS/CTS, toggle that line */
1642 if (tty->termios->c_cflag & CRTSCTS) {
1643 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001644 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001645 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001646 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001647 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001648 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001649}
1650
Alan Cox60b33c12011-02-14 16:26:14 +00001651static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001652{
Alan Cox95da3102008-07-22 11:09:07 +01001653 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001654 struct moschip_port *mos7840_port;
1655 unsigned int result;
1656 __u16 msr;
1657 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001658 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001659 mos7840_port = mos7840_get_port_private(port);
1660
Harvey Harrison441b62c2008-03-03 16:08:34 -08001661 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001662
1663 if (mos7840_port == NULL)
1664 return -ENODEV;
1665
1666 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1667 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1668 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1669 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1670 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1671 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1672 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1673 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1674 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1675
Harvey Harrison441b62c2008-03-03 16:08:34 -08001676 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001677
1678 return result;
1679}
1680
Alan Cox20b9d172011-02-14 16:26:50 +00001681static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001682 unsigned int set, unsigned int clear)
1683{
Alan Cox95da3102008-07-22 11:09:07 +01001684 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001685 struct moschip_port *mos7840_port;
1686 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001687 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001688
Harvey Harrison441b62c2008-03-03 16:08:34 -08001689 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001690
1691 mos7840_port = mos7840_get_port_private(port);
1692
1693 if (mos7840_port == NULL)
1694 return -ENODEV;
1695
Alan Coxe2984492008-02-20 20:51:45 +00001696 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001697 mcr = mos7840_port->shadowMCR;
1698 if (clear & TIOCM_RTS)
1699 mcr &= ~MCR_RTS;
1700 if (clear & TIOCM_DTR)
1701 mcr &= ~MCR_DTR;
1702 if (clear & TIOCM_LOOP)
1703 mcr &= ~MCR_LOOPBACK;
1704
1705 if (set & TIOCM_RTS)
1706 mcr |= MCR_RTS;
1707 if (set & TIOCM_DTR)
1708 mcr |= MCR_DTR;
1709 if (set & TIOCM_LOOP)
1710 mcr |= MCR_LOOPBACK;
1711
1712 mos7840_port->shadowMCR = mcr;
1713
Paul B Schroeder3f542972006-08-31 19:41:47 -05001714 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1715 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301716 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001717 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001718 }
1719
1720 return 0;
1721}
1722
1723/*****************************************************************************
1724 * mos7840_calc_baud_rate_divisor
1725 * this function calculates the proper baud rate divisor for the specified
1726 * baud rate.
1727 *****************************************************************************/
1728static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001729 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001730{
1731
Harvey Harrison441b62c2008-03-03 16:08:34 -08001732 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001733
1734 if (baudRate <= 115200) {
1735 *divisor = 115200 / baudRate;
1736 *clk_sel_val = 0x0;
1737 }
1738 if ((baudRate > 115200) && (baudRate <= 230400)) {
1739 *divisor = 230400 / baudRate;
1740 *clk_sel_val = 0x10;
1741 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1742 *divisor = 403200 / baudRate;
1743 *clk_sel_val = 0x20;
1744 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1745 *divisor = 460800 / baudRate;
1746 *clk_sel_val = 0x30;
1747 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1748 *divisor = 806400 / baudRate;
1749 *clk_sel_val = 0x40;
1750 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1751 *divisor = 921600 / baudRate;
1752 *clk_sel_val = 0x50;
1753 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1754 *divisor = 1572864 / baudRate;
1755 *clk_sel_val = 0x60;
1756 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1757 *divisor = 3145728 / baudRate;
1758 *clk_sel_val = 0x70;
1759 }
1760 return 0;
1761
1762#ifdef NOTMCS7840
1763
1764 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1765 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1766 *divisor = mos7840_divisor_table[i].Divisor;
1767 return 0;
1768 }
1769 }
1770
1771 /* After trying for all the standard baud rates *
1772 * Try calculating the divisor for this baud rate */
1773
1774 if (baudrate > 75 && baudrate < 230400) {
1775 /* get the divisor */
1776 custom = (__u16) (230400L / baudrate);
1777
1778 /* Check for round off */
1779 round1 = (__u16) (2304000L / baudrate);
1780 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001781 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001782 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001783 *divisor = custom;
1784
Tony Cook84fe6e72009-04-18 22:55:06 +09301785 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001786 return 0;
1787 }
1788
Tony Cook84fe6e72009-04-18 22:55:06 +09301789 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001790 return -1;
1791#endif
1792}
1793
1794/*****************************************************************************
1795 * mos7840_send_cmd_write_baud_rate
1796 * this function sends the proper command to change the baud rate of the
1797 * specified port.
1798 *****************************************************************************/
1799
1800static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1801 int baudRate)
1802{
1803 int divisor = 0;
1804 int status;
1805 __u16 Data;
1806 unsigned char number;
1807 __u16 clk_sel_val;
1808 struct usb_serial_port *port;
1809
1810 if (mos7840_port == NULL)
1811 return -1;
1812
1813 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001814 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301815 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001816 return -1;
1817 }
1818
Harvey Harrison441b62c2008-03-03 16:08:34 -08001819 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301820 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001821 return -1;
1822 }
1823
Tony Cook84fe6e72009-04-18 22:55:06 +09301824 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001825
1826 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1827
Harvey Harrison441b62c2008-03-03 16:08:34 -08001828 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001829 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001830 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001831 if (baudRate > 115200) {
1832#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001833 /* NOTE: need to see the pther register to modify */
1834 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001835 Data = 0x2b;
1836 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001837 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1838 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001839 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301840 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001841 return -1;
1842 }
1843#endif
1844
1845 } else {
1846#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001847 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001848 Data = 0xb;
1849 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001850 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1851 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001852 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301853 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001854 return -1;
1855 }
1856#endif
1857
1858 }
1859
Alan Cox880af9d2008-07-22 11:16:12 +01001860 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001861 clk_sel_val = 0x0;
1862 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001863 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001864 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001865 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1866 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001867 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301868 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001869 return -1;
1870 }
1871 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001872 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1873 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001874 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301875 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001876 return -1;
1877 }
1878 /* Calculate the Divisor */
1879
1880 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001881 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001882 return status;
1883 }
1884 /* Enable access to divisor latch */
1885 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1886 mos7840_port->shadowLCR = Data;
1887 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1888
1889 /* Write the divisor */
1890 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301891 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001892 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1893
1894 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301895 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001896 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1897
1898 /* Disable access to divisor latch */
1899 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1900 mos7840_port->shadowLCR = Data;
1901 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1902
1903 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001904 return status;
1905}
1906
1907/*****************************************************************************
1908 * mos7840_change_port_settings
1909 * This routine is called to set the UART on the device to match
1910 * the specified new settings.
1911 *****************************************************************************/
1912
Alan Cox95da3102008-07-22 11:09:07 +01001913static void mos7840_change_port_settings(struct tty_struct *tty,
1914 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001915{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001916 int baud;
1917 unsigned cflag;
1918 unsigned iflag;
1919 __u8 lData;
1920 __u8 lParity;
1921 __u8 lStop;
1922 int status;
1923 __u16 Data;
1924 struct usb_serial_port *port;
1925 struct usb_serial *serial;
1926
1927 if (mos7840_port == NULL)
1928 return;
1929
1930 port = (struct usb_serial_port *)mos7840_port->port;
1931
Harvey Harrison441b62c2008-03-03 16:08:34 -08001932 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301933 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001934 return;
1935 }
1936
Harvey Harrison441b62c2008-03-03 16:08:34 -08001937 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301938 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001939 return;
1940 }
1941
1942 serial = port->serial;
1943
Harvey Harrison441b62c2008-03-03 16:08:34 -08001944 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001945
1946 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001947 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001948 return;
1949 }
1950
Tony Cook84fe6e72009-04-18 22:55:06 +09301951 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001952
1953 lData = LCR_BITS_8;
1954 lStop = LCR_STOP_1;
1955 lParity = LCR_PAR_NONE;
1956
1957 cflag = tty->termios->c_cflag;
1958 iflag = tty->termios->c_iflag;
1959
1960 /* Change the number of bits */
1961 if (cflag & CSIZE) {
1962 switch (cflag & CSIZE) {
1963 case CS5:
1964 lData = LCR_BITS_5;
1965 break;
1966
1967 case CS6:
1968 lData = LCR_BITS_6;
1969 break;
1970
1971 case CS7:
1972 lData = LCR_BITS_7;
1973 break;
1974 default:
1975 case CS8:
1976 lData = LCR_BITS_8;
1977 break;
1978 }
1979 }
1980 /* Change the Parity bit */
1981 if (cflag & PARENB) {
1982 if (cflag & PARODD) {
1983 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001984 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001985 } else {
1986 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001987 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001988 }
1989
1990 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001991 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001992 }
1993
Alan Cox880af9d2008-07-22 11:16:12 +01001994 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001995 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001996
1997 /* Change the Stop bit */
1998 if (cflag & CSTOPB) {
1999 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002000 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002001 } else {
2002 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002003 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002004 }
2005
2006 /* Update the LCR with the correct value */
2007 mos7840_port->shadowLCR &=
2008 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2009 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2010
Tony Cook84fe6e72009-04-18 22:55:06 +09302011 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002012 mos7840_port->shadowLCR);
2013 /* Disable Interrupts */
2014 Data = 0x00;
2015 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2016
2017 Data = 0x00;
2018 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2019
2020 Data = 0xcf;
2021 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2022
2023 /* Send the updated LCR value to the mos7840 */
2024 Data = mos7840_port->shadowLCR;
2025
2026 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2027
2028 Data = 0x00b;
2029 mos7840_port->shadowMCR = Data;
2030 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2031 Data = 0x00b;
2032 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2033
2034 /* set up the MCR register and send it to the mos7840 */
2035
2036 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002037 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002038 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002039
Alan Cox880af9d2008-07-22 11:16:12 +01002040 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002041 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002042 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002043 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002044
2045 Data = mos7840_port->shadowMCR;
2046 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2047
2048 /* Determine divisor based on baud rate */
2049 baud = tty_get_baud_rate(tty);
2050
2051 if (!baud) {
2052 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302053 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002054 baud = 9600;
2055 }
2056
Harvey Harrison441b62c2008-03-03 16:08:34 -08002057 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002058 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2059
2060 /* Enable Interrupts */
2061 Data = 0x0c;
2062 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2063
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002064 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002065 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002066 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002067 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002068 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002069 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002070 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002071 }
2072 }
2073 wake_up(&mos7840_port->delta_msr_wait);
2074 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302075 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002076 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002077}
2078
2079/*****************************************************************************
2080 * mos7840_set_termios
2081 * this function is called by the tty driver when it wants to change
2082 * the termios structure
2083 *****************************************************************************/
2084
Alan Cox95da3102008-07-22 11:09:07 +01002085static void mos7840_set_termios(struct tty_struct *tty,
2086 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002087 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002088{
2089 int status;
2090 unsigned int cflag;
2091 struct usb_serial *serial;
2092 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302093 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002094 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302095 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002096 return;
2097 }
2098
2099 serial = port->serial;
2100
Harvey Harrison441b62c2008-03-03 16:08:34 -08002101 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302102 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002103 return;
2104 }
2105
2106 mos7840_port = mos7840_get_port_private(port);
2107
2108 if (mos7840_port == NULL)
2109 return;
2110
Paul B Schroeder3f542972006-08-31 19:41:47 -05002111 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002112 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002113 return;
2114 }
2115
Tony Cook84fe6e72009-04-18 22:55:06 +09302116 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002117
2118 cflag = tty->termios->c_cflag;
2119
Harvey Harrison441b62c2008-03-03 16:08:34 -08002120 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002121 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002122 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002123 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002124 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002125
2126 /* change the port settings to the new ones specified */
2127
Alan Cox95da3102008-07-22 11:09:07 +01002128 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002129
2130 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302131 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132 return;
2133 }
2134
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002135 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002136 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002137 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2138 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002139 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002140 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002141 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002142 }
2143 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002144}
2145
2146/*****************************************************************************
2147 * mos7840_get_lsr_info - get line status register info
2148 *
2149 * Purpose: Let user call ioctl() to get info when the UART physically
2150 * is emptied. On bus types like RS485, the transmitter must
2151 * release the bus after transmitting. This must be done when
2152 * the transmit shift register is empty, not be done when the
2153 * transmit holding register is empty. This functionality
2154 * allows an RS485 driver to be written in user space.
2155 *****************************************************************************/
2156
Alan Cox95da3102008-07-22 11:09:07 +01002157static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002158 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002159{
2160 int count;
2161 unsigned int result = 0;
2162
Alan Cox95da3102008-07-22 11:09:07 +01002163 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002164 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002165 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002166 result = TIOCSER_TEMT;
2167 }
2168
2169 if (copy_to_user(value, &result, sizeof(int)))
2170 return -EFAULT;
2171 return 0;
2172}
2173
2174/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002175 * mos7840_get_serial_info
2176 * function to get information about serial port
2177 *****************************************************************************/
2178
2179static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002180 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002181{
2182 struct serial_struct tmp;
2183
2184 if (mos7840_port == NULL)
2185 return -1;
2186
2187 if (!retinfo)
2188 return -EFAULT;
2189
2190 memset(&tmp, 0, sizeof(tmp));
2191
2192 tmp.type = PORT_16550A;
2193 tmp.line = mos7840_port->port->serial->minor;
2194 tmp.port = mos7840_port->port->number;
2195 tmp.irq = 0;
2196 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2197 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2198 tmp.baud_base = 9600;
2199 tmp.close_delay = 5 * HZ;
2200 tmp.closing_wait = 30 * HZ;
2201
2202 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2203 return -EFAULT;
2204 return 0;
2205}
2206
Alan Cox0bca1b92010-09-16 18:21:40 +01002207static int mos7840_get_icount(struct tty_struct *tty,
2208 struct serial_icounter_struct *icount)
2209{
2210 struct usb_serial_port *port = tty->driver_data;
2211 struct moschip_port *mos7840_port;
2212 struct async_icount cnow;
2213
2214 mos7840_port = mos7840_get_port_private(port);
2215 cnow = mos7840_port->icount;
2216
2217 smp_rmb();
2218 icount->cts = cnow.cts;
2219 icount->dsr = cnow.dsr;
2220 icount->rng = cnow.rng;
2221 icount->dcd = cnow.dcd;
2222 icount->rx = cnow.rx;
2223 icount->tx = cnow.tx;
2224 icount->frame = cnow.frame;
2225 icount->overrun = cnow.overrun;
2226 icount->parity = cnow.parity;
2227 icount->brk = cnow.brk;
2228 icount->buf_overrun = cnow.buf_overrun;
2229
2230 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2231 port->number, icount->rx, icount->tx);
2232 return 0;
2233}
2234
Paul B Schroeder3f542972006-08-31 19:41:47 -05002235/*****************************************************************************
2236 * SerialIoctl
2237 * this function handles any ioctl calls to the driver
2238 *****************************************************************************/
2239
Alan Cox00a0d0d2011-02-14 16:27:06 +00002240static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002241 unsigned int cmd, unsigned long arg)
2242{
Alan Cox95da3102008-07-22 11:09:07 +01002243 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002244 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002245 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002246
2247 struct async_icount cnow;
2248 struct async_icount cprev;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002249
Harvey Harrison441b62c2008-03-03 16:08:34 -08002250 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302251 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002252 return -1;
2253 }
2254
2255 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002256
2257 if (mos7840_port == NULL)
2258 return -1;
2259
Harvey Harrison441b62c2008-03-03 16:08:34 -08002260 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002261
2262 switch (cmd) {
2263 /* return number of bytes available */
2264
Paul B Schroeder3f542972006-08-31 19:41:47 -05002265 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002266 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002267 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002268
Paul B Schroeder3f542972006-08-31 19:41:47 -05002269 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002270 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002271 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002272
2273 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002274 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002275 break;
2276
2277 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002278 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002279 cprev = mos7840_port->icount;
2280 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002281 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002282 mos7840_port->delta_msr_cond = 0;
2283 wait_event_interruptible(mos7840_port->delta_msr_wait,
2284 (mos7840_port->
2285 delta_msr_cond == 1));
2286
2287 /* see if a signal did it */
2288 if (signal_pending(current))
2289 return -ERESTARTSYS;
2290 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002291 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002292 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2293 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2294 return -EIO; /* no change => error */
2295 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2296 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2297 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2298 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2299 return 0;
2300 }
2301 cprev = cnow;
2302 }
2303 /* NOTREACHED */
2304 break;
2305
Paul B Schroeder3f542972006-08-31 19:41:47 -05002306 default:
2307 break;
2308 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002309 return -ENOIOCTLCMD;
2310}
2311
2312static int mos7840_calc_num_ports(struct usb_serial *serial)
2313{
Donald Lee093ea2d2012-03-14 15:26:33 +08002314 __u16 Data = 0x00;
2315 int ret = 0;
2316 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002317
Donald Lee093ea2d2012-03-14 15:26:33 +08002318 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2319 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &Data,
2320 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2321
2322 if ((Data & 0x01) == 0) {
2323 mos7840_num_ports = 2;
2324 serial->num_bulk_in = 2;
2325 serial->num_bulk_out = 2;
2326 serial->num_ports = 2;
2327 } else {
2328 mos7840_num_ports = 4;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002329 serial->num_bulk_in = 4;
2330 serial->num_bulk_out = 4;
Donald Lee093ea2d2012-03-14 15:26:33 +08002331 serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002332 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002333
Paul B Schroeder3f542972006-08-31 19:41:47 -05002334 return mos7840_num_ports;
2335}
2336
2337/****************************************************************************
2338 * mos7840_startup
2339 ****************************************************************************/
2340
2341static int mos7840_startup(struct usb_serial *serial)
2342{
2343 struct moschip_port *mos7840_port;
2344 struct usb_device *dev;
2345 int i, status;
2346
2347 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302348 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002349
2350 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302351 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002352 return -1;
2353 }
2354
2355 dev = serial->dev;
2356
Tony Cook84fe6e72009-04-18 22:55:06 +09302357 dbg("%s", "Entering...");
2358 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002359
2360 /* we set up the pointers to the endpoints in the mos7840_open *
2361 * function, as the structures aren't created yet. */
2362
2363 /* set up port private structures */
2364 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302365 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002366 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002367 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002368 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002369 status = -ENOMEM;
2370 i--; /* don't follow NULL pointer cleaning up */
2371 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002372 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002373
Alan Cox880af9d2008-07-22 11:16:12 +01002374 /* Initialize all port interrupt end point to port 0 int
2375 * endpoint. Our device has only one interrupt end point
2376 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002377
2378 mos7840_port->port = serial->port[i];
2379 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002380 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002381
Tony Cook37768ad2009-04-18 22:42:18 +09302382 /* minor is not initialised until later by
2383 * usb-serial.c:get_free_serial() and cannot therefore be used
2384 * to index device instances */
2385 mos7840_port->port_num = i + 1;
2386 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2387 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2388 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2389 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002390
2391 if (mos7840_port->port_num == 1) {
2392 mos7840_port->SpRegOffset = 0x0;
2393 mos7840_port->ControlRegOffset = 0x1;
2394 mos7840_port->DcrRegOffset = 0x4;
2395 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002396 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002397 mos7840_port->SpRegOffset = 0x8;
2398 mos7840_port->ControlRegOffset = 0x9;
2399 mos7840_port->DcrRegOffset = 0x16;
2400 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002401 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002402 mos7840_port->SpRegOffset = 0xa;
2403 mos7840_port->ControlRegOffset = 0xb;
2404 mos7840_port->DcrRegOffset = 0x19;
2405 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002406 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002407 mos7840_port->SpRegOffset = 0xa;
2408 mos7840_port->ControlRegOffset = 0xb;
2409 mos7840_port->DcrRegOffset = 0x19;
2410 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002411 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002412 mos7840_port->SpRegOffset = 0xc;
2413 mos7840_port->ControlRegOffset = 0xd;
2414 mos7840_port->DcrRegOffset = 0x1c;
2415 }
2416 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002417 mos7840_set_port_private(serial->port[i], mos7840_port);
2418
Alan Cox880af9d2008-07-22 11:16:12 +01002419 /* enable rx_disable bit in control register */
2420 status = mos7840_get_reg_sync(serial->port[i],
2421 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002422 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302423 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002424 break;
2425 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302426 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002427 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002428 Data |= 0x08; /* setting driver done bit */
2429 Data |= 0x04; /* sp1_bit to have cts change reflect in
2430 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002431
Alan Cox880af9d2008-07-22 11:16:12 +01002432 /* Data |= 0x20; //rx_disable bit */
2433 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002434 mos7840_port->ControlRegOffset, Data);
2435 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302436 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002437 break;
2438 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302439 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002440 status);
2441
Alan Cox880af9d2008-07-22 11:16:12 +01002442 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2443 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002444 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002445 status = mos7840_set_reg_sync(serial->port[i],
2446 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002447 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302448 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002449 break;
2450 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302451 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002452
2453 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002454 status = mos7840_set_reg_sync(serial->port[i],
2455 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002456 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302457 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002458 break;
2459 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302460 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002461
2462 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002463 status = mos7840_set_reg_sync(serial->port[i],
2464 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002465 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302466 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002467 break;
2468 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302469 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002470
Alan Cox880af9d2008-07-22 11:16:12 +01002471 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002472 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002473 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002474 CLK_START_VALUE_REGISTER, Data);
2475 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302476 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002477 break;
2478 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302479 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002480
2481 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002482 status = mos7840_set_reg_sync(serial->port[i],
2483 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002484 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302485 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002486 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002487 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002488 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302489 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002490 status);
2491
Alan Cox880af9d2008-07-22 11:16:12 +01002492 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002493 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002494 status = mos7840_set_uart_reg(serial->port[i],
2495 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002496 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302497 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002498 status);
2499 break;
2500 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302501 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002502 status);
2503
Alan Cox880af9d2008-07-22 11:16:12 +01002504 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002505 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002506 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002507
2508 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002509 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002510 (__u16) (ZLP_REG1 +
2511 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302512 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002513 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002514 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002515 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302516 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002517 i + 2, status);
2518 break;
2519 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302520 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002521 i + 2, status);
2522 } else {
2523 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002524 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002525 (__u16) (ZLP_REG1 +
2526 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302527 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002528 (__u16) (ZLP_REG1 +
2529 ((__u16) mos7840_port->port_num) - 0x1));
2530 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302531 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002532 i + 1, status);
2533 break;
2534 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302535 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002536 i + 1, status);
2537
2538 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002539 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002540 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002541 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2542 GFP_KERNEL);
2543 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2544 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002545 status = -ENOMEM;
2546 goto error;
2547 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002548 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302549 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002550
Alan Cox880af9d2008-07-22 11:16:12 +01002551 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002552 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002553 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2554 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302555 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002556 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002557 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302558 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002559
2560 /* setting configuration feature to one */
2561 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002562 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002563 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002564error:
2565 for (/* nothing */; i >= 0; i--) {
2566 mos7840_port = mos7840_get_port_private(serial->port[i]);
2567
2568 kfree(mos7840_port->dr);
2569 kfree(mos7840_port->ctrl_buf);
2570 usb_free_urb(mos7840_port->control_urb);
2571 kfree(mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002572 }
2573 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002574}
2575
2576/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002577 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002578 * This function is called whenever the device is removed from the usb bus.
2579 ****************************************************************************/
2580
Alan Sternf9c99bb2009-06-02 11:53:55 -04002581static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002582{
2583 int i;
2584 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002585 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002586
2587 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302588 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002589 return;
2590 }
2591
Alan Cox880af9d2008-07-22 11:16:12 +01002592 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002593
2594 /* free private structure allocated for serial port *
2595 * stop reads and writes on all ports */
2596
2597 for (i = 0; i < serial->num_ports; ++i) {
2598 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302599 dbg ("mos7840_port %d = %p", i, mos7840_port);
2600 if (mos7840_port) {
Tony Cook37768ad2009-04-18 22:42:18 +09302601 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002602 }
2603 }
2604
2605 dbg("%s", "Thank u :: ");
2606
2607}
2608
2609/****************************************************************************
2610 * mos7840_release
2611 * This function is called when the usb_serial structure is freed.
2612 ****************************************************************************/
2613
2614static void mos7840_release(struct usb_serial *serial)
2615{
2616 int i;
2617 struct moschip_port *mos7840_port;
2618 dbg("%s", " release :entering..........");
2619
2620 if (!serial) {
2621 dbg("%s", "Invalid Handler");
2622 return;
2623 }
2624
2625 /* check for the ports to be closed,close the ports and disconnect */
2626
2627 /* free private structure allocated for serial port *
2628 * stop reads and writes on all ports */
2629
2630 for (i = 0; i < serial->num_ports; ++i) {
2631 mos7840_port = mos7840_get_port_private(serial->port[i]);
2632 dbg("mos7840_port %d = %p", i, mos7840_port);
2633 if (mos7840_port) {
Johan Hovoldb296aac2012-10-25 13:35:09 +02002634 usb_free_urb(mos7840_port->control_urb);
Tony Cook37768ad2009-04-18 22:42:18 +09302635 kfree(mos7840_port->ctrl_buf);
2636 kfree(mos7840_port->dr);
2637 kfree(mos7840_port);
2638 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002639 }
2640
Tony Cook84fe6e72009-04-18 22:55:06 +09302641 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002642
2643}
2644
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002645static struct usb_driver io_driver = {
2646 .name = "mos7840",
2647 .probe = usb_serial_probe,
2648 .disconnect = usb_serial_disconnect,
2649 .id_table = moschip_id_table_combined,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002650};
2651
Paul B Schroeder3f542972006-08-31 19:41:47 -05002652static struct usb_serial_driver moschip7840_4port_device = {
2653 .driver = {
2654 .owner = THIS_MODULE,
2655 .name = "mos7840",
2656 },
2657 .description = DRIVER_DESC,
2658 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002659 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002660 .open = mos7840_open,
2661 .close = mos7840_close,
2662 .write = mos7840_write,
2663 .write_room = mos7840_write_room,
2664 .chars_in_buffer = mos7840_chars_in_buffer,
2665 .throttle = mos7840_throttle,
2666 .unthrottle = mos7840_unthrottle,
2667 .calc_num_ports = mos7840_calc_num_ports,
2668#ifdef MCSSerialProbe
2669 .probe = mos7840_serial_probe,
2670#endif
2671 .ioctl = mos7840_ioctl,
2672 .set_termios = mos7840_set_termios,
2673 .break_ctl = mos7840_break,
2674 .tiocmget = mos7840_tiocmget,
2675 .tiocmset = mos7840_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002676 .get_icount = mos7840_get_icount,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002677 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002678 .disconnect = mos7840_disconnect,
2679 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002680 .read_bulk_callback = mos7840_bulk_in_callback,
2681 .read_int_callback = mos7840_interrupt_callback,
2682};
2683
Alan Stern4d2a7af2012-02-23 14:57:09 -05002684static struct usb_serial_driver * const serial_drivers[] = {
2685 &moschip7840_4port_device, NULL
2686};
2687
Greg Kroah-Hartmane7414d92012-02-28 13:12:10 -08002688module_usb_serial_driver(io_driver, serial_drivers);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002689
Paul B Schroeder3f542972006-08-31 19:41:47 -05002690MODULE_DESCRIPTION(DRIVER_DESC);
2691MODULE_LICENSE("GPL");
2692
2693module_param(debug, bool, S_IRUGO | S_IWUSR);
2694MODULE_PARM_DESC(debug, "Debug enabled or not");