21 #include <linux/kernel.h>
22 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
29 #include <linux/module.h>
32 #include <linux/serial.h>
44 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
46 #define TI_FIRMWARE_BUF_SIZE 16284
48 #define TI_WRITE_BUF_SIZE 1024
50 #define TI_TRANSFER_TIMEOUT 2
52 #define TI_DEFAULT_CLOSING_WAIT 4000
55 #define TI_SET_SERIAL_FLAGS 0
58 #define TI_READ_URB_RUNNING 0
59 #define TI_READ_URB_STOPPING 1
60 #define TI_READ_URB_STOPPED 2
62 #define TI_EXTRA_VID_PID_COUNT 5
106 static int ti_write_room(
struct tty_struct *tty);
107 static int ti_chars_in_buffer(
struct tty_struct *tty);
108 static void ti_throttle(
struct tty_struct *tty);
109 static void ti_unthrottle(
struct tty_struct *tty);
111 unsigned int cmd,
unsigned long arg);
112 static int ti_get_icount(
struct tty_struct *tty,
114 static void ti_set_termios(
struct tty_struct *tty,
116 static int ti_tiocmget(
struct tty_struct *tty);
117 static int ti_tiocmset(
struct tty_struct *tty,
118 unsigned int set,
unsigned int clear);
119 static void ti_break(
struct tty_struct *tty,
int break_state);
120 static void ti_interrupt_callback(
struct urb *
urb);
121 static void ti_bulk_in_callback(
struct urb *
urb);
122 static void ti_bulk_out_callback(
struct urb *
urb);
126 static void ti_send(
struct ti_port *tport);
127 static int ti_set_mcr(
struct ti_port *tport,
unsigned int mcr);
128 static int ti_get_lsr(
struct ti_port *tport);
129 static int ti_get_serial_info(
struct ti_port *tport,
133 static void ti_handle_new_msr(
struct ti_port *tport,
__u8 msr);
135 static void ti_drain(
struct ti_port *tport,
unsigned long timeout,
int flush);
148 static int ti_download_firmware(
struct ti_device *tdev);
156 static unsigned int vendor_3410_count;
158 static unsigned int product_3410_count;
160 static unsigned int vendor_5052_count;
162 static unsigned int product_5052_count;
219 .name =
"ti_usb_3410_5052_1",
221 .description =
"TI USB 3410 1 port adapter",
222 .id_table = ti_id_table_3410,
224 .attach = ti_startup,
225 .release = ti_release,
226 .port_probe = ti_port_probe,
227 .port_remove = ti_port_remove,
231 .write_room = ti_write_room,
232 .chars_in_buffer = ti_chars_in_buffer,
233 .throttle = ti_throttle,
234 .unthrottle = ti_unthrottle,
236 .set_termios = ti_set_termios,
237 .tiocmget = ti_tiocmget,
238 .tiocmset = ti_tiocmset,
239 .get_icount = ti_get_icount,
240 .break_ctl = ti_break,
241 .read_int_callback = ti_interrupt_callback,
242 .read_bulk_callback = ti_bulk_in_callback,
243 .write_bulk_callback = ti_bulk_out_callback,
249 .name =
"ti_usb_3410_5052_2",
251 .description =
"TI USB 5052 2 port adapter",
252 .id_table = ti_id_table_5052,
254 .attach = ti_startup,
255 .release = ti_release,
256 .port_probe = ti_port_probe,
257 .port_remove = ti_port_remove,
261 .write_room = ti_write_room,
262 .chars_in_buffer = ti_chars_in_buffer,
263 .throttle = ti_throttle,
264 .unthrottle = ti_unthrottle,
266 .set_termios = ti_set_termios,
267 .tiocmget = ti_tiocmget,
268 .tiocmset = ti_tiocmset,
269 .get_icount = ti_get_icount,
270 .break_ctl = ti_break,
271 .read_int_callback = ti_interrupt_callback,
272 .read_bulk_callback = ti_bulk_in_callback,
273 .write_bulk_callback = ti_bulk_out_callback,
277 &ti_1port_device, &ti_2port_device,
NULL
296 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
300 "Vendor ids for 3410 based devices, 1-5 short integers");
303 "Product ids for 3410 based devices, 1-5 short integers");
306 "Vendor ids for 5052 based devices, 1-5 short integers");
309 "Product ids for 5052 based devices, 1-5 short integers");
316 static int __init ti_init(
void)
323 for (i = 0; i <
min(vendor_3410_count, product_3410_count); i++, j++, c++) {
324 ti_id_table_3410[
j].
idVendor = vendor_3410[
i];
326 ti_id_table_3410[
j].
match_flags = USB_DEVICE_ID_MATCH_DEVICE;
327 ti_id_table_combined[
c].idVendor = vendor_3410[
i];
328 ti_id_table_combined[
c].idProduct = product_3410[
i];
329 ti_id_table_combined[
c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
332 for (i = 0; i <
min(vendor_5052_count, product_5052_count); i++, j++, c++) {
333 ti_id_table_5052[
j].
idVendor = vendor_5052[
i];
335 ti_id_table_5052[
j].
match_flags = USB_DEVICE_ID_MATCH_DEVICE;
336 ti_id_table_combined[
c].idVendor = vendor_5052[
i];
337 ti_id_table_combined[
c].idProduct = product_5052[
i];
338 ti_id_table_combined[
c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
344 static void __exit ti_exit(
void)
356 struct usb_device *
dev = serial->
dev;
360 "%s - product 0x%4X, num configurations %d, configuration value %d",
362 dev->descriptor.bNumConfigurations,
363 dev->actconfig->desc.bConfigurationValue);
368 dev_err(&dev->dev,
"%s - out of memory\n", __func__);
373 usb_set_serial_data(serial, tdev);
378 dev_dbg(&dev->dev,
"%s - device type is %s\n", __func__,
382 if (dev->descriptor.bNumConfigurations == 1) {
383 status = ti_download_firmware(tdev);
401 status = status ? status : -
ENODEV;
409 usb_set_serial_data(serial,
NULL);
414 static void ti_release(
struct usb_serial *serial)
416 struct ti_device *tdev = usb_get_serial_data(serial);
430 if (port == port->
serial->port[0])
445 usb_set_serial_port_data(port, tport);
454 tport = usb_get_serial_port_data(port);
463 struct ti_port *tport = usb_get_serial_port_data(port);
465 struct usb_device *
dev;
492 dev_dbg(&port->
dev,
"%s - start interrupt in urb\n", __func__);
493 urb = tdev->
td_serial->port[0]->interrupt_in_urb;
495 dev_err(&port->
dev,
"%s - no interrupt urb\n", __func__);
502 dev_err(&port->
dev,
"%s - submit interrupt urb failed, %d\n", __func__, status);
508 ti_set_termios(tty, port, &tty->termios);
510 dev_dbg(&port->
dev,
"%s - sending TI_OPEN_PORT\n", __func__);
514 dev_err(&port->
dev,
"%s - cannot send open command, %d\n",
519 dev_dbg(&port->
dev,
"%s - sending TI_START_PORT\n", __func__);
523 dev_err(&port->
dev,
"%s - cannot send start command, %d\n",
528 dev_dbg(&port->
dev,
"%s - sending TI_PURGE_PORT\n", __func__);
532 dev_err(&port->
dev,
"%s - cannot clear input buffers, %d\n",
539 dev_err(&port->
dev,
"%s - cannot clear output buffers, %d\n",
550 ti_set_termios(tty, port, &tty->termios);
552 dev_dbg(&port->
dev,
"%s - sending TI_OPEN_PORT (2)\n", __func__);
556 dev_err(&port->
dev,
"%s - cannot send open command (2), %d\n",
561 dev_dbg(&port->
dev,
"%s - sending TI_START_PORT (2)\n", __func__);
565 dev_err(&port->
dev,
"%s - cannot send start command (2), %d\n",
571 dev_dbg(&port->
dev,
"%s - start read urb\n", __func__);
574 dev_err(&port->
dev,
"%s - no read urb\n", __func__);
579 urb->context = tport;
582 dev_err(&port->
dev,
"%s - submit read urb failed, %d\n",
597 dev_dbg(&port->
dev,
"%s - exit %d\n", __func__, status);
610 tdev = usb_get_serial_data(port->
serial);
611 tport = usb_get_serial_port_data(port);
625 dev_dbg(&port->
dev,
"%s - sending TI_CLOSE_PORT\n", __func__);
630 "%s - cannot send close port command, %d\n"
635 --tport->
tp_tdev->td_open_port_count;
636 if (tport->
tp_tdev->td_open_port_count <= 0) {
639 tport->
tp_tdev->td_open_port_count = 0;
649 struct ti_port *tport = usb_get_serial_port_data(port);
652 dev_dbg(&port->
dev,
"%s - write request of 0 bytes\n", __func__);
667 static int ti_write_room(
struct tty_struct *tty)
670 struct ti_port *tport = usb_get_serial_port_data(port);
679 spin_unlock_irqrestore(&tport->
tp_lock, flags);
681 dev_dbg(&port->
dev,
"%s - returns %d\n", __func__, room);
686 static int ti_chars_in_buffer(
struct tty_struct *tty)
689 struct ti_port *tport = usb_get_serial_port_data(port);
698 spin_unlock_irqrestore(&tport->
tp_lock, flags);
700 dev_dbg(&port->
dev,
"%s - returns %d\n", __func__, chars);
705 static void ti_throttle(
struct tty_struct *tty)
708 struct ti_port *tport = usb_get_serial_port_data(port);
714 ti_stop_read(tport, tty);
719 static void ti_unthrottle(
struct tty_struct *tty)
722 struct ti_port *tport = usb_get_serial_port_data(port);
729 status = ti_restart_read(tport, tty);
731 dev_err(&port->
dev,
"%s - cannot restart read, %d\n",
736 static int ti_get_icount(
struct tty_struct *tty,
740 struct ti_port *tport = usb_get_serial_port_data(port);
743 dev_dbg(&port->
dev,
"%s - TIOCGICOUNT RX=%d, TX=%d\n", __func__,
750 icount->
rx = cnow.
rx;
751 icount->
tx = cnow.
tx;
762 unsigned int cmd,
unsigned long arg)
765 struct ti_port *tport = usb_get_serial_port_data(port);
769 dev_dbg(&port->
dev,
"%s - cmd = 0x%04X\n", __func__, cmd);
776 dev_dbg(&port->
dev,
"%s - TIOCGSERIAL\n", __func__);
777 return ti_get_serial_info(tport,
780 dev_dbg(&port->
dev,
"%s - TIOCSSERIAL\n", __func__);
781 return ti_set_serial_info(tty, tport,
784 dev_dbg(&port->
dev,
"%s - TIOCMIWAIT\n", __func__);
791 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
792 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
794 if (((arg &
TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
795 ((arg &
TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
796 ((arg &
TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
797 ((arg &
TIOCM_CTS) && (cnow.cts != cprev.cts)))
807 static void ti_set_termios(
struct tty_struct *tty,
810 struct ti_port *tport = usb_get_serial_port_data(port);
818 cflag = tty->termios.c_cflag;
819 iflag = tty->termios.c_iflag;
821 dev_dbg(&port->
dev,
"%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
822 dev_dbg(&port->
dev,
"%s - old clfag %08x, old iflag %08x\n", __func__,
830 dev_err(&port->
dev,
"%s - out of memory\n", __func__);
841 switch (cflag &
CSIZE) {
858 tty->termios.c_cflag &= ~
CMSPAR;
885 ti_restart_read(tport, tty);
895 ti_restart_read(tport, tty);
904 if (tport->
tp_tdev->td_is_3410)
914 "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
926 dev_err(&port->
dev,
"%s - cannot set config on port %d, %d\n",
927 __func__, port_number, status);
932 if ((cflag & CBAUD) ==
B0)
934 status = ti_set_mcr(tport, mcr);
937 "%s - cannot set modem control on port %d, %d\n",
938 __func__, port_number, status);
944 static int ti_tiocmget(
struct tty_struct *tty)
947 struct ti_port *tport = usb_get_serial_port_data(port);
959 spin_unlock_irqrestore(&tport->
tp_lock, flags);
969 dev_dbg(&port->
dev,
"%s - 0x%04X\n", __func__, result);
975 static int ti_tiocmset(
struct tty_struct *tty,
976 unsigned int set,
unsigned int clear)
979 struct ti_port *tport = usb_get_serial_port_data(port);
1002 spin_unlock_irqrestore(&tport->
tp_lock, flags);
1004 return ti_set_mcr(tport, mcr);
1008 static void ti_break(
struct tty_struct *tty,
int break_state)
1011 struct ti_port *tport = usb_get_serial_port_data(port);
1014 dev_dbg(&port->
dev,
"%s - state = %d\n", __func__, break_state);
1021 status = ti_write_byte(port, tport->
tp_tdev,
1026 dev_dbg(&port->
dev,
"%s - error setting break, %d\n", __func__, status);
1030 static void ti_interrupt_callback(
struct urb *urb)
1036 struct device *dev = &urb->dev->dev;
1037 unsigned char *data = urb->transfer_buffer;
1038 int length = urb->actual_length;
1041 int status = urb->status;
1051 dev_dbg(dev,
"%s - urb shutting down, %d\n", __func__, status);
1055 dev_err(dev,
"%s - nonzero urb status, %d\n", __func__, status);
1061 dev_dbg(dev,
"%s - bad packet size, %d\n", __func__, length);
1066 dev_err(dev,
"%s - hardware error, %d\n", __func__, data[1]);
1073 dev_dbg(dev,
"%s - port_number %d, function %d, data 0x%02X\n",
1074 __func__, port_number,
function, data[1]);
1077 dev_err(dev,
"%s - bad port number, %d\n",
1078 __func__, port_number);
1084 tport = usb_get_serial_port_data(port);
1090 dev_err(dev,
"%s - DATA ERROR, port %d, data 0x%02X\n",
1091 __func__, port_number, data[1]);
1096 dev_dbg(dev,
"%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
1097 ti_handle_new_msr(tport, msr);
1101 dev_err(dev,
"%s - unknown interrupt code, 0x%02X\n",
1109 dev_err(dev,
"%s - resubmit interrupt urb failed, %d\n",
1114 static void ti_bulk_in_callback(
struct urb *urb)
1116 struct ti_port *tport = urb->context;
1118 struct device *dev = &urb->dev->dev;
1119 int status = urb->status;
1129 dev_dbg(dev,
"%s - urb shutting down, %d\n", __func__, status);
1130 tport->
tp_tdev->td_urb_error = 1;
1134 dev_err(dev,
"%s - nonzero urb status, %d\n",
1136 tport->
tp_tdev->td_urb_error = 1;
1140 if (status == -
EPIPE)
1144 dev_err(dev,
"%s - stopping read!\n", __func__);
1150 if (urb->actual_length) {
1151 usb_serial_debug_data(dev, __func__, urb->actual_length,
1152 urb->transfer_buffer);
1155 dev_dbg(dev,
"%s - port closed, dropping data\n",
1158 ti_recv(&urb->dev->dev, tty,
1159 urb->transfer_buffer,
1160 urb->actual_length);
1162 tport->
tp_icount.rx += urb->actual_length;
1178 dev_err(dev,
"%s - resubmit read urb failed, %d\n",
1183 static void ti_bulk_out_callback(
struct urb *urb)
1185 struct ti_port *tport = urb->context;
1187 int status = urb->status;
1197 dev_dbg(&port->
dev,
"%s - urb shutting down, %d\n", __func__, status);
1198 tport->
tp_tdev->td_urb_error = 1;
1204 tport->
tp_tdev->td_urb_error = 1;
1214 unsigned char *data,
int length)
1219 cnt = tty_insert_flip_string(tty, data, length);
1221 dev_err(dev,
"%s - dropping data, %d bytes lost\n",
1222 __func__, length - cnt);
1229 }
while (length > 0);
1234 static void ti_send(
struct ti_port *tport)
1239 unsigned long flags;
1255 spin_unlock_irqrestore(&tport->
tp_lock, flags);
1257 usb_serial_debug_data(&port->
dev, __func__, count,
1261 usb_sndbulkpipe(port->
serial->dev,
1263 port->
write_urb->transfer_buffer, count,
1264 ti_bulk_out_callback, tport);
1275 spin_unlock_irqrestore(&tport->
tp_lock, flags);
1285 spin_unlock_irqrestore(&tport->
tp_lock, flags);
1291 static int ti_set_mcr(
struct ti_port *tport,
unsigned int mcr)
1293 unsigned long flags;
1303 spin_unlock_irqrestore(&tport->
tp_lock, flags);
1309 static int ti_get_lsr(
struct ti_port *tport)
1320 dev_err(&port->
dev,
"%s - out of memory\n", __func__);
1328 "%s - get port status command failed, %d\n",
1343 static int ti_get_serial_info(
struct ti_port *tport,
1352 memset(&ret_serial, 0,
sizeof(ret_serial));
1355 ret_serial.line = port->
serial->minor;
1357 ret_serial.flags = tport->
tp_flags;
1359 ret_serial.baud_base = tport->
tp_tdev->td_is_3410 ? 921600 : 460800;
1362 if (
copy_to_user(ret_arg, &ret_serial,
sizeof(*ret_arg)))
1384 static void ti_handle_new_msr(
struct ti_port *tport,
__u8 msr)
1388 unsigned long flags;
1390 dev_dbg(&tport->
tp_port->dev,
"%s - msr 0x%02X\n", __func__, msr);
1404 spin_unlock_irqrestore(&tport->
tp_lock, flags);
1423 static void ti_drain(
struct ti_port *tport,
unsigned long timeout,
int flush)
1429 spin_lock_irq(&tport->
tp_lock);
1433 init_waitqueue_entry(&wait,
current);
1438 || timeout == 0 || signal_pending(
current)
1440 || port->
serial->disconnected)
1442 spin_unlock_irq(&tport->
tp_lock);
1444 spin_lock_irq(&tport->
tp_lock);
1453 spin_unlock_irq(&tport->
tp_lock);
1460 while ((
long)(jiffies - timeout) < 0 && !signal_pending(
current)
1462 && !port->
serial->disconnected) {
1463 if (ti_get_lsr(tport))
1475 unsigned long flags;
1482 spin_unlock_irqrestore(&tport->
tp_lock, flags);
1490 unsigned long flags;
1496 urb = tport->
tp_port->read_urb;
1497 spin_unlock_irqrestore(&tport->
tp_lock, flags);
1498 urb->context = tport;
1502 spin_unlock_irqrestore(&tport->
tp_lock, flags);
1515 usb_sndctrlpipe(tdev->
td_serial->dev, 0), command,
1517 value, moduleid, data, size, 1000);
1529 static int ti_command_in_sync(
struct ti_device *tdev,
__u8 command,
1535 usb_rcvctrlpipe(tdev->
td_serial->dev, 0), command,
1537 value, moduleid, data, size, 1000);
1557 dev_dbg(&port->
dev,
"%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1563 dev_err(&port->
dev,
"%s - out of memory\n", __func__);
1576 (
__u8 *)data, size);
1579 dev_err(&port->
dev,
"%s - failed, %d\n", __func__, status);
1586 static int ti_do_download(
struct usb_device *dev,
int pipe,
1597 cs = (
__u8)(cs + buffer[
pos]);
1604 dev_dbg(&dev->dev,
"%s - downloading firmware\n", __func__);
1605 for (pos = 0; pos <
size; pos +=
done) {
1615 static int ti_download_firmware(
struct ti_device *tdev)
1620 struct usb_device *dev = tdev->
td_serial->dev;
1621 unsigned int pipe = usb_sndbulkpipe(dev,
1622 tdev->
td_serial->port[0]->bulk_out_endpointAddress);
1627 sprintf(buf,
"ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1628 dev->descriptor.idProduct);
1634 switch (dev->descriptor.idProduct) {
1636 strcpy(buf,
"mts_cdma.fw");
1639 strcpy(buf,
"mts_gsm.fw");
1642 strcpy(buf,
"mts_edge.fw");
1645 strcpy(buf,
"mts_mt9234mu.fw");
1648 strcpy(buf,
"mts_mt9234zba.fw");
1651 strcpy(buf,
"mts_mt9234zba.fw");
1654 if (buf[0] ==
'\0') {
1656 strcpy(buf,
"ti_3410.fw");
1658 strcpy(buf,
"ti_5052.fw");
1663 dev_err(&dev->dev,
"%s - firmware not found\n", __func__);
1667 dev_err(&dev->dev,
"%s - firmware too large %zu\n", __func__, fw_p->
size);
1677 status = ti_do_download(dev, pipe, buffer, fw_p->
size);
1680 dev_dbg(&dev->dev,
"%s ENOMEM\n", __func__);
1685 dev_err(&dev->dev,
"%s - error downloading firmware, %d\n",
1690 dev_dbg(&dev->dev,
"%s - download successful\n", __func__);