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/kernel.h>
72 #include <linux/errno.h>
73 #include <linux/init.h>
74 #include <linux/slab.h>
75 #include <linux/tty.h>
76 #include <linux/tty_driver.h>
77 #include <linux/tty_flip.h>
78 #include <linux/module.h>
79 #include <linux/spinlock.h>
80 #include <linux/ioctl.h>
81 #include <linux/serial.h>
82 #include <linux/circ_buf.h>
83 #include <asm/uaccess.h>
84 #include <asm/semaphore.h>
85 #include <linux/usb.h>
86 #include <linux/usb/serial.h>
88 #include "ti_usb_3410_5052.h"
89 #include "ti_fw_3410.h" /* firmware image for 3410 */
90 #include "ti_fw_5052.h" /* firmware image for 5052 */
95 #define TI_DRIVER_VERSION "v0.9"
96 #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
97 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
99 #define TI_FIRMWARE_BUF_SIZE 16284
101 #define TI_WRITE_BUF_SIZE 1024
103 #define TI_TRANSFER_TIMEOUT 2
105 #define TI_DEFAULT_LOW_LATENCY 0
106 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
108 /* supported setserial flags */
109 #define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
111 /* read urb states */
112 #define TI_READ_URB_RUNNING 0
113 #define TI_READ_URB_STOPPING 1
114 #define TI_READ_URB_STOPPED 2
116 #define TI_EXTRA_VID_PID_COUNT 5
126 __u8 tp_uart_mode; /* 232 or 485 modes */
127 unsigned int tp_uart_base_addr;
129 int tp_closing_wait;/* in .01 secs */
130 struct async_icount tp_icount;
131 wait_queue_head_t tp_msr_wait; /* wait for msr change */
132 wait_queue_head_t tp_write_wait;
133 struct ti_device *tp_tdev;
134 struct usb_serial_port *tp_port;
136 int tp_read_urb_state;
137 int tp_write_urb_in_use;
138 struct circ_buf *tp_write_buf;
142 struct semaphore td_open_close_sem;
143 int td_open_port_count;
144 struct usb_serial *td_serial;
150 /* Function Declarations */
152 static int ti_startup(struct usb_serial *serial);
153 static void ti_shutdown(struct usb_serial *serial);
154 static int ti_open(struct usb_serial_port *port, struct file *file);
155 static void ti_close(struct usb_serial_port *port, struct file *file);
156 static int ti_write(struct usb_serial_port *port, const unsigned char *data,
158 static int ti_write_room(struct usb_serial_port *port);
159 static int ti_chars_in_buffer(struct usb_serial_port *port);
160 static void ti_throttle(struct usb_serial_port *port);
161 static void ti_unthrottle(struct usb_serial_port *port);
162 static int ti_ioctl(struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
163 static void ti_set_termios(struct usb_serial_port *port,
164 struct ktermios *old_termios);
165 static int ti_tiocmget(struct usb_serial_port *port, struct file *file);
166 static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
167 unsigned int set, unsigned int clear);
168 static void ti_break(struct usb_serial_port *port, int break_state);
169 static void ti_interrupt_callback(struct urb *urb);
170 static void ti_bulk_in_callback(struct urb *urb);
171 static void ti_bulk_out_callback(struct urb *urb);
173 static void ti_recv(struct device *dev, struct tty_struct *tty,
174 unsigned char *data, int length);
175 static void ti_send(struct ti_port *tport);
176 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
177 static int ti_get_lsr(struct ti_port *tport);
178 static int ti_get_serial_info(struct ti_port *tport,
179 struct serial_struct __user *ret_arg);
180 static int ti_set_serial_info(struct ti_port *tport,
181 struct serial_struct __user *new_arg);
182 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
184 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
186 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
187 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
189 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
190 __u16 moduleid, __u16 value, __u8 *data, int size);
191 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
192 __u16 moduleid, __u16 value, __u8 *data, int size);
194 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
195 __u8 mask, __u8 byte);
197 static int ti_download_firmware(struct ti_device *tdev,
198 unsigned char *firmware, unsigned int firmware_size);
200 /* circular buffer */
201 static struct circ_buf *ti_buf_alloc(void);
202 static void ti_buf_free(struct circ_buf *cb);
203 static void ti_buf_clear(struct circ_buf *cb);
204 static int ti_buf_data_avail(struct circ_buf *cb);
205 static int ti_buf_space_avail(struct circ_buf *cb);
206 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
207 static int ti_buf_get(struct circ_buf *cb, char *buf, int count);
212 /* module parameters */
214 static int low_latency = TI_DEFAULT_LOW_LATENCY;
215 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
216 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
217 static int vendor_3410_count;
218 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
219 static int product_3410_count;
220 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
221 static int vendor_5052_count;
222 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
223 static int product_5052_count;
225 /* supported devices */
226 /* the array dimension is the number of default entries plus */
227 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
229 static struct usb_device_id ti_id_table_3410[1+TI_EXTRA_VID_PID_COUNT+1] = {
230 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
231 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_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_3410_EZ430_ID) },
244 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
245 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
246 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
247 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
251 static struct usb_driver ti_usb_driver = {
252 .name = "ti_usb_3410_5052",
253 .probe = usb_serial_probe,
254 .disconnect = usb_serial_disconnect,
255 .id_table = ti_id_table_combined,
259 static struct usb_serial_driver ti_1port_device = {
261 .owner = THIS_MODULE,
262 .name = "ti_usb_3410_5052_1",
264 .description = "TI USB 3410 1 port adapter",
265 .usb_driver = &ti_usb_driver,
266 .id_table = ti_id_table_3410,
267 .num_interrupt_in = 1,
271 .attach = ti_startup,
272 .shutdown = ti_shutdown,
276 .write_room = ti_write_room,
277 .chars_in_buffer = ti_chars_in_buffer,
278 .throttle = ti_throttle,
279 .unthrottle = ti_unthrottle,
281 .set_termios = ti_set_termios,
282 .tiocmget = ti_tiocmget,
283 .tiocmset = ti_tiocmset,
284 .break_ctl = ti_break,
285 .read_int_callback = ti_interrupt_callback,
286 .read_bulk_callback = ti_bulk_in_callback,
287 .write_bulk_callback = ti_bulk_out_callback,
290 static struct usb_serial_driver ti_2port_device = {
292 .owner = THIS_MODULE,
293 .name = "ti_usb_3410_5052_2",
295 .description = "TI USB 5052 2 port adapter",
296 .usb_driver = &ti_usb_driver,
297 .id_table = ti_id_table_5052,
298 .num_interrupt_in = 1,
302 .attach = ti_startup,
303 .shutdown = ti_shutdown,
307 .write_room = ti_write_room,
308 .chars_in_buffer = ti_chars_in_buffer,
309 .throttle = ti_throttle,
310 .unthrottle = ti_unthrottle,
312 .set_termios = ti_set_termios,
313 .tiocmget = ti_tiocmget,
314 .tiocmset = ti_tiocmset,
315 .break_ctl = ti_break,
316 .read_int_callback = ti_interrupt_callback,
317 .read_bulk_callback = ti_bulk_in_callback,
318 .write_bulk_callback = ti_bulk_out_callback,
324 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
325 MODULE_DESCRIPTION(TI_DRIVER_DESC);
326 MODULE_VERSION(TI_DRIVER_VERSION);
327 MODULE_LICENSE("GPL");
329 module_param(debug, bool, S_IRUGO | S_IWUSR);
330 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
332 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
333 MODULE_PARM_DESC(low_latency, "TTY low_latency flag, 0=off, 1=on, default is off");
335 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
336 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain in close, in .01 secs, default is 4000");
338 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
339 MODULE_PARM_DESC(vendor_3410, "Vendor ids for 3410 based devices, 1-5 short integers");
340 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
341 MODULE_PARM_DESC(product_3410, "Product ids for 3410 based devices, 1-5 short integers");
342 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
343 MODULE_PARM_DESC(vendor_5052, "Vendor ids for 5052 based devices, 1-5 short integers");
344 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
345 MODULE_PARM_DESC(product_5052, "Product ids for 5052 based devices, 1-5 short integers");
347 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
352 static int __init ti_init(void)
357 /* insert extra vendor and product ids */
358 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
359 for (i=0; i<min(vendor_3410_count,product_3410_count); i++,j++) {
360 ti_id_table_3410[j].idVendor = vendor_3410[i];
361 ti_id_table_3410[j].idProduct = product_3410[i];
362 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
364 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
365 for (i=0; i<min(vendor_5052_count,product_5052_count); i++,j++) {
366 ti_id_table_5052[j].idVendor = vendor_5052[i];
367 ti_id_table_5052[j].idProduct = product_5052[i];
368 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
371 ret = usb_serial_register(&ti_1port_device);
374 ret = usb_serial_register(&ti_2port_device);
378 ret = usb_register(&ti_usb_driver);
382 info(TI_DRIVER_DESC " " TI_DRIVER_VERSION);
387 usb_serial_deregister(&ti_2port_device);
389 usb_serial_deregister(&ti_1port_device);
395 static void __exit ti_exit(void)
397 usb_serial_deregister(&ti_1port_device);
398 usb_serial_deregister(&ti_2port_device);
399 usb_deregister(&ti_usb_driver);
403 module_init(ti_init);
404 module_exit(ti_exit);
407 static int ti_startup(struct usb_serial *serial)
409 struct ti_device *tdev;
410 struct ti_port *tport;
411 struct usb_device *dev = serial->dev;
416 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
417 __FUNCTION__, le16_to_cpu(dev->descriptor.idProduct),
418 dev->descriptor.bNumConfigurations,
419 dev->actconfig->desc.bConfigurationValue);
421 /* create device structure */
422 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
424 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
427 sema_init(&tdev->td_open_close_sem, 1);
428 tdev->td_serial = serial;
429 usb_set_serial_data(serial, tdev);
431 /* determine device type */
432 if (usb_match_id(serial->interface, ti_id_table_3410))
433 tdev->td_is_3410 = 1;
434 dbg("%s - device type is %s", __FUNCTION__, tdev->td_is_3410 ? "3410" : "5052");
436 /* if we have only 1 configuration, download firmware */
437 if (dev->descriptor.bNumConfigurations == 1) {
439 if (tdev->td_is_3410)
440 status = ti_download_firmware(tdev, ti_fw_3410,
443 status = ti_download_firmware(tdev, ti_fw_5052,
448 /* 3410 must be reset, 5052 resets itself */
449 if (tdev->td_is_3410) {
450 msleep_interruptible(100);
451 usb_reset_device(dev);
458 /* the second configuration must be set (in sysfs by hotplug script) */
459 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
464 /* set up port structures */
465 for (i = 0; i < serial->num_ports; ++i) {
466 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
468 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
472 spin_lock_init(&tport->tp_lock);
473 tport->tp_uart_base_addr = (i == 0 ? TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
474 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
475 tport->tp_closing_wait = closing_wait;
476 init_waitqueue_head(&tport->tp_msr_wait);
477 init_waitqueue_head(&tport->tp_write_wait);
478 tport->tp_write_buf = ti_buf_alloc();
479 if (tport->tp_write_buf == NULL) {
480 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
485 tport->tp_port = serial->port[i];
486 tport->tp_tdev = tdev;
487 usb_set_serial_port_data(serial->port[i], tport);
488 tport->tp_uart_mode = 0; /* default is RS232 */
494 for (--i; i>=0; --i) {
495 tport = usb_get_serial_port_data(serial->port[i]);
496 ti_buf_free(tport->tp_write_buf);
498 usb_set_serial_port_data(serial->port[i], NULL);
502 usb_set_serial_data(serial, NULL);
507 static void ti_shutdown(struct usb_serial *serial)
510 struct ti_device *tdev = usb_get_serial_data(serial);
511 struct ti_port *tport;
513 dbg("%s", __FUNCTION__);
515 for (i=0; i < serial->num_ports; ++i) {
516 tport = usb_get_serial_port_data(serial->port[i]);
518 ti_buf_free(tport->tp_write_buf);
520 usb_set_serial_port_data(serial->port[i], NULL);
525 usb_set_serial_data(serial, NULL);
529 static int ti_open(struct usb_serial_port *port, struct file *file)
531 struct ti_port *tport = usb_get_serial_port_data(port);
532 struct ti_device *tdev;
533 struct usb_device *dev;
537 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
538 TI_PIPE_TIMEOUT_ENABLE |
539 (TI_TRANSFER_TIMEOUT << 2));
541 dbg("%s - port %d", __FUNCTION__, port->number);
546 dev = port->serial->dev;
547 tdev = tport->tp_tdev;
549 /* only one open on any port on a device at a time */
550 if (down_interruptible(&tdev->td_open_close_sem))
554 port->tty->low_latency =
555 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
557 port_number = port->number - port->serial->minor;
559 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
562 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
564 /* start interrupt urb the first time a port is opened on this device */
565 if (tdev->td_open_port_count == 0) {
566 dbg("%s - start interrupt in urb", __FUNCTION__);
567 urb = tdev->td_serial->port[0]->interrupt_in_urb;
569 dev_err(&port->dev, "%s - no interrupt urb\n", __FUNCTION__);
573 urb->complete = ti_interrupt_callback;
576 status = usb_submit_urb(urb, GFP_KERNEL);
578 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __FUNCTION__, status);
583 ti_set_termios(port, NULL);
585 dbg("%s - sending TI_OPEN_PORT", __FUNCTION__);
586 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
587 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
589 dev_err(&port->dev, "%s - cannot send open command, %d\n", __FUNCTION__, status);
593 dbg("%s - sending TI_START_PORT", __FUNCTION__);
594 status = ti_command_out_sync(tdev, TI_START_PORT,
595 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
597 dev_err(&port->dev, "%s - cannot send start command, %d\n", __FUNCTION__, status);
601 dbg("%s - sending TI_PURGE_PORT", __FUNCTION__);
602 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
603 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
605 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", __FUNCTION__, status);
608 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
609 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
611 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", __FUNCTION__, status);
615 /* reset the data toggle on the bulk endpoints to work around bug in
616 * host controllers where things get out of sync some times */
617 usb_clear_halt(dev, port->write_urb->pipe);
618 usb_clear_halt(dev, port->read_urb->pipe);
620 ti_set_termios(port, NULL);
622 dbg("%s - sending TI_OPEN_PORT (2)", __FUNCTION__);
623 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
624 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
626 dev_err(&port->dev, "%s - cannot send open command (2), %d\n", __FUNCTION__, status);
630 dbg("%s - sending TI_START_PORT (2)", __FUNCTION__);
631 status = ti_command_out_sync(tdev, TI_START_PORT,
632 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
634 dev_err(&port->dev, "%s - cannot send start command (2), %d\n", __FUNCTION__, status);
639 dbg("%s - start read urb", __FUNCTION__);
640 urb = port->read_urb;
642 dev_err(&port->dev, "%s - no read urb\n", __FUNCTION__);
646 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
647 urb->complete = ti_bulk_in_callback;
648 urb->context = tport;
650 status = usb_submit_urb(urb, GFP_KERNEL);
652 dev_err(&port->dev, "%s - submit read urb failed, %d\n", __FUNCTION__, status);
656 tport->tp_is_open = 1;
657 ++tdev->td_open_port_count;
662 if (tdev->td_open_port_count == 0)
663 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
665 up(&tdev->td_open_close_sem);
666 dbg("%s - exit %d", __FUNCTION__, status);
671 static void ti_close(struct usb_serial_port *port, struct file *file)
673 struct ti_device *tdev;
674 struct ti_port *tport;
679 dbg("%s - port %d", __FUNCTION__, port->number);
681 tdev = usb_get_serial_data(port->serial);
682 tport = usb_get_serial_port_data(port);
683 if (tdev == NULL || tport == NULL)
686 tport->tp_is_open = 0;
688 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
690 usb_kill_urb(port->read_urb);
691 usb_kill_urb(port->write_urb);
692 tport->tp_write_urb_in_use = 0;
694 port_number = port->number - port->serial->minor;
696 dbg("%s - sending TI_CLOSE_PORT", __FUNCTION__);
697 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
698 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
700 dev_err(&port->dev, "%s - cannot send close port command, %d\n" , __FUNCTION__, status);
702 /* if down is interrupted, continue anyway */
703 do_up = !down_interruptible(&tdev->td_open_close_sem);
704 --tport->tp_tdev->td_open_port_count;
705 if (tport->tp_tdev->td_open_port_count <= 0) {
706 /* last port is closed, shut down interrupt urb */
707 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
708 tport->tp_tdev->td_open_port_count = 0;
711 up(&tdev->td_open_close_sem);
713 dbg("%s - exit", __FUNCTION__);
717 static int ti_write(struct usb_serial_port *port, const unsigned char *data,
720 struct ti_port *tport = usb_get_serial_port_data(port);
723 dbg("%s - port %d", __FUNCTION__, port->number);
726 dbg("%s - write request of 0 bytes", __FUNCTION__);
730 if (tport == NULL || !tport->tp_is_open)
733 spin_lock_irqsave(&tport->tp_lock, flags);
734 count = ti_buf_put(tport->tp_write_buf, data, count);
735 spin_unlock_irqrestore(&tport->tp_lock, flags);
743 static int ti_write_room(struct usb_serial_port *port)
745 struct ti_port *tport = usb_get_serial_port_data(port);
749 dbg("%s - port %d", __FUNCTION__, port->number);
754 spin_lock_irqsave(&tport->tp_lock, flags);
755 room = ti_buf_space_avail(tport->tp_write_buf);
756 spin_unlock_irqrestore(&tport->tp_lock, flags);
758 dbg("%s - returns %d", __FUNCTION__, room);
763 static int ti_chars_in_buffer(struct usb_serial_port *port)
765 struct ti_port *tport = usb_get_serial_port_data(port);
769 dbg("%s - port %d", __FUNCTION__, port->number);
774 spin_lock_irqsave(&tport->tp_lock, flags);
775 chars = ti_buf_data_avail(tport->tp_write_buf);
776 spin_unlock_irqrestore(&tport->tp_lock, flags);
778 dbg("%s - returns %d", __FUNCTION__, chars);
783 static void ti_throttle(struct usb_serial_port *port)
785 struct ti_port *tport = usb_get_serial_port_data(port);
786 struct tty_struct *tty;
788 dbg("%s - port %d", __FUNCTION__, port->number);
795 dbg("%s - no tty", __FUNCTION__);
799 if (I_IXOFF(tty) || C_CRTSCTS(tty))
800 ti_stop_read(tport, tty);
805 static void ti_unthrottle(struct usb_serial_port *port)
807 struct ti_port *tport = usb_get_serial_port_data(port);
808 struct tty_struct *tty;
811 dbg("%s - port %d", __FUNCTION__, port->number);
818 dbg("%s - no tty", __FUNCTION__);
822 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
823 status = ti_restart_read(tport, tty);
825 dev_err(&port->dev, "%s - cannot restart read, %d\n", __FUNCTION__, status);
830 static int ti_ioctl(struct usb_serial_port *port, struct file *file,
831 unsigned int cmd, unsigned long arg)
833 struct ti_port *tport = usb_get_serial_port_data(port);
834 struct async_icount cnow;
835 struct async_icount cprev;
837 dbg("%s - port %d, cmd = 0x%04X", __FUNCTION__, port->number, cmd);
844 dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
845 return ti_get_serial_info(tport, (struct serial_struct __user *)arg);
849 dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
850 return ti_set_serial_info(tport, (struct serial_struct __user *)arg);
854 dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
855 cprev = tport->tp_icount;
857 interruptible_sleep_on(&tport->tp_msr_wait);
858 if (signal_pending(current))
860 cnow = tport->tp_icount;
861 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
862 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
863 return -EIO; /* no change => error */
864 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
865 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
866 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
867 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
875 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, tport->tp_icount.rx, tport->tp_icount.tx);
876 if (copy_to_user((void __user *)arg, &tport->tp_icount, sizeof(tport->tp_icount)))
885 static void ti_set_termios(struct usb_serial_port *port,
886 struct ktermios *old_termios)
888 struct ti_port *tport = usb_get_serial_port_data(port);
889 struct tty_struct *tty = port->tty;
890 struct ti_uart_config *config;
891 tcflag_t cflag,iflag;
894 int port_number = port->number - port->serial->minor;
897 dbg("%s - port %d", __FUNCTION__, port->number);
899 if (!tty || !tty->termios) {
900 dbg("%s - no tty or termios", __FUNCTION__);
904 cflag = tty->termios->c_cflag;
905 iflag = tty->termios->c_iflag;
907 if (old_termios && cflag == old_termios->c_cflag
908 && iflag == old_termios->c_iflag) {
909 dbg("%s - nothing to change", __FUNCTION__);
913 dbg("%s - clfag %08x, iflag %08x", __FUNCTION__, cflag, iflag);
916 dbg("%s - old clfag %08x, old iflag %08x", __FUNCTION__, old_termios->c_cflag, old_termios->c_iflag);
921 config = kmalloc(sizeof(*config), GFP_KERNEL);
923 dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
929 /* these flags must be set */
930 config->wFlags |= TI_UART_ENABLE_MS_INTS;
931 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
932 config->bUartMode = (__u8)(tport->tp_uart_mode);
934 switch (cflag & CSIZE) {
936 config->bDataBits = TI_UART_5_DATA_BITS;
939 config->bDataBits = TI_UART_6_DATA_BITS;
942 config->bDataBits = TI_UART_7_DATA_BITS;
946 config->bDataBits = TI_UART_8_DATA_BITS;
950 if (cflag & PARENB) {
951 if (cflag & PARODD) {
952 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
953 config->bParity = TI_UART_ODD_PARITY;
955 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
956 config->bParity = TI_UART_EVEN_PARITY;
959 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
960 config->bParity = TI_UART_NO_PARITY;
964 config->bStopBits = TI_UART_2_STOP_BITS;
966 config->bStopBits = TI_UART_1_STOP_BITS;
968 if (cflag & CRTSCTS) {
969 /* RTS flow control must be off to drop RTS for baud rate B0 */
970 if ((cflag & CBAUD) != B0)
971 config->wFlags |= TI_UART_ENABLE_RTS_IN;
972 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
975 ti_restart_read(tport, tty);
978 if (I_IXOFF(tty) || I_IXON(tty)) {
979 config->cXon = START_CHAR(tty);
980 config->cXoff = STOP_CHAR(tty);
983 config->wFlags |= TI_UART_ENABLE_X_IN;
985 ti_restart_read(tport, tty);
988 config->wFlags |= TI_UART_ENABLE_X_OUT;
991 baud = tty_get_baud_rate(tty);
992 if (!baud) baud = 9600;
993 if (tport->tp_tdev->td_is_3410)
994 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
996 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
998 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
999 __FUNCTION__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
1001 cpu_to_be16s(&config->wBaudRate);
1002 cpu_to_be16s(&config->wFlags);
1004 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
1005 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
1008 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", __FUNCTION__, port_number, status);
1010 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
1011 mcr = tport->tp_shadow_mcr;
1012 /* if baud rate is B0, clear RTS and DTR */
1013 if ((cflag & CBAUD) == B0)
1014 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1015 status = ti_set_mcr(tport, mcr);
1017 dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n", __FUNCTION__, port_number, status);
1023 static int ti_tiocmget(struct usb_serial_port *port, struct file *file)
1025 struct ti_port *tport = usb_get_serial_port_data(port);
1026 unsigned int result;
1030 dbg("%s - port %d", __FUNCTION__, port->number);
1035 msr = tport->tp_msr;
1036 mcr = tport->tp_shadow_mcr;
1038 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1039 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1040 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1041 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1042 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1043 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1044 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1046 dbg("%s - 0x%04X", __FUNCTION__, result);
1052 static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
1053 unsigned int set, unsigned int clear)
1055 struct ti_port *tport = usb_get_serial_port_data(port);
1058 dbg("%s - port %d", __FUNCTION__, port->number);
1063 mcr = tport->tp_shadow_mcr;
1065 if (set & TIOCM_RTS)
1067 if (set & TIOCM_DTR)
1069 if (set & TIOCM_LOOP)
1072 if (clear & TIOCM_RTS)
1074 if (clear & TIOCM_DTR)
1076 if (clear & TIOCM_LOOP)
1077 mcr &= ~TI_MCR_LOOP;
1079 return ti_set_mcr(tport, mcr);
1083 static void ti_break(struct usb_serial_port *port, int break_state)
1085 struct ti_port *tport = usb_get_serial_port_data(port);
1088 dbg("%s - state = %d", __FUNCTION__, break_state);
1093 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1095 status = ti_write_byte(tport->tp_tdev,
1096 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1097 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1100 dbg("%s - error setting break, %d", __FUNCTION__, status);
1104 static void ti_interrupt_callback(struct urb *urb)
1106 struct ti_device *tdev = (struct ti_device *)urb->context;
1107 struct usb_serial_port *port;
1108 struct usb_serial *serial = tdev->td_serial;
1109 struct ti_port *tport;
1110 struct device *dev = &urb->dev->dev;
1111 unsigned char *data = urb->transfer_buffer;
1112 int length = urb->actual_length;
1115 int status = urb->status;
1119 dbg("%s", __FUNCTION__);
1127 dbg("%s - urb shutting down, %d", __FUNCTION__, status);
1128 tdev->td_urb_error = 1;
1131 dev_err(dev, "%s - nonzero urb status, %d\n",
1132 __FUNCTION__, status);
1133 tdev->td_urb_error = 1;
1138 dbg("%s - bad packet size, %d", __FUNCTION__, length);
1142 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1143 dev_err(dev, "%s - hardware error, %d\n", __FUNCTION__, data[1]);
1147 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1148 function = TI_GET_FUNC_FROM_CODE(data[0]);
1150 dbg("%s - port_number %d, function %d, data 0x%02X", __FUNCTION__, port_number, function, data[1]);
1152 if (port_number >= serial->num_ports) {
1153 dev_err(dev, "%s - bad port number, %d\n", __FUNCTION__, port_number);
1157 port = serial->port[port_number];
1159 tport = usb_get_serial_port_data(port);
1164 case TI_CODE_DATA_ERROR:
1165 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", __FUNCTION__, port_number, data[1]);
1168 case TI_CODE_MODEM_STATUS:
1170 dbg("%s - port %d, msr 0x%02X", __FUNCTION__, port_number, msr);
1171 ti_handle_new_msr(tport, msr);
1175 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", __FUNCTION__, data[1]);
1180 retval = usb_submit_urb(urb, GFP_ATOMIC);
1182 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1183 __FUNCTION__, retval);
1187 static void ti_bulk_in_callback(struct urb *urb)
1189 struct ti_port *tport = (struct ti_port *)urb->context;
1190 struct usb_serial_port *port = tport->tp_port;
1191 struct device *dev = &urb->dev->dev;
1192 int status = urb->status;
1195 dbg("%s", __FUNCTION__);
1203 dbg("%s - urb shutting down, %d", __FUNCTION__, status);
1204 tport->tp_tdev->td_urb_error = 1;
1205 wake_up_interruptible(&tport->tp_write_wait);
1208 dev_err(dev, "%s - nonzero urb status, %d\n",
1209 __FUNCTION__, status );
1210 tport->tp_tdev->td_urb_error = 1;
1211 wake_up_interruptible(&tport->tp_write_wait);
1214 if (status == -EPIPE)
1218 dev_err(dev, "%s - stopping read!\n", __FUNCTION__);
1222 if (port->tty && urb->actual_length) {
1223 usb_serial_debug_data(debug, dev, __FUNCTION__,
1224 urb->actual_length, urb->transfer_buffer);
1226 if (!tport->tp_is_open)
1227 dbg("%s - port closed, dropping data", __FUNCTION__);
1229 ti_recv(&urb->dev->dev, port->tty, urb->transfer_buffer,
1230 urb->actual_length);
1232 spin_lock(&tport->tp_lock);
1233 tport->tp_icount.rx += urb->actual_length;
1234 spin_unlock(&tport->tp_lock);
1238 /* continue to read unless stopping */
1239 spin_lock(&tport->tp_lock);
1240 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1241 urb->dev = port->serial->dev;
1242 retval = usb_submit_urb(urb, GFP_ATOMIC);
1243 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1244 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1246 spin_unlock(&tport->tp_lock);
1248 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1249 __FUNCTION__, retval);
1253 static void ti_bulk_out_callback(struct urb *urb)
1255 struct ti_port *tport = (struct ti_port *)urb->context;
1256 struct usb_serial_port *port = tport->tp_port;
1257 struct device *dev = &urb->dev->dev;
1258 int status = urb->status;
1260 dbg("%s - port %d", __FUNCTION__, port->number);
1262 tport->tp_write_urb_in_use = 0;
1270 dbg("%s - urb shutting down, %d", __FUNCTION__, status);
1271 tport->tp_tdev->td_urb_error = 1;
1272 wake_up_interruptible(&tport->tp_write_wait);
1275 dev_err(dev, "%s - nonzero urb status, %d\n",
1276 __FUNCTION__, status);
1277 tport->tp_tdev->td_urb_error = 1;
1278 wake_up_interruptible(&tport->tp_write_wait);
1281 /* send any buffered data */
1286 static void ti_recv(struct device *dev, struct tty_struct *tty,
1287 unsigned char *data, int length)
1292 cnt = tty_buffer_request_room(tty, length);
1294 dev_err(dev, "%s - dropping data, %d bytes lost\n", __FUNCTION__, length - cnt);
1298 tty_insert_flip_string(tty, data, cnt);
1299 tty_flip_buffer_push(tty);
1302 } while (length > 0);
1307 static void ti_send(struct ti_port *tport)
1310 struct usb_serial_port *port = tport->tp_port;
1311 struct tty_struct *tty = port->tty;
1312 unsigned long flags;
1315 dbg("%s - port %d", __FUNCTION__, port->number);
1317 spin_lock_irqsave(&tport->tp_lock, flags);
1319 if (tport->tp_write_urb_in_use) {
1320 spin_unlock_irqrestore(&tport->tp_lock, flags);
1324 count = ti_buf_get(tport->tp_write_buf,
1325 port->write_urb->transfer_buffer,
1326 port->bulk_out_size);
1329 spin_unlock_irqrestore(&tport->tp_lock, flags);
1333 tport->tp_write_urb_in_use = 1;
1335 spin_unlock_irqrestore(&tport->tp_lock, flags);
1337 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
1339 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1340 usb_sndbulkpipe(port->serial->dev,
1341 port->bulk_out_endpointAddress),
1342 port->write_urb->transfer_buffer, count,
1343 ti_bulk_out_callback, tport);
1345 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1347 dev_err(&port->dev, "%s - submit write urb failed, %d\n", __FUNCTION__, result);
1348 tport->tp_write_urb_in_use = 0;
1349 /* TODO: reschedule ti_send */
1351 spin_lock_irqsave(&tport->tp_lock, flags);
1352 tport->tp_icount.tx += count;
1353 spin_unlock_irqrestore(&tport->tp_lock, flags);
1356 /* more room in the buffer for new writes, wakeup */
1359 wake_up_interruptible(&tport->tp_write_wait);
1363 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1367 status = ti_write_byte(tport->tp_tdev,
1368 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1369 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1372 tport->tp_shadow_mcr = mcr;
1378 static int ti_get_lsr(struct ti_port *tport)
1381 struct ti_device *tdev = tport->tp_tdev;
1382 struct usb_serial_port *port = tport->tp_port;
1383 int port_number = port->number - port->serial->minor;
1384 struct ti_port_status *data;
1386 dbg("%s - port %d", __FUNCTION__, port->number);
1388 size = sizeof(struct ti_port_status);
1389 data = kmalloc(size, GFP_KERNEL);
1391 dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
1395 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1396 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1398 dev_err(&port->dev, "%s - get port status command failed, %d\n", __FUNCTION__, status);
1402 dbg("%s - lsr 0x%02X", __FUNCTION__, data->bLSR);
1404 tport->tp_lsr = data->bLSR;
1412 static int ti_get_serial_info(struct ti_port *tport,
1413 struct serial_struct __user *ret_arg)
1415 struct usb_serial_port *port = tport->tp_port;
1416 struct serial_struct ret_serial;
1421 memset(&ret_serial, 0, sizeof(ret_serial));
1423 ret_serial.type = PORT_16550A;
1424 ret_serial.line = port->serial->minor;
1425 ret_serial.port = port->number - port->serial->minor;
1426 ret_serial.flags = tport->tp_flags;
1427 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1428 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1429 ret_serial.closing_wait = tport->tp_closing_wait;
1431 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1438 static int ti_set_serial_info(struct ti_port *tport,
1439 struct serial_struct __user *new_arg)
1441 struct usb_serial_port *port = tport->tp_port;
1442 struct serial_struct new_serial;
1444 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1447 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1449 port->tty->low_latency =
1450 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1451 tport->tp_closing_wait = new_serial.closing_wait;
1457 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1459 struct async_icount *icount;
1460 struct tty_struct *tty;
1461 unsigned long flags;
1463 dbg("%s - msr 0x%02X", __FUNCTION__, msr);
1465 if (msr & TI_MSR_DELTA_MASK) {
1466 spin_lock_irqsave(&tport->tp_lock, flags);
1467 icount = &tport->tp_icount;
1468 if (msr & TI_MSR_DELTA_CTS)
1470 if (msr & TI_MSR_DELTA_DSR)
1472 if (msr & TI_MSR_DELTA_CD)
1474 if (msr & TI_MSR_DELTA_RI)
1476 wake_up_interruptible(&tport->tp_msr_wait);
1477 spin_unlock_irqrestore(&tport->tp_lock, flags);
1480 tport->tp_msr = msr & TI_MSR_MASK;
1482 /* handle CTS flow control */
1483 tty = tport->tp_port->tty;
1484 if (tty && C_CRTSCTS(tty)) {
1485 if (msr & TI_MSR_CTS) {
1486 tty->hw_stopped = 0;
1489 tty->hw_stopped = 1;
1495 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1497 struct ti_device *tdev = tport->tp_tdev;
1498 struct usb_serial_port *port = tport->tp_port;
1500 unsigned long flags;
1502 dbg("%s - port %d", __FUNCTION__, port->number);
1504 spin_lock_irqsave(&tport->tp_lock, flags);
1506 /* wait for data to drain from the buffer */
1507 tdev->td_urb_error = 0;
1508 init_waitqueue_entry(&wait, current);
1509 add_wait_queue(&tport->tp_write_wait, &wait);
1511 set_current_state(TASK_INTERRUPTIBLE);
1512 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1513 || timeout == 0 || signal_pending(current)
1514 || tdev->td_urb_error
1515 || !usb_get_intfdata(port->serial->interface)) /* disconnect */
1517 spin_unlock_irqrestore(&tport->tp_lock, flags);
1518 timeout = schedule_timeout(timeout);
1519 spin_lock_irqsave(&tport->tp_lock, flags);
1521 set_current_state(TASK_RUNNING);
1522 remove_wait_queue(&tport->tp_write_wait, &wait);
1524 /* flush any remaining data in the buffer */
1526 ti_buf_clear(tport->tp_write_buf);
1528 spin_unlock_irqrestore(&tport->tp_lock, flags);
1530 /* wait for data to drain from the device */
1531 /* wait for empty tx register, plus 20 ms */
1533 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1534 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1535 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1536 && usb_get_intfdata(port->serial->interface)) { /* not disconnected */
1537 if (ti_get_lsr(tport))
1539 msleep_interruptible(20);
1544 static void ti_stop_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_RUNNING)
1551 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1553 spin_unlock_irqrestore(&tport->tp_lock, flags);
1557 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1561 unsigned long flags;
1563 spin_lock_irqsave(&tport->tp_lock, flags);
1565 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1566 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1567 urb = tport->tp_port->read_urb;
1568 spin_unlock_irqrestore(&tport->tp_lock, flags);
1569 urb->complete = ti_bulk_in_callback;
1570 urb->context = tport;
1571 urb->dev = tport->tp_port->serial->dev;
1572 status = usb_submit_urb(urb, GFP_KERNEL);
1574 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1575 spin_unlock_irqrestore(&tport->tp_lock, flags);
1582 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1583 __u16 moduleid, __u16 value, __u8 *data, int size)
1587 status = usb_control_msg(tdev->td_serial->dev,
1588 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1589 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1590 value, moduleid, data, size, 1000);
1602 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1603 __u16 moduleid, __u16 value, __u8 *data, int size)
1607 status = usb_control_msg(tdev->td_serial->dev,
1608 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1609 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1610 value, moduleid, data, size, 1000);
1622 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1623 __u8 mask, __u8 byte)
1627 struct ti_write_data_bytes *data;
1628 struct device *dev = &tdev->td_serial->dev->dev;
1630 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X", __FUNCTION__, addr, mask, byte);
1632 size = sizeof(struct ti_write_data_bytes) + 2;
1633 data = kmalloc(size, GFP_KERNEL);
1635 dev_err(dev, "%s - out of memory\n", __FUNCTION__);
1639 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1640 data->bDataType = TI_RW_DATA_BYTE;
1641 data->bDataCounter = 1;
1642 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1643 data->wBaseAddrLo = cpu_to_be16(addr);
1644 data->bData[0] = mask;
1645 data->bData[1] = byte;
1647 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1648 (__u8 *)data, size);
1651 dev_err(dev, "%s - failed, %d\n", __FUNCTION__, status);
1659 static int ti_download_firmware(struct ti_device *tdev,
1660 unsigned char *firmware, unsigned int firmware_size)
1669 struct usb_device *dev = tdev->td_serial->dev;
1670 struct ti_firmware_header *header;
1671 unsigned int pipe = usb_sndbulkpipe(dev,
1672 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1675 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1676 buffer = kmalloc(buffer_size, GFP_KERNEL);
1678 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
1682 memcpy(buffer, firmware, firmware_size);
1683 memset(buffer+firmware_size, 0xff, buffer_size-firmware_size);
1685 for(pos = sizeof(struct ti_firmware_header); pos < buffer_size; pos++)
1686 cs = (__u8)(cs + buffer[pos]);
1688 header = (struct ti_firmware_header *)buffer;
1689 header->wLength = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_firmware_header)));
1690 header->bCheckSum = cs;
1692 dbg("%s - downloading firmware", __FUNCTION__);
1693 for (pos = 0; pos < buffer_size; pos += done) {
1694 len = min(buffer_size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1695 status = usb_bulk_msg(dev, pipe, buffer+pos, len, &done, 1000);
1703 dev_err(&dev->dev, "%s - error downloading firmware, %d\n", __FUNCTION__, status);
1707 dbg("%s - download successful", __FUNCTION__);
1713 /* Circular Buffer Functions */
1718 * Allocate a circular buffer and all associated memory.
1721 static struct circ_buf *ti_buf_alloc(void)
1723 struct circ_buf *cb;
1725 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
1729 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1730 if (cb->buf == NULL) {
1744 * Free the buffer and all associated memory.
1747 static void ti_buf_free(struct circ_buf *cb)
1757 * Clear out all data in the circular buffer.
1760 static void ti_buf_clear(struct circ_buf *cb)
1762 cb->head = cb->tail = 0;
1769 * Return the number of bytes of data available in the circular
1773 static int ti_buf_data_avail(struct circ_buf *cb)
1775 return CIRC_CNT(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1780 * ti_buf_space_avail
1782 * Return the number of bytes of space available in the circular
1786 static int ti_buf_space_avail(struct circ_buf *cb)
1788 return CIRC_SPACE(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1795 * Copy data data from a user buffer and put it into the circular buffer.
1796 * Restrict to the amount of space available.
1798 * Return the number of bytes copied.
1801 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1806 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1811 memcpy(cb->buf + cb->head, buf, c);
1812 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1825 * Get data from the circular buffer and copy to the given buffer.
1826 * Restrict to the amount of data available.
1828 * Return the number of bytes copied.
1831 static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1836 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1841 memcpy(buf, cb->buf + cb->tail, c);
1842 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);