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