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