Char: sx, remove bogus iomap
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / serial / ti_usb_3410_5052.c
blob31c42d1cae13cf746e49cce12c9f588247da2c5c
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/firmware.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 <linux/mutex.h>
85 #include <linux/uaccess.h>
86 #include <linux/usb.h>
87 #include <linux/usb/serial.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 tty_struct *tty, struct usb_serial_port *port,
153 struct file *file);
154 static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
155 struct file *file);
156 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
157 const unsigned char *data, int count);
158 static int ti_write_room(struct tty_struct *tty);
159 static int ti_chars_in_buffer(struct tty_struct *tty);
160 static void ti_throttle(struct tty_struct *tty);
161 static void ti_unthrottle(struct tty_struct *tty);
162 static int ti_ioctl(struct tty_struct *tty, struct file *file,
163 unsigned int cmd, unsigned long arg);
164 static void ti_set_termios(struct tty_struct *tty,
165 struct usb_serial_port *port, struct ktermios *old_termios);
166 static int ti_tiocmget(struct tty_struct *tty, struct file *file);
167 static int ti_tiocmset(struct tty_struct *tty, struct file *file,
168 unsigned int set, unsigned int clear);
169 static void ti_break(struct tty_struct *tty, int break_state);
170 static void ti_interrupt_callback(struct urb *urb);
171 static void ti_bulk_in_callback(struct urb *urb);
172 static void ti_bulk_out_callback(struct urb *urb);
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 tty_struct *tty, 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, int type);
200 /* circular buffer */
201 static struct circ_buf *ti_buf_alloc(void);
202 static void ti_buf_free(struct circ_buf *cb);
203 static void ti_buf_clear(struct circ_buf *cb);
204 static int ti_buf_data_avail(struct circ_buf *cb);
205 static int ti_buf_space_avail(struct circ_buf *cb);
206 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
207 static int ti_buf_get(struct circ_buf *cb, char *buf, int count);
210 /* Data */
212 /* module parameters */
213 static int debug;
214 static int low_latency = TI_DEFAULT_LOW_LATENCY;
215 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
216 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
217 static unsigned int vendor_3410_count;
218 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
219 static unsigned int product_3410_count;
220 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
221 static unsigned int vendor_5052_count;
222 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
223 static unsigned int product_5052_count;
225 /* supported devices */
226 /* the array dimension is the number of default entries plus */
227 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
228 /* null entry */
229 static struct usb_device_id ti_id_table_3410[1+TI_EXTRA_VID_PID_COUNT+1] = {
230 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
231 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
234 static struct usb_device_id ti_id_table_5052[4+TI_EXTRA_VID_PID_COUNT+1] = {
235 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
236 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
237 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
238 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
241 static struct usb_device_id ti_id_table_combined[] = {
242 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
243 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
244 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
245 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
246 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
247 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
251 static struct usb_driver ti_usb_driver = {
252 .name = "ti_usb_3410_5052",
253 .probe = usb_serial_probe,
254 .disconnect = usb_serial_disconnect,
255 .id_table = ti_id_table_combined,
256 .no_dynamic_id = 1,
259 static struct usb_serial_driver ti_1port_device = {
260 .driver = {
261 .owner = THIS_MODULE,
262 .name = "ti_usb_3410_5052_1",
264 .description = "TI USB 3410 1 port adapter",
265 .usb_driver = &ti_usb_driver,
266 .id_table = ti_id_table_3410,
267 .num_ports = 1,
268 .attach = ti_startup,
269 .shutdown = ti_shutdown,
270 .open = ti_open,
271 .close = ti_close,
272 .write = ti_write,
273 .write_room = ti_write_room,
274 .chars_in_buffer = ti_chars_in_buffer,
275 .throttle = ti_throttle,
276 .unthrottle = ti_unthrottle,
277 .ioctl = ti_ioctl,
278 .set_termios = ti_set_termios,
279 .tiocmget = ti_tiocmget,
280 .tiocmset = ti_tiocmset,
281 .break_ctl = ti_break,
282 .read_int_callback = ti_interrupt_callback,
283 .read_bulk_callback = ti_bulk_in_callback,
284 .write_bulk_callback = ti_bulk_out_callback,
287 static struct usb_serial_driver ti_2port_device = {
288 .driver = {
289 .owner = THIS_MODULE,
290 .name = "ti_usb_3410_5052_2",
292 .description = "TI USB 5052 2 port adapter",
293 .usb_driver = &ti_usb_driver,
294 .id_table = ti_id_table_5052,
295 .num_ports = 2,
296 .attach = ti_startup,
297 .shutdown = ti_shutdown,
298 .open = ti_open,
299 .close = ti_close,
300 .write = ti_write,
301 .write_room = ti_write_room,
302 .chars_in_buffer = ti_chars_in_buffer,
303 .throttle = ti_throttle,
304 .unthrottle = ti_unthrottle,
305 .ioctl = ti_ioctl,
306 .set_termios = ti_set_termios,
307 .tiocmget = ti_tiocmget,
308 .tiocmset = ti_tiocmset,
309 .break_ctl = ti_break,
310 .read_int_callback = ti_interrupt_callback,
311 .read_bulk_callback = ti_bulk_in_callback,
312 .write_bulk_callback = ti_bulk_out_callback,
316 /* Module */
318 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
319 MODULE_DESCRIPTION(TI_DRIVER_DESC);
320 MODULE_VERSION(TI_DRIVER_VERSION);
321 MODULE_LICENSE("GPL");
323 MODULE_FIRMWARE("ti_3410.fw");
324 MODULE_FIRMWARE("ti_5052.fw");
326 module_param(debug, bool, S_IRUGO | S_IWUSR);
327 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
329 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
330 MODULE_PARM_DESC(low_latency,
331 "TTY low_latency flag, 0=off, 1=on, default is off");
333 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
334 MODULE_PARM_DESC(closing_wait,
335 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
337 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
338 MODULE_PARM_DESC(vendor_3410,
339 "Vendor ids for 3410 based devices, 1-5 short integers");
340 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
341 MODULE_PARM_DESC(product_3410,
342 "Product ids for 3410 based devices, 1-5 short integers");
343 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
344 MODULE_PARM_DESC(vendor_5052,
345 "Vendor ids for 5052 based devices, 1-5 short integers");
346 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
347 MODULE_PARM_DESC(product_5052,
348 "Product ids for 5052 based devices, 1-5 short integers");
350 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
353 /* Functions */
355 static int __init ti_init(void)
357 int i, j;
358 int ret;
360 /* insert extra vendor and product ids */
361 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
362 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++) {
363 ti_id_table_3410[j].idVendor = vendor_3410[i];
364 ti_id_table_3410[j].idProduct = product_3410[i];
365 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
367 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
368 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++) {
369 ti_id_table_5052[j].idVendor = vendor_5052[i];
370 ti_id_table_5052[j].idProduct = product_5052[i];
371 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
374 ret = usb_serial_register(&ti_1port_device);
375 if (ret)
376 goto failed_1port;
377 ret = usb_serial_register(&ti_2port_device);
378 if (ret)
379 goto failed_2port;
381 ret = usb_register(&ti_usb_driver);
382 if (ret)
383 goto failed_usb;
385 printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":"
386 TI_DRIVER_DESC "\n");
388 return 0;
390 failed_usb:
391 usb_serial_deregister(&ti_2port_device);
392 failed_2port:
393 usb_serial_deregister(&ti_1port_device);
394 failed_1port:
395 return ret;
399 static void __exit ti_exit(void)
401 usb_serial_deregister(&ti_1port_device);
402 usb_serial_deregister(&ti_2port_device);
403 usb_deregister(&ti_usb_driver);
407 module_init(ti_init);
408 module_exit(ti_exit);
411 static int ti_startup(struct usb_serial *serial)
413 struct ti_device *tdev;
414 struct ti_port *tport;
415 struct usb_device *dev = serial->dev;
416 int status;
417 int i;
420 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
421 __func__, le16_to_cpu(dev->descriptor.idProduct),
422 dev->descriptor.bNumConfigurations,
423 dev->actconfig->desc.bConfigurationValue);
425 /* create device structure */
426 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
427 if (tdev == NULL) {
428 dev_err(&dev->dev, "%s - out of memory\n", __func__);
429 return -ENOMEM;
431 mutex_init(&tdev->td_open_close_lock);
432 tdev->td_serial = serial;
433 usb_set_serial_data(serial, tdev);
435 /* determine device type */
436 if (usb_match_id(serial->interface, ti_id_table_3410))
437 tdev->td_is_3410 = 1;
438 dbg("%s - device type is %s", __func__,
439 tdev->td_is_3410 ? "3410" : "5052");
441 /* if we have only 1 configuration, download firmware */
442 if (dev->descriptor.bNumConfigurations == 1) {
443 if (tdev->td_is_3410)
444 status = ti_download_firmware(tdev, 3410);
445 else
446 status = ti_download_firmware(tdev, 5052);
447 if (status)
448 goto free_tdev;
450 /* 3410 must be reset, 5052 resets itself */
451 if (tdev->td_is_3410) {
452 msleep_interruptible(100);
453 usb_reset_device(dev);
456 status = -ENODEV;
457 goto free_tdev;
460 /* the second configuration must be set (in sysfs by hotplug script) */
461 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
462 status = -ENODEV;
463 goto free_tdev;
466 /* set up port structures */
467 for (i = 0; i < serial->num_ports; ++i) {
468 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
469 if (tport == NULL) {
470 dev_err(&dev->dev, "%s - out of memory\n", __func__);
471 status = -ENOMEM;
472 goto free_tports;
474 spin_lock_init(&tport->tp_lock);
475 tport->tp_uart_base_addr = (i == 0 ?
476 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
477 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
478 tport->tp_closing_wait = closing_wait;
479 init_waitqueue_head(&tport->tp_msr_wait);
480 init_waitqueue_head(&tport->tp_write_wait);
481 tport->tp_write_buf = ti_buf_alloc();
482 if (tport->tp_write_buf == NULL) {
483 dev_err(&dev->dev, "%s - out of memory\n", __func__);
484 kfree(tport);
485 status = -ENOMEM;
486 goto free_tports;
488 tport->tp_port = serial->port[i];
489 tport->tp_tdev = tdev;
490 usb_set_serial_port_data(serial->port[i], tport);
491 tport->tp_uart_mode = 0; /* default is RS232 */
494 return 0;
496 free_tports:
497 for (--i; i >= 0; --i) {
498 tport = usb_get_serial_port_data(serial->port[i]);
499 ti_buf_free(tport->tp_write_buf);
500 kfree(tport);
501 usb_set_serial_port_data(serial->port[i], NULL);
503 free_tdev:
504 kfree(tdev);
505 usb_set_serial_data(serial, NULL);
506 return status;
510 static void ti_shutdown(struct usb_serial *serial)
512 int i;
513 struct ti_device *tdev = usb_get_serial_data(serial);
514 struct ti_port *tport;
516 dbg("%s", __func__);
518 for (i = 0; i < serial->num_ports; ++i) {
519 tport = usb_get_serial_port_data(serial->port[i]);
520 if (tport) {
521 ti_buf_free(tport->tp_write_buf);
522 kfree(tport);
523 usb_set_serial_port_data(serial->port[i], NULL);
527 kfree(tdev);
528 usb_set_serial_data(serial, NULL);
532 static int ti_open(struct tty_struct *tty,
533 struct usb_serial_port *port, struct file *file)
535 struct ti_port *tport = usb_get_serial_port_data(port);
536 struct ti_device *tdev;
537 struct usb_device *dev;
538 struct urb *urb;
539 int port_number;
540 int status;
541 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
542 TI_PIPE_TIMEOUT_ENABLE |
543 (TI_TRANSFER_TIMEOUT << 2));
545 dbg("%s - port %d", __func__, port->number);
547 if (tport == NULL)
548 return -ENODEV;
550 dev = port->serial->dev;
551 tdev = tport->tp_tdev;
553 /* only one open on any port on a device at a time */
554 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
555 return -ERESTARTSYS;
557 if (tty)
558 tty->low_latency =
559 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
561 port_number = port->number - port->serial->minor;
563 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
565 tport->tp_msr = 0;
566 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
568 /* start interrupt urb the first time a port is opened on this device */
569 if (tdev->td_open_port_count == 0) {
570 dbg("%s - start interrupt in urb", __func__);
571 urb = tdev->td_serial->port[0]->interrupt_in_urb;
572 if (!urb) {
573 dev_err(&port->dev, "%s - no interrupt urb\n",
574 __func__);
575 status = -EINVAL;
576 goto release_lock;
578 urb->complete = ti_interrupt_callback;
579 urb->context = tdev;
580 urb->dev = dev;
581 status = usb_submit_urb(urb, GFP_KERNEL);
582 if (status) {
583 dev_err(&port->dev,
584 "%s - submit interrupt urb failed, %d\n",
585 __func__, status);
586 goto release_lock;
590 if (tty)
591 ti_set_termios(tty, port, tty->termios);
593 dbg("%s - sending TI_OPEN_PORT", __func__);
594 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
595 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
596 if (status) {
597 dev_err(&port->dev, "%s - cannot send open command, %d\n",
598 __func__, status);
599 goto unlink_int_urb;
602 dbg("%s - sending TI_START_PORT", __func__);
603 status = ti_command_out_sync(tdev, TI_START_PORT,
604 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
605 if (status) {
606 dev_err(&port->dev, "%s - cannot send start command, %d\n",
607 __func__, status);
608 goto unlink_int_urb;
611 dbg("%s - sending TI_PURGE_PORT", __func__);
612 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
613 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
614 if (status) {
615 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
616 __func__, status);
617 goto unlink_int_urb;
619 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
620 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
621 if (status) {
622 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
623 __func__, status);
624 goto unlink_int_urb;
627 /* reset the data toggle on the bulk endpoints to work around bug in
628 * host controllers where things get out of sync some times */
629 usb_clear_halt(dev, port->write_urb->pipe);
630 usb_clear_halt(dev, port->read_urb->pipe);
632 if (tty)
633 ti_set_termios(tty, port, tty->termios);
635 dbg("%s - sending TI_OPEN_PORT (2)", __func__);
636 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
637 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
638 if (status) {
639 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
640 __func__, status);
641 goto unlink_int_urb;
644 dbg("%s - sending TI_START_PORT (2)", __func__);
645 status = ti_command_out_sync(tdev, TI_START_PORT,
646 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
647 if (status) {
648 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
649 __func__, status);
650 goto unlink_int_urb;
653 /* start read urb */
654 dbg("%s - start read urb", __func__);
655 urb = port->read_urb;
656 if (!urb) {
657 dev_err(&port->dev, "%s - no read urb\n", __func__);
658 status = -EINVAL;
659 goto unlink_int_urb;
661 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
662 urb->complete = ti_bulk_in_callback;
663 urb->context = tport;
664 urb->dev = dev;
665 status = usb_submit_urb(urb, GFP_KERNEL);
666 if (status) {
667 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
668 __func__, status);
669 goto unlink_int_urb;
672 tport->tp_is_open = 1;
673 ++tdev->td_open_port_count;
675 goto release_lock;
677 unlink_int_urb:
678 if (tdev->td_open_port_count == 0)
679 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
680 release_lock:
681 mutex_unlock(&tdev->td_open_close_lock);
682 dbg("%s - exit %d", __func__, status);
683 return status;
687 static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
688 struct file *file)
690 struct ti_device *tdev;
691 struct ti_port *tport;
692 int port_number;
693 int status;
694 int do_unlock;
696 dbg("%s - port %d", __func__, port->number);
698 tdev = usb_get_serial_data(port->serial);
699 tport = usb_get_serial_port_data(port);
700 if (tdev == NULL || tport == NULL)
701 return;
703 tport->tp_is_open = 0;
705 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
707 usb_kill_urb(port->read_urb);
708 usb_kill_urb(port->write_urb);
709 tport->tp_write_urb_in_use = 0;
711 port_number = port->number - port->serial->minor;
713 dbg("%s - sending TI_CLOSE_PORT", __func__);
714 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
715 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
716 if (status)
717 dev_err(&port->dev,
718 "%s - cannot send close port command, %d\n"
719 , __func__, status);
721 /* if mutex_lock is interrupted, continue anyway */
722 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
723 --tport->tp_tdev->td_open_port_count;
724 if (tport->tp_tdev->td_open_port_count <= 0) {
725 /* last port is closed, shut down interrupt urb */
726 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
727 tport->tp_tdev->td_open_port_count = 0;
729 if (do_unlock)
730 mutex_unlock(&tdev->td_open_close_lock);
732 dbg("%s - exit", __func__);
736 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
737 const unsigned char *data, int count)
739 struct ti_port *tport = usb_get_serial_port_data(port);
740 unsigned long flags;
742 dbg("%s - port %d", __func__, port->number);
744 if (count == 0) {
745 dbg("%s - write request of 0 bytes", __func__);
746 return 0;
749 if (tport == NULL || !tport->tp_is_open)
750 return -ENODEV;
752 spin_lock_irqsave(&tport->tp_lock, flags);
753 count = ti_buf_put(tport->tp_write_buf, data, count);
754 spin_unlock_irqrestore(&tport->tp_lock, flags);
756 ti_send(tport);
758 return count;
762 static int ti_write_room(struct tty_struct *tty)
764 struct usb_serial_port *port = tty->driver_data;
765 struct ti_port *tport = usb_get_serial_port_data(port);
766 int room = 0;
767 unsigned long flags;
769 dbg("%s - port %d", __func__, port->number);
771 if (tport == NULL)
772 return -ENODEV;
774 spin_lock_irqsave(&tport->tp_lock, flags);
775 room = ti_buf_space_avail(tport->tp_write_buf);
776 spin_unlock_irqrestore(&tport->tp_lock, flags);
778 dbg("%s - returns %d", __func__, room);
779 return room;
783 static int ti_chars_in_buffer(struct tty_struct *tty)
785 struct usb_serial_port *port = tty->driver_data;
786 struct ti_port *tport = usb_get_serial_port_data(port);
787 int chars = 0;
788 unsigned long flags;
790 dbg("%s - port %d", __func__, port->number);
792 if (tport == NULL)
793 return -ENODEV;
795 spin_lock_irqsave(&tport->tp_lock, flags);
796 chars = ti_buf_data_avail(tport->tp_write_buf);
797 spin_unlock_irqrestore(&tport->tp_lock, flags);
799 dbg("%s - returns %d", __func__, chars);
800 return chars;
804 static void ti_throttle(struct tty_struct *tty)
806 struct usb_serial_port *port = tty->driver_data;
807 struct ti_port *tport = usb_get_serial_port_data(port);
809 dbg("%s - port %d", __func__, port->number);
811 if (tport == NULL)
812 return;
814 if (I_IXOFF(tty) || C_CRTSCTS(tty))
815 ti_stop_read(tport, tty);
820 static void ti_unthrottle(struct tty_struct *tty)
822 struct usb_serial_port *port = tty->driver_data;
823 struct ti_port *tport = usb_get_serial_port_data(port);
824 int status;
826 dbg("%s - port %d", __func__, port->number);
828 if (tport == NULL)
829 return;
831 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
832 status = ti_restart_read(tport, tty);
833 if (status)
834 dev_err(&port->dev, "%s - cannot restart read, %d\n",
835 __func__, status);
840 static int ti_ioctl(struct tty_struct *tty, struct file *file,
841 unsigned int cmd, unsigned long arg)
843 struct usb_serial_port *port = tty->driver_data;
844 struct ti_port *tport = usb_get_serial_port_data(port);
845 struct async_icount cnow;
846 struct async_icount cprev;
848 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
850 if (tport == NULL)
851 return -ENODEV;
853 switch (cmd) {
854 case TIOCGSERIAL:
855 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
856 return ti_get_serial_info(tport,
857 (struct serial_struct __user *)arg);
858 case TIOCSSERIAL:
859 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
860 return ti_set_serial_info(tty, tport,
861 (struct serial_struct __user *)arg);
862 case TIOCMIWAIT:
863 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
864 cprev = tport->tp_icount;
865 while (1) {
866 interruptible_sleep_on(&tport->tp_msr_wait);
867 if (signal_pending(current))
868 return -ERESTARTSYS;
869 cnow = tport->tp_icount;
870 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
871 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
872 return -EIO; /* no change => error */
873 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
874 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
875 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
876 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
877 return 0;
878 cprev = cnow;
880 break;
881 case TIOCGICOUNT:
882 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
883 __func__, port->number,
884 tport->tp_icount.rx, tport->tp_icount.tx);
885 if (copy_to_user((void __user *)arg, &tport->tp_icount,
886 sizeof(tport->tp_icount)))
887 return -EFAULT;
888 return 0;
890 return -ENOIOCTLCMD;
894 static void ti_set_termios(struct tty_struct *tty,
895 struct usb_serial_port *port, struct ktermios *old_termios)
897 struct ti_port *tport = usb_get_serial_port_data(port);
898 struct ti_uart_config *config;
899 tcflag_t cflag, iflag;
900 int baud;
901 int status;
902 int port_number = port->number - port->serial->minor;
903 unsigned int mcr;
905 dbg("%s - port %d", __func__, port->number);
907 cflag = tty->termios->c_cflag;
908 iflag = tty->termios->c_iflag;
910 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
911 dbg("%s - old clfag %08x, old iflag %08x", __func__,
912 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", __func__);
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 /* CMSPAR isn't supported by this driver */
947 tty->termios->c_cflag &= ~CMSPAR;
949 if (cflag & PARENB) {
950 if (cflag & PARODD) {
951 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
952 config->bParity = TI_UART_ODD_PARITY;
953 } else {
954 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
955 config->bParity = TI_UART_EVEN_PARITY;
957 } else {
958 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
959 config->bParity = TI_UART_NO_PARITY;
962 if (cflag & CSTOPB)
963 config->bStopBits = TI_UART_2_STOP_BITS;
964 else
965 config->bStopBits = TI_UART_1_STOP_BITS;
967 if (cflag & CRTSCTS) {
968 /* RTS flow control must be off to drop RTS for baud rate B0 */
969 if ((cflag & CBAUD) != B0)
970 config->wFlags |= TI_UART_ENABLE_RTS_IN;
971 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
972 } else {
973 tty->hw_stopped = 0;
974 ti_restart_read(tport, tty);
977 if (I_IXOFF(tty) || I_IXON(tty)) {
978 config->cXon = START_CHAR(tty);
979 config->cXoff = STOP_CHAR(tty);
981 if (I_IXOFF(tty))
982 config->wFlags |= TI_UART_ENABLE_X_IN;
983 else
984 ti_restart_read(tport, tty);
986 if (I_IXON(tty))
987 config->wFlags |= TI_UART_ENABLE_X_OUT;
990 baud = tty_get_baud_rate(tty);
991 if (!baud)
992 baud = 9600;
993 if (tport->tp_tdev->td_is_3410)
994 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
995 else
996 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
998 /* FIXME: Should calculate resulting baud here and report it back */
999 if ((cflag & CBAUD) != B0)
1000 tty_encode_baud_rate(tty, baud, baud);
1002 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
1003 __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
1005 cpu_to_be16s(&config->wBaudRate);
1006 cpu_to_be16s(&config->wFlags);
1008 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
1009 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
1010 sizeof(*config));
1011 if (status)
1012 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
1013 __func__, port_number, status);
1015 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
1016 mcr = tport->tp_shadow_mcr;
1017 /* if baud rate is B0, clear RTS and DTR */
1018 if ((cflag & CBAUD) == B0)
1019 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1020 status = ti_set_mcr(tport, mcr);
1021 if (status)
1022 dev_err(&port->dev,
1023 "%s - cannot set modem control on port %d, %d\n",
1024 __func__, port_number, status);
1026 kfree(config);
1030 static int ti_tiocmget(struct tty_struct *tty, struct file *file)
1032 struct usb_serial_port *port = tty->driver_data;
1033 struct ti_port *tport = usb_get_serial_port_data(port);
1034 unsigned int result;
1035 unsigned int msr;
1036 unsigned int mcr;
1037 unsigned long flags;
1039 dbg("%s - port %d", __func__, port->number);
1041 if (tport == NULL)
1042 return -ENODEV;
1044 spin_lock_irqsave(&tport->tp_lock, flags);
1045 msr = tport->tp_msr;
1046 mcr = tport->tp_shadow_mcr;
1047 spin_unlock_irqrestore(&tport->tp_lock, flags);
1049 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1050 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1051 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1052 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1053 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1054 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1055 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1057 dbg("%s - 0x%04X", __func__, result);
1059 return result;
1063 static int ti_tiocmset(struct tty_struct *tty, struct file *file,
1064 unsigned int set, unsigned int clear)
1066 struct usb_serial_port *port = tty->driver_data;
1067 struct ti_port *tport = usb_get_serial_port_data(port);
1068 unsigned int mcr;
1069 unsigned long flags;
1071 dbg("%s - port %d", __func__, port->number);
1073 if (tport == NULL)
1074 return -ENODEV;
1076 spin_lock_irqsave(&tport->tp_lock, flags);
1077 mcr = tport->tp_shadow_mcr;
1079 if (set & TIOCM_RTS)
1080 mcr |= TI_MCR_RTS;
1081 if (set & TIOCM_DTR)
1082 mcr |= TI_MCR_DTR;
1083 if (set & TIOCM_LOOP)
1084 mcr |= TI_MCR_LOOP;
1086 if (clear & TIOCM_RTS)
1087 mcr &= ~TI_MCR_RTS;
1088 if (clear & TIOCM_DTR)
1089 mcr &= ~TI_MCR_DTR;
1090 if (clear & TIOCM_LOOP)
1091 mcr &= ~TI_MCR_LOOP;
1092 spin_unlock_irqrestore(&tport->tp_lock, flags);
1094 return ti_set_mcr(tport, mcr);
1098 static void ti_break(struct tty_struct *tty, int break_state)
1100 struct usb_serial_port *port = tty->driver_data;
1101 struct ti_port *tport = usb_get_serial_port_data(port);
1102 int status;
1104 dbg("%s - state = %d", __func__, break_state);
1106 if (tport == NULL)
1107 return;
1109 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1111 status = ti_write_byte(tport->tp_tdev,
1112 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1113 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1115 if (status)
1116 dbg("%s - error setting break, %d", __func__, status);
1120 static void ti_interrupt_callback(struct urb *urb)
1122 struct ti_device *tdev = urb->context;
1123 struct usb_serial_port *port;
1124 struct usb_serial *serial = tdev->td_serial;
1125 struct ti_port *tport;
1126 struct device *dev = &urb->dev->dev;
1127 unsigned char *data = urb->transfer_buffer;
1128 int length = urb->actual_length;
1129 int port_number;
1130 int function;
1131 int status = urb->status;
1132 int retval;
1133 __u8 msr;
1135 dbg("%s", __func__);
1137 switch (status) {
1138 case 0:
1139 break;
1140 case -ECONNRESET:
1141 case -ENOENT:
1142 case -ESHUTDOWN:
1143 dbg("%s - urb shutting down, %d", __func__, status);
1144 tdev->td_urb_error = 1;
1145 return;
1146 default:
1147 dev_err(dev, "%s - nonzero urb status, %d\n",
1148 __func__, status);
1149 tdev->td_urb_error = 1;
1150 goto exit;
1153 if (length != 2) {
1154 dbg("%s - bad packet size, %d", __func__, length);
1155 goto exit;
1158 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1159 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1160 goto exit;
1163 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1164 function = TI_GET_FUNC_FROM_CODE(data[0]);
1166 dbg("%s - port_number %d, function %d, data 0x%02X",
1167 __func__, port_number, function, data[1]);
1169 if (port_number >= serial->num_ports) {
1170 dev_err(dev, "%s - bad port number, %d\n",
1171 __func__, port_number);
1172 goto exit;
1175 port = serial->port[port_number];
1177 tport = usb_get_serial_port_data(port);
1178 if (!tport)
1179 goto exit;
1181 switch (function) {
1182 case TI_CODE_DATA_ERROR:
1183 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1184 __func__, port_number, data[1]);
1185 break;
1187 case TI_CODE_MODEM_STATUS:
1188 msr = data[1];
1189 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
1190 ti_handle_new_msr(tport, msr);
1191 break;
1193 default:
1194 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1195 __func__, data[1]);
1196 break;
1199 exit:
1200 retval = usb_submit_urb(urb, GFP_ATOMIC);
1201 if (retval)
1202 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1203 __func__, retval);
1207 static void ti_bulk_in_callback(struct urb *urb)
1209 struct ti_port *tport = urb->context;
1210 struct usb_serial_port *port = tport->tp_port;
1211 struct device *dev = &urb->dev->dev;
1212 int status = urb->status;
1213 int retval = 0;
1214 struct tty_struct *tty;
1216 dbg("%s", __func__);
1218 switch (status) {
1219 case 0:
1220 break;
1221 case -ECONNRESET:
1222 case -ENOENT:
1223 case -ESHUTDOWN:
1224 dbg("%s - urb shutting down, %d", __func__, status);
1225 tport->tp_tdev->td_urb_error = 1;
1226 wake_up_interruptible(&tport->tp_write_wait);
1227 return;
1228 default:
1229 dev_err(dev, "%s - nonzero urb status, %d\n",
1230 __func__, status);
1231 tport->tp_tdev->td_urb_error = 1;
1232 wake_up_interruptible(&tport->tp_write_wait);
1235 if (status == -EPIPE)
1236 goto exit;
1238 if (status) {
1239 dev_err(dev, "%s - stopping read!\n", __func__);
1240 return;
1243 tty = tty_port_tty_get(&port->port);
1244 if (tty && urb->actual_length) {
1245 usb_serial_debug_data(debug, dev, __func__,
1246 urb->actual_length, urb->transfer_buffer);
1248 if (!tport->tp_is_open)
1249 dbg("%s - port closed, dropping data", __func__);
1250 else
1251 ti_recv(&urb->dev->dev, tty,
1252 urb->transfer_buffer,
1253 urb->actual_length);
1255 spin_lock(&tport->tp_lock);
1256 tport->tp_icount.rx += urb->actual_length;
1257 spin_unlock(&tport->tp_lock);
1258 tty_kref_put(tty);
1261 exit:
1262 /* continue to read unless stopping */
1263 spin_lock(&tport->tp_lock);
1264 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1265 urb->dev = port->serial->dev;
1266 retval = usb_submit_urb(urb, GFP_ATOMIC);
1267 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1268 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1270 spin_unlock(&tport->tp_lock);
1271 if (retval)
1272 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1273 __func__, retval);
1277 static void ti_bulk_out_callback(struct urb *urb)
1279 struct ti_port *tport = urb->context;
1280 struct usb_serial_port *port = tport->tp_port;
1281 struct device *dev = &urb->dev->dev;
1282 int status = urb->status;
1284 dbg("%s - port %d", __func__, port->number);
1286 tport->tp_write_urb_in_use = 0;
1288 switch (status) {
1289 case 0:
1290 break;
1291 case -ECONNRESET:
1292 case -ENOENT:
1293 case -ESHUTDOWN:
1294 dbg("%s - urb shutting down, %d", __func__, status);
1295 tport->tp_tdev->td_urb_error = 1;
1296 wake_up_interruptible(&tport->tp_write_wait);
1297 return;
1298 default:
1299 dev_err(dev, "%s - nonzero urb status, %d\n",
1300 __func__, status);
1301 tport->tp_tdev->td_urb_error = 1;
1302 wake_up_interruptible(&tport->tp_write_wait);
1305 /* send any buffered data */
1306 ti_send(tport);
1310 static void ti_recv(struct device *dev, struct tty_struct *tty,
1311 unsigned char *data, int length)
1313 int cnt;
1315 do {
1316 cnt = tty_buffer_request_room(tty, length);
1317 if (cnt < length) {
1318 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1319 __func__, length - cnt);
1320 if (cnt == 0)
1321 break;
1323 tty_insert_flip_string(tty, data, cnt);
1324 tty_flip_buffer_push(tty);
1325 data += cnt;
1326 length -= cnt;
1327 } while (length > 0);
1332 static void ti_send(struct ti_port *tport)
1334 int count, result;
1335 struct usb_serial_port *port = tport->tp_port;
1336 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1337 unsigned long flags;
1340 dbg("%s - port %d", __func__, port->number);
1342 spin_lock_irqsave(&tport->tp_lock, flags);
1344 if (tport->tp_write_urb_in_use)
1345 goto unlock;
1347 count = ti_buf_get(tport->tp_write_buf,
1348 port->write_urb->transfer_buffer,
1349 port->bulk_out_size);
1351 if (count == 0)
1352 goto unlock;
1354 tport->tp_write_urb_in_use = 1;
1356 spin_unlock_irqrestore(&tport->tp_lock, flags);
1358 usb_serial_debug_data(debug, &port->dev, __func__, count,
1359 port->write_urb->transfer_buffer);
1361 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1362 usb_sndbulkpipe(port->serial->dev,
1363 port->bulk_out_endpointAddress),
1364 port->write_urb->transfer_buffer, count,
1365 ti_bulk_out_callback, tport);
1367 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1368 if (result) {
1369 dev_err(&port->dev, "%s - submit write urb failed, %d\n",
1370 __func__, result);
1371 tport->tp_write_urb_in_use = 0;
1372 /* TODO: reschedule ti_send */
1373 } else {
1374 spin_lock_irqsave(&tport->tp_lock, flags);
1375 tport->tp_icount.tx += count;
1376 spin_unlock_irqrestore(&tport->tp_lock, flags);
1379 /* more room in the buffer for new writes, wakeup */
1380 if (tty)
1381 tty_wakeup(tty);
1382 tty_kref_put(tty);
1383 wake_up_interruptible(&tport->tp_write_wait);
1384 return;
1385 unlock:
1386 spin_unlock_irqrestore(&tport->tp_lock, flags);
1387 tty_kref_put(tty);
1388 return;
1392 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1394 unsigned long flags;
1395 int status;
1397 status = ti_write_byte(tport->tp_tdev,
1398 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1399 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1401 spin_lock_irqsave(&tport->tp_lock, flags);
1402 if (!status)
1403 tport->tp_shadow_mcr = mcr;
1404 spin_unlock_irqrestore(&tport->tp_lock, flags);
1406 return status;
1410 static int ti_get_lsr(struct ti_port *tport)
1412 int size, status;
1413 struct ti_device *tdev = tport->tp_tdev;
1414 struct usb_serial_port *port = tport->tp_port;
1415 int port_number = port->number - port->serial->minor;
1416 struct ti_port_status *data;
1418 dbg("%s - port %d", __func__, port->number);
1420 size = sizeof(struct ti_port_status);
1421 data = kmalloc(size, GFP_KERNEL);
1422 if (!data) {
1423 dev_err(&port->dev, "%s - out of memory\n", __func__);
1424 return -ENOMEM;
1427 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1428 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1429 if (status) {
1430 dev_err(&port->dev,
1431 "%s - get port status command failed, %d\n",
1432 __func__, status);
1433 goto free_data;
1436 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1438 tport->tp_lsr = data->bLSR;
1440 free_data:
1441 kfree(data);
1442 return status;
1446 static int ti_get_serial_info(struct ti_port *tport,
1447 struct serial_struct __user *ret_arg)
1449 struct usb_serial_port *port = tport->tp_port;
1450 struct serial_struct ret_serial;
1452 if (!ret_arg)
1453 return -EFAULT;
1455 memset(&ret_serial, 0, sizeof(ret_serial));
1457 ret_serial.type = PORT_16550A;
1458 ret_serial.line = port->serial->minor;
1459 ret_serial.port = port->number - port->serial->minor;
1460 ret_serial.flags = tport->tp_flags;
1461 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1462 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1463 ret_serial.closing_wait = tport->tp_closing_wait;
1465 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1466 return -EFAULT;
1468 return 0;
1472 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1473 struct serial_struct __user *new_arg)
1475 struct serial_struct new_serial;
1477 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1478 return -EFAULT;
1480 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1481 tty->low_latency = (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1482 tport->tp_closing_wait = new_serial.closing_wait;
1484 return 0;
1488 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1490 struct async_icount *icount;
1491 struct tty_struct *tty;
1492 unsigned long flags;
1494 dbg("%s - msr 0x%02X", __func__, msr);
1496 if (msr & TI_MSR_DELTA_MASK) {
1497 spin_lock_irqsave(&tport->tp_lock, flags);
1498 icount = &tport->tp_icount;
1499 if (msr & TI_MSR_DELTA_CTS)
1500 icount->cts++;
1501 if (msr & TI_MSR_DELTA_DSR)
1502 icount->dsr++;
1503 if (msr & TI_MSR_DELTA_CD)
1504 icount->dcd++;
1505 if (msr & TI_MSR_DELTA_RI)
1506 icount->rng++;
1507 wake_up_interruptible(&tport->tp_msr_wait);
1508 spin_unlock_irqrestore(&tport->tp_lock, flags);
1511 tport->tp_msr = msr & TI_MSR_MASK;
1513 /* handle CTS flow control */
1514 tty = tty_port_tty_get(&tport->tp_port->port);
1515 if (tty && C_CRTSCTS(tty)) {
1516 if (msr & TI_MSR_CTS) {
1517 tty->hw_stopped = 0;
1518 tty_wakeup(tty);
1519 } else {
1520 tty->hw_stopped = 1;
1523 tty_kref_put(tty);
1527 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1529 struct ti_device *tdev = tport->tp_tdev;
1530 struct usb_serial_port *port = tport->tp_port;
1531 wait_queue_t wait;
1533 dbg("%s - port %d", __func__, port->number);
1535 spin_lock_irq(&tport->tp_lock);
1537 /* wait for data to drain from the buffer */
1538 tdev->td_urb_error = 0;
1539 init_waitqueue_entry(&wait, current);
1540 add_wait_queue(&tport->tp_write_wait, &wait);
1541 for (;;) {
1542 set_current_state(TASK_INTERRUPTIBLE);
1543 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1544 || timeout == 0 || signal_pending(current)
1545 || tdev->td_urb_error
1546 || port->serial->disconnected) /* disconnect */
1547 break;
1548 spin_unlock_irq(&tport->tp_lock);
1549 timeout = schedule_timeout(timeout);
1550 spin_lock_irq(&tport->tp_lock);
1552 set_current_state(TASK_RUNNING);
1553 remove_wait_queue(&tport->tp_write_wait, &wait);
1555 /* flush any remaining data in the buffer */
1556 if (flush)
1557 ti_buf_clear(tport->tp_write_buf);
1559 spin_unlock_irq(&tport->tp_lock);
1561 mutex_lock(&port->serial->disc_mutex);
1562 /* wait for data to drain from the device */
1563 /* wait for empty tx register, plus 20 ms */
1564 timeout += jiffies;
1565 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1566 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1567 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1568 && !port->serial->disconnected) {
1569 if (ti_get_lsr(tport))
1570 break;
1571 mutex_unlock(&port->serial->disc_mutex);
1572 msleep_interruptible(20);
1573 mutex_lock(&port->serial->disc_mutex);
1575 mutex_unlock(&port->serial->disc_mutex);
1579 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1581 unsigned long flags;
1583 spin_lock_irqsave(&tport->tp_lock, flags);
1585 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1586 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1588 spin_unlock_irqrestore(&tport->tp_lock, flags);
1592 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1594 struct urb *urb;
1595 int status = 0;
1596 unsigned long flags;
1598 spin_lock_irqsave(&tport->tp_lock, flags);
1600 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1601 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1602 urb = tport->tp_port->read_urb;
1603 spin_unlock_irqrestore(&tport->tp_lock, flags);
1604 urb->complete = ti_bulk_in_callback;
1605 urb->context = tport;
1606 urb->dev = tport->tp_port->serial->dev;
1607 status = usb_submit_urb(urb, GFP_KERNEL);
1608 } else {
1609 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1610 spin_unlock_irqrestore(&tport->tp_lock, flags);
1613 return status;
1617 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1618 __u16 moduleid, __u16 value, __u8 *data, int size)
1620 int status;
1622 status = usb_control_msg(tdev->td_serial->dev,
1623 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1624 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1625 value, moduleid, data, size, 1000);
1627 if (status == size)
1628 status = 0;
1630 if (status > 0)
1631 status = -ECOMM;
1633 return status;
1637 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1638 __u16 moduleid, __u16 value, __u8 *data, int size)
1640 int status;
1642 status = usb_control_msg(tdev->td_serial->dev,
1643 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1644 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1645 value, moduleid, data, size, 1000);
1647 if (status == size)
1648 status = 0;
1650 if (status > 0)
1651 status = -ECOMM;
1653 return status;
1657 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1658 __u8 mask, __u8 byte)
1660 int status;
1661 unsigned int size;
1662 struct ti_write_data_bytes *data;
1663 struct device *dev = &tdev->td_serial->dev->dev;
1665 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1666 __func__, addr, mask, byte);
1668 size = sizeof(struct ti_write_data_bytes) + 2;
1669 data = kmalloc(size, GFP_KERNEL);
1670 if (!data) {
1671 dev_err(dev, "%s - out of memory\n", __func__);
1672 return -ENOMEM;
1675 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1676 data->bDataType = TI_RW_DATA_BYTE;
1677 data->bDataCounter = 1;
1678 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1679 data->wBaseAddrLo = cpu_to_be16(addr);
1680 data->bData[0] = mask;
1681 data->bData[1] = byte;
1683 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1684 (__u8 *)data, size);
1686 if (status < 0)
1687 dev_err(dev, "%s - failed, %d\n", __func__, status);
1689 kfree(data);
1691 return status;
1694 static int ti_do_download(struct usb_device *dev, int pipe,
1695 u8 *buffer, int size)
1697 int pos;
1698 u8 cs = 0;
1699 int done;
1700 struct ti_firmware_header *header;
1701 int status;
1702 int len;
1704 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1705 cs = (__u8)(cs + buffer[pos]);
1707 header = (struct ti_firmware_header *)buffer;
1708 header->wLength = cpu_to_le16((__u16)(size
1709 - sizeof(struct ti_firmware_header)));
1710 header->bCheckSum = cs;
1712 dbg("%s - downloading firmware", __func__);
1713 for (pos = 0; pos < size; pos += done) {
1714 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1715 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1716 &done, 1000);
1717 if (status)
1718 break;
1720 return status;
1723 static int ti_download_firmware(struct ti_device *tdev, int type)
1725 int status = -ENOMEM;
1726 int buffer_size;
1727 __u8 *buffer;
1728 struct usb_device *dev = tdev->td_serial->dev;
1729 unsigned int pipe = usb_sndbulkpipe(dev,
1730 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1731 const struct firmware *fw_p;
1732 char buf[32];
1733 sprintf(buf, "ti_usb-%d.bin", type);
1735 if (request_firmware(&fw_p, buf, &dev->dev)) {
1736 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1737 return -ENOENT;
1739 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1740 dev_err(&dev->dev, "%s - firmware too large\n", __func__);
1741 return -ENOENT;
1744 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1745 buffer = kmalloc(buffer_size, GFP_KERNEL);
1746 if (buffer) {
1747 memcpy(buffer, fw_p->data, fw_p->size);
1748 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1749 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1750 kfree(buffer);
1752 release_firmware(fw_p);
1753 if (status) {
1754 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1755 __func__, status);
1756 return status;
1759 dbg("%s - download successful", __func__);
1761 return 0;
1765 /* Circular Buffer Functions */
1768 * ti_buf_alloc
1770 * Allocate a circular buffer and all associated memory.
1773 static struct circ_buf *ti_buf_alloc(void)
1775 struct circ_buf *cb;
1777 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
1778 if (cb == NULL)
1779 return NULL;
1781 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1782 if (cb->buf == NULL) {
1783 kfree(cb);
1784 return NULL;
1787 ti_buf_clear(cb);
1789 return cb;
1794 * ti_buf_free
1796 * Free the buffer and all associated memory.
1799 static void ti_buf_free(struct circ_buf *cb)
1801 kfree(cb->buf);
1802 kfree(cb);
1807 * ti_buf_clear
1809 * Clear out all data in the circular buffer.
1812 static void ti_buf_clear(struct circ_buf *cb)
1814 cb->head = cb->tail = 0;
1819 * ti_buf_data_avail
1821 * Return the number of bytes of data available in the circular
1822 * buffer.
1825 static int ti_buf_data_avail(struct circ_buf *cb)
1827 return CIRC_CNT(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1832 * ti_buf_space_avail
1834 * Return the number of bytes of space available in the circular
1835 * buffer.
1838 static int ti_buf_space_avail(struct circ_buf *cb)
1840 return CIRC_SPACE(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1845 * ti_buf_put
1847 * Copy data data from a user buffer and put it into the circular buffer.
1848 * Restrict to the amount of space available.
1850 * Return the number of bytes copied.
1853 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1855 int c, ret = 0;
1857 while (1) {
1858 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1859 if (count < c)
1860 c = count;
1861 if (c <= 0)
1862 break;
1863 memcpy(cb->buf + cb->head, buf, c);
1864 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1865 buf += c;
1866 count -= c;
1867 ret += c;
1870 return ret;
1875 * ti_buf_get
1877 * Get data from the circular buffer and copy to the given buffer.
1878 * Restrict to the amount of data available.
1880 * Return the number of bytes copied.
1883 static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1885 int c, ret = 0;
1887 while (1) {
1888 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1889 if (count < c)
1890 c = count;
1891 if (c <= 0)
1892 break;
1893 memcpy(buf, cb->buf + cb->tail, c);
1894 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);
1895 buf += c;
1896 count -= c;
1897 ret += c;
1900 return ret;