xref: /openbmc/linux/drivers/media/rc/imon_raw.c (revision be709d48)
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 	u8 ir_buf[8] __aligned(__alignof__(u64));
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 d = be64_to_cpup((__be64 *)imon->ir_buf) >> 24;
33 	int offset = 40;
34 	int bit;
35 
36 	dev_dbg(imon->dev, "data: %*ph", 8, imon->ir_buf);
37 
38 	do {
39 		bit = fls64(d & (BIT_ULL(offset) - 1));
40 		if (bit < offset) {
41 			dev_dbg(imon->dev, "pulse: %d bits", offset - bit);
42 			rawir.pulse = true;
43 			rawir.duration = (offset - bit) * BIT_DURATION;
44 			ir_raw_event_store_with_filter(imon->rcdev, &rawir);
45 
46 			if (bit == 0)
47 				break;
48 
49 			offset = bit;
50 		}
51 
52 		bit = fls64(~d & (BIT_ULL(offset) - 1));
53 		dev_dbg(imon->dev, "space: %d bits", offset - bit);
54 
55 		rawir.pulse = false;
56 		rawir.duration = (offset - bit) * BIT_DURATION;
57 		ir_raw_event_store_with_filter(imon->rcdev, &rawir);
58 
59 		offset = bit;
60 	} while (offset > 0);
61 
62 	if (imon->ir_buf[7] == 0x0a) {
63 		ir_raw_event_set_idle(imon->rcdev, true);
64 		ir_raw_event_handle(imon->rcdev);
65 	}
66 }
67 
68 static void imon_ir_rx(struct urb *urb)
69 {
70 	struct imon *imon = urb->context;
71 	int ret;
72 
73 	switch (urb->status) {
74 	case 0:
75 		if (imon->ir_buf[7] != 0xff)
76 			imon_ir_data(imon);
77 		break;
78 	case -ECONNRESET:
79 	case -ENOENT:
80 	case -ESHUTDOWN:
81 		usb_unlink_urb(urb);
82 		return;
83 	case -EPIPE:
84 	default:
85 		dev_dbg(imon->dev, "error: urb status = %d", urb->status);
86 		break;
87 	}
88 
89 	ret = usb_submit_urb(urb, GFP_ATOMIC);
90 	if (ret && ret != -ENODEV)
91 		dev_warn(imon->dev, "failed to resubmit urb: %d", ret);
92 }
93 
94 static int imon_probe(struct usb_interface *intf,
95 		      const struct usb_device_id *id)
96 {
97 	struct usb_endpoint_descriptor *ir_ep = NULL;
98 	struct usb_host_interface *idesc;
99 	struct usb_device *udev;
100 	struct rc_dev *rcdev;
101 	struct imon *imon;
102 	int i, ret;
103 
104 	udev = interface_to_usbdev(intf);
105 	idesc = intf->cur_altsetting;
106 
107 	for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
108 		struct usb_endpoint_descriptor *ep = &idesc->endpoint[i].desc;
109 
110 		if (usb_endpoint_is_int_in(ep)) {
111 			ir_ep = ep;
112 			break;
113 		}
114 	}
115 
116 	if (!ir_ep) {
117 		dev_err(&intf->dev, "IR endpoint missing");
118 		return -ENODEV;
119 	}
120 
121 	imon = devm_kmalloc(&intf->dev, sizeof(*imon), GFP_KERNEL);
122 	if (!imon)
123 		return -ENOMEM;
124 
125 	imon->ir_urb = usb_alloc_urb(0, GFP_KERNEL);
126 	if (!imon->ir_urb)
127 		return -ENOMEM;
128 
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);
134 
135 	rcdev = devm_rc_allocate_device(&intf->dev, RC_DRIVER_IR_RAW);
136 	if (!rcdev) {
137 		ret = -ENOMEM;
138 		goto free_urb;
139 	}
140 
141 	usb_make_path(udev, imon->phys, sizeof(imon->phys));
142 
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;
151 	rcdev->priv = imon;
152 
153 	ret = devm_rc_register_device(&intf->dev, rcdev);
154 	if (ret)
155 		goto free_urb;
156 
157 	imon->rcdev = rcdev;
158 
159 	ret = usb_submit_urb(imon->ir_urb, GFP_KERNEL);
160 	if (ret)
161 		goto free_urb;
162 
163 	usb_set_intfdata(intf, imon);
164 
165 	return 0;
166 
167 free_urb:
168 	usb_free_urb(imon->ir_urb);
169 	return ret;
170 }
171 
172 static void imon_disconnect(struct usb_interface *intf)
173 {
174 	struct imon *imon = usb_get_intfdata(intf);
175 
176 	usb_kill_urb(imon->ir_urb);
177 	usb_free_urb(imon->ir_urb);
178 }
179 
180 static const struct usb_device_id imon_table[] = {
181 	/* SoundGraph iMON (IR only) -- sg_imon.inf */
182 	{ USB_DEVICE(0x04e8, 0xff30) },
183 	{}
184 };
185 
186 static struct usb_driver imon_driver = {
187 	.name = KBUILD_MODNAME,
188 	.probe = imon_probe,
189 	.disconnect = imon_disconnect,
190 	.id_table = imon_table
191 };
192 
193 module_usb_driver(imon_driver);
194 
195 MODULE_DESCRIPTION("Early raw iMON IR devices");
196 MODULE_AUTHOR("Sean Young <sean@mess.org>");
197 MODULE_LICENSE("GPL");
198 MODULE_DEVICE_TABLE(usb, imon_table);
199