usb-serial: fix crash when sub-driver updates firmware
[linux-2.6/mini2440.git] / drivers / usb / serial / ti_usb_3410_5052.c
blob9c4c700c7cc640ec4400bf8ab00175938da18ceb
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>.
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/firmware.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h>
29 #include <linux/module.h>
30 #include <linux/spinlock.h>
31 #include <linux/ioctl.h>
32 #include <linux/serial.h>
33 #include <linux/circ_buf.h>
34 #include <linux/mutex.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
39 #include "ti_usb_3410_5052.h"
41 /* Defines */
43 #define TI_DRIVER_VERSION "v0.9"
44 #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
45 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
47 #define TI_FIRMWARE_BUF_SIZE 16284
49 #define TI_WRITE_BUF_SIZE 1024
51 #define TI_TRANSFER_TIMEOUT 2
53 #define TI_DEFAULT_LOW_LATENCY 0
54 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
56 /* supported setserial flags */
57 #define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
59 /* read urb states */
60 #define TI_READ_URB_RUNNING 0
61 #define TI_READ_URB_STOPPING 1
62 #define TI_READ_URB_STOPPED 2
64 #define TI_EXTRA_VID_PID_COUNT 5
67 /* Structures */
69 struct ti_port {
70 int tp_is_open;
71 __u8 tp_msr;
72 __u8 tp_lsr;
73 __u8 tp_shadow_mcr;
74 __u8 tp_uart_mode; /* 232 or 485 modes */
75 unsigned int tp_uart_base_addr;
76 int tp_flags;
77 int tp_closing_wait;/* in .01 secs */
78 struct async_icount tp_icount;
79 wait_queue_head_t tp_msr_wait; /* wait for msr change */
80 wait_queue_head_t tp_write_wait;
81 struct ti_device *tp_tdev;
82 struct usb_serial_port *tp_port;
83 spinlock_t tp_lock;
84 int tp_read_urb_state;
85 int tp_write_urb_in_use;
86 struct circ_buf *tp_write_buf;
89 struct ti_device {
90 struct mutex td_open_close_lock;
91 int td_open_port_count;
92 struct usb_serial *td_serial;
93 int td_is_3410;
94 int td_urb_error;
98 /* Function Declarations */
100 static int ti_startup(struct usb_serial *serial);
101 static void ti_shutdown(struct usb_serial *serial);
102 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port,
103 struct file *file);
104 static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
105 struct file *file);
106 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
107 const unsigned char *data, int count);
108 static int ti_write_room(struct tty_struct *tty);
109 static int ti_chars_in_buffer(struct tty_struct *tty);
110 static void ti_throttle(struct tty_struct *tty);
111 static void ti_unthrottle(struct tty_struct *tty);
112 static int ti_ioctl(struct tty_struct *tty, struct file *file,
113 unsigned int cmd, unsigned long arg);
114 static void ti_set_termios(struct tty_struct *tty,
115 struct usb_serial_port *port, struct ktermios *old_termios);
116 static int ti_tiocmget(struct tty_struct *tty, struct file *file);
117 static int ti_tiocmset(struct tty_struct *tty, struct file *file,
118 unsigned int set, unsigned int clear);
119 static void ti_break(struct tty_struct *tty, int break_state);
120 static void ti_interrupt_callback(struct urb *urb);
121 static void ti_bulk_in_callback(struct urb *urb);
122 static void ti_bulk_out_callback(struct urb *urb);
124 static void ti_recv(struct device *dev, struct tty_struct *tty,
125 unsigned char *data, int length);
126 static void ti_send(struct ti_port *tport);
127 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
128 static int ti_get_lsr(struct ti_port *tport);
129 static int ti_get_serial_info(struct ti_port *tport,
130 struct serial_struct __user *ret_arg);
131 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
132 struct serial_struct __user *new_arg);
133 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
135 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
137 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
138 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
140 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
141 __u16 moduleid, __u16 value, __u8 *data, int size);
142 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
143 __u16 moduleid, __u16 value, __u8 *data, int size);
145 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
146 __u8 mask, __u8 byte);
148 static int ti_download_firmware(struct ti_device *tdev);
150 /* circular buffer */
151 static struct circ_buf *ti_buf_alloc(void);
152 static void ti_buf_free(struct circ_buf *cb);
153 static void ti_buf_clear(struct circ_buf *cb);
154 static int ti_buf_data_avail(struct circ_buf *cb);
155 static int ti_buf_space_avail(struct circ_buf *cb);
156 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
157 static int ti_buf_get(struct circ_buf *cb, char *buf, int count);
160 /* Data */
162 /* module parameters */
163 static int debug;
164 static int low_latency = TI_DEFAULT_LOW_LATENCY;
165 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
166 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
167 static unsigned int vendor_3410_count;
168 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
169 static unsigned int product_3410_count;
170 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
171 static unsigned int vendor_5052_count;
172 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
173 static unsigned int product_5052_count;
175 /* supported devices */
176 /* the array dimension is the number of default entries plus */
177 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
178 /* null entry */
179 static struct usb_device_id ti_id_table_3410[10+TI_EXTRA_VID_PID_COUNT+1] = {
180 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
181 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
182 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
183 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
184 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
185 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
186 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
187 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
188 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
189 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
192 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
193 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
194 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
195 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
196 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
197 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
200 static struct usb_device_id ti_id_table_combined[14+2*TI_EXTRA_VID_PID_COUNT+1] = {
201 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
202 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
203 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
204 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
205 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
206 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
207 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
208 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
209 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
210 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
211 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
212 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
213 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
214 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
218 static struct usb_driver ti_usb_driver = {
219 .name = "ti_usb_3410_5052",
220 .probe = usb_serial_probe,
221 .disconnect = usb_serial_disconnect,
222 .id_table = ti_id_table_combined,
223 .no_dynamic_id = 1,
226 static struct usb_serial_driver ti_1port_device = {
227 .driver = {
228 .owner = THIS_MODULE,
229 .name = "ti_usb_3410_5052_1",
231 .description = "TI USB 3410 1 port adapter",
232 .usb_driver = &ti_usb_driver,
233 .id_table = ti_id_table_3410,
234 .num_ports = 1,
235 .attach = ti_startup,
236 .shutdown = ti_shutdown,
237 .open = ti_open,
238 .close = ti_close,
239 .write = ti_write,
240 .write_room = ti_write_room,
241 .chars_in_buffer = ti_chars_in_buffer,
242 .throttle = ti_throttle,
243 .unthrottle = ti_unthrottle,
244 .ioctl = ti_ioctl,
245 .set_termios = ti_set_termios,
246 .tiocmget = ti_tiocmget,
247 .tiocmset = ti_tiocmset,
248 .break_ctl = ti_break,
249 .read_int_callback = ti_interrupt_callback,
250 .read_bulk_callback = ti_bulk_in_callback,
251 .write_bulk_callback = ti_bulk_out_callback,
254 static struct usb_serial_driver ti_2port_device = {
255 .driver = {
256 .owner = THIS_MODULE,
257 .name = "ti_usb_3410_5052_2",
259 .description = "TI USB 5052 2 port adapter",
260 .usb_driver = &ti_usb_driver,
261 .id_table = ti_id_table_5052,
262 .num_ports = 2,
263 .attach = ti_startup,
264 .shutdown = ti_shutdown,
265 .open = ti_open,
266 .close = ti_close,
267 .write = ti_write,
268 .write_room = ti_write_room,
269 .chars_in_buffer = ti_chars_in_buffer,
270 .throttle = ti_throttle,
271 .unthrottle = ti_unthrottle,
272 .ioctl = ti_ioctl,
273 .set_termios = ti_set_termios,
274 .tiocmget = ti_tiocmget,
275 .tiocmset = ti_tiocmset,
276 .break_ctl = ti_break,
277 .read_int_callback = ti_interrupt_callback,
278 .read_bulk_callback = ti_bulk_in_callback,
279 .write_bulk_callback = ti_bulk_out_callback,
283 /* Module */
285 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
286 MODULE_DESCRIPTION(TI_DRIVER_DESC);
287 MODULE_VERSION(TI_DRIVER_VERSION);
288 MODULE_LICENSE("GPL");
290 MODULE_FIRMWARE("ti_3410.fw");
291 MODULE_FIRMWARE("ti_5052.fw");
292 MODULE_FIRMWARE("mts_cdma.fw");
293 MODULE_FIRMWARE("mts_gsm.fw");
294 MODULE_FIRMWARE("mts_edge.fw");
296 module_param(debug, bool, S_IRUGO | S_IWUSR);
297 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
299 module_param(low_latency, bool, S_IRUGO | S_IWUSR);
300 MODULE_PARM_DESC(low_latency,
301 "TTY low_latency flag, 0=off, 1=on, default is off");
303 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
304 MODULE_PARM_DESC(closing_wait,
305 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
307 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
308 MODULE_PARM_DESC(vendor_3410,
309 "Vendor ids for 3410 based devices, 1-5 short integers");
310 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
311 MODULE_PARM_DESC(product_3410,
312 "Product ids for 3410 based devices, 1-5 short integers");
313 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
314 MODULE_PARM_DESC(vendor_5052,
315 "Vendor ids for 5052 based devices, 1-5 short integers");
316 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
317 MODULE_PARM_DESC(product_5052,
318 "Product ids for 5052 based devices, 1-5 short integers");
320 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
323 /* Functions */
325 static int __init ti_init(void)
327 int i, j, c;
328 int ret;
330 /* insert extra vendor and product ids */
331 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
332 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
333 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
334 ti_id_table_3410[j].idVendor = vendor_3410[i];
335 ti_id_table_3410[j].idProduct = product_3410[i];
336 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
337 ti_id_table_combined[c].idVendor = vendor_3410[i];
338 ti_id_table_combined[c].idProduct = product_3410[i];
339 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
341 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
342 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
343 ti_id_table_5052[j].idVendor = vendor_5052[i];
344 ti_id_table_5052[j].idProduct = product_5052[i];
345 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
346 ti_id_table_combined[c].idVendor = vendor_5052[i];
347 ti_id_table_combined[c].idProduct = product_5052[i];
348 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
351 ret = usb_serial_register(&ti_1port_device);
352 if (ret)
353 goto failed_1port;
354 ret = usb_serial_register(&ti_2port_device);
355 if (ret)
356 goto failed_2port;
358 ret = usb_register(&ti_usb_driver);
359 if (ret)
360 goto failed_usb;
362 printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":"
363 TI_DRIVER_DESC "\n");
365 return 0;
367 failed_usb:
368 usb_serial_deregister(&ti_2port_device);
369 failed_2port:
370 usb_serial_deregister(&ti_1port_device);
371 failed_1port:
372 return ret;
376 static void __exit ti_exit(void)
378 usb_serial_deregister(&ti_1port_device);
379 usb_serial_deregister(&ti_2port_device);
380 usb_deregister(&ti_usb_driver);
384 module_init(ti_init);
385 module_exit(ti_exit);
388 static int ti_startup(struct usb_serial *serial)
390 struct ti_device *tdev;
391 struct ti_port *tport;
392 struct usb_device *dev = serial->dev;
393 int status;
394 int i;
397 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
398 __func__, le16_to_cpu(dev->descriptor.idProduct),
399 dev->descriptor.bNumConfigurations,
400 dev->actconfig->desc.bConfigurationValue);
402 /* create device structure */
403 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
404 if (tdev == NULL) {
405 dev_err(&dev->dev, "%s - out of memory\n", __func__);
406 return -ENOMEM;
408 mutex_init(&tdev->td_open_close_lock);
409 tdev->td_serial = serial;
410 usb_set_serial_data(serial, tdev);
412 /* determine device type */
413 if (usb_match_id(serial->interface, ti_id_table_3410))
414 tdev->td_is_3410 = 1;
415 dbg("%s - device type is %s", __func__,
416 tdev->td_is_3410 ? "3410" : "5052");
418 /* if we have only 1 configuration, download firmware */
419 if (dev->descriptor.bNumConfigurations == 1) {
420 if ((status = ti_download_firmware(tdev)) != 0)
421 goto free_tdev;
423 /* 3410 must be reset, 5052 resets itself */
424 if (tdev->td_is_3410) {
425 msleep_interruptible(100);
426 usb_reset_device(dev);
429 status = -ENODEV;
430 goto free_tdev;
433 /* the second configuration must be set */
434 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
435 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
436 status = status ? status : -ENODEV;
437 goto free_tdev;
440 /* set up port structures */
441 for (i = 0; i < serial->num_ports; ++i) {
442 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
443 if (tport == NULL) {
444 dev_err(&dev->dev, "%s - out of memory\n", __func__);
445 status = -ENOMEM;
446 goto free_tports;
448 spin_lock_init(&tport->tp_lock);
449 tport->tp_uart_base_addr = (i == 0 ?
450 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
451 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
452 tport->tp_closing_wait = closing_wait;
453 init_waitqueue_head(&tport->tp_msr_wait);
454 init_waitqueue_head(&tport->tp_write_wait);
455 tport->tp_write_buf = ti_buf_alloc();
456 if (tport->tp_write_buf == NULL) {
457 dev_err(&dev->dev, "%s - out of memory\n", __func__);
458 kfree(tport);
459 status = -ENOMEM;
460 goto free_tports;
462 tport->tp_port = serial->port[i];
463 tport->tp_tdev = tdev;
464 usb_set_serial_port_data(serial->port[i], tport);
465 tport->tp_uart_mode = 0; /* default is RS232 */
468 return 0;
470 free_tports:
471 for (--i; i >= 0; --i) {
472 tport = usb_get_serial_port_data(serial->port[i]);
473 ti_buf_free(tport->tp_write_buf);
474 kfree(tport);
475 usb_set_serial_port_data(serial->port[i], NULL);
477 free_tdev:
478 kfree(tdev);
479 usb_set_serial_data(serial, NULL);
480 return status;
484 static void ti_shutdown(struct usb_serial *serial)
486 int i;
487 struct ti_device *tdev = usb_get_serial_data(serial);
488 struct ti_port *tport;
490 dbg("%s", __func__);
492 for (i = 0; i < serial->num_ports; ++i) {
493 tport = usb_get_serial_port_data(serial->port[i]);
494 if (tport) {
495 ti_buf_free(tport->tp_write_buf);
496 kfree(tport);
497 usb_set_serial_port_data(serial->port[i], NULL);
501 kfree(tdev);
502 usb_set_serial_data(serial, NULL);
506 static int ti_open(struct tty_struct *tty,
507 struct usb_serial_port *port, struct file *file)
509 struct ti_port *tport = usb_get_serial_port_data(port);
510 struct ti_device *tdev;
511 struct usb_device *dev;
512 struct urb *urb;
513 int port_number;
514 int status;
515 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
516 TI_PIPE_TIMEOUT_ENABLE |
517 (TI_TRANSFER_TIMEOUT << 2));
519 dbg("%s - port %d", __func__, port->number);
521 if (tport == NULL)
522 return -ENODEV;
524 dev = port->serial->dev;
525 tdev = tport->tp_tdev;
527 /* only one open on any port on a device at a time */
528 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
529 return -ERESTARTSYS;
531 if (tty)
532 tty->low_latency =
533 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
535 port_number = port->number - port->serial->minor;
537 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
539 tport->tp_msr = 0;
540 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
542 /* start interrupt urb the first time a port is opened on this device */
543 if (tdev->td_open_port_count == 0) {
544 dbg("%s - start interrupt in urb", __func__);
545 urb = tdev->td_serial->port[0]->interrupt_in_urb;
546 if (!urb) {
547 dev_err(&port->dev, "%s - no interrupt urb\n",
548 __func__);
549 status = -EINVAL;
550 goto release_lock;
552 urb->complete = ti_interrupt_callback;
553 urb->context = tdev;
554 urb->dev = dev;
555 status = usb_submit_urb(urb, GFP_KERNEL);
556 if (status) {
557 dev_err(&port->dev,
558 "%s - submit interrupt urb failed, %d\n",
559 __func__, status);
560 goto release_lock;
564 if (tty)
565 ti_set_termios(tty, port, tty->termios);
567 dbg("%s - sending TI_OPEN_PORT", __func__);
568 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
569 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
570 if (status) {
571 dev_err(&port->dev, "%s - cannot send open command, %d\n",
572 __func__, status);
573 goto unlink_int_urb;
576 dbg("%s - sending TI_START_PORT", __func__);
577 status = ti_command_out_sync(tdev, TI_START_PORT,
578 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
579 if (status) {
580 dev_err(&port->dev, "%s - cannot send start command, %d\n",
581 __func__, status);
582 goto unlink_int_urb;
585 dbg("%s - sending TI_PURGE_PORT", __func__);
586 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
587 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
588 if (status) {
589 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
590 __func__, status);
591 goto unlink_int_urb;
593 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
594 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
595 if (status) {
596 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
597 __func__, status);
598 goto unlink_int_urb;
601 /* reset the data toggle on the bulk endpoints to work around bug in
602 * host controllers where things get out of sync some times */
603 usb_clear_halt(dev, port->write_urb->pipe);
604 usb_clear_halt(dev, port->read_urb->pipe);
606 if (tty)
607 ti_set_termios(tty, port, tty->termios);
609 dbg("%s - sending TI_OPEN_PORT (2)", __func__);
610 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
611 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
612 if (status) {
613 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
614 __func__, status);
615 goto unlink_int_urb;
618 dbg("%s - sending TI_START_PORT (2)", __func__);
619 status = ti_command_out_sync(tdev, TI_START_PORT,
620 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
621 if (status) {
622 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
623 __func__, status);
624 goto unlink_int_urb;
627 /* start read urb */
628 dbg("%s - start read urb", __func__);
629 urb = port->read_urb;
630 if (!urb) {
631 dev_err(&port->dev, "%s - no read urb\n", __func__);
632 status = -EINVAL;
633 goto unlink_int_urb;
635 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
636 urb->complete = ti_bulk_in_callback;
637 urb->context = tport;
638 urb->dev = dev;
639 status = usb_submit_urb(urb, GFP_KERNEL);
640 if (status) {
641 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
642 __func__, status);
643 goto unlink_int_urb;
646 tport->tp_is_open = 1;
647 ++tdev->td_open_port_count;
649 goto release_lock;
651 unlink_int_urb:
652 if (tdev->td_open_port_count == 0)
653 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
654 release_lock:
655 mutex_unlock(&tdev->td_open_close_lock);
656 dbg("%s - exit %d", __func__, status);
657 return status;
661 static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
662 struct file *file)
664 struct ti_device *tdev;
665 struct ti_port *tport;
666 int port_number;
667 int status;
668 int do_unlock;
670 dbg("%s - port %d", __func__, port->number);
672 tdev = usb_get_serial_data(port->serial);
673 tport = usb_get_serial_port_data(port);
674 if (tdev == NULL || tport == NULL)
675 return;
677 tport->tp_is_open = 0;
679 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
681 usb_kill_urb(port->read_urb);
682 usb_kill_urb(port->write_urb);
683 tport->tp_write_urb_in_use = 0;
685 port_number = port->number - port->serial->minor;
687 dbg("%s - sending TI_CLOSE_PORT", __func__);
688 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
689 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
690 if (status)
691 dev_err(&port->dev,
692 "%s - cannot send close port command, %d\n"
693 , __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 tty_struct *tty, struct usb_serial_port *port,
711 const unsigned char *data, 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 tty_struct *tty)
738 struct usb_serial_port *port = tty->driver_data;
739 struct ti_port *tport = usb_get_serial_port_data(port);
740 int room = 0;
741 unsigned long flags;
743 dbg("%s - port %d", __func__, port->number);
745 if (tport == NULL)
746 return -ENODEV;
748 spin_lock_irqsave(&tport->tp_lock, flags);
749 room = ti_buf_space_avail(tport->tp_write_buf);
750 spin_unlock_irqrestore(&tport->tp_lock, flags);
752 dbg("%s - returns %d", __func__, room);
753 return room;
757 static int ti_chars_in_buffer(struct tty_struct *tty)
759 struct usb_serial_port *port = tty->driver_data;
760 struct ti_port *tport = usb_get_serial_port_data(port);
761 int chars = 0;
762 unsigned long flags;
764 dbg("%s - port %d", __func__, port->number);
766 if (tport == NULL)
767 return -ENODEV;
769 spin_lock_irqsave(&tport->tp_lock, flags);
770 chars = ti_buf_data_avail(tport->tp_write_buf);
771 spin_unlock_irqrestore(&tport->tp_lock, flags);
773 dbg("%s - returns %d", __func__, chars);
774 return chars;
778 static void ti_throttle(struct tty_struct *tty)
780 struct usb_serial_port *port = tty->driver_data;
781 struct ti_port *tport = usb_get_serial_port_data(port);
783 dbg("%s - port %d", __func__, port->number);
785 if (tport == NULL)
786 return;
788 if (I_IXOFF(tty) || C_CRTSCTS(tty))
789 ti_stop_read(tport, tty);
794 static void ti_unthrottle(struct tty_struct *tty)
796 struct usb_serial_port *port = tty->driver_data;
797 struct ti_port *tport = usb_get_serial_port_data(port);
798 int status;
800 dbg("%s - port %d", __func__, port->number);
802 if (tport == NULL)
803 return;
805 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
806 status = ti_restart_read(tport, tty);
807 if (status)
808 dev_err(&port->dev, "%s - cannot restart read, %d\n",
809 __func__, status);
814 static int ti_ioctl(struct tty_struct *tty, struct file *file,
815 unsigned int cmd, unsigned long arg)
817 struct usb_serial_port *port = tty->driver_data;
818 struct ti_port *tport = usb_get_serial_port_data(port);
819 struct async_icount cnow;
820 struct async_icount cprev;
822 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
824 if (tport == NULL)
825 return -ENODEV;
827 switch (cmd) {
828 case TIOCGSERIAL:
829 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
830 return ti_get_serial_info(tport,
831 (struct serial_struct __user *)arg);
832 case TIOCSSERIAL:
833 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
834 return ti_set_serial_info(tty, tport,
835 (struct serial_struct __user *)arg);
836 case TIOCMIWAIT:
837 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
838 cprev = tport->tp_icount;
839 while (1) {
840 interruptible_sleep_on(&tport->tp_msr_wait);
841 if (signal_pending(current))
842 return -ERESTARTSYS;
843 cnow = tport->tp_icount;
844 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
845 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
846 return -EIO; /* no change => error */
847 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
848 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
849 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
850 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
851 return 0;
852 cprev = cnow;
854 break;
855 case TIOCGICOUNT:
856 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
857 __func__, port->number,
858 tport->tp_icount.rx, tport->tp_icount.tx);
859 if (copy_to_user((void __user *)arg, &tport->tp_icount,
860 sizeof(tport->tp_icount)))
861 return -EFAULT;
862 return 0;
864 return -ENOIOCTLCMD;
868 static void ti_set_termios(struct tty_struct *tty,
869 struct usb_serial_port *port, struct ktermios *old_termios)
871 struct ti_port *tport = usb_get_serial_port_data(port);
872 struct ti_uart_config *config;
873 tcflag_t cflag, iflag;
874 int baud;
875 int status;
876 int port_number = port->number - port->serial->minor;
877 unsigned int mcr;
879 dbg("%s - port %d", __func__, port->number);
881 cflag = tty->termios->c_cflag;
882 iflag = tty->termios->c_iflag;
884 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
885 dbg("%s - old clfag %08x, old iflag %08x", __func__,
886 old_termios->c_cflag, old_termios->c_iflag);
888 if (tport == NULL)
889 return;
891 config = kmalloc(sizeof(*config), GFP_KERNEL);
892 if (!config) {
893 dev_err(&port->dev, "%s - out of memory\n", __func__);
894 return;
897 config->wFlags = 0;
899 /* these flags must be set */
900 config->wFlags |= TI_UART_ENABLE_MS_INTS;
901 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
902 config->bUartMode = (__u8)(tport->tp_uart_mode);
904 switch (cflag & CSIZE) {
905 case CS5:
906 config->bDataBits = TI_UART_5_DATA_BITS;
907 break;
908 case CS6:
909 config->bDataBits = TI_UART_6_DATA_BITS;
910 break;
911 case CS7:
912 config->bDataBits = TI_UART_7_DATA_BITS;
913 break;
914 default:
915 case CS8:
916 config->bDataBits = TI_UART_8_DATA_BITS;
917 break;
920 /* CMSPAR isn't supported by this driver */
921 tty->termios->c_cflag &= ~CMSPAR;
923 if (cflag & PARENB) {
924 if (cflag & PARODD) {
925 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
926 config->bParity = TI_UART_ODD_PARITY;
927 } else {
928 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
929 config->bParity = TI_UART_EVEN_PARITY;
931 } else {
932 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
933 config->bParity = TI_UART_NO_PARITY;
936 if (cflag & CSTOPB)
937 config->bStopBits = TI_UART_2_STOP_BITS;
938 else
939 config->bStopBits = TI_UART_1_STOP_BITS;
941 if (cflag & CRTSCTS) {
942 /* RTS flow control must be off to drop RTS for baud rate B0 */
943 if ((cflag & CBAUD) != B0)
944 config->wFlags |= TI_UART_ENABLE_RTS_IN;
945 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
946 } else {
947 tty->hw_stopped = 0;
948 ti_restart_read(tport, tty);
951 if (I_IXOFF(tty) || I_IXON(tty)) {
952 config->cXon = START_CHAR(tty);
953 config->cXoff = STOP_CHAR(tty);
955 if (I_IXOFF(tty))
956 config->wFlags |= TI_UART_ENABLE_X_IN;
957 else
958 ti_restart_read(tport, tty);
960 if (I_IXON(tty))
961 config->wFlags |= TI_UART_ENABLE_X_OUT;
964 baud = tty_get_baud_rate(tty);
965 if (!baud)
966 baud = 9600;
967 if (tport->tp_tdev->td_is_3410)
968 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
969 else
970 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
972 /* FIXME: Should calculate resulting baud here and report it back */
973 if ((cflag & CBAUD) != B0)
974 tty_encode_baud_rate(tty, baud, baud);
976 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
977 __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
979 cpu_to_be16s(&config->wBaudRate);
980 cpu_to_be16s(&config->wFlags);
982 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
983 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
984 sizeof(*config));
985 if (status)
986 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
987 __func__, port_number, status);
989 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
990 mcr = tport->tp_shadow_mcr;
991 /* if baud rate is B0, clear RTS and DTR */
992 if ((cflag & CBAUD) == B0)
993 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
994 status = ti_set_mcr(tport, mcr);
995 if (status)
996 dev_err(&port->dev,
997 "%s - cannot set modem control on port %d, %d\n",
998 __func__, port_number, status);
1000 kfree(config);
1004 static int ti_tiocmget(struct tty_struct *tty, struct file *file)
1006 struct usb_serial_port *port = tty->driver_data;
1007 struct ti_port *tport = usb_get_serial_port_data(port);
1008 unsigned int result;
1009 unsigned int msr;
1010 unsigned int mcr;
1011 unsigned long flags;
1013 dbg("%s - port %d", __func__, port->number);
1015 if (tport == NULL)
1016 return -ENODEV;
1018 spin_lock_irqsave(&tport->tp_lock, flags);
1019 msr = tport->tp_msr;
1020 mcr = tport->tp_shadow_mcr;
1021 spin_unlock_irqrestore(&tport->tp_lock, flags);
1023 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1024 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1025 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1026 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1027 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1028 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1029 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1031 dbg("%s - 0x%04X", __func__, result);
1033 return result;
1037 static int ti_tiocmset(struct tty_struct *tty, struct file *file,
1038 unsigned int set, unsigned int clear)
1040 struct usb_serial_port *port = tty->driver_data;
1041 struct ti_port *tport = usb_get_serial_port_data(port);
1042 unsigned int mcr;
1043 unsigned long flags;
1045 dbg("%s - port %d", __func__, port->number);
1047 if (tport == NULL)
1048 return -ENODEV;
1050 spin_lock_irqsave(&tport->tp_lock, flags);
1051 mcr = tport->tp_shadow_mcr;
1053 if (set & TIOCM_RTS)
1054 mcr |= TI_MCR_RTS;
1055 if (set & TIOCM_DTR)
1056 mcr |= TI_MCR_DTR;
1057 if (set & TIOCM_LOOP)
1058 mcr |= TI_MCR_LOOP;
1060 if (clear & TIOCM_RTS)
1061 mcr &= ~TI_MCR_RTS;
1062 if (clear & TIOCM_DTR)
1063 mcr &= ~TI_MCR_DTR;
1064 if (clear & TIOCM_LOOP)
1065 mcr &= ~TI_MCR_LOOP;
1066 spin_unlock_irqrestore(&tport->tp_lock, flags);
1068 return ti_set_mcr(tport, mcr);
1072 static void ti_break(struct tty_struct *tty, int break_state)
1074 struct usb_serial_port *port = tty->driver_data;
1075 struct ti_port *tport = usb_get_serial_port_data(port);
1076 int status;
1078 dbg("%s - state = %d", __func__, break_state);
1080 if (tport == NULL)
1081 return;
1083 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1085 status = ti_write_byte(tport->tp_tdev,
1086 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1087 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1089 if (status)
1090 dbg("%s - error setting break, %d", __func__, status);
1094 static void ti_interrupt_callback(struct urb *urb)
1096 struct ti_device *tdev = urb->context;
1097 struct usb_serial_port *port;
1098 struct usb_serial *serial = tdev->td_serial;
1099 struct ti_port *tport;
1100 struct device *dev = &urb->dev->dev;
1101 unsigned char *data = urb->transfer_buffer;
1102 int length = urb->actual_length;
1103 int port_number;
1104 int function;
1105 int status = urb->status;
1106 int retval;
1107 __u8 msr;
1109 dbg("%s", __func__);
1111 switch (status) {
1112 case 0:
1113 break;
1114 case -ECONNRESET:
1115 case -ENOENT:
1116 case -ESHUTDOWN:
1117 dbg("%s - urb shutting down, %d", __func__, status);
1118 tdev->td_urb_error = 1;
1119 return;
1120 default:
1121 dev_err(dev, "%s - nonzero urb status, %d\n",
1122 __func__, status);
1123 tdev->td_urb_error = 1;
1124 goto exit;
1127 if (length != 2) {
1128 dbg("%s - bad packet size, %d", __func__, length);
1129 goto exit;
1132 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1133 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1134 goto exit;
1137 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1138 function = TI_GET_FUNC_FROM_CODE(data[0]);
1140 dbg("%s - port_number %d, function %d, data 0x%02X",
1141 __func__, port_number, function, data[1]);
1143 if (port_number >= serial->num_ports) {
1144 dev_err(dev, "%s - bad port number, %d\n",
1145 __func__, port_number);
1146 goto exit;
1149 port = serial->port[port_number];
1151 tport = usb_get_serial_port_data(port);
1152 if (!tport)
1153 goto exit;
1155 switch (function) {
1156 case TI_CODE_DATA_ERROR:
1157 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1158 __func__, port_number, data[1]);
1159 break;
1161 case TI_CODE_MODEM_STATUS:
1162 msr = data[1];
1163 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
1164 ti_handle_new_msr(tport, msr);
1165 break;
1167 default:
1168 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1169 __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;
1188 struct tty_struct *tty;
1190 dbg("%s", __func__);
1192 switch (status) {
1193 case 0:
1194 break;
1195 case -ECONNRESET:
1196 case -ENOENT:
1197 case -ESHUTDOWN:
1198 dbg("%s - urb shutting down, %d", __func__, status);
1199 tport->tp_tdev->td_urb_error = 1;
1200 wake_up_interruptible(&tport->tp_write_wait);
1201 return;
1202 default:
1203 dev_err(dev, "%s - nonzero urb status, %d\n",
1204 __func__, status);
1205 tport->tp_tdev->td_urb_error = 1;
1206 wake_up_interruptible(&tport->tp_write_wait);
1209 if (status == -EPIPE)
1210 goto exit;
1212 if (status) {
1213 dev_err(dev, "%s - stopping read!\n", __func__);
1214 return;
1217 tty = tty_port_tty_get(&port->port);
1218 if (tty) {
1219 if (urb->actual_length) {
1220 usb_serial_debug_data(debug, dev, __func__,
1221 urb->actual_length, urb->transfer_buffer);
1223 if (!tport->tp_is_open)
1224 dbg("%s - port closed, dropping data",
1225 __func__);
1226 else
1227 ti_recv(&urb->dev->dev, tty,
1228 urb->transfer_buffer,
1229 urb->actual_length);
1230 spin_lock(&tport->tp_lock);
1231 tport->tp_icount.rx += urb->actual_length;
1232 spin_unlock(&tport->tp_lock);
1234 tty_kref_put(tty);
1237 exit:
1238 /* continue to read unless stopping */
1239 spin_lock(&tport->tp_lock);
1240 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1241 urb->dev = port->serial->dev;
1242 retval = usb_submit_urb(urb, GFP_ATOMIC);
1243 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1244 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1246 spin_unlock(&tport->tp_lock);
1247 if (retval)
1248 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1249 __func__, retval);
1253 static void ti_bulk_out_callback(struct urb *urb)
1255 struct ti_port *tport = urb->context;
1256 struct usb_serial_port *port = tport->tp_port;
1257 struct device *dev = &urb->dev->dev;
1258 int status = urb->status;
1260 dbg("%s - port %d", __func__, port->number);
1262 tport->tp_write_urb_in_use = 0;
1264 switch (status) {
1265 case 0:
1266 break;
1267 case -ECONNRESET:
1268 case -ENOENT:
1269 case -ESHUTDOWN:
1270 dbg("%s - urb shutting down, %d", __func__, status);
1271 tport->tp_tdev->td_urb_error = 1;
1272 wake_up_interruptible(&tport->tp_write_wait);
1273 return;
1274 default:
1275 dev_err(dev, "%s - nonzero urb status, %d\n",
1276 __func__, status);
1277 tport->tp_tdev->td_urb_error = 1;
1278 wake_up_interruptible(&tport->tp_write_wait);
1281 /* send any buffered data */
1282 ti_send(tport);
1286 static void ti_recv(struct device *dev, struct tty_struct *tty,
1287 unsigned char *data, int length)
1289 int cnt;
1291 do {
1292 cnt = tty_buffer_request_room(tty, length);
1293 if (cnt < length) {
1294 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1295 __func__, length - cnt);
1296 if (cnt == 0)
1297 break;
1299 tty_insert_flip_string(tty, data, cnt);
1300 tty_flip_buffer_push(tty);
1301 data += cnt;
1302 length -= cnt;
1303 } while (length > 0);
1308 static void ti_send(struct ti_port *tport)
1310 int count, result;
1311 struct usb_serial_port *port = tport->tp_port;
1312 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1313 unsigned long flags;
1316 dbg("%s - port %d", __func__, port->number);
1318 spin_lock_irqsave(&tport->tp_lock, flags);
1320 if (tport->tp_write_urb_in_use)
1321 goto unlock;
1323 count = ti_buf_get(tport->tp_write_buf,
1324 port->write_urb->transfer_buffer,
1325 port->bulk_out_size);
1327 if (count == 0)
1328 goto unlock;
1330 tport->tp_write_urb_in_use = 1;
1332 spin_unlock_irqrestore(&tport->tp_lock, flags);
1334 usb_serial_debug_data(debug, &port->dev, __func__, count,
1335 port->write_urb->transfer_buffer);
1337 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1338 usb_sndbulkpipe(port->serial->dev,
1339 port->bulk_out_endpointAddress),
1340 port->write_urb->transfer_buffer, count,
1341 ti_bulk_out_callback, tport);
1343 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1344 if (result) {
1345 dev_err(&port->dev, "%s - submit write urb failed, %d\n",
1346 __func__, result);
1347 tport->tp_write_urb_in_use = 0;
1348 /* TODO: reschedule ti_send */
1349 } else {
1350 spin_lock_irqsave(&tport->tp_lock, flags);
1351 tport->tp_icount.tx += count;
1352 spin_unlock_irqrestore(&tport->tp_lock, flags);
1355 /* more room in the buffer for new writes, wakeup */
1356 if (tty)
1357 tty_wakeup(tty);
1358 tty_kref_put(tty);
1359 wake_up_interruptible(&tport->tp_write_wait);
1360 return;
1361 unlock:
1362 spin_unlock_irqrestore(&tport->tp_lock, flags);
1363 tty_kref_put(tty);
1364 return;
1368 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1370 unsigned long flags;
1371 int status;
1373 status = ti_write_byte(tport->tp_tdev,
1374 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1375 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1377 spin_lock_irqsave(&tport->tp_lock, flags);
1378 if (!status)
1379 tport->tp_shadow_mcr = mcr;
1380 spin_unlock_irqrestore(&tport->tp_lock, flags);
1382 return status;
1386 static int ti_get_lsr(struct ti_port *tport)
1388 int size, status;
1389 struct ti_device *tdev = tport->tp_tdev;
1390 struct usb_serial_port *port = tport->tp_port;
1391 int port_number = port->number - port->serial->minor;
1392 struct ti_port_status *data;
1394 dbg("%s - port %d", __func__, port->number);
1396 size = sizeof(struct ti_port_status);
1397 data = kmalloc(size, GFP_KERNEL);
1398 if (!data) {
1399 dev_err(&port->dev, "%s - out of memory\n", __func__);
1400 return -ENOMEM;
1403 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1404 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1405 if (status) {
1406 dev_err(&port->dev,
1407 "%s - get port status command failed, %d\n",
1408 __func__, status);
1409 goto free_data;
1412 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1414 tport->tp_lsr = data->bLSR;
1416 free_data:
1417 kfree(data);
1418 return status;
1422 static int ti_get_serial_info(struct ti_port *tport,
1423 struct serial_struct __user *ret_arg)
1425 struct usb_serial_port *port = tport->tp_port;
1426 struct serial_struct ret_serial;
1428 if (!ret_arg)
1429 return -EFAULT;
1431 memset(&ret_serial, 0, sizeof(ret_serial));
1433 ret_serial.type = PORT_16550A;
1434 ret_serial.line = port->serial->minor;
1435 ret_serial.port = port->number - port->serial->minor;
1436 ret_serial.flags = tport->tp_flags;
1437 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1438 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1439 ret_serial.closing_wait = tport->tp_closing_wait;
1441 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1442 return -EFAULT;
1444 return 0;
1448 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1449 struct serial_struct __user *new_arg)
1451 struct serial_struct new_serial;
1453 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1454 return -EFAULT;
1456 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1457 tty->low_latency = (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1458 tport->tp_closing_wait = new_serial.closing_wait;
1460 return 0;
1464 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1466 struct async_icount *icount;
1467 struct tty_struct *tty;
1468 unsigned long flags;
1470 dbg("%s - msr 0x%02X", __func__, msr);
1472 if (msr & TI_MSR_DELTA_MASK) {
1473 spin_lock_irqsave(&tport->tp_lock, flags);
1474 icount = &tport->tp_icount;
1475 if (msr & TI_MSR_DELTA_CTS)
1476 icount->cts++;
1477 if (msr & TI_MSR_DELTA_DSR)
1478 icount->dsr++;
1479 if (msr & TI_MSR_DELTA_CD)
1480 icount->dcd++;
1481 if (msr & TI_MSR_DELTA_RI)
1482 icount->rng++;
1483 wake_up_interruptible(&tport->tp_msr_wait);
1484 spin_unlock_irqrestore(&tport->tp_lock, flags);
1487 tport->tp_msr = msr & TI_MSR_MASK;
1489 /* handle CTS flow control */
1490 tty = tty_port_tty_get(&tport->tp_port->port);
1491 if (tty && C_CRTSCTS(tty)) {
1492 if (msr & TI_MSR_CTS) {
1493 tty->hw_stopped = 0;
1494 tty_wakeup(tty);
1495 } else {
1496 tty->hw_stopped = 1;
1499 tty_kref_put(tty);
1503 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1505 struct ti_device *tdev = tport->tp_tdev;
1506 struct usb_serial_port *port = tport->tp_port;
1507 wait_queue_t wait;
1509 dbg("%s - port %d", __func__, port->number);
1511 spin_lock_irq(&tport->tp_lock);
1513 /* wait for data to drain from the buffer */
1514 tdev->td_urb_error = 0;
1515 init_waitqueue_entry(&wait, current);
1516 add_wait_queue(&tport->tp_write_wait, &wait);
1517 for (;;) {
1518 set_current_state(TASK_INTERRUPTIBLE);
1519 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1520 || timeout == 0 || signal_pending(current)
1521 || tdev->td_urb_error
1522 || port->serial->disconnected) /* disconnect */
1523 break;
1524 spin_unlock_irq(&tport->tp_lock);
1525 timeout = schedule_timeout(timeout);
1526 spin_lock_irq(&tport->tp_lock);
1528 set_current_state(TASK_RUNNING);
1529 remove_wait_queue(&tport->tp_write_wait, &wait);
1531 /* flush any remaining data in the buffer */
1532 if (flush)
1533 ti_buf_clear(tport->tp_write_buf);
1535 spin_unlock_irq(&tport->tp_lock);
1537 mutex_lock(&port->serial->disc_mutex);
1538 /* wait for data to drain from the device */
1539 /* wait for empty tx register, plus 20 ms */
1540 timeout += jiffies;
1541 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1542 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1543 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1544 && !port->serial->disconnected) {
1545 if (ti_get_lsr(tport))
1546 break;
1547 mutex_unlock(&port->serial->disc_mutex);
1548 msleep_interruptible(20);
1549 mutex_lock(&port->serial->disc_mutex);
1551 mutex_unlock(&port->serial->disc_mutex);
1555 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1557 unsigned long flags;
1559 spin_lock_irqsave(&tport->tp_lock, flags);
1561 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1562 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1564 spin_unlock_irqrestore(&tport->tp_lock, flags);
1568 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1570 struct urb *urb;
1571 int status = 0;
1572 unsigned long flags;
1574 spin_lock_irqsave(&tport->tp_lock, flags);
1576 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1577 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1578 urb = tport->tp_port->read_urb;
1579 spin_unlock_irqrestore(&tport->tp_lock, flags);
1580 urb->complete = ti_bulk_in_callback;
1581 urb->context = tport;
1582 urb->dev = tport->tp_port->serial->dev;
1583 status = usb_submit_urb(urb, GFP_KERNEL);
1584 } else {
1585 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1586 spin_unlock_irqrestore(&tport->tp_lock, flags);
1589 return status;
1593 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1594 __u16 moduleid, __u16 value, __u8 *data, int size)
1596 int status;
1598 status = usb_control_msg(tdev->td_serial->dev,
1599 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1600 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1601 value, moduleid, data, size, 1000);
1603 if (status == size)
1604 status = 0;
1606 if (status > 0)
1607 status = -ECOMM;
1609 return status;
1613 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1614 __u16 moduleid, __u16 value, __u8 *data, int size)
1616 int status;
1618 status = usb_control_msg(tdev->td_serial->dev,
1619 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1620 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1621 value, moduleid, data, size, 1000);
1623 if (status == size)
1624 status = 0;
1626 if (status > 0)
1627 status = -ECOMM;
1629 return status;
1633 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1634 __u8 mask, __u8 byte)
1636 int status;
1637 unsigned int size;
1638 struct ti_write_data_bytes *data;
1639 struct device *dev = &tdev->td_serial->dev->dev;
1641 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1642 __func__, addr, mask, byte);
1644 size = sizeof(struct ti_write_data_bytes) + 2;
1645 data = kmalloc(size, GFP_KERNEL);
1646 if (!data) {
1647 dev_err(dev, "%s - out of memory\n", __func__);
1648 return -ENOMEM;
1651 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1652 data->bDataType = TI_RW_DATA_BYTE;
1653 data->bDataCounter = 1;
1654 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1655 data->wBaseAddrLo = cpu_to_be16(addr);
1656 data->bData[0] = mask;
1657 data->bData[1] = byte;
1659 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1660 (__u8 *)data, size);
1662 if (status < 0)
1663 dev_err(dev, "%s - failed, %d\n", __func__, status);
1665 kfree(data);
1667 return status;
1670 static int ti_do_download(struct usb_device *dev, int pipe,
1671 u8 *buffer, int size)
1673 int pos;
1674 u8 cs = 0;
1675 int done;
1676 struct ti_firmware_header *header;
1677 int status;
1678 int len;
1680 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1681 cs = (__u8)(cs + buffer[pos]);
1683 header = (struct ti_firmware_header *)buffer;
1684 header->wLength = cpu_to_le16((__u16)(size
1685 - sizeof(struct ti_firmware_header)));
1686 header->bCheckSum = cs;
1688 dbg("%s - downloading firmware", __func__);
1689 for (pos = 0; pos < size; pos += done) {
1690 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1691 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1692 &done, 1000);
1693 if (status)
1694 break;
1696 return status;
1699 static int ti_download_firmware(struct ti_device *tdev)
1701 int status;
1702 int buffer_size;
1703 __u8 *buffer;
1704 struct usb_device *dev = tdev->td_serial->dev;
1705 unsigned int pipe = usb_sndbulkpipe(dev,
1706 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1707 const struct firmware *fw_p;
1708 char buf[32];
1710 /* try ID specific firmware first, then try generic firmware */
1711 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1712 dev->descriptor.idProduct);
1713 if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) {
1714 buf[0] = '\0';
1715 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1716 switch (dev->descriptor.idProduct) {
1717 case MTS_CDMA_PRODUCT_ID:
1718 strcpy(buf, "mts_cdma.fw");
1719 break;
1720 case MTS_GSM_PRODUCT_ID:
1721 strcpy(buf, "mts_gsm.fw");
1722 break;
1723 case MTS_EDGE_PRODUCT_ID:
1724 strcpy(buf, "mts_edge.fw");
1725 break;
1728 if (buf[0] == '\0') {
1729 if (tdev->td_is_3410)
1730 strcpy(buf, "ti_3410.fw");
1731 else
1732 strcpy(buf, "ti_5052.fw");
1734 status = request_firmware(&fw_p, buf, &dev->dev);
1736 if (status) {
1737 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1738 return -ENOENT;
1740 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1741 dev_err(&dev->dev, "%s - firmware too large\n", __func__);
1742 return -ENOENT;
1745 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1746 buffer = kmalloc(buffer_size, GFP_KERNEL);
1747 if (buffer) {
1748 memcpy(buffer, fw_p->data, fw_p->size);
1749 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1750 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1751 kfree(buffer);
1752 } else {
1753 status = -ENOMEM;
1755 release_firmware(fw_p);
1756 if (status) {
1757 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1758 __func__, status);
1759 return status;
1762 dbg("%s - download successful", __func__);
1764 return 0;
1768 /* Circular Buffer Functions */
1771 * ti_buf_alloc
1773 * Allocate a circular buffer and all associated memory.
1776 static struct circ_buf *ti_buf_alloc(void)
1778 struct circ_buf *cb;
1780 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
1781 if (cb == NULL)
1782 return NULL;
1784 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1785 if (cb->buf == NULL) {
1786 kfree(cb);
1787 return NULL;
1790 ti_buf_clear(cb);
1792 return cb;
1797 * ti_buf_free
1799 * Free the buffer and all associated memory.
1802 static void ti_buf_free(struct circ_buf *cb)
1804 kfree(cb->buf);
1805 kfree(cb);
1810 * ti_buf_clear
1812 * Clear out all data in the circular buffer.
1815 static void ti_buf_clear(struct circ_buf *cb)
1817 cb->head = cb->tail = 0;
1822 * ti_buf_data_avail
1824 * Return the number of bytes of data available in the circular
1825 * buffer.
1828 static int ti_buf_data_avail(struct circ_buf *cb)
1830 return CIRC_CNT(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1835 * ti_buf_space_avail
1837 * Return the number of bytes of space available in the circular
1838 * buffer.
1841 static int ti_buf_space_avail(struct circ_buf *cb)
1843 return CIRC_SPACE(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1848 * ti_buf_put
1850 * Copy data data from a user buffer and put it into the circular buffer.
1851 * Restrict to the amount of space available.
1853 * Return the number of bytes copied.
1856 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1858 int c, ret = 0;
1860 while (1) {
1861 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1862 if (count < c)
1863 c = count;
1864 if (c <= 0)
1865 break;
1866 memcpy(cb->buf + cb->head, buf, c);
1867 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1868 buf += c;
1869 count -= c;
1870 ret += c;
1873 return ret;
1878 * ti_buf_get
1880 * Get data from the circular buffer and copy to the given buffer.
1881 * Restrict to the amount of data available.
1883 * Return the number of bytes copied.
1886 static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1888 int c, ret = 0;
1890 while (1) {
1891 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1892 if (count < c)
1893 c = count;
1894 if (c <= 0)
1895 break;
1896 memcpy(buf, cb->buf + cb->tail, c);
1897 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);
1898 buf += c;
1899 count -= c;
1900 ret += c;
1903 return ret;