Merge tag 'batadv-net-for-davem-20170125' of git://git.open-mesh.org/linux-merge
[linux-2.6-block.git] / drivers / usb / serial / mos7840.c
CommitLineData
3f542974
PS
1/*
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>
3f542974
PS
27#include <linux/slab.h>
28#include <linux/tty.h>
29#include <linux/tty_driver.h>
30#include <linux/tty_flip.h>
31#include <linux/module.h>
32#include <linux/serial.h>
33#include <linux/usb.h>
34#include <linux/usb/serial.h>
880af9db 35#include <linux/uaccess.h>
3f542974 36
3f542974
PS
37#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
38
39/*
40 * 16C50 UART register defines
41 */
42
43#define LCR_BITS_5 0x00 /* 5 bits/char */
44#define LCR_BITS_6 0x01 /* 6 bits/char */
45#define LCR_BITS_7 0x02 /* 7 bits/char */
46#define LCR_BITS_8 0x03 /* 8 bits/char */
47#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
48
49#define LCR_STOP_1 0x00 /* 1 stop bit */
50#define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
51#define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
52#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
53
54#define LCR_PAR_NONE 0x00 /* No parity */
55#define LCR_PAR_ODD 0x08 /* Odd parity */
56#define LCR_PAR_EVEN 0x18 /* Even parity */
57#define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
58#define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
59#define LCR_PAR_MASK 0x38 /* Mask for parity field */
60
61#define LCR_SET_BREAK 0x40 /* Set Break condition */
62#define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
63
64#define MCR_DTR 0x01 /* Assert DTR */
65#define MCR_RTS 0x02 /* Assert RTS */
66#define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
67#define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
68#define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
69#define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
70
71#define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
72#define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
73#define MOS7840_MSR_RI 0x40 /* Current state of RI */
74#define MOS7840_MSR_CD 0x80 /* Current state of CD */
75
76/*
77 * Defines used for sending commands to port
78 */
79
1e658489 80#define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
3f542974
PS
81
82#define MOS_PORT1 0x0200
83#define MOS_PORT2 0x0300
84#define MOS_VENREG 0x0000
85#define MOS_MAX_PORT 0x02
86#define MOS_WRITE 0x0E
87#define MOS_READ 0x0D
88
89/* Requests */
90#define MCS_RD_RTYPE 0xC0
91#define MCS_WR_RTYPE 0x40
92#define MCS_RDREQ 0x0D
93#define MCS_WRREQ 0x0E
94#define MCS_CTRL_TIMEOUT 500
95#define VENDOR_READ_LENGTH (0x01)
96
97#define MAX_NAME_LEN 64
98
880af9db
AC
99#define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
100#define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
3f542974
PS
101
102/* For higher baud Rates use TIOCEXBAUD */
103#define TIOCEXBAUD 0x5462
104
105/* vendor id and device id defines */
106
11e1abb4 107/* The native mos7840/7820 component */
3f542974
PS
108#define USB_VENDOR_ID_MOSCHIP 0x9710
109#define MOSCHIP_DEVICE_ID_7840 0x7840
110#define MOSCHIP_DEVICE_ID_7820 0x7820
0eafe4de 111#define MOSCHIP_DEVICE_ID_7810 0x7810
11e1abb4
DL
112/* The native component can have its vendor/device id's overridden
113 * in vendor-specific implementations. Such devices can be handled
68e24113 114 * by making a change here, in id_table.
11e1abb4 115 */
870408c8
DL
116#define USB_VENDOR_ID_BANDB 0x0856
117#define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
118#define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
119#define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
120#define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
121#define BANDB_DEVICE_ID_US9ML2_2 0xAC29
122#define BANDB_DEVICE_ID_US9ML2_4 0xAC30
123#define BANDB_DEVICE_ID_USPTL4_2 0xAC31
124#define BANDB_DEVICE_ID_USPTL4_4 0xAC32
125#define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
126#define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
127#define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
128#define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
129#define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
3f542974 130
9d498bea
RL
131/* This driver also supports
132 * ATEN UC2324 device using Moschip MCS7840
133 * ATEN UC2322 device using Moschip MCS7820
134 */
e9b8cffa
TC
135#define USB_VENDOR_ID_ATENINTL 0x0557
136#define ATENINTL_DEVICE_ID_UC2324 0x2011
9d498bea 137#define ATENINTL_DEVICE_ID_UC2322 0x7820
e9b8cffa 138
11e1abb4 139/* Interrupt Routine Defines */
3f542974
PS
140
141#define SERIAL_IIR_RLS 0x06
142#define SERIAL_IIR_MS 0x00
143
144/*
145 * Emulation of the bit mask on the LINE STATUS REGISTER.
146 */
147#define SERIAL_LSR_DR 0x0001
148#define SERIAL_LSR_OE 0x0002
149#define SERIAL_LSR_PE 0x0004
150#define SERIAL_LSR_FE 0x0008
151#define SERIAL_LSR_BI 0x0010
152
153#define MOS_MSR_DELTA_CTS 0x10
154#define MOS_MSR_DELTA_DSR 0x20
155#define MOS_MSR_DELTA_RI 0x40
156#define MOS_MSR_DELTA_CD 0x80
157
880af9db 158/* Serial Port register Address */
3f542974
PS
159#define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
160#define FIFO_CONTROL_REGISTER ((__u16)(0x02))
161#define LINE_CONTROL_REGISTER ((__u16)(0x03))
162#define MODEM_CONTROL_REGISTER ((__u16)(0x04))
163#define LINE_STATUS_REGISTER ((__u16)(0x05))
164#define MODEM_STATUS_REGISTER ((__u16)(0x06))
165#define SCRATCH_PAD_REGISTER ((__u16)(0x07))
166#define DIVISOR_LATCH_LSB ((__u16)(0x00))
167#define DIVISOR_LATCH_MSB ((__u16)(0x01))
168
169#define CLK_MULTI_REGISTER ((__u16)(0x02))
170#define CLK_START_VALUE_REGISTER ((__u16)(0x03))
093ea2d3 171#define GPIO_REGISTER ((__u16)(0x07))
3f542974
PS
172
173#define SERIAL_LCR_DLAB ((__u16)(0x0080))
174
175/*
176 * URB POOL related defines
177 */
178#define NUM_URBS 16 /* URB Count */
179#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
180
0eafe4de
D
181/* LED on/off milliseconds*/
182#define LED_ON_MS 500
183#define LED_OFF_MS 500
184
d8a083cc
JH
185enum mos7840_flag {
186 MOS7840_FLAG_CTRL_BUSY,
05cf0dec 187 MOS7840_FLAG_LED_BUSY,
d8a083cc 188};
3f542974 189
b9c87663 190static const struct usb_device_id id_table[] = {
3f542974
PS
191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
192 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
0eafe4de 193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
acf509ae 194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
870408c8 195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
acf509ae 196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
870408c8 197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
acf509ae
CB
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
11e1abb4 202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
870408c8 203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
acf509ae 204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
870408c8 205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
27f1281d 206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
e9b8cffa 207 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
9d498bea 208 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
3f542974
PS
209 {} /* terminating entry */
210};
68e24113 211MODULE_DEVICE_TABLE(usb, id_table);
3f542974
PS
212
213/* This structure holds all of the local port information */
214
215struct moschip_port {
216 int port_num; /*Actual port number in the device(1,2,etc) */
3f542974
PS
217 struct urb *read_urb; /* read URB for this port */
218 __u8 shadowLCR; /* last LCR value received */
219 __u8 shadowMCR; /* last MCR value received */
220 char open;
0de9a702 221 char open_ports;
3f542974
PS
222 struct usb_serial_port *port; /* loop back to the owner of this object */
223
880af9db 224 /* Offsets */
3f542974
PS
225 __u8 SpRegOffset;
226 __u8 ControlRegOffset;
227 __u8 DcrRegOffset;
880af9db 228 /* for processing control URBS in interrupt context */
3f542974 229 struct urb *control_urb;
0de9a702 230 struct usb_ctrlrequest *dr;
3f542974
PS
231 char *ctrl_buf;
232 int MsrLsr;
233
0de9a702 234 spinlock_t pool_lock;
3f542974 235 struct urb *write_urb_pool[NUM_URBS];
0de9a702 236 char busy[NUM_URBS];
50de36f7 237 bool read_urb_busy;
3f542974 238
0eafe4de
D
239 /* For device(s) with LED indicator */
240 bool has_led;
0eafe4de
D
241 struct timer_list led_timer1; /* Timer for LED on */
242 struct timer_list led_timer2; /* Timer for LED off */
05cf0dec
JH
243 struct urb *led_urb;
244 struct usb_ctrlrequest *led_dr;
d8a083cc
JH
245
246 unsigned long flags;
0eafe4de 247};
3f542974 248
3f542974
PS
249/*
250 * mos7840_set_reg_sync
251 * To set the Control register by calling usb_fill_control_urb function
252 * by passing usb_sndctrlpipe function as parameter.
253 */
254
255static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
256 __u16 val)
257{
258 struct usb_device *dev = port->serial->dev;
259 val = val & 0x00ff;
9c134a14 260 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
3f542974
PS
261
262 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
263 MCS_WR_RTYPE, val, reg, NULL, 0,
264 MOS_WDR_TIMEOUT);
265}
266
267/*
268 * mos7840_get_reg_sync
269 * To set the Uart register by calling usb_fill_control_urb function by
270 * passing usb_rcvctrlpipe function as parameter.
271 */
272
273static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
880af9db 274 __u16 *val)
3f542974
PS
275{
276 struct usb_device *dev = port->serial->dev;
277 int ret = 0;
9e221a35
JH
278 u8 *buf;
279
280 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
281 if (!buf)
282 return -ENOMEM;
3f542974
PS
283
284 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
9e221a35 285 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
3f542974 286 MOS_WDR_TIMEOUT);
9e221a35 287 *val = buf[0];
9c134a14 288 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
9e221a35
JH
289
290 kfree(buf);
3f542974
PS
291 return ret;
292}
293
294/*
295 * mos7840_set_uart_reg
296 * To set the Uart register by calling usb_fill_control_urb function by
297 * passing usb_sndctrlpipe function as parameter.
298 */
299
300static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
301 __u16 val)
302{
303
304 struct usb_device *dev = port->serial->dev;
305 val = val & 0x00ff;
880af9db
AC
306 /* For the UART control registers, the application number need
307 to be Or'ed */
0de9a702 308 if (port->serial->num_ports == 4) {
1143832e 309 val |= ((__u16)port->port_number + 1) << 8;
3f542974 310 } else {
1143832e
GKH
311 if (port->port_number == 0) {
312 val |= ((__u16)port->port_number + 1) << 8;
3f542974 313 } else {
1143832e 314 val |= ((__u16)port->port_number + 2) << 8;
3f542974
PS
315 }
316 }
9c134a14 317 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
3f542974
PS
318 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
319 MCS_WR_RTYPE, val, reg, NULL, 0,
320 MOS_WDR_TIMEOUT);
321
322}
323
324/*
325 * mos7840_get_uart_reg
326 * To set the Control register by calling usb_fill_control_urb function
327 * by passing usb_rcvctrlpipe function as parameter.
328 */
329static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
880af9db 330 __u16 *val)
3f542974
PS
331{
332 struct usb_device *dev = port->serial->dev;
333 int ret = 0;
334 __u16 Wval;
9e221a35
JH
335 u8 *buf;
336
337 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
338 if (!buf)
339 return -ENOMEM;
3f542974 340
880af9db 341 /* Wval is same as application number */
0de9a702 342 if (port->serial->num_ports == 4) {
1143832e 343 Wval = ((__u16)port->port_number + 1) << 8;
3f542974 344 } else {
1143832e
GKH
345 if (port->port_number == 0) {
346 Wval = ((__u16)port->port_number + 1) << 8;
3f542974 347 } else {
1143832e 348 Wval = ((__u16)port->port_number + 2) << 8;
3f542974
PS
349 }
350 }
9c134a14 351 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
3f542974 352 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
9e221a35 353 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
3f542974 354 MOS_WDR_TIMEOUT);
9e221a35
JH
355 *val = buf[0];
356
357 kfree(buf);
3f542974
PS
358 return ret;
359}
360
9c134a14
GKH
361static void mos7840_dump_serial_port(struct usb_serial_port *port,
362 struct moschip_port *mos7840_port)
3f542974
PS
363{
364
9c134a14
GKH
365 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
366 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
367 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
3f542974
PS
368
369}
370
371/************************************************************************/
372/************************************************************************/
373/* I N T E R F A C E F U N C T I O N S */
374/* I N T E R F A C E F U N C T I O N S */
375/************************************************************************/
376/************************************************************************/
377
378static inline void mos7840_set_port_private(struct usb_serial_port *port,
379 struct moschip_port *data)
380{
381 usb_set_serial_port_data(port, (void *)data);
382}
383
384static inline struct moschip_port *mos7840_get_port_private(struct
385 usb_serial_port
386 *port)
387{
388 return (struct moschip_port *)usb_get_serial_port_data(port);
389}
390
0de9a702 391static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
3f542974
PS
392{
393 struct moschip_port *mos7840_port;
394 struct async_icount *icount;
395 mos7840_port = port;
3f542974
PS
396 if (new_msr &
397 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
398 MOS_MSR_DELTA_CD)) {
8c1a07ff 399 icount = &mos7840_port->port->icount;
3f542974
PS
400
401 /* update input line counters */
c9fac853 402 if (new_msr & MOS_MSR_DELTA_CTS)
3f542974 403 icount->cts++;
c9fac853 404 if (new_msr & MOS_MSR_DELTA_DSR)
3f542974 405 icount->dsr++;
c9fac853 406 if (new_msr & MOS_MSR_DELTA_CD)
3f542974 407 icount->dcd++;
c9fac853 408 if (new_msr & MOS_MSR_DELTA_RI)
3f542974 409 icount->rng++;
e670c6af 410
0c613371 411 wake_up_interruptible(&port->port->port.delta_msr_wait);
3f542974 412 }
3f542974
PS
413}
414
0de9a702 415static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
3f542974
PS
416{
417 struct async_icount *icount;
418
3f542974 419 if (new_lsr & SERIAL_LSR_BI) {
880af9db
AC
420 /*
421 * Parity and Framing errors only count if they
422 * occur exclusive of a break being
423 * received.
424 */
3f542974
PS
425 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
426 }
427
428 /* update input line counters */
8c1a07ff 429 icount = &port->port->icount;
c9fac853 430 if (new_lsr & SERIAL_LSR_BI)
3f542974 431 icount->brk++;
c9fac853 432 if (new_lsr & SERIAL_LSR_OE)
3f542974 433 icount->overrun++;
c9fac853 434 if (new_lsr & SERIAL_LSR_PE)
3f542974 435 icount->parity++;
c9fac853 436 if (new_lsr & SERIAL_LSR_FE)
3f542974 437 icount->frame++;
3f542974
PS
438}
439
440/************************************************************************/
441/************************************************************************/
442/* U S B C A L L B A C K F U N C T I O N S */
443/* U S B C A L L B A C K F U N C T I O N S */
444/************************************************************************/
445/************************************************************************/
446
7d12e780 447static void mos7840_control_callback(struct urb *urb)
3f542974
PS
448{
449 unsigned char *data;
450 struct moschip_port *mos7840_port;
9c134a14 451 struct device *dev = &urb->dev->dev;
3f542974 452 __u8 regval = 0x0;
0643c724 453 int status = urb->status;
3f542974 454
cdc97792 455 mos7840_port = urb->context;
0de9a702 456
0643c724 457 switch (status) {
3f542974
PS
458 case 0:
459 /* success */
460 break;
461 case -ECONNRESET:
462 case -ENOENT:
463 case -ESHUTDOWN:
464 /* this urb is terminated, clean up */
9c134a14 465 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
d8a083cc 466 goto out;
3f542974 467 default:
9c134a14 468 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
d8a083cc 469 goto out;
3f542974
PS
470 }
471
9c134a14
GKH
472 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
473 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
474 mos7840_port->MsrLsr, mos7840_port->port_num);
3f542974
PS
475 data = urb->transfer_buffer;
476 regval = (__u8) data[0];
9c134a14 477 dev_dbg(dev, "%s data is %x\n", __func__, regval);
3f542974
PS
478 if (mos7840_port->MsrLsr == 0)
479 mos7840_handle_new_msr(mos7840_port, regval);
480 else if (mos7840_port->MsrLsr == 1)
481 mos7840_handle_new_lsr(mos7840_port, regval);
d8a083cc
JH
482out:
483 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
3f542974
PS
484}
485
486static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
880af9db 487 __u16 *val)
3f542974
PS
488{
489 struct usb_device *dev = mcs->port->serial->dev;
0de9a702
ON
490 struct usb_ctrlrequest *dr = mcs->dr;
491 unsigned char *buffer = mcs->ctrl_buf;
492 int ret;
3f542974 493
d8a083cc
JH
494 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
495 return -EBUSY;
496
3f542974
PS
497 dr->bRequestType = MCS_RD_RTYPE;
498 dr->bRequest = MCS_RDREQ;
880af9db 499 dr->wValue = cpu_to_le16(Wval); /* 0 */
3f542974
PS
500 dr->wIndex = cpu_to_le16(reg);
501 dr->wLength = cpu_to_le16(2);
502
503 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
504 (unsigned char *)dr, buffer, 2,
505 mos7840_control_callback, mcs);
506 mcs->control_urb->transfer_buffer_length = 2;
507 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
d8a083cc
JH
508 if (ret)
509 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
510
3f542974
PS
511 return ret;
512}
513
0eafe4de
D
514static void mos7840_set_led_callback(struct urb *urb)
515{
516 switch (urb->status) {
517 case 0:
518 /* Success */
519 break;
520 case -ECONNRESET:
521 case -ENOENT:
522 case -ESHUTDOWN:
523 /* This urb is terminated, clean up */
d9a38a87 524 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
9c134a14 525 __func__, urb->status);
0eafe4de
D
526 break;
527 default:
d9a38a87 528 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
9c134a14 529 __func__, urb->status);
0eafe4de
D
530 }
531}
532
533static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
534 __u16 reg)
535{
536 struct usb_device *dev = mcs->port->serial->dev;
05cf0dec 537 struct usb_ctrlrequest *dr = mcs->led_dr;
0eafe4de
D
538
539 dr->bRequestType = MCS_WR_RTYPE;
540 dr->bRequest = MCS_WRREQ;
541 dr->wValue = cpu_to_le16(wval);
542 dr->wIndex = cpu_to_le16(reg);
543 dr->wLength = cpu_to_le16(0);
544
05cf0dec 545 usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
0eafe4de
D
546 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
547
05cf0dec 548 usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
0eafe4de
D
549}
550
551static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
552 __u16 val)
553{
554 struct usb_device *dev = port->serial->dev;
555
556 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
557 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
558}
559
560static void mos7840_led_off(unsigned long arg)
561{
562 struct moschip_port *mcs = (struct moschip_port *) arg;
563
564 /* Turn off LED */
565 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
566 mod_timer(&mcs->led_timer2,
567 jiffies + msecs_to_jiffies(LED_OFF_MS));
568}
569
570static void mos7840_led_flag_off(unsigned long arg)
571{
572 struct moschip_port *mcs = (struct moschip_port *) arg;
573
05cf0dec
JH
574 clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
575}
576
577static void mos7840_led_activity(struct usb_serial_port *port)
578{
579 struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
580
581 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
582 return;
583
584 mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
585 mod_timer(&mos7840_port->led_timer1,
586 jiffies + msecs_to_jiffies(LED_ON_MS));
0eafe4de
D
587}
588
3f542974
PS
589/*****************************************************************************
590 * mos7840_interrupt_callback
591 * this is the callback function for when we have received data on the
592 * interrupt endpoint.
593 *****************************************************************************/
594
7d12e780 595static void mos7840_interrupt_callback(struct urb *urb)
3f542974
PS
596{
597 int result;
598 int length;
599 struct moschip_port *mos7840_port;
600 struct usb_serial *serial;
601 __u16 Data;
602 unsigned char *data;
603 __u8 sp[5], st;
0de9a702
ON
604 int i, rv = 0;
605 __u16 wval, wreg = 0;
0643c724 606 int status = urb->status;
3f542974 607
0643c724 608 switch (status) {
3f542974
PS
609 case 0:
610 /* success */
611 break;
612 case -ECONNRESET:
613 case -ENOENT:
614 case -ESHUTDOWN:
615 /* this urb is terminated, clean up */
9c134a14
GKH
616 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
617 __func__, status);
3f542974
PS
618 return;
619 default:
9c134a14
GKH
620 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
621 __func__, status);
3f542974
PS
622 goto exit;
623 }
624
625 length = urb->actual_length;
626 data = urb->transfer_buffer;
627
cdc97792 628 serial = urb->context;
3f542974
PS
629
630 /* Moschip get 5 bytes
631 * Byte 1 IIR Port 1 (port.number is 0)
632 * Byte 2 IIR Port 2 (port.number is 1)
633 * Byte 3 IIR Port 3 (port.number is 2)
634 * Byte 4 IIR Port 4 (port.number is 3)
635 * Byte 5 FIFO status for both */
636
365a0442 637 if (length > 5) {
9c134a14 638 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
3f542974
PS
639 return;
640 }
641
642 sp[0] = (__u8) data[0];
643 sp[1] = (__u8) data[1];
644 sp[2] = (__u8) data[2];
645 sp[3] = (__u8) data[3];
646 st = (__u8) data[4];
647
648 for (i = 0; i < serial->num_ports; i++) {
649 mos7840_port = mos7840_get_port_private(serial->port[i]);
1143832e 650 wval = ((__u16)serial->port[i]->port_number + 1) << 8;
3f542974
PS
651 if (mos7840_port->open) {
652 if (sp[i] & 0x01) {
9c134a14 653 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
3f542974
PS
654 } else {
655 switch (sp[i] & 0x0f) {
656 case SERIAL_IIR_RLS:
9c134a14
GKH
657 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
658 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
3f542974 659 mos7840_port->MsrLsr = 1;
0de9a702 660 wreg = LINE_STATUS_REGISTER;
3f542974
PS
661 break;
662 case SERIAL_IIR_MS:
9c134a14 663 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
3f542974 664 mos7840_port->MsrLsr = 0;
0de9a702 665 wreg = MODEM_STATUS_REGISTER;
3f542974
PS
666 break;
667 }
e681b66f 668 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
3f542974
PS
669 }
670 }
671 }
880af9db
AC
672 if (!(rv < 0))
673 /* the completion handler for the control urb will resubmit */
0de9a702
ON
674 return;
675exit:
3f542974
PS
676 result = usb_submit_urb(urb, GFP_ATOMIC);
677 if (result) {
678 dev_err(&urb->dev->dev,
679 "%s - Error %d submitting interrupt urb\n",
441b62c1 680 __func__, result);
3f542974 681 }
3f542974
PS
682}
683
684static int mos7840_port_paranoia_check(struct usb_serial_port *port,
685 const char *function)
686{
687 if (!port) {
9c134a14 688 pr_debug("%s - port == NULL\n", function);
3f542974
PS
689 return -1;
690 }
691 if (!port->serial) {
9c134a14 692 pr_debug("%s - port->serial == NULL\n", function);
3f542974
PS
693 return -1;
694 }
695
696 return 0;
697}
698
699/* Inline functions to check the sanity of a pointer that is passed to us */
700static int mos7840_serial_paranoia_check(struct usb_serial *serial,
701 const char *function)
702{
703 if (!serial) {
9c134a14 704 pr_debug("%s - serial == NULL\n", function);
3f542974
PS
705 return -1;
706 }
707 if (!serial->type) {
9c134a14 708 pr_debug("%s - serial->type == NULL!\n", function);
3f542974
PS
709 return -1;
710 }
711
712 return 0;
713}
714
715static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
716 const char *function)
717{
718 /* if no port was specified, or it fails a paranoia check */
719 if (!port ||
720 mos7840_port_paranoia_check(port, function) ||
721 mos7840_serial_paranoia_check(port->serial, function)) {
880af9db
AC
722 /* then say that we don't have a valid usb_serial thing,
723 * which will end up genrating -ENODEV return values */
3f542974
PS
724 return NULL;
725 }
726
727 return port->serial;
728}
729
730/*****************************************************************************
731 * mos7840_bulk_in_callback
732 * this is the callback function for when we have received data on the
733 * bulk in endpoint.
734 *****************************************************************************/
735
7d12e780 736static void mos7840_bulk_in_callback(struct urb *urb)
3f542974 737{
0643c724 738 int retval;
3f542974
PS
739 unsigned char *data;
740 struct usb_serial *serial;
741 struct usb_serial_port *port;
742 struct moschip_port *mos7840_port;
0643c724 743 int status = urb->status;
3f542974 744
cdc97792 745 mos7840_port = urb->context;
9c134a14 746 if (!mos7840_port)
50de36f7 747 return;
50de36f7
GKH
748
749 if (status) {
9c134a14 750 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
50de36f7 751 mos7840_port->read_urb_busy = false;
3f542974
PS
752 return;
753 }
754
9c134a14 755 port = mos7840_port->port;
441b62c1 756 if (mos7840_port_paranoia_check(port, __func__)) {
50de36f7 757 mos7840_port->read_urb_busy = false;
3f542974
PS
758 return;
759 }
760
441b62c1 761 serial = mos7840_get_usb_serial(port, __func__);
3f542974 762 if (!serial) {
50de36f7 763 mos7840_port->read_urb_busy = false;
3f542974
PS
764 return;
765 }
766
3f542974 767 data = urb->transfer_buffer;
59d33f2f 768 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
3f542974 769
3f542974 770 if (urb->actual_length) {
05c7cd39 771 struct tty_port *tport = &mos7840_port->port->port;
2e124b4a
JS
772 tty_insert_flip_string(tport, data, urb->actual_length);
773 tty_flip_buffer_push(tport);
8c1a07ff
JH
774 port->icount.rx += urb->actual_length;
775 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
3f542974
PS
776 }
777
778 if (!mos7840_port->read_urb) {
9c134a14 779 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
50de36f7 780 mos7840_port->read_urb_busy = false;
3f542974
PS
781 return;
782 }
783
05cf0dec
JH
784 if (mos7840_port->has_led)
785 mos7840_led_activity(port);
3f542974 786
50de36f7 787 mos7840_port->read_urb_busy = true;
0643c724 788 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
0de9a702 789
0643c724 790 if (retval) {
9c134a14 791 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
50de36f7 792 mos7840_port->read_urb_busy = false;
3f542974
PS
793 }
794}
795
796/*****************************************************************************
797 * mos7840_bulk_out_data_callback
880af9db
AC
798 * this is the callback function for when we have finished sending
799 * serial data on the bulk out endpoint.
3f542974
PS
800 *****************************************************************************/
801
7d12e780 802static void mos7840_bulk_out_data_callback(struct urb *urb)
3f542974
PS
803{
804 struct moschip_port *mos7840_port;
9c134a14 805 struct usb_serial_port *port;
0643c724 806 int status = urb->status;
0de9a702
ON
807 int i;
808
cdc97792 809 mos7840_port = urb->context;
9c134a14 810 port = mos7840_port->port;
0de9a702
ON
811 spin_lock(&mos7840_port->pool_lock);
812 for (i = 0; i < NUM_URBS; i++) {
813 if (urb == mos7840_port->write_urb_pool[i]) {
814 mos7840_port->busy[i] = 0;
815 break;
816 }
817 }
818 spin_unlock(&mos7840_port->pool_lock);
819
0643c724 820 if (status) {
9c134a14 821 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
3f542974
PS
822 return;
823 }
824
9c134a14 825 if (mos7840_port_paranoia_check(port, __func__))
3f542974 826 return;
3f542974 827
6aad04f2
JS
828 if (mos7840_port->open)
829 tty_port_tty_wakeup(&port->port);
3f542974
PS
830
831}
832
833/************************************************************************/
834/* 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 */
835/************************************************************************/
3f542974
PS
836
837/*****************************************************************************
838 * mos7840_open
839 * this function is called by the tty driver when a port is opened
840 * If successful, we return 0
841 * Otherwise we return a negative error number.
842 *****************************************************************************/
843
a509a7e4 844static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
3f542974
PS
845{
846 int response;
847 int j;
848 struct usb_serial *serial;
849 struct urb *urb;
850 __u16 Data;
851 int status;
852 struct moschip_port *mos7840_port;
0de9a702 853 struct moschip_port *port0;
3f542974 854
9c134a14 855 if (mos7840_port_paranoia_check(port, __func__))
3f542974 856 return -ENODEV;
3f542974 857
3f542974
PS
858 serial = port->serial;
859
9c134a14 860 if (mos7840_serial_paranoia_check(serial, __func__))
3f542974 861 return -ENODEV;
3f542974
PS
862
863 mos7840_port = mos7840_get_port_private(port);
0de9a702 864 port0 = mos7840_get_port_private(serial->port[0]);
3f542974 865
0de9a702 866 if (mos7840_port == NULL || port0 == NULL)
3f542974
PS
867 return -ENODEV;
868
869 usb_clear_halt(serial->dev, port->write_urb->pipe);
870 usb_clear_halt(serial->dev, port->read_urb->pipe);
0de9a702 871 port0->open_ports++;
3f542974
PS
872
873 /* Initialising the write urb pool */
874 for (j = 0; j < NUM_URBS; ++j) {
0de9a702 875 urb = usb_alloc_urb(0, GFP_KERNEL);
3f542974 876 mos7840_port->write_urb_pool[j] = urb;
10c642d0 877 if (!urb)
3f542974 878 continue;
3f542974 879
880af9db
AC
880 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
881 GFP_KERNEL);
3f542974 882 if (!urb->transfer_buffer) {
0de9a702
ON
883 usb_free_urb(urb);
884 mos7840_port->write_urb_pool[j] = NULL;
3f542974
PS
885 continue;
886 }
887 }
888
889/*****************************************************************************
890 * Initialize MCS7840 -- Write Init values to corresponding Registers
891 *
892 * Register Index
893 * 1 : IER
894 * 2 : FCR
895 * 3 : LCR
896 * 4 : MCR
897 *
898 * 0x08 : SP1/2 Control Reg
899 *****************************************************************************/
900
880af9db 901 /* NEED to check the following Block */
3f542974 902
3f542974
PS
903 Data = 0x0;
904 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
905 if (status < 0) {
9c134a14 906 dev_dbg(&port->dev, "Reading Spreg failed\n");
5f8a2e68 907 goto err;
3f542974
PS
908 }
909 Data |= 0x80;
910 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
911 if (status < 0) {
9c134a14 912 dev_dbg(&port->dev, "writing Spreg failed\n");
5f8a2e68 913 goto err;
3f542974
PS
914 }
915
916 Data &= ~0x80;
917 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
918 if (status < 0) {
9c134a14 919 dev_dbg(&port->dev, "writing Spreg failed\n");
5f8a2e68 920 goto err;
3f542974 921 }
880af9db 922 /* End of block to be checked */
3f542974 923
3f542974 924 Data = 0x0;
880af9db
AC
925 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
926 &Data);
3f542974 927 if (status < 0) {
9c134a14 928 dev_dbg(&port->dev, "Reading Controlreg failed\n");
5f8a2e68 929 goto err;
3f542974 930 }
880af9db
AC
931 Data |= 0x08; /* Driver done bit */
932 Data |= 0x20; /* rx_disable */
933 status = mos7840_set_reg_sync(port,
934 mos7840_port->ControlRegOffset, Data);
3f542974 935 if (status < 0) {
9c134a14 936 dev_dbg(&port->dev, "writing Controlreg failed\n");
5f8a2e68 937 goto err;
3f542974 938 }
880af9db
AC
939 /* do register settings here */
940 /* Set all regs to the device default values. */
941 /***********************************
942 * First Disable all interrupts.
943 ***********************************/
3f542974 944 Data = 0x00;
3f542974
PS
945 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
946 if (status < 0) {
9c134a14 947 dev_dbg(&port->dev, "disabling interrupts failed\n");
5f8a2e68 948 goto err;
3f542974 949 }
880af9db 950 /* Set FIFO_CONTROL_REGISTER to the default value */
3f542974 951 Data = 0x00;
3f542974
PS
952 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
953 if (status < 0) {
9c134a14 954 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
5f8a2e68 955 goto err;
3f542974
PS
956 }
957
958 Data = 0xcf;
3f542974
PS
959 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
960 if (status < 0) {
9c134a14 961 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
5f8a2e68 962 goto err;
3f542974
PS
963 }
964
965 Data = 0x03;
3f542974
PS
966 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
967 mos7840_port->shadowLCR = Data;
968
969 Data = 0x0b;
3f542974
PS
970 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
971 mos7840_port->shadowMCR = Data;
972
973 Data = 0x00;
3f542974
PS
974 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
975 mos7840_port->shadowLCR = Data;
976
880af9db 977 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
3f542974
PS
978 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
979
980 Data = 0x0c;
3f542974
PS
981 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
982
983 Data = 0x0;
3f542974
PS
984 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
985
986 Data = 0x00;
3f542974
PS
987 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
988
989 Data = Data & ~SERIAL_LCR_DLAB;
3f542974
PS
990 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
991 mos7840_port->shadowLCR = Data;
992
880af9db 993 /* clearing Bulkin and Bulkout Fifo */
3f542974 994 Data = 0x0;
3f542974
PS
995 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
996
997 Data = Data | 0x0c;
3f542974
PS
998 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
999
1000 Data = Data & ~0x0c;
3f542974 1001 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
880af9db 1002 /* Finally enable all interrupts */
3f542974 1003 Data = 0x0c;
3f542974
PS
1004 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1005
880af9db 1006 /* clearing rx_disable */
3f542974 1007 Data = 0x0;
880af9db
AC
1008 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1009 &Data);
3f542974 1010 Data = Data & ~0x20;
880af9db
AC
1011 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1012 Data);
3f542974 1013
880af9db 1014 /* rx_negate */
3f542974 1015 Data = 0x0;
880af9db
AC
1016 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1017 &Data);
3f542974 1018 Data = Data | 0x10;
880af9db
AC
1019 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1020 Data);
3f542974 1021
880af9db
AC
1022 /* Check to see if we've set up our endpoint info yet *
1023 * (can't set it up in mos7840_startup as the structures *
1024 * were not set up at that time.) */
0de9a702 1025 if (port0->open_ports == 1) {
3f542974 1026 if (serial->port[0]->interrupt_in_buffer == NULL) {
3f542974 1027 /* set up interrupt urb */
3f542974 1028 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
880af9db
AC
1029 serial->dev,
1030 usb_rcvintpipe(serial->dev,
1031 serial->port[0]->interrupt_in_endpointAddress),
1032 serial->port[0]->interrupt_in_buffer,
1033 serial->port[0]->interrupt_in_urb->
1034 transfer_buffer_length,
1035 mos7840_interrupt_callback,
1036 serial,
1037 serial->port[0]->interrupt_in_urb->interval);
3f542974 1038
472d7e55 1039 /* start interrupt read for mos7840 */
3f542974
PS
1040 response =
1041 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1042 GFP_KERNEL);
1043 if (response) {
194343d9
GKH
1044 dev_err(&port->dev, "%s - Error %d submitting "
1045 "interrupt urb\n", __func__, response);
3f542974
PS
1046 }
1047
1048 }
1049
1050 }
1051
1052 /* see if we've set up our endpoint info yet *
1053 * (can't set it up in mos7840_startup as the *
1054 * structures were not set up at that time.) */
1055
1143832e 1056 dev_dbg(&port->dev, "port number is %d\n", port->port_number);
e5b1e206 1057 dev_dbg(&port->dev, "minor number is %d\n", port->minor);
9c134a14
GKH
1058 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1059 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1060 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1061 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
3f542974
PS
1062 mos7840_port->read_urb = port->read_urb;
1063
1064 /* set up our bulk in urb */
1143832e 1065 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
093ea2d3
DL
1066 usb_fill_bulk_urb(mos7840_port->read_urb,
1067 serial->dev,
1068 usb_rcvbulkpipe(serial->dev,
1069 (port->bulk_in_endpointAddress) + 2),
1070 port->bulk_in_buffer,
1071 mos7840_port->read_urb->transfer_buffer_length,
1072 mos7840_bulk_in_callback, mos7840_port);
1073 } else {
1074 usb_fill_bulk_urb(mos7840_port->read_urb,
1075 serial->dev,
1076 usb_rcvbulkpipe(serial->dev,
1077 port->bulk_in_endpointAddress),
1078 port->bulk_in_buffer,
1079 mos7840_port->read_urb->transfer_buffer_length,
1080 mos7840_bulk_in_callback, mos7840_port);
1081 }
3f542974 1082
9c134a14 1083 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
50de36f7 1084 mos7840_port->read_urb_busy = true;
3f542974
PS
1085 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1086 if (response) {
194343d9
GKH
1087 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1088 __func__, response);
50de36f7 1089 mos7840_port->read_urb_busy = false;
3f542974
PS
1090 }
1091
3f542974 1092 /* initialize our port settings */
880af9db
AC
1093 /* Must set to enable ints! */
1094 mos7840_port->shadowMCR = MCR_MASTER_IE;
3f542974
PS
1095 /* send a open port command */
1096 mos7840_port->open = 1;
880af9db 1097 /* mos7840_change_port_settings(mos7840_port,old_termios); */
3f542974 1098
3f542974 1099 return 0;
5f8a2e68
JH
1100err:
1101 for (j = 0; j < NUM_URBS; ++j) {
1102 urb = mos7840_port->write_urb_pool[j];
1103 if (!urb)
1104 continue;
1105 kfree(urb->transfer_buffer);
1106 usb_free_urb(urb);
1107 }
1108 return status;
3f542974
PS
1109}
1110
1111/*****************************************************************************
1112 * mos7840_chars_in_buffer
1113 * this function is called by the tty driver when it wants to know how many
1114 * bytes of data we currently have outstanding in the port (data that has
1115 * been written, but hasn't made it out the port yet)
1116 * If successful, we return the number of bytes left to be written in the
1117 * system,
95da310e 1118 * Otherwise we return zero.
3f542974
PS
1119 *****************************************************************************/
1120
95da310e 1121static int mos7840_chars_in_buffer(struct tty_struct *tty)
3f542974 1122{
95da310e 1123 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1124 int i;
1125 int chars = 0;
0de9a702 1126 unsigned long flags;
3f542974
PS
1127 struct moschip_port *mos7840_port;
1128
9c134a14 1129 if (mos7840_port_paranoia_check(port, __func__))
95da310e 1130 return 0;
3f542974
PS
1131
1132 mos7840_port = mos7840_get_port_private(port);
3363155b 1133 if (mos7840_port == NULL)
95da310e 1134 return 0;
3f542974 1135
880af9db 1136 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
5c263b92
MF
1137 for (i = 0; i < NUM_URBS; ++i) {
1138 if (mos7840_port->busy[i]) {
1139 struct urb *urb = mos7840_port->write_urb_pool[i];
1140 chars += urb->transfer_buffer_length;
1141 }
1142 }
880af9db 1143 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
9c134a14 1144 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
0de9a702 1145 return chars;
3f542974
PS
1146
1147}
1148
3f542974
PS
1149/*****************************************************************************
1150 * mos7840_close
1151 * this function is called by the tty driver when a port is closed
1152 *****************************************************************************/
1153
335f8514 1154static void mos7840_close(struct usb_serial_port *port)
3f542974
PS
1155{
1156 struct usb_serial *serial;
1157 struct moschip_port *mos7840_port;
0de9a702 1158 struct moschip_port *port0;
3f542974
PS
1159 int j;
1160 __u16 Data;
1161
9c134a14 1162 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1163 return;
3f542974 1164
441b62c1 1165 serial = mos7840_get_usb_serial(port, __func__);
9c134a14 1166 if (!serial)
3f542974 1167 return;
3f542974
PS
1168
1169 mos7840_port = mos7840_get_port_private(port);
0de9a702 1170 port0 = mos7840_get_port_private(serial->port[0]);
3f542974 1171
0de9a702 1172 if (mos7840_port == NULL || port0 == NULL)
3f542974 1173 return;
3f542974
PS
1174
1175 for (j = 0; j < NUM_URBS; ++j)
1176 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1177
1178 /* Freeing Write URBs */
1179 for (j = 0; j < NUM_URBS; ++j) {
1180 if (mos7840_port->write_urb_pool[j]) {
91c72df1 1181 kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
3f542974
PS
1182 usb_free_urb(mos7840_port->write_urb_pool[j]);
1183 }
1184 }
1185
bb3529c6
JH
1186 usb_kill_urb(mos7840_port->read_urb);
1187 mos7840_port->read_urb_busy = false;
1188
0de9a702 1189 port0->open_ports--;
1143832e 1190 dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports);
0de9a702 1191 if (port0->open_ports == 0) {
3f542974 1192 if (serial->port[0]->interrupt_in_urb) {
9c134a14 1193 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
0de9a702 1194 usb_kill_urb(serial->port[0]->interrupt_in_urb);
3f542974
PS
1195 }
1196 }
1197
3f542974
PS
1198 Data = 0x0;
1199 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1200
1201 Data = 0x00;
1202 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1203
1204 mos7840_port->open = 0;
3f542974
PS
1205}
1206
3f542974
PS
1207/*****************************************************************************
1208 * mos7840_break
1209 * this function sends a break to the port
1210 *****************************************************************************/
95da310e 1211static void mos7840_break(struct tty_struct *tty, int break_state)
3f542974 1212{
95da310e 1213 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1214 unsigned char data;
1215 struct usb_serial *serial;
1216 struct moschip_port *mos7840_port;
1217
9c134a14 1218 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1219 return;
3f542974 1220
441b62c1 1221 serial = mos7840_get_usb_serial(port, __func__);
9c134a14 1222 if (!serial)
3f542974 1223 return;
3f542974
PS
1224
1225 mos7840_port = mos7840_get_port_private(port);
1226
880af9db 1227 if (mos7840_port == NULL)
3f542974 1228 return;
3f542974 1229
95da310e 1230 if (break_state == -1)
3f542974 1231 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
95da310e 1232 else
3f542974 1233 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
3f542974 1234
6b447f04 1235 /* FIXME: no locking on shadowLCR anywhere in driver */
3f542974 1236 mos7840_port->shadowLCR = data;
9c134a14 1237 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
3f542974
PS
1238 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1239 mos7840_port->shadowLCR);
3f542974
PS
1240}
1241
1242/*****************************************************************************
1243 * mos7840_write_room
1244 * this function is called by the tty driver when it wants to know how many
1245 * bytes of data we can accept for a specific port.
1246 * If successful, we return the amount of room that we have for this port
1247 * Otherwise we return a negative error number.
1248 *****************************************************************************/
1249
95da310e 1250static int mos7840_write_room(struct tty_struct *tty)
3f542974 1251{
95da310e 1252 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1253 int i;
1254 int room = 0;
0de9a702 1255 unsigned long flags;
3f542974
PS
1256 struct moschip_port *mos7840_port;
1257
9c134a14 1258 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1259 return -1;
3f542974
PS
1260
1261 mos7840_port = mos7840_get_port_private(port);
9c134a14 1262 if (mos7840_port == NULL)
3f542974 1263 return -1;
3f542974 1264
0de9a702 1265 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
3f542974 1266 for (i = 0; i < NUM_URBS; ++i) {
880af9db 1267 if (!mos7840_port->busy[i])
3f542974 1268 room += URB_TRANSFER_BUFFER_SIZE;
3f542974 1269 }
0de9a702 1270 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
3f542974 1271
0de9a702 1272 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
9c134a14 1273 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
0de9a702 1274 return room;
3f542974
PS
1275
1276}
1277
1278/*****************************************************************************
1279 * mos7840_write
1280 * this function is called by the tty driver when data should be written to
1281 * the port.
1282 * If successful, we return the number of bytes written, otherwise we
1283 * return a negative error number.
1284 *****************************************************************************/
1285
95da310e 1286static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
3f542974
PS
1287 const unsigned char *data, int count)
1288{
1289 int status;
1290 int i;
1291 int bytes_sent = 0;
1292 int transfer_size;
0de9a702 1293 unsigned long flags;
3f542974
PS
1294
1295 struct moschip_port *mos7840_port;
1296 struct usb_serial *serial;
1297 struct urb *urb;
880af9db 1298 /* __u16 Data; */
3f542974
PS
1299 const unsigned char *current_position = data;
1300 unsigned char *data1;
3f542974 1301
9c134a14 1302 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1303 return -1;
3f542974
PS
1304
1305 serial = port->serial;
9c134a14 1306 if (mos7840_serial_paranoia_check(serial, __func__))
3f542974 1307 return -1;
3f542974
PS
1308
1309 mos7840_port = mos7840_get_port_private(port);
9c134a14 1310 if (mos7840_port == NULL)
3f542974 1311 return -1;
3f542974
PS
1312
1313 /* try to find a free urb in the list */
1314 urb = NULL;
1315
0de9a702 1316 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
3f542974 1317 for (i = 0; i < NUM_URBS; ++i) {
0de9a702
ON
1318 if (!mos7840_port->busy[i]) {
1319 mos7840_port->busy[i] = 1;
3f542974 1320 urb = mos7840_port->write_urb_pool[i];
9c134a14 1321 dev_dbg(&port->dev, "URB:%d\n", i);
3f542974
PS
1322 break;
1323 }
1324 }
0de9a702 1325 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
3f542974
PS
1326
1327 if (urb == NULL) {
9c134a14 1328 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
3f542974
PS
1329 goto exit;
1330 }
1331
1332 if (urb->transfer_buffer == NULL) {
3b7c7e52
AK
1333 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1334 GFP_ATOMIC);
10c642d0 1335 if (!urb->transfer_buffer)
3f542974 1336 goto exit;
3f542974
PS
1337 }
1338 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1339
97c4965d 1340 memcpy(urb->transfer_buffer, current_position, transfer_size);
3f542974
PS
1341
1342 /* fill urb with data and submit */
1143832e 1343 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
093ea2d3
DL
1344 usb_fill_bulk_urb(urb,
1345 serial->dev,
1346 usb_sndbulkpipe(serial->dev,
1347 (port->bulk_out_endpointAddress) + 2),
1348 urb->transfer_buffer,
1349 transfer_size,
1350 mos7840_bulk_out_data_callback, mos7840_port);
1351 } else {
1352 usb_fill_bulk_urb(urb,
1353 serial->dev,
1354 usb_sndbulkpipe(serial->dev,
1355 port->bulk_out_endpointAddress),
1356 urb->transfer_buffer,
1357 transfer_size,
1358 mos7840_bulk_out_data_callback, mos7840_port);
1359 }
3f542974
PS
1360
1361 data1 = urb->transfer_buffer;
9c134a14 1362 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
3f542974 1363
05cf0dec
JH
1364 if (mos7840_port->has_led)
1365 mos7840_led_activity(port);
0eafe4de 1366
3f542974
PS
1367 /* send it down the pipe */
1368 status = usb_submit_urb(urb, GFP_ATOMIC);
1369
1370 if (status) {
0de9a702 1371 mos7840_port->busy[i] = 0;
22a416c4 1372 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
194343d9 1373 "with status = %d\n", __func__, status);
3f542974
PS
1374 bytes_sent = status;
1375 goto exit;
1376 }
1377 bytes_sent = transfer_size;
8c1a07ff
JH
1378 port->icount.tx += transfer_size;
1379 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
95da310e 1380exit:
3f542974
PS
1381 return bytes_sent;
1382
1383}
1384
1385/*****************************************************************************
1386 * mos7840_throttle
1387 * this function is called by the tty driver when it wants to stop the data
1388 * being read from the port.
1389 *****************************************************************************/
1390
95da310e 1391static void mos7840_throttle(struct tty_struct *tty)
3f542974 1392{
95da310e 1393 struct usb_serial_port *port = tty->driver_data;
3f542974 1394 struct moschip_port *mos7840_port;
3f542974
PS
1395 int status;
1396
9c134a14 1397 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1398 return;
3f542974
PS
1399
1400 mos7840_port = mos7840_get_port_private(port);
1401
1402 if (mos7840_port == NULL)
1403 return;
1404
1405 if (!mos7840_port->open) {
9c134a14 1406 dev_dbg(&port->dev, "%s", "port not opened\n");
3f542974
PS
1407 return;
1408 }
1409
3f542974
PS
1410 /* if we are implementing XON/XOFF, send the stop character */
1411 if (I_IXOFF(tty)) {
1412 unsigned char stop_char = STOP_CHAR(tty);
95da310e
AC
1413 status = mos7840_write(tty, port, &stop_char, 1);
1414 if (status <= 0)
3f542974 1415 return;
3f542974 1416 }
3f542974 1417 /* if we are implementing RTS/CTS, toggle that line */
9db276f8 1418 if (C_CRTSCTS(tty)) {
3f542974 1419 mos7840_port->shadowMCR &= ~MCR_RTS;
95da310e 1420 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
3f542974 1421 mos7840_port->shadowMCR);
95da310e 1422 if (status < 0)
3f542974 1423 return;
3f542974 1424 }
3f542974
PS
1425}
1426
1427/*****************************************************************************
1428 * mos7840_unthrottle
880af9db
AC
1429 * this function is called by the tty driver when it wants to resume
1430 * the data being read from the port (called after mos7840_throttle is
1431 * called)
3f542974 1432 *****************************************************************************/
95da310e 1433static void mos7840_unthrottle(struct tty_struct *tty)
3f542974 1434{
95da310e 1435 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1436 int status;
1437 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1438
9c134a14 1439 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1440 return;
3f542974
PS
1441
1442 if (mos7840_port == NULL)
1443 return;
1444
1445 if (!mos7840_port->open) {
9c134a14 1446 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
3f542974
PS
1447 return;
1448 }
1449
3f542974
PS
1450 /* if we are implementing XON/XOFF, send the start character */
1451 if (I_IXOFF(tty)) {
1452 unsigned char start_char = START_CHAR(tty);
95da310e
AC
1453 status = mos7840_write(tty, port, &start_char, 1);
1454 if (status <= 0)
3f542974 1455 return;
3f542974
PS
1456 }
1457
1458 /* if we are implementing RTS/CTS, toggle that line */
9db276f8 1459 if (C_CRTSCTS(tty)) {
3f542974 1460 mos7840_port->shadowMCR |= MCR_RTS;
95da310e 1461 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
3f542974 1462 mos7840_port->shadowMCR);
95da310e 1463 if (status < 0)
3f542974 1464 return;
3f542974 1465 }
3f542974
PS
1466}
1467
60b33c13 1468static int mos7840_tiocmget(struct tty_struct *tty)
3f542974 1469{
95da310e 1470 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1471 struct moschip_port *mos7840_port;
1472 unsigned int result;
1473 __u16 msr;
1474 __u16 mcr;
880af9db 1475 int status;
3f542974
PS
1476 mos7840_port = mos7840_get_port_private(port);
1477
3f542974
PS
1478 if (mos7840_port == NULL)
1479 return -ENODEV;
1480
1481 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
a91ccd26
JH
1482 if (status != 1)
1483 return -EIO;
3f542974 1484 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
a91ccd26
JH
1485 if (status != 1)
1486 return -EIO;
3f542974
PS
1487 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1488 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1489 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1490 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1491 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1492 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1493 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1494
9c134a14 1495 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
3f542974
PS
1496
1497 return result;
1498}
1499
20b9d177 1500static int mos7840_tiocmset(struct tty_struct *tty,
3f542974
PS
1501 unsigned int set, unsigned int clear)
1502{
95da310e 1503 struct usb_serial_port *port = tty->driver_data;
3f542974
PS
1504 struct moschip_port *mos7840_port;
1505 unsigned int mcr;
87521c46 1506 int status;
3f542974 1507
3f542974
PS
1508 mos7840_port = mos7840_get_port_private(port);
1509
1510 if (mos7840_port == NULL)
1511 return -ENODEV;
1512
e2984494 1513 /* FIXME: What locks the port registers ? */
3f542974
PS
1514 mcr = mos7840_port->shadowMCR;
1515 if (clear & TIOCM_RTS)
1516 mcr &= ~MCR_RTS;
1517 if (clear & TIOCM_DTR)
1518 mcr &= ~MCR_DTR;
1519 if (clear & TIOCM_LOOP)
1520 mcr &= ~MCR_LOOPBACK;
1521
1522 if (set & TIOCM_RTS)
1523 mcr |= MCR_RTS;
1524 if (set & TIOCM_DTR)
1525 mcr |= MCR_DTR;
1526 if (set & TIOCM_LOOP)
1527 mcr |= MCR_LOOPBACK;
1528
1529 mos7840_port->shadowMCR = mcr;
1530
3f542974
PS
1531 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1532 if (status < 0) {
9c134a14 1533 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
87521c46 1534 return status;
3f542974
PS
1535 }
1536
1537 return 0;
1538}
1539
1540/*****************************************************************************
1541 * mos7840_calc_baud_rate_divisor
1542 * this function calculates the proper baud rate divisor for the specified
1543 * baud rate.
1544 *****************************************************************************/
9c134a14
GKH
1545static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1546 int baudRate, int *divisor,
880af9db 1547 __u16 *clk_sel_val)
3f542974 1548{
9c134a14 1549 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
3f542974
PS
1550
1551 if (baudRate <= 115200) {
1552 *divisor = 115200 / baudRate;
1553 *clk_sel_val = 0x0;
1554 }
1555 if ((baudRate > 115200) && (baudRate <= 230400)) {
1556 *divisor = 230400 / baudRate;
1557 *clk_sel_val = 0x10;
1558 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1559 *divisor = 403200 / baudRate;
1560 *clk_sel_val = 0x20;
1561 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1562 *divisor = 460800 / baudRate;
1563 *clk_sel_val = 0x30;
1564 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1565 *divisor = 806400 / baudRate;
1566 *clk_sel_val = 0x40;
1567 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1568 *divisor = 921600 / baudRate;
1569 *clk_sel_val = 0x50;
1570 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1571 *divisor = 1572864 / baudRate;
1572 *clk_sel_val = 0x60;
1573 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1574 *divisor = 3145728 / baudRate;
1575 *clk_sel_val = 0x70;
1576 }
1577 return 0;
3f542974
PS
1578}
1579
1580/*****************************************************************************
1581 * mos7840_send_cmd_write_baud_rate
1582 * this function sends the proper command to change the baud rate of the
1583 * specified port.
1584 *****************************************************************************/
1585
1586static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1587 int baudRate)
1588{
1589 int divisor = 0;
1590 int status;
1591 __u16 Data;
1592 unsigned char number;
1593 __u16 clk_sel_val;
1594 struct usb_serial_port *port;
1595
1596 if (mos7840_port == NULL)
1597 return -1;
1598
9c134a14
GKH
1599 port = mos7840_port->port;
1600 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1601 return -1;
3f542974 1602
9c134a14 1603 if (mos7840_serial_paranoia_check(port->serial, __func__))
3f542974 1604 return -1;
3f542974 1605
1143832e 1606 number = mos7840_port->port->port_number;
3f542974 1607
1143832e 1608 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
880af9db 1609 /* reset clk_uart_sel in spregOffset */
3f542974
PS
1610 if (baudRate > 115200) {
1611#ifdef HW_flow_control
880af9db
AC
1612 /* NOTE: need to see the pther register to modify */
1613 /* setting h/w flow control bit to 1 */
3f542974
PS
1614 Data = 0x2b;
1615 mos7840_port->shadowMCR = Data;
880af9db
AC
1616 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1617 Data);
3f542974 1618 if (status < 0) {
9c134a14 1619 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
3f542974
PS
1620 return -1;
1621 }
1622#endif
1623
1624 } else {
1625#ifdef HW_flow_control
093ea2d3 1626 /* setting h/w flow control bit to 0 */
3f542974
PS
1627 Data = 0xb;
1628 mos7840_port->shadowMCR = Data;
880af9db
AC
1629 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1630 Data);
3f542974 1631 if (status < 0) {
9c134a14 1632 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
3f542974
PS
1633 return -1;
1634 }
1635#endif
1636
1637 }
1638
880af9db 1639 if (1) { /* baudRate <= 115200) */
3f542974
PS
1640 clk_sel_val = 0x0;
1641 Data = 0x0;
9c134a14 1642 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
3f542974 1643 &clk_sel_val);
880af9db
AC
1644 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1645 &Data);
3f542974 1646 if (status < 0) {
9c134a14 1647 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
3f542974
PS
1648 return -1;
1649 }
1650 Data = (Data & 0x8f) | clk_sel_val;
880af9db
AC
1651 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1652 Data);
3f542974 1653 if (status < 0) {
9c134a14 1654 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
3f542974
PS
1655 return -1;
1656 }
1657 /* Calculate the Divisor */
1658
1659 if (status) {
194343d9 1660 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
3f542974
PS
1661 return status;
1662 }
1663 /* Enable access to divisor latch */
1664 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1665 mos7840_port->shadowLCR = Data;
1666 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1667
1668 /* Write the divisor */
1669 Data = (unsigned char)(divisor & 0xff);
9c134a14 1670 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
3f542974
PS
1671 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1672
1673 Data = (unsigned char)((divisor & 0xff00) >> 8);
9c134a14 1674 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
3f542974
PS
1675 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1676
1677 /* Disable access to divisor latch */
1678 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1679 mos7840_port->shadowLCR = Data;
1680 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1681
1682 }
3f542974
PS
1683 return status;
1684}
1685
1686/*****************************************************************************
1687 * mos7840_change_port_settings
1688 * This routine is called to set the UART on the device to match
1689 * the specified new settings.
1690 *****************************************************************************/
1691
95da310e
AC
1692static void mos7840_change_port_settings(struct tty_struct *tty,
1693 struct moschip_port *mos7840_port, struct ktermios *old_termios)
3f542974 1694{
3f542974
PS
1695 int baud;
1696 unsigned cflag;
1697 unsigned iflag;
1698 __u8 lData;
1699 __u8 lParity;
1700 __u8 lStop;
1701 int status;
1702 __u16 Data;
1703 struct usb_serial_port *port;
1704 struct usb_serial *serial;
1705
1706 if (mos7840_port == NULL)
1707 return;
1708
9c134a14 1709 port = mos7840_port->port;
3f542974 1710
9c134a14 1711 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1712 return;
3f542974 1713
9c134a14 1714 if (mos7840_serial_paranoia_check(port->serial, __func__))
3f542974 1715 return;
3f542974
PS
1716
1717 serial = port->serial;
1718
3f542974 1719 if (!mos7840_port->open) {
9c134a14 1720 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
3f542974
PS
1721 return;
1722 }
1723
3f542974
PS
1724 lData = LCR_BITS_8;
1725 lStop = LCR_STOP_1;
1726 lParity = LCR_PAR_NONE;
1727
adc8d746
AC
1728 cflag = tty->termios.c_cflag;
1729 iflag = tty->termios.c_iflag;
3f542974
PS
1730
1731 /* Change the number of bits */
78692cc3
CL
1732 switch (cflag & CSIZE) {
1733 case CS5:
1734 lData = LCR_BITS_5;
1735 break;
3f542974 1736
78692cc3
CL
1737 case CS6:
1738 lData = LCR_BITS_6;
1739 break;
3f542974 1740
78692cc3
CL
1741 case CS7:
1742 lData = LCR_BITS_7;
1743 break;
1744
1745 default:
1746 case CS8:
1747 lData = LCR_BITS_8;
1748 break;
3f542974 1749 }
78692cc3 1750
3f542974
PS
1751 /* Change the Parity bit */
1752 if (cflag & PARENB) {
1753 if (cflag & PARODD) {
1754 lParity = LCR_PAR_ODD;
9c134a14 1755 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
3f542974
PS
1756 } else {
1757 lParity = LCR_PAR_EVEN;
9c134a14 1758 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
3f542974
PS
1759 }
1760
1761 } else {
9c134a14 1762 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
3f542974
PS
1763 }
1764
880af9db 1765 if (cflag & CMSPAR)
3f542974 1766 lParity = lParity | 0x20;
3f542974
PS
1767
1768 /* Change the Stop bit */
1769 if (cflag & CSTOPB) {
1770 lStop = LCR_STOP_2;
9c134a14 1771 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
3f542974
PS
1772 } else {
1773 lStop = LCR_STOP_1;
9c134a14 1774 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
3f542974
PS
1775 }
1776
1777 /* Update the LCR with the correct value */
1778 mos7840_port->shadowLCR &=
1779 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1780 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1781
9c134a14
GKH
1782 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1783 mos7840_port->shadowLCR);
3f542974
PS
1784 /* Disable Interrupts */
1785 Data = 0x00;
1786 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1787
1788 Data = 0x00;
1789 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1790
1791 Data = 0xcf;
1792 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1793
1794 /* Send the updated LCR value to the mos7840 */
1795 Data = mos7840_port->shadowLCR;
1796
1797 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1798
1799 Data = 0x00b;
1800 mos7840_port->shadowMCR = Data;
1801 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1802 Data = 0x00b;
1803 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1804
1805 /* set up the MCR register and send it to the mos7840 */
1806
1807 mos7840_port->shadowMCR = MCR_MASTER_IE;
880af9db 1808 if (cflag & CBAUD)
3f542974 1809 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
3f542974 1810
880af9db 1811 if (cflag & CRTSCTS)
3f542974 1812 mos7840_port->shadowMCR |= (MCR_XON_ANY);
880af9db 1813 else
3f542974 1814 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
3f542974
PS
1815
1816 Data = mos7840_port->shadowMCR;
1817 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1818
1819 /* Determine divisor based on baud rate */
1820 baud = tty_get_baud_rate(tty);
1821
1822 if (!baud) {
1823 /* pick a default, any default... */
9c134a14 1824 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
3f542974
PS
1825 baud = 9600;
1826 }
1827
9c134a14 1828 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
3f542974
PS
1829 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1830
1831 /* Enable Interrupts */
1832 Data = 0x0c;
1833 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1834
ce9d8562 1835 if (!mos7840_port->read_urb_busy) {
50de36f7 1836 mos7840_port->read_urb_busy = true;
9d380190 1837 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
3f542974 1838 if (status) {
9c134a14 1839 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
3f542974 1840 status);
50de36f7 1841 mos7840_port->read_urb_busy = false;
3f542974
PS
1842 }
1843 }
9c134a14
GKH
1844 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1845 mos7840_port->shadowLCR);
3f542974
PS
1846}
1847
1848/*****************************************************************************
1849 * mos7840_set_termios
1850 * this function is called by the tty driver when it wants to change
1851 * the termios structure
1852 *****************************************************************************/
1853
95da310e
AC
1854static void mos7840_set_termios(struct tty_struct *tty,
1855 struct usb_serial_port *port,
606d099c 1856 struct ktermios *old_termios)
3f542974
PS
1857{
1858 int status;
1859 unsigned int cflag;
1860 struct usb_serial *serial;
1861 struct moschip_port *mos7840_port;
3363155b 1862
9c134a14 1863 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1864 return;
3f542974
PS
1865
1866 serial = port->serial;
1867
9c134a14 1868 if (mos7840_serial_paranoia_check(serial, __func__))
3f542974 1869 return;
3f542974
PS
1870
1871 mos7840_port = mos7840_get_port_private(port);
1872
1873 if (mos7840_port == NULL)
1874 return;
1875
3f542974 1876 if (!mos7840_port->open) {
9c134a14 1877 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
3f542974
PS
1878 return;
1879 }
1880
9c134a14 1881 dev_dbg(&port->dev, "%s", "setting termios - \n");
3f542974 1882
adc8d746 1883 cflag = tty->termios.c_cflag;
3f542974 1884
9c134a14 1885 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
d9a80746 1886 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
9c134a14
GKH
1887 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
1888 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
3f542974
PS
1889
1890 /* change the port settings to the new ones specified */
1891
95da310e 1892 mos7840_change_port_settings(tty, mos7840_port, old_termios);
3f542974
PS
1893
1894 if (!mos7840_port->read_urb) {
9c134a14 1895 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
3f542974
PS
1896 return;
1897 }
1898
ce9d8562 1899 if (!mos7840_port->read_urb_busy) {
50de36f7 1900 mos7840_port->read_urb_busy = true;
9d380190 1901 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
3f542974 1902 if (status) {
9c134a14 1903 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
3f542974 1904 status);
50de36f7 1905 mos7840_port->read_urb_busy = false;
3f542974
PS
1906 }
1907 }
3f542974
PS
1908}
1909
1910/*****************************************************************************
1911 * mos7840_get_lsr_info - get line status register info
1912 *
1913 * Purpose: Let user call ioctl() to get info when the UART physically
1914 * is emptied. On bus types like RS485, the transmitter must
1915 * release the bus after transmitting. This must be done when
1916 * the transmit shift register is empty, not be done when the
1917 * transmit holding register is empty. This functionality
1918 * allows an RS485 driver to be written in user space.
1919 *****************************************************************************/
1920
95da310e 1921static int mos7840_get_lsr_info(struct tty_struct *tty,
97c4965d 1922 unsigned int __user *value)
3f542974
PS
1923{
1924 int count;
1925 unsigned int result = 0;
1926
95da310e 1927 count = mos7840_chars_in_buffer(tty);
9c134a14 1928 if (count == 0)
3f542974 1929 result = TIOCSER_TEMT;
3f542974
PS
1930
1931 if (copy_to_user(value, &result, sizeof(int)))
1932 return -EFAULT;
1933 return 0;
1934}
1935
3f542974
PS
1936/*****************************************************************************
1937 * mos7840_get_serial_info
1938 * function to get information about serial port
1939 *****************************************************************************/
1940
1941static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
97c4965d 1942 struct serial_struct __user *retinfo)
3f542974
PS
1943{
1944 struct serial_struct tmp;
1945
1946 if (mos7840_port == NULL)
1947 return -1;
1948
3f542974
PS
1949 memset(&tmp, 0, sizeof(tmp));
1950
1951 tmp.type = PORT_16550A;
e5b1e206 1952 tmp.line = mos7840_port->port->minor;
1143832e 1953 tmp.port = mos7840_port->port->port_number;
3f542974
PS
1954 tmp.irq = 0;
1955 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1956 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1957 tmp.baud_base = 9600;
1958 tmp.close_delay = 5 * HZ;
1959 tmp.closing_wait = 30 * HZ;
1960
1961 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1962 return -EFAULT;
1963 return 0;
1964}
1965
1966/*****************************************************************************
1967 * SerialIoctl
1968 * this function handles any ioctl calls to the driver
1969 *****************************************************************************/
1970
00a0d0d6 1971static int mos7840_ioctl(struct tty_struct *tty,
3f542974
PS
1972 unsigned int cmd, unsigned long arg)
1973{
95da310e 1974 struct usb_serial_port *port = tty->driver_data;
97c4965d 1975 void __user *argp = (void __user *)arg;
3f542974 1976 struct moschip_port *mos7840_port;
3f542974 1977
9c134a14 1978 if (mos7840_port_paranoia_check(port, __func__))
3f542974 1979 return -1;
3f542974
PS
1980
1981 mos7840_port = mos7840_get_port_private(port);
3f542974
PS
1982
1983 if (mos7840_port == NULL)
1984 return -1;
1985
3f542974
PS
1986 switch (cmd) {
1987 /* return number of bytes available */
1988
3f542974 1989 case TIOCSERGETLSR:
9c134a14 1990 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
95da310e 1991 return mos7840_get_lsr_info(tty, argp);
3f542974 1992
3f542974 1993 case TIOCGSERIAL:
9c134a14 1994 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
97c4965d 1995 return mos7840_get_serial_info(mos7840_port, argp);
3f542974
PS
1996
1997 case TIOCSSERIAL:
9c134a14 1998 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
3f542974 1999 break;
3f542974
PS
2000 default:
2001 break;
2002 }
3f542974
PS
2003 return -ENOIOCTLCMD;
2004}
2005
0eafe4de
D
2006static int mos7810_check(struct usb_serial *serial)
2007{
2008 int i, pass_count = 0;
15ee89c3 2009 u8 *buf;
0eafe4de
D
2010 __u16 data = 0, mcr_data = 0;
2011 __u16 test_pattern = 0x55AA;
15ee89c3
JH
2012 int res;
2013
2014 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2015 if (!buf)
2016 return 0; /* failed to identify 7810 */
0eafe4de
D
2017
2018 /* Store MCR setting */
15ee89c3 2019 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
0eafe4de 2020 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
15ee89c3
JH
2021 buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2022 if (res == VENDOR_READ_LENGTH)
2023 mcr_data = *buf;
0eafe4de
D
2024
2025 for (i = 0; i < 16; i++) {
2026 /* Send the 1-bit test pattern out to MCS7810 test pin */
2027 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2028 MCS_WRREQ, MCS_WR_RTYPE,
2029 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2030 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2031
2032 /* Read the test pattern back */
15ee89c3
JH
2033 res = usb_control_msg(serial->dev,
2034 usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
2035 MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2036 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2037 if (res == VENDOR_READ_LENGTH)
2038 data = *buf;
0eafe4de
D
2039
2040 /* If this is a MCS7810 device, both test patterns must match */
2041 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2042 break;
2043
2044 pass_count++;
2045 }
2046
2047 /* Restore MCR setting */
2048 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2049 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2050 0, MOS_WDR_TIMEOUT);
2051
15ee89c3
JH
2052 kfree(buf);
2053
0eafe4de
D
2054 if (pass_count == 16)
2055 return 1;
2056
2057 return 0;
2058}
2059
40c24f28
JH
2060static int mos7840_probe(struct usb_serial *serial,
2061 const struct usb_device_id *id)
3f542974 2062{
d551ec9b 2063 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
15ee89c3 2064 u8 *buf;
40c24f28
JH
2065 int device_type;
2066
2067 if (product == MOSCHIP_DEVICE_ID_7810 ||
2068 product == MOSCHIP_DEVICE_ID_7820) {
2069 device_type = product;
2070 goto out;
2071 }
093ea2d3 2072
15ee89c3 2073 buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
40c24f28
JH
2074 if (!buf)
2075 return -ENOMEM;
2076
2077 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
15ee89c3
JH
2078 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2079 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
093ea2d3 2080
40c24f28
JH
2081 /* For a MCS7840 device GPIO0 must be set to 1 */
2082 if (buf[0] & 0x01)
2083 device_type = MOSCHIP_DEVICE_ID_7840;
2084 else if (mos7810_check(serial))
2085 device_type = MOSCHIP_DEVICE_ID_7810;
2086 else
2087 device_type = MOSCHIP_DEVICE_ID_7820;
2088
2089 kfree(buf);
2090out:
683a0e4d 2091 usb_set_serial_data(serial, (void *)(unsigned long)device_type);
40c24f28
JH
2092
2093 return 0;
2094}
2095
2096static int mos7840_calc_num_ports(struct usb_serial *serial)
2097{
683a0e4d 2098 int device_type = (unsigned long)usb_get_serial_data(serial);
40c24f28 2099 int mos7840_num_ports;
093ea2d3 2100
0eafe4de 2101 mos7840_num_ports = (device_type >> 4) & 0x000F;
0eafe4de 2102
3f542974
PS
2103 return mos7840_num_ports;
2104}
2105
5c75633e
JH
2106static int mos7840_attach(struct usb_serial *serial)
2107{
2108 if (serial->num_bulk_in < serial->num_ports ||
2109 serial->num_bulk_out < serial->num_ports) {
2110 dev_err(&serial->interface->dev, "missing endpoints\n");
2111 return -ENODEV;
2112 }
2113
2114 return 0;
2115}
2116
80c00750 2117static int mos7840_port_probe(struct usb_serial_port *port)
3f542974 2118{
80c00750 2119 struct usb_serial *serial = port->serial;
683a0e4d 2120 int device_type = (unsigned long)usb_get_serial_data(serial);
3f542974 2121 struct moschip_port *mos7840_port;
80c00750
JH
2122 int status;
2123 int pnum;
3f542974 2124 __u16 Data;
3f542974 2125
3f542974
PS
2126 /* we set up the pointers to the endpoints in the mos7840_open *
2127 * function, as the structures aren't created yet. */
2128
1143832e 2129 pnum = port->port_number;
80c00750 2130
ae685eff
JH
2131 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2132 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
10c642d0 2133 if (!mos7840_port)
ae685eff 2134 return -ENOMEM;
3f542974 2135
ae685eff
JH
2136 /* Initialize all port interrupt end point to port 0 int
2137 * endpoint. Our device has only one interrupt end point
2138 * common to all port */
2139
2140 mos7840_port->port = port;
2141 mos7840_set_port_private(port, mos7840_port);
2142 spin_lock_init(&mos7840_port->pool_lock);
2143
2144 /* minor is not initialised until later by
2145 * usb-serial.c:get_free_serial() and cannot therefore be used
2146 * to index device instances */
2147 mos7840_port->port_num = pnum + 1;
e5b1e206 2148 dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
ae685eff 2149 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
ae685eff
JH
2150
2151 if (mos7840_port->port_num == 1) {
2152 mos7840_port->SpRegOffset = 0x0;
2153 mos7840_port->ControlRegOffset = 0x1;
2154 mos7840_port->DcrRegOffset = 0x4;
2155 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2156 mos7840_port->SpRegOffset = 0x8;
2157 mos7840_port->ControlRegOffset = 0x9;
2158 mos7840_port->DcrRegOffset = 0x16;
2159 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2160 mos7840_port->SpRegOffset = 0xa;
2161 mos7840_port->ControlRegOffset = 0xb;
2162 mos7840_port->DcrRegOffset = 0x19;
2163 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2164 mos7840_port->SpRegOffset = 0xa;
2165 mos7840_port->ControlRegOffset = 0xb;
2166 mos7840_port->DcrRegOffset = 0x19;
2167 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2168 mos7840_port->SpRegOffset = 0xc;
2169 mos7840_port->ControlRegOffset = 0xd;
2170 mos7840_port->DcrRegOffset = 0x1c;
2171 }
2172 mos7840_dump_serial_port(port, mos7840_port);
2173 mos7840_set_port_private(port, mos7840_port);
2174
2175 /* enable rx_disable bit in control register */
2176 status = mos7840_get_reg_sync(port,
2177 mos7840_port->ControlRegOffset, &Data);
2178 if (status < 0) {
2179 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2180 goto out;
2181 } else
2182 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2183 Data |= 0x08; /* setting driver done bit */
2184 Data |= 0x04; /* sp1_bit to have cts change reflect in
2185 modem status reg */
2186
2187 /* Data |= 0x20; //rx_disable bit */
2188 status = mos7840_set_reg_sync(port,
2189 mos7840_port->ControlRegOffset, Data);
2190 if (status < 0) {
2191 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2192 goto out;
2193 } else
2194 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2195
2196 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2197 and 0x24 in DCR3 */
2198 Data = 0x01;
2199 status = mos7840_set_reg_sync(port,
2200 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2201 if (status < 0) {
2202 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2203 goto out;
2204 } else
2205 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
3f542974 2206
ae685eff
JH
2207 Data = 0x05;
2208 status = mos7840_set_reg_sync(port,
2209 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2210 if (status < 0) {
2211 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2212 goto out;
2213 } else
2214 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
3f542974 2215
ae685eff
JH
2216 Data = 0x24;
2217 status = mos7840_set_reg_sync(port,
2218 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2219 if (status < 0) {
2220 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2221 goto out;
2222 } else
2223 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
3f542974 2224
ae685eff
JH
2225 /* write values in clkstart0x0 and clkmulti 0x20 */
2226 Data = 0x0;
2227 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2228 if (status < 0) {
2229 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2230 goto out;
2231 } else
2232 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
3f542974 2233
ae685eff
JH
2234 Data = 0x20;
2235 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2236 if (status < 0) {
2237 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2238 goto error;
2239 } else
2240 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
3f542974 2241
ae685eff
JH
2242 /* write value 0x0 to scratchpad register */
2243 Data = 0x00;
2244 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2245 if (status < 0) {
2246 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2247 goto out;
2248 } else
2249 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2250
2251 /* Zero Length flag register */
2252 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2253 Data = 0xff;
80c00750 2254 status = mos7840_set_reg_sync(port,
ae685eff
JH
2255 (__u16) (ZLP_REG1 +
2256 ((__u16)mos7840_port->port_num)), Data);
2257 dev_dbg(&port->dev, "ZLIP offset %x\n",
2258 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
3f542974 2259 if (status < 0) {
ae685eff
JH
2260 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2261 goto out;
3f542974 2262 } else
ae685eff
JH
2263 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2264 } else {
2265 Data = 0xff;
80c00750 2266 status = mos7840_set_reg_sync(port,
ae685eff
JH
2267 (__u16) (ZLP_REG1 +
2268 ((__u16)mos7840_port->port_num) - 0x1), Data);
2269 dev_dbg(&port->dev, "ZLIP offset %x\n",
2270 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
3f542974 2271 if (status < 0) {
ae685eff
JH
2272 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2273 goto out;
3f542974 2274 } else
ae685eff 2275 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
3f542974 2276
ae685eff
JH
2277 }
2278 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2279 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2280 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2281 GFP_KERNEL);
2282 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2283 !mos7840_port->dr) {
2284 status = -ENOMEM;
2285 goto error;
2286 }
0eafe4de 2287
ae685eff 2288 mos7840_port->has_led = false;
0eafe4de 2289
ae685eff
JH
2290 /* Initialize LED timers */
2291 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2292 mos7840_port->has_led = true;
0eafe4de 2293
05cf0dec
JH
2294 mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
2295 mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
2296 GFP_KERNEL);
2297 if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
2298 status = -ENOMEM;
2299 goto error;
2300 }
2301
f05b7cb6
VT
2302 setup_timer(&mos7840_port->led_timer1, mos7840_led_off,
2303 (unsigned long)mos7840_port);
ae685eff
JH
2304 mos7840_port->led_timer1.expires =
2305 jiffies + msecs_to_jiffies(LED_ON_MS);
f05b7cb6
VT
2306 setup_timer(&mos7840_port->led_timer2, mos7840_led_flag_off,
2307 (unsigned long)mos7840_port);
ae685eff
JH
2308 mos7840_port->led_timer2.expires =
2309 jiffies + msecs_to_jiffies(LED_OFF_MS);
0eafe4de 2310
ae685eff
JH
2311 /* Turn off LED */
2312 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2313 }
2314out:
80c00750
JH
2315 if (pnum == serial->num_ports - 1) {
2316 /* Zero Length flag enable */
2317 Data = 0x0f;
2318 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2319 if (status < 0) {
2320 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2321 goto error;
2322 } else
2323 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2324
2325 /* setting configuration feature to one */
2326 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2327 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2328 MOS_WDR_TIMEOUT);
2329 }
3f542974 2330 return 0;
0de9a702 2331error:
05cf0dec
JH
2332 kfree(mos7840_port->led_dr);
2333 usb_free_urb(mos7840_port->led_urb);
80c00750
JH
2334 kfree(mos7840_port->dr);
2335 kfree(mos7840_port->ctrl_buf);
2336 usb_free_urb(mos7840_port->control_urb);
2337 kfree(mos7840_port);
0de9a702 2338
0de9a702 2339 return status;
3f542974
PS
2340}
2341
80c00750 2342static int mos7840_port_remove(struct usb_serial_port *port)
3f542974 2343{
3f542974 2344 struct moschip_port *mos7840_port;
3f542974 2345
80c00750 2346 mos7840_port = mos7840_get_port_private(port);
3f542974 2347
80c00750
JH
2348 if (mos7840_port->has_led) {
2349 /* Turn off LED */
2350 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
3f542974 2351
80c00750
JH
2352 del_timer_sync(&mos7840_port->led_timer1);
2353 del_timer_sync(&mos7840_port->led_timer2);
05cf0dec
JH
2354
2355 usb_kill_urb(mos7840_port->led_urb);
2356 usb_free_urb(mos7840_port->led_urb);
2357 kfree(mos7840_port->led_dr);
f9c99bb8 2358 }
80c00750
JH
2359 usb_kill_urb(mos7840_port->control_urb);
2360 usb_free_urb(mos7840_port->control_urb);
2361 kfree(mos7840_port->ctrl_buf);
2362 kfree(mos7840_port->dr);
2363 kfree(mos7840_port);
f9c99bb8 2364
80c00750 2365 return 0;
3f542974
PS
2366}
2367
2368static struct usb_serial_driver moschip7840_4port_device = {
2369 .driver = {
2370 .owner = THIS_MODULE,
2371 .name = "mos7840",
2372 },
2373 .description = DRIVER_DESC,
68e24113 2374 .id_table = id_table,
3f542974 2375 .num_ports = 4,
3f542974
PS
2376 .open = mos7840_open,
2377 .close = mos7840_close,
2378 .write = mos7840_write,
2379 .write_room = mos7840_write_room,
2380 .chars_in_buffer = mos7840_chars_in_buffer,
2381 .throttle = mos7840_throttle,
2382 .unthrottle = mos7840_unthrottle,
2383 .calc_num_ports = mos7840_calc_num_ports,
40c24f28 2384 .probe = mos7840_probe,
3f542974
PS
2385 .ioctl = mos7840_ioctl,
2386 .set_termios = mos7840_set_termios,
2387 .break_ctl = mos7840_break,
2388 .tiocmget = mos7840_tiocmget,
2389 .tiocmset = mos7840_tiocmset,
0c613371 2390 .tiocmiwait = usb_serial_generic_tiocmiwait,
8c1a07ff 2391 .get_icount = usb_serial_generic_get_icount,
5c75633e 2392 .attach = mos7840_attach,
80c00750
JH
2393 .port_probe = mos7840_port_probe,
2394 .port_remove = mos7840_port_remove,
3f542974
PS
2395 .read_bulk_callback = mos7840_bulk_in_callback,
2396 .read_int_callback = mos7840_interrupt_callback,
2397};
2398
4d2a7aff
AS
2399static struct usb_serial_driver * const serial_drivers[] = {
2400 &moschip7840_4port_device, NULL
2401};
2402
68e24113 2403module_usb_serial_driver(serial_drivers, id_table);
3f542974 2404
3f542974
PS
2405MODULE_DESCRIPTION(DRIVER_DESC);
2406MODULE_LICENSE("GPL");