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