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