Merge branch 'usb-3.3-rc4' into usb-next
[cascardo/linux.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 bool 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->context = tdev;
541                 status = usb_submit_urb(urb, GFP_KERNEL);
542                 if (status) {
543                         dev_err(&port->dev,
544                                 "%s - submit interrupt urb failed, %d\n",
545                                         __func__, status);
546                         goto release_lock;
547                 }
548         }
549
550         if (tty)
551                 ti_set_termios(tty, port, tty->termios);
552
553         dbg("%s - sending TI_OPEN_PORT", __func__);
554         status = ti_command_out_sync(tdev, TI_OPEN_PORT,
555                 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
556         if (status) {
557                 dev_err(&port->dev, "%s - cannot send open command, %d\n",
558                                                         __func__, status);
559                 goto unlink_int_urb;
560         }
561
562         dbg("%s - sending TI_START_PORT", __func__);
563         status = ti_command_out_sync(tdev, TI_START_PORT,
564                 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
565         if (status) {
566                 dev_err(&port->dev, "%s - cannot send start command, %d\n",
567                                                         __func__, status);
568                 goto unlink_int_urb;
569         }
570
571         dbg("%s - sending TI_PURGE_PORT", __func__);
572         status = ti_command_out_sync(tdev, TI_PURGE_PORT,
573                 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
574         if (status) {
575                 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
576                                                         __func__, status);
577                 goto unlink_int_urb;
578         }
579         status = ti_command_out_sync(tdev, TI_PURGE_PORT,
580                 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
581         if (status) {
582                 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
583                                                         __func__, status);
584                 goto unlink_int_urb;
585         }
586
587         /* reset the data toggle on the bulk endpoints to work around bug in
588          * host controllers where things get out of sync some times */
589         usb_clear_halt(dev, port->write_urb->pipe);
590         usb_clear_halt(dev, port->read_urb->pipe);
591
592         if (tty)
593                 ti_set_termios(tty, port, tty->termios);
594
595         dbg("%s - sending TI_OPEN_PORT (2)", __func__);
596         status = ti_command_out_sync(tdev, TI_OPEN_PORT,
597                 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
598         if (status) {
599                 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
600                                                         __func__, status);
601                 goto unlink_int_urb;
602         }
603
604         dbg("%s - sending TI_START_PORT (2)", __func__);
605         status = ti_command_out_sync(tdev, TI_START_PORT,
606                 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
607         if (status) {
608                 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
609                                                         __func__, status);
610                 goto unlink_int_urb;
611         }
612
613         /* start read urb */
614         dbg("%s - start read urb", __func__);
615         urb = port->read_urb;
616         if (!urb) {
617                 dev_err(&port->dev, "%s - no read urb\n", __func__);
618                 status = -EINVAL;
619                 goto unlink_int_urb;
620         }
621         tport->tp_read_urb_state = TI_READ_URB_RUNNING;
622         urb->context = tport;
623         status = usb_submit_urb(urb, GFP_KERNEL);
624         if (status) {
625                 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
626                                                         __func__, status);
627                 goto unlink_int_urb;
628         }
629
630         tport->tp_is_open = 1;
631         ++tdev->td_open_port_count;
632
633         goto release_lock;
634
635 unlink_int_urb:
636         if (tdev->td_open_port_count == 0)
637                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
638 release_lock:
639         mutex_unlock(&tdev->td_open_close_lock);
640         dbg("%s - exit %d", __func__, status);
641         return status;
642 }
643
644
645 static void ti_close(struct usb_serial_port *port)
646 {
647         struct ti_device *tdev;
648         struct ti_port *tport;
649         int port_number;
650         int status;
651         int do_unlock;
652
653         dbg("%s - port %d", __func__, port->number);
654
655         tdev = usb_get_serial_data(port->serial);
656         tport = usb_get_serial_port_data(port);
657         if (tdev == NULL || tport == NULL)
658                 return;
659
660         tport->tp_is_open = 0;
661
662         ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
663
664         usb_kill_urb(port->read_urb);
665         usb_kill_urb(port->write_urb);
666         tport->tp_write_urb_in_use = 0;
667
668         port_number = port->number - port->serial->minor;
669
670         dbg("%s - sending TI_CLOSE_PORT", __func__);
671         status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
672                      (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
673         if (status)
674                 dev_err(&port->dev,
675                         "%s - cannot send close port command, %d\n"
676                                                         , __func__, status);
677
678         /* if mutex_lock is interrupted, continue anyway */
679         do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
680         --tport->tp_tdev->td_open_port_count;
681         if (tport->tp_tdev->td_open_port_count <= 0) {
682                 /* last port is closed, shut down interrupt urb */
683                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
684                 tport->tp_tdev->td_open_port_count = 0;
685         }
686         if (do_unlock)
687                 mutex_unlock(&tdev->td_open_close_lock);
688
689         dbg("%s - exit", __func__);
690 }
691
692
693 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
694                         const unsigned char *data, int count)
695 {
696         struct ti_port *tport = usb_get_serial_port_data(port);
697
698         dbg("%s - port %d", __func__, port->number);
699
700         if (count == 0) {
701                 dbg("%s - write request of 0 bytes", __func__);
702                 return 0;
703         }
704
705         if (tport == NULL || !tport->tp_is_open)
706                 return -ENODEV;
707
708         count = kfifo_in_locked(&tport->write_fifo, data, count,
709                                                         &tport->tp_lock);
710         ti_send(tport);
711
712         return count;
713 }
714
715
716 static int ti_write_room(struct tty_struct *tty)
717 {
718         struct usb_serial_port *port = tty->driver_data;
719         struct ti_port *tport = usb_get_serial_port_data(port);
720         int room = 0;
721         unsigned long flags;
722
723         dbg("%s - port %d", __func__, port->number);
724
725         if (tport == NULL)
726                 return 0;
727
728         spin_lock_irqsave(&tport->tp_lock, flags);
729         room = kfifo_avail(&tport->write_fifo);
730         spin_unlock_irqrestore(&tport->tp_lock, flags);
731
732         dbg("%s - returns %d", __func__, room);
733         return room;
734 }
735
736
737 static int ti_chars_in_buffer(struct tty_struct *tty)
738 {
739         struct usb_serial_port *port = tty->driver_data;
740         struct ti_port *tport = usb_get_serial_port_data(port);
741         int chars = 0;
742         unsigned long flags;
743
744         dbg("%s - port %d", __func__, port->number);
745
746         if (tport == NULL)
747                 return 0;
748
749         spin_lock_irqsave(&tport->tp_lock, flags);
750         chars = kfifo_len(&tport->write_fifo);
751         spin_unlock_irqrestore(&tport->tp_lock, flags);
752
753         dbg("%s - returns %d", __func__, chars);
754         return chars;
755 }
756
757
758 static void ti_throttle(struct tty_struct *tty)
759 {
760         struct usb_serial_port *port = tty->driver_data;
761         struct ti_port *tport = usb_get_serial_port_data(port);
762
763         dbg("%s - port %d", __func__, port->number);
764
765         if (tport == NULL)
766                 return;
767
768         if (I_IXOFF(tty) || C_CRTSCTS(tty))
769                 ti_stop_read(tport, tty);
770
771 }
772
773
774 static void ti_unthrottle(struct tty_struct *tty)
775 {
776         struct usb_serial_port *port = tty->driver_data;
777         struct ti_port *tport = usb_get_serial_port_data(port);
778         int status;
779
780         dbg("%s - port %d", __func__, port->number);
781
782         if (tport == NULL)
783                 return;
784
785         if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
786                 status = ti_restart_read(tport, tty);
787                 if (status)
788                         dev_err(&port->dev, "%s - cannot restart read, %d\n",
789                                                         __func__, status);
790         }
791 }
792
793 static int ti_get_icount(struct tty_struct *tty,
794                 struct serial_icounter_struct *icount)
795 {
796         struct usb_serial_port *port = tty->driver_data;
797         struct ti_port *tport = usb_get_serial_port_data(port);
798         struct async_icount cnow = tport->tp_icount;
799
800         dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
801                 __func__, port->number,
802                 cnow.rx, cnow.tx);
803
804         icount->cts = cnow.cts;
805         icount->dsr = cnow.dsr;
806         icount->rng = cnow.rng;
807         icount->dcd = cnow.dcd;
808         icount->rx = cnow.rx;
809         icount->tx = cnow.tx;
810         icount->frame = cnow.frame;
811         icount->overrun = cnow.overrun;
812         icount->parity = cnow.parity;
813         icount->brk = cnow.brk;
814         icount->buf_overrun = cnow.buf_overrun;
815
816         return 0;
817 }
818
819 static int ti_ioctl(struct tty_struct *tty,
820         unsigned int cmd, unsigned long arg)
821 {
822         struct usb_serial_port *port = tty->driver_data;
823         struct ti_port *tport = usb_get_serial_port_data(port);
824         struct async_icount cnow;
825         struct async_icount cprev;
826
827         dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
828
829         if (tport == NULL)
830                 return -ENODEV;
831
832         switch (cmd) {
833         case TIOCGSERIAL:
834                 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
835                 return ti_get_serial_info(tport,
836                                 (struct serial_struct __user *)arg);
837         case TIOCSSERIAL:
838                 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
839                 return ti_set_serial_info(tty, tport,
840                                 (struct serial_struct __user *)arg);
841         case TIOCMIWAIT:
842                 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
843                 cprev = tport->tp_icount;
844                 while (1) {
845                         interruptible_sleep_on(&tport->tp_msr_wait);
846                         if (signal_pending(current))
847                                 return -ERESTARTSYS;
848                         cnow = tport->tp_icount;
849                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
850                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
851                                 return -EIO; /* no change => error */
852                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
853                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
854                             ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
855                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
856                                 return 0;
857                         cprev = cnow;
858                 }
859                 break;
860         }
861         return -ENOIOCTLCMD;
862 }
863
864
865 static void ti_set_termios(struct tty_struct *tty,
866                 struct usb_serial_port *port, struct ktermios *old_termios)
867 {
868         struct ti_port *tport = usb_get_serial_port_data(port);
869         struct ti_uart_config *config;
870         tcflag_t cflag, iflag;
871         int baud;
872         int status;
873         int port_number = port->number - port->serial->minor;
874         unsigned int mcr;
875
876         dbg("%s - port %d", __func__, port->number);
877
878         cflag = tty->termios->c_cflag;
879         iflag = tty->termios->c_iflag;
880
881         dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
882         dbg("%s - old clfag %08x, old iflag %08x", __func__,
883                                 old_termios->c_cflag, old_termios->c_iflag);
884
885         if (tport == NULL)
886                 return;
887
888         config = kmalloc(sizeof(*config), GFP_KERNEL);
889         if (!config) {
890                 dev_err(&port->dev, "%s - out of memory\n", __func__);
891                 return;
892         }
893
894         config->wFlags = 0;
895
896         /* these flags must be set */
897         config->wFlags |= TI_UART_ENABLE_MS_INTS;
898         config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
899         config->bUartMode = (__u8)(tport->tp_uart_mode);
900
901         switch (cflag & CSIZE) {
902         case CS5:
903                     config->bDataBits = TI_UART_5_DATA_BITS;
904                     break;
905         case CS6:
906                     config->bDataBits = TI_UART_6_DATA_BITS;
907                     break;
908         case CS7:
909                     config->bDataBits = TI_UART_7_DATA_BITS;
910                     break;
911         default:
912         case CS8:
913                     config->bDataBits = TI_UART_8_DATA_BITS;
914                     break;
915         }
916
917         /* CMSPAR isn't supported by this driver */
918         tty->termios->c_cflag &= ~CMSPAR;
919
920         if (cflag & PARENB) {
921                 if (cflag & PARODD) {
922                         config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
923                         config->bParity = TI_UART_ODD_PARITY;
924                 } else {
925                         config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
926                         config->bParity = TI_UART_EVEN_PARITY;
927                 }
928         } else {
929                 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
930                 config->bParity = TI_UART_NO_PARITY;
931         }
932
933         if (cflag & CSTOPB)
934                 config->bStopBits = TI_UART_2_STOP_BITS;
935         else
936                 config->bStopBits = TI_UART_1_STOP_BITS;
937
938         if (cflag & CRTSCTS) {
939                 /* RTS flow control must be off to drop RTS for baud rate B0 */
940                 if ((cflag & CBAUD) != B0)
941                         config->wFlags |= TI_UART_ENABLE_RTS_IN;
942                 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
943         } else {
944                 tty->hw_stopped = 0;
945                 ti_restart_read(tport, tty);
946         }
947
948         if (I_IXOFF(tty) || I_IXON(tty)) {
949                 config->cXon  = START_CHAR(tty);
950                 config->cXoff = STOP_CHAR(tty);
951
952                 if (I_IXOFF(tty))
953                         config->wFlags |= TI_UART_ENABLE_X_IN;
954                 else
955                         ti_restart_read(tport, tty);
956
957                 if (I_IXON(tty))
958                         config->wFlags |= TI_UART_ENABLE_X_OUT;
959         }
960
961         baud = tty_get_baud_rate(tty);
962         if (!baud)
963                 baud = 9600;
964         if (tport->tp_tdev->td_is_3410)
965                 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
966         else
967                 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
968
969         /* FIXME: Should calculate resulting baud here and report it back */
970         if ((cflag & CBAUD) != B0)
971                 tty_encode_baud_rate(tty, baud, baud);
972
973         dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
974         __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
975
976         cpu_to_be16s(&config->wBaudRate);
977         cpu_to_be16s(&config->wFlags);
978
979         status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
980                 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
981                 sizeof(*config));
982         if (status)
983                 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
984                                         __func__, port_number, status);
985
986         /* SET_CONFIG asserts RTS and DTR, reset them correctly */
987         mcr = tport->tp_shadow_mcr;
988         /* if baud rate is B0, clear RTS and DTR */
989         if ((cflag & CBAUD) == B0)
990                 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
991         status = ti_set_mcr(tport, mcr);
992         if (status)
993                 dev_err(&port->dev,
994                         "%s - cannot set modem control on port %d, %d\n",
995                                                 __func__, port_number, status);
996
997         kfree(config);
998 }
999
1000
1001 static int ti_tiocmget(struct tty_struct *tty)
1002 {
1003         struct usb_serial_port *port = tty->driver_data;
1004         struct ti_port *tport = usb_get_serial_port_data(port);
1005         unsigned int result;
1006         unsigned int msr;
1007         unsigned int mcr;
1008         unsigned long flags;
1009
1010         dbg("%s - port %d", __func__, port->number);
1011
1012         if (tport == NULL)
1013                 return -ENODEV;
1014
1015         spin_lock_irqsave(&tport->tp_lock, flags);
1016         msr = tport->tp_msr;
1017         mcr = tport->tp_shadow_mcr;
1018         spin_unlock_irqrestore(&tport->tp_lock, flags);
1019
1020         result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1021                 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1022                 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1023                 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1024                 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1025                 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1026                 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1027
1028         dbg("%s - 0x%04X", __func__, result);
1029
1030         return result;
1031 }
1032
1033
1034 static int ti_tiocmset(struct tty_struct *tty,
1035                                 unsigned int set, unsigned int clear)
1036 {
1037         struct usb_serial_port *port = tty->driver_data;
1038         struct ti_port *tport = usb_get_serial_port_data(port);
1039         unsigned int mcr;
1040         unsigned long flags;
1041
1042         dbg("%s - port %d", __func__, port->number);
1043
1044         if (tport == NULL)
1045                 return -ENODEV;
1046
1047         spin_lock_irqsave(&tport->tp_lock, flags);
1048         mcr = tport->tp_shadow_mcr;
1049
1050         if (set & TIOCM_RTS)
1051                 mcr |= TI_MCR_RTS;
1052         if (set & TIOCM_DTR)
1053                 mcr |= TI_MCR_DTR;
1054         if (set & TIOCM_LOOP)
1055                 mcr |= TI_MCR_LOOP;
1056
1057         if (clear & TIOCM_RTS)
1058                 mcr &= ~TI_MCR_RTS;
1059         if (clear & TIOCM_DTR)
1060                 mcr &= ~TI_MCR_DTR;
1061         if (clear & TIOCM_LOOP)
1062                 mcr &= ~TI_MCR_LOOP;
1063         spin_unlock_irqrestore(&tport->tp_lock, flags);
1064
1065         return ti_set_mcr(tport, mcr);
1066 }
1067
1068
1069 static void ti_break(struct tty_struct *tty, int break_state)
1070 {
1071         struct usb_serial_port *port = tty->driver_data;
1072         struct ti_port *tport = usb_get_serial_port_data(port);
1073         int status;
1074
1075         dbg("%s - state = %d", __func__, break_state);
1076
1077         if (tport == NULL)
1078                 return;
1079
1080         ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1081
1082         status = ti_write_byte(tport->tp_tdev,
1083                 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1084                 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1085
1086         if (status)
1087                 dbg("%s - error setting break, %d", __func__, status);
1088 }
1089
1090
1091 static void ti_interrupt_callback(struct urb *urb)
1092 {
1093         struct ti_device *tdev = urb->context;
1094         struct usb_serial_port *port;
1095         struct usb_serial *serial = tdev->td_serial;
1096         struct ti_port *tport;
1097         struct device *dev = &urb->dev->dev;
1098         unsigned char *data = urb->transfer_buffer;
1099         int length = urb->actual_length;
1100         int port_number;
1101         int function;
1102         int status = urb->status;
1103         int retval;
1104         __u8 msr;
1105
1106         dbg("%s", __func__);
1107
1108         switch (status) {
1109         case 0:
1110                 break;
1111         case -ECONNRESET:
1112         case -ENOENT:
1113         case -ESHUTDOWN:
1114                 dbg("%s - urb shutting down, %d", __func__, status);
1115                 tdev->td_urb_error = 1;
1116                 return;
1117         default:
1118                 dev_err(dev, "%s - nonzero urb status, %d\n",
1119                         __func__, status);
1120                 tdev->td_urb_error = 1;
1121                 goto exit;
1122         }
1123
1124         if (length != 2) {
1125                 dbg("%s - bad packet size, %d", __func__, length);
1126                 goto exit;
1127         }
1128
1129         if (data[0] == TI_CODE_HARDWARE_ERROR) {
1130                 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1131                 goto exit;
1132         }
1133
1134         port_number = TI_GET_PORT_FROM_CODE(data[0]);
1135         function = TI_GET_FUNC_FROM_CODE(data[0]);
1136
1137         dbg("%s - port_number %d, function %d, data 0x%02X",
1138                                 __func__, port_number, function, data[1]);
1139
1140         if (port_number >= serial->num_ports) {
1141                 dev_err(dev, "%s - bad port number, %d\n",
1142                                                 __func__, port_number);
1143                 goto exit;
1144         }
1145
1146         port = serial->port[port_number];
1147
1148         tport = usb_get_serial_port_data(port);
1149         if (!tport)
1150                 goto exit;
1151
1152         switch (function) {
1153         case TI_CODE_DATA_ERROR:
1154                 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1155                                         __func__, port_number, data[1]);
1156                 break;
1157
1158         case TI_CODE_MODEM_STATUS:
1159                 msr = data[1];
1160                 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
1161                 ti_handle_new_msr(tport, msr);
1162                 break;
1163
1164         default:
1165                 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1166                                                         __func__, data[1]);
1167                 break;
1168         }
1169
1170 exit:
1171         retval = usb_submit_urb(urb, GFP_ATOMIC);
1172         if (retval)
1173                 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1174                         __func__, retval);
1175 }
1176
1177
1178 static void ti_bulk_in_callback(struct urb *urb)
1179 {
1180         struct ti_port *tport = urb->context;
1181         struct usb_serial_port *port = tport->tp_port;
1182         struct device *dev = &urb->dev->dev;
1183         int status = urb->status;
1184         int retval = 0;
1185         struct tty_struct *tty;
1186
1187         dbg("%s", __func__);
1188
1189         switch (status) {
1190         case 0:
1191                 break;
1192         case -ECONNRESET:
1193         case -ENOENT:
1194         case -ESHUTDOWN:
1195                 dbg("%s - urb shutting down, %d", __func__, status);
1196                 tport->tp_tdev->td_urb_error = 1;
1197                 wake_up_interruptible(&tport->tp_write_wait);
1198                 return;
1199         default:
1200                 dev_err(dev, "%s - nonzero urb status, %d\n",
1201                         __func__, status);
1202                 tport->tp_tdev->td_urb_error = 1;
1203                 wake_up_interruptible(&tport->tp_write_wait);
1204         }
1205
1206         if (status == -EPIPE)
1207                 goto exit;
1208
1209         if (status) {
1210                 dev_err(dev, "%s - stopping read!\n", __func__);
1211                 return;
1212         }
1213
1214         tty = tty_port_tty_get(&port->port);
1215         if (tty) {
1216                 if (urb->actual_length) {
1217                         usb_serial_debug_data(debug, dev, __func__,
1218                                 urb->actual_length, urb->transfer_buffer);
1219
1220                         if (!tport->tp_is_open)
1221                                 dbg("%s - port closed, dropping data",
1222                                         __func__);
1223                         else
1224                                 ti_recv(&urb->dev->dev, tty,
1225                                                 urb->transfer_buffer,
1226                                                 urb->actual_length);
1227                         spin_lock(&tport->tp_lock);
1228                         tport->tp_icount.rx += urb->actual_length;
1229                         spin_unlock(&tport->tp_lock);
1230                 }
1231                 tty_kref_put(tty);
1232         }
1233
1234 exit:
1235         /* continue to read unless stopping */
1236         spin_lock(&tport->tp_lock);
1237         if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1238                 retval = usb_submit_urb(urb, GFP_ATOMIC);
1239         else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
1240                 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1241
1242         spin_unlock(&tport->tp_lock);
1243         if (retval)
1244                 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1245                         __func__, retval);
1246 }
1247
1248
1249 static void ti_bulk_out_callback(struct urb *urb)
1250 {
1251         struct ti_port *tport = urb->context;
1252         struct usb_serial_port *port = tport->tp_port;
1253         int status = urb->status;
1254
1255         dbg("%s - port %d", __func__, port->number);
1256
1257         tport->tp_write_urb_in_use = 0;
1258
1259         switch (status) {
1260         case 0:
1261                 break;
1262         case -ECONNRESET:
1263         case -ENOENT:
1264         case -ESHUTDOWN:
1265                 dbg("%s - urb shutting down, %d", __func__, status);
1266                 tport->tp_tdev->td_urb_error = 1;
1267                 wake_up_interruptible(&tport->tp_write_wait);
1268                 return;
1269         default:
1270                 dev_err_console(port, "%s - nonzero urb status, %d\n",
1271                         __func__, status);
1272                 tport->tp_tdev->td_urb_error = 1;
1273                 wake_up_interruptible(&tport->tp_write_wait);
1274         }
1275
1276         /* send any buffered data */
1277         ti_send(tport);
1278 }
1279
1280
1281 static void ti_recv(struct device *dev, struct tty_struct *tty,
1282         unsigned char *data, int length)
1283 {
1284         int cnt;
1285
1286         do {
1287                 cnt = tty_insert_flip_string(tty, data, length);
1288                 if (cnt < length) {
1289                         dev_err(dev, "%s - dropping data, %d bytes lost\n",
1290                                                 __func__, length - cnt);
1291                         if (cnt == 0)
1292                                 break;
1293                 }
1294                 tty_flip_buffer_push(tty);
1295                 data += cnt;
1296                 length -= cnt;
1297         } while (length > 0);
1298
1299 }
1300
1301
1302 static void ti_send(struct ti_port *tport)
1303 {
1304         int count, result;
1305         struct usb_serial_port *port = tport->tp_port;
1306         struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1307         unsigned long flags;
1308
1309
1310         dbg("%s - port %d", __func__, port->number);
1311
1312         spin_lock_irqsave(&tport->tp_lock, flags);
1313
1314         if (tport->tp_write_urb_in_use)
1315                 goto unlock;
1316
1317         count = kfifo_out(&tport->write_fifo,
1318                                 port->write_urb->transfer_buffer,
1319                                 port->bulk_out_size);
1320
1321         if (count == 0)
1322                 goto unlock;
1323
1324         tport->tp_write_urb_in_use = 1;
1325
1326         spin_unlock_irqrestore(&tport->tp_lock, flags);
1327
1328         usb_serial_debug_data(debug, &port->dev, __func__, count,
1329                                         port->write_urb->transfer_buffer);
1330
1331         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1332                            usb_sndbulkpipe(port->serial->dev,
1333                                             port->bulk_out_endpointAddress),
1334                            port->write_urb->transfer_buffer, count,
1335                            ti_bulk_out_callback, tport);
1336
1337         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1338         if (result) {
1339                 dev_err_console(port, "%s - submit write urb failed, %d\n",
1340                                                         __func__, result);
1341                 tport->tp_write_urb_in_use = 0;
1342                 /* TODO: reschedule ti_send */
1343         } else {
1344                 spin_lock_irqsave(&tport->tp_lock, flags);
1345                 tport->tp_icount.tx += count;
1346                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1347         }
1348
1349         /* more room in the buffer for new writes, wakeup */
1350         if (tty)
1351                 tty_wakeup(tty);
1352         tty_kref_put(tty);
1353         wake_up_interruptible(&tport->tp_write_wait);
1354         return;
1355 unlock:
1356         spin_unlock_irqrestore(&tport->tp_lock, flags);
1357         tty_kref_put(tty);
1358         return;
1359 }
1360
1361
1362 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1363 {
1364         unsigned long flags;
1365         int status;
1366
1367         status = ti_write_byte(tport->tp_tdev,
1368                 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1369                 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1370
1371         spin_lock_irqsave(&tport->tp_lock, flags);
1372         if (!status)
1373                 tport->tp_shadow_mcr = mcr;
1374         spin_unlock_irqrestore(&tport->tp_lock, flags);
1375
1376         return status;
1377 }
1378
1379
1380 static int ti_get_lsr(struct ti_port *tport)
1381 {
1382         int size, status;
1383         struct ti_device *tdev = tport->tp_tdev;
1384         struct usb_serial_port *port = tport->tp_port;
1385         int port_number = port->number - port->serial->minor;
1386         struct ti_port_status *data;
1387
1388         dbg("%s - port %d", __func__, port->number);
1389
1390         size = sizeof(struct ti_port_status);
1391         data = kmalloc(size, GFP_KERNEL);
1392         if (!data) {
1393                 dev_err(&port->dev, "%s - out of memory\n", __func__);
1394                 return -ENOMEM;
1395         }
1396
1397         status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1398                 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1399         if (status) {
1400                 dev_err(&port->dev,
1401                         "%s - get port status command failed, %d\n",
1402                                                         __func__, status);
1403                 goto free_data;
1404         }
1405
1406         dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1407
1408         tport->tp_lsr = data->bLSR;
1409
1410 free_data:
1411         kfree(data);
1412         return status;
1413 }
1414
1415
1416 static int ti_get_serial_info(struct ti_port *tport,
1417         struct serial_struct __user *ret_arg)
1418 {
1419         struct usb_serial_port *port = tport->tp_port;
1420         struct serial_struct ret_serial;
1421
1422         if (!ret_arg)
1423                 return -EFAULT;
1424
1425         memset(&ret_serial, 0, sizeof(ret_serial));
1426
1427         ret_serial.type = PORT_16550A;
1428         ret_serial.line = port->serial->minor;
1429         ret_serial.port = port->number - port->serial->minor;
1430         ret_serial.flags = tport->tp_flags;
1431         ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1432         ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1433         ret_serial.closing_wait = tport->tp_closing_wait;
1434
1435         if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1436                 return -EFAULT;
1437
1438         return 0;
1439 }
1440
1441
1442 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1443         struct serial_struct __user *new_arg)
1444 {
1445         struct serial_struct new_serial;
1446
1447         if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1448                 return -EFAULT;
1449
1450         tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1451         tport->tp_closing_wait = new_serial.closing_wait;
1452
1453         return 0;
1454 }
1455
1456
1457 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1458 {
1459         struct async_icount *icount;
1460         struct tty_struct *tty;
1461         unsigned long flags;
1462
1463         dbg("%s - msr 0x%02X", __func__, msr);
1464
1465         if (msr & TI_MSR_DELTA_MASK) {
1466                 spin_lock_irqsave(&tport->tp_lock, flags);
1467                 icount = &tport->tp_icount;
1468                 if (msr & TI_MSR_DELTA_CTS)
1469                         icount->cts++;
1470                 if (msr & TI_MSR_DELTA_DSR)
1471                         icount->dsr++;
1472                 if (msr & TI_MSR_DELTA_CD)
1473                         icount->dcd++;
1474                 if (msr & TI_MSR_DELTA_RI)
1475                         icount->rng++;
1476                 wake_up_interruptible(&tport->tp_msr_wait);
1477                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1478         }
1479
1480         tport->tp_msr = msr & TI_MSR_MASK;
1481
1482         /* handle CTS flow control */
1483         tty = tty_port_tty_get(&tport->tp_port->port);
1484         if (tty && C_CRTSCTS(tty)) {
1485                 if (msr & TI_MSR_CTS) {
1486                         tty->hw_stopped = 0;
1487                         tty_wakeup(tty);
1488                 } else {
1489                         tty->hw_stopped = 1;
1490                 }
1491         }
1492         tty_kref_put(tty);
1493 }
1494
1495
1496 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1497 {
1498         struct ti_device *tdev = tport->tp_tdev;
1499         struct usb_serial_port *port = tport->tp_port;
1500         wait_queue_t wait;
1501
1502         dbg("%s - port %d", __func__, port->number);
1503
1504         spin_lock_irq(&tport->tp_lock);
1505
1506         /* wait for data to drain from the buffer */
1507         tdev->td_urb_error = 0;
1508         init_waitqueue_entry(&wait, current);
1509         add_wait_queue(&tport->tp_write_wait, &wait);
1510         for (;;) {
1511                 set_current_state(TASK_INTERRUPTIBLE);
1512                 if (kfifo_len(&tport->write_fifo) == 0
1513                 || timeout == 0 || signal_pending(current)
1514                 || tdev->td_urb_error
1515                 || port->serial->disconnected)  /* disconnect */
1516                         break;
1517                 spin_unlock_irq(&tport->tp_lock);
1518                 timeout = schedule_timeout(timeout);
1519                 spin_lock_irq(&tport->tp_lock);
1520         }
1521         set_current_state(TASK_RUNNING);
1522         remove_wait_queue(&tport->tp_write_wait, &wait);
1523
1524         /* flush any remaining data in the buffer */
1525         if (flush)
1526                 kfifo_reset_out(&tport->write_fifo);
1527
1528         spin_unlock_irq(&tport->tp_lock);
1529
1530         mutex_lock(&port->serial->disc_mutex);
1531         /* wait for data to drain from the device */
1532         /* wait for empty tx register, plus 20 ms */
1533         timeout += jiffies;
1534         tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1535         while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1536         && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1537         && !port->serial->disconnected) {
1538                 if (ti_get_lsr(tport))
1539                         break;
1540                 mutex_unlock(&port->serial->disc_mutex);
1541                 msleep_interruptible(20);
1542                 mutex_lock(&port->serial->disc_mutex);
1543         }
1544         mutex_unlock(&port->serial->disc_mutex);
1545 }
1546
1547
1548 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1549 {
1550         unsigned long flags;
1551
1552         spin_lock_irqsave(&tport->tp_lock, flags);
1553
1554         if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1555                 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1556
1557         spin_unlock_irqrestore(&tport->tp_lock, flags);
1558 }
1559
1560
1561 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1562 {
1563         struct urb *urb;
1564         int status = 0;
1565         unsigned long flags;
1566
1567         spin_lock_irqsave(&tport->tp_lock, flags);
1568
1569         if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1570                 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1571                 urb = tport->tp_port->read_urb;
1572                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1573                 urb->context = tport;
1574                 status = usb_submit_urb(urb, GFP_KERNEL);
1575         } else  {
1576                 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1577                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1578         }
1579
1580         return status;
1581 }
1582
1583
1584 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1585         __u16 moduleid, __u16 value, __u8 *data, int size)
1586 {
1587         int status;
1588
1589         status = usb_control_msg(tdev->td_serial->dev,
1590                 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1591                 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1592                 value, moduleid, data, size, 1000);
1593
1594         if (status == size)
1595                 status = 0;
1596
1597         if (status > 0)
1598                 status = -ECOMM;
1599
1600         return status;
1601 }
1602
1603
1604 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1605         __u16 moduleid, __u16 value, __u8 *data, int size)
1606 {
1607         int status;
1608
1609         status = usb_control_msg(tdev->td_serial->dev,
1610                 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1611                 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1612                 value, moduleid, data, size, 1000);
1613
1614         if (status == size)
1615                 status = 0;
1616
1617         if (status > 0)
1618                 status = -ECOMM;
1619
1620         return status;
1621 }
1622
1623
1624 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1625         __u8 mask, __u8 byte)
1626 {
1627         int status;
1628         unsigned int size;
1629         struct ti_write_data_bytes *data;
1630         struct device *dev = &tdev->td_serial->dev->dev;
1631
1632         dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1633                                         __func__, addr, mask, byte);
1634
1635         size = sizeof(struct ti_write_data_bytes) + 2;
1636         data = kmalloc(size, GFP_KERNEL);
1637         if (!data) {
1638                 dev_err(dev, "%s - out of memory\n", __func__);
1639                 return -ENOMEM;
1640         }
1641
1642         data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1643         data->bDataType = TI_RW_DATA_BYTE;
1644         data->bDataCounter = 1;
1645         data->wBaseAddrHi = cpu_to_be16(addr>>16);
1646         data->wBaseAddrLo = cpu_to_be16(addr);
1647         data->bData[0] = mask;
1648         data->bData[1] = byte;
1649
1650         status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1651                 (__u8 *)data, size);
1652
1653         if (status < 0)
1654                 dev_err(dev, "%s - failed, %d\n", __func__, status);
1655
1656         kfree(data);
1657
1658         return status;
1659 }
1660
1661 static int ti_do_download(struct usb_device *dev, int pipe,
1662                                                 u8 *buffer, int size)
1663 {
1664         int pos;
1665         u8 cs = 0;
1666         int done;
1667         struct ti_firmware_header *header;
1668         int status = 0;
1669         int len;
1670
1671         for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1672                 cs = (__u8)(cs + buffer[pos]);
1673
1674         header = (struct ti_firmware_header *)buffer;
1675         header->wLength = cpu_to_le16((__u16)(size
1676                                         - sizeof(struct ti_firmware_header)));
1677         header->bCheckSum = cs;
1678
1679         dbg("%s - downloading firmware", __func__);
1680         for (pos = 0; pos < size; pos += done) {
1681                 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1682                 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1683                                                                 &done, 1000);
1684                 if (status)
1685                         break;
1686         }
1687         return status;
1688 }
1689
1690 static int ti_download_firmware(struct ti_device *tdev)
1691 {
1692         int status;
1693         int buffer_size;
1694         __u8 *buffer;
1695         struct usb_device *dev = tdev->td_serial->dev;
1696         unsigned int pipe = usb_sndbulkpipe(dev,
1697                 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1698         const struct firmware *fw_p;
1699         char buf[32];
1700
1701         dbg("%s\n", __func__);
1702         /* try ID specific firmware first, then try generic firmware */
1703         sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1704             dev->descriptor.idProduct);
1705         if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) {
1706                 buf[0] = '\0';
1707                 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1708                         switch (dev->descriptor.idProduct) {
1709                         case MTS_CDMA_PRODUCT_ID:
1710                                 strcpy(buf, "mts_cdma.fw");
1711                                 break;
1712                         case MTS_GSM_PRODUCT_ID:
1713                                 strcpy(buf, "mts_gsm.fw");
1714                                 break;
1715                         case MTS_EDGE_PRODUCT_ID:
1716                                 strcpy(buf, "mts_edge.fw");
1717                                 break;
1718                         case MTS_MT9234MU_PRODUCT_ID:
1719                                 strcpy(buf, "mts_mt9234mu.fw");
1720                                 break;
1721                         case MTS_MT9234ZBA_PRODUCT_ID:
1722                                 strcpy(buf, "mts_mt9234zba.fw");
1723                                 break;
1724                         case MTS_MT9234ZBAOLD_PRODUCT_ID:
1725                                 strcpy(buf, "mts_mt9234zba.fw");
1726                                 break;                  }
1727                 }
1728                 if (buf[0] == '\0') {
1729                         if (tdev->td_is_3410)
1730                                 strcpy(buf, "ti_3410.fw");
1731                         else
1732                                 strcpy(buf, "ti_5052.fw");
1733                 }
1734                 status = request_firmware(&fw_p, buf, &dev->dev);
1735         }
1736         if (status) {
1737                 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1738                 return -ENOENT;
1739         }
1740         if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1741                 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1742                 release_firmware(fw_p);
1743                 return -ENOENT;
1744         }
1745
1746         buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1747         buffer = kmalloc(buffer_size, GFP_KERNEL);
1748         if (buffer) {
1749                 memcpy(buffer, fw_p->data, fw_p->size);
1750                 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1751                 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1752                 kfree(buffer);
1753         } else {
1754                 dbg("%s ENOMEM\n", __func__);
1755                 status = -ENOMEM;
1756         }
1757         release_firmware(fw_p);
1758         if (status) {
1759                 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1760                                                         __func__, status);
1761                 return status;
1762         }
1763
1764         dbg("%s - download successful", __func__);
1765
1766         return 0;
1767 }