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