blob: f8424d1bfc1b279a5e4a51fc4453fb3e3f847ffb [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 */
122#define USB_VENDOR_ID_BANDB 0x0856
Cliff Brakeacf509a2009-12-01 09:53:43 -0500123#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
124#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
125#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
126#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
127#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
128#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
Blaise Gassend27f12812009-12-18 15:23:38 -0800129#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
130#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
131#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
Paul B Schroeder3f542972006-08-31 19:41:47 -0500132
Russell Lang9d498be2009-07-17 19:29:20 +1000133/* This driver also supports
134 * ATEN UC2324 device using Moschip MCS7840
135 * ATEN UC2322 device using Moschip MCS7820
136 */
Tony Cooke9b8cff2009-04-18 22:42:18 +0930137#define USB_VENDOR_ID_ATENINTL 0x0557
138#define ATENINTL_DEVICE_ID_UC2324 0x2011
Russell Lang9d498be2009-07-17 19:29:20 +1000139#define ATENINTL_DEVICE_ID_UC2322 0x7820
Tony Cooke9b8cff2009-04-18 22:42:18 +0930140
David Ludlow11e1abb2008-02-25 17:30:52 -0500141/* Interrupt Routine Defines */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500142
143#define SERIAL_IIR_RLS 0x06
144#define SERIAL_IIR_MS 0x00
145
146/*
147 * Emulation of the bit mask on the LINE STATUS REGISTER.
148 */
149#define SERIAL_LSR_DR 0x0001
150#define SERIAL_LSR_OE 0x0002
151#define SERIAL_LSR_PE 0x0004
152#define SERIAL_LSR_FE 0x0008
153#define SERIAL_LSR_BI 0x0010
154
155#define MOS_MSR_DELTA_CTS 0x10
156#define MOS_MSR_DELTA_DSR 0x20
157#define MOS_MSR_DELTA_RI 0x40
158#define MOS_MSR_DELTA_CD 0x80
159
Alan Cox880af9d2008-07-22 11:16:12 +0100160/* Serial Port register Address */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500161#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
162#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
163#define LINE_CONTROL_REGISTER ((__u16)(0x03))
164#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
165#define LINE_STATUS_REGISTER ((__u16)(0x05))
166#define MODEM_STATUS_REGISTER ((__u16)(0x06))
167#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
168#define DIVISOR_LATCH_LSB ((__u16)(0x00))
169#define DIVISOR_LATCH_MSB ((__u16)(0x01))
170
171#define CLK_MULTI_REGISTER ((__u16)(0x02))
172#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
173
174#define SERIAL_LCR_DLAB ((__u16)(0x0080))
175
176/*
177 * URB POOL related defines
178 */
179#define NUM_URBS 16 /* URB Count */
180#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
181
182
Németh Márton7d40d7e2010-01-10 15:34:24 +0100183static const struct usb_device_id moschip_port_id_table[] = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500184 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
185 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500186 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
187 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
188 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
189 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
190 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930195 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000196 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500197 {} /* terminating entry */
198};
199
Németh Márton7d40d7e2010-01-10 15:34:24 +0100200static const struct usb_device_id moschip_id_table_combined[] __devinitconst = {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500201 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
202 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
David Ludlow11e1abb2008-02-25 17:30:52 -0500209 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
Cliff Brakeacf509a2009-12-01 09:53:43 -0500210 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
Blaise Gassend27f12812009-12-18 15:23:38 -0800211 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
Tony Cooke9b8cff2009-04-18 22:42:18 +0930212 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
Russell Lang9d498be2009-07-17 19:29:20 +1000213 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
Paul B Schroeder3f542972006-08-31 19:41:47 -0500214 {} /* terminating entry */
215};
216
217MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
218
219/* This structure holds all of the local port information */
220
221struct moschip_port {
222 int port_num; /*Actual port number in the device(1,2,etc) */
223 struct urb *write_urb; /* write URB for this port */
224 struct urb *read_urb; /* read URB for this port */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100225 struct urb *int_urb;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500226 __u8 shadowLCR; /* last LCR value received */
227 __u8 shadowMCR; /* last MCR value received */
228 char open;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100229 char open_ports;
230 char zombie;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500231 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
232 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
233 int delta_msr_cond;
234 struct async_icount icount;
235 struct usb_serial_port *port; /* loop back to the owner of this object */
236
Alan Cox880af9d2008-07-22 11:16:12 +0100237 /* Offsets */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500238 __u8 SpRegOffset;
239 __u8 ControlRegOffset;
240 __u8 DcrRegOffset;
Alan Cox880af9d2008-07-22 11:16:12 +0100241 /* for processing control URBS in interrupt context */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500242 struct urb *control_urb;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100243 struct usb_ctrlrequest *dr;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500244 char *ctrl_buf;
245 int MsrLsr;
246
Oliver Neukum0de9a702007-03-16 20:28:28 +0100247 spinlock_t pool_lock;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500248 struct urb *write_urb_pool[NUM_URBS];
Oliver Neukum0de9a702007-03-16 20:28:28 +0100249 char busy[NUM_URBS];
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800250 bool read_urb_busy;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500251};
252
253
254static int debug;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500255
256/*
257 * mos7840_set_reg_sync
258 * To set the Control register by calling usb_fill_control_urb function
259 * by passing usb_sndctrlpipe function as parameter.
260 */
261
262static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
263 __u16 val)
264{
265 struct usb_device *dev = port->serial->dev;
266 val = val & 0x00ff;
Tony Cook84fe6e72009-04-18 22:55:06 +0930267 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500268
269 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
270 MCS_WR_RTYPE, val, reg, NULL, 0,
271 MOS_WDR_TIMEOUT);
272}
273
274/*
275 * mos7840_get_reg_sync
276 * To set the Uart register by calling usb_fill_control_urb function by
277 * passing usb_rcvctrlpipe function as parameter.
278 */
279
280static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100281 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500282{
283 struct usb_device *dev = port->serial->dev;
284 int ret = 0;
Johan Hovold9e221a32009-12-28 23:01:55 +0100285 u8 *buf;
286
287 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
288 if (!buf)
289 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500290
291 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100292 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500293 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100294 *val = buf[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930295 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
Johan Hovold9e221a32009-12-28 23:01:55 +0100296
297 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500298 return ret;
299}
300
301/*
302 * mos7840_set_uart_reg
303 * To set the Uart register by calling usb_fill_control_urb function by
304 * passing usb_sndctrlpipe function as parameter.
305 */
306
307static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
308 __u16 val)
309{
310
311 struct usb_device *dev = port->serial->dev;
312 val = val & 0x00ff;
Alan Cox880af9d2008-07-22 11:16:12 +0100313 /* For the UART control registers, the application number need
314 to be Or'ed */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100315 if (port->serial->num_ports == 4) {
Alan Cox880af9d2008-07-22 11:16:12 +0100316 val |= (((__u16) port->number -
317 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930318 dbg("mos7840_set_uart_reg application number is %x", val);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500319 } else {
320 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100321 val |= (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500322 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930323 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500324 val);
325 } else {
326 val |=
327 (((__u16) port->number -
328 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930329 dbg("mos7840_set_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500330 val);
331 }
332 }
333 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
334 MCS_WR_RTYPE, val, reg, NULL, 0,
335 MOS_WDR_TIMEOUT);
336
337}
338
339/*
340 * mos7840_get_uart_reg
341 * To set the Control register by calling usb_fill_control_urb function
342 * by passing usb_rcvctrlpipe function as parameter.
343 */
344static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100345 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500346{
347 struct usb_device *dev = port->serial->dev;
348 int ret = 0;
349 __u16 Wval;
Johan Hovold9e221a32009-12-28 23:01:55 +0100350 u8 *buf;
351
352 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
353 if (!buf)
354 return -ENOMEM;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500355
Tony Cook84fe6e72009-04-18 22:55:06 +0930356 /* dbg("application number is %4x",
Alan Cox880af9d2008-07-22 11:16:12 +0100357 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
358 /* Wval is same as application number */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100359 if (port->serial->num_ports == 4) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500360 Wval =
361 (((__u16) port->number - (__u16) (port->serial->minor)) +
362 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930363 dbg("mos7840_get_uart_reg application number is %x", Wval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500364 } else {
365 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
Alan Cox880af9d2008-07-22 11:16:12 +0100366 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500367 (__u16) (port->serial->minor)) + 1) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930368 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500369 Wval);
370 } else {
Alan Cox880af9d2008-07-22 11:16:12 +0100371 Wval = (((__u16) port->number -
Paul B Schroeder3f542972006-08-31 19:41:47 -0500372 (__u16) (port->serial->minor)) + 2) << 8;
Tony Cook84fe6e72009-04-18 22:55:06 +0930373 dbg("mos7840_get_uart_reg application number is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500374 Wval);
375 }
376 }
377 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
Johan Hovold9e221a32009-12-28 23:01:55 +0100378 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500379 MOS_WDR_TIMEOUT);
Johan Hovold9e221a32009-12-28 23:01:55 +0100380 *val = buf[0];
381
382 kfree(buf);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500383 return ret;
384}
385
386static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
387{
388
Tony Cook84fe6e72009-04-18 22:55:06 +0930389 dbg("***************************************");
390 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
391 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
392 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
393 dbg("***************************************");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500394
395}
396
397/************************************************************************/
398/************************************************************************/
399/* I N T E R F A C E F U N C T I O N S */
400/* I N T E R F A C E F U N C T I O N S */
401/************************************************************************/
402/************************************************************************/
403
404static inline void mos7840_set_port_private(struct usb_serial_port *port,
405 struct moschip_port *data)
406{
407 usb_set_serial_port_data(port, (void *)data);
408}
409
410static inline struct moschip_port *mos7840_get_port_private(struct
411 usb_serial_port
412 *port)
413{
414 return (struct moschip_port *)usb_get_serial_port_data(port);
415}
416
Oliver Neukum0de9a702007-03-16 20:28:28 +0100417static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500418{
419 struct moschip_port *mos7840_port;
420 struct async_icount *icount;
421 mos7840_port = port;
422 icount = &mos7840_port->icount;
423 if (new_msr &
424 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
425 MOS_MSR_DELTA_CD)) {
426 icount = &mos7840_port->icount;
427
428 /* update input line counters */
429 if (new_msr & MOS_MSR_DELTA_CTS) {
430 icount->cts++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100431 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500432 }
433 if (new_msr & MOS_MSR_DELTA_DSR) {
434 icount->dsr++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100435 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500436 }
437 if (new_msr & MOS_MSR_DELTA_CD) {
438 icount->dcd++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100439 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500440 }
441 if (new_msr & MOS_MSR_DELTA_RI) {
442 icount->rng++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100443 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500444 }
445 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500446}
447
Oliver Neukum0de9a702007-03-16 20:28:28 +0100448static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500449{
450 struct async_icount *icount;
451
Harvey Harrison441b62c2008-03-03 16:08:34 -0800452 dbg("%s - %02x", __func__, new_lsr);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500453
454 if (new_lsr & SERIAL_LSR_BI) {
Alan Cox880af9d2008-07-22 11:16:12 +0100455 /*
456 * Parity and Framing errors only count if they
457 * occur exclusive of a break being
458 * received.
459 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500460 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
461 }
462
463 /* update input line counters */
464 icount = &port->icount;
465 if (new_lsr & SERIAL_LSR_BI) {
466 icount->brk++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100467 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500468 }
469 if (new_lsr & SERIAL_LSR_OE) {
470 icount->overrun++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100471 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500472 }
473 if (new_lsr & SERIAL_LSR_PE) {
474 icount->parity++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100475 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500476 }
477 if (new_lsr & SERIAL_LSR_FE) {
478 icount->frame++;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100479 smp_wmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -0500480 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500481}
482
483/************************************************************************/
484/************************************************************************/
485/* U S B C A L L B A C K F U N C T I O N S */
486/* U S B C A L L B A C K F U N C T I O N S */
487/************************************************************************/
488/************************************************************************/
489
David Howells7d12e782006-10-05 14:55:46 +0100490static void mos7840_control_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500491{
492 unsigned char *data;
493 struct moschip_port *mos7840_port;
494 __u8 regval = 0x0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100495 int result = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700496 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500497
Ming Leicdc97792008-02-24 18:41:47 +0800498 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100499
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700500 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500501 case 0:
502 /* success */
503 break;
504 case -ECONNRESET:
505 case -ENOENT:
506 case -ESHUTDOWN:
507 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800508 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700509 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500510 return;
511 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800512 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700513 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500514 goto exit;
515 }
516
Tony Cook84fe6e72009-04-18 22:55:06 +0930517 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
518 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -0500519 mos7840_port->MsrLsr, mos7840_port->port_num);
520 data = urb->transfer_buffer;
521 regval = (__u8) data[0];
Tony Cook84fe6e72009-04-18 22:55:06 +0930522 dbg("%s data is %x", __func__, regval);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500523 if (mos7840_port->MsrLsr == 0)
524 mos7840_handle_new_msr(mos7840_port, regval);
525 else if (mos7840_port->MsrLsr == 1)
526 mos7840_handle_new_lsr(mos7840_port, regval);
527
Oliver Neukum0de9a702007-03-16 20:28:28 +0100528exit:
529 spin_lock(&mos7840_port->pool_lock);
530 if (!mos7840_port->zombie)
531 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
532 spin_unlock(&mos7840_port->pool_lock);
533 if (result) {
534 dev_err(&urb->dev->dev,
535 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800536 __func__, result);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100537 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500538}
539
540static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
Alan Cox880af9d2008-07-22 11:16:12 +0100541 __u16 *val)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500542{
543 struct usb_device *dev = mcs->port->serial->dev;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100544 struct usb_ctrlrequest *dr = mcs->dr;
545 unsigned char *buffer = mcs->ctrl_buf;
546 int ret;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500547
Paul B Schroeder3f542972006-08-31 19:41:47 -0500548 dr->bRequestType = MCS_RD_RTYPE;
549 dr->bRequest = MCS_RDREQ;
Alan Cox880af9d2008-07-22 11:16:12 +0100550 dr->wValue = cpu_to_le16(Wval); /* 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500551 dr->wIndex = cpu_to_le16(reg);
552 dr->wLength = cpu_to_le16(2);
553
554 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
555 (unsigned char *)dr, buffer, 2,
556 mos7840_control_callback, mcs);
557 mcs->control_urb->transfer_buffer_length = 2;
558 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
559 return ret;
560}
561
562/*****************************************************************************
563 * mos7840_interrupt_callback
564 * this is the callback function for when we have received data on the
565 * interrupt endpoint.
566 *****************************************************************************/
567
David Howells7d12e782006-10-05 14:55:46 +0100568static void mos7840_interrupt_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500569{
570 int result;
571 int length;
572 struct moschip_port *mos7840_port;
573 struct usb_serial *serial;
574 __u16 Data;
575 unsigned char *data;
576 __u8 sp[5], st;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100577 int i, rv = 0;
578 __u16 wval, wreg = 0;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700579 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500580
Tony Cook84fe6e72009-04-18 22:55:06 +0930581 dbg("%s", " : Entering");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500582
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700583 switch (status) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500584 case 0:
585 /* success */
586 break;
587 case -ECONNRESET:
588 case -ENOENT:
589 case -ESHUTDOWN:
590 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800591 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700592 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500593 return;
594 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800595 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700596 status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500597 goto exit;
598 }
599
600 length = urb->actual_length;
601 data = urb->transfer_buffer;
602
Ming Leicdc97792008-02-24 18:41:47 +0800603 serial = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500604
605 /* Moschip get 5 bytes
606 * Byte 1 IIR Port 1 (port.number is 0)
607 * Byte 2 IIR Port 2 (port.number is 1)
608 * Byte 3 IIR Port 3 (port.number is 2)
609 * Byte 4 IIR Port 4 (port.number is 3)
610 * Byte 5 FIFO status for both */
611
612 if (length && length > 5) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930613 dbg("%s", "Wrong data !!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500614 return;
615 }
616
617 sp[0] = (__u8) data[0];
618 sp[1] = (__u8) data[1];
619 sp[2] = (__u8) data[2];
620 sp[3] = (__u8) data[3];
621 st = (__u8) data[4];
622
623 for (i = 0; i < serial->num_ports; i++) {
624 mos7840_port = mos7840_get_port_private(serial->port[i]);
625 wval =
626 (((__u16) serial->port[i]->number -
627 (__u16) (serial->minor)) + 1) << 8;
628 if (mos7840_port->open) {
629 if (sp[i] & 0x01) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930630 dbg("SP%d No Interrupt !!!", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500631 } else {
632 switch (sp[i] & 0x0f) {
633 case SERIAL_IIR_RLS:
634 dbg("Serial Port %d: Receiver status error or ", i);
Tony Cook84fe6e72009-04-18 22:55:06 +0930635 dbg("address bit detected in 9-bit mode");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500636 mos7840_port->MsrLsr = 1;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100637 wreg = LINE_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500638 break;
639 case SERIAL_IIR_MS:
Tony Cook84fe6e72009-04-18 22:55:06 +0930640 dbg("Serial Port %d: Modem status change", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500641 mos7840_port->MsrLsr = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100642 wreg = MODEM_STATUS_REGISTER;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500643 break;
644 }
Oliver Neukum0de9a702007-03-16 20:28:28 +0100645 spin_lock(&mos7840_port->pool_lock);
646 if (!mos7840_port->zombie) {
647 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
648 } else {
649 spin_unlock(&mos7840_port->pool_lock);
650 return;
651 }
652 spin_unlock(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500653 }
654 }
655 }
Alan Cox880af9d2008-07-22 11:16:12 +0100656 if (!(rv < 0))
657 /* the completion handler for the control urb will resubmit */
Oliver Neukum0de9a702007-03-16 20:28:28 +0100658 return;
659exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -0500660 result = usb_submit_urb(urb, GFP_ATOMIC);
661 if (result) {
662 dev_err(&urb->dev->dev,
663 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800664 __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500665 }
Paul B Schroeder3f542972006-08-31 19:41:47 -0500666}
667
668static int mos7840_port_paranoia_check(struct usb_serial_port *port,
669 const char *function)
670{
671 if (!port) {
672 dbg("%s - port == NULL", function);
673 return -1;
674 }
675 if (!port->serial) {
676 dbg("%s - port->serial == NULL", function);
677 return -1;
678 }
679
680 return 0;
681}
682
683/* Inline functions to check the sanity of a pointer that is passed to us */
684static int mos7840_serial_paranoia_check(struct usb_serial *serial,
685 const char *function)
686{
687 if (!serial) {
688 dbg("%s - serial == NULL", function);
689 return -1;
690 }
691 if (!serial->type) {
692 dbg("%s - serial->type == NULL!", function);
693 return -1;
694 }
695
696 return 0;
697}
698
699static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
700 const char *function)
701{
702 /* if no port was specified, or it fails a paranoia check */
703 if (!port ||
704 mos7840_port_paranoia_check(port, function) ||
705 mos7840_serial_paranoia_check(port->serial, function)) {
Alan Cox880af9d2008-07-22 11:16:12 +0100706 /* then say that we don't have a valid usb_serial thing,
707 * which will end up genrating -ENODEV return values */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500708 return NULL;
709 }
710
711 return port->serial;
712}
713
714/*****************************************************************************
715 * mos7840_bulk_in_callback
716 * this is the callback function for when we have received data on the
717 * bulk in endpoint.
718 *****************************************************************************/
719
David Howells7d12e782006-10-05 14:55:46 +0100720static void mos7840_bulk_in_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500721{
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700722 int retval;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500723 unsigned char *data;
724 struct usb_serial *serial;
725 struct usb_serial_port *port;
726 struct moschip_port *mos7840_port;
727 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700728 int status = urb->status;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500729
Ming Leicdc97792008-02-24 18:41:47 +0800730 mos7840_port = urb->context;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500731 if (!mos7840_port) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930732 dbg("%s", "NULL mos7840_port pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800733 mos7840_port->read_urb_busy = false;
734 return;
735 }
736
737 if (status) {
738 dbg("nonzero read bulk status received: %d", status);
739 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500740 return;
741 }
742
743 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800744 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930745 dbg("%s", "Port Paranoia failed");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800746 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500747 return;
748 }
749
Harvey Harrison441b62c2008-03-03 16:08:34 -0800750 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500751 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930752 dbg("%s", "Bad serial pointer");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800753 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500754 return;
755 }
756
Tony Cook84fe6e72009-04-18 22:55:06 +0930757 dbg("%s", "Entering... ");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500758
759 data = urb->transfer_buffer;
760
Tony Cook84fe6e72009-04-18 22:55:06 +0930761 dbg("%s", "Entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500762
763 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100764 tty = tty_port_tty_get(&mos7840_port->port->port);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500765 if (tty) {
Paul B Schroeder3f542972006-08-31 19:41:47 -0500766 tty_insert_flip_string(tty, data, urb->actual_length);
Tony Cook84fe6e72009-04-18 22:55:06 +0930767 dbg(" %s ", data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500768 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100769 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500770 }
771 mos7840_port->icount.rx += urb->actual_length;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100772 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +0930773 dbg("mos7840_port->icount.rx is %d:",
Paul B Schroeder3f542972006-08-31 19:41:47 -0500774 mos7840_port->icount.rx);
775 }
776
777 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930778 dbg("%s", "URB KILLED !!!");
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800779 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500780 return;
781 }
782
Paul B Schroeder3f542972006-08-31 19:41:47 -0500783
Oliver Neukum0de9a702007-03-16 20:28:28 +0100784 mos7840_port->read_urb->dev = serial->dev;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500785
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800786 mos7840_port->read_urb_busy = true;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700787 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100788
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700789 if (retval) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -0800790 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
791 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500792 }
793}
794
795/*****************************************************************************
796 * mos7840_bulk_out_data_callback
Alan Cox880af9d2008-07-22 11:16:12 +0100797 * this is the callback function for when we have finished sending
798 * serial data on the bulk out endpoint.
Paul B Schroeder3f542972006-08-31 19:41:47 -0500799 *****************************************************************************/
800
David Howells7d12e782006-10-05 14:55:46 +0100801static void mos7840_bulk_out_data_callback(struct urb *urb)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500802{
803 struct moschip_port *mos7840_port;
804 struct tty_struct *tty;
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700805 int status = urb->status;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100806 int i;
807
Ming Leicdc97792008-02-24 18:41:47 +0800808 mos7840_port = urb->context;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100809 spin_lock(&mos7840_port->pool_lock);
810 for (i = 0; i < NUM_URBS; i++) {
811 if (urb == mos7840_port->write_urb_pool[i]) {
812 mos7840_port->busy[i] = 0;
813 break;
814 }
815 }
816 spin_unlock(&mos7840_port->pool_lock);
817
Greg Kroah-Hartman0643c722007-06-15 15:44:13 -0700818 if (status) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930819 dbg("nonzero write bulk status received:%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500820 return;
821 }
822
Harvey Harrison441b62c2008-03-03 16:08:34 -0800823 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930824 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500825 return;
826 }
827
Tony Cook84fe6e72009-04-18 22:55:06 +0930828 dbg("%s", "Entering .........");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500829
Alan Cox4a90f092008-10-13 10:39:46 +0100830 tty = tty_port_tty_get(&mos7840_port->port->port);
Jiri Slabyb963a842007-02-10 01:44:55 -0800831 if (tty && mos7840_port->open)
832 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100833 tty_kref_put(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500834
835}
836
837/************************************************************************/
838/* 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 */
839/************************************************************************/
840#ifdef MCSSerialProbe
841static int mos7840_serial_probe(struct usb_serial *serial,
842 const struct usb_device_id *id)
843{
844
845 /*need to implement the mode_reg reading and updating\
846 structures usb_serial_ device_type\
847 (i.e num_ports, num_bulkin,bulkout etc) */
848 /* Also we can update the changes attach */
849 return 1;
850}
851#endif
852
853/*****************************************************************************
854 * mos7840_open
855 * this function is called by the tty driver when a port is opened
856 * If successful, we return 0
857 * Otherwise we return a negative error number.
858 *****************************************************************************/
859
Alan Coxa509a7e2009-09-19 13:13:26 -0700860static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500861{
862 int response;
863 int j;
864 struct usb_serial *serial;
865 struct urb *urb;
866 __u16 Data;
867 int status;
868 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +0100869 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500870
Tony Cook84fe6e72009-04-18 22:55:06 +0930871 dbg ("%s enter", __func__);
872
Harvey Harrison441b62c2008-03-03 16:08:34 -0800873 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930874 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500875 return -ENODEV;
876 }
877
Paul B Schroeder3f542972006-08-31 19:41:47 -0500878 serial = port->serial;
879
Harvey Harrison441b62c2008-03-03 16:08:34 -0800880 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930881 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500882 return -ENODEV;
883 }
884
885 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100886 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500887
Oliver Neukum0de9a702007-03-16 20:28:28 +0100888 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -0500889 return -ENODEV;
890
891 usb_clear_halt(serial->dev, port->write_urb->pipe);
892 usb_clear_halt(serial->dev, port->read_urb->pipe);
Oliver Neukum0de9a702007-03-16 20:28:28 +0100893 port0->open_ports++;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500894
895 /* Initialising the write urb pool */
896 for (j = 0; j < NUM_URBS; ++j) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100897 urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500898 mos7840_port->write_urb_pool[j] = urb;
899
900 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700901 dev_err(&port->dev, "No more urbs???\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500902 continue;
903 }
904
Alan Cox880af9d2008-07-22 11:16:12 +0100905 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
906 GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500907 if (!urb->transfer_buffer) {
Oliver Neukum0de9a702007-03-16 20:28:28 +0100908 usb_free_urb(urb);
909 mos7840_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700910 dev_err(&port->dev,
911 "%s-out of memory for urb buffers.\n",
912 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500913 continue;
914 }
915 }
916
917/*****************************************************************************
918 * Initialize MCS7840 -- Write Init values to corresponding Registers
919 *
920 * Register Index
921 * 1 : IER
922 * 2 : FCR
923 * 3 : LCR
924 * 4 : MCR
925 *
926 * 0x08 : SP1/2 Control Reg
927 *****************************************************************************/
928
Alan Cox880af9d2008-07-22 11:16:12 +0100929 /* NEED to check the following Block */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500930
Paul B Schroeder3f542972006-08-31 19:41:47 -0500931 Data = 0x0;
932 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
933 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930934 dbg("Reading Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500935 return -1;
936 }
937 Data |= 0x80;
938 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
939 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930940 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500941 return -1;
942 }
943
944 Data &= ~0x80;
945 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
946 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930947 dbg("writing Spreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500948 return -1;
949 }
Alan Cox880af9d2008-07-22 11:16:12 +0100950 /* End of block to be checked */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500951
Paul B Schroeder3f542972006-08-31 19:41:47 -0500952 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +0100953 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
954 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500955 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930956 dbg("Reading Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500957 return -1;
958 }
Alan Cox880af9d2008-07-22 11:16:12 +0100959 Data |= 0x08; /* Driver done bit */
960 Data |= 0x20; /* rx_disable */
961 status = mos7840_set_reg_sync(port,
962 mos7840_port->ControlRegOffset, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -0500963 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930964 dbg("writing Controlreg failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500965 return -1;
966 }
Alan Cox880af9d2008-07-22 11:16:12 +0100967 /* do register settings here */
968 /* Set all regs to the device default values. */
969 /***********************************
970 * First Disable all interrupts.
971 ***********************************/
Paul B Schroeder3f542972006-08-31 19:41:47 -0500972 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500973 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
974 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930975 dbg("disabling interrupts failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500976 return -1;
977 }
Alan Cox880af9d2008-07-22 11:16:12 +0100978 /* Set FIFO_CONTROL_REGISTER to the default value */
Paul B Schroeder3f542972006-08-31 19:41:47 -0500979 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500980 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
981 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930982 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500983 return -1;
984 }
985
986 Data = 0xcf;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500987 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
988 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +0930989 dbg("Writing FIFO_CONTROL_REGISTER failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -0500990 return -1;
991 }
992
993 Data = 0x03;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500994 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
995 mos7840_port->shadowLCR = Data;
996
997 Data = 0x0b;
Paul B Schroeder3f542972006-08-31 19:41:47 -0500998 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
999 mos7840_port->shadowMCR = Data;
1000
1001 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001002 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1003 mos7840_port->shadowLCR = Data;
1004
Alan Cox880af9d2008-07-22 11:16:12 +01001005 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001006 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1007
1008 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001009 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1010
1011 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001012 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1013
1014 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001015 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1016
1017 Data = Data & ~SERIAL_LCR_DLAB;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001018 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1019 mos7840_port->shadowLCR = Data;
1020
Alan Cox880af9d2008-07-22 11:16:12 +01001021 /* clearing Bulkin and Bulkout Fifo */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001022 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001023 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1024
1025 Data = Data | 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001026 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1027
1028 Data = Data & ~0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001029 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
Alan Cox880af9d2008-07-22 11:16:12 +01001030 /* Finally enable all interrupts */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001031 Data = 0x0c;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001032 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1033
Alan Cox880af9d2008-07-22 11:16:12 +01001034 /* clearing rx_disable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001035 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001036 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1037 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001038 Data = Data & ~0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01001039 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1040 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001041
Alan Cox880af9d2008-07-22 11:16:12 +01001042 /* rx_negate */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001043 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001044 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1045 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001046 Data = Data | 0x10;
Alan Cox880af9d2008-07-22 11:16:12 +01001047 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1048 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001049
Alan Cox880af9d2008-07-22 11:16:12 +01001050 /* Check to see if we've set up our endpoint info yet *
1051 * (can't set it up in mos7840_startup as the structures *
1052 * were not set up at that time.) */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001053 if (port0->open_ports == 1) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001054 if (serial->port[0]->interrupt_in_buffer == NULL) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001055 /* set up interrupt urb */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001056 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
Alan Cox880af9d2008-07-22 11:16:12 +01001057 serial->dev,
1058 usb_rcvintpipe(serial->dev,
1059 serial->port[0]->interrupt_in_endpointAddress),
1060 serial->port[0]->interrupt_in_buffer,
1061 serial->port[0]->interrupt_in_urb->
1062 transfer_buffer_length,
1063 mos7840_interrupt_callback,
1064 serial,
1065 serial->port[0]->interrupt_in_urb->interval);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001066
1067 /* start interrupt read for mos7840 *
1068 * will continue as long as mos7840 is connected */
1069
1070 response =
1071 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1072 GFP_KERNEL);
1073 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001074 dev_err(&port->dev, "%s - Error %d submitting "
1075 "interrupt urb\n", __func__, response);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001076 }
1077
1078 }
1079
1080 }
1081
1082 /* see if we've set up our endpoint info yet *
1083 * (can't set it up in mos7840_startup as the *
1084 * structures were not set up at that time.) */
1085
Tony Cook84fe6e72009-04-18 22:55:06 +09301086 dbg("port number is %d", port->number);
1087 dbg("serial number is %d", port->serial->minor);
1088 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1089 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1090 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1091 dbg("port's number in the device is %d", mos7840_port->port_num);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001092 mos7840_port->read_urb = port->read_urb;
1093
1094 /* set up our bulk in urb */
1095
1096 usb_fill_bulk_urb(mos7840_port->read_urb,
1097 serial->dev,
1098 usb_rcvbulkpipe(serial->dev,
1099 port->bulk_in_endpointAddress),
1100 port->bulk_in_buffer,
1101 mos7840_port->read_urb->transfer_buffer_length,
1102 mos7840_bulk_in_callback, mos7840_port);
1103
Tony Cook84fe6e72009-04-18 22:55:06 +09301104 dbg("mos7840_open: bulkin endpoint is %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001105 port->bulk_in_endpointAddress);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001106 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001107 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1108 if (response) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001109 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1110 __func__, response);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001111 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001112 }
1113
1114 /* initialize our wait queues */
1115 init_waitqueue_head(&mos7840_port->wait_chase);
1116 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1117
1118 /* initialize our icount structure */
1119 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1120
1121 /* initialize our port settings */
Alan Cox880af9d2008-07-22 11:16:12 +01001122 /* Must set to enable ints! */
1123 mos7840_port->shadowMCR = MCR_MASTER_IE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001124 /* send a open port command */
1125 mos7840_port->open = 1;
Alan Cox880af9d2008-07-22 11:16:12 +01001126 /* mos7840_change_port_settings(mos7840_port,old_termios); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001127 mos7840_port->icount.tx = 0;
1128 mos7840_port->icount.rx = 0;
1129
Tony Cook84fe6e72009-04-18 22:55:06 +09301130 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
Alan Cox880af9d2008-07-22 11:16:12 +01001131 serial, mos7840_port, port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001132
Tony Cook84fe6e72009-04-18 22:55:06 +09301133 dbg ("%s leave", __func__);
1134
Paul B Schroeder3f542972006-08-31 19:41:47 -05001135 return 0;
1136
1137}
1138
1139/*****************************************************************************
1140 * mos7840_chars_in_buffer
1141 * this function is called by the tty driver when it wants to know how many
1142 * bytes of data we currently have outstanding in the port (data that has
1143 * been written, but hasn't made it out the port yet)
1144 * If successful, we return the number of bytes left to be written in the
1145 * system,
Alan Cox95da3102008-07-22 11:09:07 +01001146 * Otherwise we return zero.
Paul B Schroeder3f542972006-08-31 19:41:47 -05001147 *****************************************************************************/
1148
Alan Cox95da3102008-07-22 11:09:07 +01001149static int mos7840_chars_in_buffer(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001150{
Alan Cox95da3102008-07-22 11:09:07 +01001151 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001152 int i;
1153 int chars = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001154 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001155 struct moschip_port *mos7840_port;
1156
Tony Cook84fe6e72009-04-18 22:55:06 +09301157 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001158
Harvey Harrison441b62c2008-03-03 16:08:34 -08001159 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301160 dbg("%s", "Invalid port");
Alan Cox95da3102008-07-22 11:09:07 +01001161 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001162 }
1163
1164 mos7840_port = mos7840_get_port_private(port);
1165 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301166 dbg("%s", "mos7840_break:leaving ...........");
Alan Cox95da3102008-07-22 11:09:07 +01001167 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001168 }
1169
Alan Cox880af9d2008-07-22 11:16:12 +01001170 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Alan Cox95da3102008-07-22 11:09:07 +01001171 for (i = 0; i < NUM_URBS; ++i)
1172 if (mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001173 chars += URB_TRANSFER_BUFFER_SIZE;
Alan Cox880af9d2008-07-22 11:16:12 +01001174 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001175 dbg("%s - returns %d", __func__, chars);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001176 return chars;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001177
1178}
1179
Paul B Schroeder3f542972006-08-31 19:41:47 -05001180/*****************************************************************************
1181 * mos7840_close
1182 * this function is called by the tty driver when a port is closed
1183 *****************************************************************************/
1184
Alan Cox335f8512009-06-11 12:26:29 +01001185static void mos7840_close(struct usb_serial_port *port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001186{
1187 struct usb_serial *serial;
1188 struct moschip_port *mos7840_port;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001189 struct moschip_port *port0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001190 int j;
1191 __u16 Data;
1192
Tony Cook84fe6e72009-04-18 22:55:06 +09301193 dbg("%s", "mos7840_close:entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001194
Harvey Harrison441b62c2008-03-03 16:08:34 -08001195 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301196 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001197 return;
1198 }
1199
Harvey Harrison441b62c2008-03-03 16:08:34 -08001200 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001201 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301202 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001203 return;
1204 }
1205
1206 mos7840_port = mos7840_get_port_private(port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001207 port0 = mos7840_get_port_private(serial->port[0]);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001208
Oliver Neukum0de9a702007-03-16 20:28:28 +01001209 if (mos7840_port == NULL || port0 == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001210 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001211
1212 for (j = 0; j < NUM_URBS; ++j)
1213 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1214
1215 /* Freeing Write URBs */
1216 for (j = 0; j < NUM_URBS; ++j) {
1217 if (mos7840_port->write_urb_pool[j]) {
1218 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1219 kfree(mos7840_port->write_urb_pool[j]->
1220 transfer_buffer);
1221
1222 usb_free_urb(mos7840_port->write_urb_pool[j]);
1223 }
1224 }
1225
Paul B Schroeder3f542972006-08-31 19:41:47 -05001226 /* While closing port, shutdown all bulk read, write *
1227 * and interrupt read if they exists */
1228 if (serial->dev) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001229 if (mos7840_port->write_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301230 dbg("%s", "Shutdown bulk write");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001231 usb_kill_urb(mos7840_port->write_urb);
1232 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001233 if (mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301234 dbg("%s", "Shutdown bulk read");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001235 usb_kill_urb(mos7840_port->read_urb);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001236 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001237 }
1238 if ((&mos7840_port->control_urb)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301239 dbg("%s", "Shutdown control read");
Alan Cox880af9d2008-07-22 11:16:12 +01001240 /*/ usb_kill_urb (mos7840_port->control_urb); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001241 }
1242 }
Alan Cox880af9d2008-07-22 11:16:12 +01001243/* if(mos7840_port->ctrl_buf != NULL) */
1244/* kfree(mos7840_port->ctrl_buf); */
Oliver Neukum0de9a702007-03-16 20:28:28 +01001245 port0->open_ports--;
Tony Cook84fe6e72009-04-18 22:55:06 +09301246 dbg("mos7840_num_open_ports in close%d:in port%d",
Oliver Neukum0de9a702007-03-16 20:28:28 +01001247 port0->open_ports, port->number);
1248 if (port0->open_ports == 0) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05001249 if (serial->port[0]->interrupt_in_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301250 dbg("%s", "Shutdown interrupt_in_urb");
Oliver Neukum0de9a702007-03-16 20:28:28 +01001251 usb_kill_urb(serial->port[0]->interrupt_in_urb);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001252 }
1253 }
1254
1255 if (mos7840_port->write_urb) {
1256 /* if this urb had a transfer buffer already (old tx) free it */
Alan Cox880af9d2008-07-22 11:16:12 +01001257 if (mos7840_port->write_urb->transfer_buffer != NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001258 kfree(mos7840_port->write_urb->transfer_buffer);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001259 usb_free_urb(mos7840_port->write_urb);
1260 }
1261
1262 Data = 0x0;
1263 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1264
1265 Data = 0x00;
1266 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1267
1268 mos7840_port->open = 0;
1269
Tony Cook84fe6e72009-04-18 22:55:06 +09301270 dbg("%s", "Leaving ............");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001271}
1272
1273/************************************************************************
1274 *
1275 * mos7840_block_until_chase_response
1276 *
1277 * This function will block the close until one of the following:
1278 * 1. Response to our Chase comes from mos7840
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001279 * 2. A timeout of 10 seconds without activity has expired
Paul B Schroeder3f542972006-08-31 19:41:47 -05001280 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1281 *
1282 ************************************************************************/
1283
Alan Cox95da3102008-07-22 11:09:07 +01001284static void mos7840_block_until_chase_response(struct tty_struct *tty,
1285 struct moschip_port *mos7840_port)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001286{
1287 int timeout = 1 * HZ;
1288 int wait = 10;
1289 int count;
1290
1291 while (1) {
Alan Cox95da3102008-07-22 11:09:07 +01001292 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001293
1294 /* Check for Buffer status */
Alan Cox880af9d2008-07-22 11:16:12 +01001295 if (count <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001296 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001297
1298 /* Block the thread for a while */
1299 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1300 timeout);
1301 /* No activity.. count down section */
1302 wait--;
1303 if (wait == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001304 dbg("%s - TIMEOUT", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001305 return;
1306 } else {
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001307 /* Reset timeout value back to seconds */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001308 wait = 10;
1309 }
1310 }
1311
1312}
1313
1314/*****************************************************************************
1315 * mos7840_break
1316 * this function sends a break to the port
1317 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001318static void mos7840_break(struct tty_struct *tty, int break_state)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001319{
Alan Cox95da3102008-07-22 11:09:07 +01001320 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001321 unsigned char data;
1322 struct usb_serial *serial;
1323 struct moschip_port *mos7840_port;
1324
Tony Cook84fe6e72009-04-18 22:55:06 +09301325 dbg("%s", "Entering ...........");
1326 dbg("mos7840_break: Start");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001327
Harvey Harrison441b62c2008-03-03 16:08:34 -08001328 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301329 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001330 return;
1331 }
1332
Harvey Harrison441b62c2008-03-03 16:08:34 -08001333 serial = mos7840_get_usb_serial(port, __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001334 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301335 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001336 return;
1337 }
1338
1339 mos7840_port = mos7840_get_port_private(port);
1340
Alan Cox880af9d2008-07-22 11:16:12 +01001341 if (mos7840_port == NULL)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001342 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001343
Alan Cox95da3102008-07-22 11:09:07 +01001344 if (serial->dev)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001345 /* flush and block until tx is empty */
Alan Cox95da3102008-07-22 11:09:07 +01001346 mos7840_block_until_chase_response(tty, mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001347
Alan Cox95da3102008-07-22 11:09:07 +01001348 if (break_state == -1)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001349 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
Alan Cox95da3102008-07-22 11:09:07 +01001350 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05001351 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001352
Alan Cox6b447f042009-01-02 13:48:56 +00001353 /* FIXME: no locking on shadowLCR anywhere in driver */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001354 mos7840_port->shadowLCR = data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301355 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001356 mos7840_port->shadowLCR);
1357 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1358 mos7840_port->shadowLCR);
1359
1360 return;
1361}
1362
1363/*****************************************************************************
1364 * mos7840_write_room
1365 * this function is called by the tty driver when it wants to know how many
1366 * bytes of data we can accept for a specific port.
1367 * If successful, we return the amount of room that we have for this port
1368 * Otherwise we return a negative error number.
1369 *****************************************************************************/
1370
Alan Cox95da3102008-07-22 11:09:07 +01001371static int mos7840_write_room(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001372{
Alan Cox95da3102008-07-22 11:09:07 +01001373 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001374 int i;
1375 int room = 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001376 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001377 struct moschip_port *mos7840_port;
1378
Tony Cook84fe6e72009-04-18 22:55:06 +09301379 dbg("%s", " mos7840_write_room:entering ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001380
Harvey Harrison441b62c2008-03-03 16:08:34 -08001381 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301382 dbg("%s", "Invalid port");
1383 dbg("%s", " mos7840_write_room:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001384 return -1;
1385 }
1386
1387 mos7840_port = mos7840_get_port_private(port);
1388 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301389 dbg("%s", "mos7840_break:leaving ...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001390 return -1;
1391 }
1392
Oliver Neukum0de9a702007-03-16 20:28:28 +01001393 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001394 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox880af9d2008-07-22 11:16:12 +01001395 if (!mos7840_port->busy[i])
Paul B Schroeder3f542972006-08-31 19:41:47 -05001396 room += URB_TRANSFER_BUFFER_SIZE;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001397 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001398 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001399
Oliver Neukum0de9a702007-03-16 20:28:28 +01001400 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001401 dbg("%s - returns %d", __func__, room);
Oliver Neukum0de9a702007-03-16 20:28:28 +01001402 return room;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001403
1404}
1405
1406/*****************************************************************************
1407 * mos7840_write
1408 * this function is called by the tty driver when data should be written to
1409 * the port.
1410 * If successful, we return the number of bytes written, otherwise we
1411 * return a negative error number.
1412 *****************************************************************************/
1413
Alan Cox95da3102008-07-22 11:09:07 +01001414static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001415 const unsigned char *data, int count)
1416{
1417 int status;
1418 int i;
1419 int bytes_sent = 0;
1420 int transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001421 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001422
1423 struct moschip_port *mos7840_port;
1424 struct usb_serial *serial;
1425 struct urb *urb;
Alan Cox880af9d2008-07-22 11:16:12 +01001426 /* __u16 Data; */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001427 const unsigned char *current_position = data;
1428 unsigned char *data1;
Tony Cook84fe6e72009-04-18 22:55:06 +09301429 dbg("%s", "entering ...........");
1430 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Alan Cox880af9d2008-07-22 11:16:12 +01001431 mos7840_port->shadowLCR); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001432
1433#ifdef NOTMOS7840
1434 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001435 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1436 mos7840_port->shadowLCR = Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09301437 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1438 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001439 mos7840_port->shadowLCR);
1440
Alan Cox880af9d2008-07-22 11:16:12 +01001441 /* Data = 0x03; */
1442 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1443 /* mos7840_port->shadowLCR=Data;//Need to add later */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001444
Alan Cox880af9d2008-07-22 11:16:12 +01001445 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001446 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1447
Alan Cox880af9d2008-07-22 11:16:12 +01001448 /* Data = 0x0c; */
1449 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001450 Data = 0x00;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001451 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301452 dbg("mos7840_write:DLL value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001453
1454 Data = 0x0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001455 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09301456 dbg("mos7840_write:DLM value is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001457
1458 Data = Data & ~SERIAL_LCR_DLAB;
Tony Cook84fe6e72009-04-18 22:55:06 +09301459 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05001460 mos7840_port->shadowLCR);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001461 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1462#endif
1463
Harvey Harrison441b62c2008-03-03 16:08:34 -08001464 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301465 dbg("%s", "Port Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001466 return -1;
1467 }
1468
1469 serial = port->serial;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001470 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301471 dbg("%s", "Serial Paranoia failed");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001472 return -1;
1473 }
1474
1475 mos7840_port = mos7840_get_port_private(port);
1476 if (mos7840_port == NULL) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301477 dbg("%s", "mos7840_port is NULL");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001478 return -1;
1479 }
1480
1481 /* try to find a free urb in the list */
1482 urb = NULL;
1483
Oliver Neukum0de9a702007-03-16 20:28:28 +01001484 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001485 for (i = 0; i < NUM_URBS; ++i) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001486 if (!mos7840_port->busy[i]) {
1487 mos7840_port->busy[i] = 1;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001488 urb = mos7840_port->write_urb_pool[i];
Tony Cook84fe6e72009-04-18 22:55:06 +09301489 dbg("URB:%d", i);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001490 break;
1491 }
1492 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01001493 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001494
1495 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001496 dbg("%s - no more free urbs", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001497 goto exit;
1498 }
1499
1500 if (urb->transfer_buffer == NULL) {
1501 urb->transfer_buffer =
1502 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1503
1504 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001505 dev_err(&port->dev, "%s no more kernel memory...\n",
1506 __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001507 goto exit;
1508 }
1509 }
1510 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1511
Al Viro97c49652006-10-09 20:29:03 +01001512 memcpy(urb->transfer_buffer, current_position, transfer_size);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001513
1514 /* fill urb with data and submit */
1515 usb_fill_bulk_urb(urb,
1516 serial->dev,
1517 usb_sndbulkpipe(serial->dev,
1518 port->bulk_out_endpointAddress),
1519 urb->transfer_buffer,
1520 transfer_size,
1521 mos7840_bulk_out_data_callback, mos7840_port);
1522
1523 data1 = urb->transfer_buffer;
Tony Cook84fe6e72009-04-18 22:55:06 +09301524 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001525
1526 /* send it down the pipe */
1527 status = usb_submit_urb(urb, GFP_ATOMIC);
1528
1529 if (status) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01001530 mos7840_port->busy[i] = 0;
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001531 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1532 "with status = %d\n", __func__, status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001533 bytes_sent = status;
1534 goto exit;
1535 }
1536 bytes_sent = transfer_size;
1537 mos7840_port->icount.tx += transfer_size;
Oliver Neukum0de9a702007-03-16 20:28:28 +01001538 smp_wmb();
Tony Cook84fe6e72009-04-18 22:55:06 +09301539 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
Alan Cox95da3102008-07-22 11:09:07 +01001540exit:
Paul B Schroeder3f542972006-08-31 19:41:47 -05001541 return bytes_sent;
1542
1543}
1544
1545/*****************************************************************************
1546 * mos7840_throttle
1547 * this function is called by the tty driver when it wants to stop the data
1548 * being read from the port.
1549 *****************************************************************************/
1550
Alan Cox95da3102008-07-22 11:09:07 +01001551static void mos7840_throttle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001552{
Alan Cox95da3102008-07-22 11:09:07 +01001553 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001554 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001555 int status;
1556
Harvey Harrison441b62c2008-03-03 16:08:34 -08001557 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301558 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001559 return;
1560 }
1561
Tony Cook84fe6e72009-04-18 22:55:06 +09301562 dbg("- port %d", port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001563
1564 mos7840_port = mos7840_get_port_private(port);
1565
1566 if (mos7840_port == NULL)
1567 return;
1568
1569 if (!mos7840_port->open) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301570 dbg("%s", "port not opened");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001571 return;
1572 }
1573
Tony Cook84fe6e72009-04-18 22:55:06 +09301574 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001575
Paul B Schroeder3f542972006-08-31 19:41:47 -05001576 /* if we are implementing XON/XOFF, send the stop character */
1577 if (I_IXOFF(tty)) {
1578 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001579 status = mos7840_write(tty, port, &stop_char, 1);
1580 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001581 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001582 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001583 /* if we are implementing RTS/CTS, toggle that line */
1584 if (tty->termios->c_cflag & CRTSCTS) {
1585 mos7840_port->shadowMCR &= ~MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001586 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001587 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001588 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001589 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001590 }
1591
1592 return;
1593}
1594
1595/*****************************************************************************
1596 * mos7840_unthrottle
Alan Cox880af9d2008-07-22 11:16:12 +01001597 * this function is called by the tty driver when it wants to resume
1598 * the data being read from the port (called after mos7840_throttle is
1599 * called)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001600 *****************************************************************************/
Alan Cox95da3102008-07-22 11:09:07 +01001601static void mos7840_unthrottle(struct tty_struct *tty)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001602{
Alan Cox95da3102008-07-22 11:09:07 +01001603 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001604 int status;
1605 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1606
Harvey Harrison441b62c2008-03-03 16:08:34 -08001607 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301608 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001609 return;
1610 }
1611
1612 if (mos7840_port == NULL)
1613 return;
1614
1615 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001616 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001617 return;
1618 }
1619
Tony Cook84fe6e72009-04-18 22:55:06 +09301620 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001621
Paul B Schroeder3f542972006-08-31 19:41:47 -05001622 /* if we are implementing XON/XOFF, send the start character */
1623 if (I_IXOFF(tty)) {
1624 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001625 status = mos7840_write(tty, port, &start_char, 1);
1626 if (status <= 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001627 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001628 }
1629
1630 /* if we are implementing RTS/CTS, toggle that line */
1631 if (tty->termios->c_cflag & CRTSCTS) {
1632 mos7840_port->shadowMCR |= MCR_RTS;
Alan Cox95da3102008-07-22 11:09:07 +01001633 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001634 mos7840_port->shadowMCR);
Alan Cox95da3102008-07-22 11:09:07 +01001635 if (status < 0)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001636 return;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001637 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001638}
1639
Alan Cox95da3102008-07-22 11:09:07 +01001640static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001641{
Alan Cox95da3102008-07-22 11:09:07 +01001642 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001643 struct moschip_port *mos7840_port;
1644 unsigned int result;
1645 __u16 msr;
1646 __u16 mcr;
Alan Cox880af9d2008-07-22 11:16:12 +01001647 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001648 mos7840_port = mos7840_get_port_private(port);
1649
Harvey Harrison441b62c2008-03-03 16:08:34 -08001650 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001651
1652 if (mos7840_port == NULL)
1653 return -ENODEV;
1654
1655 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1656 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1657 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1658 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1659 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1660 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1661 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1662 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1663 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1664
Harvey Harrison441b62c2008-03-03 16:08:34 -08001665 dbg("%s - 0x%04X", __func__, result);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001666
1667 return result;
1668}
1669
Alan Cox95da3102008-07-22 11:09:07 +01001670static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001671 unsigned int set, unsigned int clear)
1672{
Alan Cox95da3102008-07-22 11:09:07 +01001673 struct usb_serial_port *port = tty->driver_data;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001674 struct moschip_port *mos7840_port;
1675 unsigned int mcr;
Roel Kluin87521c42008-04-17 06:16:24 +02001676 int status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001677
Harvey Harrison441b62c2008-03-03 16:08:34 -08001678 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001679
1680 mos7840_port = mos7840_get_port_private(port);
1681
1682 if (mos7840_port == NULL)
1683 return -ENODEV;
1684
Alan Coxe2984492008-02-20 20:51:45 +00001685 /* FIXME: What locks the port registers ? */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001686 mcr = mos7840_port->shadowMCR;
1687 if (clear & TIOCM_RTS)
1688 mcr &= ~MCR_RTS;
1689 if (clear & TIOCM_DTR)
1690 mcr &= ~MCR_DTR;
1691 if (clear & TIOCM_LOOP)
1692 mcr &= ~MCR_LOOPBACK;
1693
1694 if (set & TIOCM_RTS)
1695 mcr |= MCR_RTS;
1696 if (set & TIOCM_DTR)
1697 mcr |= MCR_DTR;
1698 if (set & TIOCM_LOOP)
1699 mcr |= MCR_LOOPBACK;
1700
1701 mos7840_port->shadowMCR = mcr;
1702
Paul B Schroeder3f542972006-08-31 19:41:47 -05001703 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1704 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301705 dbg("setting MODEM_CONTROL_REGISTER Failed");
Roel Kluin87521c42008-04-17 06:16:24 +02001706 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001707 }
1708
1709 return 0;
1710}
1711
1712/*****************************************************************************
1713 * mos7840_calc_baud_rate_divisor
1714 * this function calculates the proper baud rate divisor for the specified
1715 * baud rate.
1716 *****************************************************************************/
1717static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
Alan Cox880af9d2008-07-22 11:16:12 +01001718 __u16 *clk_sel_val)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001719{
1720
Harvey Harrison441b62c2008-03-03 16:08:34 -08001721 dbg("%s - %d", __func__, baudRate);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001722
1723 if (baudRate <= 115200) {
1724 *divisor = 115200 / baudRate;
1725 *clk_sel_val = 0x0;
1726 }
1727 if ((baudRate > 115200) && (baudRate <= 230400)) {
1728 *divisor = 230400 / baudRate;
1729 *clk_sel_val = 0x10;
1730 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1731 *divisor = 403200 / baudRate;
1732 *clk_sel_val = 0x20;
1733 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1734 *divisor = 460800 / baudRate;
1735 *clk_sel_val = 0x30;
1736 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1737 *divisor = 806400 / baudRate;
1738 *clk_sel_val = 0x40;
1739 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1740 *divisor = 921600 / baudRate;
1741 *clk_sel_val = 0x50;
1742 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1743 *divisor = 1572864 / baudRate;
1744 *clk_sel_val = 0x60;
1745 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1746 *divisor = 3145728 / baudRate;
1747 *clk_sel_val = 0x70;
1748 }
1749 return 0;
1750
1751#ifdef NOTMCS7840
1752
1753 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1754 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1755 *divisor = mos7840_divisor_table[i].Divisor;
1756 return 0;
1757 }
1758 }
1759
1760 /* After trying for all the standard baud rates *
1761 * Try calculating the divisor for this baud rate */
1762
1763 if (baudrate > 75 && baudrate < 230400) {
1764 /* get the divisor */
1765 custom = (__u16) (230400L / baudrate);
1766
1767 /* Check for round off */
1768 round1 = (__u16) (2304000L / baudrate);
1769 round = (__u16) (round1 - (custom * 10));
Alan Cox880af9d2008-07-22 11:16:12 +01001770 if (round > 4)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001771 custom++;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001772 *divisor = custom;
1773
Tony Cook84fe6e72009-04-18 22:55:06 +09301774 dbg(" Baud %d = %d", baudrate, custom);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001775 return 0;
1776 }
1777
Tony Cook84fe6e72009-04-18 22:55:06 +09301778 dbg("%s", " Baud calculation Failed...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001779 return -1;
1780#endif
1781}
1782
1783/*****************************************************************************
1784 * mos7840_send_cmd_write_baud_rate
1785 * this function sends the proper command to change the baud rate of the
1786 * specified port.
1787 *****************************************************************************/
1788
1789static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1790 int baudRate)
1791{
1792 int divisor = 0;
1793 int status;
1794 __u16 Data;
1795 unsigned char number;
1796 __u16 clk_sel_val;
1797 struct usb_serial_port *port;
1798
1799 if (mos7840_port == NULL)
1800 return -1;
1801
1802 port = (struct usb_serial_port *)mos7840_port->port;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001803 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301804 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001805 return -1;
1806 }
1807
Harvey Harrison441b62c2008-03-03 16:08:34 -08001808 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301809 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001810 return -1;
1811 }
1812
Tony Cook84fe6e72009-04-18 22:55:06 +09301813 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001814
1815 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1816
Harvey Harrison441b62c2008-03-03 16:08:34 -08001817 dbg("%s - port = %d, baud = %d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001818 mos7840_port->port->number, baudRate);
Alan Cox880af9d2008-07-22 11:16:12 +01001819 /* reset clk_uart_sel in spregOffset */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001820 if (baudRate > 115200) {
1821#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001822 /* NOTE: need to see the pther register to modify */
1823 /* setting h/w flow control bit to 1 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001824 Data = 0x2b;
1825 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001826 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1827 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001828 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301829 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001830 return -1;
1831 }
1832#endif
1833
1834 } else {
1835#ifdef HW_flow_control
Alan Cox880af9d2008-07-22 11:16:12 +01001836 / *setting h/w flow control bit to 0 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001837 Data = 0xb;
1838 mos7840_port->shadowMCR = Data;
Alan Cox880af9d2008-07-22 11:16:12 +01001839 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1840 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001841 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301842 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001843 return -1;
1844 }
1845#endif
1846
1847 }
1848
Alan Cox880af9d2008-07-22 11:16:12 +01001849 if (1) { /* baudRate <= 115200) */
Paul B Schroeder3f542972006-08-31 19:41:47 -05001850 clk_sel_val = 0x0;
1851 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01001852 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
Paul B Schroeder3f542972006-08-31 19:41:47 -05001853 &clk_sel_val);
Alan Cox880af9d2008-07-22 11:16:12 +01001854 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1855 &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001856 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301857 dbg("reading spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001858 return -1;
1859 }
1860 Data = (Data & 0x8f) | clk_sel_val;
Alan Cox880af9d2008-07-22 11:16:12 +01001861 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1862 Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001863 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301864 dbg("Writing spreg failed in set_serial_baud");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001865 return -1;
1866 }
1867 /* Calculate the Divisor */
1868
1869 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001870 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001871 return status;
1872 }
1873 /* Enable access to divisor latch */
1874 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1875 mos7840_port->shadowLCR = Data;
1876 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1877
1878 /* Write the divisor */
1879 Data = (unsigned char)(divisor & 0xff);
Tony Cook84fe6e72009-04-18 22:55:06 +09301880 dbg("set_serial_baud Value to write DLL is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001881 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1882
1883 Data = (unsigned char)((divisor & 0xff00) >> 8);
Tony Cook84fe6e72009-04-18 22:55:06 +09301884 dbg("set_serial_baud Value to write DLM is %x", Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001885 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1886
1887 /* Disable access to divisor latch */
1888 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1889 mos7840_port->shadowLCR = Data;
1890 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1891
1892 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05001893 return status;
1894}
1895
1896/*****************************************************************************
1897 * mos7840_change_port_settings
1898 * This routine is called to set the UART on the device to match
1899 * the specified new settings.
1900 *****************************************************************************/
1901
Alan Cox95da3102008-07-22 11:09:07 +01001902static void mos7840_change_port_settings(struct tty_struct *tty,
1903 struct moschip_port *mos7840_port, struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001904{
Paul B Schroeder3f542972006-08-31 19:41:47 -05001905 int baud;
1906 unsigned cflag;
1907 unsigned iflag;
1908 __u8 lData;
1909 __u8 lParity;
1910 __u8 lStop;
1911 int status;
1912 __u16 Data;
1913 struct usb_serial_port *port;
1914 struct usb_serial *serial;
1915
1916 if (mos7840_port == NULL)
1917 return;
1918
1919 port = (struct usb_serial_port *)mos7840_port->port;
1920
Harvey Harrison441b62c2008-03-03 16:08:34 -08001921 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301922 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001923 return;
1924 }
1925
Harvey Harrison441b62c2008-03-03 16:08:34 -08001926 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09301927 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001928 return;
1929 }
1930
1931 serial = port->serial;
1932
Harvey Harrison441b62c2008-03-03 16:08:34 -08001933 dbg("%s - port %d", __func__, mos7840_port->port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001934
1935 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001936 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001937 return;
1938 }
1939
Tony Cook84fe6e72009-04-18 22:55:06 +09301940 dbg("%s", "Entering ..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05001941
1942 lData = LCR_BITS_8;
1943 lStop = LCR_STOP_1;
1944 lParity = LCR_PAR_NONE;
1945
1946 cflag = tty->termios->c_cflag;
1947 iflag = tty->termios->c_iflag;
1948
1949 /* Change the number of bits */
1950 if (cflag & CSIZE) {
1951 switch (cflag & CSIZE) {
1952 case CS5:
1953 lData = LCR_BITS_5;
1954 break;
1955
1956 case CS6:
1957 lData = LCR_BITS_6;
1958 break;
1959
1960 case CS7:
1961 lData = LCR_BITS_7;
1962 break;
1963 default:
1964 case CS8:
1965 lData = LCR_BITS_8;
1966 break;
1967 }
1968 }
1969 /* Change the Parity bit */
1970 if (cflag & PARENB) {
1971 if (cflag & PARODD) {
1972 lParity = LCR_PAR_ODD;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001973 dbg("%s - parity = odd", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001974 } else {
1975 lParity = LCR_PAR_EVEN;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001976 dbg("%s - parity = even", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001977 }
1978
1979 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001980 dbg("%s - parity = none", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001981 }
1982
Alan Cox880af9d2008-07-22 11:16:12 +01001983 if (cflag & CMSPAR)
Paul B Schroeder3f542972006-08-31 19:41:47 -05001984 lParity = lParity | 0x20;
Paul B Schroeder3f542972006-08-31 19:41:47 -05001985
1986 /* Change the Stop bit */
1987 if (cflag & CSTOPB) {
1988 lStop = LCR_STOP_2;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001989 dbg("%s - stop bits = 2", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001990 } else {
1991 lStop = LCR_STOP_1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001992 dbg("%s - stop bits = 1", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05001993 }
1994
1995 /* Update the LCR with the correct value */
1996 mos7840_port->shadowLCR &=
1997 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1998 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1999
Tony Cook84fe6e72009-04-18 22:55:06 +09302000 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002001 mos7840_port->shadowLCR);
2002 /* Disable Interrupts */
2003 Data = 0x00;
2004 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2005
2006 Data = 0x00;
2007 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2008
2009 Data = 0xcf;
2010 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2011
2012 /* Send the updated LCR value to the mos7840 */
2013 Data = mos7840_port->shadowLCR;
2014
2015 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2016
2017 Data = 0x00b;
2018 mos7840_port->shadowMCR = Data;
2019 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2020 Data = 0x00b;
2021 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2022
2023 /* set up the MCR register and send it to the mos7840 */
2024
2025 mos7840_port->shadowMCR = MCR_MASTER_IE;
Alan Cox880af9d2008-07-22 11:16:12 +01002026 if (cflag & CBAUD)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002027 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002028
Alan Cox880af9d2008-07-22 11:16:12 +01002029 if (cflag & CRTSCTS)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002030 mos7840_port->shadowMCR |= (MCR_XON_ANY);
Alan Cox880af9d2008-07-22 11:16:12 +01002031 else
Paul B Schroeder3f542972006-08-31 19:41:47 -05002032 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002033
2034 Data = mos7840_port->shadowMCR;
2035 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2036
2037 /* Determine divisor based on baud rate */
2038 baud = tty_get_baud_rate(tty);
2039
2040 if (!baud) {
2041 /* pick a default, any default... */
Tony Cook84fe6e72009-04-18 22:55:06 +09302042 dbg("%s", "Picked default baud...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002043 baud = 9600;
2044 }
2045
Harvey Harrison441b62c2008-03-03 16:08:34 -08002046 dbg("%s - baud rate = %d", __func__, baud);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002047 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2048
2049 /* Enable Interrupts */
2050 Data = 0x0c;
2051 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2052
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002053 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002054 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002055 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002056 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002057 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002058 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002059 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002060 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002061 }
2062 }
2063 wake_up(&mos7840_port->delta_msr_wait);
2064 mos7840_port->delta_msr_cond = 1;
Tony Cook84fe6e72009-04-18 22:55:06 +09302065 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002066 mos7840_port->shadowLCR);
2067
2068 return;
2069}
2070
2071/*****************************************************************************
2072 * mos7840_set_termios
2073 * this function is called by the tty driver when it wants to change
2074 * the termios structure
2075 *****************************************************************************/
2076
Alan Cox95da3102008-07-22 11:09:07 +01002077static void mos7840_set_termios(struct tty_struct *tty,
2078 struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -08002079 struct ktermios *old_termios)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002080{
2081 int status;
2082 unsigned int cflag;
2083 struct usb_serial *serial;
2084 struct moschip_port *mos7840_port;
Tony Cook84fe6e72009-04-18 22:55:06 +09302085 dbg("mos7840_set_termios: START");
Harvey Harrison441b62c2008-03-03 16:08:34 -08002086 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302087 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002088 return;
2089 }
2090
2091 serial = port->serial;
2092
Harvey Harrison441b62c2008-03-03 16:08:34 -08002093 if (mos7840_serial_paranoia_check(serial, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302094 dbg("%s", "Invalid Serial");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002095 return;
2096 }
2097
2098 mos7840_port = mos7840_get_port_private(port);
2099
2100 if (mos7840_port == NULL)
2101 return;
2102
Paul B Schroeder3f542972006-08-31 19:41:47 -05002103 if (!mos7840_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002104 dbg("%s - port not opened", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002105 return;
2106 }
2107
Tony Cook84fe6e72009-04-18 22:55:06 +09302108 dbg("%s", "setting termios - ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002109
2110 cflag = tty->termios->c_cflag;
2111
Harvey Harrison441b62c2008-03-03 16:08:34 -08002112 dbg("%s - clfag %08x iflag %08x", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002113 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002114 dbg("%s - old clfag %08x old iflag %08x", __func__,
Alan Cox3d3ddce2007-10-15 20:53:35 +01002115 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Harvey Harrison441b62c2008-03-03 16:08:34 -08002116 dbg("%s - port %d", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002117
2118 /* change the port settings to the new ones specified */
2119
Alan Cox95da3102008-07-22 11:09:07 +01002120 mos7840_change_port_settings(tty, mos7840_port, old_termios);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002121
2122 if (!mos7840_port->read_urb) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302123 dbg("%s", "URB KILLED !!!!!");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002124 return;
2125 }
2126
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002127 if (mos7840_port->read_urb_busy == false) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002128 mos7840_port->read_urb->dev = serial->dev;
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002129 mos7840_port->read_urb_busy = true;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002130 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2131 if (status) {
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002132 dbg("usb_submit_urb(read bulk) failed, status = %d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002133 status);
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08002134 mos7840_port->read_urb_busy = false;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002135 }
2136 }
2137 return;
2138}
2139
2140/*****************************************************************************
2141 * mos7840_get_lsr_info - get line status register info
2142 *
2143 * Purpose: Let user call ioctl() to get info when the UART physically
2144 * is emptied. On bus types like RS485, the transmitter must
2145 * release the bus after transmitting. This must be done when
2146 * the transmit shift register is empty, not be done when the
2147 * transmit holding register is empty. This functionality
2148 * allows an RS485 driver to be written in user space.
2149 *****************************************************************************/
2150
Alan Cox95da3102008-07-22 11:09:07 +01002151static int mos7840_get_lsr_info(struct tty_struct *tty,
Al Viro97c49652006-10-09 20:29:03 +01002152 unsigned int __user *value)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002153{
2154 int count;
2155 unsigned int result = 0;
2156
Alan Cox95da3102008-07-22 11:09:07 +01002157 count = mos7840_chars_in_buffer(tty);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002158 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002159 dbg("%s -- Empty", __func__);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002160 result = TIOCSER_TEMT;
2161 }
2162
2163 if (copy_to_user(value, &result, sizeof(int)))
2164 return -EFAULT;
2165 return 0;
2166}
2167
2168/*****************************************************************************
Paul B Schroeder3f542972006-08-31 19:41:47 -05002169 * mos7840_get_serial_info
2170 * function to get information about serial port
2171 *****************************************************************************/
2172
2173static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
Al Viro97c49652006-10-09 20:29:03 +01002174 struct serial_struct __user *retinfo)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002175{
2176 struct serial_struct tmp;
2177
2178 if (mos7840_port == NULL)
2179 return -1;
2180
2181 if (!retinfo)
2182 return -EFAULT;
2183
2184 memset(&tmp, 0, sizeof(tmp));
2185
2186 tmp.type = PORT_16550A;
2187 tmp.line = mos7840_port->port->serial->minor;
2188 tmp.port = mos7840_port->port->number;
2189 tmp.irq = 0;
2190 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2191 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2192 tmp.baud_base = 9600;
2193 tmp.close_delay = 5 * HZ;
2194 tmp.closing_wait = 30 * HZ;
2195
2196 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2197 return -EFAULT;
2198 return 0;
2199}
2200
2201/*****************************************************************************
2202 * SerialIoctl
2203 * this function handles any ioctl calls to the driver
2204 *****************************************************************************/
2205
Alan Cox95da3102008-07-22 11:09:07 +01002206static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002207 unsigned int cmd, unsigned long arg)
2208{
Alan Cox95da3102008-07-22 11:09:07 +01002209 struct usb_serial_port *port = tty->driver_data;
Al Viro97c49652006-10-09 20:29:03 +01002210 void __user *argp = (void __user *)arg;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002211 struct moschip_port *mos7840_port;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002212
2213 struct async_icount cnow;
2214 struct async_icount cprev;
2215 struct serial_icounter_struct icount;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002216
Harvey Harrison441b62c2008-03-03 16:08:34 -08002217 if (mos7840_port_paranoia_check(port, __func__)) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302218 dbg("%s", "Invalid port");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002219 return -1;
2220 }
2221
2222 mos7840_port = mos7840_get_port_private(port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002223
2224 if (mos7840_port == NULL)
2225 return -1;
2226
Harvey Harrison441b62c2008-03-03 16:08:34 -08002227 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002228
2229 switch (cmd) {
2230 /* return number of bytes available */
2231
Paul B Schroeder3f542972006-08-31 19:41:47 -05002232 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002233 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox95da3102008-07-22 11:09:07 +01002234 return mos7840_get_lsr_info(tty, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002235 return 0;
2236
Paul B Schroeder3f542972006-08-31 19:41:47 -05002237 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002238 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Al Viro97c49652006-10-09 20:29:03 +01002239 return mos7840_get_serial_info(mos7840_port, argp);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002240
2241 case TIOCSSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002242 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002243 break;
2244
2245 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002246 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002247 cprev = mos7840_port->icount;
2248 while (1) {
Alan Cox880af9d2008-07-22 11:16:12 +01002249 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002250 mos7840_port->delta_msr_cond = 0;
2251 wait_event_interruptible(mos7840_port->delta_msr_wait,
2252 (mos7840_port->
2253 delta_msr_cond == 1));
2254
2255 /* see if a signal did it */
2256 if (signal_pending(current))
2257 return -ERESTARTSYS;
2258 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002259 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002260 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2261 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2262 return -EIO; /* no change => error */
2263 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2264 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2265 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2266 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2267 return 0;
2268 }
2269 cprev = cnow;
2270 }
2271 /* NOTREACHED */
2272 break;
2273
2274 case TIOCGICOUNT:
2275 cnow = mos7840_port->icount;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002276 smp_rmb();
Paul B Schroeder3f542972006-08-31 19:41:47 -05002277 icount.cts = cnow.cts;
2278 icount.dsr = cnow.dsr;
2279 icount.rng = cnow.rng;
2280 icount.dcd = cnow.dcd;
2281 icount.rx = cnow.rx;
2282 icount.tx = cnow.tx;
2283 icount.frame = cnow.frame;
2284 icount.overrun = cnow.overrun;
2285 icount.parity = cnow.parity;
2286 icount.brk = cnow.brk;
2287 icount.buf_overrun = cnow.buf_overrun;
2288
Harvey Harrison441b62c2008-03-03 16:08:34 -08002289 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002290 port->number, icount.rx, icount.tx);
Al Viro97c49652006-10-09 20:29:03 +01002291 if (copy_to_user(argp, &icount, sizeof(icount)))
Paul B Schroeder3f542972006-08-31 19:41:47 -05002292 return -EFAULT;
2293 return 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002294 default:
2295 break;
2296 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002297 return -ENOIOCTLCMD;
2298}
2299
2300static int mos7840_calc_num_ports(struct usb_serial *serial)
2301{
Oliver Neukum0de9a702007-03-16 20:28:28 +01002302 int mos7840_num_ports = 0;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002303
Tony Cook84fe6e72009-04-18 22:55:06 +09302304 dbg("numberofendpoints: cur %d, alt %d",
2305 (int)serial->interface->cur_altsetting->desc.bNumEndpoints,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002306 (int)serial->interface->altsetting->desc.bNumEndpoints);
2307 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002308 mos7840_num_ports = serial->num_ports = 2;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002309 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002310 serial->num_bulk_in = 4;
2311 serial->num_bulk_out = 4;
2312 mos7840_num_ports = serial->num_ports = 4;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002313 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302314 dbg ("mos7840_num_ports = %d", mos7840_num_ports);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002315 return mos7840_num_ports;
2316}
2317
2318/****************************************************************************
2319 * mos7840_startup
2320 ****************************************************************************/
2321
2322static int mos7840_startup(struct usb_serial *serial)
2323{
2324 struct moschip_port *mos7840_port;
2325 struct usb_device *dev;
2326 int i, status;
2327
2328 __u16 Data;
Tony Cook84fe6e72009-04-18 22:55:06 +09302329 dbg("%s", "mos7840_startup :Entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002330
2331 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302332 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002333 return -1;
2334 }
2335
2336 dev = serial->dev;
2337
Tony Cook84fe6e72009-04-18 22:55:06 +09302338 dbg("%s", "Entering...");
2339 dbg ("mos7840_startup: serial = %p", serial);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002340
2341 /* we set up the pointers to the endpoints in the mos7840_open *
2342 * function, as the structures aren't created yet. */
2343
2344 /* set up port private structures */
2345 for (i = 0; i < serial->num_ports; ++i) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302346 dbg ("mos7840_startup: configuring port %d............", i);
Burman Yan7ac9da12006-11-22 20:54:38 +02002347 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002348 if (mos7840_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002349 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002350 status = -ENOMEM;
2351 i--; /* don't follow NULL pointer cleaning up */
2352 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002353 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002354
Alan Cox880af9d2008-07-22 11:16:12 +01002355 /* Initialize all port interrupt end point to port 0 int
2356 * endpoint. Our device has only one interrupt end point
2357 * common to all port */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002358
2359 mos7840_port->port = serial->port[i];
2360 mos7840_set_port_private(serial->port[i], mos7840_port);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002361 spin_lock_init(&mos7840_port->pool_lock);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002362
Tony Cook37768ad2009-04-18 22:42:18 +09302363 /* minor is not initialised until later by
2364 * usb-serial.c:get_free_serial() and cannot therefore be used
2365 * to index device instances */
2366 mos7840_port->port_num = i + 1;
2367 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2368 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2369 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2370 dbg ("serial->minor = %d", serial->minor);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002371
2372 if (mos7840_port->port_num == 1) {
2373 mos7840_port->SpRegOffset = 0x0;
2374 mos7840_port->ControlRegOffset = 0x1;
2375 mos7840_port->DcrRegOffset = 0x4;
2376 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002377 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002378 mos7840_port->SpRegOffset = 0x8;
2379 mos7840_port->ControlRegOffset = 0x9;
2380 mos7840_port->DcrRegOffset = 0x16;
2381 } else if ((mos7840_port->port_num == 2)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002382 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002383 mos7840_port->SpRegOffset = 0xa;
2384 mos7840_port->ControlRegOffset = 0xb;
2385 mos7840_port->DcrRegOffset = 0x19;
2386 } else if ((mos7840_port->port_num == 3)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002387 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002388 mos7840_port->SpRegOffset = 0xa;
2389 mos7840_port->ControlRegOffset = 0xb;
2390 mos7840_port->DcrRegOffset = 0x19;
2391 } else if ((mos7840_port->port_num == 4)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002392 && (serial->num_ports == 4)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002393 mos7840_port->SpRegOffset = 0xc;
2394 mos7840_port->ControlRegOffset = 0xd;
2395 mos7840_port->DcrRegOffset = 0x1c;
2396 }
2397 mos7840_dump_serial_port(mos7840_port);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002398 mos7840_set_port_private(serial->port[i], mos7840_port);
2399
Alan Cox880af9d2008-07-22 11:16:12 +01002400 /* enable rx_disable bit in control register */
2401 status = mos7840_get_reg_sync(serial->port[i],
2402 mos7840_port->ControlRegOffset, &Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002403 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302404 dbg("Reading ControlReg failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002405 break;
2406 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302407 dbg("ControlReg Reading success val is %x, status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002408 Data, status);
Alan Cox880af9d2008-07-22 11:16:12 +01002409 Data |= 0x08; /* setting driver done bit */
2410 Data |= 0x04; /* sp1_bit to have cts change reflect in
2411 modem status reg */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002412
Alan Cox880af9d2008-07-22 11:16:12 +01002413 /* Data |= 0x20; //rx_disable bit */
2414 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002415 mos7840_port->ControlRegOffset, Data);
2416 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302417 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002418 break;
2419 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302420 dbg("ControlReg Writing success(rx_disable) status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002421 status);
2422
Alan Cox880af9d2008-07-22 11:16:12 +01002423 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2424 and 0x24 in DCR3 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002425 Data = 0x01;
Alan Cox880af9d2008-07-22 11:16:12 +01002426 status = mos7840_set_reg_sync(serial->port[i],
2427 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002428 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302429 dbg("Writing DCR0 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002430 break;
2431 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302432 dbg("DCR0 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002433
2434 Data = 0x05;
Alan Cox880af9d2008-07-22 11:16:12 +01002435 status = mos7840_set_reg_sync(serial->port[i],
2436 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002437 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302438 dbg("Writing DCR1 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002439 break;
2440 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302441 dbg("DCR1 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002442
2443 Data = 0x24;
Alan Cox880af9d2008-07-22 11:16:12 +01002444 status = mos7840_set_reg_sync(serial->port[i],
2445 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002446 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302447 dbg("Writing DCR2 failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002448 break;
2449 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302450 dbg("DCR2 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002451
Alan Cox880af9d2008-07-22 11:16:12 +01002452 /* write values in clkstart0x0 and clkmulti 0x20 */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002453 Data = 0x0;
Alan Cox880af9d2008-07-22 11:16:12 +01002454 status = mos7840_set_reg_sync(serial->port[i],
Paul B Schroeder3f542972006-08-31 19:41:47 -05002455 CLK_START_VALUE_REGISTER, Data);
2456 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302457 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002458 break;
2459 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302460 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002461
2462 Data = 0x20;
Alan Cox880af9d2008-07-22 11:16:12 +01002463 status = mos7840_set_reg_sync(serial->port[i],
2464 CLK_MULTI_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002465 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302466 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002467 status);
Oliver Neukum0de9a702007-03-16 20:28:28 +01002468 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002469 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302470 dbg("CLK_MULTI_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002471 status);
2472
Alan Cox880af9d2008-07-22 11:16:12 +01002473 /* write value 0x0 to scratchpad register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002474 Data = 0x00;
Alan Cox880af9d2008-07-22 11:16:12 +01002475 status = mos7840_set_uart_reg(serial->port[i],
2476 SCRATCH_PAD_REGISTER, Data);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002477 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302478 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002479 status);
2480 break;
2481 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302482 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002483 status);
2484
Alan Cox880af9d2008-07-22 11:16:12 +01002485 /* Zero Length flag register */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002486 if ((mos7840_port->port_num != 1)
Oliver Neukum0de9a702007-03-16 20:28:28 +01002487 && (serial->num_ports == 2)) {
Paul B Schroeder3f542972006-08-31 19:41:47 -05002488
2489 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002490 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002491 (__u16) (ZLP_REG1 +
2492 ((__u16)mos7840_port->port_num)), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302493 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002494 (__u16) (ZLP_REG1 +
Alan Cox880af9d2008-07-22 11:16:12 +01002495 ((__u16) mos7840_port->port_num)));
Paul B Schroeder3f542972006-08-31 19:41:47 -05002496 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302497 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002498 i + 2, status);
2499 break;
2500 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302501 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002502 i + 2, status);
2503 } else {
2504 Data = 0xff;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002505 status = mos7840_set_reg_sync(serial->port[i],
Alan Cox880af9d2008-07-22 11:16:12 +01002506 (__u16) (ZLP_REG1 +
2507 ((__u16)mos7840_port->port_num) - 0x1), Data);
Tony Cook84fe6e72009-04-18 22:55:06 +09302508 dbg("ZLIP offset %x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002509 (__u16) (ZLP_REG1 +
2510 ((__u16) mos7840_port->port_num) - 0x1));
2511 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302512 dbg("Writing ZLP_REG%d failed status-0x%x",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002513 i + 1, status);
2514 break;
2515 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302516 dbg("ZLP_REG%d Writing success status%d",
Paul B Schroeder3f542972006-08-31 19:41:47 -05002517 i + 1, status);
2518
2519 }
Oliver Neukum0de9a702007-03-16 20:28:28 +01002520 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002521 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
Alan Cox880af9d2008-07-22 11:16:12 +01002522 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2523 GFP_KERNEL);
2524 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2525 !mos7840_port->dr) {
Oliver Neukum0de9a702007-03-16 20:28:28 +01002526 status = -ENOMEM;
2527 goto error;
2528 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002529 }
Tony Cook84fe6e72009-04-18 22:55:06 +09302530 dbg ("mos7840_startup: all ports configured...........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002531
Alan Cox880af9d2008-07-22 11:16:12 +01002532 /* Zero Length flag enable */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002533 Data = 0x0f;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002534 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2535 if (status < 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302536 dbg("Writing ZLP_REG5 failed status-0x%x", status);
Roel Kluin7ced46c2007-10-27 03:36:37 +02002537 goto error;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002538 } else
Tony Cook84fe6e72009-04-18 22:55:06 +09302539 dbg("ZLP_REG5 Writing success status%d", status);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002540
2541 /* setting configuration feature to one */
2542 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Al Viro97c49652006-10-09 20:29:03 +01002543 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002544 return 0;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002545error:
2546 for (/* nothing */; i >= 0; i--) {
2547 mos7840_port = mos7840_get_port_private(serial->port[i]);
2548
2549 kfree(mos7840_port->dr);
2550 kfree(mos7840_port->ctrl_buf);
2551 usb_free_urb(mos7840_port->control_urb);
2552 kfree(mos7840_port);
2553 serial->port[i] = NULL;
2554 }
2555 return status;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002556}
2557
2558/****************************************************************************
Alan Sternf9c99bb2009-06-02 11:53:55 -04002559 * mos7840_disconnect
Paul B Schroeder3f542972006-08-31 19:41:47 -05002560 * This function is called whenever the device is removed from the usb bus.
2561 ****************************************************************************/
2562
Alan Sternf9c99bb2009-06-02 11:53:55 -04002563static void mos7840_disconnect(struct usb_serial *serial)
Paul B Schroeder3f542972006-08-31 19:41:47 -05002564{
2565 int i;
Oliver Neukum0de9a702007-03-16 20:28:28 +01002566 unsigned long flags;
Paul B Schroeder3f542972006-08-31 19:41:47 -05002567 struct moschip_port *mos7840_port;
Alan Sternf9c99bb2009-06-02 11:53:55 -04002568 dbg("%s", " disconnect :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002569
2570 if (!serial) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302571 dbg("%s", "Invalid Handler");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002572 return;
2573 }
2574
Alan Cox880af9d2008-07-22 11:16:12 +01002575 /* check for the ports to be closed,close the ports and disconnect */
Paul B Schroeder3f542972006-08-31 19:41:47 -05002576
2577 /* free private structure allocated for serial port *
2578 * stop reads and writes on all ports */
2579
2580 for (i = 0; i < serial->num_ports; ++i) {
2581 mos7840_port = mos7840_get_port_private(serial->port[i]);
Tony Cook37768ad2009-04-18 22:42:18 +09302582 dbg ("mos7840_port %d = %p", i, mos7840_port);
2583 if (mos7840_port) {
2584 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2585 mos7840_port->zombie = 1;
2586 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2587 usb_kill_urb(mos7840_port->control_urb);
Alan Sternf9c99bb2009-06-02 11:53:55 -04002588 }
2589 }
2590
2591 dbg("%s", "Thank u :: ");
2592
2593}
2594
2595/****************************************************************************
2596 * mos7840_release
2597 * This function is called when the usb_serial structure is freed.
2598 ****************************************************************************/
2599
2600static void mos7840_release(struct usb_serial *serial)
2601{
2602 int i;
2603 struct moschip_port *mos7840_port;
2604 dbg("%s", " release :entering..........");
2605
2606 if (!serial) {
2607 dbg("%s", "Invalid Handler");
2608 return;
2609 }
2610
2611 /* check for the ports to be closed,close the ports and disconnect */
2612
2613 /* free private structure allocated for serial port *
2614 * stop reads and writes on all ports */
2615
2616 for (i = 0; i < serial->num_ports; ++i) {
2617 mos7840_port = mos7840_get_port_private(serial->port[i]);
2618 dbg("mos7840_port %d = %p", i, mos7840_port);
2619 if (mos7840_port) {
Tony Cook37768ad2009-04-18 22:42:18 +09302620 kfree(mos7840_port->ctrl_buf);
2621 kfree(mos7840_port->dr);
2622 kfree(mos7840_port);
2623 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002624 }
2625
Tony Cook84fe6e72009-04-18 22:55:06 +09302626 dbg("%s", "Thank u :: ");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002627
2628}
2629
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002630static struct usb_driver io_driver = {
2631 .name = "mos7840",
2632 .probe = usb_serial_probe,
2633 .disconnect = usb_serial_disconnect,
2634 .id_table = moschip_id_table_combined,
2635 .no_dynamic_id = 1,
2636};
2637
Paul B Schroeder3f542972006-08-31 19:41:47 -05002638static struct usb_serial_driver moschip7840_4port_device = {
2639 .driver = {
2640 .owner = THIS_MODULE,
2641 .name = "mos7840",
2642 },
2643 .description = DRIVER_DESC,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002644 .usb_driver = &io_driver,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002645 .id_table = moschip_port_id_table,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002646 .num_ports = 4,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002647 .open = mos7840_open,
2648 .close = mos7840_close,
2649 .write = mos7840_write,
2650 .write_room = mos7840_write_room,
2651 .chars_in_buffer = mos7840_chars_in_buffer,
2652 .throttle = mos7840_throttle,
2653 .unthrottle = mos7840_unthrottle,
2654 .calc_num_ports = mos7840_calc_num_ports,
2655#ifdef MCSSerialProbe
2656 .probe = mos7840_serial_probe,
2657#endif
2658 .ioctl = mos7840_ioctl,
2659 .set_termios = mos7840_set_termios,
2660 .break_ctl = mos7840_break,
2661 .tiocmget = mos7840_tiocmget,
2662 .tiocmset = mos7840_tiocmset,
2663 .attach = mos7840_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002664 .disconnect = mos7840_disconnect,
2665 .release = mos7840_release,
Paul B Schroeder3f542972006-08-31 19:41:47 -05002666 .read_bulk_callback = mos7840_bulk_in_callback,
2667 .read_int_callback = mos7840_interrupt_callback,
2668};
2669
Paul B Schroeder3f542972006-08-31 19:41:47 -05002670/****************************************************************************
2671 * moschip7840_init
2672 * This is called by the module subsystem, or on startup to initialize us
2673 ****************************************************************************/
2674static int __init moschip7840_init(void)
2675{
2676 int retval;
2677
Tony Cook84fe6e72009-04-18 22:55:06 +09302678 dbg("%s", " mos7840_init :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002679
2680 /* Register with the usb serial */
2681 retval = usb_serial_register(&moschip7840_4port_device);
2682
2683 if (retval)
2684 goto failed_port_device_register;
2685
Tony Cook84fe6e72009-04-18 22:55:06 +09302686 dbg("%s", "Entering...");
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07002687 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2688 DRIVER_DESC "\n");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002689
2690 /* Register with the usb */
2691 retval = usb_register(&io_driver);
Paul B Schroeder3f542972006-08-31 19:41:47 -05002692 if (retval == 0) {
Tony Cook84fe6e72009-04-18 22:55:06 +09302693 dbg("%s", "Leaving...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002694 return 0;
2695 }
Paul B Schroeder3f542972006-08-31 19:41:47 -05002696 usb_serial_deregister(&moschip7840_4port_device);
Alan Cox880af9d2008-07-22 11:16:12 +01002697failed_port_device_register:
Paul B Schroeder3f542972006-08-31 19:41:47 -05002698 return retval;
2699}
2700
2701/****************************************************************************
2702 * moschip7840_exit
2703 * Called when the driver is about to be unloaded.
2704 ****************************************************************************/
2705static void __exit moschip7840_exit(void)
2706{
2707
Tony Cook84fe6e72009-04-18 22:55:06 +09302708 dbg("%s", " mos7840_exit :entering..........");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002709
2710 usb_deregister(&io_driver);
2711
2712 usb_serial_deregister(&moschip7840_4port_device);
2713
Tony Cook84fe6e72009-04-18 22:55:06 +09302714 dbg("%s", "Entering...");
Paul B Schroeder3f542972006-08-31 19:41:47 -05002715}
2716
2717module_init(moschip7840_init);
2718module_exit(moschip7840_exit);
2719
2720/* Module information */
2721MODULE_DESCRIPTION(DRIVER_DESC);
2722MODULE_LICENSE("GPL");
2723
2724module_param(debug, bool, S_IRUGO | S_IWUSR);
2725MODULE_PARM_DESC(debug, "Debug enabled or not");