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