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