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_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
44 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
46 #define TI_FIRMWARE_BUF_SIZE 16284
48 #define TI_WRITE_BUF_SIZE 1024
50 #define TI_TRANSFER_TIMEOUT 2
52 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
54 /* supported setserial flags */
55 #define TI_SET_SERIAL_FLAGS 0
58 #define TI_READ_URB_RUNNING 0
59 #define TI_READ_URB_STOPPING 1
60 #define TI_READ_URB_STOPPED 2
62 #define TI_EXTRA_VID_PID_COUNT 5
71 __u8 tp_uart_mode; /* 232 or 485 modes */
72 unsigned int tp_uart_base_addr;
74 wait_queue_head_t tp_write_wait;
75 struct ti_device *tp_tdev;
76 struct usb_serial_port *tp_port;
78 int tp_read_urb_state;
79 int tp_write_urb_in_use;
80 struct kfifo write_fifo;
84 struct mutex td_open_close_lock;
85 int td_open_port_count;
86 struct usb_serial *td_serial;
92 /* Function Declarations */
94 static int ti_startup(struct usb_serial *serial);
95 static void ti_release(struct usb_serial *serial);
96 static int ti_port_probe(struct usb_serial_port *port);
97 static int ti_port_remove(struct usb_serial_port *port);
98 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
99 static void ti_close(struct usb_serial_port *port);
100 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
101 const unsigned char *data, int count);
102 static int ti_write_room(struct tty_struct *tty);
103 static int ti_chars_in_buffer(struct tty_struct *tty);
104 static bool ti_tx_empty(struct usb_serial_port *port);
105 static void ti_throttle(struct tty_struct *tty);
106 static void ti_unthrottle(struct tty_struct *tty);
107 static int ti_ioctl(struct tty_struct *tty,
108 unsigned int cmd, unsigned long arg);
109 static void ti_set_termios(struct tty_struct *tty,
110 struct usb_serial_port *port, struct ktermios *old_termios);
111 static int ti_tiocmget(struct tty_struct *tty);
112 static int ti_tiocmset(struct tty_struct *tty,
113 unsigned int set, unsigned int clear);
114 static void ti_break(struct tty_struct *tty, int break_state);
115 static void ti_interrupt_callback(struct urb *urb);
116 static void ti_bulk_in_callback(struct urb *urb);
117 static void ti_bulk_out_callback(struct urb *urb);
119 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
121 static void ti_send(struct ti_port *tport);
122 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
123 static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
124 static int ti_get_serial_info(struct ti_port *tport,
125 struct serial_struct __user *ret_arg);
126 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
127 struct serial_struct __user *new_arg);
128 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
130 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
131 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
133 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
134 __u16 moduleid, __u16 value, __u8 *data, int size);
135 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
136 __u16 moduleid, __u16 value, __u8 *data, int size);
138 static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
139 unsigned long addr, __u8 mask, __u8 byte);
141 static int ti_download_firmware(struct ti_device *tdev);
146 /* module parameters */
147 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
148 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
149 static unsigned int vendor_3410_count;
150 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
151 static unsigned int product_3410_count;
152 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
153 static unsigned int vendor_5052_count;
154 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
155 static unsigned int product_5052_count;
157 /* supported devices */
158 /* the array dimension is the number of default entries plus */
159 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
161 static struct usb_device_id ti_id_table_3410[15+TI_EXTRA_VID_PID_COUNT+1] = {
162 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
163 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
164 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
165 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
166 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
167 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
168 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
169 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
170 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
171 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
172 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
173 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
174 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
175 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
176 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
179 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
180 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
181 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
182 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
183 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
186 static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = {
187 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
188 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
189 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
190 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
191 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
192 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
193 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
194 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
195 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
196 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
197 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
198 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
199 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
200 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
201 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
202 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
203 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
204 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
205 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
209 static struct usb_serial_driver ti_1port_device = {
211 .owner = THIS_MODULE,
212 .name = "ti_usb_3410_5052_1",
214 .description = "TI USB 3410 1 port adapter",
215 .id_table = ti_id_table_3410,
217 .attach = ti_startup,
218 .release = ti_release,
219 .port_probe = ti_port_probe,
220 .port_remove = ti_port_remove,
224 .write_room = ti_write_room,
225 .chars_in_buffer = ti_chars_in_buffer,
226 .tx_empty = ti_tx_empty,
227 .throttle = ti_throttle,
228 .unthrottle = ti_unthrottle,
230 .set_termios = ti_set_termios,
231 .tiocmget = ti_tiocmget,
232 .tiocmset = ti_tiocmset,
233 .tiocmiwait = usb_serial_generic_tiocmiwait,
234 .get_icount = usb_serial_generic_get_icount,
235 .break_ctl = ti_break,
236 .read_int_callback = ti_interrupt_callback,
237 .read_bulk_callback = ti_bulk_in_callback,
238 .write_bulk_callback = ti_bulk_out_callback,
241 static struct usb_serial_driver ti_2port_device = {
243 .owner = THIS_MODULE,
244 .name = "ti_usb_3410_5052_2",
246 .description = "TI USB 5052 2 port adapter",
247 .id_table = ti_id_table_5052,
249 .attach = ti_startup,
250 .release = ti_release,
251 .port_probe = ti_port_probe,
252 .port_remove = ti_port_remove,
256 .write_room = ti_write_room,
257 .chars_in_buffer = ti_chars_in_buffer,
258 .tx_empty = ti_tx_empty,
259 .throttle = ti_throttle,
260 .unthrottle = ti_unthrottle,
262 .set_termios = ti_set_termios,
263 .tiocmget = ti_tiocmget,
264 .tiocmset = ti_tiocmset,
265 .tiocmiwait = usb_serial_generic_tiocmiwait,
266 .get_icount = usb_serial_generic_get_icount,
267 .break_ctl = ti_break,
268 .read_int_callback = ti_interrupt_callback,
269 .read_bulk_callback = ti_bulk_in_callback,
270 .write_bulk_callback = ti_bulk_out_callback,
273 static struct usb_serial_driver * const serial_drivers[] = {
274 &ti_1port_device, &ti_2port_device, NULL
279 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
280 MODULE_DESCRIPTION(TI_DRIVER_DESC);
281 MODULE_LICENSE("GPL");
283 MODULE_FIRMWARE("ti_3410.fw");
284 MODULE_FIRMWARE("ti_5052.fw");
285 MODULE_FIRMWARE("mts_cdma.fw");
286 MODULE_FIRMWARE("mts_gsm.fw");
287 MODULE_FIRMWARE("mts_edge.fw");
288 MODULE_FIRMWARE("mts_mt9234mu.fw");
289 MODULE_FIRMWARE("mts_mt9234zba.fw");
291 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
292 MODULE_PARM_DESC(closing_wait,
293 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
295 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
296 MODULE_PARM_DESC(vendor_3410,
297 "Vendor ids for 3410 based devices, 1-5 short integers");
298 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
299 MODULE_PARM_DESC(product_3410,
300 "Product ids for 3410 based devices, 1-5 short integers");
301 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
302 MODULE_PARM_DESC(vendor_5052,
303 "Vendor ids for 5052 based devices, 1-5 short integers");
304 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
305 MODULE_PARM_DESC(product_5052,
306 "Product ids for 5052 based devices, 1-5 short integers");
308 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
313 static int __init ti_init(void)
317 /* insert extra vendor and product ids */
318 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
319 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
320 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
321 ti_id_table_3410[j].idVendor = vendor_3410[i];
322 ti_id_table_3410[j].idProduct = product_3410[i];
323 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
324 ti_id_table_combined[c].idVendor = vendor_3410[i];
325 ti_id_table_combined[c].idProduct = product_3410[i];
326 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
328 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
329 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
330 ti_id_table_5052[j].idVendor = vendor_5052[i];
331 ti_id_table_5052[j].idProduct = product_5052[i];
332 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
333 ti_id_table_combined[c].idVendor = vendor_5052[i];
334 ti_id_table_combined[c].idProduct = product_5052[i];
335 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
338 return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ti_id_table_combined);
341 static void __exit ti_exit(void)
343 usb_serial_deregister_drivers(serial_drivers);
346 module_init(ti_init);
347 module_exit(ti_exit);
350 static int ti_startup(struct usb_serial *serial)
352 struct ti_device *tdev;
353 struct usb_device *dev = serial->dev;
357 "%s - product 0x%4X, num configurations %d, configuration value %d",
358 __func__, le16_to_cpu(dev->descriptor.idProduct),
359 dev->descriptor.bNumConfigurations,
360 dev->actconfig->desc.bConfigurationValue);
362 /* create device structure */
363 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
365 dev_err(&dev->dev, "%s - out of memory\n", __func__);
368 mutex_init(&tdev->td_open_close_lock);
369 tdev->td_serial = serial;
370 usb_set_serial_data(serial, tdev);
372 /* determine device type */
373 if (usb_match_id(serial->interface, ti_id_table_3410))
374 tdev->td_is_3410 = 1;
375 dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
376 tdev->td_is_3410 ? "3410" : "5052");
378 /* if we have only 1 configuration, download firmware */
379 if (dev->descriptor.bNumConfigurations == 1) {
380 status = ti_download_firmware(tdev);
385 /* 3410 must be reset, 5052 resets itself */
386 if (tdev->td_is_3410) {
387 msleep_interruptible(100);
388 usb_reset_device(dev);
395 /* the second configuration must be set */
396 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
397 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
398 status = status ? status : -ENODEV;
406 usb_set_serial_data(serial, NULL);
411 static void ti_release(struct usb_serial *serial)
413 struct ti_device *tdev = usb_get_serial_data(serial);
418 static int ti_port_probe(struct usb_serial_port *port)
420 struct ti_port *tport;
422 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
426 spin_lock_init(&tport->tp_lock);
427 if (port == port->serial->port[0])
428 tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
430 tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
431 port->port.closing_wait = msecs_to_jiffies(10 * closing_wait);
432 init_waitqueue_head(&tport->tp_write_wait);
433 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE, GFP_KERNEL)) {
437 tport->tp_port = port;
438 tport->tp_tdev = usb_get_serial_data(port->serial);
439 tport->tp_uart_mode = 0; /* default is RS232 */
441 usb_set_serial_port_data(port, tport);
446 static int ti_port_remove(struct usb_serial_port *port)
448 struct ti_port *tport;
450 tport = usb_get_serial_port_data(port);
451 kfifo_free(&tport->write_fifo);
457 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
459 struct ti_port *tport = usb_get_serial_port_data(port);
460 struct ti_device *tdev;
461 struct usb_device *dev;
465 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
466 TI_PIPE_TIMEOUT_ENABLE |
467 (TI_TRANSFER_TIMEOUT << 2));
472 dev = port->serial->dev;
473 tdev = tport->tp_tdev;
475 /* only one open on any port on a device at a time */
476 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
479 port_number = port->number - port->serial->minor;
482 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
484 /* start interrupt urb the first time a port is opened on this device */
485 if (tdev->td_open_port_count == 0) {
486 dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
487 urb = tdev->td_serial->port[0]->interrupt_in_urb;
489 dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
494 status = usb_submit_urb(urb, GFP_KERNEL);
496 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
502 ti_set_termios(tty, port, &tty->termios);
504 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__);
505 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
506 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
508 dev_err(&port->dev, "%s - cannot send open command, %d\n",
513 dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__);
514 status = ti_command_out_sync(tdev, TI_START_PORT,
515 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
517 dev_err(&port->dev, "%s - cannot send start command, %d\n",
522 dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__);
523 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
524 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
526 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
530 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
531 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
533 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
538 /* reset the data toggle on the bulk endpoints to work around bug in
539 * host controllers where things get out of sync some times */
540 usb_clear_halt(dev, port->write_urb->pipe);
541 usb_clear_halt(dev, port->read_urb->pipe);
544 ti_set_termios(tty, port, &tty->termios);
546 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __func__);
547 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
548 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
550 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
555 dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __func__);
556 status = ti_command_out_sync(tdev, TI_START_PORT,
557 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
559 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
565 dev_dbg(&port->dev, "%s - start read urb\n", __func__);
566 urb = port->read_urb;
568 dev_err(&port->dev, "%s - no read urb\n", __func__);
572 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
573 urb->context = tport;
574 status = usb_submit_urb(urb, GFP_KERNEL);
576 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
581 tport->tp_is_open = 1;
582 ++tdev->td_open_port_count;
584 port->port.drain_delay = 3;
589 if (tdev->td_open_port_count == 0)
590 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
592 mutex_unlock(&tdev->td_open_close_lock);
593 dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
598 static void ti_close(struct usb_serial_port *port)
600 struct ti_device *tdev;
601 struct ti_port *tport;
607 tdev = usb_get_serial_data(port->serial);
608 tport = usb_get_serial_port_data(port);
609 if (tdev == NULL || tport == NULL)
612 tport->tp_is_open = 0;
614 usb_kill_urb(port->read_urb);
615 usb_kill_urb(port->write_urb);
616 tport->tp_write_urb_in_use = 0;
617 spin_lock_irqsave(&tport->tp_lock, flags);
618 kfifo_reset_out(&tport->write_fifo);
619 spin_unlock_irqrestore(&tport->tp_lock, flags);
621 port_number = port->number - port->serial->minor;
623 dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
624 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
625 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
628 "%s - cannot send close port command, %d\n"
631 /* if mutex_lock is interrupted, continue anyway */
632 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
633 --tport->tp_tdev->td_open_port_count;
634 if (tport->tp_tdev->td_open_port_count <= 0) {
635 /* last port is closed, shut down interrupt urb */
636 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
637 tport->tp_tdev->td_open_port_count = 0;
640 mutex_unlock(&tdev->td_open_close_lock);
644 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
645 const unsigned char *data, int count)
647 struct ti_port *tport = usb_get_serial_port_data(port);
650 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
654 if (tport == NULL || !tport->tp_is_open)
657 count = kfifo_in_locked(&tport->write_fifo, data, count,
665 static int ti_write_room(struct tty_struct *tty)
667 struct usb_serial_port *port = tty->driver_data;
668 struct ti_port *tport = usb_get_serial_port_data(port);
675 spin_lock_irqsave(&tport->tp_lock, flags);
676 room = kfifo_avail(&tport->write_fifo);
677 spin_unlock_irqrestore(&tport->tp_lock, flags);
679 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
684 static int ti_chars_in_buffer(struct tty_struct *tty)
686 struct usb_serial_port *port = tty->driver_data;
687 struct ti_port *tport = usb_get_serial_port_data(port);
694 spin_lock_irqsave(&tport->tp_lock, flags);
695 chars = kfifo_len(&tport->write_fifo);
696 spin_unlock_irqrestore(&tport->tp_lock, flags);
698 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
702 static bool ti_tx_empty(struct usb_serial_port *port)
704 struct ti_port *tport = usb_get_serial_port_data(port);
708 ret = ti_get_lsr(tport, &lsr);
709 if (!ret && !(lsr & TI_LSR_TX_EMPTY))
715 static void ti_throttle(struct tty_struct *tty)
717 struct usb_serial_port *port = tty->driver_data;
718 struct ti_port *tport = usb_get_serial_port_data(port);
723 if (I_IXOFF(tty) || C_CRTSCTS(tty))
724 ti_stop_read(tport, tty);
729 static void ti_unthrottle(struct tty_struct *tty)
731 struct usb_serial_port *port = tty->driver_data;
732 struct ti_port *tport = usb_get_serial_port_data(port);
738 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
739 status = ti_restart_read(tport, tty);
741 dev_err(&port->dev, "%s - cannot restart read, %d\n",
746 static int ti_ioctl(struct tty_struct *tty,
747 unsigned int cmd, unsigned long arg)
749 struct usb_serial_port *port = tty->driver_data;
750 struct ti_port *tport = usb_get_serial_port_data(port);
752 dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd);
759 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
760 return ti_get_serial_info(tport,
761 (struct serial_struct __user *)arg);
763 dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
764 return ti_set_serial_info(tty, tport,
765 (struct serial_struct __user *)arg);
771 static void ti_set_termios(struct tty_struct *tty,
772 struct usb_serial_port *port, struct ktermios *old_termios)
774 struct ti_port *tport = usb_get_serial_port_data(port);
775 struct ti_uart_config *config;
776 tcflag_t cflag, iflag;
779 int port_number = port->number - port->serial->minor;
782 cflag = tty->termios.c_cflag;
783 iflag = tty->termios.c_iflag;
785 dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
786 dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
787 old_termios->c_cflag, old_termios->c_iflag);
792 config = kmalloc(sizeof(*config), GFP_KERNEL);
794 dev_err(&port->dev, "%s - out of memory\n", __func__);
800 /* these flags must be set */
801 config->wFlags |= TI_UART_ENABLE_MS_INTS;
802 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
803 config->bUartMode = (__u8)(tport->tp_uart_mode);
805 switch (cflag & CSIZE) {
807 config->bDataBits = TI_UART_5_DATA_BITS;
810 config->bDataBits = TI_UART_6_DATA_BITS;
813 config->bDataBits = TI_UART_7_DATA_BITS;
817 config->bDataBits = TI_UART_8_DATA_BITS;
821 /* CMSPAR isn't supported by this driver */
822 tty->termios.c_cflag &= ~CMSPAR;
824 if (cflag & PARENB) {
825 if (cflag & PARODD) {
826 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
827 config->bParity = TI_UART_ODD_PARITY;
829 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
830 config->bParity = TI_UART_EVEN_PARITY;
833 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
834 config->bParity = TI_UART_NO_PARITY;
838 config->bStopBits = TI_UART_2_STOP_BITS;
840 config->bStopBits = TI_UART_1_STOP_BITS;
842 if (cflag & CRTSCTS) {
843 /* RTS flow control must be off to drop RTS for baud rate B0 */
844 if ((cflag & CBAUD) != B0)
845 config->wFlags |= TI_UART_ENABLE_RTS_IN;
846 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
849 ti_restart_read(tport, tty);
852 if (I_IXOFF(tty) || I_IXON(tty)) {
853 config->cXon = START_CHAR(tty);
854 config->cXoff = STOP_CHAR(tty);
857 config->wFlags |= TI_UART_ENABLE_X_IN;
859 ti_restart_read(tport, tty);
862 config->wFlags |= TI_UART_ENABLE_X_OUT;
865 baud = tty_get_baud_rate(tty);
868 if (tport->tp_tdev->td_is_3410)
869 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
871 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
873 /* FIXME: Should calculate resulting baud here and report it back */
874 if ((cflag & CBAUD) != B0)
875 tty_encode_baud_rate(tty, baud, baud);
878 "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
879 __func__, baud, config->wBaudRate, config->wFlags,
880 config->bDataBits, config->bParity, config->bStopBits,
881 config->cXon, config->cXoff, config->bUartMode);
883 cpu_to_be16s(&config->wBaudRate);
884 cpu_to_be16s(&config->wFlags);
886 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
887 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
890 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
891 __func__, port_number, status);
893 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
894 mcr = tport->tp_shadow_mcr;
895 /* if baud rate is B0, clear RTS and DTR */
896 if ((cflag & CBAUD) == B0)
897 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
898 status = ti_set_mcr(tport, mcr);
901 "%s - cannot set modem control on port %d, %d\n",
902 __func__, port_number, status);
908 static int ti_tiocmget(struct tty_struct *tty)
910 struct usb_serial_port *port = tty->driver_data;
911 struct ti_port *tport = usb_get_serial_port_data(port);
920 spin_lock_irqsave(&tport->tp_lock, flags);
922 mcr = tport->tp_shadow_mcr;
923 spin_unlock_irqrestore(&tport->tp_lock, flags);
925 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
926 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
927 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
928 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
929 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
930 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
931 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
933 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
939 static int ti_tiocmset(struct tty_struct *tty,
940 unsigned int set, unsigned int clear)
942 struct usb_serial_port *port = tty->driver_data;
943 struct ti_port *tport = usb_get_serial_port_data(port);
950 spin_lock_irqsave(&tport->tp_lock, flags);
951 mcr = tport->tp_shadow_mcr;
957 if (set & TIOCM_LOOP)
960 if (clear & TIOCM_RTS)
962 if (clear & TIOCM_DTR)
964 if (clear & TIOCM_LOOP)
966 spin_unlock_irqrestore(&tport->tp_lock, flags);
968 return ti_set_mcr(tport, mcr);
972 static void ti_break(struct tty_struct *tty, int break_state)
974 struct usb_serial_port *port = tty->driver_data;
975 struct ti_port *tport = usb_get_serial_port_data(port);
978 dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
983 status = ti_write_byte(port, tport->tp_tdev,
984 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
985 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
988 dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
992 static void ti_interrupt_callback(struct urb *urb)
994 struct ti_device *tdev = urb->context;
995 struct usb_serial_port *port;
996 struct usb_serial *serial = tdev->td_serial;
997 struct ti_port *tport;
998 struct device *dev = &urb->dev->dev;
999 unsigned char *data = urb->transfer_buffer;
1000 int length = urb->actual_length;
1003 int status = urb->status;
1013 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1014 tdev->td_urb_error = 1;
1017 dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
1018 tdev->td_urb_error = 1;
1023 dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
1027 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1028 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1032 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1033 function = TI_GET_FUNC_FROM_CODE(data[0]);
1035 dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
1036 __func__, port_number, function, data[1]);
1038 if (port_number >= serial->num_ports) {
1039 dev_err(dev, "%s - bad port number, %d\n",
1040 __func__, port_number);
1044 port = serial->port[port_number];
1046 tport = usb_get_serial_port_data(port);
1051 case TI_CODE_DATA_ERROR:
1052 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1053 __func__, port_number, data[1]);
1056 case TI_CODE_MODEM_STATUS:
1058 dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
1059 ti_handle_new_msr(tport, msr);
1063 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1069 retval = usb_submit_urb(urb, GFP_ATOMIC);
1071 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1076 static void ti_bulk_in_callback(struct urb *urb)
1078 struct ti_port *tport = urb->context;
1079 struct usb_serial_port *port = tport->tp_port;
1080 struct device *dev = &urb->dev->dev;
1081 int status = urb->status;
1090 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1091 tport->tp_tdev->td_urb_error = 1;
1092 wake_up_interruptible(&tport->tp_write_wait);
1095 dev_err(dev, "%s - nonzero urb status, %d\n",
1097 tport->tp_tdev->td_urb_error = 1;
1098 wake_up_interruptible(&tport->tp_write_wait);
1101 if (status == -EPIPE)
1105 dev_err(dev, "%s - stopping read!\n", __func__);
1109 if (urb->actual_length) {
1110 usb_serial_debug_data(dev, __func__, urb->actual_length,
1111 urb->transfer_buffer);
1113 if (!tport->tp_is_open)
1114 dev_dbg(dev, "%s - port closed, dropping data\n",
1117 ti_recv(port, urb->transfer_buffer, urb->actual_length);
1118 spin_lock(&tport->tp_lock);
1119 port->icount.rx += urb->actual_length;
1120 spin_unlock(&tport->tp_lock);
1124 /* continue to read unless stopping */
1125 spin_lock(&tport->tp_lock);
1126 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1127 retval = usb_submit_urb(urb, GFP_ATOMIC);
1128 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
1129 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1131 spin_unlock(&tport->tp_lock);
1133 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1138 static void ti_bulk_out_callback(struct urb *urb)
1140 struct ti_port *tport = urb->context;
1141 struct usb_serial_port *port = tport->tp_port;
1142 int status = urb->status;
1144 tport->tp_write_urb_in_use = 0;
1152 dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
1153 tport->tp_tdev->td_urb_error = 1;
1154 wake_up_interruptible(&tport->tp_write_wait);
1157 dev_err_console(port, "%s - nonzero urb status, %d\n",
1159 tport->tp_tdev->td_urb_error = 1;
1160 wake_up_interruptible(&tport->tp_write_wait);
1163 /* send any buffered data */
1168 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
1174 cnt = tty_insert_flip_string(&port->port, data, length);
1176 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1177 __func__, length - cnt);
1181 tty_flip_buffer_push(&port->port);
1184 } while (length > 0);
1188 static void ti_send(struct ti_port *tport)
1191 struct usb_serial_port *port = tport->tp_port;
1192 unsigned long flags;
1194 spin_lock_irqsave(&tport->tp_lock, flags);
1196 if (tport->tp_write_urb_in_use)
1199 count = kfifo_out(&tport->write_fifo,
1200 port->write_urb->transfer_buffer,
1201 port->bulk_out_size);
1206 tport->tp_write_urb_in_use = 1;
1208 spin_unlock_irqrestore(&tport->tp_lock, flags);
1210 usb_serial_debug_data(&port->dev, __func__, count,
1211 port->write_urb->transfer_buffer);
1213 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1214 usb_sndbulkpipe(port->serial->dev,
1215 port->bulk_out_endpointAddress),
1216 port->write_urb->transfer_buffer, count,
1217 ti_bulk_out_callback, tport);
1219 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1221 dev_err_console(port, "%s - submit write urb failed, %d\n",
1223 tport->tp_write_urb_in_use = 0;
1224 /* TODO: reschedule ti_send */
1226 spin_lock_irqsave(&tport->tp_lock, flags);
1227 port->icount.tx += count;
1228 spin_unlock_irqrestore(&tport->tp_lock, flags);
1231 /* more room in the buffer for new writes, wakeup */
1232 tty_port_tty_wakeup(&port->port);
1234 wake_up_interruptible(&tport->tp_write_wait);
1237 spin_unlock_irqrestore(&tport->tp_lock, flags);
1242 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1244 unsigned long flags;
1247 status = ti_write_byte(tport->tp_port, tport->tp_tdev,
1248 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1249 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1251 spin_lock_irqsave(&tport->tp_lock, flags);
1253 tport->tp_shadow_mcr = mcr;
1254 spin_unlock_irqrestore(&tport->tp_lock, flags);
1260 static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
1263 struct ti_device *tdev = tport->tp_tdev;
1264 struct usb_serial_port *port = tport->tp_port;
1265 int port_number = port->number - port->serial->minor;
1266 struct ti_port_status *data;
1268 size = sizeof(struct ti_port_status);
1269 data = kmalloc(size, GFP_KERNEL);
1271 dev_err(&port->dev, "%s - out of memory\n", __func__);
1275 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1276 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1279 "%s - get port status command failed, %d\n",
1284 dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
1294 static int ti_get_serial_info(struct ti_port *tport,
1295 struct serial_struct __user *ret_arg)
1297 struct usb_serial_port *port = tport->tp_port;
1298 struct serial_struct ret_serial;
1304 cwait = port->port.closing_wait;
1305 if (cwait != ASYNC_CLOSING_WAIT_NONE)
1306 cwait = jiffies_to_msecs(cwait) / 10;
1308 memset(&ret_serial, 0, sizeof(ret_serial));
1310 ret_serial.type = PORT_16550A;
1311 ret_serial.line = port->serial->minor;
1312 ret_serial.port = port->number - port->serial->minor;
1313 ret_serial.flags = tport->tp_flags;
1314 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1315 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1316 ret_serial.closing_wait = cwait;
1318 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1325 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1326 struct serial_struct __user *new_arg)
1328 struct serial_struct new_serial;
1331 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1334 cwait = new_serial.closing_wait;
1335 if (cwait != ASYNC_CLOSING_WAIT_NONE)
1336 cwait = msecs_to_jiffies(10 * new_serial.closing_wait);
1338 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1339 tport->tp_port->port.closing_wait = cwait;
1345 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1347 struct async_icount *icount;
1348 struct tty_struct *tty;
1349 unsigned long flags;
1351 dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
1353 if (msr & TI_MSR_DELTA_MASK) {
1354 spin_lock_irqsave(&tport->tp_lock, flags);
1355 icount = &tport->tp_port->icount;
1356 if (msr & TI_MSR_DELTA_CTS)
1358 if (msr & TI_MSR_DELTA_DSR)
1360 if (msr & TI_MSR_DELTA_CD)
1362 if (msr & TI_MSR_DELTA_RI)
1364 wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
1365 spin_unlock_irqrestore(&tport->tp_lock, flags);
1368 tport->tp_msr = msr & TI_MSR_MASK;
1370 /* handle CTS flow control */
1371 tty = tty_port_tty_get(&tport->tp_port->port);
1372 if (tty && C_CRTSCTS(tty)) {
1373 if (msr & TI_MSR_CTS) {
1374 tty->hw_stopped = 0;
1377 tty->hw_stopped = 1;
1384 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1386 unsigned long flags;
1388 spin_lock_irqsave(&tport->tp_lock, flags);
1390 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1391 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1393 spin_unlock_irqrestore(&tport->tp_lock, flags);
1397 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1401 unsigned long flags;
1403 spin_lock_irqsave(&tport->tp_lock, flags);
1405 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1406 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1407 urb = tport->tp_port->read_urb;
1408 spin_unlock_irqrestore(&tport->tp_lock, flags);
1409 urb->context = tport;
1410 status = usb_submit_urb(urb, GFP_KERNEL);
1412 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1413 spin_unlock_irqrestore(&tport->tp_lock, flags);
1420 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1421 __u16 moduleid, __u16 value, __u8 *data, int size)
1425 status = usb_control_msg(tdev->td_serial->dev,
1426 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1427 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1428 value, moduleid, data, size, 1000);
1440 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1441 __u16 moduleid, __u16 value, __u8 *data, int size)
1445 status = usb_control_msg(tdev->td_serial->dev,
1446 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1447 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1448 value, moduleid, data, size, 1000);
1460 static int ti_write_byte(struct usb_serial_port *port,
1461 struct ti_device *tdev, unsigned long addr,
1462 __u8 mask, __u8 byte)
1466 struct ti_write_data_bytes *data;
1468 dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1471 size = sizeof(struct ti_write_data_bytes) + 2;
1472 data = kmalloc(size, GFP_KERNEL);
1474 dev_err(&port->dev, "%s - out of memory\n", __func__);
1478 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1479 data->bDataType = TI_RW_DATA_BYTE;
1480 data->bDataCounter = 1;
1481 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1482 data->wBaseAddrLo = cpu_to_be16(addr);
1483 data->bData[0] = mask;
1484 data->bData[1] = byte;
1486 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1487 (__u8 *)data, size);
1490 dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
1497 static int ti_do_download(struct usb_device *dev, int pipe,
1498 u8 *buffer, int size)
1503 struct ti_firmware_header *header;
1507 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1508 cs = (__u8)(cs + buffer[pos]);
1510 header = (struct ti_firmware_header *)buffer;
1511 header->wLength = cpu_to_le16((__u16)(size
1512 - sizeof(struct ti_firmware_header)));
1513 header->bCheckSum = cs;
1515 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
1516 for (pos = 0; pos < size; pos += done) {
1517 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1518 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1526 static int ti_download_firmware(struct ti_device *tdev)
1531 struct usb_device *dev = tdev->td_serial->dev;
1532 unsigned int pipe = usb_sndbulkpipe(dev,
1533 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1534 const struct firmware *fw_p;
1537 /* try ID specific firmware first, then try generic firmware */
1538 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1539 dev->descriptor.idProduct);
1540 status = request_firmware(&fw_p, buf, &dev->dev);
1544 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1545 switch (dev->descriptor.idProduct) {
1546 case MTS_CDMA_PRODUCT_ID:
1547 strcpy(buf, "mts_cdma.fw");
1549 case MTS_GSM_PRODUCT_ID:
1550 strcpy(buf, "mts_gsm.fw");
1552 case MTS_EDGE_PRODUCT_ID:
1553 strcpy(buf, "mts_edge.fw");
1555 case MTS_MT9234MU_PRODUCT_ID:
1556 strcpy(buf, "mts_mt9234mu.fw");
1558 case MTS_MT9234ZBA_PRODUCT_ID:
1559 strcpy(buf, "mts_mt9234zba.fw");
1561 case MTS_MT9234ZBAOLD_PRODUCT_ID:
1562 strcpy(buf, "mts_mt9234zba.fw");
1565 if (buf[0] == '\0') {
1566 if (tdev->td_is_3410)
1567 strcpy(buf, "ti_3410.fw");
1569 strcpy(buf, "ti_5052.fw");
1571 status = request_firmware(&fw_p, buf, &dev->dev);
1574 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1577 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1578 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1579 release_firmware(fw_p);
1583 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1584 buffer = kmalloc(buffer_size, GFP_KERNEL);
1586 memcpy(buffer, fw_p->data, fw_p->size);
1587 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1588 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1591 dev_dbg(&dev->dev, "%s ENOMEM\n", __func__);
1594 release_firmware(fw_p);
1596 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1601 dev_dbg(&dev->dev, "%s - download successful\n", __func__);