linux/drivers/staging/media/lirc/lirc_imon.c
<<
>>
Prefs
   1/*
   2 *   lirc_imon.c:  LIRC/VFD/LCD driver for SoundGraph iMON IR/VFD/LCD
   3 *                 including the iMON PAD model
   4 *
   5 *   Copyright(C) 2004  Venky Raju(dev@venky.ws)
   6 *   Copyright(C) 2009  Jarod Wilson <jarod@wilsonet.com>
   7 *
   8 *   lirc_imon 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 *   This program is distributed in the hope that it will be useful,
  14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 *   GNU General Public License for more details.
  17 *
  18 *   You should have received a copy of the GNU General Public License
  19 *   along with this program; if not, write to the Free Software
  20 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21 */
  22
  23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24
  25#include <linux/errno.h>
  26#include <linux/init.h>
  27#include <linux/kernel.h>
  28#include <linux/module.h>
  29#include <linux/slab.h>
  30#include <linux/uaccess.h>
  31#include <linux/usb.h>
  32
  33#include <media/lirc.h>
  34#include <media/lirc_dev.h>
  35
  36
  37#define MOD_AUTHOR      "Venky Raju <dev@venky.ws>"
  38#define MOD_DESC        "Driver for SoundGraph iMON MultiMedia IR/Display"
  39#define MOD_NAME        "lirc_imon"
  40#define MOD_VERSION     "0.8"
  41
  42#define DISPLAY_MINOR_BASE      144
  43#define DEVICE_NAME     "lcd%d"
  44
  45#define BUF_CHUNK_SIZE  4
  46#define BUF_SIZE        128
  47
  48#define BIT_DURATION    250     /* each bit received is 250us */
  49
  50/*** P R O T O T Y P E S ***/
  51
  52/* USB Callback prototypes */
  53static int imon_probe(struct usb_interface *interface,
  54                      const struct usb_device_id *id);
  55static void imon_disconnect(struct usb_interface *interface);
  56static void usb_rx_callback(struct urb *urb);
  57static void usb_tx_callback(struct urb *urb);
  58
  59/* suspend/resume support */
  60static int imon_resume(struct usb_interface *intf);
  61static int imon_suspend(struct usb_interface *intf, pm_message_t message);
  62
  63/* Display file_operations function prototypes */
  64static int display_open(struct inode *inode, struct file *file);
  65static int display_close(struct inode *inode, struct file *file);
  66
  67/* VFD write operation */
  68static ssize_t vfd_write(struct file *file, const char __user *buf,
  69                         size_t n_bytes, loff_t *pos);
  70
  71/* LIRC driver function prototypes */
  72static int ir_open(void *data);
  73static void ir_close(void *data);
  74
  75/*** G L O B A L S ***/
  76#define IMON_DATA_BUF_SZ        35
  77
  78struct imon_context {
  79        struct usb_device *usbdev;
  80        /* Newer devices have two interfaces */
  81        int display;                    /* not all controllers do */
  82        int display_isopen;             /* display port has been opened */
  83        int ir_isopen;                  /* IR port open */
  84        int dev_present;                /* USB device presence */
  85        struct mutex ctx_lock;          /* to lock this object */
  86        wait_queue_head_t remove_ok;    /* For unexpected USB disconnects */
  87
  88        int vfd_proto_6p;               /* some VFD require a 6th packet */
  89
  90        struct lirc_driver *driver;
  91        struct usb_endpoint_descriptor *rx_endpoint;
  92        struct usb_endpoint_descriptor *tx_endpoint;
  93        struct urb *rx_urb;
  94        struct urb *tx_urb;
  95        unsigned char usb_rx_buf[8];
  96        unsigned char usb_tx_buf[8];
  97
  98        struct rx_data {
  99                int count;              /* length of 0 or 1 sequence */
 100                int prev_bit;           /* logic level of sequence */
 101                int initial_space;      /* initial space flag */
 102        } rx;
 103
 104        struct tx_t {
 105                unsigned char data_buf[IMON_DATA_BUF_SZ]; /* user data buffer */
 106                struct completion finished;     /* wait for write to finish */
 107                atomic_t busy;                  /* write in progress */
 108                int status;                     /* status of tx completion */
 109        } tx;
 110};
 111
 112static const struct file_operations display_fops = {
 113        .owner          = THIS_MODULE,
 114        .open           = &display_open,
 115        .write          = &vfd_write,
 116        .release        = &display_close,
 117        .llseek         = noop_llseek,
 118};
 119
 120/*
 121 * USB Device ID for iMON USB Control Boards
 122 *
 123 * The Windows drivers contain 6 different inf files, more or less one for
 124 * each new device until the 0x0034-0x0046 devices, which all use the same
 125 * driver. Some of the devices in the 34-46 range haven't been definitively
 126 * identified yet. Early devices have either a TriGem Computer, Inc. or a
 127 * Samsung vendor ID (0x0aa8 and 0x04e8 respectively), while all later
 128 * devices use the SoundGraph vendor ID (0x15c2).
 129 */
 130static struct usb_device_id imon_usb_id_table[] = {
 131        /* TriGem iMON (IR only) -- TG_iMON.inf */
 132        { USB_DEVICE(0x0aa8, 0x8001) },
 133
 134        /* SoundGraph iMON (IR only) -- sg_imon.inf */
 135        { USB_DEVICE(0x04e8, 0xff30) },
 136
 137        /* SoundGraph iMON VFD (IR & VFD) -- iMON_VFD.inf */
 138        { USB_DEVICE(0x0aa8, 0xffda) },
 139
 140        /* SoundGraph iMON SS (IR & VFD) -- iMON_SS.inf */
 141        { USB_DEVICE(0x15c2, 0xffda) },
 142
 143        {}
 144};
 145
 146/* Some iMON VFD models requires a 6th packet for VFD writes */
 147static struct usb_device_id vfd_proto_6p_list[] = {
 148        { USB_DEVICE(0x15c2, 0xffda) },
 149        {}
 150};
 151
 152/* Some iMON devices have no lcd/vfd, don't set one up */
 153static struct usb_device_id ir_only_list[] = {
 154        { USB_DEVICE(0x0aa8, 0x8001) },
 155        { USB_DEVICE(0x04e8, 0xff30) },
 156        {}
 157};
 158
 159/* USB Device data */
 160static struct usb_driver imon_driver = {
 161        .name           = MOD_NAME,
 162        .probe          = imon_probe,
 163        .disconnect     = imon_disconnect,
 164        .suspend        = imon_suspend,
 165        .resume         = imon_resume,
 166        .id_table       = imon_usb_id_table,
 167};
 168
 169static struct usb_class_driver imon_class = {
 170        .name           = DEVICE_NAME,
 171        .fops           = &display_fops,
 172        .minor_base     = DISPLAY_MINOR_BASE,
 173};
 174
 175/* to prevent races between open() and disconnect(), probing, etc */
 176static DEFINE_MUTEX(driver_lock);
 177
 178static int debug;
 179
 180/***  M O D U L E   C O D E ***/
 181
 182MODULE_AUTHOR(MOD_AUTHOR);
 183MODULE_DESCRIPTION(MOD_DESC);
 184MODULE_VERSION(MOD_VERSION);
 185MODULE_LICENSE("GPL");
 186MODULE_DEVICE_TABLE(usb, imon_usb_id_table);
 187module_param(debug, int, S_IRUGO | S_IWUSR);
 188MODULE_PARM_DESC(debug, "Debug messages: 0=no, 1=yes(default: no)");
 189
 190static void free_imon_context(struct imon_context *context)
 191{
 192        struct device *dev = context->driver->dev;
 193        usb_free_urb(context->tx_urb);
 194        usb_free_urb(context->rx_urb);
 195        lirc_buffer_free(context->driver->rbuf);
 196        kfree(context->driver->rbuf);
 197        kfree(context->driver);
 198        kfree(context);
 199
 200        dev_dbg(dev, "%s: iMON context freed\n", __func__);
 201}
 202
 203static void deregister_from_lirc(struct imon_context *context)
 204{
 205        int retval;
 206        int minor = context->driver->minor;
 207
 208        retval = lirc_unregister_driver(minor);
 209        if (retval)
 210                dev_err(&context->usbdev->dev,
 211                        ": %s: unable to deregister from lirc(%d)",
 212                        __func__, retval);
 213        else
 214                dev_info(&context->usbdev->dev,
 215                         "Deregistered iMON driver (minor:%d)\n", minor);
 216
 217}
 218
 219/**
 220 * Called when the Display device (e.g. /dev/lcd0)
 221 * is opened by the application.
 222 */
 223static int display_open(struct inode *inode, struct file *file)
 224{
 225        struct usb_interface *interface;
 226        struct imon_context *context = NULL;
 227        int subminor;
 228        int retval = 0;
 229
 230        /* prevent races with disconnect */
 231        mutex_lock(&driver_lock);
 232
 233        subminor = iminor(inode);
 234        interface = usb_find_interface(&imon_driver, subminor);
 235        if (!interface) {
 236                pr_err("%s: could not find interface for minor %d\n",
 237                       __func__, subminor);
 238                retval = -ENODEV;
 239                goto exit;
 240        }
 241        context = usb_get_intfdata(interface);
 242
 243        if (!context) {
 244                dev_err(&interface->dev,
 245                        "%s: no context found for minor %d\n",
 246                        __func__, subminor);
 247                retval = -ENODEV;
 248                goto exit;
 249        }
 250
 251        mutex_lock(&context->ctx_lock);
 252
 253        if (!context->display) {
 254                dev_err(&interface->dev,
 255                        "%s: display not supported by device\n", __func__);
 256                retval = -ENODEV;
 257        } else if (context->display_isopen) {
 258                dev_err(&interface->dev,
 259                        "%s: display port is already open\n", __func__);
 260                retval = -EBUSY;
 261        } else {
 262                context->display_isopen = 1;
 263                file->private_data = context;
 264                dev_info(context->driver->dev, "display port opened\n");
 265        }
 266
 267        mutex_unlock(&context->ctx_lock);
 268
 269exit:
 270        mutex_unlock(&driver_lock);
 271        return retval;
 272}
 273
 274/**
 275 * Called when the display device (e.g. /dev/lcd0)
 276 * is closed by the application.
 277 */
 278static int display_close(struct inode *inode, struct file *file)
 279{
 280        struct imon_context *context = NULL;
 281        int retval = 0;
 282
 283        context = file->private_data;
 284
 285        if (!context) {
 286                pr_err("%s: no context for device\n", __func__);
 287                return -ENODEV;
 288        }
 289
 290        mutex_lock(&context->ctx_lock);
 291
 292        if (!context->display) {
 293                dev_err(&context->usbdev->dev,
 294                        "%s: display not supported by device\n", __func__);
 295                retval = -ENODEV;
 296        } else if (!context->display_isopen) {
 297                dev_err(&context->usbdev->dev,
 298                        "%s: display is not open\n", __func__);
 299                retval = -EIO;
 300        } else {
 301                context->display_isopen = 0;
 302                dev_info(context->driver->dev, "display port closed\n");
 303                if (!context->dev_present && !context->ir_isopen) {
 304                        /*
 305                         * Device disconnected before close and IR port is not
 306                         * open. If IR port is open, context will be deleted by
 307                         * ir_close.
 308                         */
 309                        mutex_unlock(&context->ctx_lock);
 310                        free_imon_context(context);
 311                        return retval;
 312                }
 313        }
 314
 315        mutex_unlock(&context->ctx_lock);
 316        return retval;
 317}
 318
 319/**
 320 * Sends a packet to the device -- this function must be called
 321 * with context->ctx_lock held.
 322 */
 323static int send_packet(struct imon_context *context)
 324{
 325        unsigned int pipe;
 326        int interval = 0;
 327        int retval = 0;
 328
 329        /* Check if we need to use control or interrupt urb */
 330        pipe = usb_sndintpipe(context->usbdev,
 331                              context->tx_endpoint->bEndpointAddress);
 332        interval = context->tx_endpoint->bInterval;
 333
 334        usb_fill_int_urb(context->tx_urb, context->usbdev, pipe,
 335                         context->usb_tx_buf,
 336                         sizeof(context->usb_tx_buf),
 337                         usb_tx_callback, context, interval);
 338
 339        context->tx_urb->actual_length = 0;
 340
 341        init_completion(&context->tx.finished);
 342        atomic_set(&(context->tx.busy), 1);
 343
 344        retval = usb_submit_urb(context->tx_urb, GFP_KERNEL);
 345        if (retval) {
 346                atomic_set(&(context->tx.busy), 0);
 347                dev_err(&context->usbdev->dev,
 348                        "%s: error submitting urb(%d)\n", __func__, retval);
 349        } else {
 350                /* Wait for transmission to complete (or abort) */
 351                mutex_unlock(&context->ctx_lock);
 352                retval = wait_for_completion_interruptible(
 353                                &context->tx.finished);
 354                if (retval)
 355                        dev_err(&context->usbdev->dev,
 356                                "%s: task interrupted\n", __func__);
 357                mutex_lock(&context->ctx_lock);
 358
 359                retval = context->tx.status;
 360                if (retval)
 361                        dev_err(&context->usbdev->dev,
 362                                "%s: packet tx failed (%d)\n",
 363                                __func__, retval);
 364        }
 365
 366        return retval;
 367}
 368
 369/**
 370 * Writes data to the VFD.  The iMON VFD is 2x16 characters
 371 * and requires data in 5 consecutive USB interrupt packets,
 372 * each packet but the last carrying 7 bytes.
 373 *
 374 * I don't know if the VFD board supports features such as
 375 * scrolling, clearing rows, blanking, etc. so at
 376 * the caller must provide a full screen of data.  If fewer
 377 * than 32 bytes are provided spaces will be appended to
 378 * generate a full screen.
 379 */
 380static ssize_t vfd_write(struct file *file, const char __user *buf,
 381                         size_t n_bytes, loff_t *pos)
 382{
 383        int i;
 384        int offset;
 385        int seq;
 386        int retval = 0;
 387        struct imon_context *context;
 388        const unsigned char vfd_packet6[] = {
 389                0x01, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF };
 390        int *data_buf = NULL;
 391
 392        context = file->private_data;
 393        if (!context) {
 394                pr_err("%s: no context for device\n", __func__);
 395                return -ENODEV;
 396        }
 397
 398        mutex_lock(&context->ctx_lock);
 399
 400        if (!context->dev_present) {
 401                dev_err(&context->usbdev->dev,
 402                        "%s: no iMON device present\n", __func__);
 403                retval = -ENODEV;
 404                goto exit;
 405        }
 406
 407        if (n_bytes <= 0 || n_bytes > IMON_DATA_BUF_SZ - 3) {
 408                dev_err(&context->usbdev->dev,
 409                        "%s: invalid payload size\n", __func__);
 410                retval = -EINVAL;
 411                goto exit;
 412        }
 413
 414        data_buf = memdup_user(buf, n_bytes);
 415        if (IS_ERR(data_buf)) {
 416                retval = PTR_ERR(data_buf);
 417                goto exit;
 418        }
 419
 420        memcpy(context->tx.data_buf, data_buf, n_bytes);
 421
 422        /* Pad with spaces */
 423        for (i = n_bytes; i < IMON_DATA_BUF_SZ - 3; ++i)
 424                context->tx.data_buf[i] = ' ';
 425
 426        for (i = IMON_DATA_BUF_SZ - 3; i < IMON_DATA_BUF_SZ; ++i)
 427                context->tx.data_buf[i] = 0xFF;
 428
 429        offset = 0;
 430        seq = 0;
 431
 432        do {
 433                memcpy(context->usb_tx_buf, context->tx.data_buf + offset, 7);
 434                context->usb_tx_buf[7] = (unsigned char) seq;
 435
 436                retval = send_packet(context);
 437                if (retval) {
 438                        dev_err(&context->usbdev->dev,
 439                                "%s: send packet failed for packet #%d\n",
 440                                __func__, seq/2);
 441                        goto exit;
 442                } else {
 443                        seq += 2;
 444                        offset += 7;
 445                }
 446
 447        } while (offset < IMON_DATA_BUF_SZ);
 448
 449        if (context->vfd_proto_6p) {
 450                /* Send packet #6 */
 451                memcpy(context->usb_tx_buf, &vfd_packet6, sizeof(vfd_packet6));
 452                context->usb_tx_buf[7] = (unsigned char) seq;
 453                retval = send_packet(context);
 454                if (retval)
 455                        dev_err(&context->usbdev->dev,
 456                                "%s: send packet failed for packet #%d\n",
 457                                        __func__, seq/2);
 458        }
 459
 460exit:
 461        mutex_unlock(&context->ctx_lock);
 462        kfree(data_buf);
 463
 464        return (!retval) ? n_bytes : retval;
 465}
 466
 467/**
 468 * Callback function for USB core API: transmit data
 469 */
 470static void usb_tx_callback(struct urb *urb)
 471{
 472        struct imon_context *context;
 473
 474        if (!urb)
 475                return;
 476        context = (struct imon_context *)urb->context;
 477        if (!context)
 478                return;
 479
 480        context->tx.status = urb->status;
 481
 482        /* notify waiters that write has finished */
 483        atomic_set(&context->tx.busy, 0);
 484        complete(&context->tx.finished);
 485
 486        return;
 487}
 488
 489/**
 490 * Called by lirc_dev when the application opens /dev/lirc
 491 */
 492static int ir_open(void *data)
 493{
 494        int retval = 0;
 495        struct imon_context *context;
 496
 497        /* prevent races with disconnect */
 498        mutex_lock(&driver_lock);
 499
 500        context = (struct imon_context *)data;
 501
 502        /* initial IR protocol decode variables */
 503        context->rx.count = 0;
 504        context->rx.initial_space = 1;
 505        context->rx.prev_bit = 0;
 506
 507        context->ir_isopen = 1;
 508        dev_info(context->driver->dev, "IR port opened\n");
 509
 510        mutex_unlock(&driver_lock);
 511        return retval;
 512}
 513
 514/**
 515 * Called by lirc_dev when the application closes /dev/lirc
 516 */
 517static void ir_close(void *data)
 518{
 519        struct imon_context *context;
 520
 521        context = (struct imon_context *)data;
 522        if (!context) {
 523                pr_err("%s: no context for device\n", __func__);
 524                return;
 525        }
 526
 527        mutex_lock(&context->ctx_lock);
 528
 529        context->ir_isopen = 0;
 530        dev_info(context->driver->dev, "IR port closed\n");
 531
 532        if (!context->dev_present) {
 533                /*
 534                 * Device disconnected while IR port was still open. Driver
 535                 * was not deregistered at disconnect time, so do it now.
 536                 */
 537                deregister_from_lirc(context);
 538
 539                if (!context->display_isopen) {
 540                        mutex_unlock(&context->ctx_lock);
 541                        free_imon_context(context);
 542                        return;
 543                }
 544                /*
 545                 * If display port is open, context will be deleted by
 546                 * display_close
 547                 */
 548        }
 549
 550        mutex_unlock(&context->ctx_lock);
 551        return;
 552}
 553
 554/**
 555 * Convert bit count to time duration (in us) and submit
 556 * the value to lirc_dev.
 557 */
 558static void submit_data(struct imon_context *context)
 559{
 560        unsigned char buf[4];
 561        int value = context->rx.count;
 562        int i;
 563
 564        dev_dbg(context->driver->dev, "submitting data to LIRC\n");
 565
 566        value *= BIT_DURATION;
 567        value &= PULSE_MASK;
 568        if (context->rx.prev_bit)
 569                value |= PULSE_BIT;
 570
 571        for (i = 0; i < 4; ++i)
 572                buf[i] = value>>(i*8);
 573
 574        lirc_buffer_write(context->driver->rbuf, buf);
 575        wake_up(&context->driver->rbuf->wait_poll);
 576        return;
 577}
 578
 579static inline int tv2int(const struct timeval *a, const struct timeval *b)
 580{
 581        int usecs = 0;
 582        int sec   = 0;
 583
 584        if (b->tv_usec > a->tv_usec) {
 585                usecs = 1000000;
 586                sec--;
 587        }
 588
 589        usecs += a->tv_usec - b->tv_usec;
 590
 591        sec += a->tv_sec - b->tv_sec;
 592        sec *= 1000;
 593        usecs /= 1000;
 594        sec += usecs;
 595
 596        if (sec < 0)
 597                sec = 1000;
 598
 599        return sec;
 600}
 601
 602/**
 603 * Process the incoming packet
 604 */
 605static void imon_incoming_packet(struct imon_context *context,
 606                                 struct urb *urb, int intf)
 607{
 608        int len = urb->actual_length;
 609        unsigned char *buf = urb->transfer_buffer;
 610        struct device *dev = context->driver->dev;
 611        int octet, bit;
 612        unsigned char mask;
 613        int i;
 614
 615        /*
 616         * just bail out if no listening IR client
 617         */
 618        if (!context->ir_isopen)
 619                return;
 620
 621        if (len != 8) {
 622                dev_warn(dev, "imon %s: invalid incoming packet "
 623                         "size (len = %d, intf%d)\n", __func__, len, intf);
 624                return;
 625        }
 626
 627        if (debug) {
 628                printk(KERN_INFO "raw packet: ");
 629                for (i = 0; i < len; ++i)
 630                        printk("%02x ", buf[i]);
 631                printk("\n");
 632        }
 633
 634        /*
 635         * Translate received data to pulse and space lengths.
 636         * Received data is active low, i.e. pulses are 0 and
 637         * spaces are 1.
 638         *
 639         * My original algorithm was essentially similar to
 640         * Changwoo Ryu's with the exception that he switched
 641         * the incoming bits to active high and also fed an
 642         * initial space to LIRC at the start of a new sequence
 643         * if the previous bit was a pulse.
 644         *
 645         * I've decided to adopt his algorithm.
 646         */
 647
 648        if (buf[7] == 1 && context->rx.initial_space) {
 649                /* LIRC requires a leading space */
 650                context->rx.prev_bit = 0;
 651                context->rx.count = 4;
 652                submit_data(context);
 653                context->rx.count = 0;
 654        }
 655
 656        for (octet = 0; octet < 5; ++octet) {
 657                mask = 0x80;
 658                for (bit = 0; bit < 8; ++bit) {
 659                        int curr_bit = !(buf[octet] & mask);
 660                        if (curr_bit != context->rx.prev_bit) {
 661                                if (context->rx.count) {
 662                                        submit_data(context);
 663                                        context->rx.count = 0;
 664                                }
 665                                context->rx.prev_bit = curr_bit;
 666                        }
 667                        ++context->rx.count;
 668                        mask >>= 1;
 669                }
 670        }
 671
 672        if (buf[7] == 10) {
 673                if (context->rx.count) {
 674                        submit_data(context);
 675                        context->rx.count = 0;
 676                }
 677                context->rx.initial_space = context->rx.prev_bit;
 678        }
 679}
 680
 681/**
 682 * Callback function for USB core API: receive data
 683 */
 684static void usb_rx_callback(struct urb *urb)
 685{
 686        struct imon_context *context;
 687        int intfnum = 0;
 688
 689        if (!urb)
 690                return;
 691
 692        context = (struct imon_context *)urb->context;
 693        if (!context)
 694                return;
 695
 696        switch (urb->status) {
 697        case -ENOENT:           /* usbcore unlink successful! */
 698                return;
 699
 700        case 0:
 701                imon_incoming_packet(context, urb, intfnum);
 702                break;
 703
 704        default:
 705                dev_warn(context->driver->dev, "imon %s: status(%d): ignored\n",
 706                         __func__, urb->status);
 707                break;
 708        }
 709
 710        usb_submit_urb(context->rx_urb, GFP_ATOMIC);
 711
 712        return;
 713}
 714
 715/**
 716 * Callback function for USB core API: Probe
 717 */
 718static int imon_probe(struct usb_interface *interface,
 719                      const struct usb_device_id *id)
 720{
 721        struct usb_device *usbdev = NULL;
 722        struct usb_host_interface *iface_desc = NULL;
 723        struct usb_endpoint_descriptor *rx_endpoint = NULL;
 724        struct usb_endpoint_descriptor *tx_endpoint = NULL;
 725        struct urb *rx_urb = NULL;
 726        struct urb *tx_urb = NULL;
 727        struct lirc_driver *driver = NULL;
 728        struct lirc_buffer *rbuf = NULL;
 729        struct device *dev = &interface->dev;
 730        int ifnum;
 731        int lirc_minor = 0;
 732        int num_endpts;
 733        int retval = 0;
 734        int display_ep_found = 0;
 735        int ir_ep_found = 0;
 736        int alloc_status = 0;
 737        int vfd_proto_6p = 0;
 738        struct imon_context *context = NULL;
 739        int i;
 740        u16 vendor, product;
 741
 742        /* prevent races probing devices w/multiple interfaces */
 743        mutex_lock(&driver_lock);
 744
 745        context = kzalloc(sizeof(struct imon_context), GFP_KERNEL);
 746        if (!context) {
 747                alloc_status = 1;
 748                goto alloc_status_switch;
 749        }
 750
 751        /*
 752         * Try to auto-detect the type of display if the user hasn't set
 753         * it by hand via the display_type modparam. Default is VFD.
 754         */
 755        if (usb_match_id(interface, ir_only_list))
 756                context->display = 0;
 757        else
 758                context->display = 1;
 759
 760        usbdev     = usb_get_dev(interface_to_usbdev(interface));
 761        iface_desc = interface->cur_altsetting;
 762        num_endpts = iface_desc->desc.bNumEndpoints;
 763        ifnum      = iface_desc->desc.bInterfaceNumber;
 764        vendor     = le16_to_cpu(usbdev->descriptor.idVendor);
 765        product    = le16_to_cpu(usbdev->descriptor.idProduct);
 766
 767        dev_dbg(dev, "%s: found iMON device (%04x:%04x, intf%d)\n",
 768                __func__, vendor, product, ifnum);
 769
 770        /*
 771         * Scan the endpoint list and set:
 772         *      first input endpoint = IR endpoint
 773         *      first output endpoint = display endpoint
 774         */
 775        for (i = 0; i < num_endpts && !(ir_ep_found && display_ep_found); ++i) {
 776                struct usb_endpoint_descriptor *ep;
 777                int ep_dir;
 778                int ep_type;
 779                ep = &iface_desc->endpoint[i].desc;
 780                ep_dir = ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK;
 781                ep_type = ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
 782
 783                if (!ir_ep_found &&
 784                        ep_dir == USB_DIR_IN &&
 785                        ep_type == USB_ENDPOINT_XFER_INT) {
 786
 787                        rx_endpoint = ep;
 788                        ir_ep_found = 1;
 789                        dev_dbg(dev, "%s: found IR endpoint\n", __func__);
 790
 791                } else if (!display_ep_found && ep_dir == USB_DIR_OUT &&
 792                           ep_type == USB_ENDPOINT_XFER_INT) {
 793                        tx_endpoint = ep;
 794                        display_ep_found = 1;
 795                        dev_dbg(dev, "%s: found display endpoint\n", __func__);
 796                }
 797        }
 798
 799        /*
 800         * Some iMON receivers have no display. Unfortunately, it seems
 801         * that SoundGraph recycles device IDs between devices both with
 802         * and without... :\
 803         */
 804        if (context->display == 0) {
 805                display_ep_found = 0;
 806                dev_dbg(dev, "%s: device has no display\n", __func__);
 807        }
 808
 809        /* Input endpoint is mandatory */
 810        if (!ir_ep_found) {
 811                dev_err(dev, "%s: no valid input (IR) endpoint found.\n", __func__);
 812                retval = -ENODEV;
 813                alloc_status = 2;
 814                goto alloc_status_switch;
 815        }
 816
 817        /* Determine if display requires 6 packets */
 818        if (display_ep_found) {
 819                if (usb_match_id(interface, vfd_proto_6p_list))
 820                        vfd_proto_6p = 1;
 821
 822                dev_dbg(dev, "%s: vfd_proto_6p: %d\n",
 823                        __func__, vfd_proto_6p);
 824        }
 825
 826        driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL);
 827        if (!driver) {
 828                alloc_status = 2;
 829                goto alloc_status_switch;
 830        }
 831        rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
 832        if (!rbuf) {
 833                alloc_status = 3;
 834                goto alloc_status_switch;
 835        }
 836        if (lirc_buffer_init(rbuf, BUF_CHUNK_SIZE, BUF_SIZE)) {
 837                dev_err(dev, "%s: lirc_buffer_init failed\n", __func__);
 838                alloc_status = 4;
 839                goto alloc_status_switch;
 840        }
 841        rx_urb = usb_alloc_urb(0, GFP_KERNEL);
 842        if (!rx_urb) {
 843                dev_err(dev, "%s: usb_alloc_urb failed for IR urb\n", __func__);
 844                alloc_status = 5;
 845                goto alloc_status_switch;
 846        }
 847        tx_urb = usb_alloc_urb(0, GFP_KERNEL);
 848        if (!tx_urb) {
 849                dev_err(dev, "%s: usb_alloc_urb failed for display urb\n",
 850                    __func__);
 851                alloc_status = 6;
 852                goto alloc_status_switch;
 853        }
 854
 855        mutex_init(&context->ctx_lock);
 856        context->vfd_proto_6p = vfd_proto_6p;
 857
 858        strcpy(driver->name, MOD_NAME);
 859        driver->minor = -1;
 860        driver->code_length = BUF_CHUNK_SIZE * 8;
 861        driver->sample_rate = 0;
 862        driver->features = LIRC_CAN_REC_MODE2;
 863        driver->data = context;
 864        driver->rbuf = rbuf;
 865        driver->set_use_inc = ir_open;
 866        driver->set_use_dec = ir_close;
 867        driver->dev = &interface->dev;
 868        driver->owner = THIS_MODULE;
 869
 870        mutex_lock(&context->ctx_lock);
 871
 872        context->driver = driver;
 873        /* start out in keyboard mode */
 874
 875        lirc_minor = lirc_register_driver(driver);
 876        if (lirc_minor < 0) {
 877                dev_err(dev, "%s: lirc_register_driver failed\n", __func__);
 878                alloc_status = 7;
 879                goto unlock;
 880        } else
 881                dev_info(dev, "Registered iMON driver "
 882                         "(lirc minor: %d)\n", lirc_minor);
 883
 884        /* Needed while unregistering! */
 885        driver->minor = lirc_minor;
 886
 887        context->usbdev = usbdev;
 888        context->dev_present = 1;
 889        context->rx_endpoint = rx_endpoint;
 890        context->rx_urb = rx_urb;
 891
 892        /*
 893         * tx is used to send characters to lcd/vfd, associate RF
 894         * remotes, set IR protocol, and maybe more...
 895         */
 896        context->tx_endpoint = tx_endpoint;
 897        context->tx_urb = tx_urb;
 898
 899        if (display_ep_found)
 900                context->display = 1;
 901
 902        usb_fill_int_urb(context->rx_urb, context->usbdev,
 903                usb_rcvintpipe(context->usbdev,
 904                        context->rx_endpoint->bEndpointAddress),
 905                context->usb_rx_buf, sizeof(context->usb_rx_buf),
 906                usb_rx_callback, context,
 907                context->rx_endpoint->bInterval);
 908
 909        retval = usb_submit_urb(context->rx_urb, GFP_KERNEL);
 910
 911        if (retval) {
 912                dev_err(dev, "%s: usb_submit_urb failed for intf0 (%d)\n",
 913                        __func__, retval);
 914                mutex_unlock(&context->ctx_lock);
 915                goto exit;
 916        }
 917
 918        usb_set_intfdata(interface, context);
 919
 920        if (context->display && ifnum == 0) {
 921                dev_dbg(dev, "%s: Registering iMON display with sysfs\n",
 922                        __func__);
 923
 924                if (usb_register_dev(interface, &imon_class)) {
 925                        /* Not a fatal error, so ignore */
 926                        dev_info(dev, "%s: could not get a minor number for "
 927                                 "display\n", __func__);
 928                }
 929        }
 930
 931        dev_info(dev, "iMON device (%04x:%04x, intf%d) on "
 932                 "usb<%d:%d> initialized\n", vendor, product, ifnum,
 933                 usbdev->bus->busnum, usbdev->devnum);
 934
 935unlock:
 936        mutex_unlock(&context->ctx_lock);
 937alloc_status_switch:
 938
 939        switch (alloc_status) {
 940        case 7:
 941                usb_free_urb(tx_urb);
 942        case 6:
 943                usb_free_urb(rx_urb);
 944        case 5:
 945                if (rbuf)
 946                        lirc_buffer_free(rbuf);
 947        case 4:
 948                kfree(rbuf);
 949        case 3:
 950                kfree(driver);
 951        case 2:
 952                kfree(context);
 953                context = NULL;
 954        case 1:
 955                if (retval != -ENODEV)
 956                        retval = -ENOMEM;
 957                break;
 958        case 0:
 959                retval = 0;
 960        }
 961
 962exit:
 963        mutex_unlock(&driver_lock);
 964
 965        return retval;
 966}
 967
 968/**
 969 * Callback function for USB core API: disconnect
 970 */
 971static void imon_disconnect(struct usb_interface *interface)
 972{
 973        struct imon_context *context;
 974        int ifnum;
 975
 976        /* prevent races with ir_open()/display_open() */
 977        mutex_lock(&driver_lock);
 978
 979        context = usb_get_intfdata(interface);
 980        ifnum = interface->cur_altsetting->desc.bInterfaceNumber;
 981
 982        mutex_lock(&context->ctx_lock);
 983
 984        usb_set_intfdata(interface, NULL);
 985
 986        /* Abort ongoing write */
 987        if (atomic_read(&context->tx.busy)) {
 988                usb_kill_urb(context->tx_urb);
 989                complete_all(&context->tx.finished);
 990        }
 991
 992        context->dev_present = 0;
 993        usb_kill_urb(context->rx_urb);
 994        if (context->display)
 995                usb_deregister_dev(interface, &imon_class);
 996
 997        if (!context->ir_isopen && !context->dev_present) {
 998                deregister_from_lirc(context);
 999                mutex_unlock(&context->ctx_lock);
1000                if (!context->display_isopen)
1001                        free_imon_context(context);
1002        } else
1003                mutex_unlock(&context->ctx_lock);
1004
1005        mutex_unlock(&driver_lock);
1006
1007        dev_info(&interface->dev, "%s: iMON device (intf%d) disconnected\n",
1008                 __func__, ifnum);
1009}
1010
1011static int imon_suspend(struct usb_interface *intf, pm_message_t message)
1012{
1013        struct imon_context *context = usb_get_intfdata(intf);
1014
1015        usb_kill_urb(context->rx_urb);
1016
1017        return 0;
1018}
1019
1020static int imon_resume(struct usb_interface *intf)
1021{
1022        int rc = 0;
1023        struct imon_context *context = usb_get_intfdata(intf);
1024
1025        usb_fill_int_urb(context->rx_urb, context->usbdev,
1026                usb_rcvintpipe(context->usbdev,
1027                        context->rx_endpoint->bEndpointAddress),
1028                context->usb_rx_buf, sizeof(context->usb_rx_buf),
1029                usb_rx_callback, context,
1030                context->rx_endpoint->bInterval);
1031
1032        rc = usb_submit_urb(context->rx_urb, GFP_ATOMIC);
1033
1034        return rc;
1035}
1036
1037module_usb_driver(imon_driver);
1038