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