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/config.h> 49 #include <linux/kernel.h> 50 #include <linux/errno.h> 51 #include <linux/init.h> 52 #include <linux/slab.h> 53 #include <linux/tty.h> 54 #include <linux/tty_driver.h> 55 #include <linux/tty_flip.h> 56 #include <linux/module.h> 57 #include <asm/uaccess.h> 58 #include <linux/usb.h> 59 #include "usb-serial.h" 60 #include "kl5kusb105.h" 61 62 static int debug; 63 64 /* 65 * Version Information 66 */ 67 #define DRIVER_VERSION "v0.3a" 68 #define DRIVER_AUTHOR "Utz-Uwe Haus <haus@uuhaus.de>" 69 #define DRIVER_DESC "KLSI KL5KUSB105 chipset USB->Serial Converter driver" 70 71 72 /* 73 * Function prototypes 74 */ 75 static int klsi_105_startup (struct usb_serial *serial); 76 static void klsi_105_shutdown (struct usb_serial *serial); 77 static int klsi_105_open (struct usb_serial_port *port, 78 struct file *filp); 79 static void klsi_105_close (struct usb_serial_port *port, 80 struct file *filp); 81 static int klsi_105_write (struct usb_serial_port *port, 82 const unsigned char *buf, 83 int count); 84 static void klsi_105_write_bulk_callback (struct urb *urb, struct pt_regs *regs); 85 static int klsi_105_chars_in_buffer (struct usb_serial_port *port); 86 static int klsi_105_write_room (struct usb_serial_port *port); 87 88 static void klsi_105_read_bulk_callback (struct urb *urb, struct pt_regs *regs); 89 static void klsi_105_set_termios (struct usb_serial_port *port, 90 struct termios * old); 91 static int klsi_105_ioctl (struct usb_serial_port *port, 92 struct file * file, 93 unsigned int cmd, 94 unsigned long arg); 95 static void klsi_105_throttle (struct usb_serial_port *port); 96 static void klsi_105_unthrottle (struct usb_serial_port *port); 97 /* 98 static void klsi_105_break_ctl (struct usb_serial_port *port, 99 int break_state ); 100 */ 101 static int klsi_105_tiocmget (struct usb_serial_port *port, 102 struct file *file); 103 static int klsi_105_tiocmset (struct usb_serial_port *port, 104 struct file *file, unsigned int set, 105 unsigned int clear); 106 107 /* 108 * All of the device info needed for the KLSI converters. 109 */ 110 static struct usb_device_id id_table [] = { 111 { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) }, 112 { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) }, 113 { } /* Terminating entry */ 114 }; 115 116 MODULE_DEVICE_TABLE (usb, id_table); 117 118 static struct usb_driver kl5kusb105d_driver = { 119 .owner = THIS_MODULE, 120 .name = "kl5kusb105d", 121 .probe = usb_serial_probe, 122 .disconnect = usb_serial_disconnect, 123 .id_table = id_table, 124 }; 125 126 static struct usb_serial_device_type kl5kusb105d_device = { 127 .owner = THIS_MODULE, 128 .name = "KL5KUSB105D / PalmConnect", 129 .short_name = "kl5kusb105d", 130 .id_table = id_table, 131 .num_interrupt_in = 1, 132 .num_bulk_in = 1, 133 .num_bulk_out = 1, 134 .num_ports = 1, 135 .open = klsi_105_open, 136 .close = klsi_105_close, 137 .write = klsi_105_write, 138 .write_bulk_callback = klsi_105_write_bulk_callback, 139 .chars_in_buffer = klsi_105_chars_in_buffer, 140 .write_room = klsi_105_write_room, 141 .read_bulk_callback =klsi_105_read_bulk_callback, 142 .ioctl = klsi_105_ioctl, 143 .set_termios = klsi_105_set_termios, 144 /*.break_ctl = klsi_105_break_ctl,*/ 145 .tiocmget = klsi_105_tiocmget, 146 .tiocmset = klsi_105_tiocmset, 147 .attach = klsi_105_startup, 148 .shutdown = klsi_105_shutdown, 149 .throttle = klsi_105_throttle, 150 .unthrottle = klsi_105_unthrottle, 151 }; 152 153 struct klsi_105_port_settings { 154 __u8 pktlen; /* always 5, it seems */ 155 __u8 baudrate; 156 __u8 databits; 157 __u8 unknown1; 158 __u8 unknown2; 159 } __attribute__ ((packed)); 160 161 /* we implement a pool of NUM_URBS urbs per usb_serial */ 162 #define NUM_URBS 1 163 #define URB_TRANSFER_BUFFER_SIZE 64 164 struct klsi_105_private { 165 struct klsi_105_port_settings cfg; 166 struct termios termios; 167 unsigned long line_state; /* modem line settings */ 168 /* write pool */ 169 struct urb * write_urb_pool[NUM_URBS]; 170 spinlock_t lock; 171 unsigned long bytes_in; 172 unsigned long bytes_out; 173 }; 174 175 176 /* 177 * Handle vendor specific USB requests 178 */ 179 180 181 #define KLSI_TIMEOUT 5000 /* default urb timeout */ 182 183 static int klsi_105_chg_port_settings(struct usb_serial_port *port, 184 struct klsi_105_port_settings *settings) 185 { 186 int rc; 187 188 rc = usb_control_msg(port->serial->dev, 189 usb_sndctrlpipe(port->serial->dev, 0), 190 KL5KUSB105A_SIO_SET_DATA, 191 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE, 192 0, /* value */ 193 0, /* index */ 194 settings, 195 sizeof(struct klsi_105_port_settings), 196 KLSI_TIMEOUT); 197 if (rc < 0) 198 err("Change port settings failed (error = %d)", rc); 199 info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d", 200 __FUNCTION__, 201 settings->pktlen, 202 settings->baudrate, settings->databits, 203 settings->unknown1, settings->unknown2); 204 return rc; 205 } /* klsi_105_chg_port_settings */ 206 207 /* translate a 16-bit status value from the device to linux's TIO bits */ 208 static unsigned long klsi_105_status2linestate(const __u16 status) 209 { 210 unsigned long res = 0; 211 212 res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0) 213 | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0) 214 ; 215 216 return res; 217 } 218 /* 219 * Read line control via vendor command and return result through 220 * *line_state_p 221 */ 222 /* It seems that the status buffer has always only 2 bytes length */ 223 #define KLSI_STATUSBUF_LEN 2 224 static int klsi_105_get_line_state(struct usb_serial_port *port, 225 unsigned long *line_state_p) 226 { 227 int rc; 228 __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1}; 229 __u16 status; 230 231 info("%s - sending SIO Poll request", __FUNCTION__); 232 rc = usb_control_msg(port->serial->dev, 233 usb_rcvctrlpipe(port->serial->dev, 0), 234 KL5KUSB105A_SIO_POLL, 235 USB_TYPE_VENDOR | USB_DIR_IN, 236 0, /* value */ 237 0, /* index */ 238 status_buf, KLSI_STATUSBUF_LEN, 239 10000 240 ); 241 if (rc < 0) 242 err("Reading line status failed (error = %d)", rc); 243 else { 244 status = status_buf[0] + (status_buf[1]<<8); 245 246 info("%s - read status %x %x", __FUNCTION__, 247 status_buf[0], status_buf[1]); 248 249 *line_state_p = klsi_105_status2linestate(status); 250 } 251 252 return rc; 253 } 254 255 256 /* 257 * Driver's tty interface functions 258 */ 259 260 static int klsi_105_startup (struct usb_serial *serial) 261 { 262 struct klsi_105_private *priv; 263 int i; 264 265 /* check if we support the product id (see keyspan.c) 266 * FIXME 267 */ 268 269 /* allocate the private data structure */ 270 for (i=0; i<serial->num_ports; i++) { 271 int j; 272 priv = kmalloc(sizeof(struct klsi_105_private), 273 GFP_KERNEL); 274 if (!priv) { 275 dbg("%skmalloc for klsi_105_private failed.", __FUNCTION__); 276 return -ENOMEM; 277 } 278 /* set initial values for control structures */ 279 priv->cfg.pktlen = 5; 280 priv->cfg.baudrate = kl5kusb105a_sio_b9600; 281 priv->cfg.databits = kl5kusb105a_dtb_8; 282 priv->cfg.unknown1 = 0; 283 priv->cfg.unknown2 = 1; 284 285 priv->line_state = 0; 286 287 priv->bytes_in = 0; 288 priv->bytes_out = 0; 289 usb_set_serial_port_data(serial->port[i], priv); 290 291 spin_lock_init (&priv->lock); 292 for (j=0; j<NUM_URBS; j++) { 293 struct urb* urb = usb_alloc_urb(0, GFP_KERNEL); 294 295 priv->write_urb_pool[j] = urb; 296 if (urb == NULL) { 297 err("No more urbs???"); 298 continue; 299 } 300 301 urb->transfer_buffer = NULL; 302 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, 303 GFP_KERNEL); 304 if (!urb->transfer_buffer) { 305 err("%s - out of memory for urb buffers.", __FUNCTION__); 306 continue; 307 } 308 } 309 310 /* priv->termios is left uninitalized until port opening */ 311 init_waitqueue_head(&serial->port[i]->write_wait); 312 } 313 314 return (0); 315 } /* klsi_105_startup */ 316 317 318 static void klsi_105_shutdown (struct usb_serial *serial) 319 { 320 int i; 321 322 dbg("%s", __FUNCTION__); 323 324 /* stop reads and writes on all ports */ 325 for (i=0; i < serial->num_ports; ++i) { 326 struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]); 327 unsigned long flags; 328 329 if (priv) { 330 /* kill our write urb pool */ 331 int j; 332 struct urb **write_urbs = priv->write_urb_pool; 333 spin_lock_irqsave(&priv->lock,flags); 334 335 for (j = 0; j < NUM_URBS; j++) { 336 if (write_urbs[j]) { 337 /* FIXME - uncomment the following 338 * usb_kill_urb call when the host 339 * controllers get fixed to set 340 * urb->dev = NULL after the urb is 341 * finished. Otherwise this call 342 * oopses. */ 343 /* usb_kill_urb(write_urbs[j]); */ 344 if (write_urbs[j]->transfer_buffer) 345 kfree(write_urbs[j]->transfer_buffer); 346 usb_free_urb (write_urbs[j]); 347 } 348 } 349 350 spin_unlock_irqrestore (&priv->lock, flags); 351 352 kfree(priv); 353 usb_set_serial_port_data(serial->port[i], NULL); 354 } 355 } 356 } /* klsi_105_shutdown */ 357 358 static int klsi_105_open (struct usb_serial_port *port, struct file *filp) 359 { 360 struct klsi_105_private *priv = usb_get_serial_port_data(port); 361 int retval = 0; 362 int rc; 363 int i; 364 unsigned long line_state; 365 struct klsi_105_port_settings cfg; 366 unsigned long flags; 367 368 dbg("%s port %d", __FUNCTION__, port->number); 369 370 /* force low_latency on so that our tty_push actually forces 371 * the data through 372 * port->tty->low_latency = 1; */ 373 374 /* Do a defined restart: 375 * Set up sane default baud rate and send the 'READ_ON' 376 * vendor command. 377 * FIXME: set modem line control (how?) 378 * Then read the modem line control and store values in 379 * priv->line_state. 380 */ 381 cfg.pktlen = 5; 382 cfg.baudrate = kl5kusb105a_sio_b9600; 383 cfg.databits = kl5kusb105a_dtb_8; 384 cfg.unknown1 = 0; 385 cfg.unknown2 = 1; 386 klsi_105_chg_port_settings(port, &cfg); 387 388 /* set up termios structure */ 389 spin_lock_irqsave (&priv->lock, flags); 390 priv->termios.c_iflag = port->tty->termios->c_iflag; 391 priv->termios.c_oflag = port->tty->termios->c_oflag; 392 priv->termios.c_cflag = port->tty->termios->c_cflag; 393 priv->termios.c_lflag = port->tty->termios->c_lflag; 394 for (i=0; i<NCCS; i++) 395 priv->termios.c_cc[i] = port->tty->termios->c_cc[i]; 396 priv->cfg.pktlen = cfg.pktlen; 397 priv->cfg.baudrate = cfg.baudrate; 398 priv->cfg.databits = cfg.databits; 399 priv->cfg.unknown1 = cfg.unknown1; 400 priv->cfg.unknown2 = cfg.unknown2; 401 spin_unlock_irqrestore (&priv->lock, flags); 402 403 /* READ_ON and urb submission */ 404 usb_fill_bulk_urb(port->read_urb, port->serial->dev, 405 usb_rcvbulkpipe(port->serial->dev, 406 port->bulk_in_endpointAddress), 407 port->read_urb->transfer_buffer, 408 port->read_urb->transfer_buffer_length, 409 klsi_105_read_bulk_callback, 410 port); 411 412 rc = usb_submit_urb(port->read_urb, GFP_KERNEL); 413 if (rc) { 414 err("%s - failed submitting read urb, error %d", __FUNCTION__, rc); 415 retval = rc; 416 goto exit; 417 } 418 419 rc = usb_control_msg(port->serial->dev, 420 usb_sndctrlpipe(port->serial->dev,0), 421 KL5KUSB105A_SIO_CONFIGURE, 422 USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE, 423 KL5KUSB105A_SIO_CONFIGURE_READ_ON, 424 0, /* index */ 425 NULL, 426 0, 427 KLSI_TIMEOUT); 428 if (rc < 0) { 429 err("Enabling read failed (error = %d)", rc); 430 retval = rc; 431 } else 432 dbg("%s - enabled reading", __FUNCTION__); 433 434 rc = klsi_105_get_line_state(port, &line_state); 435 if (rc >= 0) { 436 spin_lock_irqsave (&priv->lock, flags); 437 priv->line_state = line_state; 438 spin_unlock_irqrestore (&priv->lock, flags); 439 dbg("%s - read line state 0x%lx", __FUNCTION__, line_state); 440 retval = 0; 441 } else 442 retval = rc; 443 444 exit: 445 return retval; 446 } /* klsi_105_open */ 447 448 449 static void klsi_105_close (struct usb_serial_port *port, struct file *filp) 450 { 451 struct klsi_105_private *priv = usb_get_serial_port_data(port); 452 int rc; 453 454 dbg("%s port %d", __FUNCTION__, port->number); 455 456 /* send READ_OFF */ 457 rc = usb_control_msg (port->serial->dev, 458 usb_sndctrlpipe(port->serial->dev, 0), 459 KL5KUSB105A_SIO_CONFIGURE, 460 USB_TYPE_VENDOR | USB_DIR_OUT, 461 KL5KUSB105A_SIO_CONFIGURE_READ_OFF, 462 0, /* index */ 463 NULL, 0, 464 KLSI_TIMEOUT); 465 if (rc < 0) 466 err("Disabling read failed (error = %d)", rc); 467 468 /* shutdown our bulk reads and writes */ 469 usb_kill_urb(port->write_urb); 470 usb_kill_urb(port->read_urb); 471 /* unlink our write pool */ 472 /* FIXME */ 473 /* wgg - do I need this? I think so. */ 474 usb_kill_urb(port->interrupt_in_urb); 475 info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out); 476 } /* klsi_105_close */ 477 478 479 /* We need to write a complete 64-byte data block and encode the 480 * number actually sent in the first double-byte, LSB-order. That 481 * leaves at most 62 bytes of payload. 482 */ 483 #define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */ 484 485 486 static int klsi_105_write (struct usb_serial_port *port, 487 const unsigned char *buf, int count) 488 { 489 struct klsi_105_private *priv = usb_get_serial_port_data(port); 490 int result, size; 491 int bytes_sent=0; 492 493 dbg("%s - port %d", __FUNCTION__, port->number); 494 495 while (count > 0) { 496 /* try to find a free urb (write 0 bytes if none) */ 497 struct urb *urb = NULL; 498 unsigned long flags; 499 int i; 500 /* since the pool is per-port we might not need the spin lock !? */ 501 spin_lock_irqsave (&priv->lock, flags); 502 for (i=0; i<NUM_URBS; i++) { 503 if (priv->write_urb_pool[i]->status != -EINPROGRESS) { 504 urb = priv->write_urb_pool[i]; 505 dbg("%s - using pool URB %d", __FUNCTION__, i); 506 break; 507 } 508 } 509 spin_unlock_irqrestore (&priv->lock, flags); 510 511 if (urb==NULL) { 512 dbg("%s - no more free urbs", __FUNCTION__); 513 goto exit; 514 } 515 516 if (urb->transfer_buffer == NULL) { 517 urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC); 518 if (urb->transfer_buffer == NULL) { 519 err("%s - no more kernel memory...", __FUNCTION__); 520 goto exit; 521 } 522 } 523 524 size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET); 525 size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET); 526 527 memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size); 528 529 /* write payload size into transfer buffer */ 530 ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF); 531 ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8); 532 533 /* set up our urb */ 534 usb_fill_bulk_urb(urb, port->serial->dev, 535 usb_sndbulkpipe(port->serial->dev, 536 port->bulk_out_endpointAddress), 537 urb->transfer_buffer, 538 URB_TRANSFER_BUFFER_SIZE, 539 klsi_105_write_bulk_callback, 540 port); 541 542 /* send the data out the bulk port */ 543 result = usb_submit_urb(urb, GFP_ATOMIC); 544 if (result) { 545 err("%s - failed submitting write urb, error %d", __FUNCTION__, result); 546 goto exit; 547 } 548 buf += size; 549 bytes_sent += size; 550 count -= size; 551 } 552 exit: 553 /* lockless, but it's for debug info only... */ 554 priv->bytes_out+=bytes_sent; 555 556 return bytes_sent; /* that's how much we wrote */ 557 } /* klsi_105_write */ 558 559 static void klsi_105_write_bulk_callback ( struct urb *urb, struct pt_regs *regs) 560 { 561 struct usb_serial_port *port = (struct usb_serial_port *)urb->context; 562 563 dbg("%s - port %d", __FUNCTION__, port->number); 564 565 if (urb->status) { 566 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, 567 urb->status); 568 return; 569 } 570 571 /* from generic_write_bulk_callback */ 572 schedule_work(&port->work); 573 } /* klsi_105_write_bulk_completion_callback */ 574 575 576 /* return number of characters currently in the writing process */ 577 static int klsi_105_chars_in_buffer (struct usb_serial_port *port) 578 { 579 int chars = 0; 580 int i; 581 unsigned long flags; 582 struct klsi_105_private *priv = usb_get_serial_port_data(port); 583 584 spin_lock_irqsave (&priv->lock, flags); 585 586 for (i = 0; i < NUM_URBS; ++i) { 587 if (priv->write_urb_pool[i]->status == -EINPROGRESS) { 588 chars += URB_TRANSFER_BUFFER_SIZE; 589 } 590 } 591 592 spin_unlock_irqrestore (&priv->lock, flags); 593 594 dbg("%s - returns %d", __FUNCTION__, chars); 595 return (chars); 596 } 597 598 static int klsi_105_write_room (struct usb_serial_port *port) 599 { 600 unsigned long flags; 601 int i; 602 int room = 0; 603 struct klsi_105_private *priv = usb_get_serial_port_data(port); 604 605 spin_lock_irqsave (&priv->lock, flags); 606 for (i = 0; i < NUM_URBS; ++i) { 607 if (priv->write_urb_pool[i]->status != -EINPROGRESS) { 608 room += URB_TRANSFER_BUFFER_SIZE; 609 } 610 } 611 612 spin_unlock_irqrestore (&priv->lock, flags); 613 614 dbg("%s - returns %d", __FUNCTION__, room); 615 return (room); 616 } 617 618 619 620 static void klsi_105_read_bulk_callback (struct urb *urb, struct pt_regs *regs) 621 { 622 struct usb_serial_port *port = (struct usb_serial_port *)urb->context; 623 struct klsi_105_private *priv = usb_get_serial_port_data(port); 624 struct tty_struct *tty; 625 unsigned char *data = urb->transfer_buffer; 626 int rc; 627 628 dbg("%s - port %d", __FUNCTION__, port->number); 629 630 /* The urb might have been killed. */ 631 if (urb->status) { 632 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, 633 urb->status); 634 return; 635 } 636 637 /* The data received is again preceded by a length double-byte in LSB- 638 * first order (see klsi_105_write() ) 639 */ 640 if (urb->actual_length == 0) { 641 /* empty urbs seem to happen, we ignore them */ 642 /* dbg("%s - emtpy URB", __FUNCTION__); */ 643 ; 644 } else if (urb->actual_length <= 2) { 645 dbg("%s - size %d URB not understood", __FUNCTION__, 646 urb->actual_length); 647 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, 648 urb->actual_length, data); 649 } else { 650 int i; 651 int bytes_sent = ((__u8 *) data)[0] + 652 ((unsigned int) ((__u8 *) data)[1] << 8); 653 tty = port->tty; 654 /* we should immediately resubmit the URB, before attempting 655 * to pass the data on to the tty layer. But that needs locking 656 * against re-entry an then mixed-up data because of 657 * intermixed tty_flip_buffer_push()s 658 * FIXME 659 */ 660 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, 661 urb->actual_length, data); 662 663 if (bytes_sent + 2 > urb->actual_length) { 664 dbg("%s - trying to read more data than available" 665 " (%d vs. %d)", __FUNCTION__, 666 bytes_sent+2, urb->actual_length); 667 /* cap at implied limit */ 668 bytes_sent = urb->actual_length - 2; 669 } 670 671 for (i = 2; i < 2+bytes_sent; i++) { 672 /* if we insert more than TTY_FLIPBUF_SIZE characters, 673 * we drop them. */ 674 if(tty->flip.count >= TTY_FLIPBUF_SIZE) { 675 tty_flip_buffer_push(tty); 676 } 677 /* this doesn't actually push the data through unless 678 * tty->low_latency is set */ 679 tty_insert_flip_char(tty, ((__u8*) data)[i], 0); 680 } 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 termios *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 int klsi_105_ioctl (struct usb_serial_port *port, struct file * file, 914 unsigned int cmd, unsigned long arg) 915 { 916 struct klsi_105_private *priv = usb_get_serial_port_data(port); 917 void __user *user_arg = (void __user *)arg; 918 919 dbg("%scmd=0x%x", __FUNCTION__, cmd); 920 921 /* Based on code from acm.c and others */ 922 switch (cmd) { 923 case TIOCMIWAIT: 924 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/ 925 /* TODO */ 926 dbg("%s - TIOCMIWAIT not handled", __FUNCTION__); 927 return -ENOIOCTLCMD; 928 case TIOCGICOUNT: 929 /* return count of modemline transitions */ 930 /* TODO */ 931 dbg("%s - TIOCGICOUNT not handled", __FUNCTION__); 932 return -ENOIOCTLCMD; 933 case TCGETS: 934 /* return current info to caller */ 935 dbg("%s - TCGETS data faked/incomplete", __FUNCTION__); 936 937 if (!access_ok(VERIFY_WRITE, user_arg, sizeof(struct termios))) 938 return -EFAULT; 939 940 if (kernel_termios_to_user_termios((struct termios __user *)arg, 941 &priv->termios)) 942 return -EFAULT; 943 return 0; 944 case TCSETS: 945 /* set port termios to the one given by the user */ 946 dbg("%s - TCSETS not handled", __FUNCTION__); 947 948 if (!access_ok(VERIFY_READ, user_arg, sizeof(struct termios))) 949 return -EFAULT; 950 951 if (user_termios_to_kernel_termios(&priv->termios, 952 (struct termios __user *)arg)) 953 return -EFAULT; 954 klsi_105_set_termios(port, &priv->termios); 955 return 0; 956 case TCSETSW: { 957 /* set port termios and try to wait for completion of last 958 * write operation */ 959 /* We guess here. If there are not too many write urbs 960 * outstanding, we lie. */ 961 /* what is the right way to wait here? schedule() ? */ 962 /* 963 while (klsi_105_chars_in_buffer(port) > (NUM_URBS / 4 ) * URB_TRANSFER_BUFFER_SIZE) 964 schedule(); 965 */ 966 return -ENOIOCTLCMD; 967 } 968 default: 969 dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd); 970 return(-ENOIOCTLCMD); 971 break; 972 } 973 return 0; 974 } /* klsi_105_ioctl */ 975 976 static void klsi_105_throttle (struct usb_serial_port *port) 977 { 978 dbg("%s - port %d", __FUNCTION__, port->number); 979 usb_kill_urb(port->read_urb); 980 } 981 982 static void klsi_105_unthrottle (struct usb_serial_port *port) 983 { 984 int result; 985 986 dbg("%s - port %d", __FUNCTION__, port->number); 987 988 port->read_urb->dev = port->serial->dev; 989 result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 990 if (result) 991 err("%s - failed submitting read urb, error %d", __FUNCTION__, 992 result); 993 } 994 995 996 997 static int __init klsi_105_init (void) 998 { 999 int retval; 1000 retval = usb_serial_register(&kl5kusb105d_device); 1001 if (retval) 1002 goto failed_usb_serial_register; 1003 retval = usb_register(&kl5kusb105d_driver); 1004 if (retval) 1005 goto failed_usb_register; 1006 1007 info(DRIVER_DESC " " DRIVER_VERSION); 1008 return 0; 1009 failed_usb_register: 1010 usb_serial_deregister(&kl5kusb105d_device); 1011 failed_usb_serial_register: 1012 return retval; 1013 } 1014 1015 1016 static void __exit klsi_105_exit (void) 1017 { 1018 usb_deregister (&kl5kusb105d_driver); 1019 usb_serial_deregister (&kl5kusb105d_device); 1020 } 1021 1022 1023 module_init (klsi_105_init); 1024 module_exit (klsi_105_exit); 1025 1026 MODULE_AUTHOR( DRIVER_AUTHOR ); 1027 MODULE_DESCRIPTION( DRIVER_DESC ); 1028 MODULE_LICENSE("GPL"); 1029 1030 1031 module_param(debug, bool, S_IRUGO | S_IWUSR); 1032 MODULE_PARM_DESC(debug, "enable extensive debugging messages"); 1033 1034 /* vim: set sts=8 ts=8 sw=8: */ 1035