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