]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/usb/serial/ti_usb_3410_5052.c
USB: ti_usb_3410_5052: removed duplicated include
[mirror_ubuntu-artful-kernel.git] / drivers / usb / serial / ti_usb_3410_5052.c
1 /* vi: ts=8 sw=8
2 *
3 * TI 3410/5052 USB Serial Driver
4 *
5 * Copyright (C) 2004 Texas Instruments
6 *
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
10 *
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.
15 *
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>.
19 *
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.
23 *
24 * #!/bin/bash
25 *
26 * BOOT_CONFIG=1
27 * ACTIVE_CONFIG=2
28 *
29 * if [[ "$ACTION" != "add" ]]
30 * then
31 * exit
32 * fi
33 *
34 * CONFIG_PATH=/sys${DEVPATH%/?*}/bConfigurationValue
35 *
36 * if [[ 0`cat $CONFIG_PATH` -ne $BOOT_CONFIG ]]
37 * then
38 * exit
39 * fi
40 *
41 * PRODUCT=${PRODUCT%/?*} # delete version
42 * VENDOR_ID=`printf "%d" 0x${PRODUCT%/?*}`
43 * PRODUCT_ID=`printf "%d" 0x${PRODUCT#*?/}`
44 *
45 * PARAM_PATH=/sys/module/ti_usb_3410_5052/parameters
46 *
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 * }
59 *
60 * IFS=$IFS,
61 *
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
69 */
70
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>
88
89 #include "ti_usb_3410_5052.h"
90
91 /* Defines */
92
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"
96
97 #define TI_FIRMWARE_BUF_SIZE 16284
98
99 #define TI_WRITE_BUF_SIZE 1024
100
101 #define TI_TRANSFER_TIMEOUT 2
102
103 #define TI_DEFAULT_LOW_LATENCY 0
104 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
105
106 /* supported setserial flags */
107 #define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
108
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
113
114 #define TI_EXTRA_VID_PID_COUNT 5
115
116
117 /* Structures */
118
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;
137 };
138
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;
145 };
146
147
148 /* Function Declarations */
149
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);
173
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);
184
185 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
186
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);
189
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);
194
195 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
196 __u8 mask, __u8 byte);
197
198 static int ti_download_firmware(struct ti_device *tdev, int type);
199
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);
208
209
210 /* Data */
211
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;
224
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) },
232 };
233
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) },
239 };
240
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) },
248 { }
249 };
250
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,
257 };
258
259 static struct usb_serial_driver ti_1port_device = {
260 .driver = {
261 .owner = THIS_MODULE,
262 .name = "ti_usb_3410_5052_1",
263 },
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,
285 };
286
287 static struct usb_serial_driver ti_2port_device = {
288 .driver = {
289 .owner = THIS_MODULE,
290 .name = "ti_usb_3410_5052_2",
291 },
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,
313 };
314
315
316 /* Module */
317
318 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
319 MODULE_DESCRIPTION(TI_DRIVER_DESC);
320 MODULE_VERSION(TI_DRIVER_VERSION);
321 MODULE_LICENSE("GPL");
322
323 MODULE_FIRMWARE("ti_3410.fw");
324 MODULE_FIRMWARE("ti_5052.fw");
325
326 module_param(debug, bool, S_IRUGO | S_IWUSR);
327 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
328
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");
332
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");
336
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");
349
350 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
351
352
353 /* Functions */
354
355 static int __init ti_init(void)
356 {
357 int i, j;
358 int ret;
359
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;
366 }
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;
372 }
373
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;
380
381 ret = usb_register(&ti_usb_driver);
382 if (ret)
383 goto failed_usb;
384
385 info(TI_DRIVER_DESC " " TI_DRIVER_VERSION);
386
387 return 0;
388
389 failed_usb:
390 usb_serial_deregister(&ti_2port_device);
391 failed_2port:
392 usb_serial_deregister(&ti_1port_device);
393 failed_1port:
394 return ret;
395 }
396
397
398 static void __exit ti_exit(void)
399 {
400 usb_serial_deregister(&ti_1port_device);
401 usb_serial_deregister(&ti_2port_device);
402 usb_deregister(&ti_usb_driver);
403 }
404
405
406 module_init(ti_init);
407 module_exit(ti_exit);
408
409
410 static int ti_startup(struct usb_serial *serial)
411 {
412 struct ti_device *tdev;
413 struct ti_port *tport;
414 struct usb_device *dev = serial->dev;
415 int status;
416 int i;
417
418
419 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
420 __func__, le16_to_cpu(dev->descriptor.idProduct),
421 dev->descriptor.bNumConfigurations,
422 dev->actconfig->desc.bConfigurationValue);
423
424 /* create device structure */
425 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
426 if (tdev == NULL) {
427 dev_err(&dev->dev, "%s - out of memory\n", __func__);
428 return -ENOMEM;
429 }
430 mutex_init(&tdev->td_open_close_lock);
431 tdev->td_serial = serial;
432 usb_set_serial_data(serial, tdev);
433
434 /* determine device type */
435 if (usb_match_id(serial->interface, ti_id_table_3410))
436 tdev->td_is_3410 = 1;
437 dbg("%s - device type is %s", __func__,
438 tdev->td_is_3410 ? "3410" : "5052");
439
440 /* if we have only 1 configuration, download firmware */
441 if (dev->descriptor.bNumConfigurations == 1) {
442 if (tdev->td_is_3410)
443 status = ti_download_firmware(tdev, 3410);
444 else
445 status = ti_download_firmware(tdev, 5052);
446 if (status)
447 goto free_tdev;
448
449 /* 3410 must be reset, 5052 resets itself */
450 if (tdev->td_is_3410) {
451 msleep_interruptible(100);
452 usb_reset_device(dev);
453 }
454
455 status = -ENODEV;
456 goto free_tdev;
457 }
458
459 /* the second configuration must be set (in sysfs by hotplug script) */
460 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
461 status = -ENODEV;
462 goto free_tdev;
463 }
464
465 /* set up port structures */
466 for (i = 0; i < serial->num_ports; ++i) {
467 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
468 if (tport == NULL) {
469 dev_err(&dev->dev, "%s - out of memory\n", __func__);
470 status = -ENOMEM;
471 goto free_tports;
472 }
473 spin_lock_init(&tport->tp_lock);
474 tport->tp_uart_base_addr = (i == 0 ?
475 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
476 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
477 tport->tp_closing_wait = closing_wait;
478 init_waitqueue_head(&tport->tp_msr_wait);
479 init_waitqueue_head(&tport->tp_write_wait);
480 tport->tp_write_buf = ti_buf_alloc();
481 if (tport->tp_write_buf == NULL) {
482 dev_err(&dev->dev, "%s - out of memory\n", __func__);
483 kfree(tport);
484 status = -ENOMEM;
485 goto free_tports;
486 }
487 tport->tp_port = serial->port[i];
488 tport->tp_tdev = tdev;
489 usb_set_serial_port_data(serial->port[i], tport);
490 tport->tp_uart_mode = 0; /* default is RS232 */
491 }
492
493 return 0;
494
495 free_tports:
496 for (--i; i >= 0; --i) {
497 tport = usb_get_serial_port_data(serial->port[i]);
498 ti_buf_free(tport->tp_write_buf);
499 kfree(tport);
500 usb_set_serial_port_data(serial->port[i], NULL);
501 }
502 free_tdev:
503 kfree(tdev);
504 usb_set_serial_data(serial, NULL);
505 return status;
506 }
507
508
509 static void ti_shutdown(struct usb_serial *serial)
510 {
511 int i;
512 struct ti_device *tdev = usb_get_serial_data(serial);
513 struct ti_port *tport;
514
515 dbg("%s", __func__);
516
517 for (i = 0; i < serial->num_ports; ++i) {
518 tport = usb_get_serial_port_data(serial->port[i]);
519 if (tport) {
520 ti_buf_free(tport->tp_write_buf);
521 kfree(tport);
522 usb_set_serial_port_data(serial->port[i], NULL);
523 }
524 }
525
526 kfree(tdev);
527 usb_set_serial_data(serial, NULL);
528 }
529
530
531 static int ti_open(struct tty_struct *tty,
532 struct usb_serial_port *port, struct file *file)
533 {
534 struct ti_port *tport = usb_get_serial_port_data(port);
535 struct ti_device *tdev;
536 struct usb_device *dev;
537 struct urb *urb;
538 int port_number;
539 int status;
540 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
541 TI_PIPE_TIMEOUT_ENABLE |
542 (TI_TRANSFER_TIMEOUT << 2));
543
544 dbg("%s - port %d", __func__, port->number);
545
546 if (tport == NULL)
547 return -ENODEV;
548
549 dev = port->serial->dev;
550 tdev = tport->tp_tdev;
551
552 /* only one open on any port on a device at a time */
553 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
554 return -ERESTARTSYS;
555
556 if (tty)
557 tty->low_latency =
558 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
559
560 port_number = port->number - port->serial->minor;
561
562 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
563
564 tport->tp_msr = 0;
565 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
566
567 /* start interrupt urb the first time a port is opened on this device */
568 if (tdev->td_open_port_count == 0) {
569 dbg("%s - start interrupt in urb", __func__);
570 urb = tdev->td_serial->port[0]->interrupt_in_urb;
571 if (!urb) {
572 dev_err(&port->dev, "%s - no interrupt urb\n",
573 __func__);
574 status = -EINVAL;
575 goto release_lock;
576 }
577 urb->complete = ti_interrupt_callback;
578 urb->context = tdev;
579 urb->dev = dev;
580 status = usb_submit_urb(urb, GFP_KERNEL);
581 if (status) {
582 dev_err(&port->dev,
583 "%s - submit interrupt urb failed, %d\n",
584 __func__, status);
585 goto release_lock;
586 }
587 }
588
589 if (tty)
590 ti_set_termios(tty, port, tty->termios);
591
592 dbg("%s - sending TI_OPEN_PORT", __func__);
593 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
594 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
595 if (status) {
596 dev_err(&port->dev, "%s - cannot send open command, %d\n",
597 __func__, status);
598 goto unlink_int_urb;
599 }
600
601 dbg("%s - sending TI_START_PORT", __func__);
602 status = ti_command_out_sync(tdev, TI_START_PORT,
603 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
604 if (status) {
605 dev_err(&port->dev, "%s - cannot send start command, %d\n",
606 __func__, status);
607 goto unlink_int_urb;
608 }
609
610 dbg("%s - sending TI_PURGE_PORT", __func__);
611 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
612 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
613 if (status) {
614 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
615 __func__, status);
616 goto unlink_int_urb;
617 }
618 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
619 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
620 if (status) {
621 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
622 __func__, status);
623 goto unlink_int_urb;
624 }
625
626 /* reset the data toggle on the bulk endpoints to work around bug in
627 * host controllers where things get out of sync some times */
628 usb_clear_halt(dev, port->write_urb->pipe);
629 usb_clear_halt(dev, port->read_urb->pipe);
630
631 if (tty)
632 ti_set_termios(tty, port, tty->termios);
633
634 dbg("%s - sending TI_OPEN_PORT (2)", __func__);
635 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
636 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
637 if (status) {
638 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
639 __func__, status);
640 goto unlink_int_urb;
641 }
642
643 dbg("%s - sending TI_START_PORT (2)", __func__);
644 status = ti_command_out_sync(tdev, TI_START_PORT,
645 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
646 if (status) {
647 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
648 __func__, status);
649 goto unlink_int_urb;
650 }
651
652 /* start read urb */
653 dbg("%s - start read urb", __func__);
654 urb = port->read_urb;
655 if (!urb) {
656 dev_err(&port->dev, "%s - no read urb\n", __func__);
657 status = -EINVAL;
658 goto unlink_int_urb;
659 }
660 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
661 urb->complete = ti_bulk_in_callback;
662 urb->context = tport;
663 urb->dev = dev;
664 status = usb_submit_urb(urb, GFP_KERNEL);
665 if (status) {
666 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
667 __func__, status);
668 goto unlink_int_urb;
669 }
670
671 tport->tp_is_open = 1;
672 ++tdev->td_open_port_count;
673
674 goto release_lock;
675
676 unlink_int_urb:
677 if (tdev->td_open_port_count == 0)
678 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
679 release_lock:
680 mutex_unlock(&tdev->td_open_close_lock);
681 dbg("%s - exit %d", __func__, status);
682 return status;
683 }
684
685
686 static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
687 struct file *file)
688 {
689 struct ti_device *tdev;
690 struct ti_port *tport;
691 int port_number;
692 int status;
693 int do_unlock;
694
695 dbg("%s - port %d", __func__, port->number);
696
697 tdev = usb_get_serial_data(port->serial);
698 tport = usb_get_serial_port_data(port);
699 if (tdev == NULL || tport == NULL)
700 return;
701
702 tport->tp_is_open = 0;
703
704 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
705
706 usb_kill_urb(port->read_urb);
707 usb_kill_urb(port->write_urb);
708 tport->tp_write_urb_in_use = 0;
709
710 port_number = port->number - port->serial->minor;
711
712 dbg("%s - sending TI_CLOSE_PORT", __func__);
713 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
714 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
715 if (status)
716 dev_err(&port->dev,
717 "%s - cannot send close port command, %d\n"
718 , __func__, status);
719
720 /* if mutex_lock is interrupted, continue anyway */
721 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
722 --tport->tp_tdev->td_open_port_count;
723 if (tport->tp_tdev->td_open_port_count <= 0) {
724 /* last port is closed, shut down interrupt urb */
725 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
726 tport->tp_tdev->td_open_port_count = 0;
727 }
728 if (do_unlock)
729 mutex_unlock(&tdev->td_open_close_lock);
730
731 dbg("%s - exit", __func__);
732 }
733
734
735 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
736 const unsigned char *data, int count)
737 {
738 struct ti_port *tport = usb_get_serial_port_data(port);
739 unsigned long flags;
740
741 dbg("%s - port %d", __func__, port->number);
742
743 if (count == 0) {
744 dbg("%s - write request of 0 bytes", __func__);
745 return 0;
746 }
747
748 if (tport == NULL || !tport->tp_is_open)
749 return -ENODEV;
750
751 spin_lock_irqsave(&tport->tp_lock, flags);
752 count = ti_buf_put(tport->tp_write_buf, data, count);
753 spin_unlock_irqrestore(&tport->tp_lock, flags);
754
755 ti_send(tport);
756
757 return count;
758 }
759
760
761 static int ti_write_room(struct tty_struct *tty)
762 {
763 struct usb_serial_port *port = tty->driver_data;
764 struct ti_port *tport = usb_get_serial_port_data(port);
765 int room = 0;
766 unsigned long flags;
767
768 dbg("%s - port %d", __func__, port->number);
769
770 if (tport == NULL)
771 return -ENODEV;
772
773 spin_lock_irqsave(&tport->tp_lock, flags);
774 room = ti_buf_space_avail(tport->tp_write_buf);
775 spin_unlock_irqrestore(&tport->tp_lock, flags);
776
777 dbg("%s - returns %d", __func__, room);
778 return room;
779 }
780
781
782 static int ti_chars_in_buffer(struct tty_struct *tty)
783 {
784 struct usb_serial_port *port = tty->driver_data;
785 struct ti_port *tport = usb_get_serial_port_data(port);
786 int chars = 0;
787 unsigned long flags;
788
789 dbg("%s - port %d", __func__, port->number);
790
791 if (tport == NULL)
792 return -ENODEV;
793
794 spin_lock_irqsave(&tport->tp_lock, flags);
795 chars = ti_buf_data_avail(tport->tp_write_buf);
796 spin_unlock_irqrestore(&tport->tp_lock, flags);
797
798 dbg("%s - returns %d", __func__, chars);
799 return chars;
800 }
801
802
803 static void ti_throttle(struct tty_struct *tty)
804 {
805 struct usb_serial_port *port = tty->driver_data;
806 struct ti_port *tport = usb_get_serial_port_data(port);
807
808 dbg("%s - port %d", __func__, port->number);
809
810 if (tport == NULL)
811 return;
812
813 if (I_IXOFF(tty) || C_CRTSCTS(tty))
814 ti_stop_read(tport, tty);
815
816 }
817
818
819 static void ti_unthrottle(struct tty_struct *tty)
820 {
821 struct usb_serial_port *port = tty->driver_data;
822 struct ti_port *tport = usb_get_serial_port_data(port);
823 int status;
824
825 dbg("%s - port %d", __func__, port->number);
826
827 if (tport == NULL)
828 return;
829
830 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
831 status = ti_restart_read(tport, tty);
832 if (status)
833 dev_err(&port->dev, "%s - cannot restart read, %d\n",
834 __func__, status);
835 }
836 }
837
838
839 static int ti_ioctl(struct tty_struct *tty, struct file *file,
840 unsigned int cmd, unsigned long arg)
841 {
842 struct usb_serial_port *port = tty->driver_data;
843 struct ti_port *tport = usb_get_serial_port_data(port);
844 struct async_icount cnow;
845 struct async_icount cprev;
846
847 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
848
849 if (tport == NULL)
850 return -ENODEV;
851
852 switch (cmd) {
853 case TIOCGSERIAL:
854 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
855 return ti_get_serial_info(tport,
856 (struct serial_struct __user *)arg);
857 case TIOCSSERIAL:
858 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
859 return ti_set_serial_info(tty, tport,
860 (struct serial_struct __user *)arg);
861 case TIOCMIWAIT:
862 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
863 cprev = tport->tp_icount;
864 while (1) {
865 interruptible_sleep_on(&tport->tp_msr_wait);
866 if (signal_pending(current))
867 return -ERESTARTSYS;
868 cnow = tport->tp_icount;
869 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
870 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
871 return -EIO; /* no change => error */
872 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
873 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
874 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
875 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
876 return 0;
877 cprev = cnow;
878 }
879 break;
880 case TIOCGICOUNT:
881 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
882 __func__, port->number,
883 tport->tp_icount.rx, tport->tp_icount.tx);
884 if (copy_to_user((void __user *)arg, &tport->tp_icount,
885 sizeof(tport->tp_icount)))
886 return -EFAULT;
887 return 0;
888 }
889 return -ENOIOCTLCMD;
890 }
891
892
893 static void ti_set_termios(struct tty_struct *tty,
894 struct usb_serial_port *port, struct ktermios *old_termios)
895 {
896 struct ti_port *tport = usb_get_serial_port_data(port);
897 struct ti_uart_config *config;
898 tcflag_t cflag, iflag;
899 int baud;
900 int status;
901 int port_number = port->number - port->serial->minor;
902 unsigned int mcr;
903
904 dbg("%s - port %d", __func__, port->number);
905
906 cflag = tty->termios->c_cflag;
907 iflag = tty->termios->c_iflag;
908
909 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
910 dbg("%s - old clfag %08x, old iflag %08x", __func__,
911 old_termios->c_cflag, old_termios->c_iflag);
912
913 if (tport == NULL)
914 return;
915
916 config = kmalloc(sizeof(*config), GFP_KERNEL);
917 if (!config) {
918 dev_err(&port->dev, "%s - out of memory\n", __func__);
919 return;
920 }
921
922 config->wFlags = 0;
923
924 /* these flags must be set */
925 config->wFlags |= TI_UART_ENABLE_MS_INTS;
926 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
927 config->bUartMode = (__u8)(tport->tp_uart_mode);
928
929 switch (cflag & CSIZE) {
930 case CS5:
931 config->bDataBits = TI_UART_5_DATA_BITS;
932 break;
933 case CS6:
934 config->bDataBits = TI_UART_6_DATA_BITS;
935 break;
936 case CS7:
937 config->bDataBits = TI_UART_7_DATA_BITS;
938 break;
939 default:
940 case CS8:
941 config->bDataBits = TI_UART_8_DATA_BITS;
942 break;
943 }
944
945 /* CMSPAR isn't supported by this driver */
946 tty->termios->c_cflag &= ~CMSPAR;
947
948 if (cflag & PARENB) {
949 if (cflag & PARODD) {
950 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
951 config->bParity = TI_UART_ODD_PARITY;
952 } else {
953 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
954 config->bParity = TI_UART_EVEN_PARITY;
955 }
956 } else {
957 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
958 config->bParity = TI_UART_NO_PARITY;
959 }
960
961 if (cflag & CSTOPB)
962 config->bStopBits = TI_UART_2_STOP_BITS;
963 else
964 config->bStopBits = TI_UART_1_STOP_BITS;
965
966 if (cflag & CRTSCTS) {
967 /* RTS flow control must be off to drop RTS for baud rate B0 */
968 if ((cflag & CBAUD) != B0)
969 config->wFlags |= TI_UART_ENABLE_RTS_IN;
970 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
971 } else {
972 tty->hw_stopped = 0;
973 ti_restart_read(tport, tty);
974 }
975
976 if (I_IXOFF(tty) || I_IXON(tty)) {
977 config->cXon = START_CHAR(tty);
978 config->cXoff = STOP_CHAR(tty);
979
980 if (I_IXOFF(tty))
981 config->wFlags |= TI_UART_ENABLE_X_IN;
982 else
983 ti_restart_read(tport, tty);
984
985 if (I_IXON(tty))
986 config->wFlags |= TI_UART_ENABLE_X_OUT;
987 }
988
989 baud = tty_get_baud_rate(tty);
990 if (!baud)
991 baud = 9600;
992 if (tport->tp_tdev->td_is_3410)
993 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
994 else
995 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
996
997 /* FIXME: Should calculate resulting baud here and report it back */
998 if ((cflag & CBAUD) != B0)
999 tty_encode_baud_rate(tty, baud, baud);
1000
1001 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
1002 __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
1003
1004 cpu_to_be16s(&config->wBaudRate);
1005 cpu_to_be16s(&config->wFlags);
1006
1007 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
1008 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
1009 sizeof(*config));
1010 if (status)
1011 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
1012 __func__, port_number, status);
1013
1014 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
1015 mcr = tport->tp_shadow_mcr;
1016 /* if baud rate is B0, clear RTS and DTR */
1017 if ((cflag & CBAUD) == B0)
1018 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1019 status = ti_set_mcr(tport, mcr);
1020 if (status)
1021 dev_err(&port->dev,
1022 "%s - cannot set modem control on port %d, %d\n",
1023 __func__, port_number, status);
1024
1025 kfree(config);
1026 }
1027
1028
1029 static int ti_tiocmget(struct tty_struct *tty, struct file *file)
1030 {
1031 struct usb_serial_port *port = tty->driver_data;
1032 struct ti_port *tport = usb_get_serial_port_data(port);
1033 unsigned int result;
1034 unsigned int msr;
1035 unsigned int mcr;
1036 unsigned long flags;
1037
1038 dbg("%s - port %d", __func__, port->number);
1039
1040 if (tport == NULL)
1041 return -ENODEV;
1042
1043 spin_lock_irqsave(&tport->tp_lock, flags);
1044 msr = tport->tp_msr;
1045 mcr = tport->tp_shadow_mcr;
1046 spin_unlock_irqrestore(&tport->tp_lock, flags);
1047
1048 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1049 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1050 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1051 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1052 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1053 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1054 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1055
1056 dbg("%s - 0x%04X", __func__, result);
1057
1058 return result;
1059 }
1060
1061
1062 static int ti_tiocmset(struct tty_struct *tty, struct file *file,
1063 unsigned int set, unsigned int clear)
1064 {
1065 struct usb_serial_port *port = tty->driver_data;
1066 struct ti_port *tport = usb_get_serial_port_data(port);
1067 unsigned int mcr;
1068 unsigned long flags;
1069
1070 dbg("%s - port %d", __func__, port->number);
1071
1072 if (tport == NULL)
1073 return -ENODEV;
1074
1075 spin_lock_irqsave(&tport->tp_lock, flags);
1076 mcr = tport->tp_shadow_mcr;
1077
1078 if (set & TIOCM_RTS)
1079 mcr |= TI_MCR_RTS;
1080 if (set & TIOCM_DTR)
1081 mcr |= TI_MCR_DTR;
1082 if (set & TIOCM_LOOP)
1083 mcr |= TI_MCR_LOOP;
1084
1085 if (clear & TIOCM_RTS)
1086 mcr &= ~TI_MCR_RTS;
1087 if (clear & TIOCM_DTR)
1088 mcr &= ~TI_MCR_DTR;
1089 if (clear & TIOCM_LOOP)
1090 mcr &= ~TI_MCR_LOOP;
1091 spin_unlock_irqrestore(&tport->tp_lock, flags);
1092
1093 return ti_set_mcr(tport, mcr);
1094 }
1095
1096
1097 static void ti_break(struct tty_struct *tty, int break_state)
1098 {
1099 struct usb_serial_port *port = tty->driver_data;
1100 struct ti_port *tport = usb_get_serial_port_data(port);
1101 int status;
1102
1103 dbg("%s - state = %d", __func__, break_state);
1104
1105 if (tport == NULL)
1106 return;
1107
1108 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1109
1110 status = ti_write_byte(tport->tp_tdev,
1111 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1112 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1113
1114 if (status)
1115 dbg("%s - error setting break, %d", __func__, status);
1116 }
1117
1118
1119 static void ti_interrupt_callback(struct urb *urb)
1120 {
1121 struct ti_device *tdev = urb->context;
1122 struct usb_serial_port *port;
1123 struct usb_serial *serial = tdev->td_serial;
1124 struct ti_port *tport;
1125 struct device *dev = &urb->dev->dev;
1126 unsigned char *data = urb->transfer_buffer;
1127 int length = urb->actual_length;
1128 int port_number;
1129 int function;
1130 int status = urb->status;
1131 int retval;
1132 __u8 msr;
1133
1134 dbg("%s", __func__);
1135
1136 switch (status) {
1137 case 0:
1138 break;
1139 case -ECONNRESET:
1140 case -ENOENT:
1141 case -ESHUTDOWN:
1142 dbg("%s - urb shutting down, %d", __func__, status);
1143 tdev->td_urb_error = 1;
1144 return;
1145 default:
1146 dev_err(dev, "%s - nonzero urb status, %d\n",
1147 __func__, status);
1148 tdev->td_urb_error = 1;
1149 goto exit;
1150 }
1151
1152 if (length != 2) {
1153 dbg("%s - bad packet size, %d", __func__, length);
1154 goto exit;
1155 }
1156
1157 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1158 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1159 goto exit;
1160 }
1161
1162 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1163 function = TI_GET_FUNC_FROM_CODE(data[0]);
1164
1165 dbg("%s - port_number %d, function %d, data 0x%02X",
1166 __func__, port_number, function, data[1]);
1167
1168 if (port_number >= serial->num_ports) {
1169 dev_err(dev, "%s - bad port number, %d\n",
1170 __func__, port_number);
1171 goto exit;
1172 }
1173
1174 port = serial->port[port_number];
1175
1176 tport = usb_get_serial_port_data(port);
1177 if (!tport)
1178 goto exit;
1179
1180 switch (function) {
1181 case TI_CODE_DATA_ERROR:
1182 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1183 __func__, port_number, data[1]);
1184 break;
1185
1186 case TI_CODE_MODEM_STATUS:
1187 msr = data[1];
1188 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
1189 ti_handle_new_msr(tport, msr);
1190 break;
1191
1192 default:
1193 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1194 __func__, data[1]);
1195 break;
1196 }
1197
1198 exit:
1199 retval = usb_submit_urb(urb, GFP_ATOMIC);
1200 if (retval)
1201 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1202 __func__, retval);
1203 }
1204
1205
1206 static void ti_bulk_in_callback(struct urb *urb)
1207 {
1208 struct ti_port *tport = urb->context;
1209 struct usb_serial_port *port = tport->tp_port;
1210 struct device *dev = &urb->dev->dev;
1211 int status = urb->status;
1212 int retval = 0;
1213 struct tty_struct *tty;
1214
1215 dbg("%s", __func__);
1216
1217 switch (status) {
1218 case 0:
1219 break;
1220 case -ECONNRESET:
1221 case -ENOENT:
1222 case -ESHUTDOWN:
1223 dbg("%s - urb shutting down, %d", __func__, status);
1224 tport->tp_tdev->td_urb_error = 1;
1225 wake_up_interruptible(&tport->tp_write_wait);
1226 return;
1227 default:
1228 dev_err(dev, "%s - nonzero urb status, %d\n",
1229 __func__, status);
1230 tport->tp_tdev->td_urb_error = 1;
1231 wake_up_interruptible(&tport->tp_write_wait);
1232 }
1233
1234 if (status == -EPIPE)
1235 goto exit;
1236
1237 if (status) {
1238 dev_err(dev, "%s - stopping read!\n", __func__);
1239 return;
1240 }
1241
1242 tty = tty_port_tty_get(&port->port);
1243 if (tty && urb->actual_length) {
1244 usb_serial_debug_data(debug, dev, __func__,
1245 urb->actual_length, urb->transfer_buffer);
1246
1247 if (!tport->tp_is_open)
1248 dbg("%s - port closed, dropping data", __func__);
1249 else
1250 ti_recv(&urb->dev->dev, tty,
1251 urb->transfer_buffer,
1252 urb->actual_length);
1253
1254 spin_lock(&tport->tp_lock);
1255 tport->tp_icount.rx += urb->actual_length;
1256 spin_unlock(&tport->tp_lock);
1257 tty_kref_put(tty);
1258 }
1259
1260 exit:
1261 /* continue to read unless stopping */
1262 spin_lock(&tport->tp_lock);
1263 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1264 urb->dev = port->serial->dev;
1265 retval = usb_submit_urb(urb, GFP_ATOMIC);
1266 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1267 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1268 }
1269 spin_unlock(&tport->tp_lock);
1270 if (retval)
1271 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1272 __func__, retval);
1273 }
1274
1275
1276 static void ti_bulk_out_callback(struct urb *urb)
1277 {
1278 struct ti_port *tport = urb->context;
1279 struct usb_serial_port *port = tport->tp_port;
1280 struct device *dev = &urb->dev->dev;
1281 int status = urb->status;
1282
1283 dbg("%s - port %d", __func__, port->number);
1284
1285 tport->tp_write_urb_in_use = 0;
1286
1287 switch (status) {
1288 case 0:
1289 break;
1290 case -ECONNRESET:
1291 case -ENOENT:
1292 case -ESHUTDOWN:
1293 dbg("%s - urb shutting down, %d", __func__, status);
1294 tport->tp_tdev->td_urb_error = 1;
1295 wake_up_interruptible(&tport->tp_write_wait);
1296 return;
1297 default:
1298 dev_err(dev, "%s - nonzero urb status, %d\n",
1299 __func__, status);
1300 tport->tp_tdev->td_urb_error = 1;
1301 wake_up_interruptible(&tport->tp_write_wait);
1302 }
1303
1304 /* send any buffered data */
1305 ti_send(tport);
1306 }
1307
1308
1309 static void ti_recv(struct device *dev, struct tty_struct *tty,
1310 unsigned char *data, int length)
1311 {
1312 int cnt;
1313
1314 do {
1315 cnt = tty_buffer_request_room(tty, length);
1316 if (cnt < length) {
1317 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1318 __func__, length - cnt);
1319 if (cnt == 0)
1320 break;
1321 }
1322 tty_insert_flip_string(tty, data, cnt);
1323 tty_flip_buffer_push(tty);
1324 data += cnt;
1325 length -= cnt;
1326 } while (length > 0);
1327
1328 }
1329
1330
1331 static void ti_send(struct ti_port *tport)
1332 {
1333 int count, result;
1334 struct usb_serial_port *port = tport->tp_port;
1335 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1336 unsigned long flags;
1337
1338
1339 dbg("%s - port %d", __func__, port->number);
1340
1341 spin_lock_irqsave(&tport->tp_lock, flags);
1342
1343 if (tport->tp_write_urb_in_use)
1344 goto unlock;
1345
1346 count = ti_buf_get(tport->tp_write_buf,
1347 port->write_urb->transfer_buffer,
1348 port->bulk_out_size);
1349
1350 if (count == 0)
1351 goto unlock;
1352
1353 tport->tp_write_urb_in_use = 1;
1354
1355 spin_unlock_irqrestore(&tport->tp_lock, flags);
1356
1357 usb_serial_debug_data(debug, &port->dev, __func__, count,
1358 port->write_urb->transfer_buffer);
1359
1360 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1361 usb_sndbulkpipe(port->serial->dev,
1362 port->bulk_out_endpointAddress),
1363 port->write_urb->transfer_buffer, count,
1364 ti_bulk_out_callback, tport);
1365
1366 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1367 if (result) {
1368 dev_err(&port->dev, "%s - submit write urb failed, %d\n",
1369 __func__, result);
1370 tport->tp_write_urb_in_use = 0;
1371 /* TODO: reschedule ti_send */
1372 } else {
1373 spin_lock_irqsave(&tport->tp_lock, flags);
1374 tport->tp_icount.tx += count;
1375 spin_unlock_irqrestore(&tport->tp_lock, flags);
1376 }
1377
1378 /* more room in the buffer for new writes, wakeup */
1379 if (tty)
1380 tty_wakeup(tty);
1381 tty_kref_put(tty);
1382 wake_up_interruptible(&tport->tp_write_wait);
1383 return;
1384 unlock:
1385 spin_unlock_irqrestore(&tport->tp_lock, flags);
1386 tty_kref_put(tty);
1387 return;
1388 }
1389
1390
1391 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1392 {
1393 unsigned long flags;
1394 int status;
1395
1396 status = ti_write_byte(tport->tp_tdev,
1397 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1398 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1399
1400 spin_lock_irqsave(&tport->tp_lock, flags);
1401 if (!status)
1402 tport->tp_shadow_mcr = mcr;
1403 spin_unlock_irqrestore(&tport->tp_lock, flags);
1404
1405 return status;
1406 }
1407
1408
1409 static int ti_get_lsr(struct ti_port *tport)
1410 {
1411 int size, status;
1412 struct ti_device *tdev = tport->tp_tdev;
1413 struct usb_serial_port *port = tport->tp_port;
1414 int port_number = port->number - port->serial->minor;
1415 struct ti_port_status *data;
1416
1417 dbg("%s - port %d", __func__, port->number);
1418
1419 size = sizeof(struct ti_port_status);
1420 data = kmalloc(size, GFP_KERNEL);
1421 if (!data) {
1422 dev_err(&port->dev, "%s - out of memory\n", __func__);
1423 return -ENOMEM;
1424 }
1425
1426 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1427 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1428 if (status) {
1429 dev_err(&port->dev,
1430 "%s - get port status command failed, %d\n",
1431 __func__, status);
1432 goto free_data;
1433 }
1434
1435 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
1436
1437 tport->tp_lsr = data->bLSR;
1438
1439 free_data:
1440 kfree(data);
1441 return status;
1442 }
1443
1444
1445 static int ti_get_serial_info(struct ti_port *tport,
1446 struct serial_struct __user *ret_arg)
1447 {
1448 struct usb_serial_port *port = tport->tp_port;
1449 struct serial_struct ret_serial;
1450
1451 if (!ret_arg)
1452 return -EFAULT;
1453
1454 memset(&ret_serial, 0, sizeof(ret_serial));
1455
1456 ret_serial.type = PORT_16550A;
1457 ret_serial.line = port->serial->minor;
1458 ret_serial.port = port->number - port->serial->minor;
1459 ret_serial.flags = tport->tp_flags;
1460 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1461 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1462 ret_serial.closing_wait = tport->tp_closing_wait;
1463
1464 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1465 return -EFAULT;
1466
1467 return 0;
1468 }
1469
1470
1471 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1472 struct serial_struct __user *new_arg)
1473 {
1474 struct serial_struct new_serial;
1475
1476 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1477 return -EFAULT;
1478
1479 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1480 tty->low_latency = (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1481 tport->tp_closing_wait = new_serial.closing_wait;
1482
1483 return 0;
1484 }
1485
1486
1487 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1488 {
1489 struct async_icount *icount;
1490 struct tty_struct *tty;
1491 unsigned long flags;
1492
1493 dbg("%s - msr 0x%02X", __func__, msr);
1494
1495 if (msr & TI_MSR_DELTA_MASK) {
1496 spin_lock_irqsave(&tport->tp_lock, flags);
1497 icount = &tport->tp_icount;
1498 if (msr & TI_MSR_DELTA_CTS)
1499 icount->cts++;
1500 if (msr & TI_MSR_DELTA_DSR)
1501 icount->dsr++;
1502 if (msr & TI_MSR_DELTA_CD)
1503 icount->dcd++;
1504 if (msr & TI_MSR_DELTA_RI)
1505 icount->rng++;
1506 wake_up_interruptible(&tport->tp_msr_wait);
1507 spin_unlock_irqrestore(&tport->tp_lock, flags);
1508 }
1509
1510 tport->tp_msr = msr & TI_MSR_MASK;
1511
1512 /* handle CTS flow control */
1513 tty = tty_port_tty_get(&tport->tp_port->port);
1514 if (tty && C_CRTSCTS(tty)) {
1515 if (msr & TI_MSR_CTS) {
1516 tty->hw_stopped = 0;
1517 tty_wakeup(tty);
1518 } else {
1519 tty->hw_stopped = 1;
1520 }
1521 }
1522 tty_kref_put(tty);
1523 }
1524
1525
1526 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1527 {
1528 struct ti_device *tdev = tport->tp_tdev;
1529 struct usb_serial_port *port = tport->tp_port;
1530 wait_queue_t wait;
1531
1532 dbg("%s - port %d", __func__, port->number);
1533
1534 spin_lock_irq(&tport->tp_lock);
1535
1536 /* wait for data to drain from the buffer */
1537 tdev->td_urb_error = 0;
1538 init_waitqueue_entry(&wait, current);
1539 add_wait_queue(&tport->tp_write_wait, &wait);
1540 for (;;) {
1541 set_current_state(TASK_INTERRUPTIBLE);
1542 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1543 || timeout == 0 || signal_pending(current)
1544 || tdev->td_urb_error
1545 || port->serial->disconnected) /* disconnect */
1546 break;
1547 spin_unlock_irq(&tport->tp_lock);
1548 timeout = schedule_timeout(timeout);
1549 spin_lock_irq(&tport->tp_lock);
1550 }
1551 set_current_state(TASK_RUNNING);
1552 remove_wait_queue(&tport->tp_write_wait, &wait);
1553
1554 /* flush any remaining data in the buffer */
1555 if (flush)
1556 ti_buf_clear(tport->tp_write_buf);
1557
1558 spin_unlock_irq(&tport->tp_lock);
1559
1560 mutex_lock(&port->serial->disc_mutex);
1561 /* wait for data to drain from the device */
1562 /* wait for empty tx register, plus 20 ms */
1563 timeout += jiffies;
1564 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1565 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1566 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1567 && !port->serial->disconnected) {
1568 if (ti_get_lsr(tport))
1569 break;
1570 mutex_unlock(&port->serial->disc_mutex);
1571 msleep_interruptible(20);
1572 mutex_lock(&port->serial->disc_mutex);
1573 }
1574 mutex_unlock(&port->serial->disc_mutex);
1575 }
1576
1577
1578 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1579 {
1580 unsigned long flags;
1581
1582 spin_lock_irqsave(&tport->tp_lock, flags);
1583
1584 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1585 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1586
1587 spin_unlock_irqrestore(&tport->tp_lock, flags);
1588 }
1589
1590
1591 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1592 {
1593 struct urb *urb;
1594 int status = 0;
1595 unsigned long flags;
1596
1597 spin_lock_irqsave(&tport->tp_lock, flags);
1598
1599 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1600 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1601 urb = tport->tp_port->read_urb;
1602 spin_unlock_irqrestore(&tport->tp_lock, flags);
1603 urb->complete = ti_bulk_in_callback;
1604 urb->context = tport;
1605 urb->dev = tport->tp_port->serial->dev;
1606 status = usb_submit_urb(urb, GFP_KERNEL);
1607 } else {
1608 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1609 spin_unlock_irqrestore(&tport->tp_lock, flags);
1610 }
1611
1612 return status;
1613 }
1614
1615
1616 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1617 __u16 moduleid, __u16 value, __u8 *data, int size)
1618 {
1619 int status;
1620
1621 status = usb_control_msg(tdev->td_serial->dev,
1622 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1623 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1624 value, moduleid, data, size, 1000);
1625
1626 if (status == size)
1627 status = 0;
1628
1629 if (status > 0)
1630 status = -ECOMM;
1631
1632 return status;
1633 }
1634
1635
1636 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1637 __u16 moduleid, __u16 value, __u8 *data, int size)
1638 {
1639 int status;
1640
1641 status = usb_control_msg(tdev->td_serial->dev,
1642 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1643 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1644 value, moduleid, data, size, 1000);
1645
1646 if (status == size)
1647 status = 0;
1648
1649 if (status > 0)
1650 status = -ECOMM;
1651
1652 return status;
1653 }
1654
1655
1656 static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1657 __u8 mask, __u8 byte)
1658 {
1659 int status;
1660 unsigned int size;
1661 struct ti_write_data_bytes *data;
1662 struct device *dev = &tdev->td_serial->dev->dev;
1663
1664 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1665 __func__, addr, mask, byte);
1666
1667 size = sizeof(struct ti_write_data_bytes) + 2;
1668 data = kmalloc(size, GFP_KERNEL);
1669 if (!data) {
1670 dev_err(dev, "%s - out of memory\n", __func__);
1671 return -ENOMEM;
1672 }
1673
1674 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1675 data->bDataType = TI_RW_DATA_BYTE;
1676 data->bDataCounter = 1;
1677 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1678 data->wBaseAddrLo = cpu_to_be16(addr);
1679 data->bData[0] = mask;
1680 data->bData[1] = byte;
1681
1682 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1683 (__u8 *)data, size);
1684
1685 if (status < 0)
1686 dev_err(dev, "%s - failed, %d\n", __func__, status);
1687
1688 kfree(data);
1689
1690 return status;
1691 }
1692
1693 static int ti_do_download(struct usb_device *dev, int pipe,
1694 u8 *buffer, int size)
1695 {
1696 int pos;
1697 u8 cs = 0;
1698 int done;
1699 struct ti_firmware_header *header;
1700 int status;
1701 int len;
1702
1703 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1704 cs = (__u8)(cs + buffer[pos]);
1705
1706 header = (struct ti_firmware_header *)buffer;
1707 header->wLength = cpu_to_le16((__u16)(size
1708 - sizeof(struct ti_firmware_header)));
1709 header->bCheckSum = cs;
1710
1711 dbg("%s - downloading firmware", __func__);
1712 for (pos = 0; pos < size; pos += done) {
1713 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1714 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1715 &done, 1000);
1716 if (status)
1717 break;
1718 }
1719 return status;
1720 }
1721
1722 static int ti_download_firmware(struct ti_device *tdev, int type)
1723 {
1724 int status = -ENOMEM;
1725 int buffer_size;
1726 __u8 *buffer;
1727 struct usb_device *dev = tdev->td_serial->dev;
1728 unsigned int pipe = usb_sndbulkpipe(dev,
1729 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1730 const struct firmware *fw_p;
1731 char buf[32];
1732 sprintf(buf, "ti_usb-%d.bin", type);
1733
1734 if (request_firmware(&fw_p, buf, &dev->dev)) {
1735 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1736 return -ENOENT;
1737 }
1738 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1739 dev_err(&dev->dev, "%s - firmware too large\n", __func__);
1740 return -ENOENT;
1741 }
1742
1743 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1744 buffer = kmalloc(buffer_size, GFP_KERNEL);
1745 if (buffer) {
1746 memcpy(buffer, fw_p->data, fw_p->size);
1747 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1748 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1749 kfree(buffer);
1750 }
1751 release_firmware(fw_p);
1752 if (status) {
1753 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1754 __func__, status);
1755 return status;
1756 }
1757
1758 dbg("%s - download successful", __func__);
1759
1760 return 0;
1761 }
1762
1763
1764 /* Circular Buffer Functions */
1765
1766 /*
1767 * ti_buf_alloc
1768 *
1769 * Allocate a circular buffer and all associated memory.
1770 */
1771
1772 static struct circ_buf *ti_buf_alloc(void)
1773 {
1774 struct circ_buf *cb;
1775
1776 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
1777 if (cb == NULL)
1778 return NULL;
1779
1780 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1781 if (cb->buf == NULL) {
1782 kfree(cb);
1783 return NULL;
1784 }
1785
1786 ti_buf_clear(cb);
1787
1788 return cb;
1789 }
1790
1791
1792 /*
1793 * ti_buf_free
1794 *
1795 * Free the buffer and all associated memory.
1796 */
1797
1798 static void ti_buf_free(struct circ_buf *cb)
1799 {
1800 kfree(cb->buf);
1801 kfree(cb);
1802 }
1803
1804
1805 /*
1806 * ti_buf_clear
1807 *
1808 * Clear out all data in the circular buffer.
1809 */
1810
1811 static void ti_buf_clear(struct circ_buf *cb)
1812 {
1813 cb->head = cb->tail = 0;
1814 }
1815
1816
1817 /*
1818 * ti_buf_data_avail
1819 *
1820 * Return the number of bytes of data available in the circular
1821 * buffer.
1822 */
1823
1824 static int ti_buf_data_avail(struct circ_buf *cb)
1825 {
1826 return CIRC_CNT(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1827 }
1828
1829
1830 /*
1831 * ti_buf_space_avail
1832 *
1833 * Return the number of bytes of space available in the circular
1834 * buffer.
1835 */
1836
1837 static int ti_buf_space_avail(struct circ_buf *cb)
1838 {
1839 return CIRC_SPACE(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1840 }
1841
1842
1843 /*
1844 * ti_buf_put
1845 *
1846 * Copy data data from a user buffer and put it into the circular buffer.
1847 * Restrict to the amount of space available.
1848 *
1849 * Return the number of bytes copied.
1850 */
1851
1852 static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1853 {
1854 int c, ret = 0;
1855
1856 while (1) {
1857 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1858 if (count < c)
1859 c = count;
1860 if (c <= 0)
1861 break;
1862 memcpy(cb->buf + cb->head, buf, c);
1863 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1864 buf += c;
1865 count -= c;
1866 ret += c;
1867 }
1868
1869 return ret;
1870 }
1871
1872
1873 /*
1874 * ti_buf_get
1875 *
1876 * Get data from the circular buffer and copy to the given buffer.
1877 * Restrict to the amount of data available.
1878 *
1879 * Return the number of bytes copied.
1880 */
1881
1882 static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1883 {
1884 int c, ret = 0;
1885
1886 while (1) {
1887 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1888 if (count < c)
1889 c = count;
1890 if (c <= 0)
1891 break;
1892 memcpy(buf, cb->buf + cb->tail, c);
1893 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);
1894 buf += c;
1895 count -= c;
1896 ret += c;
1897 }
1898
1899 return ret;
1900 }