ti_usb_3410_5052: use request_firmware()
[linux-2.6/btrfs-unstable.git] / drivers / usb / serial / ti_usb_3410_5052.c
bloba26a629dfc4f7d4f6d65ac5d0648537047f3f49c
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/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"
91 /* Defines */
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
117 /* Structures */
119 struct ti_port {
120 int tp_is_open;
121 __u8 tp_msr;
122 __u8 tp_lsr;
123 __u8 tp_shadow_mcr;
124 __u8 tp_uart_mode; /* 232 or 485 modes */
125 unsigned int tp_uart_base_addr;
126 int tp_flags;
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;
133 spinlock_t tp_lock;
134 int tp_read_urb_state;
135 int tp_write_urb_in_use;
136 struct circ_buf *tp_write_buf;
139 struct ti_device {
140 struct mutex td_open_close_lock;
141 int td_open_port_count;
142 struct usb_serial *td_serial;
143 int td_is_3410;
144 int td_urb_error;
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,
155 int count);
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);
208 /* Data */
210 /* module parameters */
211 static int debug;
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 */
226 /* null entry */
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,
254 .no_dynamic_id = 1,
257 static struct usb_serial_driver ti_1port_device = {
258 .driver = {
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,
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_driver ti_2port_device = {
286 .driver = {
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,
293 .num_ports = 2,
294 .attach = ti_startup,
295 .shutdown = ti_shutdown,
296 .open = ti_open,
297 .close = ti_close,
298 .write = ti_write,
299 .write_room = ti_write_room,
300 .chars_in_buffer = ti_chars_in_buffer,
301 .throttle = ti_throttle,
302 .unthrottle = ti_unthrottle,
303 .ioctl = ti_ioctl,
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,
314 /* Module */
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);
345 /* Functions */
347 static int __init ti_init(void)
349 int i,j;
350 int ret;
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);
367 if (ret)
368 goto failed_1port;
369 ret = usb_serial_register(&ti_2port_device);
370 if (ret)
371 goto failed_2port;
373 ret = usb_register(&ti_usb_driver);
374 if (ret)
375 goto failed_usb;
377 info(TI_DRIVER_DESC " " TI_DRIVER_VERSION);
379 return 0;
381 failed_usb:
382 usb_serial_deregister(&ti_2port_device);
383 failed_2port:
384 usb_serial_deregister(&ti_1port_device);
385 failed_1port:
386 return ret;
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;
407 int status;
408 int i;
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);
418 if (tdev == NULL) {
419 dev_err(&dev->dev, "%s - out of memory\n", __func__);
420 return -ENOMEM;
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");
436 else
437 status = ti_download_firmware(tdev, "ti_5052.fw");
438 if (status)
439 goto free_tdev;
441 /* 3410 must be reset, 5052 resets itself */
442 if (tdev->td_is_3410) {
443 msleep_interruptible(100);
444 usb_reset_device(dev);
447 status = -ENODEV;
448 goto free_tdev;
451 /* the second configuration must be set (in sysfs by hotplug script) */
452 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
453 status = -ENODEV;
454 goto free_tdev;
457 /* set up port structures */
458 for (i = 0; i < serial->num_ports; ++i) {
459 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
460 if (tport == NULL) {
461 dev_err(&dev->dev, "%s - out of memory\n", __func__);
462 status = -ENOMEM;
463 goto free_tports;
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__);
474 kfree(tport);
475 status = -ENOMEM;
476 goto free_tports;
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 */
484 return 0;
486 free_tports:
487 for (--i; i>=0; --i) {
488 tport = usb_get_serial_port_data(serial->port[i]);
489 ti_buf_free(tport->tp_write_buf);
490 kfree(tport);
491 usb_set_serial_port_data(serial->port[i], NULL);
493 free_tdev:
494 kfree(tdev);
495 usb_set_serial_data(serial, NULL);
496 return status;
500 static void ti_shutdown(struct usb_serial *serial)
502 int i;
503 struct ti_device *tdev = usb_get_serial_data(serial);
504 struct ti_port *tport;
506 dbg("%s", __func__);
508 for (i=0; i < serial->num_ports; ++i) {
509 tport = usb_get_serial_port_data(serial->port[i]);
510 if (tport) {
511 ti_buf_free(tport->tp_write_buf);
512 kfree(tport);
513 usb_set_serial_port_data(serial->port[i], NULL);
517 kfree(tdev);
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;
527 struct urb *urb;
528 int port_number;
529 int status;
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);
536 if (tport == NULL)
537 return -ENODEV;
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))
544 return -ERESTARTSYS;
546 if (port->tty)
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));
554 tport->tp_msr = 0;
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;
561 if (!urb) {
562 dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
563 status = -EINVAL;
564 goto release_lock;
566 urb->complete = ti_interrupt_callback;
567 urb->context = tdev;
568 urb->dev = dev;
569 status = usb_submit_urb(urb, GFP_KERNEL);
570 if (status) {
571 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
572 goto release_lock;
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);
581 if (status) {
582 dev_err(&port->dev, "%s - cannot send open command, %d\n", __func__, status);
583 goto unlink_int_urb;
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);
589 if (status) {
590 dev_err(&port->dev, "%s - cannot send start command, %d\n", __func__, status);
591 goto unlink_int_urb;
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);
597 if (status) {
598 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", __func__, status);
599 goto unlink_int_urb;
601 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
602 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
603 if (status) {
604 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", __func__, status);
605 goto unlink_int_urb;
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);
618 if (status) {
619 dev_err(&port->dev, "%s - cannot send open command (2), %d\n", __func__, status);
620 goto unlink_int_urb;
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);
626 if (status) {
627 dev_err(&port->dev, "%s - cannot send start command (2), %d\n", __func__, status);
628 goto unlink_int_urb;
631 /* start read urb */
632 dbg("%s - start read urb", __func__);
633 urb = port->read_urb;
634 if (!urb) {
635 dev_err(&port->dev, "%s - no read urb\n", __func__);
636 status = -EINVAL;
637 goto unlink_int_urb;
639 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
640 urb->complete = ti_bulk_in_callback;
641 urb->context = tport;
642 urb->dev = dev;
643 status = usb_submit_urb(urb, GFP_KERNEL);
644 if (status) {
645 dev_err(&port->dev, "%s - submit read urb failed, %d\n", __func__, status);
646 goto unlink_int_urb;
649 tport->tp_is_open = 1;
650 ++tdev->td_open_port_count;
652 goto release_lock;
654 unlink_int_urb:
655 if (tdev->td_open_port_count == 0)
656 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
657 release_lock:
658 mutex_unlock(&tdev->td_open_close_lock);
659 dbg("%s - exit %d", __func__, status);
660 return status;
664 static void ti_close(struct usb_serial_port *port, struct file *file)
666 struct ti_device *tdev;
667 struct ti_port *tport;
668 int port_number;
669 int status;
670 int do_unlock;
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)
677 return;
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);
692 if (status)
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;
703 if (do_unlock)
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,
711 int count)
713 struct ti_port *tport = usb_get_serial_port_data(port);
714 unsigned long flags;
716 dbg("%s - port %d", __func__, port->number);
718 if (count == 0) {
719 dbg("%s - write request of 0 bytes", __func__);
720 return 0;
723 if (tport == NULL || !tport->tp_is_open)
724 return -ENODEV;
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);
730 ti_send(tport);
732 return count;
736 static int ti_write_room(struct usb_serial_port *port)
738 struct ti_port *tport = usb_get_serial_port_data(port);
739 int room = 0;
740 unsigned long flags;
742 dbg("%s - port %d", __func__, port->number);
744 if (tport == NULL)
745 return -ENODEV;
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);
752 return room;
756 static int ti_chars_in_buffer(struct usb_serial_port *port)
758 struct ti_port *tport = usb_get_serial_port_data(port);
759 int chars = 0;
760 unsigned long flags;
762 dbg("%s - port %d", __func__, port->number);
764 if (tport == NULL)
765 return -ENODEV;
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);
772 return 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);
783 if (tport == NULL)
784 return;
786 tty = port->tty;
787 if (!tty) {
788 dbg("%s - no tty", __func__);
789 return;
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;
802 int status;
804 dbg("%s - port %d", __func__, port->number);
806 if (tport == NULL)
807 return;
809 tty = port->tty;
810 if (!tty) {
811 dbg("%s - no tty", __func__);
812 return;
815 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
816 status = ti_restart_read(tport, tty);
817 if (status)
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);
832 if (tport == NULL)
833 return -ENODEV;
835 switch (cmd) {
836 case TIOCGSERIAL:
837 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
838 return ti_get_serial_info(tport, (struct serial_struct __user *)arg);
839 break;
841 case TIOCSSERIAL:
842 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
843 return ti_set_serial_info(tport, (struct serial_struct __user *)arg);
844 break;
846 case TIOCMIWAIT:
847 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
848 cprev = tport->tp_icount;
849 while (1) {
850 interruptible_sleep_on(&tport->tp_msr_wait);
851 if (signal_pending(current))
852 return -ERESTARTSYS;
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)) ) {
861 return 0;
863 cprev = cnow;
865 break;
867 case TIOCGICOUNT:
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)))
870 return -EFAULT;
871 return 0;
874 return -ENOIOCTLCMD;
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;
885 int baud;
886 int status;
887 int port_number = port->number - port->serial->minor;
888 unsigned int mcr;
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);
898 if (tport == NULL)
899 return;
901 config = kmalloc(sizeof(*config), GFP_KERNEL);
902 if (!config) {
903 dev_err(&port->dev, "%s - out of memory\n", __func__);
904 return;
907 config->wFlags = 0;
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) {
915 case CS5:
916 config->bDataBits = TI_UART_5_DATA_BITS;
917 break;
918 case CS6:
919 config->bDataBits = TI_UART_6_DATA_BITS;
920 break;
921 case CS7:
922 config->bDataBits = TI_UART_7_DATA_BITS;
923 break;
924 default:
925 case CS8:
926 config->bDataBits = TI_UART_8_DATA_BITS;
927 break;
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;
937 } else {
938 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
939 config->bParity = TI_UART_EVEN_PARITY;
941 } else {
942 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
943 config->bParity = TI_UART_NO_PARITY;
946 if (cflag & CSTOPB)
947 config->bStopBits = TI_UART_2_STOP_BITS;
948 else
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;
956 } else {
957 tty->hw_stopped = 0;
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);
965 if (I_IXOFF(tty))
966 config->wFlags |= TI_UART_ENABLE_X_IN;
967 else
968 ti_restart_read(tport, tty);
970 if (I_IXON(tty))
971 config->wFlags |= TI_UART_ENABLE_X_OUT;
974 baud = tty_get_baud_rate(tty);
975 if (!baud)
976 baud = 9600;
977 if (tport->tp_tdev->td_is_3410)
978 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
979 else
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,
994 sizeof(*config));
995 if (status)
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);
1004 if (status)
1005 dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n", __func__, port_number, status);
1007 kfree(config);
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;
1015 unsigned int msr;
1016 unsigned int mcr;
1017 unsigned long flags;
1019 dbg("%s - port %d", __func__, port->number);
1021 if (tport == NULL)
1022 return -ENODEV;
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);
1039 return 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);
1047 unsigned int mcr;
1048 unsigned long flags;
1050 dbg("%s - port %d", __func__, port->number);
1052 if (tport == NULL)
1053 return -ENODEV;
1055 spin_lock_irqsave(&tport->tp_lock, flags);
1056 mcr = tport->tp_shadow_mcr;
1058 if (set & TIOCM_RTS)
1059 mcr |= TI_MCR_RTS;
1060 if (set & TIOCM_DTR)
1061 mcr |= TI_MCR_DTR;
1062 if (set & TIOCM_LOOP)
1063 mcr |= TI_MCR_LOOP;
1065 if (clear & TIOCM_RTS)
1066 mcr &= ~TI_MCR_RTS;
1067 if (clear & TIOCM_DTR)
1068 mcr &= ~TI_MCR_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);
1080 int status;
1082 dbg("%s - state = %d", __func__, break_state);
1084 if (tport == NULL)
1085 return;
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);
1093 if (status)
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;
1107 int port_number;
1108 int function;
1109 int status = urb->status;
1110 int retval;
1111 __u8 msr;
1113 dbg("%s", __func__);
1115 switch (status) {
1116 case 0:
1117 break;
1118 case -ECONNRESET:
1119 case -ENOENT:
1120 case -ESHUTDOWN:
1121 dbg("%s - urb shutting down, %d", __func__, status);
1122 tdev->td_urb_error = 1;
1123 return;
1124 default:
1125 dev_err(dev, "%s - nonzero urb status, %d\n",
1126 __func__, status);
1127 tdev->td_urb_error = 1;
1128 goto exit;
1131 if (length != 2) {
1132 dbg("%s - bad packet size, %d", __func__, length);
1133 goto exit;
1136 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1137 dev_err(dev, "%s - hardware error, %d\n", __func__, 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", __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);
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", __func__, port_number, data[1]);
1160 break;
1162 case TI_CODE_MODEM_STATUS:
1163 msr = data[1];
1164 dbg("%s - port %d, msr 0x%02X", __func__, 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", __func__, data[1]);
1170 break;
1173 exit:
1174 retval = usb_submit_urb(urb, GFP_ATOMIC);
1175 if (retval)
1176 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1177 __func__, retval);
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;
1187 int retval = 0;
1189 dbg("%s", __func__);
1191 switch (status) {
1192 case 0:
1193 break;
1194 case -ECONNRESET:
1195 case -ENOENT:
1196 case -ESHUTDOWN:
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);
1200 return;
1201 default:
1202 dev_err(dev, "%s - nonzero urb status, %d\n",
1203 __func__, status );
1204 tport->tp_tdev->td_urb_error = 1;
1205 wake_up_interruptible(&tport->tp_write_wait);
1208 if (status == -EPIPE)
1209 goto exit;
1211 if (status) {
1212 dev_err(dev, "%s - stopping read!\n", __func__);
1213 return;
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__);
1222 else
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);
1231 exit:
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);
1241 if (retval)
1242 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1243 __func__, retval);
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;
1258 switch (status) {
1259 case 0:
1260 break;
1261 case -ECONNRESET:
1262 case -ENOENT:
1263 case -ESHUTDOWN:
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);
1267 return;
1268 default:
1269 dev_err(dev, "%s - nonzero urb status, %d\n",
1270 __func__, status);
1271 tport->tp_tdev->td_urb_error = 1;
1272 wake_up_interruptible(&tport->tp_write_wait);
1275 /* send any buffered data */
1276 ti_send(tport);
1280 static void ti_recv(struct device *dev, struct tty_struct *tty,
1281 unsigned char *data, int length)
1283 int cnt;
1285 do {
1286 cnt = tty_buffer_request_room(tty, length);
1287 if (cnt < length) {
1288 dev_err(dev, "%s - dropping data, %d bytes lost\n", __func__, length - cnt);
1289 if(cnt == 0)
1290 break;
1292 tty_insert_flip_string(tty, data, cnt);
1293 tty_flip_buffer_push(tty);
1294 data += cnt;
1295 length -= cnt;
1296 } while (length > 0);
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", __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);
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, __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);
1340 if (result) {
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 */
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 unsigned long flags;
1360 int status;
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);
1367 if (!status)
1368 tport->tp_shadow_mcr = mcr;
1369 spin_unlock_irqrestore(&tport->tp_lock, flags);
1371 return status;
1375 static int ti_get_lsr(struct ti_port *tport)
1377 int size,status;
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);
1387 if (!data) {
1388 dev_err(&port->dev, "%s - out of memory\n", __func__);
1389 return -ENOMEM;
1392 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1393 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1394 if (status) {
1395 dev_err(&port->dev, "%s - get port status command failed, %d\n", __func__, status);
1396 goto free_data;
1399 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1401 tport->tp_lsr = data->bLSR;
1403 free_data:
1404 kfree(data);
1405 return status;
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;
1415 if (!ret_arg)
1416 return -EFAULT;
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)))
1429 return -EFAULT;
1431 return 0;
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)))
1442 return -EFAULT;
1444 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1445 if (port->tty)
1446 port->tty->low_latency =
1447 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1448 tport->tp_closing_wait = new_serial.closing_wait;
1450 return 0;
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)
1466 icount->cts++;
1467 if (msr & TI_MSR_DELTA_DSR)
1468 icount->dsr++;
1469 if (msr & TI_MSR_DELTA_CD)
1470 icount->dcd++;
1471 if (msr & TI_MSR_DELTA_RI)
1472 icount->rng++;
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;
1484 tty_wakeup(tty);
1485 } else {
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;
1496 wait_queue_t wait;
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);
1506 for (;;) {
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 */
1512 break;
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 */
1521 if (flush)
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 */
1529 timeout += jiffies;
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))
1535 break;
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)
1559 struct urb *urb;
1560 int status = 0;
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);
1573 } else {
1574 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1575 spin_unlock_irqrestore(&tport->tp_lock, flags);
1578 return status;
1582 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1583 __u16 moduleid, __u16 value, __u8 *data, int size)
1585 int status;
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);
1592 if (status == size)
1593 status = 0;
1595 if (status > 0)
1596 status = -ECOMM;
1598 return status;
1602 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1603 __u16 moduleid, __u16 value, __u8 *data, int size)
1605 int status;
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);
1612 if (status == size)
1613 status = 0;
1615 if (status > 0)
1616 status = -ECOMM;
1618 return status;
1622 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1623 __u8 mask, __u8 byte)
1625 int status;
1626 unsigned int size;
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);
1634 if (!data) {
1635 dev_err(dev, "%s - out of memory\n", __func__);
1636 return -ENOMEM;
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);
1650 if (status < 0)
1651 dev_err(dev, "%s - failed, %d\n", __func__, status);
1653 kfree(data);
1655 return status;
1659 static int ti_download_firmware(struct ti_device *tdev,
1660 char *fw_name)
1662 const struct firmware *fw;
1663 int status = 0;
1664 int buffer_size;
1665 int pos;
1666 int len;
1667 int done;
1668 __u8 cs = 0;
1669 __u8 *buffer;
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",
1679 __func__, fw_name);
1680 return -ENOENT;
1682 if (fw->size > buffer_size) {
1683 dev_err(&dev->dev, "%s - firmware \"%s\" is too large\n",
1684 __func__, fw_name);
1685 release_firmware(fw);
1686 return -EINVAL;
1689 buffer = kmalloc(buffer_size, GFP_KERNEL);
1690 if (!buffer) {
1691 dev_err(&dev->dev, "%s - out of memory\n", __func__);
1692 release_firmware(fw);
1693 return -ENOMEM;
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);
1710 if (status)
1711 break;
1714 kfree(buffer);
1715 release_firmware(fw);
1717 if (status) {
1718 dev_err(&dev->dev, "%s - error downloading firmware, %d\n", __func__, status);
1719 return status;
1722 dbg("%s - download successful", __func__);
1724 return 0;
1728 /* Circular Buffer Functions */
1731 * ti_buf_alloc
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);
1741 if (cb == NULL)
1742 return NULL;
1744 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1745 if (cb->buf == NULL) {
1746 kfree(cb);
1747 return NULL;
1750 ti_buf_clear(cb);
1752 return cb;
1757 * ti_buf_free
1759 * Free the buffer and all associated memory.
1762 static void ti_buf_free(struct circ_buf *cb)
1764 kfree(cb->buf);
1765 kfree(cb);
1770 * ti_buf_clear
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;
1782 * ti_buf_data_avail
1784 * Return the number of bytes of data available in the circular
1785 * buffer.
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
1798 * buffer.
1801 static int ti_buf_space_avail(struct circ_buf *cb)
1803 return CIRC_SPACE(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1808 * ti_buf_put
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)
1818 int c, ret = 0;
1820 while (1) {
1821 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1822 if (count < c)
1823 c = count;
1824 if (c <= 0)
1825 break;
1826 memcpy(cb->buf + cb->head, buf, c);
1827 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1828 buf += c;
1829 count -= c;
1830 ret += c;
1833 return ret;
1838 * ti_buf_get
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)
1848 int c, ret = 0;
1850 while (1) {
1851 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1852 if (count < c)
1853 c = count;
1854 if (c <= 0)
1855 break;
1856 memcpy(buf, cb->buf + cb->tail, c);
1857 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);
1858 buf += c;
1859 count -= c;
1860 ret += c;
1863 return ret;