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