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