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