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>.
20 * This driver needs this hotplug script in /etc/hotplug/usb/ti_usb_3410_5052
21 * or in /etc/hotplug.d/usb/ti_usb_3410_5052.hotplug to set the device
29 * if [[ "$ACTION" != "add" ]]
34 * CONFIG_PATH=/sys${DEVPATH%/?*}/bConfigurationValue
36 * if [[ 0`cat $CONFIG_PATH` -ne $BOOT_CONFIG ]]
41 * PRODUCT=${PRODUCT%/?*} # delete version
42 * VENDOR_ID=`printf "%d" 0x${PRODUCT%/?*}`
43 * PRODUCT_ID=`printf "%d" 0x${PRODUCT#*?/}`
45 * PARAM_PATH=/sys/module/ti_usb_3410_5052/parameters
62 * if (scan $VENDOR_ID 1105 `cat $PARAM_PATH/vendor_3410` &&
63 * scan $PRODUCT_ID 13328 `cat $PARAM_PATH/product_3410`) ||
64 * (scan $VENDOR_ID 1105 `cat $PARAM_PATH/vendor_5052` &&
65 * scan $PRODUCT_ID 20562 20818 20570 20575 `cat $PARAM_PATH/product_5052`)
67 * echo $ACTIVE_CONFIG > $CONFIG_PATH
71 #include <linux/config.h>
72 #include <linux/kernel.h>
73 #include <linux/errno.h>
74 #include <linux/init.h>
75 #include <linux/slab.h>
76 #include <linux/tty.h>
77 #include <linux/tty_driver.h>
78 #include <linux/tty_flip.h>
79 #include <linux/module.h>
80 #include <linux/spinlock.h>
81 #include <linux/ioctl.h>
82 #include <linux/serial.h>
83 #include <linux/circ_buf.h>
84 #include <asm/uaccess.h>
85 #include <asm/semaphore.h>
86 #include <linux/usb.h>
88 #include "usb-serial.h"
89 #include "ti_usb_3410_5052.h"
90 #include "ti_fw_3410.h" /* firmware image for 3410 */
91 #include "ti_fw_5052.h" /* firmware image for 5052 */
96 #define TI_DRIVER_VERSION "v0.9"
97 #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
98 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
100 #define TI_FIRMWARE_BUF_SIZE 16284
102 #define TI_WRITE_BUF_SIZE 1024
104 #define TI_TRANSFER_TIMEOUT 2
106 #define TI_DEFAULT_LOW_LATENCY 0
107 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
109 /* supported setserial flags */
110 #define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
112 /* read urb states */
113 #define TI_READ_URB_RUNNING 0
114 #define TI_READ_URB_STOPPING 1
115 #define TI_READ_URB_STOPPED 2
117 #define TI_EXTRA_VID_PID_COUNT 5
127 __u8 tp_uart_mode; /* 232 or 485 modes */
128 unsigned int tp_uart_base_addr;
130 int tp_closing_wait;/* in .01 secs */
131 struct async_icount tp_icount;
132 wait_queue_head_t tp_msr_wait; /* wait for msr change */
133 wait_queue_head_t tp_write_wait;
134 struct ti_device *tp_tdev;
135 struct usb_serial_port *tp_port;
137 int tp_read_urb_state;
138 int tp_write_urb_in_use;
139 struct circ_buf *tp_write_buf;
143 struct semaphore td_open_close_sem;
144 int td_open_port_count;
145 struct usb_serial *td_serial;
151 /* Function Declarations */
153 static int ti_startup(struct usb_serial *serial);
154 static void ti_shutdown(struct usb_serial *serial);
155 static int ti_open(struct usb_serial_port *port, struct file *file);
156 static void ti_close(struct usb_serial_port *port, struct file *file);
157 static int ti_write(struct usb_serial_port *port, const unsigned char *data,
159 static int ti_write_room(struct usb_serial_port *port);
160 static int ti_chars_in_buffer(struct usb_serial_port *port);
161 static void ti_throttle(struct usb_serial_port *port);
162 static void ti_unthrottle(struct usb_serial_port *port);
163 static int ti_ioctl(struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
164 static void ti_set_termios(struct usb_serial_port *port,
165 struct termios *old_termios);
166 static int ti_tiocmget(struct usb_serial_port *port, struct file *file);
167 static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
168 unsigned int set, unsigned int clear);
169 static void ti_break(struct usb_serial_port *port, int break_state);
170 static void ti_interrupt_callback(struct urb *urb, struct pt_regs *regs);
171 static void ti_bulk_in_callback(struct urb *urb, struct pt_regs *regs);
172 static void ti_bulk_out_callback(struct urb *urb, struct pt_regs *regs);
174 static void ti_recv(struct device *dev, struct tty_struct *tty,
175 unsigned char *data, int length);
176 static void ti_send(struct ti_port *tport);
177 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
178 static int ti_get_lsr(struct ti_port *tport);
179 static int ti_get_serial_info(struct ti_port *tport,
180 struct serial_struct __user *ret_arg);
181 static int ti_set_serial_info(struct ti_port *tport,
182 struct serial_struct __user *new_arg);
183 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
185 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
187 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
188 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
190 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
191 __u16 moduleid, __u16 value, __u8 *data, int size);
192 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
193 __u16 moduleid, __u16 value, __u8 *data, int size);
195 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
196 __u8 mask, __u8 byte);
198 static int ti_download_firmware(struct ti_device *tdev,
199 unsigned char *firmware, unsigned int firmware_size);
201 /* circular buffer */
202 static struct circ_buf *ti_buf_alloc(void);
203 static void ti_buf_free(struct circ_buf *cb);
204 static void ti_buf_clear(struct circ_buf *cb);
205 static int ti_buf_data_avail(struct circ_buf *cb);
206 static int ti_buf_space_avail(struct circ_buf *cb);
207 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
208 static int ti_buf_get(struct circ_buf *cb, char *buf, int count);
213 /* module parameters */
215 static int low_latency = TI_DEFAULT_LOW_LATENCY;
216 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
217 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
218 static int vendor_3410_count;
219 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
220 static int product_3410_count;
221 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
222 static int vendor_5052_count;
223 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
224 static int product_5052_count;
226 /* supported devices */
227 /* the array dimension is the number of default entries plus */
228 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
230 static struct usb_device_id ti_id_table_3410[1+TI_EXTRA_VID_PID_COUNT+1] = {
231 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
234 static struct usb_device_id ti_id_table_5052[4+TI_EXTRA_VID_PID_COUNT+1] = {
235 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
236 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
237 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
238 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
241 static struct usb_device_id ti_id_table_combined[] = {
242 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
243 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
244 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
245 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
246 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
250 static struct usb_driver ti_usb_driver = {
251 .name = "ti_usb_3410_5052",
252 .probe = usb_serial_probe,
253 .disconnect = usb_serial_disconnect,
254 .id_table = ti_id_table_combined,
258 static struct usb_serial_driver ti_1port_device = {
260 .owner = THIS_MODULE,
261 .name = "ti_usb_3410_5052_1",
263 .description = "TI USB 3410 1 port adapter",
264 .id_table = ti_id_table_3410,
265 .num_interrupt_in = 1,
269 .attach = ti_startup,
270 .shutdown = ti_shutdown,
274 .write_room = ti_write_room,
275 .chars_in_buffer = ti_chars_in_buffer,
276 .throttle = ti_throttle,
277 .unthrottle = ti_unthrottle,
279 .set_termios = ti_set_termios,
280 .tiocmget = ti_tiocmget,
281 .tiocmset = ti_tiocmset,
282 .break_ctl = ti_break,
283 .read_int_callback = ti_interrupt_callback,
284 .read_bulk_callback = ti_bulk_in_callback,
285 .write_bulk_callback = ti_bulk_out_callback,
288 static struct usb_serial_driver ti_2port_device = {
290 .owner = THIS_MODULE,
291 .name = "ti_usb_3410_5052_2",
293 .description = "TI USB 5052 2 port adapter",
294 .id_table = ti_id_table_5052,
295 .num_interrupt_in = 1,
299 .attach = ti_startup,
300 .shutdown = ti_shutdown,
304 .write_room = ti_write_room,
305 .chars_in_buffer = ti_chars_in_buffer,
306 .throttle = ti_throttle,
307 .unthrottle = ti_unthrottle,
309 .set_termios = ti_set_termios,
310 .tiocmget = ti_tiocmget,
311 .tiocmset = ti_tiocmset,
312 .break_ctl = ti_break,
313 .read_int_callback = ti_interrupt_callback,
314 .read_bulk_callback = ti_bulk_in_callback,
315 .write_bulk_callback = ti_bulk_out_callback,
321 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
322 MODULE_DESCRIPTION(TI_DRIVER_DESC);
323 MODULE_VERSION(TI_DRIVER_VERSION);
324 MODULE_LICENSE("GPL");
326 module_param(debug, bool, S_IRUGO | S_IWUSR);
327 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
329 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
330 MODULE_PARM_DESC(low_latency, "TTY low_latency flag, 0=off, 1=on, default is off");
332 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
333 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain in close, in .01 secs, default is 4000");
335 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
336 MODULE_PARM_DESC(vendor_3410, "Vendor ids for 3410 based devices, 1-5 short integers");
337 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
338 MODULE_PARM_DESC(product_3410, "Product ids for 3410 based devices, 1-5 short integers");
339 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
340 MODULE_PARM_DESC(vendor_5052, "Vendor ids for 5052 based devices, 1-5 short integers");
341 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
342 MODULE_PARM_DESC(product_5052, "Product ids for 5052 based devices, 1-5 short integers");
344 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
349 static int __init ti_init(void)
354 /* insert extra vendor and product ids */
355 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
356 for (i=0; i<min(vendor_3410_count,product_3410_count); i++,j++) {
357 ti_id_table_3410[j].idVendor = vendor_3410[i];
358 ti_id_table_3410[j].idProduct = product_3410[i];
359 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
361 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
362 for (i=0; i<min(vendor_5052_count,product_5052_count); i++,j++) {
363 ti_id_table_5052[j].idVendor = vendor_5052[i];
364 ti_id_table_5052[j].idProduct = product_5052[i];
365 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
368 ret = usb_serial_register(&ti_1port_device);
371 ret = usb_serial_register(&ti_2port_device);
375 ret = usb_register(&ti_usb_driver);
379 info(TI_DRIVER_DESC " " TI_DRIVER_VERSION);
384 usb_serial_deregister(&ti_2port_device);
386 usb_serial_deregister(&ti_1port_device);
392 static void __exit ti_exit(void)
394 usb_serial_deregister(&ti_1port_device);
395 usb_serial_deregister(&ti_2port_device);
396 usb_deregister(&ti_usb_driver);
400 module_init(ti_init);
401 module_exit(ti_exit);
404 static int ti_startup(struct usb_serial *serial)
406 struct ti_device *tdev;
407 struct ti_port *tport;
408 struct usb_device *dev = serial->dev;
413 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
414 __FUNCTION__, le16_to_cpu(dev->descriptor.idProduct),
415 dev->descriptor.bNumConfigurations,
416 dev->actconfig->desc.bConfigurationValue);
418 /* create device structure */
419 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
421 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
424 sema_init(&tdev->td_open_close_sem, 1);
425 tdev->td_serial = serial;
426 usb_set_serial_data(serial, tdev);
428 /* determine device type */
429 if (usb_match_id(serial->interface, ti_id_table_3410))
430 tdev->td_is_3410 = 1;
431 dbg("%s - device type is %s", __FUNCTION__, tdev->td_is_3410 ? "3410" : "5052");
433 /* if we have only 1 configuration, download firmware */
434 if (dev->descriptor.bNumConfigurations == 1) {
436 if (tdev->td_is_3410)
437 status = ti_download_firmware(tdev, ti_fw_3410,
440 status = ti_download_firmware(tdev, ti_fw_5052,
445 /* 3410 must be reset, 5052 resets itself */
446 if (tdev->td_is_3410) {
447 msleep_interruptible(100);
448 usb_reset_device(dev);
455 /* the second configuration must be set (in sysfs by hotplug script) */
456 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
461 /* set up port structures */
462 for (i = 0; i < serial->num_ports; ++i) {
463 tport = kmalloc(sizeof(struct ti_port), GFP_KERNEL);
465 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
469 memset(tport, 0, sizeof(struct ti_port));
470 spin_lock_init(&tport->tp_lock);
471 tport->tp_uart_base_addr = (i == 0 ? TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
472 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
473 tport->tp_closing_wait = closing_wait;
474 init_waitqueue_head(&tport->tp_msr_wait);
475 init_waitqueue_head(&tport->tp_write_wait);
476 tport->tp_write_buf = ti_buf_alloc();
477 if (tport->tp_write_buf == NULL) {
478 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
483 tport->tp_port = serial->port[i];
484 tport->tp_tdev = tdev;
485 usb_set_serial_port_data(serial->port[i], tport);
486 tport->tp_uart_mode = 0; /* default is RS232 */
492 for (--i; i>=0; --i) {
493 tport = usb_get_serial_port_data(serial->port[i]);
494 ti_buf_free(tport->tp_write_buf);
496 usb_set_serial_port_data(serial->port[i], NULL);
500 usb_set_serial_data(serial, NULL);
505 static void ti_shutdown(struct usb_serial *serial)
508 struct ti_device *tdev = usb_get_serial_data(serial);
509 struct ti_port *tport;
511 dbg("%s", __FUNCTION__);
513 for (i=0; i < serial->num_ports; ++i) {
514 tport = usb_get_serial_port_data(serial->port[i]);
516 ti_buf_free(tport->tp_write_buf);
518 usb_set_serial_port_data(serial->port[i], NULL);
523 usb_set_serial_data(serial, NULL);
527 static int ti_open(struct usb_serial_port *port, struct file *file)
529 struct ti_port *tport = usb_get_serial_port_data(port);
530 struct ti_device *tdev;
531 struct usb_device *dev;
535 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
536 TI_PIPE_TIMEOUT_ENABLE |
537 (TI_TRANSFER_TIMEOUT << 2));
539 dbg("%s - port %d", __FUNCTION__, port->number);
544 dev = port->serial->dev;
545 tdev = tport->tp_tdev;
547 /* only one open on any port on a device at a time */
548 if (down_interruptible(&tdev->td_open_close_sem))
552 port->tty->low_latency =
553 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
555 port_number = port->number - port->serial->minor;
557 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
560 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
562 /* start interrupt urb the first time a port is opened on this device */
563 if (tdev->td_open_port_count == 0) {
564 dbg("%s - start interrupt in urb", __FUNCTION__);
565 urb = tdev->td_serial->port[0]->interrupt_in_urb;
567 dev_err(&port->dev, "%s - no interrupt urb\n", __FUNCTION__);
571 urb->complete = ti_interrupt_callback;
574 status = usb_submit_urb(urb, GFP_KERNEL);
576 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __FUNCTION__, status);
581 ti_set_termios(port, NULL);
583 dbg("%s - sending TI_OPEN_PORT", __FUNCTION__);
584 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
585 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
587 dev_err(&port->dev, "%s - cannot send open command, %d\n", __FUNCTION__, status);
591 dbg("%s - sending TI_START_PORT", __FUNCTION__);
592 status = ti_command_out_sync(tdev, TI_START_PORT,
593 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
595 dev_err(&port->dev, "%s - cannot send start command, %d\n", __FUNCTION__, status);
599 dbg("%s - sending TI_PURGE_PORT", __FUNCTION__);
600 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
601 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
603 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", __FUNCTION__, status);
606 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
607 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
609 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", __FUNCTION__, status);
613 /* reset the data toggle on the bulk endpoints to work around bug in
614 * host controllers where things get out of sync some times */
615 usb_clear_halt(dev, port->write_urb->pipe);
616 usb_clear_halt(dev, port->read_urb->pipe);
618 ti_set_termios(port, NULL);
620 dbg("%s - sending TI_OPEN_PORT (2)", __FUNCTION__);
621 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
622 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
624 dev_err(&port->dev, "%s - cannot send open command (2), %d\n", __FUNCTION__, status);
628 dbg("%s - sending TI_START_PORT (2)", __FUNCTION__);
629 status = ti_command_out_sync(tdev, TI_START_PORT,
630 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
632 dev_err(&port->dev, "%s - cannot send start command (2), %d\n", __FUNCTION__, status);
637 dbg("%s - start read urb", __FUNCTION__);
638 urb = port->read_urb;
640 dev_err(&port->dev, "%s - no read urb\n", __FUNCTION__);
644 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
645 urb->complete = ti_bulk_in_callback;
646 urb->context = tport;
648 status = usb_submit_urb(urb, GFP_KERNEL);
650 dev_err(&port->dev, "%s - submit read urb failed, %d\n", __FUNCTION__, status);
654 tport->tp_is_open = 1;
655 ++tdev->td_open_port_count;
660 if (tdev->td_open_port_count == 0)
661 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
663 up(&tdev->td_open_close_sem);
664 dbg("%s - exit %d", __FUNCTION__, status);
669 static void ti_close(struct usb_serial_port *port, struct file *file)
671 struct ti_device *tdev;
672 struct ti_port *tport;
677 dbg("%s - port %d", __FUNCTION__, port->number);
679 tdev = usb_get_serial_data(port->serial);
680 tport = usb_get_serial_port_data(port);
681 if (tdev == NULL || tport == NULL)
684 tport->tp_is_open = 0;
686 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
688 usb_kill_urb(port->read_urb);
689 usb_kill_urb(port->write_urb);
690 tport->tp_write_urb_in_use = 0;
692 port_number = port->number - port->serial->minor;
694 dbg("%s - sending TI_CLOSE_PORT", __FUNCTION__);
695 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
696 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
698 dev_err(&port->dev, "%s - cannot send close port command, %d\n" , __FUNCTION__, status);
700 /* if down is interrupted, continue anyway */
701 do_up = !down_interruptible(&tdev->td_open_close_sem);
702 --tport->tp_tdev->td_open_port_count;
703 if (tport->tp_tdev->td_open_port_count <= 0) {
704 /* last port is closed, shut down interrupt urb */
705 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
706 tport->tp_tdev->td_open_port_count = 0;
709 up(&tdev->td_open_close_sem);
711 dbg("%s - exit", __FUNCTION__);
715 static int ti_write(struct usb_serial_port *port, const unsigned char *data,
718 struct ti_port *tport = usb_get_serial_port_data(port);
721 dbg("%s - port %d", __FUNCTION__, port->number);
724 dbg("%s - write request of 0 bytes", __FUNCTION__);
728 if (tport == NULL || !tport->tp_is_open)
731 spin_lock_irqsave(&tport->tp_lock, flags);
732 count = ti_buf_put(tport->tp_write_buf, data, count);
733 spin_unlock_irqrestore(&tport->tp_lock, flags);
741 static int ti_write_room(struct usb_serial_port *port)
743 struct ti_port *tport = usb_get_serial_port_data(port);
747 dbg("%s - port %d", __FUNCTION__, port->number);
752 spin_lock_irqsave(&tport->tp_lock, flags);
753 room = ti_buf_space_avail(tport->tp_write_buf);
754 spin_unlock_irqrestore(&tport->tp_lock, flags);
756 dbg("%s - returns %d", __FUNCTION__, room);
761 static int ti_chars_in_buffer(struct usb_serial_port *port)
763 struct ti_port *tport = usb_get_serial_port_data(port);
767 dbg("%s - port %d", __FUNCTION__, port->number);
772 spin_lock_irqsave(&tport->tp_lock, flags);
773 chars = ti_buf_data_avail(tport->tp_write_buf);
774 spin_unlock_irqrestore(&tport->tp_lock, flags);
776 dbg("%s - returns %d", __FUNCTION__, chars);
781 static void ti_throttle(struct usb_serial_port *port)
783 struct ti_port *tport = usb_get_serial_port_data(port);
784 struct tty_struct *tty;
786 dbg("%s - port %d", __FUNCTION__, port->number);
793 dbg("%s - no tty", __FUNCTION__);
797 if (I_IXOFF(tty) || C_CRTSCTS(tty))
798 ti_stop_read(tport, tty);
803 static void ti_unthrottle(struct usb_serial_port *port)
805 struct ti_port *tport = usb_get_serial_port_data(port);
806 struct tty_struct *tty;
809 dbg("%s - port %d", __FUNCTION__, port->number);
816 dbg("%s - no tty", __FUNCTION__);
820 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
821 status = ti_restart_read(tport, tty);
823 dev_err(&port->dev, "%s - cannot restart read, %d\n", __FUNCTION__, status);
828 static int ti_ioctl(struct usb_serial_port *port, struct file *file,
829 unsigned int cmd, unsigned long arg)
831 struct ti_port *tport = usb_get_serial_port_data(port);
832 struct async_icount cnow;
833 struct async_icount cprev;
835 dbg("%s - port %d, cmd = 0x%04X", __FUNCTION__, port->number, cmd);
842 dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
843 return ti_get_serial_info(tport, (struct serial_struct __user *)arg);
847 dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
848 return ti_set_serial_info(tport, (struct serial_struct __user *)arg);
852 dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
853 cprev = tport->tp_icount;
855 interruptible_sleep_on(&tport->tp_msr_wait);
856 if (signal_pending(current))
858 cnow = tport->tp_icount;
859 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
860 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
861 return -EIO; /* no change => error */
862 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
863 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
864 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
865 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
873 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, tport->tp_icount.rx, tport->tp_icount.tx);
874 if (copy_to_user((void __user *)arg, &tport->tp_icount, sizeof(tport->tp_icount)))
883 static void ti_set_termios(struct usb_serial_port *port,
884 struct termios *old_termios)
886 struct ti_port *tport = usb_get_serial_port_data(port);
887 struct tty_struct *tty = port->tty;
888 struct ti_uart_config *config;
889 tcflag_t cflag,iflag;
892 int port_number = port->number - port->serial->minor;
895 dbg("%s - port %d", __FUNCTION__, port->number);
897 if (!tty || !tty->termios) {
898 dbg("%s - no tty or termios", __FUNCTION__);
902 cflag = tty->termios->c_cflag;
903 iflag = tty->termios->c_iflag;
905 if (old_termios && cflag == old_termios->c_cflag
906 && iflag == old_termios->c_iflag) {
907 dbg("%s - nothing to change", __FUNCTION__);
911 dbg("%s - clfag %08x, iflag %08x", __FUNCTION__, cflag, iflag);
914 dbg("%s - old clfag %08x, old iflag %08x", __FUNCTION__, old_termios->c_cflag, old_termios->c_iflag);
919 config = kmalloc(sizeof(*config), GFP_KERNEL);
921 dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
927 /* these flags must be set */
928 config->wFlags |= TI_UART_ENABLE_MS_INTS;
929 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
930 config->bUartMode = (__u8)(tport->tp_uart_mode);
932 switch (cflag & CSIZE) {
934 config->bDataBits = TI_UART_5_DATA_BITS;
937 config->bDataBits = TI_UART_6_DATA_BITS;
940 config->bDataBits = TI_UART_7_DATA_BITS;
944 config->bDataBits = TI_UART_8_DATA_BITS;
948 if (cflag & PARENB) {
949 if (cflag & PARODD) {
950 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
951 config->bParity = TI_UART_ODD_PARITY;
953 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
954 config->bParity = TI_UART_EVEN_PARITY;
957 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
958 config->bParity = TI_UART_NO_PARITY;
962 config->bStopBits = TI_UART_2_STOP_BITS;
964 config->bStopBits = TI_UART_1_STOP_BITS;
966 if (cflag & CRTSCTS) {
967 /* RTS flow control must be off to drop RTS for baud rate B0 */
968 if ((cflag & CBAUD) != B0)
969 config->wFlags |= TI_UART_ENABLE_RTS_IN;
970 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
973 ti_restart_read(tport, tty);
976 if (I_IXOFF(tty) || I_IXON(tty)) {
977 config->cXon = START_CHAR(tty);
978 config->cXoff = STOP_CHAR(tty);
981 config->wFlags |= TI_UART_ENABLE_X_IN;
983 ti_restart_read(tport, tty);
986 config->wFlags |= TI_UART_ENABLE_X_OUT;
989 baud = tty_get_baud_rate(tty);
990 if (!baud) baud = 9600;
991 if (tport->tp_tdev->td_is_3410)
992 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
994 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
996 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
997 __FUNCTION__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
999 cpu_to_be16s(&config->wBaudRate);
1000 cpu_to_be16s(&config->wFlags);
1002 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
1003 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
1006 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", __FUNCTION__, port_number, status);
1008 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
1009 mcr = tport->tp_shadow_mcr;
1010 /* if baud rate is B0, clear RTS and DTR */
1011 if ((cflag & CBAUD) == B0)
1012 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1013 status = ti_set_mcr(tport, mcr);
1015 dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n", __FUNCTION__, port_number, status);
1021 static int ti_tiocmget(struct usb_serial_port *port, struct file *file)
1023 struct ti_port *tport = usb_get_serial_port_data(port);
1024 unsigned int result;
1028 dbg("%s - port %d", __FUNCTION__, port->number);
1033 msr = tport->tp_msr;
1034 mcr = tport->tp_shadow_mcr;
1036 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1037 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1038 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1039 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1040 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1041 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1042 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1044 dbg("%s - 0x%04X", __FUNCTION__, result);
1050 static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
1051 unsigned int set, unsigned int clear)
1053 struct ti_port *tport = usb_get_serial_port_data(port);
1056 dbg("%s - port %d", __FUNCTION__, port->number);
1061 mcr = tport->tp_shadow_mcr;
1063 if (set & TIOCM_RTS)
1065 if (set & TIOCM_DTR)
1067 if (set & TIOCM_LOOP)
1070 if (clear & TIOCM_RTS)
1072 if (clear & TIOCM_DTR)
1074 if (clear & TIOCM_LOOP)
1075 mcr &= ~TI_MCR_LOOP;
1077 return ti_set_mcr(tport, mcr);
1081 static void ti_break(struct usb_serial_port *port, int break_state)
1083 struct ti_port *tport = usb_get_serial_port_data(port);
1086 dbg("%s - state = %d", __FUNCTION__, break_state);
1091 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1093 status = ti_write_byte(tport->tp_tdev,
1094 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1095 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1098 dbg("%s - error setting break, %d", __FUNCTION__, status);
1102 static void ti_interrupt_callback(struct urb *urb, struct pt_regs *regs)
1104 struct ti_device *tdev = (struct ti_device *)urb->context;
1105 struct usb_serial_port *port;
1106 struct usb_serial *serial = tdev->td_serial;
1107 struct ti_port *tport;
1108 struct device *dev = &urb->dev->dev;
1109 unsigned char *data = urb->transfer_buffer;
1110 int length = urb->actual_length;
1116 dbg("%s", __FUNCTION__);
1118 switch (urb->status) {
1124 dbg("%s - urb shutting down, %d", __FUNCTION__, urb->status);
1125 tdev->td_urb_error = 1;
1128 dev_err(dev, "%s - nonzero urb status, %d\n", __FUNCTION__, urb->status);
1129 tdev->td_urb_error = 1;
1134 dbg("%s - bad packet size, %d", __FUNCTION__, length);
1138 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1139 dev_err(dev, "%s - hardware error, %d\n", __FUNCTION__, data[1]);
1143 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1144 function = TI_GET_FUNC_FROM_CODE(data[0]);
1146 dbg("%s - port_number %d, function %d, data 0x%02X", __FUNCTION__, port_number, function, data[1]);
1148 if (port_number >= serial->num_ports) {
1149 dev_err(dev, "%s - bad port number, %d\n", __FUNCTION__, port_number);
1153 port = serial->port[port_number];
1155 tport = usb_get_serial_port_data(port);
1160 case TI_CODE_DATA_ERROR:
1161 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", __FUNCTION__, port_number, data[1]);
1164 case TI_CODE_MODEM_STATUS:
1166 dbg("%s - port %d, msr 0x%02X", __FUNCTION__, port_number, msr);
1167 ti_handle_new_msr(tport, msr);
1171 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", __FUNCTION__, data[1]);
1176 status = usb_submit_urb(urb, GFP_ATOMIC);
1178 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n", __FUNCTION__, status);
1182 static void ti_bulk_in_callback(struct urb *urb, struct pt_regs *regs)
1184 struct ti_port *tport = (struct ti_port *)urb->context;
1185 struct usb_serial_port *port = tport->tp_port;
1186 struct device *dev = &urb->dev->dev;
1189 dbg("%s", __FUNCTION__);
1191 switch (urb->status) {
1197 dbg("%s - urb shutting down, %d", __FUNCTION__, urb->status);
1198 tport->tp_tdev->td_urb_error = 1;
1199 wake_up_interruptible(&tport->tp_write_wait);
1202 dev_err(dev, "%s - nonzero urb status, %d\n", __FUNCTION__, urb->status );
1203 tport->tp_tdev->td_urb_error = 1;
1204 wake_up_interruptible(&tport->tp_write_wait);
1207 if (urb->status == -EPIPE)
1211 dev_err(dev, "%s - stopping read!\n", __FUNCTION__);
1215 if (port->tty && urb->actual_length) {
1216 usb_serial_debug_data(debug, dev, __FUNCTION__,
1217 urb->actual_length, urb->transfer_buffer);
1219 if (!tport->tp_is_open)
1220 dbg("%s - port closed, dropping data", __FUNCTION__);
1222 ti_recv(&urb->dev->dev, port->tty, urb->transfer_buffer,
1223 urb->actual_length);
1225 spin_lock(&tport->tp_lock);
1226 tport->tp_icount.rx += urb->actual_length;
1227 spin_unlock(&tport->tp_lock);
1231 /* continue to read unless stopping */
1232 spin_lock(&tport->tp_lock);
1233 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1234 urb->dev = port->serial->dev;
1235 status = usb_submit_urb(urb, GFP_ATOMIC);
1236 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1237 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1239 spin_unlock(&tport->tp_lock);
1241 dev_err(dev, "%s - resubmit read urb failed, %d\n", __FUNCTION__, status);
1245 static void ti_bulk_out_callback(struct urb *urb, struct pt_regs *regs)
1247 struct ti_port *tport = (struct ti_port *)urb->context;
1248 struct usb_serial_port *port = tport->tp_port;
1249 struct device *dev = &urb->dev->dev;
1251 dbg("%s - port %d", __FUNCTION__, port->number);
1253 tport->tp_write_urb_in_use = 0;
1255 switch (urb->status) {
1261 dbg("%s - urb shutting down, %d", __FUNCTION__, urb->status);
1262 tport->tp_tdev->td_urb_error = 1;
1263 wake_up_interruptible(&tport->tp_write_wait);
1266 dev_err(dev, "%s - nonzero urb status, %d\n", __FUNCTION__, urb->status);
1267 tport->tp_tdev->td_urb_error = 1;
1268 wake_up_interruptible(&tport->tp_write_wait);
1271 /* send any buffered data */
1276 static void ti_recv(struct device *dev, struct tty_struct *tty,
1277 unsigned char *data, int length)
1282 cnt = tty_buffer_request_room(tty, length);
1284 dev_err(dev, "%s - dropping data, %d bytes lost\n", __FUNCTION__, length - cnt);
1288 tty_insert_flip_string(tty, data, cnt);
1289 tty_flip_buffer_push(tty);
1292 } while (length > 0);
1297 static void ti_send(struct ti_port *tport)
1300 struct usb_serial_port *port = tport->tp_port;
1301 struct tty_struct *tty = port->tty;
1302 unsigned long flags;
1305 dbg("%s - port %d", __FUNCTION__, port->number);
1307 spin_lock_irqsave(&tport->tp_lock, flags);
1309 if (tport->tp_write_urb_in_use) {
1310 spin_unlock_irqrestore(&tport->tp_lock, flags);
1314 count = ti_buf_get(tport->tp_write_buf,
1315 port->write_urb->transfer_buffer,
1316 port->bulk_out_size);
1319 spin_unlock_irqrestore(&tport->tp_lock, flags);
1323 tport->tp_write_urb_in_use = 1;
1325 spin_unlock_irqrestore(&tport->tp_lock, flags);
1327 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
1329 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1330 usb_sndbulkpipe(port->serial->dev,
1331 port->bulk_out_endpointAddress),
1332 port->write_urb->transfer_buffer, count,
1333 ti_bulk_out_callback, tport);
1335 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1337 dev_err(&port->dev, "%s - submit write urb failed, %d\n", __FUNCTION__, result);
1338 tport->tp_write_urb_in_use = 0;
1339 /* TODO: reschedule ti_send */
1341 spin_lock_irqsave(&tport->tp_lock, flags);
1342 tport->tp_icount.tx += count;
1343 spin_unlock_irqrestore(&tport->tp_lock, flags);
1346 /* more room in the buffer for new writes, wakeup */
1349 wake_up_interruptible(&tport->tp_write_wait);
1353 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1357 status = ti_write_byte(tport->tp_tdev,
1358 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1359 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1362 tport->tp_shadow_mcr = mcr;
1368 static int ti_get_lsr(struct ti_port *tport)
1371 struct ti_device *tdev = tport->tp_tdev;
1372 struct usb_serial_port *port = tport->tp_port;
1373 int port_number = port->number - port->serial->minor;
1374 struct ti_port_status *data;
1376 dbg("%s - port %d", __FUNCTION__, port->number);
1378 size = sizeof(struct ti_port_status);
1379 data = kmalloc(size, GFP_KERNEL);
1381 dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
1385 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1386 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1388 dev_err(&port->dev, "%s - get port status command failed, %d\n", __FUNCTION__, status);
1392 dbg("%s - lsr 0x%02X", __FUNCTION__, data->bLSR);
1394 tport->tp_lsr = data->bLSR;
1402 static int ti_get_serial_info(struct ti_port *tport,
1403 struct serial_struct __user *ret_arg)
1405 struct usb_serial_port *port = tport->tp_port;
1406 struct serial_struct ret_serial;
1411 memset(&ret_serial, 0, sizeof(ret_serial));
1413 ret_serial.type = PORT_16550A;
1414 ret_serial.line = port->serial->minor;
1415 ret_serial.port = port->number - port->serial->minor;
1416 ret_serial.flags = tport->tp_flags;
1417 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1418 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1419 ret_serial.closing_wait = tport->tp_closing_wait;
1421 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1428 static int ti_set_serial_info(struct ti_port *tport,
1429 struct serial_struct __user *new_arg)
1431 struct usb_serial_port *port = tport->tp_port;
1432 struct serial_struct new_serial;
1434 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1437 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1439 port->tty->low_latency =
1440 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1441 tport->tp_closing_wait = new_serial.closing_wait;
1447 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1449 struct async_icount *icount;
1450 struct tty_struct *tty;
1451 unsigned long flags;
1453 dbg("%s - msr 0x%02X", __FUNCTION__, msr);
1455 if (msr & TI_MSR_DELTA_MASK) {
1456 spin_lock_irqsave(&tport->tp_lock, flags);
1457 icount = &tport->tp_icount;
1458 if (msr & TI_MSR_DELTA_CTS)
1460 if (msr & TI_MSR_DELTA_DSR)
1462 if (msr & TI_MSR_DELTA_CD)
1464 if (msr & TI_MSR_DELTA_RI)
1466 wake_up_interruptible(&tport->tp_msr_wait);
1467 spin_unlock_irqrestore(&tport->tp_lock, flags);
1470 tport->tp_msr = msr & TI_MSR_MASK;
1472 /* handle CTS flow control */
1473 tty = tport->tp_port->tty;
1474 if (tty && C_CRTSCTS(tty)) {
1475 if (msr & TI_MSR_CTS) {
1476 tty->hw_stopped = 0;
1479 tty->hw_stopped = 1;
1485 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1487 struct ti_device *tdev = tport->tp_tdev;
1488 struct usb_serial_port *port = tport->tp_port;
1490 unsigned long flags;
1492 dbg("%s - port %d", __FUNCTION__, port->number);
1494 spin_lock_irqsave(&tport->tp_lock, flags);
1496 /* wait for data to drain from the buffer */
1497 tdev->td_urb_error = 0;
1498 init_waitqueue_entry(&wait, current);
1499 add_wait_queue(&tport->tp_write_wait, &wait);
1501 set_current_state(TASK_INTERRUPTIBLE);
1502 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1503 || timeout == 0 || signal_pending(current)
1504 || tdev->td_urb_error
1505 || !usb_get_intfdata(port->serial->interface)) /* disconnect */
1507 spin_unlock_irqrestore(&tport->tp_lock, flags);
1508 timeout = schedule_timeout(timeout);
1509 spin_lock_irqsave(&tport->tp_lock, flags);
1511 set_current_state(TASK_RUNNING);
1512 remove_wait_queue(&tport->tp_write_wait, &wait);
1514 /* flush any remaining data in the buffer */
1516 ti_buf_clear(tport->tp_write_buf);
1518 spin_unlock_irqrestore(&tport->tp_lock, flags);
1520 /* wait for data to drain from the device */
1521 /* wait for empty tx register, plus 20 ms */
1523 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1524 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1525 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1526 && usb_get_intfdata(port->serial->interface)) { /* not disconnected */
1527 if (ti_get_lsr(tport))
1529 msleep_interruptible(20);
1534 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1536 unsigned long flags;
1538 spin_lock_irqsave(&tport->tp_lock, flags);
1540 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1541 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1543 spin_unlock_irqrestore(&tport->tp_lock, flags);
1547 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1551 unsigned long flags;
1553 spin_lock_irqsave(&tport->tp_lock, flags);
1555 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1556 urb = tport->tp_port->read_urb;
1557 urb->complete = ti_bulk_in_callback;
1558 urb->context = tport;
1559 urb->dev = tport->tp_port->serial->dev;
1560 status = usb_submit_urb(urb, GFP_KERNEL);
1562 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1564 spin_unlock_irqrestore(&tport->tp_lock, flags);
1570 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1571 __u16 moduleid, __u16 value, __u8 *data, int size)
1575 status = usb_control_msg(tdev->td_serial->dev,
1576 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1577 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1578 value, moduleid, data, size, 1000);
1590 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1591 __u16 moduleid, __u16 value, __u8 *data, int size)
1595 status = usb_control_msg(tdev->td_serial->dev,
1596 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1597 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1598 value, moduleid, data, size, 1000);
1610 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1611 __u8 mask, __u8 byte)
1615 struct ti_write_data_bytes *data;
1616 struct device *dev = &tdev->td_serial->dev->dev;
1618 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X", __FUNCTION__, addr, mask, byte);
1620 size = sizeof(struct ti_write_data_bytes) + 2;
1621 data = kmalloc(size, GFP_KERNEL);
1623 dev_err(dev, "%s - out of memory\n", __FUNCTION__);
1627 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1628 data->bDataType = TI_RW_DATA_BYTE;
1629 data->bDataCounter = 1;
1630 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1631 data->wBaseAddrLo = cpu_to_be16(addr);
1632 data->bData[0] = mask;
1633 data->bData[1] = byte;
1635 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1636 (__u8 *)data, size);
1639 dev_err(dev, "%s - failed, %d\n", __FUNCTION__, status);
1647 static int ti_download_firmware(struct ti_device *tdev,
1648 unsigned char *firmware, unsigned int firmware_size)
1657 struct usb_device *dev = tdev->td_serial->dev;
1658 struct ti_firmware_header *header;
1659 unsigned int pipe = usb_sndbulkpipe(dev,
1660 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1663 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1664 buffer = kmalloc(buffer_size, GFP_KERNEL);
1666 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
1670 memcpy(buffer, firmware, firmware_size);
1671 memset(buffer+firmware_size, 0xff, buffer_size-firmware_size);
1673 for(pos = sizeof(struct ti_firmware_header); pos < buffer_size; pos++)
1674 cs = (__u8)(cs + buffer[pos]);
1676 header = (struct ti_firmware_header *)buffer;
1677 header->wLength = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_firmware_header)));
1678 header->bCheckSum = cs;
1680 dbg("%s - downloading firmware", __FUNCTION__);
1681 for (pos = 0; pos < buffer_size; pos += done) {
1682 len = min(buffer_size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1683 status = usb_bulk_msg(dev, pipe, buffer+pos, len, &done, 1000);
1691 dev_err(&dev->dev, "%s - error downloading firmware, %d\n", __FUNCTION__, status);
1695 dbg("%s - download successful", __FUNCTION__);
1701 /* Circular Buffer Functions */
1706 * Allocate a circular buffer and all associated memory.
1709 static struct circ_buf *ti_buf_alloc(void)
1711 struct circ_buf *cb;
1713 cb = (struct circ_buf *)kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
1717 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1718 if (cb->buf == NULL) {
1732 * Free the buffer and all associated memory.
1735 static void ti_buf_free(struct circ_buf *cb)
1745 * Clear out all data in the circular buffer.
1748 static void ti_buf_clear(struct circ_buf *cb)
1750 cb->head = cb->tail = 0;
1757 * Return the number of bytes of data available in the circular
1761 static int ti_buf_data_avail(struct circ_buf *cb)
1763 return CIRC_CNT(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1768 * ti_buf_space_avail
1770 * Return the number of bytes of space available in the circular
1774 static int ti_buf_space_avail(struct circ_buf *cb)
1776 return CIRC_SPACE(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1783 * Copy data data from a user buffer and put it into the circular buffer.
1784 * Restrict to the amount of space available.
1786 * Return the number of bytes copied.
1789 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1794 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1799 memcpy(cb->buf + cb->head, buf, c);
1800 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1813 * Get data from the circular buffer and copy to the given buffer.
1814 * Restrict to the amount of data available.
1816 * Return the number of bytes copied.
1819 static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1824 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1829 memcpy(buf, cb->buf + cb->tail, c);
1830 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);