blob: 6d16717ef4f7f51be98c69521b2702e390409dd1 [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>
Johan Hovold074ef652010-05-19 00:01:35 +020033#include <linux/kfifo.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
Johan Hovold074ef652010-05-19 00:01:35 +020043#define TI_DRIVER_VERSION "v0.10"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
54
55/* supported setserial flags */
Oliver Neukum2400a2b2009-04-20 17:28:53 +020056#define TI_SET_SERIAL_FLAGS 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/* read urb states */
59#define TI_READ_URB_RUNNING 0
60#define TI_READ_URB_STOPPING 1
61#define TI_READ_URB_STOPPED 2
62
63#define TI_EXTRA_VID_PID_COUNT 5
64
65
66/* Structures */
67
68struct ti_port {
69 int tp_is_open;
70 __u8 tp_msr;
71 __u8 tp_lsr;
72 __u8 tp_shadow_mcr;
73 __u8 tp_uart_mode; /* 232 or 485 modes */
74 unsigned int tp_uart_base_addr;
75 int tp_flags;
76 int tp_closing_wait;/* in .01 secs */
77 struct async_icount tp_icount;
78 wait_queue_head_t tp_msr_wait; /* wait for msr change */
79 wait_queue_head_t tp_write_wait;
80 struct ti_device *tp_tdev;
81 struct usb_serial_port *tp_port;
82 spinlock_t tp_lock;
83 int tp_read_urb_state;
84 int tp_write_urb_in_use;
Johan Hovold074ef652010-05-19 00:01:35 +020085 struct kfifo write_fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
88struct ti_device {
Matthias Kaehlcke9da00682007-11-21 15:13:16 -080089 struct mutex td_open_close_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int td_open_port_count;
91 struct usb_serial *td_serial;
92 int td_is_3410;
93 int td_urb_error;
94};
95
96
97/* Function Declarations */
98
99static int ti_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400100static void ti_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -0700101static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100102static void ti_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +0100103static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Coxa30fa792008-07-22 11:15:17 +0100104 const unsigned char *data, int count);
Alan Cox95da3102008-07-22 11:09:07 +0100105static int ti_write_room(struct tty_struct *tty);
106static int ti_chars_in_buffer(struct tty_struct *tty);
107static void ti_throttle(struct tty_struct *tty);
108static void ti_unthrottle(struct tty_struct *tty);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000109static int ti_ioctl(struct tty_struct *tty,
Alan Coxa30fa792008-07-22 11:15:17 +0100110 unsigned int cmd, unsigned long arg);
Alan Cox0bca1b92010-09-16 18:21:40 +0100111static int ti_get_icount(struct tty_struct *tty,
112 struct serial_icounter_struct *icount);
Alan Coxa30fa792008-07-22 11:15:17 +0100113static void ti_set_termios(struct tty_struct *tty,
114 struct usb_serial_port *port, struct ktermios *old_termios);
Alan Cox60b33c12011-02-14 16:26:14 +0000115static int ti_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000116static int ti_tiocmset(struct tty_struct *tty,
Alan Coxa30fa792008-07-22 11:15:17 +0100117 unsigned int set, unsigned int clear);
Alan Cox95da3102008-07-22 11:09:07 +0100118static void ti_break(struct tty_struct *tty, int break_state);
David Howells7d12e782006-10-05 14:55:46 +0100119static void ti_interrupt_callback(struct urb *urb);
120static void ti_bulk_in_callback(struct urb *urb);
121static void ti_bulk_out_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123static void ti_recv(struct device *dev, struct tty_struct *tty,
124 unsigned char *data, int length);
125static void ti_send(struct ti_port *tport);
126static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
127static int ti_get_lsr(struct ti_port *tport);
128static int ti_get_serial_info(struct ti_port *tport,
129 struct serial_struct __user *ret_arg);
Alan Cox4a90f092008-10-13 10:39:46 +0100130static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct serial_struct __user *new_arg);
132static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
133
134static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
135
136static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
137static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
138
139static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
140 __u16 moduleid, __u16 value, __u8 *data, int size);
141static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
142 __u16 moduleid, __u16 value, __u8 *data, int size);
143
144static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
145 __u8 mask, __u8 byte);
146
Chris Adams05a3d902009-01-11 19:48:53 +0000147static int ti_download_firmware(struct ti_device *tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/* Data */
151
152/* module parameters */
153static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
155static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100156static unsigned int vendor_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100158static unsigned int product_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100160static unsigned int vendor_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100162static unsigned int product_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/* supported devices */
165/* the array dimension is the number of default entries plus */
166/* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
167/* null entry */
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700168static struct usb_device_id ti_id_table_3410[13+TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000170 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Chris Adamscb7a7c62009-01-11 19:49:00 +0000171 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
172 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
173 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
174 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
175 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700176 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
177 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
178 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
Oliver Neukum1a1fab512009-01-12 13:31:16 +0100179 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
Oliver Neukum97dcf042009-02-04 16:38:33 +0100180 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
181 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Oliver Neukum97dcf042009-02-04 16:38:33 +0100184static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 { 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
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700191static struct usb_device_id ti_id_table_combined[17+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) },
Chris Adamscb7a7c62009-01-11 19:49:00 +0000194 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
195 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
196 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
197 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
198 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700199 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
200 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
201 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
203 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
204 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
205 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
Oliver Neukum1a1fab512009-01-12 13:31:16 +0100206 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
Oliver Neukum97dcf042009-02-04 16:38:33 +0100207 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
208 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 { }
210};
211
212static struct usb_driver ti_usb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .name = "ti_usb_3410_5052",
214 .probe = usb_serial_probe,
215 .disconnect = usb_serial_disconnect,
216 .id_table = ti_id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800217 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700220static struct usb_serial_driver ti_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700221 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700222 .owner = THIS_MODULE,
223 .name = "ti_usb_3410_5052_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700224 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700225 .description = "TI USB 3410 1 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100226 .usb_driver = &ti_usb_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 .id_table = ti_id_table_3410,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 .num_ports = 1,
229 .attach = ti_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400230 .release = ti_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .open = ti_open,
232 .close = ti_close,
233 .write = ti_write,
234 .write_room = ti_write_room,
235 .chars_in_buffer = ti_chars_in_buffer,
236 .throttle = ti_throttle,
237 .unthrottle = ti_unthrottle,
238 .ioctl = ti_ioctl,
239 .set_termios = ti_set_termios,
240 .tiocmget = ti_tiocmget,
241 .tiocmset = ti_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +0100242 .get_icount = ti_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .break_ctl = ti_break,
244 .read_int_callback = ti_interrupt_callback,
245 .read_bulk_callback = ti_bulk_in_callback,
246 .write_bulk_callback = ti_bulk_out_callback,
247};
248
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700249static struct usb_serial_driver ti_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700250 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700251 .owner = THIS_MODULE,
252 .name = "ti_usb_3410_5052_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700253 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700254 .description = "TI USB 5052 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100255 .usb_driver = &ti_usb_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 .id_table = ti_id_table_5052,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 .num_ports = 2,
258 .attach = ti_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400259 .release = ti_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .open = ti_open,
261 .close = ti_close,
262 .write = ti_write,
263 .write_room = ti_write_room,
264 .chars_in_buffer = ti_chars_in_buffer,
265 .throttle = ti_throttle,
266 .unthrottle = ti_unthrottle,
267 .ioctl = ti_ioctl,
268 .set_termios = ti_set_termios,
269 .tiocmget = ti_tiocmget,
270 .tiocmset = ti_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +0100271 .get_icount = ti_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .break_ctl = ti_break,
273 .read_int_callback = ti_interrupt_callback,
274 .read_bulk_callback = ti_bulk_in_callback,
275 .write_bulk_callback = ti_bulk_out_callback,
276};
277
278
279/* Module */
280
281MODULE_AUTHOR(TI_DRIVER_AUTHOR);
282MODULE_DESCRIPTION(TI_DRIVER_DESC);
283MODULE_VERSION(TI_DRIVER_VERSION);
284MODULE_LICENSE("GPL");
285
David Woodhouse5f24e2d2008-05-30 18:49:51 +0300286MODULE_FIRMWARE("ti_3410.fw");
287MODULE_FIRMWARE("ti_5052.fw");
Chris Adams7df52312009-01-11 19:49:11 +0000288MODULE_FIRMWARE("mts_cdma.fw");
289MODULE_FIRMWARE("mts_gsm.fw");
290MODULE_FIRMWARE("mts_edge.fw");
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700291MODULE_FIRMWARE("mts_mt9234mu.fw");
292MODULE_FIRMWARE("mts_mt9234zba.fw");
David Woodhouse5f24e2d2008-05-30 18:49:51 +0300293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294module_param(debug, bool, S_IRUGO | S_IWUSR);
295MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297module_param(closing_wait, int, S_IRUGO | S_IWUSR);
Alan Coxa30fa792008-07-22 11:15:17 +0100298MODULE_PARM_DESC(closing_wait,
299 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100302MODULE_PARM_DESC(vendor_3410,
303 "Vendor ids for 3410 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100305MODULE_PARM_DESC(product_3410,
306 "Product ids for 3410 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100308MODULE_PARM_DESC(vendor_5052,
309 "Vendor ids for 5052 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100311MODULE_PARM_DESC(product_5052,
312 "Product ids for 5052 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
315
316
317/* Functions */
318
319static int __init ti_init(void)
320{
Chris Adams05a3d902009-01-11 19:48:53 +0000321 int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int ret;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* insert extra vendor and product ids */
Chris Adams05a3d902009-01-11 19:48:53 +0000325 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
Tobias Klauser52950ed2005-12-11 16:20:08 +0100326 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
Chris Adams05a3d902009-01-11 19:48:53 +0000327 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 ti_id_table_3410[j].idVendor = vendor_3410[i];
329 ti_id_table_3410[j].idProduct = product_3410[i];
330 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Chris Adams05a3d902009-01-11 19:48:53 +0000331 ti_id_table_combined[c].idVendor = vendor_3410[i];
332 ti_id_table_combined[c].idProduct = product_3410[i];
333 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Tobias Klauser52950ed2005-12-11 16:20:08 +0100335 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
Chris Adams05a3d902009-01-11 19:48:53 +0000336 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 ti_id_table_5052[j].idVendor = vendor_5052[i];
338 ti_id_table_5052[j].idProduct = product_5052[i];
339 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Chris Adams05a3d902009-01-11 19:48:53 +0000340 ti_id_table_combined[c].idVendor = vendor_5052[i];
341 ti_id_table_combined[c].idProduct = product_5052[i];
342 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 ret = usb_serial_register(&ti_1port_device);
346 if (ret)
347 goto failed_1port;
348 ret = usb_serial_register(&ti_2port_device);
349 if (ret)
350 goto failed_2port;
351
352 ret = usb_register(&ti_usb_driver);
353 if (ret)
354 goto failed_usb;
355
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700356 printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":"
357 TI_DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 return 0;
360
361failed_usb:
362 usb_serial_deregister(&ti_2port_device);
363failed_2port:
364 usb_serial_deregister(&ti_1port_device);
365failed_1port:
366 return ret;
367}
368
369
370static void __exit ti_exit(void)
371{
Ionut Nicub14de382010-12-28 22:21:08 +0200372 usb_deregister(&ti_usb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 usb_serial_deregister(&ti_1port_device);
374 usb_serial_deregister(&ti_2port_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377
378module_init(ti_init);
379module_exit(ti_exit);
380
381
382static int ti_startup(struct usb_serial *serial)
383{
384 struct ti_device *tdev;
385 struct ti_port *tport;
386 struct usb_device *dev = serial->dev;
387 int status;
388 int i;
389
390
391 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800392 __func__, le16_to_cpu(dev->descriptor.idProduct),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 dev->descriptor.bNumConfigurations,
394 dev->actconfig->desc.bConfigurationValue);
395
396 /* create device structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100397 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (tdev == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800399 dev_err(&dev->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return -ENOMEM;
401 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800402 mutex_init(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 tdev->td_serial = serial;
404 usb_set_serial_data(serial, tdev);
405
406 /* determine device type */
407 if (usb_match_id(serial->interface, ti_id_table_3410))
408 tdev->td_is_3410 = 1;
Alan Coxa30fa792008-07-22 11:15:17 +0100409 dbg("%s - device type is %s", __func__,
410 tdev->td_is_3410 ? "3410" : "5052");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* if we have only 1 configuration, download firmware */
413 if (dev->descriptor.bNumConfigurations == 1) {
Chris Adams05a3d902009-01-11 19:48:53 +0000414 if ((status = ti_download_firmware(tdev)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto free_tdev;
416
417 /* 3410 must be reset, 5052 resets itself */
418 if (tdev->td_is_3410) {
419 msleep_interruptible(100);
420 usb_reset_device(dev);
421 }
422
423 status = -ENODEV;
424 goto free_tdev;
Alan Coxa30fa792008-07-22 11:15:17 +0100425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Oliver Neukum413ba6f2008-12-16 12:25:55 +0100427 /* the second configuration must be set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
Oliver Neukum413ba6f2008-12-16 12:25:55 +0100429 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
430 status = status ? status : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto free_tdev;
432 }
433
434 /* set up port structures */
435 for (i = 0; i < serial->num_ports; ++i) {
Burman Yan7ac9da12006-11-22 20:54:38 +0200436 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (tport == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800438 dev_err(&dev->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 status = -ENOMEM;
440 goto free_tports;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 spin_lock_init(&tport->tp_lock);
Alan Coxa30fa792008-07-22 11:15:17 +0100443 tport->tp_uart_base_addr = (i == 0 ?
444 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 tport->tp_closing_wait = closing_wait;
446 init_waitqueue_head(&tport->tp_msr_wait);
447 init_waitqueue_head(&tport->tp_write_wait);
Johan Hovold074ef652010-05-19 00:01:35 +0200448 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE,
449 GFP_KERNEL)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800450 dev_err(&dev->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 kfree(tport);
452 status = -ENOMEM;
453 goto free_tports;
454 }
455 tport->tp_port = serial->port[i];
456 tport->tp_tdev = tdev;
457 usb_set_serial_port_data(serial->port[i], tport);
458 tport->tp_uart_mode = 0; /* default is RS232 */
459 }
Alan Coxa30fa792008-07-22 11:15:17 +0100460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462
463free_tports:
Alan Coxa30fa792008-07-22 11:15:17 +0100464 for (--i; i >= 0; --i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 tport = usb_get_serial_port_data(serial->port[i]);
Johan Hovold074ef652010-05-19 00:01:35 +0200466 kfifo_free(&tport->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 kfree(tport);
468 usb_set_serial_port_data(serial->port[i], NULL);
469 }
470free_tdev:
471 kfree(tdev);
472 usb_set_serial_data(serial, NULL);
473 return status;
474}
475
476
Alan Sternf9c99bb2009-06-02 11:53:55 -0400477static void ti_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 int i;
480 struct ti_device *tdev = usb_get_serial_data(serial);
481 struct ti_port *tport;
482
Harvey Harrison441b62c2008-03-03 16:08:34 -0800483 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Alan Coxa30fa792008-07-22 11:15:17 +0100485 for (i = 0; i < serial->num_ports; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 tport = usb_get_serial_port_data(serial->port[i]);
487 if (tport) {
Johan Hovold074ef652010-05-19 00:01:35 +0200488 kfifo_free(&tport->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 kfree(tport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 }
492
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700493 kfree(tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496
Alan Coxa509a7e2009-09-19 13:13:26 -0700497static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct ti_port *tport = usb_get_serial_port_data(port);
500 struct ti_device *tdev;
501 struct usb_device *dev;
502 struct urb *urb;
503 int port_number;
504 int status;
Alan Coxa30fa792008-07-22 11:15:17 +0100505 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
506 TI_PIPE_TIMEOUT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 (TI_TRANSFER_TIMEOUT << 2));
508
Harvey Harrison441b62c2008-03-03 16:08:34 -0800509 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (tport == NULL)
512 return -ENODEV;
513
514 dev = port->serial->dev;
515 tdev = tport->tp_tdev;
516
517 /* only one open on any port on a device at a time */
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800518 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -ERESTARTSYS;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 port_number = port->number - port->serial->minor;
522
523 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
524
525 tport->tp_msr = 0;
526 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
527
528 /* start interrupt urb the first time a port is opened on this device */
529 if (tdev->td_open_port_count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800530 dbg("%s - start interrupt in urb", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 urb = tdev->td_serial->port[0]->interrupt_in_urb;
532 if (!urb) {
Alan Coxa30fa792008-07-22 11:15:17 +0100533 dev_err(&port->dev, "%s - no interrupt urb\n",
534 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 status = -EINVAL;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800536 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 urb->complete = ti_interrupt_callback;
539 urb->context = tdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 status = usb_submit_urb(urb, GFP_KERNEL);
541 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100542 dev_err(&port->dev,
543 "%s - submit interrupt urb failed, %d\n",
544 __func__, status);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800545 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 }
548
Alan Cox95da3102008-07-22 11:09:07 +0100549 if (tty)
550 ti_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Harvey Harrison441b62c2008-03-03 16:08:34 -0800552 dbg("%s - sending TI_OPEN_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
554 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
555 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100556 dev_err(&port->dev, "%s - cannot send open command, %d\n",
557 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto unlink_int_urb;
559 }
560
Harvey Harrison441b62c2008-03-03 16:08:34 -0800561 dbg("%s - sending TI_START_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 status = ti_command_out_sync(tdev, TI_START_PORT,
563 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
564 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100565 dev_err(&port->dev, "%s - cannot send start command, %d\n",
566 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto unlink_int_urb;
568 }
569
Harvey Harrison441b62c2008-03-03 16:08:34 -0800570 dbg("%s - sending TI_PURGE_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
572 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
573 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100574 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
575 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto unlink_int_urb;
577 }
578 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
579 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
580 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100581 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
582 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto unlink_int_urb;
584 }
585
586 /* reset the data toggle on the bulk endpoints to work around bug in
587 * host controllers where things get out of sync some times */
588 usb_clear_halt(dev, port->write_urb->pipe);
589 usb_clear_halt(dev, port->read_urb->pipe);
590
Alan Cox95da3102008-07-22 11:09:07 +0100591 if (tty)
592 ti_set_termios(tty, port, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Harvey Harrison441b62c2008-03-03 16:08:34 -0800594 dbg("%s - sending TI_OPEN_PORT (2)", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
596 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
597 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100598 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
599 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto unlink_int_urb;
601 }
602
Harvey Harrison441b62c2008-03-03 16:08:34 -0800603 dbg("%s - sending TI_START_PORT (2)", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 status = ti_command_out_sync(tdev, TI_START_PORT,
605 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
606 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100607 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
608 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 goto unlink_int_urb;
610 }
611
612 /* start read urb */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800613 dbg("%s - start read urb", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 urb = port->read_urb;
615 if (!urb) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800616 dev_err(&port->dev, "%s - no read urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 status = -EINVAL;
618 goto unlink_int_urb;
619 }
620 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
621 urb->complete = ti_bulk_in_callback;
622 urb->context = tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 status = usb_submit_urb(urb, GFP_KERNEL);
624 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100625 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
626 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 goto unlink_int_urb;
628 }
629
630 tport->tp_is_open = 1;
631 ++tdev->td_open_port_count;
632
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800633 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635unlink_int_urb:
636 if (tdev->td_open_port_count == 0)
637 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800638release_lock:
639 mutex_unlock(&tdev->td_open_close_lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800640 dbg("%s - exit %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return status;
642}
643
644
Alan Cox335f8512009-06-11 12:26:29 +0100645static void ti_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct ti_device *tdev;
648 struct ti_port *tport;
649 int port_number;
650 int status;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800651 int do_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Harvey Harrison441b62c2008-03-03 16:08:34 -0800653 dbg("%s - port %d", __func__, port->number);
Alan Coxa30fa792008-07-22 11:15:17 +0100654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 tdev = usb_get_serial_data(port->serial);
656 tport = usb_get_serial_port_data(port);
657 if (tdev == NULL || tport == NULL)
658 return;
659
660 tport->tp_is_open = 0;
661
662 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
663
664 usb_kill_urb(port->read_urb);
665 usb_kill_urb(port->write_urb);
666 tport->tp_write_urb_in_use = 0;
667
668 port_number = port->number - port->serial->minor;
669
Harvey Harrison441b62c2008-03-03 16:08:34 -0800670 dbg("%s - sending TI_CLOSE_PORT", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
672 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
673 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100674 dev_err(&port->dev,
675 "%s - cannot send close port command, %d\n"
676 , __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800678 /* if mutex_lock is interrupted, continue anyway */
679 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 --tport->tp_tdev->td_open_port_count;
681 if (tport->tp_tdev->td_open_port_count <= 0) {
682 /* last port is closed, shut down interrupt urb */
683 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
684 tport->tp_tdev->td_open_port_count = 0;
685 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800686 if (do_unlock)
687 mutex_unlock(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Harvey Harrison441b62c2008-03-03 16:08:34 -0800689 dbg("%s - exit", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692
Alan Cox95da3102008-07-22 11:09:07 +0100693static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
694 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Harvey Harrison441b62c2008-03-03 16:08:34 -0800698 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800701 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
703 }
704
705 if (tport == NULL || !tport->tp_is_open)
706 return -ENODEV;
707
Johan Hovold074ef652010-05-19 00:01:35 +0200708 count = kfifo_in_locked(&tport->write_fifo, data, count,
709 &tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 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)
Alan Cox23198fd2009-07-20 16:05:27 +0100726 return 0;
Alan Coxa30fa792008-07-22 11:15:17 +0100727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold074ef652010-05-19 00:01:35 +0200729 room = kfifo_avail(&tport->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 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)
Alan Cox23198fd2009-07-20 16:05:27 +0100747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold074ef652010-05-19 00:01:35 +0200750 chars = kfifo_len(&tport->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 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
Alan Cox0bca1b92010-09-16 18:21:40 +0100793static int ti_get_icount(struct tty_struct *tty,
794 struct serial_icounter_struct *icount)
795{
796 struct usb_serial_port *port = tty->driver_data;
797 struct ti_port *tport = usb_get_serial_port_data(port);
798 struct async_icount cnow = tport->tp_icount;
799
800 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
801 __func__, port->number,
802 cnow.rx, cnow.tx);
803
804 icount->cts = cnow.cts;
805 icount->dsr = cnow.dsr;
806 icount->rng = cnow.rng;
807 icount->dcd = cnow.dcd;
808 icount->rx = cnow.rx;
809 icount->tx = cnow.tx;
810 icount->frame = cnow.frame;
811 icount->overrun = cnow.overrun;
812 icount->parity = cnow.parity;
813 icount->brk = cnow.brk;
814 icount->buf_overrun = cnow.buf_overrun;
815
816 return 0;
817}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Alan Cox00a0d0d2011-02-14 16:27:06 +0000819static int ti_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 unsigned int cmd, unsigned long arg)
821{
Alan Cox95da3102008-07-22 11:09:07 +0100822 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct ti_port *tport = usb_get_serial_port_data(port);
824 struct async_icount cnow;
825 struct async_icount cprev;
826
Harvey Harrison441b62c2008-03-03 16:08:34 -0800827 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (tport == NULL)
830 return -ENODEV;
831
832 switch (cmd) {
Alan Coxa30fa792008-07-22 11:15:17 +0100833 case TIOCGSERIAL:
834 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
835 return ti_get_serial_info(tport,
836 (struct serial_struct __user *)arg);
837 case TIOCSSERIAL:
838 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
Alan Cox4a90f092008-10-13 10:39:46 +0100839 return ti_set_serial_info(tty, tport,
840 (struct serial_struct __user *)arg);
Alan Coxa30fa792008-07-22 11:15:17 +0100841 case TIOCMIWAIT:
842 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
843 cprev = tport->tp_icount;
844 while (1) {
845 interruptible_sleep_on(&tport->tp_msr_wait);
846 if (signal_pending(current))
847 return -ERESTARTSYS;
848 cnow = tport->tp_icount;
849 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
850 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
851 return -EIO; /* no change => error */
852 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
853 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
854 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
855 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
856 return 0;
857 cprev = cnow;
858 }
859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -ENOIOCTLCMD;
862}
863
864
Alan Cox95da3102008-07-22 11:09:07 +0100865static void ti_set_termios(struct tty_struct *tty,
866 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct ti_uart_config *config;
Alan Coxa30fa792008-07-22 11:15:17 +0100870 tcflag_t cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int baud;
872 int status;
873 int port_number = port->number - port->serial->minor;
874 unsigned int mcr;
875
Harvey Harrison441b62c2008-03-03 16:08:34 -0800876 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 cflag = tty->termios->c_cflag;
879 iflag = tty->termios->c_iflag;
880
Harvey Harrison441b62c2008-03-03 16:08:34 -0800881 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
Alan Coxa30fa792008-07-22 11:15:17 +0100882 dbg("%s - old clfag %08x, old iflag %08x", __func__,
883 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (tport == NULL)
886 return;
887
888 config = kmalloc(sizeof(*config), GFP_KERNEL);
889 if (!config) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800890 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return;
892 }
893
894 config->wFlags = 0;
895
896 /* these flags must be set */
897 config->wFlags |= TI_UART_ENABLE_MS_INTS;
898 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
899 config->bUartMode = (__u8)(tport->tp_uart_mode);
900
901 switch (cflag & CSIZE) {
Alan Coxa30fa792008-07-22 11:15:17 +0100902 case CS5:
903 config->bDataBits = TI_UART_5_DATA_BITS;
904 break;
905 case CS6:
906 config->bDataBits = TI_UART_6_DATA_BITS;
907 break;
908 case CS7:
909 config->bDataBits = TI_UART_7_DATA_BITS;
910 break;
911 default:
912 case CS8:
913 config->bDataBits = TI_UART_8_DATA_BITS;
914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
Alan Cox537699e2008-01-03 17:03:11 +0000917 /* CMSPAR isn't supported by this driver */
918 tty->termios->c_cflag &= ~CMSPAR;
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (cflag & PARENB) {
921 if (cflag & PARODD) {
922 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
923 config->bParity = TI_UART_ODD_PARITY;
924 } else {
925 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
926 config->bParity = TI_UART_EVEN_PARITY;
927 }
928 } else {
929 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
Alan Coxa30fa792008-07-22 11:15:17 +0100930 config->bParity = TI_UART_NO_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
933 if (cflag & CSTOPB)
934 config->bStopBits = TI_UART_2_STOP_BITS;
935 else
936 config->bStopBits = TI_UART_1_STOP_BITS;
937
938 if (cflag & CRTSCTS) {
939 /* RTS flow control must be off to drop RTS for baud rate B0 */
940 if ((cflag & CBAUD) != B0)
941 config->wFlags |= TI_UART_ENABLE_RTS_IN;
942 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
943 } else {
944 tty->hw_stopped = 0;
945 ti_restart_read(tport, tty);
946 }
947
948 if (I_IXOFF(tty) || I_IXON(tty)) {
949 config->cXon = START_CHAR(tty);
950 config->cXoff = STOP_CHAR(tty);
951
952 if (I_IXOFF(tty))
953 config->wFlags |= TI_UART_ENABLE_X_IN;
954 else
955 ti_restart_read(tport, tty);
956
957 if (I_IXON(tty))
958 config->wFlags |= TI_UART_ENABLE_X_OUT;
959 }
960
961 baud = tty_get_baud_rate(tty);
Alan Cox537699e2008-01-03 17:03:11 +0000962 if (!baud)
963 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (tport->tp_tdev->td_is_3410)
965 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
966 else
967 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
968
Alan Cox537699e2008-01-03 17:03:11 +0000969 /* FIXME: Should calculate resulting baud here and report it back */
970 if ((cflag & CBAUD) != B0)
971 tty_encode_baud_rate(tty, baud, baud);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 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 -0800974 __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 -0700975
976 cpu_to_be16s(&config->wBaudRate);
977 cpu_to_be16s(&config->wFlags);
978
979 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
980 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
981 sizeof(*config));
982 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100983 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
984 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
987 mcr = tport->tp_shadow_mcr;
988 /* if baud rate is B0, clear RTS and DTR */
989 if ((cflag & CBAUD) == B0)
990 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
991 status = ti_set_mcr(tport, mcr);
992 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100993 dev_err(&port->dev,
994 "%s - cannot set modem control on port %d, %d\n",
995 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 kfree(config);
998}
999
1000
Alan Cox60b33c12011-02-14 16:26:14 +00001001static int ti_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Alan Cox95da3102008-07-22 11:09:07 +01001003 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct ti_port *tport = usb_get_serial_port_data(port);
1005 unsigned int result;
1006 unsigned int msr;
1007 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001008 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Harvey Harrison441b62c2008-03-03 16:08:34 -08001010 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (tport == NULL)
1013 return -ENODEV;
1014
Alan Cox04ca89d2008-02-20 21:41:40 +00001015 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 msr = tport->tp_msr;
1017 mcr = tport->tp_shadow_mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001018 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1021 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1022 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1023 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1024 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1025 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1026 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1027
Harvey Harrison441b62c2008-03-03 16:08:34 -08001028 dbg("%s - 0x%04X", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 return result;
1031}
1032
1033
Alan Cox20b9d172011-02-14 16:26:50 +00001034static int ti_tiocmset(struct tty_struct *tty,
1035 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Alan Cox95da3102008-07-22 11:09:07 +01001037 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct ti_port *tport = usb_get_serial_port_data(port);
1039 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001040 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Harvey Harrison441b62c2008-03-03 16:08:34 -08001042 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (tport == NULL)
1045 return -ENODEV;
1046
Alan Cox04ca89d2008-02-20 21:41:40 +00001047 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 mcr = tport->tp_shadow_mcr;
1049
1050 if (set & TIOCM_RTS)
1051 mcr |= TI_MCR_RTS;
1052 if (set & TIOCM_DTR)
1053 mcr |= TI_MCR_DTR;
1054 if (set & TIOCM_LOOP)
1055 mcr |= TI_MCR_LOOP;
1056
1057 if (clear & TIOCM_RTS)
1058 mcr &= ~TI_MCR_RTS;
1059 if (clear & TIOCM_DTR)
1060 mcr &= ~TI_MCR_DTR;
1061 if (clear & TIOCM_LOOP)
1062 mcr &= ~TI_MCR_LOOP;
Alan Cox04ca89d2008-02-20 21:41:40 +00001063 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 return ti_set_mcr(tport, mcr);
1066}
1067
1068
Alan Cox95da3102008-07-22 11:09:07 +01001069static void ti_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Alan Cox95da3102008-07-22 11:09:07 +01001071 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct ti_port *tport = usb_get_serial_port_data(port);
1073 int status;
1074
Harvey Harrison441b62c2008-03-03 16:08:34 -08001075 dbg("%s - state = %d", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (tport == NULL)
1078 return;
1079
1080 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1081
1082 status = ti_write_byte(tport->tp_tdev,
1083 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1084 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1085
1086 if (status)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001087 dbg("%s - error setting break, %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090
David Howells7d12e782006-10-05 14:55:46 +01001091static void ti_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Ming Leicdc97792008-02-24 18:41:47 +08001093 struct ti_device *tdev = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct usb_serial_port *port;
1095 struct usb_serial *serial = tdev->td_serial;
1096 struct ti_port *tport;
1097 struct device *dev = &urb->dev->dev;
1098 unsigned char *data = urb->transfer_buffer;
1099 int length = urb->actual_length;
1100 int port_number;
1101 int function;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001102 int status = urb->status;
1103 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 __u8 msr;
1105
Harvey Harrison441b62c2008-03-03 16:08:34 -08001106 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001108 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 case 0:
1110 break;
1111 case -ECONNRESET:
1112 case -ENOENT:
1113 case -ESHUTDOWN:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001114 dbg("%s - urb shutting down, %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 tdev->td_urb_error = 1;
1116 return;
1117 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001118 dev_err(dev, "%s - nonzero urb status, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001119 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 tdev->td_urb_error = 1;
1121 goto exit;
1122 }
1123
1124 if (length != 2) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001125 dbg("%s - bad packet size, %d", __func__, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 goto exit;
1127 }
1128
1129 if (data[0] == TI_CODE_HARDWARE_ERROR) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001130 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto exit;
1132 }
1133
1134 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1135 function = TI_GET_FUNC_FROM_CODE(data[0]);
1136
Alan Coxa30fa792008-07-22 11:15:17 +01001137 dbg("%s - port_number %d, function %d, data 0x%02X",
1138 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 if (port_number >= serial->num_ports) {
Alan Coxa30fa792008-07-22 11:15:17 +01001141 dev_err(dev, "%s - bad port number, %d\n",
1142 __func__, port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 goto exit;
1144 }
1145
1146 port = serial->port[port_number];
1147
1148 tport = usb_get_serial_port_data(port);
1149 if (!tport)
1150 goto exit;
1151
1152 switch (function) {
1153 case TI_CODE_DATA_ERROR:
Alan Coxa30fa792008-07-22 11:15:17 +01001154 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1155 __func__, port_number, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 break;
1157
1158 case TI_CODE_MODEM_STATUS:
1159 msr = data[1];
Harvey Harrison441b62c2008-03-03 16:08:34 -08001160 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 ti_handle_new_msr(tport, msr);
1162 break;
1163
1164 default:
Alan Coxa30fa792008-07-22 11:15:17 +01001165 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1166 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168 }
1169
1170exit:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001171 retval = usb_submit_urb(urb, GFP_ATOMIC);
1172 if (retval)
1173 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001174 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177
David Howells7d12e782006-10-05 14:55:46 +01001178static void ti_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Ming Leicdc97792008-02-24 18:41:47 +08001180 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 struct usb_serial_port *port = tport->tp_port;
1182 struct device *dev = &urb->dev->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001183 int status = urb->status;
1184 int retval = 0;
Alan Cox4a90f092008-10-13 10:39:46 +01001185 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Harvey Harrison441b62c2008-03-03 16:08:34 -08001187 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001189 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 case 0:
1191 break;
1192 case -ECONNRESET:
1193 case -ENOENT:
1194 case -ESHUTDOWN:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001195 dbg("%s - urb shutting down, %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 tport->tp_tdev->td_urb_error = 1;
1197 wake_up_interruptible(&tport->tp_write_wait);
1198 return;
1199 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001200 dev_err(dev, "%s - nonzero urb status, %d\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001201 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 tport->tp_tdev->td_urb_error = 1;
1203 wake_up_interruptible(&tport->tp_write_wait);
1204 }
1205
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001206 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto exit;
1208
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001209 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001210 dev_err(dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return;
1212 }
1213
Alan Cox4a90f092008-10-13 10:39:46 +01001214 tty = tty_port_tty_get(&port->port);
Alan Coxcf545092009-04-14 14:58:11 +01001215 if (tty) {
1216 if (urb->actual_length) {
1217 usb_serial_debug_data(debug, dev, __func__,
1218 urb->actual_length, urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Alan Coxcf545092009-04-14 14:58:11 +01001220 if (!tport->tp_is_open)
1221 dbg("%s - port closed, dropping data",
1222 __func__);
1223 else
1224 ti_recv(&urb->dev->dev, tty,
Alan Coxa30fa792008-07-22 11:15:17 +01001225 urb->transfer_buffer,
1226 urb->actual_length);
Alan Coxcf545092009-04-14 14:58:11 +01001227 spin_lock(&tport->tp_lock);
1228 tport->tp_icount.rx += urb->actual_length;
1229 spin_unlock(&tport->tp_lock);
1230 }
Alan Cox4a90f092008-10-13 10:39:46 +01001231 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
1234exit:
1235 /* continue to read unless stopping */
1236 spin_lock(&tport->tp_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001237 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001238 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001239 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 spin_unlock(&tport->tp_lock);
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001243 if (retval)
1244 dev_err(dev, "%s - resubmit read urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001245 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248
David Howells7d12e782006-10-05 14:55:46 +01001249static void ti_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Ming Leicdc97792008-02-24 18:41:47 +08001251 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 struct usb_serial_port *port = tport->tp_port;
1253 struct device *dev = &urb->dev->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001254 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Harvey Harrison441b62c2008-03-03 16:08:34 -08001256 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 tport->tp_write_urb_in_use = 0;
1259
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001260 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 case 0:
1262 break;
1263 case -ECONNRESET:
1264 case -ENOENT:
1265 case -ESHUTDOWN:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001266 dbg("%s - urb shutting down, %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 tport->tp_tdev->td_urb_error = 1;
1268 wake_up_interruptible(&tport->tp_write_wait);
1269 return;
1270 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001271 dev_err(dev, "%s - nonzero urb status, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001272 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 tport->tp_tdev->td_urb_error = 1;
1274 wake_up_interruptible(&tport->tp_write_wait);
1275 }
1276
1277 /* send any buffered data */
1278 ti_send(tport);
1279}
1280
1281
1282static void ti_recv(struct device *dev, struct tty_struct *tty,
1283 unsigned char *data, int length)
1284{
1285 int cnt;
1286
1287 do {
Alan Coxa108bfc2010-02-18 16:44:01 +00001288 cnt = tty_insert_flip_string(tty, data, length);
Alan Cox33f0f882006-01-09 20:54:13 -08001289 if (cnt < length) {
Alan Coxa30fa792008-07-22 11:15:17 +01001290 dev_err(dev, "%s - dropping data, %d bytes lost\n",
1291 __func__, length - cnt);
1292 if (cnt == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001293 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Alan Cox33f0f882006-01-09 20:54:13 -08001295 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 data += cnt;
1297 length -= cnt;
1298 } while (length > 0);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302
1303static void ti_send(struct ti_port *tport)
1304{
1305 int count, result;
1306 struct usb_serial_port *port = tport->tp_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001307 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 unsigned long flags;
1309
1310
Harvey Harrison441b62c2008-03-03 16:08:34 -08001311 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 spin_lock_irqsave(&tport->tp_lock, flags);
1314
Alan Cox4a90f092008-10-13 10:39:46 +01001315 if (tport->tp_write_urb_in_use)
1316 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Johan Hovold074ef652010-05-19 00:01:35 +02001318 count = kfifo_out(&tport->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 port->write_urb->transfer_buffer,
1320 port->bulk_out_size);
1321
Alan Cox4a90f092008-10-13 10:39:46 +01001322 if (count == 0)
1323 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 tport->tp_write_urb_in_use = 1;
1326
1327 spin_unlock_irqrestore(&tport->tp_lock, flags);
1328
Alan Coxa30fa792008-07-22 11:15:17 +01001329 usb_serial_debug_data(debug, &port->dev, __func__, count,
1330 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1333 usb_sndbulkpipe(port->serial->dev,
1334 port->bulk_out_endpointAddress),
1335 port->write_urb->transfer_buffer, count,
1336 ti_bulk_out_callback, tport);
1337
1338 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1339 if (result) {
Alan Coxa30fa792008-07-22 11:15:17 +01001340 dev_err(&port->dev, "%s - submit write urb failed, %d\n",
1341 __func__, result);
1342 tport->tp_write_urb_in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* TODO: reschedule ti_send */
1344 } else {
1345 spin_lock_irqsave(&tport->tp_lock, flags);
1346 tport->tp_icount.tx += count;
1347 spin_unlock_irqrestore(&tport->tp_lock, flags);
1348 }
1349
1350 /* more room in the buffer for new writes, wakeup */
1351 if (tty)
1352 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001353 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 wake_up_interruptible(&tport->tp_write_wait);
Alan Cox4a90f092008-10-13 10:39:46 +01001355 return;
1356unlock:
1357 spin_unlock_irqrestore(&tport->tp_lock, flags);
1358 tty_kref_put(tty);
1359 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362
1363static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1364{
Alan Cox04ca89d2008-02-20 21:41:40 +00001365 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int status;
1367
1368 status = ti_write_byte(tport->tp_tdev,
1369 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1370 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1371
Alan Cox04ca89d2008-02-20 21:41:40 +00001372 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (!status)
1374 tport->tp_shadow_mcr = mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001375 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 return status;
1378}
1379
1380
1381static int ti_get_lsr(struct ti_port *tport)
1382{
Alan Coxa30fa792008-07-22 11:15:17 +01001383 int size, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 struct ti_device *tdev = tport->tp_tdev;
1385 struct usb_serial_port *port = tport->tp_port;
1386 int port_number = port->number - port->serial->minor;
1387 struct ti_port_status *data;
1388
Harvey Harrison441b62c2008-03-03 16:08:34 -08001389 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 size = sizeof(struct ti_port_status);
1392 data = kmalloc(size, GFP_KERNEL);
1393 if (!data) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001394 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return -ENOMEM;
1396 }
1397
1398 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1399 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1400 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001401 dev_err(&port->dev,
1402 "%s - get port status command failed, %d\n",
1403 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 goto free_data;
1405 }
1406
Harvey Harrison441b62c2008-03-03 16:08:34 -08001407 dbg("%s - lsr 0x%02X", __func__, data->bLSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 tport->tp_lsr = data->bLSR;
1410
1411free_data:
1412 kfree(data);
1413 return status;
1414}
1415
1416
1417static int ti_get_serial_info(struct ti_port *tport,
1418 struct serial_struct __user *ret_arg)
1419{
1420 struct usb_serial_port *port = tport->tp_port;
1421 struct serial_struct ret_serial;
1422
1423 if (!ret_arg)
1424 return -EFAULT;
1425
1426 memset(&ret_serial, 0, sizeof(ret_serial));
1427
1428 ret_serial.type = PORT_16550A;
1429 ret_serial.line = port->serial->minor;
1430 ret_serial.port = port->number - port->serial->minor;
1431 ret_serial.flags = tport->tp_flags;
1432 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1433 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1434 ret_serial.closing_wait = tport->tp_closing_wait;
1435
1436 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1437 return -EFAULT;
1438
1439 return 0;
1440}
1441
1442
Alan Cox4a90f092008-10-13 10:39:46 +01001443static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 struct serial_struct __user *new_arg)
1445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 tport->tp_closing_wait = new_serial.closing_wait;
1453
1454 return 0;
1455}
1456
1457
1458static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1459{
1460 struct async_icount *icount;
1461 struct tty_struct *tty;
1462 unsigned long flags;
1463
Harvey Harrison441b62c2008-03-03 16:08:34 -08001464 dbg("%s - msr 0x%02X", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 if (msr & TI_MSR_DELTA_MASK) {
1467 spin_lock_irqsave(&tport->tp_lock, flags);
1468 icount = &tport->tp_icount;
1469 if (msr & TI_MSR_DELTA_CTS)
1470 icount->cts++;
1471 if (msr & TI_MSR_DELTA_DSR)
1472 icount->dsr++;
1473 if (msr & TI_MSR_DELTA_CD)
1474 icount->dcd++;
1475 if (msr & TI_MSR_DELTA_RI)
1476 icount->rng++;
1477 wake_up_interruptible(&tport->tp_msr_wait);
1478 spin_unlock_irqrestore(&tport->tp_lock, flags);
1479 }
1480
1481 tport->tp_msr = msr & TI_MSR_MASK;
1482
1483 /* handle CTS flow control */
Alan Cox4a90f092008-10-13 10:39:46 +01001484 tty = tty_port_tty_get(&tport->tp_port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (tty && C_CRTSCTS(tty)) {
1486 if (msr & TI_MSR_CTS) {
1487 tty->hw_stopped = 0;
1488 tty_wakeup(tty);
1489 } else {
1490 tty->hw_stopped = 1;
1491 }
1492 }
Alan Cox4a90f092008-10-13 10:39:46 +01001493 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496
1497static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1498{
1499 struct ti_device *tdev = tport->tp_tdev;
1500 struct usb_serial_port *port = tport->tp_port;
1501 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Harvey Harrison441b62c2008-03-03 16:08:34 -08001503 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Oliver Neukum0915f492008-01-23 12:28:45 +01001505 spin_lock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 /* wait for data to drain from the buffer */
1508 tdev->td_urb_error = 0;
1509 init_waitqueue_entry(&wait, current);
1510 add_wait_queue(&tport->tp_write_wait, &wait);
1511 for (;;) {
1512 set_current_state(TASK_INTERRUPTIBLE);
Johan Hovold074ef652010-05-19 00:01:35 +02001513 if (kfifo_len(&tport->write_fifo) == 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 || timeout == 0 || signal_pending(current)
1515 || tdev->td_urb_error
Oliver Neukum0915f492008-01-23 12:28:45 +01001516 || port->serial->disconnected) /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 break;
Oliver Neukum0915f492008-01-23 12:28:45 +01001518 spin_unlock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 timeout = schedule_timeout(timeout);
Oliver Neukum0915f492008-01-23 12:28:45 +01001520 spin_lock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522 set_current_state(TASK_RUNNING);
1523 remove_wait_queue(&tport->tp_write_wait, &wait);
1524
1525 /* flush any remaining data in the buffer */
1526 if (flush)
Johan Hovold074ef652010-05-19 00:01:35 +02001527 kfifo_reset_out(&tport->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Oliver Neukum0915f492008-01-23 12:28:45 +01001529 spin_unlock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Oliver Neukum0915f492008-01-23 12:28:45 +01001531 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /* wait for data to drain from the device */
1533 /* wait for empty tx register, plus 20 ms */
1534 timeout += jiffies;
1535 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1536 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1537 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
Oliver Neukum0915f492008-01-23 12:28:45 +01001538 && !port->serial->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (ti_get_lsr(tport))
1540 break;
Oliver Neukum0915f492008-01-23 12:28:45 +01001541 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 msleep_interruptible(20);
Oliver Neukum0915f492008-01-23 12:28:45 +01001543 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
Oliver Neukum0915f492008-01-23 12:28:45 +01001545 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548
1549static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1550{
1551 unsigned long flags;
1552
1553 spin_lock_irqsave(&tport->tp_lock, flags);
1554
1555 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1556 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1557
1558 spin_unlock_irqrestore(&tport->tp_lock, flags);
1559}
1560
1561
1562static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1563{
1564 struct urb *urb;
1565 int status = 0;
1566 unsigned long flags;
1567
1568 spin_lock_irqsave(&tport->tp_lock, flags);
1569
1570 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
Oliver Neukum944dc182007-05-07 08:33:18 +02001571 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 urb = tport->tp_port->read_urb;
Oliver Neukum944dc182007-05-07 08:33:18 +02001573 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 urb->complete = ti_bulk_in_callback;
1575 urb->context = tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 status = usb_submit_urb(urb, GFP_KERNEL);
Oliver Neukum944dc182007-05-07 08:33:18 +02001577 } else {
1578 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1579 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 return status;
1583}
1584
1585
1586static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1587 __u16 moduleid, __u16 value, __u8 *data, int size)
1588{
1589 int status;
1590
1591 status = usb_control_msg(tdev->td_serial->dev,
1592 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1593 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1594 value, moduleid, data, size, 1000);
1595
1596 if (status == size)
1597 status = 0;
1598
1599 if (status > 0)
1600 status = -ECOMM;
1601
1602 return status;
1603}
1604
1605
1606static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1607 __u16 moduleid, __u16 value, __u8 *data, int size)
1608{
1609 int status;
1610
1611 status = usb_control_msg(tdev->td_serial->dev,
1612 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1613 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1614 value, moduleid, data, size, 1000);
1615
1616 if (status == size)
1617 status = 0;
1618
1619 if (status > 0)
1620 status = -ECOMM;
1621
1622 return status;
1623}
1624
1625
1626static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
1627 __u8 mask, __u8 byte)
1628{
1629 int status;
1630 unsigned int size;
1631 struct ti_write_data_bytes *data;
1632 struct device *dev = &tdev->td_serial->dev->dev;
1633
Alan Coxa30fa792008-07-22 11:15:17 +01001634 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1635 __func__, addr, mask, byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 size = sizeof(struct ti_write_data_bytes) + 2;
1638 data = kmalloc(size, GFP_KERNEL);
1639 if (!data) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001640 dev_err(dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return -ENOMEM;
1642 }
1643
1644 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1645 data->bDataType = TI_RW_DATA_BYTE;
1646 data->bDataCounter = 1;
1647 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1648 data->wBaseAddrLo = cpu_to_be16(addr);
1649 data->bData[0] = mask;
1650 data->bData[1] = byte;
1651
1652 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1653 (__u8 *)data, size);
1654
1655 if (status < 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001656 dev_err(dev, "%s - failed, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 kfree(data);
1659
1660 return status;
1661}
1662
Alan Cox95da3102008-07-22 11:09:07 +01001663static int ti_do_download(struct usb_device *dev, int pipe,
1664 u8 *buffer, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 int pos;
Alan Cox95da3102008-07-22 11:09:07 +01001667 u8 cs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 struct ti_firmware_header *header;
Oliver Neukum87ea8c82009-06-30 09:44:24 +02001670 int status = 0;
Alan Cox95da3102008-07-22 11:09:07 +01001671 int len;
Alan Coxa30fa792008-07-22 11:15:17 +01001672
1673 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 cs = (__u8)(cs + buffer[pos]);
1675
1676 header = (struct ti_firmware_header *)buffer;
Alan Coxa30fa792008-07-22 11:15:17 +01001677 header->wLength = cpu_to_le16((__u16)(size
Alan Cox95da3102008-07-22 11:09:07 +01001678 - sizeof(struct ti_firmware_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 header->bCheckSum = cs;
1680
Harvey Harrison441b62c2008-03-03 16:08:34 -08001681 dbg("%s - downloading firmware", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01001682 for (pos = 0; pos < size; pos += done) {
1683 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1684 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1685 &done, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (status)
1687 break;
1688 }
Alan Cox95da3102008-07-22 11:09:07 +01001689 return status;
1690}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Chris Adams05a3d902009-01-11 19:48:53 +00001692static int ti_download_firmware(struct ti_device *tdev)
Alan Cox95da3102008-07-22 11:09:07 +01001693{
Chris Adams05a3d902009-01-11 19:48:53 +00001694 int status;
Alan Cox95da3102008-07-22 11:09:07 +01001695 int buffer_size;
1696 __u8 *buffer;
1697 struct usb_device *dev = tdev->td_serial->dev;
1698 unsigned int pipe = usb_sndbulkpipe(dev,
1699 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1700 const struct firmware *fw_p;
1701 char buf[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Alex Manoussakiscdc04832010-04-22 15:18:20 -07001703 dbg("%s\n", __func__);
Chris Adams05a3d902009-01-11 19:48:53 +00001704 /* try ID specific firmware first, then try generic firmware */
1705 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1706 dev->descriptor.idProduct);
1707 if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) {
Chris Adamscb7a7c62009-01-11 19:49:00 +00001708 buf[0] = '\0';
1709 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1710 switch (dev->descriptor.idProduct) {
1711 case MTS_CDMA_PRODUCT_ID:
1712 strcpy(buf, "mts_cdma.fw");
1713 break;
1714 case MTS_GSM_PRODUCT_ID:
1715 strcpy(buf, "mts_gsm.fw");
1716 break;
1717 case MTS_EDGE_PRODUCT_ID:
1718 strcpy(buf, "mts_edge.fw");
1719 break;
Alex Manoussakiscdc04832010-04-22 15:18:20 -07001720 case MTS_MT9234MU_PRODUCT_ID:
1721 strcpy(buf, "mts_mt9234mu.fw");
1722 break;
1723 case MTS_MT9234ZBA_PRODUCT_ID:
1724 strcpy(buf, "mts_mt9234zba.fw");
1725 break;
1726 case MTS_MT9234ZBAOLD_PRODUCT_ID:
1727 strcpy(buf, "mts_mt9234zba.fw");
1728 break; }
Chris Adamscb7a7c62009-01-11 19:49:00 +00001729 }
1730 if (buf[0] == '\0') {
1731 if (tdev->td_is_3410)
1732 strcpy(buf, "ti_3410.fw");
1733 else
1734 strcpy(buf, "ti_5052.fw");
1735 }
Chris Adams05a3d902009-01-11 19:48:53 +00001736 status = request_firmware(&fw_p, buf, &dev->dev);
1737 }
1738 if (status) {
Alan Cox95da3102008-07-22 11:09:07 +01001739 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1740 return -ENOENT;
1741 }
1742 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
Randy Dunlap75181f32010-04-15 11:38:56 -07001743 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
Jesper Juhl357f45d2011-06-13 22:50:41 +02001744 release_firmware(fw_p);
Alan Cox95da3102008-07-22 11:09:07 +01001745 return -ENOENT;
1746 }
1747
1748 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1749 buffer = kmalloc(buffer_size, GFP_KERNEL);
1750 if (buffer) {
1751 memcpy(buffer, fw_p->data, fw_p->size);
1752 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
Chris Adams2bcbe4c2008-09-10 14:11:38 -07001753 status = ti_do_download(dev, pipe, buffer, fw_p->size);
Alan Cox95da3102008-07-22 11:09:07 +01001754 kfree(buffer);
Chris Adams05a3d902009-01-11 19:48:53 +00001755 } else {
Alex Manoussakiscdc04832010-04-22 15:18:20 -07001756 dbg("%s ENOMEM\n", __func__);
Chris Adams05a3d902009-01-11 19:48:53 +00001757 status = -ENOMEM;
Alan Cox95da3102008-07-22 11:09:07 +01001758 }
1759 release_firmware(fw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001761 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1762 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return status;
1764 }
1765
Harvey Harrison441b62c2008-03-03 16:08:34 -08001766 dbg("%s - download successful", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 return 0;
1769}