blob: c526550694a06270acf60b056274ec764a75e5bc [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
Németh Márton7d40d7e2010-01-10 15:34:24 +0100209static const struct usb_device_id moschip_id_table_combined[] __devinitconst = {
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);
Alan Cox95da3102008-07-22 11:09:07 +01001192 for (i = 0; i < NUM_URBS; ++i)
1193 if (mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001194 chars += URB_TRANSFER_BUFFER_SIZE;
Alan Cox880af9d2008-07-22 11:16:12 +01001195 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001196 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001197 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001198
1199}
1200
Paul B Schroeder3f542972006-08-31 19:41:47 -05001201/*****************************************************************************
1202 * mos7840_close
1203 * this function is called by the tty driver when a port is closed
1204 *****************************************************************************/
1205
Alan Cox335f8512009-06-11 12:26:29 +01001206static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001207{
1208 struct usb_serial *serial;
1209 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001210 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001211 int j;
1212 __u16 Data;
1213
Tony Cook84fe6e72009-04-18 22:55:06 +09301214 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001215
Harvey Harrison441b62c2008-03-03 16:08:34 -08001216 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301217 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001218 return;
1219 }
1220
Harvey Harrison441b62c2008-03-03 16:08:34 -08001221 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001222 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301223 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001224 return;
1225 }
1226
1227 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001228 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001229
Oliver Neukum0de9a702007-03-16 20:28:28 +01001230 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001231 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001232
1233 for (j = 0; j < NUM_URBS; ++j)
1234 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1235
1236 /* Freeing Write URBs */
1237 for (j = 0; j < NUM_URBS; ++j) {
1238 if (mos7840_port->write_urb_pool[j]) {
1239 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1240 kfree(mos7840_port->write_urb_pool[j]->
1241 transfer_buffer);
1242
1243 usb_free_urb(mos7840_port->write_urb_pool[j]);
1244 }
1245 }
1246
Paul B Schroeder3f542972006-08-31 19:41:47 -05001247 /* While closing port, shutdown all bulk read, write *
1248 * and interrupt read if they exists */
1249 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001250 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301251 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001252 usb_kill_urb(mos7840_port->write_urb);
1253 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001254 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301255 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001256 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001257 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001258 }
1259 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301260 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001261 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001262 }
1263 }
Alan Cox880af9d2008-07-22 11:16:12 +01001264/* if(mos7840_port->ctrl_buf != NULL) */
1265/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001266 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301267 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001268 port0->open_ports, port->number);
1269 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001270 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301271 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001272 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001273 }
1274 }
1275
1276 if (mos7840_port->write_urb) {
1277 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001278 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001279 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001280 usb_free_urb(mos7840_port->write_urb);
1281 }
1282
1283 Data = 0x0;
1284 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1285
1286 Data = 0x00;
1287 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1288
1289 mos7840_port->open = 0;
1290
Tony Cook84fe6e72009-04-18 22:55:06 +09301291 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001292}
1293
1294/************************************************************************
1295 *
1296 * mos7840_block_until_chase_response
1297 *
1298 * This function will block the close until one of the following:
1299 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001300 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001301 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1302 *
1303 ************************************************************************/
1304
Alan Cox95da3102008-07-22 11:09:07 +01001305static void mos7840_block_until_chase_response(struct tty_struct *tty,
1306 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001307{
1308 int timeout = 1 * HZ;
1309 int wait = 10;
1310 int count;
1311
1312 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001313 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001314
1315 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001316 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001317 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001318
1319 /* Block the thread for a while */
1320 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1321 timeout);
1322 /* No activity.. count down section */
1323 wait--;
1324 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001325 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001326 return;
1327 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001328 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001329 wait = 10;
1330 }
1331 }
1332
1333}
1334
1335/*****************************************************************************
1336 * mos7840_break
1337 * this function sends a break to the port
1338 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001339static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001340{
Alan Cox95da3102008-07-22 11:09:07 +01001341 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001342 unsigned char data;
1343 struct usb_serial *serial;
1344 struct moschip_port *mos7840_port;
1345
Tony Cook84fe6e72009-04-18 22:55:06 +09301346 dbg("%s", "Entering ...........");
1347 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001348
Harvey Harrison441b62c2008-03-03 16:08:34 -08001349 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301350 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001351 return;
1352 }
1353
Harvey Harrison441b62c2008-03-03 16:08:34 -08001354 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001355 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301356 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001357 return;
1358 }
1359
1360 mos7840_port = mos7840_get_port_private(port);
1361
Alan Cox880af9d2008-07-22 11:16:12 +01001362 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001363 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001364
Alan Cox95da3102008-07-22 11:09:07 +01001365 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001366 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001367 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001368
Alan Cox95da3102008-07-22 11:09:07 +01001369 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001370 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001371 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001372 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001373
Alan Cox6b447f042009-01-02 13:48:56 +00001374 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001375 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301376 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001377 mos7840_port->shadowLCR);
1378 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1379 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001380}
1381
1382/*****************************************************************************
1383 * mos7840_write_room
1384 * this function is called by the tty driver when it wants to know how many
1385 * bytes of data we can accept for a specific port.
1386 * If successful, we return the amount of room that we have for this port
1387 * Otherwise we return a negative error number.
1388 *****************************************************************************/
1389
Alan Cox95da3102008-07-22 11:09:07 +01001390static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001391{
Alan Cox95da3102008-07-22 11:09:07 +01001392 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001393 int i;
1394 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001395 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001396 struct moschip_port *mos7840_port;
1397
Tony Cook84fe6e72009-04-18 22:55:06 +09301398 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001399
Harvey Harrison441b62c2008-03-03 16:08:34 -08001400 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301401 dbg("%s", "Invalid port");
1402 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001403 return -1;
1404 }
1405
1406 mos7840_port = mos7840_get_port_private(port);
1407 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301408 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001409 return -1;
1410 }
1411
Oliver Neukum0de9a702007-03-16 20:28:28 +01001412 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001413 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001414 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001415 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001416 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001417 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001418
Oliver Neukum0de9a702007-03-16 20:28:28 +01001419 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001420 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001421 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001422
1423}
1424
1425/*****************************************************************************
1426 * mos7840_write
1427 * this function is called by the tty driver when data should be written to
1428 * the port.
1429 * If successful, we return the number of bytes written, otherwise we
1430 * return a negative error number.
1431 *****************************************************************************/
1432
Alan Cox95da3102008-07-22 11:09:07 +01001433static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001434 const unsigned char *data, int count)
1435{
1436 int status;
1437 int i;
1438 int bytes_sent = 0;
1439 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001440 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001441
1442 struct moschip_port *mos7840_port;
1443 struct usb_serial *serial;
1444 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001445 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001446 const unsigned char *current_position = data;
1447 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301448 dbg("%s", "entering ...........");
1449 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001450 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001451
1452#ifdef NOTMOS7840
1453 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001454 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1455 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301456 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1457 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001458 mos7840_port->shadowLCR);
1459
Alan Cox880af9d2008-07-22 11:16:12 +01001460 /* Data = 0x03; */
1461 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1462 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001463
Alan Cox880af9d2008-07-22 11:16:12 +01001464 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001465 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1466
Alan Cox880af9d2008-07-22 11:16:12 +01001467 /* Data = 0x0c; */
1468 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001469 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001470 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301471 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001472
1473 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001474 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301475 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001476
1477 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301478 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001479 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001480 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1481#endif
1482
Harvey Harrison441b62c2008-03-03 16:08:34 -08001483 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301484 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001485 return -1;
1486 }
1487
1488 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001489 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301490 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001491 return -1;
1492 }
1493
1494 mos7840_port = mos7840_get_port_private(port);
1495 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301496 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001497 return -1;
1498 }
1499
1500 /* try to find a free urb in the list */
1501 urb = NULL;
1502
Oliver Neukum0de9a702007-03-16 20:28:28 +01001503 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001504 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001505 if (!mos7840_port->busy[i]) {
1506 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001507 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301508 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001509 break;
1510 }
1511 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001512 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001513
1514 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001515 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001516 goto exit;
1517 }
1518
1519 if (urb->transfer_buffer == NULL) {
1520 urb->transfer_buffer =
1521 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1522
1523 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001524 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001525 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001526 goto exit;
1527 }
1528 }
1529 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1530
Al Viro97c49652006-10-09 20:29:03 +01001531 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001532
1533 /* fill urb with data and submit */
Donald Lee093ea2d2012-03-14 15:26:33 +08001534 if ((serial->num_ports == 2)
1535 && ((((__u16)port->number -
1536 (__u16)(port->serial->minor)) % 2) != 0)) {
1537 usb_fill_bulk_urb(urb,
1538 serial->dev,
1539 usb_sndbulkpipe(serial->dev,
1540 (port->bulk_out_endpointAddress) + 2),
1541 urb->transfer_buffer,
1542 transfer_size,
1543 mos7840_bulk_out_data_callback, mos7840_port);
1544 } else {
1545 usb_fill_bulk_urb(urb,
1546 serial->dev,
1547 usb_sndbulkpipe(serial->dev,
1548 port->bulk_out_endpointAddress),
1549 urb->transfer_buffer,
1550 transfer_size,
1551 mos7840_bulk_out_data_callback, mos7840_port);
1552 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001553
1554 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301555 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001556
1557 /* send it down the pipe */
1558 status = usb_submit_urb(urb, GFP_ATOMIC);
1559
1560 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001561 mos7840_port->busy[i] = 0;
Johan Hovold22a416c2012-02-10 13:20:51 +01001562 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001563 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001564 bytes_sent = status;
1565 goto exit;
1566 }
1567 bytes_sent = transfer_size;
1568 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001569 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301570 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001571exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001572 return bytes_sent;
1573
1574}
1575
1576/*****************************************************************************
1577 * mos7840_throttle
1578 * this function is called by the tty driver when it wants to stop the data
1579 * being read from the port.
1580 *****************************************************************************/
1581
Alan Cox95da3102008-07-22 11:09:07 +01001582static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001583{
Alan Cox95da3102008-07-22 11:09:07 +01001584 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001585 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001586 int status;
1587
Harvey Harrison441b62c2008-03-03 16:08:34 -08001588 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301589 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001590 return;
1591 }
1592
Tony Cook84fe6e72009-04-18 22:55:06 +09301593 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001594
1595 mos7840_port = mos7840_get_port_private(port);
1596
1597 if (mos7840_port == NULL)
1598 return;
1599
1600 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301601 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001602 return;
1603 }
1604
Tony Cook84fe6e72009-04-18 22:55:06 +09301605 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001606
Paul B Schroeder3f542972006-08-31 19:41:47 -05001607 /* if we are implementing XON/XOFF, send the stop character */
1608 if (I_IXOFF(tty)) {
1609 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001610 status = mos7840_write(tty, port, &stop_char, 1);
1611 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001612 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001613 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001614 /* if we are implementing RTS/CTS, toggle that line */
1615 if (tty->termios->c_cflag & CRTSCTS) {
1616 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001617 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001618 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001619 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001620 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001621 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001622}
1623
1624/*****************************************************************************
1625 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001626 * this function is called by the tty driver when it wants to resume
1627 * the data being read from the port (called after mos7840_throttle is
1628 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001629 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001630static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001631{
Alan Cox95da3102008-07-22 11:09:07 +01001632 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001633 int status;
1634 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1635
Harvey Harrison441b62c2008-03-03 16:08:34 -08001636 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301637 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001638 return;
1639 }
1640
1641 if (mos7840_port == NULL)
1642 return;
1643
1644 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001645 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001646 return;
1647 }
1648
Tony Cook84fe6e72009-04-18 22:55:06 +09301649 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001650
Paul B Schroeder3f542972006-08-31 19:41:47 -05001651 /* if we are implementing XON/XOFF, send the start character */
1652 if (I_IXOFF(tty)) {
1653 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001654 status = mos7840_write(tty, port, &start_char, 1);
1655 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001656 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001657 }
1658
1659 /* if we are implementing RTS/CTS, toggle that line */
1660 if (tty->termios->c_cflag & CRTSCTS) {
1661 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001662 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001663 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001664 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001665 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001666 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001667}
1668
Alan Cox60b33c12011-02-14 16:26:14 +00001669static int mos7840_tiocmget(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001670{
Alan Cox95da3102008-07-22 11:09:07 +01001671 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001672 struct moschip_port *mos7840_port;
1673 unsigned int result;
1674 __u16 msr;
1675 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001676 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001677 mos7840_port = mos7840_get_port_private(port);
1678
Harvey Harrison441b62c2008-03-03 16:08:34 -08001679 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001680
1681 if (mos7840_port == NULL)
1682 return -ENODEV;
1683
1684 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1685 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1686 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1687 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1688 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1689 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1690 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1691 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1692 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1693
Harvey Harrison441b62c2008-03-03 16:08:34 -08001694 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001695
1696 return result;
1697}
1698
Alan Cox20b9d172011-02-14 16:26:50 +00001699static int mos7840_tiocmset(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001700 unsigned int set, unsigned int clear)
1701{
Alan Cox95da3102008-07-22 11:09:07 +01001702 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001703 struct moschip_port *mos7840_port;
1704 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001705 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001706
Harvey Harrison441b62c2008-03-03 16:08:34 -08001707 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001708
1709 mos7840_port = mos7840_get_port_private(port);
1710
1711 if (mos7840_port == NULL)
1712 return -ENODEV;
1713
Alan Coxe2984492008-02-20 20:51:45 +00001714 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001715 mcr = mos7840_port->shadowMCR;
1716 if (clear & TIOCM_RTS)
1717 mcr &= ~MCR_RTS;
1718 if (clear & TIOCM_DTR)
1719 mcr &= ~MCR_DTR;
1720 if (clear & TIOCM_LOOP)
1721 mcr &= ~MCR_LOOPBACK;
1722
1723 if (set & TIOCM_RTS)
1724 mcr |= MCR_RTS;
1725 if (set & TIOCM_DTR)
1726 mcr |= MCR_DTR;
1727 if (set & TIOCM_LOOP)
1728 mcr |= MCR_LOOPBACK;
1729
1730 mos7840_port->shadowMCR = mcr;
1731
Paul B Schroeder3f542972006-08-31 19:41:47 -05001732 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1733 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301734 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001735 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001736 }
1737
1738 return 0;
1739}
1740
1741/*****************************************************************************
1742 * mos7840_calc_baud_rate_divisor
1743 * this function calculates the proper baud rate divisor for the specified
1744 * baud rate.
1745 *****************************************************************************/
1746static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001747 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001748{
1749
Harvey Harrison441b62c2008-03-03 16:08:34 -08001750 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001751
1752 if (baudRate <= 115200) {
1753 *divisor = 115200 / baudRate;
1754 *clk_sel_val = 0x0;
1755 }
1756 if ((baudRate > 115200) && (baudRate <= 230400)) {
1757 *divisor = 230400 / baudRate;
1758 *clk_sel_val = 0x10;
1759 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1760 *divisor = 403200 / baudRate;
1761 *clk_sel_val = 0x20;
1762 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1763 *divisor = 460800 / baudRate;
1764 *clk_sel_val = 0x30;
1765 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1766 *divisor = 806400 / baudRate;
1767 *clk_sel_val = 0x40;
1768 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1769 *divisor = 921600 / baudRate;
1770 *clk_sel_val = 0x50;
1771 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1772 *divisor = 1572864 / baudRate;
1773 *clk_sel_val = 0x60;
1774 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1775 *divisor = 3145728 / baudRate;
1776 *clk_sel_val = 0x70;
1777 }
1778 return 0;
1779
1780#ifdef NOTMCS7840
1781
1782 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1783 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1784 *divisor = mos7840_divisor_table[i].Divisor;
1785 return 0;
1786 }
1787 }
1788
1789 /* After trying for all the standard baud rates *
1790 * Try calculating the divisor for this baud rate */
1791
1792 if (baudrate > 75 && baudrate < 230400) {
1793 /* get the divisor */
1794 custom = (__u16) (230400L / baudrate);
1795
1796 /* Check for round off */
1797 round1 = (__u16) (2304000L / baudrate);
1798 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001799 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001800 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001801 *divisor = custom;
1802
Tony Cook84fe6e72009-04-18 22:55:06 +09301803 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001804 return 0;
1805 }
1806
Tony Cook84fe6e72009-04-18 22:55:06 +09301807 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001808 return -1;
1809#endif
1810}
1811
1812/*****************************************************************************
1813 * mos7840_send_cmd_write_baud_rate
1814 * this function sends the proper command to change the baud rate of the
1815 * specified port.
1816 *****************************************************************************/
1817
1818static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1819 int baudRate)
1820{
1821 int divisor = 0;
1822 int status;
1823 __u16 Data;
1824 unsigned char number;
1825 __u16 clk_sel_val;
1826 struct usb_serial_port *port;
1827
1828 if (mos7840_port == NULL)
1829 return -1;
1830
1831 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001832 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301833 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001834 return -1;
1835 }
1836
Harvey Harrison441b62c2008-03-03 16:08:34 -08001837 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301838 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001839 return -1;
1840 }
1841
Tony Cook84fe6e72009-04-18 22:55:06 +09301842 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001843
1844 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1845
Harvey Harrison441b62c2008-03-03 16:08:34 -08001846 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001847 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001848 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001849 if (baudRate > 115200) {
1850#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001851 /* NOTE: need to see the pther register to modify */
1852 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001853 Data = 0x2b;
1854 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001855 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1856 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001857 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301858 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001859 return -1;
1860 }
1861#endif
1862
1863 } else {
1864#ifdef HW_flow_control
Donald Lee093ea2d2012-03-14 15:26:33 +08001865 /* setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001866 Data = 0xb;
1867 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001868 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1869 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001870 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301871 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001872 return -1;
1873 }
1874#endif
1875
1876 }
1877
Alan Cox880af9d2008-07-22 11:16:12 +01001878 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001879 clk_sel_val = 0x0;
1880 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001881 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001882 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001883 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1884 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001885 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301886 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001887 return -1;
1888 }
1889 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001890 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1891 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001892 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301893 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001894 return -1;
1895 }
1896 /* Calculate the Divisor */
1897
1898 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001899 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001900 return status;
1901 }
1902 /* Enable access to divisor latch */
1903 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1904 mos7840_port->shadowLCR = Data;
1905 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1906
1907 /* Write the divisor */
1908 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301909 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001910 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1911
1912 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301913 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001914 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1915
1916 /* Disable access to divisor latch */
1917 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1918 mos7840_port->shadowLCR = Data;
1919 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1920
1921 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001922 return status;
1923}
1924
1925/*****************************************************************************
1926 * mos7840_change_port_settings
1927 * This routine is called to set the UART on the device to match
1928 * the specified new settings.
1929 *****************************************************************************/
1930
Alan Cox95da3102008-07-22 11:09:07 +01001931static void mos7840_change_port_settings(struct tty_struct *tty,
1932 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001933{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001934 int baud;
1935 unsigned cflag;
1936 unsigned iflag;
1937 __u8 lData;
1938 __u8 lParity;
1939 __u8 lStop;
1940 int status;
1941 __u16 Data;
1942 struct usb_serial_port *port;
1943 struct usb_serial *serial;
1944
1945 if (mos7840_port == NULL)
1946 return;
1947
1948 port = (struct usb_serial_port *)mos7840_port->port;
1949
Harvey Harrison441b62c2008-03-03 16:08:34 -08001950 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301951 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001952 return;
1953 }
1954
Harvey Harrison441b62c2008-03-03 16:08:34 -08001955 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301956 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001957 return;
1958 }
1959
1960 serial = port->serial;
1961
Harvey Harrison441b62c2008-03-03 16:08:34 -08001962 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001963
1964 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001965 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001966 return;
1967 }
1968
Tony Cook84fe6e72009-04-18 22:55:06 +09301969 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001970
1971 lData = LCR_BITS_8;
1972 lStop = LCR_STOP_1;
1973 lParity = LCR_PAR_NONE;
1974
1975 cflag = tty->termios->c_cflag;
1976 iflag = tty->termios->c_iflag;
1977
1978 /* Change the number of bits */
1979 if (cflag & CSIZE) {
1980 switch (cflag & CSIZE) {
1981 case CS5:
1982 lData = LCR_BITS_5;
1983 break;
1984
1985 case CS6:
1986 lData = LCR_BITS_6;
1987 break;
1988
1989 case CS7:
1990 lData = LCR_BITS_7;
1991 break;
1992 default:
1993 case CS8:
1994 lData = LCR_BITS_8;
1995 break;
1996 }
1997 }
1998 /* Change the Parity bit */
1999 if (cflag & PARENB) {
2000 if (cflag & PARODD) {
2001 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002002 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002003 } else {
2004 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002005 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002006 }
2007
2008 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002009 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002010 }
2011
Alan Cox880af9d2008-07-22 11:16:12 +01002012 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002013 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002014
2015 /* Change the Stop bit */
2016 if (cflag & CSTOPB) {
2017 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002018 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002019 } else {
2020 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08002021 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002022 }
2023
2024 /* Update the LCR with the correct value */
2025 mos7840_port->shadowLCR &=
2026 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2027 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2028
Tony Cook84fe6e72009-04-18 22:55:06 +09302029 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002030 mos7840_port->shadowLCR);
2031 /* Disable Interrupts */
2032 Data = 0x00;
2033 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2034
2035 Data = 0x00;
2036 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2037
2038 Data = 0xcf;
2039 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2040
2041 /* Send the updated LCR value to the mos7840 */
2042 Data = mos7840_port->shadowLCR;
2043
2044 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2045
2046 Data = 0x00b;
2047 mos7840_port->shadowMCR = Data;
2048 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2049 Data = 0x00b;
2050 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2051
2052 /* set up the MCR register and send it to the mos7840 */
2053
2054 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002055 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002056 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002057
Alan Cox880af9d2008-07-22 11:16:12 +01002058 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002059 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002060 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002061 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002062
2063 Data = mos7840_port->shadowMCR;
2064 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2065
2066 /* Determine divisor based on baud rate */
2067 baud = tty_get_baud_rate(tty);
2068
2069 if (!baud) {
2070 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302071 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002072 baud = 9600;
2073 }
2074
Harvey Harrison441b62c2008-03-03 16:08:34 -08002075 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002076 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2077
2078 /* Enable Interrupts */
2079 Data = 0x0c;
2080 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2081
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002082 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002083 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002084 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002085 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002086 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002087 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002088 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002089 }
2090 }
2091 wake_up(&mos7840_port->delta_msr_wait);
2092 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302093 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002094 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002095}
2096
2097/*****************************************************************************
2098 * mos7840_set_termios
2099 * this function is called by the tty driver when it wants to change
2100 * the termios structure
2101 *****************************************************************************/
2102
Alan Cox95da3102008-07-22 11:09:07 +01002103static void mos7840_set_termios(struct tty_struct *tty,
2104 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002105 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002106{
2107 int status;
2108 unsigned int cflag;
2109 struct usb_serial *serial;
2110 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302111 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002112 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302113 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002114 return;
2115 }
2116
2117 serial = port->serial;
2118
Harvey Harrison441b62c2008-03-03 16:08:34 -08002119 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302120 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002121 return;
2122 }
2123
2124 mos7840_port = mos7840_get_port_private(port);
2125
2126 if (mos7840_port == NULL)
2127 return;
2128
Paul B Schroeder3f542972006-08-31 19:41:47 -05002129 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002130 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002131 return;
2132 }
2133
Tony Cook84fe6e72009-04-18 22:55:06 +09302134 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002135
2136 cflag = tty->termios->c_cflag;
2137
Harvey Harrison441b62c2008-03-03 16:08:34 -08002138 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002139 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002140 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002141 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002142 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002143
2144 /* change the port settings to the new ones specified */
2145
Alan Cox95da3102008-07-22 11:09:07 +01002146 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002147
2148 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302149 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002150 return;
2151 }
2152
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002153 if (mos7840_port->read_urb_busy == false) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002154 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002155 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2156 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002157 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002158 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002159 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002160 }
2161 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002162}
2163
2164/*****************************************************************************
2165 * mos7840_get_lsr_info - get line status register info
2166 *
2167 * Purpose: Let user call ioctl() to get info when the UART physically
2168 * is emptied. On bus types like RS485, the transmitter must
2169 * release the bus after transmitting. This must be done when
2170 * the transmit shift register is empty, not be done when the
2171 * transmit holding register is empty. This functionality
2172 * allows an RS485 driver to be written in user space.
2173 *****************************************************************************/
2174
Alan Cox95da3102008-07-22 11:09:07 +01002175static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002176 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002177{
2178 int count;
2179 unsigned int result = 0;
2180
Alan Cox95da3102008-07-22 11:09:07 +01002181 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002182 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002183 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002184 result = TIOCSER_TEMT;
2185 }
2186
2187 if (copy_to_user(value, &result, sizeof(int)))
2188 return -EFAULT;
2189 return 0;
2190}
2191
2192/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002193 * mos7840_get_serial_info
2194 * function to get information about serial port
2195 *****************************************************************************/
2196
2197static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002198 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002199{
2200 struct serial_struct tmp;
2201
2202 if (mos7840_port == NULL)
2203 return -1;
2204
2205 if (!retinfo)
2206 return -EFAULT;
2207
2208 memset(&tmp, 0, sizeof(tmp));
2209
2210 tmp.type = PORT_16550A;
2211 tmp.line = mos7840_port->port->serial->minor;
2212 tmp.port = mos7840_port->port->number;
2213 tmp.irq = 0;
2214 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2215 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2216 tmp.baud_base = 9600;
2217 tmp.close_delay = 5 * HZ;
2218 tmp.closing_wait = 30 * HZ;
2219
2220 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2221 return -EFAULT;
2222 return 0;
2223}
2224
Alan Cox0bca1b92010-09-16 18:21:40 +01002225static int mos7840_get_icount(struct tty_struct *tty,
2226 struct serial_icounter_struct *icount)
2227{
2228 struct usb_serial_port *port = tty->driver_data;
2229 struct moschip_port *mos7840_port;
2230 struct async_icount cnow;
2231
2232 mos7840_port = mos7840_get_port_private(port);
2233 cnow = mos7840_port->icount;
2234
2235 smp_rmb();
2236 icount->cts = cnow.cts;
2237 icount->dsr = cnow.dsr;
2238 icount->rng = cnow.rng;
2239 icount->dcd = cnow.dcd;
2240 icount->rx = cnow.rx;
2241 icount->tx = cnow.tx;
2242 icount->frame = cnow.frame;
2243 icount->overrun = cnow.overrun;
2244 icount->parity = cnow.parity;
2245 icount->brk = cnow.brk;
2246 icount->buf_overrun = cnow.buf_overrun;
2247
2248 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2249 port->number, icount->rx, icount->tx);
2250 return 0;
2251}
2252
Paul B Schroeder3f542972006-08-31 19:41:47 -05002253/*****************************************************************************
2254 * SerialIoctl
2255 * this function handles any ioctl calls to the driver
2256 *****************************************************************************/
2257
Alan Cox00a0d0d2011-02-14 16:27:06 +00002258static int mos7840_ioctl(struct tty_struct *tty,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002259 unsigned int cmd, unsigned long arg)
2260{
Alan Cox95da3102008-07-22 11:09:07 +01002261 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002262 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002263 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002264
2265 struct async_icount cnow;
2266 struct async_icount cprev;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002267
Harvey Harrison441b62c2008-03-03 16:08:34 -08002268 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302269 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002270 return -1;
2271 }
2272
2273 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002274
2275 if (mos7840_port == NULL)
2276 return -1;
2277
Harvey Harrison441b62c2008-03-03 16:08:34 -08002278 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002279
2280 switch (cmd) {
2281 /* return number of bytes available */
2282
Paul B Schroeder3f542972006-08-31 19:41:47 -05002283 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002284 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002285 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002286
Paul B Schroeder3f542972006-08-31 19:41:47 -05002287 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002288 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002289 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002290
2291 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002292 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002293 break;
2294
2295 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002296 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002297 cprev = mos7840_port->icount;
2298 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002299 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002300 mos7840_port->delta_msr_cond = 0;
2301 wait_event_interruptible(mos7840_port->delta_msr_wait,
2302 (mos7840_port->
2303 delta_msr_cond == 1));
2304
2305 /* see if a signal did it */
2306 if (signal_pending(current))
2307 return -ERESTARTSYS;
2308 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002309 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002310 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2311 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2312 return -EIO; /* no change => error */
2313 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2314 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2315 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2316 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2317 return 0;
2318 }
2319 cprev = cnow;
2320 }
2321 /* NOTREACHED */
2322 break;
2323
Paul B Schroeder3f542972006-08-31 19:41:47 -05002324 default:
2325 break;
2326 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002327 return -ENOIOCTLCMD;
2328}
2329
2330static int mos7840_calc_num_ports(struct usb_serial *serial)
2331{
Donald Lee093ea2d2012-03-14 15:26:33 +08002332 __u16 Data = 0x00;
2333 int ret = 0;
2334 int mos7840_num_ports;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002335
Donald Lee093ea2d2012-03-14 15:26:33 +08002336 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2337 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &Data,
2338 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2339
2340 if ((Data & 0x01) == 0) {
2341 mos7840_num_ports = 2;
2342 serial->num_bulk_in = 2;
2343 serial->num_bulk_out = 2;
2344 serial->num_ports = 2;
2345 } else {
2346 mos7840_num_ports = 4;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002347 serial->num_bulk_in = 4;
2348 serial->num_bulk_out = 4;
Donald Lee093ea2d2012-03-14 15:26:33 +08002349 serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002350 }
Donald Lee093ea2d2012-03-14 15:26:33 +08002351
Paul B Schroeder3f542972006-08-31 19:41:47 -05002352 return mos7840_num_ports;
2353}
2354
2355/****************************************************************************
2356 * mos7840_startup
2357 ****************************************************************************/
2358
2359static int mos7840_startup(struct usb_serial *serial)
2360{
2361 struct moschip_port *mos7840_port;
2362 struct usb_device *dev;
2363 int i, status;
2364
2365 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302366 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002367
2368 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302369 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002370 return -1;
2371 }
2372
2373 dev = serial->dev;
2374
Tony Cook84fe6e72009-04-18 22:55:06 +09302375 dbg("%s", "Entering...");
2376 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002377
2378 /* we set up the pointers to the endpoints in the mos7840_open *
2379 * function, as the structures aren't created yet. */
2380
2381 /* set up port private structures */
2382 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302383 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002384 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002385 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002386 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002387 status = -ENOMEM;
2388 i--; /* don't follow NULL pointer cleaning up */
2389 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002390 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002391
Alan Cox880af9d2008-07-22 11:16:12 +01002392 /* Initialize all port interrupt end point to port 0 int
2393 * endpoint. Our device has only one interrupt end point
2394 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002395
2396 mos7840_port->port = serial->port[i];
2397 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002398 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002399
Tony Cook37768ad2009-04-18 22:42:18 +09302400 /* minor is not initialised until later by
2401 * usb-serial.c:get_free_serial() and cannot therefore be used
2402 * to index device instances */
2403 mos7840_port->port_num = i + 1;
2404 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2405 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2406 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2407 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002408
2409 if (mos7840_port->port_num == 1) {
2410 mos7840_port->SpRegOffset = 0x0;
2411 mos7840_port->ControlRegOffset = 0x1;
2412 mos7840_port->DcrRegOffset = 0x4;
2413 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002414 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002415 mos7840_port->SpRegOffset = 0x8;
2416 mos7840_port->ControlRegOffset = 0x9;
2417 mos7840_port->DcrRegOffset = 0x16;
2418 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002419 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002420 mos7840_port->SpRegOffset = 0xa;
2421 mos7840_port->ControlRegOffset = 0xb;
2422 mos7840_port->DcrRegOffset = 0x19;
2423 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002424 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002425 mos7840_port->SpRegOffset = 0xa;
2426 mos7840_port->ControlRegOffset = 0xb;
2427 mos7840_port->DcrRegOffset = 0x19;
2428 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002429 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002430 mos7840_port->SpRegOffset = 0xc;
2431 mos7840_port->ControlRegOffset = 0xd;
2432 mos7840_port->DcrRegOffset = 0x1c;
2433 }
2434 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002435 mos7840_set_port_private(serial->port[i], mos7840_port);
2436
Alan Cox880af9d2008-07-22 11:16:12 +01002437 /* enable rx_disable bit in control register */
2438 status = mos7840_get_reg_sync(serial->port[i],
2439 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002440 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302441 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002442 break;
2443 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302444 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002445 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002446 Data |= 0x08; /* setting driver done bit */
2447 Data |= 0x04; /* sp1_bit to have cts change reflect in
2448 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002449
Alan Cox880af9d2008-07-22 11:16:12 +01002450 /* Data |= 0x20; //rx_disable bit */
2451 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002452 mos7840_port->ControlRegOffset, Data);
2453 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302454 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002455 break;
2456 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302457 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002458 status);
2459
Alan Cox880af9d2008-07-22 11:16:12 +01002460 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2461 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002462 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002463 status = mos7840_set_reg_sync(serial->port[i],
2464 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002465 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302466 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002467 break;
2468 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302469 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002470
2471 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002472 status = mos7840_set_reg_sync(serial->port[i],
2473 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002474 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302475 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002476 break;
2477 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302478 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002479
2480 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002481 status = mos7840_set_reg_sync(serial->port[i],
2482 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002483 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302484 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002485 break;
2486 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302487 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002488
Alan Cox880af9d2008-07-22 11:16:12 +01002489 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002490 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002491 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002492 CLK_START_VALUE_REGISTER, Data);
2493 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302494 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002495 break;
2496 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302497 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002498
2499 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002500 status = mos7840_set_reg_sync(serial->port[i],
2501 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002502 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302503 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002504 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002505 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002506 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302507 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002508 status);
2509
Alan Cox880af9d2008-07-22 11:16:12 +01002510 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002511 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002512 status = mos7840_set_uart_reg(serial->port[i],
2513 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002514 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302515 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002516 status);
2517 break;
2518 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302519 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002520 status);
2521
Alan Cox880af9d2008-07-22 11:16:12 +01002522 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002523 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002524 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002525
2526 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002527 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002528 (__u16) (ZLP_REG1 +
2529 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302530 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002531 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002532 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002533 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302534 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002535 i + 2, status);
2536 break;
2537 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302538 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002539 i + 2, status);
2540 } else {
2541 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002542 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002543 (__u16) (ZLP_REG1 +
2544 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302545 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002546 (__u16) (ZLP_REG1 +
2547 ((__u16) mos7840_port->port_num) - 0x1));
2548 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302549 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002550 i + 1, status);
2551 break;
2552 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302553 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002554 i + 1, status);
2555
2556 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002557 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002558 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002559 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2560 GFP_KERNEL);
2561 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2562 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002563 status = -ENOMEM;
2564 goto error;
2565 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002566 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302567 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002568
Alan Cox880af9d2008-07-22 11:16:12 +01002569 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002570 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002571 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2572 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302573 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002574 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002575 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302576 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002577
2578 /* setting configuration feature to one */
2579 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002580 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002581 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002582error:
2583 for (/* nothing */; i >= 0; i--) {
2584 mos7840_port = mos7840_get_port_private(serial->port[i]);
2585
2586 kfree(mos7840_port->dr);
2587 kfree(mos7840_port->ctrl_buf);
2588 usb_free_urb(mos7840_port->control_urb);
2589 kfree(mos7840_port);
2590 serial->port[i] = NULL;
2591 }
2592 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002593}
2594
2595/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002596 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002597 * This function is called whenever the device is removed from the usb bus.
2598 ****************************************************************************/
2599
Alan Sternf9c99bb2009-06-02 11:53:55 -04002600static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002601{
2602 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002603 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002604 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002605 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002606
2607 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302608 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002609 return;
2610 }
2611
Alan Cox880af9d2008-07-22 11:16:12 +01002612 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002613
2614 /* free private structure allocated for serial port *
2615 * stop reads and writes on all ports */
2616
2617 for (i = 0; i < serial->num_ports; ++i) {
2618 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302619 dbg ("mos7840_port %d = %p", i, mos7840_port);
2620 if (mos7840_port) {
2621 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2622 mos7840_port->zombie = 1;
2623 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2624 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002625 }
2626 }
2627
2628 dbg("%s", "Thank u :: ");
2629
2630}
2631
2632/****************************************************************************
2633 * mos7840_release
2634 * This function is called when the usb_serial structure is freed.
2635 ****************************************************************************/
2636
2637static void mos7840_release(struct usb_serial *serial)
2638{
2639 int i;
2640 struct moschip_port *mos7840_port;
2641 dbg("%s", " release :entering..........");
2642
2643 if (!serial) {
2644 dbg("%s", "Invalid Handler");
2645 return;
2646 }
2647
2648 /* check for the ports to be closed,close the ports and disconnect */
2649
2650 /* free private structure allocated for serial port *
2651 * stop reads and writes on all ports */
2652
2653 for (i = 0; i < serial->num_ports; ++i) {
2654 mos7840_port = mos7840_get_port_private(serial->port[i]);
2655 dbg("mos7840_port %d = %p", i, mos7840_port);
2656 if (mos7840_port) {
Tony Cook37768ad2009-04-18 22:42:18 +09302657 kfree(mos7840_port->ctrl_buf);
2658 kfree(mos7840_port->dr);
2659 kfree(mos7840_port);
2660 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002661 }
2662
Tony Cook84fe6e72009-04-18 22:55:06 +09302663 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002664
2665}
2666
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002667static struct usb_driver io_driver = {
2668 .name = "mos7840",
2669 .probe = usb_serial_probe,
2670 .disconnect = usb_serial_disconnect,
2671 .id_table = moschip_id_table_combined,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002672};
2673
Paul B Schroeder3f542972006-08-31 19:41:47 -05002674static struct usb_serial_driver moschip7840_4port_device = {
2675 .driver = {
2676 .owner = THIS_MODULE,
2677 .name = "mos7840",
2678 },
2679 .description = DRIVER_DESC,
2680 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002681 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002682 .open = mos7840_open,
2683 .close = mos7840_close,
2684 .write = mos7840_write,
2685 .write_room = mos7840_write_room,
2686 .chars_in_buffer = mos7840_chars_in_buffer,
2687 .throttle = mos7840_throttle,
2688 .unthrottle = mos7840_unthrottle,
2689 .calc_num_ports = mos7840_calc_num_ports,
2690#ifdef MCSSerialProbe
2691 .probe = mos7840_serial_probe,
2692#endif
2693 .ioctl = mos7840_ioctl,
2694 .set_termios = mos7840_set_termios,
2695 .break_ctl = mos7840_break,
2696 .tiocmget = mos7840_tiocmget,
2697 .tiocmset = mos7840_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002698 .get_icount = mos7840_get_icount,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002699 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002700 .disconnect = mos7840_disconnect,
2701 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002702 .read_bulk_callback = mos7840_bulk_in_callback,
2703 .read_int_callback = mos7840_interrupt_callback,
2704};
2705
Alan Stern4d2a7af2012-02-23 14:57:09 -05002706static struct usb_serial_driver * const serial_drivers[] = {
2707 &moschip7840_4port_device, NULL
2708};
2709
Greg Kroah-Hartmane7414d92012-02-28 13:12:10 -08002710module_usb_serial_driver(io_driver, serial_drivers);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002711
Paul B Schroeder3f542972006-08-31 19:41:47 -05002712MODULE_DESCRIPTION(DRIVER_DESC);
2713MODULE_LICENSE("GPL");
2714
2715module_param(debug, bool, S_IRUGO | S_IWUSR);
2716MODULE_PARM_DESC(debug, "Debug enabled or not");