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