linux/drivers/usb/serial/ir-usb.c History
<<
>>
Prefs
   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@lxorguk.ukuu.org.uk>
  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
  79static int debug;
  80
  81/* if overridden by the user, then use their value for the size of the read and
  82 * write urbs */
  83static int buffer_size;
  84
  85/* if overridden by the user, then use the specified number of XBOFs */
  86static int xbof = -1;
  87
  88static int  ir_startup (struct usb_serial *serial);
  89static int  ir_open(struct tty_struct *tty, struct usb_serial_port *port);
  90static void ir_close(struct usb_serial_port *port);
  91static int  ir_write(struct tty_struct *tty, struct usb_serial_port *port,
  92                                        const unsigned char *buf, int count);
  93static void ir_write_bulk_callback (struct urb *urb);
  94static void ir_read_bulk_callback (struct urb *urb);
  95static void ir_set_termios(struct tty_struct *tty,
  96                struct usb_serial_port *port, struct ktermios *old_termios);
  97
  98/* Not that this lot means you can only have one per system */
  99static u8 ir_baud;
 100static u8 ir_xbof;
 101static u8 ir_add_bof;
 102
 103static const struct usb_device_id ir_id_table[] = {
 104        { USB_DEVICE(0x050f, 0x0180) },         /* KC Technology, KC-180 */
 105        { USB_DEVICE(0x08e9, 0x0100) },         /* XTNDAccess */
 106        { USB_DEVICE(0x09c4, 0x0011) },         /* ACTiSys ACT-IR2000U */
 107        { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, USB_SUBCLASS_IRDA, 0) },
 108        { }                                     /* Terminating entry */
 109};
 110
 111MODULE_DEVICE_TABLE(usb, ir_id_table);
 112
 113static struct usb_driver ir_driver = {
 114        .name           = "ir-usb",
 115        .probe          = usb_serial_probe,
 116        .disconnect     = usb_serial_disconnect,
 117        .id_table       = ir_id_table,
 118        .no_dynamic_id  = 1,
 119};
 120
 121static struct usb_serial_driver ir_device = {
 122        .driver = {
 123                .owner  = THIS_MODULE,
 124                .name   = "ir-usb",
 125        },
 126        .description            = "IR Dongle",
 127        .usb_driver             = &ir_driver,
 128        .id_table               = ir_id_table,
 129        .num_ports              = 1,
 130        .set_termios            = ir_set_termios,
 131        .attach                 = ir_startup,
 132        .open                   = ir_open,
 133        .close                  = ir_close,
 134        .write                  = ir_write,
 135        .write_bulk_callback    = ir_write_bulk_callback,
 136        .read_bulk_callback     = ir_read_bulk_callback,
 137};
 138
 139static inline void irda_usb_dump_class_desc(struct usb_irda_cs_descriptor *desc)
 140{
 141        dbg("bLength=%x", desc->bLength);
 142        dbg("bDescriptorType=%x", desc->bDescriptorType);
 143        dbg("bcdSpecRevision=%x", __le16_to_cpu(desc->bcdSpecRevision));
 144        dbg("bmDataSize=%x", desc->bmDataSize);
 145        dbg("bmWindowSize=%x", desc->bmWindowSize);
 146        dbg("bmMinTurnaroundTime=%d", desc->bmMinTurnaroundTime);
 147        dbg("wBaudRate=%x", __le16_to_cpu(desc->wBaudRate));
 148        dbg("bmAdditionalBOFs=%x", desc->bmAdditionalBOFs);
 149        dbg("bIrdaRateSniff=%x", desc->bIrdaRateSniff);
 150        dbg("bMaxUnicastList=%x", desc->bMaxUnicastList);
 151}
 152
 153/*------------------------------------------------------------------*/
 154/*
 155 * Function irda_usb_find_class_desc(dev, ifnum)
 156 *
 157 *    Returns instance of IrDA class descriptor, or NULL if not found
 158 *
 159 * The class descriptor is some extra info that IrDA USB devices will
 160 * offer to us, describing their IrDA characteristics. We will use that in
 161 * irda_usb_init_qos()
 162 *
 163 * Based on the same function in drivers/net/irda/irda-usb.c
 164 */
 165static struct usb_irda_cs_descriptor *
 166irda_usb_find_class_desc(struct usb_device *dev, unsigned int ifnum)
 167{
 168        struct usb_irda_cs_descriptor *desc;
 169        int ret;
 170
 171        desc = kzalloc(sizeof(*desc), GFP_KERNEL);
 172        if (!desc)
 173                return NULL;
 174
 175        ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
 176                        USB_REQ_CS_IRDA_GET_CLASS_DESC,
 177                        USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
 178                        0, ifnum, desc, sizeof(*desc), 1000);
 179
 180        dbg("%s -  ret=%d", __func__, ret);
 181        if (ret < sizeof(*desc)) {
 182                dbg("%s - class descriptor read %s (%d)",
 183                                __func__,
 184                                (ret < 0) ? "failed" : "too short",
 185                                ret);
 186                goto error;
 187        }
 188        if (desc->bDescriptorType != USB_DT_CS_IRDA) {
 189                dbg("%s - bad class descriptor type", __func__);
 190                goto error;
 191        }
 192
 193        irda_usb_dump_class_desc(desc);
 194        return desc;
 195
 196error:
 197        kfree(desc);
 198        return NULL;
 199}
 200
 201
 202static u8 ir_xbof_change(u8 xbof)
 203{
 204        u8 result;
 205
 206        /* reference irda-usb.c */
 207        switch (xbof) {
 208        case 48:
 209                result = 0x10;
 210                break;
 211        case 28:
 212        case 24:
 213                result = 0x20;
 214                break;
 215        default:
 216        case 12:
 217                result = 0x30;
 218                break;
 219        case  5:
 220        case  6:
 221                result = 0x40;
 222                break;
 223        case  3:
 224                result = 0x50;
 225                break;
 226        case  2:
 227                result = 0x60;
 228                break;
 229        case  1:
 230                result = 0x70;
 231                break;
 232        case  0:
 233                result = 0x80;
 234                break;
 235        }
 236
 237        return(result);
 238}
 239
 240
 241static int ir_startup(struct usb_serial *serial)
 242{
 243        struct usb_irda_cs_descriptor *irda_desc;
 244
 245        irda_desc = irda_usb_find_class_desc(serial->dev, 0);
 246        if (!irda_desc) {
 247                dev_err(&serial->dev->dev,
 248                        "IRDA class descriptor not found, device not bound\n");
 249                return -ENODEV;
 250        }
 251
 252        dbg("%s - Baud rates supported:%s%s%s%s%s%s%s%s%s",
 253                __func__,
 254                (irda_desc->wBaudRate & USB_IRDA_BR_2400) ? " 2400" : "",
 255                (irda_desc->wBaudRate & USB_IRDA_BR_9600) ? " 9600" : "",
 256                (irda_desc->wBaudRate & USB_IRDA_BR_19200) ? " 19200" : "",
 257                (irda_desc->wBaudRate & USB_IRDA_BR_38400) ? " 38400" : "",
 258                (irda_desc->wBaudRate & USB_IRDA_BR_57600) ? " 57600" : "",
 259                (irda_desc->wBaudRate & USB_IRDA_BR_115200) ? " 115200" : "",
 260                (irda_desc->wBaudRate & USB_IRDA_BR_576000) ? " 576000" : "",
 261                (irda_desc->wBaudRate & USB_IRDA_BR_1152000) ? " 1152000" : "",
 262                (irda_desc->wBaudRate & USB_IRDA_BR_4000000) ? " 4000000" : "");
 263
 264        switch (irda_desc->bmAdditionalBOFs) {
 265        case USB_IRDA_AB_48:
 266                ir_add_bof = 48;
 267                break;
 268        case USB_IRDA_AB_24:
 269                ir_add_bof = 24;
 270                break;
 271        case USB_IRDA_AB_12:
 272                ir_add_bof = 12;
 273                break;
 274        case USB_IRDA_AB_6:
 275                ir_add_bof = 6;
 276                break;
 277        case USB_IRDA_AB_3:
 278                ir_add_bof = 3;
 279                break;
 280        case USB_IRDA_AB_2:
 281                ir_add_bof = 2;
 282                break;
 283        case USB_IRDA_AB_1:
 284                ir_add_bof = 1;
 285                break;
 286        case USB_IRDA_AB_0:
 287                ir_add_bof = 0;
 288                break;
 289        default:
 290                break;
 291        }
 292
 293        kfree(irda_desc);
 294
 295        return 0;
 296}
 297
 298static int ir_open(struct tty_struct *tty, struct usb_serial_port *port)
 299{
 300        char *buffer;
 301        int result = 0;
 302
 303        dbg("%s - port %d", __func__, port->number);
 304
 305        if (buffer_size) {
 306                /* override the default buffer sizes */
 307                buffer = kmalloc(buffer_size, GFP_KERNEL);
 308                if (!buffer) {
 309                        dev_err(&port->dev, "%s - out of memory.\n", __func__);
 310                        return -ENOMEM;
 311                }
 312                kfree(port->read_urb->transfer_buffer);
 313                port->read_urb->transfer_buffer = buffer;
 314                port->read_urb->transfer_buffer_length = buffer_size;
 315                port->bulk_in_buffer = buffer;
 316
 317                buffer = kmalloc(buffer_size, GFP_KERNEL);
 318                if (!buffer) {
 319                        dev_err(&port->dev, "%s - out of memory.\n", __func__);
 320                        return -ENOMEM;
 321                }
 322                kfree(port->write_urb->transfer_buffer);
 323                port->write_urb->transfer_buffer = buffer;
 324                port->write_urb->transfer_buffer_length = buffer_size;
 325                port->bulk_out_buffer = buffer;
 326                port->bulk_out_size = buffer_size;
 327        }
 328
 329        /* Start reading from the device */
 330        usb_fill_bulk_urb(
 331                port->read_urb,
 332                port->serial->dev,
 333                usb_rcvbulkpipe(port->serial->dev,
 334                        port->bulk_in_endpointAddress),
 335                port->read_urb->transfer_buffer,
 336                port->read_urb->transfer_buffer_length,
 337                ir_read_bulk_callback,
 338                port);
 339        result = usb_submit_urb(port->read_urb, GFP_KERNEL);
 340        if (result)
 341                dev_err(&port->dev,
 342                        "%s - failed submitting read urb, error %d\n",
 343                        __func__, result);
 344
 345        return result;
 346}
 347
 348static void ir_close(struct usb_serial_port *port)
 349{
 350        dbg("%s - port %d", __func__, port->number);
 351
 352        /* shutdown our bulk read */
 353        usb_kill_urb(port->read_urb);
 354}
 355
 356static int ir_write(struct tty_struct *tty, struct usb_serial_port *port,
 357                                        const unsigned char *buf, int count)
 358{
 359        unsigned char *transfer_buffer;
 360        int result;
 361        int transfer_size;
 362
 363        dbg("%s - port = %d, count = %d", __func__, port->number, count);
 364
 365        if (count == 0)
 366                return 0;
 367
 368        spin_lock_bh(&port->lock);
 369        if (port->write_urb_busy) {
 370                spin_unlock_bh(&port->lock);
 371                dbg("%s - already writing", __func__);
 372                return 0;
 373        }
 374        port->write_urb_busy = 1;
 375        spin_unlock_bh(&port->lock);
 376
 377        transfer_buffer = port->write_urb->transfer_buffer;
 378        transfer_size = min(count, port->bulk_out_size - 1);
 379
 380        /*
 381         * The first byte of the packet we send to the device contains an
 382         * inbound header which indicates an additional number of BOFs and
 383         * a baud rate change.
 384         *
 385         * See section 5.4.2.2 of the USB IrDA spec.
 386         */
 387        *transfer_buffer = ir_xbof | ir_baud;
 388        ++transfer_buffer;
 389
 390        memcpy(transfer_buffer, buf, transfer_size);
 391
 392        usb_fill_bulk_urb(
 393                port->write_urb,
 394                port->serial->dev,
 395                usb_sndbulkpipe(port->serial->dev,
 396                        port->bulk_out_endpointAddress),
 397                port->write_urb->transfer_buffer,
 398                transfer_size + 1,
 399                ir_write_bulk_callback,
 400                port);
 401
 402        port->write_urb->transfer_flags = URB_ZERO_PACKET;
 403
 404        result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
 405        if (result) {
 406                port->write_urb_busy = 0;
 407                dev_err(&port->dev,
 408                        "%s - failed submitting write urb, error %d\n",
 409                        __func__, result);
 410        } else
 411                result = transfer_size;
 412
 413        return result;
 414}
 415
 416static void ir_write_bulk_callback(struct urb *urb)
 417{
 418        struct usb_serial_port *port = urb->context;
 419        int status = urb->status;
 420
 421        dbg("%s - port %d", __func__, port->number);
 422
 423        port->write_urb_busy = 0;
 424        if (status) {
 425                dbg("%s - nonzero write bulk status received: %d",
 426                    __func__, status);
 427                return;
 428        }
 429
 430        usb_serial_debug_data(
 431                debug,
 432                &port->dev,
 433                __func__,
 434                urb->actual_length,
 435                urb->transfer_buffer);
 436
 437        usb_serial_port_softint(port);
 438}
 439
 440static void ir_read_bulk_callback(struct urb *urb)
 441{
 442        struct usb_serial_port *port = urb->context;
 443        struct tty_struct *tty;
 444        unsigned char *data = urb->transfer_buffer;
 445        int result;
 446        int status = urb->status;
 447
 448        dbg("%s - port %d", __func__, port->number);
 449
 450        switch (status) {
 451        case 0: /* Successful */
 452                /*
 453                 * The first byte of the packet we get from the device
 454                 * contains a busy indicator and baud rate change.
 455                 * See section 5.4.1.2 of the USB IrDA spec.
 456                 */
 457                if ((*data & 0x0f) > 0)
 458                        ir_baud = *data & 0x0f;
 459                usb_serial_debug_data(debug, &port->dev, __func__,
 460                                                urb->actual_length, data);
 461                tty = tty_port_tty_get(&port->port);
 462                tty_insert_flip_string(tty, data+1, urb->actual_length - 1);
 463                tty_flip_buffer_push(tty);
 464                tty_kref_put(tty);
 465
 466                /*
 467                 * No break here.
 468                 * We want to resubmit the urb so we can read
 469                 * again.
 470                 */
 471
 472        case -EPROTO: /* taking inspiration from pl2303.c */
 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                                __func__, result);
 488                        break ;
 489        default:
 490                dbg("%s - nonzero read bulk status received: %d",
 491                        __func__, status);
 492                break ;
 493        }
 494        return;
 495}
 496
 497static void ir_set_termios(struct tty_struct *tty,
 498                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", __func__, port->number);
 506
 507        baud = tty_get_baud_rate(tty);
 508
 509        /*
 510         * FIXME, we should compare the baud request against the
 511         * capability stated in the IR header that we got in the
 512         * startup function.
 513         */
 514
 515        switch (baud) {
 516        case 2400:
 517                ir_baud = USB_IRDA_BR_2400;
 518                break;
 519        case 9600:
 520                ir_baud = USB_IRDA_BR_9600;
 521                break;
 522        case 19200:
 523                ir_baud = USB_IRDA_BR_19200;
 524                break;
 525        case 38400:
 526                ir_baud = USB_IRDA_BR_38400;
 527                break;
 528        case 57600:
 529                ir_baud = USB_IRDA_BR_57600;
 530                break;
 531        case 115200:
 532                ir_baud = USB_IRDA_BR_115200;
 533                break;
 534        case 576000:
 535                ir_baud = USB_IRDA_BR_576000;
 536                break;
 537        case 1152000:
 538                ir_baud = USB_IRDA_BR_1152000;
 539                break;
 540        case 4000000:
 541                ir_baud = USB_IRDA_BR_4000000;
 542                break;
 543        default:
 544                ir_baud = USB_IRDA_BR_9600;
 545                baud = 9600;
 546        }
 547
 548        if (xbof == -1)
 549                ir_xbof = ir_xbof_change(ir_add_bof);
 550        else
 551                ir_xbof = ir_xbof_change(xbof) ;
 552
 553        /* FIXME need to check to see if our write urb is busy right
 554         * now, or use a urb pool.
 555         *
 556         * send the baud change out on an "empty" data packet
 557         */
 558        transfer_buffer = port->write_urb->transfer_buffer;
 559        *transfer_buffer = ir_xbof | ir_baud;
 560
 561        usb_fill_bulk_urb(
 562                port->write_urb,
 563                port->serial->dev,
 564                usb_sndbulkpipe(port->serial->dev,
 565                        port->bulk_out_endpointAddress),
 566                port->write_urb->transfer_buffer,
 567                1,
 568                ir_write_bulk_callback,
 569                port);
 570
 571        port->write_urb->transfer_flags = URB_ZERO_PACKET;
 572
 573        result = usb_submit_urb(port->write_urb, GFP_KERNEL);
 574        if (result)
 575                dev_err(&port->dev,
 576                                "%s - failed submitting write urb, error %d\n",
 577                                __func__, result);
 578
 579        /* Only speed changes are supported */
 580        tty_termios_copy_hw(tty->termios, old_termios);
 581        tty_encode_baud_rate(tty, baud, baud);
 582}
 583
 584static int __init ir_init(void)
 585{
 586        int retval;
 587
 588        retval = usb_serial_register(&ir_device);
 589        if (retval)
 590                goto failed_usb_serial_register;
 591
 592        retval = usb_register(&ir_driver);
 593        if (retval)
 594                goto failed_usb_register;
 595
 596        printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
 597               DRIVER_DESC "\n");
 598
 599        return 0;
 600
 601failed_usb_register:
 602        usb_serial_deregister(&ir_device);
 603
 604failed_usb_serial_register:
 605        return retval;
 606}
 607
 608static void __exit ir_exit(void)
 609{
 610        usb_deregister(&ir_driver);
 611        usb_serial_deregister(&ir_device);
 612}
 613
 614
 615module_init(ir_init);
 616module_exit(ir_exit);
 617
 618MODULE_AUTHOR(DRIVER_AUTHOR);
 619MODULE_DESCRIPTION(DRIVER_DESC);
 620MODULE_LICENSE("GPL");
 621
 622module_param(debug, bool, S_IRUGO | S_IWUSR);
 623MODULE_PARM_DESC(debug, "Debug enabled or not");
 624module_param(xbof, int, 0);
 625MODULE_PARM_DESC(xbof, "Force specific number of XBOFs");
 626module_param(buffer_size, int, 0);
 627MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers");
 628
 629
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.