1 /* 2 * KLSI KL5KUSB105 chip RS232 converter driver 3 * 4 * Copyright (C) 2001 Utz-Uwe Haus <haus@uuhaus.de> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * All information about the device was acquired using SniffUSB ans snoopUSB 12 * on Windows98. 13 * It was written out of frustration with the PalmConnect USB Serial adapter 14 * sold by Palm Inc. 15 * Neither Palm, nor their contractor (MCCI) or their supplier (KLSI) provided 16 * information that was not already available. 17 * 18 * It seems that KLSI bought some silicon-design information from ScanLogic, 19 * whose SL11R processor is at the core of the KL5KUSB chipset from KLSI. 20 * KLSI has firmware available for their devices; it is probable that the 21 * firmware differs from that used by KLSI in their products. If you have an 22 * original KLSI device and can provide some information on it, I would be 23 * most interested in adding support for it here. If you have any information 24 * on the protocol used (or find errors in my reverse-engineered stuff), please 25 * let me know. 26 * 27 * The code was only tested with a PalmConnect USB adapter; if you 28 * are adventurous, try it with any KLSI-based device and let me know how it 29 * breaks so that I can fix it! 30 */ 31 32 /* TODO: 33 * check modem line signals 34 * implement handshaking or decide that we do not support it 35 */ 36 37 /* History: 38 * 0.3a - implemented pools of write URBs 39 * 0.3 - alpha version for public testing 40 * 0.2 - TIOCMGET works, so autopilot(1) can be used! 41 * 0.1 - can be used to to pilot-xfer -p /dev/ttyUSB0 -l 42 * 43 * The driver skeleton is mainly based on mct_u232.c and various other 44 * pieces of code shamelessly copied from the drivers/usb/serial/ directory. 45 */ 46 47 48 #include <linux/kernel.h> 49 #include <linux/errno.h> 50 #include <linux/init.h> 51 #include <linux/slab.h> 52 #include <linux/tty.h> 53 #include <linux/tty_driver.h> 54 #include <linux/tty_flip.h> 55 #include <linux/module.h> 56 #include <asm/uaccess.h> 57 #include <linux/usb.h> 58 #include <linux/usb/serial.h> 59 #include "kl5kusb105.h" 60 61 static int debug; 62 63 /* 64 * Version Information 65 */ 66 #define DRIVER_VERSION "v0.3a" 67 #define DRIVER_AUTHOR "Utz-Uwe Haus <haus@uuhaus.de>" 68 #define DRIVER_DESC "KLSI KL5KUSB105 chipset USB->Serial Converter driver" 69 70 71 /* 72 * Function prototypes 73 */ 74 static int klsi_105_startup (struct usb_serial *serial); 75 static void klsi_105_shutdown (struct usb_serial *serial); 76 static int klsi_105_open (struct usb_serial_port *port, 77 struct file *filp); 78 static void klsi_105_close (struct usb_serial_port *port, 79 struct file *filp); 80 static int klsi_105_write (struct usb_serial_port *port, 81 const unsigned char *buf, 82 int count); 83 static void klsi_105_write_bulk_callback (struct urb *urb); 84 static int klsi_105_chars_in_buffer (struct usb_serial_port *port); 85 static int klsi_105_write_room (struct usb_serial_port *port); 86 87 static void klsi_105_read_bulk_callback (struct urb *urb); 88 static void klsi_105_set_termios (struct usb_serial_port *port, 89 struct ktermios *old); 90 static void klsi_105_throttle (struct usb_serial_port *port); 91 static void klsi_105_unthrottle (struct usb_serial_port *port); 92 /* 93 static void klsi_105_break_ctl (struct usb_serial_port *port, 94 int break_state ); 95 */ 96 static int klsi_105_tiocmget (struct usb_serial_port *port, 97 struct file *file); 98 static int klsi_105_tiocmset (struct usb_serial_port *port, 99 struct file *file, unsigned int set, 100 unsigned int clear); 101 102 /* 103 * All of the device info needed for the KLSI converters. 104 */ 105 static struct usb_device_id id_table [] = { 106 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) }, 107 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) }, 108 { } /* Terminating entry */ 109 }; 110 111 MODULE_DEVICE_TABLE (usb, id_table); 112 113 static struct usb_driver kl5kusb105d_driver = { 114 .name = "kl5kusb105d", 115 .probe = usb_serial_probe, 116 .disconnect = usb_serial_disconnect, 117 .id_table = id_table, 118 .no_dynamic_id = 1, 119 }; 120 121 static struct usb_serial_driver kl5kusb105d_device = { 122 .driver = { 123 .owner = THIS_MODULE, 124 .name = "kl5kusb105d", 125 }, 126 .description = "KL5KUSB105D / PalmConnect", 127 .usb_driver = &kl5kusb105d_driver, 128 .id_table = id_table, 129 .num_interrupt_in = 1, 130 .num_bulk_in = 1, 131 .num_bulk_out = 1, 132 .num_ports = 1, 133 .open = klsi_105_open, 134 .close = klsi_105_close, 135 .write = klsi_105_write, 136 .write_bulk_callback = klsi_105_write_bulk_callback, 137 .chars_in_buffer = klsi_105_chars_in_buffer, 138 .write_room = klsi_105_write_room, 139 .read_bulk_callback =klsi_105_read_bulk_callback, 140 .set_termios = klsi_105_set_termios, 141 /*.break_ctl = klsi_105_break_ctl,*/ 142 .tiocmget = klsi_105_tiocmget, 143 .tiocmset = klsi_105_tiocmset, 144 .attach = klsi_105_startup, 145 .shutdown = klsi_105_shutdown, 146 .throttle = klsi_105_throttle, 147 .unthrottle = klsi_105_unthrottle, 148 }; 149 150 struct klsi_105_port_settings { 151 __u8 pktlen; /* always 5, it seems */ 152 __u8 baudrate; 153 __u8 databits; 154 __u8 unknown1; 155 __u8 unknown2; 156 } __attribute__ ((packed)); 157 158 /* we implement a pool of NUM_URBS urbs per usb_serial */ 159 #define NUM_URBS 1 160 #define URB_TRANSFER_BUFFER_SIZE 64 161 struct klsi_105_private { 162 struct klsi_105_port_settings cfg; 163 struct ktermios termios; 164 unsigned long line_state; /* modem line settings */ 165 /* write pool */ 166 struct urb * write_urb_pool[NUM_URBS]; 167 spinlock_t lock; 168 unsigned long bytes_in; 169 unsigned long bytes_out; 170 }; 171 172 173 /* 174 * Handle vendor specific USB requests 175 */ 176 177 178 #define KLSI_TIMEOUT 5000 /* default urb timeout */ 179 180 static int klsi_105_chg_port_settings(struct usb_serial_port *port, 181 struct klsi_105_port_settings *settings) 182 { 183 int rc; 184 185 rc = usb_control_msg(port->serial->dev, 186 usb_sndctrlpipe(port->serial->dev, 0), 187 KL5KUSB105A_SIO_SET_DATA, 188 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE, 189 0, /* value */ 190 0, /* index */ 191 settings, 192 sizeof(struct klsi_105_port_settings), 193 KLSI_TIMEOUT); 194 if (rc < 0) 195 err("Change port settings failed (error = %d)", rc); 196 info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d", 197 __FUNCTION__, 198 settings->pktlen, 199 settings->baudrate, settings->databits, 200 settings->unknown1, settings->unknown2); 201 return rc; 202 } /* klsi_105_chg_port_settings */ 203 204 /* translate a 16-bit status value from the device to linux's TIO bits */ 205 static unsigned long klsi_105_status2linestate(const __u16 status) 206 { 207 unsigned long res = 0; 208 209 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0) 210 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0) 211 ; 212 213 return res; 214 } 215 /* 216 * Read line control via vendor command and return result through 217 * *line_state_p 218 */ 219 /* It seems that the status buffer has always only 2 bytes length */ 220 #define KLSI_STATUSBUF_LEN 2 221 static int klsi_105_get_line_state(struct usb_serial_port *port, 222 unsigned long *line_state_p) 223 { 224 int rc; 225 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1}; 226 __u16 status; 227 228 info("%s - sending SIO Poll request", __FUNCTION__); 229 rc = usb_control_msg(port->serial->dev, 230 usb_rcvctrlpipe(port->serial->dev, 0), 231 KL5KUSB105A_SIO_POLL, 232 USB_TYPE_VENDOR | USB_DIR_IN, 233 0, /* value */ 234 0, /* index */ 235 status_buf, KLSI_STATUSBUF_LEN, 236 10000 237 ); 238 if (rc < 0) 239 err("Reading line status failed (error = %d)", rc); 240 else { 241 status = le16_to_cpu(*(u16 *)status_buf); 242 243 info("%s - read status %x %x", __FUNCTION__, 244 status_buf[0], status_buf[1]); 245 246 *line_state_p = klsi_105_status2linestate(status); 247 } 248 249 return rc; 250 } 251 252 253 /* 254 * Driver's tty interface functions 255 */ 256 257 static int klsi_105_startup (struct usb_serial *serial) 258 { 259 struct klsi_105_private *priv; 260 int i, j; 261 262 /* check if we support the product id (see keyspan.c) 263 * FIXME 264 */ 265 266 /* allocate the private data structure */ 267 for (i=0; i<serial->num_ports; i++) { 268 priv = kmalloc(sizeof(struct klsi_105_private), 269 GFP_KERNEL); 270 if (!priv) { 271 dbg("%skmalloc for klsi_105_private failed.", __FUNCTION__); 272 i--; 273 goto err_cleanup; 274 } 275 /* set initial values for control structures */ 276 priv->cfg.pktlen = 5; 277 priv->cfg.baudrate = kl5kusb105a_sio_b9600; 278 priv->cfg.databits = kl5kusb105a_dtb_8; 279 priv->cfg.unknown1 = 0; 280 priv->cfg.unknown2 = 1; 281 282 priv->line_state = 0; 283 284 priv->bytes_in = 0; 285 priv->bytes_out = 0; 286 usb_set_serial_port_data(serial->port[i], priv); 287 288 spin_lock_init (&priv->lock); 289 for (j=0; j<NUM_URBS; j++) { 290 struct urb* urb = usb_alloc_urb(0, GFP_KERNEL); 291 292 priv->write_urb_pool[j] = urb; 293 if (urb == NULL) { 294 err("No more urbs???"); 295 goto err_cleanup; 296 } 297 298 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, 299 GFP_KERNEL); 300 if (!urb->transfer_buffer) { 301 err("%s - out of memory for urb buffers.", __FUNCTION__); 302 goto err_cleanup; 303 } 304 } 305 306 /* priv->termios is left uninitalized until port opening */ 307 init_waitqueue_head(&serial->port[i]->write_wait); 308 } 309 310 return 0; 311 312 err_cleanup: 313 for (; i >= 0; i--) { 314 priv = usb_get_serial_port_data(serial->port[i]); 315 for (j=0; j < NUM_URBS; j++) { 316 if (priv->write_urb_pool[j]) { 317 kfree(priv->write_urb_pool[j]->transfer_buffer); 318 usb_free_urb(priv->write_urb_pool[j]); 319 } 320 } 321 usb_set_serial_port_data(serial->port[i], NULL); 322 } 323 return -ENOMEM; 324 } /* klsi_105_startup */ 325 326 327 static void klsi_105_shutdown (struct usb_serial *serial) 328 { 329 int i; 330 331 dbg("%s", __FUNCTION__); 332 333 /* stop reads and writes on all ports */ 334 for (i=0; i < serial->num_ports; ++i) { 335 struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]); 336 unsigned long flags; 337 338 if (priv) { 339 /* kill our write urb pool */ 340 int j; 341 struct urb **write_urbs = priv->write_urb_pool; 342 spin_lock_irqsave(&priv->lock,flags); 343 344 for (j = 0; j < NUM_URBS; j++) { 345 if (write_urbs[j]) { 346 /* FIXME - uncomment the following 347 * usb_kill_urb call when the host 348 * controllers get fixed to set 349 * urb->dev = NULL after the urb is 350 * finished. Otherwise this call 351 * oopses. */ 352 /* usb_kill_urb(write_urbs[j]); */ 353 kfree(write_urbs[j]->transfer_buffer); 354 usb_free_urb (write_urbs[j]); 355 } 356 } 357 358 spin_unlock_irqrestore (&priv->lock, flags); 359 360 kfree(priv); 361 usb_set_serial_port_data(serial->port[i], NULL); 362 } 363 } 364 } /* klsi_105_shutdown */ 365 366 static int klsi_105_open (struct usb_serial_port *port, struct file *filp) 367 { 368 struct klsi_105_private *priv = usb_get_serial_port_data(port); 369 int retval = 0; 370 int rc; 371 int i; 372 unsigned long line_state; 373 struct klsi_105_port_settings cfg; 374 unsigned long flags; 375 376 dbg("%s port %d", __FUNCTION__, port->number); 377 378 /* force low_latency on so that our tty_push actually forces 379 * the data through 380 * port->tty->low_latency = 1; */ 381 382 /* Do a defined restart: 383 * Set up sane default baud rate and send the 'READ_ON' 384 * vendor command. 385 * FIXME: set modem line control (how?) 386 * Then read the modem line control and store values in 387 * priv->line_state. 388 */ 389 cfg.pktlen = 5; 390 cfg.baudrate = kl5kusb105a_sio_b9600; 391 cfg.databits = kl5kusb105a_dtb_8; 392 cfg.unknown1 = 0; 393 cfg.unknown2 = 1; 394 klsi_105_chg_port_settings(port, &cfg); 395 396 /* set up termios structure */ 397 spin_lock_irqsave (&priv->lock, flags); 398 priv->termios.c_iflag = port->tty->termios->c_iflag; 399 priv->termios.c_oflag = port->tty->termios->c_oflag; 400 priv->termios.c_cflag = port->tty->termios->c_cflag; 401 priv->termios.c_lflag = port->tty->termios->c_lflag; 402 for (i=0; i<NCCS; i++) 403 priv->termios.c_cc[i] = port->tty->termios->c_cc[i]; 404 priv->cfg.pktlen = cfg.pktlen; 405 priv->cfg.baudrate = cfg.baudrate; 406 priv->cfg.databits = cfg.databits; 407 priv->cfg.unknown1 = cfg.unknown1; 408 priv->cfg.unknown2 = cfg.unknown2; 409 spin_unlock_irqrestore (&priv->lock, flags); 410 411 /* READ_ON and urb submission */ 412 usb_fill_bulk_urb(port->read_urb, port->serial->dev, 413 usb_rcvbulkpipe(port->serial->dev, 414 port->bulk_in_endpointAddress), 415 port->read_urb->transfer_buffer, 416 port->read_urb->transfer_buffer_length, 417 klsi_105_read_bulk_callback, 418 port); 419 420 rc = usb_submit_urb(port->read_urb, GFP_KERNEL); 421 if (rc) { 422 err("%s - failed submitting read urb, error %d", __FUNCTION__, rc); 423 retval = rc; 424 goto exit; 425 } 426 427 rc = usb_control_msg(port->serial->dev, 428 usb_sndctrlpipe(port->serial->dev,0), 429 KL5KUSB105A_SIO_CONFIGURE, 430 USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE, 431 KL5KUSB105A_SIO_CONFIGURE_READ_ON, 432 0, /* index */ 433 NULL, 434 0, 435 KLSI_TIMEOUT); 436 if (rc < 0) { 437 err("Enabling read failed (error = %d)", rc); 438 retval = rc; 439 } else 440 dbg("%s - enabled reading", __FUNCTION__); 441 442 rc = klsi_105_get_line_state(port, &line_state); 443 if (rc >= 0) { 444 spin_lock_irqsave (&priv->lock, flags); 445 priv->line_state = line_state; 446 spin_unlock_irqrestore (&priv->lock, flags); 447 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state); 448 retval = 0; 449 } else 450 retval = rc; 451 452 exit: 453 return retval; 454 } /* klsi_105_open */ 455 456 457 static void klsi_105_close (struct usb_serial_port *port, struct file *filp) 458 { 459 struct klsi_105_private *priv = usb_get_serial_port_data(port); 460 int rc; 461 462 dbg("%s port %d", __FUNCTION__, port->number); 463 464 /* send READ_OFF */ 465 rc = usb_control_msg (port->serial->dev, 466 usb_sndctrlpipe(port->serial->dev, 0), 467 KL5KUSB105A_SIO_CONFIGURE, 468 USB_TYPE_VENDOR | USB_DIR_OUT, 469 KL5KUSB105A_SIO_CONFIGURE_READ_OFF, 470 0, /* index */ 471 NULL, 0, 472 KLSI_TIMEOUT); 473 if (rc < 0) 474 err("Disabling read failed (error = %d)", rc); 475 476 /* shutdown our bulk reads and writes */ 477 usb_kill_urb(port->write_urb); 478 usb_kill_urb(port->read_urb); 479 /* unlink our write pool */ 480 /* FIXME */ 481 /* wgg - do I need this? I think so. */ 482 usb_kill_urb(port->interrupt_in_urb); 483 info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out); 484 } /* klsi_105_close */ 485 486 487 /* We need to write a complete 64-byte data block and encode the 488 * number actually sent in the first double-byte, LSB-order. That 489 * leaves at most 62 bytes of payload. 490 */ 491 #define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */ 492 493 494 static int klsi_105_write (struct usb_serial_port *port, 495 const unsigned char *buf, int count) 496 { 497 struct klsi_105_private *priv = usb_get_serial_port_data(port); 498 int result, size; 499 int bytes_sent=0; 500 501 dbg("%s - port %d", __FUNCTION__, port->number); 502 503 while (count > 0) { 504 /* try to find a free urb (write 0 bytes if none) */ 505 struct urb *urb = NULL; 506 unsigned long flags; 507 int i; 508 /* since the pool is per-port we might not need the spin lock !? */ 509 spin_lock_irqsave (&priv->lock, flags); 510 for (i=0; i<NUM_URBS; i++) { 511 if (priv->write_urb_pool[i]->status != -EINPROGRESS) { 512 urb = priv->write_urb_pool[i]; 513 dbg("%s - using pool URB %d", __FUNCTION__, i); 514 break; 515 } 516 } 517 spin_unlock_irqrestore (&priv->lock, flags); 518 519 if (urb==NULL) { 520 dbg("%s - no more free urbs", __FUNCTION__); 521 goto exit; 522 } 523 524 if (urb->transfer_buffer == NULL) { 525 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC); 526 if (urb->transfer_buffer == NULL) { 527 err("%s - no more kernel memory...", __FUNCTION__); 528 goto exit; 529 } 530 } 531 532 size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET); 533 size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET); 534 535 memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size); 536 537 /* write payload size into transfer buffer */ 538 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF); 539 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8); 540 541 /* set up our urb */ 542 usb_fill_bulk_urb(urb, port->serial->dev, 543 usb_sndbulkpipe(port->serial->dev, 544 port->bulk_out_endpointAddress), 545 urb->transfer_buffer, 546 URB_TRANSFER_BUFFER_SIZE, 547 klsi_105_write_bulk_callback, 548 port); 549 550 /* send the data out the bulk port */ 551 result = usb_submit_urb(urb, GFP_ATOMIC); 552 if (result) { 553 err("%s - failed submitting write urb, error %d", __FUNCTION__, result); 554 goto exit; 555 } 556 buf += size; 557 bytes_sent += size; 558 count -= size; 559 } 560 exit: 561 /* lockless, but it's for debug info only... */ 562 priv->bytes_out+=bytes_sent; 563 564 return bytes_sent; /* that's how much we wrote */ 565 } /* klsi_105_write */ 566 567 static void klsi_105_write_bulk_callback ( struct urb *urb) 568 { 569 struct usb_serial_port *port = (struct usb_serial_port *)urb->context; 570 int status = urb->status; 571 572 dbg("%s - port %d", __FUNCTION__, port->number); 573 574 if (status) { 575 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, 576 status); 577 return; 578 } 579 580 usb_serial_port_softint(port); 581 } /* klsi_105_write_bulk_completion_callback */ 582 583 584 /* return number of characters currently in the writing process */ 585 static int klsi_105_chars_in_buffer (struct usb_serial_port *port) 586 { 587 int chars = 0; 588 int i; 589 unsigned long flags; 590 struct klsi_105_private *priv = usb_get_serial_port_data(port); 591 592 spin_lock_irqsave (&priv->lock, flags); 593 594 for (i = 0; i < NUM_URBS; ++i) { 595 if (priv->write_urb_pool[i]->status == -EINPROGRESS) { 596 chars += URB_TRANSFER_BUFFER_SIZE; 597 } 598 } 599 600 spin_unlock_irqrestore (&priv->lock, flags); 601 602 dbg("%s - returns %d", __FUNCTION__, chars); 603 return (chars); 604 } 605 606 static int klsi_105_write_room (struct usb_serial_port *port) 607 { 608 unsigned long flags; 609 int i; 610 int room = 0; 611 struct klsi_105_private *priv = usb_get_serial_port_data(port); 612 613 spin_lock_irqsave (&priv->lock, flags); 614 for (i = 0; i < NUM_URBS; ++i) { 615 if (priv->write_urb_pool[i]->status != -EINPROGRESS) { 616 room += URB_TRANSFER_BUFFER_SIZE; 617 } 618 } 619 620 spin_unlock_irqrestore (&priv->lock, flags); 621 622 dbg("%s - returns %d", __FUNCTION__, room); 623 return (room); 624 } 625 626 627 628 static void klsi_105_read_bulk_callback (struct urb *urb) 629 { 630 struct usb_serial_port *port = (struct usb_serial_port *)urb->context; 631 struct klsi_105_private *priv = usb_get_serial_port_data(port); 632 struct tty_struct *tty; 633 unsigned char *data = urb->transfer_buffer; 634 int rc; 635 int status = urb->status; 636 637 dbg("%s - port %d", __FUNCTION__, port->number); 638 639 /* The urb might have been killed. */ 640 if (status) { 641 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, 642 status); 643 return; 644 } 645 646 /* The data received is again preceded by a length double-byte in LSB- 647 * first order (see klsi_105_write() ) 648 */ 649 if (urb->actual_length == 0) { 650 /* empty urbs seem to happen, we ignore them */ 651 /* dbg("%s - emtpy URB", __FUNCTION__); */ 652 ; 653 } else if (urb->actual_length <= 2) { 654 dbg("%s - size %d URB not understood", __FUNCTION__, 655 urb->actual_length); 656 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, 657 urb->actual_length, data); 658 } else { 659 int bytes_sent = ((__u8 *) data)[0] + 660 ((unsigned int) ((__u8 *) data)[1] << 8); 661 tty = port->tty; 662 /* we should immediately resubmit the URB, before attempting 663 * to pass the data on to the tty layer. But that needs locking 664 * against re-entry an then mixed-up data because of 665 * intermixed tty_flip_buffer_push()s 666 * FIXME 667 */ 668 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, 669 urb->actual_length, data); 670 671 if (bytes_sent + 2 > urb->actual_length) { 672 dbg("%s - trying to read more data than available" 673 " (%d vs. %d)", __FUNCTION__, 674 bytes_sent+2, urb->actual_length); 675 /* cap at implied limit */ 676 bytes_sent = urb->actual_length - 2; 677 } 678 679 tty_buffer_request_room(tty, bytes_sent); 680 tty_insert_flip_string(tty, data + 2, bytes_sent); 681 tty_flip_buffer_push(tty); 682 683 /* again lockless, but debug info only */ 684 priv->bytes_in += bytes_sent; 685 } 686 /* Continue trying to always read */ 687 usb_fill_bulk_urb(port->read_urb, port->serial->dev, 688 usb_rcvbulkpipe(port->serial->dev, 689 port->bulk_in_endpointAddress), 690 port->read_urb->transfer_buffer, 691 port->read_urb->transfer_buffer_length, 692 klsi_105_read_bulk_callback, 693 port); 694 rc = usb_submit_urb(port->read_urb, GFP_ATOMIC); 695 if (rc) 696 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, rc); 697 } /* klsi_105_read_bulk_callback */ 698 699 700 static void klsi_105_set_termios (struct usb_serial_port *port, 701 struct ktermios *old_termios) 702 { 703 struct klsi_105_private *priv = usb_get_serial_port_data(port); 704 unsigned int iflag = port->tty->termios->c_iflag; 705 unsigned int old_iflag = old_termios->c_iflag; 706 unsigned int cflag = port->tty->termios->c_cflag; 707 unsigned int old_cflag = old_termios->c_cflag; 708 struct klsi_105_port_settings cfg; 709 unsigned long flags; 710 711 /* lock while we are modifying the settings */ 712 spin_lock_irqsave (&priv->lock, flags); 713 714 /* 715 * Update baud rate 716 */ 717 if( (cflag & CBAUD) != (old_cflag & CBAUD) ) { 718 /* reassert DTR and (maybe) RTS on transition from B0 */ 719 if( (old_cflag & CBAUD) == B0 ) { 720 dbg("%s: baud was B0", __FUNCTION__); 721 #if 0 722 priv->control_state |= TIOCM_DTR; 723 /* don't set RTS if using hardware flow control */ 724 if (!(old_cflag & CRTSCTS)) { 725 priv->control_state |= TIOCM_RTS; 726 } 727 mct_u232_set_modem_ctrl(serial, priv->control_state); 728 #endif 729 } 730 731 switch(cflag & CBAUD) { 732 case B0: /* handled below */ 733 break; 734 case B1200: priv->cfg.baudrate = kl5kusb105a_sio_b1200; 735 break; 736 case B2400: priv->cfg.baudrate = kl5kusb105a_sio_b2400; 737 break; 738 case B4800: priv->cfg.baudrate = kl5kusb105a_sio_b4800; 739 break; 740 case B9600: priv->cfg.baudrate = kl5kusb105a_sio_b9600; 741 break; 742 case B19200: priv->cfg.baudrate = kl5kusb105a_sio_b19200; 743 break; 744 case B38400: priv->cfg.baudrate = kl5kusb105a_sio_b38400; 745 break; 746 case B57600: priv->cfg.baudrate = kl5kusb105a_sio_b57600; 747 break; 748 case B115200: priv->cfg.baudrate = kl5kusb105a_sio_b115200; 749 break; 750 default: 751 err("KLSI USB->Serial converter:" 752 " unsupported baudrate request, using default" 753 " of 9600"); 754 priv->cfg.baudrate = kl5kusb105a_sio_b9600; 755 break; 756 } 757 if ((cflag & CBAUD) == B0 ) { 758 dbg("%s: baud is B0", __FUNCTION__); 759 /* Drop RTS and DTR */ 760 /* maybe this should be simulated by sending read 761 * disable and read enable messages? 762 */ 763 ; 764 #if 0 765 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS); 766 mct_u232_set_modem_ctrl(serial, priv->control_state); 767 #endif 768 } 769 } 770 771 if ((cflag & CSIZE) != (old_cflag & CSIZE)) { 772 /* set the number of data bits */ 773 switch (cflag & CSIZE) { 774 case CS5: 775 dbg("%s - 5 bits/byte not supported", __FUNCTION__); 776 spin_unlock_irqrestore (&priv->lock, flags); 777 return ; 778 case CS6: 779 dbg("%s - 6 bits/byte not supported", __FUNCTION__); 780 spin_unlock_irqrestore (&priv->lock, flags); 781 return ; 782 case CS7: 783 priv->cfg.databits = kl5kusb105a_dtb_7; 784 break; 785 case CS8: 786 priv->cfg.databits = kl5kusb105a_dtb_8; 787 break; 788 default: 789 err("CSIZE was not CS5-CS8, using default of 8"); 790 priv->cfg.databits = kl5kusb105a_dtb_8; 791 break; 792 } 793 } 794 795 /* 796 * Update line control register (LCR) 797 */ 798 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD)) 799 || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) { 800 801 #if 0 802 priv->last_lcr = 0; 803 804 /* set the parity */ 805 if (cflag & PARENB) 806 priv->last_lcr |= (cflag & PARODD) ? 807 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN; 808 else 809 priv->last_lcr |= MCT_U232_PARITY_NONE; 810 811 /* set the number of stop bits */ 812 priv->last_lcr |= (cflag & CSTOPB) ? 813 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1; 814 815 mct_u232_set_line_ctrl(serial, priv->last_lcr); 816 #endif 817 ; 818 } 819 820 /* 821 * Set flow control: well, I do not really now how to handle DTR/RTS. 822 * Just do what we have seen with SniffUSB on Win98. 823 */ 824 if( (iflag & IXOFF) != (old_iflag & IXOFF) 825 || (iflag & IXON) != (old_iflag & IXON) 826 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) { 827 828 /* Drop DTR/RTS if no flow control otherwise assert */ 829 #if 0 830 if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) ) 831 priv->control_state |= TIOCM_DTR | TIOCM_RTS; 832 else 833 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS); 834 mct_u232_set_modem_ctrl(serial, priv->control_state); 835 #endif 836 ; 837 } 838 memcpy (&cfg, &priv->cfg, sizeof(cfg)); 839 spin_unlock_irqrestore (&priv->lock, flags); 840 841 /* now commit changes to device */ 842 klsi_105_chg_port_settings(port, &cfg); 843 } /* klsi_105_set_termios */ 844 845 846 #if 0 847 static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state ) 848 { 849 struct usb_serial *serial = port->serial; 850 struct mct_u232_private *priv = (struct mct_u232_private *)port->private; 851 unsigned char lcr = priv->last_lcr; 852 853 dbg("%sstate=%d", __FUNCTION__, break_state); 854 855 if (break_state) 856 lcr |= MCT_U232_SET_BREAK; 857 858 mct_u232_set_line_ctrl(serial, lcr); 859 } /* mct_u232_break_ctl */ 860 #endif 861 862 static int klsi_105_tiocmget (struct usb_serial_port *port, struct file *file) 863 { 864 struct klsi_105_private *priv = usb_get_serial_port_data(port); 865 unsigned long flags; 866 int rc; 867 unsigned long line_state; 868 dbg("%s - request, just guessing", __FUNCTION__); 869 870 rc = klsi_105_get_line_state(port, &line_state); 871 if (rc < 0) { 872 err("Reading line control failed (error = %d)", rc); 873 /* better return value? EAGAIN? */ 874 return rc; 875 } 876 877 spin_lock_irqsave (&priv->lock, flags); 878 priv->line_state = line_state; 879 spin_unlock_irqrestore (&priv->lock, flags); 880 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state); 881 return (int)line_state; 882 } 883 884 static int klsi_105_tiocmset (struct usb_serial_port *port, struct file *file, 885 unsigned int set, unsigned int clear) 886 { 887 int retval = -EINVAL; 888 889 dbg("%s", __FUNCTION__); 890 891 /* if this ever gets implemented, it should be done something like this: 892 struct usb_serial *serial = port->serial; 893 struct klsi_105_private *priv = usb_get_serial_port_data(port); 894 unsigned long flags; 895 int control; 896 897 spin_lock_irqsave (&priv->lock, flags); 898 if (set & TIOCM_RTS) 899 priv->control_state |= TIOCM_RTS; 900 if (set & TIOCM_DTR) 901 priv->control_state |= TIOCM_DTR; 902 if (clear & TIOCM_RTS) 903 priv->control_state &= ~TIOCM_RTS; 904 if (clear & TIOCM_DTR) 905 priv->control_state &= ~TIOCM_DTR; 906 control = priv->control_state; 907 spin_unlock_irqrestore (&priv->lock, flags); 908 retval = mct_u232_set_modem_ctrl(serial, control); 909 */ 910 return retval; 911 } 912 913 static void klsi_105_throttle (struct usb_serial_port *port) 914 { 915 dbg("%s - port %d", __FUNCTION__, port->number); 916 usb_kill_urb(port->read_urb); 917 } 918 919 static void klsi_105_unthrottle (struct usb_serial_port *port) 920 { 921 int result; 922 923 dbg("%s - port %d", __FUNCTION__, port->number); 924 925 port->read_urb->dev = port->serial->dev; 926 result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 927 if (result) 928 err("%s - failed submitting read urb, error %d", __FUNCTION__, 929 result); 930 } 931 932 933 934 static int __init klsi_105_init (void) 935 { 936 int retval; 937 retval = usb_serial_register(&kl5kusb105d_device); 938 if (retval) 939 goto failed_usb_serial_register; 940 retval = usb_register(&kl5kusb105d_driver); 941 if (retval) 942 goto failed_usb_register; 943 944 info(DRIVER_DESC " " DRIVER_VERSION); 945 return 0; 946 failed_usb_register: 947 usb_serial_deregister(&kl5kusb105d_device); 948 failed_usb_serial_register: 949 return retval; 950 } 951 952 953 static void __exit klsi_105_exit (void) 954 { 955 usb_deregister (&kl5kusb105d_driver); 956 usb_serial_deregister (&kl5kusb105d_device); 957 } 958 959 960 module_init (klsi_105_init); 961 module_exit (klsi_105_exit); 962 963 MODULE_AUTHOR( DRIVER_AUTHOR ); 964 MODULE_DESCRIPTION( DRIVER_DESC ); 965 MODULE_LICENSE("GPL"); 966 967 968 module_param(debug, bool, S_IRUGO | S_IWUSR); 969 MODULE_PARM_DESC(debug, "enable extensive debugging messages"); 970 971 /* vim: set sts=8 ts=8 sw=8: */ 972