blob: c1505c3c557a2408f502cb97abffb83c10bdc576 [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 */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100238 struct urb *int_urb;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500239 __u8 shadowLCR; /* last LCR value received */
240 __u8 shadowMCR; /* last MCR value received */
241 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100242 char open_ports;
243 char zombie;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500244 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
245 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
246 int delta_msr_cond;
247 struct async_icount icount;
248 struct usb_serial_port *port; /* loop back to the owner of this object */
249
Alan Cox880af9d2008-07-22 11:16:12 +0100250 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500251 __u8 SpRegOffset;
252 __u8 ControlRegOffset;
253 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100254 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500255 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100256 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500257 char *ctrl_buf;
258 int MsrLsr;
259
Oliver Neukum0de9a702007-03-16 20:28:28 +0100260 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500261 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100262 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800263 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500264};
265
266
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030267static bool debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500268
269/*
270 * mos7840_set_reg_sync
271 * To set the Control register by calling usb_fill_control_urb function
272 * by passing usb_sndctrlpipe function as parameter.
273 */
274
275static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
276 __u16 val)
277{
278 struct usb_device *dev = port->serial->dev;
279 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930280 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500281
282 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
283 MCS_WR_RTYPE, val, reg, NULL, 0,
284 MOS_WDR_TIMEOUT);
285}
286
287/*
288 * mos7840_get_reg_sync
289 * To set the Uart register by calling usb_fill_control_urb function by
290 * passing usb_rcvctrlpipe function as parameter.
291 */
292
293static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100294 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500295{
296 struct usb_device *dev = port->serial->dev;
297 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100298 u8 *buf;
299
300 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
301 if (!buf)
302 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500303
304 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100305 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500306 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100307 *val = buf[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930308 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100309
310 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500311 return ret;
312}
313
314/*
315 * mos7840_set_uart_reg
316 * To set the Uart register by calling usb_fill_control_urb function by
317 * passing usb_sndctrlpipe function as parameter.
318 */
319
320static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
321 __u16 val)
322{
323
324 struct usb_device *dev = port->serial->dev;
325 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100326 /* For the UART control registers, the application number need
327 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100328 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100329 val |= (((__u16) port->number -
330 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930331 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500332 } else {
333 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100334 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500335 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930336 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500337 val);
338 } else {
339 val |=
340 (((__u16) port->number -
341 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930342 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500343 val);
344 }
345 }
346 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
347 MCS_WR_RTYPE, val, reg, NULL, 0,
348 MOS_WDR_TIMEOUT);
349
350}
351
352/*
353 * mos7840_get_uart_reg
354 * To set the Control register by calling usb_fill_control_urb function
355 * by passing usb_rcvctrlpipe function as parameter.
356 */
357static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100358 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500359{
360 struct usb_device *dev = port->serial->dev;
361 int ret = 0;
362 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100363 u8 *buf;
364
365 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
366 if (!buf)
367 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500368
Tony Cook84fe6e72009-04-18 22:55:06 +0930369 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100370 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
371 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100372 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500373 Wval =
374 (((__u16) port->number - (__u16) (port->serial->minor)) +
375 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930376 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500377 } else {
378 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100379 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500380 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930381 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500382 Wval);
383 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100384 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500385 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930386 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500387 Wval);
388 }
389 }
390 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100391 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500392 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100393 *val = buf[0];
394
395 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500396 return ret;
397}
398
399static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
400{
401
Tony Cook84fe6e72009-04-18 22:55:06 +0930402 dbg("***************************************");
403 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
404 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
405 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
406 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500407
408}
409
410/************************************************************************/
411/************************************************************************/
412/* I N T E R F A C E F U N C T I O N S */
413/* I N T E R F A C E F U N C T I O N S */
414/************************************************************************/
415/************************************************************************/
416
417static inline void mos7840_set_port_private(struct usb_serial_port *port,
418 struct moschip_port *data)
419{
420 usb_set_serial_port_data(port, (void *)data);
421}
422
423static inline struct moschip_port *mos7840_get_port_private(struct
424 usb_serial_port
425 *port)
426{
427 return (struct moschip_port *)usb_get_serial_port_data(port);
428}
429
Oliver Neukum0de9a702007-03-16 20:28:28 +0100430static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500431{
432 struct moschip_port *mos7840_port;
433 struct async_icount *icount;
434 mos7840_port = port;
435 icount = &mos7840_port->icount;
436 if (new_msr &
437 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
438 MOS_MSR_DELTA_CD)) {
439 icount = &mos7840_port->icount;
440
441 /* update input line counters */
442 if (new_msr & MOS_MSR_DELTA_CTS) {
443 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100444 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500445 }
446 if (new_msr & MOS_MSR_DELTA_DSR) {
447 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100448 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500449 }
450 if (new_msr & MOS_MSR_DELTA_CD) {
451 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100452 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500453 }
454 if (new_msr & MOS_MSR_DELTA_RI) {
455 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100456 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500457 }
458 }
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;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100508 int result = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700509 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500510
Ming Leicdc97792008-02-24 18:41:47 +0800511 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100512
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700513 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500514 case 0:
515 /* success */
516 break;
517 case -ECONNRESET:
518 case -ENOENT:
519 case -ESHUTDOWN:
520 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800521 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700522 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500523 return;
524 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800525 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700526 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500527 goto exit;
528 }
529
Tony Cook84fe6e72009-04-18 22:55:06 +0930530 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
531 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500532 mos7840_port->MsrLsr, mos7840_port->port_num);
533 data = urb->transfer_buffer;
534 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930535 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500536 if (mos7840_port->MsrLsr == 0)
537 mos7840_handle_new_msr(mos7840_port, regval);
538 else if (mos7840_port->MsrLsr == 1)
539 mos7840_handle_new_lsr(mos7840_port, regval);
540
Oliver Neukum0de9a702007-03-16 20:28:28 +0100541exit:
542 spin_lock(&mos7840_port->pool_lock);
543 if (!mos7840_port->zombie)
544 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
545 spin_unlock(&mos7840_port->pool_lock);
546 if (result) {
547 dev_err(&urb->dev->dev,
548 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800549 __func__, result);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100550 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500551}
552
553static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100554 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500555{
556 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100557 struct usb_ctrlrequest *dr = mcs->dr;
558 unsigned char *buffer = mcs->ctrl_buf;
559 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500560
Paul B Schroeder3f542972006-08-31 19:41:47 -0500561 dr->bRequestType = MCS_RD_RTYPE;
562 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100563 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500564 dr->wIndex = cpu_to_le16(reg);
565 dr->wLength = cpu_to_le16(2);
566
567 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
568 (unsigned char *)dr, buffer, 2,
569 mos7840_control_callback, mcs);
570 mcs->control_urb->transfer_buffer_length = 2;
571 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
572 return ret;
573}
574
575/*****************************************************************************
576 * mos7840_interrupt_callback
577 * this is the callback function for when we have received data on the
578 * interrupt endpoint.
579 *****************************************************************************/
580
David Howells7d12e782006-10-05 14:55:46 +0100581static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500582{
583 int result;
584 int length;
585 struct moschip_port *mos7840_port;
586 struct usb_serial *serial;
587 __u16 Data;
588 unsigned char *data;
589 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100590 int i, rv = 0;
591 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700592 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500593
Tony Cook84fe6e72009-04-18 22:55:06 +0930594 dbg("%s", " : Entering");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500595
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700596 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500597 case 0:
598 /* success */
599 break;
600 case -ECONNRESET:
601 case -ENOENT:
602 case -ESHUTDOWN:
603 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800604 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700605 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500606 return;
607 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800608 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700609 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500610 goto exit;
611 }
612
613 length = urb->actual_length;
614 data = urb->transfer_buffer;
615
Ming Leicdc97792008-02-24 18:41:47 +0800616 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500617
618 /* Moschip get 5 bytes
619 * Byte 1 IIR Port 1 (port.number is 0)
620 * Byte 2 IIR Port 2 (port.number is 1)
621 * Byte 3 IIR Port 3 (port.number is 2)
622 * Byte 4 IIR Port 4 (port.number is 3)
623 * Byte 5 FIFO status for both */
624
625 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930626 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500627 return;
628 }
629
630 sp[0] = (__u8) data[0];
631 sp[1] = (__u8) data[1];
632 sp[2] = (__u8) data[2];
633 sp[3] = (__u8) data[3];
634 st = (__u8) data[4];
635
636 for (i = 0; i < serial->num_ports; i++) {
637 mos7840_port = mos7840_get_port_private(serial->port[i]);
638 wval =
639 (((__u16) serial->port[i]->number -
640 (__u16) (serial->minor)) + 1) << 8;
641 if (mos7840_port->open) {
642 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930643 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500644 } else {
645 switch (sp[i] & 0x0f) {
646 case SERIAL_IIR_RLS:
647 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930648 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500649 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100650 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500651 break;
652 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930653 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500654 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100655 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500656 break;
657 }
Oliver Neukum0de9a702007-03-16 20:28:28 +0100658 spin_lock(&mos7840_port->pool_lock);
659 if (!mos7840_port->zombie) {
660 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
661 } else {
662 spin_unlock(&mos7840_port->pool_lock);
663 return;
664 }
665 spin_unlock(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500666 }
667 }
668 }
Alan Cox880af9d2008-07-22 11:16:12 +0100669 if (!(rv < 0))
670 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100671 return;
672exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500673 result = usb_submit_urb(urb, GFP_ATOMIC);
674 if (result) {
675 dev_err(&urb->dev->dev,
676 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800677 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500678 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500679}
680
681static int mos7840_port_paranoia_check(struct usb_serial_port *port,
682 const char *function)
683{
684 if (!port) {
685 dbg("%s - port == NULL", function);
686 return -1;
687 }
688 if (!port->serial) {
689 dbg("%s - port->serial == NULL", function);
690 return -1;
691 }
692
693 return 0;
694}
695
696/* Inline functions to check the sanity of a pointer that is passed to us */
697static int mos7840_serial_paranoia_check(struct usb_serial *serial,
698 const char *function)
699{
700 if (!serial) {
701 dbg("%s - serial == NULL", function);
702 return -1;
703 }
704 if (!serial->type) {
705 dbg("%s - serial->type == NULL!", function);
706 return -1;
707 }
708
709 return 0;
710}
711
712static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
713 const char *function)
714{
715 /* if no port was specified, or it fails a paranoia check */
716 if (!port ||
717 mos7840_port_paranoia_check(port, function) ||
718 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100719 /* then say that we don't have a valid usb_serial thing,
720 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500721 return NULL;
722 }
723
724 return port->serial;
725}
726
727/*****************************************************************************
728 * mos7840_bulk_in_callback
729 * this is the callback function for when we have received data on the
730 * bulk in endpoint.
731 *****************************************************************************/
732
David Howells7d12e782006-10-05 14:55:46 +0100733static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500734{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700735 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500736 unsigned char *data;
737 struct usb_serial *serial;
738 struct usb_serial_port *port;
739 struct moschip_port *mos7840_port;
740 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700741 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500742
Ming Leicdc97792008-02-24 18:41:47 +0800743 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500744 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930745 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800746 return;
747 }
748
749 if (status) {
750 dbg("nonzero read bulk status received: %d", status);
751 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500752 return;
753 }
754
755 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800756 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930757 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800758 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500759 return;
760 }
761
Harvey Harrison441b62c2008-03-03 16:08:34 -0800762 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500763 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930764 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800765 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500766 return;
767 }
768
Tony Cook84fe6e72009-04-18 22:55:06 +0930769 dbg("%s", "Entering... ");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500770
771 data = urb->transfer_buffer;
772
Tony Cook84fe6e72009-04-18 22:55:06 +0930773 dbg("%s", "Entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500774
775 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100776 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500777 if (tty) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500778 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930779 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500780 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100781 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500782 }
783 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100784 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930785 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500786 mos7840_port->icount.rx);
787 }
788
789 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930790 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800791 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500792 return;
793 }
794
Paul B Schroeder3f542972006-08-31 19:41:47 -0500795
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800796 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700797 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100798
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700799 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800800 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
801 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500802 }
803}
804
805/*****************************************************************************
806 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100807 * this is the callback function for when we have finished sending
808 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500809 *****************************************************************************/
810
David Howells7d12e782006-10-05 14:55:46 +0100811static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500812{
813 struct moschip_port *mos7840_port;
814 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700815 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100816 int i;
817
Ming Leicdc97792008-02-24 18:41:47 +0800818 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100819 spin_lock(&mos7840_port->pool_lock);
820 for (i = 0; i < NUM_URBS; i++) {
821 if (urb == mos7840_port->write_urb_pool[i]) {
822 mos7840_port->busy[i] = 0;
823 break;
824 }
825 }
826 spin_unlock(&mos7840_port->pool_lock);
827
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700828 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930829 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500830 return;
831 }
832
Harvey Harrison441b62c2008-03-03 16:08:34 -0800833 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930834 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500835 return;
836 }
837
Tony Cook84fe6e72009-04-18 22:55:06 +0930838 dbg("%s", "Entering .........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500839
Alan Cox4a90f092008-10-13 10:39:46 +0100840 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800841 if (tty && mos7840_port->open)
842 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100843 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500844
845}
846
847/************************************************************************/
848/* 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 */
849/************************************************************************/
850#ifdef MCSSerialProbe
851static int mos7840_serial_probe(struct usb_serial *serial,
852 const struct usb_device_id *id)
853{
854
855 /*need to implement the mode_reg reading and updating\
856 structures usb_serial_ device_type\
857 (i.e num_ports, num_bulkin,bulkout etc) */
858 /* Also we can update the changes attach */
859 return 1;
860}
861#endif
862
863/*****************************************************************************
864 * mos7840_open
865 * this function is called by the tty driver when a port is opened
866 * If successful, we return 0
867 * Otherwise we return a negative error number.
868 *****************************************************************************/
869
Alan Coxa509a7e2009-09-19 13:13:26 -0700870static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500871{
872 int response;
873 int j;
874 struct usb_serial *serial;
875 struct urb *urb;
876 __u16 Data;
877 int status;
878 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100879 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500880
Tony Cook84fe6e72009-04-18 22:55:06 +0930881 dbg ("%s enter", __func__);
882
Harvey Harrison441b62c2008-03-03 16:08:34 -0800883 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930884 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500885 return -ENODEV;
886 }
887
Paul B Schroeder3f542972006-08-31 19:41:47 -0500888 serial = port->serial;
889
Harvey Harrison441b62c2008-03-03 16:08:34 -0800890 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930891 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500892 return -ENODEV;
893 }
894
895 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100896 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500897
Oliver Neukum0de9a702007-03-16 20:28:28 +0100898 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500899 return -ENODEV;
900
901 usb_clear_halt(serial->dev, port->write_urb->pipe);
902 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100903 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500904
905 /* Initialising the write urb pool */
906 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100907 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500908 mos7840_port->write_urb_pool[j] = urb;
909
910 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700911 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500912 continue;
913 }
914
Alan Cox880af9d2008-07-22 11:16:12 +0100915 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
916 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500917 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100918 usb_free_urb(urb);
919 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700920 dev_err(&port->dev,
921 "%s-out of memory for urb buffers.\n",
922 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500923 continue;
924 }
925 }
926
927/*****************************************************************************
928 * Initialize MCS7840 -- Write Init values to corresponding Registers
929 *
930 * Register Index
931 * 1 : IER
932 * 2 : FCR
933 * 3 : LCR
934 * 4 : MCR
935 *
936 * 0x08 : SP1/2 Control Reg
937 *****************************************************************************/
938
Alan Cox880af9d2008-07-22 11:16:12 +0100939 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500940
Paul B Schroeder3f542972006-08-31 19:41:47 -0500941 Data = 0x0;
942 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
943 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930944 dbg("Reading Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500945 return -1;
946 }
947 Data |= 0x80;
948 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
949 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930950 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500951 return -1;
952 }
953
954 Data &= ~0x80;
955 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
956 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930957 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500958 return -1;
959 }
Alan Cox880af9d2008-07-22 11:16:12 +0100960 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500961
Paul B Schroeder3f542972006-08-31 19:41:47 -0500962 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100963 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
964 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500965 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930966 dbg("Reading Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500967 return -1;
968 }
Alan Cox880af9d2008-07-22 11:16:12 +0100969 Data |= 0x08; /* Driver done bit */
970 Data |= 0x20; /* rx_disable */
971 status = mos7840_set_reg_sync(port,
972 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500973 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930974 dbg("writing Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500975 return -1;
976 }
Alan Cox880af9d2008-07-22 11:16:12 +0100977 /* do register settings here */
978 /* Set all regs to the device default values. */
979 /***********************************
980 * First Disable all interrupts.
981 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500982 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500983 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
984 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930985 dbg("disabling interrupts failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500986 return -1;
987 }
Alan Cox880af9d2008-07-22 11:16:12 +0100988 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500989 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500990 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
991 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930992 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500993 return -1;
994 }
995
996 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500997 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
998 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930999 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001000 return -1;
1001 }
1002
1003 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001004 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1005 mos7840_port->shadowLCR = Data;
1006
1007 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001008 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1009 mos7840_port->shadowMCR = Data;
1010
1011 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001012 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1013 mos7840_port->shadowLCR = Data;
1014
Alan Cox880af9d2008-07-22 11:16:12 +01001015 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001016 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1017
1018 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001019 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1020
1021 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001022 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1023
1024 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001025 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1026
1027 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001028 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1029 mos7840_port->shadowLCR = Data;
1030
Alan Cox880af9d2008-07-22 11:16:12 +01001031 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001032 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001033 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1034
1035 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001036 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1037
1038 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001039 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001040 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001041 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001042 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1043
Alan Cox880af9d2008-07-22 11:16:12 +01001044 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001045 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001046 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1047 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001048 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001049 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1050 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001051
Alan Cox880af9d2008-07-22 11:16:12 +01001052 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001053 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001054 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1055 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001056 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001057 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1058 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001059
Alan Cox880af9d2008-07-22 11:16:12 +01001060 /* Check to see if we've set up our endpoint info yet *
1061 * (can't set it up in mos7840_startup as the structures *
1062 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001063 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001064 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001065 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001066 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001067 serial->dev,
1068 usb_rcvintpipe(serial->dev,
1069 serial->port[0]->interrupt_in_endpointAddress),
1070 serial->port[0]->interrupt_in_buffer,
1071 serial->port[0]->interrupt_in_urb->
1072 transfer_buffer_length,
1073 mos7840_interrupt_callback,
1074 serial,
1075 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001076
1077 /* start interrupt read for mos7840 *
1078 * will continue as long as mos7840 is connected */
1079
1080 response =
1081 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1082 GFP_KERNEL);
1083 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001084 dev_err(&port->dev, "%s - Error %d submitting "
1085 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001086 }
1087
1088 }
1089
1090 }
1091
1092 /* see if we've set up our endpoint info yet *
1093 * (can't set it up in mos7840_startup as the *
1094 * structures were not set up at that time.) */
1095
Tony Cook84fe6e72009-04-18 22:55:06 +09301096 dbg("port number is %d", port->number);
1097 dbg("serial number is %d", port->serial->minor);
1098 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1099 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1100 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1101 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001102 mos7840_port->read_urb = port->read_urb;
1103
1104 /* set up our bulk in urb */
Donald Lee093ea2d2012-03-14 15:26:33 +08001105 if ((serial->num_ports == 2)
1106 && ((((__u16)port->number -
1107 (__u16)(port->serial->minor)) % 2) != 0)) {
1108 usb_fill_bulk_urb(mos7840_port->read_urb,
1109 serial->dev,
1110 usb_rcvbulkpipe(serial->dev,
1111 (port->bulk_in_endpointAddress) + 2),
1112 port->bulk_in_buffer,
1113 mos7840_port->read_urb->transfer_buffer_length,
1114 mos7840_bulk_in_callback, mos7840_port);
1115 } else {
1116 usb_fill_bulk_urb(mos7840_port->read_urb,
1117 serial->dev,
1118 usb_rcvbulkpipe(serial->dev,
1119 port->bulk_in_endpointAddress),
1120 port->bulk_in_buffer,
1121 mos7840_port->read_urb->transfer_buffer_length,
1122 mos7840_bulk_in_callback, mos7840_port);
1123 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001124
Tony Cook84fe6e72009-04-18 22:55:06 +09301125 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001126 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001127 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001128 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1129 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001130 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1131 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001132 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001133 }
1134
1135 /* initialize our wait queues */
1136 init_waitqueue_head(&mos7840_port->wait_chase);
1137 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1138
1139 /* initialize our icount structure */
1140 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1141
1142 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001143 /* Must set to enable ints! */
1144 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001145 /* send a open port command */
1146 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001147 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001148 mos7840_port->icount.tx = 0;
1149 mos7840_port->icount.rx = 0;
1150
Tony Cook84fe6e72009-04-18 22:55:06 +09301151 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001152 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001153
Tony Cook84fe6e72009-04-18 22:55:06 +09301154 dbg ("%s leave", __func__);
1155
Paul B Schroeder3f542972006-08-31 19:41:47 -05001156 return 0;
1157
1158}
1159
1160/*****************************************************************************
1161 * mos7840_chars_in_buffer
1162 * this function is called by the tty driver when it wants to know how many
1163 * bytes of data we currently have outstanding in the port (data that has
1164 * been written, but hasn't made it out the port yet)
1165 * If successful, we return the number of bytes left to be written in the
1166 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001167 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001168 *****************************************************************************/
1169
Alan Cox95da3102008-07-22 11:09:07 +01001170static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001171{
Alan Cox95da3102008-07-22 11:09:07 +01001172 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001173 int i;
1174 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001175 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001176 struct moschip_port *mos7840_port;
1177
Tony Cook84fe6e72009-04-18 22:55:06 +09301178 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001179
Harvey Harrison441b62c2008-03-03 16:08:34 -08001180 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301181 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001182 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001183 }
1184
1185 mos7840_port = mos7840_get_port_private(port);
1186 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301187 dbg("%s", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001188 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001189 }
1190
Alan Cox880af9d2008-07-22 11:16:12 +01001191 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Mark Ferrell4893ce52012-07-24 14:15:13 -05001192 for (i = 0; i < NUM_URBS; ++i) {
1193 if (mos7840_port->busy[i]) {
1194 struct urb *urb = mos7840_port->write_urb_pool[i];
1195 chars += urb->transfer_buffer_length;
1196 }
1197 }
Alan Cox880af9d2008-07-22 11:16:12 +01001198 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001199 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001200 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001201
1202}
1203
Paul B Schroeder3f542972006-08-31 19:41:47 -05001204/*****************************************************************************
1205 * mos7840_close
1206 * this function is called by the tty driver when a port is closed
1207 *****************************************************************************/
1208
Alan Cox335f8512009-06-11 12:26:29 +01001209static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001210{
1211 struct usb_serial *serial;
1212 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001213 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001214 int j;
1215 __u16 Data;
1216
Tony Cook84fe6e72009-04-18 22:55:06 +09301217 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001218
Harvey Harrison441b62c2008-03-03 16:08:34 -08001219 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301220 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001221 return;
1222 }
1223
Harvey Harrison441b62c2008-03-03 16:08:34 -08001224 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001225 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301226 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001227 return;
1228 }
1229
1230 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001231 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001232
Oliver Neukum0de9a702007-03-16 20:28:28 +01001233 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001234 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001235
1236 for (j = 0; j < NUM_URBS; ++j)
1237 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1238
1239 /* Freeing Write URBs */
1240 for (j = 0; j < NUM_URBS; ++j) {
1241 if (mos7840_port->write_urb_pool[j]) {
1242 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1243 kfree(mos7840_port->write_urb_pool[j]->
1244 transfer_buffer);
1245
1246 usb_free_urb(mos7840_port->write_urb_pool[j]);
1247 }
1248 }
1249
Paul B Schroeder3f542972006-08-31 19:41:47 -05001250 /* While closing port, shutdown all bulk read, write *
1251 * and interrupt read if they exists */
1252 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001253 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301254 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001255 usb_kill_urb(mos7840_port->write_urb);
1256 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001257 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301258 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001259 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001260 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001261 }
1262 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301263 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001264 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001265 }
1266 }
Alan Cox880af9d2008-07-22 11:16:12 +01001267/* if(mos7840_port->ctrl_buf != NULL) */
1268/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001269 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301270 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001271 port0->open_ports, port->number);
1272 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001273 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301274 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001275 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001276 }
1277 }
1278
1279 if (mos7840_port->write_urb) {
1280 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001281 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001282 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001283 usb_free_urb(mos7840_port->write_urb);
1284 }
1285
1286 Data = 0x0;
1287 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1288
1289 Data = 0x00;
1290 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1291
1292 mos7840_port->open = 0;
1293
Tony Cook84fe6e72009-04-18 22:55:06 +09301294 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001295}
1296
1297/************************************************************************
1298 *
1299 * mos7840_block_until_chase_response
1300 *
1301 * This function will block the close until one of the following:
1302 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001303 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001304 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1305 *
1306 ************************************************************************/
1307
Alan Cox95da3102008-07-22 11:09:07 +01001308static void mos7840_block_until_chase_response(struct tty_struct *tty,
1309 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001310{
1311 int timeout = 1 * HZ;
1312 int wait = 10;
1313 int count;
1314
1315 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001316 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001317
1318 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001319 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001320 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001321
1322 /* Block the thread for a while */
1323 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1324 timeout);
1325 /* No activity.. count down section */
1326 wait--;
1327 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001328 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001329 return;
1330 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001331 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001332 wait = 10;
1333 }
1334 }
1335
1336}
1337
1338/*****************************************************************************
1339 * mos7840_break
1340 * this function sends a break to the port
1341 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001342static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001343{
Alan Cox95da3102008-07-22 11:09:07 +01001344 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001345 unsigned char data;
1346 struct usb_serial *serial;
1347 struct moschip_port *mos7840_port;
1348
Tony Cook84fe6e72009-04-18 22:55:06 +09301349 dbg("%s", "Entering ...........");
1350 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001351
Harvey Harrison441b62c2008-03-03 16:08:34 -08001352 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301353 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001354 return;
1355 }
1356
Harvey Harrison441b62c2008-03-03 16:08:34 -08001357 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001358 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301359 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001360 return;
1361 }
1362
1363 mos7840_port = mos7840_get_port_private(port);
1364
Alan Cox880af9d2008-07-22 11:16:12 +01001365 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001366 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001367
Alan Cox95da3102008-07-22 11:09:07 +01001368 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001369 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001370 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001371
Alan Cox95da3102008-07-22 11:09:07 +01001372 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001373 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001374 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001375 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001376
Alan Cox6b447f042009-01-02 13:48:56 +00001377 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001378 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301379 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001380 mos7840_port->shadowLCR);
1381 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1382 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001383}
1384
1385/*****************************************************************************
1386 * mos7840_write_room
1387 * this function is called by the tty driver when it wants to know how many
1388 * bytes of data we can accept for a specific port.
1389 * If successful, we return the amount of room that we have for this port
1390 * Otherwise we return a negative error number.
1391 *****************************************************************************/
1392
Alan Cox95da3102008-07-22 11:09:07 +01001393static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001394{
Alan Cox95da3102008-07-22 11:09:07 +01001395 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001396 int i;
1397 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001398 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001399 struct moschip_port *mos7840_port;
1400
Tony Cook84fe6e72009-04-18 22:55:06 +09301401 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001402
Harvey Harrison441b62c2008-03-03 16:08:34 -08001403 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301404 dbg("%s", "Invalid port");
1405 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001406 return -1;
1407 }
1408
1409 mos7840_port = mos7840_get_port_private(port);
1410 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301411 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001412 return -1;
1413 }
1414
Oliver Neukum0de9a702007-03-16 20:28:28 +01001415 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001416 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001417 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001418 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001419 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001420 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001421
Oliver Neukum0de9a702007-03-16 20:28:28 +01001422 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001423 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001424 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001425
1426}
1427
1428/*****************************************************************************
1429 * mos7840_write
1430 * this function is called by the tty driver when data should be written to
1431 * the port.
1432 * If successful, we return the number of bytes written, otherwise we
1433 * return a negative error number.
1434 *****************************************************************************/
1435
Alan Cox95da3102008-07-22 11:09:07 +01001436static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001437 const unsigned char *data, int count)
1438{
1439 int status;
1440 int i;
1441 int bytes_sent = 0;
1442 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001443 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001444
1445 struct moschip_port *mos7840_port;
1446 struct usb_serial *serial;
1447 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001448 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001449 const unsigned char *current_position = data;
1450 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301451 dbg("%s", "entering ...........");
1452 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001453 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001454
1455#ifdef NOTMOS7840
1456 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001457 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1458 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301459 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1460 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001461 mos7840_port->shadowLCR);
1462
Alan Cox880af9d2008-07-22 11:16:12 +01001463 /* Data = 0x03; */
1464 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1465 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001466
Alan Cox880af9d2008-07-22 11:16:12 +01001467 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001468 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1469
Alan Cox880af9d2008-07-22 11:16:12 +01001470 /* Data = 0x0c; */
1471 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001472 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001473 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301474 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001475
1476 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001477 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301478 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001479
1480 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301481 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001482 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001483 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1484#endif
1485
Harvey Harrison441b62c2008-03-03 16:08:34 -08001486 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301487 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001488 return -1;
1489 }
1490
1491 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001492 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301493 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001494 return -1;
1495 }
1496
1497 mos7840_port = mos7840_get_port_private(port);
1498 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301499 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001500 return -1;
1501 }
1502
1503 /* try to find a free urb in the list */
1504 urb = NULL;
1505
Oliver Neukum0de9a702007-03-16 20:28:28 +01001506 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001507 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001508 if (!mos7840_port->busy[i]) {
1509 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001510 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301511 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001512 break;
1513 }
1514 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001515 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001516
1517 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001518 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001519 goto exit;
1520 }
1521
1522 if (urb->transfer_buffer == NULL) {
1523 urb->transfer_buffer =
1524 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1525
1526 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001527 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001528 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001529 goto exit;
1530 }
1531 }
1532 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1533
Al Viro97c49652006-10-09 20:29:03 +01001534 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001535
1536 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001537 if ((serial->num_ports == 2)
1538 && ((((__u16)port->number -
1539 (__u16)(port->serial->minor)) % 2) != 0)) {
1540 usb_fill_bulk_urb(urb,
1541 serial->dev,
1542 usb_sndbulkpipe(serial->dev,
1543 (port->bulk_out_endpointAddress) + 2),
1544 urb->transfer_buffer,
1545 transfer_size,
1546 mos7840_bulk_out_data_callback, mos7840_port);
1547 } else {
1548 usb_fill_bulk_urb(urb,
1549 serial->dev,
1550 usb_sndbulkpipe(serial->dev,
1551 port->bulk_out_endpointAddress),
1552 urb->transfer_buffer,
1553 transfer_size,
1554 mos7840_bulk_out_data_callback, mos7840_port);
1555 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001556
1557 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301558 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001559
1560 /* send it down the pipe */
1561 status = usb_submit_urb(urb, GFP_ATOMIC);
1562
1563 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001564 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001565 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001566 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001567 bytes_sent = status;
1568 goto exit;
1569 }
1570 bytes_sent = transfer_size;
1571 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001572 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301573 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001574exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001575 return bytes_sent;
1576
1577}
1578
1579/*****************************************************************************
1580 * mos7840_throttle
1581 * this function is called by the tty driver when it wants to stop the data
1582 * being read from the port.
1583 *****************************************************************************/
1584
Alan Cox95da3102008-07-22 11:09:07 +01001585static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001586{
Alan Cox95da3102008-07-22 11:09:07 +01001587 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001588 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001589 int status;
1590
Harvey Harrison441b62c2008-03-03 16:08:34 -08001591 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301592 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001593 return;
1594 }
1595
Tony Cook84fe6e72009-04-18 22:55:06 +09301596 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001597
1598 mos7840_port = mos7840_get_port_private(port);
1599
1600 if (mos7840_port == NULL)
1601 return;
1602
1603 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301604 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001605 return;
1606 }
1607
Tony Cook84fe6e72009-04-18 22:55:06 +09301608 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001609
Paul B Schroeder3f542972006-08-31 19:41:47 -05001610 /* if we are implementing XON/XOFF, send the stop character */
1611 if (I_IXOFF(tty)) {
1612 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001613 status = mos7840_write(tty, port, &stop_char, 1);
1614 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001615 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001616 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001617 /* if we are implementing RTS/CTS, toggle that line */
1618 if (tty->termios->c_cflag & CRTSCTS) {
1619 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001620 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001621 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001622 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001623 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001624 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001625}
1626
1627/*****************************************************************************
1628 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001629 * this function is called by the tty driver when it wants to resume
1630 * the data being read from the port (called after mos7840_throttle is
1631 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001632 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001633static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001634{
Alan Cox95da3102008-07-22 11:09:07 +01001635 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001636 int status;
1637 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1638
Harvey Harrison441b62c2008-03-03 16:08:34 -08001639 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301640 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001641 return;
1642 }
1643
1644 if (mos7840_port == NULL)
1645 return;
1646
1647 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001648 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001649 return;
1650 }
1651
Tony Cook84fe6e72009-04-18 22:55:06 +09301652 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001653
Paul B Schroeder3f542972006-08-31 19:41:47 -05001654 /* if we are implementing XON/XOFF, send the start character */
1655 if (I_IXOFF(tty)) {
1656 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001657 status = mos7840_write(tty, port, &start_char, 1);
1658 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001659 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001660 }
1661
1662 /* if we are implementing RTS/CTS, toggle that line */
1663 if (tty->termios->c_cflag & CRTSCTS) {
1664 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001665 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001666 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001667 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001668 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001669 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001670}
1671
Alan Cox60b33c12011-02-14 16:26:14 +00001672static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001673{
Alan Cox95da3102008-07-22 11:09:07 +01001674 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001675 struct moschip_port *mos7840_port;
1676 unsigned int result;
1677 __u16 msr;
1678 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001679 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001680 mos7840_port = mos7840_get_port_private(port);
1681
Harvey Harrison441b62c2008-03-03 16:08:34 -08001682 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001683
1684 if (mos7840_port == NULL)
1685 return -ENODEV;
1686
1687 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1688 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1689 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1690 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1691 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1692 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1693 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1694 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1695 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1696
Harvey Harrison441b62c2008-03-03 16:08:34 -08001697 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001698
1699 return result;
1700}
1701
Alan Cox20b9d172011-02-14 16:26:50 +00001702static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001703 unsigned int set, unsigned int clear)
1704{
Alan Cox95da3102008-07-22 11:09:07 +01001705 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001706 struct moschip_port *mos7840_port;
1707 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001708 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001709
Harvey Harrison441b62c2008-03-03 16:08:34 -08001710 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001711
1712 mos7840_port = mos7840_get_port_private(port);
1713
1714 if (mos7840_port == NULL)
1715 return -ENODEV;
1716
Alan Coxe2984492008-02-20 20:51:45 +00001717 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001718 mcr = mos7840_port->shadowMCR;
1719 if (clear & TIOCM_RTS)
1720 mcr &= ~MCR_RTS;
1721 if (clear & TIOCM_DTR)
1722 mcr &= ~MCR_DTR;
1723 if (clear & TIOCM_LOOP)
1724 mcr &= ~MCR_LOOPBACK;
1725
1726 if (set & TIOCM_RTS)
1727 mcr |= MCR_RTS;
1728 if (set & TIOCM_DTR)
1729 mcr |= MCR_DTR;
1730 if (set & TIOCM_LOOP)
1731 mcr |= MCR_LOOPBACK;
1732
1733 mos7840_port->shadowMCR = mcr;
1734
Paul B Schroeder3f542972006-08-31 19:41:47 -05001735 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1736 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301737 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001738 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001739 }
1740
1741 return 0;
1742}
1743
1744/*****************************************************************************
1745 * mos7840_calc_baud_rate_divisor
1746 * this function calculates the proper baud rate divisor for the specified
1747 * baud rate.
1748 *****************************************************************************/
1749static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001750 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001751{
1752
Harvey Harrison441b62c2008-03-03 16:08:34 -08001753 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001754
1755 if (baudRate <= 115200) {
1756 *divisor = 115200 / baudRate;
1757 *clk_sel_val = 0x0;
1758 }
1759 if ((baudRate > 115200) && (baudRate <= 230400)) {
1760 *divisor = 230400 / baudRate;
1761 *clk_sel_val = 0x10;
1762 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1763 *divisor = 403200 / baudRate;
1764 *clk_sel_val = 0x20;
1765 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1766 *divisor = 460800 / baudRate;
1767 *clk_sel_val = 0x30;
1768 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1769 *divisor = 806400 / baudRate;
1770 *clk_sel_val = 0x40;
1771 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1772 *divisor = 921600 / baudRate;
1773 *clk_sel_val = 0x50;
1774 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1775 *divisor = 1572864 / baudRate;
1776 *clk_sel_val = 0x60;
1777 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1778 *divisor = 3145728 / baudRate;
1779 *clk_sel_val = 0x70;
1780 }
1781 return 0;
1782
1783#ifdef NOTMCS7840
1784
1785 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1786 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1787 *divisor = mos7840_divisor_table[i].Divisor;
1788 return 0;
1789 }
1790 }
1791
1792 /* After trying for all the standard baud rates *
1793 * Try calculating the divisor for this baud rate */
1794
1795 if (baudrate > 75 && baudrate < 230400) {
1796 /* get the divisor */
1797 custom = (__u16) (230400L / baudrate);
1798
1799 /* Check for round off */
1800 round1 = (__u16) (2304000L / baudrate);
1801 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001802 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001803 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001804 *divisor = custom;
1805
Tony Cook84fe6e72009-04-18 22:55:06 +09301806 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001807 return 0;
1808 }
1809
Tony Cook84fe6e72009-04-18 22:55:06 +09301810 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001811 return -1;
1812#endif
1813}
1814
1815/*****************************************************************************
1816 * mos7840_send_cmd_write_baud_rate
1817 * this function sends the proper command to change the baud rate of the
1818 * specified port.
1819 *****************************************************************************/
1820
1821static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1822 int baudRate)
1823{
1824 int divisor = 0;
1825 int status;
1826 __u16 Data;
1827 unsigned char number;
1828 __u16 clk_sel_val;
1829 struct usb_serial_port *port;
1830
1831 if (mos7840_port == NULL)
1832 return -1;
1833
1834 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001835 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301836 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001837 return -1;
1838 }
1839
Harvey Harrison441b62c2008-03-03 16:08:34 -08001840 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301841 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001842 return -1;
1843 }
1844
Tony Cook84fe6e72009-04-18 22:55:06 +09301845 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001846
1847 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1848
Harvey Harrison441b62c2008-03-03 16:08:34 -08001849 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001850 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001851 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001852 if (baudRate > 115200) {
1853#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001854 /* NOTE: need to see the pther register to modify */
1855 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001856 Data = 0x2b;
1857 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001858 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1859 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001860 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301861 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001862 return -1;
1863 }
1864#endif
1865
1866 } else {
1867#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001868 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001869 Data = 0xb;
1870 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001871 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1872 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001873 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301874 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001875 return -1;
1876 }
1877#endif
1878
1879 }
1880
Alan Cox880af9d2008-07-22 11:16:12 +01001881 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001882 clk_sel_val = 0x0;
1883 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001884 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001885 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001886 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1887 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001888 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301889 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001890 return -1;
1891 }
1892 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001893 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1894 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001895 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301896 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001897 return -1;
1898 }
1899 /* Calculate the Divisor */
1900
1901 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001902 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001903 return status;
1904 }
1905 /* Enable access to divisor latch */
1906 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1907 mos7840_port->shadowLCR = Data;
1908 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1909
1910 /* Write the divisor */
1911 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301912 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001913 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1914
1915 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301916 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001917 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1918
1919 /* Disable access to divisor latch */
1920 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1921 mos7840_port->shadowLCR = Data;
1922 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1923
1924 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001925 return status;
1926}
1927
1928/*****************************************************************************
1929 * mos7840_change_port_settings
1930 * This routine is called to set the UART on the device to match
1931 * the specified new settings.
1932 *****************************************************************************/
1933
Alan Cox95da3102008-07-22 11:09:07 +01001934static void mos7840_change_port_settings(struct tty_struct *tty,
1935 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001936{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001937 int baud;
1938 unsigned cflag;
1939 unsigned iflag;
1940 __u8 lData;
1941 __u8 lParity;
1942 __u8 lStop;
1943 int status;
1944 __u16 Data;
1945 struct usb_serial_port *port;
1946 struct usb_serial *serial;
1947
1948 if (mos7840_port == NULL)
1949 return;
1950
1951 port = (struct usb_serial_port *)mos7840_port->port;
1952
Harvey Harrison441b62c2008-03-03 16:08:34 -08001953 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301954 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001955 return;
1956 }
1957
Harvey Harrison441b62c2008-03-03 16:08:34 -08001958 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301959 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001960 return;
1961 }
1962
1963 serial = port->serial;
1964
Harvey Harrison441b62c2008-03-03 16:08:34 -08001965 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001966
1967 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001968 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001969 return;
1970 }
1971
Tony Cook84fe6e72009-04-18 22:55:06 +09301972 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001973
1974 lData = LCR_BITS_8;
1975 lStop = LCR_STOP_1;
1976 lParity = LCR_PAR_NONE;
1977
1978 cflag = tty->termios->c_cflag;
1979 iflag = tty->termios->c_iflag;
1980
1981 /* Change the number of bits */
1982 if (cflag & CSIZE) {
1983 switch (cflag & CSIZE) {
1984 case CS5:
1985 lData = LCR_BITS_5;
1986 break;
1987
1988 case CS6:
1989 lData = LCR_BITS_6;
1990 break;
1991
1992 case CS7:
1993 lData = LCR_BITS_7;
1994 break;
1995 default:
1996 case CS8:
1997 lData = LCR_BITS_8;
1998 break;
1999 }
2000 }
2001 /* Change the Parity bit */
2002 if (cflag & PARENB) {
2003 if (cflag & PARODD) {
2004 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002005 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002006 } else {
2007 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002008 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002009 }
2010
2011 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002012 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002013 }
2014
Alan Cox880af9d2008-07-22 11:16:12 +01002015 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002016 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002017
2018 /* Change the Stop bit */
2019 if (cflag & CSTOPB) {
2020 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002021 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002022 } else {
2023 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002024 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002025 }
2026
2027 /* Update the LCR with the correct value */
2028 mos7840_port->shadowLCR &=
2029 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2030 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2031
Tony Cook84fe6e72009-04-18 22:55:06 +09302032 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002033 mos7840_port->shadowLCR);
2034 /* Disable Interrupts */
2035 Data = 0x00;
2036 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2037
2038 Data = 0x00;
2039 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2040
2041 Data = 0xcf;
2042 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2043
2044 /* Send the updated LCR value to the mos7840 */
2045 Data = mos7840_port->shadowLCR;
2046
2047 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2048
2049 Data = 0x00b;
2050 mos7840_port->shadowMCR = Data;
2051 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2052 Data = 0x00b;
2053 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2054
2055 /* set up the MCR register and send it to the mos7840 */
2056
2057 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002058 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002059 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002060
Alan Cox880af9d2008-07-22 11:16:12 +01002061 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002062 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002063 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002064 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002065
2066 Data = mos7840_port->shadowMCR;
2067 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2068
2069 /* Determine divisor based on baud rate */
2070 baud = tty_get_baud_rate(tty);
2071
2072 if (!baud) {
2073 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302074 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002075 baud = 9600;
2076 }
2077
Harvey Harrison441b62c2008-03-03 16:08:34 -08002078 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002079 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2080
2081 /* Enable Interrupts */
2082 Data = 0x0c;
2083 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2084
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002085 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002086 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002087 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002088 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002089 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002090 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002091 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002092 }
2093 }
2094 wake_up(&mos7840_port->delta_msr_wait);
2095 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302096 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002097 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002098}
2099
2100/*****************************************************************************
2101 * mos7840_set_termios
2102 * this function is called by the tty driver when it wants to change
2103 * the termios structure
2104 *****************************************************************************/
2105
Alan Cox95da3102008-07-22 11:09:07 +01002106static void mos7840_set_termios(struct tty_struct *tty,
2107 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002108 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002109{
2110 int status;
2111 unsigned int cflag;
2112 struct usb_serial *serial;
2113 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302114 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002115 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302116 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002117 return;
2118 }
2119
2120 serial = port->serial;
2121
Harvey Harrison441b62c2008-03-03 16:08:34 -08002122 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302123 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002124 return;
2125 }
2126
2127 mos7840_port = mos7840_get_port_private(port);
2128
2129 if (mos7840_port == NULL)
2130 return;
2131
Paul B Schroeder3f542972006-08-31 19:41:47 -05002132 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002133 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002134 return;
2135 }
2136
Tony Cook84fe6e72009-04-18 22:55:06 +09302137 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002138
2139 cflag = tty->termios->c_cflag;
2140
Harvey Harrison441b62c2008-03-03 16:08:34 -08002141 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002142 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002143 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002144 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002145 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002146
2147 /* change the port settings to the new ones specified */
2148
Alan Cox95da3102008-07-22 11:09:07 +01002149 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002150
2151 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302152 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002153 return;
2154 }
2155
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002156 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002157 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002158 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2159 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002160 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002161 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002162 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002163 }
2164 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002165}
2166
2167/*****************************************************************************
2168 * mos7840_get_lsr_info - get line status register info
2169 *
2170 * Purpose: Let user call ioctl() to get info when the UART physically
2171 * is emptied. On bus types like RS485, the transmitter must
2172 * release the bus after transmitting. This must be done when
2173 * the transmit shift register is empty, not be done when the
2174 * transmit holding register is empty. This functionality
2175 * allows an RS485 driver to be written in user space.
2176 *****************************************************************************/
2177
Alan Cox95da3102008-07-22 11:09:07 +01002178static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002179 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002180{
2181 int count;
2182 unsigned int result = 0;
2183
Alan Cox95da3102008-07-22 11:09:07 +01002184 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002185 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002186 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002187 result = TIOCSER_TEMT;
2188 }
2189
2190 if (copy_to_user(value, &result, sizeof(int)))
2191 return -EFAULT;
2192 return 0;
2193}
2194
2195/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002196 * mos7840_get_serial_info
2197 * function to get information about serial port
2198 *****************************************************************************/
2199
2200static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002201 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002202{
2203 struct serial_struct tmp;
2204
2205 if (mos7840_port == NULL)
2206 return -1;
2207
2208 if (!retinfo)
2209 return -EFAULT;
2210
2211 memset(&tmp, 0, sizeof(tmp));
2212
2213 tmp.type = PORT_16550A;
2214 tmp.line = mos7840_port->port->serial->minor;
2215 tmp.port = mos7840_port->port->number;
2216 tmp.irq = 0;
2217 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2218 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2219 tmp.baud_base = 9600;
2220 tmp.close_delay = 5 * HZ;
2221 tmp.closing_wait = 30 * HZ;
2222
2223 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2224 return -EFAULT;
2225 return 0;
2226}
2227
Alan Cox0bca1b92010-09-16 18:21:40 +01002228static int mos7840_get_icount(struct tty_struct *tty,
2229 struct serial_icounter_struct *icount)
2230{
2231 struct usb_serial_port *port = tty->driver_data;
2232 struct moschip_port *mos7840_port;
2233 struct async_icount cnow;
2234
2235 mos7840_port = mos7840_get_port_private(port);
2236 cnow = mos7840_port->icount;
2237
2238 smp_rmb();
2239 icount->cts = cnow.cts;
2240 icount->dsr = cnow.dsr;
2241 icount->rng = cnow.rng;
2242 icount->dcd = cnow.dcd;
2243 icount->rx = cnow.rx;
2244 icount->tx = cnow.tx;
2245 icount->frame = cnow.frame;
2246 icount->overrun = cnow.overrun;
2247 icount->parity = cnow.parity;
2248 icount->brk = cnow.brk;
2249 icount->buf_overrun = cnow.buf_overrun;
2250
2251 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2252 port->number, icount->rx, icount->tx);
2253 return 0;
2254}
2255
Paul B Schroeder3f542972006-08-31 19:41:47 -05002256/*****************************************************************************
2257 * SerialIoctl
2258 * this function handles any ioctl calls to the driver
2259 *****************************************************************************/
2260
Alan Cox00a0d0d2011-02-14 16:27:06 +00002261static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002262 unsigned int cmd, unsigned long arg)
2263{
Alan Cox95da3102008-07-22 11:09:07 +01002264 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002265 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002266 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002267
2268 struct async_icount cnow;
2269 struct async_icount cprev;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002270
Harvey Harrison441b62c2008-03-03 16:08:34 -08002271 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302272 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002273 return -1;
2274 }
2275
2276 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002277
2278 if (mos7840_port == NULL)
2279 return -1;
2280
Harvey Harrison441b62c2008-03-03 16:08:34 -08002281 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002282
2283 switch (cmd) {
2284 /* return number of bytes available */
2285
Paul B Schroeder3f542972006-08-31 19:41:47 -05002286 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002287 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002288 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002289
Paul B Schroeder3f542972006-08-31 19:41:47 -05002290 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002291 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002292 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002293
2294 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002295 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002296 break;
2297
2298 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002299 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002300 cprev = mos7840_port->icount;
2301 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002302 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002303 mos7840_port->delta_msr_cond = 0;
2304 wait_event_interruptible(mos7840_port->delta_msr_wait,
2305 (mos7840_port->
2306 delta_msr_cond == 1));
2307
2308 /* see if a signal did it */
2309 if (signal_pending(current))
2310 return -ERESTARTSYS;
2311 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002312 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002313 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2314 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2315 return -EIO; /* no change => error */
2316 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2317 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2318 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2319 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2320 return 0;
2321 }
2322 cprev = cnow;
2323 }
2324 /* NOTREACHED */
2325 break;
2326
Paul B Schroeder3f542972006-08-31 19:41:47 -05002327 default:
2328 break;
2329 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002330 return -ENOIOCTLCMD;
2331}
2332
2333static int mos7840_calc_num_ports(struct usb_serial *serial)
2334{
Donald Lee093ea2d2012-03-14 15:26:33 +08002335 __u16 Data = 0x00;
2336 int ret = 0;
2337 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002338
Donald Lee093ea2d2012-03-14 15:26:33 +08002339 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2340 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &Data,
2341 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2342
2343 if ((Data & 0x01) == 0) {
2344 mos7840_num_ports = 2;
2345 serial->num_bulk_in = 2;
2346 serial->num_bulk_out = 2;
2347 serial->num_ports = 2;
2348 } else {
2349 mos7840_num_ports = 4;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002350 serial->num_bulk_in = 4;
2351 serial->num_bulk_out = 4;
Donald Lee093ea2d2012-03-14 15:26:33 +08002352 serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002353 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002354
Paul B Schroeder3f542972006-08-31 19:41:47 -05002355 return mos7840_num_ports;
2356}
2357
2358/****************************************************************************
2359 * mos7840_startup
2360 ****************************************************************************/
2361
2362static int mos7840_startup(struct usb_serial *serial)
2363{
2364 struct moschip_port *mos7840_port;
2365 struct usb_device *dev;
2366 int i, status;
2367
2368 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302369 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002370
2371 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302372 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002373 return -1;
2374 }
2375
2376 dev = serial->dev;
2377
Tony Cook84fe6e72009-04-18 22:55:06 +09302378 dbg("%s", "Entering...");
2379 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002380
2381 /* we set up the pointers to the endpoints in the mos7840_open *
2382 * function, as the structures aren't created yet. */
2383
2384 /* set up port private structures */
2385 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302386 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002387 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002388 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002389 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002390 status = -ENOMEM;
2391 i--; /* don't follow NULL pointer cleaning up */
2392 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002393 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002394
Alan Cox880af9d2008-07-22 11:16:12 +01002395 /* Initialize all port interrupt end point to port 0 int
2396 * endpoint. Our device has only one interrupt end point
2397 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002398
2399 mos7840_port->port = serial->port[i];
2400 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002401 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002402
Tony Cook37768ad2009-04-18 22:42:18 +09302403 /* minor is not initialised until later by
2404 * usb-serial.c:get_free_serial() and cannot therefore be used
2405 * to index device instances */
2406 mos7840_port->port_num = i + 1;
2407 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2408 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2409 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2410 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002411
2412 if (mos7840_port->port_num == 1) {
2413 mos7840_port->SpRegOffset = 0x0;
2414 mos7840_port->ControlRegOffset = 0x1;
2415 mos7840_port->DcrRegOffset = 0x4;
2416 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002417 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002418 mos7840_port->SpRegOffset = 0x8;
2419 mos7840_port->ControlRegOffset = 0x9;
2420 mos7840_port->DcrRegOffset = 0x16;
2421 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002422 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002423 mos7840_port->SpRegOffset = 0xa;
2424 mos7840_port->ControlRegOffset = 0xb;
2425 mos7840_port->DcrRegOffset = 0x19;
2426 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002427 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002428 mos7840_port->SpRegOffset = 0xa;
2429 mos7840_port->ControlRegOffset = 0xb;
2430 mos7840_port->DcrRegOffset = 0x19;
2431 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002432 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002433 mos7840_port->SpRegOffset = 0xc;
2434 mos7840_port->ControlRegOffset = 0xd;
2435 mos7840_port->DcrRegOffset = 0x1c;
2436 }
2437 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002438 mos7840_set_port_private(serial->port[i], mos7840_port);
2439
Alan Cox880af9d2008-07-22 11:16:12 +01002440 /* enable rx_disable bit in control register */
2441 status = mos7840_get_reg_sync(serial->port[i],
2442 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002443 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302444 dbg("Reading ControlReg failed 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 Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002448 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002449 Data |= 0x08; /* setting driver done bit */
2450 Data |= 0x04; /* sp1_bit to have cts change reflect in
2451 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002452
Alan Cox880af9d2008-07-22 11:16:12 +01002453 /* Data |= 0x20; //rx_disable bit */
2454 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002455 mos7840_port->ControlRegOffset, Data);
2456 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302457 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002458 break;
2459 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302460 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002461 status);
2462
Alan Cox880af9d2008-07-22 11:16:12 +01002463 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2464 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002465 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002466 status = mos7840_set_reg_sync(serial->port[i],
2467 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002468 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302469 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002470 break;
2471 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302472 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002473
2474 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002475 status = mos7840_set_reg_sync(serial->port[i],
2476 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002477 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302478 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002479 break;
2480 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302481 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002482
2483 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002484 status = mos7840_set_reg_sync(serial->port[i],
2485 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002486 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302487 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002488 break;
2489 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302490 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002491
Alan Cox880af9d2008-07-22 11:16:12 +01002492 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002493 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002494 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002495 CLK_START_VALUE_REGISTER, Data);
2496 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302497 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002498 break;
2499 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302500 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002501
2502 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002503 status = mos7840_set_reg_sync(serial->port[i],
2504 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002505 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302506 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002507 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002508 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002509 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302510 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002511 status);
2512
Alan Cox880af9d2008-07-22 11:16:12 +01002513 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002514 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002515 status = mos7840_set_uart_reg(serial->port[i],
2516 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002517 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302518 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002519 status);
2520 break;
2521 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302522 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002523 status);
2524
Alan Cox880af9d2008-07-22 11:16:12 +01002525 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002526 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002527 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002528
2529 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002530 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002531 (__u16) (ZLP_REG1 +
2532 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302533 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002534 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002535 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002536 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302537 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002538 i + 2, status);
2539 break;
2540 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302541 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002542 i + 2, status);
2543 } else {
2544 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002545 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002546 (__u16) (ZLP_REG1 +
2547 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302548 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002549 (__u16) (ZLP_REG1 +
2550 ((__u16) mos7840_port->port_num) - 0x1));
2551 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302552 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002553 i + 1, status);
2554 break;
2555 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302556 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002557 i + 1, status);
2558
2559 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002560 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002561 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002562 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2563 GFP_KERNEL);
2564 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2565 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002566 status = -ENOMEM;
2567 goto error;
2568 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002569 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302570 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002571
Alan Cox880af9d2008-07-22 11:16:12 +01002572 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002573 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002574 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2575 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302576 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002577 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002578 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302579 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002580
2581 /* setting configuration feature to one */
2582 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002583 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002584 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002585error:
2586 for (/* nothing */; i >= 0; i--) {
2587 mos7840_port = mos7840_get_port_private(serial->port[i]);
2588
2589 kfree(mos7840_port->dr);
2590 kfree(mos7840_port->ctrl_buf);
2591 usb_free_urb(mos7840_port->control_urb);
2592 kfree(mos7840_port);
2593 serial->port[i] = NULL;
2594 }
2595 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002596}
2597
2598/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002599 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002600 * This function is called whenever the device is removed from the usb bus.
2601 ****************************************************************************/
2602
Alan Sternf9c99bb2009-06-02 11:53:55 -04002603static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002604{
2605 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002606 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002607 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002608 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002609
2610 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302611 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002612 return;
2613 }
2614
Alan Cox880af9d2008-07-22 11:16:12 +01002615 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002616
2617 /* free private structure allocated for serial port *
2618 * stop reads and writes on all ports */
2619
2620 for (i = 0; i < serial->num_ports; ++i) {
2621 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302622 dbg ("mos7840_port %d = %p", i, mos7840_port);
2623 if (mos7840_port) {
2624 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2625 mos7840_port->zombie = 1;
2626 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2627 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002628 }
2629 }
2630
2631 dbg("%s", "Thank u :: ");
2632
2633}
2634
2635/****************************************************************************
2636 * mos7840_release
2637 * This function is called when the usb_serial structure is freed.
2638 ****************************************************************************/
2639
2640static void mos7840_release(struct usb_serial *serial)
2641{
2642 int i;
2643 struct moschip_port *mos7840_port;
2644 dbg("%s", " release :entering..........");
2645
2646 if (!serial) {
2647 dbg("%s", "Invalid Handler");
2648 return;
2649 }
2650
2651 /* check for the ports to be closed,close the ports and disconnect */
2652
2653 /* free private structure allocated for serial port *
2654 * stop reads and writes on all ports */
2655
2656 for (i = 0; i < serial->num_ports; ++i) {
2657 mos7840_port = mos7840_get_port_private(serial->port[i]);
2658 dbg("mos7840_port %d = %p", i, mos7840_port);
2659 if (mos7840_port) {
Tony Cook37768ad2009-04-18 22:42:18 +09302660 kfree(mos7840_port->ctrl_buf);
2661 kfree(mos7840_port->dr);
2662 kfree(mos7840_port);
2663 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002664 }
2665
Tony Cook84fe6e72009-04-18 22:55:06 +09302666 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002667
2668}
2669
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002670static struct usb_driver io_driver = {
2671 .name = "mos7840",
2672 .probe = usb_serial_probe,
2673 .disconnect = usb_serial_disconnect,
2674 .id_table = moschip_id_table_combined,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002675};
2676
Paul B Schroeder3f542972006-08-31 19:41:47 -05002677static struct usb_serial_driver moschip7840_4port_device = {
2678 .driver = {
2679 .owner = THIS_MODULE,
2680 .name = "mos7840",
2681 },
2682 .description = DRIVER_DESC,
2683 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002684 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002685 .open = mos7840_open,
2686 .close = mos7840_close,
2687 .write = mos7840_write,
2688 .write_room = mos7840_write_room,
2689 .chars_in_buffer = mos7840_chars_in_buffer,
2690 .throttle = mos7840_throttle,
2691 .unthrottle = mos7840_unthrottle,
2692 .calc_num_ports = mos7840_calc_num_ports,
2693#ifdef MCSSerialProbe
2694 .probe = mos7840_serial_probe,
2695#endif
2696 .ioctl = mos7840_ioctl,
2697 .set_termios = mos7840_set_termios,
2698 .break_ctl = mos7840_break,
2699 .tiocmget = mos7840_tiocmget,
2700 .tiocmset = mos7840_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002701 .get_icount = mos7840_get_icount,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002702 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002703 .disconnect = mos7840_disconnect,
2704 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002705 .read_bulk_callback = mos7840_bulk_in_callback,
2706 .read_int_callback = mos7840_interrupt_callback,
2707};
2708
Alan Stern4d2a7af2012-02-23 14:57:09 -05002709static struct usb_serial_driver * const serial_drivers[] = {
2710 &moschip7840_4port_device, NULL
2711};
2712
Greg Kroah-Hartmane7414d92012-02-28 13:12:10 -08002713module_usb_serial_driver(io_driver, serial_drivers);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002714
Paul B Schroeder3f542972006-08-31 19:41:47 -05002715MODULE_DESCRIPTION(DRIVER_DESC);
2716MODULE_LICENSE("GPL");
2717
2718module_param(debug, bool, S_IRUGO | S_IWUSR);
2719MODULE_PARM_DESC(debug, "Debug enabled or not");