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 * ffs/find_next_bit() searches in the wrong direction, so open-code our own.
24 static inline int is_bit_set(const u8
*buf
, int bit
)
26 return buf
[bit
/ 8] & (0x80 >> (bit
& 7));
29 static void imon_ir_data(struct imon
*imon
)
31 DEFINE_IR_RAW_EVENT(rawir
);
32 int offset
= 0, size
= 5 * 8;
35 dev_dbg(imon
->dev
, "data: %*ph", 8, imon
->ir_buf
);
37 while (offset
< size
) {
39 while (!is_bit_set(imon
->ir_buf
, bit
) && bit
< size
)
41 dev_dbg(imon
->dev
, "pulse: %d bits", bit
- offset
);
44 rawir
.duration
= (bit
- offset
) * BIT_DURATION
;
45 ir_raw_event_store_with_filter(imon
->rcdev
, &rawir
);
52 while (is_bit_set(imon
->ir_buf
, bit
) && bit
< size
)
54 dev_dbg(imon
->dev
, "space: %d bits", bit
- offset
);
57 rawir
.duration
= (bit
- offset
) * BIT_DURATION
;
58 ir_raw_event_store_with_filter(imon
->rcdev
, &rawir
);
63 if (imon
->ir_buf
[7] == 0x0a) {
64 ir_raw_event_set_idle(imon
->rcdev
, true);
65 ir_raw_event_handle(imon
->rcdev
);
69 static void imon_ir_rx(struct urb
*urb
)
71 struct imon
*imon
= urb
->context
;
74 switch (urb
->status
) {
76 if (imon
->ir_buf
[7] != 0xff)
86 dev_dbg(imon
->dev
, "error: urb status = %d", urb
->status
);
90 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
91 if (ret
&& ret
!= -ENODEV
)
92 dev_warn(imon
->dev
, "failed to resubmit urb: %d", ret
);
95 static int imon_probe(struct usb_interface
*intf
,
96 const struct usb_device_id
*id
)
98 struct usb_endpoint_descriptor
*ir_ep
= NULL
;
99 struct usb_host_interface
*idesc
;
100 struct usb_device
*udev
;
101 struct rc_dev
*rcdev
;
105 udev
= interface_to_usbdev(intf
);
106 idesc
= intf
->cur_altsetting
;
108 for (i
= 0; i
< idesc
->desc
.bNumEndpoints
; i
++) {
109 struct usb_endpoint_descriptor
*ep
= &idesc
->endpoint
[i
].desc
;
111 if (usb_endpoint_is_int_in(ep
)) {
118 dev_err(&intf
->dev
, "IR endpoint missing");
122 imon
= devm_kmalloc(&intf
->dev
, sizeof(*imon
), GFP_KERNEL
);
126 imon
->ir_urb
= usb_alloc_urb(0, GFP_KERNEL
);
130 imon
->dev
= &intf
->dev
;
131 usb_fill_int_urb(imon
->ir_urb
, udev
,
132 usb_rcvintpipe(udev
, ir_ep
->bEndpointAddress
),
133 imon
->ir_buf
, sizeof(imon
->ir_buf
),
134 imon_ir_rx
, imon
, ir_ep
->bInterval
);
136 rcdev
= devm_rc_allocate_device(&intf
->dev
, RC_DRIVER_IR_RAW
);
142 usb_make_path(udev
, imon
->phys
, sizeof(imon
->phys
));
144 rcdev
->device_name
= "iMON Station";
145 rcdev
->driver_name
= KBUILD_MODNAME
;
146 rcdev
->input_phys
= imon
->phys
;
147 usb_to_input_id(udev
, &rcdev
->input_id
);
148 rcdev
->dev
.parent
= &intf
->dev
;
149 rcdev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
150 rcdev
->map_name
= RC_MAP_IMON_RSC
;
151 rcdev
->rx_resolution
= BIT_DURATION
;
154 ret
= devm_rc_register_device(&intf
->dev
, rcdev
);
160 ret
= usb_submit_urb(imon
->ir_urb
, GFP_KERNEL
);
164 usb_set_intfdata(intf
, imon
);
169 usb_free_urb(imon
->ir_urb
);
173 static void imon_disconnect(struct usb_interface
*intf
)
175 struct imon
*imon
= usb_get_intfdata(intf
);
177 usb_kill_urb(imon
->ir_urb
);
178 usb_free_urb(imon
->ir_urb
);
181 static const struct usb_device_id imon_table
[] = {
182 /* SoundGraph iMON (IR only) -- sg_imon.inf */
183 { USB_DEVICE(0x04e8, 0xff30) },
187 static struct usb_driver imon_driver
= {
188 .name
= KBUILD_MODNAME
,
190 .disconnect
= imon_disconnect
,
191 .id_table
= imon_table
194 module_usb_driver(imon_driver
);
196 MODULE_DESCRIPTION("Early raw iMON IR devices");
197 MODULE_AUTHOR("Sean Young <sean@mess.org>");
198 MODULE_LICENSE("GPL");
199 MODULE_DEVICE_TABLE(usb
, imon_table
);