blob: c9bfecfc3d506acb278e4bbaa6c4595e356672b3 [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");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500926 return -1;
927 }
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");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500932 return -1;
933 }
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");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500939 return -1;
940 }
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");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500948 return -1;
949 }
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");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500956 return -1;
957 }
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");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500967 return -1;
968 }
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");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500974 return -1;
975 }
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");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500981 return -1;
982 }
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;
1137
1138}
1139
1140/*****************************************************************************
1141 * mos7840_chars_in_buffer
1142 * this function is called by the tty driver when it wants to know how many
1143 * bytes of data we currently have outstanding in the port (data that has
1144 * been written, but hasn't made it out the port yet)
1145 * If successful, we return the number of bytes left to be written in the
1146 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001147 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001148 *****************************************************************************/
1149
Alan Cox95da3102008-07-22 11:09:07 +01001150static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001151{
Alan Cox95da3102008-07-22 11:09:07 +01001152 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001153 int i;
1154 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001155 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001156 struct moschip_port *mos7840_port;
1157
Tony Cook84fe6e72009-04-18 22:55:06 +09301158 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001159
Harvey Harrison441b62c2008-03-03 16:08:34 -08001160 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301161 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001162 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001163 }
1164
1165 mos7840_port = mos7840_get_port_private(port);
1166 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301167 dbg("%s", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001168 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001169 }
1170
Alan Cox880af9d2008-07-22 11:16:12 +01001171 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell4893ce52012-07-24 14:15:13 -05001172 for (i = 0; i < NUM_URBS; ++i) {
1173 if (mos7840_port->busy[i]) {
1174 struct urb *urb = mos7840_port->write_urb_pool[i];
1175 chars += urb->transfer_buffer_length;
1176 }
1177 }
Alan Cox880af9d2008-07-22 11:16:12 +01001178 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001179 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001180 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001181
1182}
1183
Paul B Schroeder3f542972006-08-31 19:41:47 -05001184/*****************************************************************************
1185 * mos7840_close
1186 * this function is called by the tty driver when a port is closed
1187 *****************************************************************************/
1188
Alan Cox335f8512009-06-11 12:26:29 +01001189static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001190{
1191 struct usb_serial *serial;
1192 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001193 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001194 int j;
1195 __u16 Data;
1196
Tony Cook84fe6e72009-04-18 22:55:06 +09301197 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001198
Harvey Harrison441b62c2008-03-03 16:08:34 -08001199 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301200 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001201 return;
1202 }
1203
Harvey Harrison441b62c2008-03-03 16:08:34 -08001204 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001205 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301206 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001207 return;
1208 }
1209
1210 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001211 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001212
Oliver Neukum0de9a702007-03-16 20:28:28 +01001213 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001214 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001215
1216 for (j = 0; j < NUM_URBS; ++j)
1217 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1218
1219 /* Freeing Write URBs */
1220 for (j = 0; j < NUM_URBS; ++j) {
1221 if (mos7840_port->write_urb_pool[j]) {
1222 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1223 kfree(mos7840_port->write_urb_pool[j]->
1224 transfer_buffer);
1225
1226 usb_free_urb(mos7840_port->write_urb_pool[j]);
1227 }
1228 }
1229
Paul B Schroeder3f542972006-08-31 19:41:47 -05001230 /* While closing port, shutdown all bulk read, write *
1231 * and interrupt read if they exists */
1232 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001233 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301234 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001235 usb_kill_urb(mos7840_port->write_urb);
1236 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001237 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301238 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001239 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001240 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001241 }
1242 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301243 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001244 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001245 }
1246 }
Alan Cox880af9d2008-07-22 11:16:12 +01001247/* if(mos7840_port->ctrl_buf != NULL) */
1248/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001249 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301250 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001251 port0->open_ports, port->number);
1252 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001253 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301254 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001255 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001256 }
1257 }
1258
1259 if (mos7840_port->write_urb) {
1260 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001261 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001262 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001263 usb_free_urb(mos7840_port->write_urb);
1264 }
1265
1266 Data = 0x0;
1267 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1268
1269 Data = 0x00;
1270 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1271
1272 mos7840_port->open = 0;
1273
Tony Cook84fe6e72009-04-18 22:55:06 +09301274 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001275}
1276
1277/************************************************************************
1278 *
1279 * mos7840_block_until_chase_response
1280 *
1281 * This function will block the close until one of the following:
1282 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001283 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001284 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1285 *
1286 ************************************************************************/
1287
Alan Cox95da3102008-07-22 11:09:07 +01001288static void mos7840_block_until_chase_response(struct tty_struct *tty,
1289 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001290{
1291 int timeout = 1 * HZ;
1292 int wait = 10;
1293 int count;
1294
1295 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001296 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001297
1298 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001299 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001300 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001301
1302 /* Block the thread for a while */
1303 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1304 timeout);
1305 /* No activity.. count down section */
1306 wait--;
1307 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001308 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001309 return;
1310 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001311 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001312 wait = 10;
1313 }
1314 }
1315
1316}
1317
1318/*****************************************************************************
1319 * mos7840_break
1320 * this function sends a break to the port
1321 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001322static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001323{
Alan Cox95da3102008-07-22 11:09:07 +01001324 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001325 unsigned char data;
1326 struct usb_serial *serial;
1327 struct moschip_port *mos7840_port;
1328
Tony Cook84fe6e72009-04-18 22:55:06 +09301329 dbg("%s", "Entering ...........");
1330 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001331
Harvey Harrison441b62c2008-03-03 16:08:34 -08001332 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301333 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001334 return;
1335 }
1336
Harvey Harrison441b62c2008-03-03 16:08:34 -08001337 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001338 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301339 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001340 return;
1341 }
1342
1343 mos7840_port = mos7840_get_port_private(port);
1344
Alan Cox880af9d2008-07-22 11:16:12 +01001345 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001346 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001347
Alan Cox95da3102008-07-22 11:09:07 +01001348 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001349 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001350 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001351
Alan Cox95da3102008-07-22 11:09:07 +01001352 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001353 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001354 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001355 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001356
Alan Cox6b447f042009-01-02 13:48:56 +00001357 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001358 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301359 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001360 mos7840_port->shadowLCR);
1361 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1362 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001363}
1364
1365/*****************************************************************************
1366 * mos7840_write_room
1367 * this function is called by the tty driver when it wants to know how many
1368 * bytes of data we can accept for a specific port.
1369 * If successful, we return the amount of room that we have for this port
1370 * Otherwise we return a negative error number.
1371 *****************************************************************************/
1372
Alan Cox95da3102008-07-22 11:09:07 +01001373static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001374{
Alan Cox95da3102008-07-22 11:09:07 +01001375 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001376 int i;
1377 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001378 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001379 struct moschip_port *mos7840_port;
1380
Tony Cook84fe6e72009-04-18 22:55:06 +09301381 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001382
Harvey Harrison441b62c2008-03-03 16:08:34 -08001383 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301384 dbg("%s", "Invalid port");
1385 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001386 return -1;
1387 }
1388
1389 mos7840_port = mos7840_get_port_private(port);
1390 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301391 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001392 return -1;
1393 }
1394
Oliver Neukum0de9a702007-03-16 20:28:28 +01001395 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001396 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001397 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001398 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001399 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001400 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001401
Oliver Neukum0de9a702007-03-16 20:28:28 +01001402 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001403 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001404 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001405
1406}
1407
1408/*****************************************************************************
1409 * mos7840_write
1410 * this function is called by the tty driver when data should be written to
1411 * the port.
1412 * If successful, we return the number of bytes written, otherwise we
1413 * return a negative error number.
1414 *****************************************************************************/
1415
Alan Cox95da3102008-07-22 11:09:07 +01001416static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001417 const unsigned char *data, int count)
1418{
1419 int status;
1420 int i;
1421 int bytes_sent = 0;
1422 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001423 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001424
1425 struct moschip_port *mos7840_port;
1426 struct usb_serial *serial;
1427 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001428 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001429 const unsigned char *current_position = data;
1430 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301431 dbg("%s", "entering ...........");
1432 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001433 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001434
1435#ifdef NOTMOS7840
1436 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001437 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1438 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301439 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1440 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001441 mos7840_port->shadowLCR);
1442
Alan Cox880af9d2008-07-22 11:16:12 +01001443 /* Data = 0x03; */
1444 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1445 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001446
Alan Cox880af9d2008-07-22 11:16:12 +01001447 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001448 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1449
Alan Cox880af9d2008-07-22 11:16:12 +01001450 /* Data = 0x0c; */
1451 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001452 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001453 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301454 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001455
1456 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001457 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301458 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001459
1460 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301461 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001462 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001463 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1464#endif
1465
Harvey Harrison441b62c2008-03-03 16:08:34 -08001466 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301467 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001468 return -1;
1469 }
1470
1471 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001472 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301473 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001474 return -1;
1475 }
1476
1477 mos7840_port = mos7840_get_port_private(port);
1478 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301479 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001480 return -1;
1481 }
1482
1483 /* try to find a free urb in the list */
1484 urb = NULL;
1485
Oliver Neukum0de9a702007-03-16 20:28:28 +01001486 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001487 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001488 if (!mos7840_port->busy[i]) {
1489 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001490 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301491 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001492 break;
1493 }
1494 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001495 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001496
1497 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001498 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001499 goto exit;
1500 }
1501
1502 if (urb->transfer_buffer == NULL) {
1503 urb->transfer_buffer =
1504 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1505
1506 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001507 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001508 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001509 goto exit;
1510 }
1511 }
1512 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1513
Al Viro97c49652006-10-09 20:29:03 +01001514 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001515
1516 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001517 if ((serial->num_ports == 2)
1518 && ((((__u16)port->number -
1519 (__u16)(port->serial->minor)) % 2) != 0)) {
1520 usb_fill_bulk_urb(urb,
1521 serial->dev,
1522 usb_sndbulkpipe(serial->dev,
1523 (port->bulk_out_endpointAddress) + 2),
1524 urb->transfer_buffer,
1525 transfer_size,
1526 mos7840_bulk_out_data_callback, mos7840_port);
1527 } else {
1528 usb_fill_bulk_urb(urb,
1529 serial->dev,
1530 usb_sndbulkpipe(serial->dev,
1531 port->bulk_out_endpointAddress),
1532 urb->transfer_buffer,
1533 transfer_size,
1534 mos7840_bulk_out_data_callback, mos7840_port);
1535 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001536
1537 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301538 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001539
1540 /* send it down the pipe */
1541 status = usb_submit_urb(urb, GFP_ATOMIC);
1542
1543 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001544 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001545 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001546 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001547 bytes_sent = status;
1548 goto exit;
1549 }
1550 bytes_sent = transfer_size;
1551 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001552 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301553 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001554exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001555 return bytes_sent;
1556
1557}
1558
1559/*****************************************************************************
1560 * mos7840_throttle
1561 * this function is called by the tty driver when it wants to stop the data
1562 * being read from the port.
1563 *****************************************************************************/
1564
Alan Cox95da3102008-07-22 11:09:07 +01001565static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001566{
Alan Cox95da3102008-07-22 11:09:07 +01001567 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001568 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001569 int status;
1570
Harvey Harrison441b62c2008-03-03 16:08:34 -08001571 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301572 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001573 return;
1574 }
1575
Tony Cook84fe6e72009-04-18 22:55:06 +09301576 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001577
1578 mos7840_port = mos7840_get_port_private(port);
1579
1580 if (mos7840_port == NULL)
1581 return;
1582
1583 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301584 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001585 return;
1586 }
1587
Tony Cook84fe6e72009-04-18 22:55:06 +09301588 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001589
Paul B Schroeder3f542972006-08-31 19:41:47 -05001590 /* if we are implementing XON/XOFF, send the stop character */
1591 if (I_IXOFF(tty)) {
1592 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001593 status = mos7840_write(tty, port, &stop_char, 1);
1594 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001595 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001596 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001597 /* if we are implementing RTS/CTS, toggle that line */
1598 if (tty->termios->c_cflag & CRTSCTS) {
1599 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001600 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001601 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001602 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}
1606
1607/*****************************************************************************
1608 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001609 * this function is called by the tty driver when it wants to resume
1610 * the data being read from the port (called after mos7840_throttle is
1611 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001612 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001613static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001614{
Alan Cox95da3102008-07-22 11:09:07 +01001615 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001616 int status;
1617 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1618
Harvey Harrison441b62c2008-03-03 16:08:34 -08001619 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301620 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001621 return;
1622 }
1623
1624 if (mos7840_port == NULL)
1625 return;
1626
1627 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001628 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001629 return;
1630 }
1631
Tony Cook84fe6e72009-04-18 22:55:06 +09301632 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001633
Paul B Schroeder3f542972006-08-31 19:41:47 -05001634 /* if we are implementing XON/XOFF, send the start character */
1635 if (I_IXOFF(tty)) {
1636 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001637 status = mos7840_write(tty, port, &start_char, 1);
1638 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001639 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001640 }
1641
1642 /* if we are implementing RTS/CTS, toggle that line */
1643 if (tty->termios->c_cflag & CRTSCTS) {
1644 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001645 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001646 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001647 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001648 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001649 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001650}
1651
Alan Cox60b33c12011-02-14 16:26:14 +00001652static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001653{
Alan Cox95da3102008-07-22 11:09:07 +01001654 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001655 struct moschip_port *mos7840_port;
1656 unsigned int result;
1657 __u16 msr;
1658 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001659 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001660 mos7840_port = mos7840_get_port_private(port);
1661
Harvey Harrison441b62c2008-03-03 16:08:34 -08001662 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001663
1664 if (mos7840_port == NULL)
1665 return -ENODEV;
1666
1667 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
Johan Hovolda4f40862013-10-09 17:01:09 +02001668 if (status != 1)
1669 return -EIO;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001670 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
Johan Hovolda4f40862013-10-09 17:01:09 +02001671 if (status != 1)
1672 return -EIO;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001673 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1674 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1675 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1676 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1677 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1678 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1679 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1680
Harvey Harrison441b62c2008-03-03 16:08:34 -08001681 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001682
1683 return result;
1684}
1685
Alan Cox20b9d172011-02-14 16:26:50 +00001686static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001687 unsigned int set, unsigned int clear)
1688{
Alan Cox95da3102008-07-22 11:09:07 +01001689 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001690 struct moschip_port *mos7840_port;
1691 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001692 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001693
Harvey Harrison441b62c2008-03-03 16:08:34 -08001694 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001695
1696 mos7840_port = mos7840_get_port_private(port);
1697
1698 if (mos7840_port == NULL)
1699 return -ENODEV;
1700
Alan Coxe2984492008-02-20 20:51:45 +00001701 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001702 mcr = mos7840_port->shadowMCR;
1703 if (clear & TIOCM_RTS)
1704 mcr &= ~MCR_RTS;
1705 if (clear & TIOCM_DTR)
1706 mcr &= ~MCR_DTR;
1707 if (clear & TIOCM_LOOP)
1708 mcr &= ~MCR_LOOPBACK;
1709
1710 if (set & TIOCM_RTS)
1711 mcr |= MCR_RTS;
1712 if (set & TIOCM_DTR)
1713 mcr |= MCR_DTR;
1714 if (set & TIOCM_LOOP)
1715 mcr |= MCR_LOOPBACK;
1716
1717 mos7840_port->shadowMCR = mcr;
1718
Paul B Schroeder3f542972006-08-31 19:41:47 -05001719 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1720 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301721 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001722 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001723 }
1724
1725 return 0;
1726}
1727
1728/*****************************************************************************
1729 * mos7840_calc_baud_rate_divisor
1730 * this function calculates the proper baud rate divisor for the specified
1731 * baud rate.
1732 *****************************************************************************/
1733static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001734 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001735{
1736
Harvey Harrison441b62c2008-03-03 16:08:34 -08001737 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001738
1739 if (baudRate <= 115200) {
1740 *divisor = 115200 / baudRate;
1741 *clk_sel_val = 0x0;
1742 }
1743 if ((baudRate > 115200) && (baudRate <= 230400)) {
1744 *divisor = 230400 / baudRate;
1745 *clk_sel_val = 0x10;
1746 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1747 *divisor = 403200 / baudRate;
1748 *clk_sel_val = 0x20;
1749 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1750 *divisor = 460800 / baudRate;
1751 *clk_sel_val = 0x30;
1752 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1753 *divisor = 806400 / baudRate;
1754 *clk_sel_val = 0x40;
1755 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1756 *divisor = 921600 / baudRate;
1757 *clk_sel_val = 0x50;
1758 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1759 *divisor = 1572864 / baudRate;
1760 *clk_sel_val = 0x60;
1761 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1762 *divisor = 3145728 / baudRate;
1763 *clk_sel_val = 0x70;
1764 }
1765 return 0;
1766
1767#ifdef NOTMCS7840
1768
1769 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1770 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1771 *divisor = mos7840_divisor_table[i].Divisor;
1772 return 0;
1773 }
1774 }
1775
1776 /* After trying for all the standard baud rates *
1777 * Try calculating the divisor for this baud rate */
1778
1779 if (baudrate > 75 && baudrate < 230400) {
1780 /* get the divisor */
1781 custom = (__u16) (230400L / baudrate);
1782
1783 /* Check for round off */
1784 round1 = (__u16) (2304000L / baudrate);
1785 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001786 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001787 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001788 *divisor = custom;
1789
Tony Cook84fe6e72009-04-18 22:55:06 +09301790 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001791 return 0;
1792 }
1793
Tony Cook84fe6e72009-04-18 22:55:06 +09301794 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001795 return -1;
1796#endif
1797}
1798
1799/*****************************************************************************
1800 * mos7840_send_cmd_write_baud_rate
1801 * this function sends the proper command to change the baud rate of the
1802 * specified port.
1803 *****************************************************************************/
1804
1805static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1806 int baudRate)
1807{
1808 int divisor = 0;
1809 int status;
1810 __u16 Data;
1811 unsigned char number;
1812 __u16 clk_sel_val;
1813 struct usb_serial_port *port;
1814
1815 if (mos7840_port == NULL)
1816 return -1;
1817
1818 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001819 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301820 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001821 return -1;
1822 }
1823
Harvey Harrison441b62c2008-03-03 16:08:34 -08001824 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301825 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001826 return -1;
1827 }
1828
Tony Cook84fe6e72009-04-18 22:55:06 +09301829 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001830
1831 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1832
Harvey Harrison441b62c2008-03-03 16:08:34 -08001833 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001834 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001835 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001836 if (baudRate > 115200) {
1837#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001838 /* NOTE: need to see the pther register to modify */
1839 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001840 Data = 0x2b;
1841 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001842 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1843 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001844 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301845 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001846 return -1;
1847 }
1848#endif
1849
1850 } else {
1851#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001852 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001853 Data = 0xb;
1854 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001855 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1856 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001857 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301858 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001859 return -1;
1860 }
1861#endif
1862
1863 }
1864
Alan Cox880af9d2008-07-22 11:16:12 +01001865 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001866 clk_sel_val = 0x0;
1867 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001868 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001869 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001870 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1871 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001872 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301873 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001874 return -1;
1875 }
1876 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001877 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1878 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001879 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301880 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001881 return -1;
1882 }
1883 /* Calculate the Divisor */
1884
1885 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001886 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001887 return status;
1888 }
1889 /* Enable access to divisor latch */
1890 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1891 mos7840_port->shadowLCR = Data;
1892 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1893
1894 /* Write the divisor */
1895 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301896 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001897 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1898
1899 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301900 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001901 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1902
1903 /* Disable access to divisor latch */
1904 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1905 mos7840_port->shadowLCR = Data;
1906 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1907
1908 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001909 return status;
1910}
1911
1912/*****************************************************************************
1913 * mos7840_change_port_settings
1914 * This routine is called to set the UART on the device to match
1915 * the specified new settings.
1916 *****************************************************************************/
1917
Alan Cox95da3102008-07-22 11:09:07 +01001918static void mos7840_change_port_settings(struct tty_struct *tty,
1919 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001920{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001921 int baud;
1922 unsigned cflag;
1923 unsigned iflag;
1924 __u8 lData;
1925 __u8 lParity;
1926 __u8 lStop;
1927 int status;
1928 __u16 Data;
1929 struct usb_serial_port *port;
1930 struct usb_serial *serial;
1931
1932 if (mos7840_port == NULL)
1933 return;
1934
1935 port = (struct usb_serial_port *)mos7840_port->port;
1936
Harvey Harrison441b62c2008-03-03 16:08:34 -08001937 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301938 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001939 return;
1940 }
1941
Harvey Harrison441b62c2008-03-03 16:08:34 -08001942 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301943 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001944 return;
1945 }
1946
1947 serial = port->serial;
1948
Harvey Harrison441b62c2008-03-03 16:08:34 -08001949 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001950
1951 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001952 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001953 return;
1954 }
1955
Tony Cook84fe6e72009-04-18 22:55:06 +09301956 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001957
1958 lData = LCR_BITS_8;
1959 lStop = LCR_STOP_1;
1960 lParity = LCR_PAR_NONE;
1961
1962 cflag = tty->termios->c_cflag;
1963 iflag = tty->termios->c_iflag;
1964
1965 /* Change the number of bits */
Colin Leitner7069fd02013-11-08 22:52:34 +01001966 switch (cflag & CSIZE) {
1967 case CS5:
1968 lData = LCR_BITS_5;
1969 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001970
Colin Leitner7069fd02013-11-08 22:52:34 +01001971 case CS6:
1972 lData = LCR_BITS_6;
1973 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001974
Colin Leitner7069fd02013-11-08 22:52:34 +01001975 case CS7:
1976 lData = LCR_BITS_7;
1977 break;
1978
1979 default:
1980 case CS8:
1981 lData = LCR_BITS_8;
1982 break;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001983 }
Colin Leitner7069fd02013-11-08 22:52:34 +01001984
Paul B Schroeder3f542972006-08-31 19:41:47 -05001985 /* Change the Parity bit */
1986 if (cflag & PARENB) {
1987 if (cflag & PARODD) {
1988 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001989 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001990 } else {
1991 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001992 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001993 }
1994
1995 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001996 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001997 }
1998
Alan Cox880af9d2008-07-22 11:16:12 +01001999 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002000 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002001
2002 /* Change the Stop bit */
2003 if (cflag & CSTOPB) {
2004 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002005 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002006 } else {
2007 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002008 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002009 }
2010
2011 /* Update the LCR with the correct value */
2012 mos7840_port->shadowLCR &=
2013 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2014 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2015
Tony Cook84fe6e72009-04-18 22:55:06 +09302016 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002017 mos7840_port->shadowLCR);
2018 /* Disable Interrupts */
2019 Data = 0x00;
2020 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2021
2022 Data = 0x00;
2023 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2024
2025 Data = 0xcf;
2026 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2027
2028 /* Send the updated LCR value to the mos7840 */
2029 Data = mos7840_port->shadowLCR;
2030
2031 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2032
2033 Data = 0x00b;
2034 mos7840_port->shadowMCR = Data;
2035 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2036 Data = 0x00b;
2037 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2038
2039 /* set up the MCR register and send it to the mos7840 */
2040
2041 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002042 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002043 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002044
Alan Cox880af9d2008-07-22 11:16:12 +01002045 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002046 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002047 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002048 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002049
2050 Data = mos7840_port->shadowMCR;
2051 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2052
2053 /* Determine divisor based on baud rate */
2054 baud = tty_get_baud_rate(tty);
2055
2056 if (!baud) {
2057 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302058 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002059 baud = 9600;
2060 }
2061
Harvey Harrison441b62c2008-03-03 16:08:34 -08002062 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002063 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2064
2065 /* Enable Interrupts */
2066 Data = 0x0c;
2067 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2068
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002069 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002070 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002071 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002072 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002073 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002074 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002075 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002076 }
2077 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302078 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002079 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002080}
2081
2082/*****************************************************************************
2083 * mos7840_set_termios
2084 * this function is called by the tty driver when it wants to change
2085 * the termios structure
2086 *****************************************************************************/
2087
Alan Cox95da3102008-07-22 11:09:07 +01002088static void mos7840_set_termios(struct tty_struct *tty,
2089 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002090 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002091{
2092 int status;
2093 unsigned int cflag;
2094 struct usb_serial *serial;
2095 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302096 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002097 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302098 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002099 return;
2100 }
2101
2102 serial = port->serial;
2103
Harvey Harrison441b62c2008-03-03 16:08:34 -08002104 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302105 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002106 return;
2107 }
2108
2109 mos7840_port = mos7840_get_port_private(port);
2110
2111 if (mos7840_port == NULL)
2112 return;
2113
Paul B Schroeder3f542972006-08-31 19:41:47 -05002114 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002115 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002116 return;
2117 }
2118
Tony Cook84fe6e72009-04-18 22:55:06 +09302119 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002120
2121 cflag = tty->termios->c_cflag;
2122
Harvey Harrison441b62c2008-03-03 16:08:34 -08002123 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002124 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002125 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002126 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002127 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002128
2129 /* change the port settings to the new ones specified */
2130
Alan Cox95da3102008-07-22 11:09:07 +01002131 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132
2133 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302134 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002135 return;
2136 }
2137
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002138 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002139 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002140 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2141 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002142 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002143 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002144 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002145 }
2146 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002147}
2148
2149/*****************************************************************************
2150 * mos7840_get_lsr_info - get line status register info
2151 *
2152 * Purpose: Let user call ioctl() to get info when the UART physically
2153 * is emptied. On bus types like RS485, the transmitter must
2154 * release the bus after transmitting. This must be done when
2155 * the transmit shift register is empty, not be done when the
2156 * transmit holding register is empty. This functionality
2157 * allows an RS485 driver to be written in user space.
2158 *****************************************************************************/
2159
Alan Cox95da3102008-07-22 11:09:07 +01002160static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002161 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002162{
2163 int count;
2164 unsigned int result = 0;
2165
Alan Cox95da3102008-07-22 11:09:07 +01002166 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002167 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002168 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002169 result = TIOCSER_TEMT;
2170 }
2171
2172 if (copy_to_user(value, &result, sizeof(int)))
2173 return -EFAULT;
2174 return 0;
2175}
2176
2177/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002178 * mos7840_get_serial_info
2179 * function to get information about serial port
2180 *****************************************************************************/
2181
2182static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002183 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002184{
2185 struct serial_struct tmp;
2186
2187 if (mos7840_port == NULL)
2188 return -1;
2189
2190 if (!retinfo)
2191 return -EFAULT;
2192
2193 memset(&tmp, 0, sizeof(tmp));
2194
2195 tmp.type = PORT_16550A;
2196 tmp.line = mos7840_port->port->serial->minor;
2197 tmp.port = mos7840_port->port->number;
2198 tmp.irq = 0;
2199 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2200 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2201 tmp.baud_base = 9600;
2202 tmp.close_delay = 5 * HZ;
2203 tmp.closing_wait = 30 * HZ;
2204
2205 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2206 return -EFAULT;
2207 return 0;
2208}
2209
Alan Cox0bca1b92010-09-16 18:21:40 +01002210static int mos7840_get_icount(struct tty_struct *tty,
2211 struct serial_icounter_struct *icount)
2212{
2213 struct usb_serial_port *port = tty->driver_data;
2214 struct moschip_port *mos7840_port;
2215 struct async_icount cnow;
2216
2217 mos7840_port = mos7840_get_port_private(port);
2218 cnow = mos7840_port->icount;
2219
2220 smp_rmb();
2221 icount->cts = cnow.cts;
2222 icount->dsr = cnow.dsr;
2223 icount->rng = cnow.rng;
2224 icount->dcd = cnow.dcd;
2225 icount->rx = cnow.rx;
2226 icount->tx = cnow.tx;
2227 icount->frame = cnow.frame;
2228 icount->overrun = cnow.overrun;
2229 icount->parity = cnow.parity;
2230 icount->brk = cnow.brk;
2231 icount->buf_overrun = cnow.buf_overrun;
2232
2233 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2234 port->number, icount->rx, icount->tx);
2235 return 0;
2236}
2237
Paul B Schroeder3f542972006-08-31 19:41:47 -05002238/*****************************************************************************
2239 * SerialIoctl
2240 * this function handles any ioctl calls to the driver
2241 *****************************************************************************/
2242
Alan Cox00a0d0d2011-02-14 16:27:06 +00002243static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002244 unsigned int cmd, unsigned long arg)
2245{
Alan Cox95da3102008-07-22 11:09:07 +01002246 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002247 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002248 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002249
2250 struct async_icount cnow;
2251 struct async_icount cprev;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002252
Harvey Harrison441b62c2008-03-03 16:08:34 -08002253 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302254 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002255 return -1;
2256 }
2257
2258 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002259
2260 if (mos7840_port == NULL)
2261 return -1;
2262
Harvey Harrison441b62c2008-03-03 16:08:34 -08002263 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002264
2265 switch (cmd) {
2266 /* return number of bytes available */
2267
Paul B Schroeder3f542972006-08-31 19:41:47 -05002268 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002269 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002270 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002271
Paul B Schroeder3f542972006-08-31 19:41:47 -05002272 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002273 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002274 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002275
2276 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002277 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002278 break;
2279
2280 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002281 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002282 cprev = mos7840_port->icount;
2283 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002284 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002285 mos7840_port->delta_msr_cond = 0;
Johan Hovoldced638a2013-03-19 09:21:20 +01002286 wait_event_interruptible(port->delta_msr_wait,
2287 (port->serial->disconnected ||
2288 mos7840_port->
Paul B Schroeder3f542972006-08-31 19:41:47 -05002289 delta_msr_cond == 1));
2290
2291 /* see if a signal did it */
2292 if (signal_pending(current))
2293 return -ERESTARTSYS;
Johan Hovoldced638a2013-03-19 09:21:20 +01002294
2295 if (port->serial->disconnected)
2296 return -EIO;
2297
Paul B Schroeder3f542972006-08-31 19:41:47 -05002298 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002299 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002300 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2301 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2302 return -EIO; /* no change => error */
2303 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2304 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2305 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2306 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2307 return 0;
2308 }
2309 cprev = cnow;
2310 }
2311 /* NOTREACHED */
2312 break;
2313
Paul B Schroeder3f542972006-08-31 19:41:47 -05002314 default:
2315 break;
2316 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002317 return -ENOIOCTLCMD;
2318}
2319
2320static int mos7840_calc_num_ports(struct usb_serial *serial)
2321{
Donald Lee093ea2d2012-03-14 15:26:33 +08002322 __u16 Data = 0x00;
2323 int ret = 0;
2324 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002325
Donald Lee093ea2d2012-03-14 15:26:33 +08002326 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2327 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &Data,
2328 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2329
2330 if ((Data & 0x01) == 0) {
2331 mos7840_num_ports = 2;
2332 serial->num_bulk_in = 2;
2333 serial->num_bulk_out = 2;
2334 serial->num_ports = 2;
2335 } else {
2336 mos7840_num_ports = 4;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002337 serial->num_bulk_in = 4;
2338 serial->num_bulk_out = 4;
Donald Lee093ea2d2012-03-14 15:26:33 +08002339 serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002340 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002341
Paul B Schroeder3f542972006-08-31 19:41:47 -05002342 return mos7840_num_ports;
2343}
2344
2345/****************************************************************************
2346 * mos7840_startup
2347 ****************************************************************************/
2348
2349static int mos7840_startup(struct usb_serial *serial)
2350{
2351 struct moschip_port *mos7840_port;
2352 struct usb_device *dev;
2353 int i, status;
2354
2355 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302356 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002357
2358 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302359 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002360 return -1;
2361 }
2362
2363 dev = serial->dev;
2364
Tony Cook84fe6e72009-04-18 22:55:06 +09302365 dbg("%s", "Entering...");
2366 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002367
2368 /* we set up the pointers to the endpoints in the mos7840_open *
2369 * function, as the structures aren't created yet. */
2370
2371 /* set up port private structures */
2372 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302373 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002374 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002375 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002376 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002377 status = -ENOMEM;
2378 i--; /* don't follow NULL pointer cleaning up */
2379 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002380 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002381
Alan Cox880af9d2008-07-22 11:16:12 +01002382 /* Initialize all port interrupt end point to port 0 int
2383 * endpoint. Our device has only one interrupt end point
2384 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002385
2386 mos7840_port->port = serial->port[i];
2387 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002388 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002389
Tony Cook37768ad2009-04-18 22:42:18 +09302390 /* minor is not initialised until later by
2391 * usb-serial.c:get_free_serial() and cannot therefore be used
2392 * to index device instances */
2393 mos7840_port->port_num = i + 1;
2394 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2395 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2396 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2397 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002398
2399 if (mos7840_port->port_num == 1) {
2400 mos7840_port->SpRegOffset = 0x0;
2401 mos7840_port->ControlRegOffset = 0x1;
2402 mos7840_port->DcrRegOffset = 0x4;
2403 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002404 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002405 mos7840_port->SpRegOffset = 0x8;
2406 mos7840_port->ControlRegOffset = 0x9;
2407 mos7840_port->DcrRegOffset = 0x16;
2408 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002409 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002410 mos7840_port->SpRegOffset = 0xa;
2411 mos7840_port->ControlRegOffset = 0xb;
2412 mos7840_port->DcrRegOffset = 0x19;
2413 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002414 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002415 mos7840_port->SpRegOffset = 0xa;
2416 mos7840_port->ControlRegOffset = 0xb;
2417 mos7840_port->DcrRegOffset = 0x19;
2418 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002419 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002420 mos7840_port->SpRegOffset = 0xc;
2421 mos7840_port->ControlRegOffset = 0xd;
2422 mos7840_port->DcrRegOffset = 0x1c;
2423 }
2424 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002425 mos7840_set_port_private(serial->port[i], mos7840_port);
2426
Alan Cox880af9d2008-07-22 11:16:12 +01002427 /* enable rx_disable bit in control register */
2428 status = mos7840_get_reg_sync(serial->port[i],
2429 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002430 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302431 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002432 break;
2433 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302434 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002435 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002436 Data |= 0x08; /* setting driver done bit */
2437 Data |= 0x04; /* sp1_bit to have cts change reflect in
2438 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002439
Alan Cox880af9d2008-07-22 11:16:12 +01002440 /* Data |= 0x20; //rx_disable bit */
2441 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002442 mos7840_port->ControlRegOffset, Data);
2443 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302444 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002445 break;
2446 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302447 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002448 status);
2449
Alan Cox880af9d2008-07-22 11:16:12 +01002450 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2451 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002452 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002453 status = mos7840_set_reg_sync(serial->port[i],
2454 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002455 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302456 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002457 break;
2458 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302459 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002460
2461 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002462 status = mos7840_set_reg_sync(serial->port[i],
2463 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002464 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302465 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002466 break;
2467 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302468 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002469
2470 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002471 status = mos7840_set_reg_sync(serial->port[i],
2472 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002473 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302474 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002475 break;
2476 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302477 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002478
Alan Cox880af9d2008-07-22 11:16:12 +01002479 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002480 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002481 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002482 CLK_START_VALUE_REGISTER, Data);
2483 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302484 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002485 break;
2486 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302487 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002488
2489 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002490 status = mos7840_set_reg_sync(serial->port[i],
2491 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002492 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302493 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002494 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002495 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002496 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302497 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002498 status);
2499
Alan Cox880af9d2008-07-22 11:16:12 +01002500 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002501 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002502 status = mos7840_set_uart_reg(serial->port[i],
2503 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002504 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302505 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002506 status);
2507 break;
2508 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302509 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002510 status);
2511
Alan Cox880af9d2008-07-22 11:16:12 +01002512 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002513 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002514 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002515
2516 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002517 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002518 (__u16) (ZLP_REG1 +
2519 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302520 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002521 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002522 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002523 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302524 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002525 i + 2, status);
2526 break;
2527 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302528 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002529 i + 2, status);
2530 } else {
2531 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002532 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002533 (__u16) (ZLP_REG1 +
2534 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302535 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002536 (__u16) (ZLP_REG1 +
2537 ((__u16) mos7840_port->port_num) - 0x1));
2538 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302539 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002540 i + 1, status);
2541 break;
2542 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302543 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002544 i + 1, status);
2545
2546 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002547 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002548 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002549 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2550 GFP_KERNEL);
2551 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2552 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002553 status = -ENOMEM;
2554 goto error;
2555 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002556 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302557 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002558
Alan Cox880af9d2008-07-22 11:16:12 +01002559 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002560 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002561 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2562 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302563 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002564 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002565 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302566 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002567
2568 /* setting configuration feature to one */
2569 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002570 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002571 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002572error:
2573 for (/* nothing */; i >= 0; i--) {
2574 mos7840_port = mos7840_get_port_private(serial->port[i]);
2575
2576 kfree(mos7840_port->dr);
2577 kfree(mos7840_port->ctrl_buf);
2578 usb_free_urb(mos7840_port->control_urb);
2579 kfree(mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002580 }
2581 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002582}
2583
2584/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002585 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002586 * This function is called whenever the device is removed from the usb bus.
2587 ****************************************************************************/
2588
Alan Sternf9c99bb2009-06-02 11:53:55 -04002589static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002590{
2591 int i;
2592 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002593 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002594
2595 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302596 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002597 return;
2598 }
2599
Alan Cox880af9d2008-07-22 11:16:12 +01002600 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002601
2602 /* free private structure allocated for serial port *
2603 * stop reads and writes on all ports */
2604
2605 for (i = 0; i < serial->num_ports; ++i) {
2606 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302607 dbg ("mos7840_port %d = %p", i, mos7840_port);
2608 if (mos7840_port) {
Tony Cook37768ad2009-04-18 22:42:18 +09302609 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002610 }
2611 }
2612
2613 dbg("%s", "Thank u :: ");
2614
2615}
2616
2617/****************************************************************************
2618 * mos7840_release
2619 * This function is called when the usb_serial structure is freed.
2620 ****************************************************************************/
2621
2622static void mos7840_release(struct usb_serial *serial)
2623{
2624 int i;
2625 struct moschip_port *mos7840_port;
2626 dbg("%s", " release :entering..........");
2627
2628 if (!serial) {
2629 dbg("%s", "Invalid Handler");
2630 return;
2631 }
2632
2633 /* check for the ports to be closed,close the ports and disconnect */
2634
2635 /* free private structure allocated for serial port *
2636 * stop reads and writes on all ports */
2637
2638 for (i = 0; i < serial->num_ports; ++i) {
2639 mos7840_port = mos7840_get_port_private(serial->port[i]);
2640 dbg("mos7840_port %d = %p", i, mos7840_port);
2641 if (mos7840_port) {
Johan Hovoldb296aac2012-10-25 13:35:09 +02002642 usb_free_urb(mos7840_port->control_urb);
Tony Cook37768ad2009-04-18 22:42:18 +09302643 kfree(mos7840_port->ctrl_buf);
2644 kfree(mos7840_port->dr);
2645 kfree(mos7840_port);
2646 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002647 }
2648
Tony Cook84fe6e72009-04-18 22:55:06 +09302649 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002650
2651}
2652
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002653static struct usb_driver io_driver = {
2654 .name = "mos7840",
2655 .probe = usb_serial_probe,
2656 .disconnect = usb_serial_disconnect,
2657 .id_table = moschip_id_table_combined,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002658};
2659
Paul B Schroeder3f542972006-08-31 19:41:47 -05002660static struct usb_serial_driver moschip7840_4port_device = {
2661 .driver = {
2662 .owner = THIS_MODULE,
2663 .name = "mos7840",
2664 },
2665 .description = DRIVER_DESC,
2666 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002667 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002668 .open = mos7840_open,
2669 .close = mos7840_close,
2670 .write = mos7840_write,
2671 .write_room = mos7840_write_room,
2672 .chars_in_buffer = mos7840_chars_in_buffer,
2673 .throttle = mos7840_throttle,
2674 .unthrottle = mos7840_unthrottle,
2675 .calc_num_ports = mos7840_calc_num_ports,
2676#ifdef MCSSerialProbe
2677 .probe = mos7840_serial_probe,
2678#endif
2679 .ioctl = mos7840_ioctl,
2680 .set_termios = mos7840_set_termios,
2681 .break_ctl = mos7840_break,
2682 .tiocmget = mos7840_tiocmget,
2683 .tiocmset = mos7840_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002684 .get_icount = mos7840_get_icount,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002685 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002686 .disconnect = mos7840_disconnect,
2687 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002688 .read_bulk_callback = mos7840_bulk_in_callback,
2689 .read_int_callback = mos7840_interrupt_callback,
2690};
2691
Alan Stern4d2a7af2012-02-23 14:57:09 -05002692static struct usb_serial_driver * const serial_drivers[] = {
2693 &moschip7840_4port_device, NULL
2694};
2695
Greg Kroah-Hartmane7414d92012-02-28 13:12:10 -08002696module_usb_serial_driver(io_driver, serial_drivers);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002697
Paul B Schroeder3f542972006-08-31 19:41:47 -05002698MODULE_DESCRIPTION(DRIVER_DESC);
2699MODULE_LICENSE("GPL");
2700
2701module_param(debug, bool, S_IRUGO | S_IWUSR);
2702MODULE_PARM_DESC(debug, "Debug enabled or not");