USB: ti_usb_3410_502: Fix ID table size
[pandora-kernel.git] / drivers / usb / serial / ti_usb_3410_5052.c
1 /* vi: ts=8 sw=8
2  *
3  * TI 3410/5052 USB Serial Driver
4  *
5  * Copyright (C) 2004 Texas Instruments
6  *
7  * This driver is based on the Linux io_ti driver, which is
8  *   Copyright (C) 2000-2002 Inside Out Networks
9  *   Copyright (C) 2001-2002 Greg Kroah-Hartman
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * For questions or problems with this driver, contact Texas Instruments
17  * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
18  * Peter Berger <pberger@brimson.com>.
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/firmware.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h>
29 #include <linux/module.h>
30 #include <linux/spinlock.h>
31 #include <linux/ioctl.h>
32 #include <linux/serial.h>
33 #include <linux/kfifo.h>
34 #include <linux/mutex.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
38
39 #include "ti_usb_3410_5052.h"
40
41 /* Defines */
42
43 #define TI_DRIVER_VERSION       "v0.10"
44 #define TI_DRIVER_AUTHOR        "Al Borchers <alborchers@steinerpoint.com>"
45 #define TI_DRIVER_DESC          "TI USB 3410/5052 Serial Driver"
46
47 #define TI_FIRMWARE_BUF_SIZE    16284
48
49 #define TI_WRITE_BUF_SIZE       1024
50
51 #define TI_TRANSFER_TIMEOUT     2
52
53 #define TI_DEFAULT_CLOSING_WAIT 4000            /* in .01 secs */
54
55 /* supported setserial flags */
56 #define TI_SET_SERIAL_FLAGS     0
57
58 /* read urb states */
59 #define TI_READ_URB_RUNNING     0
60 #define TI_READ_URB_STOPPING    1
61 #define TI_READ_URB_STOPPED     2
62
63 #define TI_EXTRA_VID_PID_COUNT  5
64
65
66 /* Structures */
67
68 struct ti_port {
69         int                     tp_is_open;
70         __u8                    tp_msr;
71         __u8                    tp_lsr;
72         __u8                    tp_shadow_mcr;
73         __u8                    tp_uart_mode;   /* 232 or 485 modes */
74         unsigned int            tp_uart_base_addr;
75         int                     tp_flags;
76         int                     tp_closing_wait;/* in .01 secs */
77         struct async_icount     tp_icount;
78         wait_queue_head_t       tp_write_wait;
79         struct ti_device        *tp_tdev;
80         struct usb_serial_port  *tp_port;
81         spinlock_t              tp_lock;
82         int                     tp_read_urb_state;
83         int                     tp_write_urb_in_use;
84         struct kfifo            write_fifo;
85 };
86
87 struct ti_device {
88         struct mutex            td_open_close_lock;
89         int                     td_open_port_count;
90         struct usb_serial       *td_serial;
91         int                     td_is_3410;
92         int                     td_urb_error;
93 };
94
95
96 /* Function Declarations */
97
98 static int ti_startup(struct usb_serial *serial);
99 static void ti_release(struct usb_serial *serial);
100 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
101 static void ti_close(struct usb_serial_port *port);
102 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
103                 const unsigned char *data, int count);
104 static int ti_write_room(struct tty_struct *tty);
105 static int ti_chars_in_buffer(struct tty_struct *tty);
106 static void ti_throttle(struct tty_struct *tty);
107 static void ti_unthrottle(struct tty_struct *tty);
108 static int ti_ioctl(struct tty_struct *tty,
109                 unsigned int cmd, unsigned long arg);
110 static int ti_get_icount(struct tty_struct *tty,
111                 struct serial_icounter_struct *icount);
112 static void ti_set_termios(struct tty_struct *tty,
113                 struct usb_serial_port *port, struct ktermios *old_termios);
114 static int ti_tiocmget(struct tty_struct *tty);
115 static int ti_tiocmset(struct tty_struct *tty,
116                 unsigned int set, unsigned int clear);
117 static void ti_break(struct tty_struct *tty, int break_state);
118 static void ti_interrupt_callback(struct urb *urb);
119 static void ti_bulk_in_callback(struct urb *urb);
120 static void ti_bulk_out_callback(struct urb *urb);
121
122 static void ti_recv(struct device *dev, struct tty_struct *tty,
123         unsigned char *data, int length);
124 static void ti_send(struct ti_port *tport);
125 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
126 static int ti_get_lsr(struct ti_port *tport);
127 static int ti_get_serial_info(struct ti_port *tport,
128         struct serial_struct __user *ret_arg);
129 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
130         struct serial_struct __user *new_arg);
131 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
132
133 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
134
135 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
136 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
137
138 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
139         __u16 moduleid, __u16 value, __u8 *data, int size);
140 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
141         __u16 moduleid, __u16 value, __u8 *data, int size);
142
143 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
144         __u8 mask, __u8 byte);
145
146 static int ti_download_firmware(struct ti_device *tdev);
147
148
149 /* Data */
150
151 /* module parameters */
152 static int debug;
153 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
154 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
155 static unsigned int vendor_3410_count;
156 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
157 static unsigned int product_3410_count;
158 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
159 static unsigned int vendor_5052_count;
160 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
161 static unsigned int product_5052_count;
162
163 /* supported devices */
164 /* the array dimension is the number of default entries plus */
165 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
166 /* null entry */
167 static struct usb_device_id ti_id_table_3410[16+TI_EXTRA_VID_PID_COUNT+1] = {
168         { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
169         { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
170         { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
171         { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
172         { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
173         { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
174         { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
175         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
176         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
177         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
178         { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
179         { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
180         { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
181         { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STEREO_PLUG_ID) },
182         { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
183         { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
184 };
185
186 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
187         { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
188         { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
189         { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
190         { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
191 };
192
193 static struct usb_device_id ti_id_table_combined[20+2*TI_EXTRA_VID_PID_COUNT+1] = {
194         { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
195         { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
196         { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
197         { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
198         { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
199         { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
200         { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
201         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
202         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
203         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
204         { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
205         { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
206         { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
207         { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
208         { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
209         { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
210         { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
211         { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
212         { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
213         { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
214         { }
215 };
216
217 static struct usb_driver ti_usb_driver = {
218         .name                   = "ti_usb_3410_5052",
219         .probe                  = usb_serial_probe,
220         .disconnect             = usb_serial_disconnect,
221         .id_table               = ti_id_table_combined,
222         .no_dynamic_id =        1,
223 };
224
225 static struct usb_serial_driver ti_1port_device = {
226         .driver = {
227                 .owner          = THIS_MODULE,
228                 .name           = "ti_usb_3410_5052_1",
229         },
230         .description            = "TI USB 3410 1 port adapter",
231         .usb_driver             = &ti_usb_driver,
232         .id_table               = ti_id_table_3410,
233         .num_ports              = 1,
234         .attach                 = ti_startup,
235         .release                = ti_release,
236         .open                   = ti_open,
237         .close                  = ti_close,
238         .write                  = ti_write,
239         .write_room             = ti_write_room,
240         .chars_in_buffer        = ti_chars_in_buffer,
241         .throttle               = ti_throttle,
242         .unthrottle             = ti_unthrottle,
243         .ioctl                  = ti_ioctl,
244         .set_termios            = ti_set_termios,
245         .tiocmget               = ti_tiocmget,
246         .tiocmset               = ti_tiocmset,
247         .get_icount             = ti_get_icount,
248         .break_ctl              = ti_break,
249         .read_int_callback      = ti_interrupt_callback,
250         .read_bulk_callback     = ti_bulk_in_callback,
251         .write_bulk_callback    = ti_bulk_out_callback,
252 };
253
254 static struct usb_serial_driver ti_2port_device = {
255         .driver = {
256                 .owner          = THIS_MODULE,
257                 .name           = "ti_usb_3410_5052_2",
258         },
259         .description            = "TI USB 5052 2 port adapter",
260         .usb_driver             = &ti_usb_driver,
261         .id_table               = ti_id_table_5052,
262         .num_ports              = 2,
263         .attach                 = ti_startup,
264         .release                = ti_release,
265         .open                   = ti_open,
266         .close                  = ti_close,
267         .write                  = ti_write,
268         .write_room             = ti_write_room,
269         .chars_in_buffer        = ti_chars_in_buffer,
270         .throttle               = ti_throttle,
271         .unthrottle             = ti_unthrottle,
272         .ioctl                  = ti_ioctl,
273         .set_termios            = ti_set_termios,
274         .tiocmget               = ti_tiocmget,
275         .tiocmset               = ti_tiocmset,
276         .get_icount             = ti_get_icount,
277         .break_ctl              = ti_break,
278         .read_int_callback      = ti_interrupt_callback,
279         .read_bulk_callback     = ti_bulk_in_callback,
280         .write_bulk_callback    = ti_bulk_out_callback,
281 };
282
283
284 /* Module */
285
286 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
287 MODULE_DESCRIPTION(TI_DRIVER_DESC);
288 MODULE_VERSION(TI_DRIVER_VERSION);
289 MODULE_LICENSE("GPL");
290
291 MODULE_FIRMWARE("ti_3410.fw");
292 MODULE_FIRMWARE("ti_5052.fw");
293 MODULE_FIRMWARE("mts_cdma.fw");
294 MODULE_FIRMWARE("mts_gsm.fw");
295 MODULE_FIRMWARE("mts_edge.fw");
296 MODULE_FIRMWARE("mts_mt9234mu.fw");
297 MODULE_FIRMWARE("mts_mt9234zba.fw");
298
299 module_param(debug, bool, S_IRUGO | S_IWUSR);
300 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
301
302 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
303 MODULE_PARM_DESC(closing_wait,
304     "Maximum wait for data to drain in close, in .01 secs, default is 4000");
305
306 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
307 MODULE_PARM_DESC(vendor_3410,
308                 "Vendor ids for 3410 based devices, 1-5 short integers");
309 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
310 MODULE_PARM_DESC(product_3410,
311                 "Product ids for 3410 based devices, 1-5 short integers");
312 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
313 MODULE_PARM_DESC(vendor_5052,
314                 "Vendor ids for 5052 based devices, 1-5 short integers");
315 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
316 MODULE_PARM_DESC(product_5052,
317                 "Product ids for 5052 based devices, 1-5 short integers");
318
319 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
320
321
322 /* Functions */
323
324 static int __init ti_init(void)
325 {
326         int i, j, c;
327         int ret;
328
329         /* insert extra vendor and product ids */
330         c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
331         j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
332         for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
333                 ti_id_table_3410[j].idVendor = vendor_3410[i];
334                 ti_id_table_3410[j].idProduct = product_3410[i];
335                 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
336                 ti_id_table_combined[c].idVendor = vendor_3410[i];
337                 ti_id_table_combined[c].idProduct = product_3410[i];
338                 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
339         }
340         j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
341         for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
342                 ti_id_table_5052[j].idVendor = vendor_5052[i];
343                 ti_id_table_5052[j].idProduct = product_5052[i];
344                 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
345                 ti_id_table_combined[c].idVendor = vendor_5052[i];
346                 ti_id_table_combined[c].idProduct = product_5052[i];
347                 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
348         }
349
350         ret = usb_serial_register(&ti_1port_device);
351         if (ret)
352                 goto failed_1port;
353         ret = usb_serial_register(&ti_2port_device);
354         if (ret)
355                 goto failed_2port;
356
357         ret = usb_register(&ti_usb_driver);
358         if (ret)
359                 goto failed_usb;
360
361         printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":"
362                TI_DRIVER_DESC "\n");
363
364         return 0;
365
366 failed_usb:
367         usb_serial_deregister(&ti_2port_device);
368 failed_2port:
369         usb_serial_deregister(&ti_1port_device);
370 failed_1port:
371         return ret;
372 }
373
374
375 static void __exit ti_exit(void)
376 {
377         usb_deregister(&ti_usb_driver);
378         usb_serial_deregister(&ti_1port_device);
379         usb_serial_deregister(&ti_2port_device);
380 }
381
382
383 module_init(ti_init);
384 module_exit(ti_exit);
385
386
387 static int ti_startup(struct usb_serial *serial)
388 {
389         struct ti_device *tdev;
390         struct ti_port *tport;
391         struct usb_device *dev = serial->dev;
392         int status;
393         int i;
394
395
396         dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
397             __func__, le16_to_cpu(dev->descriptor.idProduct),
398             dev->descriptor.bNumConfigurations,
399             dev->actconfig->desc.bConfigurationValue);
400
401         /* create device structure */
402         tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
403         if (tdev == NULL) {
404                 dev_err(&dev->dev, "%s - out of memory\n", __func__);
405                 return -ENOMEM;
406         }
407         mutex_init(&tdev->td_open_close_lock);
408         tdev->td_serial = serial;
409         usb_set_serial_data(serial, tdev);
410
411         /* determine device type */
412         if (serial->type == &ti_1port_device)
413                 tdev->td_is_3410 = 1;
414         dbg("%s - device type is %s", __func__,
415                                 tdev->td_is_3410 ? "3410" : "5052");
416
417         /* if we have only 1 configuration, download firmware */
418         if (dev->descriptor.bNumConfigurations == 1) {
419                 if ((status = ti_download_firmware(tdev)) != 0)
420                         goto free_tdev;
421
422                 /* 3410 must be reset, 5052 resets itself */
423                 if (tdev->td_is_3410) {
424                         msleep_interruptible(100);
425                         usb_reset_device(dev);
426                 }
427
428                 status = -ENODEV;
429                 goto free_tdev;
430         }
431
432         /* the second configuration must be set */
433         if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
434                 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
435                 status = status ? status : -ENODEV;
436                 goto free_tdev;
437         }
438
439         /* set up port structures */
440         for (i = 0; i < serial->num_ports; ++i) {
441                 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
442                 if (tport == NULL) {
443                         dev_err(&dev->dev, "%s - out of memory\n", __func__);
444                         status = -ENOMEM;
445                         goto free_tports;
446                 }
447                 spin_lock_init(&tport->tp_lock);
448                 tport->tp_uart_base_addr = (i == 0 ?
449                                 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
450                 tport->tp_closing_wait = closing_wait;
451                 init_waitqueue_head(&tport->tp_write_wait);
452                 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE,
453                                                                 GFP_KERNEL)) {
454                         dev_err(&dev->dev, "%s - out of memory\n", __func__);
455                         kfree(tport);
456                         status = -ENOMEM;
457                         goto free_tports;
458                 }
459                 tport->tp_port = serial->port[i];
460                 tport->tp_tdev = tdev;
461                 usb_set_serial_port_data(serial->port[i], tport);
462                 tport->tp_uart_mode = 0;        /* default is RS232 */
463         }
464
465         return 0;
466
467 free_tports:
468         for (--i; i >= 0; --i) {
469                 tport = usb_get_serial_port_data(serial->port[i]);
470                 kfifo_free(&tport->write_fifo);
471                 kfree(tport);
472                 usb_set_serial_port_data(serial->port[i], NULL);
473         }
474 free_tdev:
475         kfree(tdev);
476         usb_set_serial_data(serial, NULL);
477         return status;
478 }
479
480
481 static void ti_release(struct usb_serial *serial)
482 {
483         int i;
484         struct ti_device *tdev = usb_get_serial_data(serial);
485         struct ti_port *tport;
486
487         dbg("%s", __func__);
488
489         for (i = 0; i < serial->num_ports; ++i) {
490                 tport = usb_get_serial_port_data(serial->port[i]);
491                 if (tport) {
492                         kfifo_free(&tport->write_fifo);
493                         kfree(tport);
494                 }
495         }
496
497         kfree(tdev);
498 }
499
500
501 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
502 {
503         struct ti_port *tport = usb_get_serial_port_data(port);
504         struct ti_device *tdev;
505         struct usb_device *dev;
506         struct urb *urb;
507         int port_number;
508         int status;
509         __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
510                              TI_PIPE_TIMEOUT_ENABLE |
511                              (TI_TRANSFER_TIMEOUT << 2));
512
513         dbg("%s - port %d", __func__, port->number);
514
515         if (tport == NULL)
516                 return -ENODEV;
517
518         dev = port->serial->dev;
519         tdev = tport->tp_tdev;
520
521         /* only one open on any port on a device at a time */
522         if (mutex_lock_interruptible(&tdev->td_open_close_lock))
523                 return -ERESTARTSYS;
524
525         port_number = port->number - port->serial->minor;
526
527         memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
528
529         tport->tp_msr = 0;
530         tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
531
532         /* start interrupt urb the first time a port is opened on this device */
533         if (tdev->td_open_port_count == 0) {
534                 dbg("%s - start interrupt in urb", __func__);
535                 urb = tdev->td_serial->port[0]->interrupt_in_urb;
536                 if (!urb) {
537                         dev_err(&port->dev, "%s - no interrupt urb\n",
538                                                                 __func__);
539                         status = -EINVAL;
540                         goto release_lock;
541                 }
542                 urb->complete = ti_interrupt_callback;
543                 urb->context = tdev;
544                 urb->dev = dev;
545                 status = usb_submit_urb(urb, GFP_KERNEL);
546                 if (status) {
547                         dev_err(&port->dev,
548                                 "%s - submit interrupt urb failed, %d\n",
549                                         __func__, status);
550                         goto release_lock;
551                 }
552         }
553
554         if (tty)
555                 ti_set_termios(tty, port, tty->termios);
556
557         dbg("%s - sending TI_OPEN_PORT", __func__);
558         status = ti_command_out_sync(tdev, TI_OPEN_PORT,
559                 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
560         if (status) {
561                 dev_err(&port->dev, "%s - cannot send open command, %d\n",
562                                                         __func__, status);
563                 goto unlink_int_urb;
564         }
565
566         dbg("%s - sending TI_START_PORT", __func__);
567         status = ti_command_out_sync(tdev, TI_START_PORT,
568                 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
569         if (status) {
570                 dev_err(&port->dev, "%s - cannot send start command, %d\n",
571                                                         __func__, status);
572                 goto unlink_int_urb;
573         }
574
575         dbg("%s - sending TI_PURGE_PORT", __func__);
576         status = ti_command_out_sync(tdev, TI_PURGE_PORT,
577                 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
578         if (status) {
579                 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
580                                                         __func__, status);
581                 goto unlink_int_urb;
582         }
583         status = ti_command_out_sync(tdev, TI_PURGE_PORT,
584                 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
585         if (status) {
586                 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
587                                                         __func__, status);
588                 goto unlink_int_urb;
589         }
590
591         /* reset the data toggle on the bulk endpoints to work around bug in
592          * host controllers where things get out of sync some times */
593         usb_clear_halt(dev, port->write_urb->pipe);
594         usb_clear_halt(dev, port->read_urb->pipe);
595
596         if (tty)
597                 ti_set_termios(tty, port, tty->termios);
598
599         dbg("%s - sending TI_OPEN_PORT (2)", __func__);
600         status = ti_command_out_sync(tdev, TI_OPEN_PORT,
601                 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
602         if (status) {
603                 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
604                                                         __func__, status);
605                 goto unlink_int_urb;
606         }
607
608         dbg("%s - sending TI_START_PORT (2)", __func__);
609         status = ti_command_out_sync(tdev, TI_START_PORT,
610                 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
611         if (status) {
612                 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
613                                                         __func__, status);
614                 goto unlink_int_urb;
615         }
616
617         /* start read urb */
618         dbg("%s - start read urb", __func__);
619         urb = port->read_urb;
620         if (!urb) {
621                 dev_err(&port->dev, "%s - no read urb\n", __func__);
622                 status = -EINVAL;
623                 goto unlink_int_urb;
624         }
625         tport->tp_read_urb_state = TI_READ_URB_RUNNING;
626         urb->complete = ti_bulk_in_callback;
627         urb->context = tport;
628         urb->dev = dev;
629         status = usb_submit_urb(urb, GFP_KERNEL);
630         if (status) {
631                 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
632                                                         __func__, status);
633                 goto unlink_int_urb;
634         }
635
636         tport->tp_is_open = 1;
637         ++tdev->td_open_port_count;
638
639         goto release_lock;
640
641 unlink_int_urb:
642         if (tdev->td_open_port_count == 0)
643                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
644 release_lock:
645         mutex_unlock(&tdev->td_open_close_lock);
646         dbg("%s - exit %d", __func__, status);
647         return status;
648 }
649
650
651 static void ti_close(struct usb_serial_port *port)
652 {
653         struct ti_device *tdev;
654         struct ti_port *tport;
655         int port_number;
656         int status;
657         int do_unlock;
658
659         dbg("%s - port %d", __func__, port->number);
660
661         tdev = usb_get_serial_data(port->serial);
662         tport = usb_get_serial_port_data(port);
663         if (tdev == NULL || tport == NULL)
664                 return;
665
666         tport->tp_is_open = 0;
667
668         ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
669
670         usb_kill_urb(port->read_urb);
671         usb_kill_urb(port->write_urb);
672         tport->tp_write_urb_in_use = 0;
673
674         port_number = port->number - port->serial->minor;
675
676         dbg("%s - sending TI_CLOSE_PORT", __func__);
677         status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
678                      (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
679         if (status)
680                 dev_err(&port->dev,
681                         "%s - cannot send close port command, %d\n"
682                                                         , __func__, status);
683
684         /* if mutex_lock is interrupted, continue anyway */
685         do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
686         --tport->tp_tdev->td_open_port_count;
687         if (tport->tp_tdev->td_open_port_count <= 0) {
688                 /* last port is closed, shut down interrupt urb */
689                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
690                 tport->tp_tdev->td_open_port_count = 0;
691         }
692         if (do_unlock)
693                 mutex_unlock(&tdev->td_open_close_lock);
694
695         dbg("%s - exit", __func__);
696 }
697
698
699 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
700                         const unsigned char *data, int count)
701 {
702         struct ti_port *tport = usb_get_serial_port_data(port);
703
704         dbg("%s - port %d", __func__, port->number);
705
706         if (count == 0) {
707                 dbg("%s - write request of 0 bytes", __func__);
708                 return 0;
709         }
710
711         if (tport == NULL || !tport->tp_is_open)
712                 return -ENODEV;
713
714         count = kfifo_in_locked(&tport->write_fifo, data, count,
715                                                         &tport->tp_lock);
716         ti_send(tport);
717
718         return count;
719 }
720
721
722 static int ti_write_room(struct tty_struct *tty)
723 {
724         struct usb_serial_port *port = tty->driver_data;
725         struct ti_port *tport = usb_get_serial_port_data(port);
726         int room = 0;
727         unsigned long flags;
728
729         dbg("%s - port %d", __func__, port->number);
730
731         if (tport == NULL)
732                 return 0;
733
734         spin_lock_irqsave(&tport->tp_lock, flags);
735         room = kfifo_avail(&tport->write_fifo);
736         spin_unlock_irqrestore(&tport->tp_lock, flags);
737
738         dbg("%s - returns %d", __func__, room);
739         return room;
740 }
741
742
743 static int ti_chars_in_buffer(struct tty_struct *tty)
744 {
745         struct usb_serial_port *port = tty->driver_data;
746         struct ti_port *tport = usb_get_serial_port_data(port);
747         int chars = 0;
748         unsigned long flags;
749
750         dbg("%s - port %d", __func__, port->number);
751
752         if (tport == NULL)
753                 return 0;
754
755         spin_lock_irqsave(&tport->tp_lock, flags);
756         chars = kfifo_len(&tport->write_fifo);
757         spin_unlock_irqrestore(&tport->tp_lock, flags);
758
759         dbg("%s - returns %d", __func__, chars);
760         return chars;
761 }
762
763
764 static void ti_throttle(struct tty_struct *tty)
765 {
766         struct usb_serial_port *port = tty->driver_data;
767         struct ti_port *tport = usb_get_serial_port_data(port);
768
769         dbg("%s - port %d", __func__, port->number);
770
771         if (tport == NULL)
772                 return;
773
774         if (I_IXOFF(tty) || C_CRTSCTS(tty))
775                 ti_stop_read(tport, tty);
776
777 }
778
779
780 static void ti_unthrottle(struct tty_struct *tty)
781 {
782         struct usb_serial_port *port = tty->driver_data;
783         struct ti_port *tport = usb_get_serial_port_data(port);
784         int status;
785
786         dbg("%s - port %d", __func__, port->number);
787
788         if (tport == NULL)
789                 return;
790
791         if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
792                 status = ti_restart_read(tport, tty);
793                 if (status)
794                         dev_err(&port->dev, "%s - cannot restart read, %d\n",
795                                                         __func__, status);
796         }
797 }
798
799 static int ti_get_icount(struct tty_struct *tty,
800                 struct serial_icounter_struct *icount)
801 {
802         struct usb_serial_port *port = tty->driver_data;
803         struct ti_port *tport = usb_get_serial_port_data(port);
804         struct async_icount cnow = tport->tp_icount;
805
806         dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
807                 __func__, port->number,
808                 cnow.rx, cnow.tx);
809
810         icount->cts = cnow.cts;
811         icount->dsr = cnow.dsr;
812         icount->rng = cnow.rng;
813         icount->dcd = cnow.dcd;
814         icount->rx = cnow.rx;
815         icount->tx = cnow.tx;
816         icount->frame = cnow.frame;
817         icount->overrun = cnow.overrun;
818         icount->parity = cnow.parity;
819         icount->brk = cnow.brk;
820         icount->buf_overrun = cnow.buf_overrun;
821
822         return 0;
823 }
824
825 static int ti_ioctl(struct tty_struct *tty,
826         unsigned int cmd, unsigned long arg)
827 {
828         struct usb_serial_port *port = tty->driver_data;
829         struct ti_port *tport = usb_get_serial_port_data(port);
830         struct async_icount cnow;
831         struct async_icount cprev;
832
833         dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
834
835         if (tport == NULL)
836                 return -ENODEV;
837
838         switch (cmd) {
839         case TIOCGSERIAL:
840                 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
841                 return ti_get_serial_info(tport,
842                                 (struct serial_struct __user *)arg);
843         case TIOCSSERIAL:
844                 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
845                 return ti_set_serial_info(tty, tport,
846                                 (struct serial_struct __user *)arg);
847         case TIOCMIWAIT:
848                 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
849                 cprev = tport->tp_icount;
850                 while (1) {
851                         interruptible_sleep_on(&port->delta_msr_wait);
852                         if (signal_pending(current))
853                                 return -ERESTARTSYS;
854
855                         if (port->serial->disconnected)
856                                 return -EIO;
857
858                         cnow = tport->tp_icount;
859                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
860                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
861                                 return -EIO; /* no change => error */
862                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
863                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
864                             ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
865                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
866                                 return 0;
867                         cprev = cnow;
868                 }
869                 break;
870         }
871         return -ENOIOCTLCMD;
872 }
873
874
875 static void ti_set_termios(struct tty_struct *tty,
876                 struct usb_serial_port *port, struct ktermios *old_termios)
877 {
878         struct ti_port *tport = usb_get_serial_port_data(port);
879         struct ti_uart_config *config;
880         tcflag_t cflag, iflag;
881         int baud;
882         int status;
883         int port_number = port->number - port->serial->minor;
884         unsigned int mcr;
885
886         dbg("%s - port %d", __func__, port->number);
887
888         cflag = tty->termios->c_cflag;
889         iflag = tty->termios->c_iflag;
890
891         dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
892         dbg("%s - old clfag %08x, old iflag %08x", __func__,
893                                 old_termios->c_cflag, old_termios->c_iflag);
894
895         if (tport == NULL)
896                 return;
897
898         config = kmalloc(sizeof(*config), GFP_KERNEL);
899         if (!config) {
900                 dev_err(&port->dev, "%s - out of memory\n", __func__);
901                 return;
902         }
903
904         config->wFlags = 0;
905
906         /* these flags must be set */
907         config->wFlags |= TI_UART_ENABLE_MS_INTS;
908         config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
909         config->bUartMode = (__u8)(tport->tp_uart_mode);
910
911         switch (cflag & CSIZE) {
912         case CS5:
913                     config->bDataBits = TI_UART_5_DATA_BITS;
914                     break;
915         case CS6:
916                     config->bDataBits = TI_UART_6_DATA_BITS;
917                     break;
918         case CS7:
919                     config->bDataBits = TI_UART_7_DATA_BITS;
920                     break;
921         default:
922         case CS8:
923                     config->bDataBits = TI_UART_8_DATA_BITS;
924                     break;
925         }
926
927         /* CMSPAR isn't supported by this driver */
928         tty->termios->c_cflag &= ~CMSPAR;
929
930         if (cflag & PARENB) {
931                 if (cflag & PARODD) {
932                         config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
933                         config->bParity = TI_UART_ODD_PARITY;
934                 } else {
935                         config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
936                         config->bParity = TI_UART_EVEN_PARITY;
937                 }
938         } else {
939                 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
940                 config->bParity = TI_UART_NO_PARITY;
941         }
942
943         if (cflag & CSTOPB)
944                 config->bStopBits = TI_UART_2_STOP_BITS;
945         else
946                 config->bStopBits = TI_UART_1_STOP_BITS;
947
948         if (cflag & CRTSCTS) {
949                 /* RTS flow control must be off to drop RTS for baud rate B0 */
950                 if ((cflag & CBAUD) != B0)
951                         config->wFlags |= TI_UART_ENABLE_RTS_IN;
952                 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
953         } else {
954                 tty->hw_stopped = 0;
955                 ti_restart_read(tport, tty);
956         }
957
958         if (I_IXOFF(tty) || I_IXON(tty)) {
959                 config->cXon  = START_CHAR(tty);
960                 config->cXoff = STOP_CHAR(tty);
961
962                 if (I_IXOFF(tty))
963                         config->wFlags |= TI_UART_ENABLE_X_IN;
964                 else
965                         ti_restart_read(tport, tty);
966
967                 if (I_IXON(tty))
968                         config->wFlags |= TI_UART_ENABLE_X_OUT;
969         }
970
971         baud = tty_get_baud_rate(tty);
972         if (!baud)
973                 baud = 9600;
974         if (tport->tp_tdev->td_is_3410)
975                 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
976         else
977                 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
978
979         /* FIXME: Should calculate resulting baud here and report it back */
980         if ((cflag & CBAUD) != B0)
981                 tty_encode_baud_rate(tty, baud, baud);
982
983         dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
984         __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
985
986         cpu_to_be16s(&config->wBaudRate);
987         cpu_to_be16s(&config->wFlags);
988
989         status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
990                 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
991                 sizeof(*config));
992         if (status)
993                 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
994                                         __func__, port_number, status);
995
996         /* SET_CONFIG asserts RTS and DTR, reset them correctly */
997         mcr = tport->tp_shadow_mcr;
998         /* if baud rate is B0, clear RTS and DTR */
999         if ((cflag & CBAUD) == B0)
1000                 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1001         status = ti_set_mcr(tport, mcr);
1002         if (status)
1003                 dev_err(&port->dev,
1004                         "%s - cannot set modem control on port %d, %d\n",
1005                                                 __func__, port_number, status);
1006
1007         kfree(config);
1008 }
1009
1010
1011 static int ti_tiocmget(struct tty_struct *tty)
1012 {
1013         struct usb_serial_port *port = tty->driver_data;
1014         struct ti_port *tport = usb_get_serial_port_data(port);
1015         unsigned int result;
1016         unsigned int msr;
1017         unsigned int mcr;
1018         unsigned long flags;
1019
1020         dbg("%s - port %d", __func__, port->number);
1021
1022         if (tport == NULL)
1023                 return -ENODEV;
1024
1025         spin_lock_irqsave(&tport->tp_lock, flags);
1026         msr = tport->tp_msr;
1027         mcr = tport->tp_shadow_mcr;
1028         spin_unlock_irqrestore(&tport->tp_lock, flags);
1029
1030         result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1031                 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1032                 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1033                 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1034                 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1035                 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1036                 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1037
1038         dbg("%s - 0x%04X", __func__, result);
1039
1040         return result;
1041 }
1042
1043
1044 static int ti_tiocmset(struct tty_struct *tty,
1045                                 unsigned int set, unsigned int clear)
1046 {
1047         struct usb_serial_port *port = tty->driver_data;
1048         struct ti_port *tport = usb_get_serial_port_data(port);
1049         unsigned int mcr;
1050         unsigned long flags;
1051
1052         dbg("%s - port %d", __func__, port->number);
1053
1054         if (tport == NULL)
1055                 return -ENODEV;
1056
1057         spin_lock_irqsave(&tport->tp_lock, flags);
1058         mcr = tport->tp_shadow_mcr;
1059
1060         if (set & TIOCM_RTS)
1061                 mcr |= TI_MCR_RTS;
1062         if (set & TIOCM_DTR)
1063                 mcr |= TI_MCR_DTR;
1064         if (set & TIOCM_LOOP)
1065                 mcr |= TI_MCR_LOOP;
1066
1067         if (clear & TIOCM_RTS)
1068                 mcr &= ~TI_MCR_RTS;
1069         if (clear & TIOCM_DTR)
1070                 mcr &= ~TI_MCR_DTR;
1071         if (clear & TIOCM_LOOP)
1072                 mcr &= ~TI_MCR_LOOP;
1073         spin_unlock_irqrestore(&tport->tp_lock, flags);
1074
1075         return ti_set_mcr(tport, mcr);
1076 }
1077
1078
1079 static void ti_break(struct tty_struct *tty, int break_state)
1080 {
1081         struct usb_serial_port *port = tty->driver_data;
1082         struct ti_port *tport = usb_get_serial_port_data(port);
1083         int status;
1084
1085         dbg("%s - state = %d", __func__, break_state);
1086
1087         if (tport == NULL)
1088                 return;
1089
1090         ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1091
1092         status = ti_write_byte(tport->tp_tdev,
1093                 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1094                 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1095
1096         if (status)
1097                 dbg("%s - error setting break, %d", __func__, status);
1098 }
1099
1100
1101 static void ti_interrupt_callback(struct urb *urb)
1102 {
1103         struct ti_device *tdev = urb->context;
1104         struct usb_serial_port *port;
1105         struct usb_serial *serial = tdev->td_serial;
1106         struct ti_port *tport;
1107         struct device *dev = &urb->dev->dev;
1108         unsigned char *data = urb->transfer_buffer;
1109         int length = urb->actual_length;
1110         int port_number;
1111         int function;
1112         int status = urb->status;
1113         int retval;
1114         __u8 msr;
1115
1116         dbg("%s", __func__);
1117
1118         switch (status) {
1119         case 0:
1120                 break;
1121         case -ECONNRESET:
1122         case -ENOENT:
1123         case -ESHUTDOWN:
1124                 dbg("%s - urb shutting down, %d", __func__, status);
1125                 tdev->td_urb_error = 1;
1126                 return;
1127         default:
1128                 dev_err(dev, "%s - nonzero urb status, %d\n",
1129                         __func__, status);
1130                 tdev->td_urb_error = 1;
1131                 goto exit;
1132         }
1133
1134         if (length != 2) {
1135                 dbg("%s - bad packet size, %d", __func__, length);
1136                 goto exit;
1137         }
1138
1139         if (data[0] == TI_CODE_HARDWARE_ERROR) {
1140                 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1141                 goto exit;
1142         }
1143
1144         port_number = TI_GET_PORT_FROM_CODE(data[0]);
1145         function = TI_GET_FUNC_FROM_CODE(data[0]);
1146
1147         dbg("%s - port_number %d, function %d, data 0x%02X",
1148                                 __func__, port_number, function, data[1]);
1149
1150         if (port_number >= serial->num_ports) {
1151                 dev_err(dev, "%s - bad port number, %d\n",
1152                                                 __func__, port_number);
1153                 goto exit;
1154         }
1155
1156         port = serial->port[port_number];
1157
1158         tport = usb_get_serial_port_data(port);
1159         if (!tport)
1160                 goto exit;
1161
1162         switch (function) {
1163         case TI_CODE_DATA_ERROR:
1164                 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1165                                         __func__, port_number, data[1]);
1166                 break;
1167
1168         case TI_CODE_MODEM_STATUS:
1169                 msr = data[1];
1170                 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
1171                 ti_handle_new_msr(tport, msr);
1172                 break;
1173
1174         default:
1175                 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1176                                                         __func__, data[1]);
1177                 break;
1178         }
1179
1180 exit:
1181         retval = usb_submit_urb(urb, GFP_ATOMIC);
1182         if (retval)
1183                 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1184                         __func__, retval);
1185 }
1186
1187
1188 static void ti_bulk_in_callback(struct urb *urb)
1189 {
1190         struct ti_port *tport = urb->context;
1191         struct usb_serial_port *port = tport->tp_port;
1192         struct device *dev = &urb->dev->dev;
1193         int status = urb->status;
1194         int retval = 0;
1195         struct tty_struct *tty;
1196
1197         dbg("%s", __func__);
1198
1199         switch (status) {
1200         case 0:
1201                 break;
1202         case -ECONNRESET:
1203         case -ENOENT:
1204         case -ESHUTDOWN:
1205                 dbg("%s - urb shutting down, %d", __func__, status);
1206                 tport->tp_tdev->td_urb_error = 1;
1207                 wake_up_interruptible(&tport->tp_write_wait);
1208                 return;
1209         default:
1210                 dev_err(dev, "%s - nonzero urb status, %d\n",
1211                         __func__, status);
1212                 tport->tp_tdev->td_urb_error = 1;
1213                 wake_up_interruptible(&tport->tp_write_wait);
1214         }
1215
1216         if (status == -EPIPE)
1217                 goto exit;
1218
1219         if (status) {
1220                 dev_err(dev, "%s - stopping read!\n", __func__);
1221                 return;
1222         }
1223
1224         tty = tty_port_tty_get(&port->port);
1225         if (tty) {
1226                 if (urb->actual_length) {
1227                         usb_serial_debug_data(debug, dev, __func__,
1228                                 urb->actual_length, urb->transfer_buffer);
1229
1230                         if (!tport->tp_is_open)
1231                                 dbg("%s - port closed, dropping data",
1232                                         __func__);
1233                         else
1234                                 ti_recv(&urb->dev->dev, tty,
1235                                                 urb->transfer_buffer,
1236                                                 urb->actual_length);
1237                         spin_lock(&tport->tp_lock);
1238                         tport->tp_icount.rx += urb->actual_length;
1239                         spin_unlock(&tport->tp_lock);
1240                 }
1241                 tty_kref_put(tty);
1242         }
1243
1244 exit:
1245         /* continue to read unless stopping */
1246         spin_lock(&tport->tp_lock);
1247         if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1248                 urb->dev = port->serial->dev;
1249                 retval = usb_submit_urb(urb, GFP_ATOMIC);
1250         } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1251                 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1252         }
1253         spin_unlock(&tport->tp_lock);
1254         if (retval)
1255                 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1256                         __func__, retval);
1257 }
1258
1259
1260 static void ti_bulk_out_callback(struct urb *urb)
1261 {
1262         struct ti_port *tport = urb->context;
1263         struct usb_serial_port *port = tport->tp_port;
1264         struct device *dev = &urb->dev->dev;
1265         int status = urb->status;
1266
1267         dbg("%s - port %d", __func__, port->number);
1268
1269         tport->tp_write_urb_in_use = 0;
1270
1271         switch (status) {
1272         case 0:
1273                 break;
1274         case -ECONNRESET:
1275         case -ENOENT:
1276         case -ESHUTDOWN:
1277                 dbg("%s - urb shutting down, %d", __func__, status);
1278                 tport->tp_tdev->td_urb_error = 1;
1279                 wake_up_interruptible(&tport->tp_write_wait);
1280                 return;
1281         default:
1282                 dev_err(dev, "%s - nonzero urb status, %d\n",
1283                         __func__, status);
1284                 tport->tp_tdev->td_urb_error = 1;
1285                 wake_up_interruptible(&tport->tp_write_wait);
1286         }
1287
1288         /* send any buffered data */
1289         ti_send(tport);
1290 }
1291
1292
1293 static void ti_recv(struct device *dev, struct tty_struct *tty,
1294         unsigned char *data, int length)
1295 {
1296         int cnt;
1297
1298         do {
1299                 cnt = tty_insert_flip_string(tty, data, length);
1300                 if (cnt < length) {
1301                         dev_err(dev, "%s - dropping data, %d bytes lost\n",
1302                                                 __func__, length - cnt);
1303                         if (cnt == 0)
1304                                 break;
1305                 }
1306                 tty_flip_buffer_push(tty);
1307                 data += cnt;
1308                 length -= cnt;
1309         } while (length > 0);
1310
1311 }
1312
1313
1314 static void ti_send(struct ti_port *tport)
1315 {
1316         int count, result;
1317         struct usb_serial_port *port = tport->tp_port;
1318         struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1319         unsigned long flags;
1320
1321
1322         dbg("%s - port %d", __func__, port->number);
1323
1324         spin_lock_irqsave(&tport->tp_lock, flags);
1325
1326         if (tport->tp_write_urb_in_use)
1327                 goto unlock;
1328
1329         count = kfifo_out(&tport->write_fifo,
1330                                 port->write_urb->transfer_buffer,
1331                                 port->bulk_out_size);
1332
1333         if (count == 0)
1334                 goto unlock;
1335
1336         tport->tp_write_urb_in_use = 1;
1337
1338         spin_unlock_irqrestore(&tport->tp_lock, flags);
1339
1340         usb_serial_debug_data(debug, &port->dev, __func__, count,
1341                                         port->write_urb->transfer_buffer);
1342
1343         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1344                            usb_sndbulkpipe(port->serial->dev,
1345                                             port->bulk_out_endpointAddress),
1346                            port->write_urb->transfer_buffer, count,
1347                            ti_bulk_out_callback, tport);
1348
1349         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1350         if (result) {
1351                 dev_err(&port->dev, "%s - submit write urb failed, %d\n",
1352                                                         __func__, result);
1353                 tport->tp_write_urb_in_use = 0;
1354                 /* TODO: reschedule ti_send */
1355         } else {
1356                 spin_lock_irqsave(&tport->tp_lock, flags);
1357                 tport->tp_icount.tx += count;
1358                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1359         }
1360
1361         /* more room in the buffer for new writes, wakeup */
1362         if (tty)
1363                 tty_wakeup(tty);
1364         tty_kref_put(tty);
1365         wake_up_interruptible(&tport->tp_write_wait);
1366         return;
1367 unlock:
1368         spin_unlock_irqrestore(&tport->tp_lock, flags);
1369         tty_kref_put(tty);
1370         return;
1371 }
1372
1373
1374 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1375 {
1376         unsigned long flags;
1377         int status;
1378
1379         status = ti_write_byte(tport->tp_tdev,
1380                 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1381                 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1382
1383         spin_lock_irqsave(&tport->tp_lock, flags);
1384         if (!status)
1385                 tport->tp_shadow_mcr = mcr;
1386         spin_unlock_irqrestore(&tport->tp_lock, flags);
1387
1388         return status;
1389 }
1390
1391
1392 static int ti_get_lsr(struct ti_port *tport)
1393 {
1394         int size, status;
1395         struct ti_device *tdev = tport->tp_tdev;
1396         struct usb_serial_port *port = tport->tp_port;
1397         int port_number = port->number - port->serial->minor;
1398         struct ti_port_status *data;
1399
1400         dbg("%s - port %d", __func__, port->number);
1401
1402         size = sizeof(struct ti_port_status);
1403         data = kmalloc(size, GFP_KERNEL);
1404         if (!data) {
1405                 dev_err(&port->dev, "%s - out of memory\n", __func__);
1406                 return -ENOMEM;
1407         }
1408
1409         status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1410                 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1411         if (status) {
1412                 dev_err(&port->dev,
1413                         "%s - get port status command failed, %d\n",
1414                                                         __func__, status);
1415                 goto free_data;
1416         }
1417
1418         dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1419
1420         tport->tp_lsr = data->bLSR;
1421
1422 free_data:
1423         kfree(data);
1424         return status;
1425 }
1426
1427
1428 static int ti_get_serial_info(struct ti_port *tport,
1429         struct serial_struct __user *ret_arg)
1430 {
1431         struct usb_serial_port *port = tport->tp_port;
1432         struct serial_struct ret_serial;
1433
1434         if (!ret_arg)
1435                 return -EFAULT;
1436
1437         memset(&ret_serial, 0, sizeof(ret_serial));
1438
1439         ret_serial.type = PORT_16550A;
1440         ret_serial.line = port->serial->minor;
1441         ret_serial.port = port->number - port->serial->minor;
1442         ret_serial.flags = tport->tp_flags;
1443         ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1444         ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1445         ret_serial.closing_wait = tport->tp_closing_wait;
1446
1447         if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1448                 return -EFAULT;
1449
1450         return 0;
1451 }
1452
1453
1454 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1455         struct serial_struct __user *new_arg)
1456 {
1457         struct serial_struct new_serial;
1458
1459         if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1460                 return -EFAULT;
1461
1462         tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1463         tport->tp_closing_wait = new_serial.closing_wait;
1464
1465         return 0;
1466 }
1467
1468
1469 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1470 {
1471         struct async_icount *icount;
1472         struct tty_struct *tty;
1473         unsigned long flags;
1474
1475         dbg("%s - msr 0x%02X", __func__, msr);
1476
1477         if (msr & TI_MSR_DELTA_MASK) {
1478                 spin_lock_irqsave(&tport->tp_lock, flags);
1479                 icount = &tport->tp_icount;
1480                 if (msr & TI_MSR_DELTA_CTS)
1481                         icount->cts++;
1482                 if (msr & TI_MSR_DELTA_DSR)
1483                         icount->dsr++;
1484                 if (msr & TI_MSR_DELTA_CD)
1485                         icount->dcd++;
1486                 if (msr & TI_MSR_DELTA_RI)
1487                         icount->rng++;
1488                 wake_up_interruptible(&tport->tp_port->delta_msr_wait);
1489                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1490         }
1491
1492         tport->tp_msr = msr & TI_MSR_MASK;
1493
1494         /* handle CTS flow control */
1495         tty = tty_port_tty_get(&tport->tp_port->port);
1496         if (tty && C_CRTSCTS(tty)) {
1497                 if (msr & TI_MSR_CTS) {
1498                         tty->hw_stopped = 0;
1499                         tty_wakeup(tty);
1500                 } else {
1501                         tty->hw_stopped = 1;
1502                 }
1503         }
1504         tty_kref_put(tty);
1505 }
1506
1507
1508 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1509 {
1510         struct ti_device *tdev = tport->tp_tdev;
1511         struct usb_serial_port *port = tport->tp_port;
1512         wait_queue_t wait;
1513
1514         dbg("%s - port %d", __func__, port->number);
1515
1516         spin_lock_irq(&tport->tp_lock);
1517
1518         /* wait for data to drain from the buffer */
1519         tdev->td_urb_error = 0;
1520         init_waitqueue_entry(&wait, current);
1521         add_wait_queue(&tport->tp_write_wait, &wait);
1522         for (;;) {
1523                 set_current_state(TASK_INTERRUPTIBLE);
1524                 if (kfifo_len(&tport->write_fifo) == 0
1525                 || timeout == 0 || signal_pending(current)
1526                 || tdev->td_urb_error
1527                 || port->serial->disconnected)  /* disconnect */
1528                         break;
1529                 spin_unlock_irq(&tport->tp_lock);
1530                 timeout = schedule_timeout(timeout);
1531                 spin_lock_irq(&tport->tp_lock);
1532         }
1533         set_current_state(TASK_RUNNING);
1534         remove_wait_queue(&tport->tp_write_wait, &wait);
1535
1536         /* flush any remaining data in the buffer */
1537         if (flush)
1538                 kfifo_reset_out(&tport->write_fifo);
1539
1540         spin_unlock_irq(&tport->tp_lock);
1541
1542         mutex_lock(&port->serial->disc_mutex);
1543         /* wait for data to drain from the device */
1544         /* wait for empty tx register, plus 20 ms */
1545         timeout += jiffies;
1546         tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1547         while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1548         && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1549         && !port->serial->disconnected) {
1550                 if (ti_get_lsr(tport))
1551                         break;
1552                 mutex_unlock(&port->serial->disc_mutex);
1553                 msleep_interruptible(20);
1554                 mutex_lock(&port->serial->disc_mutex);
1555         }
1556         mutex_unlock(&port->serial->disc_mutex);
1557 }
1558
1559
1560 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1561 {
1562         unsigned long flags;
1563
1564         spin_lock_irqsave(&tport->tp_lock, flags);
1565
1566         if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1567                 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1568
1569         spin_unlock_irqrestore(&tport->tp_lock, flags);
1570 }
1571
1572
1573 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1574 {
1575         struct urb *urb;
1576         int status = 0;
1577         unsigned long flags;
1578
1579         spin_lock_irqsave(&tport->tp_lock, flags);
1580
1581         if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1582                 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1583                 urb = tport->tp_port->read_urb;
1584                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1585                 urb->complete = ti_bulk_in_callback;
1586                 urb->context = tport;
1587                 urb->dev = tport->tp_port->serial->dev;
1588                 status = usb_submit_urb(urb, GFP_KERNEL);
1589         } else  {
1590                 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1591                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1592         }
1593
1594         return status;
1595 }
1596
1597
1598 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1599         __u16 moduleid, __u16 value, __u8 *data, int size)
1600 {
1601         int status;
1602
1603         status = usb_control_msg(tdev->td_serial->dev,
1604                 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1605                 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1606                 value, moduleid, data, size, 1000);
1607
1608         if (status == size)
1609                 status = 0;
1610
1611         if (status > 0)
1612                 status = -ECOMM;
1613
1614         return status;
1615 }
1616
1617
1618 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1619         __u16 moduleid, __u16 value, __u8 *data, int size)
1620 {
1621         int status;
1622
1623         status = usb_control_msg(tdev->td_serial->dev,
1624                 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1625                 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1626                 value, moduleid, data, size, 1000);
1627
1628         if (status == size)
1629                 status = 0;
1630
1631         if (status > 0)
1632                 status = -ECOMM;
1633
1634         return status;
1635 }
1636
1637
1638 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1639         __u8 mask, __u8 byte)
1640 {
1641         int status;
1642         unsigned int size;
1643         struct ti_write_data_bytes *data;
1644         struct device *dev = &tdev->td_serial->dev->dev;
1645
1646         dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1647                                         __func__, addr, mask, byte);
1648
1649         size = sizeof(struct ti_write_data_bytes) + 2;
1650         data = kmalloc(size, GFP_KERNEL);
1651         if (!data) {
1652                 dev_err(dev, "%s - out of memory\n", __func__);
1653                 return -ENOMEM;
1654         }
1655
1656         data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1657         data->bDataType = TI_RW_DATA_BYTE;
1658         data->bDataCounter = 1;
1659         data->wBaseAddrHi = cpu_to_be16(addr>>16);
1660         data->wBaseAddrLo = cpu_to_be16(addr);
1661         data->bData[0] = mask;
1662         data->bData[1] = byte;
1663
1664         status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1665                 (__u8 *)data, size);
1666
1667         if (status < 0)
1668                 dev_err(dev, "%s - failed, %d\n", __func__, status);
1669
1670         kfree(data);
1671
1672         return status;
1673 }
1674
1675 static int ti_do_download(struct usb_device *dev, int pipe,
1676                                                 u8 *buffer, int size)
1677 {
1678         int pos;
1679         u8 cs = 0;
1680         int done;
1681         struct ti_firmware_header *header;
1682         int status = 0;
1683         int len;
1684
1685         for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1686                 cs = (__u8)(cs + buffer[pos]);
1687
1688         header = (struct ti_firmware_header *)buffer;
1689         header->wLength = cpu_to_le16((__u16)(size
1690                                         - sizeof(struct ti_firmware_header)));
1691         header->bCheckSum = cs;
1692
1693         dbg("%s - downloading firmware", __func__);
1694         for (pos = 0; pos < size; pos += done) {
1695                 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1696                 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1697                                                                 &done, 1000);
1698                 if (status)
1699                         break;
1700         }
1701         return status;
1702 }
1703
1704 static int ti_download_firmware(struct ti_device *tdev)
1705 {
1706         int status;
1707         int buffer_size;
1708         __u8 *buffer;
1709         struct usb_device *dev = tdev->td_serial->dev;
1710         unsigned int pipe = usb_sndbulkpipe(dev,
1711                 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1712         const struct firmware *fw_p;
1713         char buf[32];
1714
1715         dbg("%s\n", __func__);
1716         /* try ID specific firmware first, then try generic firmware */
1717         sprintf(buf, "ti_usb-v%04x-p%04x.fw",
1718                         le16_to_cpu(dev->descriptor.idVendor),
1719                         le16_to_cpu(dev->descriptor.idProduct));
1720         if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) {
1721                 buf[0] = '\0';
1722                 if (le16_to_cpu(dev->descriptor.idVendor) == MTS_VENDOR_ID) {
1723                         switch (le16_to_cpu(dev->descriptor.idProduct)) {
1724                         case MTS_CDMA_PRODUCT_ID:
1725                                 strcpy(buf, "mts_cdma.fw");
1726                                 break;
1727                         case MTS_GSM_PRODUCT_ID:
1728                                 strcpy(buf, "mts_gsm.fw");
1729                                 break;
1730                         case MTS_EDGE_PRODUCT_ID:
1731                                 strcpy(buf, "mts_edge.fw");
1732                                 break;
1733                         case MTS_MT9234MU_PRODUCT_ID:
1734                                 strcpy(buf, "mts_mt9234mu.fw");
1735                                 break;
1736                         case MTS_MT9234ZBA_PRODUCT_ID:
1737                                 strcpy(buf, "mts_mt9234zba.fw");
1738                                 break;
1739                         case MTS_MT9234ZBAOLD_PRODUCT_ID:
1740                                 strcpy(buf, "mts_mt9234zba.fw");
1741                                 break;                  }
1742                 }
1743                 if (buf[0] == '\0') {
1744                         if (tdev->td_is_3410)
1745                                 strcpy(buf, "ti_3410.fw");
1746                         else
1747                                 strcpy(buf, "ti_5052.fw");
1748                 }
1749                 status = request_firmware(&fw_p, buf, &dev->dev);
1750         }
1751         if (status) {
1752                 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1753                 return -ENOENT;
1754         }
1755         if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1756                 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1757                 release_firmware(fw_p);
1758                 return -ENOENT;
1759         }
1760
1761         buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1762         buffer = kmalloc(buffer_size, GFP_KERNEL);
1763         if (buffer) {
1764                 memcpy(buffer, fw_p->data, fw_p->size);
1765                 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1766                 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1767                 kfree(buffer);
1768         } else {
1769                 dbg("%s ENOMEM\n", __func__);
1770                 status = -ENOMEM;
1771         }
1772         release_firmware(fw_p);
1773         if (status) {
1774                 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1775                                                         __func__, status);
1776                 return status;
1777         }
1778
1779         dbg("%s - download successful", __func__);
1780
1781         return 0;
1782 }