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