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