blob: c92c5ed4e580ec6761d0acabc379cfe667faaeb1 [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) },
Andrew Lunn7fd25702012-02-20 09:31:57 +0100175 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
Darren Hart975dc332012-05-11 13:56:57 -0700176 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Oliver Neukum97dcf042009-02-04 16:38:33 +0100179static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
181 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
182 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
183 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
184};
185
Darren Hart975dc332012-05-11 13:56:57 -0700186static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
Oleg Verych1f54a6a2006-11-17 08:21:27 +0000188 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
Chris Adamscb7a7c62009-01-11 19:49:00 +0000189 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
190 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
191 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
192 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
193 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700194 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
195 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
196 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
198 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
199 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
200 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
Oliver Neukum1a1fab512009-01-12 13:31:16 +0100201 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
Oliver Neukum97dcf042009-02-04 16:38:33 +0100202 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
203 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
Andrew Lunn7fd25702012-02-20 09:31:57 +0100204 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
Darren Hart975dc332012-05-11 13:56:57 -0700205 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 { }
207};
208
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700209static struct usb_serial_driver ti_1port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700210 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700211 .owner = THIS_MODULE,
212 .name = "ti_usb_3410_5052_1",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700213 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700214 .description = "TI USB 3410 1 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 .id_table = ti_id_table_3410,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .num_ports = 1,
217 .attach = ti_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400218 .release = ti_release,
Johan Hovold51ef8472012-10-17 16:31:35 +0200219 .port_probe = ti_port_probe,
220 .port_remove = ti_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .open = ti_open,
222 .close = ti_close,
223 .write = ti_write,
224 .write_room = ti_write_room,
225 .chars_in_buffer = ti_chars_in_buffer,
Johan Hovoldff93b192013-05-05 20:32:32 +0200226 .tx_empty = ti_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 .throttle = ti_throttle,
228 .unthrottle = ti_unthrottle,
229 .ioctl = ti_ioctl,
230 .set_termios = ti_set_termios,
231 .tiocmget = ti_tiocmget,
232 .tiocmset = ti_tiocmset,
Johan Hovold6c75e262013-03-21 12:37:34 +0100233 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovold783ca352013-03-21 12:37:33 +0100234 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .break_ctl = ti_break,
236 .read_int_callback = ti_interrupt_callback,
237 .read_bulk_callback = ti_bulk_in_callback,
238 .write_bulk_callback = ti_bulk_out_callback,
239};
240
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700241static struct usb_serial_driver ti_2port_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700242 .driver = {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700243 .owner = THIS_MODULE,
244 .name = "ti_usb_3410_5052_2",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700245 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700246 .description = "TI USB 5052 2 port adapter",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .id_table = ti_id_table_5052,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .num_ports = 2,
249 .attach = ti_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400250 .release = ti_release,
Johan Hovold51ef8472012-10-17 16:31:35 +0200251 .port_probe = ti_port_probe,
252 .port_remove = ti_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .open = ti_open,
254 .close = ti_close,
255 .write = ti_write,
256 .write_room = ti_write_room,
257 .chars_in_buffer = ti_chars_in_buffer,
Johan Hovoldff93b192013-05-05 20:32:32 +0200258 .tx_empty = ti_tx_empty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .throttle = ti_throttle,
260 .unthrottle = ti_unthrottle,
261 .ioctl = ti_ioctl,
262 .set_termios = ti_set_termios,
263 .tiocmget = ti_tiocmget,
264 .tiocmset = ti_tiocmset,
Johan Hovold6c75e262013-03-21 12:37:34 +0100265 .tiocmiwait = usb_serial_generic_tiocmiwait,
Johan Hovold783ca352013-03-21 12:37:33 +0100266 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .break_ctl = ti_break,
268 .read_int_callback = ti_interrupt_callback,
269 .read_bulk_callback = ti_bulk_in_callback,
270 .write_bulk_callback = ti_bulk_out_callback,
271};
272
Alan Stern29618e92012-02-23 14:57:32 -0500273static struct usb_serial_driver * const serial_drivers[] = {
274 &ti_1port_device, &ti_2port_device, NULL
275};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/* Module */
278
279MODULE_AUTHOR(TI_DRIVER_AUTHOR);
280MODULE_DESCRIPTION(TI_DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281MODULE_LICENSE("GPL");
282
David Woodhouse5f24e2d2008-05-30 18:49:51 +0300283MODULE_FIRMWARE("ti_3410.fw");
284MODULE_FIRMWARE("ti_5052.fw");
Chris Adams7df52312009-01-11 19:49:11 +0000285MODULE_FIRMWARE("mts_cdma.fw");
286MODULE_FIRMWARE("mts_gsm.fw");
287MODULE_FIRMWARE("mts_edge.fw");
Alex Manoussakiscdc04832010-04-22 15:18:20 -0700288MODULE_FIRMWARE("mts_mt9234mu.fw");
289MODULE_FIRMWARE("mts_mt9234zba.fw");
David Woodhouse5f24e2d2008-05-30 18:49:51 +0300290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291module_param(closing_wait, int, S_IRUGO | S_IWUSR);
Alan Coxa30fa792008-07-22 11:15:17 +0100292MODULE_PARM_DESC(closing_wait,
293 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100296MODULE_PARM_DESC(vendor_3410,
297 "Vendor ids for 3410 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100299MODULE_PARM_DESC(product_3410,
300 "Product ids for 3410 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100302MODULE_PARM_DESC(vendor_5052,
303 "Vendor ids for 5052 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
Alan Coxa30fa792008-07-22 11:15:17 +0100305MODULE_PARM_DESC(product_5052,
306 "Product ids for 5052 based devices, 1-5 short integers");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
309
310
311/* Functions */
312
313static int __init ti_init(void)
314{
Chris Adams05a3d902009-01-11 19:48:53 +0000315 int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* insert extra vendor and product ids */
Chris Adams05a3d902009-01-11 19:48:53 +0000318 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
Tobias Klauser52950ed2005-12-11 16:20:08 +0100319 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
Chris Adams05a3d902009-01-11 19:48:53 +0000320 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 ti_id_table_3410[j].idVendor = vendor_3410[i];
322 ti_id_table_3410[j].idProduct = product_3410[i];
323 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Chris Adams05a3d902009-01-11 19:48:53 +0000324 ti_id_table_combined[c].idVendor = vendor_3410[i];
325 ti_id_table_combined[c].idProduct = product_3410[i];
326 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Tobias Klauser52950ed2005-12-11 16:20:08 +0100328 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
Chris Adams05a3d902009-01-11 19:48:53 +0000329 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ti_id_table_5052[j].idVendor = vendor_5052[i];
331 ti_id_table_5052[j].idProduct = product_5052[i];
332 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Chris Adams05a3d902009-01-11 19:48:53 +0000333 ti_id_table_combined[c].idVendor = vendor_5052[i];
334 ti_id_table_combined[c].idProduct = product_5052[i];
335 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
Greg Kroah-Hartmanf378dfe2012-09-18 17:09:00 +0100338 return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ti_id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static void __exit ti_exit(void)
342{
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700343 usb_serial_deregister_drivers(serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346module_init(ti_init);
347module_exit(ti_exit);
348
349
350static int ti_startup(struct usb_serial *serial)
351{
352 struct ti_device *tdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct usb_device *dev = serial->dev;
354 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700356 dev_dbg(&dev->dev,
357 "%s - product 0x%4X, num configurations %d, configuration value %d",
358 __func__, le16_to_cpu(dev->descriptor.idProduct),
359 dev->descriptor.bNumConfigurations,
360 dev->actconfig->desc.bConfigurationValue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* create device structure */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100363 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (tdev == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800365 dev_err(&dev->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return -ENOMEM;
367 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800368 mutex_init(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 tdev->td_serial = serial;
370 usb_set_serial_data(serial, tdev);
371
372 /* determine device type */
373 if (usb_match_id(serial->interface, ti_id_table_3410))
374 tdev->td_is_3410 = 1;
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700375 dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
376 tdev->td_is_3410 ? "3410" : "5052");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /* if we have only 1 configuration, download firmware */
379 if (dev->descriptor.bNumConfigurations == 1) {
Adhir Ramjiawan72b27a02012-04-09 14:01:38 +0200380 status = ti_download_firmware(tdev);
381
382 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 goto free_tdev;
384
385 /* 3410 must be reset, 5052 resets itself */
386 if (tdev->td_is_3410) {
387 msleep_interruptible(100);
388 usb_reset_device(dev);
389 }
390
391 status = -ENODEV;
392 goto free_tdev;
Alan Coxa30fa792008-07-22 11:15:17 +0100393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Oliver Neukum413ba6f2008-12-16 12:25:55 +0100395 /* the second configuration must be set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
Oliver Neukum413ba6f2008-12-16 12:25:55 +0100397 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
398 status = status ? status : -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto free_tdev;
400 }
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404free_tdev:
405 kfree(tdev);
406 usb_set_serial_data(serial, NULL);
407 return status;
408}
409
410
Alan Sternf9c99bb2009-06-02 11:53:55 -0400411static void ti_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct ti_device *tdev = usb_get_serial_data(serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700415 kfree(tdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Johan Hovold51ef8472012-10-17 16:31:35 +0200418static int ti_port_probe(struct usb_serial_port *port)
419{
420 struct ti_port *tport;
421
422 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
423 if (!tport)
424 return -ENOMEM;
425
426 spin_lock_init(&tport->tp_lock);
427 if (port == port->serial->port[0])
428 tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
429 else
430 tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
Johan Hovoldf1175da2013-04-18 17:33:23 +0200431 port->port.closing_wait = msecs_to_jiffies(10 * closing_wait);
Johan Hovold51ef8472012-10-17 16:31:35 +0200432 init_waitqueue_head(&tport->tp_write_wait);
433 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE, GFP_KERNEL)) {
434 kfree(tport);
435 return -ENOMEM;
436 }
437 tport->tp_port = port;
438 tport->tp_tdev = usb_get_serial_data(port->serial);
439 tport->tp_uart_mode = 0; /* default is RS232 */
440
441 usb_set_serial_port_data(port, tport);
442
443 return 0;
444}
445
446static int ti_port_remove(struct usb_serial_port *port)
447{
448 struct ti_port *tport;
449
450 tport = usb_get_serial_port_data(port);
451 kfifo_free(&tport->write_fifo);
452 kfree(tport);
453
454 return 0;
455}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Alan Coxa509a7e2009-09-19 13:13:26 -0700457static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct ti_port *tport = usb_get_serial_port_data(port);
460 struct ti_device *tdev;
461 struct usb_device *dev;
462 struct urb *urb;
463 int port_number;
464 int status;
Alan Coxa30fa792008-07-22 11:15:17 +0100465 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
466 TI_PIPE_TIMEOUT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 (TI_TRANSFER_TIMEOUT << 2));
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (tport == NULL)
470 return -ENODEV;
471
472 dev = port->serial->dev;
473 tdev = tport->tp_tdev;
474
475 /* only one open on any port on a device at a time */
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800476 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -ERESTARTSYS;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 port_number = port->number - port->serial->minor;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 tport->tp_msr = 0;
482 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
483
484 /* start interrupt urb the first time a port is opened on this device */
485 if (tdev->td_open_port_count == 0) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700486 dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 urb = tdev->td_serial->port[0]->interrupt_in_urb;
488 if (!urb) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700489 dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 status = -EINVAL;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800491 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 urb->context = tdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 status = usb_submit_urb(urb, GFP_KERNEL);
495 if (status) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700496 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800497 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 }
500
Alan Cox95da3102008-07-22 11:09:07 +0100501 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +0100502 ti_set_termios(tty, port, &tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700504 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
506 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
507 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100508 dev_err(&port->dev, "%s - cannot send open command, %d\n",
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700509 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto unlink_int_urb;
511 }
512
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700513 dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 status = ti_command_out_sync(tdev, TI_START_PORT,
515 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
516 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100517 dev_err(&port->dev, "%s - cannot send start command, %d\n",
518 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto unlink_int_urb;
520 }
521
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700522 dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
524 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
525 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100526 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
527 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto unlink_int_urb;
529 }
530 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
531 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
532 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100533 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
534 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto unlink_int_urb;
536 }
537
538 /* reset the data toggle on the bulk endpoints to work around bug in
539 * host controllers where things get out of sync some times */
540 usb_clear_halt(dev, port->write_urb->pipe);
541 usb_clear_halt(dev, port->read_urb->pipe);
542
Alan Cox95da3102008-07-22 11:09:07 +0100543 if (tty)
Alan Coxadc8d742012-07-14 15:31:47 +0100544 ti_set_termios(tty, port, &tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700546 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
548 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
549 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100550 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
551 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto unlink_int_urb;
553 }
554
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700555 dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 status = ti_command_out_sync(tdev, TI_START_PORT,
557 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
558 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100559 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
560 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto unlink_int_urb;
562 }
563
564 /* start read urb */
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700565 dev_dbg(&port->dev, "%s - start read urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 urb = port->read_urb;
567 if (!urb) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800568 dev_err(&port->dev, "%s - no read urb\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 status = -EINVAL;
570 goto unlink_int_urb;
571 }
572 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 urb->context = tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 status = usb_submit_urb(urb, GFP_KERNEL);
575 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +0100576 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
577 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto unlink_int_urb;
579 }
580
581 tport->tp_is_open = 1;
582 ++tdev->td_open_port_count;
583
Johan Hovoldf1175da2013-04-18 17:33:23 +0200584 port->port.drain_delay = 3;
585
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800586 goto release_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588unlink_int_urb:
589 if (tdev->td_open_port_count == 0)
590 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800591release_lock:
592 mutex_unlock(&tdev->td_open_close_lock);
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700593 dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return status;
595}
596
597
Alan Cox335f8512009-06-11 12:26:29 +0100598static void ti_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct ti_device *tdev;
601 struct ti_port *tport;
602 int port_number;
603 int status;
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800604 int do_unlock;
Johan Hovold113ec312013-04-18 17:33:19 +0200605 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 tdev = usb_get_serial_data(port->serial);
608 tport = usb_get_serial_port_data(port);
609 if (tdev == NULL || tport == NULL)
610 return;
611
612 tport->tp_is_open = 0;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 usb_kill_urb(port->read_urb);
615 usb_kill_urb(port->write_urb);
616 tport->tp_write_urb_in_use = 0;
Johan Hovold113ec312013-04-18 17:33:19 +0200617 spin_lock_irqsave(&tport->tp_lock, flags);
618 kfifo_reset_out(&tport->write_fifo);
619 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 port_number = port->number - port->serial->minor;
622
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700623 dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
625 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
626 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100627 dev_err(&port->dev,
628 "%s - cannot send close port command, %d\n"
629 , __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800631 /* if mutex_lock is interrupted, continue anyway */
632 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 --tport->tp_tdev->td_open_port_count;
634 if (tport->tp_tdev->td_open_port_count <= 0) {
635 /* last port is closed, shut down interrupt urb */
636 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
637 tport->tp_tdev->td_open_port_count = 0;
638 }
Matthias Kaehlcke9da00682007-11-21 15:13:16 -0800639 if (do_unlock)
640 mutex_unlock(&tdev->td_open_close_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643
Alan Cox95da3102008-07-22 11:09:07 +0100644static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
645 const unsigned char *data, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (count == 0) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700650 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return 0;
652 }
653
654 if (tport == NULL || !tport->tp_is_open)
655 return -ENODEV;
656
Johan Hovold074ef652010-05-19 00:01:35 +0200657 count = kfifo_in_locked(&tport->write_fifo, data, count,
658 &tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 ti_send(tport);
660
661 return count;
662}
663
664
Alan Cox95da3102008-07-22 11:09:07 +0100665static int ti_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Alan Cox95da3102008-07-22 11:09:07 +0100667 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct ti_port *tport = usb_get_serial_port_data(port);
669 int room = 0;
670 unsigned long flags;
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (tport == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +0100673 return 0;
Alan Coxa30fa792008-07-22 11:15:17 +0100674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold074ef652010-05-19 00:01:35 +0200676 room = kfifo_avail(&tport->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 spin_unlock_irqrestore(&tport->tp_lock, flags);
678
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700679 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return room;
681}
682
683
Alan Cox95da3102008-07-22 11:09:07 +0100684static int ti_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Alan Cox95da3102008-07-22 11:09:07 +0100686 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct ti_port *tport = usb_get_serial_port_data(port);
688 int chars = 0;
689 unsigned long flags;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (tport == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +0100692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold074ef652010-05-19 00:01:35 +0200695 chars = kfifo_len(&tport->write_fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 spin_unlock_irqrestore(&tport->tp_lock, flags);
697
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700698 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return chars;
700}
701
Johan Hovoldff93b192013-05-05 20:32:32 +0200702static bool ti_tx_empty(struct usb_serial_port *port)
703{
704 struct ti_port *tport = usb_get_serial_port_data(port);
705 int ret;
706 u8 lsr;
707
708 ret = ti_get_lsr(tport, &lsr);
709 if (!ret && !(lsr & TI_LSR_TX_EMPTY))
710 return false;
711
712 return true;
713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Alan Cox95da3102008-07-22 11:09:07 +0100715static void ti_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Alan Cox95da3102008-07-22 11:09:07 +0100717 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (tport == NULL)
721 return;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (I_IXOFF(tty) || C_CRTSCTS(tty))
724 ti_stop_read(tport, tty);
725
726}
727
728
Alan Cox95da3102008-07-22 11:09:07 +0100729static void ti_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Alan Cox95da3102008-07-22 11:09:07 +0100731 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int status;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (tport == NULL)
736 return;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
739 status = ti_restart_read(tport, tty);
740 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100741 dev_err(&port->dev, "%s - cannot restart read, %d\n",
742 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744}
745
Alan Cox00a0d0d2011-02-14 16:27:06 +0000746static int ti_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 unsigned int cmd, unsigned long arg)
748{
Alan Cox95da3102008-07-22 11:09:07 +0100749 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700752 dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (tport == NULL)
755 return -ENODEV;
756
757 switch (cmd) {
Alan Coxa30fa792008-07-22 11:15:17 +0100758 case TIOCGSERIAL:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700759 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
Alan Coxa30fa792008-07-22 11:15:17 +0100760 return ti_get_serial_info(tport,
761 (struct serial_struct __user *)arg);
762 case TIOCSSERIAL:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700763 dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
Alan Cox4a90f092008-10-13 10:39:46 +0100764 return ti_set_serial_info(tty, tport,
765 (struct serial_struct __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return -ENOIOCTLCMD;
768}
769
770
Alan Cox95da3102008-07-22 11:09:07 +0100771static void ti_set_termios(struct tty_struct *tty,
772 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct ti_port *tport = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct ti_uart_config *config;
Alan Coxa30fa792008-07-22 11:15:17 +0100776 tcflag_t cflag, iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int baud;
778 int status;
779 int port_number = port->number - port->serial->minor;
780 unsigned int mcr;
781
Alan Coxadc8d742012-07-14 15:31:47 +0100782 cflag = tty->termios.c_cflag;
783 iflag = tty->termios.c_iflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700785 dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
786 dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
787 old_termios->c_cflag, old_termios->c_iflag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (tport == NULL)
790 return;
791
792 config = kmalloc(sizeof(*config), GFP_KERNEL);
793 if (!config) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800794 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return;
796 }
797
798 config->wFlags = 0;
799
800 /* these flags must be set */
801 config->wFlags |= TI_UART_ENABLE_MS_INTS;
802 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
803 config->bUartMode = (__u8)(tport->tp_uart_mode);
804
805 switch (cflag & CSIZE) {
Alan Coxa30fa792008-07-22 11:15:17 +0100806 case CS5:
807 config->bDataBits = TI_UART_5_DATA_BITS;
808 break;
809 case CS6:
810 config->bDataBits = TI_UART_6_DATA_BITS;
811 break;
812 case CS7:
813 config->bDataBits = TI_UART_7_DATA_BITS;
814 break;
815 default:
816 case CS8:
817 config->bDataBits = TI_UART_8_DATA_BITS;
818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
Alan Cox537699e2008-01-03 17:03:11 +0000821 /* CMSPAR isn't supported by this driver */
Alan Coxadc8d742012-07-14 15:31:47 +0100822 tty->termios.c_cflag &= ~CMSPAR;
Alan Cox537699e2008-01-03 17:03:11 +0000823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (cflag & PARENB) {
825 if (cflag & PARODD) {
826 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
827 config->bParity = TI_UART_ODD_PARITY;
828 } else {
829 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
830 config->bParity = TI_UART_EVEN_PARITY;
831 }
832 } else {
833 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
Alan Coxa30fa792008-07-22 11:15:17 +0100834 config->bParity = TI_UART_NO_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 if (cflag & CSTOPB)
838 config->bStopBits = TI_UART_2_STOP_BITS;
839 else
840 config->bStopBits = TI_UART_1_STOP_BITS;
841
842 if (cflag & CRTSCTS) {
843 /* RTS flow control must be off to drop RTS for baud rate B0 */
844 if ((cflag & CBAUD) != B0)
845 config->wFlags |= TI_UART_ENABLE_RTS_IN;
846 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
847 } else {
848 tty->hw_stopped = 0;
849 ti_restart_read(tport, tty);
850 }
851
852 if (I_IXOFF(tty) || I_IXON(tty)) {
853 config->cXon = START_CHAR(tty);
854 config->cXoff = STOP_CHAR(tty);
855
856 if (I_IXOFF(tty))
857 config->wFlags |= TI_UART_ENABLE_X_IN;
858 else
859 ti_restart_read(tport, tty);
860
861 if (I_IXON(tty))
862 config->wFlags |= TI_UART_ENABLE_X_OUT;
863 }
864
865 baud = tty_get_baud_rate(tty);
Alan Cox537699e2008-01-03 17:03:11 +0000866 if (!baud)
867 baud = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (tport->tp_tdev->td_is_3410)
869 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
870 else
871 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
872
Alan Cox537699e2008-01-03 17:03:11 +0000873 /* FIXME: Should calculate resulting baud here and report it back */
874 if ((cflag & CBAUD) != B0)
875 tty_encode_baud_rate(tty, baud, baud);
876
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700877 dev_dbg(&port->dev,
878 "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
879 __func__, baud, config->wBaudRate, config->wFlags,
880 config->bDataBits, config->bParity, config->bStopBits,
881 config->cXon, config->cXoff, config->bUartMode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 cpu_to_be16s(&config->wBaudRate);
884 cpu_to_be16s(&config->wFlags);
885
886 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
887 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
888 sizeof(*config));
889 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100890 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
891 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
894 mcr = tport->tp_shadow_mcr;
895 /* if baud rate is B0, clear RTS and DTR */
896 if ((cflag & CBAUD) == B0)
897 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
898 status = ti_set_mcr(tport, mcr);
899 if (status)
Alan Coxa30fa792008-07-22 11:15:17 +0100900 dev_err(&port->dev,
901 "%s - cannot set modem control on port %d, %d\n",
902 __func__, port_number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 kfree(config);
905}
906
907
Alan Cox60b33c12011-02-14 16:26:14 +0000908static int ti_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Alan Cox95da3102008-07-22 11:09:07 +0100910 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct ti_port *tport = usb_get_serial_port_data(port);
912 unsigned int result;
913 unsigned int msr;
914 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000915 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (tport == NULL)
918 return -ENODEV;
919
Alan Cox04ca89d2008-02-20 21:41:40 +0000920 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 msr = tport->tp_msr;
922 mcr = tport->tp_shadow_mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000923 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
926 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
927 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
928 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
929 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
930 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
931 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
932
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700933 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 return result;
936}
937
938
Alan Cox20b9d172011-02-14 16:26:50 +0000939static int ti_tiocmset(struct tty_struct *tty,
940 unsigned int set, unsigned int clear)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Alan Cox95da3102008-07-22 11:09:07 +0100942 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 struct ti_port *tport = usb_get_serial_port_data(port);
944 unsigned int mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +0000945 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (tport == NULL)
948 return -ENODEV;
949
Alan Cox04ca89d2008-02-20 21:41:40 +0000950 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 mcr = tport->tp_shadow_mcr;
952
953 if (set & TIOCM_RTS)
954 mcr |= TI_MCR_RTS;
955 if (set & TIOCM_DTR)
956 mcr |= TI_MCR_DTR;
957 if (set & TIOCM_LOOP)
958 mcr |= TI_MCR_LOOP;
959
960 if (clear & TIOCM_RTS)
961 mcr &= ~TI_MCR_RTS;
962 if (clear & TIOCM_DTR)
963 mcr &= ~TI_MCR_DTR;
964 if (clear & TIOCM_LOOP)
965 mcr &= ~TI_MCR_LOOP;
Alan Cox04ca89d2008-02-20 21:41:40 +0000966 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 return ti_set_mcr(tport, mcr);
969}
970
971
Alan Cox95da3102008-07-22 11:09:07 +0100972static void ti_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Alan Cox95da3102008-07-22 11:09:07 +0100974 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 struct ti_port *tport = usb_get_serial_port_data(port);
976 int status;
977
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700978 dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (tport == NULL)
981 return;
982
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700983 status = ti_write_byte(port, tport->tp_tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
985 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
986
987 if (status)
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -0700988 dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991
David Howells7d12e782006-10-05 14:55:46 +0100992static void ti_interrupt_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Ming Leicdc97792008-02-24 18:41:47 +0800994 struct ti_device *tdev = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct usb_serial_port *port;
996 struct usb_serial *serial = tdev->td_serial;
997 struct ti_port *tport;
998 struct device *dev = &urb->dev->dev;
999 unsigned char *data = urb->transfer_buffer;
1000 int length = urb->actual_length;
1001 int port_number;
1002 int function;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001003 int status = urb->status;
1004 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 __u8 msr;
1006
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001007 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 case 0:
1009 break;
1010 case -ECONNRESET:
1011 case -ENOENT:
1012 case -ESHUTDOWN:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001013 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 tdev->td_urb_error = 1;
1015 return;
1016 default:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001017 dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 tdev->td_urb_error = 1;
1019 goto exit;
1020 }
1021
1022 if (length != 2) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001023 dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 goto exit;
1025 }
1026
1027 if (data[0] == TI_CODE_HARDWARE_ERROR) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001028 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 goto exit;
1030 }
1031
1032 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1033 function = TI_GET_FUNC_FROM_CODE(data[0]);
1034
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001035 dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
1036 __func__, port_number, function, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (port_number >= serial->num_ports) {
Alan Coxa30fa792008-07-22 11:15:17 +01001039 dev_err(dev, "%s - bad port number, %d\n",
1040 __func__, port_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 goto exit;
1042 }
1043
1044 port = serial->port[port_number];
1045
1046 tport = usb_get_serial_port_data(port);
1047 if (!tport)
1048 goto exit;
1049
1050 switch (function) {
1051 case TI_CODE_DATA_ERROR:
Alan Coxa30fa792008-07-22 11:15:17 +01001052 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001053 __func__, port_number, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 break;
1055
1056 case TI_CODE_MODEM_STATUS:
1057 msr = data[1];
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001058 dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ti_handle_new_msr(tport, msr);
1060 break;
1061
1062 default:
Alan Coxa30fa792008-07-22 11:15:17 +01001063 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1064 __func__, data[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 break;
1066 }
1067
1068exit:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001069 retval = usb_submit_urb(urb, GFP_ATOMIC);
1070 if (retval)
1071 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001072 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075
David Howells7d12e782006-10-05 14:55:46 +01001076static void ti_bulk_in_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Ming Leicdc97792008-02-24 18:41:47 +08001078 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct usb_serial_port *port = tport->tp_port;
1080 struct device *dev = &urb->dev->dev;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001081 int status = urb->status;
1082 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001084 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 case 0:
1086 break;
1087 case -ECONNRESET:
1088 case -ENOENT:
1089 case -ESHUTDOWN:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001090 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 tport->tp_tdev->td_urb_error = 1;
1092 wake_up_interruptible(&tport->tp_write_wait);
1093 return;
1094 default:
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001095 dev_err(dev, "%s - nonzero urb status, %d\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001096 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 tport->tp_tdev->td_urb_error = 1;
1098 wake_up_interruptible(&tport->tp_write_wait);
1099 }
1100
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001101 if (status == -EPIPE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 goto exit;
1103
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001104 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001105 dev_err(dev, "%s - stopping read!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return;
1107 }
1108
Jiri Slaby2e124b42013-01-03 15:53:06 +01001109 if (urb->actual_length) {
1110 usb_serial_debug_data(dev, __func__, urb->actual_length,
1111 urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Jiri Slaby2e124b42013-01-03 15:53:06 +01001113 if (!tport->tp_is_open)
1114 dev_dbg(dev, "%s - port closed, dropping data\n",
1115 __func__);
1116 else
1117 ti_recv(port, urb->transfer_buffer, urb->actual_length);
1118 spin_lock(&tport->tp_lock);
Johan Hovold783ca352013-03-21 12:37:33 +01001119 port->icount.rx += urb->actual_length;
Jiri Slaby2e124b42013-01-03 15:53:06 +01001120 spin_unlock(&tport->tp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
1122
1123exit:
1124 /* continue to read unless stopping */
1125 spin_lock(&tport->tp_lock);
Johan Hovold58330412011-11-06 19:06:28 +01001126 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001127 retval = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold58330412011-11-06 19:06:28 +01001128 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
Johan Hovold58330412011-11-06 19:06:28 +01001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 spin_unlock(&tport->tp_lock);
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001132 if (retval)
1133 dev_err(dev, "%s - resubmit read urb failed, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001134 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137
David Howells7d12e782006-10-05 14:55:46 +01001138static void ti_bulk_out_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Ming Leicdc97792008-02-24 18:41:47 +08001140 struct ti_port *tport = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 struct usb_serial_port *port = tport->tp_port;
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001142 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 tport->tp_write_urb_in_use = 0;
1145
Greg Kroah-Hartman52171b42007-06-15 15:44:13 -07001146 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 case 0:
1148 break;
1149 case -ECONNRESET:
1150 case -ENOENT:
1151 case -ESHUTDOWN:
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001152 dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 tport->tp_tdev->td_urb_error = 1;
1154 wake_up_interruptible(&tport->tp_write_wait);
1155 return;
1156 default:
Johan Hovold22a416c2012-02-10 13:20:51 +01001157 dev_err_console(port, "%s - nonzero urb status, %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001158 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 tport->tp_tdev->td_urb_error = 1;
1160 wake_up_interruptible(&tport->tp_write_wait);
1161 }
1162
1163 /* send any buffered data */
1164 ti_send(tport);
1165}
1166
1167
Jiri Slaby2e124b42013-01-03 15:53:06 +01001168static void ti_recv(struct usb_serial_port *port, unsigned char *data,
1169 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 int cnt;
1172
1173 do {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001174 cnt = tty_insert_flip_string(&port->port, data, length);
Alan Cox33f0f882006-01-09 20:54:13 -08001175 if (cnt < length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +01001176 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001177 __func__, length - cnt);
1178 if (cnt == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001179 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
Jiri Slaby2e124b42013-01-03 15:53:06 +01001181 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 data += cnt;
1183 length -= cnt;
1184 } while (length > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187
1188static void ti_send(struct ti_port *tport)
1189{
1190 int count, result;
1191 struct usb_serial_port *port = tport->tp_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 unsigned long flags;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 spin_lock_irqsave(&tport->tp_lock, flags);
1195
Alan Cox4a90f092008-10-13 10:39:46 +01001196 if (tport->tp_write_urb_in_use)
1197 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Johan Hovold074ef652010-05-19 00:01:35 +02001199 count = kfifo_out(&tport->write_fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 port->write_urb->transfer_buffer,
1201 port->bulk_out_size);
1202
Alan Cox4a90f092008-10-13 10:39:46 +01001203 if (count == 0)
1204 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 tport->tp_write_urb_in_use = 1;
1207
1208 spin_unlock_irqrestore(&tport->tp_lock, flags);
1209
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001210 usb_serial_debug_data(&port->dev, __func__, count,
1211 port->write_urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1214 usb_sndbulkpipe(port->serial->dev,
1215 port->bulk_out_endpointAddress),
1216 port->write_urb->transfer_buffer, count,
1217 ti_bulk_out_callback, tport);
1218
1219 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1220 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001221 dev_err_console(port, "%s - submit write urb failed, %d\n",
Alan Coxa30fa792008-07-22 11:15:17 +01001222 __func__, result);
1223 tport->tp_write_urb_in_use = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /* TODO: reschedule ti_send */
1225 } else {
1226 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold783ca352013-03-21 12:37:33 +01001227 port->icount.tx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 spin_unlock_irqrestore(&tport->tp_lock, flags);
1229 }
1230
1231 /* more room in the buffer for new writes, wakeup */
Jiri Slaby6aad04f2013-03-07 13:12:29 +01001232 tty_port_tty_wakeup(&port->port);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 wake_up_interruptible(&tport->tp_write_wait);
Alan Cox4a90f092008-10-13 10:39:46 +01001235 return;
1236unlock:
1237 spin_unlock_irqrestore(&tport->tp_lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +01001238 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
1241
1242static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1243{
Alan Cox04ca89d2008-02-20 21:41:40 +00001244 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int status;
1246
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001247 status = ti_write_byte(tport->tp_port, tport->tp_tdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1249 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1250
Alan Cox04ca89d2008-02-20 21:41:40 +00001251 spin_lock_irqsave(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (!status)
1253 tport->tp_shadow_mcr = mcr;
Alan Cox04ca89d2008-02-20 21:41:40 +00001254 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 return status;
1257}
1258
1259
Johan Hovoldb5784f72013-04-18 17:33:20 +02001260static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Alan Coxa30fa792008-07-22 11:15:17 +01001262 int size, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 struct ti_device *tdev = tport->tp_tdev;
1264 struct usb_serial_port *port = tport->tp_port;
1265 int port_number = port->number - port->serial->minor;
1266 struct ti_port_status *data;
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 size = sizeof(struct ti_port_status);
1269 data = kmalloc(size, GFP_KERNEL);
1270 if (!data) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001271 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return -ENOMEM;
1273 }
1274
1275 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1276 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1277 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001278 dev_err(&port->dev,
1279 "%s - get port status command failed, %d\n",
1280 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto free_data;
1282 }
1283
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001284 dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Johan Hovoldb5784f72013-04-18 17:33:20 +02001286 *lsr = data->bLSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288free_data:
1289 kfree(data);
1290 return status;
1291}
1292
1293
1294static int ti_get_serial_info(struct ti_port *tport,
1295 struct serial_struct __user *ret_arg)
1296{
1297 struct usb_serial_port *port = tport->tp_port;
1298 struct serial_struct ret_serial;
Johan Hovoldf1175da2013-04-18 17:33:23 +02001299 unsigned cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if (!ret_arg)
1302 return -EFAULT;
1303
Johan Hovoldf1175da2013-04-18 17:33:23 +02001304 cwait = port->port.closing_wait;
1305 if (cwait != ASYNC_CLOSING_WAIT_NONE)
1306 cwait = jiffies_to_msecs(cwait) / 10;
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 memset(&ret_serial, 0, sizeof(ret_serial));
1309
1310 ret_serial.type = PORT_16550A;
1311 ret_serial.line = port->serial->minor;
1312 ret_serial.port = port->number - port->serial->minor;
1313 ret_serial.flags = tport->tp_flags;
1314 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1315 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
Johan Hovoldf1175da2013-04-18 17:33:23 +02001316 ret_serial.closing_wait = cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1319 return -EFAULT;
1320
1321 return 0;
1322}
1323
1324
Alan Cox4a90f092008-10-13 10:39:46 +01001325static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct serial_struct __user *new_arg)
1327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct serial_struct new_serial;
Johan Hovoldf1175da2013-04-18 17:33:23 +02001329 unsigned cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1332 return -EFAULT;
1333
Johan Hovoldf1175da2013-04-18 17:33:23 +02001334 cwait = new_serial.closing_wait;
1335 if (cwait != ASYNC_CLOSING_WAIT_NONE)
1336 cwait = msecs_to_jiffies(10 * new_serial.closing_wait);
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
Johan Hovoldf1175da2013-04-18 17:33:23 +02001339 tport->tp_port->port.closing_wait = cwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 return 0;
1342}
1343
1344
1345static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1346{
1347 struct async_icount *icount;
1348 struct tty_struct *tty;
1349 unsigned long flags;
1350
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001351 dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (msr & TI_MSR_DELTA_MASK) {
1354 spin_lock_irqsave(&tport->tp_lock, flags);
Johan Hovold783ca352013-03-21 12:37:33 +01001355 icount = &tport->tp_port->icount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (msr & TI_MSR_DELTA_CTS)
1357 icount->cts++;
1358 if (msr & TI_MSR_DELTA_DSR)
1359 icount->dsr++;
1360 if (msr & TI_MSR_DELTA_CD)
1361 icount->dcd++;
1362 if (msr & TI_MSR_DELTA_RI)
1363 icount->rng++;
Johan Hovold6c75e262013-03-21 12:37:34 +01001364 wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 spin_unlock_irqrestore(&tport->tp_lock, flags);
1366 }
1367
1368 tport->tp_msr = msr & TI_MSR_MASK;
1369
1370 /* handle CTS flow control */
Alan Cox4a90f092008-10-13 10:39:46 +01001371 tty = tty_port_tty_get(&tport->tp_port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (tty && C_CRTSCTS(tty)) {
1373 if (msr & TI_MSR_CTS) {
1374 tty->hw_stopped = 0;
1375 tty_wakeup(tty);
1376 } else {
1377 tty->hw_stopped = 1;
1378 }
1379 }
Alan Cox4a90f092008-10-13 10:39:46 +01001380 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1385{
1386 unsigned long flags;
1387
1388 spin_lock_irqsave(&tport->tp_lock, flags);
1389
1390 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1391 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1392
1393 spin_unlock_irqrestore(&tport->tp_lock, flags);
1394}
1395
1396
1397static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1398{
1399 struct urb *urb;
1400 int status = 0;
1401 unsigned long flags;
1402
1403 spin_lock_irqsave(&tport->tp_lock, flags);
1404
1405 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
Oliver Neukum944dc182007-05-07 08:33:18 +02001406 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 urb = tport->tp_port->read_urb;
Oliver Neukum944dc182007-05-07 08:33:18 +02001408 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 urb->context = tport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 status = usb_submit_urb(urb, GFP_KERNEL);
Oliver Neukum944dc182007-05-07 08:33:18 +02001411 } else {
1412 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1413 spin_unlock_irqrestore(&tport->tp_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 return status;
1417}
1418
1419
1420static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1421 __u16 moduleid, __u16 value, __u8 *data, int size)
1422{
1423 int status;
1424
1425 status = usb_control_msg(tdev->td_serial->dev,
1426 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1427 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1428 value, moduleid, data, size, 1000);
1429
1430 if (status == size)
1431 status = 0;
1432
1433 if (status > 0)
1434 status = -ECOMM;
1435
1436 return status;
1437}
1438
1439
1440static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1441 __u16 moduleid, __u16 value, __u8 *data, int size)
1442{
1443 int status;
1444
1445 status = usb_control_msg(tdev->td_serial->dev,
1446 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1447 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1448 value, moduleid, data, size, 1000);
1449
1450 if (status == size)
1451 status = 0;
1452
1453 if (status > 0)
1454 status = -ECOMM;
1455
1456 return status;
1457}
1458
1459
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001460static int ti_write_byte(struct usb_serial_port *port,
1461 struct ti_device *tdev, unsigned long addr,
1462 __u8 mask, __u8 byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 int status;
1465 unsigned int size;
1466 struct ti_write_data_bytes *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001468 dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1469 addr, mask, byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 size = sizeof(struct ti_write_data_bytes) + 2;
1472 data = kmalloc(size, GFP_KERNEL);
1473 if (!data) {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001474 dev_err(&port->dev, "%s - out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return -ENOMEM;
1476 }
1477
1478 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1479 data->bDataType = TI_RW_DATA_BYTE;
1480 data->bDataCounter = 1;
1481 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1482 data->wBaseAddrLo = cpu_to_be16(addr);
1483 data->bData[0] = mask;
1484 data->bData[1] = byte;
1485
1486 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1487 (__u8 *)data, size);
1488
1489 if (status < 0)
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001490 dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 kfree(data);
1493
1494 return status;
1495}
1496
Alan Cox95da3102008-07-22 11:09:07 +01001497static int ti_do_download(struct usb_device *dev, int pipe,
1498 u8 *buffer, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 int pos;
Alan Cox95da3102008-07-22 11:09:07 +01001501 u8 cs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 struct ti_firmware_header *header;
Oliver Neukum87ea8c82009-06-30 09:44:24 +02001504 int status = 0;
Alan Cox95da3102008-07-22 11:09:07 +01001505 int len;
Alan Coxa30fa792008-07-22 11:15:17 +01001506
1507 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 cs = (__u8)(cs + buffer[pos]);
1509
1510 header = (struct ti_firmware_header *)buffer;
Alan Coxa30fa792008-07-22 11:15:17 +01001511 header->wLength = cpu_to_le16((__u16)(size
Alan Cox95da3102008-07-22 11:09:07 +01001512 - sizeof(struct ti_firmware_header)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 header->bCheckSum = cs;
1514
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001515 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +01001516 for (pos = 0; pos < size; pos += done) {
1517 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1518 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1519 &done, 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (status)
1521 break;
1522 }
Alan Cox95da3102008-07-22 11:09:07 +01001523 return status;
1524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Chris Adams05a3d902009-01-11 19:48:53 +00001526static int ti_download_firmware(struct ti_device *tdev)
Alan Cox95da3102008-07-22 11:09:07 +01001527{
Chris Adams05a3d902009-01-11 19:48:53 +00001528 int status;
Alan Cox95da3102008-07-22 11:09:07 +01001529 int buffer_size;
1530 __u8 *buffer;
1531 struct usb_device *dev = tdev->td_serial->dev;
1532 unsigned int pipe = usb_sndbulkpipe(dev,
1533 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1534 const struct firmware *fw_p;
1535 char buf[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Chris Adams05a3d902009-01-11 19:48:53 +00001537 /* try ID specific firmware first, then try generic firmware */
1538 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1539 dev->descriptor.idProduct);
Adhir Ramjiawan72b27a02012-04-09 14:01:38 +02001540 status = request_firmware(&fw_p, buf, &dev->dev);
1541
1542 if (status != 0) {
Chris Adamscb7a7c62009-01-11 19:49:00 +00001543 buf[0] = '\0';
1544 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1545 switch (dev->descriptor.idProduct) {
1546 case MTS_CDMA_PRODUCT_ID:
1547 strcpy(buf, "mts_cdma.fw");
1548 break;
1549 case MTS_GSM_PRODUCT_ID:
1550 strcpy(buf, "mts_gsm.fw");
1551 break;
1552 case MTS_EDGE_PRODUCT_ID:
1553 strcpy(buf, "mts_edge.fw");
1554 break;
Alex Manoussakiscdc04832010-04-22 15:18:20 -07001555 case MTS_MT9234MU_PRODUCT_ID:
1556 strcpy(buf, "mts_mt9234mu.fw");
1557 break;
1558 case MTS_MT9234ZBA_PRODUCT_ID:
1559 strcpy(buf, "mts_mt9234zba.fw");
1560 break;
1561 case MTS_MT9234ZBAOLD_PRODUCT_ID:
1562 strcpy(buf, "mts_mt9234zba.fw");
1563 break; }
Chris Adamscb7a7c62009-01-11 19:49:00 +00001564 }
1565 if (buf[0] == '\0') {
1566 if (tdev->td_is_3410)
1567 strcpy(buf, "ti_3410.fw");
1568 else
1569 strcpy(buf, "ti_5052.fw");
1570 }
Chris Adams05a3d902009-01-11 19:48:53 +00001571 status = request_firmware(&fw_p, buf, &dev->dev);
1572 }
1573 if (status) {
Alan Cox95da3102008-07-22 11:09:07 +01001574 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1575 return -ENOENT;
1576 }
1577 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
Randy Dunlap75181f32010-04-15 11:38:56 -07001578 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
Jesper Juhl357f45d2011-06-13 22:50:41 +02001579 release_firmware(fw_p);
Alan Cox95da3102008-07-22 11:09:07 +01001580 return -ENOENT;
1581 }
1582
1583 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1584 buffer = kmalloc(buffer_size, GFP_KERNEL);
1585 if (buffer) {
1586 memcpy(buffer, fw_p->data, fw_p->size);
1587 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
Chris Adams2bcbe4c12008-09-10 14:11:38 -07001588 status = ti_do_download(dev, pipe, buffer, fw_p->size);
Alan Cox95da3102008-07-22 11:09:07 +01001589 kfree(buffer);
Chris Adams05a3d902009-01-11 19:48:53 +00001590 } else {
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001591 dev_dbg(&dev->dev, "%s ENOMEM\n", __func__);
Chris Adams05a3d902009-01-11 19:48:53 +00001592 status = -ENOMEM;
Alan Cox95da3102008-07-22 11:09:07 +01001593 }
1594 release_firmware(fw_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (status) {
Alan Coxa30fa792008-07-22 11:15:17 +01001596 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1597 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return status;
1599 }
1600
Greg Kroah-Hartmand53839c2012-09-14 15:08:36 -07001601 dev_dbg(&dev->dev, "%s - download successful\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 return 0;
1604}