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