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