1/* 2 * USB Keyspan PDA / Xircom / Entregra Converter driver 3 * 4 * Copyright (C) 1999 - 2001 Greg Kroah-Hartman <greg@kroah.com> 5 * Copyright (C) 1999, 2000 Brian Warner <warner@lothar.com> 6 * Copyright (C) 2000 Al Borchers <borchers@steinerpoint.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * See Documentation/usb/usb-serial.txt for more information on using this 14 * driver 15 * 16 * (09/07/2001) gkh 17 * cleaned up the Xircom support. Added ids for Entregra device which is 18 * the same as the Xircom device. Enabled the code to be compiled for 19 * either Xircom or Keyspan devices. 20 * 21 * (08/11/2001) Cristian M. Craciunescu 22 * support for Xircom PGSDB9 23 * 24 * (05/31/2001) gkh 25 * switched from using spinlock to a semaphore, which fixes lots of 26 * problems. 27 * 28 * (04/08/2001) gb 29 * Identify version on module load. 30 * 31 * (11/01/2000) Adam J. Richter 32 * usb_device_id table support 33 * 34 * (10/05/2000) gkh 35 * Fixed bug with urb->dev not being set properly, now that the usb 36 * core needs it. 37 * 38 * (08/28/2000) gkh 39 * Added locks for SMP safeness. 40 * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more 41 * than once. 42 * 43 * (07/20/2000) borchers 44 * - keyspan_pda_write no longer sleeps if it is called on interrupt time; 45 * PPP and the line discipline with stty echo on can call write on 46 * interrupt time and this would cause an oops if write slept 47 * - if keyspan_pda_write is in an interrupt, it will not call 48 * usb_control_msg (which sleeps) to query the room in the device 49 * buffer, it simply uses the current room value it has 50 * - if the urb is busy or if it is throttled keyspan_pda_write just 51 * returns 0, rather than sleeping to wait for this to change; the 52 * write_chan code in n_tty.c will sleep if needed before calling 53 * keyspan_pda_write again 54 * - if the device needs to be unthrottled, write now queues up the 55 * call to usb_control_msg (which sleeps) to unthrottle the device 56 * - the wakeups from keyspan_pda_write_bulk_callback are queued rather 57 * than done directly from the callback to avoid the race in write_chan 58 * - keyspan_pda_chars_in_buffer also indicates its buffer is full if the 59 * urb status is -EINPROGRESS, meaning it cannot write at the moment 60 * 61 * (07/19/2000) gkh 62 * Added module_init and module_exit functions to handle the fact that this 63 * driver is a loadable module now. 64 * 65 * (03/26/2000) gkh 66 * Split driver up into device specific pieces. 67 * 68 */ 69 70 71#include <linux/kernel.h> 72#include <linux/errno.h> 73#include <linux/init.h> 74#include <linux/slab.h> 75#include <linux/tty.h> 76#include <linux/tty_driver.h> 77#include <linux/tty_flip.h> 78#include <linux/module.h> 79#include <linux/spinlock.h> 80#include <linux/workqueue.h> 81#include <linux/firmware.h> 82#include <linux/ihex.h> 83#include <linux/uaccess.h> 84#include <linux/usb.h> 85#include <linux/usb/serial.h> 86 87static int debug; 88 89/* make a simple define to handle if we are compiling keyspan_pda or xircom support */ 90#if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || \ 91 defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE) 92 #define KEYSPAN 93#else 94 #undef KEYSPAN 95#endif 96#if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE) 97 #define XIRCOM 98#else 99 #undef XIRCOM 100#endif 101 102/* 103 * Version Information 104 */ 105#define DRIVER_VERSION "v1.1" 106#define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>" 107#define DRIVER_DESC "USB Keyspan PDA Converter driver" 108 109struct keyspan_pda_private { 110 int tx_room; 111 int tx_throttled; 112 struct work_struct wakeup_work; 113 struct work_struct unthrottle_work; 114 struct usb_serial *serial; 115 struct usb_serial_port *port; 116}; 117 118 119#define KEYSPAN_VENDOR_ID 0x06cd 120#define KEYSPAN_PDA_FAKE_ID 0x0103 121#define KEYSPAN_PDA_ID 0x0104 /* no clue */ 122 123/* For Xircom PGSDB9 and older Entregra version of the same device */ 124#define XIRCOM_VENDOR_ID 0x085a 125#define XIRCOM_FAKE_ID 0x8027 126#define ENTREGRA_VENDOR_ID 0x1645 127#define ENTREGRA_FAKE_ID 0x8093 128 129static const struct usb_device_id id_table_combined[] = { 130#ifdef KEYSPAN 131 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) }, 132#endif 133#ifdef XIRCOM 134 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) }, 135 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) }, 136#endif 137 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) }, 138 { } /* Terminating entry */ 139}; 140 141MODULE_DEVICE_TABLE(usb, id_table_combined); 142 143static struct usb_driver keyspan_pda_driver = { 144 .name = "keyspan_pda", 145 .probe = usb_serial_probe, 146 .disconnect = usb_serial_disconnect, 147 .id_table = id_table_combined, 148 .no_dynamic_id = 1, 149}; 150 151static const struct usb_device_id id_table_std[] = { 152 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) }, 153 { } /* Terminating entry */ 154}; 155 156#ifdef KEYSPAN 157static const struct usb_device_id id_table_fake[] = { 158 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) }, 159 { } /* Terminating entry */ 160}; 161#endif 162 163#ifdef XIRCOM 164static const struct usb_device_id id_table_fake_xircom[] = { 165 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) }, 166 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) }, 167 { } 168}; 169#endif 170 171static void keyspan_pda_wakeup_write(struct work_struct *work) 172{ 173 struct keyspan_pda_private *priv = 174 container_of(work, struct keyspan_pda_private, wakeup_work); 175 struct usb_serial_port *port = priv->port; 176 struct tty_struct *tty = tty_port_tty_get(&port->port); 177 tty_wakeup(tty); 178 tty_kref_put(tty); 179} 180 181static void keyspan_pda_request_unthrottle(struct work_struct *work) 182{ 183 struct keyspan_pda_private *priv = 184 container_of(work, struct keyspan_pda_private, unthrottle_work); 185 struct usb_serial *serial = priv->serial; 186 int result; 187 188 dbg(" request_unthrottle"); 189 /* ask the device to tell us when the tx buffer becomes 190 sufficiently empty */ 191 result = usb_control_msg(serial->dev, 192 usb_sndctrlpipe(serial->dev, 0), 193 7, /* request_unthrottle */ 194 USB_TYPE_VENDOR | USB_RECIP_INTERFACE 195 | USB_DIR_OUT, 196 16, /* value: threshold */ 197 0, /* index */ 198 NULL, 199 0, 200 2000); 201 if (result < 0) 202 dbg("%s - error %d from usb_control_msg", 203 __func__, result); 204} 205 206 207static void keyspan_pda_rx_interrupt(struct urb *urb) 208{ 209 struct usb_serial_port *port = urb->context; 210 struct tty_struct *tty = tty_port_tty_get(&port->port); 211 unsigned char *data = urb->transfer_buffer; 212 int retval; 213 int status = urb->status; 214 struct keyspan_pda_private *priv; 215 priv = usb_get_serial_port_data(port); 216 217 switch (status) { 218 case 0: 219 /* success */ 220 break; 221 case -ECONNRESET: 222 case -ENOENT: 223 case -ESHUTDOWN: 224 /* this urb is terminated, clean up */ 225 dbg("%s - urb shutting down with status: %d", 226 __func__, status); 227 goto out; 228 default: 229 dbg("%s - nonzero urb status received: %d", 230 __func__, status); 231 goto exit; 232 } 233 234 /* see if the message is data or a status interrupt */ 235 switch (data[0]) { 236 case 0: 237 /* rest of message is rx data */ 238 if (urb->actual_length) { 239 tty_insert_flip_string(tty, data + 1, 240 urb->actual_length - 1); 241 tty_flip_buffer_push(tty); 242 } 243 break; 244 case 1: 245 /* status interrupt */ 246 dbg(" rx int, d1=%d, d2=%d", data[1], data[2]); 247 switch (data[1]) { 248 case 1: /* modemline change */ 249 break; 250 case 2: /* tx unthrottle interrupt */ 251 priv->tx_throttled = 0; 252 /* queue up a wakeup at scheduler time */ 253 schedule_work(&priv->wakeup_work); 254 break; 255 default: 256 break; 257 } 258 break; 259 default: 260 break; 261 } 262 263exit: 264 retval = usb_submit_urb(urb, GFP_ATOMIC); 265 if (retval) 266 dev_err(&port->dev, 267 "%s - usb_submit_urb failed with result %d", 268 __func__, retval); 269out: 270 tty_kref_put(tty); 271} 272 273 274static void keyspan_pda_rx_throttle(struct tty_struct *tty) 275{ 276 /* stop receiving characters. We just turn off the URB request, and 277 let chars pile up in the device. If we're doing hardware 278 flowcontrol, the device will signal the other end when its buffer 279 fills up. If we're doing XON/XOFF, this would be a good time to 280 send an XOFF, although it might make sense to foist that off 281 upon the device too. */ 282 struct usb_serial_port *port = tty->driver_data; 283 dbg("keyspan_pda_rx_throttle port %d", port->number); 284 usb_kill_urb(port->interrupt_in_urb); 285} 286 287 288static void keyspan_pda_rx_unthrottle(struct tty_struct *tty) 289{ 290 struct usb_serial_port *port = tty->driver_data; 291 /* just restart the receive interrupt URB */ 292 dbg("keyspan_pda_rx_unthrottle port %d", port->number); 293 port->interrupt_in_urb->dev = port->serial->dev; 294 if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL)) 295 dbg(" usb_submit_urb(read urb) failed"); 296 return; 297} 298 299 300static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud) 301{ 302 int rc; 303 int bindex; 304 305 switch (baud) { 306 case 110: 307 bindex = 0; 308 break; 309 case 300: 310 bindex = 1; 311 break; 312 case 1200: 313 bindex = 2; 314 break; 315 case 2400: 316 bindex = 3; 317 break; 318 case 4800: 319 bindex = 4; 320 break; 321 case 9600: 322 bindex = 5; 323 break; 324 case 19200: 325 bindex = 6; 326 break; 327 case 38400: 328 bindex = 7; 329 break; 330 case 57600: 331 bindex = 8; 332 break; 333 case 115200: 334 bindex = 9; 335 break; 336 default: 337 bindex = 5; /* Default to 9600 */ 338 baud = 9600; 339 } 340 341 /* rather than figure out how to sleep while waiting for this 342 to complete, I just use the "legacy" API. */ 343 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 344 0, /* set baud */ 345 USB_TYPE_VENDOR 346 | USB_RECIP_INTERFACE 347 | USB_DIR_OUT, /* type */ 348 bindex, /* value */ 349 0, /* index */ 350 NULL, /* &data */ 351 0, /* size */ 352 2000); /* timeout */ 353 if (rc < 0) 354 return 0; 355 return baud; 356} 357 358 359static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state) 360{ 361 struct usb_serial_port *port = tty->driver_data; 362 struct usb_serial *serial = port->serial; 363 int value; 364 int result; 365 366 if (break_state == -1) 367 value = 1; /* start break */ 368 else 369 value = 0; /* clear break */ 370 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 371 4, /* set break */ 372 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT, 373 value, 0, NULL, 0, 2000); 374 if (result < 0) 375 dbg("%s - error %d from usb_control_msg", 376 __func__, result); 377 /* there is something funky about this.. the TCSBRK that 'cu' performs 378 ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4 379 seconds apart, but it feels like the break sent isn't as long as it 380 is on /dev/ttyS0 */ 381} 382 383 384static void keyspan_pda_set_termios(struct tty_struct *tty, 385 struct usb_serial_port *port, struct ktermios *old_termios) 386{ 387 struct usb_serial *serial = port->serial; 388 speed_t speed; 389 390 /* cflag specifies lots of stuff: number of stop bits, parity, number 391 of data bits, baud. What can the device actually handle?: 392 CSTOPB (1 stop bit or 2) 393 PARENB (parity) 394 CSIZE (5bit .. 8bit) 395 There is minimal hw support for parity (a PSW bit seems to hold the 396 parity of whatever is in the accumulator). The UART either deals 397 with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data, 398 1 special, stop). So, with firmware changes, we could do: 399 8N1: 10 bit 400 8N2: 11 bit, extra bit always (mark?) 401 8[EOMS]1: 11 bit, extra bit is parity 402 7[EOMS]1: 10 bit, b0/b7 is parity 403 7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?) 404 405 HW flow control is dictated by the tty->termios->c_cflags & CRTSCTS 406 bit. 407 408 For now, just do baud. */ 409 410 speed = tty_get_baud_rate(tty); 411 speed = keyspan_pda_setbaud(serial, speed); 412 413 if (speed == 0) { 414 dbg("can't handle requested baud rate"); 415 /* It hasn't changed so.. */ 416 speed = tty_termios_baud_rate(old_termios); 417 } 418 /* Only speed can change so copy the old h/w parameters 419 then encode the new speed */ 420 tty_termios_copy_hw(tty->termios, old_termios); 421 tty_encode_baud_rate(tty, speed, speed); 422} 423 424 425/* modem control pins: DTR and RTS are outputs and can be controlled. 426 DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be 427 read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */ 428 429static int keyspan_pda_get_modem_info(struct usb_serial *serial, 430 unsigned char *value) 431{ 432 int rc; 433 u8 *data; 434 435 data = kmalloc(1, GFP_KERNEL); 436 if (!data) 437 return -ENOMEM; 438 439 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 440 3, /* get pins */ 441 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN, 442 0, 0, data, 1, 2000); 443 if (rc >= 0) 444 *value = *data; 445 446 kfree(data); 447 return rc; 448} 449 450 451static int keyspan_pda_set_modem_info(struct usb_serial *serial, 452 unsigned char value) 453{ 454 int rc; 455 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 456 3, /* set pins */ 457 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT, 458 value, 0, NULL, 0, 2000); 459 return rc; 460} 461 462static int keyspan_pda_tiocmget(struct tty_struct *tty, struct file *file) 463{ 464 struct usb_serial_port *port = tty->driver_data; 465 struct usb_serial *serial = port->serial; 466 int rc; 467 unsigned char status; 468 int value; 469 470 rc = keyspan_pda_get_modem_info(serial, &status); 471 if (rc < 0) 472 return rc; 473 value = 474 ((status & (1<<7)) ? TIOCM_DTR : 0) | 475 ((status & (1<<6)) ? TIOCM_CAR : 0) | 476 ((status & (1<<5)) ? TIOCM_RNG : 0) | 477 ((status & (1<<4)) ? TIOCM_DSR : 0) | 478 ((status & (1<<3)) ? TIOCM_CTS : 0) | 479 ((status & (1<<2)) ? TIOCM_RTS : 0); 480 return value; 481} 482 483static int keyspan_pda_tiocmset(struct tty_struct *tty, struct file *file, 484 unsigned int set, unsigned int clear) 485{ 486 struct usb_serial_port *port = tty->driver_data; 487 struct usb_serial *serial = port->serial; 488 int rc; 489 unsigned char status; 490 491 rc = keyspan_pda_get_modem_info(serial, &status); 492 if (rc < 0) 493 return rc; 494 495 if (set & TIOCM_RTS) 496 status |= (1<<2); 497 if (set & TIOCM_DTR) 498 status |= (1<<7); 499 500 if (clear & TIOCM_RTS) 501 status &= ~(1<<2); 502 if (clear & TIOCM_DTR) 503 status &= ~(1<<7); 504 rc = keyspan_pda_set_modem_info(serial, status); 505 return rc; 506} 507 508static int keyspan_pda_write(struct tty_struct *tty, 509 struct usb_serial_port *port, const unsigned char *buf, int count) 510{ 511 struct usb_serial *serial = port->serial; 512 int request_unthrottle = 0; 513 int rc = 0; 514 struct keyspan_pda_private *priv; 515 516 priv = usb_get_serial_port_data(port); 517 /* guess how much room is left in the device's ring buffer, and if we 518 want to send more than that, check first, updating our notion of 519 what is left. If our write will result in no room left, ask the 520 device to give us an interrupt when the room available rises above 521 a threshold, and hold off all writers (eventually, those using 522 select() or poll() too) until we receive that unthrottle interrupt. 523 Block if we can't write anything at all, otherwise write as much as 524 we can. */ 525 dbg("keyspan_pda_write(%d)", count); 526 if (count == 0) { 527 dbg(" write request of 0 bytes"); 528 return 0; 529 } 530 531 /* we might block because of: 532 the TX urb is in-flight (wait until it completes) 533 the device is full (wait until it says there is room) 534 */ 535 spin_lock_bh(&port->lock); 536 if (port->write_urb_busy || priv->tx_throttled) { 537 spin_unlock_bh(&port->lock); 538 return 0; 539 } 540 port->write_urb_busy = 1; 541 spin_unlock_bh(&port->lock); 542 543 /* At this point the URB is in our control, nobody else can submit it 544 again (the only sudden transition was the one from EINPROGRESS to 545 finished). Also, the tx process is not throttled. So we are 546 ready to write. */ 547 548 count = (count > port->bulk_out_size) ? port->bulk_out_size : count; 549 550 /* Check if we might overrun the Tx buffer. If so, ask the 551 device how much room it really has. This is done only on 552 scheduler time, since usb_control_msg() sleeps. */ 553 if (count > priv->tx_room && !in_interrupt()) { 554 u8 *room; 555 556 room = kmalloc(1, GFP_KERNEL); 557 if (!room) { 558 rc = -ENOMEM; 559 goto exit; 560 } 561 562 rc = usb_control_msg(serial->dev, 563 usb_rcvctrlpipe(serial->dev, 0), 564 6, /* write_room */ 565 USB_TYPE_VENDOR | USB_RECIP_INTERFACE 566 | USB_DIR_IN, 567 0, /* value: 0 means "remaining room" */ 568 0, /* index */ 569 room, 570 1, 571 2000); 572 if (rc > 0) { 573 dbg(" roomquery says %d", *room); 574 priv->tx_room = *room; 575 } 576 kfree(room); 577 if (rc < 0) { 578 dbg(" roomquery failed"); 579 goto exit; 580 } 581 if (rc == 0) { 582 dbg(" roomquery returned 0 bytes"); 583 rc = -EIO; /* device didn't return any data */ 584 goto exit; 585 } 586 } 587 if (count > priv->tx_room) { 588 /* we're about to completely fill the Tx buffer, so 589 we'll be throttled afterwards. */ 590 count = priv->tx_room; 591 request_unthrottle = 1; 592 } 593 594 if (count) { 595 /* now transfer data */ 596 memcpy(port->write_urb->transfer_buffer, buf, count); 597 /* send the data out the bulk port */ 598 port->write_urb->transfer_buffer_length = count; 599 600 priv->tx_room -= count; 601 602 port->write_urb->dev = port->serial->dev; 603 rc = usb_submit_urb(port->write_urb, GFP_ATOMIC); 604 if (rc) { 605 dbg(" usb_submit_urb(write bulk) failed"); 606 goto exit; 607 } 608 } else { 609 /* There wasn't any room left, so we are throttled until 610 the buffer empties a bit */ 611 request_unthrottle = 1; 612 } 613 614 if (request_unthrottle) { 615 priv->tx_throttled = 1; /* block writers */ 616 schedule_work(&priv->unthrottle_work); 617 } 618 619 rc = count; 620exit: 621 if (rc < 0) 622 port->write_urb_busy = 0; 623 return rc; 624} 625 626 627static void keyspan_pda_write_bulk_callback(struct urb *urb) 628{ 629 struct usb_serial_port *port = urb->context; 630 struct keyspan_pda_private *priv; 631 632 port->write_urb_busy = 0; 633 priv = usb_get_serial_port_data(port); 634 635 /* queue up a wakeup at scheduler time */ 636 schedule_work(&priv->wakeup_work); 637} 638 639 640static int keyspan_pda_write_room(struct tty_struct *tty) 641{ 642 struct usb_serial_port *port = tty->driver_data; 643 struct keyspan_pda_private *priv; 644 priv = usb_get_serial_port_data(port); 645 /* used by n_tty.c for processing of tabs and such. Giving it our 646 conservative guess is probably good enough, but needs testing by 647 running a console through the device. */ 648 return priv->tx_room; 649} 650 651 652static int keyspan_pda_chars_in_buffer(struct tty_struct *tty) 653{ 654 struct usb_serial_port *port = tty->driver_data; 655 struct keyspan_pda_private *priv; 656 unsigned long flags; 657 int ret = 0; 658 659 priv = usb_get_serial_port_data(port); 660 661 /* when throttled, return at least WAKEUP_CHARS to tell select() (via 662 n_tty.c:normal_poll() ) that we're not writeable. */ 663 664 spin_lock_irqsave(&port->lock, flags); 665 if (port->write_urb_busy || priv->tx_throttled) 666 ret = 256; 667 spin_unlock_irqrestore(&port->lock, flags); 668 return ret; 669} 670 671 672static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on) 673{ 674 struct usb_serial *serial = port->serial; 675 676 if (serial->dev) { 677 if (on) 678 keyspan_pda_set_modem_info(serial, (1<<7) | (1<< 2)); 679 else 680 keyspan_pda_set_modem_info(serial, 0); 681 } 682} 683 684 685static int keyspan_pda_open(struct tty_struct *tty, 686 struct usb_serial_port *port) 687{ 688 struct usb_serial *serial = port->serial; 689 u8 *room; 690 int rc = 0; 691 struct keyspan_pda_private *priv; 692 693 /* find out how much room is in the Tx ring */ 694 room = kmalloc(1, GFP_KERNEL); 695 if (!room) 696 return -ENOMEM; 697 698 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 699 6, /* write_room */ 700 USB_TYPE_VENDOR | USB_RECIP_INTERFACE 701 | USB_DIR_IN, 702 0, /* value */ 703 0, /* index */ 704 room, 705 1, 706 2000); 707 if (rc < 0) { 708 dbg("%s - roomquery failed", __func__); 709 goto error; 710 } 711 if (rc == 0) { 712 dbg("%s - roomquery returned 0 bytes", __func__); 713 rc = -EIO; 714 goto error; 715 } 716 priv = usb_get_serial_port_data(port); 717 priv->tx_room = *room; 718 priv->tx_throttled = *room ? 0 : 1; 719 720 /*Start reading from the device*/ 721 port->interrupt_in_urb->dev = serial->dev; 722 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 723 if (rc) { 724 dbg("%s - usb_submit_urb(read int) failed", __func__); 725 goto error; 726 } 727error: 728 kfree(room); 729 return rc; 730} 731static void keyspan_pda_close(struct usb_serial_port *port) 732{ 733 struct usb_serial *serial = port->serial; 734 735 if (serial->dev) { 736 /* shutdown our bulk reads and writes */ 737 usb_kill_urb(port->write_urb); 738 usb_kill_urb(port->interrupt_in_urb); 739 } 740} 741 742 743/* download the firmware to a "fake" device (pre-renumeration) */ 744static int keyspan_pda_fake_startup(struct usb_serial *serial) 745{ 746 int response; 747 const char *fw_name; 748 const struct ihex_binrec *record; 749 const struct firmware *fw; 750 751 /* download the firmware here ... */ 752 response = ezusb_set_reset(serial, 1); 753 754 if (0) { ; } 755#ifdef KEYSPAN 756 else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID) 757 fw_name = "keyspan_pda/keyspan_pda.fw"; 758#endif 759#ifdef XIRCOM 760 else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) || 761 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGRA_VENDOR_ID)) 762 fw_name = "keyspan_pda/xircom_pgs.fw"; 763#endif 764 else { 765 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n", 766 __func__); 767 return -ENODEV; 768 } 769 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) { 770 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n", 771 fw_name); 772 return -ENOENT; 773 } 774 record = (const struct ihex_binrec *)fw->data; 775 776 while (record) { 777 response = ezusb_writememory(serial, be32_to_cpu(record->addr), 778 (unsigned char *)record->data, 779 be16_to_cpu(record->len), 0xa0); 780 if (response < 0) { 781 dev_err(&serial->dev->dev, "ezusb_writememory failed " 782 "for Keyspan PDA firmware (%d %04X %p %d)\n", 783 response, be32_to_cpu(record->addr), 784 record->data, be16_to_cpu(record->len)); 785 break; 786 } 787 record = ihex_next_binrec(record); 788 } 789 release_firmware(fw); 790 /* bring device out of reset. Renumeration will occur in a moment 791 and the new device will bind to the real driver */ 792 response = ezusb_set_reset(serial, 0); 793 794 /* we want this device to fail to have a driver assigned to it. */ 795 return 1; 796} 797 798#ifdef KEYSPAN 799MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw"); 800#endif 801#ifdef XIRCOM 802MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw"); 803#endif 804 805static int keyspan_pda_startup(struct usb_serial *serial) 806{ 807 808 struct keyspan_pda_private *priv; 809 810 /* allocate the private data structures for all ports. Well, for all 811 one ports. */ 812 813 priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL); 814 if (!priv) 815 return 1; /* error */ 816 usb_set_serial_port_data(serial->port[0], priv); 817 init_waitqueue_head(&serial->port[0]->write_wait); 818 INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write); 819 INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle); 820 priv->serial = serial; 821 priv->port = serial->port[0]; 822 return 0; 823} 824 825static void keyspan_pda_release(struct usb_serial *serial) 826{ 827 dbg("%s", __func__); 828 829 kfree(usb_get_serial_port_data(serial->port[0])); 830} 831 832#ifdef KEYSPAN 833static struct usb_serial_driver keyspan_pda_fake_device = { 834 .driver = { 835 .owner = THIS_MODULE, 836 .name = "keyspan_pda_pre", 837 }, 838 .description = "Keyspan PDA - (prerenumeration)", 839 .usb_driver = &keyspan_pda_driver, 840 .id_table = id_table_fake, 841 .num_ports = 1, 842 .attach = keyspan_pda_fake_startup, 843}; 844#endif 845 846#ifdef XIRCOM 847static struct usb_serial_driver xircom_pgs_fake_device = { 848 .driver = { 849 .owner = THIS_MODULE, 850 .name = "xircom_no_firm", 851 }, 852 .description = "Xircom / Entregra PGS - (prerenumeration)", 853 .usb_driver = &keyspan_pda_driver, 854 .id_table = id_table_fake_xircom, 855 .num_ports = 1, 856 .attach = keyspan_pda_fake_startup, 857}; 858#endif 859 860static struct usb_serial_driver keyspan_pda_device = { 861 .driver = { 862 .owner = THIS_MODULE, 863 .name = "keyspan_pda", 864 }, 865 .description = "Keyspan PDA", 866 .usb_driver = &keyspan_pda_driver, 867 .id_table = id_table_std, 868 .num_ports = 1, 869 .dtr_rts = keyspan_pda_dtr_rts, 870 .open = keyspan_pda_open, 871 .close = keyspan_pda_close, 872 .write = keyspan_pda_write, 873 .write_room = keyspan_pda_write_room, 874 .write_bulk_callback = keyspan_pda_write_bulk_callback, 875 .read_int_callback = keyspan_pda_rx_interrupt, 876 .chars_in_buffer = keyspan_pda_chars_in_buffer, 877 .throttle = keyspan_pda_rx_throttle, 878 .unthrottle = keyspan_pda_rx_unthrottle, 879 .set_termios = keyspan_pda_set_termios, 880 .break_ctl = keyspan_pda_break_ctl, 881 .tiocmget = keyspan_pda_tiocmget, 882 .tiocmset = keyspan_pda_tiocmset, 883 .attach = keyspan_pda_startup, 884 .release = keyspan_pda_release, 885}; 886 887 888static int __init keyspan_pda_init(void) 889{ 890 int retval; 891 retval = usb_serial_register(&keyspan_pda_device); 892 if (retval) 893 goto failed_pda_register; 894#ifdef KEYSPAN 895 retval = usb_serial_register(&keyspan_pda_fake_device); 896 if (retval) 897 goto failed_pda_fake_register; 898#endif 899#ifdef XIRCOM 900 retval = usb_serial_register(&xircom_pgs_fake_device); 901 if (retval) 902 goto failed_xircom_register; 903#endif 904 retval = usb_register(&keyspan_pda_driver); 905 if (retval) 906 goto failed_usb_register; 907 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" 908 DRIVER_DESC "\n"); 909 return 0; 910failed_usb_register: 911#ifdef XIRCOM 912 usb_serial_deregister(&xircom_pgs_fake_device); 913failed_xircom_register: 914#endif /* XIRCOM */ 915#ifdef KEYSPAN 916 usb_serial_deregister(&keyspan_pda_fake_device); 917#endif 918#ifdef KEYSPAN 919failed_pda_fake_register: 920#endif 921 usb_serial_deregister(&keyspan_pda_device); 922failed_pda_register: 923 return retval; 924} 925 926 927static void __exit keyspan_pda_exit(void) 928{ 929 usb_deregister(&keyspan_pda_driver); 930 usb_serial_deregister(&keyspan_pda_device); 931#ifdef KEYSPAN 932 usb_serial_deregister(&keyspan_pda_fake_device); 933#endif 934#ifdef XIRCOM 935 usb_serial_deregister(&xircom_pgs_fake_device); 936#endif 937} 938 939 940module_init(keyspan_pda_init); 941module_exit(keyspan_pda_exit); 942 943MODULE_AUTHOR(DRIVER_AUTHOR); 944MODULE_DESCRIPTION(DRIVER_DESC); 945MODULE_LICENSE("GPL"); 946 947module_param(debug, bool, S_IRUGO | S_IWUSR); 948MODULE_PARM_DESC(debug, "Debug enabled or not"); 949