blob: d48b79fccbdac56b4d5ca634c37835b029ffb128 [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 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500243 int delta_msr_cond;
244 struct async_icount icount;
245 struct usb_serial_port *port; /* loop back to the owner of this object */
246
Alan Cox880af9d2008-07-22 11:16:12 +0100247 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500248 __u8 SpRegOffset;
249 __u8 ControlRegOffset;
250 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100251 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500252 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100253 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500254 char *ctrl_buf;
255 int MsrLsr;
256
Oliver Neukum0de9a702007-03-16 20:28:28 +0100257 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500258 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100259 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800260 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500261};
262
263
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030264static bool debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500265
266/*
267 * mos7840_set_reg_sync
268 * To set the Control register by calling usb_fill_control_urb function
269 * by passing usb_sndctrlpipe function as parameter.
270 */
271
272static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
273 __u16 val)
274{
275 struct usb_device *dev = port->serial->dev;
276 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930277 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500278
279 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
280 MCS_WR_RTYPE, val, reg, NULL, 0,
281 MOS_WDR_TIMEOUT);
282}
283
284/*
285 * mos7840_get_reg_sync
286 * To set the Uart register by calling usb_fill_control_urb function by
287 * passing usb_rcvctrlpipe function as parameter.
288 */
289
290static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100291 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500292{
293 struct usb_device *dev = port->serial->dev;
294 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100295 u8 *buf;
296
297 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
298 if (!buf)
299 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500300
301 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100302 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500303 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100304 *val = buf[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930305 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100306
307 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500308 return ret;
309}
310
311/*
312 * mos7840_set_uart_reg
313 * To set the Uart register by calling usb_fill_control_urb function by
314 * passing usb_sndctrlpipe function as parameter.
315 */
316
317static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
318 __u16 val)
319{
320
321 struct usb_device *dev = port->serial->dev;
322 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100323 /* For the UART control registers, the application number need
324 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100325 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100326 val |= (((__u16) port->number -
327 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930328 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500329 } else {
330 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100331 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500332 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930333 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500334 val);
335 } else {
336 val |=
337 (((__u16) port->number -
338 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930339 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500340 val);
341 }
342 }
343 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
344 MCS_WR_RTYPE, val, reg, NULL, 0,
345 MOS_WDR_TIMEOUT);
346
347}
348
349/*
350 * mos7840_get_uart_reg
351 * To set the Control register by calling usb_fill_control_urb function
352 * by passing usb_rcvctrlpipe function as parameter.
353 */
354static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100355 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500356{
357 struct usb_device *dev = port->serial->dev;
358 int ret = 0;
359 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100360 u8 *buf;
361
362 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
363 if (!buf)
364 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500365
Tony Cook84fe6e72009-04-18 22:55:06 +0930366 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100367 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
368 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100369 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500370 Wval =
371 (((__u16) port->number - (__u16) (port->serial->minor)) +
372 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930373 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500374 } else {
375 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100376 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500377 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930378 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500379 Wval);
380 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100381 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500382 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930383 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500384 Wval);
385 }
386 }
387 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100388 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500389 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100390 *val = buf[0];
391
392 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500393 return ret;
394}
395
396static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
397{
398
Tony Cook84fe6e72009-04-18 22:55:06 +0930399 dbg("***************************************");
400 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
401 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
402 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
403 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500404
405}
406
407/************************************************************************/
408/************************************************************************/
409/* I N T E R F A C E F U N C T I O N S */
410/* I N T E R F A C E F U N C T I O N S */
411/************************************************************************/
412/************************************************************************/
413
414static inline void mos7840_set_port_private(struct usb_serial_port *port,
415 struct moschip_port *data)
416{
417 usb_set_serial_port_data(port, (void *)data);
418}
419
420static inline struct moschip_port *mos7840_get_port_private(struct
421 usb_serial_port
422 *port)
423{
424 return (struct moschip_port *)usb_get_serial_port_data(port);
425}
426
Oliver Neukum0de9a702007-03-16 20:28:28 +0100427static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500428{
429 struct moschip_port *mos7840_port;
430 struct async_icount *icount;
431 mos7840_port = port;
432 icount = &mos7840_port->icount;
433 if (new_msr &
434 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
435 MOS_MSR_DELTA_CD)) {
436 icount = &mos7840_port->icount;
437
438 /* update input line counters */
439 if (new_msr & MOS_MSR_DELTA_CTS) {
440 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100441 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500442 }
443 if (new_msr & MOS_MSR_DELTA_DSR) {
444 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100445 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500446 }
447 if (new_msr & MOS_MSR_DELTA_CD) {
448 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100449 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500450 }
451 if (new_msr & MOS_MSR_DELTA_RI) {
452 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100453 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500454 }
Johan Hovoldee2470d2013-03-19 09:21:19 +0100455
456 mos7840_port->delta_msr_cond = 1;
Johan Hovoldced638a2013-03-19 09:21:20 +0100457 wake_up_interruptible(&port->port->delta_msr_wait);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500458 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500459}
460
Oliver Neukum0de9a702007-03-16 20:28:28 +0100461static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500462{
463 struct async_icount *icount;
464
Harvey Harrison441b62c2008-03-03 16:08:34 -0800465 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500466
467 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100468 /*
469 * Parity and Framing errors only count if they
470 * occur exclusive of a break being
471 * received.
472 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500473 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
474 }
475
476 /* update input line counters */
477 icount = &port->icount;
478 if (new_lsr & SERIAL_LSR_BI) {
479 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100480 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500481 }
482 if (new_lsr & SERIAL_LSR_OE) {
483 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100484 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500485 }
486 if (new_lsr & SERIAL_LSR_PE) {
487 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100488 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500489 }
490 if (new_lsr & SERIAL_LSR_FE) {
491 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100492 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500493 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500494}
495
496/************************************************************************/
497/************************************************************************/
498/* U S B C A L L B A C K F U N C T I O N S */
499/* U S B C A L L B A C K F U N C T I O N S */
500/************************************************************************/
501/************************************************************************/
502
David Howells7d12e782006-10-05 14:55:46 +0100503static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500504{
505 unsigned char *data;
506 struct moschip_port *mos7840_port;
507 __u8 regval = 0x0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700508 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500509
Ming Leicdc97792008-02-24 18:41:47 +0800510 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100511
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700512 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500513 case 0:
514 /* success */
515 break;
516 case -ECONNRESET:
517 case -ENOENT:
518 case -ESHUTDOWN:
519 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800520 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700521 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500522 return;
523 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800524 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700525 status);
Johan Hovold380c05e2012-10-25 18:56:32 +0200526 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500527 }
528
Tony Cook84fe6e72009-04-18 22:55:06 +0930529 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
530 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500531 mos7840_port->MsrLsr, mos7840_port->port_num);
532 data = urb->transfer_buffer;
533 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930534 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500535 if (mos7840_port->MsrLsr == 0)
536 mos7840_handle_new_msr(mos7840_port, regval);
537 else if (mos7840_port->MsrLsr == 1)
538 mos7840_handle_new_lsr(mos7840_port, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500539}
540
541static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100542 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500543{
544 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100545 struct usb_ctrlrequest *dr = mcs->dr;
546 unsigned char *buffer = mcs->ctrl_buf;
547 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500548
Paul B Schroeder3f542972006-08-31 19:41:47 -0500549 dr->bRequestType = MCS_RD_RTYPE;
550 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100551 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500552 dr->wIndex = cpu_to_le16(reg);
553 dr->wLength = cpu_to_le16(2);
554
555 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
556 (unsigned char *)dr, buffer, 2,
557 mos7840_control_callback, mcs);
558 mcs->control_urb->transfer_buffer_length = 2;
559 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
560 return ret;
561}
562
563/*****************************************************************************
564 * mos7840_interrupt_callback
565 * this is the callback function for when we have received data on the
566 * interrupt endpoint.
567 *****************************************************************************/
568
David Howells7d12e782006-10-05 14:55:46 +0100569static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500570{
571 int result;
572 int length;
573 struct moschip_port *mos7840_port;
574 struct usb_serial *serial;
575 __u16 Data;
576 unsigned char *data;
577 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100578 int i, rv = 0;
579 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700580 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500581
Tony Cook84fe6e72009-04-18 22:55:06 +0930582 dbg("%s", " : Entering");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500583
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700584 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500585 case 0:
586 /* success */
587 break;
588 case -ECONNRESET:
589 case -ENOENT:
590 case -ESHUTDOWN:
591 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800592 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700593 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500594 return;
595 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800596 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700597 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500598 goto exit;
599 }
600
601 length = urb->actual_length;
602 data = urb->transfer_buffer;
603
Ming Leicdc97792008-02-24 18:41:47 +0800604 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500605
606 /* Moschip get 5 bytes
607 * Byte 1 IIR Port 1 (port.number is 0)
608 * Byte 2 IIR Port 2 (port.number is 1)
609 * Byte 3 IIR Port 3 (port.number is 2)
610 * Byte 4 IIR Port 4 (port.number is 3)
611 * Byte 5 FIFO status for both */
612
613 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930614 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500615 return;
616 }
617
618 sp[0] = (__u8) data[0];
619 sp[1] = (__u8) data[1];
620 sp[2] = (__u8) data[2];
621 sp[3] = (__u8) data[3];
622 st = (__u8) data[4];
623
624 for (i = 0; i < serial->num_ports; i++) {
625 mos7840_port = mos7840_get_port_private(serial->port[i]);
626 wval =
627 (((__u16) serial->port[i]->number -
628 (__u16) (serial->minor)) + 1) << 8;
629 if (mos7840_port->open) {
630 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930631 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500632 } else {
633 switch (sp[i] & 0x0f) {
634 case SERIAL_IIR_RLS:
635 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930636 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500637 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100638 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500639 break;
640 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930641 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500642 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100643 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500644 break;
645 }
Johan Hovold8d649dd2012-10-25 18:56:33 +0200646 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500647 }
648 }
649 }
Alan Cox880af9d2008-07-22 11:16:12 +0100650 if (!(rv < 0))
651 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100652 return;
653exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500654 result = usb_submit_urb(urb, GFP_ATOMIC);
655 if (result) {
656 dev_err(&urb->dev->dev,
657 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800658 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500659 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500660}
661
662static int mos7840_port_paranoia_check(struct usb_serial_port *port,
663 const char *function)
664{
665 if (!port) {
666 dbg("%s - port == NULL", function);
667 return -1;
668 }
669 if (!port->serial) {
670 dbg("%s - port->serial == NULL", function);
671 return -1;
672 }
673
674 return 0;
675}
676
677/* Inline functions to check the sanity of a pointer that is passed to us */
678static int mos7840_serial_paranoia_check(struct usb_serial *serial,
679 const char *function)
680{
681 if (!serial) {
682 dbg("%s - serial == NULL", function);
683 return -1;
684 }
685 if (!serial->type) {
686 dbg("%s - serial->type == NULL!", function);
687 return -1;
688 }
689
690 return 0;
691}
692
693static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
694 const char *function)
695{
696 /* if no port was specified, or it fails a paranoia check */
697 if (!port ||
698 mos7840_port_paranoia_check(port, function) ||
699 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100700 /* then say that we don't have a valid usb_serial thing,
701 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500702 return NULL;
703 }
704
705 return port->serial;
706}
707
708/*****************************************************************************
709 * mos7840_bulk_in_callback
710 * this is the callback function for when we have received data on the
711 * bulk in endpoint.
712 *****************************************************************************/
713
David Howells7d12e782006-10-05 14:55:46 +0100714static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500715{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700716 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500717 unsigned char *data;
718 struct usb_serial *serial;
719 struct usb_serial_port *port;
720 struct moschip_port *mos7840_port;
721 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700722 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500723
Ming Leicdc97792008-02-24 18:41:47 +0800724 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500725 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930726 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800727 return;
728 }
729
730 if (status) {
731 dbg("nonzero read bulk status received: %d", status);
732 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500733 return;
734 }
735
736 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800737 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930738 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800739 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500740 return;
741 }
742
Harvey Harrison441b62c2008-03-03 16:08:34 -0800743 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500744 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930745 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800746 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500747 return;
748 }
749
Tony Cook84fe6e72009-04-18 22:55:06 +0930750 dbg("%s", "Entering... ");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500751
752 data = urb->transfer_buffer;
753
Tony Cook84fe6e72009-04-18 22:55:06 +0930754 dbg("%s", "Entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500755
756 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100757 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500758 if (tty) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500759 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930760 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500761 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100762 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500763 }
764 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100765 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930766 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500767 mos7840_port->icount.rx);
768 }
769
770 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930771 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800772 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500773 return;
774 }
775
Paul B Schroeder3f542972006-08-31 19:41:47 -0500776
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800777 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700778 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100779
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700780 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800781 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
782 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500783 }
784}
785
786/*****************************************************************************
787 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100788 * this is the callback function for when we have finished sending
789 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500790 *****************************************************************************/
791
David Howells7d12e782006-10-05 14:55:46 +0100792static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500793{
794 struct moschip_port *mos7840_port;
795 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700796 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100797 int i;
798
Ming Leicdc97792008-02-24 18:41:47 +0800799 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100800 spin_lock(&mos7840_port->pool_lock);
801 for (i = 0; i < NUM_URBS; i++) {
802 if (urb == mos7840_port->write_urb_pool[i]) {
803 mos7840_port->busy[i] = 0;
804 break;
805 }
806 }
807 spin_unlock(&mos7840_port->pool_lock);
808
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700809 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930810 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500811 return;
812 }
813
Harvey Harrison441b62c2008-03-03 16:08:34 -0800814 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930815 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500816 return;
817 }
818
Tony Cook84fe6e72009-04-18 22:55:06 +0930819 dbg("%s", "Entering .........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500820
Alan Cox4a90f092008-10-13 10:39:46 +0100821 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800822 if (tty && mos7840_port->open)
823 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100824 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500825
826}
827
828/************************************************************************/
829/* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
830/************************************************************************/
831#ifdef MCSSerialProbe
832static int mos7840_serial_probe(struct usb_serial *serial,
833 const struct usb_device_id *id)
834{
835
836 /*need to implement the mode_reg reading and updating\
837 structures usb_serial_ device_type\
838 (i.e num_ports, num_bulkin,bulkout etc) */
839 /* Also we can update the changes attach */
840 return 1;
841}
842#endif
843
844/*****************************************************************************
845 * mos7840_open
846 * this function is called by the tty driver when a port is opened
847 * If successful, we return 0
848 * Otherwise we return a negative error number.
849 *****************************************************************************/
850
Alan Coxa509a7e2009-09-19 13:13:26 -0700851static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500852{
853 int response;
854 int j;
855 struct usb_serial *serial;
856 struct urb *urb;
857 __u16 Data;
858 int status;
859 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100860 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500861
Tony Cook84fe6e72009-04-18 22:55:06 +0930862 dbg ("%s enter", __func__);
863
Harvey Harrison441b62c2008-03-03 16:08:34 -0800864 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930865 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500866 return -ENODEV;
867 }
868
Paul B Schroeder3f542972006-08-31 19:41:47 -0500869 serial = port->serial;
870
Harvey Harrison441b62c2008-03-03 16:08:34 -0800871 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930872 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500873 return -ENODEV;
874 }
875
876 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100877 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500878
Oliver Neukum0de9a702007-03-16 20:28:28 +0100879 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500880 return -ENODEV;
881
882 usb_clear_halt(serial->dev, port->write_urb->pipe);
883 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100884 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500885
886 /* Initialising the write urb pool */
887 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100888 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500889 mos7840_port->write_urb_pool[j] = urb;
890
891 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700892 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500893 continue;
894 }
895
Alan Cox880af9d2008-07-22 11:16:12 +0100896 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
897 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500898 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100899 usb_free_urb(urb);
900 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700901 dev_err(&port->dev,
902 "%s-out of memory for urb buffers.\n",
903 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500904 continue;
905 }
906 }
907
908/*****************************************************************************
909 * Initialize MCS7840 -- Write Init values to corresponding Registers
910 *
911 * Register Index
912 * 1 : IER
913 * 2 : FCR
914 * 3 : LCR
915 * 4 : MCR
916 *
917 * 0x08 : SP1/2 Control Reg
918 *****************************************************************************/
919
Alan Cox880af9d2008-07-22 11:16:12 +0100920 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500921
Paul B Schroeder3f542972006-08-31 19:41:47 -0500922 Data = 0x0;
923 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
924 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930925 dbg("Reading Spreg failed");
Johan Hovold6ff428d2013-07-01 14:03:33 +0200926 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500927 }
928 Data |= 0x80;
929 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
930 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930931 dbg("writing Spreg failed");
Johan Hovold6ff428d2013-07-01 14:03:33 +0200932 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500933 }
934
935 Data &= ~0x80;
936 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
937 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930938 dbg("writing Spreg failed");
Johan Hovold6ff428d2013-07-01 14:03:33 +0200939 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500940 }
Alan Cox880af9d2008-07-22 11:16:12 +0100941 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500942
Paul B Schroeder3f542972006-08-31 19:41:47 -0500943 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100944 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
945 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500946 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930947 dbg("Reading Controlreg failed");
Johan Hovold6ff428d2013-07-01 14:03:33 +0200948 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500949 }
Alan Cox880af9d2008-07-22 11:16:12 +0100950 Data |= 0x08; /* Driver done bit */
951 Data |= 0x20; /* rx_disable */
952 status = mos7840_set_reg_sync(port,
953 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500954 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930955 dbg("writing Controlreg failed");
Johan Hovold6ff428d2013-07-01 14:03:33 +0200956 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500957 }
Alan Cox880af9d2008-07-22 11:16:12 +0100958 /* do register settings here */
959 /* Set all regs to the device default values. */
960 /***********************************
961 * First Disable all interrupts.
962 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500963 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500964 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
965 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930966 dbg("disabling interrupts failed");
Johan Hovold6ff428d2013-07-01 14:03:33 +0200967 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500968 }
Alan Cox880af9d2008-07-22 11:16:12 +0100969 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500970 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500971 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
972 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930973 dbg("Writing FIFO_CONTROL_REGISTER failed");
Johan Hovold6ff428d2013-07-01 14:03:33 +0200974 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500975 }
976
977 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500978 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
979 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930980 dbg("Writing FIFO_CONTROL_REGISTER failed");
Johan Hovold6ff428d2013-07-01 14:03:33 +0200981 goto err;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500982 }
983
984 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500985 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
986 mos7840_port->shadowLCR = Data;
987
988 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500989 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
990 mos7840_port->shadowMCR = Data;
991
992 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500993 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
994 mos7840_port->shadowLCR = Data;
995
Alan Cox880af9d2008-07-22 11:16:12 +0100996 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500997 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
998
999 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001000 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1001
1002 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001003 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1004
1005 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001006 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1007
1008 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001009 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1010 mos7840_port->shadowLCR = Data;
1011
Alan Cox880af9d2008-07-22 11:16:12 +01001012 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001013 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001014 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1015
1016 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001017 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1018
1019 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001020 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001021 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001022 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001023 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1024
Alan Cox880af9d2008-07-22 11:16:12 +01001025 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001026 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001027 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1028 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001029 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001030 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1031 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001032
Alan Cox880af9d2008-07-22 11:16:12 +01001033 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001034 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001035 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1036 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001037 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001038 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1039 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001040
Alan Cox880af9d2008-07-22 11:16:12 +01001041 /* Check to see if we've set up our endpoint info yet *
1042 * (can't set it up in mos7840_startup as the structures *
1043 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001044 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001045 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001046 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001047 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001048 serial->dev,
1049 usb_rcvintpipe(serial->dev,
1050 serial->port[0]->interrupt_in_endpointAddress),
1051 serial->port[0]->interrupt_in_buffer,
1052 serial->port[0]->interrupt_in_urb->
1053 transfer_buffer_length,
1054 mos7840_interrupt_callback,
1055 serial,
1056 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001057
1058 /* start interrupt read for mos7840 *
1059 * will continue as long as mos7840 is connected */
1060
1061 response =
1062 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1063 GFP_KERNEL);
1064 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001065 dev_err(&port->dev, "%s - Error %d submitting "
1066 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001067 }
1068
1069 }
1070
1071 }
1072
1073 /* see if we've set up our endpoint info yet *
1074 * (can't set it up in mos7840_startup as the *
1075 * structures were not set up at that time.) */
1076
Tony Cook84fe6e72009-04-18 22:55:06 +09301077 dbg("port number is %d", port->number);
1078 dbg("serial number is %d", port->serial->minor);
1079 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1080 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1081 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1082 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001083 mos7840_port->read_urb = port->read_urb;
1084
1085 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001086 if ((serial->num_ports == 2)
1087 && ((((__u16)port->number -
1088 (__u16)(port->serial->minor)) % 2) != 0)) {
1089 usb_fill_bulk_urb(mos7840_port->read_urb,
1090 serial->dev,
1091 usb_rcvbulkpipe(serial->dev,
1092 (port->bulk_in_endpointAddress) + 2),
1093 port->bulk_in_buffer,
1094 mos7840_port->read_urb->transfer_buffer_length,
1095 mos7840_bulk_in_callback, mos7840_port);
1096 } else {
1097 usb_fill_bulk_urb(mos7840_port->read_urb,
1098 serial->dev,
1099 usb_rcvbulkpipe(serial->dev,
1100 port->bulk_in_endpointAddress),
1101 port->bulk_in_buffer,
1102 mos7840_port->read_urb->transfer_buffer_length,
1103 mos7840_bulk_in_callback, mos7840_port);
1104 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001105
Tony Cook84fe6e72009-04-18 22:55:06 +09301106 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001107 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001108 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001109 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1110 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001111 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1112 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001113 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001114 }
1115
1116 /* initialize our wait queues */
1117 init_waitqueue_head(&mos7840_port->wait_chase);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001118
1119 /* initialize our icount structure */
1120 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1121
1122 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001123 /* Must set to enable ints! */
1124 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001125 /* send a open port command */
1126 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001127 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001128 mos7840_port->icount.tx = 0;
1129 mos7840_port->icount.rx = 0;
1130
Tony Cook84fe6e72009-04-18 22:55:06 +09301131 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001132 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001133
Tony Cook84fe6e72009-04-18 22:55:06 +09301134 dbg ("%s leave", __func__);
1135
Paul B Schroeder3f542972006-08-31 19:41:47 -05001136 return 0;
Johan Hovold6ff428d2013-07-01 14:03:33 +02001137err:
1138 for (j = 0; j < NUM_URBS; ++j) {
1139 urb = mos7840_port->write_urb_pool[j];
1140 if (!urb)
1141 continue;
1142 kfree(urb->transfer_buffer);
1143 usb_free_urb(urb);
1144 }
1145 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001146}
1147
1148/*****************************************************************************
1149 * mos7840_chars_in_buffer
1150 * this function is called by the tty driver when it wants to know how many
1151 * bytes of data we currently have outstanding in the port (data that has
1152 * been written, but hasn't made it out the port yet)
1153 * If successful, we return the number of bytes left to be written in the
1154 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001155 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001156 *****************************************************************************/
1157
Alan Cox95da3102008-07-22 11:09:07 +01001158static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001159{
Alan Cox95da3102008-07-22 11:09:07 +01001160 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001161 int i;
1162 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001163 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001164 struct moschip_port *mos7840_port;
1165
Tony Cook84fe6e72009-04-18 22:55:06 +09301166 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001167
Harvey Harrison441b62c2008-03-03 16:08:34 -08001168 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301169 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001170 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001171 }
1172
1173 mos7840_port = mos7840_get_port_private(port);
1174 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301175 dbg("%s", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001176 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001177 }
1178
Alan Cox880af9d2008-07-22 11:16:12 +01001179 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell4893ce52012-07-24 14:15:13 -05001180 for (i = 0; i < NUM_URBS; ++i) {
1181 if (mos7840_port->busy[i]) {
1182 struct urb *urb = mos7840_port->write_urb_pool[i];
1183 chars += urb->transfer_buffer_length;
1184 }
1185 }
Alan Cox880af9d2008-07-22 11:16:12 +01001186 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001187 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001188 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001189
1190}
1191
Paul B Schroeder3f542972006-08-31 19:41:47 -05001192/*****************************************************************************
1193 * mos7840_close
1194 * this function is called by the tty driver when a port is closed
1195 *****************************************************************************/
1196
Alan Cox335f8512009-06-11 12:26:29 +01001197static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001198{
1199 struct usb_serial *serial;
1200 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001201 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001202 int j;
1203 __u16 Data;
1204
Tony Cook84fe6e72009-04-18 22:55:06 +09301205 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001206
Harvey Harrison441b62c2008-03-03 16:08:34 -08001207 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301208 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001209 return;
1210 }
1211
Harvey Harrison441b62c2008-03-03 16:08:34 -08001212 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001213 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301214 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001215 return;
1216 }
1217
1218 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001219 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001220
Oliver Neukum0de9a702007-03-16 20:28:28 +01001221 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001222 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001223
1224 for (j = 0; j < NUM_URBS; ++j)
1225 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1226
1227 /* Freeing Write URBs */
1228 for (j = 0; j < NUM_URBS; ++j) {
1229 if (mos7840_port->write_urb_pool[j]) {
1230 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1231 kfree(mos7840_port->write_urb_pool[j]->
1232 transfer_buffer);
1233
1234 usb_free_urb(mos7840_port->write_urb_pool[j]);
1235 }
1236 }
1237
Paul B Schroeder3f542972006-08-31 19:41:47 -05001238 /* While closing port, shutdown all bulk read, write *
1239 * and interrupt read if they exists */
1240 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001241 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301242 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001243 usb_kill_urb(mos7840_port->write_urb);
1244 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001245 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301246 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001247 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001248 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001249 }
1250 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301251 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001252 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001253 }
1254 }
Alan Cox880af9d2008-07-22 11:16:12 +01001255/* if(mos7840_port->ctrl_buf != NULL) */
1256/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001257 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301258 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001259 port0->open_ports, port->number);
1260 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001261 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301262 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001263 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001264 }
1265 }
1266
1267 if (mos7840_port->write_urb) {
1268 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001269 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001270 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001271 usb_free_urb(mos7840_port->write_urb);
1272 }
1273
1274 Data = 0x0;
1275 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1276
1277 Data = 0x00;
1278 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1279
1280 mos7840_port->open = 0;
1281
Tony Cook84fe6e72009-04-18 22:55:06 +09301282 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001283}
1284
1285/************************************************************************
1286 *
1287 * mos7840_block_until_chase_response
1288 *
1289 * This function will block the close until one of the following:
1290 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001291 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001292 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1293 *
1294 ************************************************************************/
1295
Alan Cox95da3102008-07-22 11:09:07 +01001296static void mos7840_block_until_chase_response(struct tty_struct *tty,
1297 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001298{
1299 int timeout = 1 * HZ;
1300 int wait = 10;
1301 int count;
1302
1303 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001304 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001305
1306 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001307 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001308 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001309
1310 /* Block the thread for a while */
1311 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1312 timeout);
1313 /* No activity.. count down section */
1314 wait--;
1315 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001316 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001317 return;
1318 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001319 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001320 wait = 10;
1321 }
1322 }
1323
1324}
1325
1326/*****************************************************************************
1327 * mos7840_break
1328 * this function sends a break to the port
1329 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001330static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001331{
Alan Cox95da3102008-07-22 11:09:07 +01001332 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001333 unsigned char data;
1334 struct usb_serial *serial;
1335 struct moschip_port *mos7840_port;
1336
Tony Cook84fe6e72009-04-18 22:55:06 +09301337 dbg("%s", "Entering ...........");
1338 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001339
Harvey Harrison441b62c2008-03-03 16:08:34 -08001340 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301341 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001342 return;
1343 }
1344
Harvey Harrison441b62c2008-03-03 16:08:34 -08001345 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001346 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301347 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001348 return;
1349 }
1350
1351 mos7840_port = mos7840_get_port_private(port);
1352
Alan Cox880af9d2008-07-22 11:16:12 +01001353 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001354 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001355
Alan Cox95da3102008-07-22 11:09:07 +01001356 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001357 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001358 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001359
Alan Cox95da3102008-07-22 11:09:07 +01001360 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001361 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001362 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001363 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001364
Alan Cox6b447f042009-01-02 13:48:56 +00001365 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001366 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301367 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001368 mos7840_port->shadowLCR);
1369 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1370 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001371}
1372
1373/*****************************************************************************
1374 * mos7840_write_room
1375 * this function is called by the tty driver when it wants to know how many
1376 * bytes of data we can accept for a specific port.
1377 * If successful, we return the amount of room that we have for this port
1378 * Otherwise we return a negative error number.
1379 *****************************************************************************/
1380
Alan Cox95da3102008-07-22 11:09:07 +01001381static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001382{
Alan Cox95da3102008-07-22 11:09:07 +01001383 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001384 int i;
1385 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001386 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001387 struct moschip_port *mos7840_port;
1388
Tony Cook84fe6e72009-04-18 22:55:06 +09301389 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001390
Harvey Harrison441b62c2008-03-03 16:08:34 -08001391 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301392 dbg("%s", "Invalid port");
1393 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001394 return -1;
1395 }
1396
1397 mos7840_port = mos7840_get_port_private(port);
1398 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301399 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001400 return -1;
1401 }
1402
Oliver Neukum0de9a702007-03-16 20:28:28 +01001403 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001404 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001405 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001406 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001407 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001408 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001409
Oliver Neukum0de9a702007-03-16 20:28:28 +01001410 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001411 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001412 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001413
1414}
1415
1416/*****************************************************************************
1417 * mos7840_write
1418 * this function is called by the tty driver when data should be written to
1419 * the port.
1420 * If successful, we return the number of bytes written, otherwise we
1421 * return a negative error number.
1422 *****************************************************************************/
1423
Alan Cox95da3102008-07-22 11:09:07 +01001424static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001425 const unsigned char *data, int count)
1426{
1427 int status;
1428 int i;
1429 int bytes_sent = 0;
1430 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001431 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001432
1433 struct moschip_port *mos7840_port;
1434 struct usb_serial *serial;
1435 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001436 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001437 const unsigned char *current_position = data;
1438 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301439 dbg("%s", "entering ...........");
1440 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001441 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001442
1443#ifdef NOTMOS7840
1444 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001445 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1446 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301447 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1448 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001449 mos7840_port->shadowLCR);
1450
Alan Cox880af9d2008-07-22 11:16:12 +01001451 /* Data = 0x03; */
1452 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1453 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001454
Alan Cox880af9d2008-07-22 11:16:12 +01001455 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001456 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1457
Alan Cox880af9d2008-07-22 11:16:12 +01001458 /* Data = 0x0c; */
1459 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001460 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001461 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301462 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001463
1464 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001465 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301466 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001467
1468 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301469 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001470 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001471 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1472#endif
1473
Harvey Harrison441b62c2008-03-03 16:08:34 -08001474 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301475 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001476 return -1;
1477 }
1478
1479 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001480 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301481 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001482 return -1;
1483 }
1484
1485 mos7840_port = mos7840_get_port_private(port);
1486 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301487 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001488 return -1;
1489 }
1490
1491 /* try to find a free urb in the list */
1492 urb = NULL;
1493
Oliver Neukum0de9a702007-03-16 20:28:28 +01001494 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001495 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001496 if (!mos7840_port->busy[i]) {
1497 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001498 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301499 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001500 break;
1501 }
1502 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001503 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001504
1505 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001506 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001507 goto exit;
1508 }
1509
1510 if (urb->transfer_buffer == NULL) {
1511 urb->transfer_buffer =
1512 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1513
1514 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001515 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001516 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001517 goto exit;
1518 }
1519 }
1520 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1521
Al Viro97c49652006-10-09 20:29:03 +01001522 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001523
1524 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001525 if ((serial->num_ports == 2)
1526 && ((((__u16)port->number -
1527 (__u16)(port->serial->minor)) % 2) != 0)) {
1528 usb_fill_bulk_urb(urb,
1529 serial->dev,
1530 usb_sndbulkpipe(serial->dev,
1531 (port->bulk_out_endpointAddress) + 2),
1532 urb->transfer_buffer,
1533 transfer_size,
1534 mos7840_bulk_out_data_callback, mos7840_port);
1535 } else {
1536 usb_fill_bulk_urb(urb,
1537 serial->dev,
1538 usb_sndbulkpipe(serial->dev,
1539 port->bulk_out_endpointAddress),
1540 urb->transfer_buffer,
1541 transfer_size,
1542 mos7840_bulk_out_data_callback, mos7840_port);
1543 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001544
1545 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301546 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001547
1548 /* send it down the pipe */
1549 status = usb_submit_urb(urb, GFP_ATOMIC);
1550
1551 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001552 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001553 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001554 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001555 bytes_sent = status;
1556 goto exit;
1557 }
1558 bytes_sent = transfer_size;
1559 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001560 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301561 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001562exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001563 return bytes_sent;
1564
1565}
1566
1567/*****************************************************************************
1568 * mos7840_throttle
1569 * this function is called by the tty driver when it wants to stop the data
1570 * being read from the port.
1571 *****************************************************************************/
1572
Alan Cox95da3102008-07-22 11:09:07 +01001573static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001574{
Alan Cox95da3102008-07-22 11:09:07 +01001575 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001576 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001577 int status;
1578
Harvey Harrison441b62c2008-03-03 16:08:34 -08001579 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301580 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001581 return;
1582 }
1583
Tony Cook84fe6e72009-04-18 22:55:06 +09301584 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001585
1586 mos7840_port = mos7840_get_port_private(port);
1587
1588 if (mos7840_port == NULL)
1589 return;
1590
1591 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301592 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001593 return;
1594 }
1595
Tony Cook84fe6e72009-04-18 22:55:06 +09301596 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001597
Paul B Schroeder3f542972006-08-31 19:41:47 -05001598 /* if we are implementing XON/XOFF, send the stop character */
1599 if (I_IXOFF(tty)) {
1600 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001601 status = mos7840_write(tty, port, &stop_char, 1);
1602 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001603 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001604 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001605 /* if we are implementing RTS/CTS, toggle that line */
1606 if (tty->termios->c_cflag & CRTSCTS) {
1607 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001608 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001609 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001610 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001611 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001612 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001613}
1614
1615/*****************************************************************************
1616 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001617 * this function is called by the tty driver when it wants to resume
1618 * the data being read from the port (called after mos7840_throttle is
1619 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001620 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001621static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001622{
Alan Cox95da3102008-07-22 11:09:07 +01001623 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001624 int status;
1625 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1626
Harvey Harrison441b62c2008-03-03 16:08:34 -08001627 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301628 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001629 return;
1630 }
1631
1632 if (mos7840_port == NULL)
1633 return;
1634
1635 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001636 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001637 return;
1638 }
1639
Tony Cook84fe6e72009-04-18 22:55:06 +09301640 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001641
Paul B Schroeder3f542972006-08-31 19:41:47 -05001642 /* if we are implementing XON/XOFF, send the start character */
1643 if (I_IXOFF(tty)) {
1644 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001645 status = mos7840_write(tty, port, &start_char, 1);
1646 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001647 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001648 }
1649
1650 /* if we are implementing RTS/CTS, toggle that line */
1651 if (tty->termios->c_cflag & CRTSCTS) {
1652 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001653 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001654 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001655 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001656 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001657 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001658}
1659
Alan Cox60b33c12011-02-14 16:26:14 +00001660static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001661{
Alan Cox95da3102008-07-22 11:09:07 +01001662 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001663 struct moschip_port *mos7840_port;
1664 unsigned int result;
1665 __u16 msr;
1666 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001667 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001668 mos7840_port = mos7840_get_port_private(port);
1669
Harvey Harrison441b62c2008-03-03 16:08:34 -08001670 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001671
1672 if (mos7840_port == NULL)
1673 return -ENODEV;
1674
1675 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
Johan Hovolda4f40862013-10-09 17:01:09 +02001676 if (status != 1)
1677 return -EIO;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001678 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
Johan Hovolda4f40862013-10-09 17:01:09 +02001679 if (status != 1)
1680 return -EIO;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001681 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1682 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1683 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1684 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1685 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1686 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1687 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1688
Harvey Harrison441b62c2008-03-03 16:08:34 -08001689 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001690
1691 return result;
1692}
1693
Alan Cox20b9d172011-02-14 16:26:50 +00001694static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001695 unsigned int set, unsigned int clear)
1696{
Alan Cox95da3102008-07-22 11:09:07 +01001697 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001698 struct moschip_port *mos7840_port;
1699 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001700 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001701
Harvey Harrison441b62c2008-03-03 16:08:34 -08001702 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001703
1704 mos7840_port = mos7840_get_port_private(port);
1705
1706 if (mos7840_port == NULL)
1707 return -ENODEV;
1708
Alan Coxe2984492008-02-20 20:51:45 +00001709 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001710 mcr = mos7840_port->shadowMCR;
1711 if (clear & TIOCM_RTS)
1712 mcr &= ~MCR_RTS;
1713 if (clear & TIOCM_DTR)
1714 mcr &= ~MCR_DTR;
1715 if (clear & TIOCM_LOOP)
1716 mcr &= ~MCR_LOOPBACK;
1717
1718 if (set & TIOCM_RTS)
1719 mcr |= MCR_RTS;
1720 if (set & TIOCM_DTR)
1721 mcr |= MCR_DTR;
1722 if (set & TIOCM_LOOP)
1723 mcr |= MCR_LOOPBACK;
1724
1725 mos7840_port->shadowMCR = mcr;
1726
Paul B Schroeder3f542972006-08-31 19:41:47 -05001727 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1728 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301729 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001730 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001731 }
1732
1733 return 0;
1734}
1735
1736/*****************************************************************************
1737 * mos7840_calc_baud_rate_divisor
1738 * this function calculates the proper baud rate divisor for the specified
1739 * baud rate.
1740 *****************************************************************************/
1741static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001742 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001743{
1744
Harvey Harrison441b62c2008-03-03 16:08:34 -08001745 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001746
1747 if (baudRate <= 115200) {
1748 *divisor = 115200 / baudRate;
1749 *clk_sel_val = 0x0;
1750 }
1751 if ((baudRate > 115200) && (baudRate <= 230400)) {
1752 *divisor = 230400 / baudRate;
1753 *clk_sel_val = 0x10;
1754 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1755 *divisor = 403200 / baudRate;
1756 *clk_sel_val = 0x20;
1757 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1758 *divisor = 460800 / baudRate;
1759 *clk_sel_val = 0x30;
1760 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1761 *divisor = 806400 / baudRate;
1762 *clk_sel_val = 0x40;
1763 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1764 *divisor = 921600 / baudRate;
1765 *clk_sel_val = 0x50;
1766 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1767 *divisor = 1572864 / baudRate;
1768 *clk_sel_val = 0x60;
1769 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1770 *divisor = 3145728 / baudRate;
1771 *clk_sel_val = 0x70;
1772 }
1773 return 0;
1774
1775#ifdef NOTMCS7840
1776
1777 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1778 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1779 *divisor = mos7840_divisor_table[i].Divisor;
1780 return 0;
1781 }
1782 }
1783
1784 /* After trying for all the standard baud rates *
1785 * Try calculating the divisor for this baud rate */
1786
1787 if (baudrate > 75 && baudrate < 230400) {
1788 /* get the divisor */
1789 custom = (__u16) (230400L / baudrate);
1790
1791 /* Check for round off */
1792 round1 = (__u16) (2304000L / baudrate);
1793 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001794 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001795 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001796 *divisor = custom;
1797
Tony Cook84fe6e72009-04-18 22:55:06 +09301798 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001799 return 0;
1800 }
1801
Tony Cook84fe6e72009-04-18 22:55:06 +09301802 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001803 return -1;
1804#endif
1805}
1806
1807/*****************************************************************************
1808 * mos7840_send_cmd_write_baud_rate
1809 * this function sends the proper command to change the baud rate of the
1810 * specified port.
1811 *****************************************************************************/
1812
1813static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1814 int baudRate)
1815{
1816 int divisor = 0;
1817 int status;
1818 __u16 Data;
1819 unsigned char number;
1820 __u16 clk_sel_val;
1821 struct usb_serial_port *port;
1822
1823 if (mos7840_port == NULL)
1824 return -1;
1825
1826 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001827 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301828 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001829 return -1;
1830 }
1831
Harvey Harrison441b62c2008-03-03 16:08:34 -08001832 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301833 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001834 return -1;
1835 }
1836
Tony Cook84fe6e72009-04-18 22:55:06 +09301837 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001838
1839 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1840
Harvey Harrison441b62c2008-03-03 16:08:34 -08001841 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001842 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001843 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001844 if (baudRate > 115200) {
1845#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001846 /* NOTE: need to see the pther register to modify */
1847 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001848 Data = 0x2b;
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 } else {
1859#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001860 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001861 Data = 0xb;
1862 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001863 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1864 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001865 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301866 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001867 return -1;
1868 }
1869#endif
1870
1871 }
1872
Alan Cox880af9d2008-07-22 11:16:12 +01001873 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001874 clk_sel_val = 0x0;
1875 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001876 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001877 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001878 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1879 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001880 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301881 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001882 return -1;
1883 }
1884 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001885 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1886 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001887 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301888 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001889 return -1;
1890 }
1891 /* Calculate the Divisor */
1892
1893 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001894 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001895 return status;
1896 }
1897 /* Enable access to divisor latch */
1898 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1899 mos7840_port->shadowLCR = Data;
1900 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1901
1902 /* Write the divisor */
1903 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301904 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001905 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1906
1907 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301908 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001909 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1910
1911 /* Disable access to divisor latch */
1912 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1913 mos7840_port->shadowLCR = Data;
1914 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1915
1916 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001917 return status;
1918}
1919
1920/*****************************************************************************
1921 * mos7840_change_port_settings
1922 * This routine is called to set the UART on the device to match
1923 * the specified new settings.
1924 *****************************************************************************/
1925
Alan Cox95da3102008-07-22 11:09:07 +01001926static void mos7840_change_port_settings(struct tty_struct *tty,
1927 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001928{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001929 int baud;
1930 unsigned cflag;
1931 unsigned iflag;
1932 __u8 lData;
1933 __u8 lParity;
1934 __u8 lStop;
1935 int status;
1936 __u16 Data;
1937 struct usb_serial_port *port;
1938 struct usb_serial *serial;
1939
1940 if (mos7840_port == NULL)
1941 return;
1942
1943 port = (struct usb_serial_port *)mos7840_port->port;
1944
Harvey Harrison441b62c2008-03-03 16:08:34 -08001945 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301946 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001947 return;
1948 }
1949
Harvey Harrison441b62c2008-03-03 16:08:34 -08001950 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301951 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001952 return;
1953 }
1954
1955 serial = port->serial;
1956
Harvey Harrison441b62c2008-03-03 16:08:34 -08001957 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001958
1959 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001960 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001961 return;
1962 }
1963
Tony Cook84fe6e72009-04-18 22:55:06 +09301964 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001965
1966 lData = LCR_BITS_8;
1967 lStop = LCR_STOP_1;
1968 lParity = LCR_PAR_NONE;
1969
1970 cflag = tty->termios->c_cflag;
1971 iflag = tty->termios->c_iflag;
1972
1973 /* Change the number of bits */
Colin Leitner7069fd02013-11-08 22:52:34 +01001974 switch (cflag & CSIZE) {
1975 case CS5:
1976 lData = LCR_BITS_5;
1977 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001978
Colin Leitner7069fd02013-11-08 22:52:34 +01001979 case CS6:
1980 lData = LCR_BITS_6;
1981 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001982
Colin Leitner7069fd02013-11-08 22:52:34 +01001983 case CS7:
1984 lData = LCR_BITS_7;
1985 break;
1986
1987 default:
1988 case CS8:
1989 lData = LCR_BITS_8;
1990 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001991 }
Colin Leitner7069fd02013-11-08 22:52:34 +01001992
Paul B Schroeder3f542972006-08-31 19:41:47 -05001993 /* Change the Parity bit */
1994 if (cflag & PARENB) {
1995 if (cflag & PARODD) {
1996 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001997 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001998 } else {
1999 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002000 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002001 }
2002
2003 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002004 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002005 }
2006
Alan Cox880af9d2008-07-22 11:16:12 +01002007 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002008 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002009
2010 /* Change the Stop bit */
2011 if (cflag & CSTOPB) {
2012 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002013 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002014 } else {
2015 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002016 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002017 }
2018
2019 /* Update the LCR with the correct value */
2020 mos7840_port->shadowLCR &=
2021 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2022 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2023
Tony Cook84fe6e72009-04-18 22:55:06 +09302024 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002025 mos7840_port->shadowLCR);
2026 /* Disable Interrupts */
2027 Data = 0x00;
2028 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2029
2030 Data = 0x00;
2031 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2032
2033 Data = 0xcf;
2034 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2035
2036 /* Send the updated LCR value to the mos7840 */
2037 Data = mos7840_port->shadowLCR;
2038
2039 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2040
2041 Data = 0x00b;
2042 mos7840_port->shadowMCR = Data;
2043 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2044 Data = 0x00b;
2045 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2046
2047 /* set up the MCR register and send it to the mos7840 */
2048
2049 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002050 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002051 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002052
Alan Cox880af9d2008-07-22 11:16:12 +01002053 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002054 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002055 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002056 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002057
2058 Data = mos7840_port->shadowMCR;
2059 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2060
2061 /* Determine divisor based on baud rate */
2062 baud = tty_get_baud_rate(tty);
2063
2064 if (!baud) {
2065 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302066 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002067 baud = 9600;
2068 }
2069
Harvey Harrison441b62c2008-03-03 16:08:34 -08002070 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002071 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2072
2073 /* Enable Interrupts */
2074 Data = 0x0c;
2075 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2076
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002077 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002078 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002079 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002080 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002081 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002082 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002083 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002084 }
2085 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302086 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002087 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002088}
2089
2090/*****************************************************************************
2091 * mos7840_set_termios
2092 * this function is called by the tty driver when it wants to change
2093 * the termios structure
2094 *****************************************************************************/
2095
Alan Cox95da3102008-07-22 11:09:07 +01002096static void mos7840_set_termios(struct tty_struct *tty,
2097 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002098 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002099{
2100 int status;
2101 unsigned int cflag;
2102 struct usb_serial *serial;
2103 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302104 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002105 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302106 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002107 return;
2108 }
2109
2110 serial = port->serial;
2111
Harvey Harrison441b62c2008-03-03 16:08:34 -08002112 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302113 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002114 return;
2115 }
2116
2117 mos7840_port = mos7840_get_port_private(port);
2118
2119 if (mos7840_port == NULL)
2120 return;
2121
Paul B Schroeder3f542972006-08-31 19:41:47 -05002122 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002123 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002124 return;
2125 }
2126
Tony Cook84fe6e72009-04-18 22:55:06 +09302127 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002128
2129 cflag = tty->termios->c_cflag;
2130
Harvey Harrison441b62c2008-03-03 16:08:34 -08002131 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002133 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002134 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002135 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002136
2137 /* change the port settings to the new ones specified */
2138
Alan Cox95da3102008-07-22 11:09:07 +01002139 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002140
2141 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302142 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002143 return;
2144 }
2145
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002146 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002147 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002148 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2149 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002150 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002151 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002152 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002153 }
2154 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002155}
2156
2157/*****************************************************************************
2158 * mos7840_get_lsr_info - get line status register info
2159 *
2160 * Purpose: Let user call ioctl() to get info when the UART physically
2161 * is emptied. On bus types like RS485, the transmitter must
2162 * release the bus after transmitting. This must be done when
2163 * the transmit shift register is empty, not be done when the
2164 * transmit holding register is empty. This functionality
2165 * allows an RS485 driver to be written in user space.
2166 *****************************************************************************/
2167
Alan Cox95da3102008-07-22 11:09:07 +01002168static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002169 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002170{
2171 int count;
2172 unsigned int result = 0;
2173
Alan Cox95da3102008-07-22 11:09:07 +01002174 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002175 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002176 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002177 result = TIOCSER_TEMT;
2178 }
2179
2180 if (copy_to_user(value, &result, sizeof(int)))
2181 return -EFAULT;
2182 return 0;
2183}
2184
2185/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002186 * mos7840_get_serial_info
2187 * function to get information about serial port
2188 *****************************************************************************/
2189
2190static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002191 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002192{
2193 struct serial_struct tmp;
2194
2195 if (mos7840_port == NULL)
2196 return -1;
2197
2198 if (!retinfo)
2199 return -EFAULT;
2200
2201 memset(&tmp, 0, sizeof(tmp));
2202
2203 tmp.type = PORT_16550A;
2204 tmp.line = mos7840_port->port->serial->minor;
2205 tmp.port = mos7840_port->port->number;
2206 tmp.irq = 0;
2207 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2208 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2209 tmp.baud_base = 9600;
2210 tmp.close_delay = 5 * HZ;
2211 tmp.closing_wait = 30 * HZ;
2212
2213 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2214 return -EFAULT;
2215 return 0;
2216}
2217
Alan Cox0bca1b92010-09-16 18:21:40 +01002218static int mos7840_get_icount(struct tty_struct *tty,
2219 struct serial_icounter_struct *icount)
2220{
2221 struct usb_serial_port *port = tty->driver_data;
2222 struct moschip_port *mos7840_port;
2223 struct async_icount cnow;
2224
2225 mos7840_port = mos7840_get_port_private(port);
2226 cnow = mos7840_port->icount;
2227
2228 smp_rmb();
2229 icount->cts = cnow.cts;
2230 icount->dsr = cnow.dsr;
2231 icount->rng = cnow.rng;
2232 icount->dcd = cnow.dcd;
2233 icount->rx = cnow.rx;
2234 icount->tx = cnow.tx;
2235 icount->frame = cnow.frame;
2236 icount->overrun = cnow.overrun;
2237 icount->parity = cnow.parity;
2238 icount->brk = cnow.brk;
2239 icount->buf_overrun = cnow.buf_overrun;
2240
2241 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2242 port->number, icount->rx, icount->tx);
2243 return 0;
2244}
2245
Paul B Schroeder3f542972006-08-31 19:41:47 -05002246/*****************************************************************************
2247 * SerialIoctl
2248 * this function handles any ioctl calls to the driver
2249 *****************************************************************************/
2250
Alan Cox00a0d0d2011-02-14 16:27:06 +00002251static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002252 unsigned int cmd, unsigned long arg)
2253{
Alan Cox95da3102008-07-22 11:09:07 +01002254 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002255 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002256 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002257
2258 struct async_icount cnow;
2259 struct async_icount cprev;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002260
Harvey Harrison441b62c2008-03-03 16:08:34 -08002261 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302262 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002263 return -1;
2264 }
2265
2266 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002267
2268 if (mos7840_port == NULL)
2269 return -1;
2270
Harvey Harrison441b62c2008-03-03 16:08:34 -08002271 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002272
2273 switch (cmd) {
2274 /* return number of bytes available */
2275
Paul B Schroeder3f542972006-08-31 19:41:47 -05002276 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002277 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002278 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002279
Paul B Schroeder3f542972006-08-31 19:41:47 -05002280 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002281 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002282 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002283
2284 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002285 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002286 break;
2287
2288 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002289 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002290 cprev = mos7840_port->icount;
2291 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002292 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002293 mos7840_port->delta_msr_cond = 0;
Johan Hovoldced638a2013-03-19 09:21:20 +01002294 wait_event_interruptible(port->delta_msr_wait,
2295 (port->serial->disconnected ||
2296 mos7840_port->
Paul B Schroeder3f542972006-08-31 19:41:47 -05002297 delta_msr_cond == 1));
2298
2299 /* see if a signal did it */
2300 if (signal_pending(current))
2301 return -ERESTARTSYS;
Johan Hovoldced638a2013-03-19 09:21:20 +01002302
2303 if (port->serial->disconnected)
2304 return -EIO;
2305
Paul B Schroeder3f542972006-08-31 19:41:47 -05002306 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002307 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002308 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2309 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2310 return -EIO; /* no change => error */
2311 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2312 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2313 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2314 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2315 return 0;
2316 }
2317 cprev = cnow;
2318 }
2319 /* NOTREACHED */
2320 break;
2321
Paul B Schroeder3f542972006-08-31 19:41:47 -05002322 default:
2323 break;
2324 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002325 return -ENOIOCTLCMD;
2326}
2327
2328static int mos7840_calc_num_ports(struct usb_serial *serial)
2329{
Donald Lee093ea2d2012-03-14 15:26:33 +08002330 __u16 Data = 0x00;
2331 int ret = 0;
2332 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002333
Donald Lee093ea2d2012-03-14 15:26:33 +08002334 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2335 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &Data,
2336 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2337
2338 if ((Data & 0x01) == 0) {
2339 mos7840_num_ports = 2;
2340 serial->num_bulk_in = 2;
2341 serial->num_bulk_out = 2;
2342 serial->num_ports = 2;
2343 } else {
2344 mos7840_num_ports = 4;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002345 serial->num_bulk_in = 4;
2346 serial->num_bulk_out = 4;
Donald Lee093ea2d2012-03-14 15:26:33 +08002347 serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002348 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002349
Paul B Schroeder3f542972006-08-31 19:41:47 -05002350 return mos7840_num_ports;
2351}
2352
2353/****************************************************************************
2354 * mos7840_startup
2355 ****************************************************************************/
2356
2357static int mos7840_startup(struct usb_serial *serial)
2358{
2359 struct moschip_port *mos7840_port;
2360 struct usb_device *dev;
2361 int i, status;
2362
2363 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302364 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002365
2366 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302367 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002368 return -1;
2369 }
2370
2371 dev = serial->dev;
2372
Tony Cook84fe6e72009-04-18 22:55:06 +09302373 dbg("%s", "Entering...");
2374 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002375
2376 /* we set up the pointers to the endpoints in the mos7840_open *
2377 * function, as the structures aren't created yet. */
2378
2379 /* set up port private structures */
2380 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302381 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002382 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002383 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002384 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002385 status = -ENOMEM;
2386 i--; /* don't follow NULL pointer cleaning up */
2387 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002388 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002389
Alan Cox880af9d2008-07-22 11:16:12 +01002390 /* Initialize all port interrupt end point to port 0 int
2391 * endpoint. Our device has only one interrupt end point
2392 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002393
2394 mos7840_port->port = serial->port[i];
2395 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002396 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002397
Tony Cook37768ad2009-04-18 22:42:18 +09302398 /* minor is not initialised until later by
2399 * usb-serial.c:get_free_serial() and cannot therefore be used
2400 * to index device instances */
2401 mos7840_port->port_num = i + 1;
2402 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2403 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2404 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2405 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002406
2407 if (mos7840_port->port_num == 1) {
2408 mos7840_port->SpRegOffset = 0x0;
2409 mos7840_port->ControlRegOffset = 0x1;
2410 mos7840_port->DcrRegOffset = 0x4;
2411 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002412 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002413 mos7840_port->SpRegOffset = 0x8;
2414 mos7840_port->ControlRegOffset = 0x9;
2415 mos7840_port->DcrRegOffset = 0x16;
2416 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002417 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002418 mos7840_port->SpRegOffset = 0xa;
2419 mos7840_port->ControlRegOffset = 0xb;
2420 mos7840_port->DcrRegOffset = 0x19;
2421 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002422 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002423 mos7840_port->SpRegOffset = 0xa;
2424 mos7840_port->ControlRegOffset = 0xb;
2425 mos7840_port->DcrRegOffset = 0x19;
2426 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002427 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002428 mos7840_port->SpRegOffset = 0xc;
2429 mos7840_port->ControlRegOffset = 0xd;
2430 mos7840_port->DcrRegOffset = 0x1c;
2431 }
2432 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002433 mos7840_set_port_private(serial->port[i], mos7840_port);
2434
Alan Cox880af9d2008-07-22 11:16:12 +01002435 /* enable rx_disable bit in control register */
2436 status = mos7840_get_reg_sync(serial->port[i],
2437 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002438 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302439 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002440 break;
2441 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302442 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002443 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002444 Data |= 0x08; /* setting driver done bit */
2445 Data |= 0x04; /* sp1_bit to have cts change reflect in
2446 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002447
Alan Cox880af9d2008-07-22 11:16:12 +01002448 /* Data |= 0x20; //rx_disable bit */
2449 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002450 mos7840_port->ControlRegOffset, Data);
2451 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302452 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002453 break;
2454 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302455 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002456 status);
2457
Alan Cox880af9d2008-07-22 11:16:12 +01002458 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2459 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002460 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002461 status = mos7840_set_reg_sync(serial->port[i],
2462 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002463 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302464 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002465 break;
2466 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302467 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002468
2469 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002470 status = mos7840_set_reg_sync(serial->port[i],
2471 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002472 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302473 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002474 break;
2475 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302476 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002477
2478 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002479 status = mos7840_set_reg_sync(serial->port[i],
2480 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002481 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302482 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002483 break;
2484 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302485 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002486
Alan Cox880af9d2008-07-22 11:16:12 +01002487 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002488 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002489 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002490 CLK_START_VALUE_REGISTER, Data);
2491 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302492 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002493 break;
2494 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302495 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002496
2497 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002498 status = mos7840_set_reg_sync(serial->port[i],
2499 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002500 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302501 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002502 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002503 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002504 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302505 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002506 status);
2507
Alan Cox880af9d2008-07-22 11:16:12 +01002508 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002509 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002510 status = mos7840_set_uart_reg(serial->port[i],
2511 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002512 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302513 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002514 status);
2515 break;
2516 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302517 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002518 status);
2519
Alan Cox880af9d2008-07-22 11:16:12 +01002520 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002521 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002522 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002523
2524 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002525 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002526 (__u16) (ZLP_REG1 +
2527 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302528 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002529 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002530 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002531 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302532 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002533 i + 2, status);
2534 break;
2535 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302536 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002537 i + 2, status);
2538 } else {
2539 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002540 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002541 (__u16) (ZLP_REG1 +
2542 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302543 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002544 (__u16) (ZLP_REG1 +
2545 ((__u16) mos7840_port->port_num) - 0x1));
2546 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302547 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002548 i + 1, status);
2549 break;
2550 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302551 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002552 i + 1, status);
2553
2554 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002555 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002556 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002557 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2558 GFP_KERNEL);
2559 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2560 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002561 status = -ENOMEM;
2562 goto error;
2563 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002564 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302565 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002566
Alan Cox880af9d2008-07-22 11:16:12 +01002567 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002568 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002569 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2570 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302571 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002572 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002573 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302574 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002575
2576 /* setting configuration feature to one */
2577 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002578 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002579 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002580error:
2581 for (/* nothing */; i >= 0; i--) {
2582 mos7840_port = mos7840_get_port_private(serial->port[i]);
2583
2584 kfree(mos7840_port->dr);
2585 kfree(mos7840_port->ctrl_buf);
2586 usb_free_urb(mos7840_port->control_urb);
2587 kfree(mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002588 }
2589 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002590}
2591
2592/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002593 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002594 * This function is called whenever the device is removed from the usb bus.
2595 ****************************************************************************/
2596
Alan Sternf9c99bb2009-06-02 11:53:55 -04002597static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002598{
2599 int i;
2600 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002601 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002602
2603 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302604 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002605 return;
2606 }
2607
Alan Cox880af9d2008-07-22 11:16:12 +01002608 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002609
2610 /* free private structure allocated for serial port *
2611 * stop reads and writes on all ports */
2612
2613 for (i = 0; i < serial->num_ports; ++i) {
2614 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302615 dbg ("mos7840_port %d = %p", i, mos7840_port);
2616 if (mos7840_port) {
Tony Cook37768ad2009-04-18 22:42:18 +09302617 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002618 }
2619 }
2620
2621 dbg("%s", "Thank u :: ");
2622
2623}
2624
2625/****************************************************************************
2626 * mos7840_release
2627 * This function is called when the usb_serial structure is freed.
2628 ****************************************************************************/
2629
2630static void mos7840_release(struct usb_serial *serial)
2631{
2632 int i;
2633 struct moschip_port *mos7840_port;
2634 dbg("%s", " release :entering..........");
2635
2636 if (!serial) {
2637 dbg("%s", "Invalid Handler");
2638 return;
2639 }
2640
2641 /* check for the ports to be closed,close the ports and disconnect */
2642
2643 /* free private structure allocated for serial port *
2644 * stop reads and writes on all ports */
2645
2646 for (i = 0; i < serial->num_ports; ++i) {
2647 mos7840_port = mos7840_get_port_private(serial->port[i]);
2648 dbg("mos7840_port %d = %p", i, mos7840_port);
2649 if (mos7840_port) {
Johan Hovoldb296aac2012-10-25 13:35:09 +02002650 usb_free_urb(mos7840_port->control_urb);
Tony Cook37768ad2009-04-18 22:42:18 +09302651 kfree(mos7840_port->ctrl_buf);
2652 kfree(mos7840_port->dr);
2653 kfree(mos7840_port);
2654 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002655 }
2656
Tony Cook84fe6e72009-04-18 22:55:06 +09302657 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002658
2659}
2660
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002661static struct usb_driver io_driver = {
2662 .name = "mos7840",
2663 .probe = usb_serial_probe,
2664 .disconnect = usb_serial_disconnect,
2665 .id_table = moschip_id_table_combined,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002666};
2667
Paul B Schroeder3f542972006-08-31 19:41:47 -05002668static struct usb_serial_driver moschip7840_4port_device = {
2669 .driver = {
2670 .owner = THIS_MODULE,
2671 .name = "mos7840",
2672 },
2673 .description = DRIVER_DESC,
2674 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002675 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002676 .open = mos7840_open,
2677 .close = mos7840_close,
2678 .write = mos7840_write,
2679 .write_room = mos7840_write_room,
2680 .chars_in_buffer = mos7840_chars_in_buffer,
2681 .throttle = mos7840_throttle,
2682 .unthrottle = mos7840_unthrottle,
2683 .calc_num_ports = mos7840_calc_num_ports,
2684#ifdef MCSSerialProbe
2685 .probe = mos7840_serial_probe,
2686#endif
2687 .ioctl = mos7840_ioctl,
2688 .set_termios = mos7840_set_termios,
2689 .break_ctl = mos7840_break,
2690 .tiocmget = mos7840_tiocmget,
2691 .tiocmset = mos7840_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002692 .get_icount = mos7840_get_icount,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002693 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002694 .disconnect = mos7840_disconnect,
2695 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002696 .read_bulk_callback = mos7840_bulk_in_callback,
2697 .read_int_callback = mos7840_interrupt_callback,
2698};
2699
Alan Stern4d2a7af2012-02-23 14:57:09 -05002700static struct usb_serial_driver * const serial_drivers[] = {
2701 &moschip7840_4port_device, NULL
2702};
2703
Greg Kroah-Hartmane7414d92012-02-28 13:12:10 -08002704module_usb_serial_driver(io_driver, serial_drivers);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002705
Paul B Schroeder3f542972006-08-31 19:41:47 -05002706MODULE_DESCRIPTION(DRIVER_DESC);
2707MODULE_LICENSE("GPL");
2708
2709module_param(debug, bool, S_IRUGO | S_IWUSR);
2710MODULE_PARM_DESC(debug, "Debug enabled or not");