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_msr_wait; /* wait for msr change */
79 wait_queue_head_t tp_write_wait;
80 struct ti_device *tp_tdev;
81 struct usb_serial_port *tp_port;
83 int tp_read_urb_state;
84 int tp_write_urb_in_use;
85 struct kfifo write_fifo;
89 struct mutex td_open_close_lock;
90 int td_open_port_count;
91 struct usb_serial *td_serial;
97 /* Function Declarations */
99 static int ti_startup(struct usb_serial *serial);
100 static void ti_release(struct usb_serial *serial);
101 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
102 static void ti_close(struct usb_serial_port *port);
103 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
104 const unsigned char *data, int count);
105 static int ti_write_room(struct tty_struct *tty);
106 static int ti_chars_in_buffer(struct tty_struct *tty);
107 static void ti_throttle(struct tty_struct *tty);
108 static void ti_unthrottle(struct tty_struct *tty);
109 static int ti_ioctl(struct tty_struct *tty,
110 unsigned int cmd, unsigned long arg);
111 static int ti_get_icount(struct tty_struct *tty,
112 struct serial_icounter_struct *icount);
113 static void ti_set_termios(struct tty_struct *tty,
114 struct usb_serial_port *port, struct ktermios *old_termios);
115 static int ti_tiocmget(struct tty_struct *tty);
116 static int ti_tiocmset(struct tty_struct *tty,
117 unsigned int set, unsigned int clear);
118 static void ti_break(struct tty_struct *tty, int break_state);
119 static void ti_interrupt_callback(struct urb *urb);
120 static void ti_bulk_in_callback(struct urb *urb);
121 static void ti_bulk_out_callback(struct urb *urb);
123 static void ti_recv(struct device *dev, struct tty_struct *tty,
124 unsigned char *data, int length);
125 static void ti_send(struct ti_port *tport);
126 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
127 static int ti_get_lsr(struct ti_port *tport);
128 static int ti_get_serial_info(struct ti_port *tport,
129 struct serial_struct __user *ret_arg);
130 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
131 struct serial_struct __user *new_arg);
132 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
134 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
136 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
137 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
139 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
140 __u16 moduleid, __u16 value, __u8 *data, int size);
141 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
142 __u16 moduleid, __u16 value, __u8 *data, int size);
144 static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
145 unsigned long addr, __u8 mask, __u8 byte);
147 static int ti_download_firmware(struct ti_device *tdev);
152 /* 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[15+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_PRODUCT_ID) },
182 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
185 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
186 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
187 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
188 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
189 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
192 static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = {
193 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
194 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
195 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
196 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
197 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
198 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
199 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
200 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
201 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
202 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
203 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
204 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
205 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
206 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
207 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
208 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
209 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
210 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
211 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
215 static struct usb_serial_driver ti_1port_device = {
217 .owner = THIS_MODULE,
218 .name = "ti_usb_3410_5052_1",
220 .description = "TI USB 3410 1 port adapter",
221 .id_table = ti_id_table_3410,
223 .attach = ti_startup,
224 .release = ti_release,
228 .write_room = ti_write_room,
229 .chars_in_buffer = ti_chars_in_buffer,
230 .throttle = ti_throttle,
231 .unthrottle = ti_unthrottle,
233 .set_termios = ti_set_termios,
234 .tiocmget = ti_tiocmget,
235 .tiocmset = ti_tiocmset,
236 .get_icount = ti_get_icount,
237 .break_ctl = ti_break,
238 .read_int_callback = ti_interrupt_callback,
239 .read_bulk_callback = ti_bulk_in_callback,
240 .write_bulk_callback = ti_bulk_out_callback,
243 static struct usb_serial_driver ti_2port_device = {
245 .owner = THIS_MODULE,
246 .name = "ti_usb_3410_5052_2",
248 .description = "TI USB 5052 2 port adapter",
249 .id_table = ti_id_table_5052,
251 .attach = ti_startup,
252 .release = ti_release,
256 .write_room = ti_write_room,
257 .chars_in_buffer = ti_chars_in_buffer,
258 .throttle = ti_throttle,
259 .unthrottle = ti_unthrottle,
261 .set_termios = ti_set_termios,
262 .tiocmget = ti_tiocmget,
263 .tiocmset = ti_tiocmset,
264 .get_icount = ti_get_icount,
265 .break_ctl = ti_break,
266 .read_int_callback = ti_interrupt_callback,
267 .read_bulk_callback = ti_bulk_in_callback,
268 .write_bulk_callback = ti_bulk_out_callback,
271 static struct usb_serial_driver * const serial_drivers[] = {
272 &ti_1port_device, &ti_2port_device, NULL
277 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
278 MODULE_DESCRIPTION(TI_DRIVER_DESC);
279 MODULE_VERSION(TI_DRIVER_VERSION);
280 MODULE_LICENSE("GPL");
282 MODULE_FIRMWARE("ti_3410.fw");
283 MODULE_FIRMWARE("ti_5052.fw");
284 MODULE_FIRMWARE("mts_cdma.fw");
285 MODULE_FIRMWARE("mts_gsm.fw");
286 MODULE_FIRMWARE("mts_edge.fw");
287 MODULE_FIRMWARE("mts_mt9234mu.fw");
288 MODULE_FIRMWARE("mts_mt9234zba.fw");
290 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
291 MODULE_PARM_DESC(closing_wait,
292 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
294 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
295 MODULE_PARM_DESC(vendor_3410,
296 "Vendor ids for 3410 based devices, 1-5 short integers");
297 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
298 MODULE_PARM_DESC(product_3410,
299 "Product ids for 3410 based devices, 1-5 short integers");
300 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
301 MODULE_PARM_DESC(vendor_5052,
302 "Vendor ids for 5052 based devices, 1-5 short integers");
303 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
304 MODULE_PARM_DESC(product_5052,
305 "Product ids for 5052 based devices, 1-5 short integers");
307 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
312 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 ret = usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ti_id_table_combined);
340 printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":"
341 TI_DRIVER_DESC "\n");
345 static void __exit ti_exit(void)
347 usb_serial_deregister_drivers(serial_drivers);
350 module_init(ti_init);
351 module_exit(ti_exit);
354 static int ti_startup(struct usb_serial *serial)
356 struct ti_device *tdev;
357 struct ti_port *tport;
358 struct usb_device *dev = serial->dev;
364 "%s - product 0x%4X, num configurations %d, configuration value %d",
365 __func__, le16_to_cpu(dev->descriptor.idProduct),
366 dev->descriptor.bNumConfigurations,
367 dev->actconfig->desc.bConfigurationValue);
369 /* create device structure */
370 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
372 dev_err(&dev->dev, "%s - out of memory\n", __func__);
375 mutex_init(&tdev->td_open_close_lock);
376 tdev->td_serial = serial;
377 usb_set_serial_data(serial, tdev);
379 /* determine device type */
380 if (usb_match_id(serial->interface, ti_id_table_3410))
381 tdev->td_is_3410 = 1;
382 dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
383 tdev->td_is_3410 ? "3410" : "5052");
385 /* if we have only 1 configuration, download firmware */
386 if (dev->descriptor.bNumConfigurations == 1) {
387 status = ti_download_firmware(tdev);
392 /* 3410 must be reset, 5052 resets itself */
393 if (tdev->td_is_3410) {
394 msleep_interruptible(100);
395 usb_reset_device(dev);
402 /* the second configuration must be set */
403 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
404 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
405 status = status ? status : -ENODEV;
409 /* set up port structures */
410 for (i = 0; i < serial->num_ports; ++i) {
411 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
413 dev_err(&dev->dev, "%s - out of memory\n", __func__);
417 spin_lock_init(&tport->tp_lock);
418 tport->tp_uart_base_addr = (i == 0 ?
419 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
420 tport->tp_closing_wait = closing_wait;
421 init_waitqueue_head(&tport->tp_msr_wait);
422 init_waitqueue_head(&tport->tp_write_wait);
423 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE,
425 dev_err(&dev->dev, "%s - out of memory\n", __func__);
430 tport->tp_port = serial->port[i];
431 tport->tp_tdev = tdev;
432 usb_set_serial_port_data(serial->port[i], tport);
433 tport->tp_uart_mode = 0; /* default is RS232 */
439 for (--i; i >= 0; --i) {
440 tport = usb_get_serial_port_data(serial->port[i]);
441 kfifo_free(&tport->write_fifo);
443 usb_set_serial_port_data(serial->port[i], NULL);
447 usb_set_serial_data(serial, NULL);
452 static void ti_release(struct usb_serial *serial)
455 struct ti_device *tdev = usb_get_serial_data(serial);
456 struct ti_port *tport;
458 for (i = 0; i < serial->num_ports; ++i) {
459 tport = usb_get_serial_port_data(serial->port[i]);
461 kfifo_free(&tport->write_fifo);
470 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
472 struct ti_port *tport = usb_get_serial_port_data(port);
473 struct ti_device *tdev;
474 struct usb_device *dev;
478 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
479 TI_PIPE_TIMEOUT_ENABLE |
480 (TI_TRANSFER_TIMEOUT << 2));
485 dev = port->serial->dev;
486 tdev = tport->tp_tdev;
488 /* only one open on any port on a device at a time */
489 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
492 port_number = port->number - port->serial->minor;
494 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
497 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
499 /* start interrupt urb the first time a port is opened on this device */
500 if (tdev->td_open_port_count == 0) {
501 dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
502 urb = tdev->td_serial->port[0]->interrupt_in_urb;
504 dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
509 status = usb_submit_urb(urb, GFP_KERNEL);
511 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
517 ti_set_termios(tty, port, tty->termios);
519 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__);
520 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
521 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
523 dev_err(&port->dev, "%s - cannot send open command, %d\n",
528 dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__);
529 status = ti_command_out_sync(tdev, TI_START_PORT,
530 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
532 dev_err(&port->dev, "%s - cannot send start command, %d\n",
537 dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__);
538 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
539 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
541 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
545 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
546 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
548 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
553 /* reset the data toggle on the bulk endpoints to work around bug in
554 * host controllers where things get out of sync some times */
555 usb_clear_halt(dev, port->write_urb->pipe);
556 usb_clear_halt(dev, port->read_urb->pipe);
559 ti_set_termios(tty, port, tty->termios);
561 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __func__);
562 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
563 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
565 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
570 dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __func__);
571 status = ti_command_out_sync(tdev, TI_START_PORT,
572 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
574 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
580 dev_dbg(&port->dev, "%s - start read urb\n", __func__);
581 urb = port->read_urb;
583 dev_err(&port->dev, "%s - no read urb\n", __func__);
587 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
588 urb->context = tport;
589 status = usb_submit_urb(urb, GFP_KERNEL);
591 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
596 tport->tp_is_open = 1;
597 ++tdev->td_open_port_count;
602 if (tdev->td_open_port_count == 0)
603 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
605 mutex_unlock(&tdev->td_open_close_lock);
606 dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
611 static void ti_close(struct usb_serial_port *port)
613 struct ti_device *tdev;
614 struct ti_port *tport;
619 tdev = usb_get_serial_data(port->serial);
620 tport = usb_get_serial_port_data(port);
621 if (tdev == NULL || tport == NULL)
624 tport->tp_is_open = 0;
626 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
628 usb_kill_urb(port->read_urb);
629 usb_kill_urb(port->write_urb);
630 tport->tp_write_urb_in_use = 0;
632 port_number = port->number - port->serial->minor;
634 dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
635 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
636 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
639 "%s - cannot send close port command, %d\n"
642 /* if mutex_lock is interrupted, continue anyway */
643 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
644 --tport->tp_tdev->td_open_port_count;
645 if (tport->tp_tdev->td_open_port_count <= 0) {
646 /* last port is closed, shut down interrupt urb */
647 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
648 tport->tp_tdev->td_open_port_count = 0;
651 mutex_unlock(&tdev->td_open_close_lock);
655 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
656 const unsigned char *data, int count)
658 struct ti_port *tport = usb_get_serial_port_data(port);
661 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
665 if (tport == NULL || !tport->tp_is_open)
668 count = kfifo_in_locked(&tport->write_fifo, data, count,
676 static int ti_write_room(struct tty_struct *tty)
678 struct usb_serial_port *port = tty->driver_data;
679 struct ti_port *tport = usb_get_serial_port_data(port);
686 spin_lock_irqsave(&tport->tp_lock, flags);
687 room = kfifo_avail(&tport->write_fifo);
688 spin_unlock_irqrestore(&tport->tp_lock, flags);
690 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
695 static int ti_chars_in_buffer(struct tty_struct *tty)
697 struct usb_serial_port *port = tty->driver_data;
698 struct ti_port *tport = usb_get_serial_port_data(port);
705 spin_lock_irqsave(&tport->tp_lock, flags);
706 chars = kfifo_len(&tport->write_fifo);
707 spin_unlock_irqrestore(&tport->tp_lock, flags);
709 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
714 static void ti_throttle(struct tty_struct *tty)
716 struct usb_serial_port *port = tty->driver_data;
717 struct ti_port *tport = usb_get_serial_port_data(port);
722 if (I_IXOFF(tty) || C_CRTSCTS(tty))
723 ti_stop_read(tport, tty);
728 static void ti_unthrottle(struct tty_struct *tty)
730 struct usb_serial_port *port = tty->driver_data;
731 struct ti_port *tport = usb_get_serial_port_data(port);
737 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
738 status = ti_restart_read(tport, tty);
740 dev_err(&port->dev, "%s - cannot restart read, %d\n",
745 static int ti_get_icount(struct tty_struct *tty,
746 struct serial_icounter_struct *icount)
748 struct usb_serial_port *port = tty->driver_data;
749 struct ti_port *tport = usb_get_serial_port_data(port);
750 struct async_icount cnow = tport->tp_icount;
752 dev_dbg(&port->dev, "%s - TIOCGICOUNT RX=%d, TX=%d\n", __func__,
755 icount->cts = cnow.cts;
756 icount->dsr = cnow.dsr;
757 icount->rng = cnow.rng;
758 icount->dcd = cnow.dcd;
759 icount->rx = cnow.rx;
760 icount->tx = cnow.tx;
761 icount->frame = cnow.frame;
762 icount->overrun = cnow.overrun;
763 icount->parity = cnow.parity;
764 icount->brk = cnow.brk;
765 icount->buf_overrun = cnow.buf_overrun;
770 static int ti_ioctl(struct tty_struct *tty,
771 unsigned int cmd, unsigned long arg)
773 struct usb_serial_port *port = tty->driver_data;
774 struct ti_port *tport = usb_get_serial_port_data(port);
775 struct async_icount cnow;
776 struct async_icount cprev;
778 dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd);
785 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
786 return ti_get_serial_info(tport,
787 (struct serial_struct __user *)arg);
789 dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
790 return ti_set_serial_info(tty, tport,
791 (struct serial_struct __user *)arg);
793 dev_dbg(&port->dev, "%s - TIOCMIWAIT\n", __func__);
794 cprev = tport->tp_icount;
796 interruptible_sleep_on(&tport->tp_msr_wait);
797 if (signal_pending(current))
799 cnow = tport->tp_icount;
800 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
801 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
802 return -EIO; /* no change => error */
803 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
804 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
805 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
806 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
816 static void ti_set_termios(struct tty_struct *tty,
817 struct usb_serial_port *port, struct ktermios *old_termios)
819 struct ti_port *tport = usb_get_serial_port_data(port);
820 struct ti_uart_config *config;
821 tcflag_t cflag, iflag;
824 int port_number = port->number - port->serial->minor;
827 cflag = tty->termios->c_cflag;
828 iflag = tty->termios->c_iflag;
830 dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
831 dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
832 old_termios->c_cflag, old_termios->c_iflag);
837 config = kmalloc(sizeof(*config), GFP_KERNEL);
839 dev_err(&port->dev, "%s - out of memory\n", __func__);
845 /* these flags must be set */
846 config->wFlags |= TI_UART_ENABLE_MS_INTS;
847 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
848 config->bUartMode = (__u8)(tport->tp_uart_mode);
850 switch (cflag & CSIZE) {
852 config->bDataBits = TI_UART_5_DATA_BITS;
855 config->bDataBits = TI_UART_6_DATA_BITS;
858 config->bDataBits = TI_UART_7_DATA_BITS;
862 config->bDataBits = TI_UART_8_DATA_BITS;
866 /* CMSPAR isn't supported by this driver */
867 tty->termios->c_cflag &= ~CMSPAR;
869 if (cflag & PARENB) {
870 if (cflag & PARODD) {
871 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
872 config->bParity = TI_UART_ODD_PARITY;
874 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
875 config->bParity = TI_UART_EVEN_PARITY;
878 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
879 config->bParity = TI_UART_NO_PARITY;
883 config->bStopBits = TI_UART_2_STOP_BITS;
885 config->bStopBits = TI_UART_1_STOP_BITS;
887 if (cflag & CRTSCTS) {
888 /* RTS flow control must be off to drop RTS for baud rate B0 */
889 if ((cflag & CBAUD) != B0)
890 config->wFlags |= TI_UART_ENABLE_RTS_IN;
891 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
894 ti_restart_read(tport, tty);
897 if (I_IXOFF(tty) || I_IXON(tty)) {
898 config->cXon = START_CHAR(tty);
899 config->cXoff = STOP_CHAR(tty);
902 config->wFlags |= TI_UART_ENABLE_X_IN;
904 ti_restart_read(tport, tty);
907 config->wFlags |= TI_UART_ENABLE_X_OUT;
910 baud = tty_get_baud_rate(tty);
913 if (tport->tp_tdev->td_is_3410)
914 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
916 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
918 /* FIXME: Should calculate resulting baud here and report it back */
919 if ((cflag & CBAUD) != B0)
920 tty_encode_baud_rate(tty, baud, baud);
923 "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
924 __func__, baud, config->wBaudRate, config->wFlags,
925 config->bDataBits, config->bParity, config->bStopBits,
926 config->cXon, config->cXoff, config->bUartMode);
928 cpu_to_be16s(&config->wBaudRate);
929 cpu_to_be16s(&config->wFlags);
931 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
932 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
935 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
936 __func__, port_number, status);
938 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
939 mcr = tport->tp_shadow_mcr;
940 /* if baud rate is B0, clear RTS and DTR */
941 if ((cflag & CBAUD) == B0)
942 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
943 status = ti_set_mcr(tport, mcr);
946 "%s - cannot set modem control on port %d, %d\n",
947 __func__, port_number, status);
953 static int ti_tiocmget(struct tty_struct *tty)
955 struct usb_serial_port *port = tty->driver_data;
956 struct ti_port *tport = usb_get_serial_port_data(port);
965 spin_lock_irqsave(&tport->tp_lock, flags);
967 mcr = tport->tp_shadow_mcr;
968 spin_unlock_irqrestore(&tport->tp_lock, flags);
970 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
971 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
972 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
973 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
974 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
975 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
976 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
978 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
984 static int ti_tiocmset(struct tty_struct *tty,
985 unsigned int set, unsigned int clear)
987 struct usb_serial_port *port = tty->driver_data;
988 struct ti_port *tport = usb_get_serial_port_data(port);
995 spin_lock_irqsave(&tport->tp_lock, flags);
996 mcr = tport->tp_shadow_mcr;
1000 if (set & TIOCM_DTR)
1002 if (set & TIOCM_LOOP)
1005 if (clear & TIOCM_RTS)
1007 if (clear & TIOCM_DTR)
1009 if (clear & TIOCM_LOOP)
1010 mcr &= ~TI_MCR_LOOP;
1011 spin_unlock_irqrestore(&tport->tp_lock, flags);
1013 return ti_set_mcr(tport, mcr);
1017 static void ti_break(struct tty_struct *tty, int break_state)
1019 struct usb_serial_port *port = tty->driver_data;
1020 struct ti_port *tport = usb_get_serial_port_data(port);
1023 dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
1028 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1030 status = ti_write_byte(port, tport->tp_tdev,
1031 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1032 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1035 dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
1039 static void ti_interrupt_callback(struct urb *urb)
1041 struct ti_device *tdev = urb->context;
1042 struct usb_serial_port *port;
1043 struct usb_serial *serial = tdev->td_serial;
1044 struct ti_port *tport;
1045 struct device *dev = &urb->dev->dev;
1046 unsigned char *data = urb->transfer_buffer;
1047 int length = urb->actual_length;
1050 int status = urb->status;
1060 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1061 tdev->td_urb_error = 1;
1064 dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
1065 tdev->td_urb_error = 1;
1070 dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
1074 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1075 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1079 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1080 function = TI_GET_FUNC_FROM_CODE(data[0]);
1082 dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
1083 __func__, port_number, function, data[1]);
1085 if (port_number >= serial->num_ports) {
1086 dev_err(dev, "%s - bad port number, %d\n",
1087 __func__, port_number);
1091 port = serial->port[port_number];
1093 tport = usb_get_serial_port_data(port);
1098 case TI_CODE_DATA_ERROR:
1099 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1100 __func__, port_number, data[1]);
1103 case TI_CODE_MODEM_STATUS:
1105 dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
1106 ti_handle_new_msr(tport, msr);
1110 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1116 retval = usb_submit_urb(urb, GFP_ATOMIC);
1118 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1123 static void ti_bulk_in_callback(struct urb *urb)
1125 struct ti_port *tport = urb->context;
1126 struct usb_serial_port *port = tport->tp_port;
1127 struct device *dev = &urb->dev->dev;
1128 int status = urb->status;
1130 struct tty_struct *tty;
1138 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1139 tport->tp_tdev->td_urb_error = 1;
1140 wake_up_interruptible(&tport->tp_write_wait);
1143 dev_err(dev, "%s - nonzero urb status, %d\n",
1145 tport->tp_tdev->td_urb_error = 1;
1146 wake_up_interruptible(&tport->tp_write_wait);
1149 if (status == -EPIPE)
1153 dev_err(dev, "%s - stopping read!\n", __func__);
1157 tty = tty_port_tty_get(&port->port);
1159 if (urb->actual_length) {
1160 usb_serial_debug_data(dev, __func__, urb->actual_length,
1161 urb->transfer_buffer);
1163 if (!tport->tp_is_open)
1164 dev_dbg(dev, "%s - port closed, dropping data\n",
1167 ti_recv(&urb->dev->dev, tty,
1168 urb->transfer_buffer,
1169 urb->actual_length);
1170 spin_lock(&tport->tp_lock);
1171 tport->tp_icount.rx += urb->actual_length;
1172 spin_unlock(&tport->tp_lock);
1178 /* continue to read unless stopping */
1179 spin_lock(&tport->tp_lock);
1180 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1181 retval = usb_submit_urb(urb, GFP_ATOMIC);
1182 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
1183 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1185 spin_unlock(&tport->tp_lock);
1187 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1192 static void ti_bulk_out_callback(struct urb *urb)
1194 struct ti_port *tport = urb->context;
1195 struct usb_serial_port *port = tport->tp_port;
1196 int status = urb->status;
1198 tport->tp_write_urb_in_use = 0;
1206 dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
1207 tport->tp_tdev->td_urb_error = 1;
1208 wake_up_interruptible(&tport->tp_write_wait);
1211 dev_err_console(port, "%s - nonzero urb status, %d\n",
1213 tport->tp_tdev->td_urb_error = 1;
1214 wake_up_interruptible(&tport->tp_write_wait);
1217 /* send any buffered data */
1222 static void ti_recv(struct device *dev, struct tty_struct *tty,
1223 unsigned char *data, int length)
1228 cnt = tty_insert_flip_string(tty, data, length);
1230 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1231 __func__, length - cnt);
1235 tty_flip_buffer_push(tty);
1238 } while (length > 0);
1243 static void ti_send(struct ti_port *tport)
1246 struct usb_serial_port *port = tport->tp_port;
1247 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1248 unsigned long flags;
1250 spin_lock_irqsave(&tport->tp_lock, flags);
1252 if (tport->tp_write_urb_in_use)
1255 count = kfifo_out(&tport->write_fifo,
1256 port->write_urb->transfer_buffer,
1257 port->bulk_out_size);
1262 tport->tp_write_urb_in_use = 1;
1264 spin_unlock_irqrestore(&tport->tp_lock, flags);
1266 usb_serial_debug_data(&port->dev, __func__, count,
1267 port->write_urb->transfer_buffer);
1269 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1270 usb_sndbulkpipe(port->serial->dev,
1271 port->bulk_out_endpointAddress),
1272 port->write_urb->transfer_buffer, count,
1273 ti_bulk_out_callback, tport);
1275 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1277 dev_err_console(port, "%s - submit write urb failed, %d\n",
1279 tport->tp_write_urb_in_use = 0;
1280 /* TODO: reschedule ti_send */
1282 spin_lock_irqsave(&tport->tp_lock, flags);
1283 tport->tp_icount.tx += count;
1284 spin_unlock_irqrestore(&tport->tp_lock, flags);
1287 /* more room in the buffer for new writes, wakeup */
1291 wake_up_interruptible(&tport->tp_write_wait);
1294 spin_unlock_irqrestore(&tport->tp_lock, flags);
1300 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1302 unsigned long flags;
1305 status = ti_write_byte(tport->tp_port, tport->tp_tdev,
1306 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1307 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1309 spin_lock_irqsave(&tport->tp_lock, flags);
1311 tport->tp_shadow_mcr = mcr;
1312 spin_unlock_irqrestore(&tport->tp_lock, flags);
1318 static int ti_get_lsr(struct ti_port *tport)
1321 struct ti_device *tdev = tport->tp_tdev;
1322 struct usb_serial_port *port = tport->tp_port;
1323 int port_number = port->number - port->serial->minor;
1324 struct ti_port_status *data;
1326 size = sizeof(struct ti_port_status);
1327 data = kmalloc(size, GFP_KERNEL);
1329 dev_err(&port->dev, "%s - out of memory\n", __func__);
1333 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1334 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1337 "%s - get port status command failed, %d\n",
1342 dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
1344 tport->tp_lsr = data->bLSR;
1352 static int ti_get_serial_info(struct ti_port *tport,
1353 struct serial_struct __user *ret_arg)
1355 struct usb_serial_port *port = tport->tp_port;
1356 struct serial_struct ret_serial;
1361 memset(&ret_serial, 0, sizeof(ret_serial));
1363 ret_serial.type = PORT_16550A;
1364 ret_serial.line = port->serial->minor;
1365 ret_serial.port = port->number - port->serial->minor;
1366 ret_serial.flags = tport->tp_flags;
1367 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1368 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1369 ret_serial.closing_wait = tport->tp_closing_wait;
1371 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1378 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1379 struct serial_struct __user *new_arg)
1381 struct serial_struct new_serial;
1383 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1386 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1387 tport->tp_closing_wait = new_serial.closing_wait;
1393 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1395 struct async_icount *icount;
1396 struct tty_struct *tty;
1397 unsigned long flags;
1399 dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
1401 if (msr & TI_MSR_DELTA_MASK) {
1402 spin_lock_irqsave(&tport->tp_lock, flags);
1403 icount = &tport->tp_icount;
1404 if (msr & TI_MSR_DELTA_CTS)
1406 if (msr & TI_MSR_DELTA_DSR)
1408 if (msr & TI_MSR_DELTA_CD)
1410 if (msr & TI_MSR_DELTA_RI)
1412 wake_up_interruptible(&tport->tp_msr_wait);
1413 spin_unlock_irqrestore(&tport->tp_lock, flags);
1416 tport->tp_msr = msr & TI_MSR_MASK;
1418 /* handle CTS flow control */
1419 tty = tty_port_tty_get(&tport->tp_port->port);
1420 if (tty && C_CRTSCTS(tty)) {
1421 if (msr & TI_MSR_CTS) {
1422 tty->hw_stopped = 0;
1425 tty->hw_stopped = 1;
1432 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1434 struct ti_device *tdev = tport->tp_tdev;
1435 struct usb_serial_port *port = tport->tp_port;
1438 spin_lock_irq(&tport->tp_lock);
1440 /* wait for data to drain from the buffer */
1441 tdev->td_urb_error = 0;
1442 init_waitqueue_entry(&wait, current);
1443 add_wait_queue(&tport->tp_write_wait, &wait);
1445 set_current_state(TASK_INTERRUPTIBLE);
1446 if (kfifo_len(&tport->write_fifo) == 0
1447 || timeout == 0 || signal_pending(current)
1448 || tdev->td_urb_error
1449 || port->serial->disconnected) /* disconnect */
1451 spin_unlock_irq(&tport->tp_lock);
1452 timeout = schedule_timeout(timeout);
1453 spin_lock_irq(&tport->tp_lock);
1455 set_current_state(TASK_RUNNING);
1456 remove_wait_queue(&tport->tp_write_wait, &wait);
1458 /* flush any remaining data in the buffer */
1460 kfifo_reset_out(&tport->write_fifo);
1462 spin_unlock_irq(&tport->tp_lock);
1464 mutex_lock(&port->serial->disc_mutex);
1465 /* wait for data to drain from the device */
1466 /* wait for empty tx register, plus 20 ms */
1468 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1469 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1470 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1471 && !port->serial->disconnected) {
1472 if (ti_get_lsr(tport))
1474 mutex_unlock(&port->serial->disc_mutex);
1475 msleep_interruptible(20);
1476 mutex_lock(&port->serial->disc_mutex);
1478 mutex_unlock(&port->serial->disc_mutex);
1482 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1484 unsigned long flags;
1486 spin_lock_irqsave(&tport->tp_lock, flags);
1488 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1489 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1491 spin_unlock_irqrestore(&tport->tp_lock, flags);
1495 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1499 unsigned long flags;
1501 spin_lock_irqsave(&tport->tp_lock, flags);
1503 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1504 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1505 urb = tport->tp_port->read_urb;
1506 spin_unlock_irqrestore(&tport->tp_lock, flags);
1507 urb->context = tport;
1508 status = usb_submit_urb(urb, GFP_KERNEL);
1510 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1511 spin_unlock_irqrestore(&tport->tp_lock, flags);
1518 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1519 __u16 moduleid, __u16 value, __u8 *data, int size)
1523 status = usb_control_msg(tdev->td_serial->dev,
1524 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1525 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1526 value, moduleid, data, size, 1000);
1538 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1539 __u16 moduleid, __u16 value, __u8 *data, int size)
1543 status = usb_control_msg(tdev->td_serial->dev,
1544 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1545 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1546 value, moduleid, data, size, 1000);
1558 static int ti_write_byte(struct usb_serial_port *port,
1559 struct ti_device *tdev, unsigned long addr,
1560 __u8 mask, __u8 byte)
1564 struct ti_write_data_bytes *data;
1566 dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1569 size = sizeof(struct ti_write_data_bytes) + 2;
1570 data = kmalloc(size, GFP_KERNEL);
1572 dev_err(&port->dev, "%s - out of memory\n", __func__);
1576 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1577 data->bDataType = TI_RW_DATA_BYTE;
1578 data->bDataCounter = 1;
1579 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1580 data->wBaseAddrLo = cpu_to_be16(addr);
1581 data->bData[0] = mask;
1582 data->bData[1] = byte;
1584 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1585 (__u8 *)data, size);
1588 dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
1595 static int ti_do_download(struct usb_device *dev, int pipe,
1596 u8 *buffer, int size)
1601 struct ti_firmware_header *header;
1605 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1606 cs = (__u8)(cs + buffer[pos]);
1608 header = (struct ti_firmware_header *)buffer;
1609 header->wLength = cpu_to_le16((__u16)(size
1610 - sizeof(struct ti_firmware_header)));
1611 header->bCheckSum = cs;
1613 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
1614 for (pos = 0; pos < size; pos += done) {
1615 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1616 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1624 static int ti_download_firmware(struct ti_device *tdev)
1629 struct usb_device *dev = tdev->td_serial->dev;
1630 unsigned int pipe = usb_sndbulkpipe(dev,
1631 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1632 const struct firmware *fw_p;
1635 /* try ID specific firmware first, then try generic firmware */
1636 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1637 dev->descriptor.idProduct);
1638 status = request_firmware(&fw_p, buf, &dev->dev);
1642 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1643 switch (dev->descriptor.idProduct) {
1644 case MTS_CDMA_PRODUCT_ID:
1645 strcpy(buf, "mts_cdma.fw");
1647 case MTS_GSM_PRODUCT_ID:
1648 strcpy(buf, "mts_gsm.fw");
1650 case MTS_EDGE_PRODUCT_ID:
1651 strcpy(buf, "mts_edge.fw");
1653 case MTS_MT9234MU_PRODUCT_ID:
1654 strcpy(buf, "mts_mt9234mu.fw");
1656 case MTS_MT9234ZBA_PRODUCT_ID:
1657 strcpy(buf, "mts_mt9234zba.fw");
1659 case MTS_MT9234ZBAOLD_PRODUCT_ID:
1660 strcpy(buf, "mts_mt9234zba.fw");
1663 if (buf[0] == '\0') {
1664 if (tdev->td_is_3410)
1665 strcpy(buf, "ti_3410.fw");
1667 strcpy(buf, "ti_5052.fw");
1669 status = request_firmware(&fw_p, buf, &dev->dev);
1672 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1675 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1676 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1677 release_firmware(fw_p);
1681 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1682 buffer = kmalloc(buffer_size, GFP_KERNEL);
1684 memcpy(buffer, fw_p->data, fw_p->size);
1685 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1686 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1689 dev_dbg(&dev->dev, "%s ENOMEM\n", __func__);
1692 release_firmware(fw_p);
1694 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1699 dev_dbg(&dev->dev, "%s - download successful\n", __func__);