linux/drivers/media/rc/redrat3.c
<<
>>
Prefs
   1/*
   2 * USB RedRat3 IR Transceiver rc-core driver
   3 *
   4 * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com>
   5 *  based heavily on the work of Stephen Cox, with additional
   6 *  help from RedRat Ltd.
   7 *
   8 * This driver began life based an an old version of the first-generation
   9 * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then
  10 * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's
  11 * Chris Dodge.
  12 *
  13 * The driver was then ported to rc-core and significantly rewritten again,
  14 * by Jarod, using the in-kernel mceusb driver as a guide, after an initial
  15 * port effort was started by Stephen.
  16 *
  17 * TODO LIST:
  18 * - fix lirc not showing repeats properly
  19 * --
  20 *
  21 * The RedRat3 is a USB transceiver with both send & receive,
  22 * with 2 separate sensors available for receive to enable
  23 * both good long range reception for general use, and good
  24 * short range reception when required for learning a signal.
  25 *
  26 * http://www.redrat.co.uk/
  27 *
  28 * It uses its own little protocol to communicate, the required
  29 * parts of which are embedded within this driver.
  30 * --
  31 *
  32 * This program is free software; you can redistribute it and/or modify
  33 * it under the terms of the GNU General Public License as published by
  34 * the Free Software Foundation; either version 2 of the License, or
  35 * (at your option) any later version.
  36 *
  37 * This program is distributed in the hope that it will be useful,
  38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  40 * GNU General Public License for more details.
  41 *
  42 * You should have received a copy of the GNU General Public License
  43 * along with this program; if not, write to the Free Software
  44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  45 *
  46 */
  47
  48#include <linux/device.h>
  49#include <linux/module.h>
  50#include <linux/slab.h>
  51#include <linux/usb.h>
  52#include <linux/usb/input.h>
  53#include <media/rc-core.h>
  54
  55/* Driver Information */
  56#define DRIVER_VERSION "0.70"
  57#define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
  58#define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
  59#define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
  60#define DRIVER_NAME "redrat3"
  61
  62/* module parameters */
  63#ifdef CONFIG_USB_DEBUG
  64static int debug = 1;
  65#else
  66static int debug;
  67#endif
  68
  69#define RR3_DEBUG_STANDARD              0x1
  70#define RR3_DEBUG_FUNCTION_TRACE        0x2
  71
  72#define rr3_dbg(dev, fmt, ...)                                  \
  73        do {                                                    \
  74                if (debug & RR3_DEBUG_STANDARD)                 \
  75                        dev_info(dev, fmt, ## __VA_ARGS__);     \
  76        } while (0)
  77
  78#define rr3_ftr(dev, fmt, ...)                                  \
  79        do {                                                    \
  80                if (debug & RR3_DEBUG_FUNCTION_TRACE)           \
  81                        dev_info(dev, fmt, ## __VA_ARGS__);     \
  82        } while (0)
  83
  84/* bulk data transfer types */
  85#define RR3_ERROR               0x01
  86#define RR3_MOD_SIGNAL_IN       0x20
  87#define RR3_MOD_SIGNAL_OUT      0x21
  88
  89/* Get the RR firmware version */
  90#define RR3_FW_VERSION          0xb1
  91#define RR3_FW_VERSION_LEN      64
  92/* Send encoded signal bulk-sent earlier*/
  93#define RR3_TX_SEND_SIGNAL      0xb3
  94#define RR3_SET_IR_PARAM        0xb7
  95#define RR3_GET_IR_PARAM        0xb8
  96/* Blink the red LED on the device */
  97#define RR3_BLINK_LED           0xb9
  98/* Read serial number of device */
  99#define RR3_READ_SER_NO         0xba
 100#define RR3_SER_NO_LEN          4
 101/* Start capture with the RC receiver */
 102#define RR3_RC_DET_ENABLE       0xbb
 103/* Stop capture with the RC receiver */
 104#define RR3_RC_DET_DISABLE      0xbc
 105/* Return the status of RC detector capture */
 106#define RR3_RC_DET_STATUS       0xbd
 107/* Reset redrat */
 108#define RR3_RESET               0xa0
 109
 110/* Max number of lengths in the signal. */
 111#define RR3_IR_IO_MAX_LENGTHS   0x01
 112/* Periods to measure mod. freq. */
 113#define RR3_IR_IO_PERIODS_MF    0x02
 114/* Size of memory for main signal data */
 115#define RR3_IR_IO_SIG_MEM_SIZE  0x03
 116/* Delta value when measuring lengths */
 117#define RR3_IR_IO_LENGTH_FUZZ   0x04
 118/* Timeout for end of signal detection */
 119#define RR3_IR_IO_SIG_TIMEOUT   0x05
 120/* Minumum value for pause recognition. */
 121#define RR3_IR_IO_MIN_PAUSE     0x06
 122
 123/* Clock freq. of EZ-USB chip */
 124#define RR3_CLK                 24000000
 125/* Clock periods per timer count */
 126#define RR3_CLK_PER_COUNT       12
 127/* (RR3_CLK / RR3_CLK_PER_COUNT) */
 128#define RR3_CLK_CONV_FACTOR     2000000
 129/* USB bulk-in IR data endpoint address */
 130#define RR3_BULK_IN_EP_ADDR     0x82
 131
 132/* Raw Modulated signal data value offsets */
 133#define RR3_PAUSE_OFFSET        0
 134#define RR3_FREQ_COUNT_OFFSET   4
 135#define RR3_NUM_PERIOD_OFFSET   6
 136#define RR3_MAX_LENGTHS_OFFSET  8
 137#define RR3_NUM_LENGTHS_OFFSET  9
 138#define RR3_MAX_SIGS_OFFSET     10
 139#define RR3_NUM_SIGS_OFFSET     12
 140#define RR3_REPEATS_OFFSET      14
 141
 142/* Size of the fixed-length portion of the signal */
 143#define RR3_HEADER_LENGTH       15
 144#define RR3_DRIVER_MAXLENS      128
 145#define RR3_MAX_SIG_SIZE        512
 146#define RR3_MAX_BUF_SIZE        \
 147        ((2 * RR3_HEADER_LENGTH) + RR3_DRIVER_MAXLENS + RR3_MAX_SIG_SIZE)
 148#define RR3_TIME_UNIT           50
 149#define RR3_END_OF_SIGNAL       0x7f
 150#define RR3_TX_HEADER_OFFSET    4
 151#define RR3_TX_TRAILER_LEN      2
 152#define RR3_RX_MIN_TIMEOUT      5
 153#define RR3_RX_MAX_TIMEOUT      2000
 154
 155/* The 8051's CPUCS Register address */
 156#define RR3_CPUCS_REG_ADDR      0x7f92
 157
 158#define USB_RR3USB_VENDOR_ID    0x112a
 159#define USB_RR3USB_PRODUCT_ID   0x0001
 160#define USB_RR3IIUSB_PRODUCT_ID 0x0005
 161
 162/* table of devices that work with this driver */
 163static struct usb_device_id redrat3_dev_table[] = {
 164        /* Original version of the RedRat3 */
 165        {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
 166        /* Second Version/release of the RedRat3 - RetRat3-II */
 167        {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
 168        {}                      /* Terminating entry */
 169};
 170
 171/* Structure to hold all of our device specific stuff */
 172struct redrat3_dev {
 173        /* core device bits */
 174        struct rc_dev *rc;
 175        struct device *dev;
 176
 177        /* save off the usb device pointer */
 178        struct usb_device *udev;
 179
 180        /* the receive endpoint */
 181        struct usb_endpoint_descriptor *ep_in;
 182        /* the buffer to receive data */
 183        unsigned char *bulk_in_buf;
 184        /* urb used to read ir data */
 185        struct urb *read_urb;
 186
 187        /* the send endpoint */
 188        struct usb_endpoint_descriptor *ep_out;
 189        /* the buffer to send data */
 190        unsigned char *bulk_out_buf;
 191        /* the urb used to send data */
 192        struct urb *write_urb;
 193
 194        /* usb dma */
 195        dma_addr_t dma_in;
 196        dma_addr_t dma_out;
 197
 198        /* locks this structure */
 199        struct mutex lock;
 200
 201        /* rx signal timeout timer */
 202        struct timer_list rx_timeout;
 203        u32 hw_timeout;
 204
 205        /* is the detector enabled*/
 206        bool det_enabled;
 207        /* Is the device currently transmitting?*/
 208        bool transmitting;
 209
 210        /* store for current packet */
 211        char pbuf[RR3_MAX_BUF_SIZE];
 212        u16 pktlen;
 213        u16 pkttype;
 214        u16 bytes_read;
 215        /* indicate whether we are going to reprocess
 216         * the USB callback with a bigger buffer */
 217        int buftoosmall;
 218        char *datap;
 219
 220        u32 carrier;
 221
 222        char name[128];
 223        char phys[64];
 224};
 225
 226/* All incoming data buffers adhere to a very specific data format */
 227struct redrat3_signal_header {
 228        u16 length;     /* Length of data being transferred */
 229        u16 transfer_type; /* Type of data transferred */
 230        u32 pause;      /* Pause between main and repeat signals */
 231        u16 mod_freq_count; /* Value of timer on mod. freq. measurement */
 232        u16 no_periods; /* No. of periods over which mod. freq. is measured */
 233        u8 max_lengths; /* Max no. of lengths (i.e. size of array) */
 234        u8 no_lengths;  /* Actual no. of elements in lengths array */
 235        u16 max_sig_size; /* Max no. of values in signal data array */
 236        u16 sig_size;   /* Acuto no. of values in signal data array */
 237        u8 no_repeats;  /* No. of repeats of repeat signal section */
 238        /* Here forward is the lengths and signal data */
 239};
 240
 241static void redrat3_dump_signal_header(struct redrat3_signal_header *header)
 242{
 243        pr_info("%s:\n", __func__);
 244        pr_info(" * length: %u, transfer_type: 0x%02x\n",
 245                header->length, header->transfer_type);
 246        pr_info(" * pause: %u, freq_count: %u, no_periods: %u\n",
 247                header->pause, header->mod_freq_count, header->no_periods);
 248        pr_info(" * lengths: %u (max: %u)\n",
 249                header->no_lengths, header->max_lengths);
 250        pr_info(" * sig_size: %u (max: %u)\n",
 251                header->sig_size, header->max_sig_size);
 252        pr_info(" * repeats: %u\n", header->no_repeats);
 253}
 254
 255static void redrat3_dump_signal_data(char *buffer, u16 len)
 256{
 257        int offset, i;
 258        char *data_vals;
 259
 260        pr_info("%s:", __func__);
 261
 262        offset = RR3_TX_HEADER_OFFSET + RR3_HEADER_LENGTH
 263                 + (RR3_DRIVER_MAXLENS * sizeof(u16));
 264
 265        /* read RR3_DRIVER_MAXLENS from ctrl msg */
 266        data_vals = buffer + offset;
 267
 268        for (i = 0; i < len; i++) {
 269                if (i % 10 == 0)
 270                        pr_cont("\n * ");
 271                pr_cont("%02x ", *data_vals++);
 272        }
 273
 274        pr_cont("\n");
 275}
 276
 277/*
 278 * redrat3_issue_async
 279 *
 280 *  Issues an async read to the ir data in port..
 281 *  sets the callback to be redrat3_handle_async
 282 */
 283static void redrat3_issue_async(struct redrat3_dev *rr3)
 284{
 285        int res;
 286
 287        rr3_ftr(rr3->dev, "Entering %s\n", __func__);
 288
 289        if (!rr3->det_enabled) {
 290                dev_warn(rr3->dev, "not issuing async read, "
 291                         "detector not enabled\n");
 292                return;
 293        }
 294
 295        memset(rr3->bulk_in_buf, 0, rr3->ep_in->wMaxPacketSize);
 296        res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
 297        if (res)
 298                rr3_dbg(rr3->dev, "%s: receive request FAILED! "
 299                        "(res %d, len %d)\n", __func__, res,
 300                        rr3->read_urb->transfer_buffer_length);
 301}
 302
 303static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
 304{
 305        if (!rr3->transmitting && (code != 0x40))
 306                dev_info(rr3->dev, "fw error code 0x%02x: ", code);
 307
 308        switch (code) {
 309        case 0x00:
 310                pr_cont("No Error\n");
 311                break;
 312
 313        /* Codes 0x20 through 0x2f are IR Firmware Errors */
 314        case 0x20:
 315                pr_cont("Initial signal pulse not long enough "
 316                        "to measure carrier frequency\n");
 317                break;
 318        case 0x21:
 319                pr_cont("Not enough length values allocated for signal\n");
 320                break;
 321        case 0x22:
 322                pr_cont("Not enough memory allocated for signal data\n");
 323                break;
 324        case 0x23:
 325                pr_cont("Too many signal repeats\n");
 326                break;
 327        case 0x28:
 328                pr_cont("Insufficient memory available for IR signal "
 329                        "data memory allocation\n");
 330                break;
 331        case 0x29:
 332                pr_cont("Insufficient memory available "
 333                        "for IrDa signal data memory allocation\n");
 334                break;
 335
 336        /* Codes 0x30 through 0x3f are USB Firmware Errors */
 337        case 0x30:
 338                pr_cont("Insufficient memory available for bulk "
 339                        "transfer structure\n");
 340                break;
 341
 342        /*
 343         * Other error codes... These are primarily errors that can occur in
 344         * the control messages sent to the redrat
 345         */
 346        case 0x40:
 347                if (!rr3->transmitting)
 348                        pr_cont("Signal capture has been terminated\n");
 349                break;
 350        case 0x41:
 351                pr_cont("Attempt to set/get and unknown signal I/O "
 352                        "algorithm parameter\n");
 353                break;
 354        case 0x42:
 355                pr_cont("Signal capture already started\n");
 356                break;
 357
 358        default:
 359                pr_cont("Unknown Error\n");
 360                break;
 361        }
 362}
 363
 364static u32 redrat3_val_to_mod_freq(struct redrat3_signal_header *ph)
 365{
 366        u32 mod_freq = 0;
 367
 368        if (ph->mod_freq_count != 0)
 369                mod_freq = (RR3_CLK * ph->no_periods) /
 370                                (ph->mod_freq_count * RR3_CLK_PER_COUNT);
 371
 372        return mod_freq;
 373}
 374
 375/* this function scales down the figures for the same result... */
 376static u32 redrat3_len_to_us(u32 length)
 377{
 378        u32 biglen = length * 1000;
 379        u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
 380        u32 result = (u32) (biglen / divisor);
 381
 382        /* don't allow zero lengths to go back, breaks lirc */
 383        return result ? result : 1;
 384}
 385
 386/*
 387 * convert us back into redrat3 lengths
 388 *
 389 * length * 1000   length * 1000000
 390 * ------------- = ---------------- = micro
 391 * rr3clk / 1000       rr3clk
 392
 393 * 6 * 2       4 * 3        micro * rr3clk          micro * rr3clk / 1000
 394 * ----- = 4   ----- = 6    -------------- = len    ---------------------
 395 *   3           2             1000000                    1000
 396 */
 397static u32 redrat3_us_to_len(u32 microsec)
 398{
 399        u32 result;
 400        u32 divisor;
 401
 402        microsec &= IR_MAX_DURATION;
 403        divisor = (RR3_CLK_CONV_FACTOR / 1000);
 404        result = (u32)(microsec * divisor) / 1000;
 405
 406        /* don't allow zero lengths to go back, breaks lirc */
 407        return result ? result : 1;
 408
 409}
 410
 411/* timer callback to send reset event */
 412static void redrat3_rx_timeout(unsigned long data)
 413{
 414        struct redrat3_dev *rr3 = (struct redrat3_dev *)data;
 415
 416        rr3_dbg(rr3->dev, "calling ir_raw_event_reset\n");
 417        ir_raw_event_reset(rr3->rc);
 418}
 419
 420static void redrat3_process_ir_data(struct redrat3_dev *rr3)
 421{
 422        DEFINE_IR_RAW_EVENT(rawir);
 423        struct redrat3_signal_header header;
 424        struct device *dev;
 425        int i, trailer = 0;
 426        unsigned long delay;
 427        u32 mod_freq, single_len;
 428        u16 *len_vals;
 429        u8 *data_vals;
 430        u32 tmp32;
 431        u16 tmp16;
 432        char *sig_data;
 433
 434        if (!rr3) {
 435                pr_err("%s called with no context!\n", __func__);
 436                return;
 437        }
 438
 439        rr3_ftr(rr3->dev, "Entered %s\n", __func__);
 440
 441        dev = rr3->dev;
 442        sig_data = rr3->pbuf;
 443
 444        header.length = rr3->pktlen;
 445        header.transfer_type = rr3->pkttype;
 446
 447        /* Sanity check */
 448        if (!(header.length >= RR3_HEADER_LENGTH))
 449                dev_warn(dev, "read returned less than rr3 header len\n");
 450
 451        /* Make sure we reset the IR kfifo after a bit of inactivity */
 452        delay = usecs_to_jiffies(rr3->hw_timeout);
 453        mod_timer(&rr3->rx_timeout, jiffies + delay);
 454
 455        memcpy(&tmp32, sig_data + RR3_PAUSE_OFFSET, sizeof(tmp32));
 456        header.pause = be32_to_cpu(tmp32);
 457
 458        memcpy(&tmp16, sig_data + RR3_FREQ_COUNT_OFFSET, sizeof(tmp16));
 459        header.mod_freq_count = be16_to_cpu(tmp16);
 460
 461        memcpy(&tmp16, sig_data + RR3_NUM_PERIOD_OFFSET, sizeof(tmp16));
 462        header.no_periods = be16_to_cpu(tmp16);
 463
 464        header.max_lengths = sig_data[RR3_MAX_LENGTHS_OFFSET];
 465        header.no_lengths = sig_data[RR3_NUM_LENGTHS_OFFSET];
 466
 467        memcpy(&tmp16, sig_data + RR3_MAX_SIGS_OFFSET, sizeof(tmp16));
 468        header.max_sig_size = be16_to_cpu(tmp16);
 469
 470        memcpy(&tmp16, sig_data + RR3_NUM_SIGS_OFFSET, sizeof(tmp16));
 471        header.sig_size = be16_to_cpu(tmp16);
 472
 473        header.no_repeats= sig_data[RR3_REPEATS_OFFSET];
 474
 475        if (debug) {
 476                redrat3_dump_signal_header(&header);
 477                redrat3_dump_signal_data(sig_data, header.sig_size);
 478        }
 479
 480        mod_freq = redrat3_val_to_mod_freq(&header);
 481        rr3_dbg(dev, "Got mod_freq of %u\n", mod_freq);
 482
 483        /* Here we pull out the 'length' values from the signal */
 484        len_vals = (u16 *)(sig_data + RR3_HEADER_LENGTH);
 485
 486        data_vals = sig_data + RR3_HEADER_LENGTH +
 487                    (header.max_lengths * sizeof(u16));
 488
 489        /* process each rr3 encoded byte into an int */
 490        for (i = 0; i < header.sig_size; i++) {
 491                u16 val = len_vals[data_vals[i]];
 492                single_len = redrat3_len_to_us((u32)be16_to_cpu(val));
 493
 494                /* we should always get pulse/space/pulse/space samples */
 495                if (i % 2)
 496                        rawir.pulse = false;
 497                else
 498                        rawir.pulse = true;
 499
 500                rawir.duration = US_TO_NS(single_len);
 501                /* Save initial pulse length to fudge trailer */
 502                if (i == 0)
 503                        trailer = rawir.duration;
 504                /* cap the value to IR_MAX_DURATION */
 505                rawir.duration &= IR_MAX_DURATION;
 506
 507                rr3_dbg(dev, "storing %s with duration %d (i: %d)\n",
 508                        rawir.pulse ? "pulse" : "space", rawir.duration, i);
 509                ir_raw_event_store_with_filter(rr3->rc, &rawir);
 510        }
 511
 512        /* add a trailing space, if need be */
 513        if (i % 2) {
 514                rawir.pulse = false;
 515                /* this duration is made up, and may not be ideal... */
 516                if (trailer < US_TO_NS(1000))
 517                        rawir.duration = US_TO_NS(2800);
 518                else
 519                        rawir.duration = trailer;
 520                rr3_dbg(dev, "storing trailing space with duration %d\n",
 521                        rawir.duration);
 522                ir_raw_event_store_with_filter(rr3->rc, &rawir);
 523        }
 524
 525        rr3_dbg(dev, "calling ir_raw_event_handle\n");
 526        ir_raw_event_handle(rr3->rc);
 527
 528        return;
 529}
 530
 531/* Util fn to send rr3 cmds */
 532static u8 redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
 533{
 534        struct usb_device *udev;
 535        u8 *data;
 536        int res;
 537
 538        data = kzalloc(sizeof(u8), GFP_KERNEL);
 539        if (!data)
 540                return -ENOMEM;
 541
 542        udev = rr3->udev;
 543        res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
 544                              USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
 545                              0x0000, 0x0000, data, sizeof(u8), HZ * 10);
 546
 547        if (res < 0) {
 548                dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
 549                        __func__, res, *data);
 550                res = -EIO;
 551        } else
 552                res = (u8)data[0];
 553
 554        kfree(data);
 555
 556        return res;
 557}
 558
 559/* Enables the long range detector and starts async receive */
 560static int redrat3_enable_detector(struct redrat3_dev *rr3)
 561{
 562        struct device *dev = rr3->dev;
 563        u8 ret;
 564
 565        rr3_ftr(dev, "Entering %s\n", __func__);
 566
 567        ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
 568        if (ret != 0)
 569                dev_dbg(dev, "%s: unexpected ret of %d\n",
 570                        __func__, ret);
 571
 572        ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
 573        if (ret != 1) {
 574                dev_err(dev, "%s: detector status: %d, should be 1\n",
 575                        __func__, ret);
 576                return -EIO;
 577        }
 578
 579        rr3->det_enabled = true;
 580        redrat3_issue_async(rr3);
 581
 582        return 0;
 583}
 584
 585/* Disables the rr3 long range detector */
 586static void redrat3_disable_detector(struct redrat3_dev *rr3)
 587{
 588        struct device *dev = rr3->dev;
 589        u8 ret;
 590
 591        rr3_ftr(dev, "Entering %s\n", __func__);
 592
 593        ret = redrat3_send_cmd(RR3_RC_DET_DISABLE, rr3);
 594        if (ret != 0)
 595                dev_err(dev, "%s: failure!\n", __func__);
 596
 597        ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
 598        if (ret != 0)
 599                dev_warn(dev, "%s: detector status: %d, should be 0\n",
 600                         __func__, ret);
 601
 602        rr3->det_enabled = false;
 603}
 604
 605static inline void redrat3_delete(struct redrat3_dev *rr3,
 606                                  struct usb_device *udev)
 607{
 608        rr3_ftr(rr3->dev, "%s cleaning up\n", __func__);
 609        usb_kill_urb(rr3->read_urb);
 610        usb_kill_urb(rr3->write_urb);
 611
 612        usb_free_urb(rr3->read_urb);
 613        usb_free_urb(rr3->write_urb);
 614
 615        usb_free_coherent(udev, rr3->ep_in->wMaxPacketSize,
 616                          rr3->bulk_in_buf, rr3->dma_in);
 617        usb_free_coherent(udev, rr3->ep_out->wMaxPacketSize,
 618                          rr3->bulk_out_buf, rr3->dma_out);
 619
 620        kfree(rr3);
 621}
 622
 623static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
 624{
 625        u32 *tmp;
 626        u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
 627        int len, ret, pipe;
 628
 629        len = sizeof(*tmp);
 630        tmp = kzalloc(len, GFP_KERNEL);
 631        if (!tmp) {
 632                dev_warn(rr3->dev, "Memory allocation faillure\n");
 633                return timeout;
 634        }
 635
 636        pipe = usb_rcvctrlpipe(rr3->udev, 0);
 637        ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
 638                              USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
 639                              RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
 640        if (ret != len) {
 641                dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
 642                return timeout;
 643        }
 644
 645        timeout = redrat3_len_to_us(be32_to_cpu(*tmp));
 646
 647        rr3_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
 648        return timeout;
 649}
 650
 651static void redrat3_reset(struct redrat3_dev *rr3)
 652{
 653        struct usb_device *udev = rr3->udev;
 654        struct device *dev = rr3->dev;
 655        int rc, rxpipe, txpipe;
 656        u8 *val;
 657        int len = sizeof(u8);
 658
 659        rr3_ftr(dev, "Entering %s\n", __func__);
 660
 661        rxpipe = usb_rcvctrlpipe(udev, 0);
 662        txpipe = usb_sndctrlpipe(udev, 0);
 663
 664        val = kzalloc(len, GFP_KERNEL);
 665        if (!val) {
 666                dev_err(dev, "Memory allocation failure\n");
 667                return;
 668        }
 669
 670        *val = 0x01;
 671        rc = usb_control_msg(udev, rxpipe, RR3_RESET,
 672                             USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
 673                             RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
 674        rr3_dbg(dev, "reset returned 0x%02x\n", rc);
 675
 676        *val = 5;
 677        rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
 678                             USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
 679                             RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
 680        rr3_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
 681
 682        *val = RR3_DRIVER_MAXLENS;
 683        rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
 684                             USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
 685                             RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
 686        rr3_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
 687
 688        kfree(val);
 689}
 690
 691static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
 692{
 693        int rc = 0;
 694        char *buffer;
 695
 696        rr3_ftr(rr3->dev, "Entering %s\n", __func__);
 697
 698        buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
 699        if (!buffer) {
 700                dev_err(rr3->dev, "Memory allocation failure\n");
 701                return;
 702        }
 703
 704        rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
 705                             RR3_FW_VERSION,
 706                             USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
 707                             0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
 708
 709        if (rc >= 0)
 710                dev_info(rr3->dev, "Firmware rev: %s", buffer);
 711        else
 712                dev_err(rr3->dev, "Problem fetching firmware ID\n");
 713
 714        kfree(buffer);
 715        rr3_ftr(rr3->dev, "Exiting %s\n", __func__);
 716}
 717
 718static void redrat3_read_packet_start(struct redrat3_dev *rr3, int len)
 719{
 720        u16 tx_error;
 721        u16 hdrlen;
 722
 723        rr3_ftr(rr3->dev, "Entering %s\n", __func__);
 724
 725        /* grab the Length and type of transfer */
 726        memcpy(&(rr3->pktlen), (unsigned char *) rr3->bulk_in_buf,
 727               sizeof(rr3->pktlen));
 728        memcpy(&(rr3->pkttype), ((unsigned char *) rr3->bulk_in_buf +
 729                sizeof(rr3->pktlen)),
 730               sizeof(rr3->pkttype));
 731
 732        /*data needs conversion to know what its real values are*/
 733        rr3->pktlen = be16_to_cpu(rr3->pktlen);
 734        rr3->pkttype = be16_to_cpu(rr3->pkttype);
 735
 736        switch (rr3->pkttype) {
 737        case RR3_ERROR:
 738                memcpy(&tx_error, ((unsigned char *)rr3->bulk_in_buf
 739                        + (sizeof(rr3->pktlen) + sizeof(rr3->pkttype))),
 740                       sizeof(tx_error));
 741                tx_error = be16_to_cpu(tx_error);
 742                redrat3_dump_fw_error(rr3, tx_error);
 743                break;
 744
 745        case RR3_MOD_SIGNAL_IN:
 746                hdrlen = sizeof(rr3->pktlen) + sizeof(rr3->pkttype);
 747                rr3->bytes_read = len;
 748                rr3->bytes_read -= hdrlen;
 749                rr3->datap = &(rr3->pbuf[0]);
 750
 751                memcpy(rr3->datap, ((unsigned char *)rr3->bulk_in_buf + hdrlen),
 752                       rr3->bytes_read);
 753                rr3->datap += rr3->bytes_read;
 754                rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
 755                        rr3->bytes_read, rr3->pktlen);
 756                break;
 757
 758        default:
 759                rr3_dbg(rr3->dev, "ignoring packet with type 0x%02x, "
 760                        "len of %d, 0x%02x\n", rr3->pkttype, len, rr3->pktlen);
 761                break;
 762        }
 763}
 764
 765static void redrat3_read_packet_continue(struct redrat3_dev *rr3, int len)
 766{
 767
 768        rr3_ftr(rr3->dev, "Entering %s\n", __func__);
 769
 770        memcpy(rr3->datap, (unsigned char *)rr3->bulk_in_buf, len);
 771        rr3->datap += len;
 772
 773        rr3->bytes_read += len;
 774        rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
 775                rr3->bytes_read, rr3->pktlen);
 776}
 777
 778/* gather IR data from incoming urb, process it when we have enough */
 779static int redrat3_get_ir_data(struct redrat3_dev *rr3, int len)
 780{
 781        struct device *dev = rr3->dev;
 782        int ret = 0;
 783
 784        rr3_ftr(dev, "Entering %s\n", __func__);
 785
 786        if (rr3->pktlen > RR3_MAX_BUF_SIZE) {
 787                dev_err(rr3->dev, "error: packet larger than buffer\n");
 788                ret = -EINVAL;
 789                goto out;
 790        }
 791
 792        if ((rr3->bytes_read == 0) &&
 793            (len >= (sizeof(rr3->pkttype) + sizeof(rr3->pktlen)))) {
 794                redrat3_read_packet_start(rr3, len);
 795        } else if (rr3->bytes_read != 0) {
 796                redrat3_read_packet_continue(rr3, len);
 797        } else if (rr3->bytes_read == 0) {
 798                dev_err(dev, "error: no packet data read\n");
 799                ret = -ENODATA;
 800                goto out;
 801        }
 802
 803        if (rr3->bytes_read > rr3->pktlen) {
 804                dev_err(dev, "bytes_read (%d) greater than pktlen (%d)\n",
 805                        rr3->bytes_read, rr3->pktlen);
 806                ret = -EINVAL;
 807                goto out;
 808        } else if (rr3->bytes_read < rr3->pktlen)
 809                /* we're still accumulating data */
 810                return 0;
 811
 812        /* if we get here, we've got IR data to decode */
 813        if (rr3->pkttype == RR3_MOD_SIGNAL_IN)
 814                redrat3_process_ir_data(rr3);
 815        else
 816                rr3_dbg(dev, "discarding non-signal data packet "
 817                        "(type 0x%02x)\n", rr3->pkttype);
 818
 819out:
 820        rr3->bytes_read = 0;
 821        rr3->pktlen = 0;
 822        rr3->pkttype = 0;
 823        return ret;
 824}
 825
 826/* callback function from USB when async USB request has completed */
 827static void redrat3_handle_async(struct urb *urb, struct pt_regs *regs)
 828{
 829        struct redrat3_dev *rr3;
 830
 831        if (!urb)
 832                return;
 833
 834        rr3 = urb->context;
 835        if (!rr3) {
 836                pr_err("%s called with invalid context!\n", __func__);
 837                usb_unlink_urb(urb);
 838                return;
 839        }
 840
 841        rr3_ftr(rr3->dev, "Entering %s\n", __func__);
 842
 843        if (!rr3->det_enabled) {
 844                rr3_dbg(rr3->dev, "received a read callback but detector "
 845                        "disabled - ignoring\n");
 846                return;
 847        }
 848
 849        switch (urb->status) {
 850        case 0:
 851                redrat3_get_ir_data(rr3, urb->actual_length);
 852                break;
 853
 854        case -ECONNRESET:
 855        case -ENOENT:
 856        case -ESHUTDOWN:
 857                usb_unlink_urb(urb);
 858                return;
 859
 860        case -EPIPE:
 861        default:
 862                dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
 863                rr3->bytes_read = 0;
 864                rr3->pktlen = 0;
 865                rr3->pkttype = 0;
 866                break;
 867        }
 868
 869        if (!rr3->transmitting)
 870                redrat3_issue_async(rr3);
 871        else
 872                rr3_dbg(rr3->dev, "IR transmit in progress\n");
 873}
 874
 875static void redrat3_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
 876{
 877        struct redrat3_dev *rr3;
 878        int len;
 879
 880        if (!urb)
 881                return;
 882
 883        rr3 = urb->context;
 884        if (rr3) {
 885                len = urb->actual_length;
 886                rr3_ftr(rr3->dev, "%s: called (status=%d len=%d)\n",
 887                        __func__, urb->status, len);
 888        }
 889}
 890
 891static u16 mod_freq_to_val(unsigned int mod_freq)
 892{
 893        int mult = 6000000;
 894
 895        /* Clk used in mod. freq. generation is CLK24/4. */
 896        return (u16)(65536 - (mult / mod_freq));
 897}
 898
 899static int redrat3_set_tx_carrier(struct rc_dev *dev, u32 carrier)
 900{
 901        struct redrat3_dev *rr3 = dev->priv;
 902
 903        rr3->carrier = carrier;
 904
 905        return carrier;
 906}
 907
 908static int redrat3_transmit_ir(struct rc_dev *rcdev, int *txbuf, u32 n)
 909{
 910        struct redrat3_dev *rr3 = rcdev->priv;
 911        struct device *dev = rr3->dev;
 912        struct redrat3_signal_header header;
 913        int i, j, count, ret, ret_len, offset;
 914        int lencheck, cur_sample_len, pipe;
 915        char *buffer = NULL, *sigdata = NULL;
 916        int *sample_lens = NULL;
 917        u32 tmpi;
 918        u16 tmps;
 919        u8 *datap;
 920        u8 curlencheck = 0;
 921        u16 *lengths_ptr;
 922        int sendbuf_len;
 923
 924        rr3_ftr(dev, "Entering %s\n", __func__);
 925
 926        if (rr3->transmitting) {
 927                dev_warn(dev, "%s: transmitter already in use\n", __func__);
 928                return -EAGAIN;
 929        }
 930
 931        count = n / sizeof(int);
 932        if (count > (RR3_DRIVER_MAXLENS * 2))
 933                return -EINVAL;
 934
 935        rr3->transmitting = true;
 936
 937        redrat3_disable_detector(rr3);
 938
 939        if (rr3->det_enabled) {
 940                dev_err(dev, "%s: cannot tx while rx is enabled\n", __func__);
 941                ret = -EIO;
 942                goto out;
 943        }
 944
 945        sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
 946        if (!sample_lens) {
 947                ret = -ENOMEM;
 948                goto out;
 949        }
 950
 951        for (i = 0; i < count; i++) {
 952                for (lencheck = 0; lencheck < curlencheck; lencheck++) {
 953                        cur_sample_len = redrat3_us_to_len(txbuf[i]);
 954                        if (sample_lens[lencheck] == cur_sample_len)
 955                                break;
 956                }
 957                if (lencheck == curlencheck) {
 958                        cur_sample_len = redrat3_us_to_len(txbuf[i]);
 959                        rr3_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
 960                                i, txbuf[i], curlencheck, cur_sample_len);
 961                        if (curlencheck < 255) {
 962                                /* now convert the value to a proper
 963                                 * rr3 value.. */
 964                                sample_lens[curlencheck] = cur_sample_len;
 965                                curlencheck++;
 966                        } else {
 967                                dev_err(dev, "signal too long\n");
 968                                ret = -EINVAL;
 969                                goto out;
 970                        }
 971                }
 972        }
 973
 974        sigdata = kzalloc((count + RR3_TX_TRAILER_LEN), GFP_KERNEL);
 975        if (!sigdata) {
 976                ret = -ENOMEM;
 977                goto out;
 978        }
 979
 980        sigdata[count] = RR3_END_OF_SIGNAL;
 981        sigdata[count + 1] = RR3_END_OF_SIGNAL;
 982        for (i = 0; i < count; i++) {
 983                for (j = 0; j < curlencheck; j++) {
 984                        if (sample_lens[j] == redrat3_us_to_len(txbuf[i]))
 985                                sigdata[i] = j;
 986                }
 987        }
 988
 989        offset = RR3_TX_HEADER_OFFSET;
 990        sendbuf_len = RR3_HEADER_LENGTH + (sizeof(u16) * RR3_DRIVER_MAXLENS)
 991                        + count + RR3_TX_TRAILER_LEN + offset;
 992
 993        buffer = kzalloc(sendbuf_len, GFP_KERNEL);
 994        if (!buffer) {
 995                ret = -ENOMEM;
 996                goto out;
 997        }
 998
 999        /* fill in our packet header */
1000        header.length = sendbuf_len - offset;
1001        header.transfer_type = RR3_MOD_SIGNAL_OUT;
1002        header.pause = redrat3_len_to_us(100);
1003        header.mod_freq_count = mod_freq_to_val(rr3->carrier);
1004        header.no_periods = 0; /* n/a to transmit */
1005        header.max_lengths = RR3_DRIVER_MAXLENS;
1006        header.no_lengths = curlencheck;
1007        header.max_sig_size = RR3_MAX_SIG_SIZE;
1008        header.sig_size = count + RR3_TX_TRAILER_LEN;
1009        /* we currently rely on repeat handling in the IR encoding source */
1010        header.no_repeats = 0;
1011
1012        tmps = cpu_to_be16(header.length);
1013        memcpy(buffer, &tmps, 2);
1014
1015        tmps = cpu_to_be16(header.transfer_type);
1016        memcpy(buffer + 2, &tmps, 2);
1017
1018        tmpi = cpu_to_be32(header.pause);
1019        memcpy(buffer + offset, &tmpi, sizeof(tmpi));
1020
1021        tmps = cpu_to_be16(header.mod_freq_count);
1022        memcpy(buffer + offset + RR3_FREQ_COUNT_OFFSET, &tmps, 2);
1023
1024        buffer[offset + RR3_NUM_LENGTHS_OFFSET] = header.no_lengths;
1025
1026        tmps = cpu_to_be16(header.sig_size);
1027        memcpy(buffer + offset + RR3_NUM_SIGS_OFFSET, &tmps, 2);
1028
1029        buffer[offset + RR3_REPEATS_OFFSET] = header.no_repeats;
1030
1031        lengths_ptr = (u16 *)(buffer + offset + RR3_HEADER_LENGTH);
1032        for (i = 0; i < curlencheck; ++i)
1033                lengths_ptr[i] = cpu_to_be16(sample_lens[i]);
1034
1035        datap = (u8 *)(buffer + offset + RR3_HEADER_LENGTH +
1036                            (sizeof(u16) * RR3_DRIVER_MAXLENS));
1037        memcpy(datap, sigdata, (count + RR3_TX_TRAILER_LEN));
1038
1039        if (debug) {
1040                redrat3_dump_signal_header(&header);
1041                redrat3_dump_signal_data(buffer, header.sig_size);
1042        }
1043
1044        pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
1045        tmps = usb_bulk_msg(rr3->udev, pipe, buffer,
1046                            sendbuf_len, &ret_len, 10 * HZ);
1047        rr3_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, tmps);
1048
1049        /* now tell the hardware to transmit what we sent it */
1050        pipe = usb_rcvctrlpipe(rr3->udev, 0);
1051        ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
1052                              USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
1053                              0, 0, buffer, 2, HZ * 10);
1054
1055        if (ret < 0)
1056                dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
1057        else
1058                ret = n;
1059
1060out:
1061        kfree(sample_lens);
1062        kfree(buffer);
1063        kfree(sigdata);
1064
1065        rr3->transmitting = false;
1066
1067        redrat3_enable_detector(rr3);
1068
1069        return ret;
1070}
1071
1072static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
1073{
1074        struct device *dev = rr3->dev;
1075        struct rc_dev *rc;
1076        int ret = -ENODEV;
1077        u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
1078
1079        rc = rc_allocate_device();
1080        if (!rc) {
1081                dev_err(dev, "remote input dev allocation failed\n");
1082                goto out;
1083        }
1084
1085        snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
1086                 "Infrared Remote Transceiver (%04x:%04x)",
1087                 prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
1088                 le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
1089
1090        usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
1091
1092        rc->input_name = rr3->name;
1093        rc->input_phys = rr3->phys;
1094        usb_to_input_id(rr3->udev, &rc->input_id);
1095        rc->dev.parent = dev;
1096        rc->priv = rr3;
1097        rc->driver_type = RC_DRIVER_IR_RAW;
1098        rc->allowed_protos = RC_TYPE_ALL;
1099        rc->timeout = US_TO_NS(2750);
1100        rc->tx_ir = redrat3_transmit_ir;
1101        rc->s_tx_carrier = redrat3_set_tx_carrier;
1102        rc->driver_name = DRIVER_NAME;
1103        rc->map_name = RC_MAP_HAUPPAUGE;
1104
1105        ret = rc_register_device(rc);
1106        if (ret < 0) {
1107                dev_err(dev, "remote dev registration failed\n");
1108                goto out;
1109        }
1110
1111        return rc;
1112
1113out:
1114        rc_free_device(rc);
1115        return NULL;
1116}
1117
1118static int __devinit redrat3_dev_probe(struct usb_interface *intf,
1119                                       const struct usb_device_id *id)
1120{
1121        struct usb_device *udev = interface_to_usbdev(intf);
1122        struct device *dev = &intf->dev;
1123        struct usb_host_interface *uhi;
1124        struct redrat3_dev *rr3;
1125        struct usb_endpoint_descriptor *ep;
1126        struct usb_endpoint_descriptor *ep_in = NULL;
1127        struct usb_endpoint_descriptor *ep_out = NULL;
1128        u8 addr, attrs;
1129        int pipe, i;
1130        int retval = -ENOMEM;
1131
1132        rr3_ftr(dev, "%s called\n", __func__);
1133
1134        uhi = intf->cur_altsetting;
1135
1136        /* find our bulk-in and bulk-out endpoints */
1137        for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
1138                ep = &uhi->endpoint[i].desc;
1139                addr = ep->bEndpointAddress;
1140                attrs = ep->bmAttributes;
1141
1142                if ((ep_in == NULL) &&
1143                    ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
1144                    ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
1145                     USB_ENDPOINT_XFER_BULK)) {
1146                        rr3_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
1147                                ep->bEndpointAddress);
1148                        /* data comes in on 0x82, 0x81 is for other data... */
1149                        if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
1150                                ep_in = ep;
1151                }
1152
1153                if ((ep_out == NULL) &&
1154                    ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
1155                    ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
1156                     USB_ENDPOINT_XFER_BULK)) {
1157                        rr3_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
1158                                ep->bEndpointAddress);
1159                        ep_out = ep;
1160                }
1161        }
1162
1163        if (!ep_in || !ep_out) {
1164                dev_err(dev, "Couldn't find both in and out endpoints\n");
1165                retval = -ENODEV;
1166                goto no_endpoints;
1167        }
1168
1169        /* allocate memory for our device state and initialize it */
1170        rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
1171        if (rr3 == NULL) {
1172                dev_err(dev, "Memory allocation failure\n");
1173                goto no_endpoints;
1174        }
1175
1176        rr3->dev = &intf->dev;
1177
1178        /* set up bulk-in endpoint */
1179        rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
1180        if (!rr3->read_urb) {
1181                dev_err(dev, "Read urb allocation failure\n");
1182                goto error;
1183        }
1184
1185        rr3->ep_in = ep_in;
1186        rr3->bulk_in_buf = usb_alloc_coherent(udev, ep_in->wMaxPacketSize,
1187                                              GFP_ATOMIC, &rr3->dma_in);
1188        if (!rr3->bulk_in_buf) {
1189                dev_err(dev, "Read buffer allocation failure\n");
1190                goto error;
1191        }
1192
1193        pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
1194        usb_fill_bulk_urb(rr3->read_urb, udev, pipe,
1195                          rr3->bulk_in_buf, ep_in->wMaxPacketSize,
1196                          (usb_complete_t)redrat3_handle_async, rr3);
1197
1198        /* set up bulk-out endpoint*/
1199        rr3->write_urb = usb_alloc_urb(0, GFP_KERNEL);
1200        if (!rr3->write_urb) {
1201                dev_err(dev, "Write urb allocation failure\n");
1202                goto error;
1203        }
1204
1205        rr3->ep_out = ep_out;
1206        rr3->bulk_out_buf = usb_alloc_coherent(udev, ep_out->wMaxPacketSize,
1207                                               GFP_ATOMIC, &rr3->dma_out);
1208        if (!rr3->bulk_out_buf) {
1209                dev_err(dev, "Write buffer allocation failure\n");
1210                goto error;
1211        }
1212
1213        pipe = usb_sndbulkpipe(udev, ep_out->bEndpointAddress);
1214        usb_fill_bulk_urb(rr3->write_urb, udev, pipe,
1215                          rr3->bulk_out_buf, ep_out->wMaxPacketSize,
1216                          (usb_complete_t)redrat3_write_bulk_callback, rr3);
1217
1218        mutex_init(&rr3->lock);
1219        rr3->udev = udev;
1220
1221        redrat3_reset(rr3);
1222        redrat3_get_firmware_rev(rr3);
1223
1224        /* might be all we need to do? */
1225        retval = redrat3_enable_detector(rr3);
1226        if (retval < 0)
1227                goto error;
1228
1229        /* store current hardware timeout, in us, will use for kfifo resets */
1230        rr3->hw_timeout = redrat3_get_timeout(rr3);
1231
1232        /* default.. will get overridden by any sends with a freq defined */
1233        rr3->carrier = 38000;
1234
1235        rr3->rc = redrat3_init_rc_dev(rr3);
1236        if (!rr3->rc)
1237                goto error;
1238
1239        setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3);
1240
1241        /* we can register the device now, as it is ready */
1242        usb_set_intfdata(intf, rr3);
1243
1244        rr3_ftr(dev, "Exiting %s\n", __func__);
1245        return 0;
1246
1247error:
1248        redrat3_delete(rr3, rr3->udev);
1249
1250no_endpoints:
1251        dev_err(dev, "%s: retval = %x", __func__, retval);
1252
1253        return retval;
1254}
1255
1256static void __devexit redrat3_dev_disconnect(struct usb_interface *intf)
1257{
1258        struct usb_device *udev = interface_to_usbdev(intf);
1259        struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1260
1261        rr3_ftr(&intf->dev, "Entering %s\n", __func__);
1262
1263        if (!rr3)
1264                return;
1265
1266        redrat3_disable_detector(rr3);
1267
1268        usb_set_intfdata(intf, NULL);
1269        rc_unregister_device(rr3->rc);
1270        del_timer_sync(&rr3->rx_timeout);
1271        redrat3_delete(rr3, udev);
1272
1273        rr3_ftr(&intf->dev, "RedRat3 IR Transceiver now disconnected\n");
1274}
1275
1276static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1277{
1278        struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1279        rr3_ftr(rr3->dev, "suspend\n");
1280        usb_kill_urb(rr3->read_urb);
1281        return 0;
1282}
1283
1284static int redrat3_dev_resume(struct usb_interface *intf)
1285{
1286        struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1287        rr3_ftr(rr3->dev, "resume\n");
1288        if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
1289                return -EIO;
1290        return 0;
1291}
1292
1293static struct usb_driver redrat3_dev_driver = {
1294        .name           = DRIVER_NAME,
1295        .probe          = redrat3_dev_probe,
1296        .disconnect     = redrat3_dev_disconnect,
1297        .suspend        = redrat3_dev_suspend,
1298        .resume         = redrat3_dev_resume,
1299        .reset_resume   = redrat3_dev_resume,
1300        .id_table       = redrat3_dev_table
1301};
1302
1303static int __init redrat3_dev_init(void)
1304{
1305        int ret;
1306
1307        ret = usb_register(&redrat3_dev_driver);
1308        if (ret < 0)
1309                pr_err(DRIVER_NAME
1310                       ": usb register failed, result = %d\n", ret);
1311
1312        return ret;
1313}
1314
1315static void __exit redrat3_dev_exit(void)
1316{
1317        usb_deregister(&redrat3_dev_driver);
1318}
1319
1320module_init(redrat3_dev_init);
1321module_exit(redrat3_dev_exit);
1322
1323MODULE_DESCRIPTION(DRIVER_DESC);
1324MODULE_AUTHOR(DRIVER_AUTHOR);
1325MODULE_AUTHOR(DRIVER_AUTHOR2);
1326MODULE_LICENSE("GPL");
1327MODULE_DEVICE_TABLE(usb, redrat3_dev_table);
1328
1329module_param(debug, int, S_IRUGO | S_IWUSR);
1330MODULE_PARM_DESC(debug, "Enable module debug spew. 0 = no debugging (default) "
1331                 "0x1 = standard debug messages, 0x2 = function tracing debug. "
1332                 "Flag bits are addative (i.e., 0x3 for both debug types).");
1333