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