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