[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / usb / serial / ti_usb_3410_5052.c
blob59c88de3e7ae337459ea7cea7262639fe28d5e5e
1 /* vi: ts=8 sw=8
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
22 * configuration.
24 * #!/bin/bash
26 * BOOT_CONFIG=1
27 * ACTIVE_CONFIG=2
29 * if [[ "$ACTION" != "add" ]]
30 * then
31 * exit
32 * fi
34 * CONFIG_PATH=/sys${DEVPATH%/?*}/bConfigurationValue
36 * if [[ 0`cat $CONFIG_PATH` -ne $BOOT_CONFIG ]]
37 * then
38 * exit
39 * fi
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
47 * function scan() {
48 * s=$1
49 * shift
50 * for i
51 * do
52 * if [[ $s -eq $i ]]
53 * then
54 * return 0
55 * fi
56 * done
57 * return 1
58 * }
60 * IFS=$IFS,
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`)
66 * then
67 * echo $ACTIVE_CONFIG > $CONFIG_PATH
68 * fi
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 */
94 /* Defines */
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
120 /* Structures */
122 struct ti_port {
123 int tp_is_open;
124 __u8 tp_msr;
125 __u8 tp_lsr;
126 __u8 tp_shadow_mcr;
127 __u8 tp_uart_mode; /* 232 or 485 modes */
128 unsigned int tp_uart_base_addr;
129 int tp_flags;
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;
136 spinlock_t tp_lock;
137 int tp_read_urb_state;
138 int tp_write_urb_in_use;
139 struct circ_buf *tp_write_buf;
142 struct ti_device {
143 struct semaphore td_open_close_sem;
144 int td_open_port_count;
145 struct usb_serial *td_serial;
146 int td_is_3410;
147 int td_urb_error;
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,
158 int count);
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);
211 /* Data */
213 /* module parameters */
214 static int debug;
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 */
229 /* null entry */
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 .owner = THIS_MODULE,
252 .name = "ti_usb_3410_5052",
253 .probe = usb_serial_probe,
254 .disconnect = usb_serial_disconnect,
255 .id_table = ti_id_table_combined,
258 static struct usb_serial_device_type ti_1port_device = {
259 .owner = THIS_MODULE,
260 .name = "TI USB 3410 1 port adapter",
261 .id_table = ti_id_table_3410,
262 .num_interrupt_in = 1,
263 .num_bulk_in = 1,
264 .num_bulk_out = 1,
265 .num_ports = 1,
266 .attach = ti_startup,
267 .shutdown = ti_shutdown,
268 .open = ti_open,
269 .close = ti_close,
270 .write = ti_write,
271 .write_room = ti_write_room,
272 .chars_in_buffer = ti_chars_in_buffer,
273 .throttle = ti_throttle,
274 .unthrottle = ti_unthrottle,
275 .ioctl = ti_ioctl,
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_device_type ti_2port_device = {
286 .owner = THIS_MODULE,
287 .name = "TI USB 5052 2 port adapter",
288 .id_table = ti_id_table_5052,
289 .num_interrupt_in = 1,
290 .num_bulk_in = 2,
291 .num_bulk_out = 2,
292 .num_ports = 2,
293 .attach = ti_startup,
294 .shutdown = ti_shutdown,
295 .open = ti_open,
296 .close = ti_close,
297 .write = ti_write,
298 .write_room = ti_write_room,
299 .chars_in_buffer = ti_chars_in_buffer,
300 .throttle = ti_throttle,
301 .unthrottle = ti_unthrottle,
302 .ioctl = ti_ioctl,
303 .set_termios = ti_set_termios,
304 .tiocmget = ti_tiocmget,
305 .tiocmset = ti_tiocmset,
306 .break_ctl = ti_break,
307 .read_int_callback = ti_interrupt_callback,
308 .read_bulk_callback = ti_bulk_in_callback,
309 .write_bulk_callback = ti_bulk_out_callback,
313 /* Module */
315 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
316 MODULE_DESCRIPTION(TI_DRIVER_DESC);
317 MODULE_VERSION(TI_DRIVER_VERSION);
318 MODULE_LICENSE("GPL");
320 module_param(debug, bool, S_IRUGO | S_IWUSR);
321 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
323 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
324 MODULE_PARM_DESC(low_latency, "TTY low_latency flag, 0=off, 1=on, default is off");
326 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
327 MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain in close, in .01 secs, default is 4000");
329 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
330 MODULE_PARM_DESC(vendor_3410, "Vendor ids for 3410 based devices, 1-5 short integers");
331 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
332 MODULE_PARM_DESC(product_3410, "Product ids for 3410 based devices, 1-5 short integers");
333 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
334 MODULE_PARM_DESC(vendor_5052, "Vendor ids for 5052 based devices, 1-5 short integers");
335 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
336 MODULE_PARM_DESC(product_5052, "Product ids for 5052 based devices, 1-5 short integers");
338 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
341 /* Functions */
343 static int __init ti_init(void)
345 int i,j;
346 int ret;
349 /* insert extra vendor and product ids */
350 j = sizeof(ti_id_table_3410)/sizeof(struct usb_device_id)
351 - TI_EXTRA_VID_PID_COUNT - 1;
352 for (i=0; i<min(vendor_3410_count,product_3410_count); i++,j++) {
353 ti_id_table_3410[j].idVendor = vendor_3410[i];
354 ti_id_table_3410[j].idProduct = product_3410[i];
355 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
357 j = sizeof(ti_id_table_5052)/sizeof(struct usb_device_id)
358 - TI_EXTRA_VID_PID_COUNT - 1;
359 for (i=0; i<min(vendor_5052_count,product_5052_count); i++,j++) {
360 ti_id_table_5052[j].idVendor = vendor_5052[i];
361 ti_id_table_5052[j].idProduct = product_5052[i];
362 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
365 ret = usb_serial_register(&ti_1port_device);
366 if (ret)
367 goto failed_1port;
368 ret = usb_serial_register(&ti_2port_device);
369 if (ret)
370 goto failed_2port;
372 ret = usb_register(&ti_usb_driver);
373 if (ret)
374 goto failed_usb;
376 info(TI_DRIVER_DESC " " TI_DRIVER_VERSION);
378 return 0;
380 failed_usb:
381 usb_serial_deregister(&ti_2port_device);
382 failed_2port:
383 usb_serial_deregister(&ti_1port_device);
384 failed_1port:
385 return ret;
389 static void __exit ti_exit(void)
391 usb_serial_deregister(&ti_1port_device);
392 usb_serial_deregister(&ti_2port_device);
393 usb_deregister(&ti_usb_driver);
397 module_init(ti_init);
398 module_exit(ti_exit);
401 static int ti_startup(struct usb_serial *serial)
403 struct ti_device *tdev;
404 struct ti_port *tport;
405 struct usb_device *dev = serial->dev;
406 int status;
407 int i;
410 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
411 __FUNCTION__, le16_to_cpu(dev->descriptor.idProduct),
412 dev->descriptor.bNumConfigurations,
413 dev->actconfig->desc.bConfigurationValue);
415 /* create device structure */
416 tdev = kmalloc(sizeof(struct ti_device), GFP_KERNEL);
417 if (tdev == NULL) {
418 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
419 return -ENOMEM;
421 memset(tdev, 0, sizeof(struct ti_device));
422 sema_init(&tdev->td_open_close_sem, 1);
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", __FUNCTION__, 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_fw_3410,
436 sizeof(ti_fw_3410));
437 else
438 status = ti_download_firmware(tdev, ti_fw_5052,
439 sizeof(ti_fw_5052));
440 if (status)
441 goto free_tdev;
443 /* 3410 must be reset, 5052 resets itself */
444 if (tdev->td_is_3410) {
445 msleep_interruptible(100);
446 usb_reset_device(dev);
449 status = -ENODEV;
450 goto free_tdev;
453 /* the second configuration must be set (in sysfs by hotplug script) */
454 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
455 status = -ENODEV;
456 goto free_tdev;
459 /* set up port structures */
460 for (i = 0; i < serial->num_ports; ++i) {
461 tport = kmalloc(sizeof(struct ti_port), GFP_KERNEL);
462 if (tport == NULL) {
463 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
464 status = -ENOMEM;
465 goto free_tports;
467 memset(tport, 0, sizeof(struct ti_port));
468 spin_lock_init(&tport->tp_lock);
469 tport->tp_uart_base_addr = (i == 0 ? TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
470 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
471 tport->tp_closing_wait = closing_wait;
472 init_waitqueue_head(&tport->tp_msr_wait);
473 init_waitqueue_head(&tport->tp_write_wait);
474 tport->tp_write_buf = ti_buf_alloc();
475 if (tport->tp_write_buf == NULL) {
476 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
477 kfree(tport);
478 status = -ENOMEM;
479 goto free_tports;
481 tport->tp_port = serial->port[i];
482 tport->tp_tdev = tdev;
483 usb_set_serial_port_data(serial->port[i], tport);
484 tport->tp_uart_mode = 0; /* default is RS232 */
487 return 0;
489 free_tports:
490 for (--i; i>=0; --i) {
491 tport = usb_get_serial_port_data(serial->port[i]);
492 ti_buf_free(tport->tp_write_buf);
493 kfree(tport);
494 usb_set_serial_port_data(serial->port[i], NULL);
496 free_tdev:
497 kfree(tdev);
498 usb_set_serial_data(serial, NULL);
499 return status;
503 static void ti_shutdown(struct usb_serial *serial)
505 int i;
506 struct ti_device *tdev = usb_get_serial_data(serial);
507 struct ti_port *tport;
509 dbg("%s", __FUNCTION__);
511 for (i=0; i < serial->num_ports; ++i) {
512 tport = usb_get_serial_port_data(serial->port[i]);
513 if (tport) {
514 ti_buf_free(tport->tp_write_buf);
515 kfree(tport);
516 usb_set_serial_port_data(serial->port[i], NULL);
520 kfree(tdev);
521 usb_set_serial_data(serial, NULL);
525 static int ti_open(struct usb_serial_port *port, struct file *file)
527 struct ti_port *tport = usb_get_serial_port_data(port);
528 struct ti_device *tdev;
529 struct usb_device *dev;
530 struct urb *urb;
531 int port_number;
532 int status;
533 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
534 TI_PIPE_TIMEOUT_ENABLE |
535 (TI_TRANSFER_TIMEOUT << 2));
537 dbg("%s - port %d", __FUNCTION__, port->number);
539 if (tport == NULL)
540 return -ENODEV;
542 dev = port->serial->dev;
543 tdev = tport->tp_tdev;
545 /* only one open on any port on a device at a time */
546 if (down_interruptible(&tdev->td_open_close_sem))
547 return -ERESTARTSYS;
549 if (port->tty)
550 port->tty->low_latency =
551 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
553 port_number = port->number - port->serial->minor;
555 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
557 tport->tp_msr = 0;
558 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
560 /* start interrupt urb the first time a port is opened on this device */
561 if (tdev->td_open_port_count == 0) {
562 dbg("%s - start interrupt in urb", __FUNCTION__);
563 urb = tdev->td_serial->port[0]->interrupt_in_urb;
564 if (!urb) {
565 dev_err(&port->dev, "%s - no interrupt urb\n", __FUNCTION__);
566 status = -EINVAL;
567 goto up_sem;
569 urb->complete = ti_interrupt_callback;
570 urb->context = tdev;
571 urb->dev = dev;
572 status = usb_submit_urb(urb, GFP_KERNEL);
573 if (status) {
574 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __FUNCTION__, status);
575 goto up_sem;
579 ti_set_termios(port, NULL);
581 dbg("%s - sending TI_OPEN_PORT", __FUNCTION__);
582 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
583 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
584 if (status) {
585 dev_err(&port->dev, "%s - cannot send open command, %d\n", __FUNCTION__, status);
586 goto unlink_int_urb;
589 dbg("%s - sending TI_START_PORT", __FUNCTION__);
590 status = ti_command_out_sync(tdev, TI_START_PORT,
591 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
592 if (status) {
593 dev_err(&port->dev, "%s - cannot send start command, %d\n", __FUNCTION__, status);
594 goto unlink_int_urb;
597 dbg("%s - sending TI_PURGE_PORT", __FUNCTION__);
598 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
599 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
600 if (status) {
601 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", __FUNCTION__, status);
602 goto unlink_int_urb;
604 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
605 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
606 if (status) {
607 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", __FUNCTION__, status);
608 goto unlink_int_urb;
611 /* reset the data toggle on the bulk endpoints to work around bug in
612 * host controllers where things get out of sync some times */
613 usb_clear_halt(dev, port->write_urb->pipe);
614 usb_clear_halt(dev, port->read_urb->pipe);
616 ti_set_termios(port, NULL);
618 dbg("%s - sending TI_OPEN_PORT (2)", __FUNCTION__);
619 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
620 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
621 if (status) {
622 dev_err(&port->dev, "%s - cannot send open command (2), %d\n", __FUNCTION__, status);
623 goto unlink_int_urb;
626 dbg("%s - sending TI_START_PORT (2)", __FUNCTION__);
627 status = ti_command_out_sync(tdev, TI_START_PORT,
628 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
629 if (status) {
630 dev_err(&port->dev, "%s - cannot send start command (2), %d\n", __FUNCTION__, status);
631 goto unlink_int_urb;
634 /* start read urb */
635 dbg("%s - start read urb", __FUNCTION__);
636 urb = port->read_urb;
637 if (!urb) {
638 dev_err(&port->dev, "%s - no read urb\n", __FUNCTION__);
639 status = -EINVAL;
640 goto unlink_int_urb;
642 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
643 urb->complete = ti_bulk_in_callback;
644 urb->context = tport;
645 urb->dev = dev;
646 status = usb_submit_urb(urb, GFP_KERNEL);
647 if (status) {
648 dev_err(&port->dev, "%s - submit read urb failed, %d\n", __FUNCTION__, status);
649 goto unlink_int_urb;
652 tport->tp_is_open = 1;
653 ++tdev->td_open_port_count;
655 goto up_sem;
657 unlink_int_urb:
658 if (tdev->td_open_port_count == 0)
659 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
660 up_sem:
661 up(&tdev->td_open_close_sem);
662 dbg("%s - exit %d", __FUNCTION__, status);
663 return status;
667 static void ti_close(struct usb_serial_port *port, struct file *file)
669 struct ti_device *tdev;
670 struct ti_port *tport;
671 int port_number;
672 int status;
673 int do_up;
675 dbg("%s - port %d", __FUNCTION__, port->number);
677 tdev = usb_get_serial_data(port->serial);
678 tport = usb_get_serial_port_data(port);
679 if (tdev == NULL || tport == NULL)
680 return;
682 tport->tp_is_open = 0;
684 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
686 usb_kill_urb(port->read_urb);
687 usb_kill_urb(port->write_urb);
688 tport->tp_write_urb_in_use = 0;
690 port_number = port->number - port->serial->minor;
692 dbg("%s - sending TI_CLOSE_PORT", __FUNCTION__);
693 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
694 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
695 if (status)
696 dev_err(&port->dev, "%s - cannot send close port command, %d\n" , __FUNCTION__, status);
698 /* if down is interrupted, continue anyway */
699 do_up = !down_interruptible(&tdev->td_open_close_sem);
700 --tport->tp_tdev->td_open_port_count;
701 if (tport->tp_tdev->td_open_port_count <= 0) {
702 /* last port is closed, shut down interrupt urb */
703 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
704 tport->tp_tdev->td_open_port_count = 0;
706 if (do_up)
707 up(&tdev->td_open_close_sem);
709 dbg("%s - exit", __FUNCTION__);
713 static int ti_write(struct usb_serial_port *port, const unsigned char *data,
714 int count)
716 struct ti_port *tport = usb_get_serial_port_data(port);
717 unsigned long flags;
719 dbg("%s - port %d", __FUNCTION__, port->number);
721 if (count == 0) {
722 dbg("%s - write request of 0 bytes", __FUNCTION__);
723 return 0;
726 if (tport == NULL || !tport->tp_is_open)
727 return -ENODEV;
729 spin_lock_irqsave(&tport->tp_lock, flags);
730 count = ti_buf_put(tport->tp_write_buf, data, count);
731 spin_unlock_irqrestore(&tport->tp_lock, flags);
733 ti_send(tport);
735 return count;
739 static int ti_write_room(struct usb_serial_port *port)
741 struct ti_port *tport = usb_get_serial_port_data(port);
742 int room = 0;
743 unsigned long flags;
745 dbg("%s - port %d", __FUNCTION__, port->number);
747 if (tport == NULL)
748 return -ENODEV;
750 spin_lock_irqsave(&tport->tp_lock, flags);
751 room = ti_buf_space_avail(tport->tp_write_buf);
752 spin_unlock_irqrestore(&tport->tp_lock, flags);
754 dbg("%s - returns %d", __FUNCTION__, room);
755 return room;
759 static int ti_chars_in_buffer(struct usb_serial_port *port)
761 struct ti_port *tport = usb_get_serial_port_data(port);
762 int chars = 0;
763 unsigned long flags;
765 dbg("%s - port %d", __FUNCTION__, port->number);
767 if (tport == NULL)
768 return -ENODEV;
770 spin_lock_irqsave(&tport->tp_lock, flags);
771 chars = ti_buf_data_avail(tport->tp_write_buf);
772 spin_unlock_irqrestore(&tport->tp_lock, flags);
774 dbg("%s - returns %d", __FUNCTION__, chars);
775 return chars;
779 static void ti_throttle(struct usb_serial_port *port)
781 struct ti_port *tport = usb_get_serial_port_data(port);
782 struct tty_struct *tty;
784 dbg("%s - port %d", __FUNCTION__, port->number);
786 if (tport == NULL)
787 return;
789 tty = port->tty;
790 if (!tty) {
791 dbg("%s - no tty", __FUNCTION__);
792 return;
795 if (I_IXOFF(tty) || C_CRTSCTS(tty))
796 ti_stop_read(tport, tty);
801 static void ti_unthrottle(struct usb_serial_port *port)
803 struct ti_port *tport = usb_get_serial_port_data(port);
804 struct tty_struct *tty;
805 int status;
807 dbg("%s - port %d", __FUNCTION__, port->number);
809 if (tport == NULL)
810 return;
812 tty = port->tty;
813 if (!tty) {
814 dbg("%s - no tty", __FUNCTION__);
815 return;
818 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
819 status = ti_restart_read(tport, tty);
820 if (status)
821 dev_err(&port->dev, "%s - cannot restart read, %d\n", __FUNCTION__, status);
826 static int ti_ioctl(struct usb_serial_port *port, struct file *file,
827 unsigned int cmd, unsigned long arg)
829 struct ti_port *tport = usb_get_serial_port_data(port);
830 struct async_icount cnow;
831 struct async_icount cprev;
833 dbg("%s - port %d, cmd = 0x%04X", __FUNCTION__, port->number, cmd);
835 if (tport == NULL)
836 return -ENODEV;
838 switch (cmd) {
839 case TIOCGSERIAL:
840 dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
841 return ti_get_serial_info(tport, (struct serial_struct __user *)arg);
842 break;
844 case TIOCSSERIAL:
845 dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
846 return ti_set_serial_info(tport, (struct serial_struct __user *)arg);
847 break;
849 case TIOCMIWAIT:
850 dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
851 cprev = tport->tp_icount;
852 while (1) {
853 interruptible_sleep_on(&tport->tp_msr_wait);
854 if (signal_pending(current))
855 return -ERESTARTSYS;
856 cnow = tport->tp_icount;
857 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
858 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
859 return -EIO; /* no change => error */
860 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
861 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
862 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
863 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
864 return 0;
866 cprev = cnow;
868 break;
870 case TIOCGICOUNT:
871 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, tport->tp_icount.rx, tport->tp_icount.tx);
872 if (copy_to_user((void __user *)arg, &tport->tp_icount, sizeof(tport->tp_icount)))
873 return -EFAULT;
874 return 0;
877 return -ENOIOCTLCMD;
881 static void ti_set_termios(struct usb_serial_port *port,
882 struct termios *old_termios)
884 struct ti_port *tport = usb_get_serial_port_data(port);
885 struct tty_struct *tty = port->tty;
886 struct ti_uart_config *config;
887 tcflag_t cflag,iflag;
888 int baud;
889 int status;
890 int port_number = port->number - port->serial->minor;
891 unsigned int mcr;
893 dbg("%s - port %d", __FUNCTION__, port->number);
895 if (!tty || !tty->termios) {
896 dbg("%s - no tty or termios", __FUNCTION__);
897 return;
900 cflag = tty->termios->c_cflag;
901 iflag = tty->termios->c_iflag;
903 if (old_termios && cflag == old_termios->c_cflag
904 && iflag == old_termios->c_iflag) {
905 dbg("%s - nothing to change", __FUNCTION__);
906 return;
909 dbg("%s - clfag %08x, iflag %08x", __FUNCTION__, cflag, iflag);
911 if (old_termios)
912 dbg("%s - old clfag %08x, old iflag %08x", __FUNCTION__, old_termios->c_cflag, old_termios->c_iflag);
914 if (tport == NULL)
915 return;
917 config = kmalloc(sizeof(*config), GFP_KERNEL);
918 if (!config) {
919 dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
920 return;
923 config->wFlags = 0;
925 /* these flags must be set */
926 config->wFlags |= TI_UART_ENABLE_MS_INTS;
927 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
928 config->bUartMode = (__u8)(tport->tp_uart_mode);
930 switch (cflag & CSIZE) {
931 case CS5:
932 config->bDataBits = TI_UART_5_DATA_BITS;
933 break;
934 case CS6:
935 config->bDataBits = TI_UART_6_DATA_BITS;
936 break;
937 case CS7:
938 config->bDataBits = TI_UART_7_DATA_BITS;
939 break;
940 default:
941 case CS8:
942 config->bDataBits = TI_UART_8_DATA_BITS;
943 break;
946 if (cflag & PARENB) {
947 if (cflag & PARODD) {
948 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
949 config->bParity = TI_UART_ODD_PARITY;
950 } else {
951 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
952 config->bParity = TI_UART_EVEN_PARITY;
954 } else {
955 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
956 config->bParity = TI_UART_NO_PARITY;
959 if (cflag & CSTOPB)
960 config->bStopBits = TI_UART_2_STOP_BITS;
961 else
962 config->bStopBits = TI_UART_1_STOP_BITS;
964 if (cflag & CRTSCTS) {
965 /* RTS flow control must be off to drop RTS for baud rate B0 */
966 if ((cflag & CBAUD) != B0)
967 config->wFlags |= TI_UART_ENABLE_RTS_IN;
968 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
969 } else {
970 tty->hw_stopped = 0;
971 ti_restart_read(tport, tty);
974 if (I_IXOFF(tty) || I_IXON(tty)) {
975 config->cXon = START_CHAR(tty);
976 config->cXoff = STOP_CHAR(tty);
978 if (I_IXOFF(tty))
979 config->wFlags |= TI_UART_ENABLE_X_IN;
980 else
981 ti_restart_read(tport, tty);
983 if (I_IXON(tty))
984 config->wFlags |= TI_UART_ENABLE_X_OUT;
987 baud = tty_get_baud_rate(tty);
988 if (!baud) baud = 9600;
989 if (tport->tp_tdev->td_is_3410)
990 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
991 else
992 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
994 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
995 __FUNCTION__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
997 cpu_to_be16s(&config->wBaudRate);
998 cpu_to_be16s(&config->wFlags);
1000 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
1001 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
1002 sizeof(*config));
1003 if (status)
1004 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", __FUNCTION__, port_number, status);
1006 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
1007 mcr = tport->tp_shadow_mcr;
1008 /* if baud rate is B0, clear RTS and DTR */
1009 if ((cflag & CBAUD) == B0)
1010 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1011 status = ti_set_mcr(tport, mcr);
1012 if (status)
1013 dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n", __FUNCTION__, port_number, status);
1015 kfree(config);
1019 static int ti_tiocmget(struct usb_serial_port *port, struct file *file)
1021 struct ti_port *tport = usb_get_serial_port_data(port);
1022 unsigned int result;
1023 unsigned int msr;
1024 unsigned int mcr;
1026 dbg("%s - port %d", __FUNCTION__, port->number);
1028 if (tport == NULL)
1029 return -ENODEV;
1031 msr = tport->tp_msr;
1032 mcr = tport->tp_shadow_mcr;
1034 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1035 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1036 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1037 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1038 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1039 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1040 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1042 dbg("%s - 0x%04X", __FUNCTION__, result);
1044 return result;
1048 static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
1049 unsigned int set, unsigned int clear)
1051 struct ti_port *tport = usb_get_serial_port_data(port);
1052 unsigned int mcr;
1054 dbg("%s - port %d", __FUNCTION__, port->number);
1056 if (tport == NULL)
1057 return -ENODEV;
1059 mcr = tport->tp_shadow_mcr;
1061 if (set & TIOCM_RTS)
1062 mcr |= TI_MCR_RTS;
1063 if (set & TIOCM_DTR)
1064 mcr |= TI_MCR_DTR;
1065 if (set & TIOCM_LOOP)
1066 mcr |= TI_MCR_LOOP;
1068 if (clear & TIOCM_RTS)
1069 mcr &= ~TI_MCR_RTS;
1070 if (clear & TIOCM_DTR)
1071 mcr &= ~TI_MCR_DTR;
1072 if (clear & TIOCM_LOOP)
1073 mcr &= ~TI_MCR_LOOP;
1075 return ti_set_mcr(tport, mcr);
1079 static void ti_break(struct usb_serial_port *port, int break_state)
1081 struct ti_port *tport = usb_get_serial_port_data(port);
1082 int status;
1084 dbg("%s - state = %d", __FUNCTION__, break_state);
1086 if (tport == NULL)
1087 return;
1089 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1091 status = ti_write_byte(tport->tp_tdev,
1092 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1093 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1095 if (status)
1096 dbg("%s - error setting break, %d", __FUNCTION__, status);
1100 static void ti_interrupt_callback(struct urb *urb, struct pt_regs *regs)
1102 struct ti_device *tdev = (struct ti_device *)urb->context;
1103 struct usb_serial_port *port;
1104 struct usb_serial *serial = tdev->td_serial;
1105 struct ti_port *tport;
1106 struct device *dev = &urb->dev->dev;
1107 unsigned char *data = urb->transfer_buffer;
1108 int length = urb->actual_length;
1109 int port_number;
1110 int function;
1111 int status;
1112 __u8 msr;
1114 dbg("%s", __FUNCTION__);
1116 switch (urb->status) {
1117 case 0:
1118 break;
1119 case -ECONNRESET:
1120 case -ENOENT:
1121 case -ESHUTDOWN:
1122 dbg("%s - urb shutting down, %d", __FUNCTION__, urb->status);
1123 tdev->td_urb_error = 1;
1124 return;
1125 default:
1126 dev_err(dev, "%s - nonzero urb status, %d\n", __FUNCTION__, urb->status);
1127 tdev->td_urb_error = 1;
1128 goto exit;
1131 if (length != 2) {
1132 dbg("%s - bad packet size, %d", __FUNCTION__, length);
1133 goto exit;
1136 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1137 dev_err(dev, "%s - hardware error, %d\n", __FUNCTION__, data[1]);
1138 goto exit;
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", __FUNCTION__, port_number, function, data[1]);
1146 if (port_number >= serial->num_ports) {
1147 dev_err(dev, "%s - bad port number, %d\n", __FUNCTION__, port_number);
1148 goto exit;
1151 port = serial->port[port_number];
1153 tport = usb_get_serial_port_data(port);
1154 if (!tport)
1155 goto exit;
1157 switch (function) {
1158 case TI_CODE_DATA_ERROR:
1159 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", __FUNCTION__, port_number, data[1]);
1160 break;
1162 case TI_CODE_MODEM_STATUS:
1163 msr = data[1];
1164 dbg("%s - port %d, msr 0x%02X", __FUNCTION__, port_number, msr);
1165 ti_handle_new_msr(tport, msr);
1166 break;
1168 default:
1169 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", __FUNCTION__, data[1]);
1170 break;
1173 exit:
1174 status = usb_submit_urb(urb, GFP_ATOMIC);
1175 if (status)
1176 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n", __FUNCTION__, status);
1180 static void ti_bulk_in_callback(struct urb *urb, struct pt_regs *regs)
1182 struct ti_port *tport = (struct ti_port *)urb->context;
1183 struct usb_serial_port *port = tport->tp_port;
1184 struct device *dev = &urb->dev->dev;
1185 int status = 0;
1187 dbg("%s", __FUNCTION__);
1189 switch (urb->status) {
1190 case 0:
1191 break;
1192 case -ECONNRESET:
1193 case -ENOENT:
1194 case -ESHUTDOWN:
1195 dbg("%s - urb shutting down, %d", __FUNCTION__, urb->status);
1196 tport->tp_tdev->td_urb_error = 1;
1197 wake_up_interruptible(&tport->tp_write_wait);
1198 return;
1199 default:
1200 dev_err(dev, "%s - nonzero urb status, %d\n", __FUNCTION__, urb->status );
1201 tport->tp_tdev->td_urb_error = 1;
1202 wake_up_interruptible(&tport->tp_write_wait);
1205 if (urb->status == -EPIPE)
1206 goto exit;
1208 if (urb->status) {
1209 dev_err(dev, "%s - stopping read!\n", __FUNCTION__);
1210 return;
1213 if (port->tty && urb->actual_length) {
1214 usb_serial_debug_data(debug, dev, __FUNCTION__,
1215 urb->actual_length, urb->transfer_buffer);
1217 if (!tport->tp_is_open)
1218 dbg("%s - port closed, dropping data", __FUNCTION__);
1219 else
1220 ti_recv(&urb->dev->dev, port->tty, urb->transfer_buffer,
1221 urb->actual_length);
1223 spin_lock(&tport->tp_lock);
1224 tport->tp_icount.rx += urb->actual_length;
1225 spin_unlock(&tport->tp_lock);
1228 exit:
1229 /* continue to read unless stopping */
1230 spin_lock(&tport->tp_lock);
1231 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1232 urb->dev = port->serial->dev;
1233 status = usb_submit_urb(urb, GFP_ATOMIC);
1234 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1235 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1237 spin_unlock(&tport->tp_lock);
1238 if (status)
1239 dev_err(dev, "%s - resubmit read urb failed, %d\n", __FUNCTION__, status);
1243 static void ti_bulk_out_callback(struct urb *urb, struct pt_regs *regs)
1245 struct ti_port *tport = (struct ti_port *)urb->context;
1246 struct usb_serial_port *port = tport->tp_port;
1247 struct device *dev = &urb->dev->dev;
1249 dbg("%s - port %d", __FUNCTION__, port->number);
1251 tport->tp_write_urb_in_use = 0;
1253 switch (urb->status) {
1254 case 0:
1255 break;
1256 case -ECONNRESET:
1257 case -ENOENT:
1258 case -ESHUTDOWN:
1259 dbg("%s - urb shutting down, %d", __FUNCTION__, urb->status);
1260 tport->tp_tdev->td_urb_error = 1;
1261 wake_up_interruptible(&tport->tp_write_wait);
1262 return;
1263 default:
1264 dev_err(dev, "%s - nonzero urb status, %d\n", __FUNCTION__, urb->status);
1265 tport->tp_tdev->td_urb_error = 1;
1266 wake_up_interruptible(&tport->tp_write_wait);
1269 /* send any buffered data */
1270 ti_send(tport);
1274 static void ti_recv(struct device *dev, struct tty_struct *tty,
1275 unsigned char *data, int length)
1277 int cnt;
1279 do {
1280 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1281 tty_flip_buffer_push(tty);
1282 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
1283 dev_err(dev, "%s - dropping data, %d bytes lost\n", __FUNCTION__, length);
1284 return;
1287 cnt = min(length, TTY_FLIPBUF_SIZE - tty->flip.count);
1288 memcpy(tty->flip.char_buf_ptr, data, cnt);
1289 memset(tty->flip.flag_buf_ptr, 0, cnt);
1290 tty->flip.char_buf_ptr += cnt;
1291 tty->flip.flag_buf_ptr += cnt;
1292 tty->flip.count += cnt;
1293 data += cnt;
1294 length -= cnt;
1295 } while (length > 0);
1297 tty_flip_buffer_push(tty);
1301 static void ti_send(struct ti_port *tport)
1303 int count, result;
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", __FUNCTION__, 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);
1315 return;
1318 count = ti_buf_get(tport->tp_write_buf,
1319 port->write_urb->transfer_buffer,
1320 port->bulk_out_size);
1322 if (count == 0) {
1323 spin_unlock_irqrestore(&tport->tp_lock, flags);
1324 return;
1327 tport->tp_write_urb_in_use = 1;
1329 spin_unlock_irqrestore(&tport->tp_lock, flags);
1331 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, 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);
1340 if (result) {
1341 dev_err(&port->dev, "%s - submit write urb failed, %d\n", __FUNCTION__, result);
1342 tport->tp_write_urb_in_use = 0;
1343 /* TODO: reschedule ti_send */
1344 } else {
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 */
1351 if (tty)
1352 tty_wakeup(tty);
1353 wake_up_interruptible(&tport->tp_write_wait);
1357 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1359 int status;
1361 status = ti_write_byte(tport->tp_tdev,
1362 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1363 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1365 if (!status)
1366 tport->tp_shadow_mcr = mcr;
1368 return status;
1372 static int ti_get_lsr(struct ti_port *tport)
1374 int size,status;
1375 struct ti_device *tdev = tport->tp_tdev;
1376 struct usb_serial_port *port = tport->tp_port;
1377 int port_number = port->number - port->serial->minor;
1378 struct ti_port_status *data;
1380 dbg("%s - port %d", __FUNCTION__, port->number);
1382 size = sizeof(struct ti_port_status);
1383 data = kmalloc(size, GFP_KERNEL);
1384 if (!data) {
1385 dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
1386 return -ENOMEM;
1389 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1390 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1391 if (status) {
1392 dev_err(&port->dev, "%s - get port status command failed, %d\n", __FUNCTION__, status);
1393 goto free_data;
1396 dbg("%s - lsr 0x%02X", __FUNCTION__, data->bLSR);
1398 tport->tp_lsr = data->bLSR;
1400 free_data:
1401 kfree(data);
1402 return status;
1406 static int ti_get_serial_info(struct ti_port *tport,
1407 struct serial_struct __user *ret_arg)
1409 struct usb_serial_port *port = tport->tp_port;
1410 struct serial_struct ret_serial;
1412 if (!ret_arg)
1413 return -EFAULT;
1415 memset(&ret_serial, 0, sizeof(ret_serial));
1417 ret_serial.type = PORT_16550A;
1418 ret_serial.line = port->serial->minor;
1419 ret_serial.port = port->number - port->serial->minor;
1420 ret_serial.flags = tport->tp_flags;
1421 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1422 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1423 ret_serial.closing_wait = tport->tp_closing_wait;
1425 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1426 return -EFAULT;
1428 return 0;
1432 static int ti_set_serial_info(struct ti_port *tport,
1433 struct serial_struct __user *new_arg)
1435 struct usb_serial_port *port = tport->tp_port;
1436 struct serial_struct new_serial;
1438 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1439 return -EFAULT;
1441 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1442 if (port->tty)
1443 port->tty->low_latency =
1444 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1445 tport->tp_closing_wait = new_serial.closing_wait;
1447 return 0;
1451 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1453 struct async_icount *icount;
1454 struct tty_struct *tty;
1455 unsigned long flags;
1457 dbg("%s - msr 0x%02X", __FUNCTION__, msr);
1459 if (msr & TI_MSR_DELTA_MASK) {
1460 spin_lock_irqsave(&tport->tp_lock, flags);
1461 icount = &tport->tp_icount;
1462 if (msr & TI_MSR_DELTA_CTS)
1463 icount->cts++;
1464 if (msr & TI_MSR_DELTA_DSR)
1465 icount->dsr++;
1466 if (msr & TI_MSR_DELTA_CD)
1467 icount->dcd++;
1468 if (msr & TI_MSR_DELTA_RI)
1469 icount->rng++;
1470 wake_up_interruptible(&tport->tp_msr_wait);
1471 spin_unlock_irqrestore(&tport->tp_lock, flags);
1474 tport->tp_msr = msr & TI_MSR_MASK;
1476 /* handle CTS flow control */
1477 tty = tport->tp_port->tty;
1478 if (tty && C_CRTSCTS(tty)) {
1479 if (msr & TI_MSR_CTS) {
1480 tty->hw_stopped = 0;
1481 tty_wakeup(tty);
1482 } else {
1483 tty->hw_stopped = 1;
1489 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1491 struct ti_device *tdev = tport->tp_tdev;
1492 struct usb_serial_port *port = tport->tp_port;
1493 wait_queue_t wait;
1494 unsigned long flags;
1496 dbg("%s - port %d", __FUNCTION__, port->number);
1498 spin_lock_irqsave(&tport->tp_lock, flags);
1500 /* wait for data to drain from the buffer */
1501 tdev->td_urb_error = 0;
1502 init_waitqueue_entry(&wait, current);
1503 add_wait_queue(&tport->tp_write_wait, &wait);
1504 for (;;) {
1505 set_current_state(TASK_INTERRUPTIBLE);
1506 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1507 || timeout == 0 || signal_pending(current)
1508 || tdev->td_urb_error
1509 || !usb_get_intfdata(port->serial->interface)) /* disconnect */
1510 break;
1511 spin_unlock_irqrestore(&tport->tp_lock, flags);
1512 timeout = schedule_timeout(timeout);
1513 spin_lock_irqsave(&tport->tp_lock, flags);
1515 set_current_state(TASK_RUNNING);
1516 remove_wait_queue(&tport->tp_write_wait, &wait);
1518 /* flush any remaining data in the buffer */
1519 if (flush)
1520 ti_buf_clear(tport->tp_write_buf);
1522 spin_unlock_irqrestore(&tport->tp_lock, flags);
1524 /* wait for data to drain from the device */
1525 /* wait for empty tx register, plus 20 ms */
1526 timeout += jiffies;
1527 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1528 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1529 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1530 && usb_get_intfdata(port->serial->interface)) { /* not disconnected */
1531 if (ti_get_lsr(tport))
1532 break;
1533 msleep_interruptible(20);
1538 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1540 unsigned long flags;
1542 spin_lock_irqsave(&tport->tp_lock, flags);
1544 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1545 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1547 spin_unlock_irqrestore(&tport->tp_lock, flags);
1551 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1553 struct urb *urb;
1554 int status = 0;
1555 unsigned long flags;
1557 spin_lock_irqsave(&tport->tp_lock, flags);
1559 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1560 urb = tport->tp_port->read_urb;
1561 urb->complete = ti_bulk_in_callback;
1562 urb->context = tport;
1563 urb->dev = tport->tp_port->serial->dev;
1564 status = usb_submit_urb(urb, GFP_KERNEL);
1566 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1568 spin_unlock_irqrestore(&tport->tp_lock, flags);
1570 return status;
1574 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1575 __u16 moduleid, __u16 value, __u8 *data, int size)
1577 int status;
1579 status = usb_control_msg(tdev->td_serial->dev,
1580 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1581 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1582 value, moduleid, data, size, 1000);
1584 if (status == size)
1585 status = 0;
1587 if (status > 0)
1588 status = -ECOMM;
1590 return status;
1594 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1595 __u16 moduleid, __u16 value, __u8 *data, int size)
1597 int status;
1599 status = usb_control_msg(tdev->td_serial->dev,
1600 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1601 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1602 value, moduleid, data, size, 1000);
1604 if (status == size)
1605 status = 0;
1607 if (status > 0)
1608 status = -ECOMM;
1610 return status;
1614 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1615 __u8 mask, __u8 byte)
1617 int status;
1618 unsigned int size;
1619 struct ti_write_data_bytes *data;
1620 struct device *dev = &tdev->td_serial->dev->dev;
1622 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X", __FUNCTION__, addr, mask, byte);
1624 size = sizeof(struct ti_write_data_bytes) + 2;
1625 data = kmalloc(size, GFP_KERNEL);
1626 if (!data) {
1627 dev_err(dev, "%s - out of memory\n", __FUNCTION__);
1628 return -ENOMEM;
1631 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1632 data->bDataType = TI_RW_DATA_BYTE;
1633 data->bDataCounter = 1;
1634 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1635 data->wBaseAddrLo = cpu_to_be16(addr);
1636 data->bData[0] = mask;
1637 data->bData[1] = byte;
1639 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1640 (__u8 *)data, size);
1642 if (status < 0)
1643 dev_err(dev, "%s - failed, %d\n", __FUNCTION__, status);
1645 kfree(data);
1647 return status;
1651 static int ti_download_firmware(struct ti_device *tdev,
1652 unsigned char *firmware, unsigned int firmware_size)
1654 int status = 0;
1655 int buffer_size;
1656 int pos;
1657 int len;
1658 int done;
1659 __u8 cs = 0;
1660 __u8 *buffer;
1661 struct usb_device *dev = tdev->td_serial->dev;
1662 struct ti_firmware_header *header;
1663 unsigned int pipe = usb_sndbulkpipe(dev,
1664 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1667 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1668 buffer = kmalloc(buffer_size, GFP_KERNEL);
1669 if (!buffer) {
1670 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
1671 return -ENOMEM;
1674 memcpy(buffer, firmware, firmware_size);
1675 memset(buffer+firmware_size, 0xff, buffer_size-firmware_size);
1677 for(pos = sizeof(struct ti_firmware_header); pos < buffer_size; pos++)
1678 cs = (__u8)(cs + buffer[pos]);
1680 header = (struct ti_firmware_header *)buffer;
1681 header->wLength = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_firmware_header)));
1682 header->bCheckSum = cs;
1684 dbg("%s - downloading firmware", __FUNCTION__);
1685 for (pos = 0; pos < buffer_size; pos += done) {
1686 len = min(buffer_size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1687 status = usb_bulk_msg(dev, pipe, buffer+pos, len, &done, 1000);
1688 if (status)
1689 break;
1692 kfree(buffer);
1694 if (status) {
1695 dev_err(&dev->dev, "%s - error downloading firmware, %d\n", __FUNCTION__, status);
1696 return status;
1699 dbg("%s - download successful", __FUNCTION__);
1701 return 0;
1705 /* Circular Buffer Functions */
1708 * ti_buf_alloc
1710 * Allocate a circular buffer and all associated memory.
1713 static struct circ_buf *ti_buf_alloc(void)
1715 struct circ_buf *cb;
1717 cb = (struct circ_buf *)kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
1718 if (cb == NULL)
1719 return NULL;
1721 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1722 if (cb->buf == NULL) {
1723 kfree(cb);
1724 return NULL;
1727 ti_buf_clear(cb);
1729 return cb;
1734 * ti_buf_free
1736 * Free the buffer and all associated memory.
1739 static void ti_buf_free(struct circ_buf *cb)
1741 kfree(cb->buf);
1742 kfree(cb);
1747 * ti_buf_clear
1749 * Clear out all data in the circular buffer.
1752 static void ti_buf_clear(struct circ_buf *cb)
1754 cb->head = cb->tail = 0;
1759 * ti_buf_data_avail
1761 * Return the number of bytes of data available in the circular
1762 * buffer.
1765 static int ti_buf_data_avail(struct circ_buf *cb)
1767 return CIRC_CNT(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1772 * ti_buf_space_avail
1774 * Return the number of bytes of space available in the circular
1775 * buffer.
1778 static int ti_buf_space_avail(struct circ_buf *cb)
1780 return CIRC_SPACE(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1785 * ti_buf_put
1787 * Copy data data from a user buffer and put it into the circular buffer.
1788 * Restrict to the amount of space available.
1790 * Return the number of bytes copied.
1793 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1795 int c, ret = 0;
1797 while (1) {
1798 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1799 if (count < c)
1800 c = count;
1801 if (c <= 0)
1802 break;
1803 memcpy(cb->buf + cb->head, buf, c);
1804 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1805 buf += c;
1806 count -= c;
1807 ret += c;
1810 return ret;
1815 * ti_buf_get
1817 * Get data from the circular buffer and copy to the given buffer.
1818 * Restrict to the amount of data available.
1820 * Return the number of bytes copied.
1823 static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1825 int c, ret = 0;
1827 while (1) {
1828 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1829 if (count < c)
1830 c = count;
1831 if (c <= 0)
1832 break;
1833 memcpy(buf, cb->buf + cb->tail, c);
1834 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);
1835 buf += c;
1836 count -= c;
1837 ret += c;
1840 return ret;