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/kfifo.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.10"
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_CLOSING_WAIT 4000 /* in .01 secs */
55 /* supported setserial flags */
56 #define TI_SET_SERIAL_FLAGS 0
59 #define TI_READ_URB_RUNNING 0
60 #define TI_READ_URB_STOPPING 1
61 #define TI_READ_URB_STOPPED 2
63 #define TI_EXTRA_VID_PID_COUNT 5
73 __u8 tp_uart_mode; /* 232 or 485 modes */
74 unsigned int tp_uart_base_addr;
76 int tp_closing_wait;/* in .01 secs */
77 struct async_icount tp_icount;
78 wait_queue_head_t tp_write_wait;
79 struct ti_device *tp_tdev;
80 struct usb_serial_port *tp_port;
82 int tp_read_urb_state;
83 int tp_write_urb_in_use;
84 struct kfifo write_fifo;
88 struct mutex td_open_close_lock;
89 int td_open_port_count;
90 struct usb_serial *td_serial;
96 /* Function Declarations */
98 static int ti_startup(struct usb_serial *serial);
99 static void ti_release(struct usb_serial *serial);
100 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
101 static void ti_close(struct usb_serial_port *port);
102 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
103 const unsigned char *data, int count);
104 static int ti_write_room(struct tty_struct *tty);
105 static int ti_chars_in_buffer(struct tty_struct *tty);
106 static void ti_throttle(struct tty_struct *tty);
107 static void ti_unthrottle(struct tty_struct *tty);
108 static int ti_ioctl(struct tty_struct *tty,
109 unsigned int cmd, unsigned long arg);
110 static int ti_get_icount(struct tty_struct *tty,
111 struct serial_icounter_struct *icount);
112 static void ti_set_termios(struct tty_struct *tty,
113 struct usb_serial_port *port, struct ktermios *old_termios);
114 static int ti_tiocmget(struct tty_struct *tty);
115 static int ti_tiocmset(struct tty_struct *tty,
116 unsigned int set, unsigned int clear);
117 static void ti_break(struct tty_struct *tty, int break_state);
118 static void ti_interrupt_callback(struct urb *urb);
119 static void ti_bulk_in_callback(struct urb *urb);
120 static void ti_bulk_out_callback(struct urb *urb);
122 static void ti_recv(struct device *dev, struct tty_struct *tty,
123 unsigned char *data, int length);
124 static void ti_send(struct ti_port *tport);
125 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
126 static int ti_get_lsr(struct ti_port *tport);
127 static int ti_get_serial_info(struct ti_port *tport,
128 struct serial_struct __user *ret_arg);
129 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
130 struct serial_struct __user *new_arg);
131 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
133 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
135 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
136 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
138 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
139 __u16 moduleid, __u16 value, __u8 *data, int size);
140 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
141 __u16 moduleid, __u16 value, __u8 *data, int size);
143 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
144 __u8 mask, __u8 byte);
146 static int ti_download_firmware(struct ti_device *tdev);
151 /* module parameters */
153 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
154 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
155 static unsigned int vendor_3410_count;
156 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
157 static unsigned int product_3410_count;
158 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
159 static unsigned int vendor_5052_count;
160 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
161 static unsigned int product_5052_count;
163 /* supported devices */
164 /* the array dimension is the number of default entries plus */
165 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
167 static struct usb_device_id ti_id_table_3410[17+TI_EXTRA_VID_PID_COUNT+1] = {
168 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
169 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
170 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
171 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
172 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
173 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
174 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
175 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
176 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
177 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
178 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
179 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
180 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
181 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STEREO_PLUG_ID) },
182 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
183 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
184 { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
187 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
188 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
189 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
190 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
191 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
194 static struct usb_device_id ti_id_table_combined[21+2*TI_EXTRA_VID_PID_COUNT+1] = {
195 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
196 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
197 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
198 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
199 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
200 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
201 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
202 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
203 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
204 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
205 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
206 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
207 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
208 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
209 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
210 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
211 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
212 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
213 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
214 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
215 { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
219 static struct usb_driver ti_usb_driver = {
220 .name = "ti_usb_3410_5052",
221 .probe = usb_serial_probe,
222 .disconnect = usb_serial_disconnect,
223 .id_table = ti_id_table_combined,
227 static struct usb_serial_driver ti_1port_device = {
229 .owner = THIS_MODULE,
230 .name = "ti_usb_3410_5052_1",
232 .description = "TI USB 3410 1 port adapter",
233 .usb_driver = &ti_usb_driver,
234 .id_table = ti_id_table_3410,
236 .attach = ti_startup,
237 .release = ti_release,
241 .write_room = ti_write_room,
242 .chars_in_buffer = ti_chars_in_buffer,
243 .throttle = ti_throttle,
244 .unthrottle = ti_unthrottle,
246 .set_termios = ti_set_termios,
247 .tiocmget = ti_tiocmget,
248 .tiocmset = ti_tiocmset,
249 .get_icount = ti_get_icount,
250 .break_ctl = ti_break,
251 .read_int_callback = ti_interrupt_callback,
252 .read_bulk_callback = ti_bulk_in_callback,
253 .write_bulk_callback = ti_bulk_out_callback,
256 static struct usb_serial_driver ti_2port_device = {
258 .owner = THIS_MODULE,
259 .name = "ti_usb_3410_5052_2",
261 .description = "TI USB 5052 2 port adapter",
262 .usb_driver = &ti_usb_driver,
263 .id_table = ti_id_table_5052,
265 .attach = ti_startup,
266 .release = ti_release,
270 .write_room = ti_write_room,
271 .chars_in_buffer = ti_chars_in_buffer,
272 .throttle = ti_throttle,
273 .unthrottle = ti_unthrottle,
275 .set_termios = ti_set_termios,
276 .tiocmget = ti_tiocmget,
277 .tiocmset = ti_tiocmset,
278 .get_icount = ti_get_icount,
279 .break_ctl = ti_break,
280 .read_int_callback = ti_interrupt_callback,
281 .read_bulk_callback = ti_bulk_in_callback,
282 .write_bulk_callback = ti_bulk_out_callback,
288 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
289 MODULE_DESCRIPTION(TI_DRIVER_DESC);
290 MODULE_VERSION(TI_DRIVER_VERSION);
291 MODULE_LICENSE("GPL");
293 MODULE_FIRMWARE("ti_3410.fw");
294 MODULE_FIRMWARE("ti_5052.fw");
295 MODULE_FIRMWARE("mts_cdma.fw");
296 MODULE_FIRMWARE("mts_gsm.fw");
297 MODULE_FIRMWARE("mts_edge.fw");
298 MODULE_FIRMWARE("mts_mt9234mu.fw");
299 MODULE_FIRMWARE("mts_mt9234zba.fw");
301 module_param(debug, bool, S_IRUGO | S_IWUSR);
302 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
304 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
305 MODULE_PARM_DESC(closing_wait,
306 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
308 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
309 MODULE_PARM_DESC(vendor_3410,
310 "Vendor ids for 3410 based devices, 1-5 short integers");
311 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
312 MODULE_PARM_DESC(product_3410,
313 "Product ids for 3410 based devices, 1-5 short integers");
314 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
315 MODULE_PARM_DESC(vendor_5052,
316 "Vendor ids for 5052 based devices, 1-5 short integers");
317 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
318 MODULE_PARM_DESC(product_5052,
319 "Product ids for 5052 based devices, 1-5 short integers");
321 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
326 static int __init ti_init(void)
331 /* insert extra vendor and product ids */
332 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
333 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
334 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
335 ti_id_table_3410[j].idVendor = vendor_3410[i];
336 ti_id_table_3410[j].idProduct = product_3410[i];
337 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
338 ti_id_table_combined[c].idVendor = vendor_3410[i];
339 ti_id_table_combined[c].idProduct = product_3410[i];
340 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
342 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
343 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
344 ti_id_table_5052[j].idVendor = vendor_5052[i];
345 ti_id_table_5052[j].idProduct = product_5052[i];
346 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
347 ti_id_table_combined[c].idVendor = vendor_5052[i];
348 ti_id_table_combined[c].idProduct = product_5052[i];
349 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
352 ret = usb_serial_register(&ti_1port_device);
355 ret = usb_serial_register(&ti_2port_device);
359 ret = usb_register(&ti_usb_driver);
363 printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":"
364 TI_DRIVER_DESC "\n");
369 usb_serial_deregister(&ti_2port_device);
371 usb_serial_deregister(&ti_1port_device);
377 static void __exit ti_exit(void)
379 usb_deregister(&ti_usb_driver);
380 usb_serial_deregister(&ti_1port_device);
381 usb_serial_deregister(&ti_2port_device);
385 module_init(ti_init);
386 module_exit(ti_exit);
389 static int ti_startup(struct usb_serial *serial)
391 struct ti_device *tdev;
392 struct ti_port *tport;
393 struct usb_device *dev = serial->dev;
398 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
399 __func__, le16_to_cpu(dev->descriptor.idProduct),
400 dev->descriptor.bNumConfigurations,
401 dev->actconfig->desc.bConfigurationValue);
403 /* create device structure */
404 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
406 dev_err(&dev->dev, "%s - out of memory\n", __func__);
409 mutex_init(&tdev->td_open_close_lock);
410 tdev->td_serial = serial;
411 usb_set_serial_data(serial, tdev);
413 /* determine device type */
414 if (serial->type == &ti_1port_device)
415 tdev->td_is_3410 = 1;
416 dbg("%s - device type is %s", __func__,
417 tdev->td_is_3410 ? "3410" : "5052");
419 /* if we have only 1 configuration, download firmware */
420 if (dev->descriptor.bNumConfigurations == 1) {
421 if ((status = ti_download_firmware(tdev)) != 0)
424 /* 3410 must be reset, 5052 resets itself */
425 if (tdev->td_is_3410) {
426 msleep_interruptible(100);
427 usb_reset_device(dev);
434 /* the second configuration must be set */
435 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
436 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
437 status = status ? status : -ENODEV;
441 if (serial->num_bulk_in < serial->num_ports ||
442 serial->num_bulk_out < serial->num_ports) {
443 dev_err(&serial->interface->dev, "missing endpoints\n");
448 /* set up port structures */
449 for (i = 0; i < serial->num_ports; ++i) {
450 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
452 dev_err(&dev->dev, "%s - out of memory\n", __func__);
456 spin_lock_init(&tport->tp_lock);
457 tport->tp_uart_base_addr = (i == 0 ?
458 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
459 tport->tp_closing_wait = closing_wait;
460 init_waitqueue_head(&tport->tp_write_wait);
461 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE,
463 dev_err(&dev->dev, "%s - out of memory\n", __func__);
468 tport->tp_port = serial->port[i];
469 tport->tp_tdev = tdev;
470 usb_set_serial_port_data(serial->port[i], tport);
471 tport->tp_uart_mode = 0; /* default is RS232 */
477 for (--i; i >= 0; --i) {
478 tport = usb_get_serial_port_data(serial->port[i]);
479 kfifo_free(&tport->write_fifo);
481 usb_set_serial_port_data(serial->port[i], NULL);
485 usb_set_serial_data(serial, NULL);
490 static void ti_release(struct usb_serial *serial)
493 struct ti_device *tdev = usb_get_serial_data(serial);
494 struct ti_port *tport;
498 for (i = 0; i < serial->num_ports; ++i) {
499 tport = usb_get_serial_port_data(serial->port[i]);
501 kfifo_free(&tport->write_fifo);
510 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
512 struct ti_port *tport = usb_get_serial_port_data(port);
513 struct ti_device *tdev;
514 struct usb_device *dev;
518 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
519 TI_PIPE_TIMEOUT_ENABLE |
520 (TI_TRANSFER_TIMEOUT << 2));
522 dbg("%s - port %d", __func__, port->number);
527 dev = port->serial->dev;
528 tdev = tport->tp_tdev;
530 /* only one open on any port on a device at a time */
531 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
534 port_number = port->number - port->serial->minor;
536 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
539 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
541 /* start interrupt urb the first time a port is opened on this device */
542 if (tdev->td_open_port_count == 0) {
543 dbg("%s - start interrupt in urb", __func__);
544 urb = tdev->td_serial->port[0]->interrupt_in_urb;
546 dev_err(&port->dev, "%s - no interrupt urb\n",
551 urb->complete = ti_interrupt_callback;
554 status = usb_submit_urb(urb, GFP_KERNEL);
557 "%s - submit interrupt urb failed, %d\n",
564 ti_set_termios(tty, port, tty->termios);
566 dbg("%s - sending TI_OPEN_PORT", __func__);
567 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
568 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
570 dev_err(&port->dev, "%s - cannot send open command, %d\n",
575 dbg("%s - sending TI_START_PORT", __func__);
576 status = ti_command_out_sync(tdev, TI_START_PORT,
577 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
579 dev_err(&port->dev, "%s - cannot send start command, %d\n",
584 dbg("%s - sending TI_PURGE_PORT", __func__);
585 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
586 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
588 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
592 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
593 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
595 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
600 /* reset the data toggle on the bulk endpoints to work around bug in
601 * host controllers where things get out of sync some times */
602 usb_clear_halt(dev, port->write_urb->pipe);
603 usb_clear_halt(dev, port->read_urb->pipe);
606 ti_set_termios(tty, port, tty->termios);
608 dbg("%s - sending TI_OPEN_PORT (2)", __func__);
609 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
610 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
612 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
617 dbg("%s - sending TI_START_PORT (2)", __func__);
618 status = ti_command_out_sync(tdev, TI_START_PORT,
619 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
621 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
627 dbg("%s - start read urb", __func__);
628 urb = port->read_urb;
630 dev_err(&port->dev, "%s - no read urb\n", __func__);
634 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
635 urb->complete = ti_bulk_in_callback;
636 urb->context = tport;
638 status = usb_submit_urb(urb, GFP_KERNEL);
640 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
645 tport->tp_is_open = 1;
646 ++tdev->td_open_port_count;
651 if (tdev->td_open_port_count == 0)
652 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
654 mutex_unlock(&tdev->td_open_close_lock);
655 dbg("%s - exit %d", __func__, status);
660 static void ti_close(struct usb_serial_port *port)
662 struct ti_device *tdev;
663 struct ti_port *tport;
668 dbg("%s - port %d", __func__, port->number);
670 tdev = usb_get_serial_data(port->serial);
671 tport = usb_get_serial_port_data(port);
672 if (tdev == NULL || tport == NULL)
675 tport->tp_is_open = 0;
677 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
679 usb_kill_urb(port->read_urb);
680 usb_kill_urb(port->write_urb);
681 tport->tp_write_urb_in_use = 0;
683 port_number = port->number - port->serial->minor;
685 dbg("%s - sending TI_CLOSE_PORT", __func__);
686 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
687 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
690 "%s - cannot send close port command, %d\n"
693 /* if mutex_lock is interrupted, continue anyway */
694 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
695 --tport->tp_tdev->td_open_port_count;
696 if (tport->tp_tdev->td_open_port_count <= 0) {
697 /* last port is closed, shut down interrupt urb */
698 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
699 tport->tp_tdev->td_open_port_count = 0;
702 mutex_unlock(&tdev->td_open_close_lock);
704 dbg("%s - exit", __func__);
708 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
709 const unsigned char *data, int count)
711 struct ti_port *tport = usb_get_serial_port_data(port);
713 dbg("%s - port %d", __func__, port->number);
716 dbg("%s - write request of 0 bytes", __func__);
720 if (tport == NULL || !tport->tp_is_open)
723 count = kfifo_in_locked(&tport->write_fifo, data, count,
731 static int ti_write_room(struct tty_struct *tty)
733 struct usb_serial_port *port = tty->driver_data;
734 struct ti_port *tport = usb_get_serial_port_data(port);
738 dbg("%s - port %d", __func__, port->number);
743 spin_lock_irqsave(&tport->tp_lock, flags);
744 room = kfifo_avail(&tport->write_fifo);
745 spin_unlock_irqrestore(&tport->tp_lock, flags);
747 dbg("%s - returns %d", __func__, room);
752 static int ti_chars_in_buffer(struct tty_struct *tty)
754 struct usb_serial_port *port = tty->driver_data;
755 struct ti_port *tport = usb_get_serial_port_data(port);
759 dbg("%s - port %d", __func__, port->number);
764 spin_lock_irqsave(&tport->tp_lock, flags);
765 chars = kfifo_len(&tport->write_fifo);
766 spin_unlock_irqrestore(&tport->tp_lock, flags);
768 dbg("%s - returns %d", __func__, chars);
773 static void ti_throttle(struct tty_struct *tty)
775 struct usb_serial_port *port = tty->driver_data;
776 struct ti_port *tport = usb_get_serial_port_data(port);
778 dbg("%s - port %d", __func__, port->number);
783 if (I_IXOFF(tty) || C_CRTSCTS(tty))
784 ti_stop_read(tport, tty);
789 static void ti_unthrottle(struct tty_struct *tty)
791 struct usb_serial_port *port = tty->driver_data;
792 struct ti_port *tport = usb_get_serial_port_data(port);
795 dbg("%s - port %d", __func__, port->number);
800 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
801 status = ti_restart_read(tport, tty);
803 dev_err(&port->dev, "%s - cannot restart read, %d\n",
808 static int ti_get_icount(struct tty_struct *tty,
809 struct serial_icounter_struct *icount)
811 struct usb_serial_port *port = tty->driver_data;
812 struct ti_port *tport = usb_get_serial_port_data(port);
813 struct async_icount cnow = tport->tp_icount;
815 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
816 __func__, port->number,
819 icount->cts = cnow.cts;
820 icount->dsr = cnow.dsr;
821 icount->rng = cnow.rng;
822 icount->dcd = cnow.dcd;
823 icount->rx = cnow.rx;
824 icount->tx = cnow.tx;
825 icount->frame = cnow.frame;
826 icount->overrun = cnow.overrun;
827 icount->parity = cnow.parity;
828 icount->brk = cnow.brk;
829 icount->buf_overrun = cnow.buf_overrun;
834 static int ti_ioctl(struct tty_struct *tty,
835 unsigned int cmd, unsigned long arg)
837 struct usb_serial_port *port = tty->driver_data;
838 struct ti_port *tport = usb_get_serial_port_data(port);
839 struct async_icount cnow;
840 struct async_icount cprev;
842 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
849 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
850 return ti_get_serial_info(tport,
851 (struct serial_struct __user *)arg);
853 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
854 return ti_set_serial_info(tty, tport,
855 (struct serial_struct __user *)arg);
857 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
858 cprev = tport->tp_icount;
860 interruptible_sleep_on(&port->delta_msr_wait);
861 if (signal_pending(current))
864 if (port->serial->disconnected)
867 cnow = tport->tp_icount;
868 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
869 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
870 return -EIO; /* no change => error */
871 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
872 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
873 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
874 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
884 static void ti_set_termios(struct tty_struct *tty,
885 struct usb_serial_port *port, struct ktermios *old_termios)
887 struct ti_port *tport = usb_get_serial_port_data(port);
888 struct ti_uart_config *config;
889 tcflag_t cflag, iflag;
892 int port_number = port->number - port->serial->minor;
895 dbg("%s - port %d", __func__, port->number);
897 cflag = tty->termios->c_cflag;
898 iflag = tty->termios->c_iflag;
900 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
901 dbg("%s - old clfag %08x, old iflag %08x", __func__,
902 old_termios->c_cflag, old_termios->c_iflag);
907 config = kmalloc(sizeof(*config), GFP_KERNEL);
909 dev_err(&port->dev, "%s - out of memory\n", __func__);
915 /* these flags must be set */
916 config->wFlags |= TI_UART_ENABLE_MS_INTS;
917 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
918 config->bUartMode = (__u8)(tport->tp_uart_mode);
920 switch (cflag & CSIZE) {
922 config->bDataBits = TI_UART_5_DATA_BITS;
925 config->bDataBits = TI_UART_6_DATA_BITS;
928 config->bDataBits = TI_UART_7_DATA_BITS;
932 config->bDataBits = TI_UART_8_DATA_BITS;
936 /* CMSPAR isn't supported by this driver */
937 tty->termios->c_cflag &= ~CMSPAR;
939 if (cflag & PARENB) {
940 if (cflag & PARODD) {
941 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
942 config->bParity = TI_UART_ODD_PARITY;
944 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
945 config->bParity = TI_UART_EVEN_PARITY;
948 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
949 config->bParity = TI_UART_NO_PARITY;
953 config->bStopBits = TI_UART_2_STOP_BITS;
955 config->bStopBits = TI_UART_1_STOP_BITS;
957 if (cflag & CRTSCTS) {
958 /* RTS flow control must be off to drop RTS for baud rate B0 */
959 if ((cflag & CBAUD) != B0)
960 config->wFlags |= TI_UART_ENABLE_RTS_IN;
961 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
964 ti_restart_read(tport, tty);
967 if (I_IXOFF(tty) || I_IXON(tty)) {
968 config->cXon = START_CHAR(tty);
969 config->cXoff = STOP_CHAR(tty);
972 config->wFlags |= TI_UART_ENABLE_X_IN;
974 ti_restart_read(tport, tty);
977 config->wFlags |= TI_UART_ENABLE_X_OUT;
980 baud = tty_get_baud_rate(tty);
983 if (tport->tp_tdev->td_is_3410)
984 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
986 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
988 /* FIXME: Should calculate resulting baud here and report it back */
989 if ((cflag & CBAUD) != B0)
990 tty_encode_baud_rate(tty, baud, baud);
992 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
993 __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
995 cpu_to_be16s(&config->wBaudRate);
996 cpu_to_be16s(&config->wFlags);
998 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
999 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
1002 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
1003 __func__, port_number, status);
1005 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
1006 mcr = tport->tp_shadow_mcr;
1007 /* if baud rate is B0, clear RTS and DTR */
1008 if ((cflag & CBAUD) == B0)
1009 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1010 status = ti_set_mcr(tport, mcr);
1013 "%s - cannot set modem control on port %d, %d\n",
1014 __func__, port_number, status);
1020 static int ti_tiocmget(struct tty_struct *tty)
1022 struct usb_serial_port *port = tty->driver_data;
1023 struct ti_port *tport = usb_get_serial_port_data(port);
1024 unsigned int result;
1027 unsigned long flags;
1029 dbg("%s - port %d", __func__, port->number);
1034 spin_lock_irqsave(&tport->tp_lock, flags);
1035 msr = tport->tp_msr;
1036 mcr = tport->tp_shadow_mcr;
1037 spin_unlock_irqrestore(&tport->tp_lock, flags);
1039 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1040 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1041 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1042 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1043 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1044 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1045 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1047 dbg("%s - 0x%04X", __func__, result);
1053 static int ti_tiocmset(struct tty_struct *tty,
1054 unsigned int set, unsigned int clear)
1056 struct usb_serial_port *port = tty->driver_data;
1057 struct ti_port *tport = usb_get_serial_port_data(port);
1059 unsigned long flags;
1061 dbg("%s - port %d", __func__, port->number);
1066 spin_lock_irqsave(&tport->tp_lock, flags);
1067 mcr = tport->tp_shadow_mcr;
1069 if (set & TIOCM_RTS)
1071 if (set & TIOCM_DTR)
1073 if (set & TIOCM_LOOP)
1076 if (clear & TIOCM_RTS)
1078 if (clear & TIOCM_DTR)
1080 if (clear & TIOCM_LOOP)
1081 mcr &= ~TI_MCR_LOOP;
1082 spin_unlock_irqrestore(&tport->tp_lock, flags);
1084 return ti_set_mcr(tport, mcr);
1088 static void ti_break(struct tty_struct *tty, int break_state)
1090 struct usb_serial_port *port = tty->driver_data;
1091 struct ti_port *tport = usb_get_serial_port_data(port);
1094 dbg("%s - state = %d", __func__, break_state);
1099 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1101 status = ti_write_byte(tport->tp_tdev,
1102 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1103 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1106 dbg("%s - error setting break, %d", __func__, status);
1110 static void ti_interrupt_callback(struct urb *urb)
1112 struct ti_device *tdev = urb->context;
1113 struct usb_serial_port *port;
1114 struct usb_serial *serial = tdev->td_serial;
1115 struct ti_port *tport;
1116 struct device *dev = &urb->dev->dev;
1117 unsigned char *data = urb->transfer_buffer;
1118 int length = urb->actual_length;
1121 int status = urb->status;
1125 dbg("%s", __func__);
1133 dbg("%s - urb shutting down, %d", __func__, status);
1134 tdev->td_urb_error = 1;
1137 dev_err(dev, "%s - nonzero urb status, %d\n",
1139 tdev->td_urb_error = 1;
1144 dbg("%s - bad packet size, %d", __func__, length);
1148 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1149 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1153 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1154 function = TI_GET_FUNC_FROM_CODE(data[0]);
1156 dbg("%s - port_number %d, function %d, data 0x%02X",
1157 __func__, port_number, function, data[1]);
1159 if (port_number >= serial->num_ports) {
1160 dev_err(dev, "%s - bad port number, %d\n",
1161 __func__, port_number);
1165 port = serial->port[port_number];
1167 tport = usb_get_serial_port_data(port);
1172 case TI_CODE_DATA_ERROR:
1173 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1174 __func__, port_number, data[1]);
1177 case TI_CODE_MODEM_STATUS:
1179 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
1180 ti_handle_new_msr(tport, msr);
1184 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1190 retval = usb_submit_urb(urb, GFP_ATOMIC);
1192 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1197 static void ti_bulk_in_callback(struct urb *urb)
1199 struct ti_port *tport = urb->context;
1200 struct usb_serial_port *port = tport->tp_port;
1201 struct device *dev = &urb->dev->dev;
1202 int status = urb->status;
1204 struct tty_struct *tty;
1206 dbg("%s", __func__);
1214 dbg("%s - urb shutting down, %d", __func__, status);
1215 tport->tp_tdev->td_urb_error = 1;
1216 wake_up_interruptible(&tport->tp_write_wait);
1219 dev_err(dev, "%s - nonzero urb status, %d\n",
1221 tport->tp_tdev->td_urb_error = 1;
1222 wake_up_interruptible(&tport->tp_write_wait);
1225 if (status == -EPIPE)
1229 dev_err(dev, "%s - stopping read!\n", __func__);
1233 tty = tty_port_tty_get(&port->port);
1235 if (urb->actual_length) {
1236 usb_serial_debug_data(debug, dev, __func__,
1237 urb->actual_length, urb->transfer_buffer);
1239 if (!tport->tp_is_open)
1240 dbg("%s - port closed, dropping data",
1243 ti_recv(&urb->dev->dev, tty,
1244 urb->transfer_buffer,
1245 urb->actual_length);
1246 spin_lock(&tport->tp_lock);
1247 tport->tp_icount.rx += urb->actual_length;
1248 spin_unlock(&tport->tp_lock);
1254 /* continue to read unless stopping */
1255 spin_lock(&tport->tp_lock);
1256 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1257 urb->dev = port->serial->dev;
1258 retval = usb_submit_urb(urb, GFP_ATOMIC);
1259 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1260 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1262 spin_unlock(&tport->tp_lock);
1264 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1269 static void ti_bulk_out_callback(struct urb *urb)
1271 struct ti_port *tport = urb->context;
1272 struct usb_serial_port *port = tport->tp_port;
1273 struct device *dev = &urb->dev->dev;
1274 int status = urb->status;
1276 dbg("%s - port %d", __func__, port->number);
1278 tport->tp_write_urb_in_use = 0;
1286 dbg("%s - urb shutting down, %d", __func__, status);
1287 tport->tp_tdev->td_urb_error = 1;
1288 wake_up_interruptible(&tport->tp_write_wait);
1291 dev_err(dev, "%s - nonzero urb status, %d\n",
1293 tport->tp_tdev->td_urb_error = 1;
1294 wake_up_interruptible(&tport->tp_write_wait);
1297 /* send any buffered data */
1302 static void ti_recv(struct device *dev, struct tty_struct *tty,
1303 unsigned char *data, int length)
1308 cnt = tty_insert_flip_string(tty, data, length);
1310 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1311 __func__, length - cnt);
1315 tty_flip_buffer_push(tty);
1318 } while (length > 0);
1323 static void ti_send(struct ti_port *tport)
1326 struct usb_serial_port *port = tport->tp_port;
1327 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1328 unsigned long flags;
1331 dbg("%s - port %d", __func__, port->number);
1333 spin_lock_irqsave(&tport->tp_lock, flags);
1335 if (tport->tp_write_urb_in_use)
1338 count = kfifo_out(&tport->write_fifo,
1339 port->write_urb->transfer_buffer,
1340 port->bulk_out_size);
1345 tport->tp_write_urb_in_use = 1;
1347 spin_unlock_irqrestore(&tport->tp_lock, flags);
1349 usb_serial_debug_data(debug, &port->dev, __func__, count,
1350 port->write_urb->transfer_buffer);
1352 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1353 usb_sndbulkpipe(port->serial->dev,
1354 port->bulk_out_endpointAddress),
1355 port->write_urb->transfer_buffer, count,
1356 ti_bulk_out_callback, tport);
1358 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1360 dev_err(&port->dev, "%s - submit write urb failed, %d\n",
1362 tport->tp_write_urb_in_use = 0;
1363 /* TODO: reschedule ti_send */
1365 spin_lock_irqsave(&tport->tp_lock, flags);
1366 tport->tp_icount.tx += count;
1367 spin_unlock_irqrestore(&tport->tp_lock, flags);
1370 /* more room in the buffer for new writes, wakeup */
1374 wake_up_interruptible(&tport->tp_write_wait);
1377 spin_unlock_irqrestore(&tport->tp_lock, flags);
1383 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1385 unsigned long flags;
1388 status = ti_write_byte(tport->tp_tdev,
1389 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1390 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1392 spin_lock_irqsave(&tport->tp_lock, flags);
1394 tport->tp_shadow_mcr = mcr;
1395 spin_unlock_irqrestore(&tport->tp_lock, flags);
1401 static int ti_get_lsr(struct ti_port *tport)
1404 struct ti_device *tdev = tport->tp_tdev;
1405 struct usb_serial_port *port = tport->tp_port;
1406 int port_number = port->number - port->serial->minor;
1407 struct ti_port_status *data;
1409 dbg("%s - port %d", __func__, port->number);
1411 size = sizeof(struct ti_port_status);
1412 data = kmalloc(size, GFP_KERNEL);
1414 dev_err(&port->dev, "%s - out of memory\n", __func__);
1418 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1419 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1422 "%s - get port status command failed, %d\n",
1427 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1429 tport->tp_lsr = data->bLSR;
1437 static int ti_get_serial_info(struct ti_port *tport,
1438 struct serial_struct __user *ret_arg)
1440 struct usb_serial_port *port = tport->tp_port;
1441 struct serial_struct ret_serial;
1446 memset(&ret_serial, 0, sizeof(ret_serial));
1448 ret_serial.type = PORT_16550A;
1449 ret_serial.line = port->serial->minor;
1450 ret_serial.port = port->number - port->serial->minor;
1451 ret_serial.flags = tport->tp_flags;
1452 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1453 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1454 ret_serial.closing_wait = tport->tp_closing_wait;
1456 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1463 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1464 struct serial_struct __user *new_arg)
1466 struct serial_struct new_serial;
1468 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1471 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1472 tport->tp_closing_wait = new_serial.closing_wait;
1478 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1480 struct async_icount *icount;
1481 struct tty_struct *tty;
1482 unsigned long flags;
1484 dbg("%s - msr 0x%02X", __func__, msr);
1486 if (msr & TI_MSR_DELTA_MASK) {
1487 spin_lock_irqsave(&tport->tp_lock, flags);
1488 icount = &tport->tp_icount;
1489 if (msr & TI_MSR_DELTA_CTS)
1491 if (msr & TI_MSR_DELTA_DSR)
1493 if (msr & TI_MSR_DELTA_CD)
1495 if (msr & TI_MSR_DELTA_RI)
1497 wake_up_interruptible(&tport->tp_port->delta_msr_wait);
1498 spin_unlock_irqrestore(&tport->tp_lock, flags);
1501 tport->tp_msr = msr & TI_MSR_MASK;
1503 /* handle CTS flow control */
1504 tty = tty_port_tty_get(&tport->tp_port->port);
1505 if (tty && C_CRTSCTS(tty)) {
1506 if (msr & TI_MSR_CTS) {
1507 tty->hw_stopped = 0;
1510 tty->hw_stopped = 1;
1517 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1519 struct ti_device *tdev = tport->tp_tdev;
1520 struct usb_serial_port *port = tport->tp_port;
1523 dbg("%s - port %d", __func__, port->number);
1525 spin_lock_irq(&tport->tp_lock);
1527 /* wait for data to drain from the buffer */
1528 tdev->td_urb_error = 0;
1529 init_waitqueue_entry(&wait, current);
1530 add_wait_queue(&tport->tp_write_wait, &wait);
1532 set_current_state(TASK_INTERRUPTIBLE);
1533 if (kfifo_len(&tport->write_fifo) == 0
1534 || timeout == 0 || signal_pending(current)
1535 || tdev->td_urb_error
1536 || port->serial->disconnected) /* disconnect */
1538 spin_unlock_irq(&tport->tp_lock);
1539 timeout = schedule_timeout(timeout);
1540 spin_lock_irq(&tport->tp_lock);
1542 set_current_state(TASK_RUNNING);
1543 remove_wait_queue(&tport->tp_write_wait, &wait);
1545 /* flush any remaining data in the buffer */
1547 kfifo_reset_out(&tport->write_fifo);
1549 spin_unlock_irq(&tport->tp_lock);
1551 mutex_lock(&port->serial->disc_mutex);
1552 /* wait for data to drain from the device */
1553 /* wait for empty tx register, plus 20 ms */
1555 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1556 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1557 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1558 && !port->serial->disconnected) {
1559 if (ti_get_lsr(tport))
1561 mutex_unlock(&port->serial->disc_mutex);
1562 msleep_interruptible(20);
1563 mutex_lock(&port->serial->disc_mutex);
1565 mutex_unlock(&port->serial->disc_mutex);
1569 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1571 unsigned long flags;
1573 spin_lock_irqsave(&tport->tp_lock, flags);
1575 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1576 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1578 spin_unlock_irqrestore(&tport->tp_lock, flags);
1582 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1586 unsigned long flags;
1588 spin_lock_irqsave(&tport->tp_lock, flags);
1590 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1591 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1592 urb = tport->tp_port->read_urb;
1593 spin_unlock_irqrestore(&tport->tp_lock, flags);
1594 urb->complete = ti_bulk_in_callback;
1595 urb->context = tport;
1596 urb->dev = tport->tp_port->serial->dev;
1597 status = usb_submit_urb(urb, GFP_KERNEL);
1599 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1600 spin_unlock_irqrestore(&tport->tp_lock, flags);
1607 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1608 __u16 moduleid, __u16 value, __u8 *data, int size)
1612 status = usb_control_msg(tdev->td_serial->dev,
1613 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1614 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1615 value, moduleid, data, size, 1000);
1627 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1628 __u16 moduleid, __u16 value, __u8 *data, int size)
1632 status = usb_control_msg(tdev->td_serial->dev,
1633 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1634 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1635 value, moduleid, data, size, 1000);
1647 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1648 __u8 mask, __u8 byte)
1652 struct ti_write_data_bytes *data;
1653 struct device *dev = &tdev->td_serial->dev->dev;
1655 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1656 __func__, addr, mask, byte);
1658 size = sizeof(struct ti_write_data_bytes) + 2;
1659 data = kmalloc(size, GFP_KERNEL);
1661 dev_err(dev, "%s - out of memory\n", __func__);
1665 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1666 data->bDataType = TI_RW_DATA_BYTE;
1667 data->bDataCounter = 1;
1668 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1669 data->wBaseAddrLo = cpu_to_be16(addr);
1670 data->bData[0] = mask;
1671 data->bData[1] = byte;
1673 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1674 (__u8 *)data, size);
1677 dev_err(dev, "%s - failed, %d\n", __func__, status);
1684 static int ti_do_download(struct usb_device *dev, int pipe,
1685 u8 *buffer, int size)
1690 struct ti_firmware_header *header;
1694 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1695 cs = (__u8)(cs + buffer[pos]);
1697 header = (struct ti_firmware_header *)buffer;
1698 header->wLength = cpu_to_le16((__u16)(size
1699 - sizeof(struct ti_firmware_header)));
1700 header->bCheckSum = cs;
1702 dbg("%s - downloading firmware", __func__);
1703 for (pos = 0; pos < size; pos += done) {
1704 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1705 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1713 static int ti_download_firmware(struct ti_device *tdev)
1718 struct usb_device *dev = tdev->td_serial->dev;
1719 unsigned int pipe = usb_sndbulkpipe(dev,
1720 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1721 const struct firmware *fw_p;
1724 dbg("%s\n", __func__);
1725 /* try ID specific firmware first, then try generic firmware */
1726 sprintf(buf, "ti_usb-v%04x-p%04x.fw",
1727 le16_to_cpu(dev->descriptor.idVendor),
1728 le16_to_cpu(dev->descriptor.idProduct));
1729 if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) {
1731 if (le16_to_cpu(dev->descriptor.idVendor) == MTS_VENDOR_ID) {
1732 switch (le16_to_cpu(dev->descriptor.idProduct)) {
1733 case MTS_CDMA_PRODUCT_ID:
1734 strcpy(buf, "mts_cdma.fw");
1736 case MTS_GSM_PRODUCT_ID:
1737 strcpy(buf, "mts_gsm.fw");
1739 case MTS_EDGE_PRODUCT_ID:
1740 strcpy(buf, "mts_edge.fw");
1742 case MTS_MT9234MU_PRODUCT_ID:
1743 strcpy(buf, "mts_mt9234mu.fw");
1745 case MTS_MT9234ZBA_PRODUCT_ID:
1746 strcpy(buf, "mts_mt9234zba.fw");
1748 case MTS_MT9234ZBAOLD_PRODUCT_ID:
1749 strcpy(buf, "mts_mt9234zba.fw");
1752 if (buf[0] == '\0') {
1753 if (tdev->td_is_3410)
1754 strcpy(buf, "ti_3410.fw");
1756 strcpy(buf, "ti_5052.fw");
1758 status = request_firmware(&fw_p, buf, &dev->dev);
1761 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1764 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1765 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1766 release_firmware(fw_p);
1770 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1771 buffer = kmalloc(buffer_size, GFP_KERNEL);
1773 memcpy(buffer, fw_p->data, fw_p->size);
1774 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1775 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1778 dbg("%s ENOMEM\n", __func__);
1781 release_firmware(fw_p);
1783 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1788 dbg("%s - download successful", __func__);