Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * IgorPlug-USB IR Receiver
0004  *
0005  * Copyright (C) 2014 Sean Young <sean@mess.org>
0006  *
0007  * Supports the standard homebrew IgorPlugUSB receiver with Igor's firmware.
0008  * See http://www.cesko.host.sk/IgorPlugUSB/IgorPlug-USB%20(AVR)_eng.htm
0009  *
0010  * Based on the lirc_igorplugusb.c driver:
0011  *  Copyright (C) 2004 Jan M. Hochstein
0012  *  <hochstein@algo.informatik.tu-darmstadt.de>
0013  */
0014 #include <linux/device.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/usb.h>
0018 #include <linux/usb/input.h>
0019 #include <media/rc-core.h>
0020 
0021 #define DRIVER_DESC     "IgorPlug-USB IR Receiver"
0022 #define DRIVER_NAME     "igorplugusb"
0023 
0024 #define HEADERLEN       3
0025 #define BUFLEN          36
0026 #define MAX_PACKET      (HEADERLEN + BUFLEN)
0027 
0028 #define SET_INFRABUFFER_EMPTY   1
0029 #define GET_INFRACODE       2
0030 
0031 
0032 struct igorplugusb {
0033     struct rc_dev *rc;
0034     struct device *dev;
0035 
0036     struct urb *urb;
0037     struct usb_ctrlrequest request;
0038 
0039     struct timer_list timer;
0040 
0041     u8 *buf_in;
0042 
0043     char phys[64];
0044 };
0045 
0046 static void igorplugusb_cmd(struct igorplugusb *ir, int cmd);
0047 
0048 static void igorplugusb_irdata(struct igorplugusb *ir, unsigned len)
0049 {
0050     struct ir_raw_event rawir = {};
0051     unsigned i, start, overflow;
0052 
0053     dev_dbg(ir->dev, "irdata: %*ph (len=%u)", len, ir->buf_in, len);
0054 
0055     /*
0056      * If more than 36 pulses and spaces follow each other, the igorplugusb
0057      * overwrites its buffer from the beginning. The overflow value is the
0058      * last offset which was not overwritten. Everything from this offset
0059      * onwards occurred before everything until this offset.
0060      */
0061     overflow = ir->buf_in[2];
0062     i = start = overflow + HEADERLEN;
0063 
0064     if (start >= len) {
0065         dev_err(ir->dev, "receive overflow invalid: %u", overflow);
0066     } else {
0067         if (overflow > 0) {
0068             dev_warn(ir->dev, "receive overflow, at least %u lost",
0069                                 overflow);
0070             ir_raw_event_overflow(ir->rc);
0071         }
0072 
0073         do {
0074             rawir.duration = ir->buf_in[i] * 85;
0075             rawir.pulse = i & 1;
0076 
0077             ir_raw_event_store_with_filter(ir->rc, &rawir);
0078 
0079             if (++i == len)
0080                 i = HEADERLEN;
0081         } while (i != start);
0082 
0083         /* add a trailing space */
0084         rawir.duration = ir->rc->timeout;
0085         rawir.pulse = false;
0086         ir_raw_event_store_with_filter(ir->rc, &rawir);
0087 
0088         ir_raw_event_handle(ir->rc);
0089     }
0090 
0091     igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
0092 }
0093 
0094 static void igorplugusb_callback(struct urb *urb)
0095 {
0096     struct usb_ctrlrequest *req;
0097     struct igorplugusb *ir = urb->context;
0098 
0099     req = (struct usb_ctrlrequest *)urb->setup_packet;
0100 
0101     switch (urb->status) {
0102     case 0:
0103         if (req->bRequest == GET_INFRACODE &&
0104                     urb->actual_length > HEADERLEN)
0105             igorplugusb_irdata(ir, urb->actual_length);
0106         else /* request IR */
0107             mod_timer(&ir->timer, jiffies + msecs_to_jiffies(50));
0108         break;
0109     case -EPROTO:
0110     case -ECONNRESET:
0111     case -ENOENT:
0112     case -ESHUTDOWN:
0113         return;
0114     default:
0115         dev_warn(ir->dev, "Error: urb status = %d\n", urb->status);
0116         igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
0117         break;
0118     }
0119 }
0120 
0121 static void igorplugusb_cmd(struct igorplugusb *ir, int cmd)
0122 {
0123     int ret;
0124 
0125     ir->request.bRequest = cmd;
0126     ir->urb->transfer_flags = 0;
0127     ret = usb_submit_urb(ir->urb, GFP_ATOMIC);
0128     if (ret && ret != -EPERM)
0129         dev_err(ir->dev, "submit urb failed: %d", ret);
0130 }
0131 
0132 static void igorplugusb_timer(struct timer_list *t)
0133 {
0134     struct igorplugusb *ir = from_timer(ir, t, timer);
0135 
0136     igorplugusb_cmd(ir, GET_INFRACODE);
0137 }
0138 
0139 static int igorplugusb_probe(struct usb_interface *intf,
0140                     const struct usb_device_id *id)
0141 {
0142     struct usb_device *udev;
0143     struct usb_host_interface *idesc;
0144     struct usb_endpoint_descriptor *ep;
0145     struct igorplugusb *ir;
0146     struct rc_dev *rc;
0147     int ret = -ENOMEM;
0148 
0149     udev = interface_to_usbdev(intf);
0150     idesc = intf->cur_altsetting;
0151 
0152     if (idesc->desc.bNumEndpoints != 1) {
0153         dev_err(&intf->dev, "incorrect number of endpoints");
0154         return -ENODEV;
0155     }
0156 
0157     ep = &idesc->endpoint[0].desc;
0158     if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_control(ep)) {
0159         dev_err(&intf->dev, "endpoint incorrect");
0160         return -ENODEV;
0161     }
0162 
0163     ir = devm_kzalloc(&intf->dev, sizeof(*ir), GFP_KERNEL);
0164     if (!ir)
0165         return -ENOMEM;
0166 
0167     ir->dev = &intf->dev;
0168 
0169     timer_setup(&ir->timer, igorplugusb_timer, 0);
0170 
0171     ir->request.bRequest = GET_INFRACODE;
0172     ir->request.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
0173     ir->request.wLength = cpu_to_le16(MAX_PACKET);
0174 
0175     ir->urb = usb_alloc_urb(0, GFP_KERNEL);
0176     if (!ir->urb)
0177         goto fail;
0178 
0179     ir->buf_in = kmalloc(MAX_PACKET, GFP_KERNEL);
0180     if (!ir->buf_in)
0181         goto fail;
0182     usb_fill_control_urb(ir->urb, udev,
0183         usb_rcvctrlpipe(udev, 0), (uint8_t *)&ir->request,
0184         ir->buf_in, MAX_PACKET, igorplugusb_callback, ir);
0185 
0186     usb_make_path(udev, ir->phys, sizeof(ir->phys));
0187 
0188     rc = rc_allocate_device(RC_DRIVER_IR_RAW);
0189     if (!rc)
0190         goto fail;
0191 
0192     rc->device_name = DRIVER_DESC;
0193     rc->input_phys = ir->phys;
0194     usb_to_input_id(udev, &rc->input_id);
0195     rc->dev.parent = &intf->dev;
0196     /*
0197      * This device can only store 36 pulses + spaces, which is not enough
0198      * for the NEC protocol and many others.
0199      */
0200     rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER &
0201         ~(RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32 |
0202           RC_PROTO_BIT_RC6_6A_20 | RC_PROTO_BIT_RC6_6A_24 |
0203           RC_PROTO_BIT_RC6_6A_32 | RC_PROTO_BIT_RC6_MCE |
0204           RC_PROTO_BIT_SONY20 | RC_PROTO_BIT_SANYO);
0205 
0206     rc->priv = ir;
0207     rc->driver_name = DRIVER_NAME;
0208     rc->map_name = RC_MAP_HAUPPAUGE;
0209     rc->timeout = MS_TO_US(100);
0210     rc->rx_resolution = 85;
0211 
0212     ir->rc = rc;
0213     ret = rc_register_device(rc);
0214     if (ret) {
0215         dev_err(&intf->dev, "failed to register rc device: %d", ret);
0216         goto fail;
0217     }
0218 
0219     usb_set_intfdata(intf, ir);
0220 
0221     igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
0222 
0223     return 0;
0224 fail:
0225     usb_poison_urb(ir->urb);
0226     del_timer(&ir->timer);
0227     usb_unpoison_urb(ir->urb);
0228     usb_free_urb(ir->urb);
0229     rc_free_device(ir->rc);
0230     kfree(ir->buf_in);
0231 
0232     return ret;
0233 }
0234 
0235 static void igorplugusb_disconnect(struct usb_interface *intf)
0236 {
0237     struct igorplugusb *ir = usb_get_intfdata(intf);
0238 
0239     rc_unregister_device(ir->rc);
0240     usb_poison_urb(ir->urb);
0241     del_timer_sync(&ir->timer);
0242     usb_set_intfdata(intf, NULL);
0243     usb_unpoison_urb(ir->urb);
0244     usb_free_urb(ir->urb);
0245     kfree(ir->buf_in);
0246 }
0247 
0248 static const struct usb_device_id igorplugusb_table[] = {
0249     /* Igor Plug USB (Atmel's Manufact. ID) */
0250     { USB_DEVICE(0x03eb, 0x0002) },
0251     /* Fit PC2 Infrared Adapter */
0252     { USB_DEVICE(0x03eb, 0x21fe) },
0253     /* Terminating entry */
0254     { }
0255 };
0256 
0257 static struct usb_driver igorplugusb_driver = {
0258     .name = DRIVER_NAME,
0259     .probe = igorplugusb_probe,
0260     .disconnect = igorplugusb_disconnect,
0261     .id_table = igorplugusb_table
0262 };
0263 
0264 module_usb_driver(igorplugusb_driver);
0265 
0266 MODULE_DESCRIPTION(DRIVER_DESC);
0267 MODULE_AUTHOR("Sean Young <sean@mess.org>");
0268 MODULE_LICENSE("GPL");
0269 MODULE_DEVICE_TABLE(usb, igorplugusb_table);