Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * USB RedRat3 IR Transceiver rc-core driver
0004  *
0005  * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com>
0006  *  based heavily on the work of Stephen Cox, with additional
0007  *  help from RedRat Ltd.
0008  *
0009  * This driver began life based on an old version of the first-generation
0010  * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then
0011  * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's
0012  * Chris Dodge.
0013  *
0014  * The driver was then ported to rc-core and significantly rewritten again,
0015  * by Jarod, using the in-kernel mceusb driver as a guide, after an initial
0016  * port effort was started by Stephen.
0017  *
0018  * TODO LIST:
0019  * - fix lirc not showing repeats properly
0020  * --
0021  *
0022  * The RedRat3 is a USB transceiver with both send & receive,
0023  * with 2 separate sensors available for receive to enable
0024  * both good long range reception for general use, and good
0025  * short range reception when required for learning a signal.
0026  *
0027  * http://www.redrat.co.uk/
0028  *
0029  * It uses its own little protocol to communicate, the required
0030  * parts of which are embedded within this driver.
0031  * --
0032  */
0033 
0034 #include <asm/unaligned.h>
0035 #include <linux/device.h>
0036 #include <linux/leds.h>
0037 #include <linux/module.h>
0038 #include <linux/slab.h>
0039 #include <linux/usb.h>
0040 #include <linux/usb/input.h>
0041 #include <media/rc-core.h>
0042 
0043 /* Driver Information */
0044 #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
0045 #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
0046 #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
0047 #define DRIVER_NAME "redrat3"
0048 
0049 /* bulk data transfer types */
0050 #define RR3_ERROR       0x01
0051 #define RR3_MOD_SIGNAL_IN   0x20
0052 #define RR3_MOD_SIGNAL_OUT  0x21
0053 
0054 /* Get the RR firmware version */
0055 #define RR3_FW_VERSION      0xb1
0056 #define RR3_FW_VERSION_LEN  64
0057 /* Send encoded signal bulk-sent earlier*/
0058 #define RR3_TX_SEND_SIGNAL  0xb3
0059 #define RR3_SET_IR_PARAM    0xb7
0060 #define RR3_GET_IR_PARAM    0xb8
0061 /* Blink the red LED on the device */
0062 #define RR3_BLINK_LED       0xb9
0063 /* Read serial number of device */
0064 #define RR3_READ_SER_NO     0xba
0065 #define RR3_SER_NO_LEN      4
0066 /* Start capture with the RC receiver */
0067 #define RR3_RC_DET_ENABLE   0xbb
0068 /* Stop capture with the RC receiver */
0069 #define RR3_RC_DET_DISABLE  0xbc
0070 /* Start capture with the wideband receiver */
0071 #define RR3_MODSIG_CAPTURE     0xb2
0072 /* Return the status of RC detector capture */
0073 #define RR3_RC_DET_STATUS   0xbd
0074 /* Reset redrat */
0075 #define RR3_RESET       0xa0
0076 
0077 /* Max number of lengths in the signal. */
0078 #define RR3_IR_IO_MAX_LENGTHS   0x01
0079 /* Periods to measure mod. freq. */
0080 #define RR3_IR_IO_PERIODS_MF    0x02
0081 /* Size of memory for main signal data */
0082 #define RR3_IR_IO_SIG_MEM_SIZE  0x03
0083 /* Delta value when measuring lengths */
0084 #define RR3_IR_IO_LENGTH_FUZZ   0x04
0085 /* Timeout for end of signal detection */
0086 #define RR3_IR_IO_SIG_TIMEOUT   0x05
0087 /* Minimum value for pause recognition. */
0088 #define RR3_IR_IO_MIN_PAUSE 0x06
0089 
0090 /* Clock freq. of EZ-USB chip */
0091 #define RR3_CLK         24000000
0092 /* Clock periods per timer count */
0093 #define RR3_CLK_PER_COUNT   12
0094 /* (RR3_CLK / RR3_CLK_PER_COUNT) */
0095 #define RR3_CLK_CONV_FACTOR 2000000
0096 /* USB bulk-in wideband IR data endpoint address */
0097 #define RR3_WIDE_IN_EP_ADDR 0x81
0098 /* USB bulk-in narrowband IR data endpoint address */
0099 #define RR3_NARROW_IN_EP_ADDR   0x82
0100 
0101 /* Size of the fixed-length portion of the signal */
0102 #define RR3_DRIVER_MAXLENS  255
0103 #define RR3_MAX_SIG_SIZE    512
0104 #define RR3_TIME_UNIT       50
0105 #define RR3_END_OF_SIGNAL   0x7f
0106 #define RR3_TX_TRAILER_LEN  2
0107 #define RR3_RX_MIN_TIMEOUT  5
0108 #define RR3_RX_MAX_TIMEOUT  2000
0109 
0110 /* The 8051's CPUCS Register address */
0111 #define RR3_CPUCS_REG_ADDR  0x7f92
0112 
0113 #define USB_RR3USB_VENDOR_ID    0x112a
0114 #define USB_RR3USB_PRODUCT_ID   0x0001
0115 #define USB_RR3IIUSB_PRODUCT_ID 0x0005
0116 
0117 
0118 /*
0119  * The redrat3 encodes an IR signal as set of different lengths and a set
0120  * of indices into those lengths. This sets how much two lengths must
0121  * differ before they are considered distinct, the value is specified
0122  * in microseconds.
0123  * Default 5, value 0 to 127.
0124  */
0125 static int length_fuzz = 5;
0126 module_param(length_fuzz, uint, 0644);
0127 MODULE_PARM_DESC(length_fuzz, "Length Fuzz (0-127)");
0128 
0129 /*
0130  * When receiving a continuous ir stream (for example when a user is
0131  * holding a button down on a remote), this specifies the minimum size
0132  * of a space when the redrat3 sends a irdata packet to the host. Specified
0133  * in milliseconds. Default value 18ms.
0134  * The value can be between 2 and 30 inclusive.
0135  */
0136 static int minimum_pause = 18;
0137 module_param(minimum_pause, uint, 0644);
0138 MODULE_PARM_DESC(minimum_pause, "Minimum Pause in ms (2-30)");
0139 
0140 /*
0141  * The carrier frequency is measured during the first pulse of the IR
0142  * signal. The larger the number of periods used To measure, the more
0143  * accurate the result is likely to be, however some signals have short
0144  * initial pulses, so in some case it may be necessary to reduce this value.
0145  * Default 8, value 1 to 255.
0146  */
0147 static int periods_measure_carrier = 8;
0148 module_param(periods_measure_carrier, uint, 0644);
0149 MODULE_PARM_DESC(periods_measure_carrier, "Number of Periods to Measure Carrier (1-255)");
0150 
0151 
0152 struct redrat3_header {
0153     __be16 length;
0154     __be16 transfer_type;
0155 } __packed;
0156 
0157 /* sending and receiving irdata */
0158 struct redrat3_irdata {
0159     struct redrat3_header header;
0160     __be32 pause;
0161     __be16 mod_freq_count;
0162     __be16 num_periods;
0163     __u8 max_lengths;
0164     __u8 no_lengths;
0165     __be16 max_sig_size;
0166     __be16 sig_size;
0167     __u8 no_repeats;
0168     __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
0169     __u8 sigdata[RR3_MAX_SIG_SIZE];
0170 } __packed;
0171 
0172 /* firmware errors */
0173 struct redrat3_error {
0174     struct redrat3_header header;
0175     __be16 fw_error;
0176 } __packed;
0177 
0178 /* table of devices that work with this driver */
0179 static const struct usb_device_id redrat3_dev_table[] = {
0180     /* Original version of the RedRat3 */
0181     {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
0182     /* Second Version/release of the RedRat3 - RetRat3-II */
0183     {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
0184     {}          /* Terminating entry */
0185 };
0186 
0187 /* Structure to hold all of our device specific stuff */
0188 struct redrat3_dev {
0189     /* core device bits */
0190     struct rc_dev *rc;
0191     struct device *dev;
0192 
0193     /* led control */
0194     struct led_classdev led;
0195     atomic_t flash;
0196     struct usb_ctrlrequest flash_control;
0197     struct urb *flash_urb;
0198     u8 flash_in_buf;
0199 
0200     /* learning */
0201     bool wideband;
0202     struct usb_ctrlrequest learn_control;
0203     struct urb *learn_urb;
0204     u8 learn_buf;
0205 
0206     /* save off the usb device pointer */
0207     struct usb_device *udev;
0208 
0209     /* the receive endpoint */
0210     struct usb_endpoint_descriptor *ep_narrow;
0211     /* the buffer to receive data */
0212     void *bulk_in_buf;
0213     /* urb used to read ir data */
0214     struct urb *narrow_urb;
0215     struct urb *wide_urb;
0216 
0217     /* the send endpoint */
0218     struct usb_endpoint_descriptor *ep_out;
0219 
0220     /* usb dma */
0221     dma_addr_t dma_in;
0222 
0223     /* Is the device currently transmitting?*/
0224     bool transmitting;
0225 
0226     /* store for current packet */
0227     struct redrat3_irdata irdata;
0228     u16 bytes_read;
0229 
0230     u32 carrier;
0231 
0232     char name[64];
0233     char phys[64];
0234 };
0235 
0236 static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
0237 {
0238     if (!rr3->transmitting && (code != 0x40))
0239         dev_info(rr3->dev, "fw error code 0x%02x: ", code);
0240 
0241     switch (code) {
0242     case 0x00:
0243         pr_cont("No Error\n");
0244         break;
0245 
0246     /* Codes 0x20 through 0x2f are IR Firmware Errors */
0247     case 0x20:
0248         pr_cont("Initial signal pulse not long enough to measure carrier frequency\n");
0249         break;
0250     case 0x21:
0251         pr_cont("Not enough length values allocated for signal\n");
0252         break;
0253     case 0x22:
0254         pr_cont("Not enough memory allocated for signal data\n");
0255         break;
0256     case 0x23:
0257         pr_cont("Too many signal repeats\n");
0258         break;
0259     case 0x28:
0260         pr_cont("Insufficient memory available for IR signal data memory allocation\n");
0261         break;
0262     case 0x29:
0263         pr_cont("Insufficient memory available for IrDa signal data memory allocation\n");
0264         break;
0265 
0266     /* Codes 0x30 through 0x3f are USB Firmware Errors */
0267     case 0x30:
0268         pr_cont("Insufficient memory available for bulk transfer structure\n");
0269         break;
0270 
0271     /*
0272      * Other error codes... These are primarily errors that can occur in
0273      * the control messages sent to the redrat
0274      */
0275     case 0x40:
0276         if (!rr3->transmitting)
0277             pr_cont("Signal capture has been terminated\n");
0278         break;
0279     case 0x41:
0280         pr_cont("Attempt to set/get and unknown signal I/O algorithm parameter\n");
0281         break;
0282     case 0x42:
0283         pr_cont("Signal capture already started\n");
0284         break;
0285 
0286     default:
0287         pr_cont("Unknown Error\n");
0288         break;
0289     }
0290 }
0291 
0292 static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
0293 {
0294     u32 mod_freq = 0;
0295     u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
0296 
0297     if (mod_freq_count != 0)
0298         mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
0299             (mod_freq_count * RR3_CLK_PER_COUNT);
0300 
0301     return mod_freq;
0302 }
0303 
0304 /* this function scales down the figures for the same result... */
0305 static u32 redrat3_len_to_us(u32 length)
0306 {
0307     u32 biglen = length * 1000;
0308     u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
0309     u32 result = (u32) (biglen / divisor);
0310 
0311     /* don't allow zero lengths to go back, breaks lirc */
0312     return result ? result : 1;
0313 }
0314 
0315 /*
0316  * convert us back into redrat3 lengths
0317  *
0318  * length * 1000   length * 1000000
0319  * ------------- = ---------------- = micro
0320  * rr3clk / 1000       rr3clk
0321 
0322  * 6 * 2       4 * 3        micro * rr3clk          micro * rr3clk / 1000
0323  * ----- = 4   ----- = 6    -------------- = len    ---------------------
0324  *   3           2             1000000                    1000
0325  */
0326 static u32 redrat3_us_to_len(u32 microsec)
0327 {
0328     u32 result;
0329     u32 divisor;
0330 
0331     microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec;
0332     divisor = (RR3_CLK_CONV_FACTOR / 1000);
0333     result = (u32)(microsec * divisor) / 1000;
0334 
0335     /* don't allow zero lengths to go back, breaks lirc */
0336     return result ? result : 1;
0337 }
0338 
0339 static void redrat3_process_ir_data(struct redrat3_dev *rr3)
0340 {
0341     struct ir_raw_event rawir = {};
0342     struct device *dev;
0343     unsigned int i, sig_size, offset, val;
0344     u32 mod_freq;
0345 
0346     dev = rr3->dev;
0347 
0348     mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
0349     dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
0350     if (mod_freq && rr3->wideband) {
0351         struct ir_raw_event ev = {
0352             .carrier_report = 1,
0353             .carrier = mod_freq
0354         };
0355 
0356         ir_raw_event_store(rr3->rc, &ev);
0357     }
0358 
0359     /* process each rr3 encoded byte into an int */
0360     sig_size = be16_to_cpu(rr3->irdata.sig_size);
0361     for (i = 0; i < sig_size; i++) {
0362         offset = rr3->irdata.sigdata[i];
0363         val = get_unaligned_be16(&rr3->irdata.lens[offset]);
0364 
0365         /* we should always get pulse/space/pulse/space samples */
0366         if (i % 2)
0367             rawir.pulse = false;
0368         else
0369             rawir.pulse = true;
0370 
0371         rawir.duration = redrat3_len_to_us(val);
0372         /* cap the value to IR_MAX_DURATION */
0373         rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
0374                  IR_MAX_DURATION : rawir.duration;
0375 
0376         dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
0377             rawir.pulse ? "pulse" : "space", rawir.duration, i);
0378         ir_raw_event_store_with_filter(rr3->rc, &rawir);
0379     }
0380 
0381     /* add a trailing space */
0382     rawir.pulse = false;
0383     rawir.timeout = true;
0384     rawir.duration = rr3->rc->timeout;
0385     dev_dbg(dev, "storing trailing timeout with duration %d\n",
0386                             rawir.duration);
0387     ir_raw_event_store_with_filter(rr3->rc, &rawir);
0388 
0389     dev_dbg(dev, "calling ir_raw_event_handle\n");
0390     ir_raw_event_handle(rr3->rc);
0391 }
0392 
0393 /* Util fn to send rr3 cmds */
0394 static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
0395 {
0396     struct usb_device *udev;
0397     u8 *data;
0398     int res;
0399 
0400     data = kzalloc(sizeof(u8), GFP_KERNEL);
0401     if (!data)
0402         return -ENOMEM;
0403 
0404     udev = rr3->udev;
0405     res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
0406                   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0407                   0x0000, 0x0000, data, sizeof(u8), 10000);
0408 
0409     if (res < 0) {
0410         dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
0411             __func__, res, *data);
0412         res = -EIO;
0413     } else
0414         res = data[0];
0415 
0416     kfree(data);
0417 
0418     return res;
0419 }
0420 
0421 /* Enables the long range detector and starts async receive */
0422 static int redrat3_enable_detector(struct redrat3_dev *rr3)
0423 {
0424     struct device *dev = rr3->dev;
0425     u8 ret;
0426 
0427     ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
0428     if (ret != 0)
0429         dev_dbg(dev, "%s: unexpected ret of %d\n",
0430             __func__, ret);
0431 
0432     ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
0433     if (ret != 1) {
0434         dev_err(dev, "%s: detector status: %d, should be 1\n",
0435             __func__, ret);
0436         return -EIO;
0437     }
0438 
0439     ret = usb_submit_urb(rr3->narrow_urb, GFP_KERNEL);
0440     if (ret) {
0441         dev_err(rr3->dev, "narrow band urb failed: %d", ret);
0442         return ret;
0443     }
0444 
0445     ret = usb_submit_urb(rr3->wide_urb, GFP_KERNEL);
0446     if (ret)
0447         dev_err(rr3->dev, "wide band urb failed: %d", ret);
0448 
0449     return ret;
0450 }
0451 
0452 static inline void redrat3_delete(struct redrat3_dev *rr3,
0453                   struct usb_device *udev)
0454 {
0455     usb_kill_urb(rr3->narrow_urb);
0456     usb_kill_urb(rr3->wide_urb);
0457     usb_kill_urb(rr3->flash_urb);
0458     usb_kill_urb(rr3->learn_urb);
0459     usb_free_urb(rr3->narrow_urb);
0460     usb_free_urb(rr3->wide_urb);
0461     usb_free_urb(rr3->flash_urb);
0462     usb_free_urb(rr3->learn_urb);
0463     usb_free_coherent(udev, le16_to_cpu(rr3->ep_narrow->wMaxPacketSize),
0464               rr3->bulk_in_buf, rr3->dma_in);
0465 
0466     kfree(rr3);
0467 }
0468 
0469 static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
0470 {
0471     __be32 *tmp;
0472     u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
0473     int len, ret, pipe;
0474 
0475     len = sizeof(*tmp);
0476     tmp = kzalloc(len, GFP_KERNEL);
0477     if (!tmp)
0478         return timeout;
0479 
0480     pipe = usb_rcvctrlpipe(rr3->udev, 0);
0481     ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
0482                   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0483                   RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, 5000);
0484     if (ret != len)
0485         dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
0486     else {
0487         timeout = redrat3_len_to_us(be32_to_cpup(tmp));
0488 
0489         dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
0490     }
0491 
0492     kfree(tmp);
0493 
0494     return timeout;
0495 }
0496 
0497 static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutus)
0498 {
0499     struct redrat3_dev *rr3 = rc_dev->priv;
0500     struct usb_device *udev = rr3->udev;
0501     struct device *dev = rr3->dev;
0502     __be32 *timeout;
0503     int ret;
0504 
0505     timeout = kmalloc(sizeof(*timeout), GFP_KERNEL);
0506     if (!timeout)
0507         return -ENOMEM;
0508 
0509     *timeout = cpu_to_be32(redrat3_us_to_len(timeoutus));
0510     ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM,
0511              USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0512              RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout),
0513              25000);
0514     dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n",
0515                         be32_to_cpu(*timeout), ret);
0516 
0517     if (ret == sizeof(*timeout))
0518         ret = 0;
0519     else if (ret >= 0)
0520         ret = -EIO;
0521 
0522     kfree(timeout);
0523 
0524     return ret;
0525 }
0526 
0527 static void redrat3_reset(struct redrat3_dev *rr3)
0528 {
0529     struct usb_device *udev = rr3->udev;
0530     struct device *dev = rr3->dev;
0531     int rc, rxpipe, txpipe;
0532     u8 *val;
0533     size_t const len = sizeof(*val);
0534 
0535     rxpipe = usb_rcvctrlpipe(udev, 0);
0536     txpipe = usb_sndctrlpipe(udev, 0);
0537 
0538     val = kmalloc(len, GFP_KERNEL);
0539     if (!val)
0540         return;
0541 
0542     *val = 0x01;
0543     rc = usb_control_msg(udev, rxpipe, RR3_RESET,
0544                  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0545                  RR3_CPUCS_REG_ADDR, 0, val, len, 25000);
0546     dev_dbg(dev, "reset returned 0x%02x\n", rc);
0547 
0548     *val = length_fuzz;
0549     rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
0550                  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0551                  RR3_IR_IO_LENGTH_FUZZ, 0, val, len, 25000);
0552     dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
0553 
0554     *val = (65536 - (minimum_pause * 2000)) / 256;
0555     rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
0556                  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0557                  RR3_IR_IO_MIN_PAUSE, 0, val, len, 25000);
0558     dev_dbg(dev, "set ir parm min pause %d rc 0x%02x\n", *val, rc);
0559 
0560     *val = periods_measure_carrier;
0561     rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
0562                  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0563                  RR3_IR_IO_PERIODS_MF, 0, val, len, 25000);
0564     dev_dbg(dev, "set ir parm periods measure carrier %d rc 0x%02x", *val,
0565                                     rc);
0566 
0567     *val = RR3_DRIVER_MAXLENS;
0568     rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
0569                  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
0570                  RR3_IR_IO_MAX_LENGTHS, 0, val, len, 25000);
0571     dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
0572 
0573     kfree(val);
0574 }
0575 
0576 static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
0577 {
0578     int rc;
0579     char *buffer;
0580 
0581     buffer = kcalloc(RR3_FW_VERSION_LEN + 1, sizeof(*buffer), GFP_KERNEL);
0582     if (!buffer)
0583         return;
0584 
0585     rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
0586                  RR3_FW_VERSION,
0587                  USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0588                  0, 0, buffer, RR3_FW_VERSION_LEN, 5000);
0589 
0590     if (rc >= 0)
0591         dev_info(rr3->dev, "Firmware rev: %s", buffer);
0592     else
0593         dev_err(rr3->dev, "Problem fetching firmware ID\n");
0594 
0595     kfree(buffer);
0596 }
0597 
0598 static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
0599 {
0600     struct redrat3_header *header = rr3->bulk_in_buf;
0601     unsigned pktlen, pkttype;
0602 
0603     /* grab the Length and type of transfer */
0604     pktlen = be16_to_cpu(header->length);
0605     pkttype = be16_to_cpu(header->transfer_type);
0606 
0607     if (pktlen > sizeof(rr3->irdata)) {
0608         dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
0609         return;
0610     }
0611 
0612     switch (pkttype) {
0613     case RR3_ERROR:
0614         if (len >= sizeof(struct redrat3_error)) {
0615             struct redrat3_error *error = rr3->bulk_in_buf;
0616             unsigned fw_error = be16_to_cpu(error->fw_error);
0617             redrat3_dump_fw_error(rr3, fw_error);
0618         }
0619         break;
0620 
0621     case RR3_MOD_SIGNAL_IN:
0622         memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
0623         rr3->bytes_read = len;
0624         dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
0625             rr3->bytes_read, pktlen);
0626         break;
0627 
0628     default:
0629         dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
0630                         pkttype, len, pktlen);
0631         break;
0632     }
0633 }
0634 
0635 static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
0636 {
0637     void *irdata = &rr3->irdata;
0638 
0639     if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
0640         dev_warn(rr3->dev, "too much data for packet\n");
0641         rr3->bytes_read = 0;
0642         return;
0643     }
0644 
0645     memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
0646 
0647     rr3->bytes_read += len;
0648     dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
0649                  be16_to_cpu(rr3->irdata.header.length));
0650 }
0651 
0652 /* gather IR data from incoming urb, process it when we have enough */
0653 static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
0654 {
0655     struct device *dev = rr3->dev;
0656     unsigned pkttype;
0657     int ret = 0;
0658 
0659     if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
0660         redrat3_read_packet_start(rr3, len);
0661     } else if (rr3->bytes_read != 0) {
0662         redrat3_read_packet_continue(rr3, len);
0663     } else if (rr3->bytes_read == 0) {
0664         dev_err(dev, "error: no packet data read\n");
0665         ret = -ENODATA;
0666         goto out;
0667     }
0668 
0669     if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
0670                         sizeof(struct redrat3_header))
0671         /* we're still accumulating data */
0672         return 0;
0673 
0674     /* if we get here, we've got IR data to decode */
0675     pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
0676     if (pkttype == RR3_MOD_SIGNAL_IN)
0677         redrat3_process_ir_data(rr3);
0678     else
0679         dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
0680                                 pkttype);
0681 
0682 out:
0683     rr3->bytes_read = 0;
0684     return ret;
0685 }
0686 
0687 /* callback function from USB when async USB request has completed */
0688 static void redrat3_handle_async(struct urb *urb)
0689 {
0690     struct redrat3_dev *rr3 = urb->context;
0691     int ret;
0692 
0693     switch (urb->status) {
0694     case 0:
0695         ret = redrat3_get_ir_data(rr3, urb->actual_length);
0696         if (!ret && rr3->wideband && !rr3->learn_urb->hcpriv) {
0697             ret = usb_submit_urb(rr3->learn_urb, GFP_ATOMIC);
0698             if (ret)
0699                 dev_err(rr3->dev, "Failed to submit learning urb: %d",
0700                                     ret);
0701         }
0702 
0703         if (!ret) {
0704             /* no error, prepare to read more */
0705             ret = usb_submit_urb(urb, GFP_ATOMIC);
0706             if (ret)
0707                 dev_err(rr3->dev, "Failed to resubmit urb: %d",
0708                                     ret);
0709         }
0710         break;
0711 
0712     case -ECONNRESET:
0713     case -ENOENT:
0714     case -ESHUTDOWN:
0715         usb_unlink_urb(urb);
0716         return;
0717 
0718     case -EPIPE:
0719     default:
0720         dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
0721         rr3->bytes_read = 0;
0722         break;
0723     }
0724 }
0725 
0726 static u16 mod_freq_to_val(unsigned int mod_freq)
0727 {
0728     int mult = 6000000;
0729 
0730     /* Clk used in mod. freq. generation is CLK24/4. */
0731     return 65536 - (mult / mod_freq);
0732 }
0733 
0734 static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
0735 {
0736     struct redrat3_dev *rr3 = rcdev->priv;
0737     struct device *dev = rr3->dev;
0738 
0739     dev_dbg(dev, "Setting modulation frequency to %u", carrier);
0740     if (carrier == 0)
0741         return -EINVAL;
0742 
0743     rr3->carrier = carrier;
0744 
0745     return 0;
0746 }
0747 
0748 static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
0749                 unsigned count)
0750 {
0751     struct redrat3_dev *rr3 = rcdev->priv;
0752     struct device *dev = rr3->dev;
0753     struct redrat3_irdata *irdata = NULL;
0754     int ret, ret_len;
0755     int lencheck, cur_sample_len, pipe;
0756     int *sample_lens = NULL;
0757     u8 curlencheck = 0;
0758     unsigned i, sendbuf_len;
0759 
0760     if (rr3->transmitting) {
0761         dev_warn(dev, "%s: transmitter already in use\n", __func__);
0762         return -EAGAIN;
0763     }
0764 
0765     if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
0766         return -EINVAL;
0767 
0768     /* rr3 will disable rc detector on transmit */
0769     rr3->transmitting = true;
0770 
0771     sample_lens = kcalloc(RR3_DRIVER_MAXLENS,
0772                   sizeof(*sample_lens),
0773                   GFP_KERNEL);
0774     if (!sample_lens)
0775         return -ENOMEM;
0776 
0777     irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
0778     if (!irdata) {
0779         ret = -ENOMEM;
0780         goto out;
0781     }
0782 
0783     for (i = 0; i < count; i++) {
0784         cur_sample_len = redrat3_us_to_len(txbuf[i]);
0785         if (cur_sample_len > 0xffff) {
0786             dev_warn(dev, "transmit period of %uus truncated to %uus\n",
0787                     txbuf[i], redrat3_len_to_us(0xffff));
0788             cur_sample_len = 0xffff;
0789         }
0790         for (lencheck = 0; lencheck < curlencheck; lencheck++) {
0791             if (sample_lens[lencheck] == cur_sample_len)
0792                 break;
0793         }
0794         if (lencheck == curlencheck) {
0795             dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
0796                 i, txbuf[i], curlencheck, cur_sample_len);
0797             if (curlencheck < RR3_DRIVER_MAXLENS) {
0798                 /* now convert the value to a proper
0799                  * rr3 value.. */
0800                 sample_lens[curlencheck] = cur_sample_len;
0801                 put_unaligned_be16(cur_sample_len,
0802                         &irdata->lens[curlencheck]);
0803                 curlencheck++;
0804             } else {
0805                 ret = -EINVAL;
0806                 goto out;
0807             }
0808         }
0809         irdata->sigdata[i] = lencheck;
0810     }
0811 
0812     irdata->sigdata[count] = RR3_END_OF_SIGNAL;
0813     irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
0814 
0815     sendbuf_len = offsetof(struct redrat3_irdata,
0816                     sigdata[count + RR3_TX_TRAILER_LEN]);
0817     /* fill in our packet header */
0818     irdata->header.length = cpu_to_be16(sendbuf_len -
0819                         sizeof(struct redrat3_header));
0820     irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
0821     irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
0822     irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
0823     irdata->no_lengths = curlencheck;
0824     irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
0825 
0826     pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
0827     ret = usb_bulk_msg(rr3->udev, pipe, irdata,
0828                 sendbuf_len, &ret_len, 10000);
0829     dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
0830 
0831     /* now tell the hardware to transmit what we sent it */
0832     pipe = usb_rcvctrlpipe(rr3->udev, 0);
0833     ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
0834                   USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0835                   0, 0, irdata, 2, 10000);
0836 
0837     if (ret < 0)
0838         dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
0839     else
0840         ret = count;
0841 
0842 out:
0843     kfree(irdata);
0844     kfree(sample_lens);
0845 
0846     rr3->transmitting = false;
0847     /* rr3 re-enables rc detector because it was enabled before */
0848 
0849     return ret;
0850 }
0851 
0852 static void redrat3_brightness_set(struct led_classdev *led_dev, enum
0853                         led_brightness brightness)
0854 {
0855     struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
0856                                     led);
0857 
0858     if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
0859         int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
0860         if (ret != 0) {
0861             dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
0862                 __func__, ret);
0863             atomic_set(&rr3->flash, 0);
0864         }
0865     }
0866 }
0867 
0868 static int redrat3_wideband_receiver(struct rc_dev *rcdev, int enable)
0869 {
0870     struct redrat3_dev *rr3 = rcdev->priv;
0871     int ret = 0;
0872 
0873     rr3->wideband = enable != 0;
0874 
0875     if (enable) {
0876         ret = usb_submit_urb(rr3->learn_urb, GFP_KERNEL);
0877         if (ret)
0878             dev_err(rr3->dev, "Failed to submit learning urb: %d",
0879                                     ret);
0880     }
0881 
0882     return ret;
0883 }
0884 
0885 static void redrat3_learn_complete(struct urb *urb)
0886 {
0887     struct redrat3_dev *rr3 = urb->context;
0888 
0889     switch (urb->status) {
0890     case 0:
0891         break;
0892     case -ECONNRESET:
0893     case -ENOENT:
0894     case -ESHUTDOWN:
0895         usb_unlink_urb(urb);
0896         return;
0897     case -EPIPE:
0898     default:
0899         dev_err(rr3->dev, "Error: learn urb status = %d", urb->status);
0900         break;
0901     }
0902 }
0903 
0904 static void redrat3_led_complete(struct urb *urb)
0905 {
0906     struct redrat3_dev *rr3 = urb->context;
0907 
0908     switch (urb->status) {
0909     case 0:
0910         break;
0911     case -ECONNRESET:
0912     case -ENOENT:
0913     case -ESHUTDOWN:
0914         usb_unlink_urb(urb);
0915         return;
0916     case -EPIPE:
0917     default:
0918         dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
0919         break;
0920     }
0921 
0922     rr3->led.brightness = LED_OFF;
0923     atomic_dec(&rr3->flash);
0924 }
0925 
0926 static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
0927 {
0928     struct device *dev = rr3->dev;
0929     struct rc_dev *rc;
0930     int ret;
0931     u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
0932 
0933     rc = rc_allocate_device(RC_DRIVER_IR_RAW);
0934     if (!rc)
0935         return NULL;
0936 
0937     snprintf(rr3->name, sizeof(rr3->name),
0938          "RedRat3%s Infrared Remote Transceiver",
0939          prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "");
0940 
0941     usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
0942 
0943     rc->device_name = rr3->name;
0944     rc->input_phys = rr3->phys;
0945     usb_to_input_id(rr3->udev, &rc->input_id);
0946     rc->dev.parent = dev;
0947     rc->priv = rr3;
0948     rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
0949     rc->min_timeout = MS_TO_US(RR3_RX_MIN_TIMEOUT);
0950     rc->max_timeout = MS_TO_US(RR3_RX_MAX_TIMEOUT);
0951     rc->timeout = redrat3_get_timeout(rr3);
0952     rc->s_timeout = redrat3_set_timeout;
0953     rc->tx_ir = redrat3_transmit_ir;
0954     rc->s_tx_carrier = redrat3_set_tx_carrier;
0955     rc->s_carrier_report = redrat3_wideband_receiver;
0956     rc->driver_name = DRIVER_NAME;
0957     rc->rx_resolution = 2;
0958     rc->map_name = RC_MAP_HAUPPAUGE;
0959 
0960     ret = rc_register_device(rc);
0961     if (ret < 0) {
0962         dev_err(dev, "remote dev registration failed\n");
0963         goto out;
0964     }
0965 
0966     return rc;
0967 
0968 out:
0969     rc_free_device(rc);
0970     return NULL;
0971 }
0972 
0973 static int redrat3_dev_probe(struct usb_interface *intf,
0974                  const struct usb_device_id *id)
0975 {
0976     struct usb_device *udev = interface_to_usbdev(intf);
0977     struct device *dev = &intf->dev;
0978     struct usb_host_interface *uhi;
0979     struct redrat3_dev *rr3;
0980     struct usb_endpoint_descriptor *ep;
0981     struct usb_endpoint_descriptor *ep_narrow = NULL;
0982     struct usb_endpoint_descriptor *ep_wide = NULL;
0983     struct usb_endpoint_descriptor *ep_out = NULL;
0984     u8 addr, attrs;
0985     int pipe, i;
0986     int retval = -ENOMEM;
0987 
0988     uhi = intf->cur_altsetting;
0989 
0990     /* find our bulk-in and bulk-out endpoints */
0991     for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
0992         ep = &uhi->endpoint[i].desc;
0993         addr = ep->bEndpointAddress;
0994         attrs = ep->bmAttributes;
0995 
0996         if (((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
0997             ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
0998              USB_ENDPOINT_XFER_BULK)) {
0999             dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
1000                 ep->bEndpointAddress);
1001             /* data comes in on 0x82, 0x81 is for learning */
1002             if (ep->bEndpointAddress == RR3_NARROW_IN_EP_ADDR)
1003                 ep_narrow = ep;
1004             if (ep->bEndpointAddress == RR3_WIDE_IN_EP_ADDR)
1005                 ep_wide = ep;
1006         }
1007 
1008         if ((ep_out == NULL) &&
1009             ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
1010             ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
1011              USB_ENDPOINT_XFER_BULK)) {
1012             dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
1013                 ep->bEndpointAddress);
1014             ep_out = ep;
1015         }
1016     }
1017 
1018     if (!ep_narrow || !ep_out || !ep_wide) {
1019         dev_err(dev, "Couldn't find all endpoints\n");
1020         retval = -ENODEV;
1021         goto no_endpoints;
1022     }
1023 
1024     /* allocate memory for our device state and initialize it */
1025     rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
1026     if (!rr3)
1027         goto no_endpoints;
1028 
1029     rr3->dev = &intf->dev;
1030     rr3->ep_narrow = ep_narrow;
1031     rr3->ep_out = ep_out;
1032     rr3->udev = udev;
1033 
1034     /* set up bulk-in endpoint */
1035     rr3->narrow_urb = usb_alloc_urb(0, GFP_KERNEL);
1036     if (!rr3->narrow_urb)
1037         goto redrat_free;
1038 
1039     rr3->wide_urb = usb_alloc_urb(0, GFP_KERNEL);
1040     if (!rr3->wide_urb)
1041         goto redrat_free;
1042 
1043     rr3->bulk_in_buf = usb_alloc_coherent(udev,
1044         le16_to_cpu(ep_narrow->wMaxPacketSize),
1045         GFP_KERNEL, &rr3->dma_in);
1046     if (!rr3->bulk_in_buf)
1047         goto redrat_free;
1048 
1049     pipe = usb_rcvbulkpipe(udev, ep_narrow->bEndpointAddress);
1050     usb_fill_bulk_urb(rr3->narrow_urb, udev, pipe, rr3->bulk_in_buf,
1051         le16_to_cpu(ep_narrow->wMaxPacketSize),
1052         redrat3_handle_async, rr3);
1053     rr3->narrow_urb->transfer_dma = rr3->dma_in;
1054     rr3->narrow_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1055 
1056     pipe = usb_rcvbulkpipe(udev, ep_wide->bEndpointAddress);
1057     usb_fill_bulk_urb(rr3->wide_urb, udev, pipe, rr3->bulk_in_buf,
1058         le16_to_cpu(ep_narrow->wMaxPacketSize),
1059         redrat3_handle_async, rr3);
1060     rr3->wide_urb->transfer_dma = rr3->dma_in;
1061     rr3->wide_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1062 
1063     redrat3_reset(rr3);
1064     redrat3_get_firmware_rev(rr3);
1065 
1066     /* default.. will get overridden by any sends with a freq defined */
1067     rr3->carrier = 38000;
1068 
1069     atomic_set(&rr3->flash, 0);
1070     rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
1071     if (!rr3->flash_urb)
1072         goto redrat_free;
1073 
1074     /* learn urb */
1075     rr3->learn_urb = usb_alloc_urb(0, GFP_KERNEL);
1076     if (!rr3->learn_urb)
1077         goto redrat_free;
1078 
1079     /* setup packet is 'c0 b2 0000 0000 0001' */
1080     rr3->learn_control.bRequestType = 0xc0;
1081     rr3->learn_control.bRequest = RR3_MODSIG_CAPTURE;
1082     rr3->learn_control.wLength = cpu_to_le16(1);
1083 
1084     usb_fill_control_urb(rr3->learn_urb, udev, usb_rcvctrlpipe(udev, 0),
1085             (unsigned char *)&rr3->learn_control,
1086             &rr3->learn_buf, sizeof(rr3->learn_buf),
1087             redrat3_learn_complete, rr3);
1088 
1089     /* setup packet is 'c0 b9 0000 0000 0001' */
1090     rr3->flash_control.bRequestType = 0xc0;
1091     rr3->flash_control.bRequest = RR3_BLINK_LED;
1092     rr3->flash_control.wLength = cpu_to_le16(1);
1093 
1094     usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
1095             (unsigned char *)&rr3->flash_control,
1096             &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
1097             redrat3_led_complete, rr3);
1098 
1099     /* led control */
1100     rr3->led.name = "redrat3:red:feedback";
1101     rr3->led.default_trigger = "rc-feedback";
1102     rr3->led.brightness_set = redrat3_brightness_set;
1103     retval = led_classdev_register(&intf->dev, &rr3->led);
1104     if (retval)
1105         goto redrat_free;
1106 
1107     rr3->rc = redrat3_init_rc_dev(rr3);
1108     if (!rr3->rc) {
1109         retval = -ENOMEM;
1110         goto led_free;
1111     }
1112 
1113     /* might be all we need to do? */
1114     retval = redrat3_enable_detector(rr3);
1115     if (retval < 0)
1116         goto led_free;
1117 
1118     /* we can register the device now, as it is ready */
1119     usb_set_intfdata(intf, rr3);
1120 
1121     return 0;
1122 
1123 led_free:
1124     led_classdev_unregister(&rr3->led);
1125 redrat_free:
1126     redrat3_delete(rr3, rr3->udev);
1127 
1128 no_endpoints:
1129     return retval;
1130 }
1131 
1132 static void redrat3_dev_disconnect(struct usb_interface *intf)
1133 {
1134     struct usb_device *udev = interface_to_usbdev(intf);
1135     struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1136 
1137     usb_set_intfdata(intf, NULL);
1138     rc_unregister_device(rr3->rc);
1139     led_classdev_unregister(&rr3->led);
1140     redrat3_delete(rr3, udev);
1141 }
1142 
1143 static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1144 {
1145     struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1146 
1147     led_classdev_suspend(&rr3->led);
1148     usb_kill_urb(rr3->narrow_urb);
1149     usb_kill_urb(rr3->wide_urb);
1150     usb_kill_urb(rr3->flash_urb);
1151     return 0;
1152 }
1153 
1154 static int redrat3_dev_resume(struct usb_interface *intf)
1155 {
1156     struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1157 
1158     if (usb_submit_urb(rr3->narrow_urb, GFP_NOIO))
1159         return -EIO;
1160     if (usb_submit_urb(rr3->wide_urb, GFP_NOIO))
1161         return -EIO;
1162     led_classdev_resume(&rr3->led);
1163     return 0;
1164 }
1165 
1166 static struct usb_driver redrat3_dev_driver = {
1167     .name       = DRIVER_NAME,
1168     .probe      = redrat3_dev_probe,
1169     .disconnect = redrat3_dev_disconnect,
1170     .suspend    = redrat3_dev_suspend,
1171     .resume     = redrat3_dev_resume,
1172     .reset_resume   = redrat3_dev_resume,
1173     .id_table   = redrat3_dev_table
1174 };
1175 
1176 module_usb_driver(redrat3_dev_driver);
1177 
1178 MODULE_DESCRIPTION(DRIVER_DESC);
1179 MODULE_AUTHOR(DRIVER_AUTHOR);
1180 MODULE_AUTHOR(DRIVER_AUTHOR2);
1181 MODULE_LICENSE("GPL");
1182 MODULE_DEVICE_TABLE(usb, redrat3_dev_table);