1 /* vi: ts=8 sw=8 2 * 3 * TI 3410/5052 USB Serial Driver 4 * 5 * Copyright (C) 2004 Texas Instruments 6 * 7 * This driver is based on the Linux io_ti driver, which is 8 * Copyright (C) 2000-2002 Inside Out Networks 9 * Copyright (C) 2001-2002 Greg Kroah-Hartman 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * For questions or problems with this driver, contact Texas Instruments 17 * technical support, or Al Borchers <alborchers@steinerpoint.com>, or 18 * Peter Berger <pberger@brimson.com>. 19 */ 20 21 #include <linux/kernel.h> 22 #include <linux/errno.h> 23 #include <linux/firmware.h> 24 #include <linux/init.h> 25 #include <linux/slab.h> 26 #include <linux/tty.h> 27 #include <linux/tty_driver.h> 28 #include <linux/tty_flip.h> 29 #include <linux/module.h> 30 #include <linux/spinlock.h> 31 #include <linux/ioctl.h> 32 #include <linux/serial.h> 33 #include <linux/kfifo.h> 34 #include <linux/mutex.h> 35 #include <linux/uaccess.h> 36 #include <linux/usb.h> 37 #include <linux/usb/serial.h> 38 39 #include "ti_usb_3410_5052.h" 40 41 /* Defines */ 42 43 #define TI_DRIVER_VERSION "v0.10" 44 #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>" 45 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver" 46 47 #define TI_FIRMWARE_BUF_SIZE 16284 48 49 #define TI_WRITE_BUF_SIZE 1024 50 51 #define TI_TRANSFER_TIMEOUT 2 52 53 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */ 54 55 /* supported setserial flags */ 56 #define TI_SET_SERIAL_FLAGS 0 57 58 /* read urb states */ 59 #define TI_READ_URB_RUNNING 0 60 #define TI_READ_URB_STOPPING 1 61 #define TI_READ_URB_STOPPED 2 62 63 #define TI_EXTRA_VID_PID_COUNT 5 64 65 66 /* Structures */ 67 68 struct ti_port { 69 int tp_is_open; 70 __u8 tp_msr; 71 __u8 tp_lsr; 72 __u8 tp_shadow_mcr; 73 __u8 tp_uart_mode; /* 232 or 485 modes */ 74 unsigned int tp_uart_base_addr; 75 int tp_flags; 76 int tp_closing_wait;/* in .01 secs */ 77 struct async_icount tp_icount; 78 wait_queue_head_t tp_msr_wait; /* wait for msr change */ 79 wait_queue_head_t tp_write_wait; 80 struct ti_device *tp_tdev; 81 struct usb_serial_port *tp_port; 82 spinlock_t tp_lock; 83 int tp_read_urb_state; 84 int tp_write_urb_in_use; 85 struct kfifo write_fifo; 86 }; 87 88 struct ti_device { 89 struct mutex td_open_close_lock; 90 int td_open_port_count; 91 struct usb_serial *td_serial; 92 int td_is_3410; 93 int td_urb_error; 94 }; 95 96 97 /* Function Declarations */ 98 99 static int ti_startup(struct usb_serial *serial); 100 static void ti_release(struct usb_serial *serial); 101 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port); 102 static void ti_close(struct usb_serial_port *port); 103 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port, 104 const unsigned char *data, int count); 105 static int ti_write_room(struct tty_struct *tty); 106 static int ti_chars_in_buffer(struct tty_struct *tty); 107 static void ti_throttle(struct tty_struct *tty); 108 static void ti_unthrottle(struct tty_struct *tty); 109 static int ti_ioctl(struct tty_struct *tty, 110 unsigned int cmd, unsigned long arg); 111 static int ti_get_icount(struct tty_struct *tty, 112 struct serial_icounter_struct *icount); 113 static void ti_set_termios(struct tty_struct *tty, 114 struct usb_serial_port *port, struct ktermios *old_termios); 115 static int ti_tiocmget(struct tty_struct *tty); 116 static int ti_tiocmset(struct tty_struct *tty, 117 unsigned int set, unsigned int clear); 118 static void ti_break(struct tty_struct *tty, int break_state); 119 static void ti_interrupt_callback(struct urb *urb); 120 static void ti_bulk_in_callback(struct urb *urb); 121 static void ti_bulk_out_callback(struct urb *urb); 122 123 static void ti_recv(struct device *dev, struct tty_struct *tty, 124 unsigned char *data, int length); 125 static void ti_send(struct ti_port *tport); 126 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr); 127 static int ti_get_lsr(struct ti_port *tport); 128 static int ti_get_serial_info(struct ti_port *tport, 129 struct serial_struct __user *ret_arg); 130 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport, 131 struct serial_struct __user *new_arg); 132 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr); 133 134 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush); 135 136 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty); 137 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty); 138 139 static int ti_command_out_sync(struct ti_device *tdev, __u8 command, 140 __u16 moduleid, __u16 value, __u8 *data, int size); 141 static int ti_command_in_sync(struct ti_device *tdev, __u8 command, 142 __u16 moduleid, __u16 value, __u8 *data, int size); 143 144 static int ti_write_byte(struct ti_device *tdev, unsigned long addr, 145 __u8 mask, __u8 byte); 146 147 static int ti_download_firmware(struct ti_device *tdev); 148 149 150 /* Data */ 151 152 /* module parameters */ 153 static bool debug; 154 static int closing_wait = TI_DEFAULT_CLOSING_WAIT; 155 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT]; 156 static unsigned int vendor_3410_count; 157 static ushort product_3410[TI_EXTRA_VID_PID_COUNT]; 158 static unsigned int product_3410_count; 159 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT]; 160 static unsigned int vendor_5052_count; 161 static ushort product_5052[TI_EXTRA_VID_PID_COUNT]; 162 static unsigned int product_5052_count; 163 164 /* supported devices */ 165 /* the array dimension is the number of default entries plus */ 166 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */ 167 /* null entry */ 168 static struct usb_device_id ti_id_table_3410[14+TI_EXTRA_VID_PID_COUNT+1] = { 169 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) }, 170 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) }, 171 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) }, 172 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) }, 173 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) }, 174 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) }, 175 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) }, 176 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) }, 177 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) }, 178 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) }, 179 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) }, 180 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) }, 181 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) }, 182 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) }, 183 }; 184 185 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = { 186 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) }, 187 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) }, 188 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) }, 189 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) }, 190 }; 191 192 static struct usb_device_id ti_id_table_combined[18+2*TI_EXTRA_VID_PID_COUNT+1] = { 193 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) }, 194 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) }, 195 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) }, 196 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) }, 197 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) }, 198 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) }, 199 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) }, 200 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) }, 201 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) }, 202 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) }, 203 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) }, 204 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) }, 205 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) }, 206 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) }, 207 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) }, 208 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) }, 209 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) }, 210 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) }, 211 { } 212 }; 213 214 static struct usb_driver ti_usb_driver = { 215 .name = "ti_usb_3410_5052", 216 .probe = usb_serial_probe, 217 .disconnect = usb_serial_disconnect, 218 .id_table = ti_id_table_combined, 219 .no_dynamic_id = 1, 220 }; 221 222 static struct usb_serial_driver ti_1port_device = { 223 .driver = { 224 .owner = THIS_MODULE, 225 .name = "ti_usb_3410_5052_1", 226 }, 227 .description = "TI USB 3410 1 port adapter", 228 .usb_driver = &ti_usb_driver, 229 .id_table = ti_id_table_3410, 230 .num_ports = 1, 231 .attach = ti_startup, 232 .release = ti_release, 233 .open = ti_open, 234 .close = ti_close, 235 .write = ti_write, 236 .write_room = ti_write_room, 237 .chars_in_buffer = ti_chars_in_buffer, 238 .throttle = ti_throttle, 239 .unthrottle = ti_unthrottle, 240 .ioctl = ti_ioctl, 241 .set_termios = ti_set_termios, 242 .tiocmget = ti_tiocmget, 243 .tiocmset = ti_tiocmset, 244 .get_icount = ti_get_icount, 245 .break_ctl = ti_break, 246 .read_int_callback = ti_interrupt_callback, 247 .read_bulk_callback = ti_bulk_in_callback, 248 .write_bulk_callback = ti_bulk_out_callback, 249 }; 250 251 static struct usb_serial_driver ti_2port_device = { 252 .driver = { 253 .owner = THIS_MODULE, 254 .name = "ti_usb_3410_5052_2", 255 }, 256 .description = "TI USB 5052 2 port adapter", 257 .usb_driver = &ti_usb_driver, 258 .id_table = ti_id_table_5052, 259 .num_ports = 2, 260 .attach = ti_startup, 261 .release = ti_release, 262 .open = ti_open, 263 .close = ti_close, 264 .write = ti_write, 265 .write_room = ti_write_room, 266 .chars_in_buffer = ti_chars_in_buffer, 267 .throttle = ti_throttle, 268 .unthrottle = ti_unthrottle, 269 .ioctl = ti_ioctl, 270 .set_termios = ti_set_termios, 271 .tiocmget = ti_tiocmget, 272 .tiocmset = ti_tiocmset, 273 .get_icount = ti_get_icount, 274 .break_ctl = ti_break, 275 .read_int_callback = ti_interrupt_callback, 276 .read_bulk_callback = ti_bulk_in_callback, 277 .write_bulk_callback = ti_bulk_out_callback, 278 }; 279 280 281 /* Module */ 282 283 MODULE_AUTHOR(TI_DRIVER_AUTHOR); 284 MODULE_DESCRIPTION(TI_DRIVER_DESC); 285 MODULE_VERSION(TI_DRIVER_VERSION); 286 MODULE_LICENSE("GPL"); 287 288 MODULE_FIRMWARE("ti_3410.fw"); 289 MODULE_FIRMWARE("ti_5052.fw"); 290 MODULE_FIRMWARE("mts_cdma.fw"); 291 MODULE_FIRMWARE("mts_gsm.fw"); 292 MODULE_FIRMWARE("mts_edge.fw"); 293 MODULE_FIRMWARE("mts_mt9234mu.fw"); 294 MODULE_FIRMWARE("mts_mt9234zba.fw"); 295 296 module_param(debug, bool, S_IRUGO | S_IWUSR); 297 MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes"); 298 299 module_param(closing_wait, int, S_IRUGO | S_IWUSR); 300 MODULE_PARM_DESC(closing_wait, 301 "Maximum wait for data to drain in close, in .01 secs, default is 4000"); 302 303 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO); 304 MODULE_PARM_DESC(vendor_3410, 305 "Vendor ids for 3410 based devices, 1-5 short integers"); 306 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO); 307 MODULE_PARM_DESC(product_3410, 308 "Product ids for 3410 based devices, 1-5 short integers"); 309 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO); 310 MODULE_PARM_DESC(vendor_5052, 311 "Vendor ids for 5052 based devices, 1-5 short integers"); 312 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO); 313 MODULE_PARM_DESC(product_5052, 314 "Product ids for 5052 based devices, 1-5 short integers"); 315 316 MODULE_DEVICE_TABLE(usb, ti_id_table_combined); 317 318 319 /* Functions */ 320 321 static int __init ti_init(void) 322 { 323 int i, j, c; 324 int ret; 325 326 /* insert extra vendor and product ids */ 327 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1; 328 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1; 329 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) { 330 ti_id_table_3410[j].idVendor = vendor_3410[i]; 331 ti_id_table_3410[j].idProduct = product_3410[i]; 332 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE; 333 ti_id_table_combined[c].idVendor = vendor_3410[i]; 334 ti_id_table_combined[c].idProduct = product_3410[i]; 335 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE; 336 } 337 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1; 338 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) { 339 ti_id_table_5052[j].idVendor = vendor_5052[i]; 340 ti_id_table_5052[j].idProduct = product_5052[i]; 341 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE; 342 ti_id_table_combined[c].idVendor = vendor_5052[i]; 343 ti_id_table_combined[c].idProduct = product_5052[i]; 344 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE; 345 } 346 347 ret = usb_serial_register(&ti_1port_device); 348 if (ret) 349 goto failed_1port; 350 ret = usb_serial_register(&ti_2port_device); 351 if (ret) 352 goto failed_2port; 353 354 ret = usb_register(&ti_usb_driver); 355 if (ret) 356 goto failed_usb; 357 358 printk(KERN_INFO KBUILD_MODNAME ": " TI_DRIVER_VERSION ":" 359 TI_DRIVER_DESC "\n"); 360 361 return 0; 362 363 failed_usb: 364 usb_serial_deregister(&ti_2port_device); 365 failed_2port: 366 usb_serial_deregister(&ti_1port_device); 367 failed_1port: 368 return ret; 369 } 370 371 372 static void __exit ti_exit(void) 373 { 374 usb_deregister(&ti_usb_driver); 375 usb_serial_deregister(&ti_1port_device); 376 usb_serial_deregister(&ti_2port_device); 377 } 378 379 380 module_init(ti_init); 381 module_exit(ti_exit); 382 383 384 static int ti_startup(struct usb_serial *serial) 385 { 386 struct ti_device *tdev; 387 struct ti_port *tport; 388 struct usb_device *dev = serial->dev; 389 int status; 390 int i; 391 392 393 dbg("%s - product 0x%4X, num configurations %d, configuration value %d", 394 __func__, le16_to_cpu(dev->descriptor.idProduct), 395 dev->descriptor.bNumConfigurations, 396 dev->actconfig->desc.bConfigurationValue); 397 398 /* create device structure */ 399 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL); 400 if (tdev == NULL) { 401 dev_err(&dev->dev, "%s - out of memory\n", __func__); 402 return -ENOMEM; 403 } 404 mutex_init(&tdev->td_open_close_lock); 405 tdev->td_serial = serial; 406 usb_set_serial_data(serial, tdev); 407 408 /* determine device type */ 409 if (usb_match_id(serial->interface, ti_id_table_3410)) 410 tdev->td_is_3410 = 1; 411 dbg("%s - device type is %s", __func__, 412 tdev->td_is_3410 ? "3410" : "5052"); 413 414 /* if we have only 1 configuration, download firmware */ 415 if (dev->descriptor.bNumConfigurations == 1) { 416 if ((status = ti_download_firmware(tdev)) != 0) 417 goto free_tdev; 418 419 /* 3410 must be reset, 5052 resets itself */ 420 if (tdev->td_is_3410) { 421 msleep_interruptible(100); 422 usb_reset_device(dev); 423 } 424 425 status = -ENODEV; 426 goto free_tdev; 427 } 428 429 /* the second configuration must be set */ 430 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) { 431 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG); 432 status = status ? status : -ENODEV; 433 goto free_tdev; 434 } 435 436 /* set up port structures */ 437 for (i = 0; i < serial->num_ports; ++i) { 438 tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL); 439 if (tport == NULL) { 440 dev_err(&dev->dev, "%s - out of memory\n", __func__); 441 status = -ENOMEM; 442 goto free_tports; 443 } 444 spin_lock_init(&tport->tp_lock); 445 tport->tp_uart_base_addr = (i == 0 ? 446 TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR); 447 tport->tp_closing_wait = closing_wait; 448 init_waitqueue_head(&tport->tp_msr_wait); 449 init_waitqueue_head(&tport->tp_write_wait); 450 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE, 451 GFP_KERNEL)) { 452 dev_err(&dev->dev, "%s - out of memory\n", __func__); 453 kfree(tport); 454 status = -ENOMEM; 455 goto free_tports; 456 } 457 tport->tp_port = serial->port[i]; 458 tport->tp_tdev = tdev; 459 usb_set_serial_port_data(serial->port[i], tport); 460 tport->tp_uart_mode = 0; /* default is RS232 */ 461 } 462 463 return 0; 464 465 free_tports: 466 for (--i; i >= 0; --i) { 467 tport = usb_get_serial_port_data(serial->port[i]); 468 kfifo_free(&tport->write_fifo); 469 kfree(tport); 470 usb_set_serial_port_data(serial->port[i], NULL); 471 } 472 free_tdev: 473 kfree(tdev); 474 usb_set_serial_data(serial, NULL); 475 return status; 476 } 477 478 479 static void ti_release(struct usb_serial *serial) 480 { 481 int i; 482 struct ti_device *tdev = usb_get_serial_data(serial); 483 struct ti_port *tport; 484 485 dbg("%s", __func__); 486 487 for (i = 0; i < serial->num_ports; ++i) { 488 tport = usb_get_serial_port_data(serial->port[i]); 489 if (tport) { 490 kfifo_free(&tport->write_fifo); 491 kfree(tport); 492 } 493 } 494 495 kfree(tdev); 496 } 497 498 499 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port) 500 { 501 struct ti_port *tport = usb_get_serial_port_data(port); 502 struct ti_device *tdev; 503 struct usb_device *dev; 504 struct urb *urb; 505 int port_number; 506 int status; 507 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS | 508 TI_PIPE_TIMEOUT_ENABLE | 509 (TI_TRANSFER_TIMEOUT << 2)); 510 511 dbg("%s - port %d", __func__, port->number); 512 513 if (tport == NULL) 514 return -ENODEV; 515 516 dev = port->serial->dev; 517 tdev = tport->tp_tdev; 518 519 /* only one open on any port on a device at a time */ 520 if (mutex_lock_interruptible(&tdev->td_open_close_lock)) 521 return -ERESTARTSYS; 522 523 port_number = port->number - port->serial->minor; 524 525 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount)); 526 527 tport->tp_msr = 0; 528 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR); 529 530 /* start interrupt urb the first time a port is opened on this device */ 531 if (tdev->td_open_port_count == 0) { 532 dbg("%s - start interrupt in urb", __func__); 533 urb = tdev->td_serial->port[0]->interrupt_in_urb; 534 if (!urb) { 535 dev_err(&port->dev, "%s - no interrupt urb\n", 536 __func__); 537 status = -EINVAL; 538 goto release_lock; 539 } 540 urb->context = tdev; 541 status = usb_submit_urb(urb, GFP_KERNEL); 542 if (status) { 543 dev_err(&port->dev, 544 "%s - submit interrupt urb failed, %d\n", 545 __func__, status); 546 goto release_lock; 547 } 548 } 549 550 if (tty) 551 ti_set_termios(tty, port, tty->termios); 552 553 dbg("%s - sending TI_OPEN_PORT", __func__); 554 status = ti_command_out_sync(tdev, TI_OPEN_PORT, 555 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0); 556 if (status) { 557 dev_err(&port->dev, "%s - cannot send open command, %d\n", 558 __func__, status); 559 goto unlink_int_urb; 560 } 561 562 dbg("%s - sending TI_START_PORT", __func__); 563 status = ti_command_out_sync(tdev, TI_START_PORT, 564 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0); 565 if (status) { 566 dev_err(&port->dev, "%s - cannot send start command, %d\n", 567 __func__, status); 568 goto unlink_int_urb; 569 } 570 571 dbg("%s - sending TI_PURGE_PORT", __func__); 572 status = ti_command_out_sync(tdev, TI_PURGE_PORT, 573 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0); 574 if (status) { 575 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", 576 __func__, status); 577 goto unlink_int_urb; 578 } 579 status = ti_command_out_sync(tdev, TI_PURGE_PORT, 580 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0); 581 if (status) { 582 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", 583 __func__, status); 584 goto unlink_int_urb; 585 } 586 587 /* reset the data toggle on the bulk endpoints to work around bug in 588 * host controllers where things get out of sync some times */ 589 usb_clear_halt(dev, port->write_urb->pipe); 590 usb_clear_halt(dev, port->read_urb->pipe); 591 592 if (tty) 593 ti_set_termios(tty, port, tty->termios); 594 595 dbg("%s - sending TI_OPEN_PORT (2)", __func__); 596 status = ti_command_out_sync(tdev, TI_OPEN_PORT, 597 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0); 598 if (status) { 599 dev_err(&port->dev, "%s - cannot send open command (2), %d\n", 600 __func__, status); 601 goto unlink_int_urb; 602 } 603 604 dbg("%s - sending TI_START_PORT (2)", __func__); 605 status = ti_command_out_sync(tdev, TI_START_PORT, 606 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0); 607 if (status) { 608 dev_err(&port->dev, "%s - cannot send start command (2), %d\n", 609 __func__, status); 610 goto unlink_int_urb; 611 } 612 613 /* start read urb */ 614 dbg("%s - start read urb", __func__); 615 urb = port->read_urb; 616 if (!urb) { 617 dev_err(&port->dev, "%s - no read urb\n", __func__); 618 status = -EINVAL; 619 goto unlink_int_urb; 620 } 621 tport->tp_read_urb_state = TI_READ_URB_RUNNING; 622 urb->context = tport; 623 status = usb_submit_urb(urb, GFP_KERNEL); 624 if (status) { 625 dev_err(&port->dev, "%s - submit read urb failed, %d\n", 626 __func__, status); 627 goto unlink_int_urb; 628 } 629 630 tport->tp_is_open = 1; 631 ++tdev->td_open_port_count; 632 633 goto release_lock; 634 635 unlink_int_urb: 636 if (tdev->td_open_port_count == 0) 637 usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 638 release_lock: 639 mutex_unlock(&tdev->td_open_close_lock); 640 dbg("%s - exit %d", __func__, status); 641 return status; 642 } 643 644 645 static void ti_close(struct usb_serial_port *port) 646 { 647 struct ti_device *tdev; 648 struct ti_port *tport; 649 int port_number; 650 int status; 651 int do_unlock; 652 653 dbg("%s - port %d", __func__, port->number); 654 655 tdev = usb_get_serial_data(port->serial); 656 tport = usb_get_serial_port_data(port); 657 if (tdev == NULL || tport == NULL) 658 return; 659 660 tport->tp_is_open = 0; 661 662 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1); 663 664 usb_kill_urb(port->read_urb); 665 usb_kill_urb(port->write_urb); 666 tport->tp_write_urb_in_use = 0; 667 668 port_number = port->number - port->serial->minor; 669 670 dbg("%s - sending TI_CLOSE_PORT", __func__); 671 status = ti_command_out_sync(tdev, TI_CLOSE_PORT, 672 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0); 673 if (status) 674 dev_err(&port->dev, 675 "%s - cannot send close port command, %d\n" 676 , __func__, status); 677 678 /* if mutex_lock is interrupted, continue anyway */ 679 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock); 680 --tport->tp_tdev->td_open_port_count; 681 if (tport->tp_tdev->td_open_port_count <= 0) { 682 /* last port is closed, shut down interrupt urb */ 683 usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 684 tport->tp_tdev->td_open_port_count = 0; 685 } 686 if (do_unlock) 687 mutex_unlock(&tdev->td_open_close_lock); 688 689 dbg("%s - exit", __func__); 690 } 691 692 693 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port, 694 const unsigned char *data, int count) 695 { 696 struct ti_port *tport = usb_get_serial_port_data(port); 697 698 dbg("%s - port %d", __func__, port->number); 699 700 if (count == 0) { 701 dbg("%s - write request of 0 bytes", __func__); 702 return 0; 703 } 704 705 if (tport == NULL || !tport->tp_is_open) 706 return -ENODEV; 707 708 count = kfifo_in_locked(&tport->write_fifo, data, count, 709 &tport->tp_lock); 710 ti_send(tport); 711 712 return count; 713 } 714 715 716 static int ti_write_room(struct tty_struct *tty) 717 { 718 struct usb_serial_port *port = tty->driver_data; 719 struct ti_port *tport = usb_get_serial_port_data(port); 720 int room = 0; 721 unsigned long flags; 722 723 dbg("%s - port %d", __func__, port->number); 724 725 if (tport == NULL) 726 return 0; 727 728 spin_lock_irqsave(&tport->tp_lock, flags); 729 room = kfifo_avail(&tport->write_fifo); 730 spin_unlock_irqrestore(&tport->tp_lock, flags); 731 732 dbg("%s - returns %d", __func__, room); 733 return room; 734 } 735 736 737 static int ti_chars_in_buffer(struct tty_struct *tty) 738 { 739 struct usb_serial_port *port = tty->driver_data; 740 struct ti_port *tport = usb_get_serial_port_data(port); 741 int chars = 0; 742 unsigned long flags; 743 744 dbg("%s - port %d", __func__, port->number); 745 746 if (tport == NULL) 747 return 0; 748 749 spin_lock_irqsave(&tport->tp_lock, flags); 750 chars = kfifo_len(&tport->write_fifo); 751 spin_unlock_irqrestore(&tport->tp_lock, flags); 752 753 dbg("%s - returns %d", __func__, chars); 754 return chars; 755 } 756 757 758 static void ti_throttle(struct tty_struct *tty) 759 { 760 struct usb_serial_port *port = tty->driver_data; 761 struct ti_port *tport = usb_get_serial_port_data(port); 762 763 dbg("%s - port %d", __func__, port->number); 764 765 if (tport == NULL) 766 return; 767 768 if (I_IXOFF(tty) || C_CRTSCTS(tty)) 769 ti_stop_read(tport, tty); 770 771 } 772 773 774 static void ti_unthrottle(struct tty_struct *tty) 775 { 776 struct usb_serial_port *port = tty->driver_data; 777 struct ti_port *tport = usb_get_serial_port_data(port); 778 int status; 779 780 dbg("%s - port %d", __func__, port->number); 781 782 if (tport == NULL) 783 return; 784 785 if (I_IXOFF(tty) || C_CRTSCTS(tty)) { 786 status = ti_restart_read(tport, tty); 787 if (status) 788 dev_err(&port->dev, "%s - cannot restart read, %d\n", 789 __func__, status); 790 } 791 } 792 793 static int ti_get_icount(struct tty_struct *tty, 794 struct serial_icounter_struct *icount) 795 { 796 struct usb_serial_port *port = tty->driver_data; 797 struct ti_port *tport = usb_get_serial_port_data(port); 798 struct async_icount cnow = tport->tp_icount; 799 800 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", 801 __func__, port->number, 802 cnow.rx, cnow.tx); 803 804 icount->cts = cnow.cts; 805 icount->dsr = cnow.dsr; 806 icount->rng = cnow.rng; 807 icount->dcd = cnow.dcd; 808 icount->rx = cnow.rx; 809 icount->tx = cnow.tx; 810 icount->frame = cnow.frame; 811 icount->overrun = cnow.overrun; 812 icount->parity = cnow.parity; 813 icount->brk = cnow.brk; 814 icount->buf_overrun = cnow.buf_overrun; 815 816 return 0; 817 } 818 819 static int ti_ioctl(struct tty_struct *tty, 820 unsigned int cmd, unsigned long arg) 821 { 822 struct usb_serial_port *port = tty->driver_data; 823 struct ti_port *tport = usb_get_serial_port_data(port); 824 struct async_icount cnow; 825 struct async_icount cprev; 826 827 dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd); 828 829 if (tport == NULL) 830 return -ENODEV; 831 832 switch (cmd) { 833 case TIOCGSERIAL: 834 dbg("%s - (%d) TIOCGSERIAL", __func__, port->number); 835 return ti_get_serial_info(tport, 836 (struct serial_struct __user *)arg); 837 case TIOCSSERIAL: 838 dbg("%s - (%d) TIOCSSERIAL", __func__, port->number); 839 return ti_set_serial_info(tty, tport, 840 (struct serial_struct __user *)arg); 841 case TIOCMIWAIT: 842 dbg("%s - (%d) TIOCMIWAIT", __func__, port->number); 843 cprev = tport->tp_icount; 844 while (1) { 845 interruptible_sleep_on(&tport->tp_msr_wait); 846 if (signal_pending(current)) 847 return -ERESTARTSYS; 848 cnow = tport->tp_icount; 849 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 850 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 851 return -EIO; /* no change => error */ 852 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 853 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 854 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 855 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) 856 return 0; 857 cprev = cnow; 858 } 859 break; 860 } 861 return -ENOIOCTLCMD; 862 } 863 864 865 static void ti_set_termios(struct tty_struct *tty, 866 struct usb_serial_port *port, struct ktermios *old_termios) 867 { 868 struct ti_port *tport = usb_get_serial_port_data(port); 869 struct ti_uart_config *config; 870 tcflag_t cflag, iflag; 871 int baud; 872 int status; 873 int port_number = port->number - port->serial->minor; 874 unsigned int mcr; 875 876 dbg("%s - port %d", __func__, port->number); 877 878 cflag = tty->termios->c_cflag; 879 iflag = tty->termios->c_iflag; 880 881 dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag); 882 dbg("%s - old clfag %08x, old iflag %08x", __func__, 883 old_termios->c_cflag, old_termios->c_iflag); 884 885 if (tport == NULL) 886 return; 887 888 config = kmalloc(sizeof(*config), GFP_KERNEL); 889 if (!config) { 890 dev_err(&port->dev, "%s - out of memory\n", __func__); 891 return; 892 } 893 894 config->wFlags = 0; 895 896 /* these flags must be set */ 897 config->wFlags |= TI_UART_ENABLE_MS_INTS; 898 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA; 899 config->bUartMode = (__u8)(tport->tp_uart_mode); 900 901 switch (cflag & CSIZE) { 902 case CS5: 903 config->bDataBits = TI_UART_5_DATA_BITS; 904 break; 905 case CS6: 906 config->bDataBits = TI_UART_6_DATA_BITS; 907 break; 908 case CS7: 909 config->bDataBits = TI_UART_7_DATA_BITS; 910 break; 911 default: 912 case CS8: 913 config->bDataBits = TI_UART_8_DATA_BITS; 914 break; 915 } 916 917 /* CMSPAR isn't supported by this driver */ 918 tty->termios->c_cflag &= ~CMSPAR; 919 920 if (cflag & PARENB) { 921 if (cflag & PARODD) { 922 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING; 923 config->bParity = TI_UART_ODD_PARITY; 924 } else { 925 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING; 926 config->bParity = TI_UART_EVEN_PARITY; 927 } 928 } else { 929 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING; 930 config->bParity = TI_UART_NO_PARITY; 931 } 932 933 if (cflag & CSTOPB) 934 config->bStopBits = TI_UART_2_STOP_BITS; 935 else 936 config->bStopBits = TI_UART_1_STOP_BITS; 937 938 if (cflag & CRTSCTS) { 939 /* RTS flow control must be off to drop RTS for baud rate B0 */ 940 if ((cflag & CBAUD) != B0) 941 config->wFlags |= TI_UART_ENABLE_RTS_IN; 942 config->wFlags |= TI_UART_ENABLE_CTS_OUT; 943 } else { 944 tty->hw_stopped = 0; 945 ti_restart_read(tport, tty); 946 } 947 948 if (I_IXOFF(tty) || I_IXON(tty)) { 949 config->cXon = START_CHAR(tty); 950 config->cXoff = STOP_CHAR(tty); 951 952 if (I_IXOFF(tty)) 953 config->wFlags |= TI_UART_ENABLE_X_IN; 954 else 955 ti_restart_read(tport, tty); 956 957 if (I_IXON(tty)) 958 config->wFlags |= TI_UART_ENABLE_X_OUT; 959 } 960 961 baud = tty_get_baud_rate(tty); 962 if (!baud) 963 baud = 9600; 964 if (tport->tp_tdev->td_is_3410) 965 config->wBaudRate = (__u16)((923077 + baud/2) / baud); 966 else 967 config->wBaudRate = (__u16)((461538 + baud/2) / baud); 968 969 /* FIXME: Should calculate resulting baud here and report it back */ 970 if ((cflag & CBAUD) != B0) 971 tty_encode_baud_rate(tty, baud, baud); 972 973 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d", 974 __func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode); 975 976 cpu_to_be16s(&config->wBaudRate); 977 cpu_to_be16s(&config->wFlags); 978 979 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG, 980 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config, 981 sizeof(*config)); 982 if (status) 983 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", 984 __func__, port_number, status); 985 986 /* SET_CONFIG asserts RTS and DTR, reset them correctly */ 987 mcr = tport->tp_shadow_mcr; 988 /* if baud rate is B0, clear RTS and DTR */ 989 if ((cflag & CBAUD) == B0) 990 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS); 991 status = ti_set_mcr(tport, mcr); 992 if (status) 993 dev_err(&port->dev, 994 "%s - cannot set modem control on port %d, %d\n", 995 __func__, port_number, status); 996 997 kfree(config); 998 } 999 1000 1001 static int ti_tiocmget(struct tty_struct *tty) 1002 { 1003 struct usb_serial_port *port = tty->driver_data; 1004 struct ti_port *tport = usb_get_serial_port_data(port); 1005 unsigned int result; 1006 unsigned int msr; 1007 unsigned int mcr; 1008 unsigned long flags; 1009 1010 dbg("%s - port %d", __func__, port->number); 1011 1012 if (tport == NULL) 1013 return -ENODEV; 1014 1015 spin_lock_irqsave(&tport->tp_lock, flags); 1016 msr = tport->tp_msr; 1017 mcr = tport->tp_shadow_mcr; 1018 spin_unlock_irqrestore(&tport->tp_lock, flags); 1019 1020 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0) 1021 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0) 1022 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0) 1023 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0) 1024 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0) 1025 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0) 1026 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0); 1027 1028 dbg("%s - 0x%04X", __func__, result); 1029 1030 return result; 1031 } 1032 1033 1034 static int ti_tiocmset(struct tty_struct *tty, 1035 unsigned int set, unsigned int clear) 1036 { 1037 struct usb_serial_port *port = tty->driver_data; 1038 struct ti_port *tport = usb_get_serial_port_data(port); 1039 unsigned int mcr; 1040 unsigned long flags; 1041 1042 dbg("%s - port %d", __func__, port->number); 1043 1044 if (tport == NULL) 1045 return -ENODEV; 1046 1047 spin_lock_irqsave(&tport->tp_lock, flags); 1048 mcr = tport->tp_shadow_mcr; 1049 1050 if (set & TIOCM_RTS) 1051 mcr |= TI_MCR_RTS; 1052 if (set & TIOCM_DTR) 1053 mcr |= TI_MCR_DTR; 1054 if (set & TIOCM_LOOP) 1055 mcr |= TI_MCR_LOOP; 1056 1057 if (clear & TIOCM_RTS) 1058 mcr &= ~TI_MCR_RTS; 1059 if (clear & TIOCM_DTR) 1060 mcr &= ~TI_MCR_DTR; 1061 if (clear & TIOCM_LOOP) 1062 mcr &= ~TI_MCR_LOOP; 1063 spin_unlock_irqrestore(&tport->tp_lock, flags); 1064 1065 return ti_set_mcr(tport, mcr); 1066 } 1067 1068 1069 static void ti_break(struct tty_struct *tty, int break_state) 1070 { 1071 struct usb_serial_port *port = tty->driver_data; 1072 struct ti_port *tport = usb_get_serial_port_data(port); 1073 int status; 1074 1075 dbg("%s - state = %d", __func__, break_state); 1076 1077 if (tport == NULL) 1078 return; 1079 1080 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0); 1081 1082 status = ti_write_byte(tport->tp_tdev, 1083 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR, 1084 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0); 1085 1086 if (status) 1087 dbg("%s - error setting break, %d", __func__, status); 1088 } 1089 1090 1091 static void ti_interrupt_callback(struct urb *urb) 1092 { 1093 struct ti_device *tdev = urb->context; 1094 struct usb_serial_port *port; 1095 struct usb_serial *serial = tdev->td_serial; 1096 struct ti_port *tport; 1097 struct device *dev = &urb->dev->dev; 1098 unsigned char *data = urb->transfer_buffer; 1099 int length = urb->actual_length; 1100 int port_number; 1101 int function; 1102 int status = urb->status; 1103 int retval; 1104 __u8 msr; 1105 1106 dbg("%s", __func__); 1107 1108 switch (status) { 1109 case 0: 1110 break; 1111 case -ECONNRESET: 1112 case -ENOENT: 1113 case -ESHUTDOWN: 1114 dbg("%s - urb shutting down, %d", __func__, status); 1115 tdev->td_urb_error = 1; 1116 return; 1117 default: 1118 dev_err(dev, "%s - nonzero urb status, %d\n", 1119 __func__, status); 1120 tdev->td_urb_error = 1; 1121 goto exit; 1122 } 1123 1124 if (length != 2) { 1125 dbg("%s - bad packet size, %d", __func__, length); 1126 goto exit; 1127 } 1128 1129 if (data[0] == TI_CODE_HARDWARE_ERROR) { 1130 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]); 1131 goto exit; 1132 } 1133 1134 port_number = TI_GET_PORT_FROM_CODE(data[0]); 1135 function = TI_GET_FUNC_FROM_CODE(data[0]); 1136 1137 dbg("%s - port_number %d, function %d, data 0x%02X", 1138 __func__, port_number, function, data[1]); 1139 1140 if (port_number >= serial->num_ports) { 1141 dev_err(dev, "%s - bad port number, %d\n", 1142 __func__, port_number); 1143 goto exit; 1144 } 1145 1146 port = serial->port[port_number]; 1147 1148 tport = usb_get_serial_port_data(port); 1149 if (!tport) 1150 goto exit; 1151 1152 switch (function) { 1153 case TI_CODE_DATA_ERROR: 1154 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", 1155 __func__, port_number, data[1]); 1156 break; 1157 1158 case TI_CODE_MODEM_STATUS: 1159 msr = data[1]; 1160 dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr); 1161 ti_handle_new_msr(tport, msr); 1162 break; 1163 1164 default: 1165 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", 1166 __func__, data[1]); 1167 break; 1168 } 1169 1170 exit: 1171 retval = usb_submit_urb(urb, GFP_ATOMIC); 1172 if (retval) 1173 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n", 1174 __func__, retval); 1175 } 1176 1177 1178 static void ti_bulk_in_callback(struct urb *urb) 1179 { 1180 struct ti_port *tport = urb->context; 1181 struct usb_serial_port *port = tport->tp_port; 1182 struct device *dev = &urb->dev->dev; 1183 int status = urb->status; 1184 int retval = 0; 1185 struct tty_struct *tty; 1186 1187 dbg("%s", __func__); 1188 1189 switch (status) { 1190 case 0: 1191 break; 1192 case -ECONNRESET: 1193 case -ENOENT: 1194 case -ESHUTDOWN: 1195 dbg("%s - urb shutting down, %d", __func__, status); 1196 tport->tp_tdev->td_urb_error = 1; 1197 wake_up_interruptible(&tport->tp_write_wait); 1198 return; 1199 default: 1200 dev_err(dev, "%s - nonzero urb status, %d\n", 1201 __func__, status); 1202 tport->tp_tdev->td_urb_error = 1; 1203 wake_up_interruptible(&tport->tp_write_wait); 1204 } 1205 1206 if (status == -EPIPE) 1207 goto exit; 1208 1209 if (status) { 1210 dev_err(dev, "%s - stopping read!\n", __func__); 1211 return; 1212 } 1213 1214 tty = tty_port_tty_get(&port->port); 1215 if (tty) { 1216 if (urb->actual_length) { 1217 usb_serial_debug_data(debug, dev, __func__, 1218 urb->actual_length, urb->transfer_buffer); 1219 1220 if (!tport->tp_is_open) 1221 dbg("%s - port closed, dropping data", 1222 __func__); 1223 else 1224 ti_recv(&urb->dev->dev, tty, 1225 urb->transfer_buffer, 1226 urb->actual_length); 1227 spin_lock(&tport->tp_lock); 1228 tport->tp_icount.rx += urb->actual_length; 1229 spin_unlock(&tport->tp_lock); 1230 } 1231 tty_kref_put(tty); 1232 } 1233 1234 exit: 1235 /* continue to read unless stopping */ 1236 spin_lock(&tport->tp_lock); 1237 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) 1238 retval = usb_submit_urb(urb, GFP_ATOMIC); 1239 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) 1240 tport->tp_read_urb_state = TI_READ_URB_STOPPED; 1241 1242 spin_unlock(&tport->tp_lock); 1243 if (retval) 1244 dev_err(dev, "%s - resubmit read urb failed, %d\n", 1245 __func__, retval); 1246 } 1247 1248 1249 static void ti_bulk_out_callback(struct urb *urb) 1250 { 1251 struct ti_port *tport = urb->context; 1252 struct usb_serial_port *port = tport->tp_port; 1253 struct device *dev = &urb->dev->dev; 1254 int status = urb->status; 1255 1256 dbg("%s - port %d", __func__, port->number); 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 dbg("%s - urb shutting down, %d", __func__, status); 1267 tport->tp_tdev->td_urb_error = 1; 1268 wake_up_interruptible(&tport->tp_write_wait); 1269 return; 1270 default: 1271 dev_err(dev, "%s - nonzero urb status, %d\n", 1272 __func__, status); 1273 tport->tp_tdev->td_urb_error = 1; 1274 wake_up_interruptible(&tport->tp_write_wait); 1275 } 1276 1277 /* send any buffered data */ 1278 ti_send(tport); 1279 } 1280 1281 1282 static void ti_recv(struct device *dev, struct tty_struct *tty, 1283 unsigned char *data, int length) 1284 { 1285 int cnt; 1286 1287 do { 1288 cnt = tty_insert_flip_string(tty, data, length); 1289 if (cnt < length) { 1290 dev_err(dev, "%s - dropping data, %d bytes lost\n", 1291 __func__, length - cnt); 1292 if (cnt == 0) 1293 break; 1294 } 1295 tty_flip_buffer_push(tty); 1296 data += cnt; 1297 length -= cnt; 1298 } while (length > 0); 1299 1300 } 1301 1302 1303 static void ti_send(struct ti_port *tport) 1304 { 1305 int count, result; 1306 struct usb_serial_port *port = tport->tp_port; 1307 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */ 1308 unsigned long flags; 1309 1310 1311 dbg("%s - port %d", __func__, port->number); 1312 1313 spin_lock_irqsave(&tport->tp_lock, flags); 1314 1315 if (tport->tp_write_urb_in_use) 1316 goto unlock; 1317 1318 count = kfifo_out(&tport->write_fifo, 1319 port->write_urb->transfer_buffer, 1320 port->bulk_out_size); 1321 1322 if (count == 0) 1323 goto unlock; 1324 1325 tport->tp_write_urb_in_use = 1; 1326 1327 spin_unlock_irqrestore(&tport->tp_lock, flags); 1328 1329 usb_serial_debug_data(debug, &port->dev, __func__, count, 1330 port->write_urb->transfer_buffer); 1331 1332 usb_fill_bulk_urb(port->write_urb, port->serial->dev, 1333 usb_sndbulkpipe(port->serial->dev, 1334 port->bulk_out_endpointAddress), 1335 port->write_urb->transfer_buffer, count, 1336 ti_bulk_out_callback, tport); 1337 1338 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 1339 if (result) { 1340 dev_err(&port->dev, "%s - submit write urb failed, %d\n", 1341 __func__, result); 1342 tport->tp_write_urb_in_use = 0; 1343 /* TODO: reschedule ti_send */ 1344 } else { 1345 spin_lock_irqsave(&tport->tp_lock, flags); 1346 tport->tp_icount.tx += count; 1347 spin_unlock_irqrestore(&tport->tp_lock, flags); 1348 } 1349 1350 /* more room in the buffer for new writes, wakeup */ 1351 if (tty) 1352 tty_wakeup(tty); 1353 tty_kref_put(tty); 1354 wake_up_interruptible(&tport->tp_write_wait); 1355 return; 1356 unlock: 1357 spin_unlock_irqrestore(&tport->tp_lock, flags); 1358 tty_kref_put(tty); 1359 return; 1360 } 1361 1362 1363 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr) 1364 { 1365 unsigned long flags; 1366 int status; 1367 1368 status = ti_write_byte(tport->tp_tdev, 1369 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR, 1370 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr); 1371 1372 spin_lock_irqsave(&tport->tp_lock, flags); 1373 if (!status) 1374 tport->tp_shadow_mcr = mcr; 1375 spin_unlock_irqrestore(&tport->tp_lock, flags); 1376 1377 return status; 1378 } 1379 1380 1381 static int ti_get_lsr(struct ti_port *tport) 1382 { 1383 int size, status; 1384 struct ti_device *tdev = tport->tp_tdev; 1385 struct usb_serial_port *port = tport->tp_port; 1386 int port_number = port->number - port->serial->minor; 1387 struct ti_port_status *data; 1388 1389 dbg("%s - port %d", __func__, port->number); 1390 1391 size = sizeof(struct ti_port_status); 1392 data = kmalloc(size, GFP_KERNEL); 1393 if (!data) { 1394 dev_err(&port->dev, "%s - out of memory\n", __func__); 1395 return -ENOMEM; 1396 } 1397 1398 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS, 1399 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size); 1400 if (status) { 1401 dev_err(&port->dev, 1402 "%s - get port status command failed, %d\n", 1403 __func__, status); 1404 goto free_data; 1405 } 1406 1407 dbg("%s - lsr 0x%02X", __func__, data->bLSR); 1408 1409 tport->tp_lsr = data->bLSR; 1410 1411 free_data: 1412 kfree(data); 1413 return status; 1414 } 1415 1416 1417 static int ti_get_serial_info(struct ti_port *tport, 1418 struct serial_struct __user *ret_arg) 1419 { 1420 struct usb_serial_port *port = tport->tp_port; 1421 struct serial_struct ret_serial; 1422 1423 if (!ret_arg) 1424 return -EFAULT; 1425 1426 memset(&ret_serial, 0, sizeof(ret_serial)); 1427 1428 ret_serial.type = PORT_16550A; 1429 ret_serial.line = port->serial->minor; 1430 ret_serial.port = port->number - port->serial->minor; 1431 ret_serial.flags = tport->tp_flags; 1432 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE; 1433 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800; 1434 ret_serial.closing_wait = tport->tp_closing_wait; 1435 1436 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg))) 1437 return -EFAULT; 1438 1439 return 0; 1440 } 1441 1442 1443 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport, 1444 struct serial_struct __user *new_arg) 1445 { 1446 struct serial_struct new_serial; 1447 1448 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial))) 1449 return -EFAULT; 1450 1451 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS; 1452 tport->tp_closing_wait = new_serial.closing_wait; 1453 1454 return 0; 1455 } 1456 1457 1458 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr) 1459 { 1460 struct async_icount *icount; 1461 struct tty_struct *tty; 1462 unsigned long flags; 1463 1464 dbg("%s - msr 0x%02X", __func__, msr); 1465 1466 if (msr & TI_MSR_DELTA_MASK) { 1467 spin_lock_irqsave(&tport->tp_lock, flags); 1468 icount = &tport->tp_icount; 1469 if (msr & TI_MSR_DELTA_CTS) 1470 icount->cts++; 1471 if (msr & TI_MSR_DELTA_DSR) 1472 icount->dsr++; 1473 if (msr & TI_MSR_DELTA_CD) 1474 icount->dcd++; 1475 if (msr & TI_MSR_DELTA_RI) 1476 icount->rng++; 1477 wake_up_interruptible(&tport->tp_msr_wait); 1478 spin_unlock_irqrestore(&tport->tp_lock, flags); 1479 } 1480 1481 tport->tp_msr = msr & TI_MSR_MASK; 1482 1483 /* handle CTS flow control */ 1484 tty = tty_port_tty_get(&tport->tp_port->port); 1485 if (tty && C_CRTSCTS(tty)) { 1486 if (msr & TI_MSR_CTS) { 1487 tty->hw_stopped = 0; 1488 tty_wakeup(tty); 1489 } else { 1490 tty->hw_stopped = 1; 1491 } 1492 } 1493 tty_kref_put(tty); 1494 } 1495 1496 1497 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush) 1498 { 1499 struct ti_device *tdev = tport->tp_tdev; 1500 struct usb_serial_port *port = tport->tp_port; 1501 wait_queue_t wait; 1502 1503 dbg("%s - port %d", __func__, port->number); 1504 1505 spin_lock_irq(&tport->tp_lock); 1506 1507 /* wait for data to drain from the buffer */ 1508 tdev->td_urb_error = 0; 1509 init_waitqueue_entry(&wait, current); 1510 add_wait_queue(&tport->tp_write_wait, &wait); 1511 for (;;) { 1512 set_current_state(TASK_INTERRUPTIBLE); 1513 if (kfifo_len(&tport->write_fifo) == 0 1514 || timeout == 0 || signal_pending(current) 1515 || tdev->td_urb_error 1516 || port->serial->disconnected) /* disconnect */ 1517 break; 1518 spin_unlock_irq(&tport->tp_lock); 1519 timeout = schedule_timeout(timeout); 1520 spin_lock_irq(&tport->tp_lock); 1521 } 1522 set_current_state(TASK_RUNNING); 1523 remove_wait_queue(&tport->tp_write_wait, &wait); 1524 1525 /* flush any remaining data in the buffer */ 1526 if (flush) 1527 kfifo_reset_out(&tport->write_fifo); 1528 1529 spin_unlock_irq(&tport->tp_lock); 1530 1531 mutex_lock(&port->serial->disc_mutex); 1532 /* wait for data to drain from the device */ 1533 /* wait for empty tx register, plus 20 ms */ 1534 timeout += jiffies; 1535 tport->tp_lsr &= ~TI_LSR_TX_EMPTY; 1536 while ((long)(jiffies - timeout) < 0 && !signal_pending(current) 1537 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error 1538 && !port->serial->disconnected) { 1539 if (ti_get_lsr(tport)) 1540 break; 1541 mutex_unlock(&port->serial->disc_mutex); 1542 msleep_interruptible(20); 1543 mutex_lock(&port->serial->disc_mutex); 1544 } 1545 mutex_unlock(&port->serial->disc_mutex); 1546 } 1547 1548 1549 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty) 1550 { 1551 unsigned long flags; 1552 1553 spin_lock_irqsave(&tport->tp_lock, flags); 1554 1555 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) 1556 tport->tp_read_urb_state = TI_READ_URB_STOPPING; 1557 1558 spin_unlock_irqrestore(&tport->tp_lock, flags); 1559 } 1560 1561 1562 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty) 1563 { 1564 struct urb *urb; 1565 int status = 0; 1566 unsigned long flags; 1567 1568 spin_lock_irqsave(&tport->tp_lock, flags); 1569 1570 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) { 1571 tport->tp_read_urb_state = TI_READ_URB_RUNNING; 1572 urb = tport->tp_port->read_urb; 1573 spin_unlock_irqrestore(&tport->tp_lock, flags); 1574 urb->context = tport; 1575 status = usb_submit_urb(urb, GFP_KERNEL); 1576 } else { 1577 tport->tp_read_urb_state = TI_READ_URB_RUNNING; 1578 spin_unlock_irqrestore(&tport->tp_lock, flags); 1579 } 1580 1581 return status; 1582 } 1583 1584 1585 static int ti_command_out_sync(struct ti_device *tdev, __u8 command, 1586 __u16 moduleid, __u16 value, __u8 *data, int size) 1587 { 1588 int status; 1589 1590 status = usb_control_msg(tdev->td_serial->dev, 1591 usb_sndctrlpipe(tdev->td_serial->dev, 0), command, 1592 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT), 1593 value, moduleid, data, size, 1000); 1594 1595 if (status == size) 1596 status = 0; 1597 1598 if (status > 0) 1599 status = -ECOMM; 1600 1601 return status; 1602 } 1603 1604 1605 static int ti_command_in_sync(struct ti_device *tdev, __u8 command, 1606 __u16 moduleid, __u16 value, __u8 *data, int size) 1607 { 1608 int status; 1609 1610 status = usb_control_msg(tdev->td_serial->dev, 1611 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command, 1612 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN), 1613 value, moduleid, data, size, 1000); 1614 1615 if (status == size) 1616 status = 0; 1617 1618 if (status > 0) 1619 status = -ECOMM; 1620 1621 return status; 1622 } 1623 1624 1625 static int ti_write_byte(struct ti_device *tdev, unsigned long addr, 1626 __u8 mask, __u8 byte) 1627 { 1628 int status; 1629 unsigned int size; 1630 struct ti_write_data_bytes *data; 1631 struct device *dev = &tdev->td_serial->dev->dev; 1632 1633 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X", 1634 __func__, addr, mask, byte); 1635 1636 size = sizeof(struct ti_write_data_bytes) + 2; 1637 data = kmalloc(size, GFP_KERNEL); 1638 if (!data) { 1639 dev_err(dev, "%s - out of memory\n", __func__); 1640 return -ENOMEM; 1641 } 1642 1643 data->bAddrType = TI_RW_DATA_ADDR_XDATA; 1644 data->bDataType = TI_RW_DATA_BYTE; 1645 data->bDataCounter = 1; 1646 data->wBaseAddrHi = cpu_to_be16(addr>>16); 1647 data->wBaseAddrLo = cpu_to_be16(addr); 1648 data->bData[0] = mask; 1649 data->bData[1] = byte; 1650 1651 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0, 1652 (__u8 *)data, size); 1653 1654 if (status < 0) 1655 dev_err(dev, "%s - failed, %d\n", __func__, status); 1656 1657 kfree(data); 1658 1659 return status; 1660 } 1661 1662 static int ti_do_download(struct usb_device *dev, int pipe, 1663 u8 *buffer, int size) 1664 { 1665 int pos; 1666 u8 cs = 0; 1667 int done; 1668 struct ti_firmware_header *header; 1669 int status = 0; 1670 int len; 1671 1672 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++) 1673 cs = (__u8)(cs + buffer[pos]); 1674 1675 header = (struct ti_firmware_header *)buffer; 1676 header->wLength = cpu_to_le16((__u16)(size 1677 - sizeof(struct ti_firmware_header))); 1678 header->bCheckSum = cs; 1679 1680 dbg("%s - downloading firmware", __func__); 1681 for (pos = 0; pos < size; pos += done) { 1682 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE); 1683 status = usb_bulk_msg(dev, pipe, buffer + pos, len, 1684 &done, 1000); 1685 if (status) 1686 break; 1687 } 1688 return status; 1689 } 1690 1691 static int ti_download_firmware(struct ti_device *tdev) 1692 { 1693 int status; 1694 int buffer_size; 1695 __u8 *buffer; 1696 struct usb_device *dev = tdev->td_serial->dev; 1697 unsigned int pipe = usb_sndbulkpipe(dev, 1698 tdev->td_serial->port[0]->bulk_out_endpointAddress); 1699 const struct firmware *fw_p; 1700 char buf[32]; 1701 1702 dbg("%s\n", __func__); 1703 /* try ID specific firmware first, then try generic firmware */ 1704 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor, 1705 dev->descriptor.idProduct); 1706 if ((status = request_firmware(&fw_p, buf, &dev->dev)) != 0) { 1707 buf[0] = '\0'; 1708 if (dev->descriptor.idVendor == MTS_VENDOR_ID) { 1709 switch (dev->descriptor.idProduct) { 1710 case MTS_CDMA_PRODUCT_ID: 1711 strcpy(buf, "mts_cdma.fw"); 1712 break; 1713 case MTS_GSM_PRODUCT_ID: 1714 strcpy(buf, "mts_gsm.fw"); 1715 break; 1716 case MTS_EDGE_PRODUCT_ID: 1717 strcpy(buf, "mts_edge.fw"); 1718 break; 1719 case MTS_MT9234MU_PRODUCT_ID: 1720 strcpy(buf, "mts_mt9234mu.fw"); 1721 break; 1722 case MTS_MT9234ZBA_PRODUCT_ID: 1723 strcpy(buf, "mts_mt9234zba.fw"); 1724 break; 1725 case MTS_MT9234ZBAOLD_PRODUCT_ID: 1726 strcpy(buf, "mts_mt9234zba.fw"); 1727 break; } 1728 } 1729 if (buf[0] == '\0') { 1730 if (tdev->td_is_3410) 1731 strcpy(buf, "ti_3410.fw"); 1732 else 1733 strcpy(buf, "ti_5052.fw"); 1734 } 1735 status = request_firmware(&fw_p, buf, &dev->dev); 1736 } 1737 if (status) { 1738 dev_err(&dev->dev, "%s - firmware not found\n", __func__); 1739 return -ENOENT; 1740 } 1741 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) { 1742 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size); 1743 release_firmware(fw_p); 1744 return -ENOENT; 1745 } 1746 1747 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header); 1748 buffer = kmalloc(buffer_size, GFP_KERNEL); 1749 if (buffer) { 1750 memcpy(buffer, fw_p->data, fw_p->size); 1751 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size); 1752 status = ti_do_download(dev, pipe, buffer, fw_p->size); 1753 kfree(buffer); 1754 } else { 1755 dbg("%s ENOMEM\n", __func__); 1756 status = -ENOMEM; 1757 } 1758 release_firmware(fw_p); 1759 if (status) { 1760 dev_err(&dev->dev, "%s - error downloading firmware, %d\n", 1761 __func__, status); 1762 return status; 1763 } 1764 1765 dbg("%s - download successful", __func__); 1766 1767 return 0; 1768 } 1769