1 /* 2 * USB IR Dongle driver 3 * 4 * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com) 5 * Copyright (C) 2002 Gary Brubaker (xavyer@ix.netcom.com) 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This driver allows a USB IrDA device to be used as a "dumb" serial device. 13 * This can be useful if you do not have access to a full IrDA stack on the 14 * other side of the connection. If you do have an IrDA stack on both devices, 15 * please use the usb-irda driver, as it contains the proper error checking and 16 * other goodness of a full IrDA stack. 17 * 18 * Portions of this driver were taken from drivers/net/irda/irda-usb.c, which 19 * was written by Roman Weissgaerber <weissg@vienna.at>, Dag Brattli 20 * <dag@brattli.net>, and Jean Tourrilhes <jt@hpl.hp.com> 21 * 22 * See Documentation/usb/usb-serial.txt for more information on using this 23 * driver 24 * 25 * 2008_Jun_02 Felipe Balbi <me@felipebalbi.com> 26 * Introduced common header to be used also in USB Gadget Framework. 27 * Still needs some other style fixes. 28 * 29 * 2007_Jun_21 Alan Cox <alan@redhat.com> 30 * Minimal cleanups for some of the driver problens and tty layer abuse. 31 * Still needs fixing to allow multiple dongles. 32 * 33 * 2002_Mar_07 greg kh 34 * moved some needed structures and #define values from the 35 * net/irda/irda-usb.h file into our file, as we don't want to depend on 36 * that codebase compiling correctly :) 37 * 38 * 2002_Jan_14 gb 39 * Added module parameter to force specific number of XBOFs. 40 * Added ir_xbof_change(). 41 * Reorganized read_bulk_callback error handling. 42 * Switched from FILL_BULK_URB() to usb_fill_bulk_urb(). 43 * 44 * 2001_Nov_08 greg kh 45 * Changed the irda_usb_find_class_desc() function based on comments and 46 * code from Martin Diehl. 47 * 48 * 2001_Nov_01 greg kh 49 * Added support for more IrDA USB devices. 50 * Added support for zero packet. Added buffer override paramater, so 51 * users can transfer larger packets at once if they wish. Both patches 52 * came from Dag Brattli <dag@obexcode.com>. 53 * 54 * 2001_Oct_07 greg kh 55 * initial version released. 56 */ 57 58 #include <linux/kernel.h> 59 #include <linux/errno.h> 60 #include <linux/init.h> 61 #include <linux/slab.h> 62 #include <linux/tty.h> 63 #include <linux/tty_driver.h> 64 #include <linux/tty_flip.h> 65 #include <linux/module.h> 66 #include <linux/spinlock.h> 67 #include <linux/uaccess.h> 68 #include <linux/usb.h> 69 #include <linux/usb/serial.h> 70 #include <linux/usb/irda.h> 71 72 /* 73 * Version Information 74 */ 75 #define DRIVER_VERSION "v0.4" 76 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>" 77 #define DRIVER_DESC "USB IR Dongle driver" 78 79 static int debug; 80 81 /* if overridden by the user, then use their value for the size of the read and 82 * write urbs */ 83 static int buffer_size; 84 85 /* if overridden by the user, then use the specified number of XBOFs */ 86 static int xbof = -1; 87 88 static int ir_startup (struct usb_serial *serial); 89 static int ir_open(struct tty_struct *tty, struct usb_serial_port *port, 90 struct file *filep); 91 static void ir_close(struct tty_struct *tty, struct usb_serial_port *port, 92 struct file *filep); 93 static int ir_write(struct tty_struct *tty, struct usb_serial_port *port, 94 const unsigned char *buf, int count); 95 static void ir_write_bulk_callback (struct urb *urb); 96 static void ir_read_bulk_callback (struct urb *urb); 97 static void ir_set_termios(struct tty_struct *tty, 98 struct usb_serial_port *port, struct ktermios *old_termios); 99 100 /* Not that this lot means you can only have one per system */ 101 static u8 ir_baud; 102 static u8 ir_xbof; 103 static u8 ir_add_bof; 104 105 static struct usb_device_id ir_id_table[] = { 106 { USB_DEVICE(0x050f, 0x0180) }, /* KC Technology, KC-180 */ 107 { USB_DEVICE(0x08e9, 0x0100) }, /* XTNDAccess */ 108 { USB_DEVICE(0x09c4, 0x0011) }, /* ACTiSys ACT-IR2000U */ 109 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) }, 110 { } /* Terminating entry */ 111 }; 112 113 MODULE_DEVICE_TABLE(usb, ir_id_table); 114 115 static struct usb_driver ir_driver = { 116 .name = "ir-usb", 117 .probe = usb_serial_probe, 118 .disconnect = usb_serial_disconnect, 119 .id_table = ir_id_table, 120 .no_dynamic_id = 1, 121 }; 122 123 static struct usb_serial_driver ir_device = { 124 .driver = { 125 .owner = THIS_MODULE, 126 .name = "ir-usb", 127 }, 128 .description = "IR Dongle", 129 .usb_driver = &ir_driver, 130 .id_table = ir_id_table, 131 .num_ports = 1, 132 .set_termios = ir_set_termios, 133 .attach = ir_startup, 134 .open = ir_open, 135 .close = ir_close, 136 .write = ir_write, 137 .write_bulk_callback = ir_write_bulk_callback, 138 .read_bulk_callback = ir_read_bulk_callback, 139 }; 140 141 static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc) 142 { 143 dbg("bLength=%x", desc->bLength); 144 dbg("bDescriptorType=%x", desc->bDescriptorType); 145 dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision)); 146 dbg("bmDataSize=%x", desc->bmDataSize); 147 dbg("bmWindowSize=%x", desc->bmWindowSize); 148 dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime); 149 dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate)); 150 dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs); 151 dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff); 152 dbg("bMaxUnicastList=%x", desc->bMaxUnicastList); 153 } 154 155 /*------------------------------------------------------------------*/ 156 /* 157 * Function irda_usb_find_class_desc(dev, ifnum) 158 * 159 * Returns instance of IrDA class descriptor, or NULL if not found 160 * 161 * The class descriptor is some extra info that IrDA USB devices will 162 * offer to us, describing their IrDA characteristics. We will use that in 163 * irda_usb_init_qos() 164 * 165 * Based on the same function in drivers/net/irda/irda-usb.c 166 */ 167 static struct usb_irda_cs_descriptor * 168 irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum) 169 { 170 struct usb_irda_cs_descriptor *desc; 171 int ret; 172 173 desc = kzalloc(sizeof(*desc), GFP_KERNEL); 174 if (!desc) 175 return NULL; 176 177 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 178 USB_REQ_CS_IRDA_GET_CLASS_DESC, 179 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 180 0, ifnum, desc, sizeof(*desc), 1000); 181 182 dbg("%s - ret=%d", __func__, ret); 183 if (ret < sizeof(*desc)) { 184 dbg("%s - class descriptor read %s (%d)", 185 __func__, 186 (ret < 0) ? "failed" : "too short", 187 ret); 188 goto error; 189 } 190 if (desc->bDescriptorType != USB_DT_CS_IRDA) { 191 dbg("%s - bad class descriptor type", __func__); 192 goto error; 193 } 194 195 irda_usb_dump_class_desc(desc); 196 return desc; 197 198 error: 199 kfree(desc); 200 return NULL; 201 } 202 203 204 static u8 ir_xbof_change(u8 xbof) 205 { 206 u8 result; 207 208 /* reference irda-usb.c */ 209 switch (xbof) { 210 case 48: 211 result = 0x10; 212 break; 213 case 28: 214 case 24: 215 result = 0x20; 216 break; 217 default: 218 case 12: 219 result = 0x30; 220 break; 221 case 5: 222 case 6: 223 result = 0x40; 224 break; 225 case 3: 226 result = 0x50; 227 break; 228 case 2: 229 result = 0x60; 230 break; 231 case 1: 232 result = 0x70; 233 break; 234 case 0: 235 result = 0x80; 236 break; 237 } 238 239 return(result); 240 } 241 242 243 static int ir_startup(struct usb_serial *serial) 244 { 245 struct usb_irda_cs_descriptor *irda_desc; 246 247 irda_desc = irda_usb_find_class_desc(serial->dev, 0); 248 if (!irda_desc) { 249 dev_err(&serial->dev->dev, 250 "IRDA class descriptor not found, device not bound\n"); 251 return -ENODEV; 252 } 253 254 dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s", 255 __func__, 256 (irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "", 257 (irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "", 258 (irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "", 259 (irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "", 260 (irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "", 261 (irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "", 262 (irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "", 263 (irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "", 264 (irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : ""); 265 266 switch (irda_desc->bmAdditionalBOFs) { 267 case USB_IRDA_AB_48: 268 ir_add_bof = 48; 269 break; 270 case USB_IRDA_AB_24: 271 ir_add_bof = 24; 272 break; 273 case USB_IRDA_AB_12: 274 ir_add_bof = 12; 275 break; 276 case USB_IRDA_AB_6: 277 ir_add_bof = 6; 278 break; 279 case USB_IRDA_AB_3: 280 ir_add_bof = 3; 281 break; 282 case USB_IRDA_AB_2: 283 ir_add_bof = 2; 284 break; 285 case USB_IRDA_AB_1: 286 ir_add_bof = 1; 287 break; 288 case USB_IRDA_AB_0: 289 ir_add_bof = 0; 290 break; 291 default: 292 break; 293 } 294 295 kfree(irda_desc); 296 297 return 0; 298 } 299 300 static int ir_open(struct tty_struct *tty, 301 struct usb_serial_port *port, struct file *filp) 302 { 303 char *buffer; 304 int result = 0; 305 306 dbg("%s - port %d", __func__, port->number); 307 308 if (buffer_size) { 309 /* override the default buffer sizes */ 310 buffer = kmalloc(buffer_size, GFP_KERNEL); 311 if (!buffer) { 312 dev_err(&port->dev, "%s - out of memory.\n", __func__); 313 return -ENOMEM; 314 } 315 kfree(port->read_urb->transfer_buffer); 316 port->read_urb->transfer_buffer = buffer; 317 port->read_urb->transfer_buffer_length = buffer_size; 318 319 buffer = kmalloc(buffer_size, GFP_KERNEL); 320 if (!buffer) { 321 dev_err(&port->dev, "%s - out of memory.\n", __func__); 322 return -ENOMEM; 323 } 324 kfree(port->write_urb->transfer_buffer); 325 port->write_urb->transfer_buffer = buffer; 326 port->write_urb->transfer_buffer_length = buffer_size; 327 port->bulk_out_size = buffer_size; 328 } 329 330 /* Start reading from the device */ 331 usb_fill_bulk_urb( 332 port->read_urb, 333 port->serial->dev, 334 usb_rcvbulkpipe(port->serial->dev, 335 port->bulk_in_endpointAddress), 336 port->read_urb->transfer_buffer, 337 port->read_urb->transfer_buffer_length, 338 ir_read_bulk_callback, 339 port); 340 result = usb_submit_urb(port->read_urb, GFP_KERNEL); 341 if (result) 342 dev_err(&port->dev, 343 "%s - failed submitting read urb, error %d\n", 344 __func__, result); 345 346 return result; 347 } 348 349 static void ir_close(struct tty_struct *tty, 350 struct usb_serial_port *port, struct file * filp) 351 { 352 dbg("%s - port %d", __func__, port->number); 353 354 /* shutdown our bulk read */ 355 usb_kill_urb(port->read_urb); 356 } 357 358 static int ir_write(struct tty_struct *tty, struct usb_serial_port *port, 359 const unsigned char *buf, int count) 360 { 361 unsigned char *transfer_buffer; 362 int result; 363 int transfer_size; 364 365 dbg("%s - port = %d, count = %d", __func__, port->number, count); 366 367 if (count == 0) 368 return 0; 369 370 spin_lock_bh(&port->lock); 371 if (port->write_urb_busy) { 372 spin_unlock_bh(&port->lock); 373 dbg("%s - already writing", __func__); 374 return 0; 375 } 376 port->write_urb_busy = 1; 377 spin_unlock_bh(&port->lock); 378 379 transfer_buffer = port->write_urb->transfer_buffer; 380 transfer_size = min(count, port->bulk_out_size - 1); 381 382 /* 383 * The first byte of the packet we send to the device contains an 384 * inbound header which indicates an additional number of BOFs and 385 * a baud rate change. 386 * 387 * See section 5.4.2.2 of the USB IrDA spec. 388 */ 389 *transfer_buffer = ir_xbof | ir_baud; 390 ++transfer_buffer; 391 392 memcpy(transfer_buffer, buf, transfer_size); 393 394 usb_fill_bulk_urb( 395 port->write_urb, 396 port->serial->dev, 397 usb_sndbulkpipe(port->serial->dev, 398 port->bulk_out_endpointAddress), 399 port->write_urb->transfer_buffer, 400 transfer_size + 1, 401 ir_write_bulk_callback, 402 port); 403 404 port->write_urb->transfer_flags = URB_ZERO_PACKET; 405 406 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 407 if (result) { 408 port->write_urb_busy = 0; 409 dev_err(&port->dev, 410 "%s - failed submitting write urb, error %d\n", 411 __func__, result); 412 } else 413 result = transfer_size; 414 415 return result; 416 } 417 418 static void ir_write_bulk_callback(struct urb *urb) 419 { 420 struct usb_serial_port *port = urb->context; 421 int status = urb->status; 422 423 dbg("%s - port %d", __func__, port->number); 424 425 port->write_urb_busy = 0; 426 if (status) { 427 dbg("%s - nonzero write bulk status received: %d", 428 __func__, status); 429 return; 430 } 431 432 usb_serial_debug_data( 433 debug, 434 &port->dev, 435 __func__, 436 urb->actual_length, 437 urb->transfer_buffer); 438 439 usb_serial_port_softint(port); 440 } 441 442 static void ir_read_bulk_callback(struct urb *urb) 443 { 444 struct usb_serial_port *port = urb->context; 445 struct tty_struct *tty; 446 unsigned char *data = urb->transfer_buffer; 447 int result; 448 int status = urb->status; 449 450 dbg("%s - port %d", __func__, port->number); 451 452 if (!port->port.count) { 453 dbg("%s - port closed.", __func__); 454 return; 455 } 456 457 switch (status) { 458 case 0: /* Successful */ 459 /* 460 * The first byte of the packet we get from the device 461 * contains a busy indicator and baud rate change. 462 * See section 5.4.1.2 of the USB IrDA spec. 463 */ 464 if ((*data & 0x0f) > 0) 465 ir_baud = *data & 0x0f; 466 usb_serial_debug_data(debug, &port->dev, __func__, 467 urb->actual_length, data); 468 tty = port->port.tty; 469 if (tty_buffer_request_room(tty, urb->actual_length - 1)) { 470 tty_insert_flip_string(tty, data+1, urb->actual_length - 1); 471 tty_flip_buffer_push(tty); 472 } 473 474 /* 475 * No break here. 476 * We want to resubmit the urb so we can read 477 * again. 478 */ 479 480 case -EPROTO: /* taking inspiration from pl2303.c */ 481 /* Continue trying to always read */ 482 usb_fill_bulk_urb( 483 port->read_urb, 484 port->serial->dev, 485 usb_rcvbulkpipe(port->serial->dev, 486 port->bulk_in_endpointAddress), 487 port->read_urb->transfer_buffer, 488 port->read_urb->transfer_buffer_length, 489 ir_read_bulk_callback, 490 port); 491 492 result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 493 if (result) 494 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n", 495 __func__, result); 496 break ; 497 default: 498 dbg("%s - nonzero read bulk status received: %d", 499 __func__, status); 500 break ; 501 } 502 return; 503 } 504 505 static void ir_set_termios(struct tty_struct *tty, 506 struct usb_serial_port *port, struct ktermios *old_termios) 507 { 508 unsigned char *transfer_buffer; 509 int result; 510 speed_t baud; 511 int ir_baud; 512 513 dbg("%s - port %d", __func__, port->number); 514 515 baud = tty_get_baud_rate(tty); 516 517 /* 518 * FIXME, we should compare the baud request against the 519 * capability stated in the IR header that we got in the 520 * startup function. 521 */ 522 523 switch (baud) { 524 case 2400: 525 ir_baud = USB_IRDA_BR_2400; 526 break; 527 case 9600: 528 ir_baud = USB_IRDA_BR_9600; 529 break; 530 case 19200: 531 ir_baud = USB_IRDA_BR_19200; 532 break; 533 case 38400: 534 ir_baud = USB_IRDA_BR_38400; 535 break; 536 case 57600: 537 ir_baud = USB_IRDA_BR_57600; 538 break; 539 case 115200: 540 ir_baud = USB_IRDA_BR_115200; 541 break; 542 case 576000: 543 ir_baud = USB_IRDA_BR_576000; 544 break; 545 case 1152000: 546 ir_baud = USB_IRDA_BR_1152000; 547 break; 548 case 4000000: 549 ir_baud = USB_IRDA_BR_4000000; 550 break; 551 default: 552 ir_baud = USB_IRDA_BR_9600; 553 baud = 9600; 554 } 555 556 if (xbof == -1) 557 ir_xbof = ir_xbof_change(ir_add_bof); 558 else 559 ir_xbof = ir_xbof_change(xbof) ; 560 561 /* FIXME need to check to see if our write urb is busy right 562 * now, or use a urb pool. 563 * 564 * send the baud change out on an "empty" data packet 565 */ 566 transfer_buffer = port->write_urb->transfer_buffer; 567 *transfer_buffer = ir_xbof | ir_baud; 568 569 usb_fill_bulk_urb( 570 port->write_urb, 571 port->serial->dev, 572 usb_sndbulkpipe(port->serial->dev, 573 port->bulk_out_endpointAddress), 574 port->write_urb->transfer_buffer, 575 1, 576 ir_write_bulk_callback, 577 port); 578 579 port->write_urb->transfer_flags = URB_ZERO_PACKET; 580 581 result = usb_submit_urb(port->write_urb, GFP_KERNEL); 582 if (result) 583 dev_err(&port->dev, 584 "%s - failed submitting write urb, error %d\n", 585 __func__, result); 586 587 /* Only speed changes are supported */ 588 tty_termios_copy_hw(tty->termios, old_termios); 589 tty_encode_baud_rate(tty, baud, baud); 590 } 591 592 static int __init ir_init(void) 593 { 594 int retval; 595 596 retval = usb_serial_register(&ir_device); 597 if (retval) 598 goto failed_usb_serial_register; 599 600 retval = usb_register(&ir_driver); 601 if (retval) 602 goto failed_usb_register; 603 604 info(DRIVER_DESC " " DRIVER_VERSION); 605 606 return 0; 607 608 failed_usb_register: 609 usb_serial_deregister(&ir_device); 610 611 failed_usb_serial_register: 612 return retval; 613 } 614 615 static void __exit ir_exit(void) 616 { 617 usb_deregister(&ir_driver); 618 usb_serial_deregister(&ir_device); 619 } 620 621 622 module_init(ir_init); 623 module_exit(ir_exit); 624 625 MODULE_AUTHOR(DRIVER_AUTHOR); 626 MODULE_DESCRIPTION(DRIVER_DESC); 627 MODULE_LICENSE("GPL"); 628 629 module_param(debug, bool, S_IRUGO | S_IWUSR); 630 MODULE_PARM_DESC(debug, "Debug enabled or not"); 631 module_param(xbof, int, 0); 632 MODULE_PARM_DESC(xbof, "Force specific number of XBOFs"); 633 module_param(buffer_size, int, 0); 634 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers"); 635 636