blob: a49d2a746ccfdf38f0275f3bf836524b5daccc4f [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
44#define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
45
46#define TI_FIRMWARE_BUF_SIZE 16284
47
48#define TI_WRITE_BUF_SIZE 1024
49
50#define TI_TRANSFER_TIMEOUT 2
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
53
54/* supported setserial flags */
Oliver Neukum2400a2b2009-04-20 17:28:53 +020055#define TI_SET_SERIAL_FLAGS 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* read urb states */
58#define TI_READ_URB_RUNNING 0
59#define TI_READ_URB_STOPPING 1
60#define TI_READ_URB_STOPPED 2
61
62#define TI_EXTRA_VID_PID_COUNT 5
63
64
65/* Structures */
66
67struct ti_port {
68 int tp_is_open;
69 __u8 tp_msr;
70 __u8 tp_lsr;
71 __u8 tp_shadow_mcr;
72 __u8 tp_uart_mode; /* 232 or 485 modes */
73 unsigned int tp_uart_base_addr;
74 int tp_flags;
75 int tp_closing_wait;/* in .01 secs */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 wait_queue_head_t tp_write_wait;
77 struct ti_device *tp_tdev;
78 struct usb_serial_port *tp_port;
79 spinlock_t tp_lock;
80 int tp_read_urb_state;
81 int tp_write_urb_in_use;
Johan Hovold074ef652010-05-19 00:01:35 +020082 struct kfifo write_fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
85struct ti_device {
Matthias Kaehlcke9da00682007-11-21 15:13:16 -080086 struct mutex td_open_close_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 int td_open_port_count;
88 struct usb_serial *td_serial;
89 int td_is_3410;
90 int td_urb_error;
91};
92
93
94/* Function Declarations */
95
96static int ti_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040097static void ti_release(struct usb_serial *serial);
Johan Hovold51ef8472012-10-17 16:31:35 +020098static int ti_port_probe(struct usb_serial_port *port);
99static int ti_port_remove(struct usb_serial_port *port);
Alan Coxa509a7e2009-09-19 13:13:26 -0700100static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100101static void ti_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +0100102static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Coxa30fa792008-07-22 11:15:17 +0100103 const unsigned char *data, int count);
Alan Cox95da3102008-07-22 11:09:07 +0100104static int ti_write_room(struct tty_struct *tty);
105static int ti_chars_in_buffer(struct tty_struct *tty);
106static void ti_throttle(struct tty_struct *tty);
107static void ti_unthrottle(struct tty_struct *tty);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000108static int ti_ioctl(struct tty_struct *tty,
Alan Coxa30fa792008-07-22 11:15:17 +0100109 unsigned int cmd, unsigned long arg);
110static void ti_set_termios(struct tty_struct *tty,
111 struct usb_serial_port *port, struct ktermios *old_termios);
Alan Cox60b33c12011-02-14 16:26:14 +0000112static int ti_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000113static int ti_tiocmset(struct tty_struct *tty,
Alan Coxa30fa792008-07-22 11:15:17 +0100114 unsigned int set, unsigned int clear);
Alan Cox95da3102008-07-22 11:09:07 +0100115static void ti_break(struct tty_struct *tty, int break_state);
David Howells7d12e782006-10-05 14:55:46 +0100116static void ti_interrupt_callback(struct urb *urb);
117static void ti_bulk_in_callback(struct urb *urb);
118static void ti_bulk_out_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jiri Slaby2e124b42013-01-03 15:53:06 +0100120static void ti_recv(struct usb_serial_port *port, unsigned char *data,
121 int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static void ti_send(struct ti_port *tport);
123static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
124static int ti_get_lsr(struct ti_port *tport);
125static int ti_get_serial_info(struct ti_port *tport,
126 struct serial_struct __user *ret_arg);
Alan Cox4a90f092008-10-13 10:39:46 +0100127static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct serial_struct __user *new_arg);
129static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
130
Johan Hovold113ec312013-04-18 17:33:19 +0200131static void ti_drain(struct ti_port *tport, unsigned long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
134static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
135
136static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
137 __u16 moduleid, __u16 value, __u8 *data, int size);
138static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
139 __u16 moduleid, __u16 value, __u8 *data, int size);
140
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700141static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
142 unsigned long addr, __u8 mask, __u8 byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Chris Adams05a3d902009-01-11 19:48:53 +0000144static int ti_download_firmware(struct ti_device *tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/* Data */
148
149/* module parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
151static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100152static unsigned int vendor_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100154static unsigned int product_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100156static unsigned int vendor_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100158static unsigned int product_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/* supported devices */
161/* the array dimension is the number of default entries plus */
162/* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
163/* null entry */
Darren Hart975dc332012-05-11 13:56:57 -0700164static struct usb_device_id ti_id_table_3410[15+TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000166 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Chris Adamscb7a7c62009-01-11 19:49:00 +0000167 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
168 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
169 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
170 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
171 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700172 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
173 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
174 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
Oliver Neukum1a1fab512009-01-12 13:31:16 +0100175 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
Oliver Neukum97dcf042009-02-04 16:38:33 +0100176 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
177 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
Andrew Lunn7fd25702012-02-20 09:31:57 +0100178 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
Darren Hart975dc332012-05-11 13:56:57 -0700179 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
Oliver Neukum97dcf042009-02-04 16:38:33 +0100182static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
184 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
185 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
186 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
187};
188
Darren Hart975dc332012-05-11 13:56:57 -0700189static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000191 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Chris Adamscb7a7c62009-01-11 19:49:00 +0000192 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
193 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
194 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
195 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
196 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700197 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
198 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
199 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
201 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
202 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
203 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
Oliver Neukum1a1fab512009-01-12 13:31:16 +0100204 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
Oliver Neukum97dcf042009-02-04 16:38:33 +0100205 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
206 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
Andrew Lunn7fd25702012-02-20 09:31:57 +0100207 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
Darren Hart975dc332012-05-11 13:56:57 -0700208 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 { }
210};
211
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700212static struct usb_serial_driver ti_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700213 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700214 .owner = THIS_MODULE,
215 .name = "ti_usb_3410_5052_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700216 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700217 .description = "TI USB 3410 1 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .id_table = ti_id_table_3410,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 .num_ports = 1,
220 .attach = ti_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400221 .release = ti_release,
Johan Hovold51ef8472012-10-17 16:31:35 +0200222 .port_probe = ti_port_probe,
223 .port_remove = ti_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .open = ti_open,
225 .close = ti_close,
226 .write = ti_write,
227 .write_room = ti_write_room,
228 .chars_in_buffer = ti_chars_in_buffer,
229 .throttle = ti_throttle,
230 .unthrottle = ti_unthrottle,
231 .ioctl = ti_ioctl,
232 .set_termios = ti_set_termios,
233 .tiocmget = ti_tiocmget,
234 .tiocmset = ti_tiocmset,
Johan Hovold6c75e262013-03-21 12:37:34 +0100235 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovold783ca352013-03-21 12:37:33 +0100236 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .break_ctl = ti_break,
238 .read_int_callback = ti_interrupt_callback,
239 .read_bulk_callback = ti_bulk_in_callback,
240 .write_bulk_callback = ti_bulk_out_callback,
241};
242
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700243static struct usb_serial_driver ti_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700244 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700245 .owner = THIS_MODULE,
246 .name = "ti_usb_3410_5052_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700247 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700248 .description = "TI USB 5052 2 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .id_table = ti_id_table_5052,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 .num_ports = 2,
251 .attach = ti_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400252 .release = ti_release,
Johan Hovold51ef8472012-10-17 16:31:35 +0200253 .port_probe = ti_port_probe,
254 .port_remove = ti_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .open = ti_open,
256 .close = ti_close,
257 .write = ti_write,
258 .write_room = ti_write_room,
259 .chars_in_buffer = ti_chars_in_buffer,
260 .throttle = ti_throttle,
261 .unthrottle = ti_unthrottle,
262 .ioctl = ti_ioctl,
263 .set_termios = ti_set_termios,
264 .tiocmget = ti_tiocmget,
265 .tiocmset = ti_tiocmset,
Johan Hovold6c75e262013-03-21 12:37:34 +0100266 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovold783ca352013-03-21 12:37:33 +0100267 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .break_ctl = ti_break,
269 .read_int_callback = ti_interrupt_callback,
270 .read_bulk_callback = ti_bulk_in_callback,
271 .write_bulk_callback = ti_bulk_out_callback,
272};
273
Alan Stern29618e92012-02-23 14:57:32 -0500274static struct usb_serial_driver * const serial_drivers[] = {
275 &ti_1port_device, &ti_2port_device, NULL
276};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/* Module */
279
280MODULE_AUTHOR(TI_DRIVER_AUTHOR);
281MODULE_DESCRIPTION(TI_DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282MODULE_LICENSE("GPL");
283
David Woodhouse5f24e2d2008-05-30 18:49:51 +0300284MODULE_FIRMWARE("ti_3410.fw");
285MODULE_FIRMWARE("ti_5052.fw");
Chris Adams7df52312009-01-11 19:49:11 +0000286MODULE_FIRMWARE("mts_cdma.fw");
287MODULE_FIRMWARE("mts_gsm.fw");
288MODULE_FIRMWARE("mts_edge.fw");
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700289MODULE_FIRMWARE("mts_mt9234mu.fw");
290MODULE_FIRMWARE("mts_mt9234zba.fw");
David Woodhouse5f24e2d2008-05-30 18:49:51 +0300291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292module_param(closing_wait, int, S_IRUGO | S_IWUSR);
Alan Coxa30fa792008-07-22 11:15:17 +0100293MODULE_PARM_DESC(closing_wait,
294 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100297MODULE_PARM_DESC(vendor_3410,
298 "Vendor ids for 3410 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100300MODULE_PARM_DESC(product_3410,
301 "Product ids for 3410 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100303MODULE_PARM_DESC(vendor_5052,
304 "Vendor ids for 5052 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100306MODULE_PARM_DESC(product_5052,
307 "Product ids for 5052 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
310
311
312/* Functions */
313
314static int __init ti_init(void)
315{
Chris Adams05a3d902009-01-11 19:48:53 +0000316 int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* insert extra vendor and product ids */
Chris Adams05a3d902009-01-11 19:48:53 +0000319 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
Tobias Klauser52950ed2005-12-11 16:20:08 +0100320 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
Chris Adams05a3d902009-01-11 19:48:53 +0000321 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 ti_id_table_3410[j].idVendor = vendor_3410[i];
323 ti_id_table_3410[j].idProduct = product_3410[i];
324 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Chris Adams05a3d902009-01-11 19:48:53 +0000325 ti_id_table_combined[c].idVendor = vendor_3410[i];
326 ti_id_table_combined[c].idProduct = product_3410[i];
327 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Tobias Klauser52950ed2005-12-11 16:20:08 +0100329 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
Chris Adams05a3d902009-01-11 19:48:53 +0000330 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ti_id_table_5052[j].idVendor = vendor_5052[i];
332 ti_id_table_5052[j].idProduct = product_5052[i];
333 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Chris Adams05a3d902009-01-11 19:48:53 +0000334 ti_id_table_combined[c].idVendor = vendor_5052[i];
335 ti_id_table_combined[c].idProduct = product_5052[i];
336 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
Greg Kroah-Hartmanf378dfe2012-09-18 17:09:00 +0100339 return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ti_id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static void __exit ti_exit(void)
343{
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700344 usb_serial_deregister_drivers(serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347module_init(ti_init);
348module_exit(ti_exit);
349
350
351static int ti_startup(struct usb_serial *serial)
352{
353 struct ti_device *tdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct usb_device *dev = serial->dev;
355 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700357 dev_dbg(&dev->dev,
358 "%s - product 0x%4X, num configurations %d, configuration value %d",
359 __func__, le16_to_cpu(dev->descriptor.idProduct),
360 dev->descriptor.bNumConfigurations,
361 dev->actconfig->desc.bConfigurationValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* create device structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100364 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (tdev == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800366 dev_err(&dev->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -ENOMEM;
368 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800369 mutex_init(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 tdev->td_serial = serial;
371 usb_set_serial_data(serial, tdev);
372
373 /* determine device type */
374 if (usb_match_id(serial->interface, ti_id_table_3410))
375 tdev->td_is_3410 = 1;
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700376 dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
377 tdev->td_is_3410 ? "3410" : "5052");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* if we have only 1 configuration, download firmware */
380 if (dev->descriptor.bNumConfigurations == 1) {
Adhir Ramjiawan72b27a02012-04-09 14:01:38 +0200381 status = ti_download_firmware(tdev);
382
383 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 goto free_tdev;
385
386 /* 3410 must be reset, 5052 resets itself */
387 if (tdev->td_is_3410) {
388 msleep_interruptible(100);
389 usb_reset_device(dev);
390 }
391
392 status = -ENODEV;
393 goto free_tdev;
Alan Coxa30fa792008-07-22 11:15:17 +0100394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Oliver Neukum413ba6f2008-12-16 12:25:55 +0100396 /* the second configuration must be set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
Oliver Neukum413ba6f2008-12-16 12:25:55 +0100398 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
399 status = status ? status : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto free_tdev;
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405free_tdev:
406 kfree(tdev);
407 usb_set_serial_data(serial, NULL);
408 return status;
409}
410
411
Alan Sternf9c99bb2009-06-02 11:53:55 -0400412static void ti_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct ti_device *tdev = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700416 kfree(tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Johan Hovold51ef8472012-10-17 16:31:35 +0200419static int ti_port_probe(struct usb_serial_port *port)
420{
421 struct ti_port *tport;
422
423 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
424 if (!tport)
425 return -ENOMEM;
426
427 spin_lock_init(&tport->tp_lock);
428 if (port == port->serial->port[0])
429 tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
430 else
431 tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
432 tport->tp_closing_wait = closing_wait;
Johan Hovold51ef8472012-10-17 16:31:35 +0200433 init_waitqueue_head(&tport->tp_write_wait);
434 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE, GFP_KERNEL)) {
435 kfree(tport);
436 return -ENOMEM;
437 }
438 tport->tp_port = port;
439 tport->tp_tdev = usb_get_serial_data(port->serial);
440 tport->tp_uart_mode = 0; /* default is RS232 */
441
442 usb_set_serial_port_data(port, tport);
443
444 return 0;
445}
446
447static int ti_port_remove(struct usb_serial_port *port)
448{
449 struct ti_port *tport;
450
451 tport = usb_get_serial_port_data(port);
452 kfifo_free(&tport->write_fifo);
453 kfree(tport);
454
455 return 0;
456}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Alan Coxa509a7e2009-09-19 13:13:26 -0700458static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct ti_port *tport = usb_get_serial_port_data(port);
461 struct ti_device *tdev;
462 struct usb_device *dev;
463 struct urb *urb;
464 int port_number;
465 int status;
Alan Coxa30fa792008-07-22 11:15:17 +0100466 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
467 TI_PIPE_TIMEOUT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 (TI_TRANSFER_TIMEOUT << 2));
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (tport == NULL)
471 return -ENODEV;
472
473 dev = port->serial->dev;
474 tdev = tport->tp_tdev;
475
476 /* only one open on any port on a device at a time */
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800477 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -ERESTARTSYS;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 port_number = port->number - port->serial->minor;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 tport->tp_msr = 0;
483 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
484
485 /* start interrupt urb the first time a port is opened on this device */
486 if (tdev->td_open_port_count == 0) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700487 dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 urb = tdev->td_serial->port[0]->interrupt_in_urb;
489 if (!urb) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700490 dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 status = -EINVAL;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800492 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 urb->context = tdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 status = usb_submit_urb(urb, GFP_KERNEL);
496 if (status) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700497 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800498 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 }
501
Alan Cox95da3102008-07-22 11:09:07 +0100502 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +0100503 ti_set_termios(tty, port, &tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700505 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
507 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
508 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100509 dev_err(&port->dev, "%s - cannot send open command, %d\n",
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700510 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto unlink_int_urb;
512 }
513
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700514 dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 status = ti_command_out_sync(tdev, TI_START_PORT,
516 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
517 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100518 dev_err(&port->dev, "%s - cannot send start command, %d\n",
519 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 goto unlink_int_urb;
521 }
522
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700523 dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
525 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
526 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100527 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
528 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto unlink_int_urb;
530 }
531 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
532 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
533 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100534 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
535 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto unlink_int_urb;
537 }
538
539 /* reset the data toggle on the bulk endpoints to work around bug in
540 * host controllers where things get out of sync some times */
541 usb_clear_halt(dev, port->write_urb->pipe);
542 usb_clear_halt(dev, port->read_urb->pipe);
543
Alan Cox95da3102008-07-22 11:09:07 +0100544 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +0100545 ti_set_termios(tty, port, &tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700547 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __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 (2), %d\n",
552 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto unlink_int_urb;
554 }
555
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700556 dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __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 (2), %d\n",
561 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto unlink_int_urb;
563 }
564
565 /* start read urb */
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700566 dev_dbg(&port->dev, "%s - start read urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 urb = port->read_urb;
568 if (!urb) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800569 dev_err(&port->dev, "%s - no read urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 status = -EINVAL;
571 goto unlink_int_urb;
572 }
573 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 urb->context = tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 status = usb_submit_urb(urb, GFP_KERNEL);
576 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100577 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
578 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto unlink_int_urb;
580 }
581
582 tport->tp_is_open = 1;
583 ++tdev->td_open_port_count;
584
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800585 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587unlink_int_urb:
588 if (tdev->td_open_port_count == 0)
589 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800590release_lock:
591 mutex_unlock(&tdev->td_open_close_lock);
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700592 dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return status;
594}
595
596
Alan Cox335f8512009-06-11 12:26:29 +0100597static void ti_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct ti_device *tdev;
600 struct ti_port *tport;
601 int port_number;
602 int status;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800603 int do_unlock;
Johan Hovold113ec312013-04-18 17:33:19 +0200604 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 tdev = usb_get_serial_data(port->serial);
607 tport = usb_get_serial_port_data(port);
608 if (tdev == NULL || tport == NULL)
609 return;
610
611 tport->tp_is_open = 0;
612
Johan Hovold113ec312013-04-18 17:33:19 +0200613 ti_drain(tport, (tport->tp_closing_wait*HZ)/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 usb_kill_urb(port->read_urb);
616 usb_kill_urb(port->write_urb);
617 tport->tp_write_urb_in_use = 0;
Johan Hovold113ec312013-04-18 17:33:19 +0200618 spin_lock_irqsave(&tport->tp_lock, flags);
619 kfifo_reset_out(&tport->write_fifo);
620 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 port_number = port->number - port->serial->minor;
623
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700624 dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
626 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
627 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100628 dev_err(&port->dev,
629 "%s - cannot send close port command, %d\n"
630 , __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800632 /* if mutex_lock is interrupted, continue anyway */
633 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 --tport->tp_tdev->td_open_port_count;
635 if (tport->tp_tdev->td_open_port_count <= 0) {
636 /* last port is closed, shut down interrupt urb */
637 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
638 tport->tp_tdev->td_open_port_count = 0;
639 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800640 if (do_unlock)
641 mutex_unlock(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644
Alan Cox95da3102008-07-22 11:09:07 +0100645static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
646 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (count == 0) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700651 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return 0;
653 }
654
655 if (tport == NULL || !tport->tp_is_open)
656 return -ENODEV;
657
Johan Hovold074ef652010-05-19 00:01:35 +0200658 count = kfifo_in_locked(&tport->write_fifo, data, count,
659 &tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 ti_send(tport);
661
662 return count;
663}
664
665
Alan Cox95da3102008-07-22 11:09:07 +0100666static int ti_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Alan Cox95da3102008-07-22 11:09:07 +0100668 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct ti_port *tport = usb_get_serial_port_data(port);
670 int room = 0;
671 unsigned long flags;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (tport == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +0100674 return 0;
Alan Coxa30fa792008-07-22 11:15:17 +0100675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold074ef652010-05-19 00:01:35 +0200677 room = kfifo_avail(&tport->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 spin_unlock_irqrestore(&tport->tp_lock, flags);
679
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700680 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return room;
682}
683
684
Alan Cox95da3102008-07-22 11:09:07 +0100685static int ti_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Alan Cox95da3102008-07-22 11:09:07 +0100687 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct ti_port *tport = usb_get_serial_port_data(port);
689 int chars = 0;
690 unsigned long flags;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (tport == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +0100693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold074ef652010-05-19 00:01:35 +0200696 chars = kfifo_len(&tport->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 spin_unlock_irqrestore(&tport->tp_lock, flags);
698
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700699 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return chars;
701}
702
703
Alan Cox95da3102008-07-22 11:09:07 +0100704static void ti_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Alan Cox95da3102008-07-22 11:09:07 +0100706 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (tport == NULL)
710 return;
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (I_IXOFF(tty) || C_CRTSCTS(tty))
713 ti_stop_read(tport, tty);
714
715}
716
717
Alan Cox95da3102008-07-22 11:09:07 +0100718static void ti_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Alan Cox95da3102008-07-22 11:09:07 +0100720 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int status;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (tport == NULL)
725 return;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
728 status = ti_restart_read(tport, tty);
729 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100730 dev_err(&port->dev, "%s - cannot restart read, %d\n",
731 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733}
734
Alan Cox00a0d0d2011-02-14 16:27:06 +0000735static int ti_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 unsigned int cmd, unsigned long arg)
737{
Alan Cox95da3102008-07-22 11:09:07 +0100738 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700741 dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (tport == NULL)
744 return -ENODEV;
745
746 switch (cmd) {
Alan Coxa30fa792008-07-22 11:15:17 +0100747 case TIOCGSERIAL:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700748 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
Alan Coxa30fa792008-07-22 11:15:17 +0100749 return ti_get_serial_info(tport,
750 (struct serial_struct __user *)arg);
751 case TIOCSSERIAL:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700752 dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
Alan Cox4a90f092008-10-13 10:39:46 +0100753 return ti_set_serial_info(tty, tport,
754 (struct serial_struct __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -ENOIOCTLCMD;
757}
758
759
Alan Cox95da3102008-07-22 11:09:07 +0100760static void ti_set_termios(struct tty_struct *tty,
761 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
763 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct ti_uart_config *config;
Alan Coxa30fa792008-07-22 11:15:17 +0100765 tcflag_t cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int baud;
767 int status;
768 int port_number = port->number - port->serial->minor;
769 unsigned int mcr;
770
Alan Coxadc8d742012-07-14 15:31:47 +0100771 cflag = tty->termios.c_cflag;
772 iflag = tty->termios.c_iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700774 dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
775 dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
776 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 if (tport == NULL)
779 return;
780
781 config = kmalloc(sizeof(*config), GFP_KERNEL);
782 if (!config) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800783 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return;
785 }
786
787 config->wFlags = 0;
788
789 /* these flags must be set */
790 config->wFlags |= TI_UART_ENABLE_MS_INTS;
791 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
792 config->bUartMode = (__u8)(tport->tp_uart_mode);
793
794 switch (cflag & CSIZE) {
Alan Coxa30fa792008-07-22 11:15:17 +0100795 case CS5:
796 config->bDataBits = TI_UART_5_DATA_BITS;
797 break;
798 case CS6:
799 config->bDataBits = TI_UART_6_DATA_BITS;
800 break;
801 case CS7:
802 config->bDataBits = TI_UART_7_DATA_BITS;
803 break;
804 default:
805 case CS8:
806 config->bDataBits = TI_UART_8_DATA_BITS;
807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
Alan Cox537699e2008-01-03 17:03:11 +0000810 /* CMSPAR isn't supported by this driver */
Alan Coxadc8d742012-07-14 15:31:47 +0100811 tty->termios.c_cflag &= ~CMSPAR;
Alan Cox537699e2008-01-03 17:03:11 +0000812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (cflag & PARENB) {
814 if (cflag & PARODD) {
815 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
816 config->bParity = TI_UART_ODD_PARITY;
817 } else {
818 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
819 config->bParity = TI_UART_EVEN_PARITY;
820 }
821 } else {
822 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
Alan Coxa30fa792008-07-22 11:15:17 +0100823 config->bParity = TI_UART_NO_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
826 if (cflag & CSTOPB)
827 config->bStopBits = TI_UART_2_STOP_BITS;
828 else
829 config->bStopBits = TI_UART_1_STOP_BITS;
830
831 if (cflag & CRTSCTS) {
832 /* RTS flow control must be off to drop RTS for baud rate B0 */
833 if ((cflag & CBAUD) != B0)
834 config->wFlags |= TI_UART_ENABLE_RTS_IN;
835 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
836 } else {
837 tty->hw_stopped = 0;
838 ti_restart_read(tport, tty);
839 }
840
841 if (I_IXOFF(tty) || I_IXON(tty)) {
842 config->cXon = START_CHAR(tty);
843 config->cXoff = STOP_CHAR(tty);
844
845 if (I_IXOFF(tty))
846 config->wFlags |= TI_UART_ENABLE_X_IN;
847 else
848 ti_restart_read(tport, tty);
849
850 if (I_IXON(tty))
851 config->wFlags |= TI_UART_ENABLE_X_OUT;
852 }
853
854 baud = tty_get_baud_rate(tty);
Alan Cox537699e2008-01-03 17:03:11 +0000855 if (!baud)
856 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (tport->tp_tdev->td_is_3410)
858 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
859 else
860 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
861
Alan Cox537699e2008-01-03 17:03:11 +0000862 /* FIXME: Should calculate resulting baud here and report it back */
863 if ((cflag & CBAUD) != B0)
864 tty_encode_baud_rate(tty, baud, baud);
865
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700866 dev_dbg(&port->dev,
867 "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
868 __func__, baud, config->wBaudRate, config->wFlags,
869 config->bDataBits, config->bParity, config->bStopBits,
870 config->cXon, config->cXoff, config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 cpu_to_be16s(&config->wBaudRate);
873 cpu_to_be16s(&config->wFlags);
874
875 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
876 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
877 sizeof(*config));
878 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100879 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
880 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
883 mcr = tport->tp_shadow_mcr;
884 /* if baud rate is B0, clear RTS and DTR */
885 if ((cflag & CBAUD) == B0)
886 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
887 status = ti_set_mcr(tport, mcr);
888 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100889 dev_err(&port->dev,
890 "%s - cannot set modem control on port %d, %d\n",
891 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 kfree(config);
894}
895
896
Alan Cox60b33c12011-02-14 16:26:14 +0000897static int ti_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Alan Cox95da3102008-07-22 11:09:07 +0100899 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct ti_port *tport = usb_get_serial_port_data(port);
901 unsigned int result;
902 unsigned int msr;
903 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000904 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (tport == NULL)
907 return -ENODEV;
908
Alan Cox04ca89d2008-02-20 21:41:40 +0000909 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 msr = tport->tp_msr;
911 mcr = tport->tp_shadow_mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000912 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
915 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
916 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
917 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
918 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
919 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
920 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
921
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700922 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 return result;
925}
926
927
Alan Cox20b9d172011-02-14 16:26:50 +0000928static int ti_tiocmset(struct tty_struct *tty,
929 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Alan Cox95da3102008-07-22 11:09:07 +0100931 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 struct ti_port *tport = usb_get_serial_port_data(port);
933 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000934 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (tport == NULL)
937 return -ENODEV;
938
Alan Cox04ca89d2008-02-20 21:41:40 +0000939 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 mcr = tport->tp_shadow_mcr;
941
942 if (set & TIOCM_RTS)
943 mcr |= TI_MCR_RTS;
944 if (set & TIOCM_DTR)
945 mcr |= TI_MCR_DTR;
946 if (set & TIOCM_LOOP)
947 mcr |= TI_MCR_LOOP;
948
949 if (clear & TIOCM_RTS)
950 mcr &= ~TI_MCR_RTS;
951 if (clear & TIOCM_DTR)
952 mcr &= ~TI_MCR_DTR;
953 if (clear & TIOCM_LOOP)
954 mcr &= ~TI_MCR_LOOP;
Alan Cox04ca89d2008-02-20 21:41:40 +0000955 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 return ti_set_mcr(tport, mcr);
958}
959
960
Alan Cox95da3102008-07-22 11:09:07 +0100961static void ti_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Alan Cox95da3102008-07-22 11:09:07 +0100963 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 struct ti_port *tport = usb_get_serial_port_data(port);
965 int status;
966
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700967 dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (tport == NULL)
970 return;
971
Johan Hovold113ec312013-04-18 17:33:19 +0200972 ti_drain(tport, (tport->tp_closing_wait*HZ)/100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700974 status = ti_write_byte(port, tport->tp_tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
976 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
977
978 if (status)
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700979 dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982
David Howells7d12e782006-10-05 14:55:46 +0100983static void ti_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Ming Leicdc97792008-02-24 18:41:47 +0800985 struct ti_device *tdev = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct usb_serial_port *port;
987 struct usb_serial *serial = tdev->td_serial;
988 struct ti_port *tport;
989 struct device *dev = &urb->dev->dev;
990 unsigned char *data = urb->transfer_buffer;
991 int length = urb->actual_length;
992 int port_number;
993 int function;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -0700994 int status = urb->status;
995 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 __u8 msr;
997
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -0700998 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 case 0:
1000 break;
1001 case -ECONNRESET:
1002 case -ENOENT:
1003 case -ESHUTDOWN:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001004 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 tdev->td_urb_error = 1;
1006 return;
1007 default:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001008 dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 tdev->td_urb_error = 1;
1010 goto exit;
1011 }
1012
1013 if (length != 2) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001014 dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto exit;
1016 }
1017
1018 if (data[0] == TI_CODE_HARDWARE_ERROR) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001019 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto exit;
1021 }
1022
1023 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1024 function = TI_GET_FUNC_FROM_CODE(data[0]);
1025
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001026 dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
1027 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (port_number >= serial->num_ports) {
Alan Coxa30fa792008-07-22 11:15:17 +01001030 dev_err(dev, "%s - bad port number, %d\n",
1031 __func__, port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 goto exit;
1033 }
1034
1035 port = serial->port[port_number];
1036
1037 tport = usb_get_serial_port_data(port);
1038 if (!tport)
1039 goto exit;
1040
1041 switch (function) {
1042 case TI_CODE_DATA_ERROR:
Alan Coxa30fa792008-07-22 11:15:17 +01001043 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001044 __func__, port_number, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 break;
1046
1047 case TI_CODE_MODEM_STATUS:
1048 msr = data[1];
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001049 dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 ti_handle_new_msr(tport, msr);
1051 break;
1052
1053 default:
Alan Coxa30fa792008-07-22 11:15:17 +01001054 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1055 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 break;
1057 }
1058
1059exit:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001060 retval = usb_submit_urb(urb, GFP_ATOMIC);
1061 if (retval)
1062 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001063 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066
David Howells7d12e782006-10-05 14:55:46 +01001067static void ti_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Ming Leicdc97792008-02-24 18:41:47 +08001069 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct usb_serial_port *port = tport->tp_port;
1071 struct device *dev = &urb->dev->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001072 int status = urb->status;
1073 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001075 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 case 0:
1077 break;
1078 case -ECONNRESET:
1079 case -ENOENT:
1080 case -ESHUTDOWN:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001081 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 tport->tp_tdev->td_urb_error = 1;
1083 wake_up_interruptible(&tport->tp_write_wait);
1084 return;
1085 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001086 dev_err(dev, "%s - nonzero urb status, %d\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001087 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 tport->tp_tdev->td_urb_error = 1;
1089 wake_up_interruptible(&tport->tp_write_wait);
1090 }
1091
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001092 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto exit;
1094
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001095 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001096 dev_err(dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return;
1098 }
1099
Jiri Slaby2e124b42013-01-03 15:53:06 +01001100 if (urb->actual_length) {
1101 usb_serial_debug_data(dev, __func__, urb->actual_length,
1102 urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Jiri Slaby2e124b42013-01-03 15:53:06 +01001104 if (!tport->tp_is_open)
1105 dev_dbg(dev, "%s - port closed, dropping data\n",
1106 __func__);
1107 else
1108 ti_recv(port, urb->transfer_buffer, urb->actual_length);
1109 spin_lock(&tport->tp_lock);
Johan Hovold783ca352013-03-21 12:37:33 +01001110 port->icount.rx += urb->actual_length;
Jiri Slaby2e124b42013-01-03 15:53:06 +01001111 spin_unlock(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113
1114exit:
1115 /* continue to read unless stopping */
1116 spin_lock(&tport->tp_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001117 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001118 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001119 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 spin_unlock(&tport->tp_lock);
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001123 if (retval)
1124 dev_err(dev, "%s - resubmit read urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001125 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128
David Howells7d12e782006-10-05 14:55:46 +01001129static void ti_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Ming Leicdc97792008-02-24 18:41:47 +08001131 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct usb_serial_port *port = tport->tp_port;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001133 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 tport->tp_write_urb_in_use = 0;
1136
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001137 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 case 0:
1139 break;
1140 case -ECONNRESET:
1141 case -ENOENT:
1142 case -ESHUTDOWN:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001143 dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 tport->tp_tdev->td_urb_error = 1;
1145 wake_up_interruptible(&tport->tp_write_wait);
1146 return;
1147 default:
Johan Hovold22a416c2012-02-10 13:20:51 +01001148 dev_err_console(port, "%s - nonzero urb status, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001149 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 tport->tp_tdev->td_urb_error = 1;
1151 wake_up_interruptible(&tport->tp_write_wait);
1152 }
1153
1154 /* send any buffered data */
1155 ti_send(tport);
1156}
1157
1158
Jiri Slaby2e124b42013-01-03 15:53:06 +01001159static void ti_recv(struct usb_serial_port *port, unsigned char *data,
1160 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 int cnt;
1163
1164 do {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001165 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Cox33f0f882006-01-09 20:54:13 -08001166 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001167 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001168 __func__, length - cnt);
1169 if (cnt == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Jiri Slaby2e124b42013-01-03 15:53:06 +01001172 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 data += cnt;
1174 length -= cnt;
1175 } while (length > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178
1179static void ti_send(struct ti_port *tport)
1180{
1181 int count, result;
1182 struct usb_serial_port *port = tport->tp_port;
Alan Cox4a90f092008-10-13 10:39:46 +01001183 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 unsigned long flags;
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 spin_lock_irqsave(&tport->tp_lock, flags);
1187
Alan Cox4a90f092008-10-13 10:39:46 +01001188 if (tport->tp_write_urb_in_use)
1189 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Johan Hovold074ef652010-05-19 00:01:35 +02001191 count = kfifo_out(&tport->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 port->write_urb->transfer_buffer,
1193 port->bulk_out_size);
1194
Alan Cox4a90f092008-10-13 10:39:46 +01001195 if (count == 0)
1196 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 tport->tp_write_urb_in_use = 1;
1199
1200 spin_unlock_irqrestore(&tport->tp_lock, flags);
1201
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001202 usb_serial_debug_data(&port->dev, __func__, count,
1203 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1206 usb_sndbulkpipe(port->serial->dev,
1207 port->bulk_out_endpointAddress),
1208 port->write_urb->transfer_buffer, count,
1209 ti_bulk_out_callback, tport);
1210
1211 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1212 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001213 dev_err_console(port, "%s - submit write urb failed, %d\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001214 __func__, result);
1215 tport->tp_write_urb_in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /* TODO: reschedule ti_send */
1217 } else {
1218 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold783ca352013-03-21 12:37:33 +01001219 port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 spin_unlock_irqrestore(&tport->tp_lock, flags);
1221 }
1222
1223 /* more room in the buffer for new writes, wakeup */
1224 if (tty)
1225 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001226 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 wake_up_interruptible(&tport->tp_write_wait);
Alan Cox4a90f092008-10-13 10:39:46 +01001228 return;
1229unlock:
1230 spin_unlock_irqrestore(&tport->tp_lock, flags);
1231 tty_kref_put(tty);
1232 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235
1236static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1237{
Alan Cox04ca89d2008-02-20 21:41:40 +00001238 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int status;
1240
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001241 status = ti_write_byte(tport->tp_port, tport->tp_tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1243 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1244
Alan Cox04ca89d2008-02-20 21:41:40 +00001245 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (!status)
1247 tport->tp_shadow_mcr = mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001248 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 return status;
1251}
1252
1253
1254static int ti_get_lsr(struct ti_port *tport)
1255{
Alan Coxa30fa792008-07-22 11:15:17 +01001256 int size, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct ti_device *tdev = tport->tp_tdev;
1258 struct usb_serial_port *port = tport->tp_port;
1259 int port_number = port->number - port->serial->minor;
1260 struct ti_port_status *data;
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 size = sizeof(struct ti_port_status);
1263 data = kmalloc(size, GFP_KERNEL);
1264 if (!data) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001265 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return -ENOMEM;
1267 }
1268
1269 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1270 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1271 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001272 dev_err(&port->dev,
1273 "%s - get port status command failed, %d\n",
1274 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 goto free_data;
1276 }
1277
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001278 dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 tport->tp_lsr = data->bLSR;
1281
1282free_data:
1283 kfree(data);
1284 return status;
1285}
1286
1287
1288static int ti_get_serial_info(struct ti_port *tport,
1289 struct serial_struct __user *ret_arg)
1290{
1291 struct usb_serial_port *port = tport->tp_port;
1292 struct serial_struct ret_serial;
1293
1294 if (!ret_arg)
1295 return -EFAULT;
1296
1297 memset(&ret_serial, 0, sizeof(ret_serial));
1298
1299 ret_serial.type = PORT_16550A;
1300 ret_serial.line = port->serial->minor;
1301 ret_serial.port = port->number - port->serial->minor;
1302 ret_serial.flags = tport->tp_flags;
1303 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1304 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1305 ret_serial.closing_wait = tport->tp_closing_wait;
1306
1307 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1308 return -EFAULT;
1309
1310 return 0;
1311}
1312
1313
Alan Cox4a90f092008-10-13 10:39:46 +01001314static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct serial_struct __user *new_arg)
1316{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct serial_struct new_serial;
1318
1319 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1320 return -EFAULT;
1321
1322 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 tport->tp_closing_wait = new_serial.closing_wait;
1324
1325 return 0;
1326}
1327
1328
1329static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1330{
1331 struct async_icount *icount;
1332 struct tty_struct *tty;
1333 unsigned long flags;
1334
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001335 dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (msr & TI_MSR_DELTA_MASK) {
1338 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold783ca352013-03-21 12:37:33 +01001339 icount = &tport->tp_port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (msr & TI_MSR_DELTA_CTS)
1341 icount->cts++;
1342 if (msr & TI_MSR_DELTA_DSR)
1343 icount->dsr++;
1344 if (msr & TI_MSR_DELTA_CD)
1345 icount->dcd++;
1346 if (msr & TI_MSR_DELTA_RI)
1347 icount->rng++;
Johan Hovold6c75e262013-03-21 12:37:34 +01001348 wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 spin_unlock_irqrestore(&tport->tp_lock, flags);
1350 }
1351
1352 tport->tp_msr = msr & TI_MSR_MASK;
1353
1354 /* handle CTS flow control */
Alan Cox4a90f092008-10-13 10:39:46 +01001355 tty = tty_port_tty_get(&tport->tp_port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (tty && C_CRTSCTS(tty)) {
1357 if (msr & TI_MSR_CTS) {
1358 tty->hw_stopped = 0;
1359 tty_wakeup(tty);
1360 } else {
1361 tty->hw_stopped = 1;
1362 }
1363 }
Alan Cox4a90f092008-10-13 10:39:46 +01001364 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367
Johan Hovold113ec312013-04-18 17:33:19 +02001368static void ti_drain(struct ti_port *tport, unsigned long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
1370 struct ti_device *tdev = tport->tp_tdev;
1371 struct usb_serial_port *port = tport->tp_port;
1372 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Oliver Neukum0915f492008-01-23 12:28:45 +01001374 spin_lock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* wait for data to drain from the buffer */
1377 tdev->td_urb_error = 0;
1378 init_waitqueue_entry(&wait, current);
1379 add_wait_queue(&tport->tp_write_wait, &wait);
1380 for (;;) {
1381 set_current_state(TASK_INTERRUPTIBLE);
Johan Hovold074ef652010-05-19 00:01:35 +02001382 if (kfifo_len(&tport->write_fifo) == 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 || timeout == 0 || signal_pending(current)
1384 || tdev->td_urb_error
Oliver Neukum0915f492008-01-23 12:28:45 +01001385 || port->serial->disconnected) /* disconnect */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 break;
Oliver Neukum0915f492008-01-23 12:28:45 +01001387 spin_unlock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 timeout = schedule_timeout(timeout);
Oliver Neukum0915f492008-01-23 12:28:45 +01001389 spin_lock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391 set_current_state(TASK_RUNNING);
1392 remove_wait_queue(&tport->tp_write_wait, &wait);
Oliver Neukum0915f492008-01-23 12:28:45 +01001393 spin_unlock_irq(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Oliver Neukum0915f492008-01-23 12:28:45 +01001395 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* wait for data to drain from the device */
1397 /* wait for empty tx register, plus 20 ms */
1398 timeout += jiffies;
1399 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1400 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1401 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
Oliver Neukum0915f492008-01-23 12:28:45 +01001402 && !port->serial->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (ti_get_lsr(tport))
1404 break;
Oliver Neukum0915f492008-01-23 12:28:45 +01001405 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 msleep_interruptible(20);
Oliver Neukum0915f492008-01-23 12:28:45 +01001407 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Oliver Neukum0915f492008-01-23 12:28:45 +01001409 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
1412
1413static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1414{
1415 unsigned long flags;
1416
1417 spin_lock_irqsave(&tport->tp_lock, flags);
1418
1419 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1420 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1421
1422 spin_unlock_irqrestore(&tport->tp_lock, flags);
1423}
1424
1425
1426static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1427{
1428 struct urb *urb;
1429 int status = 0;
1430 unsigned long flags;
1431
1432 spin_lock_irqsave(&tport->tp_lock, flags);
1433
1434 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
Oliver Neukum944dc182007-05-07 08:33:18 +02001435 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 urb = tport->tp_port->read_urb;
Oliver Neukum944dc182007-05-07 08:33:18 +02001437 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 urb->context = tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 status = usb_submit_urb(urb, GFP_KERNEL);
Oliver Neukum944dc182007-05-07 08:33:18 +02001440 } else {
1441 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1442 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 return status;
1446}
1447
1448
1449static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1450 __u16 moduleid, __u16 value, __u8 *data, int size)
1451{
1452 int status;
1453
1454 status = usb_control_msg(tdev->td_serial->dev,
1455 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1456 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1457 value, moduleid, data, size, 1000);
1458
1459 if (status == size)
1460 status = 0;
1461
1462 if (status > 0)
1463 status = -ECOMM;
1464
1465 return status;
1466}
1467
1468
1469static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1470 __u16 moduleid, __u16 value, __u8 *data, int size)
1471{
1472 int status;
1473
1474 status = usb_control_msg(tdev->td_serial->dev,
1475 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1476 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1477 value, moduleid, data, size, 1000);
1478
1479 if (status == size)
1480 status = 0;
1481
1482 if (status > 0)
1483 status = -ECOMM;
1484
1485 return status;
1486}
1487
1488
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001489static int ti_write_byte(struct usb_serial_port *port,
1490 struct ti_device *tdev, unsigned long addr,
1491 __u8 mask, __u8 byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 int status;
1494 unsigned int size;
1495 struct ti_write_data_bytes *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001497 dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1498 addr, mask, byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 size = sizeof(struct ti_write_data_bytes) + 2;
1501 data = kmalloc(size, GFP_KERNEL);
1502 if (!data) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001503 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return -ENOMEM;
1505 }
1506
1507 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1508 data->bDataType = TI_RW_DATA_BYTE;
1509 data->bDataCounter = 1;
1510 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1511 data->wBaseAddrLo = cpu_to_be16(addr);
1512 data->bData[0] = mask;
1513 data->bData[1] = byte;
1514
1515 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1516 (__u8 *)data, size);
1517
1518 if (status < 0)
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001519 dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 kfree(data);
1522
1523 return status;
1524}
1525
Alan Cox95da3102008-07-22 11:09:07 +01001526static int ti_do_download(struct usb_device *dev, int pipe,
1527 u8 *buffer, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 int pos;
Alan Cox95da3102008-07-22 11:09:07 +01001530 u8 cs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 struct ti_firmware_header *header;
Oliver Neukum87ea8c82009-06-30 09:44:24 +02001533 int status = 0;
Alan Cox95da3102008-07-22 11:09:07 +01001534 int len;
Alan Coxa30fa792008-07-22 11:15:17 +01001535
1536 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 cs = (__u8)(cs + buffer[pos]);
1538
1539 header = (struct ti_firmware_header *)buffer;
Alan Coxa30fa792008-07-22 11:15:17 +01001540 header->wLength = cpu_to_le16((__u16)(size
Alan Cox95da3102008-07-22 11:09:07 +01001541 - sizeof(struct ti_firmware_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 header->bCheckSum = cs;
1543
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001544 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01001545 for (pos = 0; pos < size; pos += done) {
1546 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1547 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1548 &done, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (status)
1550 break;
1551 }
Alan Cox95da3102008-07-22 11:09:07 +01001552 return status;
1553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Chris Adams05a3d902009-01-11 19:48:53 +00001555static int ti_download_firmware(struct ti_device *tdev)
Alan Cox95da3102008-07-22 11:09:07 +01001556{
Chris Adams05a3d902009-01-11 19:48:53 +00001557 int status;
Alan Cox95da3102008-07-22 11:09:07 +01001558 int buffer_size;
1559 __u8 *buffer;
1560 struct usb_device *dev = tdev->td_serial->dev;
1561 unsigned int pipe = usb_sndbulkpipe(dev,
1562 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1563 const struct firmware *fw_p;
1564 char buf[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Chris Adams05a3d902009-01-11 19:48:53 +00001566 /* try ID specific firmware first, then try generic firmware */
1567 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1568 dev->descriptor.idProduct);
Adhir Ramjiawan72b27a02012-04-09 14:01:38 +02001569 status = request_firmware(&fw_p, buf, &dev->dev);
1570
1571 if (status != 0) {
Chris Adamscb7a7c62009-01-11 19:49:00 +00001572 buf[0] = '\0';
1573 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1574 switch (dev->descriptor.idProduct) {
1575 case MTS_CDMA_PRODUCT_ID:
1576 strcpy(buf, "mts_cdma.fw");
1577 break;
1578 case MTS_GSM_PRODUCT_ID:
1579 strcpy(buf, "mts_gsm.fw");
1580 break;
1581 case MTS_EDGE_PRODUCT_ID:
1582 strcpy(buf, "mts_edge.fw");
1583 break;
Alex Manoussakiscdc04832010-04-22 15:18:20 -07001584 case MTS_MT9234MU_PRODUCT_ID:
1585 strcpy(buf, "mts_mt9234mu.fw");
1586 break;
1587 case MTS_MT9234ZBA_PRODUCT_ID:
1588 strcpy(buf, "mts_mt9234zba.fw");
1589 break;
1590 case MTS_MT9234ZBAOLD_PRODUCT_ID:
1591 strcpy(buf, "mts_mt9234zba.fw");
1592 break; }
Chris Adamscb7a7c62009-01-11 19:49:00 +00001593 }
1594 if (buf[0] == '\0') {
1595 if (tdev->td_is_3410)
1596 strcpy(buf, "ti_3410.fw");
1597 else
1598 strcpy(buf, "ti_5052.fw");
1599 }
Chris Adams05a3d902009-01-11 19:48:53 +00001600 status = request_firmware(&fw_p, buf, &dev->dev);
1601 }
1602 if (status) {
Alan Cox95da3102008-07-22 11:09:07 +01001603 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1604 return -ENOENT;
1605 }
1606 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
Randy Dunlap75181f32010-04-15 11:38:56 -07001607 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
Jesper Juhl357f45d2011-06-13 22:50:41 +02001608 release_firmware(fw_p);
Alan Cox95da3102008-07-22 11:09:07 +01001609 return -ENOENT;
1610 }
1611
1612 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1613 buffer = kmalloc(buffer_size, GFP_KERNEL);
1614 if (buffer) {
1615 memcpy(buffer, fw_p->data, fw_p->size);
1616 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
Chris Adams2bcbe4c12008-09-10 14:11:38 -07001617 status = ti_do_download(dev, pipe, buffer, fw_p->size);
Alan Cox95da3102008-07-22 11:09:07 +01001618 kfree(buffer);
Chris Adams05a3d902009-01-11 19:48:53 +00001619 } else {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001620 dev_dbg(&dev->dev, "%s ENOMEM\n", __func__);
Chris Adams05a3d902009-01-11 19:48:53 +00001621 status = -ENOMEM;
Alan Cox95da3102008-07-22 11:09:07 +01001622 }
1623 release_firmware(fw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001625 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1626 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return status;
1628 }
1629
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001630 dev_dbg(&dev->dev, "%s - download successful\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 return 0;
1633}