root/drivers/media/rc/imon_raw.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. imon_ir_data
  2. imon_ir_rx
  3. imon_probe
  4. imon_disconnect

   1 // SPDX-License-Identifier: GPL-2.0+
   2 //
   3 // Copyright (C) 2018 Sean Young <sean@mess.org>
   4 
   5 #include <linux/module.h>
   6 #include <linux/usb.h>
   7 #include <linux/usb/input.h>
   8 #include <media/rc-core.h>
   9 
  10 /* Each bit is 250us */
  11 #define BIT_DURATION 250000
  12 
  13 struct imon {
  14         struct device *dev;
  15         struct urb *ir_urb;
  16         struct rc_dev *rcdev;
  17         __be64 ir_buf;
  18         char phys[64];
  19 };
  20 
  21 /*
  22  * The first 5 bytes of data represent IR pulse or space. Each bit, starting
  23  * from highest bit in the first byte, represents 250µs of data. It is 1
  24  * for space and 0 for pulse.
  25  *
  26  * The station sends 10 packets, and the 7th byte will be number 1 to 10, so
  27  * when we receive 10 we assume all the data has arrived.
  28  */
  29 static void imon_ir_data(struct imon *imon)
  30 {
  31         struct ir_raw_event rawir = {};
  32         u64 data = be64_to_cpu(imon->ir_buf);
  33         u8 packet_no = data & 0xff;
  34         int offset = 40;
  35         int bit;
  36 
  37         if (packet_no == 0xff)
  38                 return;
  39 
  40         dev_dbg(imon->dev, "data: %*ph", 8, &imon->ir_buf);
  41 
  42         /*
  43          * Only the first 5 bytes contain IR data. Right shift so we move
  44          * the IR bits to the lower 40 bits.
  45          */
  46         data >>= 24;
  47 
  48         do {
  49                 /*
  50                  * Find highest set bit which is less or equal to offset
  51                  *
  52                  * offset is the bit above (base 0) where we start looking.
  53                  *
  54                  * data & (BIT_ULL(offset) - 1) masks off any unwanted bits,
  55                  * so we have just bits less than offset.
  56                  *
  57                  * fls will tell us the highest bit set plus 1 (or 0 if no
  58                  * bits are set).
  59                  */
  60                 bit = fls64(data & (BIT_ULL(offset) - 1));
  61                 if (bit < offset) {
  62                         dev_dbg(imon->dev, "pulse: %d bits", offset - bit);
  63                         rawir.pulse = true;
  64                         rawir.duration = (offset - bit) * BIT_DURATION;
  65                         ir_raw_event_store_with_filter(imon->rcdev, &rawir);
  66 
  67                         if (bit == 0)
  68                                 break;
  69 
  70                         offset = bit;
  71                 }
  72 
  73                 /*
  74                  * Find highest clear bit which is less than offset.
  75                  *
  76                  * Just invert the data and use same trick as above.
  77                  */
  78                 bit = fls64(~data & (BIT_ULL(offset) - 1));
  79                 dev_dbg(imon->dev, "space: %d bits", offset - bit);
  80 
  81                 rawir.pulse = false;
  82                 rawir.duration = (offset - bit) * BIT_DURATION;
  83                 ir_raw_event_store_with_filter(imon->rcdev, &rawir);
  84 
  85                 offset = bit;
  86         } while (offset > 0);
  87 
  88         if (packet_no == 0x0a && !imon->rcdev->idle) {
  89                 ir_raw_event_set_idle(imon->rcdev, true);
  90                 ir_raw_event_handle(imon->rcdev);
  91         }
  92 }
  93 
  94 static void imon_ir_rx(struct urb *urb)
  95 {
  96         struct imon *imon = urb->context;
  97         int ret;
  98 
  99         switch (urb->status) {
 100         case 0:
 101                 imon_ir_data(imon);
 102                 break;
 103         case -ECONNRESET:
 104         case -ENOENT:
 105         case -ESHUTDOWN:
 106                 usb_unlink_urb(urb);
 107                 return;
 108         case -EPIPE:
 109         default:
 110                 dev_dbg(imon->dev, "error: urb status = %d", urb->status);
 111                 break;
 112         }
 113 
 114         ret = usb_submit_urb(urb, GFP_ATOMIC);
 115         if (ret && ret != -ENODEV)
 116                 dev_warn(imon->dev, "failed to resubmit urb: %d", ret);
 117 }
 118 
 119 static int imon_probe(struct usb_interface *intf,
 120                       const struct usb_device_id *id)
 121 {
 122         struct usb_endpoint_descriptor *ir_ep = NULL;
 123         struct usb_host_interface *idesc;
 124         struct usb_device *udev;
 125         struct rc_dev *rcdev;
 126         struct imon *imon;
 127         int i, ret;
 128 
 129         udev = interface_to_usbdev(intf);
 130         idesc = intf->cur_altsetting;
 131 
 132         for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
 133                 struct usb_endpoint_descriptor *ep = &idesc->endpoint[i].desc;
 134 
 135                 if (usb_endpoint_is_int_in(ep)) {
 136                         ir_ep = ep;
 137                         break;
 138                 }
 139         }
 140 
 141         if (!ir_ep) {
 142                 dev_err(&intf->dev, "IR endpoint missing");
 143                 return -ENODEV;
 144         }
 145 
 146         imon = devm_kmalloc(&intf->dev, sizeof(*imon), GFP_KERNEL);
 147         if (!imon)
 148                 return -ENOMEM;
 149 
 150         imon->ir_urb = usb_alloc_urb(0, GFP_KERNEL);
 151         if (!imon->ir_urb)
 152                 return -ENOMEM;
 153 
 154         imon->dev = &intf->dev;
 155         usb_fill_int_urb(imon->ir_urb, udev,
 156                          usb_rcvintpipe(udev, ir_ep->bEndpointAddress),
 157                          &imon->ir_buf, sizeof(imon->ir_buf),
 158                          imon_ir_rx, imon, ir_ep->bInterval);
 159 
 160         rcdev = devm_rc_allocate_device(&intf->dev, RC_DRIVER_IR_RAW);
 161         if (!rcdev) {
 162                 ret = -ENOMEM;
 163                 goto free_urb;
 164         }
 165 
 166         usb_make_path(udev, imon->phys, sizeof(imon->phys));
 167 
 168         rcdev->device_name = "iMON Station";
 169         rcdev->driver_name = KBUILD_MODNAME;
 170         rcdev->input_phys = imon->phys;
 171         usb_to_input_id(udev, &rcdev->input_id);
 172         rcdev->dev.parent = &intf->dev;
 173         rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
 174         rcdev->map_name = RC_MAP_IMON_RSC;
 175         rcdev->rx_resolution = BIT_DURATION;
 176         rcdev->priv = imon;
 177 
 178         ret = devm_rc_register_device(&intf->dev, rcdev);
 179         if (ret)
 180                 goto free_urb;
 181 
 182         imon->rcdev = rcdev;
 183 
 184         ret = usb_submit_urb(imon->ir_urb, GFP_KERNEL);
 185         if (ret)
 186                 goto free_urb;
 187 
 188         usb_set_intfdata(intf, imon);
 189 
 190         return 0;
 191 
 192 free_urb:
 193         usb_free_urb(imon->ir_urb);
 194         return ret;
 195 }
 196 
 197 static void imon_disconnect(struct usb_interface *intf)
 198 {
 199         struct imon *imon = usb_get_intfdata(intf);
 200 
 201         usb_kill_urb(imon->ir_urb);
 202         usb_free_urb(imon->ir_urb);
 203 }
 204 
 205 static const struct usb_device_id imon_table[] = {
 206         /* SoundGraph iMON (IR only) -- sg_imon.inf */
 207         { USB_DEVICE(0x04e8, 0xff30) },
 208         {}
 209 };
 210 
 211 static struct usb_driver imon_driver = {
 212         .name = KBUILD_MODNAME,
 213         .probe = imon_probe,
 214         .disconnect = imon_disconnect,
 215         .id_table = imon_table
 216 };
 217 
 218 module_usb_driver(imon_driver);
 219 
 220 MODULE_DESCRIPTION("Early raw iMON IR devices");
 221 MODULE_AUTHOR("Sean Young <sean@mess.org>");
 222 MODULE_LICENSE("GPL");
 223 MODULE_DEVICE_TABLE(usb, imon_table);

/* [<][>][^][v][top][bottom][index][help] */