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