GNU Linux-libre 6.7.9-gnu
[releases.git] / drivers / usb / serial / ti_usb_3410_5052.c
1 // SPDX-License-Identifier: GPL-2.0+
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  * For questions or problems with this driver, contact Texas Instruments
12  * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
13  * Peter Berger <pberger@brimson.com>.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/firmware.h>
19 #include <linux/slab.h>
20 #include <linux/tty.h>
21 #include <linux/tty_driver.h>
22 #include <linux/tty_flip.h>
23 #include <linux/module.h>
24 #include <linux/spinlock.h>
25 #include <linux/ioctl.h>
26 #include <linux/serial.h>
27 #include <linux/kfifo.h>
28 #include <linux/mutex.h>
29 #include <linux/uaccess.h>
30 #include <linux/usb.h>
31 #include <linux/usb/serial.h>
32
33 /* Configuration ids */
34 #define TI_BOOT_CONFIG                  1
35 #define TI_ACTIVE_CONFIG                2
36
37 /* Vendor and product ids */
38 #define TI_VENDOR_ID                    0x0451
39 #define IBM_VENDOR_ID                   0x04b3
40 #define STARTECH_VENDOR_ID              0x14b0
41 #define TI_3410_PRODUCT_ID              0x3410
42 #define IBM_4543_PRODUCT_ID             0x4543
43 #define IBM_454B_PRODUCT_ID             0x454b
44 #define IBM_454C_PRODUCT_ID             0x454c
45 #define TI_3410_EZ430_ID                0xF430  /* TI ez430 development tool */
46 #define TI_5052_BOOT_PRODUCT_ID         0x5052  /* no EEPROM, no firmware */
47 #define TI_5152_BOOT_PRODUCT_ID         0x5152  /* no EEPROM, no firmware */
48 #define TI_5052_EEPROM_PRODUCT_ID       0x505A  /* EEPROM, no firmware */
49 #define TI_5052_FIRMWARE_PRODUCT_ID     0x505F  /* firmware is running */
50 #define FRI2_PRODUCT_ID                 0x5053  /* Fish River Island II */
51
52 /* Multi-Tech vendor and product ids */
53 #define MTS_VENDOR_ID                   0x06E0
54 #define MTS_GSM_NO_FW_PRODUCT_ID        0xF108
55 #define MTS_CDMA_NO_FW_PRODUCT_ID       0xF109
56 #define MTS_CDMA_PRODUCT_ID             0xF110
57 #define MTS_GSM_PRODUCT_ID              0xF111
58 #define MTS_EDGE_PRODUCT_ID             0xF112
59 #define MTS_MT9234MU_PRODUCT_ID         0xF114
60 #define MTS_MT9234ZBA_PRODUCT_ID        0xF115
61 #define MTS_MT9234ZBAOLD_PRODUCT_ID     0x0319
62
63 /* Abbott Diabetics vendor and product ids */
64 #define ABBOTT_VENDOR_ID                0x1a61
65 #define ABBOTT_STEREO_PLUG_ID           0x3410
66 #define ABBOTT_PRODUCT_ID               ABBOTT_STEREO_PLUG_ID
67 #define ABBOTT_STRIP_PORT_ID            0x3420
68
69 /* Honeywell vendor and product IDs */
70 #define HONEYWELL_VENDOR_ID             0x10ac
71 #define HONEYWELL_HGI80_PRODUCT_ID      0x0102  /* Honeywell HGI80 */
72
73 /* Moxa UPORT 11x0 vendor and product IDs */
74 #define MXU1_VENDOR_ID                          0x110a
75 #define MXU1_1110_PRODUCT_ID                    0x1110
76 #define MXU1_1130_PRODUCT_ID                    0x1130
77 #define MXU1_1150_PRODUCT_ID                    0x1150
78 #define MXU1_1151_PRODUCT_ID                    0x1151
79 #define MXU1_1131_PRODUCT_ID                    0x1131
80
81 /* Commands */
82 #define TI_GET_VERSION                  0x01
83 #define TI_GET_PORT_STATUS              0x02
84 #define TI_GET_PORT_DEV_INFO            0x03
85 #define TI_GET_CONFIG                   0x04
86 #define TI_SET_CONFIG                   0x05
87 #define TI_OPEN_PORT                    0x06
88 #define TI_CLOSE_PORT                   0x07
89 #define TI_START_PORT                   0x08
90 #define TI_STOP_PORT                    0x09
91 #define TI_TEST_PORT                    0x0A
92 #define TI_PURGE_PORT                   0x0B
93 #define TI_RESET_EXT_DEVICE             0x0C
94 #define TI_WRITE_DATA                   0x80
95 #define TI_READ_DATA                    0x81
96 #define TI_REQ_TYPE_CLASS               0x82
97
98 /* Module identifiers */
99 #define TI_I2C_PORT                     0x01
100 #define TI_IEEE1284_PORT                0x02
101 #define TI_UART1_PORT                   0x03
102 #define TI_UART2_PORT                   0x04
103 #define TI_RAM_PORT                     0x05
104
105 /* Modem status */
106 #define TI_MSR_DELTA_CTS                0x01
107 #define TI_MSR_DELTA_DSR                0x02
108 #define TI_MSR_DELTA_RI                 0x04
109 #define TI_MSR_DELTA_CD                 0x08
110 #define TI_MSR_CTS                      0x10
111 #define TI_MSR_DSR                      0x20
112 #define TI_MSR_RI                       0x40
113 #define TI_MSR_CD                       0x80
114 #define TI_MSR_DELTA_MASK               0x0F
115 #define TI_MSR_MASK                     0xF0
116
117 /* Line status */
118 #define TI_LSR_OVERRUN_ERROR            0x01
119 #define TI_LSR_PARITY_ERROR             0x02
120 #define TI_LSR_FRAMING_ERROR            0x04
121 #define TI_LSR_BREAK                    0x08
122 #define TI_LSR_ERROR                    0x0F
123 #define TI_LSR_RX_FULL                  0x10
124 #define TI_LSR_TX_EMPTY                 0x20
125 #define TI_LSR_TX_EMPTY_BOTH            0x40
126
127 /* Line control */
128 #define TI_LCR_BREAK                    0x40
129
130 /* Modem control */
131 #define TI_MCR_LOOP                     0x04
132 #define TI_MCR_DTR                      0x10
133 #define TI_MCR_RTS                      0x20
134
135 /* Mask settings */
136 #define TI_UART_ENABLE_RTS_IN           0x0001
137 #define TI_UART_DISABLE_RTS             0x0002
138 #define TI_UART_ENABLE_PARITY_CHECKING  0x0008
139 #define TI_UART_ENABLE_DSR_OUT          0x0010
140 #define TI_UART_ENABLE_CTS_OUT          0x0020
141 #define TI_UART_ENABLE_X_OUT            0x0040
142 #define TI_UART_ENABLE_XA_OUT           0x0080
143 #define TI_UART_ENABLE_X_IN             0x0100
144 #define TI_UART_ENABLE_DTR_IN           0x0800
145 #define TI_UART_DISABLE_DTR             0x1000
146 #define TI_UART_ENABLE_MS_INTS          0x2000
147 #define TI_UART_ENABLE_AUTO_START_DMA   0x4000
148
149 /* Parity */
150 #define TI_UART_NO_PARITY               0x00
151 #define TI_UART_ODD_PARITY              0x01
152 #define TI_UART_EVEN_PARITY             0x02
153 #define TI_UART_MARK_PARITY             0x03
154 #define TI_UART_SPACE_PARITY            0x04
155
156 /* Stop bits */
157 #define TI_UART_1_STOP_BITS             0x00
158 #define TI_UART_1_5_STOP_BITS           0x01
159 #define TI_UART_2_STOP_BITS             0x02
160
161 /* Bits per character */
162 #define TI_UART_5_DATA_BITS             0x00
163 #define TI_UART_6_DATA_BITS             0x01
164 #define TI_UART_7_DATA_BITS             0x02
165 #define TI_UART_8_DATA_BITS             0x03
166
167 /* 232/485 modes */
168 #define TI_UART_232                     0x00
169 #define TI_UART_485_RECEIVER_DISABLED   0x01
170 #define TI_UART_485_RECEIVER_ENABLED    0x02
171
172 /* Pipe transfer mode and timeout */
173 #define TI_PIPE_MODE_CONTINUOUS         0x01
174 #define TI_PIPE_MODE_MASK               0x03
175 #define TI_PIPE_TIMEOUT_MASK            0x7C
176 #define TI_PIPE_TIMEOUT_ENABLE          0x80
177
178 /* Config struct */
179 struct ti_uart_config {
180         __be16  wBaudRate;
181         __be16  wFlags;
182         u8      bDataBits;
183         u8      bParity;
184         u8      bStopBits;
185         char    cXon;
186         char    cXoff;
187         u8      bUartMode;
188 };
189
190 /* Get port status */
191 struct ti_port_status {
192         u8 bCmdCode;
193         u8 bModuleId;
194         u8 bErrorCode;
195         u8 bMSR;
196         u8 bLSR;
197 };
198
199 /* Purge modes */
200 #define TI_PURGE_OUTPUT                 0x00
201 #define TI_PURGE_INPUT                  0x80
202
203 /* Read/Write data */
204 #define TI_RW_DATA_ADDR_SFR             0x10
205 #define TI_RW_DATA_ADDR_IDATA           0x20
206 #define TI_RW_DATA_ADDR_XDATA           0x30
207 #define TI_RW_DATA_ADDR_CODE            0x40
208 #define TI_RW_DATA_ADDR_GPIO            0x50
209 #define TI_RW_DATA_ADDR_I2C             0x60
210 #define TI_RW_DATA_ADDR_FLASH           0x70
211 #define TI_RW_DATA_ADDR_DSP             0x80
212
213 #define TI_RW_DATA_UNSPECIFIED          0x00
214 #define TI_RW_DATA_BYTE                 0x01
215 #define TI_RW_DATA_WORD                 0x02
216 #define TI_RW_DATA_DOUBLE_WORD          0x04
217
218 struct ti_write_data_bytes {
219         u8      bAddrType;
220         u8      bDataType;
221         u8      bDataCounter;
222         __be16  wBaseAddrHi;
223         __be16  wBaseAddrLo;
224         u8      bData[];
225 } __packed;
226
227 struct ti_read_data_request {
228         u8      bAddrType;
229         u8      bDataType;
230         u8      bDataCounter;
231         __be16  wBaseAddrHi;
232         __be16  wBaseAddrLo;
233 } __packed;
234
235 struct ti_read_data_bytes {
236         u8      bCmdCode;
237         u8      bModuleId;
238         u8      bErrorCode;
239         u8      bData[];
240 };
241
242 /* Interrupt struct */
243 struct ti_interrupt {
244         u8      bICode;
245         u8      bIInfo;
246 };
247
248 /* Interrupt codes */
249 #define TI_CODE_HARDWARE_ERROR          0xFF
250 #define TI_CODE_DATA_ERROR              0x03
251 #define TI_CODE_MODEM_STATUS            0x04
252
253 /* Download firmware max packet size */
254 #define TI_DOWNLOAD_MAX_PACKET_SIZE     64
255
256 /* Firmware image header */
257 struct ti_firmware_header {
258         __le16  wLength;
259         u8      bCheckSum;
260 } __packed;
261
262 /* UART addresses */
263 #define TI_UART1_BASE_ADDR              0xFFA0  /* UART 1 base address */
264 #define TI_UART2_BASE_ADDR              0xFFB0  /* UART 2 base address */
265 #define TI_UART_OFFSET_LCR              0x0002  /* UART MCR register offset */
266 #define TI_UART_OFFSET_MCR              0x0004  /* UART MCR register offset */
267
268 #define TI_DRIVER_AUTHOR        "Al Borchers <alborchers@steinerpoint.com>"
269 #define TI_DRIVER_DESC          "TI USB 3410/5052 Serial Driver"
270
271 #define TI_FIRMWARE_BUF_SIZE    16284
272
273 #define TI_TRANSFER_TIMEOUT     2
274
275 /* read urb states */
276 #define TI_READ_URB_RUNNING     0
277 #define TI_READ_URB_STOPPING    1
278 #define TI_READ_URB_STOPPED     2
279
280 #define TI_EXTRA_VID_PID_COUNT  5
281
282 struct ti_port {
283         int                     tp_is_open;
284         u8                      tp_msr;
285         u8                      tp_shadow_mcr;
286         u8                      tp_uart_mode;   /* 232 or 485 modes */
287         unsigned int            tp_uart_base_addr;
288         struct ti_device        *tp_tdev;
289         struct usb_serial_port  *tp_port;
290         spinlock_t              tp_lock;
291         int                     tp_read_urb_state;
292         int                     tp_write_urb_in_use;
293 };
294
295 struct ti_device {
296         struct mutex            td_open_close_lock;
297         int                     td_open_port_count;
298         struct usb_serial       *td_serial;
299         int                     td_is_3410;
300         bool                    td_rs485_only;
301 };
302
303 static int ti_startup(struct usb_serial *serial);
304 static void ti_release(struct usb_serial *serial);
305 static int ti_port_probe(struct usb_serial_port *port);
306 static void ti_port_remove(struct usb_serial_port *port);
307 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
308 static void ti_close(struct usb_serial_port *port);
309 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
310                 const unsigned char *data, int count);
311 static unsigned int ti_write_room(struct tty_struct *tty);
312 static unsigned int ti_chars_in_buffer(struct tty_struct *tty);
313 static bool ti_tx_empty(struct usb_serial_port *port);
314 static void ti_throttle(struct tty_struct *tty);
315 static void ti_unthrottle(struct tty_struct *tty);
316 static void ti_set_termios(struct tty_struct *tty,
317                            struct usb_serial_port *port,
318                            const struct ktermios *old_termios);
319 static int ti_tiocmget(struct tty_struct *tty);
320 static int ti_tiocmset(struct tty_struct *tty,
321                 unsigned int set, unsigned int clear);
322 static int ti_break(struct tty_struct *tty, int break_state);
323 static void ti_interrupt_callback(struct urb *urb);
324 static void ti_bulk_in_callback(struct urb *urb);
325 static void ti_bulk_out_callback(struct urb *urb);
326
327 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
328                 int length);
329 static void ti_send(struct ti_port *tport);
330 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
331 static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
332 static void ti_get_serial_info(struct tty_struct *tty, struct serial_struct *ss);
333 static void ti_handle_new_msr(struct ti_port *tport, u8 msr);
334
335 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
336 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
337
338 static int ti_command_out_sync(struct usb_device *udev, u8 command,
339                 u16 moduleid, u16 value, void *data, int size);
340 static int ti_command_in_sync(struct usb_device *udev, u8 command,
341                 u16 moduleid, u16 value, void *data, int size);
342 static int ti_port_cmd_out(struct usb_serial_port *port, u8 command,
343                 u16 value, void *data, int size);
344 static int ti_port_cmd_in(struct usb_serial_port *port, u8 command,
345                 u16 value, void *data, int size);
346
347 static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
348                          unsigned long addr, u8 mask, u8 byte);
349
350 static int ti_download_firmware(struct ti_device *tdev);
351
352 static const struct usb_device_id ti_id_table_3410[] = {
353         { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
354         { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
355         { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
356         { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
357         { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
358         { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
359         { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
360         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
361         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
362         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
363         { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
364         { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
365         { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
366         { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STEREO_PLUG_ID) },
367         { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
368         { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
369         { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
370         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1110_PRODUCT_ID) },
371         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1130_PRODUCT_ID) },
372         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1131_PRODUCT_ID) },
373         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1150_PRODUCT_ID) },
374         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1151_PRODUCT_ID) },
375         { USB_DEVICE(STARTECH_VENDOR_ID, TI_3410_PRODUCT_ID) },
376         { }     /* terminator */
377 };
378
379 static const struct usb_device_id ti_id_table_5052[] = {
380         { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
381         { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
382         { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
383         { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
384         { }
385 };
386
387 static const struct usb_device_id ti_id_table_combined[] = {
388         { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
389         { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
390         { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
391         { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
392         { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
393         { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
394         { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
395         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
396         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
397         { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
398         { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
399         { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
400         { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
401         { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
402         { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
403         { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
404         { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
405         { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
406         { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
407         { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
408         { USB_DEVICE(HONEYWELL_VENDOR_ID, HONEYWELL_HGI80_PRODUCT_ID) },
409         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1110_PRODUCT_ID) },
410         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1130_PRODUCT_ID) },
411         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1131_PRODUCT_ID) },
412         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1150_PRODUCT_ID) },
413         { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1151_PRODUCT_ID) },
414         { USB_DEVICE(STARTECH_VENDOR_ID, TI_3410_PRODUCT_ID) },
415         { }     /* terminator */
416 };
417
418 static struct usb_serial_driver ti_1port_device = {
419         .driver = {
420                 .owner          = THIS_MODULE,
421                 .name           = "ti_usb_3410_5052_1",
422         },
423         .description            = "TI USB 3410 1 port adapter",
424         .id_table               = ti_id_table_3410,
425         .num_ports              = 1,
426         .num_bulk_out           = 1,
427         .attach                 = ti_startup,
428         .release                = ti_release,
429         .port_probe             = ti_port_probe,
430         .port_remove            = ti_port_remove,
431         .open                   = ti_open,
432         .close                  = ti_close,
433         .write                  = ti_write,
434         .write_room             = ti_write_room,
435         .chars_in_buffer        = ti_chars_in_buffer,
436         .tx_empty               = ti_tx_empty,
437         .throttle               = ti_throttle,
438         .unthrottle             = ti_unthrottle,
439         .get_serial             = ti_get_serial_info,
440         .set_termios            = ti_set_termios,
441         .tiocmget               = ti_tiocmget,
442         .tiocmset               = ti_tiocmset,
443         .tiocmiwait             = usb_serial_generic_tiocmiwait,
444         .get_icount             = usb_serial_generic_get_icount,
445         .break_ctl              = ti_break,
446         .read_int_callback      = ti_interrupt_callback,
447         .read_bulk_callback     = ti_bulk_in_callback,
448         .write_bulk_callback    = ti_bulk_out_callback,
449 };
450
451 static struct usb_serial_driver ti_2port_device = {
452         .driver = {
453                 .owner          = THIS_MODULE,
454                 .name           = "ti_usb_3410_5052_2",
455         },
456         .description            = "TI USB 5052 2 port adapter",
457         .id_table               = ti_id_table_5052,
458         .num_ports              = 2,
459         .num_bulk_out           = 1,
460         .attach                 = ti_startup,
461         .release                = ti_release,
462         .port_probe             = ti_port_probe,
463         .port_remove            = ti_port_remove,
464         .open                   = ti_open,
465         .close                  = ti_close,
466         .write                  = ti_write,
467         .write_room             = ti_write_room,
468         .chars_in_buffer        = ti_chars_in_buffer,
469         .tx_empty               = ti_tx_empty,
470         .throttle               = ti_throttle,
471         .unthrottle             = ti_unthrottle,
472         .get_serial             = ti_get_serial_info,
473         .set_termios            = ti_set_termios,
474         .tiocmget               = ti_tiocmget,
475         .tiocmset               = ti_tiocmset,
476         .tiocmiwait             = usb_serial_generic_tiocmiwait,
477         .get_icount             = usb_serial_generic_get_icount,
478         .break_ctl              = ti_break,
479         .read_int_callback      = ti_interrupt_callback,
480         .read_bulk_callback     = ti_bulk_in_callback,
481         .write_bulk_callback    = ti_bulk_out_callback,
482 };
483
484 static struct usb_serial_driver * const serial_drivers[] = {
485         &ti_1port_device, &ti_2port_device, NULL
486 };
487
488 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
489 MODULE_DESCRIPTION(TI_DRIVER_DESC);
490 MODULE_LICENSE("GPL");
491
492 /*(DEBLOBBED)*/
493
494 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
495
496 module_usb_serial_driver(serial_drivers, ti_id_table_combined);
497
498 static int ti_startup(struct usb_serial *serial)
499 {
500         struct ti_device *tdev;
501         struct usb_device *dev = serial->dev;
502         struct usb_host_interface *cur_altsetting;
503         int num_endpoints;
504         u16 vid, pid;
505         int status;
506
507         dev_dbg(&dev->dev,
508                 "%s - product 0x%4X, num configurations %d, configuration value %d\n",
509                 __func__, le16_to_cpu(dev->descriptor.idProduct),
510                 dev->descriptor.bNumConfigurations,
511                 dev->actconfig->desc.bConfigurationValue);
512
513         tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
514         if (!tdev)
515                 return -ENOMEM;
516
517         mutex_init(&tdev->td_open_close_lock);
518         tdev->td_serial = serial;
519         usb_set_serial_data(serial, tdev);
520
521         /* determine device type */
522         if (serial->type == &ti_1port_device)
523                 tdev->td_is_3410 = 1;
524         dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
525                 tdev->td_is_3410 ? "3410" : "5052");
526
527         vid = le16_to_cpu(dev->descriptor.idVendor);
528         pid = le16_to_cpu(dev->descriptor.idProduct);
529         if (vid == MXU1_VENDOR_ID) {
530                 switch (pid) {
531                 case MXU1_1130_PRODUCT_ID:
532                 case MXU1_1131_PRODUCT_ID:
533                         tdev->td_rs485_only = true;
534                         break;
535                 }
536         }
537
538         cur_altsetting = serial->interface->cur_altsetting;
539         num_endpoints = cur_altsetting->desc.bNumEndpoints;
540
541         /* if we have only 1 configuration and 1 endpoint, download firmware */
542         if (dev->descriptor.bNumConfigurations == 1 && num_endpoints == 1) {
543                 status = ti_download_firmware(tdev);
544
545                 if (status != 0)
546                         goto free_tdev;
547
548                 /* 3410 must be reset, 5052 resets itself */
549                 if (tdev->td_is_3410) {
550                         msleep_interruptible(100);
551                         usb_reset_device(dev);
552                 }
553
554                 status = -ENODEV;
555                 goto free_tdev;
556         }
557
558         /* the second configuration must be set */
559         if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
560                 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
561                 status = status ? status : -ENODEV;
562                 goto free_tdev;
563         }
564
565         if (serial->num_bulk_in < serial->num_ports ||
566                         serial->num_bulk_out < serial->num_ports) {
567                 dev_err(&serial->interface->dev, "missing endpoints\n");
568                 status = -ENODEV;
569                 goto free_tdev;
570         }
571
572         return 0;
573
574 free_tdev:
575         kfree(tdev);
576         usb_set_serial_data(serial, NULL);
577         return status;
578 }
579
580
581 static void ti_release(struct usb_serial *serial)
582 {
583         struct ti_device *tdev = usb_get_serial_data(serial);
584
585         kfree(tdev);
586 }
587
588 static int ti_port_probe(struct usb_serial_port *port)
589 {
590         struct ti_port *tport;
591
592         tport = kzalloc(sizeof(*tport), GFP_KERNEL);
593         if (!tport)
594                 return -ENOMEM;
595
596         spin_lock_init(&tport->tp_lock);
597         if (port == port->serial->port[0])
598                 tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
599         else
600                 tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
601         tport->tp_port = port;
602         tport->tp_tdev = usb_get_serial_data(port->serial);
603
604         if (tport->tp_tdev->td_rs485_only)
605                 tport->tp_uart_mode = TI_UART_485_RECEIVER_DISABLED;
606         else
607                 tport->tp_uart_mode = TI_UART_232;
608
609         usb_set_serial_port_data(port, tport);
610
611         /*
612          * The TUSB5052 LSR does not tell when the transmitter shift register
613          * has emptied so add a one-character drain delay.
614          */
615         if (!tport->tp_tdev->td_is_3410)
616                 port->port.drain_delay = 1;
617
618         return 0;
619 }
620
621 static void ti_port_remove(struct usb_serial_port *port)
622 {
623         struct ti_port *tport;
624
625         tport = usb_get_serial_port_data(port);
626         kfree(tport);
627 }
628
629 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
630 {
631         struct ti_port *tport = usb_get_serial_port_data(port);
632         struct ti_device *tdev;
633         struct usb_device *dev;
634         struct urb *urb;
635         int status;
636         u16 open_settings;
637
638         open_settings = (TI_PIPE_MODE_CONTINUOUS |
639                          TI_PIPE_TIMEOUT_ENABLE |
640                          (TI_TRANSFER_TIMEOUT << 2));
641
642         dev = port->serial->dev;
643         tdev = tport->tp_tdev;
644
645         /* only one open on any port on a device at a time */
646         if (mutex_lock_interruptible(&tdev->td_open_close_lock))
647                 return -ERESTARTSYS;
648
649         tport->tp_msr = 0;
650         tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
651
652         /* start interrupt urb the first time a port is opened on this device */
653         if (tdev->td_open_port_count == 0) {
654                 dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
655                 urb = tdev->td_serial->port[0]->interrupt_in_urb;
656                 if (!urb) {
657                         dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
658                         status = -EINVAL;
659                         goto release_lock;
660                 }
661                 urb->context = tdev;
662                 status = usb_submit_urb(urb, GFP_KERNEL);
663                 if (status) {
664                         dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
665                         goto release_lock;
666                 }
667         }
668
669         if (tty)
670                 ti_set_termios(tty, port, &tty->termios);
671
672         status = ti_port_cmd_out(port, TI_OPEN_PORT, open_settings, NULL, 0);
673         if (status) {
674                 dev_err(&port->dev, "%s - cannot send open command, %d\n",
675                         __func__, status);
676                 goto unlink_int_urb;
677         }
678
679         status = ti_port_cmd_out(port, TI_START_PORT, 0, NULL, 0);
680         if (status) {
681                 dev_err(&port->dev, "%s - cannot send start command, %d\n",
682                                                         __func__, status);
683                 goto unlink_int_urb;
684         }
685
686         status = ti_port_cmd_out(port, TI_PURGE_PORT, TI_PURGE_INPUT, NULL, 0);
687         if (status) {
688                 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
689                                                         __func__, status);
690                 goto unlink_int_urb;
691         }
692         status = ti_port_cmd_out(port, TI_PURGE_PORT, TI_PURGE_OUTPUT, NULL, 0);
693         if (status) {
694                 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
695                                                         __func__, status);
696                 goto unlink_int_urb;
697         }
698
699         /* reset the data toggle on the bulk endpoints to work around bug in
700          * host controllers where things get out of sync some times */
701         usb_clear_halt(dev, port->write_urb->pipe);
702         usb_clear_halt(dev, port->read_urb->pipe);
703
704         if (tty)
705                 ti_set_termios(tty, port, &tty->termios);
706
707         status = ti_port_cmd_out(port, TI_OPEN_PORT, open_settings, NULL, 0);
708         if (status) {
709                 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
710                                                         __func__, status);
711                 goto unlink_int_urb;
712         }
713
714         status = ti_port_cmd_out(port, TI_START_PORT, 0, NULL, 0);
715         if (status) {
716                 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
717                                                         __func__, status);
718                 goto unlink_int_urb;
719         }
720
721         /* start read urb */
722         urb = port->read_urb;
723         if (!urb) {
724                 dev_err(&port->dev, "%s - no read urb\n", __func__);
725                 status = -EINVAL;
726                 goto unlink_int_urb;
727         }
728         tport->tp_read_urb_state = TI_READ_URB_RUNNING;
729         urb->context = tport;
730         status = usb_submit_urb(urb, GFP_KERNEL);
731         if (status) {
732                 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
733                                                         __func__, status);
734                 goto unlink_int_urb;
735         }
736
737         tport->tp_is_open = 1;
738         ++tdev->td_open_port_count;
739
740         goto release_lock;
741
742 unlink_int_urb:
743         if (tdev->td_open_port_count == 0)
744                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
745 release_lock:
746         mutex_unlock(&tdev->td_open_close_lock);
747         return status;
748 }
749
750
751 static void ti_close(struct usb_serial_port *port)
752 {
753         struct ti_device *tdev;
754         struct ti_port *tport;
755         int status;
756         unsigned long flags;
757
758         tdev = usb_get_serial_data(port->serial);
759         tport = usb_get_serial_port_data(port);
760
761         tport->tp_is_open = 0;
762
763         usb_kill_urb(port->read_urb);
764         usb_kill_urb(port->write_urb);
765         tport->tp_write_urb_in_use = 0;
766         spin_lock_irqsave(&tport->tp_lock, flags);
767         kfifo_reset_out(&port->write_fifo);
768         spin_unlock_irqrestore(&tport->tp_lock, flags);
769
770         status = ti_port_cmd_out(port, TI_CLOSE_PORT, 0, NULL, 0);
771         if (status)
772                 dev_err(&port->dev,
773                         "%s - cannot send close port command, %d\n"
774                                                         , __func__, status);
775
776         mutex_lock(&tdev->td_open_close_lock);
777         --tdev->td_open_port_count;
778         if (tdev->td_open_port_count == 0) {
779                 /* last port is closed, shut down interrupt urb */
780                 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
781         }
782         mutex_unlock(&tdev->td_open_close_lock);
783 }
784
785
786 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
787                         const unsigned char *data, int count)
788 {
789         struct ti_port *tport = usb_get_serial_port_data(port);
790
791         if (count == 0) {
792                 return 0;
793         }
794
795         if (!tport->tp_is_open)
796                 return -ENODEV;
797
798         count = kfifo_in_locked(&port->write_fifo, data, count,
799                                                         &tport->tp_lock);
800         ti_send(tport);
801
802         return count;
803 }
804
805
806 static unsigned int ti_write_room(struct tty_struct *tty)
807 {
808         struct usb_serial_port *port = tty->driver_data;
809         struct ti_port *tport = usb_get_serial_port_data(port);
810         unsigned int room;
811         unsigned long flags;
812
813         spin_lock_irqsave(&tport->tp_lock, flags);
814         room = kfifo_avail(&port->write_fifo);
815         spin_unlock_irqrestore(&tport->tp_lock, flags);
816
817         dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
818         return room;
819 }
820
821
822 static unsigned int ti_chars_in_buffer(struct tty_struct *tty)
823 {
824         struct usb_serial_port *port = tty->driver_data;
825         struct ti_port *tport = usb_get_serial_port_data(port);
826         unsigned int chars;
827         unsigned long flags;
828
829         spin_lock_irqsave(&tport->tp_lock, flags);
830         chars = kfifo_len(&port->write_fifo);
831         spin_unlock_irqrestore(&tport->tp_lock, flags);
832
833         dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
834         return chars;
835 }
836
837 static bool ti_tx_empty(struct usb_serial_port *port)
838 {
839         struct ti_port *tport = usb_get_serial_port_data(port);
840         u8 lsr, mask;
841         int ret;
842
843         /*
844          * TUSB5052 does not have the TEMT bit to tell if the shift register
845          * is empty.
846          */
847         if (tport->tp_tdev->td_is_3410)
848                 mask = TI_LSR_TX_EMPTY_BOTH;
849         else
850                 mask = TI_LSR_TX_EMPTY;
851
852         ret = ti_get_lsr(tport, &lsr);
853         if (!ret && !(lsr & mask))
854                 return false;
855
856         return true;
857 }
858
859 static void ti_throttle(struct tty_struct *tty)
860 {
861         struct usb_serial_port *port = tty->driver_data;
862         struct ti_port *tport = usb_get_serial_port_data(port);
863
864         if (I_IXOFF(tty) || C_CRTSCTS(tty))
865                 ti_stop_read(tport, tty);
866
867 }
868
869
870 static void ti_unthrottle(struct tty_struct *tty)
871 {
872         struct usb_serial_port *port = tty->driver_data;
873         struct ti_port *tport = usb_get_serial_port_data(port);
874         int status;
875
876         if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
877                 status = ti_restart_read(tport, tty);
878                 if (status)
879                         dev_err(&port->dev, "%s - cannot restart read, %d\n",
880                                                         __func__, status);
881         }
882 }
883
884 static void ti_set_termios(struct tty_struct *tty,
885                            struct usb_serial_port *port,
886                            const struct ktermios *old_termios)
887 {
888         struct ti_port *tport = usb_get_serial_port_data(port);
889         struct ti_uart_config *config;
890         int baud;
891         int status;
892         unsigned int mcr;
893         u16 wbaudrate;
894         u16 wflags = 0;
895
896         config = kmalloc(sizeof(*config), GFP_KERNEL);
897         if (!config)
898                 return;
899
900         /* these flags must be set */
901         wflags |= TI_UART_ENABLE_MS_INTS;
902         wflags |= TI_UART_ENABLE_AUTO_START_DMA;
903         config->bUartMode = tport->tp_uart_mode;
904
905         switch (C_CSIZE(tty)) {
906         case CS5:
907                 config->bDataBits = TI_UART_5_DATA_BITS;
908                 break;
909         case CS6:
910                 config->bDataBits = TI_UART_6_DATA_BITS;
911                 break;
912         case CS7:
913                 config->bDataBits = TI_UART_7_DATA_BITS;
914                 break;
915         default:
916         case CS8:
917                 config->bDataBits = TI_UART_8_DATA_BITS;
918                 break;
919         }
920
921         /* CMSPAR isn't supported by this driver */
922         tty->termios.c_cflag &= ~CMSPAR;
923
924         if (C_PARENB(tty)) {
925                 if (C_PARODD(tty)) {
926                         wflags |= TI_UART_ENABLE_PARITY_CHECKING;
927                         config->bParity = TI_UART_ODD_PARITY;
928                 } else {
929                         wflags |= TI_UART_ENABLE_PARITY_CHECKING;
930                         config->bParity = TI_UART_EVEN_PARITY;
931                 }
932         } else {
933                 wflags &= ~TI_UART_ENABLE_PARITY_CHECKING;
934                 config->bParity = TI_UART_NO_PARITY;
935         }
936
937         if (C_CSTOPB(tty))
938                 config->bStopBits = TI_UART_2_STOP_BITS;
939         else
940                 config->bStopBits = TI_UART_1_STOP_BITS;
941
942         if (C_CRTSCTS(tty)) {
943                 /* RTS flow control must be off to drop RTS for baud rate B0 */
944                 if ((C_BAUD(tty)) != B0)
945                         wflags |= TI_UART_ENABLE_RTS_IN;
946                 wflags |= TI_UART_ENABLE_CTS_OUT;
947         } else {
948                 ti_restart_read(tport, tty);
949         }
950
951         if (I_IXOFF(tty) || I_IXON(tty)) {
952                 config->cXon  = START_CHAR(tty);
953                 config->cXoff = STOP_CHAR(tty);
954
955                 if (I_IXOFF(tty))
956                         wflags |= TI_UART_ENABLE_X_IN;
957                 else
958                         ti_restart_read(tport, tty);
959
960                 if (I_IXON(tty))
961                         wflags |= TI_UART_ENABLE_X_OUT;
962         }
963
964         baud = tty_get_baud_rate(tty);
965         if (!baud)
966                 baud = 9600;
967         if (tport->tp_tdev->td_is_3410)
968                 wbaudrate = (923077 + baud/2) / baud;
969         else
970                 wbaudrate = (461538 + baud/2) / baud;
971
972         /* FIXME: Should calculate resulting baud here and report it back */
973         if ((C_BAUD(tty)) != B0)
974                 tty_encode_baud_rate(tty, baud, baud);
975
976         dev_dbg(&port->dev,
977                 "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d\n",
978                 __func__, baud, wbaudrate, wflags,
979                 config->bDataBits, config->bParity, config->bStopBits,
980                 config->cXon, config->cXoff, config->bUartMode);
981
982         config->wBaudRate = cpu_to_be16(wbaudrate);
983         config->wFlags = cpu_to_be16(wflags);
984
985         status = ti_port_cmd_out(port, TI_SET_CONFIG, 0, config,
986                         sizeof(*config));
987         if (status)
988                 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
989                                 __func__, port->port_number, status);
990
991         /* SET_CONFIG asserts RTS and DTR, reset them correctly */
992         mcr = tport->tp_shadow_mcr;
993         /* if baud rate is B0, clear RTS and DTR */
994         if (C_BAUD(tty) == B0)
995                 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
996         status = ti_set_mcr(tport, mcr);
997         if (status)
998                 dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n",
999                                 __func__, port->port_number, status);
1000
1001         kfree(config);
1002 }
1003
1004
1005 static int ti_tiocmget(struct tty_struct *tty)
1006 {
1007         struct usb_serial_port *port = tty->driver_data;
1008         struct ti_port *tport = usb_get_serial_port_data(port);
1009         unsigned int result;
1010         unsigned int msr;
1011         unsigned int mcr;
1012         unsigned long flags;
1013
1014         spin_lock_irqsave(&tport->tp_lock, flags);
1015         msr = tport->tp_msr;
1016         mcr = tport->tp_shadow_mcr;
1017         spin_unlock_irqrestore(&tport->tp_lock, flags);
1018
1019         result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
1020                 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
1021                 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
1022                 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
1023                 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
1024                 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
1025                 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
1026
1027         dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1028
1029         return result;
1030 }
1031
1032
1033 static int ti_tiocmset(struct tty_struct *tty,
1034                                 unsigned int set, unsigned int clear)
1035 {
1036         struct usb_serial_port *port = tty->driver_data;
1037         struct ti_port *tport = usb_get_serial_port_data(port);
1038         unsigned int mcr;
1039         unsigned long flags;
1040
1041         spin_lock_irqsave(&tport->tp_lock, flags);
1042         mcr = tport->tp_shadow_mcr;
1043
1044         if (set & TIOCM_RTS)
1045                 mcr |= TI_MCR_RTS;
1046         if (set & TIOCM_DTR)
1047                 mcr |= TI_MCR_DTR;
1048         if (set & TIOCM_LOOP)
1049                 mcr |= TI_MCR_LOOP;
1050
1051         if (clear & TIOCM_RTS)
1052                 mcr &= ~TI_MCR_RTS;
1053         if (clear & TIOCM_DTR)
1054                 mcr &= ~TI_MCR_DTR;
1055         if (clear & TIOCM_LOOP)
1056                 mcr &= ~TI_MCR_LOOP;
1057         spin_unlock_irqrestore(&tport->tp_lock, flags);
1058
1059         return ti_set_mcr(tport, mcr);
1060 }
1061
1062
1063 static int ti_break(struct tty_struct *tty, int break_state)
1064 {
1065         struct usb_serial_port *port = tty->driver_data;
1066         struct ti_port *tport = usb_get_serial_port_data(port);
1067         int status;
1068
1069         dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
1070
1071         status = ti_write_byte(port, tport->tp_tdev,
1072                 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1073                 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1074
1075         if (status) {
1076                 dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
1077                 return status;
1078         }
1079
1080         return 0;
1081 }
1082
1083 static int ti_get_port_from_code(unsigned char code)
1084 {
1085         return (code >> 6) & 0x01;
1086 }
1087
1088 static int ti_get_func_from_code(unsigned char code)
1089 {
1090         return code & 0x0f;
1091 }
1092
1093 static void ti_interrupt_callback(struct urb *urb)
1094 {
1095         struct ti_device *tdev = urb->context;
1096         struct usb_serial_port *port;
1097         struct usb_serial *serial = tdev->td_serial;
1098         struct ti_port *tport;
1099         struct device *dev = &urb->dev->dev;
1100         unsigned char *data = urb->transfer_buffer;
1101         int length = urb->actual_length;
1102         int port_number;
1103         int function;
1104         int status = urb->status;
1105         int retval;
1106         u8 msr;
1107
1108         switch (status) {
1109         case 0:
1110                 break;
1111         case -ECONNRESET:
1112         case -ENOENT:
1113         case -ESHUTDOWN:
1114                 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1115                 return;
1116         default:
1117                 dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
1118                 goto exit;
1119         }
1120
1121         if (length != 2) {
1122                 dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
1123                 goto exit;
1124         }
1125
1126         if (data[0] == TI_CODE_HARDWARE_ERROR) {
1127                 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1128                 goto exit;
1129         }
1130
1131         port_number = ti_get_port_from_code(data[0]);
1132         function = ti_get_func_from_code(data[0]);
1133
1134         dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
1135                 __func__, port_number, function, data[1]);
1136
1137         if (port_number >= serial->num_ports) {
1138                 dev_err(dev, "%s - bad port number, %d\n",
1139                                                 __func__, port_number);
1140                 goto exit;
1141         }
1142
1143         port = serial->port[port_number];
1144
1145         tport = usb_get_serial_port_data(port);
1146         if (!tport)
1147                 goto exit;
1148
1149         switch (function) {
1150         case TI_CODE_DATA_ERROR:
1151                 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1152                         __func__, port_number, data[1]);
1153                 break;
1154
1155         case TI_CODE_MODEM_STATUS:
1156                 msr = data[1];
1157                 dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
1158                 ti_handle_new_msr(tport, msr);
1159                 break;
1160
1161         default:
1162                 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1163                                                         __func__, data[1]);
1164                 break;
1165         }
1166
1167 exit:
1168         retval = usb_submit_urb(urb, GFP_ATOMIC);
1169         if (retval)
1170                 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1171                         __func__, retval);
1172 }
1173
1174
1175 static void ti_bulk_in_callback(struct urb *urb)
1176 {
1177         struct ti_port *tport = urb->context;
1178         struct usb_serial_port *port = tport->tp_port;
1179         struct device *dev = &urb->dev->dev;
1180         int status = urb->status;
1181         unsigned long flags;
1182         int retval = 0;
1183
1184         switch (status) {
1185         case 0:
1186                 break;
1187         case -ECONNRESET:
1188         case -ENOENT:
1189         case -ESHUTDOWN:
1190                 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1191                 return;
1192         default:
1193                 dev_err(dev, "%s - nonzero urb status, %d\n",
1194                         __func__, status);
1195         }
1196
1197         if (status == -EPIPE)
1198                 goto exit;
1199
1200         if (status) {
1201                 dev_err(dev, "%s - stopping read!\n", __func__);
1202                 return;
1203         }
1204
1205         if (urb->actual_length) {
1206                 usb_serial_debug_data(dev, __func__, urb->actual_length,
1207                                       urb->transfer_buffer);
1208
1209                 if (!tport->tp_is_open)
1210                         dev_dbg(dev, "%s - port closed, dropping data\n",
1211                                 __func__);
1212                 else
1213                         ti_recv(port, urb->transfer_buffer, urb->actual_length);
1214                 spin_lock_irqsave(&tport->tp_lock, flags);
1215                 port->icount.rx += urb->actual_length;
1216                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1217         }
1218
1219 exit:
1220         /* continue to read unless stopping */
1221         spin_lock_irqsave(&tport->tp_lock, flags);
1222         if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1223                 retval = usb_submit_urb(urb, GFP_ATOMIC);
1224         else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
1225                 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1226
1227         spin_unlock_irqrestore(&tport->tp_lock, flags);
1228         if (retval)
1229                 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1230                         __func__, retval);
1231 }
1232
1233
1234 static void ti_bulk_out_callback(struct urb *urb)
1235 {
1236         struct ti_port *tport = urb->context;
1237         struct usb_serial_port *port = tport->tp_port;
1238         int status = urb->status;
1239
1240         tport->tp_write_urb_in_use = 0;
1241
1242         switch (status) {
1243         case 0:
1244                 break;
1245         case -ECONNRESET:
1246         case -ENOENT:
1247         case -ESHUTDOWN:
1248                 dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
1249                 return;
1250         default:
1251                 dev_err_console(port, "%s - nonzero urb status, %d\n",
1252                         __func__, status);
1253         }
1254
1255         /* send any buffered data */
1256         ti_send(tport);
1257 }
1258
1259
1260 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
1261                 int length)
1262 {
1263         int cnt;
1264
1265         do {
1266                 cnt = tty_insert_flip_string(&port->port, data, length);
1267                 if (cnt < length) {
1268                         dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1269                                                 __func__, length - cnt);
1270                         if (cnt == 0)
1271                                 break;
1272                 }
1273                 tty_flip_buffer_push(&port->port);
1274                 data += cnt;
1275                 length -= cnt;
1276         } while (length > 0);
1277 }
1278
1279
1280 static void ti_send(struct ti_port *tport)
1281 {
1282         int count, result;
1283         struct usb_serial_port *port = tport->tp_port;
1284         unsigned long flags;
1285
1286         spin_lock_irqsave(&tport->tp_lock, flags);
1287
1288         if (tport->tp_write_urb_in_use)
1289                 goto unlock;
1290
1291         count = kfifo_out(&port->write_fifo,
1292                                 port->write_urb->transfer_buffer,
1293                                 port->bulk_out_size);
1294
1295         if (count == 0)
1296                 goto unlock;
1297
1298         tport->tp_write_urb_in_use = 1;
1299
1300         spin_unlock_irqrestore(&tport->tp_lock, flags);
1301
1302         usb_serial_debug_data(&port->dev, __func__, count,
1303                               port->write_urb->transfer_buffer);
1304
1305         usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1306                            usb_sndbulkpipe(port->serial->dev,
1307                                             port->bulk_out_endpointAddress),
1308                            port->write_urb->transfer_buffer, count,
1309                            ti_bulk_out_callback, tport);
1310
1311         result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1312         if (result) {
1313                 dev_err_console(port, "%s - submit write urb failed, %d\n",
1314                                                         __func__, result);
1315                 tport->tp_write_urb_in_use = 0;
1316                 /* TODO: reschedule ti_send */
1317         } else {
1318                 spin_lock_irqsave(&tport->tp_lock, flags);
1319                 port->icount.tx += count;
1320                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1321         }
1322
1323         /* more room in the buffer for new writes, wakeup */
1324         tty_port_tty_wakeup(&port->port);
1325
1326         return;
1327 unlock:
1328         spin_unlock_irqrestore(&tport->tp_lock, flags);
1329         return;
1330 }
1331
1332
1333 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1334 {
1335         unsigned long flags;
1336         int status;
1337
1338         status = ti_write_byte(tport->tp_port, tport->tp_tdev,
1339                 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1340                 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1341
1342         spin_lock_irqsave(&tport->tp_lock, flags);
1343         if (!status)
1344                 tport->tp_shadow_mcr = mcr;
1345         spin_unlock_irqrestore(&tport->tp_lock, flags);
1346
1347         return status;
1348 }
1349
1350
1351 static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
1352 {
1353         int size, status;
1354         struct usb_serial_port *port = tport->tp_port;
1355         struct ti_port_status *data;
1356
1357         size = sizeof(struct ti_port_status);
1358         data = kmalloc(size, GFP_KERNEL);
1359         if (!data)
1360                 return -ENOMEM;
1361
1362         status = ti_port_cmd_in(port, TI_GET_PORT_STATUS, 0, data, size);
1363         if (status) {
1364                 dev_err(&port->dev,
1365                         "%s - get port status command failed, %d\n",
1366                                                         __func__, status);
1367                 goto free_data;
1368         }
1369
1370         dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
1371
1372         *lsr = data->bLSR;
1373
1374 free_data:
1375         kfree(data);
1376         return status;
1377 }
1378
1379
1380 static void ti_get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
1381 {
1382         struct usb_serial_port *port = tty->driver_data;
1383         struct ti_port *tport = usb_get_serial_port_data(port);
1384
1385         ss->baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1386 }
1387
1388
1389 static void ti_handle_new_msr(struct ti_port *tport, u8 msr)
1390 {
1391         struct async_icount *icount;
1392         struct tty_struct *tty;
1393         unsigned long flags;
1394
1395         dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
1396
1397         if (msr & TI_MSR_DELTA_MASK) {
1398                 spin_lock_irqsave(&tport->tp_lock, flags);
1399                 icount = &tport->tp_port->icount;
1400                 if (msr & TI_MSR_DELTA_CTS)
1401                         icount->cts++;
1402                 if (msr & TI_MSR_DELTA_DSR)
1403                         icount->dsr++;
1404                 if (msr & TI_MSR_DELTA_CD)
1405                         icount->dcd++;
1406                 if (msr & TI_MSR_DELTA_RI)
1407                         icount->rng++;
1408                 wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
1409                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1410         }
1411
1412         tport->tp_msr = msr & TI_MSR_MASK;
1413
1414         /* handle CTS flow control */
1415         tty = tty_port_tty_get(&tport->tp_port->port);
1416         if (tty && C_CRTSCTS(tty)) {
1417                 if (msr & TI_MSR_CTS)
1418                         tty_wakeup(tty);
1419         }
1420         tty_kref_put(tty);
1421 }
1422
1423
1424 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1425 {
1426         unsigned long flags;
1427
1428         spin_lock_irqsave(&tport->tp_lock, flags);
1429
1430         if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1431                 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1432
1433         spin_unlock_irqrestore(&tport->tp_lock, flags);
1434 }
1435
1436
1437 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1438 {
1439         struct urb *urb;
1440         int status = 0;
1441         unsigned long flags;
1442
1443         spin_lock_irqsave(&tport->tp_lock, flags);
1444
1445         if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1446                 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1447                 urb = tport->tp_port->read_urb;
1448                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1449                 urb->context = tport;
1450                 status = usb_submit_urb(urb, GFP_KERNEL);
1451         } else  {
1452                 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1453                 spin_unlock_irqrestore(&tport->tp_lock, flags);
1454         }
1455
1456         return status;
1457 }
1458
1459 static int ti_command_out_sync(struct usb_device *udev, u8 command,
1460                 u16 moduleid, u16 value, void *data, int size)
1461 {
1462         int status;
1463
1464         status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), command,
1465                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
1466                         value, moduleid, data, size, 1000);
1467         if (status < 0)
1468                 return status;
1469
1470         return 0;
1471 }
1472
1473 static int ti_command_in_sync(struct usb_device *udev, u8 command,
1474                 u16 moduleid, u16 value, void *data, int size)
1475 {
1476         int status;
1477
1478         status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), command,
1479                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
1480                         value, moduleid, data, size, 1000);
1481         if (status == size)
1482                 status = 0;
1483         else if (status >= 0)
1484                 status = -ECOMM;
1485
1486         return status;
1487 }
1488
1489 static int ti_port_cmd_out(struct usb_serial_port *port, u8 command,
1490                 u16 value, void *data, int size)
1491 {
1492         return ti_command_out_sync(port->serial->dev, command,
1493                         TI_UART1_PORT + port->port_number,
1494                         value, data, size);
1495 }
1496
1497 static int ti_port_cmd_in(struct usb_serial_port *port, u8 command,
1498                 u16 value, void *data, int size)
1499 {
1500         return ti_command_in_sync(port->serial->dev, command,
1501                         TI_UART1_PORT + port->port_number,
1502                         value, data, size);
1503 }
1504
1505 static int ti_write_byte(struct usb_serial_port *port,
1506                          struct ti_device *tdev, unsigned long addr,
1507                          u8 mask, u8 byte)
1508 {
1509         int status;
1510         unsigned int size;
1511         struct ti_write_data_bytes *data;
1512
1513         dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1514                 addr, mask, byte);
1515
1516         size = sizeof(struct ti_write_data_bytes) + 2;
1517         data = kmalloc(size, GFP_KERNEL);
1518         if (!data)
1519                 return -ENOMEM;
1520
1521         data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1522         data->bDataType = TI_RW_DATA_BYTE;
1523         data->bDataCounter = 1;
1524         data->wBaseAddrHi = cpu_to_be16(addr>>16);
1525         data->wBaseAddrLo = cpu_to_be16(addr);
1526         data->bData[0] = mask;
1527         data->bData[1] = byte;
1528
1529         status = ti_command_out_sync(port->serial->dev, TI_WRITE_DATA,
1530                         TI_RAM_PORT, 0, data, size);
1531         if (status < 0)
1532                 dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
1533
1534         kfree(data);
1535
1536         return status;
1537 }
1538
1539 static int ti_do_download(struct usb_device *dev, int pipe,
1540                                                 u8 *buffer, int size)
1541 {
1542         int pos;
1543         u8 cs = 0;
1544         int done;
1545         struct ti_firmware_header *header;
1546         int status = 0;
1547         int len;
1548
1549         for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1550                 cs = (u8)(cs + buffer[pos]);
1551
1552         header = (struct ti_firmware_header *)buffer;
1553         header->wLength = cpu_to_le16(size - sizeof(*header));
1554         header->bCheckSum = cs;
1555
1556         dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
1557         for (pos = 0; pos < size; pos += done) {
1558                 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1559                 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1560                                                                 &done, 1000);
1561                 if (status)
1562                         break;
1563         }
1564         return status;
1565 }
1566
1567 static int ti_download_firmware(struct ti_device *tdev)
1568 {
1569         int status;
1570         int buffer_size;
1571         u8 *buffer;
1572         struct usb_device *dev = tdev->td_serial->dev;
1573         unsigned int pipe = usb_sndbulkpipe(dev,
1574                 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1575         const struct firmware *fw_p;
1576         char buf[32];
1577
1578         if (le16_to_cpu(dev->descriptor.idVendor) == MXU1_VENDOR_ID) {
1579                 snprintf(buf,
1580                         sizeof(buf),
1581                         "/*(DEBLOBBED)*/",
1582                         le16_to_cpu(dev->descriptor.idProduct));
1583
1584                 status = reject_firmware(&fw_p, buf, &dev->dev);
1585                 goto check_firmware;
1586         }
1587
1588         /* try ID specific firmware first, then try generic firmware */
1589         sprintf(buf, "/*(DEBLOBBED)*/",
1590                         le16_to_cpu(dev->descriptor.idVendor),
1591                         le16_to_cpu(dev->descriptor.idProduct));
1592         status = reject_firmware(&fw_p, buf, &dev->dev);
1593
1594         if (status != 0) {
1595                 buf[0] = '\0';
1596                 if (le16_to_cpu(dev->descriptor.idVendor) == MTS_VENDOR_ID) {
1597                         switch (le16_to_cpu(dev->descriptor.idProduct)) {
1598                         case MTS_CDMA_PRODUCT_ID:
1599                                 strcpy(buf, "/*(DEBLOBBED)*/");
1600                                 break;
1601                         case MTS_GSM_PRODUCT_ID:
1602                                 strcpy(buf, "/*(DEBLOBBED)*/");
1603                                 break;
1604                         case MTS_EDGE_PRODUCT_ID:
1605                                 strcpy(buf, "/*(DEBLOBBED)*/");
1606                                 break;
1607                         case MTS_MT9234MU_PRODUCT_ID:
1608                                 strcpy(buf, "/*(DEBLOBBED)*/");
1609                                 break;
1610                         case MTS_MT9234ZBA_PRODUCT_ID:
1611                                 strcpy(buf, "/*(DEBLOBBED)*/");
1612                                 break;
1613                         case MTS_MT9234ZBAOLD_PRODUCT_ID:
1614                                 strcpy(buf, "/*(DEBLOBBED)*/");
1615                                 break;                  }
1616                 }
1617                 if (buf[0] == '\0') {
1618                         if (tdev->td_is_3410)
1619                                 strcpy(buf, "/*(DEBLOBBED)*/");
1620                         else
1621                                 strcpy(buf, "/*(DEBLOBBED)*/");
1622                 }
1623                 status = reject_firmware(&fw_p, buf, &dev->dev);
1624         }
1625
1626 check_firmware:
1627         if (status) {
1628                 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1629                 return -ENOENT;
1630         }
1631         if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1632                 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1633                 release_firmware(fw_p);
1634                 return -ENOENT;
1635         }
1636
1637         buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1638         buffer = kmalloc(buffer_size, GFP_KERNEL);
1639         if (buffer) {
1640                 memcpy(buffer, fw_p->data, fw_p->size);
1641                 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1642                 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1643                 kfree(buffer);
1644         } else {
1645                 status = -ENOMEM;
1646         }
1647         release_firmware(fw_p);
1648         if (status) {
1649                 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1650                                                         __func__, status);
1651                 return status;
1652         }
1653
1654         dev_dbg(&dev->dev, "%s - download successful\n", __func__);
1655
1656         return 0;
1657 }