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 <linux/mutex.h>
84 #include <asm/uaccess.h>
85 #include <linux/usb.h>
86 #include <linux/usb/serial.h>
87 #include <linux/firmware.h>
89 #include "ti_usb_3410_5052.h"
93 #define TI_DRIVER_VERSION "v0.9"
94 #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
95 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
97 #define TI_FIRMWARE_BUF_SIZE 16284
99 #define TI_WRITE_BUF_SIZE 1024
101 #define TI_TRANSFER_TIMEOUT 2
103 #define TI_DEFAULT_LOW_LATENCY 0
104 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
106 /* supported setserial flags */
107 #define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
109 /* read urb states */
110 #define TI_READ_URB_RUNNING 0
111 #define TI_READ_URB_STOPPING 1
112 #define TI_READ_URB_STOPPED 2
114 #define TI_EXTRA_VID_PID_COUNT 5
124 __u8 tp_uart_mode; /* 232 or 485 modes */
125 unsigned int tp_uart_base_addr;
127 int tp_closing_wait;/* in .01 secs */
128 struct async_icount tp_icount;
129 wait_queue_head_t tp_msr_wait; /* wait for msr change */
130 wait_queue_head_t tp_write_wait;
131 struct ti_device *tp_tdev;
132 struct usb_serial_port *tp_port;
134 int tp_read_urb_state;
135 int tp_write_urb_in_use;
136 struct circ_buf *tp_write_buf;
140 struct mutex td_open_close_lock;
141 int td_open_port_count;
142 struct usb_serial *td_serial;
148 /* Function Declarations */
150 static int ti_startup(struct usb_serial *serial);
151 static void ti_shutdown(struct usb_serial *serial);
152 static int ti_open(struct usb_serial_port *port, struct file *file);
153 static void ti_close(struct usb_serial_port *port, struct file *file);
154 static int ti_write(struct usb_serial_port *port, const unsigned char *data,
156 static int ti_write_room(struct usb_serial_port *port);
157 static int ti_chars_in_buffer(struct usb_serial_port *port);
158 static void ti_throttle(struct usb_serial_port *port);
159 static void ti_unthrottle(struct usb_serial_port *port);
160 static int ti_ioctl(struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
161 static void ti_set_termios(struct usb_serial_port *port,
162 struct ktermios *old_termios);
163 static int ti_tiocmget(struct usb_serial_port *port, struct file *file);
164 static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
165 unsigned int set, unsigned int clear);
166 static void ti_break(struct usb_serial_port *port, int break_state);
167 static void ti_interrupt_callback(struct urb *urb);
168 static void ti_bulk_in_callback(struct urb *urb);
169 static void ti_bulk_out_callback(struct urb *urb);
171 static void ti_recv(struct device *dev, struct tty_struct *tty,
172 unsigned char *data, int length);
173 static void ti_send(struct ti_port *tport);
174 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
175 static int ti_get_lsr(struct ti_port *tport);
176 static int ti_get_serial_info(struct ti_port *tport,
177 struct serial_struct __user *ret_arg);
178 static int ti_set_serial_info(struct ti_port *tport,
179 struct serial_struct __user *new_arg);
180 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
182 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
184 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
185 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
187 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
188 __u16 moduleid, __u16 value, __u8 *data, int size);
189 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
190 __u16 moduleid, __u16 value, __u8 *data, int size);
192 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
193 __u8 mask, __u8 byte);
195 static int ti_download_firmware(struct ti_device *tdev, char *fw_name);
198 /* circular buffer */
199 static struct circ_buf *ti_buf_alloc(void);
200 static void ti_buf_free(struct circ_buf *cb);
201 static void ti_buf_clear(struct circ_buf *cb);
202 static int ti_buf_data_avail(struct circ_buf *cb);
203 static int ti_buf_space_avail(struct circ_buf *cb);
204 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
205 static int ti_buf_get(struct circ_buf *cb, char *buf, int count);
210 /* module parameters */
212 static int low_latency = TI_DEFAULT_LOW_LATENCY;
213 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
214 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
215 static unsigned int vendor_3410_count;
216 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
217 static unsigned int product_3410_count;
218 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
219 static unsigned int vendor_5052_count;
220 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
221 static unsigned int product_5052_count;
223 /* supported devices */
224 /* the array dimension is the number of default entries plus */
225 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
227 static struct usb_device_id ti_id_table_3410[1+TI_EXTRA_VID_PID_COUNT+1] = {
228 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
229 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
232 static struct usb_device_id ti_id_table_5052[4+TI_EXTRA_VID_PID_COUNT+1] = {
233 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
234 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
235 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
236 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
239 static struct usb_device_id ti_id_table_combined[] = {
240 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
241 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
242 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
243 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
244 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
245 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
249 static struct usb_driver ti_usb_driver = {
250 .name = "ti_usb_3410_5052",
251 .probe = usb_serial_probe,
252 .disconnect = usb_serial_disconnect,
253 .id_table = ti_id_table_combined,
257 static struct usb_serial_driver ti_1port_device = {
259 .owner = THIS_MODULE,
260 .name = "ti_usb_3410_5052_1",
262 .description = "TI USB 3410 1 port adapter",
263 .usb_driver = &ti_usb_driver,
264 .id_table = ti_id_table_3410,
266 .attach = ti_startup,
267 .shutdown = ti_shutdown,
271 .write_room = ti_write_room,
272 .chars_in_buffer = ti_chars_in_buffer,
273 .throttle = ti_throttle,
274 .unthrottle = ti_unthrottle,
276 .set_termios = ti_set_termios,
277 .tiocmget = ti_tiocmget,
278 .tiocmset = ti_tiocmset,
279 .break_ctl = ti_break,
280 .read_int_callback = ti_interrupt_callback,
281 .read_bulk_callback = ti_bulk_in_callback,
282 .write_bulk_callback = ti_bulk_out_callback,
285 static struct usb_serial_driver ti_2port_device = {
287 .owner = THIS_MODULE,
288 .name = "ti_usb_3410_5052_2",
290 .description = "TI USB 5052 2 port adapter",
291 .usb_driver = &ti_usb_driver,
292 .id_table = ti_id_table_5052,
294 .attach = ti_startup,
295 .shutdown = ti_shutdown,
299 .write_room = ti_write_room,
300 .chars_in_buffer = ti_chars_in_buffer,
301 .throttle = ti_throttle,
302 .unthrottle = ti_unthrottle,
304 .set_termios = ti_set_termios,
305 .tiocmget = ti_tiocmget,
306 .tiocmset = ti_tiocmset,
307 .break_ctl = ti_break,
308 .read_int_callback = ti_interrupt_callback,
309 .read_bulk_callback = ti_bulk_in_callback,
310 .write_bulk_callback = ti_bulk_out_callback,
316 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
317 MODULE_DESCRIPTION(TI_DRIVER_DESC);
318 MODULE_VERSION(TI_DRIVER_VERSION);
319 MODULE_LICENSE("GPL");
321 MODULE_FIRMWARE("ti_3410.fw");
322 MODULE_FIRMWARE("ti_5052.fw");
324 module_param(debug, bool, S_IRUGO | S_IWUSR);
325 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
327 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
328 MODULE_PARM_DESC(low_latency, "TTY low_latency flag, 0=off, 1=on, default is off");
330 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
331 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain in close, in .01 secs, default is 4000");
333 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
334 MODULE_PARM_DESC(vendor_3410, "Vendor ids for 3410 based devices, 1-5 short integers");
335 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
336 MODULE_PARM_DESC(product_3410, "Product ids for 3410 based devices, 1-5 short integers");
337 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
338 MODULE_PARM_DESC(vendor_5052, "Vendor ids for 5052 based devices, 1-5 short integers");
339 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
340 MODULE_PARM_DESC(product_5052, "Product ids for 5052 based devices, 1-5 short integers");
342 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
347 static int __init ti_init(void)
352 /* insert extra vendor and product ids */
353 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
354 for (i=0; i<min(vendor_3410_count,product_3410_count); i++,j++) {
355 ti_id_table_3410[j].idVendor = vendor_3410[i];
356 ti_id_table_3410[j].idProduct = product_3410[i];
357 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
359 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
360 for (i=0; i<min(vendor_5052_count,product_5052_count); i++,j++) {
361 ti_id_table_5052[j].idVendor = vendor_5052[i];
362 ti_id_table_5052[j].idProduct = product_5052[i];
363 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
366 ret = usb_serial_register(&ti_1port_device);
369 ret = usb_serial_register(&ti_2port_device);
373 ret = usb_register(&ti_usb_driver);
377 info(TI_DRIVER_DESC " " TI_DRIVER_VERSION);
382 usb_serial_deregister(&ti_2port_device);
384 usb_serial_deregister(&ti_1port_device);
390 static void __exit ti_exit(void)
392 usb_serial_deregister(&ti_1port_device);
393 usb_serial_deregister(&ti_2port_device);
394 usb_deregister(&ti_usb_driver);
398 module_init(ti_init);
399 module_exit(ti_exit);
402 static int ti_startup(struct usb_serial *serial)
404 struct ti_device *tdev;
405 struct ti_port *tport;
406 struct usb_device *dev = serial->dev;
411 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
412 __func__, le16_to_cpu(dev->descriptor.idProduct),
413 dev->descriptor.bNumConfigurations,
414 dev->actconfig->desc.bConfigurationValue);
416 /* create device structure */
417 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
419 dev_err(&dev->dev, "%s - out of memory\n", __func__);
422 mutex_init(&tdev->td_open_close_lock);
423 tdev->td_serial = serial;
424 usb_set_serial_data(serial, tdev);
426 /* determine device type */
427 if (usb_match_id(serial->interface, ti_id_table_3410))
428 tdev->td_is_3410 = 1;
429 dbg("%s - device type is %s", __func__, tdev->td_is_3410 ? "3410" : "5052");
431 /* if we have only 1 configuration, download firmware */
432 if (dev->descriptor.bNumConfigurations == 1) {
434 if (tdev->td_is_3410)
435 status = ti_download_firmware(tdev, "ti_3410.fw");
437 status = ti_download_firmware(tdev, "ti_5052.fw");
441 /* 3410 must be reset, 5052 resets itself */
442 if (tdev->td_is_3410) {
443 msleep_interruptible(100);
444 usb_reset_device(dev);
451 /* the second configuration must be set (in sysfs by hotplug script) */
452 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
457 /* set up port structures */
458 for (i = 0; i < serial->num_ports; ++i) {
459 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
461 dev_err(&dev->dev, "%s - out of memory\n", __func__);
465 spin_lock_init(&tport->tp_lock);
466 tport->tp_uart_base_addr = (i == 0 ? TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
467 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
468 tport->tp_closing_wait = closing_wait;
469 init_waitqueue_head(&tport->tp_msr_wait);
470 init_waitqueue_head(&tport->tp_write_wait);
471 tport->tp_write_buf = ti_buf_alloc();
472 if (tport->tp_write_buf == NULL) {
473 dev_err(&dev->dev, "%s - out of memory\n", __func__);
478 tport->tp_port = serial->port[i];
479 tport->tp_tdev = tdev;
480 usb_set_serial_port_data(serial->port[i], tport);
481 tport->tp_uart_mode = 0; /* default is RS232 */
487 for (--i; i>=0; --i) {
488 tport = usb_get_serial_port_data(serial->port[i]);
489 ti_buf_free(tport->tp_write_buf);
491 usb_set_serial_port_data(serial->port[i], NULL);
495 usb_set_serial_data(serial, NULL);
500 static void ti_shutdown(struct usb_serial *serial)
503 struct ti_device *tdev = usb_get_serial_data(serial);
504 struct ti_port *tport;
508 for (i=0; i < serial->num_ports; ++i) {
509 tport = usb_get_serial_port_data(serial->port[i]);
511 ti_buf_free(tport->tp_write_buf);
513 usb_set_serial_port_data(serial->port[i], NULL);
518 usb_set_serial_data(serial, NULL);
522 static int ti_open(struct usb_serial_port *port, struct file *file)
524 struct ti_port *tport = usb_get_serial_port_data(port);
525 struct ti_device *tdev;
526 struct usb_device *dev;
530 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
531 TI_PIPE_TIMEOUT_ENABLE |
532 (TI_TRANSFER_TIMEOUT << 2));
534 dbg("%s - port %d", __func__, port->number);
539 dev = port->serial->dev;
540 tdev = tport->tp_tdev;
542 /* only one open on any port on a device at a time */
543 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
547 port->tty->low_latency =
548 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
550 port_number = port->number - port->serial->minor;
552 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
555 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
557 /* start interrupt urb the first time a port is opened on this device */
558 if (tdev->td_open_port_count == 0) {
559 dbg("%s - start interrupt in urb", __func__);
560 urb = tdev->td_serial->port[0]->interrupt_in_urb;
562 dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
566 urb->complete = ti_interrupt_callback;
569 status = usb_submit_urb(urb, GFP_KERNEL);
571 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
576 ti_set_termios(port, port->tty->termios);
578 dbg("%s - sending TI_OPEN_PORT", __func__);
579 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
580 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
582 dev_err(&port->dev, "%s - cannot send open command, %d\n", __func__, status);
586 dbg("%s - sending TI_START_PORT", __func__);
587 status = ti_command_out_sync(tdev, TI_START_PORT,
588 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
590 dev_err(&port->dev, "%s - cannot send start command, %d\n", __func__, status);
594 dbg("%s - sending TI_PURGE_PORT", __func__);
595 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
596 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
598 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", __func__, status);
601 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
602 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
604 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", __func__, status);
608 /* reset the data toggle on the bulk endpoints to work around bug in
609 * host controllers where things get out of sync some times */
610 usb_clear_halt(dev, port->write_urb->pipe);
611 usb_clear_halt(dev, port->read_urb->pipe);
613 ti_set_termios(port, port->tty->termios);
615 dbg("%s - sending TI_OPEN_PORT (2)", __func__);
616 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
617 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
619 dev_err(&port->dev, "%s - cannot send open command (2), %d\n", __func__, status);
623 dbg("%s - sending TI_START_PORT (2)", __func__);
624 status = ti_command_out_sync(tdev, TI_START_PORT,
625 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
627 dev_err(&port->dev, "%s - cannot send start command (2), %d\n", __func__, status);
632 dbg("%s - start read urb", __func__);
633 urb = port->read_urb;
635 dev_err(&port->dev, "%s - no read urb\n", __func__);
639 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
640 urb->complete = ti_bulk_in_callback;
641 urb->context = tport;
643 status = usb_submit_urb(urb, GFP_KERNEL);
645 dev_err(&port->dev, "%s - submit read urb failed, %d\n", __func__, status);
649 tport->tp_is_open = 1;
650 ++tdev->td_open_port_count;
655 if (tdev->td_open_port_count == 0)
656 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
658 mutex_unlock(&tdev->td_open_close_lock);
659 dbg("%s - exit %d", __func__, status);
664 static void ti_close(struct usb_serial_port *port, struct file *file)
666 struct ti_device *tdev;
667 struct ti_port *tport;
672 dbg("%s - port %d", __func__, port->number);
674 tdev = usb_get_serial_data(port->serial);
675 tport = usb_get_serial_port_data(port);
676 if (tdev == NULL || tport == NULL)
679 tport->tp_is_open = 0;
681 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
683 usb_kill_urb(port->read_urb);
684 usb_kill_urb(port->write_urb);
685 tport->tp_write_urb_in_use = 0;
687 port_number = port->number - port->serial->minor;
689 dbg("%s - sending TI_CLOSE_PORT", __func__);
690 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
691 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
693 dev_err(&port->dev, "%s - cannot send close port command, %d\n" , __func__, status);
695 /* if mutex_lock is interrupted, continue anyway */
696 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
697 --tport->tp_tdev->td_open_port_count;
698 if (tport->tp_tdev->td_open_port_count <= 0) {
699 /* last port is closed, shut down interrupt urb */
700 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
701 tport->tp_tdev->td_open_port_count = 0;
704 mutex_unlock(&tdev->td_open_close_lock);
706 dbg("%s - exit", __func__);
710 static int ti_write(struct usb_serial_port *port, const unsigned char *data,
713 struct ti_port *tport = usb_get_serial_port_data(port);
716 dbg("%s - port %d", __func__, port->number);
719 dbg("%s - write request of 0 bytes", __func__);
723 if (tport == NULL || !tport->tp_is_open)
726 spin_lock_irqsave(&tport->tp_lock, flags);
727 count = ti_buf_put(tport->tp_write_buf, data, count);
728 spin_unlock_irqrestore(&tport->tp_lock, flags);
736 static int ti_write_room(struct usb_serial_port *port)
738 struct ti_port *tport = usb_get_serial_port_data(port);
742 dbg("%s - port %d", __func__, port->number);
747 spin_lock_irqsave(&tport->tp_lock, flags);
748 room = ti_buf_space_avail(tport->tp_write_buf);
749 spin_unlock_irqrestore(&tport->tp_lock, flags);
751 dbg("%s - returns %d", __func__, room);
756 static int ti_chars_in_buffer(struct usb_serial_port *port)
758 struct ti_port *tport = usb_get_serial_port_data(port);
762 dbg("%s - port %d", __func__, port->number);
767 spin_lock_irqsave(&tport->tp_lock, flags);
768 chars = ti_buf_data_avail(tport->tp_write_buf);
769 spin_unlock_irqrestore(&tport->tp_lock, flags);
771 dbg("%s - returns %d", __func__, chars);
776 static void ti_throttle(struct usb_serial_port *port)
778 struct ti_port *tport = usb_get_serial_port_data(port);
779 struct tty_struct *tty;
781 dbg("%s - port %d", __func__, port->number);
788 dbg("%s - no tty", __func__);
792 if (I_IXOFF(tty) || C_CRTSCTS(tty))
793 ti_stop_read(tport, tty);
798 static void ti_unthrottle(struct usb_serial_port *port)
800 struct ti_port *tport = usb_get_serial_port_data(port);
801 struct tty_struct *tty;
804 dbg("%s - port %d", __func__, port->number);
811 dbg("%s - no tty", __func__);
815 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
816 status = ti_restart_read(tport, tty);
818 dev_err(&port->dev, "%s - cannot restart read, %d\n", __func__, status);
823 static int ti_ioctl(struct usb_serial_port *port, struct file *file,
824 unsigned int cmd, unsigned long arg)
826 struct ti_port *tport = usb_get_serial_port_data(port);
827 struct async_icount cnow;
828 struct async_icount cprev;
830 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
837 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
838 return ti_get_serial_info(tport, (struct serial_struct __user *)arg);
842 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
843 return ti_set_serial_info(tport, (struct serial_struct __user *)arg);
847 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
848 cprev = tport->tp_icount;
850 interruptible_sleep_on(&tport->tp_msr_wait);
851 if (signal_pending(current))
853 cnow = tport->tp_icount;
854 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
855 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
856 return -EIO; /* no change => error */
857 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
858 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
859 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
860 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
868 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__, port->number, tport->tp_icount.rx, tport->tp_icount.tx);
869 if (copy_to_user((void __user *)arg, &tport->tp_icount, sizeof(tport->tp_icount)))
878 static void ti_set_termios(struct usb_serial_port *port,
879 struct ktermios *old_termios)
881 struct ti_port *tport = usb_get_serial_port_data(port);
882 struct tty_struct *tty = port->tty;
883 struct ti_uart_config *config;
884 tcflag_t cflag,iflag;
887 int port_number = port->number - port->serial->minor;
890 dbg("%s - port %d", __func__, port->number);
892 cflag = tty->termios->c_cflag;
893 iflag = tty->termios->c_iflag;
895 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
896 dbg("%s - old clfag %08x, old iflag %08x", __func__, old_termios->c_cflag, old_termios->c_iflag);
901 config = kmalloc(sizeof(*config), GFP_KERNEL);
903 dev_err(&port->dev, "%s - out of memory\n", __func__);
909 /* these flags must be set */
910 config->wFlags |= TI_UART_ENABLE_MS_INTS;
911 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
912 config->bUartMode = (__u8)(tport->tp_uart_mode);
914 switch (cflag & CSIZE) {
916 config->bDataBits = TI_UART_5_DATA_BITS;
919 config->bDataBits = TI_UART_6_DATA_BITS;
922 config->bDataBits = TI_UART_7_DATA_BITS;
926 config->bDataBits = TI_UART_8_DATA_BITS;
930 /* CMSPAR isn't supported by this driver */
931 tty->termios->c_cflag &= ~CMSPAR;
933 if (cflag & PARENB) {
934 if (cflag & PARODD) {
935 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
936 config->bParity = TI_UART_ODD_PARITY;
938 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
939 config->bParity = TI_UART_EVEN_PARITY;
942 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
943 config->bParity = TI_UART_NO_PARITY;
947 config->bStopBits = TI_UART_2_STOP_BITS;
949 config->bStopBits = TI_UART_1_STOP_BITS;
951 if (cflag & CRTSCTS) {
952 /* RTS flow control must be off to drop RTS for baud rate B0 */
953 if ((cflag & CBAUD) != B0)
954 config->wFlags |= TI_UART_ENABLE_RTS_IN;
955 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
958 ti_restart_read(tport, tty);
961 if (I_IXOFF(tty) || I_IXON(tty)) {
962 config->cXon = START_CHAR(tty);
963 config->cXoff = STOP_CHAR(tty);
966 config->wFlags |= TI_UART_ENABLE_X_IN;
968 ti_restart_read(tport, tty);
971 config->wFlags |= TI_UART_ENABLE_X_OUT;
974 baud = tty_get_baud_rate(tty);
977 if (tport->tp_tdev->td_is_3410)
978 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
980 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
982 /* FIXME: Should calculate resulting baud here and report it back */
983 if ((cflag & CBAUD) != B0)
984 tty_encode_baud_rate(tty, baud, baud);
986 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
987 __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
989 cpu_to_be16s(&config->wBaudRate);
990 cpu_to_be16s(&config->wFlags);
992 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
993 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
996 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", __func__, port_number, status);
998 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
999 mcr = tport->tp_shadow_mcr;
1000 /* if baud rate is B0, clear RTS and DTR */
1001 if ((cflag & CBAUD) == B0)
1002 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1003 status = ti_set_mcr(tport, mcr);
1005 dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n", __func__, port_number, status);
1011 static int ti_tiocmget(struct usb_serial_port *port, struct file *file)
1013 struct ti_port *tport = usb_get_serial_port_data(port);
1014 unsigned int result;
1017 unsigned long flags;
1019 dbg("%s - port %d", __func__, port->number);
1024 spin_lock_irqsave(&tport->tp_lock, flags);
1025 msr = tport->tp_msr;
1026 mcr = tport->tp_shadow_mcr;
1027 spin_unlock_irqrestore(&tport->tp_lock, flags);
1029 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1030 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1031 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1032 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1033 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1034 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1035 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1037 dbg("%s - 0x%04X", __func__, result);
1043 static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
1044 unsigned int set, unsigned int clear)
1046 struct ti_port *tport = usb_get_serial_port_data(port);
1048 unsigned long flags;
1050 dbg("%s - port %d", __func__, port->number);
1055 spin_lock_irqsave(&tport->tp_lock, flags);
1056 mcr = tport->tp_shadow_mcr;
1058 if (set & TIOCM_RTS)
1060 if (set & TIOCM_DTR)
1062 if (set & TIOCM_LOOP)
1065 if (clear & TIOCM_RTS)
1067 if (clear & TIOCM_DTR)
1069 if (clear & TIOCM_LOOP)
1070 mcr &= ~TI_MCR_LOOP;
1071 spin_unlock_irqrestore(&tport->tp_lock, flags);
1073 return ti_set_mcr(tport, mcr);
1077 static void ti_break(struct usb_serial_port *port, int break_state)
1079 struct ti_port *tport = usb_get_serial_port_data(port);
1082 dbg("%s - state = %d", __func__, break_state);
1087 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1089 status = ti_write_byte(tport->tp_tdev,
1090 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1091 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1094 dbg("%s - error setting break, %d", __func__, status);
1098 static void ti_interrupt_callback(struct urb *urb)
1100 struct ti_device *tdev = urb->context;
1101 struct usb_serial_port *port;
1102 struct usb_serial *serial = tdev->td_serial;
1103 struct ti_port *tport;
1104 struct device *dev = &urb->dev->dev;
1105 unsigned char *data = urb->transfer_buffer;
1106 int length = urb->actual_length;
1109 int status = urb->status;
1113 dbg("%s", __func__);
1121 dbg("%s - urb shutting down, %d", __func__, status);
1122 tdev->td_urb_error = 1;
1125 dev_err(dev, "%s - nonzero urb status, %d\n",
1127 tdev->td_urb_error = 1;
1132 dbg("%s - bad packet size, %d", __func__, length);
1136 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1137 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1141 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1142 function = TI_GET_FUNC_FROM_CODE(data[0]);
1144 dbg("%s - port_number %d, function %d, data 0x%02X", __func__, port_number, function, data[1]);
1146 if (port_number >= serial->num_ports) {
1147 dev_err(dev, "%s - bad port number, %d\n", __func__, port_number);
1151 port = serial->port[port_number];
1153 tport = usb_get_serial_port_data(port);
1158 case TI_CODE_DATA_ERROR:
1159 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", __func__, port_number, data[1]);
1162 case TI_CODE_MODEM_STATUS:
1164 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
1165 ti_handle_new_msr(tport, msr);
1169 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", __func__, data[1]);
1174 retval = usb_submit_urb(urb, GFP_ATOMIC);
1176 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1181 static void ti_bulk_in_callback(struct urb *urb)
1183 struct ti_port *tport = urb->context;
1184 struct usb_serial_port *port = tport->tp_port;
1185 struct device *dev = &urb->dev->dev;
1186 int status = urb->status;
1189 dbg("%s", __func__);
1197 dbg("%s - urb shutting down, %d", __func__, 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",
1204 tport->tp_tdev->td_urb_error = 1;
1205 wake_up_interruptible(&tport->tp_write_wait);
1208 if (status == -EPIPE)
1212 dev_err(dev, "%s - stopping read!\n", __func__);
1216 if (port->tty && urb->actual_length) {
1217 usb_serial_debug_data(debug, dev, __func__,
1218 urb->actual_length, urb->transfer_buffer);
1220 if (!tport->tp_is_open)
1221 dbg("%s - port closed, dropping data", __func__);
1223 ti_recv(&urb->dev->dev, port->tty, urb->transfer_buffer,
1224 urb->actual_length);
1226 spin_lock(&tport->tp_lock);
1227 tport->tp_icount.rx += urb->actual_length;
1228 spin_unlock(&tport->tp_lock);
1232 /* continue to read unless stopping */
1233 spin_lock(&tport->tp_lock);
1234 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1235 urb->dev = port->serial->dev;
1236 retval = usb_submit_urb(urb, GFP_ATOMIC);
1237 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1238 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1240 spin_unlock(&tport->tp_lock);
1242 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1247 static void ti_bulk_out_callback(struct urb *urb)
1249 struct ti_port *tport = urb->context;
1250 struct usb_serial_port *port = tport->tp_port;
1251 struct device *dev = &urb->dev->dev;
1252 int status = urb->status;
1254 dbg("%s - port %d", __func__, port->number);
1256 tport->tp_write_urb_in_use = 0;
1264 dbg("%s - urb shutting down, %d", __func__, status);
1265 tport->tp_tdev->td_urb_error = 1;
1266 wake_up_interruptible(&tport->tp_write_wait);
1269 dev_err(dev, "%s - nonzero urb status, %d\n",
1271 tport->tp_tdev->td_urb_error = 1;
1272 wake_up_interruptible(&tport->tp_write_wait);
1275 /* send any buffered data */
1280 static void ti_recv(struct device *dev, struct tty_struct *tty,
1281 unsigned char *data, int length)
1286 cnt = tty_buffer_request_room(tty, length);
1288 dev_err(dev, "%s - dropping data, %d bytes lost\n", __func__, length - cnt);
1292 tty_insert_flip_string(tty, data, cnt);
1293 tty_flip_buffer_push(tty);
1296 } while (length > 0);
1301 static void ti_send(struct ti_port *tport)
1304 struct usb_serial_port *port = tport->tp_port;
1305 struct tty_struct *tty = port->tty;
1306 unsigned long flags;
1309 dbg("%s - port %d", __func__, port->number);
1311 spin_lock_irqsave(&tport->tp_lock, flags);
1313 if (tport->tp_write_urb_in_use) {
1314 spin_unlock_irqrestore(&tport->tp_lock, flags);
1318 count = ti_buf_get(tport->tp_write_buf,
1319 port->write_urb->transfer_buffer,
1320 port->bulk_out_size);
1323 spin_unlock_irqrestore(&tport->tp_lock, flags);
1327 tport->tp_write_urb_in_use = 1;
1329 spin_unlock_irqrestore(&tport->tp_lock, flags);
1331 usb_serial_debug_data(debug, &port->dev, __func__, count, port->write_urb->transfer_buffer);
1333 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1334 usb_sndbulkpipe(port->serial->dev,
1335 port->bulk_out_endpointAddress),
1336 port->write_urb->transfer_buffer, count,
1337 ti_bulk_out_callback, tport);
1339 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1341 dev_err(&port->dev, "%s - submit write urb failed, %d\n", __func__, result);
1342 tport->tp_write_urb_in_use = 0;
1343 /* TODO: reschedule ti_send */
1345 spin_lock_irqsave(&tport->tp_lock, flags);
1346 tport->tp_icount.tx += count;
1347 spin_unlock_irqrestore(&tport->tp_lock, flags);
1350 /* more room in the buffer for new writes, wakeup */
1353 wake_up_interruptible(&tport->tp_write_wait);
1357 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1359 unsigned long flags;
1362 status = ti_write_byte(tport->tp_tdev,
1363 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1364 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1366 spin_lock_irqsave(&tport->tp_lock, flags);
1368 tport->tp_shadow_mcr = mcr;
1369 spin_unlock_irqrestore(&tport->tp_lock, flags);
1375 static int ti_get_lsr(struct ti_port *tport)
1378 struct ti_device *tdev = tport->tp_tdev;
1379 struct usb_serial_port *port = tport->tp_port;
1380 int port_number = port->number - port->serial->minor;
1381 struct ti_port_status *data;
1383 dbg("%s - port %d", __func__, port->number);
1385 size = sizeof(struct ti_port_status);
1386 data = kmalloc(size, GFP_KERNEL);
1388 dev_err(&port->dev, "%s - out of memory\n", __func__);
1392 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1393 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1395 dev_err(&port->dev, "%s - get port status command failed, %d\n", __func__, status);
1399 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1401 tport->tp_lsr = data->bLSR;
1409 static int ti_get_serial_info(struct ti_port *tport,
1410 struct serial_struct __user *ret_arg)
1412 struct usb_serial_port *port = tport->tp_port;
1413 struct serial_struct ret_serial;
1418 memset(&ret_serial, 0, sizeof(ret_serial));
1420 ret_serial.type = PORT_16550A;
1421 ret_serial.line = port->serial->minor;
1422 ret_serial.port = port->number - port->serial->minor;
1423 ret_serial.flags = tport->tp_flags;
1424 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1425 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1426 ret_serial.closing_wait = tport->tp_closing_wait;
1428 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1435 static int ti_set_serial_info(struct ti_port *tport,
1436 struct serial_struct __user *new_arg)
1438 struct usb_serial_port *port = tport->tp_port;
1439 struct serial_struct new_serial;
1441 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1444 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1446 port->tty->low_latency =
1447 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1448 tport->tp_closing_wait = new_serial.closing_wait;
1454 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1456 struct async_icount *icount;
1457 struct tty_struct *tty;
1458 unsigned long flags;
1460 dbg("%s - msr 0x%02X", __func__, msr);
1462 if (msr & TI_MSR_DELTA_MASK) {
1463 spin_lock_irqsave(&tport->tp_lock, flags);
1464 icount = &tport->tp_icount;
1465 if (msr & TI_MSR_DELTA_CTS)
1467 if (msr & TI_MSR_DELTA_DSR)
1469 if (msr & TI_MSR_DELTA_CD)
1471 if (msr & TI_MSR_DELTA_RI)
1473 wake_up_interruptible(&tport->tp_msr_wait);
1474 spin_unlock_irqrestore(&tport->tp_lock, flags);
1477 tport->tp_msr = msr & TI_MSR_MASK;
1479 /* handle CTS flow control */
1480 tty = tport->tp_port->tty;
1481 if (tty && C_CRTSCTS(tty)) {
1482 if (msr & TI_MSR_CTS) {
1483 tty->hw_stopped = 0;
1486 tty->hw_stopped = 1;
1492 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1494 struct ti_device *tdev = tport->tp_tdev;
1495 struct usb_serial_port *port = tport->tp_port;
1498 dbg("%s - port %d", __func__, port->number);
1500 spin_lock_irq(&tport->tp_lock);
1502 /* wait for data to drain from the buffer */
1503 tdev->td_urb_error = 0;
1504 init_waitqueue_entry(&wait, current);
1505 add_wait_queue(&tport->tp_write_wait, &wait);
1507 set_current_state(TASK_INTERRUPTIBLE);
1508 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1509 || timeout == 0 || signal_pending(current)
1510 || tdev->td_urb_error
1511 || port->serial->disconnected) /* disconnect */
1513 spin_unlock_irq(&tport->tp_lock);
1514 timeout = schedule_timeout(timeout);
1515 spin_lock_irq(&tport->tp_lock);
1517 set_current_state(TASK_RUNNING);
1518 remove_wait_queue(&tport->tp_write_wait, &wait);
1520 /* flush any remaining data in the buffer */
1522 ti_buf_clear(tport->tp_write_buf);
1524 spin_unlock_irq(&tport->tp_lock);
1526 mutex_lock(&port->serial->disc_mutex);
1527 /* wait for data to drain from the device */
1528 /* wait for empty tx register, plus 20 ms */
1530 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1531 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1532 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1533 && !port->serial->disconnected) {
1534 if (ti_get_lsr(tport))
1536 mutex_unlock(&port->serial->disc_mutex);
1537 msleep_interruptible(20);
1538 mutex_lock(&port->serial->disc_mutex);
1540 mutex_unlock(&port->serial->disc_mutex);
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", __func__, 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", __func__);
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", __func__, status);
1659 static int ti_download_firmware(struct ti_device *tdev,
1662 const struct firmware *fw;
1670 struct usb_device *dev = tdev->td_serial->dev;
1671 struct ti_firmware_header *header;
1672 unsigned int pipe = usb_sndbulkpipe(dev,
1673 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1675 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1677 if (request_firmware(&fw, fw_name, &dev->dev)) {
1678 dev_err(&dev->dev, "%s - failed to load firmware \"%s\"\n",
1682 if (fw->size > buffer_size) {
1683 dev_err(&dev->dev, "%s - firmware \"%s\" is too large\n",
1685 release_firmware(fw);
1689 buffer = kmalloc(buffer_size, GFP_KERNEL);
1691 dev_err(&dev->dev, "%s - out of memory\n", __func__);
1692 release_firmware(fw);
1696 memcpy(buffer, fw->data, fw->size);
1697 memset(buffer+fw->size, 0xff, buffer_size-fw->size);
1699 for(pos = sizeof(struct ti_firmware_header); pos < buffer_size; pos++)
1700 cs = (__u8)(cs + buffer[pos]);
1702 header = (struct ti_firmware_header *)buffer;
1703 header->wLength = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_firmware_header)));
1704 header->bCheckSum = cs;
1706 dbg("%s - downloading firmware", __func__);
1707 for (pos = 0; pos < buffer_size; pos += done) {
1708 len = min(buffer_size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1709 status = usb_bulk_msg(dev, pipe, buffer+pos, len, &done, 1000);
1715 release_firmware(fw);
1718 dev_err(&dev->dev, "%s - error downloading firmware, %d\n", __func__, status);
1722 dbg("%s - download successful", __func__);
1728 /* Circular Buffer Functions */
1733 * Allocate a circular buffer and all associated memory.
1736 static struct circ_buf *ti_buf_alloc(void)
1738 struct circ_buf *cb;
1740 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
1744 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1745 if (cb->buf == NULL) {
1759 * Free the buffer and all associated memory.
1762 static void ti_buf_free(struct circ_buf *cb)
1772 * Clear out all data in the circular buffer.
1775 static void ti_buf_clear(struct circ_buf *cb)
1777 cb->head = cb->tail = 0;
1784 * Return the number of bytes of data available in the circular
1788 static int ti_buf_data_avail(struct circ_buf *cb)
1790 return CIRC_CNT(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1795 * ti_buf_space_avail
1797 * Return the number of bytes of space available in the circular
1801 static int ti_buf_space_avail(struct circ_buf *cb)
1803 return CIRC_SPACE(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1810 * Copy data data from a user buffer and put it into the circular buffer.
1811 * Restrict to the amount of space available.
1813 * Return the number of bytes copied.
1816 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1821 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1826 memcpy(cb->buf + cb->head, buf, c);
1827 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1840 * Get data from the circular buffer and copy to the given buffer.
1841 * Restrict to the amount of data available.
1843 * Return the number of bytes copied.
1846 static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1851 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1856 memcpy(buf, cb->buf + cb->tail, c);
1857 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);