3 * TI 3410/5052 USB Serial Driver
5 * Copyright (C) 2004 Texas Instruments
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
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.
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>.
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/firmware.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h>
29 #include <linux/module.h>
30 #include <linux/spinlock.h>
31 #include <linux/ioctl.h>
32 #include <linux/serial.h>
33 #include <linux/circ_buf.h>
34 #include <linux/mutex.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
39 #include "ti_usb_3410_5052.h"
43 #define TI_DRIVER_VERSION "v0.9"
44 #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
45 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
47 #define TI_FIRMWARE_BUF_SIZE 16284
49 #define TI_WRITE_BUF_SIZE 1024
51 #define TI_TRANSFER_TIMEOUT 2
53 #define TI_DEFAULT_LOW_LATENCY 0
54 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
56 /* supported setserial flags */
57 #define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
60 #define TI_READ_URB_RUNNING 0
61 #define TI_READ_URB_STOPPING 1
62 #define TI_READ_URB_STOPPED 2
64 #define TI_EXTRA_VID_PID_COUNT 5
74 __u8 tp_uart_mode; /* 232 or 485 modes */
75 unsigned int tp_uart_base_addr;
77 int tp_closing_wait;/* in .01 secs */
78 struct async_icount tp_icount;
79 wait_queue_head_t tp_msr_wait; /* wait for msr change */
80 wait_queue_head_t tp_write_wait;
81 struct ti_device *tp_tdev;
82 struct usb_serial_port *tp_port;
84 int tp_read_urb_state;
85 int tp_write_urb_in_use;
86 struct circ_buf *tp_write_buf;
90 struct mutex td_open_close_lock;
91 int td_open_port_count;
92 struct usb_serial *td_serial;
98 /* Function Declarations */
100 static int ti_startup(struct usb_serial *serial);
101 static void ti_shutdown(struct usb_serial *serial);
102 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port,
104 static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
106 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
107 const unsigned char *data, int count);
108 static int ti_write_room(struct tty_struct *tty);
109 static int ti_chars_in_buffer(struct tty_struct *tty);
110 static void ti_throttle(struct tty_struct *tty);
111 static void ti_unthrottle(struct tty_struct *tty);
112 static int ti_ioctl(struct tty_struct *tty, struct file *file,
113 unsigned int cmd, unsigned long arg);
114 static void ti_set_termios(struct tty_struct *tty,
115 struct usb_serial_port *port, struct ktermios *old_termios);
116 static int ti_tiocmget(struct tty_struct *tty, struct file *file);
117 static int ti_tiocmset(struct tty_struct *tty, struct file *file,
118 unsigned int set, unsigned int clear);
119 static void ti_break(struct tty_struct *tty, int break_state);
120 static void ti_interrupt_callback(struct urb *urb);
121 static void ti_bulk_in_callback(struct urb *urb);
122 static void ti_bulk_out_callback(struct urb *urb);
124 static void ti_recv(struct device *dev, struct tty_struct *tty,
125 unsigned char *data, int length);
126 static void ti_send(struct ti_port *tport);
127 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
128 static int ti_get_lsr(struct ti_port *tport);
129 static int ti_get_serial_info(struct ti_port *tport,
130 struct serial_struct __user *ret_arg);
131 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
132 struct serial_struct __user *new_arg);
133 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
135 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
137 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
138 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
140 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
141 __u16 moduleid, __u16 value, __u8 *data, int size);
142 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
143 __u16 moduleid, __u16 value, __u8 *data, int size);
145 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
146 __u8 mask, __u8 byte);
148 static int ti_download_firmware(struct ti_device *tdev);
150 /* circular buffer */
151 static struct circ_buf *ti_buf_alloc(void);
152 static void ti_buf_free(struct circ_buf *cb);
153 static void ti_buf_clear(struct circ_buf *cb);
154 static int ti_buf_data_avail(struct circ_buf *cb);
155 static int ti_buf_space_avail(struct circ_buf *cb);
156 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
157 static int ti_buf_get(struct circ_buf *cb, char *buf, int count);
162 /* module parameters */
164 static int low_latency = TI_DEFAULT_LOW_LATENCY;
165 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
166 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
167 static unsigned int vendor_3410_count;
168 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
169 static unsigned int product_3410_count;
170 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
171 static unsigned int vendor_5052_count;
172 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
173 static unsigned int product_5052_count;
175 /* supported devices */
176 /* the array dimension is the number of default entries plus */
177 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
179 static struct usb_device_id ti_id_table_3410[10+TI_EXTRA_VID_PID_COUNT+1] = {
180 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
181 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
182 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
183 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
184 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
185 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
186 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
187 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
188 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
189 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
192 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
193 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
194 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
195 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
196 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
197 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
200 static struct usb_device_id ti_id_table_combined[14+2*TI_EXTRA_VID_PID_COUNT+1] = {
201 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
202 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
203 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
204 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
205 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
206 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
207 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
208 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
209 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
210 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
211 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
212 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
213 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
214 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
218 static struct usb_driver ti_usb_driver = {
219 .name = "ti_usb_3410_5052",
220 .probe = usb_serial_probe,
221 .disconnect = usb_serial_disconnect,
222 .id_table = ti_id_table_combined,
226 static struct usb_serial_driver ti_1port_device = {
228 .owner = THIS_MODULE,
229 .name = "ti_usb_3410_5052_1",
231 .description = "TI USB 3410 1 port adapter",
232 .usb_driver = &ti_usb_driver,
233 .id_table = ti_id_table_3410,
235 .attach = ti_startup,
236 .shutdown = ti_shutdown,
240 .write_room = ti_write_room,
241 .chars_in_buffer = ti_chars_in_buffer,
242 .throttle = ti_throttle,
243 .unthrottle = ti_unthrottle,
245 .set_termios = ti_set_termios,
246 .tiocmget = ti_tiocmget,
247 .tiocmset = ti_tiocmset,
248 .break_ctl = ti_break,
249 .read_int_callback = ti_interrupt_callback,
250 .read_bulk_callback = ti_bulk_in_callback,
251 .write_bulk_callback = ti_bulk_out_callback,
254 static struct usb_serial_driver ti_2port_device = {
256 .owner = THIS_MODULE,
257 .name = "ti_usb_3410_5052_2",
259 .description = "TI USB 5052 2 port adapter",
260 .usb_driver = &ti_usb_driver,
261 .id_table = ti_id_table_5052,
263 .attach = ti_startup,
264 .shutdown = ti_shutdown,
268 .write_room = ti_write_room,
269 .chars_in_buffer = ti_chars_in_buffer,
270 .throttle = ti_throttle,
271 .unthrottle = ti_unthrottle,
273 .set_termios = ti_set_termios,
274 .tiocmget = ti_tiocmget,
275 .tiocmset = ti_tiocmset,
276 .break_ctl = ti_break,
277 .read_int_callback = ti_interrupt_callback,
278 .read_bulk_callback = ti_bulk_in_callback,
279 .write_bulk_callback = ti_bulk_out_callback,
285 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
286 MODULE_DESCRIPTION(TI_DRIVER_DESC);
287 MODULE_VERSION(TI_DRIVER_VERSION);
288 MODULE_LICENSE("GPL");
290 MODULE_FIRMWARE("ti_3410.fw");
291 MODULE_FIRMWARE("ti_5052.fw");
292 MODULE_FIRMWARE("mts_cdma.fw");
293 MODULE_FIRMWARE("mts_gsm.fw");
294 MODULE_FIRMWARE("mts_edge.fw");
296 module_param(debug, bool, S_IRUGO | S_IWUSR);
297 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
299 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
300 MODULE_PARM_DESC(low_latency,
301 "TTY low_latency flag, 0=off, 1=on, default is off");
303 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
304 MODULE_PARM_DESC(closing_wait,
305 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
307 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
308 MODULE_PARM_DESC(vendor_3410,
309 "Vendor ids for 3410 based devices, 1-5 short integers");
310 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
311 MODULE_PARM_DESC(product_3410,
312 "Product ids for 3410 based devices, 1-5 short integers");
313 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
314 MODULE_PARM_DESC(vendor_5052,
315 "Vendor ids for 5052 based devices, 1-5 short integers");
316 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
317 MODULE_PARM_DESC(product_5052,
318 "Product ids for 5052 based devices, 1-5 short integers");
320 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
325 static int __init ti_init(void)
330 /* insert extra vendor and product ids */
331 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
332 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
333 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
334 ti_id_table_3410[j].idVendor = vendor_3410[i];
335 ti_id_table_3410[j].idProduct = product_3410[i];
336 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
337 ti_id_table_combined[c].idVendor = vendor_3410[i];
338 ti_id_table_combined[c].idProduct = product_3410[i];
339 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
341 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
342 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
343 ti_id_table_5052[j].idVendor = vendor_5052[i];
344 ti_id_table_5052[j].idProduct = product_5052[i];
345 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
346 ti_id_table_combined[c].idVendor = vendor_5052[i];
347 ti_id_table_combined[c].idProduct = product_5052[i];
348 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
351 ret = usb_serial_register(&ti_1port_device);
354 ret = usb_serial_register(&ti_2port_device);
358 ret = usb_register(&ti_usb_driver);
362 printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":"
363 TI_DRIVER_DESC "\n");
368 usb_serial_deregister(&ti_2port_device);
370 usb_serial_deregister(&ti_1port_device);
376 static void __exit ti_exit(void)
378 usb_serial_deregister(&ti_1port_device);
379 usb_serial_deregister(&ti_2port_device);
380 usb_deregister(&ti_usb_driver);
384 module_init(ti_init);
385 module_exit(ti_exit);
388 static int ti_startup(struct usb_serial *serial)
390 struct ti_device *tdev;
391 struct ti_port *tport;
392 struct usb_device *dev = serial->dev;
397 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
398 __func__, le16_to_cpu(dev->descriptor.idProduct),
399 dev->descriptor.bNumConfigurations,
400 dev->actconfig->desc.bConfigurationValue);
402 /* create device structure */
403 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
405 dev_err(&dev->dev, "%s - out of memory\n", __func__);
408 mutex_init(&tdev->td_open_close_lock);
409 tdev->td_serial = serial;
410 usb_set_serial_data(serial, tdev);
412 /* determine device type */
413 if (usb_match_id(serial->interface, ti_id_table_3410))
414 tdev->td_is_3410 = 1;
415 dbg("%s - device type is %s", __func__,
416 tdev->td_is_3410 ? "3410" : "5052");
418 /* if we have only 1 configuration, download firmware */
419 if (dev->descriptor.bNumConfigurations == 1) {
420 if ((status = ti_download_firmware(tdev)) != 0)
423 /* 3410 must be reset, 5052 resets itself */
424 if (tdev->td_is_3410) {
425 msleep_interruptible(100);
426 usb_reset_device(dev);
433 /* the second configuration must be set */
434 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
435 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
436 status = status ? status : -ENODEV;
440 /* set up port structures */
441 for (i = 0; i < serial->num_ports; ++i) {
442 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
444 dev_err(&dev->dev, "%s - out of memory\n", __func__);
448 spin_lock_init(&tport->tp_lock);
449 tport->tp_uart_base_addr = (i == 0 ?
450 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
451 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
452 tport->tp_closing_wait = closing_wait;
453 init_waitqueue_head(&tport->tp_msr_wait);
454 init_waitqueue_head(&tport->tp_write_wait);
455 tport->tp_write_buf = ti_buf_alloc();
456 if (tport->tp_write_buf == NULL) {
457 dev_err(&dev->dev, "%s - out of memory\n", __func__);
462 tport->tp_port = serial->port[i];
463 tport->tp_tdev = tdev;
464 usb_set_serial_port_data(serial->port[i], tport);
465 tport->tp_uart_mode = 0; /* default is RS232 */
471 for (--i; i >= 0; --i) {
472 tport = usb_get_serial_port_data(serial->port[i]);
473 ti_buf_free(tport->tp_write_buf);
475 usb_set_serial_port_data(serial->port[i], NULL);
479 usb_set_serial_data(serial, NULL);
484 static void ti_shutdown(struct usb_serial *serial)
487 struct ti_device *tdev = usb_get_serial_data(serial);
488 struct ti_port *tport;
492 for (i = 0; i < serial->num_ports; ++i) {
493 tport = usb_get_serial_port_data(serial->port[i]);
495 ti_buf_free(tport->tp_write_buf);
497 usb_set_serial_port_data(serial->port[i], NULL);
502 usb_set_serial_data(serial, NULL);
506 static int ti_open(struct tty_struct *tty,
507 struct usb_serial_port *port, struct file *file)
509 struct ti_port *tport = usb_get_serial_port_data(port);
510 struct ti_device *tdev;
511 struct usb_device *dev;
515 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
516 TI_PIPE_TIMEOUT_ENABLE |
517 (TI_TRANSFER_TIMEOUT << 2));
519 dbg("%s - port %d", __func__, port->number);
524 dev = port->serial->dev;
525 tdev = tport->tp_tdev;
527 /* only one open on any port on a device at a time */
528 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
533 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
535 port_number = port->number - port->serial->minor;
537 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
540 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
542 /* start interrupt urb the first time a port is opened on this device */
543 if (tdev->td_open_port_count == 0) {
544 dbg("%s - start interrupt in urb", __func__);
545 urb = tdev->td_serial->port[0]->interrupt_in_urb;
547 dev_err(&port->dev, "%s - no interrupt urb\n",
552 urb->complete = ti_interrupt_callback;
555 status = usb_submit_urb(urb, GFP_KERNEL);
558 "%s - submit interrupt urb failed, %d\n",
565 ti_set_termios(tty, port, tty->termios);
567 dbg("%s - sending TI_OPEN_PORT", __func__);
568 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
569 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
571 dev_err(&port->dev, "%s - cannot send open command, %d\n",
576 dbg("%s - sending TI_START_PORT", __func__);
577 status = ti_command_out_sync(tdev, TI_START_PORT,
578 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
580 dev_err(&port->dev, "%s - cannot send start command, %d\n",
585 dbg("%s - sending TI_PURGE_PORT", __func__);
586 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
587 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
589 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
593 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
594 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
596 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
601 /* reset the data toggle on the bulk endpoints to work around bug in
602 * host controllers where things get out of sync some times */
603 usb_clear_halt(dev, port->write_urb->pipe);
604 usb_clear_halt(dev, port->read_urb->pipe);
607 ti_set_termios(tty, port, tty->termios);
609 dbg("%s - sending TI_OPEN_PORT (2)", __func__);
610 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
611 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
613 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
618 dbg("%s - sending TI_START_PORT (2)", __func__);
619 status = ti_command_out_sync(tdev, TI_START_PORT,
620 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
622 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
628 dbg("%s - start read urb", __func__);
629 urb = port->read_urb;
631 dev_err(&port->dev, "%s - no read urb\n", __func__);
635 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
636 urb->complete = ti_bulk_in_callback;
637 urb->context = tport;
639 status = usb_submit_urb(urb, GFP_KERNEL);
641 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
646 tport->tp_is_open = 1;
647 ++tdev->td_open_port_count;
652 if (tdev->td_open_port_count == 0)
653 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
655 mutex_unlock(&tdev->td_open_close_lock);
656 dbg("%s - exit %d", __func__, status);
661 static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
664 struct ti_device *tdev;
665 struct ti_port *tport;
670 dbg("%s - port %d", __func__, port->number);
672 tdev = usb_get_serial_data(port->serial);
673 tport = usb_get_serial_port_data(port);
674 if (tdev == NULL || tport == NULL)
677 tport->tp_is_open = 0;
679 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
681 usb_kill_urb(port->read_urb);
682 usb_kill_urb(port->write_urb);
683 tport->tp_write_urb_in_use = 0;
685 port_number = port->number - port->serial->minor;
687 dbg("%s - sending TI_CLOSE_PORT", __func__);
688 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
689 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
692 "%s - cannot send close port command, %d\n"
695 /* if mutex_lock is interrupted, continue anyway */
696 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
697 --tport->tp_tdev->td_open_port_count;
698 if (tport->tp_tdev->td_open_port_count <= 0) {
699 /* last port is closed, shut down interrupt urb */
700 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
701 tport->tp_tdev->td_open_port_count = 0;
704 mutex_unlock(&tdev->td_open_close_lock);
706 dbg("%s - exit", __func__);
710 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
711 const unsigned char *data, int count)
713 struct ti_port *tport = usb_get_serial_port_data(port);
716 dbg("%s - port %d", __func__, port->number);
719 dbg("%s - write request of 0 bytes", __func__);
723 if (tport == NULL || !tport->tp_is_open)
726 spin_lock_irqsave(&tport->tp_lock, flags);
727 count = ti_buf_put(tport->tp_write_buf, data, count);
728 spin_unlock_irqrestore(&tport->tp_lock, flags);
736 static int ti_write_room(struct tty_struct *tty)
738 struct usb_serial_port *port = tty->driver_data;
739 struct ti_port *tport = usb_get_serial_port_data(port);
743 dbg("%s - port %d", __func__, port->number);
748 spin_lock_irqsave(&tport->tp_lock, flags);
749 room = ti_buf_space_avail(tport->tp_write_buf);
750 spin_unlock_irqrestore(&tport->tp_lock, flags);
752 dbg("%s - returns %d", __func__, room);
757 static int ti_chars_in_buffer(struct tty_struct *tty)
759 struct usb_serial_port *port = tty->driver_data;
760 struct ti_port *tport = usb_get_serial_port_data(port);
764 dbg("%s - port %d", __func__, port->number);
769 spin_lock_irqsave(&tport->tp_lock, flags);
770 chars = ti_buf_data_avail(tport->tp_write_buf);
771 spin_unlock_irqrestore(&tport->tp_lock, flags);
773 dbg("%s - returns %d", __func__, chars);
778 static void ti_throttle(struct tty_struct *tty)
780 struct usb_serial_port *port = tty->driver_data;
781 struct ti_port *tport = usb_get_serial_port_data(port);
783 dbg("%s - port %d", __func__, port->number);
788 if (I_IXOFF(tty) || C_CRTSCTS(tty))
789 ti_stop_read(tport, tty);
794 static void ti_unthrottle(struct tty_struct *tty)
796 struct usb_serial_port *port = tty->driver_data;
797 struct ti_port *tport = usb_get_serial_port_data(port);
800 dbg("%s - port %d", __func__, port->number);
805 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
806 status = ti_restart_read(tport, tty);
808 dev_err(&port->dev, "%s - cannot restart read, %d\n",
814 static int ti_ioctl(struct tty_struct *tty, struct file *file,
815 unsigned int cmd, unsigned long arg)
817 struct usb_serial_port *port = tty->driver_data;
818 struct ti_port *tport = usb_get_serial_port_data(port);
819 struct async_icount cnow;
820 struct async_icount cprev;
822 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
829 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
830 return ti_get_serial_info(tport,
831 (struct serial_struct __user *)arg);
833 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
834 return ti_set_serial_info(tty, tport,
835 (struct serial_struct __user *)arg);
837 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
838 cprev = tport->tp_icount;
840 interruptible_sleep_on(&tport->tp_msr_wait);
841 if (signal_pending(current))
843 cnow = tport->tp_icount;
844 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
845 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
846 return -EIO; /* no change => error */
847 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
848 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
849 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
850 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
856 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
857 __func__, port->number,
858 tport->tp_icount.rx, tport->tp_icount.tx);
859 if (copy_to_user((void __user *)arg, &tport->tp_icount,
860 sizeof(tport->tp_icount)))
868 static void ti_set_termios(struct tty_struct *tty,
869 struct usb_serial_port *port, struct ktermios *old_termios)
871 struct ti_port *tport = usb_get_serial_port_data(port);
872 struct ti_uart_config *config;
873 tcflag_t cflag, iflag;
876 int port_number = port->number - port->serial->minor;
879 dbg("%s - port %d", __func__, port->number);
881 cflag = tty->termios->c_cflag;
882 iflag = tty->termios->c_iflag;
884 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
885 dbg("%s - old clfag %08x, old iflag %08x", __func__,
886 old_termios->c_cflag, old_termios->c_iflag);
891 config = kmalloc(sizeof(*config), GFP_KERNEL);
893 dev_err(&port->dev, "%s - out of memory\n", __func__);
899 /* these flags must be set */
900 config->wFlags |= TI_UART_ENABLE_MS_INTS;
901 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
902 config->bUartMode = (__u8)(tport->tp_uart_mode);
904 switch (cflag & CSIZE) {
906 config->bDataBits = TI_UART_5_DATA_BITS;
909 config->bDataBits = TI_UART_6_DATA_BITS;
912 config->bDataBits = TI_UART_7_DATA_BITS;
916 config->bDataBits = TI_UART_8_DATA_BITS;
920 /* CMSPAR isn't supported by this driver */
921 tty->termios->c_cflag &= ~CMSPAR;
923 if (cflag & PARENB) {
924 if (cflag & PARODD) {
925 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
926 config->bParity = TI_UART_ODD_PARITY;
928 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
929 config->bParity = TI_UART_EVEN_PARITY;
932 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
933 config->bParity = TI_UART_NO_PARITY;
937 config->bStopBits = TI_UART_2_STOP_BITS;
939 config->bStopBits = TI_UART_1_STOP_BITS;
941 if (cflag & CRTSCTS) {
942 /* RTS flow control must be off to drop RTS for baud rate B0 */
943 if ((cflag & CBAUD) != B0)
944 config->wFlags |= TI_UART_ENABLE_RTS_IN;
945 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
948 ti_restart_read(tport, tty);
951 if (I_IXOFF(tty) || I_IXON(tty)) {
952 config->cXon = START_CHAR(tty);
953 config->cXoff = STOP_CHAR(tty);
956 config->wFlags |= TI_UART_ENABLE_X_IN;
958 ti_restart_read(tport, tty);
961 config->wFlags |= TI_UART_ENABLE_X_OUT;
964 baud = tty_get_baud_rate(tty);
967 if (tport->tp_tdev->td_is_3410)
968 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
970 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
972 /* FIXME: Should calculate resulting baud here and report it back */
973 if ((cflag & CBAUD) != B0)
974 tty_encode_baud_rate(tty, baud, baud);
976 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
977 __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
979 cpu_to_be16s(&config->wBaudRate);
980 cpu_to_be16s(&config->wFlags);
982 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
983 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
986 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
987 __func__, port_number, status);
989 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
990 mcr = tport->tp_shadow_mcr;
991 /* if baud rate is B0, clear RTS and DTR */
992 if ((cflag & CBAUD) == B0)
993 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
994 status = ti_set_mcr(tport, mcr);
997 "%s - cannot set modem control on port %d, %d\n",
998 __func__, port_number, status);
1004 static int ti_tiocmget(struct tty_struct *tty, struct file *file)
1006 struct usb_serial_port *port = tty->driver_data;
1007 struct ti_port *tport = usb_get_serial_port_data(port);
1008 unsigned int result;
1011 unsigned long flags;
1013 dbg("%s - port %d", __func__, port->number);
1018 spin_lock_irqsave(&tport->tp_lock, flags);
1019 msr = tport->tp_msr;
1020 mcr = tport->tp_shadow_mcr;
1021 spin_unlock_irqrestore(&tport->tp_lock, flags);
1023 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1024 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1025 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1026 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1027 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1028 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1029 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1031 dbg("%s - 0x%04X", __func__, result);
1037 static int ti_tiocmset(struct tty_struct *tty, struct file *file,
1038 unsigned int set, unsigned int clear)
1040 struct usb_serial_port *port = tty->driver_data;
1041 struct ti_port *tport = usb_get_serial_port_data(port);
1043 unsigned long flags;
1045 dbg("%s - port %d", __func__, port->number);
1050 spin_lock_irqsave(&tport->tp_lock, flags);
1051 mcr = tport->tp_shadow_mcr;
1053 if (set & TIOCM_RTS)
1055 if (set & TIOCM_DTR)
1057 if (set & TIOCM_LOOP)
1060 if (clear & TIOCM_RTS)
1062 if (clear & TIOCM_DTR)
1064 if (clear & TIOCM_LOOP)
1065 mcr &= ~TI_MCR_LOOP;
1066 spin_unlock_irqrestore(&tport->tp_lock, flags);
1068 return ti_set_mcr(tport, mcr);
1072 static void ti_break(struct tty_struct *tty, int break_state)
1074 struct usb_serial_port *port = tty->driver_data;
1075 struct ti_port *tport = usb_get_serial_port_data(port);
1078 dbg("%s - state = %d", __func__, break_state);
1083 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1085 status = ti_write_byte(tport->tp_tdev,
1086 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1087 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1090 dbg("%s - error setting break, %d", __func__, status);
1094 static void ti_interrupt_callback(struct urb *urb)
1096 struct ti_device *tdev = urb->context;
1097 struct usb_serial_port *port;
1098 struct usb_serial *serial = tdev->td_serial;
1099 struct ti_port *tport;
1100 struct device *dev = &urb->dev->dev;
1101 unsigned char *data = urb->transfer_buffer;
1102 int length = urb->actual_length;
1105 int status = urb->status;
1109 dbg("%s", __func__);
1117 dbg("%s - urb shutting down, %d", __func__, status);
1118 tdev->td_urb_error = 1;
1121 dev_err(dev, "%s - nonzero urb status, %d\n",
1123 tdev->td_urb_error = 1;
1128 dbg("%s - bad packet size, %d", __func__, length);
1132 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1133 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1137 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1138 function = TI_GET_FUNC_FROM_CODE(data[0]);
1140 dbg("%s - port_number %d, function %d, data 0x%02X",
1141 __func__, port_number, function, data[1]);
1143 if (port_number >= serial->num_ports) {
1144 dev_err(dev, "%s - bad port number, %d\n",
1145 __func__, port_number);
1149 port = serial->port[port_number];
1151 tport = usb_get_serial_port_data(port);
1156 case TI_CODE_DATA_ERROR:
1157 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1158 __func__, port_number, data[1]);
1161 case TI_CODE_MODEM_STATUS:
1163 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
1164 ti_handle_new_msr(tport, msr);
1168 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1174 retval = usb_submit_urb(urb, GFP_ATOMIC);
1176 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1181 static void ti_bulk_in_callback(struct urb *urb)
1183 struct ti_port *tport = urb->context;
1184 struct usb_serial_port *port = tport->tp_port;
1185 struct device *dev = &urb->dev->dev;
1186 int status = urb->status;
1188 struct tty_struct *tty;
1190 dbg("%s", __func__);
1198 dbg("%s - urb shutting down, %d", __func__, status);
1199 tport->tp_tdev->td_urb_error = 1;
1200 wake_up_interruptible(&tport->tp_write_wait);
1203 dev_err(dev, "%s - nonzero urb status, %d\n",
1205 tport->tp_tdev->td_urb_error = 1;
1206 wake_up_interruptible(&tport->tp_write_wait);
1209 if (status == -EPIPE)
1213 dev_err(dev, "%s - stopping read!\n", __func__);
1217 tty = tty_port_tty_get(&port->port);
1218 if (tty && urb->actual_length) {
1219 usb_serial_debug_data(debug, dev, __func__,
1220 urb->actual_length, urb->transfer_buffer);
1222 if (!tport->tp_is_open)
1223 dbg("%s - port closed, dropping data", __func__);
1225 ti_recv(&urb->dev->dev, tty,
1226 urb->transfer_buffer,
1227 urb->actual_length);
1229 spin_lock(&tport->tp_lock);
1230 tport->tp_icount.rx += urb->actual_length;
1231 spin_unlock(&tport->tp_lock);
1236 /* continue to read unless stopping */
1237 spin_lock(&tport->tp_lock);
1238 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1239 urb->dev = port->serial->dev;
1240 retval = usb_submit_urb(urb, GFP_ATOMIC);
1241 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1242 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1244 spin_unlock(&tport->tp_lock);
1246 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1251 static void ti_bulk_out_callback(struct urb *urb)
1253 struct ti_port *tport = urb->context;
1254 struct usb_serial_port *port = tport->tp_port;
1255 struct device *dev = &urb->dev->dev;
1256 int status = urb->status;
1258 dbg("%s - port %d", __func__, port->number);
1260 tport->tp_write_urb_in_use = 0;
1268 dbg("%s - urb shutting down, %d", __func__, status);
1269 tport->tp_tdev->td_urb_error = 1;
1270 wake_up_interruptible(&tport->tp_write_wait);
1273 dev_err(dev, "%s - nonzero urb status, %d\n",
1275 tport->tp_tdev->td_urb_error = 1;
1276 wake_up_interruptible(&tport->tp_write_wait);
1279 /* send any buffered data */
1284 static void ti_recv(struct device *dev, struct tty_struct *tty,
1285 unsigned char *data, int length)
1290 cnt = tty_buffer_request_room(tty, length);
1292 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1293 __func__, length - cnt);
1297 tty_insert_flip_string(tty, data, cnt);
1298 tty_flip_buffer_push(tty);
1301 } while (length > 0);
1306 static void ti_send(struct ti_port *tport)
1309 struct usb_serial_port *port = tport->tp_port;
1310 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1311 unsigned long flags;
1314 dbg("%s - port %d", __func__, port->number);
1316 spin_lock_irqsave(&tport->tp_lock, flags);
1318 if (tport->tp_write_urb_in_use)
1321 count = ti_buf_get(tport->tp_write_buf,
1322 port->write_urb->transfer_buffer,
1323 port->bulk_out_size);
1328 tport->tp_write_urb_in_use = 1;
1330 spin_unlock_irqrestore(&tport->tp_lock, flags);
1332 usb_serial_debug_data(debug, &port->dev, __func__, count,
1333 port->write_urb->transfer_buffer);
1335 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1336 usb_sndbulkpipe(port->serial->dev,
1337 port->bulk_out_endpointAddress),
1338 port->write_urb->transfer_buffer, count,
1339 ti_bulk_out_callback, tport);
1341 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1343 dev_err(&port->dev, "%s - submit write urb failed, %d\n",
1345 tport->tp_write_urb_in_use = 0;
1346 /* TODO: reschedule ti_send */
1348 spin_lock_irqsave(&tport->tp_lock, flags);
1349 tport->tp_icount.tx += count;
1350 spin_unlock_irqrestore(&tport->tp_lock, flags);
1353 /* more room in the buffer for new writes, wakeup */
1357 wake_up_interruptible(&tport->tp_write_wait);
1360 spin_unlock_irqrestore(&tport->tp_lock, flags);
1366 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1368 unsigned long flags;
1371 status = ti_write_byte(tport->tp_tdev,
1372 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1373 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1375 spin_lock_irqsave(&tport->tp_lock, flags);
1377 tport->tp_shadow_mcr = mcr;
1378 spin_unlock_irqrestore(&tport->tp_lock, flags);
1384 static int ti_get_lsr(struct ti_port *tport)
1387 struct ti_device *tdev = tport->tp_tdev;
1388 struct usb_serial_port *port = tport->tp_port;
1389 int port_number = port->number - port->serial->minor;
1390 struct ti_port_status *data;
1392 dbg("%s - port %d", __func__, port->number);
1394 size = sizeof(struct ti_port_status);
1395 data = kmalloc(size, GFP_KERNEL);
1397 dev_err(&port->dev, "%s - out of memory\n", __func__);
1401 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1402 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1405 "%s - get port status command failed, %d\n",
1410 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1412 tport->tp_lsr = data->bLSR;
1420 static int ti_get_serial_info(struct ti_port *tport,
1421 struct serial_struct __user *ret_arg)
1423 struct usb_serial_port *port = tport->tp_port;
1424 struct serial_struct ret_serial;
1429 memset(&ret_serial, 0, sizeof(ret_serial));
1431 ret_serial.type = PORT_16550A;
1432 ret_serial.line = port->serial->minor;
1433 ret_serial.port = port->number - port->serial->minor;
1434 ret_serial.flags = tport->tp_flags;
1435 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1436 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1437 ret_serial.closing_wait = tport->tp_closing_wait;
1439 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1446 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1447 struct serial_struct __user *new_arg)
1449 struct serial_struct new_serial;
1451 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1454 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1455 tty->low_latency = (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1456 tport->tp_closing_wait = new_serial.closing_wait;
1462 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1464 struct async_icount *icount;
1465 struct tty_struct *tty;
1466 unsigned long flags;
1468 dbg("%s - msr 0x%02X", __func__, msr);
1470 if (msr & TI_MSR_DELTA_MASK) {
1471 spin_lock_irqsave(&tport->tp_lock, flags);
1472 icount = &tport->tp_icount;
1473 if (msr & TI_MSR_DELTA_CTS)
1475 if (msr & TI_MSR_DELTA_DSR)
1477 if (msr & TI_MSR_DELTA_CD)
1479 if (msr & TI_MSR_DELTA_RI)
1481 wake_up_interruptible(&tport->tp_msr_wait);
1482 spin_unlock_irqrestore(&tport->tp_lock, flags);
1485 tport->tp_msr = msr & TI_MSR_MASK;
1487 /* handle CTS flow control */
1488 tty = tty_port_tty_get(&tport->tp_port->port);
1489 if (tty && C_CRTSCTS(tty)) {
1490 if (msr & TI_MSR_CTS) {
1491 tty->hw_stopped = 0;
1494 tty->hw_stopped = 1;
1501 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1503 struct ti_device *tdev = tport->tp_tdev;
1504 struct usb_serial_port *port = tport->tp_port;
1507 dbg("%s - port %d", __func__, port->number);
1509 spin_lock_irq(&tport->tp_lock);
1511 /* wait for data to drain from the buffer */
1512 tdev->td_urb_error = 0;
1513 init_waitqueue_entry(&wait, current);
1514 add_wait_queue(&tport->tp_write_wait, &wait);
1516 set_current_state(TASK_INTERRUPTIBLE);
1517 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1518 || timeout == 0 || signal_pending(current)
1519 || tdev->td_urb_error
1520 || port->serial->disconnected) /* disconnect */
1522 spin_unlock_irq(&tport->tp_lock);
1523 timeout = schedule_timeout(timeout);
1524 spin_lock_irq(&tport->tp_lock);
1526 set_current_state(TASK_RUNNING);
1527 remove_wait_queue(&tport->tp_write_wait, &wait);
1529 /* flush any remaining data in the buffer */
1531 ti_buf_clear(tport->tp_write_buf);
1533 spin_unlock_irq(&tport->tp_lock);
1535 mutex_lock(&port->serial->disc_mutex);
1536 /* wait for data to drain from the device */
1537 /* wait for empty tx register, plus 20 ms */
1539 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1540 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1541 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1542 && !port->serial->disconnected) {
1543 if (ti_get_lsr(tport))
1545 mutex_unlock(&port->serial->disc_mutex);
1546 msleep_interruptible(20);
1547 mutex_lock(&port->serial->disc_mutex);
1549 mutex_unlock(&port->serial->disc_mutex);
1553 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1555 unsigned long flags;
1557 spin_lock_irqsave(&tport->tp_lock, flags);
1559 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1560 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1562 spin_unlock_irqrestore(&tport->tp_lock, flags);
1566 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1570 unsigned long flags;
1572 spin_lock_irqsave(&tport->tp_lock, flags);
1574 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1575 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1576 urb = tport->tp_port->read_urb;
1577 spin_unlock_irqrestore(&tport->tp_lock, flags);
1578 urb->complete = ti_bulk_in_callback;
1579 urb->context = tport;
1580 urb->dev = tport->tp_port->serial->dev;
1581 status = usb_submit_urb(urb, GFP_KERNEL);
1583 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1584 spin_unlock_irqrestore(&tport->tp_lock, flags);
1591 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1592 __u16 moduleid, __u16 value, __u8 *data, int size)
1596 status = usb_control_msg(tdev->td_serial->dev,
1597 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1598 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1599 value, moduleid, data, size, 1000);
1611 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1612 __u16 moduleid, __u16 value, __u8 *data, int size)
1616 status = usb_control_msg(tdev->td_serial->dev,
1617 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1618 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1619 value, moduleid, data, size, 1000);
1631 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1632 __u8 mask, __u8 byte)
1636 struct ti_write_data_bytes *data;
1637 struct device *dev = &tdev->td_serial->dev->dev;
1639 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1640 __func__, addr, mask, byte);
1642 size = sizeof(struct ti_write_data_bytes) + 2;
1643 data = kmalloc(size, GFP_KERNEL);
1645 dev_err(dev, "%s - out of memory\n", __func__);
1649 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1650 data->bDataType = TI_RW_DATA_BYTE;
1651 data->bDataCounter = 1;
1652 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1653 data->wBaseAddrLo = cpu_to_be16(addr);
1654 data->bData[0] = mask;
1655 data->bData[1] = byte;
1657 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1658 (__u8 *)data, size);
1661 dev_err(dev, "%s - failed, %d\n", __func__, status);
1668 static int ti_do_download(struct usb_device *dev, int pipe,
1669 u8 *buffer, int size)
1674 struct ti_firmware_header *header;
1678 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1679 cs = (__u8)(cs + buffer[pos]);
1681 header = (struct ti_firmware_header *)buffer;
1682 header->wLength = cpu_to_le16((__u16)(size
1683 - sizeof(struct ti_firmware_header)));
1684 header->bCheckSum = cs;
1686 dbg("%s - downloading firmware", __func__);
1687 for (pos = 0; pos < size; pos += done) {
1688 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1689 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1697 static int ti_download_firmware(struct ti_device *tdev)
1702 struct usb_device *dev = tdev->td_serial->dev;
1703 unsigned int pipe = usb_sndbulkpipe(dev,
1704 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1705 const struct firmware *fw_p;
1708 /* try ID specific firmware first, then try generic firmware */
1709 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1710 dev->descriptor.idProduct);
1711 if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) {
1713 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1714 switch (dev->descriptor.idProduct) {
1715 case MTS_CDMA_PRODUCT_ID:
1716 strcpy(buf, "mts_cdma.fw");
1718 case MTS_GSM_PRODUCT_ID:
1719 strcpy(buf, "mts_gsm.fw");
1721 case MTS_EDGE_PRODUCT_ID:
1722 strcpy(buf, "mts_edge.fw");
1726 if (buf[0] == '\0') {
1727 if (tdev->td_is_3410)
1728 strcpy(buf, "ti_3410.fw");
1730 strcpy(buf, "ti_5052.fw");
1732 status = request_firmware(&fw_p, buf, &dev->dev);
1735 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1738 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1739 dev_err(&dev->dev, "%s - firmware too large\n", __func__);
1743 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1744 buffer = kmalloc(buffer_size, GFP_KERNEL);
1746 memcpy(buffer, fw_p->data, fw_p->size);
1747 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1748 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1753 release_firmware(fw_p);
1755 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1760 dbg("%s - download successful", __func__);
1766 /* Circular Buffer Functions */
1771 * Allocate a circular buffer and all associated memory.
1774 static struct circ_buf *ti_buf_alloc(void)
1776 struct circ_buf *cb;
1778 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
1782 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1783 if (cb->buf == NULL) {
1797 * Free the buffer and all associated memory.
1800 static void ti_buf_free(struct circ_buf *cb)
1810 * Clear out all data in the circular buffer.
1813 static void ti_buf_clear(struct circ_buf *cb)
1815 cb->head = cb->tail = 0;
1822 * Return the number of bytes of data available in the circular
1826 static int ti_buf_data_avail(struct circ_buf *cb)
1828 return CIRC_CNT(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1833 * ti_buf_space_avail
1835 * Return the number of bytes of space available in the circular
1839 static int ti_buf_space_avail(struct circ_buf *cb)
1841 return CIRC_SPACE(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1848 * Copy data data from a user buffer and put it into the circular buffer.
1849 * Restrict to the amount of space available.
1851 * Return the number of bytes copied.
1854 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1859 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1864 memcpy(cb->buf + cb->head, buf, c);
1865 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1878 * Get data from the circular buffer and copy to the given buffer.
1879 * Restrict to the amount of data available.
1881 * Return the number of bytes copied.
1884 static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1889 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1894 memcpy(buf, cb->buf + cb->tail, c);
1895 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);