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