blob: 1cdb1515253d07f78a68c418071b3fe8055aea65 [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>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
22#include <linux/errno.h>
Alan Cox95da3102008-07-22 11:09:07 +010023#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/tty.h>
27#include <linux/tty_driver.h>
28#include <linux/tty_flip.h>
29#include <linux/module.h>
30#include <linux/spinlock.h>
31#include <linux/ioctl.h>
32#include <linux/serial.h>
33#include <linux/circ_buf.h>
Matthias Kaehlcke9da00682007-11-21 15:13:16 -080034#include <linux/mutex.h>
Alan Coxa30fa792008-07-22 11:15:17 +010035#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070037#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "ti_usb_3410_5052.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Defines */
42
43#define TI_DRIVER_VERSION "v0.9"
44#define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
45#define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
46
47#define TI_FIRMWARE_BUF_SIZE 16284
48
49#define TI_WRITE_BUF_SIZE 1024
50
51#define TI_TRANSFER_TIMEOUT 2
52
53#define TI_DEFAULT_LOW_LATENCY 0
54#define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
55
56/* supported setserial flags */
57#define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
58
59/* read urb states */
60#define TI_READ_URB_RUNNING 0
61#define TI_READ_URB_STOPPING 1
62#define TI_READ_URB_STOPPED 2
63
64#define TI_EXTRA_VID_PID_COUNT 5
65
66
67/* Structures */
68
69struct ti_port {
70 int tp_is_open;
71 __u8 tp_msr;
72 __u8 tp_lsr;
73 __u8 tp_shadow_mcr;
74 __u8 tp_uart_mode; /* 232 or 485 modes */
75 unsigned int tp_uart_base_addr;
76 int tp_flags;
77 int tp_closing_wait;/* in .01 secs */
78 struct async_icount tp_icount;
79 wait_queue_head_t tp_msr_wait; /* wait for msr change */
80 wait_queue_head_t tp_write_wait;
81 struct ti_device *tp_tdev;
82 struct usb_serial_port *tp_port;
83 spinlock_t tp_lock;
84 int tp_read_urb_state;
85 int tp_write_urb_in_use;
86 struct circ_buf *tp_write_buf;
87};
88
89struct ti_device {
Matthias Kaehlcke9da00682007-11-21 15:13:16 -080090 struct mutex td_open_close_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 int td_open_port_count;
92 struct usb_serial *td_serial;
93 int td_is_3410;
94 int td_urb_error;
95};
96
97
98/* Function Declarations */
99
100static int ti_startup(struct usb_serial *serial);
101static void ti_shutdown(struct usb_serial *serial);
Alan Cox95da3102008-07-22 11:09:07 +0100102static int ti_open(struct tty_struct *tty, struct usb_serial_port *port,
Alan Coxa30fa792008-07-22 11:15:17 +0100103 struct file *file);
Alan Cox95da3102008-07-22 11:09:07 +0100104static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
Alan Coxa30fa792008-07-22 11:15:17 +0100105 struct file *file);
Alan Cox95da3102008-07-22 11:09:07 +0100106static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Coxa30fa792008-07-22 11:15:17 +0100107 const unsigned char *data, int count);
Alan Cox95da3102008-07-22 11:09:07 +0100108static int ti_write_room(struct tty_struct *tty);
109static int ti_chars_in_buffer(struct tty_struct *tty);
110static void ti_throttle(struct tty_struct *tty);
111static void ti_unthrottle(struct tty_struct *tty);
Alan Coxa30fa792008-07-22 11:15:17 +0100112static int ti_ioctl(struct tty_struct *tty, struct file *file,
113 unsigned int cmd, unsigned long arg);
114static void ti_set_termios(struct tty_struct *tty,
115 struct usb_serial_port *port, struct ktermios *old_termios);
Alan Cox95da3102008-07-22 11:09:07 +0100116static int ti_tiocmget(struct tty_struct *tty, struct file *file);
117static int ti_tiocmset(struct tty_struct *tty, struct file *file,
Alan Coxa30fa792008-07-22 11:15:17 +0100118 unsigned int set, unsigned int clear);
Alan Cox95da3102008-07-22 11:09:07 +0100119static void ti_break(struct tty_struct *tty, int break_state);
David Howells7d12e782006-10-05 14:55:46 +0100120static void ti_interrupt_callback(struct urb *urb);
121static void ti_bulk_in_callback(struct urb *urb);
122static void ti_bulk_out_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124static void ti_recv(struct device *dev, struct tty_struct *tty,
125 unsigned char *data, int length);
126static void ti_send(struct ti_port *tport);
127static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
128static int ti_get_lsr(struct ti_port *tport);
129static int ti_get_serial_info(struct ti_port *tport,
130 struct serial_struct __user *ret_arg);
Alan Cox4a90f092008-10-13 10:39:46 +0100131static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct serial_struct __user *new_arg);
133static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
134
135static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
136
137static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
138static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
139
140static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
141 __u16 moduleid, __u16 value, __u8 *data, int size);
142static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
143 __u16 moduleid, __u16 value, __u8 *data, int size);
144
145static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
146 __u8 mask, __u8 byte);
147
Chris Adams05a3d902009-01-11 19:48:53 +0000148static int ti_download_firmware(struct ti_device *tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/* circular buffer */
151static struct circ_buf *ti_buf_alloc(void);
152static void ti_buf_free(struct circ_buf *cb);
153static void ti_buf_clear(struct circ_buf *cb);
154static int ti_buf_data_avail(struct circ_buf *cb);
155static int ti_buf_space_avail(struct circ_buf *cb);
156static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
157static int ti_buf_get(struct circ_buf *cb, char *buf, int count);
158
159
160/* Data */
161
162/* module parameters */
163static int debug;
164static int low_latency = TI_DEFAULT_LOW_LATENCY;
165static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
166static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100167static unsigned int vendor_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100169static unsigned int product_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100171static unsigned int vendor_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100173static unsigned int product_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/* supported devices */
176/* the array dimension is the number of default entries plus */
177/* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
178/* null entry */
Chris Adams05a3d902009-01-11 19:48:53 +0000179static struct usb_device_id ti_id_table_3410[2+TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000181 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
184static struct usb_device_id ti_id_table_5052[4+TI_EXTRA_VID_PID_COUNT+1] = {
185 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
186 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
187 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
188 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
189};
190
Chris Adams05a3d902009-01-11 19:48:53 +0000191static struct usb_device_id ti_id_table_combined[6+2*TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000193 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
195 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
196 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
197 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
198 { }
199};
200
201static struct usb_driver ti_usb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .name = "ti_usb_3410_5052",
203 .probe = usb_serial_probe,
204 .disconnect = usb_serial_disconnect,
205 .id_table = ti_id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800206 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700209static struct usb_serial_driver ti_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700210 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700211 .owner = THIS_MODULE,
212 .name = "ti_usb_3410_5052_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700213 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700214 .description = "TI USB 3410 1 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100215 .usb_driver = &ti_usb_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .id_table = ti_id_table_3410,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .num_ports = 1,
218 .attach = ti_startup,
219 .shutdown = ti_shutdown,
220 .open = ti_open,
221 .close = ti_close,
222 .write = ti_write,
223 .write_room = ti_write_room,
224 .chars_in_buffer = ti_chars_in_buffer,
225 .throttle = ti_throttle,
226 .unthrottle = ti_unthrottle,
227 .ioctl = ti_ioctl,
228 .set_termios = ti_set_termios,
229 .tiocmget = ti_tiocmget,
230 .tiocmset = ti_tiocmset,
231 .break_ctl = ti_break,
232 .read_int_callback = ti_interrupt_callback,
233 .read_bulk_callback = ti_bulk_in_callback,
234 .write_bulk_callback = ti_bulk_out_callback,
235};
236
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700237static struct usb_serial_driver ti_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700238 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700239 .owner = THIS_MODULE,
240 .name = "ti_usb_3410_5052_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700241 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700242 .description = "TI USB 5052 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100243 .usb_driver = &ti_usb_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 .id_table = ti_id_table_5052,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .num_ports = 2,
246 .attach = ti_startup,
247 .shutdown = ti_shutdown,
248 .open = ti_open,
249 .close = ti_close,
250 .write = ti_write,
251 .write_room = ti_write_room,
252 .chars_in_buffer = ti_chars_in_buffer,
253 .throttle = ti_throttle,
254 .unthrottle = ti_unthrottle,
255 .ioctl = ti_ioctl,
256 .set_termios = ti_set_termios,
257 .tiocmget = ti_tiocmget,
258 .tiocmset = ti_tiocmset,
259 .break_ctl = ti_break,
260 .read_int_callback = ti_interrupt_callback,
261 .read_bulk_callback = ti_bulk_in_callback,
262 .write_bulk_callback = ti_bulk_out_callback,
263};
264
265
266/* Module */
267
268MODULE_AUTHOR(TI_DRIVER_AUTHOR);
269MODULE_DESCRIPTION(TI_DRIVER_DESC);
270MODULE_VERSION(TI_DRIVER_VERSION);
271MODULE_LICENSE("GPL");
272
David Woodhouse5f24e2d2008-05-30 18:49:51 +0300273MODULE_FIRMWARE("ti_3410.fw");
274MODULE_FIRMWARE("ti_5052.fw");
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276module_param(debug, bool, S_IRUGO | S_IWUSR);
277MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
278
279module_param(low_latency, bool, S_IRUGO | S_IWUSR);
Alan Coxa30fa792008-07-22 11:15:17 +0100280MODULE_PARM_DESC(low_latency,
281 "TTY low_latency flag, 0=off, 1=on, default is off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283module_param(closing_wait, int, S_IRUGO | S_IWUSR);
Alan Coxa30fa792008-07-22 11:15:17 +0100284MODULE_PARM_DESC(closing_wait,
285 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100288MODULE_PARM_DESC(vendor_3410,
289 "Vendor ids for 3410 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100291MODULE_PARM_DESC(product_3410,
292 "Product ids for 3410 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100294MODULE_PARM_DESC(vendor_5052,
295 "Vendor ids for 5052 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100297MODULE_PARM_DESC(product_5052,
298 "Product ids for 5052 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
301
302
303/* Functions */
304
305static int __init ti_init(void)
306{
Chris Adams05a3d902009-01-11 19:48:53 +0000307 int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int ret;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /* insert extra vendor and product ids */
Chris Adams05a3d902009-01-11 19:48:53 +0000311 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
Tobias Klauser52950ed2005-12-11 16:20:08 +0100312 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
Chris Adams05a3d902009-01-11 19:48:53 +0000313 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 ti_id_table_3410[j].idVendor = vendor_3410[i];
315 ti_id_table_3410[j].idProduct = product_3410[i];
316 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Chris Adams05a3d902009-01-11 19:48:53 +0000317 ti_id_table_combined[c].idVendor = vendor_3410[i];
318 ti_id_table_combined[c].idProduct = product_3410[i];
319 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Tobias Klauser52950ed2005-12-11 16:20:08 +0100321 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
Chris Adams05a3d902009-01-11 19:48:53 +0000322 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ti_id_table_5052[j].idVendor = vendor_5052[i];
324 ti_id_table_5052[j].idProduct = product_5052[i];
325 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Chris Adams05a3d902009-01-11 19:48:53 +0000326 ti_id_table_combined[c].idVendor = vendor_5052[i];
327 ti_id_table_combined[c].idProduct = product_5052[i];
328 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
331 ret = usb_serial_register(&ti_1port_device);
332 if (ret)
333 goto failed_1port;
334 ret = usb_serial_register(&ti_2port_device);
335 if (ret)
336 goto failed_2port;
337
338 ret = usb_register(&ti_usb_driver);
339 if (ret)
340 goto failed_usb;
341
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700342 printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":"
343 TI_DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 return 0;
346
347failed_usb:
348 usb_serial_deregister(&ti_2port_device);
349failed_2port:
350 usb_serial_deregister(&ti_1port_device);
351failed_1port:
352 return ret;
353}
354
355
356static void __exit ti_exit(void)
357{
358 usb_serial_deregister(&ti_1port_device);
359 usb_serial_deregister(&ti_2port_device);
360 usb_deregister(&ti_usb_driver);
361}
362
363
364module_init(ti_init);
365module_exit(ti_exit);
366
367
368static int ti_startup(struct usb_serial *serial)
369{
370 struct ti_device *tdev;
371 struct ti_port *tport;
372 struct usb_device *dev = serial->dev;
373 int status;
374 int i;
375
376
377 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800378 __func__, le16_to_cpu(dev->descriptor.idProduct),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 dev->descriptor.bNumConfigurations,
380 dev->actconfig->desc.bConfigurationValue);
381
382 /* create device structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100383 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (tdev == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800385 dev_err(&dev->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -ENOMEM;
387 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800388 mutex_init(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 tdev->td_serial = serial;
390 usb_set_serial_data(serial, tdev);
391
392 /* determine device type */
393 if (usb_match_id(serial->interface, ti_id_table_3410))
394 tdev->td_is_3410 = 1;
Alan Coxa30fa792008-07-22 11:15:17 +0100395 dbg("%s - device type is %s", __func__,
396 tdev->td_is_3410 ? "3410" : "5052");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /* if we have only 1 configuration, download firmware */
399 if (dev->descriptor.bNumConfigurations == 1) {
Chris Adams05a3d902009-01-11 19:48:53 +0000400 if ((status = ti_download_firmware(tdev)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 goto free_tdev;
402
403 /* 3410 must be reset, 5052 resets itself */
404 if (tdev->td_is_3410) {
405 msleep_interruptible(100);
406 usb_reset_device(dev);
407 }
408
409 status = -ENODEV;
410 goto free_tdev;
Alan Coxa30fa792008-07-22 11:15:17 +0100411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Oliver Neukum413ba6f2008-12-16 12:25:55 +0100413 /* the second configuration must be set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
Oliver Neukum413ba6f2008-12-16 12:25:55 +0100415 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
416 status = status ? status : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 goto free_tdev;
418 }
419
420 /* set up port structures */
421 for (i = 0; i < serial->num_ports; ++i) {
Burman Yan7ac9da12006-11-22 20:54:38 +0200422 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (tport == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800424 dev_err(&dev->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 status = -ENOMEM;
426 goto free_tports;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 spin_lock_init(&tport->tp_lock);
Alan Coxa30fa792008-07-22 11:15:17 +0100429 tport->tp_uart_base_addr = (i == 0 ?
430 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
432 tport->tp_closing_wait = closing_wait;
433 init_waitqueue_head(&tport->tp_msr_wait);
434 init_waitqueue_head(&tport->tp_write_wait);
435 tport->tp_write_buf = ti_buf_alloc();
436 if (tport->tp_write_buf == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800437 dev_err(&dev->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 kfree(tport);
439 status = -ENOMEM;
440 goto free_tports;
441 }
442 tport->tp_port = serial->port[i];
443 tport->tp_tdev = tdev;
444 usb_set_serial_port_data(serial->port[i], tport);
445 tport->tp_uart_mode = 0; /* default is RS232 */
446 }
Alan Coxa30fa792008-07-22 11:15:17 +0100447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0;
449
450free_tports:
Alan Coxa30fa792008-07-22 11:15:17 +0100451 for (--i; i >= 0; --i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 tport = usb_get_serial_port_data(serial->port[i]);
453 ti_buf_free(tport->tp_write_buf);
454 kfree(tport);
455 usb_set_serial_port_data(serial->port[i], NULL);
456 }
457free_tdev:
458 kfree(tdev);
459 usb_set_serial_data(serial, NULL);
460 return status;
461}
462
463
464static void ti_shutdown(struct usb_serial *serial)
465{
466 int i;
467 struct ti_device *tdev = usb_get_serial_data(serial);
468 struct ti_port *tport;
469
Harvey Harrison441b62c2008-03-03 16:08:34 -0800470 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Alan Coxa30fa792008-07-22 11:15:17 +0100472 for (i = 0; i < serial->num_ports; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 tport = usb_get_serial_port_data(serial->port[i]);
474 if (tport) {
475 ti_buf_free(tport->tp_write_buf);
476 kfree(tport);
477 usb_set_serial_port_data(serial->port[i], NULL);
478 }
479 }
480
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700481 kfree(tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 usb_set_serial_data(serial, NULL);
483}
484
485
Alan Cox95da3102008-07-22 11:09:07 +0100486static int ti_open(struct tty_struct *tty,
487 struct usb_serial_port *port, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct ti_port *tport = usb_get_serial_port_data(port);
490 struct ti_device *tdev;
491 struct usb_device *dev;
492 struct urb *urb;
493 int port_number;
494 int status;
Alan Coxa30fa792008-07-22 11:15:17 +0100495 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
496 TI_PIPE_TIMEOUT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 (TI_TRANSFER_TIMEOUT << 2));
498
Harvey Harrison441b62c2008-03-03 16:08:34 -0800499 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (tport == NULL)
502 return -ENODEV;
503
504 dev = port->serial->dev;
505 tdev = tport->tp_tdev;
506
507 /* only one open on any port on a device at a time */
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800508 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return -ERESTARTSYS;
510
Alan Cox95da3102008-07-22 11:09:07 +0100511 if (tty)
512 tty->low_latency =
513 (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 port_number = port->number - port->serial->minor;
516
517 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
518
519 tport->tp_msr = 0;
520 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
521
522 /* start interrupt urb the first time a port is opened on this device */
523 if (tdev->td_open_port_count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800524 dbg("%s - start interrupt in urb", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 urb = tdev->td_serial->port[0]->interrupt_in_urb;
526 if (!urb) {
Alan Coxa30fa792008-07-22 11:15:17 +0100527 dev_err(&port->dev, "%s - no interrupt urb\n",
528 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 status = -EINVAL;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800530 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 urb->complete = ti_interrupt_callback;
533 urb->context = tdev;
534 urb->dev = dev;
535 status = usb_submit_urb(urb, GFP_KERNEL);
536 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100537 dev_err(&port->dev,
538 "%s - submit interrupt urb failed, %d\n",
539 __func__, status);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800540 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 }
543
Alan Cox95da3102008-07-22 11:09:07 +0100544 if (tty)
545 ti_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Harvey Harrison441b62c2008-03-03 16:08:34 -0800547 dbg("%s - sending TI_OPEN_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
549 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
550 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100551 dev_err(&port->dev, "%s - cannot send open command, %d\n",
552 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto unlink_int_urb;
554 }
555
Harvey Harrison441b62c2008-03-03 16:08:34 -0800556 dbg("%s - sending TI_START_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 status = ti_command_out_sync(tdev, TI_START_PORT,
558 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
559 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100560 dev_err(&port->dev, "%s - cannot send start command, %d\n",
561 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto unlink_int_urb;
563 }
564
Harvey Harrison441b62c2008-03-03 16:08:34 -0800565 dbg("%s - sending TI_PURGE_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
567 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
568 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100569 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
570 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 goto unlink_int_urb;
572 }
573 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
574 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
575 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100576 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
577 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto unlink_int_urb;
579 }
580
581 /* reset the data toggle on the bulk endpoints to work around bug in
582 * host controllers where things get out of sync some times */
583 usb_clear_halt(dev, port->write_urb->pipe);
584 usb_clear_halt(dev, port->read_urb->pipe);
585
Alan Cox95da3102008-07-22 11:09:07 +0100586 if (tty)
587 ti_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Harvey Harrison441b62c2008-03-03 16:08:34 -0800589 dbg("%s - sending TI_OPEN_PORT (2)", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
591 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
592 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100593 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
594 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto unlink_int_urb;
596 }
597
Harvey Harrison441b62c2008-03-03 16:08:34 -0800598 dbg("%s - sending TI_START_PORT (2)", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 status = ti_command_out_sync(tdev, TI_START_PORT,
600 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
601 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100602 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
603 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto unlink_int_urb;
605 }
606
607 /* start read urb */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800608 dbg("%s - start read urb", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 urb = port->read_urb;
610 if (!urb) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800611 dev_err(&port->dev, "%s - no read urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 status = -EINVAL;
613 goto unlink_int_urb;
614 }
615 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
616 urb->complete = ti_bulk_in_callback;
617 urb->context = tport;
618 urb->dev = dev;
619 status = usb_submit_urb(urb, GFP_KERNEL);
620 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100621 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
622 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto unlink_int_urb;
624 }
625
626 tport->tp_is_open = 1;
627 ++tdev->td_open_port_count;
628
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800629 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631unlink_int_urb:
632 if (tdev->td_open_port_count == 0)
633 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800634release_lock:
635 mutex_unlock(&tdev->td_open_close_lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800636 dbg("%s - exit %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return status;
638}
639
640
Alan Cox95da3102008-07-22 11:09:07 +0100641static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
642 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct ti_device *tdev;
645 struct ti_port *tport;
646 int port_number;
647 int status;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800648 int do_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Harvey Harrison441b62c2008-03-03 16:08:34 -0800650 dbg("%s - port %d", __func__, port->number);
Alan Coxa30fa792008-07-22 11:15:17 +0100651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 tdev = usb_get_serial_data(port->serial);
653 tport = usb_get_serial_port_data(port);
654 if (tdev == NULL || tport == NULL)
655 return;
656
657 tport->tp_is_open = 0;
658
659 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
660
661 usb_kill_urb(port->read_urb);
662 usb_kill_urb(port->write_urb);
663 tport->tp_write_urb_in_use = 0;
664
665 port_number = port->number - port->serial->minor;
666
Harvey Harrison441b62c2008-03-03 16:08:34 -0800667 dbg("%s - sending TI_CLOSE_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
669 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
670 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100671 dev_err(&port->dev,
672 "%s - cannot send close port command, %d\n"
673 , __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800675 /* if mutex_lock is interrupted, continue anyway */
676 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 --tport->tp_tdev->td_open_port_count;
678 if (tport->tp_tdev->td_open_port_count <= 0) {
679 /* last port is closed, shut down interrupt urb */
680 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
681 tport->tp_tdev->td_open_port_count = 0;
682 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800683 if (do_unlock)
684 mutex_unlock(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Harvey Harrison441b62c2008-03-03 16:08:34 -0800686 dbg("%s - exit", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689
Alan Cox95da3102008-07-22 11:09:07 +0100690static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
691 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct ti_port *tport = usb_get_serial_port_data(port);
694 unsigned long flags;
695
Harvey Harrison441b62c2008-03-03 16:08:34 -0800696 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800699 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701 }
702
703 if (tport == NULL || !tport->tp_is_open)
704 return -ENODEV;
705
706 spin_lock_irqsave(&tport->tp_lock, flags);
707 count = ti_buf_put(tport->tp_write_buf, data, count);
708 spin_unlock_irqrestore(&tport->tp_lock, flags);
709
710 ti_send(tport);
711
712 return count;
713}
714
715
Alan Cox95da3102008-07-22 11:09:07 +0100716static int ti_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Alan Cox95da3102008-07-22 11:09:07 +0100718 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct ti_port *tport = usb_get_serial_port_data(port);
720 int room = 0;
721 unsigned long flags;
722
Harvey Harrison441b62c2008-03-03 16:08:34 -0800723 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (tport == NULL)
726 return -ENODEV;
Alan Coxa30fa792008-07-22 11:15:17 +0100727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 spin_lock_irqsave(&tport->tp_lock, flags);
729 room = ti_buf_space_avail(tport->tp_write_buf);
730 spin_unlock_irqrestore(&tport->tp_lock, flags);
731
Harvey Harrison441b62c2008-03-03 16:08:34 -0800732 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return room;
734}
735
736
Alan Cox95da3102008-07-22 11:09:07 +0100737static int ti_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Alan Cox95da3102008-07-22 11:09:07 +0100739 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct ti_port *tport = usb_get_serial_port_data(port);
741 int chars = 0;
742 unsigned long flags;
743
Harvey Harrison441b62c2008-03-03 16:08:34 -0800744 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (tport == NULL)
747 return -ENODEV;
748
749 spin_lock_irqsave(&tport->tp_lock, flags);
750 chars = ti_buf_data_avail(tport->tp_write_buf);
751 spin_unlock_irqrestore(&tport->tp_lock, flags);
752
Harvey Harrison441b62c2008-03-03 16:08:34 -0800753 dbg("%s - returns %d", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return chars;
755}
756
757
Alan Cox95da3102008-07-22 11:09:07 +0100758static void ti_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Alan Cox95da3102008-07-22 11:09:07 +0100760 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Harvey Harrison441b62c2008-03-03 16:08:34 -0800763 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (tport == NULL)
766 return;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (I_IXOFF(tty) || C_CRTSCTS(tty))
769 ti_stop_read(tport, tty);
770
771}
772
773
Alan Cox95da3102008-07-22 11:09:07 +0100774static void ti_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Alan Cox95da3102008-07-22 11:09:07 +0100776 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int status;
779
Harvey Harrison441b62c2008-03-03 16:08:34 -0800780 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 if (tport == NULL)
783 return;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
786 status = ti_restart_read(tport, tty);
787 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100788 dev_err(&port->dev, "%s - cannot restart read, %d\n",
789 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791}
792
793
Alan Cox95da3102008-07-22 11:09:07 +0100794static int ti_ioctl(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 unsigned int cmd, unsigned long arg)
796{
Alan Cox95da3102008-07-22 11:09:07 +0100797 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct ti_port *tport = usb_get_serial_port_data(port);
799 struct async_icount cnow;
800 struct async_icount cprev;
801
Harvey Harrison441b62c2008-03-03 16:08:34 -0800802 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (tport == NULL)
805 return -ENODEV;
806
807 switch (cmd) {
Alan Coxa30fa792008-07-22 11:15:17 +0100808 case TIOCGSERIAL:
809 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
810 return ti_get_serial_info(tport,
811 (struct serial_struct __user *)arg);
812 case TIOCSSERIAL:
813 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
Alan Cox4a90f092008-10-13 10:39:46 +0100814 return ti_set_serial_info(tty, tport,
815 (struct serial_struct __user *)arg);
Alan Coxa30fa792008-07-22 11:15:17 +0100816 case TIOCMIWAIT:
817 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
818 cprev = tport->tp_icount;
819 while (1) {
820 interruptible_sleep_on(&tport->tp_msr_wait);
821 if (signal_pending(current))
822 return -ERESTARTSYS;
823 cnow = tport->tp_icount;
824 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
825 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
826 return -EIO; /* no change => error */
827 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
828 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
829 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
830 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
831 return 0;
832 cprev = cnow;
833 }
834 break;
835 case TIOCGICOUNT:
836 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
837 __func__, port->number,
838 tport->tp_icount.rx, tport->tp_icount.tx);
839 if (copy_to_user((void __user *)arg, &tport->tp_icount,
840 sizeof(tport->tp_icount)))
841 return -EFAULT;
842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -ENOIOCTLCMD;
845}
846
847
Alan Cox95da3102008-07-22 11:09:07 +0100848static void ti_set_termios(struct tty_struct *tty,
849 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct ti_uart_config *config;
Alan Coxa30fa792008-07-22 11:15:17 +0100853 tcflag_t cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int baud;
855 int status;
856 int port_number = port->number - port->serial->minor;
857 unsigned int mcr;
858
Harvey Harrison441b62c2008-03-03 16:08:34 -0800859 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 cflag = tty->termios->c_cflag;
862 iflag = tty->termios->c_iflag;
863
Harvey Harrison441b62c2008-03-03 16:08:34 -0800864 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
Alan Coxa30fa792008-07-22 11:15:17 +0100865 dbg("%s - old clfag %08x, old iflag %08x", __func__,
866 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 if (tport == NULL)
869 return;
870
871 config = kmalloc(sizeof(*config), GFP_KERNEL);
872 if (!config) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800873 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return;
875 }
876
877 config->wFlags = 0;
878
879 /* these flags must be set */
880 config->wFlags |= TI_UART_ENABLE_MS_INTS;
881 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
882 config->bUartMode = (__u8)(tport->tp_uart_mode);
883
884 switch (cflag & CSIZE) {
Alan Coxa30fa792008-07-22 11:15:17 +0100885 case CS5:
886 config->bDataBits = TI_UART_5_DATA_BITS;
887 break;
888 case CS6:
889 config->bDataBits = TI_UART_6_DATA_BITS;
890 break;
891 case CS7:
892 config->bDataBits = TI_UART_7_DATA_BITS;
893 break;
894 default:
895 case CS8:
896 config->bDataBits = TI_UART_8_DATA_BITS;
897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
Alan Cox537699e2008-01-03 17:03:11 +0000900 /* CMSPAR isn't supported by this driver */
901 tty->termios->c_cflag &= ~CMSPAR;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (cflag & PARENB) {
904 if (cflag & PARODD) {
905 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
906 config->bParity = TI_UART_ODD_PARITY;
907 } else {
908 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
909 config->bParity = TI_UART_EVEN_PARITY;
910 }
911 } else {
912 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
Alan Coxa30fa792008-07-22 11:15:17 +0100913 config->bParity = TI_UART_NO_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
916 if (cflag & CSTOPB)
917 config->bStopBits = TI_UART_2_STOP_BITS;
918 else
919 config->bStopBits = TI_UART_1_STOP_BITS;
920
921 if (cflag & CRTSCTS) {
922 /* RTS flow control must be off to drop RTS for baud rate B0 */
923 if ((cflag & CBAUD) != B0)
924 config->wFlags |= TI_UART_ENABLE_RTS_IN;
925 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
926 } else {
927 tty->hw_stopped = 0;
928 ti_restart_read(tport, tty);
929 }
930
931 if (I_IXOFF(tty) || I_IXON(tty)) {
932 config->cXon = START_CHAR(tty);
933 config->cXoff = STOP_CHAR(tty);
934
935 if (I_IXOFF(tty))
936 config->wFlags |= TI_UART_ENABLE_X_IN;
937 else
938 ti_restart_read(tport, tty);
939
940 if (I_IXON(tty))
941 config->wFlags |= TI_UART_ENABLE_X_OUT;
942 }
943
944 baud = tty_get_baud_rate(tty);
Alan Cox537699e2008-01-03 17:03:11 +0000945 if (!baud)
946 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (tport->tp_tdev->td_is_3410)
948 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
949 else
950 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
951
Alan Cox537699e2008-01-03 17:03:11 +0000952 /* FIXME: Should calculate resulting baud here and report it back */
953 if ((cflag & CBAUD) != B0)
954 tty_encode_baud_rate(tty, baud, baud);
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800957 __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 cpu_to_be16s(&config->wBaudRate);
960 cpu_to_be16s(&config->wFlags);
961
962 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
963 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
964 sizeof(*config));
965 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100966 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
967 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
970 mcr = tport->tp_shadow_mcr;
971 /* if baud rate is B0, clear RTS and DTR */
972 if ((cflag & CBAUD) == B0)
973 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
974 status = ti_set_mcr(tport, mcr);
975 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100976 dev_err(&port->dev,
977 "%s - cannot set modem control on port %d, %d\n",
978 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 kfree(config);
981}
982
983
Alan Cox95da3102008-07-22 11:09:07 +0100984static int ti_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Alan Cox95da3102008-07-22 11:09:07 +0100986 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 struct ti_port *tport = usb_get_serial_port_data(port);
988 unsigned int result;
989 unsigned int msr;
990 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000991 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Harvey Harrison441b62c2008-03-03 16:08:34 -0800993 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (tport == NULL)
996 return -ENODEV;
997
Alan Cox04ca89d2008-02-20 21:41:40 +0000998 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 msr = tport->tp_msr;
1000 mcr = tport->tp_shadow_mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001001 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1004 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1005 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1006 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1007 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1008 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1009 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1010
Harvey Harrison441b62c2008-03-03 16:08:34 -08001011 dbg("%s - 0x%04X", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 return result;
1014}
1015
1016
Alan Cox95da3102008-07-22 11:09:07 +01001017static int ti_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 unsigned int set, unsigned int clear)
1019{
Alan Cox95da3102008-07-22 11:09:07 +01001020 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct ti_port *tport = usb_get_serial_port_data(port);
1022 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001023 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Harvey Harrison441b62c2008-03-03 16:08:34 -08001025 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 if (tport == NULL)
1028 return -ENODEV;
1029
Alan Cox04ca89d2008-02-20 21:41:40 +00001030 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 mcr = tport->tp_shadow_mcr;
1032
1033 if (set & TIOCM_RTS)
1034 mcr |= TI_MCR_RTS;
1035 if (set & TIOCM_DTR)
1036 mcr |= TI_MCR_DTR;
1037 if (set & TIOCM_LOOP)
1038 mcr |= TI_MCR_LOOP;
1039
1040 if (clear & TIOCM_RTS)
1041 mcr &= ~TI_MCR_RTS;
1042 if (clear & TIOCM_DTR)
1043 mcr &= ~TI_MCR_DTR;
1044 if (clear & TIOCM_LOOP)
1045 mcr &= ~TI_MCR_LOOP;
Alan Cox04ca89d2008-02-20 21:41:40 +00001046 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 return ti_set_mcr(tport, mcr);
1049}
1050
1051
Alan Cox95da3102008-07-22 11:09:07 +01001052static void ti_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Alan Cox95da3102008-07-22 11:09:07 +01001054 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct ti_port *tport = usb_get_serial_port_data(port);
1056 int status;
1057
Harvey Harrison441b62c2008-03-03 16:08:34 -08001058 dbg("%s - state = %d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (tport == NULL)
1061 return;
1062
1063 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1064
1065 status = ti_write_byte(tport->tp_tdev,
1066 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1067 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1068
1069 if (status)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001070 dbg("%s - error setting break, %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073
David Howells7d12e782006-10-05 14:55:46 +01001074static void ti_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Ming Leicdc97792008-02-24 18:41:47 +08001076 struct ti_device *tdev = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct usb_serial_port *port;
1078 struct usb_serial *serial = tdev->td_serial;
1079 struct ti_port *tport;
1080 struct device *dev = &urb->dev->dev;
1081 unsigned char *data = urb->transfer_buffer;
1082 int length = urb->actual_length;
1083 int port_number;
1084 int function;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001085 int status = urb->status;
1086 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 __u8 msr;
1088
Harvey Harrison441b62c2008-03-03 16:08:34 -08001089 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001091 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 case 0:
1093 break;
1094 case -ECONNRESET:
1095 case -ENOENT:
1096 case -ESHUTDOWN:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001097 dbg("%s - urb shutting down, %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 tdev->td_urb_error = 1;
1099 return;
1100 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001101 dev_err(dev, "%s - nonzero urb status, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001102 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 tdev->td_urb_error = 1;
1104 goto exit;
1105 }
1106
1107 if (length != 2) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001108 dbg("%s - bad packet size, %d", __func__, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 goto exit;
1110 }
1111
1112 if (data[0] == TI_CODE_HARDWARE_ERROR) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001113 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 goto exit;
1115 }
1116
1117 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1118 function = TI_GET_FUNC_FROM_CODE(data[0]);
1119
Alan Coxa30fa792008-07-22 11:15:17 +01001120 dbg("%s - port_number %d, function %d, data 0x%02X",
1121 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 if (port_number >= serial->num_ports) {
Alan Coxa30fa792008-07-22 11:15:17 +01001124 dev_err(dev, "%s - bad port number, %d\n",
1125 __func__, port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto exit;
1127 }
1128
1129 port = serial->port[port_number];
1130
1131 tport = usb_get_serial_port_data(port);
1132 if (!tport)
1133 goto exit;
1134
1135 switch (function) {
1136 case TI_CODE_DATA_ERROR:
Alan Coxa30fa792008-07-22 11:15:17 +01001137 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1138 __func__, port_number, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140
1141 case TI_CODE_MODEM_STATUS:
1142 msr = data[1];
Harvey Harrison441b62c2008-03-03 16:08:34 -08001143 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 ti_handle_new_msr(tport, msr);
1145 break;
1146
1147 default:
Alan Coxa30fa792008-07-22 11:15:17 +01001148 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1149 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
1151 }
1152
1153exit:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001154 retval = usb_submit_urb(urb, GFP_ATOMIC);
1155 if (retval)
1156 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001157 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
1160
David Howells7d12e782006-10-05 14:55:46 +01001161static void ti_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Ming Leicdc97792008-02-24 18:41:47 +08001163 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct usb_serial_port *port = tport->tp_port;
1165 struct device *dev = &urb->dev->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001166 int status = urb->status;
1167 int retval = 0;
Alan Cox4a90f092008-10-13 10:39:46 +01001168 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Harvey Harrison441b62c2008-03-03 16:08:34 -08001170 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001172 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 case 0:
1174 break;
1175 case -ECONNRESET:
1176 case -ENOENT:
1177 case -ESHUTDOWN:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001178 dbg("%s - urb shutting down, %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 tport->tp_tdev->td_urb_error = 1;
1180 wake_up_interruptible(&tport->tp_write_wait);
1181 return;
1182 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001183 dev_err(dev, "%s - nonzero urb status, %d\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001184 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 tport->tp_tdev->td_urb_error = 1;
1186 wake_up_interruptible(&tport->tp_write_wait);
1187 }
1188
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001189 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 goto exit;
1191
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001192 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001193 dev_err(dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return;
1195 }
1196
Alan Cox4a90f092008-10-13 10:39:46 +01001197 tty = tty_port_tty_get(&port->port);
1198 if (tty && urb->actual_length) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001199 usb_serial_debug_data(debug, dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 urb->actual_length, urb->transfer_buffer);
1201
1202 if (!tport->tp_is_open)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001203 dbg("%s - port closed, dropping data", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 else
Alan Cox4a90f092008-10-13 10:39:46 +01001205 ti_recv(&urb->dev->dev, tty,
Alan Coxa30fa792008-07-22 11:15:17 +01001206 urb->transfer_buffer,
1207 urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 spin_lock(&tport->tp_lock);
1210 tport->tp_icount.rx += urb->actual_length;
1211 spin_unlock(&tport->tp_lock);
Alan Cox4a90f092008-10-13 10:39:46 +01001212 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
1215exit:
1216 /* continue to read unless stopping */
1217 spin_lock(&tport->tp_lock);
1218 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
1219 urb->dev = port->serial->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001220 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 } else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
1222 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1223 }
1224 spin_unlock(&tport->tp_lock);
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001225 if (retval)
1226 dev_err(dev, "%s - resubmit read urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001227 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230
David Howells7d12e782006-10-05 14:55:46 +01001231static void ti_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Ming Leicdc97792008-02-24 18:41:47 +08001233 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct usb_serial_port *port = tport->tp_port;
1235 struct device *dev = &urb->dev->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001236 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Harvey Harrison441b62c2008-03-03 16:08:34 -08001238 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 tport->tp_write_urb_in_use = 0;
1241
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001242 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 case 0:
1244 break;
1245 case -ECONNRESET:
1246 case -ENOENT:
1247 case -ESHUTDOWN:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001248 dbg("%s - urb shutting down, %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 tport->tp_tdev->td_urb_error = 1;
1250 wake_up_interruptible(&tport->tp_write_wait);
1251 return;
1252 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001253 dev_err(dev, "%s - nonzero urb status, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001254 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 tport->tp_tdev->td_urb_error = 1;
1256 wake_up_interruptible(&tport->tp_write_wait);
1257 }
1258
1259 /* send any buffered data */
1260 ti_send(tport);
1261}
1262
1263
1264static void ti_recv(struct device *dev, struct tty_struct *tty,
1265 unsigned char *data, int length)
1266{
1267 int cnt;
1268
1269 do {
Alan Cox33f0f882006-01-09 20:54:13 -08001270 cnt = tty_buffer_request_room(tty, length);
1271 if (cnt < length) {
Alan Coxa30fa792008-07-22 11:15:17 +01001272 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1273 __func__, length - cnt);
1274 if (cnt == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Alan Cox33f0f882006-01-09 20:54:13 -08001277 tty_insert_flip_string(tty, data, cnt);
1278 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 data += cnt;
1280 length -= cnt;
1281 } while (length > 0);
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
1285
1286static void ti_send(struct ti_port *tport)
1287{
1288 int count, result;
1289 struct usb_serial_port *port = tport->tp_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001290 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 unsigned long flags;
1292
1293
Harvey Harrison441b62c2008-03-03 16:08:34 -08001294 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 spin_lock_irqsave(&tport->tp_lock, flags);
1297
Alan Cox4a90f092008-10-13 10:39:46 +01001298 if (tport->tp_write_urb_in_use)
1299 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 count = ti_buf_get(tport->tp_write_buf,
1302 port->write_urb->transfer_buffer,
1303 port->bulk_out_size);
1304
Alan Cox4a90f092008-10-13 10:39:46 +01001305 if (count == 0)
1306 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 tport->tp_write_urb_in_use = 1;
1309
1310 spin_unlock_irqrestore(&tport->tp_lock, flags);
1311
Alan Coxa30fa792008-07-22 11:15:17 +01001312 usb_serial_debug_data(debug, &port->dev, __func__, count,
1313 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1316 usb_sndbulkpipe(port->serial->dev,
1317 port->bulk_out_endpointAddress),
1318 port->write_urb->transfer_buffer, count,
1319 ti_bulk_out_callback, tport);
1320
1321 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1322 if (result) {
Alan Coxa30fa792008-07-22 11:15:17 +01001323 dev_err(&port->dev, "%s - submit write urb failed, %d\n",
1324 __func__, result);
1325 tport->tp_write_urb_in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* TODO: reschedule ti_send */
1327 } else {
1328 spin_lock_irqsave(&tport->tp_lock, flags);
1329 tport->tp_icount.tx += count;
1330 spin_unlock_irqrestore(&tport->tp_lock, flags);
1331 }
1332
1333 /* more room in the buffer for new writes, wakeup */
1334 if (tty)
1335 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001336 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 wake_up_interruptible(&tport->tp_write_wait);
Alan Cox4a90f092008-10-13 10:39:46 +01001338 return;
1339unlock:
1340 spin_unlock_irqrestore(&tport->tp_lock, flags);
1341 tty_kref_put(tty);
1342 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345
1346static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1347{
Alan Cox04ca89d2008-02-20 21:41:40 +00001348 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int status;
1350
1351 status = ti_write_byte(tport->tp_tdev,
1352 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1353 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1354
Alan Cox04ca89d2008-02-20 21:41:40 +00001355 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (!status)
1357 tport->tp_shadow_mcr = mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001358 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 return status;
1361}
1362
1363
1364static int ti_get_lsr(struct ti_port *tport)
1365{
Alan Coxa30fa792008-07-22 11:15:17 +01001366 int size, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 struct ti_device *tdev = tport->tp_tdev;
1368 struct usb_serial_port *port = tport->tp_port;
1369 int port_number = port->number - port->serial->minor;
1370 struct ti_port_status *data;
1371
Harvey Harrison441b62c2008-03-03 16:08:34 -08001372 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 size = sizeof(struct ti_port_status);
1375 data = kmalloc(size, GFP_KERNEL);
1376 if (!data) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001377 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return -ENOMEM;
1379 }
1380
1381 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1382 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1383 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001384 dev_err(&port->dev,
1385 "%s - get port status command failed, %d\n",
1386 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 goto free_data;
1388 }
1389
Harvey Harrison441b62c2008-03-03 16:08:34 -08001390 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 tport->tp_lsr = data->bLSR;
1393
1394free_data:
1395 kfree(data);
1396 return status;
1397}
1398
1399
1400static int ti_get_serial_info(struct ti_port *tport,
1401 struct serial_struct __user *ret_arg)
1402{
1403 struct usb_serial_port *port = tport->tp_port;
1404 struct serial_struct ret_serial;
1405
1406 if (!ret_arg)
1407 return -EFAULT;
1408
1409 memset(&ret_serial, 0, sizeof(ret_serial));
1410
1411 ret_serial.type = PORT_16550A;
1412 ret_serial.line = port->serial->minor;
1413 ret_serial.port = port->number - port->serial->minor;
1414 ret_serial.flags = tport->tp_flags;
1415 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1416 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1417 ret_serial.closing_wait = tport->tp_closing_wait;
1418
1419 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1420 return -EFAULT;
1421
1422 return 0;
1423}
1424
1425
Alan Cox4a90f092008-10-13 10:39:46 +01001426static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct serial_struct __user *new_arg)
1428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct serial_struct new_serial;
1430
1431 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1432 return -EFAULT;
1433
1434 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
Alan Cox4a90f092008-10-13 10:39:46 +01001435 tty->low_latency = (tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 tport->tp_closing_wait = new_serial.closing_wait;
1437
1438 return 0;
1439}
1440
1441
1442static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1443{
1444 struct async_icount *icount;
1445 struct tty_struct *tty;
1446 unsigned long flags;
1447
Harvey Harrison441b62c2008-03-03 16:08:34 -08001448 dbg("%s - msr 0x%02X", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 if (msr & TI_MSR_DELTA_MASK) {
1451 spin_lock_irqsave(&tport->tp_lock, flags);
1452 icount = &tport->tp_icount;
1453 if (msr & TI_MSR_DELTA_CTS)
1454 icount->cts++;
1455 if (msr & TI_MSR_DELTA_DSR)
1456 icount->dsr++;
1457 if (msr & TI_MSR_DELTA_CD)
1458 icount->dcd++;
1459 if (msr & TI_MSR_DELTA_RI)
1460 icount->rng++;
1461 wake_up_interruptible(&tport->tp_msr_wait);
1462 spin_unlock_irqrestore(&tport->tp_lock, flags);
1463 }
1464
1465 tport->tp_msr = msr & TI_MSR_MASK;
1466
1467 /* handle CTS flow control */
Alan Cox4a90f092008-10-13 10:39:46 +01001468 tty = tty_port_tty_get(&tport->tp_port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (tty && C_CRTSCTS(tty)) {
1470 if (msr & TI_MSR_CTS) {
1471 tty->hw_stopped = 0;
1472 tty_wakeup(tty);
1473 } else {
1474 tty->hw_stopped = 1;
1475 }
1476 }
Alan Cox4a90f092008-10-13 10:39:46 +01001477 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480
1481static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1482{
1483 struct ti_device *tdev = tport->tp_tdev;
1484 struct usb_serial_port *port = tport->tp_port;
1485 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Harvey Harrison441b62c2008-03-03 16:08:34 -08001487 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Oliver Neukum0915f492008-01-23 12:28:45 +01001489 spin_lock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /* wait for data to drain from the buffer */
1492 tdev->td_urb_error = 0;
1493 init_waitqueue_entry(&wait, current);
1494 add_wait_queue(&tport->tp_write_wait, &wait);
1495 for (;;) {
1496 set_current_state(TASK_INTERRUPTIBLE);
1497 if (ti_buf_data_avail(tport->tp_write_buf) == 0
1498 || timeout == 0 || signal_pending(current)
1499 || tdev->td_urb_error
Oliver Neukum0915f492008-01-23 12:28:45 +01001500 || port->serial->disconnected) /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 break;
Oliver Neukum0915f492008-01-23 12:28:45 +01001502 spin_unlock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 timeout = schedule_timeout(timeout);
Oliver Neukum0915f492008-01-23 12:28:45 +01001504 spin_lock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506 set_current_state(TASK_RUNNING);
1507 remove_wait_queue(&tport->tp_write_wait, &wait);
1508
1509 /* flush any remaining data in the buffer */
1510 if (flush)
1511 ti_buf_clear(tport->tp_write_buf);
1512
Oliver Neukum0915f492008-01-23 12:28:45 +01001513 spin_unlock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Oliver Neukum0915f492008-01-23 12:28:45 +01001515 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /* wait for data to drain from the device */
1517 /* wait for empty tx register, plus 20 ms */
1518 timeout += jiffies;
1519 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1520 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1521 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
Oliver Neukum0915f492008-01-23 12:28:45 +01001522 && !port->serial->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (ti_get_lsr(tport))
1524 break;
Oliver Neukum0915f492008-01-23 12:28:45 +01001525 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 msleep_interruptible(20);
Oliver Neukum0915f492008-01-23 12:28:45 +01001527 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Oliver Neukum0915f492008-01-23 12:28:45 +01001529 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
1532
1533static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1534{
1535 unsigned long flags;
1536
1537 spin_lock_irqsave(&tport->tp_lock, flags);
1538
1539 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1540 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1541
1542 spin_unlock_irqrestore(&tport->tp_lock, flags);
1543}
1544
1545
1546static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1547{
1548 struct urb *urb;
1549 int status = 0;
1550 unsigned long flags;
1551
1552 spin_lock_irqsave(&tport->tp_lock, flags);
1553
1554 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
Oliver Neukum944dc182007-05-07 08:33:18 +02001555 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 urb = tport->tp_port->read_urb;
Oliver Neukum944dc182007-05-07 08:33:18 +02001557 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 urb->complete = ti_bulk_in_callback;
1559 urb->context = tport;
1560 urb->dev = tport->tp_port->serial->dev;
1561 status = usb_submit_urb(urb, GFP_KERNEL);
Oliver Neukum944dc182007-05-07 08:33:18 +02001562 } else {
1563 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1564 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 return status;
1568}
1569
1570
1571static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1572 __u16 moduleid, __u16 value, __u8 *data, int size)
1573{
1574 int status;
1575
1576 status = usb_control_msg(tdev->td_serial->dev,
1577 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1578 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1579 value, moduleid, data, size, 1000);
1580
1581 if (status == size)
1582 status = 0;
1583
1584 if (status > 0)
1585 status = -ECOMM;
1586
1587 return status;
1588}
1589
1590
1591static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1592 __u16 moduleid, __u16 value, __u8 *data, int size)
1593{
1594 int status;
1595
1596 status = usb_control_msg(tdev->td_serial->dev,
1597 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1598 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1599 value, moduleid, data, size, 1000);
1600
1601 if (status == size)
1602 status = 0;
1603
1604 if (status > 0)
1605 status = -ECOMM;
1606
1607 return status;
1608}
1609
1610
1611static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1612 __u8 mask, __u8 byte)
1613{
1614 int status;
1615 unsigned int size;
1616 struct ti_write_data_bytes *data;
1617 struct device *dev = &tdev->td_serial->dev->dev;
1618
Alan Coxa30fa792008-07-22 11:15:17 +01001619 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1620 __func__, addr, mask, byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 size = sizeof(struct ti_write_data_bytes) + 2;
1623 data = kmalloc(size, GFP_KERNEL);
1624 if (!data) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001625 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return -ENOMEM;
1627 }
1628
1629 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1630 data->bDataType = TI_RW_DATA_BYTE;
1631 data->bDataCounter = 1;
1632 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1633 data->wBaseAddrLo = cpu_to_be16(addr);
1634 data->bData[0] = mask;
1635 data->bData[1] = byte;
1636
1637 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1638 (__u8 *)data, size);
1639
1640 if (status < 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001641 dev_err(dev, "%s - failed, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 kfree(data);
1644
1645 return status;
1646}
1647
Alan Cox95da3102008-07-22 11:09:07 +01001648static int ti_do_download(struct usb_device *dev, int pipe,
1649 u8 *buffer, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int pos;
Alan Cox95da3102008-07-22 11:09:07 +01001652 u8 cs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct ti_firmware_header *header;
Alan Cox95da3102008-07-22 11:09:07 +01001655 int status;
1656 int len;
Alan Coxa30fa792008-07-22 11:15:17 +01001657
1658 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 cs = (__u8)(cs + buffer[pos]);
1660
1661 header = (struct ti_firmware_header *)buffer;
Alan Coxa30fa792008-07-22 11:15:17 +01001662 header->wLength = cpu_to_le16((__u16)(size
Alan Cox95da3102008-07-22 11:09:07 +01001663 - sizeof(struct ti_firmware_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 header->bCheckSum = cs;
1665
Harvey Harrison441b62c2008-03-03 16:08:34 -08001666 dbg("%s - downloading firmware", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01001667 for (pos = 0; pos < size; pos += done) {
1668 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1669 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1670 &done, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (status)
1672 break;
1673 }
Alan Cox95da3102008-07-22 11:09:07 +01001674 return status;
1675}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Chris Adams05a3d902009-01-11 19:48:53 +00001677static int ti_download_firmware(struct ti_device *tdev)
Alan Cox95da3102008-07-22 11:09:07 +01001678{
Chris Adams05a3d902009-01-11 19:48:53 +00001679 int status;
Alan Cox95da3102008-07-22 11:09:07 +01001680 int buffer_size;
1681 __u8 *buffer;
1682 struct usb_device *dev = tdev->td_serial->dev;
1683 unsigned int pipe = usb_sndbulkpipe(dev,
1684 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1685 const struct firmware *fw_p;
1686 char buf[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Chris Adams05a3d902009-01-11 19:48:53 +00001688 /* try ID specific firmware first, then try generic firmware */
1689 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1690 dev->descriptor.idProduct);
1691 if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) {
1692 if (tdev->td_is_3410)
1693 strcpy(buf, "ti_3410.fw");
1694 else
1695 strcpy(buf, "ti_5052.fw");
1696 status = request_firmware(&fw_p, buf, &dev->dev);
1697 }
1698 if (status) {
Alan Cox95da3102008-07-22 11:09:07 +01001699 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1700 return -ENOENT;
1701 }
1702 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1703 dev_err(&dev->dev, "%s - firmware too large\n", __func__);
1704 return -ENOENT;
1705 }
1706
1707 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1708 buffer = kmalloc(buffer_size, GFP_KERNEL);
1709 if (buffer) {
1710 memcpy(buffer, fw_p->data, fw_p->size);
1711 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
Chris Adams2bcbe4c2008-09-10 14:11:38 -07001712 status = ti_do_download(dev, pipe, buffer, fw_p->size);
Alan Cox95da3102008-07-22 11:09:07 +01001713 kfree(buffer);
Chris Adams05a3d902009-01-11 19:48:53 +00001714 } else {
1715 status = -ENOMEM;
Alan Cox95da3102008-07-22 11:09:07 +01001716 }
1717 release_firmware(fw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001719 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1720 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return status;
1722 }
1723
Harvey Harrison441b62c2008-03-03 16:08:34 -08001724 dbg("%s - download successful", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 return 0;
1727}
1728
1729
1730/* Circular Buffer Functions */
1731
1732/*
1733 * ti_buf_alloc
1734 *
1735 * Allocate a circular buffer and all associated memory.
1736 */
1737
1738static struct circ_buf *ti_buf_alloc(void)
1739{
1740 struct circ_buf *cb;
1741
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001742 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (cb == NULL)
1744 return NULL;
1745
1746 cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
1747 if (cb->buf == NULL) {
1748 kfree(cb);
1749 return NULL;
1750 }
1751
1752 ti_buf_clear(cb);
1753
1754 return cb;
1755}
1756
1757
1758/*
1759 * ti_buf_free
1760 *
1761 * Free the buffer and all associated memory.
1762 */
1763
1764static void ti_buf_free(struct circ_buf *cb)
1765{
1766 kfree(cb->buf);
1767 kfree(cb);
1768}
1769
1770
1771/*
1772 * ti_buf_clear
1773 *
1774 * Clear out all data in the circular buffer.
1775 */
1776
1777static void ti_buf_clear(struct circ_buf *cb)
1778{
1779 cb->head = cb->tail = 0;
1780}
1781
1782
1783/*
1784 * ti_buf_data_avail
1785 *
1786 * Return the number of bytes of data available in the circular
1787 * buffer.
1788 */
1789
1790static int ti_buf_data_avail(struct circ_buf *cb)
1791{
Alan Coxa30fa792008-07-22 11:15:17 +01001792 return CIRC_CNT(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
1795
1796/*
1797 * ti_buf_space_avail
1798 *
1799 * Return the number of bytes of space available in the circular
1800 * buffer.
1801 */
1802
1803static int ti_buf_space_avail(struct circ_buf *cb)
1804{
Alan Coxa30fa792008-07-22 11:15:17 +01001805 return CIRC_SPACE(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
1808
1809/*
1810 * ti_buf_put
1811 *
1812 * Copy data data from a user buffer and put it into the circular buffer.
1813 * Restrict to the amount of space available.
1814 *
1815 * Return the number of bytes copied.
1816 */
1817
1818static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
1819{
1820 int c, ret = 0;
1821
1822 while (1) {
1823 c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1824 if (count < c)
1825 c = count;
1826 if (c <= 0)
1827 break;
1828 memcpy(cb->buf + cb->head, buf, c);
1829 cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
1830 buf += c;
1831 count -= c;
1832 ret += c;
1833 }
1834
1835 return ret;
1836}
1837
1838
1839/*
1840 * ti_buf_get
1841 *
1842 * Get data from the circular buffer and copy to the given buffer.
1843 * Restrict to the amount of data available.
1844 *
1845 * Return the number of bytes copied.
1846 */
1847
1848static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
1849{
1850 int c, ret = 0;
1851
1852 while (1) {
1853 c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
1854 if (count < c)
1855 c = count;
1856 if (c <= 0)
1857 break;
1858 memcpy(buf, cb->buf + cb->tail, c);
1859 cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);
1860 buf += c;
1861 count -= c;
1862 ret += c;
1863 }
1864
1865 return ret;
1866}