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 ti_device *tdev, unsigned long addr,
145 __u8 mask, __u8 byte);
147 static int ti_download_firmware(struct ti_device *tdev);
152 /* module parameters */
154 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
155 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
156 static unsigned int vendor_3410_count;
157 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
158 static unsigned int product_3410_count;
159 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
160 static unsigned int vendor_5052_count;
161 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
162 static unsigned int product_5052_count;
164 /* supported devices */
165 /* the array dimension is the number of default entries plus */
166 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
168 static struct usb_device_id ti_id_table_3410[14+TI_EXTRA_VID_PID_COUNT+1] = {
169 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
170 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
171 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
172 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
173 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
174 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
175 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
176 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
177 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
178 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
179 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
180 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
181 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
182 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_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[18+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) },
214 static struct usb_driver ti_usb_driver = {
215 .name = "ti_usb_3410_5052",
216 .probe = usb_serial_probe,
217 .disconnect = usb_serial_disconnect,
218 .id_table = ti_id_table_combined,
221 static struct usb_serial_driver ti_1port_device = {
223 .owner = THIS_MODULE,
224 .name = "ti_usb_3410_5052_1",
226 .description = "TI USB 3410 1 port adapter",
227 .id_table = ti_id_table_3410,
229 .attach = ti_startup,
230 .release = ti_release,
234 .write_room = ti_write_room,
235 .chars_in_buffer = ti_chars_in_buffer,
236 .throttle = ti_throttle,
237 .unthrottle = ti_unthrottle,
239 .set_termios = ti_set_termios,
240 .tiocmget = ti_tiocmget,
241 .tiocmset = ti_tiocmset,
242 .get_icount = ti_get_icount,
243 .break_ctl = ti_break,
244 .read_int_callback = ti_interrupt_callback,
245 .read_bulk_callback = ti_bulk_in_callback,
246 .write_bulk_callback = ti_bulk_out_callback,
249 static struct usb_serial_driver ti_2port_device = {
251 .owner = THIS_MODULE,
252 .name = "ti_usb_3410_5052_2",
254 .description = "TI USB 5052 2 port adapter",
255 .id_table = ti_id_table_5052,
257 .attach = ti_startup,
258 .release = ti_release,
262 .write_room = ti_write_room,
263 .chars_in_buffer = ti_chars_in_buffer,
264 .throttle = ti_throttle,
265 .unthrottle = ti_unthrottle,
267 .set_termios = ti_set_termios,
268 .tiocmget = ti_tiocmget,
269 .tiocmset = ti_tiocmset,
270 .get_icount = ti_get_icount,
271 .break_ctl = ti_break,
272 .read_int_callback = ti_interrupt_callback,
273 .read_bulk_callback = ti_bulk_in_callback,
274 .write_bulk_callback = ti_bulk_out_callback,
277 static struct usb_serial_driver * const serial_drivers[] = {
278 &ti_1port_device, &ti_2port_device, NULL
283 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
284 MODULE_DESCRIPTION(TI_DRIVER_DESC);
285 MODULE_VERSION(TI_DRIVER_VERSION);
286 MODULE_LICENSE("GPL");
288 MODULE_FIRMWARE("ti_3410.fw");
289 MODULE_FIRMWARE("ti_5052.fw");
290 MODULE_FIRMWARE("mts_cdma.fw");
291 MODULE_FIRMWARE("mts_gsm.fw");
292 MODULE_FIRMWARE("mts_edge.fw");
293 MODULE_FIRMWARE("mts_mt9234mu.fw");
294 MODULE_FIRMWARE("mts_mt9234zba.fw");
296 module_param(debug, bool, S_IRUGO | S_IWUSR);
297 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
299 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
300 MODULE_PARM_DESC(closing_wait,
301 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
303 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
304 MODULE_PARM_DESC(vendor_3410,
305 "Vendor ids for 3410 based devices, 1-5 short integers");
306 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
307 MODULE_PARM_DESC(product_3410,
308 "Product ids for 3410 based devices, 1-5 short integers");
309 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
310 MODULE_PARM_DESC(vendor_5052,
311 "Vendor ids for 5052 based devices, 1-5 short integers");
312 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
313 MODULE_PARM_DESC(product_5052,
314 "Product ids for 5052 based devices, 1-5 short integers");
316 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
321 static int __init ti_init(void)
326 /* insert extra vendor and product ids */
327 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
328 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
329 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
330 ti_id_table_3410[j].idVendor = vendor_3410[i];
331 ti_id_table_3410[j].idProduct = product_3410[i];
332 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
333 ti_id_table_combined[c].idVendor = vendor_3410[i];
334 ti_id_table_combined[c].idProduct = product_3410[i];
335 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
337 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
338 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
339 ti_id_table_5052[j].idVendor = vendor_5052[i];
340 ti_id_table_5052[j].idProduct = product_5052[i];
341 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
342 ti_id_table_combined[c].idVendor = vendor_5052[i];
343 ti_id_table_combined[c].idProduct = product_5052[i];
344 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
347 ret = usb_serial_register_drivers(&ti_usb_driver, serial_drivers);
349 printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":"
350 TI_DRIVER_DESC "\n");
355 static void __exit ti_exit(void)
357 usb_serial_deregister_drivers(&ti_usb_driver, serial_drivers);
361 module_init(ti_init);
362 module_exit(ti_exit);
365 static int ti_startup(struct usb_serial *serial)
367 struct ti_device *tdev;
368 struct ti_port *tport;
369 struct usb_device *dev = serial->dev;
374 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
375 __func__, le16_to_cpu(dev->descriptor.idProduct),
376 dev->descriptor.bNumConfigurations,
377 dev->actconfig->desc.bConfigurationValue);
379 /* create device structure */
380 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
382 dev_err(&dev->dev, "%s - out of memory\n", __func__);
385 mutex_init(&tdev->td_open_close_lock);
386 tdev->td_serial = serial;
387 usb_set_serial_data(serial, tdev);
389 /* determine device type */
390 if (usb_match_id(serial->interface, ti_id_table_3410))
391 tdev->td_is_3410 = 1;
392 dbg("%s - device type is %s", __func__,
393 tdev->td_is_3410 ? "3410" : "5052");
395 /* if we have only 1 configuration, download firmware */
396 if (dev->descriptor.bNumConfigurations == 1) {
397 if ((status = ti_download_firmware(tdev)) != 0)
400 /* 3410 must be reset, 5052 resets itself */
401 if (tdev->td_is_3410) {
402 msleep_interruptible(100);
403 usb_reset_device(dev);
410 /* the second configuration must be set */
411 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
412 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
413 status = status ? status : -ENODEV;
417 /* set up port structures */
418 for (i = 0; i < serial->num_ports; ++i) {
419 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
421 dev_err(&dev->dev, "%s - out of memory\n", __func__);
425 spin_lock_init(&tport->tp_lock);
426 tport->tp_uart_base_addr = (i == 0 ?
427 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
428 tport->tp_closing_wait = closing_wait;
429 init_waitqueue_head(&tport->tp_msr_wait);
430 init_waitqueue_head(&tport->tp_write_wait);
431 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE,
433 dev_err(&dev->dev, "%s - out of memory\n", __func__);
438 tport->tp_port = serial->port[i];
439 tport->tp_tdev = tdev;
440 usb_set_serial_port_data(serial->port[i], tport);
441 tport->tp_uart_mode = 0; /* default is RS232 */
447 for (--i; i >= 0; --i) {
448 tport = usb_get_serial_port_data(serial->port[i]);
449 kfifo_free(&tport->write_fifo);
451 usb_set_serial_port_data(serial->port[i], NULL);
455 usb_set_serial_data(serial, NULL);
460 static void ti_release(struct usb_serial *serial)
463 struct ti_device *tdev = usb_get_serial_data(serial);
464 struct ti_port *tport;
468 for (i = 0; i < serial->num_ports; ++i) {
469 tport = usb_get_serial_port_data(serial->port[i]);
471 kfifo_free(&tport->write_fifo);
480 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
482 struct ti_port *tport = usb_get_serial_port_data(port);
483 struct ti_device *tdev;
484 struct usb_device *dev;
488 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
489 TI_PIPE_TIMEOUT_ENABLE |
490 (TI_TRANSFER_TIMEOUT << 2));
492 dbg("%s - port %d", __func__, port->number);
497 dev = port->serial->dev;
498 tdev = tport->tp_tdev;
500 /* only one open on any port on a device at a time */
501 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
504 port_number = port->number - port->serial->minor;
506 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
509 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
511 /* start interrupt urb the first time a port is opened on this device */
512 if (tdev->td_open_port_count == 0) {
513 dbg("%s - start interrupt in urb", __func__);
514 urb = tdev->td_serial->port[0]->interrupt_in_urb;
516 dev_err(&port->dev, "%s - no interrupt urb\n",
522 status = usb_submit_urb(urb, GFP_KERNEL);
525 "%s - submit interrupt urb failed, %d\n",
532 ti_set_termios(tty, port, tty->termios);
534 dbg("%s - sending TI_OPEN_PORT", __func__);
535 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
536 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
538 dev_err(&port->dev, "%s - cannot send open command, %d\n",
543 dbg("%s - sending TI_START_PORT", __func__);
544 status = ti_command_out_sync(tdev, TI_START_PORT,
545 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
547 dev_err(&port->dev, "%s - cannot send start command, %d\n",
552 dbg("%s - sending TI_PURGE_PORT", __func__);
553 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
554 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
556 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
560 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
561 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
563 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
568 /* reset the data toggle on the bulk endpoints to work around bug in
569 * host controllers where things get out of sync some times */
570 usb_clear_halt(dev, port->write_urb->pipe);
571 usb_clear_halt(dev, port->read_urb->pipe);
574 ti_set_termios(tty, port, tty->termios);
576 dbg("%s - sending TI_OPEN_PORT (2)", __func__);
577 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
578 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
580 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
585 dbg("%s - sending TI_START_PORT (2)", __func__);
586 status = ti_command_out_sync(tdev, TI_START_PORT,
587 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
589 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
595 dbg("%s - start read urb", __func__);
596 urb = port->read_urb;
598 dev_err(&port->dev, "%s - no read urb\n", __func__);
602 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
603 urb->context = tport;
604 status = usb_submit_urb(urb, GFP_KERNEL);
606 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
611 tport->tp_is_open = 1;
612 ++tdev->td_open_port_count;
617 if (tdev->td_open_port_count == 0)
618 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
620 mutex_unlock(&tdev->td_open_close_lock);
621 dbg("%s - exit %d", __func__, status);
626 static void ti_close(struct usb_serial_port *port)
628 struct ti_device *tdev;
629 struct ti_port *tport;
634 dbg("%s - port %d", __func__, port->number);
636 tdev = usb_get_serial_data(port->serial);
637 tport = usb_get_serial_port_data(port);
638 if (tdev == NULL || tport == NULL)
641 tport->tp_is_open = 0;
643 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
645 usb_kill_urb(port->read_urb);
646 usb_kill_urb(port->write_urb);
647 tport->tp_write_urb_in_use = 0;
649 port_number = port->number - port->serial->minor;
651 dbg("%s - sending TI_CLOSE_PORT", __func__);
652 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
653 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
656 "%s - cannot send close port command, %d\n"
659 /* if mutex_lock is interrupted, continue anyway */
660 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
661 --tport->tp_tdev->td_open_port_count;
662 if (tport->tp_tdev->td_open_port_count <= 0) {
663 /* last port is closed, shut down interrupt urb */
664 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
665 tport->tp_tdev->td_open_port_count = 0;
668 mutex_unlock(&tdev->td_open_close_lock);
670 dbg("%s - exit", __func__);
674 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
675 const unsigned char *data, int count)
677 struct ti_port *tport = usb_get_serial_port_data(port);
679 dbg("%s - port %d", __func__, port->number);
682 dbg("%s - write request of 0 bytes", __func__);
686 if (tport == NULL || !tport->tp_is_open)
689 count = kfifo_in_locked(&tport->write_fifo, data, count,
697 static int ti_write_room(struct tty_struct *tty)
699 struct usb_serial_port *port = tty->driver_data;
700 struct ti_port *tport = usb_get_serial_port_data(port);
704 dbg("%s - port %d", __func__, port->number);
709 spin_lock_irqsave(&tport->tp_lock, flags);
710 room = kfifo_avail(&tport->write_fifo);
711 spin_unlock_irqrestore(&tport->tp_lock, flags);
713 dbg("%s - returns %d", __func__, room);
718 static int ti_chars_in_buffer(struct tty_struct *tty)
720 struct usb_serial_port *port = tty->driver_data;
721 struct ti_port *tport = usb_get_serial_port_data(port);
725 dbg("%s - port %d", __func__, port->number);
730 spin_lock_irqsave(&tport->tp_lock, flags);
731 chars = kfifo_len(&tport->write_fifo);
732 spin_unlock_irqrestore(&tport->tp_lock, flags);
734 dbg("%s - returns %d", __func__, chars);
739 static void ti_throttle(struct tty_struct *tty)
741 struct usb_serial_port *port = tty->driver_data;
742 struct ti_port *tport = usb_get_serial_port_data(port);
744 dbg("%s - port %d", __func__, port->number);
749 if (I_IXOFF(tty) || C_CRTSCTS(tty))
750 ti_stop_read(tport, tty);
755 static void ti_unthrottle(struct tty_struct *tty)
757 struct usb_serial_port *port = tty->driver_data;
758 struct ti_port *tport = usb_get_serial_port_data(port);
761 dbg("%s - port %d", __func__, port->number);
766 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
767 status = ti_restart_read(tport, tty);
769 dev_err(&port->dev, "%s - cannot restart read, %d\n",
774 static int ti_get_icount(struct tty_struct *tty,
775 struct serial_icounter_struct *icount)
777 struct usb_serial_port *port = tty->driver_data;
778 struct ti_port *tport = usb_get_serial_port_data(port);
779 struct async_icount cnow = tport->tp_icount;
781 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
782 __func__, port->number,
785 icount->cts = cnow.cts;
786 icount->dsr = cnow.dsr;
787 icount->rng = cnow.rng;
788 icount->dcd = cnow.dcd;
789 icount->rx = cnow.rx;
790 icount->tx = cnow.tx;
791 icount->frame = cnow.frame;
792 icount->overrun = cnow.overrun;
793 icount->parity = cnow.parity;
794 icount->brk = cnow.brk;
795 icount->buf_overrun = cnow.buf_overrun;
800 static int ti_ioctl(struct tty_struct *tty,
801 unsigned int cmd, unsigned long arg)
803 struct usb_serial_port *port = tty->driver_data;
804 struct ti_port *tport = usb_get_serial_port_data(port);
805 struct async_icount cnow;
806 struct async_icount cprev;
808 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
815 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
816 return ti_get_serial_info(tport,
817 (struct serial_struct __user *)arg);
819 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
820 return ti_set_serial_info(tty, tport,
821 (struct serial_struct __user *)arg);
823 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
824 cprev = tport->tp_icount;
826 interruptible_sleep_on(&tport->tp_msr_wait);
827 if (signal_pending(current))
829 cnow = tport->tp_icount;
830 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
831 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
832 return -EIO; /* no change => error */
833 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
834 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
835 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
836 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
846 static void ti_set_termios(struct tty_struct *tty,
847 struct usb_serial_port *port, struct ktermios *old_termios)
849 struct ti_port *tport = usb_get_serial_port_data(port);
850 struct ti_uart_config *config;
851 tcflag_t cflag, iflag;
854 int port_number = port->number - port->serial->minor;
857 dbg("%s - port %d", __func__, port->number);
859 cflag = tty->termios->c_cflag;
860 iflag = tty->termios->c_iflag;
862 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
863 dbg("%s - old clfag %08x, old iflag %08x", __func__,
864 old_termios->c_cflag, old_termios->c_iflag);
869 config = kmalloc(sizeof(*config), GFP_KERNEL);
871 dev_err(&port->dev, "%s - out of memory\n", __func__);
877 /* these flags must be set */
878 config->wFlags |= TI_UART_ENABLE_MS_INTS;
879 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
880 config->bUartMode = (__u8)(tport->tp_uart_mode);
882 switch (cflag & CSIZE) {
884 config->bDataBits = TI_UART_5_DATA_BITS;
887 config->bDataBits = TI_UART_6_DATA_BITS;
890 config->bDataBits = TI_UART_7_DATA_BITS;
894 config->bDataBits = TI_UART_8_DATA_BITS;
898 /* CMSPAR isn't supported by this driver */
899 tty->termios->c_cflag &= ~CMSPAR;
901 if (cflag & PARENB) {
902 if (cflag & PARODD) {
903 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
904 config->bParity = TI_UART_ODD_PARITY;
906 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
907 config->bParity = TI_UART_EVEN_PARITY;
910 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
911 config->bParity = TI_UART_NO_PARITY;
915 config->bStopBits = TI_UART_2_STOP_BITS;
917 config->bStopBits = TI_UART_1_STOP_BITS;
919 if (cflag & CRTSCTS) {
920 /* RTS flow control must be off to drop RTS for baud rate B0 */
921 if ((cflag & CBAUD) != B0)
922 config->wFlags |= TI_UART_ENABLE_RTS_IN;
923 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
926 ti_restart_read(tport, tty);
929 if (I_IXOFF(tty) || I_IXON(tty)) {
930 config->cXon = START_CHAR(tty);
931 config->cXoff = STOP_CHAR(tty);
934 config->wFlags |= TI_UART_ENABLE_X_IN;
936 ti_restart_read(tport, tty);
939 config->wFlags |= TI_UART_ENABLE_X_OUT;
942 baud = tty_get_baud_rate(tty);
945 if (tport->tp_tdev->td_is_3410)
946 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
948 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
950 /* FIXME: Should calculate resulting baud here and report it back */
951 if ((cflag & CBAUD) != B0)
952 tty_encode_baud_rate(tty, baud, baud);
954 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
955 __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
957 cpu_to_be16s(&config->wBaudRate);
958 cpu_to_be16s(&config->wFlags);
960 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
961 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
964 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
965 __func__, port_number, status);
967 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
968 mcr = tport->tp_shadow_mcr;
969 /* if baud rate is B0, clear RTS and DTR */
970 if ((cflag & CBAUD) == B0)
971 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
972 status = ti_set_mcr(tport, mcr);
975 "%s - cannot set modem control on port %d, %d\n",
976 __func__, port_number, status);
982 static int ti_tiocmget(struct tty_struct *tty)
984 struct usb_serial_port *port = tty->driver_data;
985 struct ti_port *tport = usb_get_serial_port_data(port);
991 dbg("%s - port %d", __func__, port->number);
996 spin_lock_irqsave(&tport->tp_lock, flags);
998 mcr = tport->tp_shadow_mcr;
999 spin_unlock_irqrestore(&tport->tp_lock, flags);
1001 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1002 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1003 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1004 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1005 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1006 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1007 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1009 dbg("%s - 0x%04X", __func__, result);
1015 static int ti_tiocmset(struct tty_struct *tty,
1016 unsigned int set, unsigned int clear)
1018 struct usb_serial_port *port = tty->driver_data;
1019 struct ti_port *tport = usb_get_serial_port_data(port);
1021 unsigned long flags;
1023 dbg("%s - port %d", __func__, port->number);
1028 spin_lock_irqsave(&tport->tp_lock, flags);
1029 mcr = tport->tp_shadow_mcr;
1031 if (set & TIOCM_RTS)
1033 if (set & TIOCM_DTR)
1035 if (set & TIOCM_LOOP)
1038 if (clear & TIOCM_RTS)
1040 if (clear & TIOCM_DTR)
1042 if (clear & TIOCM_LOOP)
1043 mcr &= ~TI_MCR_LOOP;
1044 spin_unlock_irqrestore(&tport->tp_lock, flags);
1046 return ti_set_mcr(tport, mcr);
1050 static void ti_break(struct tty_struct *tty, int break_state)
1052 struct usb_serial_port *port = tty->driver_data;
1053 struct ti_port *tport = usb_get_serial_port_data(port);
1056 dbg("%s - state = %d", __func__, break_state);
1061 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1063 status = ti_write_byte(tport->tp_tdev,
1064 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1065 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1068 dbg("%s - error setting break, %d", __func__, status);
1072 static void ti_interrupt_callback(struct urb *urb)
1074 struct ti_device *tdev = urb->context;
1075 struct usb_serial_port *port;
1076 struct usb_serial *serial = tdev->td_serial;
1077 struct ti_port *tport;
1078 struct device *dev = &urb->dev->dev;
1079 unsigned char *data = urb->transfer_buffer;
1080 int length = urb->actual_length;
1083 int status = urb->status;
1087 dbg("%s", __func__);
1095 dbg("%s - urb shutting down, %d", __func__, status);
1096 tdev->td_urb_error = 1;
1099 dev_err(dev, "%s - nonzero urb status, %d\n",
1101 tdev->td_urb_error = 1;
1106 dbg("%s - bad packet size, %d", __func__, length);
1110 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1111 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1115 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1116 function = TI_GET_FUNC_FROM_CODE(data[0]);
1118 dbg("%s - port_number %d, function %d, data 0x%02X",
1119 __func__, port_number, function, data[1]);
1121 if (port_number >= serial->num_ports) {
1122 dev_err(dev, "%s - bad port number, %d\n",
1123 __func__, port_number);
1127 port = serial->port[port_number];
1129 tport = usb_get_serial_port_data(port);
1134 case TI_CODE_DATA_ERROR:
1135 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1136 __func__, port_number, data[1]);
1139 case TI_CODE_MODEM_STATUS:
1141 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
1142 ti_handle_new_msr(tport, msr);
1146 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1152 retval = usb_submit_urb(urb, GFP_ATOMIC);
1154 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1159 static void ti_bulk_in_callback(struct urb *urb)
1161 struct ti_port *tport = urb->context;
1162 struct usb_serial_port *port = tport->tp_port;
1163 struct device *dev = &urb->dev->dev;
1164 int status = urb->status;
1166 struct tty_struct *tty;
1168 dbg("%s", __func__);
1176 dbg("%s - urb shutting down, %d", __func__, status);
1177 tport->tp_tdev->td_urb_error = 1;
1178 wake_up_interruptible(&tport->tp_write_wait);
1181 dev_err(dev, "%s - nonzero urb status, %d\n",
1183 tport->tp_tdev->td_urb_error = 1;
1184 wake_up_interruptible(&tport->tp_write_wait);
1187 if (status == -EPIPE)
1191 dev_err(dev, "%s - stopping read!\n", __func__);
1195 tty = tty_port_tty_get(&port->port);
1197 if (urb->actual_length) {
1198 usb_serial_debug_data(debug, dev, __func__,
1199 urb->actual_length, urb->transfer_buffer);
1201 if (!tport->tp_is_open)
1202 dbg("%s - port closed, dropping data",
1205 ti_recv(&urb->dev->dev, tty,
1206 urb->transfer_buffer,
1207 urb->actual_length);
1208 spin_lock(&tport->tp_lock);
1209 tport->tp_icount.rx += urb->actual_length;
1210 spin_unlock(&tport->tp_lock);
1216 /* continue to read unless stopping */
1217 spin_lock(&tport->tp_lock);
1218 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1219 retval = usb_submit_urb(urb, GFP_ATOMIC);
1220 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
1221 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1223 spin_unlock(&tport->tp_lock);
1225 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1230 static void ti_bulk_out_callback(struct urb *urb)
1232 struct ti_port *tport = urb->context;
1233 struct usb_serial_port *port = tport->tp_port;
1234 int status = urb->status;
1236 dbg("%s - port %d", __func__, port->number);
1238 tport->tp_write_urb_in_use = 0;
1246 dbg("%s - urb shutting down, %d", __func__, status);
1247 tport->tp_tdev->td_urb_error = 1;
1248 wake_up_interruptible(&tport->tp_write_wait);
1251 dev_err_console(port, "%s - nonzero urb status, %d\n",
1253 tport->tp_tdev->td_urb_error = 1;
1254 wake_up_interruptible(&tport->tp_write_wait);
1257 /* send any buffered data */
1262 static void ti_recv(struct device *dev, struct tty_struct *tty,
1263 unsigned char *data, int length)
1268 cnt = tty_insert_flip_string(tty, data, length);
1270 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1271 __func__, length - cnt);
1275 tty_flip_buffer_push(tty);
1278 } while (length > 0);
1283 static void ti_send(struct ti_port *tport)
1286 struct usb_serial_port *port = tport->tp_port;
1287 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1288 unsigned long flags;
1291 dbg("%s - port %d", __func__, port->number);
1293 spin_lock_irqsave(&tport->tp_lock, flags);
1295 if (tport->tp_write_urb_in_use)
1298 count = kfifo_out(&tport->write_fifo,
1299 port->write_urb->transfer_buffer,
1300 port->bulk_out_size);
1305 tport->tp_write_urb_in_use = 1;
1307 spin_unlock_irqrestore(&tport->tp_lock, flags);
1309 usb_serial_debug_data(debug, &port->dev, __func__, count,
1310 port->write_urb->transfer_buffer);
1312 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1313 usb_sndbulkpipe(port->serial->dev,
1314 port->bulk_out_endpointAddress),
1315 port->write_urb->transfer_buffer, count,
1316 ti_bulk_out_callback, tport);
1318 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1320 dev_err_console(port, "%s - submit write urb failed, %d\n",
1322 tport->tp_write_urb_in_use = 0;
1323 /* TODO: reschedule ti_send */
1325 spin_lock_irqsave(&tport->tp_lock, flags);
1326 tport->tp_icount.tx += count;
1327 spin_unlock_irqrestore(&tport->tp_lock, flags);
1330 /* more room in the buffer for new writes, wakeup */
1334 wake_up_interruptible(&tport->tp_write_wait);
1337 spin_unlock_irqrestore(&tport->tp_lock, flags);
1343 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1345 unsigned long flags;
1348 status = ti_write_byte(tport->tp_tdev,
1349 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1350 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1352 spin_lock_irqsave(&tport->tp_lock, flags);
1354 tport->tp_shadow_mcr = mcr;
1355 spin_unlock_irqrestore(&tport->tp_lock, flags);
1361 static int ti_get_lsr(struct ti_port *tport)
1364 struct ti_device *tdev = tport->tp_tdev;
1365 struct usb_serial_port *port = tport->tp_port;
1366 int port_number = port->number - port->serial->minor;
1367 struct ti_port_status *data;
1369 dbg("%s - port %d", __func__, port->number);
1371 size = sizeof(struct ti_port_status);
1372 data = kmalloc(size, GFP_KERNEL);
1374 dev_err(&port->dev, "%s - out of memory\n", __func__);
1378 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1379 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1382 "%s - get port status command failed, %d\n",
1387 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1389 tport->tp_lsr = data->bLSR;
1397 static int ti_get_serial_info(struct ti_port *tport,
1398 struct serial_struct __user *ret_arg)
1400 struct usb_serial_port *port = tport->tp_port;
1401 struct serial_struct ret_serial;
1406 memset(&ret_serial, 0, sizeof(ret_serial));
1408 ret_serial.type = PORT_16550A;
1409 ret_serial.line = port->serial->minor;
1410 ret_serial.port = port->number - port->serial->minor;
1411 ret_serial.flags = tport->tp_flags;
1412 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1413 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1414 ret_serial.closing_wait = tport->tp_closing_wait;
1416 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1423 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1424 struct serial_struct __user *new_arg)
1426 struct serial_struct new_serial;
1428 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1431 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1432 tport->tp_closing_wait = new_serial.closing_wait;
1438 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1440 struct async_icount *icount;
1441 struct tty_struct *tty;
1442 unsigned long flags;
1444 dbg("%s - msr 0x%02X", __func__, msr);
1446 if (msr & TI_MSR_DELTA_MASK) {
1447 spin_lock_irqsave(&tport->tp_lock, flags);
1448 icount = &tport->tp_icount;
1449 if (msr & TI_MSR_DELTA_CTS)
1451 if (msr & TI_MSR_DELTA_DSR)
1453 if (msr & TI_MSR_DELTA_CD)
1455 if (msr & TI_MSR_DELTA_RI)
1457 wake_up_interruptible(&tport->tp_msr_wait);
1458 spin_unlock_irqrestore(&tport->tp_lock, flags);
1461 tport->tp_msr = msr & TI_MSR_MASK;
1463 /* handle CTS flow control */
1464 tty = tty_port_tty_get(&tport->tp_port->port);
1465 if (tty && C_CRTSCTS(tty)) {
1466 if (msr & TI_MSR_CTS) {
1467 tty->hw_stopped = 0;
1470 tty->hw_stopped = 1;
1477 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1479 struct ti_device *tdev = tport->tp_tdev;
1480 struct usb_serial_port *port = tport->tp_port;
1483 dbg("%s - port %d", __func__, port->number);
1485 spin_lock_irq(&tport->tp_lock);
1487 /* wait for data to drain from the buffer */
1488 tdev->td_urb_error = 0;
1489 init_waitqueue_entry(&wait, current);
1490 add_wait_queue(&tport->tp_write_wait, &wait);
1492 set_current_state(TASK_INTERRUPTIBLE);
1493 if (kfifo_len(&tport->write_fifo) == 0
1494 || timeout == 0 || signal_pending(current)
1495 || tdev->td_urb_error
1496 || port->serial->disconnected) /* disconnect */
1498 spin_unlock_irq(&tport->tp_lock);
1499 timeout = schedule_timeout(timeout);
1500 spin_lock_irq(&tport->tp_lock);
1502 set_current_state(TASK_RUNNING);
1503 remove_wait_queue(&tport->tp_write_wait, &wait);
1505 /* flush any remaining data in the buffer */
1507 kfifo_reset_out(&tport->write_fifo);
1509 spin_unlock_irq(&tport->tp_lock);
1511 mutex_lock(&port->serial->disc_mutex);
1512 /* wait for data to drain from the device */
1513 /* wait for empty tx register, plus 20 ms */
1515 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1516 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1517 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1518 && !port->serial->disconnected) {
1519 if (ti_get_lsr(tport))
1521 mutex_unlock(&port->serial->disc_mutex);
1522 msleep_interruptible(20);
1523 mutex_lock(&port->serial->disc_mutex);
1525 mutex_unlock(&port->serial->disc_mutex);
1529 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1531 unsigned long flags;
1533 spin_lock_irqsave(&tport->tp_lock, flags);
1535 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1536 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1538 spin_unlock_irqrestore(&tport->tp_lock, flags);
1542 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1546 unsigned long flags;
1548 spin_lock_irqsave(&tport->tp_lock, flags);
1550 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1551 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1552 urb = tport->tp_port->read_urb;
1553 spin_unlock_irqrestore(&tport->tp_lock, flags);
1554 urb->context = tport;
1555 status = usb_submit_urb(urb, GFP_KERNEL);
1557 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1558 spin_unlock_irqrestore(&tport->tp_lock, flags);
1565 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1566 __u16 moduleid, __u16 value, __u8 *data, int size)
1570 status = usb_control_msg(tdev->td_serial->dev,
1571 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1572 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1573 value, moduleid, data, size, 1000);
1585 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1586 __u16 moduleid, __u16 value, __u8 *data, int size)
1590 status = usb_control_msg(tdev->td_serial->dev,
1591 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1592 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1593 value, moduleid, data, size, 1000);
1605 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1606 __u8 mask, __u8 byte)
1610 struct ti_write_data_bytes *data;
1611 struct device *dev = &tdev->td_serial->dev->dev;
1613 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1614 __func__, addr, mask, byte);
1616 size = sizeof(struct ti_write_data_bytes) + 2;
1617 data = kmalloc(size, GFP_KERNEL);
1619 dev_err(dev, "%s - out of memory\n", __func__);
1623 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1624 data->bDataType = TI_RW_DATA_BYTE;
1625 data->bDataCounter = 1;
1626 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1627 data->wBaseAddrLo = cpu_to_be16(addr);
1628 data->bData[0] = mask;
1629 data->bData[1] = byte;
1631 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1632 (__u8 *)data, size);
1635 dev_err(dev, "%s - failed, %d\n", __func__, status);
1642 static int ti_do_download(struct usb_device *dev, int pipe,
1643 u8 *buffer, int size)
1648 struct ti_firmware_header *header;
1652 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1653 cs = (__u8)(cs + buffer[pos]);
1655 header = (struct ti_firmware_header *)buffer;
1656 header->wLength = cpu_to_le16((__u16)(size
1657 - sizeof(struct ti_firmware_header)));
1658 header->bCheckSum = cs;
1660 dbg("%s - downloading firmware", __func__);
1661 for (pos = 0; pos < size; pos += done) {
1662 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1663 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1671 static int ti_download_firmware(struct ti_device *tdev)
1676 struct usb_device *dev = tdev->td_serial->dev;
1677 unsigned int pipe = usb_sndbulkpipe(dev,
1678 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1679 const struct firmware *fw_p;
1682 dbg("%s\n", __func__);
1683 /* try ID specific firmware first, then try generic firmware */
1684 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1685 dev->descriptor.idProduct);
1686 if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) {
1688 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1689 switch (dev->descriptor.idProduct) {
1690 case MTS_CDMA_PRODUCT_ID:
1691 strcpy(buf, "mts_cdma.fw");
1693 case MTS_GSM_PRODUCT_ID:
1694 strcpy(buf, "mts_gsm.fw");
1696 case MTS_EDGE_PRODUCT_ID:
1697 strcpy(buf, "mts_edge.fw");
1699 case MTS_MT9234MU_PRODUCT_ID:
1700 strcpy(buf, "mts_mt9234mu.fw");
1702 case MTS_MT9234ZBA_PRODUCT_ID:
1703 strcpy(buf, "mts_mt9234zba.fw");
1705 case MTS_MT9234ZBAOLD_PRODUCT_ID:
1706 strcpy(buf, "mts_mt9234zba.fw");
1709 if (buf[0] == '\0') {
1710 if (tdev->td_is_3410)
1711 strcpy(buf, "ti_3410.fw");
1713 strcpy(buf, "ti_5052.fw");
1715 status = request_firmware(&fw_p, buf, &dev->dev);
1718 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1721 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1722 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1723 release_firmware(fw_p);
1727 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1728 buffer = kmalloc(buffer_size, GFP_KERNEL);
1730 memcpy(buffer, fw_p->data, fw_p->size);
1731 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1732 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1735 dbg("%s ENOMEM\n", __func__);
1738 release_firmware(fw_p);
1740 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1745 dbg("%s - download successful", __func__);