blob: 5b470f76e91b55f15e05b4b484a643a54785ee98 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/kernel.h>
72#include <linux/errno.h>
73#include <linux/init.h>
74#include <linux/slab.h>
75#include <linux/tty.h>
76#include <linux/tty_driver.h>
77#include <linux/tty_flip.h>
78#include <linux/module.h>
79#include <linux/spinlock.h>
80#include <linux/ioctl.h>
81#include <linux/serial.h>
82#include <linux/circ_buf.h>
Matthias Kaehlcke9da00682007-11-21 15:13:16 -080083#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070086#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include "ti_usb_3410_5052.h"
89#include "ti_fw_3410.h" /* firmware image for 3410 */
90#include "ti_fw_5052.h" /* firmware image for 5052 */
91
92
93/* Defines */
94
95#define TI_DRIVER_VERSION "v0.9"
96#define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
97#define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
98
99#define TI_FIRMWARE_BUF_SIZE 16284
100
101#define TI_WRITE_BUF_SIZE 1024
102
103#define TI_TRANSFER_TIMEOUT 2
104
105#define TI_DEFAULT_LOW_LATENCY 0
106#define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
107
108/* supported setserial flags */
109#define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
110
111/* read urb states */
112#define TI_READ_URB_RUNNING 0
113#define TI_READ_URB_STOPPING 1
114#define TI_READ_URB_STOPPED 2
115
116#define TI_EXTRA_VID_PID_COUNT 5
117
118
119/* Structures */
120
121struct ti_port {
122 int tp_is_open;
123 __u8 tp_msr;
124 __u8 tp_lsr;
125 __u8 tp_shadow_mcr;
126 __u8 tp_uart_mode; /* 232 or 485 modes */
127 unsigned int tp_uart_base_addr;
128 int tp_flags;
129 int tp_closing_wait;/* in .01 secs */
130 struct async_icount tp_icount;
131 wait_queue_head_t tp_msr_wait; /* wait for msr change */
132 wait_queue_head_t tp_write_wait;
133 struct ti_device *tp_tdev;
134 struct usb_serial_port *tp_port;
135 spinlock_t tp_lock;
136 int tp_read_urb_state;
137 int tp_write_urb_in_use;
138 struct circ_buf *tp_write_buf;
139};
140
141struct ti_device {
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800142 struct mutex td_open_close_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int td_open_port_count;
144 struct usb_serial *td_serial;
145 int td_is_3410;
146 int td_urb_error;
147};
148
149
150/* Function Declarations */
151
152static int ti_startup(struct usb_serial *serial);
153static void ti_shutdown(struct usb_serial *serial);
154static int ti_open(struct usb_serial_port *port, struct file *file);
155static void ti_close(struct usb_serial_port *port, struct file *file);
156static int ti_write(struct usb_serial_port *port, const unsigned char *data,
157 int count);
158static int ti_write_room(struct usb_serial_port *port);
159static int ti_chars_in_buffer(struct usb_serial_port *port);
160static void ti_throttle(struct usb_serial_port *port);
161static void ti_unthrottle(struct usb_serial_port *port);
162static int ti_ioctl(struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
163static void ti_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800164 struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static int ti_tiocmget(struct usb_serial_port *port, struct file *file);
166static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
167 unsigned int set, unsigned int clear);
168static void ti_break(struct usb_serial_port *port, int break_state);
David Howells7d12e782006-10-05 14:55:46 +0100169static void ti_interrupt_callback(struct urb *urb);
170static void ti_bulk_in_callback(struct urb *urb);
171static void ti_bulk_out_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static void ti_recv(struct device *dev, struct tty_struct *tty,
174 unsigned char *data, int length);
175static void ti_send(struct ti_port *tport);
176static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
177static int ti_get_lsr(struct ti_port *tport);
178static int ti_get_serial_info(struct ti_port *tport,
179 struct serial_struct __user *ret_arg);
180static int ti_set_serial_info(struct ti_port *tport,
181 struct serial_struct __user *new_arg);
182static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
183
184static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
185
186static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
187static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
188
189static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
190 __u16 moduleid, __u16 value, __u8 *data, int size);
191static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
192 __u16 moduleid, __u16 value, __u8 *data, int size);
193
194static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
195 __u8 mask, __u8 byte);
196
197static int ti_download_firmware(struct ti_device *tdev,
198 unsigned char *firmware, unsigned int firmware_size);
199
200/* circular buffer */
201static struct circ_buf *ti_buf_alloc(void);
202static void ti_buf_free(struct circ_buf *cb);
203static void ti_buf_clear(struct circ_buf *cb);
204static int ti_buf_data_avail(struct circ_buf *cb);
205static int ti_buf_space_avail(struct circ_buf *cb);
206static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
207static int ti_buf_get(struct circ_buf *cb, char *buf, int count);
208
209
210/* Data */
211
212/* module parameters */
213static int debug;
214static int low_latency = TI_DEFAULT_LOW_LATENCY;
215static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
216static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100217static unsigned int vendor_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100219static unsigned int product_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100221static unsigned int vendor_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100223static unsigned int product_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
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 */
229static 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) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000231 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
234static 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
241static struct usb_device_id ti_id_table_combined[] = {
242 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000243 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 { 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
251static struct usb_driver ti_usb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .name = "ti_usb_3410_5052",
253 .probe = usb_serial_probe,
254 .disconnect = usb_serial_disconnect,
255 .id_table = ti_id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800256 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257};
258
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700259static struct usb_serial_driver ti_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700260 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700261 .owner = THIS_MODULE,
262 .name = "ti_usb_3410_5052_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700263 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700264 .description = "TI USB 3410 1 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100265 .usb_driver = &ti_usb_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .id_table = ti_id_table_3410,
Robert Spanton1bfd6692008-03-23 19:47:23 +0000267 .num_interrupt_in = NUM_DONT_CARE,
268 .num_bulk_in = NUM_DONT_CARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 .num_bulk_out = 1,
270 .num_ports = 1,
271 .attach = ti_startup,
272 .shutdown = ti_shutdown,
273 .open = ti_open,
274 .close = ti_close,
275 .write = ti_write,
276 .write_room = ti_write_room,
277 .chars_in_buffer = ti_chars_in_buffer,
278 .throttle = ti_throttle,
279 .unthrottle = ti_unthrottle,
280 .ioctl = ti_ioctl,
281 .set_termios = ti_set_termios,
282 .tiocmget = ti_tiocmget,
283 .tiocmset = ti_tiocmset,
284 .break_ctl = ti_break,
285 .read_int_callback = ti_interrupt_callback,
286 .read_bulk_callback = ti_bulk_in_callback,
287 .write_bulk_callback = ti_bulk_out_callback,
288};
289
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700290static struct usb_serial_driver ti_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700291 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700292 .owner = THIS_MODULE,
293 .name = "ti_usb_3410_5052_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700294 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700295 .description = "TI USB 5052 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100296 .usb_driver = &ti_usb_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 .id_table = ti_id_table_5052,
298 .num_interrupt_in = 1,
299 .num_bulk_in = 2,
300 .num_bulk_out = 2,
301 .num_ports = 2,
302 .attach = ti_startup,
303 .shutdown = ti_shutdown,
304 .open = ti_open,
305 .close = ti_close,
306 .write = ti_write,
307 .write_room = ti_write_room,
308 .chars_in_buffer = ti_chars_in_buffer,
309 .throttle = ti_throttle,
310 .unthrottle = ti_unthrottle,
311 .ioctl = ti_ioctl,
312 .set_termios = ti_set_termios,
313 .tiocmget = ti_tiocmget,
314 .tiocmset = ti_tiocmset,
315 .break_ctl = ti_break,
316 .read_int_callback = ti_interrupt_callback,
317 .read_bulk_callback = ti_bulk_in_callback,
318 .write_bulk_callback = ti_bulk_out_callback,
319};
320
321
322/* Module */
323
324MODULE_AUTHOR(TI_DRIVER_AUTHOR);
325MODULE_DESCRIPTION(TI_DRIVER_DESC);
326MODULE_VERSION(TI_DRIVER_VERSION);
327MODULE_LICENSE("GPL");
328
329module_param(debug, bool, S_IRUGO | S_IWUSR);
330MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
331
332module_param(low_latency, bool, S_IRUGO | S_IWUSR);
333MODULE_PARM_DESC(low_latency, "TTY low_latency flag, 0=off, 1=on, default is off");
334
335module_param(closing_wait, int, S_IRUGO | S_IWUSR);
336MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain in close, in .01 secs, default is 4000");
337
338module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
339MODULE_PARM_DESC(vendor_3410, "Vendor ids for 3410 based devices, 1-5 short integers");
340module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
341MODULE_PARM_DESC(product_3410, "Product ids for 3410 based devices, 1-5 short integers");
342module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
343MODULE_PARM_DESC(vendor_5052, "Vendor ids for 5052 based devices, 1-5 short integers");
344module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
345MODULE_PARM_DESC(product_5052, "Product ids for 5052 based devices, 1-5 short integers");
346
347MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
348
349
350/* Functions */
351
352static int __init ti_init(void)
353{
354 int i,j;
355 int ret;
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /* insert extra vendor and product ids */
Tobias Klauser52950ed2005-12-11 16:20:08 +0100358 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 for (i=0; i<min(vendor_3410_count,product_3410_count); i++,j++) {
360 ti_id_table_3410[j].idVendor = vendor_3410[i];
361 ti_id_table_3410[j].idProduct = product_3410[i];
362 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
363 }
Tobias Klauser52950ed2005-12-11 16:20:08 +0100364 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 for (i=0; i<min(vendor_5052_count,product_5052_count); i++,j++) {
366 ti_id_table_5052[j].idVendor = vendor_5052[i];
367 ti_id_table_5052[j].idProduct = product_5052[i];
368 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
369 }
370
371 ret = usb_serial_register(&ti_1port_device);
372 if (ret)
373 goto failed_1port;
374 ret = usb_serial_register(&ti_2port_device);
375 if (ret)
376 goto failed_2port;
377
378 ret = usb_register(&ti_usb_driver);
379 if (ret)
380 goto failed_usb;
381
382 info(TI_DRIVER_DESC " " TI_DRIVER_VERSION);
383
384 return 0;
385
386failed_usb:
387 usb_serial_deregister(&ti_2port_device);
388failed_2port:
389 usb_serial_deregister(&ti_1port_device);
390failed_1port:
391 return ret;
392}
393
394
395static void __exit ti_exit(void)
396{
397 usb_serial_deregister(&ti_1port_device);
398 usb_serial_deregister(&ti_2port_device);
399 usb_deregister(&ti_usb_driver);
400}
401
402
403module_init(ti_init);
404module_exit(ti_exit);
405
406
407static int ti_startup(struct usb_serial *serial)
408{
409 struct ti_device *tdev;
410 struct ti_port *tport;
411 struct usb_device *dev = serial->dev;
412 int status;
413 int i;
414
415
416 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
417 __FUNCTION__, le16_to_cpu(dev->descriptor.idProduct),
418 dev->descriptor.bNumConfigurations,
419 dev->actconfig->desc.bConfigurationValue);
420
421 /* create device structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100422 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (tdev == NULL) {
424 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
425 return -ENOMEM;
426 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800427 mutex_init(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 tdev->td_serial = serial;
429 usb_set_serial_data(serial, tdev);
430
431 /* determine device type */
432 if (usb_match_id(serial->interface, ti_id_table_3410))
433 tdev->td_is_3410 = 1;
434 dbg("%s - device type is %s", __FUNCTION__, tdev->td_is_3410 ? "3410" : "5052");
435
436 /* if we have only 1 configuration, download firmware */
437 if (dev->descriptor.bNumConfigurations == 1) {
438
439 if (tdev->td_is_3410)
440 status = ti_download_firmware(tdev, ti_fw_3410,
441 sizeof(ti_fw_3410));
442 else
443 status = ti_download_firmware(tdev, ti_fw_5052,
444 sizeof(ti_fw_5052));
445 if (status)
446 goto free_tdev;
447
448 /* 3410 must be reset, 5052 resets itself */
449 if (tdev->td_is_3410) {
450 msleep_interruptible(100);
451 usb_reset_device(dev);
452 }
453
454 status = -ENODEV;
455 goto free_tdev;
456 }
457
458 /* the second configuration must be set (in sysfs by hotplug script) */
459 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
460 status = -ENODEV;
461 goto free_tdev;
462 }
463
464 /* set up port structures */
465 for (i = 0; i < serial->num_ports; ++i) {
Burman Yan7ac9da12006-11-22 20:54:38 +0200466 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (tport == NULL) {
468 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
469 status = -ENOMEM;
470 goto free_tports;
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 spin_lock_init(&tport->tp_lock);
473 tport->tp_uart_base_addr = (i == 0 ? TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
474 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
475 tport->tp_closing_wait = closing_wait;
476 init_waitqueue_head(&tport->tp_msr_wait);
477 init_waitqueue_head(&tport->tp_write_wait);
478 tport->tp_write_buf = ti_buf_alloc();
479 if (tport->tp_write_buf == NULL) {
480 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
481 kfree(tport);
482 status = -ENOMEM;
483 goto free_tports;
484 }
485 tport->tp_port = serial->port[i];
486 tport->tp_tdev = tdev;
487 usb_set_serial_port_data(serial->port[i], tport);
488 tport->tp_uart_mode = 0; /* default is RS232 */
489 }
490
491 return 0;
492
493free_tports:
494 for (--i; i>=0; --i) {
495 tport = usb_get_serial_port_data(serial->port[i]);
496 ti_buf_free(tport->tp_write_buf);
497 kfree(tport);
498 usb_set_serial_port_data(serial->port[i], NULL);
499 }
500free_tdev:
501 kfree(tdev);
502 usb_set_serial_data(serial, NULL);
503 return status;
504}
505
506
507static void ti_shutdown(struct usb_serial *serial)
508{
509 int i;
510 struct ti_device *tdev = usb_get_serial_data(serial);
511 struct ti_port *tport;
512
513 dbg("%s", __FUNCTION__);
514
515 for (i=0; i < serial->num_ports; ++i) {
516 tport = usb_get_serial_port_data(serial->port[i]);
517 if (tport) {
518 ti_buf_free(tport->tp_write_buf);
519 kfree(tport);
520 usb_set_serial_port_data(serial->port[i], NULL);
521 }
522 }
523
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700524 kfree(tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 usb_set_serial_data(serial, NULL);
526}
527
528
529static int ti_open(struct usb_serial_port *port, struct file *file)
530{
531 struct ti_port *tport = usb_get_serial_port_data(port);
532 struct ti_device *tdev;
533 struct usb_device *dev;
534 struct urb *urb;
535 int port_number;
536 int status;
537 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
538 TI_PIPE_TIMEOUT_ENABLE |
539 (TI_TRANSFER_TIMEOUT << 2));
540
541 dbg("%s - port %d", __FUNCTION__, port->number);
542
543 if (tport == NULL)
544 return -ENODEV;
545
546 dev = port->serial->dev;
547 tdev = tport->tp_tdev;
548
549 /* only one open on any port on a device at a time */
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800550 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return -ERESTARTSYS;
552
553 if (port->tty)
554 port->tty->low_latency =
555 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
556
557 port_number = port->number - port->serial->minor;
558
559 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
560
561 tport->tp_msr = 0;
562 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
563
564 /* start interrupt urb the first time a port is opened on this device */
565 if (tdev->td_open_port_count == 0) {
566 dbg("%s - start interrupt in urb", __FUNCTION__);
567 urb = tdev->td_serial->port[0]->interrupt_in_urb;
568 if (!urb) {
569 dev_err(&port->dev, "%s - no interrupt urb\n", __FUNCTION__);
570 status = -EINVAL;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800571 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573 urb->complete = ti_interrupt_callback;
574 urb->context = tdev;
575 urb->dev = dev;
576 status = usb_submit_urb(urb, GFP_KERNEL);
577 if (status) {
578 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __FUNCTION__, status);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800579 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 }
582
Alan Cox537699e2008-01-03 17:03:11 +0000583 ti_set_termios(port, port->tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 dbg("%s - sending TI_OPEN_PORT", __FUNCTION__);
586 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
587 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
588 if (status) {
589 dev_err(&port->dev, "%s - cannot send open command, %d\n", __FUNCTION__, status);
590 goto unlink_int_urb;
591 }
592
593 dbg("%s - sending TI_START_PORT", __FUNCTION__);
594 status = ti_command_out_sync(tdev, TI_START_PORT,
595 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
596 if (status) {
597 dev_err(&port->dev, "%s - cannot send start command, %d\n", __FUNCTION__, status);
598 goto unlink_int_urb;
599 }
600
601 dbg("%s - sending TI_PURGE_PORT", __FUNCTION__);
602 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
603 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
604 if (status) {
605 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", __FUNCTION__, status);
606 goto unlink_int_urb;
607 }
608 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
609 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
610 if (status) {
611 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", __FUNCTION__, status);
612 goto unlink_int_urb;
613 }
614
615 /* reset the data toggle on the bulk endpoints to work around bug in
616 * host controllers where things get out of sync some times */
617 usb_clear_halt(dev, port->write_urb->pipe);
618 usb_clear_halt(dev, port->read_urb->pipe);
619
Alan Cox537699e2008-01-03 17:03:11 +0000620 ti_set_termios(port, port->tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 dbg("%s - sending TI_OPEN_PORT (2)", __FUNCTION__);
623 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
624 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
625 if (status) {
626 dev_err(&port->dev, "%s - cannot send open command (2), %d\n", __FUNCTION__, status);
627 goto unlink_int_urb;
628 }
629
630 dbg("%s - sending TI_START_PORT (2)", __FUNCTION__);
631 status = ti_command_out_sync(tdev, TI_START_PORT,
632 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
633 if (status) {
634 dev_err(&port->dev, "%s - cannot send start command (2), %d\n", __FUNCTION__, status);
635 goto unlink_int_urb;
636 }
637
638 /* start read urb */
639 dbg("%s - start read urb", __FUNCTION__);
640 urb = port->read_urb;
641 if (!urb) {
642 dev_err(&port->dev, "%s - no read urb\n", __FUNCTION__);
643 status = -EINVAL;
644 goto unlink_int_urb;
645 }
646 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
647 urb->complete = ti_bulk_in_callback;
648 urb->context = tport;
649 urb->dev = dev;
650 status = usb_submit_urb(urb, GFP_KERNEL);
651 if (status) {
652 dev_err(&port->dev, "%s - submit read urb failed, %d\n", __FUNCTION__, status);
653 goto unlink_int_urb;
654 }
655
656 tport->tp_is_open = 1;
657 ++tdev->td_open_port_count;
658
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800659 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661unlink_int_urb:
662 if (tdev->td_open_port_count == 0)
663 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800664release_lock:
665 mutex_unlock(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 dbg("%s - exit %d", __FUNCTION__, status);
667 return status;
668}
669
670
671static void ti_close(struct usb_serial_port *port, struct file *file)
672{
673 struct ti_device *tdev;
674 struct ti_port *tport;
675 int port_number;
676 int status;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800677 int do_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 dbg("%s - port %d", __FUNCTION__, port->number);
680
681 tdev = usb_get_serial_data(port->serial);
682 tport = usb_get_serial_port_data(port);
683 if (tdev == NULL || tport == NULL)
684 return;
685
686 tport->tp_is_open = 0;
687
688 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
689
690 usb_kill_urb(port->read_urb);
691 usb_kill_urb(port->write_urb);
692 tport->tp_write_urb_in_use = 0;
693
694 port_number = port->number - port->serial->minor;
695
696 dbg("%s - sending TI_CLOSE_PORT", __FUNCTION__);
697 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
698 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
699 if (status)
700 dev_err(&port->dev, "%s - cannot send close port command, %d\n" , __FUNCTION__, status);
701
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800702 /* if mutex_lock is interrupted, continue anyway */
703 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 --tport->tp_tdev->td_open_port_count;
705 if (tport->tp_tdev->td_open_port_count <= 0) {
706 /* last port is closed, shut down interrupt urb */
707 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
708 tport->tp_tdev->td_open_port_count = 0;
709 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800710 if (do_unlock)
711 mutex_unlock(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 dbg("%s - exit", __FUNCTION__);
714}
715
716
717static int ti_write(struct usb_serial_port *port, const unsigned char *data,
718 int count)
719{
720 struct ti_port *tport = usb_get_serial_port_data(port);
721 unsigned long flags;
722
723 dbg("%s - port %d", __FUNCTION__, port->number);
724
725 if (count == 0) {
726 dbg("%s - write request of 0 bytes", __FUNCTION__);
727 return 0;
728 }
729
730 if (tport == NULL || !tport->tp_is_open)
731 return -ENODEV;
732
733 spin_lock_irqsave(&tport->tp_lock, flags);
734 count = ti_buf_put(tport->tp_write_buf, data, count);
735 spin_unlock_irqrestore(&tport->tp_lock, flags);
736
737 ti_send(tport);
738
739 return count;
740}
741
742
743static int ti_write_room(struct usb_serial_port *port)
744{
745 struct ti_port *tport = usb_get_serial_port_data(port);
746 int room = 0;
747 unsigned long flags;
748
749 dbg("%s - port %d", __FUNCTION__, port->number);
750
751 if (tport == NULL)
752 return -ENODEV;
753
754 spin_lock_irqsave(&tport->tp_lock, flags);
755 room = ti_buf_space_avail(tport->tp_write_buf);
756 spin_unlock_irqrestore(&tport->tp_lock, flags);
757
758 dbg("%s - returns %d", __FUNCTION__, room);
759 return room;
760}
761
762
763static int ti_chars_in_buffer(struct usb_serial_port *port)
764{
765 struct ti_port *tport = usb_get_serial_port_data(port);
766 int chars = 0;
767 unsigned long flags;
768
769 dbg("%s - port %d", __FUNCTION__, port->number);
770
771 if (tport == NULL)
772 return -ENODEV;
773
774 spin_lock_irqsave(&tport->tp_lock, flags);
775 chars = ti_buf_data_avail(tport->tp_write_buf);
776 spin_unlock_irqrestore(&tport->tp_lock, flags);
777
778 dbg("%s - returns %d", __FUNCTION__, chars);
779 return chars;
780}
781
782
783static void ti_throttle(struct usb_serial_port *port)
784{
785 struct ti_port *tport = usb_get_serial_port_data(port);
786 struct tty_struct *tty;
787
788 dbg("%s - port %d", __FUNCTION__, port->number);
789
790 if (tport == NULL)
791 return;
792
793 tty = port->tty;
794 if (!tty) {
795 dbg("%s - no tty", __FUNCTION__);
796 return;
797 }
798
799 if (I_IXOFF(tty) || C_CRTSCTS(tty))
800 ti_stop_read(tport, tty);
801
802}
803
804
805static void ti_unthrottle(struct usb_serial_port *port)
806{
807 struct ti_port *tport = usb_get_serial_port_data(port);
808 struct tty_struct *tty;
809 int status;
810
811 dbg("%s - port %d", __FUNCTION__, port->number);
812
813 if (tport == NULL)
814 return;
815
816 tty = port->tty;
817 if (!tty) {
818 dbg("%s - no tty", __FUNCTION__);
819 return;
820 }
821
822 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
823 status = ti_restart_read(tport, tty);
824 if (status)
825 dev_err(&port->dev, "%s - cannot restart read, %d\n", __FUNCTION__, status);
826 }
827}
828
829
830static int ti_ioctl(struct usb_serial_port *port, struct file *file,
831 unsigned int cmd, unsigned long arg)
832{
833 struct ti_port *tport = usb_get_serial_port_data(port);
834 struct async_icount cnow;
835 struct async_icount cprev;
836
837 dbg("%s - port %d, cmd = 0x%04X", __FUNCTION__, port->number, cmd);
838
839 if (tport == NULL)
840 return -ENODEV;
841
842 switch (cmd) {
843 case TIOCGSERIAL:
844 dbg("%s - (%d) TIOCGSERIAL", __FUNCTION__, port->number);
845 return ti_get_serial_info(tport, (struct serial_struct __user *)arg);
846 break;
847
848 case TIOCSSERIAL:
849 dbg("%s - (%d) TIOCSSERIAL", __FUNCTION__, port->number);
850 return ti_set_serial_info(tport, (struct serial_struct __user *)arg);
851 break;
852
853 case TIOCMIWAIT:
854 dbg("%s - (%d) TIOCMIWAIT", __FUNCTION__, port->number);
855 cprev = tport->tp_icount;
856 while (1) {
857 interruptible_sleep_on(&tport->tp_msr_wait);
858 if (signal_pending(current))
859 return -ERESTARTSYS;
860 cnow = tport->tp_icount;
861 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
862 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
863 return -EIO; /* no change => error */
864 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
865 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
866 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
867 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
868 return 0;
869 }
870 cprev = cnow;
871 }
872 break;
873
874 case TIOCGICOUNT:
875 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__, port->number, tport->tp_icount.rx, tport->tp_icount.tx);
876 if (copy_to_user((void __user *)arg, &tport->tp_icount, sizeof(tport->tp_icount)))
877 return -EFAULT;
878 return 0;
879 }
880
881 return -ENOIOCTLCMD;
882}
883
884
885static void ti_set_termios(struct usb_serial_port *port,
Alan Cox606d0992006-12-08 02:38:45 -0800886 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct ti_port *tport = usb_get_serial_port_data(port);
889 struct tty_struct *tty = port->tty;
890 struct ti_uart_config *config;
891 tcflag_t cflag,iflag;
892 int baud;
893 int status;
894 int port_number = port->number - port->serial->minor;
895 unsigned int mcr;
896
897 dbg("%s - port %d", __FUNCTION__, port->number);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 cflag = tty->termios->c_cflag;
900 iflag = tty->termios->c_iflag;
901
Alan Cox537699e2008-01-03 17:03:11 +0000902 dbg("%s - cflag %08x, iflag %08x", __FUNCTION__, cflag, iflag);
903 dbg("%s - old clfag %08x, old iflag %08x", __FUNCTION__, old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (tport == NULL)
906 return;
907
908 config = kmalloc(sizeof(*config), GFP_KERNEL);
909 if (!config) {
910 dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
911 return;
912 }
913
914 config->wFlags = 0;
915
916 /* these flags must be set */
917 config->wFlags |= TI_UART_ENABLE_MS_INTS;
918 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
919 config->bUartMode = (__u8)(tport->tp_uart_mode);
920
921 switch (cflag & CSIZE) {
922 case CS5:
923 config->bDataBits = TI_UART_5_DATA_BITS;
924 break;
925 case CS6:
926 config->bDataBits = TI_UART_6_DATA_BITS;
927 break;
928 case CS7:
929 config->bDataBits = TI_UART_7_DATA_BITS;
930 break;
931 default:
932 case CS8:
933 config->bDataBits = TI_UART_8_DATA_BITS;
934 break;
935 }
936
Alan Cox537699e2008-01-03 17:03:11 +0000937 /* CMSPAR isn't supported by this driver */
938 tty->termios->c_cflag &= ~CMSPAR;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (cflag & PARENB) {
941 if (cflag & PARODD) {
942 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
943 config->bParity = TI_UART_ODD_PARITY;
944 } else {
945 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
946 config->bParity = TI_UART_EVEN_PARITY;
947 }
948 } else {
949 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
950 config->bParity = TI_UART_NO_PARITY;
951 }
952
953 if (cflag & CSTOPB)
954 config->bStopBits = TI_UART_2_STOP_BITS;
955 else
956 config->bStopBits = TI_UART_1_STOP_BITS;
957
958 if (cflag & CRTSCTS) {
959 /* RTS flow control must be off to drop RTS for baud rate B0 */
960 if ((cflag & CBAUD) != B0)
961 config->wFlags |= TI_UART_ENABLE_RTS_IN;
962 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
963 } else {
964 tty->hw_stopped = 0;
965 ti_restart_read(tport, tty);
966 }
967
968 if (I_IXOFF(tty) || I_IXON(tty)) {
969 config->cXon = START_CHAR(tty);
970 config->cXoff = STOP_CHAR(tty);
971
972 if (I_IXOFF(tty))
973 config->wFlags |= TI_UART_ENABLE_X_IN;
974 else
975 ti_restart_read(tport, tty);
976
977 if (I_IXON(tty))
978 config->wFlags |= TI_UART_ENABLE_X_OUT;
979 }
980
981 baud = tty_get_baud_rate(tty);
Alan Cox537699e2008-01-03 17:03:11 +0000982 if (!baud)
983 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (tport->tp_tdev->td_is_3410)
985 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
986 else
987 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
988
Alan Cox537699e2008-01-03 17:03:11 +0000989 /* FIXME: Should calculate resulting baud here and report it back */
990 if ((cflag & CBAUD) != B0)
991 tty_encode_baud_rate(tty, baud, baud);
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
994 __FUNCTION__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
995
996 cpu_to_be16s(&config->wBaudRate);
997 cpu_to_be16s(&config->wFlags);
998
999 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
1000 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
1001 sizeof(*config));
1002 if (status)
1003 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", __FUNCTION__, port_number, status);
1004
1005 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
1006 mcr = tport->tp_shadow_mcr;
1007 /* if baud rate is B0, clear RTS and DTR */
1008 if ((cflag & CBAUD) == B0)
1009 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
1010 status = ti_set_mcr(tport, mcr);
1011 if (status)
1012 dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n", __FUNCTION__, port_number, status);
1013
1014 kfree(config);
1015}
1016
1017
1018static int ti_tiocmget(struct usb_serial_port *port, struct file *file)
1019{
1020 struct ti_port *tport = usb_get_serial_port_data(port);
1021 unsigned int result;
1022 unsigned int msr;
1023 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001024 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 dbg("%s - port %d", __FUNCTION__, port->number);
1027
1028 if (tport == NULL)
1029 return -ENODEV;
1030
Alan Cox04ca89d2008-02-20 21:41:40 +00001031 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 msr = tport->tp_msr;
1033 mcr = tport->tp_shadow_mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001034 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1037 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1038 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1039 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1040 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1041 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1042 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1043
1044 dbg("%s - 0x%04X", __FUNCTION__, result);
1045
1046 return result;
1047}
1048
1049
1050static int ti_tiocmset(struct usb_serial_port *port, struct file *file,
1051 unsigned int set, unsigned int clear)
1052{
1053 struct ti_port *tport = usb_get_serial_port_data(port);
1054 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001055 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 dbg("%s - port %d", __FUNCTION__, port->number);
1058
1059 if (tport == NULL)
1060 return -ENODEV;
1061
Alan Cox04ca89d2008-02-20 21:41:40 +00001062 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 mcr = tport->tp_shadow_mcr;
1064
1065 if (set & TIOCM_RTS)
1066 mcr |= TI_MCR_RTS;
1067 if (set & TIOCM_DTR)
1068 mcr |= TI_MCR_DTR;
1069 if (set & TIOCM_LOOP)
1070 mcr |= TI_MCR_LOOP;
1071
1072 if (clear & TIOCM_RTS)
1073 mcr &= ~TI_MCR_RTS;
1074 if (clear & TIOCM_DTR)
1075 mcr &= ~TI_MCR_DTR;
1076 if (clear & TIOCM_LOOP)
1077 mcr &= ~TI_MCR_LOOP;
Alan Cox04ca89d2008-02-20 21:41:40 +00001078 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 return ti_set_mcr(tport, mcr);
1081}
1082
1083
1084static void ti_break(struct usb_serial_port *port, int break_state)
1085{
1086 struct ti_port *tport = usb_get_serial_port_data(port);
1087 int status;
1088
1089 dbg("%s - state = %d", __FUNCTION__, break_state);
1090
1091 if (tport == NULL)
1092 return;
1093
1094 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1095
1096 status = ti_write_byte(tport->tp_tdev,
1097 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1098 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1099
1100 if (status)
1101 dbg("%s - error setting break, %d", __FUNCTION__, status);
1102}
1103
1104
David Howells7d12e782006-10-05 14:55:46 +01001105static void ti_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 struct ti_device *tdev = (struct ti_device *)urb->context;
1108 struct usb_serial_port *port;
1109 struct usb_serial *serial = tdev->td_serial;
1110 struct ti_port *tport;
1111 struct device *dev = &urb->dev->dev;
1112 unsigned char *data = urb->transfer_buffer;
1113 int length = urb->actual_length;
1114 int port_number;
1115 int function;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001116 int status = urb->status;
1117 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 __u8 msr;
1119
1120 dbg("%s", __FUNCTION__);
1121
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001122 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 case 0:
1124 break;
1125 case -ECONNRESET:
1126 case -ENOENT:
1127 case -ESHUTDOWN:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001128 dbg("%s - urb shutting down, %d", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 tdev->td_urb_error = 1;
1130 return;
1131 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001132 dev_err(dev, "%s - nonzero urb status, %d\n",
1133 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 tdev->td_urb_error = 1;
1135 goto exit;
1136 }
1137
1138 if (length != 2) {
1139 dbg("%s - bad packet size, %d", __FUNCTION__, length);
1140 goto exit;
1141 }
1142
1143 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1144 dev_err(dev, "%s - hardware error, %d\n", __FUNCTION__, data[1]);
1145 goto exit;
1146 }
1147
1148 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1149 function = TI_GET_FUNC_FROM_CODE(data[0]);
1150
1151 dbg("%s - port_number %d, function %d, data 0x%02X", __FUNCTION__, port_number, function, data[1]);
1152
1153 if (port_number >= serial->num_ports) {
1154 dev_err(dev, "%s - bad port number, %d\n", __FUNCTION__, port_number);
1155 goto exit;
1156 }
1157
1158 port = serial->port[port_number];
1159
1160 tport = usb_get_serial_port_data(port);
1161 if (!tport)
1162 goto exit;
1163
1164 switch (function) {
1165 case TI_CODE_DATA_ERROR:
1166 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", __FUNCTION__, port_number, data[1]);
1167 break;
1168
1169 case TI_CODE_MODEM_STATUS:
1170 msr = data[1];
1171 dbg("%s - port %d, msr 0x%02X", __FUNCTION__, port_number, msr);
1172 ti_handle_new_msr(tport, msr);
1173 break;
1174
1175 default:
1176 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", __FUNCTION__, data[1]);
1177 break;
1178 }
1179
1180exit:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001181 retval = usb_submit_urb(urb, GFP_ATOMIC);
1182 if (retval)
1183 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1184 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187
David Howells7d12e782006-10-05 14:55:46 +01001188static void ti_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 struct ti_port *tport = (struct ti_port *)urb->context;
1191 struct usb_serial_port *port = tport->tp_port;
1192 struct device *dev = &urb->dev->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001193 int status = urb->status;
1194 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 dbg("%s", __FUNCTION__);
1197
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001198 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 case 0:
1200 break;
1201 case -ECONNRESET:
1202 case -ENOENT:
1203 case -ESHUTDOWN:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001204 dbg("%s - urb shutting down, %d", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 tport->tp_tdev->td_urb_error = 1;
1206 wake_up_interruptible(&tport->tp_write_wait);
1207 return;
1208 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001209 dev_err(dev, "%s - nonzero urb status, %d\n",
1210 __FUNCTION__, status );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 tport->tp_tdev->td_urb_error = 1;
1212 wake_up_interruptible(&tport->tp_write_wait);
1213 }
1214
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001215 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto exit;
1217
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001218 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 dev_err(dev, "%s - stopping read!\n", __FUNCTION__);
1220 return;
1221 }
1222
1223 if (port->tty && urb->actual_length) {
1224 usb_serial_debug_data(debug, dev, __FUNCTION__,
1225 urb->actual_length, urb->transfer_buffer);
1226
1227 if (!tport->tp_is_open)
1228 dbg("%s - port closed, dropping data", __FUNCTION__);
1229 else
1230 ti_recv(&urb->dev->dev, port->tty, urb->transfer_buffer,
1231 urb->actual_length);
1232
1233 spin_lock(&tport->tp_lock);
1234 tport->tp_icount.rx += urb->actual_length;
1235 spin_unlock(&tport->tp_lock);
1236 }
1237
1238exit:
1239 /* continue to read unless stopping */
1240 spin_lock(&tport->tp_lock);
1241 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1242 urb->dev = port->serial->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001243 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1245 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1246 }
1247 spin_unlock(&tport->tp_lock);
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001248 if (retval)
1249 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1250 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253
David Howells7d12e782006-10-05 14:55:46 +01001254static void ti_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 struct ti_port *tport = (struct ti_port *)urb->context;
1257 struct usb_serial_port *port = tport->tp_port;
1258 struct device *dev = &urb->dev->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001259 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 dbg("%s - port %d", __FUNCTION__, port->number);
1262
1263 tport->tp_write_urb_in_use = 0;
1264
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001265 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 case 0:
1267 break;
1268 case -ECONNRESET:
1269 case -ENOENT:
1270 case -ESHUTDOWN:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001271 dbg("%s - urb shutting down, %d", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 tport->tp_tdev->td_urb_error = 1;
1273 wake_up_interruptible(&tport->tp_write_wait);
1274 return;
1275 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001276 dev_err(dev, "%s - nonzero urb status, %d\n",
1277 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 tport->tp_tdev->td_urb_error = 1;
1279 wake_up_interruptible(&tport->tp_write_wait);
1280 }
1281
1282 /* send any buffered data */
1283 ti_send(tport);
1284}
1285
1286
1287static void ti_recv(struct device *dev, struct tty_struct *tty,
1288 unsigned char *data, int length)
1289{
1290 int cnt;
1291
1292 do {
Alan Cox33f0f882006-01-09 20:54:13 -08001293 cnt = tty_buffer_request_room(tty, length);
1294 if (cnt < length) {
1295 dev_err(dev, "%s - dropping data, %d bytes lost\n", __FUNCTION__, length - cnt);
1296 if(cnt == 0)
1297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Alan Cox33f0f882006-01-09 20:54:13 -08001299 tty_insert_flip_string(tty, data, cnt);
1300 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 data += cnt;
1302 length -= cnt;
1303 } while (length > 0);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307
1308static void ti_send(struct ti_port *tport)
1309{
1310 int count, result;
1311 struct usb_serial_port *port = tport->tp_port;
1312 struct tty_struct *tty = port->tty;
1313 unsigned long flags;
1314
1315
1316 dbg("%s - port %d", __FUNCTION__, port->number);
1317
1318 spin_lock_irqsave(&tport->tp_lock, flags);
1319
1320 if (tport->tp_write_urb_in_use) {
1321 spin_unlock_irqrestore(&tport->tp_lock, flags);
1322 return;
1323 }
1324
1325 count = ti_buf_get(tport->tp_write_buf,
1326 port->write_urb->transfer_buffer,
1327 port->bulk_out_size);
1328
1329 if (count == 0) {
1330 spin_unlock_irqrestore(&tport->tp_lock, flags);
1331 return;
1332 }
1333
1334 tport->tp_write_urb_in_use = 1;
1335
1336 spin_unlock_irqrestore(&tport->tp_lock, flags);
1337
1338 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, port->write_urb->transfer_buffer);
1339
1340 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1341 usb_sndbulkpipe(port->serial->dev,
1342 port->bulk_out_endpointAddress),
1343 port->write_urb->transfer_buffer, count,
1344 ti_bulk_out_callback, tport);
1345
1346 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1347 if (result) {
1348 dev_err(&port->dev, "%s - submit write urb failed, %d\n", __FUNCTION__, result);
1349 tport->tp_write_urb_in_use = 0;
1350 /* TODO: reschedule ti_send */
1351 } else {
1352 spin_lock_irqsave(&tport->tp_lock, flags);
1353 tport->tp_icount.tx += count;
1354 spin_unlock_irqrestore(&tport->tp_lock, flags);
1355 }
1356
1357 /* more room in the buffer for new writes, wakeup */
1358 if (tty)
1359 tty_wakeup(tty);
1360 wake_up_interruptible(&tport->tp_write_wait);
1361}
1362
1363
1364static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1365{
Alan Cox04ca89d2008-02-20 21:41:40 +00001366 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int status;
1368
1369 status = ti_write_byte(tport->tp_tdev,
1370 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1371 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1372
Alan Cox04ca89d2008-02-20 21:41:40 +00001373 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (!status)
1375 tport->tp_shadow_mcr = mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001376 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 return status;
1379}
1380
1381
1382static int ti_get_lsr(struct ti_port *tport)
1383{
1384 int size,status;
1385 struct ti_device *tdev = tport->tp_tdev;
1386 struct usb_serial_port *port = tport->tp_port;
1387 int port_number = port->number - port->serial->minor;
1388 struct ti_port_status *data;
1389
1390 dbg("%s - port %d", __FUNCTION__, port->number);
1391
1392 size = sizeof(struct ti_port_status);
1393 data = kmalloc(size, GFP_KERNEL);
1394 if (!data) {
1395 dev_err(&port->dev, "%s - out of memory\n", __FUNCTION__);
1396 return -ENOMEM;
1397 }
1398
1399 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1400 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1401 if (status) {
1402 dev_err(&port->dev, "%s - get port status command failed, %d\n", __FUNCTION__, status);
1403 goto free_data;
1404 }
1405
1406 dbg("%s - lsr 0x%02X", __FUNCTION__, data->bLSR);
1407
1408 tport->tp_lsr = data->bLSR;
1409
1410free_data:
1411 kfree(data);
1412 return status;
1413}
1414
1415
1416static int ti_get_serial_info(struct ti_port *tport,
1417 struct serial_struct __user *ret_arg)
1418{
1419 struct usb_serial_port *port = tport->tp_port;
1420 struct serial_struct ret_serial;
1421
1422 if (!ret_arg)
1423 return -EFAULT;
1424
1425 memset(&ret_serial, 0, sizeof(ret_serial));
1426
1427 ret_serial.type = PORT_16550A;
1428 ret_serial.line = port->serial->minor;
1429 ret_serial.port = port->number - port->serial->minor;
1430 ret_serial.flags = tport->tp_flags;
1431 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1432 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1433 ret_serial.closing_wait = tport->tp_closing_wait;
1434
1435 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1436 return -EFAULT;
1437
1438 return 0;
1439}
1440
1441
1442static int ti_set_serial_info(struct ti_port *tport,
1443 struct serial_struct __user *new_arg)
1444{
1445 struct usb_serial_port *port = tport->tp_port;
1446 struct serial_struct new_serial;
1447
1448 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1449 return -EFAULT;
1450
1451 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1452 if (port->tty)
1453 port->tty->low_latency =
1454 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1455 tport->tp_closing_wait = new_serial.closing_wait;
1456
1457 return 0;
1458}
1459
1460
1461static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1462{
1463 struct async_icount *icount;
1464 struct tty_struct *tty;
1465 unsigned long flags;
1466
1467 dbg("%s - msr 0x%02X", __FUNCTION__, msr);
1468
1469 if (msr & TI_MSR_DELTA_MASK) {
1470 spin_lock_irqsave(&tport->tp_lock, flags);
1471 icount = &tport->tp_icount;
1472 if (msr & TI_MSR_DELTA_CTS)
1473 icount->cts++;
1474 if (msr & TI_MSR_DELTA_DSR)
1475 icount->dsr++;
1476 if (msr & TI_MSR_DELTA_CD)
1477 icount->dcd++;
1478 if (msr & TI_MSR_DELTA_RI)
1479 icount->rng++;
1480 wake_up_interruptible(&tport->tp_msr_wait);
1481 spin_unlock_irqrestore(&tport->tp_lock, flags);
1482 }
1483
1484 tport->tp_msr = msr & TI_MSR_MASK;
1485
1486 /* handle CTS flow control */
1487 tty = tport->tp_port->tty;
1488 if (tty && C_CRTSCTS(tty)) {
1489 if (msr & TI_MSR_CTS) {
1490 tty->hw_stopped = 0;
1491 tty_wakeup(tty);
1492 } else {
1493 tty->hw_stopped = 1;
1494 }
1495 }
1496}
1497
1498
1499static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1500{
1501 struct ti_device *tdev = tport->tp_tdev;
1502 struct usb_serial_port *port = tport->tp_port;
1503 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 dbg("%s - port %d", __FUNCTION__, port->number);
1506
Oliver Neukum0915f492008-01-23 12:28:45 +01001507 spin_lock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 /* wait for data to drain from the buffer */
1510 tdev->td_urb_error = 0;
1511 init_waitqueue_entry(&wait, current);
1512 add_wait_queue(&tport->tp_write_wait, &wait);
1513 for (;;) {
1514 set_current_state(TASK_INTERRUPTIBLE);
1515 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1516 || timeout == 0 || signal_pending(current)
1517 || tdev->td_urb_error
Oliver Neukum0915f492008-01-23 12:28:45 +01001518 || port->serial->disconnected) /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 break;
Oliver Neukum0915f492008-01-23 12:28:45 +01001520 spin_unlock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 timeout = schedule_timeout(timeout);
Oliver Neukum0915f492008-01-23 12:28:45 +01001522 spin_lock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 set_current_state(TASK_RUNNING);
1525 remove_wait_queue(&tport->tp_write_wait, &wait);
1526
1527 /* flush any remaining data in the buffer */
1528 if (flush)
1529 ti_buf_clear(tport->tp_write_buf);
1530
Oliver Neukum0915f492008-01-23 12:28:45 +01001531 spin_unlock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Oliver Neukum0915f492008-01-23 12:28:45 +01001533 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /* wait for data to drain from the device */
1535 /* wait for empty tx register, plus 20 ms */
1536 timeout += jiffies;
1537 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1538 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1539 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
Oliver Neukum0915f492008-01-23 12:28:45 +01001540 && !port->serial->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (ti_get_lsr(tport))
1542 break;
Oliver Neukum0915f492008-01-23 12:28:45 +01001543 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 msleep_interruptible(20);
Oliver Neukum0915f492008-01-23 12:28:45 +01001545 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Oliver Neukum0915f492008-01-23 12:28:45 +01001547 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550
1551static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1552{
1553 unsigned long flags;
1554
1555 spin_lock_irqsave(&tport->tp_lock, flags);
1556
1557 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1558 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1559
1560 spin_unlock_irqrestore(&tport->tp_lock, flags);
1561}
1562
1563
1564static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1565{
1566 struct urb *urb;
1567 int status = 0;
1568 unsigned long flags;
1569
1570 spin_lock_irqsave(&tport->tp_lock, flags);
1571
1572 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
Oliver Neukum944dc182007-05-07 08:33:18 +02001573 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 urb = tport->tp_port->read_urb;
Oliver Neukum944dc182007-05-07 08:33:18 +02001575 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 urb->complete = ti_bulk_in_callback;
1577 urb->context = tport;
1578 urb->dev = tport->tp_port->serial->dev;
1579 status = usb_submit_urb(urb, GFP_KERNEL);
Oliver Neukum944dc182007-05-07 08:33:18 +02001580 } else {
1581 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1582 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 return status;
1586}
1587
1588
1589static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1590 __u16 moduleid, __u16 value, __u8 *data, int size)
1591{
1592 int status;
1593
1594 status = usb_control_msg(tdev->td_serial->dev,
1595 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1596 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1597 value, moduleid, data, size, 1000);
1598
1599 if (status == size)
1600 status = 0;
1601
1602 if (status > 0)
1603 status = -ECOMM;
1604
1605 return status;
1606}
1607
1608
1609static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1610 __u16 moduleid, __u16 value, __u8 *data, int size)
1611{
1612 int status;
1613
1614 status = usb_control_msg(tdev->td_serial->dev,
1615 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1616 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1617 value, moduleid, data, size, 1000);
1618
1619 if (status == size)
1620 status = 0;
1621
1622 if (status > 0)
1623 status = -ECOMM;
1624
1625 return status;
1626}
1627
1628
1629static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1630 __u8 mask, __u8 byte)
1631{
1632 int status;
1633 unsigned int size;
1634 struct ti_write_data_bytes *data;
1635 struct device *dev = &tdev->td_serial->dev->dev;
1636
1637 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X", __FUNCTION__, addr, mask, byte);
1638
1639 size = sizeof(struct ti_write_data_bytes) + 2;
1640 data = kmalloc(size, GFP_KERNEL);
1641 if (!data) {
1642 dev_err(dev, "%s - out of memory\n", __FUNCTION__);
1643 return -ENOMEM;
1644 }
1645
1646 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1647 data->bDataType = TI_RW_DATA_BYTE;
1648 data->bDataCounter = 1;
1649 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1650 data->wBaseAddrLo = cpu_to_be16(addr);
1651 data->bData[0] = mask;
1652 data->bData[1] = byte;
1653
1654 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1655 (__u8 *)data, size);
1656
1657 if (status < 0)
1658 dev_err(dev, "%s - failed, %d\n", __FUNCTION__, status);
1659
1660 kfree(data);
1661
1662 return status;
1663}
1664
1665
1666static int ti_download_firmware(struct ti_device *tdev,
1667 unsigned char *firmware, unsigned int firmware_size)
1668{
1669 int status = 0;
1670 int buffer_size;
1671 int pos;
1672 int len;
1673 int done;
1674 __u8 cs = 0;
1675 __u8 *buffer;
1676 struct usb_device *dev = tdev->td_serial->dev;
1677 struct ti_firmware_header *header;
1678 unsigned int pipe = usb_sndbulkpipe(dev,
1679 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1680
1681
1682 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1683 buffer = kmalloc(buffer_size, GFP_KERNEL);
1684 if (!buffer) {
1685 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
1686 return -ENOMEM;
1687 }
1688
1689 memcpy(buffer, firmware, firmware_size);
1690 memset(buffer+firmware_size, 0xff, buffer_size-firmware_size);
1691
1692 for(pos = sizeof(struct ti_firmware_header); pos < buffer_size; pos++)
1693 cs = (__u8)(cs + buffer[pos]);
1694
1695 header = (struct ti_firmware_header *)buffer;
1696 header->wLength = cpu_to_le16((__u16)(buffer_size - sizeof(struct ti_firmware_header)));
1697 header->bCheckSum = cs;
1698
1699 dbg("%s - downloading firmware", __FUNCTION__);
1700 for (pos = 0; pos < buffer_size; pos += done) {
1701 len = min(buffer_size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1702 status = usb_bulk_msg(dev, pipe, buffer+pos, len, &done, 1000);
1703 if (status)
1704 break;
1705 }
1706
1707 kfree(buffer);
1708
1709 if (status) {
1710 dev_err(&dev->dev, "%s - error downloading firmware, %d\n", __FUNCTION__, status);
1711 return status;
1712 }
1713
1714 dbg("%s - download successful", __FUNCTION__);
1715
1716 return 0;
1717}
1718
1719
1720/* Circular Buffer Functions */
1721
1722/*
1723 * ti_buf_alloc
1724 *
1725 * Allocate a circular buffer and all associated memory.
1726 */
1727
1728static struct circ_buf *ti_buf_alloc(void)
1729{
1730 struct circ_buf *cb;
1731
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001732 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (cb == NULL)
1734 return NULL;
1735
1736 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1737 if (cb->buf == NULL) {
1738 kfree(cb);
1739 return NULL;
1740 }
1741
1742 ti_buf_clear(cb);
1743
1744 return cb;
1745}
1746
1747
1748/*
1749 * ti_buf_free
1750 *
1751 * Free the buffer and all associated memory.
1752 */
1753
1754static void ti_buf_free(struct circ_buf *cb)
1755{
1756 kfree(cb->buf);
1757 kfree(cb);
1758}
1759
1760
1761/*
1762 * ti_buf_clear
1763 *
1764 * Clear out all data in the circular buffer.
1765 */
1766
1767static void ti_buf_clear(struct circ_buf *cb)
1768{
1769 cb->head = cb->tail = 0;
1770}
1771
1772
1773/*
1774 * ti_buf_data_avail
1775 *
1776 * Return the number of bytes of data available in the circular
1777 * buffer.
1778 */
1779
1780static int ti_buf_data_avail(struct circ_buf *cb)
1781{
1782 return CIRC_CNT(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1783}
1784
1785
1786/*
1787 * ti_buf_space_avail
1788 *
1789 * Return the number of bytes of space available in the circular
1790 * buffer.
1791 */
1792
1793static int ti_buf_space_avail(struct circ_buf *cb)
1794{
1795 return CIRC_SPACE(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
1796}
1797
1798
1799/*
1800 * ti_buf_put
1801 *
1802 * Copy data data from a user buffer and put it into the circular buffer.
1803 * Restrict to the amount of space available.
1804 *
1805 * Return the number of bytes copied.
1806 */
1807
1808static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1809{
1810 int c, ret = 0;
1811
1812 while (1) {
1813 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1814 if (count < c)
1815 c = count;
1816 if (c <= 0)
1817 break;
1818 memcpy(cb->buf + cb->head, buf, c);
1819 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1820 buf += c;
1821 count -= c;
1822 ret += c;
1823 }
1824
1825 return ret;
1826}
1827
1828
1829/*
1830 * ti_buf_get
1831 *
1832 * Get data from the circular buffer and copy to the given buffer.
1833 * Restrict to the amount of data available.
1834 *
1835 * Return the number of bytes copied.
1836 */
1837
1838static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1839{
1840 int c, ret = 0;
1841
1842 while (1) {
1843 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1844 if (count < c)
1845 c = count;
1846 if (c <= 0)
1847 break;
1848 memcpy(buf, cb->buf + cb->tail, c);
1849 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);
1850 buf += c;
1851 count -= c;
1852 ret += c;
1853 }
1854
1855 return ret;
1856}