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