1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright (C) 2018 Sean Young <sean@mess.org>
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
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 data
= be64_to_cpu(imon
->ir_buf
);
33 u8 packet_no
= data
& 0xff;
37 if (packet_no
== 0xff)
40 dev_dbg(imon
->dev
, "data: %*ph", 8, &imon
->ir_buf
);
43 * Only the first 5 bytes contain IR data. Right shift so we move
44 * the IR bits to the lower 40 bits.
50 * Find highest set bit which is less or equal to offset
52 * offset is the bit above (base 0) where we start looking.
54 * data & (BIT_ULL(offset) - 1) masks off any unwanted bits,
55 * so we have just bits less than offset.
57 * fls will tell us the highest bit set plus 1 (or 0 if no
60 rawir
.pulse
= !rawir
.pulse
;
61 bit
= fls64(data
& (BIT_ULL(offset
) - 1));
63 dev_dbg(imon
->dev
, "%s: %d bits",
64 rawir
.pulse
? "pulse" : "space", offset
- bit
);
65 rawir
.duration
= (offset
- bit
) * BIT_DURATION
;
66 ir_raw_event_store_with_filter(imon
->rcdev
, &rawir
);
74 if (packet_no
== 0x0a && !imon
->rcdev
->idle
) {
75 ir_raw_event_set_idle(imon
->rcdev
, true);
76 ir_raw_event_handle(imon
->rcdev
);
80 static void imon_ir_rx(struct urb
*urb
)
82 struct imon
*imon
= urb
->context
;
85 switch (urb
->status
) {
96 dev_dbg(imon
->dev
, "error: urb status = %d", urb
->status
);
100 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
101 if (ret
&& ret
!= -ENODEV
)
102 dev_warn(imon
->dev
, "failed to resubmit urb: %d", ret
);
105 static int imon_probe(struct usb_interface
*intf
,
106 const struct usb_device_id
*id
)
108 struct usb_endpoint_descriptor
*ir_ep
= NULL
;
109 struct usb_host_interface
*idesc
;
110 struct usb_device
*udev
;
111 struct rc_dev
*rcdev
;
115 udev
= interface_to_usbdev(intf
);
116 idesc
= intf
->cur_altsetting
;
118 for (i
= 0; i
< idesc
->desc
.bNumEndpoints
; i
++) {
119 struct usb_endpoint_descriptor
*ep
= &idesc
->endpoint
[i
].desc
;
121 if (usb_endpoint_is_int_in(ep
)) {
128 dev_err(&intf
->dev
, "IR endpoint missing");
132 imon
= devm_kmalloc(&intf
->dev
, sizeof(*imon
), GFP_KERNEL
);
136 imon
->ir_urb
= usb_alloc_urb(0, GFP_KERNEL
);
140 imon
->dev
= &intf
->dev
;
141 usb_fill_int_urb(imon
->ir_urb
, udev
,
142 usb_rcvintpipe(udev
, ir_ep
->bEndpointAddress
),
143 &imon
->ir_buf
, sizeof(imon
->ir_buf
),
144 imon_ir_rx
, imon
, ir_ep
->bInterval
);
146 rcdev
= devm_rc_allocate_device(&intf
->dev
, RC_DRIVER_IR_RAW
);
152 usb_make_path(udev
, imon
->phys
, sizeof(imon
->phys
));
154 rcdev
->device_name
= "iMON Station";
155 rcdev
->driver_name
= KBUILD_MODNAME
;
156 rcdev
->input_phys
= imon
->phys
;
157 usb_to_input_id(udev
, &rcdev
->input_id
);
158 rcdev
->dev
.parent
= &intf
->dev
;
159 rcdev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
160 rcdev
->map_name
= RC_MAP_IMON_RSC
;
161 rcdev
->rx_resolution
= BIT_DURATION
;
164 ret
= devm_rc_register_device(&intf
->dev
, rcdev
);
170 ret
= usb_submit_urb(imon
->ir_urb
, GFP_KERNEL
);
174 usb_set_intfdata(intf
, imon
);
179 usb_free_urb(imon
->ir_urb
);
183 static void imon_disconnect(struct usb_interface
*intf
)
185 struct imon
*imon
= usb_get_intfdata(intf
);
187 usb_kill_urb(imon
->ir_urb
);
188 usb_free_urb(imon
->ir_urb
);
191 static const struct usb_device_id imon_table
[] = {
192 /* SoundGraph iMON (IR only) -- sg_imon.inf */
193 { USB_DEVICE(0x04e8, 0xff30) },
197 static struct usb_driver imon_driver
= {
198 .name
= KBUILD_MODNAME
,
200 .disconnect
= imon_disconnect
,
201 .id_table
= imon_table
204 module_usb_driver(imon_driver
);
206 MODULE_DESCRIPTION("Early raw iMON IR devices");
207 MODULE_AUTHOR("Sean Young <sean@mess.org>");
208 MODULE_LICENSE("GPL");
209 MODULE_DEVICE_TABLE(usb
, imon_table
);