1 // SPDX-License-Identifier: GPL-2.0+
5 #include <linux/module.h>
7 #include <linux/usb/input.h>
8 #include <media/rc-core.h>
10 /* Each bit is 250us */
11 #define BIT_DURATION 250000
17 u8 ir_buf[8] __aligned(__alignof__(u64));
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.
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.
29 static void imon_ir_data(struct imon *imon)
31 struct ir_raw_event rawir = {};
32 u64 d = be64_to_cpup((__be64 *)imon->ir_buf) >> 24;
36 dev_dbg(imon->dev, "data: %*ph", 8, imon->ir_buf);
39 bit = fls64(d & (BIT_ULL(offset) - 1));
41 dev_dbg(imon->dev, "pulse: %d bits", offset - bit);
43 rawir.duration = (offset - bit) * BIT_DURATION;
44 ir_raw_event_store_with_filter(imon->rcdev, &rawir);
52 bit = fls64(~d & (BIT_ULL(offset) - 1));
53 dev_dbg(imon->dev, "space: %d bits", offset - bit);
56 rawir.duration = (offset - bit) * BIT_DURATION;
57 ir_raw_event_store_with_filter(imon->rcdev, &rawir);
62 if (imon->ir_buf[7] == 0x0a) {
63 ir_raw_event_set_idle(imon->rcdev, true);
64 ir_raw_event_handle(imon->rcdev);
68 static void imon_ir_rx(struct urb *urb)
70 struct imon *imon = urb->context;
73 switch (urb->status) {
75 if (imon->ir_buf[7] != 0xff)
85 dev_dbg(imon->dev, "error: urb status = %d", urb->status);
89 ret = usb_submit_urb(urb, GFP_ATOMIC);
90 if (ret && ret != -ENODEV)
91 dev_warn(imon->dev, "failed to resubmit urb: %d", ret);
94 static int imon_probe(struct usb_interface *intf,
95 const struct usb_device_id *id)
97 struct usb_endpoint_descriptor *ir_ep = NULL;
98 struct usb_host_interface *idesc;
99 struct usb_device *udev;
100 struct rc_dev *rcdev;
104 udev = interface_to_usbdev(intf);
105 idesc = intf->cur_altsetting;
107 for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
108 struct usb_endpoint_descriptor *ep = &idesc->endpoint[i].desc;
110 if (usb_endpoint_is_int_in(ep)) {
117 dev_err(&intf->dev, "IR endpoint missing");
121 imon = devm_kmalloc(&intf->dev, sizeof(*imon), GFP_KERNEL);
125 imon->ir_urb = usb_alloc_urb(0, GFP_KERNEL);
129 imon->dev = &intf->dev;
130 usb_fill_int_urb(imon->ir_urb, udev,
131 usb_rcvintpipe(udev, ir_ep->bEndpointAddress),
132 imon->ir_buf, sizeof(imon->ir_buf),
133 imon_ir_rx, imon, ir_ep->bInterval);
135 rcdev = devm_rc_allocate_device(&intf->dev, RC_DRIVER_IR_RAW);
141 usb_make_path(udev, imon->phys, sizeof(imon->phys));
143 rcdev->device_name = "iMON Station";
144 rcdev->driver_name = KBUILD_MODNAME;
145 rcdev->input_phys = imon->phys;
146 usb_to_input_id(udev, &rcdev->input_id);
147 rcdev->dev.parent = &intf->dev;
148 rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
149 rcdev->map_name = RC_MAP_IMON_RSC;
150 rcdev->rx_resolution = BIT_DURATION;
153 ret = devm_rc_register_device(&intf->dev, rcdev);
159 ret = usb_submit_urb(imon->ir_urb, GFP_KERNEL);
163 usb_set_intfdata(intf, imon);
168 usb_free_urb(imon->ir_urb);
172 static void imon_disconnect(struct usb_interface *intf)
174 struct imon *imon = usb_get_intfdata(intf);
176 usb_kill_urb(imon->ir_urb);
177 usb_free_urb(imon->ir_urb);
180 static const struct usb_device_id imon_table[] = {
181 /* SoundGraph iMON (IR only) -- sg_imon.inf */
182 { USB_DEVICE(0x04e8, 0xff30) },
186 static struct usb_driver imon_driver = {
187 .name = KBUILD_MODNAME,
189 .disconnect = imon_disconnect,
190 .id_table = imon_table
193 module_usb_driver(imon_driver);
195 MODULE_DESCRIPTION("Early raw iMON IR devices");
197 MODULE_LICENSE("GPL");
198 MODULE_DEVICE_TABLE(usb, imon_table);