blob: 7182bb774b7958f70f50efb823b084c5ea9284d5 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 __u8 tp_shadow_mcr;
71 __u8 tp_uart_mode; /* 232 or 485 modes */
72 unsigned int tp_uart_base_addr;
73 int tp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 wait_queue_head_t tp_write_wait;
75 struct ti_device *tp_tdev;
76 struct usb_serial_port *tp_port;
77 spinlock_t tp_lock;
78 int tp_read_urb_state;
79 int tp_write_urb_in_use;
Johan Hovold074ef652010-05-19 00:01:35 +020080 struct kfifo write_fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
83struct ti_device {
Matthias Kaehlcke9da00682007-11-21 15:13:16 -080084 struct mutex td_open_close_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int td_open_port_count;
86 struct usb_serial *td_serial;
87 int td_is_3410;
88 int td_urb_error;
89};
90
91
92/* Function Declarations */
93
94static int ti_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040095static void ti_release(struct usb_serial *serial);
Johan Hovold51ef8472012-10-17 16:31:35 +020096static int ti_port_probe(struct usb_serial_port *port);
97static int ti_port_remove(struct usb_serial_port *port);
Alan Coxa509a7e2009-09-19 13:13:26 -070098static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010099static void ti_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +0100100static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Coxa30fa792008-07-22 11:15:17 +0100101 const unsigned char *data, int count);
Alan Cox95da3102008-07-22 11:09:07 +0100102static int ti_write_room(struct tty_struct *tty);
103static int ti_chars_in_buffer(struct tty_struct *tty);
Johan Hovoldff93b192013-05-05 20:32:32 +0200104static bool ti_tx_empty(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +0100105static void ti_throttle(struct tty_struct *tty);
106static void ti_unthrottle(struct tty_struct *tty);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000107static int ti_ioctl(struct tty_struct *tty,
Alan Coxa30fa792008-07-22 11:15:17 +0100108 unsigned int cmd, unsigned long arg);
109static void ti_set_termios(struct tty_struct *tty,
110 struct usb_serial_port *port, struct ktermios *old_termios);
Alan Cox60b33c12011-02-14 16:26:14 +0000111static int ti_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000112static int ti_tiocmset(struct tty_struct *tty,
Alan Coxa30fa792008-07-22 11:15:17 +0100113 unsigned int set, unsigned int clear);
Alan Cox95da3102008-07-22 11:09:07 +0100114static void ti_break(struct tty_struct *tty, int break_state);
David Howells7d12e782006-10-05 14:55:46 +0100115static void ti_interrupt_callback(struct urb *urb);
116static void ti_bulk_in_callback(struct urb *urb);
117static void ti_bulk_out_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jiri Slaby2e124b42013-01-03 15:53:06 +0100119static void ti_recv(struct usb_serial_port *port, unsigned char *data,
120 int length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static void ti_send(struct ti_port *tport);
122static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
Johan Hovoldb5784f72013-04-18 17:33:20 +0200123static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static int ti_get_serial_info(struct ti_port *tport,
125 struct serial_struct __user *ret_arg);
Alan Cox4a90f092008-10-13 10:39:46 +0100126static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct serial_struct __user *new_arg);
128static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
131static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
132
133static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
134 __u16 moduleid, __u16 value, __u8 *data, int size);
135static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
136 __u16 moduleid, __u16 value, __u8 *data, int size);
137
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700138static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
139 unsigned long addr, __u8 mask, __u8 byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Chris Adams05a3d902009-01-11 19:48:53 +0000141static int ti_download_firmware(struct ti_device *tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144/* Data */
145
146/* module parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
148static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100149static unsigned int vendor_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100151static unsigned int product_3410_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100153static unsigned int vendor_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
Al Viro64a6f952007-10-14 19:35:30 +0100155static unsigned int product_5052_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157/* supported devices */
158/* the array dimension is the number of default entries plus */
159/* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
160/* null entry */
Darren Hart975dc332012-05-11 13:56:57 -0700161static struct usb_device_id ti_id_table_3410[15+TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000163 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Chris Adamscb7a7c62009-01-11 19:49:00 +0000164 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
165 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
166 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
167 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
168 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700169 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
170 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
171 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
Oliver Neukum1a1fab512009-01-12 13:31:16 +0100172 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
Oliver Neukum97dcf042009-02-04 16:38:33 +0100173 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
174 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
Anders Hammarquist35a2fbc2013-06-19 01:45:48 +0200175 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STEREO_PLUG_ID) },
176 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
Darren Hart975dc332012-05-11 13:56:57 -0700177 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
Oliver Neukum97dcf042009-02-04 16:38:33 +0100180static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
182 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
183 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
184 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
185};
186
Darren Hart975dc332012-05-11 13:56:57 -0700187static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000189 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Chris Adamscb7a7c62009-01-11 19:49:00 +0000190 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
191 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
192 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
193 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
194 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700195 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
196 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
197 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
199 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
200 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
201 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
Oliver Neukum1a1fab512009-01-12 13:31:16 +0100202 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
Oliver Neukum97dcf042009-02-04 16:38:33 +0100203 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
204 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
Andrew Lunn7fd25702012-02-20 09:31:57 +0100205 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
Darren Hart975dc332012-05-11 13:56:57 -0700206 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 { }
208};
209
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700210static struct usb_serial_driver ti_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700211 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700212 .owner = THIS_MODULE,
213 .name = "ti_usb_3410_5052_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700214 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700215 .description = "TI USB 3410 1 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .id_table = ti_id_table_3410,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .num_ports = 1,
218 .attach = ti_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400219 .release = ti_release,
Johan Hovold51ef8472012-10-17 16:31:35 +0200220 .port_probe = ti_port_probe,
221 .port_remove = ti_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 .open = ti_open,
223 .close = ti_close,
224 .write = ti_write,
225 .write_room = ti_write_room,
226 .chars_in_buffer = ti_chars_in_buffer,
Johan Hovoldff93b192013-05-05 20:32:32 +0200227 .tx_empty = ti_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 .throttle = ti_throttle,
229 .unthrottle = ti_unthrottle,
230 .ioctl = ti_ioctl,
231 .set_termios = ti_set_termios,
232 .tiocmget = ti_tiocmget,
233 .tiocmset = ti_tiocmset,
Johan Hovold6c75e262013-03-21 12:37:34 +0100234 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovold783ca352013-03-21 12:37:33 +0100235 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .break_ctl = ti_break,
237 .read_int_callback = ti_interrupt_callback,
238 .read_bulk_callback = ti_bulk_in_callback,
239 .write_bulk_callback = ti_bulk_out_callback,
240};
241
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700242static struct usb_serial_driver ti_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700243 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700244 .owner = THIS_MODULE,
245 .name = "ti_usb_3410_5052_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700246 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700247 .description = "TI USB 5052 2 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .id_table = ti_id_table_5052,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .num_ports = 2,
250 .attach = ti_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400251 .release = ti_release,
Johan Hovold51ef8472012-10-17 16:31:35 +0200252 .port_probe = ti_port_probe,
253 .port_remove = ti_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 .open = ti_open,
255 .close = ti_close,
256 .write = ti_write,
257 .write_room = ti_write_room,
258 .chars_in_buffer = ti_chars_in_buffer,
Johan Hovoldff93b192013-05-05 20:32:32 +0200259 .tx_empty = ti_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .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;
Johan Hovoldf1175da2013-04-18 17:33:23 +0200432 port->port.closing_wait = msecs_to_jiffies(10 * 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
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700480 port_number = port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
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
Johan Hovoldf1175da2013-04-18 17:33:23 +0200585 port->port.drain_delay = 3;
586
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800587 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589unlink_int_urb:
590 if (tdev->td_open_port_count == 0)
591 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800592release_lock:
593 mutex_unlock(&tdev->td_open_close_lock);
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700594 dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return status;
596}
597
598
Alan Cox335f8512009-06-11 12:26:29 +0100599static void ti_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct ti_device *tdev;
602 struct ti_port *tport;
603 int port_number;
604 int status;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800605 int do_unlock;
Johan Hovold113ec312013-04-18 17:33:19 +0200606 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 tdev = usb_get_serial_data(port->serial);
609 tport = usb_get_serial_port_data(port);
610 if (tdev == NULL || tport == NULL)
611 return;
612
613 tport->tp_is_open = 0;
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 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
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700622 port_number = port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
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
Johan Hovoldff93b192013-05-05 20:32:32 +0200703static bool ti_tx_empty(struct usb_serial_port *port)
704{
705 struct ti_port *tport = usb_get_serial_port_data(port);
706 int ret;
707 u8 lsr;
708
709 ret = ti_get_lsr(tport, &lsr);
710 if (!ret && !(lsr & TI_LSR_TX_EMPTY))
711 return false;
712
713 return true;
714}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Alan Cox95da3102008-07-22 11:09:07 +0100716static void ti_throttle(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (tport == NULL)
722 return;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (I_IXOFF(tty) || C_CRTSCTS(tty))
725 ti_stop_read(tport, tty);
726
727}
728
729
Alan Cox95da3102008-07-22 11:09:07 +0100730static void ti_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Alan Cox95da3102008-07-22 11:09:07 +0100732 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int status;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (tport == NULL)
737 return;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
740 status = ti_restart_read(tport, tty);
741 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100742 dev_err(&port->dev, "%s - cannot restart read, %d\n",
743 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745}
746
Alan Cox00a0d0d2011-02-14 16:27:06 +0000747static int ti_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 unsigned int cmd, unsigned long arg)
749{
Alan Cox95da3102008-07-22 11:09:07 +0100750 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700753 dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (tport == NULL)
756 return -ENODEV;
757
758 switch (cmd) {
Alan Coxa30fa792008-07-22 11:15:17 +0100759 case TIOCGSERIAL:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700760 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
Alan Coxa30fa792008-07-22 11:15:17 +0100761 return ti_get_serial_info(tport,
762 (struct serial_struct __user *)arg);
763 case TIOCSSERIAL:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700764 dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
Alan Cox4a90f092008-10-13 10:39:46 +0100765 return ti_set_serial_info(tty, tport,
766 (struct serial_struct __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -ENOIOCTLCMD;
769}
770
771
Alan Cox95da3102008-07-22 11:09:07 +0100772static void ti_set_termios(struct tty_struct *tty,
773 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct ti_uart_config *config;
Alan Coxa30fa792008-07-22 11:15:17 +0100777 tcflag_t cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int baud;
779 int status;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700780 int port_number = port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 unsigned int mcr;
782
Alan Coxadc8d742012-07-14 15:31:47 +0100783 cflag = tty->termios.c_cflag;
784 iflag = tty->termios.c_iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700786 dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
787 dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
788 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (tport == NULL)
791 return;
792
793 config = kmalloc(sizeof(*config), GFP_KERNEL);
794 if (!config) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800795 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return;
797 }
798
799 config->wFlags = 0;
800
801 /* these flags must be set */
802 config->wFlags |= TI_UART_ENABLE_MS_INTS;
803 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
804 config->bUartMode = (__u8)(tport->tp_uart_mode);
805
806 switch (cflag & CSIZE) {
Alan Coxa30fa792008-07-22 11:15:17 +0100807 case CS5:
808 config->bDataBits = TI_UART_5_DATA_BITS;
809 break;
810 case CS6:
811 config->bDataBits = TI_UART_6_DATA_BITS;
812 break;
813 case CS7:
814 config->bDataBits = TI_UART_7_DATA_BITS;
815 break;
816 default:
817 case CS8:
818 config->bDataBits = TI_UART_8_DATA_BITS;
819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Alan Cox537699e2008-01-03 17:03:11 +0000822 /* CMSPAR isn't supported by this driver */
Alan Coxadc8d742012-07-14 15:31:47 +0100823 tty->termios.c_cflag &= ~CMSPAR;
Alan Cox537699e2008-01-03 17:03:11 +0000824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (cflag & PARENB) {
826 if (cflag & PARODD) {
827 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
828 config->bParity = TI_UART_ODD_PARITY;
829 } else {
830 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
831 config->bParity = TI_UART_EVEN_PARITY;
832 }
833 } else {
834 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
Alan Coxa30fa792008-07-22 11:15:17 +0100835 config->bParity = TI_UART_NO_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
838 if (cflag & CSTOPB)
839 config->bStopBits = TI_UART_2_STOP_BITS;
840 else
841 config->bStopBits = TI_UART_1_STOP_BITS;
842
843 if (cflag & CRTSCTS) {
844 /* RTS flow control must be off to drop RTS for baud rate B0 */
845 if ((cflag & CBAUD) != B0)
846 config->wFlags |= TI_UART_ENABLE_RTS_IN;
847 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
848 } else {
849 tty->hw_stopped = 0;
850 ti_restart_read(tport, tty);
851 }
852
853 if (I_IXOFF(tty) || I_IXON(tty)) {
854 config->cXon = START_CHAR(tty);
855 config->cXoff = STOP_CHAR(tty);
856
857 if (I_IXOFF(tty))
858 config->wFlags |= TI_UART_ENABLE_X_IN;
859 else
860 ti_restart_read(tport, tty);
861
862 if (I_IXON(tty))
863 config->wFlags |= TI_UART_ENABLE_X_OUT;
864 }
865
866 baud = tty_get_baud_rate(tty);
Alan Cox537699e2008-01-03 17:03:11 +0000867 if (!baud)
868 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (tport->tp_tdev->td_is_3410)
870 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
871 else
872 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
873
Alan Cox537699e2008-01-03 17:03:11 +0000874 /* FIXME: Should calculate resulting baud here and report it back */
875 if ((cflag & CBAUD) != B0)
876 tty_encode_baud_rate(tty, baud, baud);
877
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700878 dev_dbg(&port->dev,
879 "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
880 __func__, baud, config->wBaudRate, config->wFlags,
881 config->bDataBits, config->bParity, config->bStopBits,
882 config->cXon, config->cXoff, config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 cpu_to_be16s(&config->wBaudRate);
885 cpu_to_be16s(&config->wFlags);
886
887 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
888 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
889 sizeof(*config));
890 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100891 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
892 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
895 mcr = tport->tp_shadow_mcr;
896 /* if baud rate is B0, clear RTS and DTR */
897 if ((cflag & CBAUD) == B0)
898 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
899 status = ti_set_mcr(tport, mcr);
900 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100901 dev_err(&port->dev,
902 "%s - cannot set modem control on port %d, %d\n",
903 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 kfree(config);
906}
907
908
Alan Cox60b33c12011-02-14 16:26:14 +0000909static int ti_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Alan Cox95da3102008-07-22 11:09:07 +0100911 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct ti_port *tport = usb_get_serial_port_data(port);
913 unsigned int result;
914 unsigned int msr;
915 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000916 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (tport == NULL)
919 return -ENODEV;
920
Alan Cox04ca89d2008-02-20 21:41:40 +0000921 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 msr = tport->tp_msr;
923 mcr = tport->tp_shadow_mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000924 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
927 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
928 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
929 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
930 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
931 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
932 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
933
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700934 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 return result;
937}
938
939
Alan Cox20b9d172011-02-14 16:26:50 +0000940static int ti_tiocmset(struct tty_struct *tty,
941 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Alan Cox95da3102008-07-22 11:09:07 +0100943 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct ti_port *tport = usb_get_serial_port_data(port);
945 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000946 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (tport == NULL)
949 return -ENODEV;
950
Alan Cox04ca89d2008-02-20 21:41:40 +0000951 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 mcr = tport->tp_shadow_mcr;
953
954 if (set & TIOCM_RTS)
955 mcr |= TI_MCR_RTS;
956 if (set & TIOCM_DTR)
957 mcr |= TI_MCR_DTR;
958 if (set & TIOCM_LOOP)
959 mcr |= TI_MCR_LOOP;
960
961 if (clear & TIOCM_RTS)
962 mcr &= ~TI_MCR_RTS;
963 if (clear & TIOCM_DTR)
964 mcr &= ~TI_MCR_DTR;
965 if (clear & TIOCM_LOOP)
966 mcr &= ~TI_MCR_LOOP;
Alan Cox04ca89d2008-02-20 21:41:40 +0000967 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 return ti_set_mcr(tport, mcr);
970}
971
972
Alan Cox95da3102008-07-22 11:09:07 +0100973static void ti_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Alan Cox95da3102008-07-22 11:09:07 +0100975 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 struct ti_port *tport = usb_get_serial_port_data(port);
977 int status;
978
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700979 dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (tport == NULL)
982 return;
983
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700984 status = ti_write_byte(port, tport->tp_tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
986 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
987
988 if (status)
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700989 dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992
David Howells7d12e782006-10-05 14:55:46 +0100993static void ti_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Ming Leicdc97792008-02-24 18:41:47 +0800995 struct ti_device *tdev = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct usb_serial_port *port;
997 struct usb_serial *serial = tdev->td_serial;
998 struct ti_port *tport;
999 struct device *dev = &urb->dev->dev;
1000 unsigned char *data = urb->transfer_buffer;
1001 int length = urb->actual_length;
1002 int port_number;
1003 int function;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001004 int status = urb->status;
1005 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 __u8 msr;
1007
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001008 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 case 0:
1010 break;
1011 case -ECONNRESET:
1012 case -ENOENT:
1013 case -ESHUTDOWN:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001014 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 tdev->td_urb_error = 1;
1016 return;
1017 default:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001018 dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 tdev->td_urb_error = 1;
1020 goto exit;
1021 }
1022
1023 if (length != 2) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001024 dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 goto exit;
1026 }
1027
1028 if (data[0] == TI_CODE_HARDWARE_ERROR) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001029 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 goto exit;
1031 }
1032
1033 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1034 function = TI_GET_FUNC_FROM_CODE(data[0]);
1035
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001036 dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
1037 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (port_number >= serial->num_ports) {
Alan Coxa30fa792008-07-22 11:15:17 +01001040 dev_err(dev, "%s - bad port number, %d\n",
1041 __func__, port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 goto exit;
1043 }
1044
1045 port = serial->port[port_number];
1046
1047 tport = usb_get_serial_port_data(port);
1048 if (!tport)
1049 goto exit;
1050
1051 switch (function) {
1052 case TI_CODE_DATA_ERROR:
Alan Coxa30fa792008-07-22 11:15:17 +01001053 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001054 __func__, port_number, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056
1057 case TI_CODE_MODEM_STATUS:
1058 msr = data[1];
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001059 dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 ti_handle_new_msr(tport, msr);
1061 break;
1062
1063 default:
Alan Coxa30fa792008-07-22 11:15:17 +01001064 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1065 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 break;
1067 }
1068
1069exit:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001070 retval = usb_submit_urb(urb, GFP_ATOMIC);
1071 if (retval)
1072 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001073 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076
David Howells7d12e782006-10-05 14:55:46 +01001077static void ti_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Ming Leicdc97792008-02-24 18:41:47 +08001079 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct usb_serial_port *port = tport->tp_port;
1081 struct device *dev = &urb->dev->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001082 int status = urb->status;
1083 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001085 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 case 0:
1087 break;
1088 case -ECONNRESET:
1089 case -ENOENT:
1090 case -ESHUTDOWN:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001091 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 tport->tp_tdev->td_urb_error = 1;
1093 wake_up_interruptible(&tport->tp_write_wait);
1094 return;
1095 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001096 dev_err(dev, "%s - nonzero urb status, %d\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001097 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 tport->tp_tdev->td_urb_error = 1;
1099 wake_up_interruptible(&tport->tp_write_wait);
1100 }
1101
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001102 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 goto exit;
1104
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001105 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001106 dev_err(dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return;
1108 }
1109
Jiri Slaby2e124b42013-01-03 15:53:06 +01001110 if (urb->actual_length) {
1111 usb_serial_debug_data(dev, __func__, urb->actual_length,
1112 urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Jiri Slaby2e124b42013-01-03 15:53:06 +01001114 if (!tport->tp_is_open)
1115 dev_dbg(dev, "%s - port closed, dropping data\n",
1116 __func__);
1117 else
1118 ti_recv(port, urb->transfer_buffer, urb->actual_length);
1119 spin_lock(&tport->tp_lock);
Johan Hovold783ca352013-03-21 12:37:33 +01001120 port->icount.rx += urb->actual_length;
Jiri Slaby2e124b42013-01-03 15:53:06 +01001121 spin_unlock(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123
1124exit:
1125 /* continue to read unless stopping */
1126 spin_lock(&tport->tp_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001127 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001128 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001129 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 spin_unlock(&tport->tp_lock);
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001133 if (retval)
1134 dev_err(dev, "%s - resubmit read urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001135 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138
David Howells7d12e782006-10-05 14:55:46 +01001139static void ti_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Ming Leicdc97792008-02-24 18:41:47 +08001141 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 struct usb_serial_port *port = tport->tp_port;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001143 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 tport->tp_write_urb_in_use = 0;
1146
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001147 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 case 0:
1149 break;
1150 case -ECONNRESET:
1151 case -ENOENT:
1152 case -ESHUTDOWN:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001153 dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 tport->tp_tdev->td_urb_error = 1;
1155 wake_up_interruptible(&tport->tp_write_wait);
1156 return;
1157 default:
Johan Hovold22a416c2012-02-10 13:20:51 +01001158 dev_err_console(port, "%s - nonzero urb status, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001159 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 tport->tp_tdev->td_urb_error = 1;
1161 wake_up_interruptible(&tport->tp_write_wait);
1162 }
1163
1164 /* send any buffered data */
1165 ti_send(tport);
1166}
1167
1168
Jiri Slaby2e124b42013-01-03 15:53:06 +01001169static void ti_recv(struct usb_serial_port *port, unsigned char *data,
1170 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 int cnt;
1173
1174 do {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001175 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Cox33f0f882006-01-09 20:54:13 -08001176 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001177 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001178 __func__, length - cnt);
1179 if (cnt == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
Jiri Slaby2e124b42013-01-03 15:53:06 +01001182 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 data += cnt;
1184 length -= cnt;
1185 } while (length > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
1188
1189static void ti_send(struct ti_port *tport)
1190{
1191 int count, result;
1192 struct usb_serial_port *port = tport->tp_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 unsigned long flags;
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 spin_lock_irqsave(&tport->tp_lock, flags);
1196
Alan Cox4a90f092008-10-13 10:39:46 +01001197 if (tport->tp_write_urb_in_use)
1198 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Johan Hovold074ef652010-05-19 00:01:35 +02001200 count = kfifo_out(&tport->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 port->write_urb->transfer_buffer,
1202 port->bulk_out_size);
1203
Alan Cox4a90f092008-10-13 10:39:46 +01001204 if (count == 0)
1205 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 tport->tp_write_urb_in_use = 1;
1208
1209 spin_unlock_irqrestore(&tport->tp_lock, flags);
1210
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001211 usb_serial_debug_data(&port->dev, __func__, count,
1212 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1215 usb_sndbulkpipe(port->serial->dev,
1216 port->bulk_out_endpointAddress),
1217 port->write_urb->transfer_buffer, count,
1218 ti_bulk_out_callback, tport);
1219
1220 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1221 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001222 dev_err_console(port, "%s - submit write urb failed, %d\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001223 __func__, result);
1224 tport->tp_write_urb_in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /* TODO: reschedule ti_send */
1226 } else {
1227 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold783ca352013-03-21 12:37:33 +01001228 port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 spin_unlock_irqrestore(&tport->tp_lock, flags);
1230 }
1231
1232 /* more room in the buffer for new writes, wakeup */
Jiri Slaby6aad04f2013-03-07 13:12:29 +01001233 tty_port_tty_wakeup(&port->port);
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 wake_up_interruptible(&tport->tp_write_wait);
Alan Cox4a90f092008-10-13 10:39:46 +01001236 return;
1237unlock:
1238 spin_unlock_irqrestore(&tport->tp_lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +01001239 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242
1243static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1244{
Alan Cox04ca89d2008-02-20 21:41:40 +00001245 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int status;
1247
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001248 status = ti_write_byte(tport->tp_port, tport->tp_tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1250 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1251
Alan Cox04ca89d2008-02-20 21:41:40 +00001252 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (!status)
1254 tport->tp_shadow_mcr = mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001255 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 return status;
1258}
1259
1260
Johan Hovoldb5784f72013-04-18 17:33:20 +02001261static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Alan Coxa30fa792008-07-22 11:15:17 +01001263 int size, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct ti_device *tdev = tport->tp_tdev;
1265 struct usb_serial_port *port = tport->tp_port;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001266 int port_number = port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 struct ti_port_status *data;
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 size = sizeof(struct ti_port_status);
1270 data = kmalloc(size, GFP_KERNEL);
1271 if (!data) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001272 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -ENOMEM;
1274 }
1275
1276 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1277 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1278 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001279 dev_err(&port->dev,
1280 "%s - get port status command failed, %d\n",
1281 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 goto free_data;
1283 }
1284
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001285 dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Johan Hovoldb5784f72013-04-18 17:33:20 +02001287 *lsr = data->bLSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289free_data:
1290 kfree(data);
1291 return status;
1292}
1293
1294
1295static int ti_get_serial_info(struct ti_port *tport,
1296 struct serial_struct __user *ret_arg)
1297{
1298 struct usb_serial_port *port = tport->tp_port;
1299 struct serial_struct ret_serial;
Johan Hovoldf1175da2013-04-18 17:33:23 +02001300 unsigned cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (!ret_arg)
1303 return -EFAULT;
1304
Johan Hovoldf1175da2013-04-18 17:33:23 +02001305 cwait = port->port.closing_wait;
1306 if (cwait != ASYNC_CLOSING_WAIT_NONE)
1307 cwait = jiffies_to_msecs(cwait) / 10;
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 memset(&ret_serial, 0, sizeof(ret_serial));
1310
1311 ret_serial.type = PORT_16550A;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07001312 ret_serial.line = port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001313 ret_serial.port = port->port_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 ret_serial.flags = tport->tp_flags;
1315 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1316 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
Johan Hovoldf1175da2013-04-18 17:33:23 +02001317 ret_serial.closing_wait = cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1320 return -EFAULT;
1321
1322 return 0;
1323}
1324
1325
Alan Cox4a90f092008-10-13 10:39:46 +01001326static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct serial_struct __user *new_arg)
1328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 struct serial_struct new_serial;
Johan Hovoldf1175da2013-04-18 17:33:23 +02001330 unsigned cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1333 return -EFAULT;
1334
Johan Hovoldf1175da2013-04-18 17:33:23 +02001335 cwait = new_serial.closing_wait;
1336 if (cwait != ASYNC_CLOSING_WAIT_NONE)
1337 cwait = msecs_to_jiffies(10 * new_serial.closing_wait);
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
Johan Hovoldf1175da2013-04-18 17:33:23 +02001340 tport->tp_port->port.closing_wait = cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 return 0;
1343}
1344
1345
1346static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1347{
1348 struct async_icount *icount;
1349 struct tty_struct *tty;
1350 unsigned long flags;
1351
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001352 dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 if (msr & TI_MSR_DELTA_MASK) {
1355 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold783ca352013-03-21 12:37:33 +01001356 icount = &tport->tp_port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (msr & TI_MSR_DELTA_CTS)
1358 icount->cts++;
1359 if (msr & TI_MSR_DELTA_DSR)
1360 icount->dsr++;
1361 if (msr & TI_MSR_DELTA_CD)
1362 icount->dcd++;
1363 if (msr & TI_MSR_DELTA_RI)
1364 icount->rng++;
Johan Hovold6c75e262013-03-21 12:37:34 +01001365 wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 spin_unlock_irqrestore(&tport->tp_lock, flags);
1367 }
1368
1369 tport->tp_msr = msr & TI_MSR_MASK;
1370
1371 /* handle CTS flow control */
Alan Cox4a90f092008-10-13 10:39:46 +01001372 tty = tty_port_tty_get(&tport->tp_port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (tty && C_CRTSCTS(tty)) {
1374 if (msr & TI_MSR_CTS) {
1375 tty->hw_stopped = 0;
1376 tty_wakeup(tty);
1377 } else {
1378 tty->hw_stopped = 1;
1379 }
1380 }
Alan Cox4a90f092008-10-13 10:39:46 +01001381 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1386{
1387 unsigned long flags;
1388
1389 spin_lock_irqsave(&tport->tp_lock, flags);
1390
1391 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1392 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1393
1394 spin_unlock_irqrestore(&tport->tp_lock, flags);
1395}
1396
1397
1398static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1399{
1400 struct urb *urb;
1401 int status = 0;
1402 unsigned long flags;
1403
1404 spin_lock_irqsave(&tport->tp_lock, flags);
1405
1406 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
Oliver Neukum944dc182007-05-07 08:33:18 +02001407 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 urb = tport->tp_port->read_urb;
Oliver Neukum944dc182007-05-07 08:33:18 +02001409 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 urb->context = tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 status = usb_submit_urb(urb, GFP_KERNEL);
Oliver Neukum944dc182007-05-07 08:33:18 +02001412 } else {
1413 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1414 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 return status;
1418}
1419
1420
1421static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1422 __u16 moduleid, __u16 value, __u8 *data, int size)
1423{
1424 int status;
1425
1426 status = usb_control_msg(tdev->td_serial->dev,
1427 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1428 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1429 value, moduleid, data, size, 1000);
1430
1431 if (status == size)
1432 status = 0;
1433
1434 if (status > 0)
1435 status = -ECOMM;
1436
1437 return status;
1438}
1439
1440
1441static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1442 __u16 moduleid, __u16 value, __u8 *data, int size)
1443{
1444 int status;
1445
1446 status = usb_control_msg(tdev->td_serial->dev,
1447 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1448 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1449 value, moduleid, data, size, 1000);
1450
1451 if (status == size)
1452 status = 0;
1453
1454 if (status > 0)
1455 status = -ECOMM;
1456
1457 return status;
1458}
1459
1460
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001461static int ti_write_byte(struct usb_serial_port *port,
1462 struct ti_device *tdev, unsigned long addr,
1463 __u8 mask, __u8 byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 int status;
1466 unsigned int size;
1467 struct ti_write_data_bytes *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001469 dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1470 addr, mask, byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 size = sizeof(struct ti_write_data_bytes) + 2;
1473 data = kmalloc(size, GFP_KERNEL);
1474 if (!data) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001475 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return -ENOMEM;
1477 }
1478
1479 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1480 data->bDataType = TI_RW_DATA_BYTE;
1481 data->bDataCounter = 1;
1482 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1483 data->wBaseAddrLo = cpu_to_be16(addr);
1484 data->bData[0] = mask;
1485 data->bData[1] = byte;
1486
1487 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1488 (__u8 *)data, size);
1489
1490 if (status < 0)
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001491 dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 kfree(data);
1494
1495 return status;
1496}
1497
Alan Cox95da3102008-07-22 11:09:07 +01001498static int ti_do_download(struct usb_device *dev, int pipe,
1499 u8 *buffer, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int pos;
Alan Cox95da3102008-07-22 11:09:07 +01001502 u8 cs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 struct ti_firmware_header *header;
Oliver Neukum87ea8c82009-06-30 09:44:24 +02001505 int status = 0;
Alan Cox95da3102008-07-22 11:09:07 +01001506 int len;
Alan Coxa30fa792008-07-22 11:15:17 +01001507
1508 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 cs = (__u8)(cs + buffer[pos]);
1510
1511 header = (struct ti_firmware_header *)buffer;
Alan Coxa30fa792008-07-22 11:15:17 +01001512 header->wLength = cpu_to_le16((__u16)(size
Alan Cox95da3102008-07-22 11:09:07 +01001513 - sizeof(struct ti_firmware_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 header->bCheckSum = cs;
1515
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001516 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01001517 for (pos = 0; pos < size; pos += done) {
1518 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1519 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1520 &done, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (status)
1522 break;
1523 }
Alan Cox95da3102008-07-22 11:09:07 +01001524 return status;
1525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Chris Adams05a3d902009-01-11 19:48:53 +00001527static int ti_download_firmware(struct ti_device *tdev)
Alan Cox95da3102008-07-22 11:09:07 +01001528{
Chris Adams05a3d902009-01-11 19:48:53 +00001529 int status;
Alan Cox95da3102008-07-22 11:09:07 +01001530 int buffer_size;
1531 __u8 *buffer;
1532 struct usb_device *dev = tdev->td_serial->dev;
1533 unsigned int pipe = usb_sndbulkpipe(dev,
1534 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1535 const struct firmware *fw_p;
1536 char buf[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Chris Adams05a3d902009-01-11 19:48:53 +00001538 /* try ID specific firmware first, then try generic firmware */
1539 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1540 dev->descriptor.idProduct);
Adhir Ramjiawan72b27a02012-04-09 14:01:38 +02001541 status = request_firmware(&fw_p, buf, &dev->dev);
1542
1543 if (status != 0) {
Chris Adamscb7a7c62009-01-11 19:49:00 +00001544 buf[0] = '\0';
1545 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1546 switch (dev->descriptor.idProduct) {
1547 case MTS_CDMA_PRODUCT_ID:
1548 strcpy(buf, "mts_cdma.fw");
1549 break;
1550 case MTS_GSM_PRODUCT_ID:
1551 strcpy(buf, "mts_gsm.fw");
1552 break;
1553 case MTS_EDGE_PRODUCT_ID:
1554 strcpy(buf, "mts_edge.fw");
1555 break;
Alex Manoussakiscdc04832010-04-22 15:18:20 -07001556 case MTS_MT9234MU_PRODUCT_ID:
1557 strcpy(buf, "mts_mt9234mu.fw");
1558 break;
1559 case MTS_MT9234ZBA_PRODUCT_ID:
1560 strcpy(buf, "mts_mt9234zba.fw");
1561 break;
1562 case MTS_MT9234ZBAOLD_PRODUCT_ID:
1563 strcpy(buf, "mts_mt9234zba.fw");
1564 break; }
Chris Adamscb7a7c62009-01-11 19:49:00 +00001565 }
1566 if (buf[0] == '\0') {
1567 if (tdev->td_is_3410)
1568 strcpy(buf, "ti_3410.fw");
1569 else
1570 strcpy(buf, "ti_5052.fw");
1571 }
Chris Adams05a3d902009-01-11 19:48:53 +00001572 status = request_firmware(&fw_p, buf, &dev->dev);
1573 }
1574 if (status) {
Alan Cox95da3102008-07-22 11:09:07 +01001575 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1576 return -ENOENT;
1577 }
1578 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
Randy Dunlap75181f32010-04-15 11:38:56 -07001579 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
Jesper Juhl357f45d2011-06-13 22:50:41 +02001580 release_firmware(fw_p);
Alan Cox95da3102008-07-22 11:09:07 +01001581 return -ENOENT;
1582 }
1583
1584 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1585 buffer = kmalloc(buffer_size, GFP_KERNEL);
1586 if (buffer) {
1587 memcpy(buffer, fw_p->data, fw_p->size);
1588 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
Chris Adams2bcbe4c12008-09-10 14:11:38 -07001589 status = ti_do_download(dev, pipe, buffer, fw_p->size);
Alan Cox95da3102008-07-22 11:09:07 +01001590 kfree(buffer);
Chris Adams05a3d902009-01-11 19:48:53 +00001591 } else {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001592 dev_dbg(&dev->dev, "%s ENOMEM\n", __func__);
Chris Adams05a3d902009-01-11 19:48:53 +00001593 status = -ENOMEM;
Alan Cox95da3102008-07-22 11:09:07 +01001594 }
1595 release_firmware(fw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001597 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1598 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return status;
1600 }
1601
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001602 dev_dbg(&dev->dev, "%s - download successful\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 return 0;
1605}