xref: /openbmc/linux/drivers/media/rc/ir_toy.c (revision 23f8bd25)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Infrared Toy and IR Droid RC core driver
5  *
6  * Copyright (C) 2020 Sean Young <sean@mess.org>
7  *
8  * http://dangerousprototypes.com/docs/USB_IR_Toy:_Sampling_mode
9  *
10  * This driver is based on the lirc driver which can be found here:
11  * https://sourceforge.net/p/lirc/git/ci/master/tree/plugins/irtoy.c
12  * Copyright (C) 2011 Peter Kooiman <pkooiman@gmail.com>
13  */
14 
15 #include <asm/unaligned.h>
16 #include <linux/completion.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/slab.h>
21 #include <linux/usb/input.h>
22 
23 #include <media/rc-core.h>
24 
25 static const u8 COMMAND_VERSION[] = { 'v' };
26 // End transmit and repeat reset command so we exit sump mode
27 static const u8 COMMAND_RESET[] = { 0xff, 0xff, 0, 0, 0, 0, 0 };
28 static const u8 COMMAND_SMODE_ENTER[] = { 's' };
29 static const u8 COMMAND_TXSTART[] = { 0x26, 0x24, 0x25, 0x03 };
30 
31 #define REPLY_XMITCOUNT 't'
32 #define REPLY_XMITSUCCESS 'C'
33 #define REPLY_VERSION 'V'
34 #define REPLY_SAMPLEMODEPROTO 'S'
35 
36 #define TIMEOUT 500
37 
38 #define LEN_XMITRES 3
39 #define LEN_VERSION 4
40 #define LEN_SAMPLEMODEPROTO 3
41 
42 #define MIN_FW_VERSION 20
43 #define UNIT_US 21
44 #define MAX_TIMEOUT_US (UNIT_US * U16_MAX)
45 
46 #define MAX_PACKET 64
47 
48 enum state {
49 	STATE_IRDATA,
50 	STATE_COMMAND_NO_RESP,
51 	STATE_COMMAND,
52 	STATE_TX,
53 };
54 
55 struct irtoy {
56 	struct device *dev;
57 	struct usb_device *usbdev;
58 
59 	struct rc_dev *rc;
60 	struct urb *urb_in, *urb_out;
61 
62 	u8 *in;
63 	u8 *out;
64 	struct completion command_done;
65 
66 	bool pulse;
67 	enum state state;
68 
69 	void *tx_buf;
70 	uint tx_len;
71 
72 	uint emitted;
73 	uint hw_version;
74 	uint sw_version;
75 	uint proto_version;
76 
77 	char phys[64];
78 };
79 
80 static void irtoy_response(struct irtoy *irtoy, u32 len)
81 {
82 	switch (irtoy->state) {
83 	case STATE_COMMAND:
84 		if (len == LEN_VERSION && irtoy->in[0] == REPLY_VERSION) {
85 			uint version;
86 
87 			irtoy->in[LEN_VERSION] = 0;
88 
89 			if (kstrtouint(irtoy->in + 1, 10, &version)) {
90 				dev_err(irtoy->dev, "invalid version %*phN. Please make sure you are using firmware v20 or higher",
91 					LEN_VERSION, irtoy->in);
92 				break;
93 			}
94 
95 			dev_dbg(irtoy->dev, "version %s\n", irtoy->in);
96 
97 			irtoy->hw_version = version / 100;
98 			irtoy->sw_version = version % 100;
99 
100 			irtoy->state = STATE_IRDATA;
101 			complete(&irtoy->command_done);
102 		} else if (len == LEN_SAMPLEMODEPROTO &&
103 			   irtoy->in[0] == REPLY_SAMPLEMODEPROTO) {
104 			uint version;
105 
106 			irtoy->in[LEN_SAMPLEMODEPROTO] = 0;
107 
108 			if (kstrtouint(irtoy->in + 1, 10, &version)) {
109 				dev_err(irtoy->dev, "invalid sample mode response %*phN",
110 					LEN_SAMPLEMODEPROTO, irtoy->in);
111 				return;
112 			}
113 
114 			dev_dbg(irtoy->dev, "protocol %s\n", irtoy->in);
115 
116 			irtoy->proto_version = version;
117 
118 			irtoy->state = STATE_IRDATA;
119 			complete(&irtoy->command_done);
120 		} else {
121 			dev_err(irtoy->dev, "unexpected response to command: %*phN\n",
122 				len, irtoy->in);
123 		}
124 		break;
125 	case STATE_IRDATA: {
126 		struct ir_raw_event rawir = { .pulse = irtoy->pulse };
127 		__be16 *in = (__be16 *)irtoy->in;
128 		int i;
129 
130 		for (i = 0; i < len / sizeof(__be16); i++) {
131 			u16 v = be16_to_cpu(in[i]);
132 
133 			if (v == 0xffff) {
134 				rawir.pulse = false;
135 			} else {
136 				rawir.duration = v * UNIT_US;
137 				ir_raw_event_store_with_timeout(irtoy->rc,
138 								&rawir);
139 			}
140 
141 			rawir.pulse = !rawir.pulse;
142 		}
143 
144 		irtoy->pulse = rawir.pulse;
145 
146 		ir_raw_event_handle(irtoy->rc);
147 		break;
148 	}
149 	case STATE_TX:
150 		if (irtoy->tx_len == 0) {
151 			if (len == LEN_XMITRES &&
152 			    irtoy->in[0] == REPLY_XMITCOUNT) {
153 				u16 emitted = get_unaligned_be16(irtoy->in + 1);
154 
155 				dev_dbg(irtoy->dev, "emitted:%u\n", emitted);
156 
157 				irtoy->emitted = emitted;
158 			} else if (len == 1 &&
159 				   irtoy->in[0] == REPLY_XMITSUCCESS) {
160 				irtoy->state = STATE_IRDATA;
161 				complete(&irtoy->command_done);
162 			}
163 		} else {
164 			// send next part of tx buffer
165 			uint space = irtoy->in[0];
166 			uint buf_len;
167 			int err;
168 
169 			if (len != 1 || space > MAX_PACKET || space == 0) {
170 				dev_err(irtoy->dev, "packet length expected: %*phN\n",
171 					len, irtoy->in);
172 				irtoy->state = STATE_IRDATA;
173 				complete(&irtoy->command_done);
174 				break;
175 			}
176 
177 			buf_len = min(space, irtoy->tx_len);
178 
179 			dev_dbg(irtoy->dev, "remaining:%u sending:%u\n",
180 				irtoy->tx_len, buf_len);
181 
182 			memcpy(irtoy->out, irtoy->tx_buf, buf_len);
183 			irtoy->urb_out->transfer_buffer_length = buf_len;
184 			err = usb_submit_urb(irtoy->urb_out, GFP_ATOMIC);
185 			if (err != 0) {
186 				dev_err(irtoy->dev, "fail to submit tx buf urb: %d\n",
187 					err);
188 				irtoy->state = STATE_IRDATA;
189 				complete(&irtoy->command_done);
190 				break;
191 			}
192 
193 			irtoy->tx_buf += buf_len;
194 			irtoy->tx_len -= buf_len;
195 		}
196 		break;
197 	case STATE_COMMAND_NO_RESP:
198 		dev_err(irtoy->dev, "unexpected response to reset: %*phN\n",
199 			len, irtoy->in);
200 	}
201 }
202 
203 static void irtoy_out_callback(struct urb *urb)
204 {
205 	struct irtoy *irtoy = urb->context;
206 
207 	if (urb->status == 0) {
208 		if (irtoy->state == STATE_COMMAND_NO_RESP)
209 			complete(&irtoy->command_done);
210 	} else {
211 		dev_warn(irtoy->dev, "out urb status: %d\n", urb->status);
212 	}
213 }
214 
215 static void irtoy_in_callback(struct urb *urb)
216 {
217 	struct irtoy *irtoy = urb->context;
218 	int ret;
219 
220 	if (urb->status == 0)
221 		irtoy_response(irtoy, urb->actual_length);
222 	else
223 		dev_dbg(irtoy->dev, "in urb status: %d\n", urb->status);
224 
225 	ret = usb_submit_urb(urb, GFP_ATOMIC);
226 	if (ret && ret != -ENODEV)
227 		dev_warn(irtoy->dev, "failed to resubmit urb: %d\n", ret);
228 }
229 
230 static int irtoy_command(struct irtoy *irtoy, const u8 *cmd, int cmd_len,
231 			 enum state state)
232 {
233 	int err;
234 
235 	init_completion(&irtoy->command_done);
236 
237 	irtoy->state = state;
238 
239 	memcpy(irtoy->out, cmd, cmd_len);
240 	irtoy->urb_out->transfer_buffer_length = cmd_len;
241 
242 	err = usb_submit_urb(irtoy->urb_out, GFP_KERNEL);
243 	if (err != 0)
244 		return err;
245 
246 	if (!wait_for_completion_timeout(&irtoy->command_done,
247 					 msecs_to_jiffies(TIMEOUT))) {
248 		usb_kill_urb(irtoy->urb_out);
249 		return -ETIMEDOUT;
250 	}
251 
252 	return 0;
253 }
254 
255 static int irtoy_setup(struct irtoy *irtoy)
256 {
257 	int err;
258 
259 	err = irtoy_command(irtoy, COMMAND_RESET, sizeof(COMMAND_RESET),
260 			    STATE_COMMAND_NO_RESP);
261 	if (err != 0) {
262 		dev_err(irtoy->dev, "could not write reset command: %d\n",
263 			err);
264 		return err;
265 	}
266 
267 	usleep_range(50, 50);
268 
269 	// get version
270 	err = irtoy_command(irtoy, COMMAND_VERSION, sizeof(COMMAND_VERSION),
271 			    STATE_COMMAND);
272 	if (err) {
273 		dev_err(irtoy->dev, "could not write version command: %d\n",
274 			err);
275 		return err;
276 	}
277 
278 	// enter sample mode
279 	err = irtoy_command(irtoy, COMMAND_SMODE_ENTER,
280 			    sizeof(COMMAND_SMODE_ENTER), STATE_COMMAND);
281 	if (err)
282 		dev_err(irtoy->dev, "could not write sample command: %d\n",
283 			err);
284 
285 	return err;
286 }
287 
288 /*
289  * When sending IR, it is imperative that we send the IR data as quickly
290  * as possible to the device, so it does not run out of IR data and
291  * introduce gaps. Allocate the buffer here, and then feed the data from
292  * the urb callback handler.
293  */
294 static int irtoy_tx(struct rc_dev *rc, uint *txbuf, uint count)
295 {
296 	struct irtoy *irtoy = rc->priv;
297 	unsigned int i, size;
298 	__be16 *buf;
299 	int err;
300 
301 	size = sizeof(u16) * (count + 1);
302 	buf = kmalloc(size, GFP_KERNEL);
303 	if (!buf)
304 		return -ENOMEM;
305 
306 	for (i = 0; i < count; i++) {
307 		u16 v = DIV_ROUND_CLOSEST(txbuf[i], UNIT_US);
308 
309 		if (!v)
310 			v = 1;
311 		buf[i] = cpu_to_be16(v);
312 	}
313 
314 	buf[count] = cpu_to_be16(0xffff);
315 
316 	irtoy->tx_buf = buf;
317 	irtoy->tx_len = size;
318 	irtoy->emitted = 0;
319 
320 	err = irtoy_command(irtoy, COMMAND_TXSTART, sizeof(COMMAND_TXSTART),
321 			    STATE_TX);
322 	kfree(buf);
323 
324 	if (err) {
325 		dev_err(irtoy->dev, "failed to send tx start command: %d\n",
326 			err);
327 		// not sure what state the device is in, reset it
328 		irtoy_setup(irtoy);
329 		return err;
330 	}
331 
332 	if (size != irtoy->emitted) {
333 		dev_err(irtoy->dev, "expected %u emitted, got %u\n", size,
334 			irtoy->emitted);
335 		// not sure what state the device is in, reset it
336 		irtoy_setup(irtoy);
337 		return -EINVAL;
338 	}
339 
340 	return count;
341 }
342 
343 static int irtoy_tx_carrier(struct rc_dev *rc, uint32_t carrier)
344 {
345 	struct irtoy *irtoy = rc->priv;
346 	u8 buf[3];
347 	int err;
348 
349 	if (carrier < 11800)
350 		return -EINVAL;
351 
352 	buf[0] = 0x06;
353 	buf[1] = DIV_ROUND_CLOSEST(48000000, 16 * carrier) - 1;
354 	buf[2] = 0;
355 
356 	err = irtoy_command(irtoy, buf, sizeof(buf), STATE_COMMAND_NO_RESP);
357 	if (err)
358 		dev_err(irtoy->dev, "could not write carrier command: %d\n",
359 			err);
360 
361 	return err;
362 }
363 
364 static int irtoy_probe(struct usb_interface *intf,
365 		       const struct usb_device_id *id)
366 {
367 	struct usb_host_interface *idesc = intf->cur_altsetting;
368 	struct usb_device *usbdev = interface_to_usbdev(intf);
369 	struct usb_endpoint_descriptor *ep_in = NULL;
370 	struct usb_endpoint_descriptor *ep_out = NULL;
371 	struct usb_endpoint_descriptor *ep = NULL;
372 	struct irtoy *irtoy;
373 	struct rc_dev *rc;
374 	struct urb *urb;
375 	int i, pipe, err = -ENOMEM;
376 
377 	for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
378 		ep = &idesc->endpoint[i].desc;
379 
380 		if (!ep_in && usb_endpoint_is_bulk_in(ep) &&
381 		    usb_endpoint_maxp(ep) == MAX_PACKET)
382 			ep_in = ep;
383 
384 		if (!ep_out && usb_endpoint_is_bulk_out(ep) &&
385 		    usb_endpoint_maxp(ep) == MAX_PACKET)
386 			ep_out = ep;
387 	}
388 
389 	if (!ep_in || !ep_out) {
390 		dev_err(&intf->dev, "required endpoints not found\n");
391 		return -ENODEV;
392 	}
393 
394 	irtoy = kzalloc(sizeof(*irtoy), GFP_KERNEL);
395 	if (!irtoy)
396 		return -ENOMEM;
397 
398 	irtoy->in = kmalloc(MAX_PACKET,  GFP_KERNEL);
399 	if (!irtoy->in)
400 		goto free_irtoy;
401 
402 	irtoy->out = kmalloc(MAX_PACKET,  GFP_KERNEL);
403 	if (!irtoy->out)
404 		goto free_irtoy;
405 
406 	rc = rc_allocate_device(RC_DRIVER_IR_RAW);
407 	if (!rc)
408 		goto free_irtoy;
409 
410 	urb = usb_alloc_urb(0, GFP_KERNEL);
411 	if (!urb)
412 		goto free_rcdev;
413 
414 	pipe = usb_rcvbulkpipe(usbdev, ep_in->bEndpointAddress);
415 	usb_fill_bulk_urb(urb, usbdev, pipe, irtoy->in, MAX_PACKET,
416 			  irtoy_in_callback, irtoy);
417 	irtoy->urb_in = urb;
418 
419 	urb = usb_alloc_urb(0, GFP_KERNEL);
420 	if (!urb)
421 		goto free_rcdev;
422 
423 	pipe = usb_sndbulkpipe(usbdev, ep_out->bEndpointAddress);
424 	usb_fill_bulk_urb(urb, usbdev, pipe, irtoy->out, MAX_PACKET,
425 			  irtoy_out_callback, irtoy);
426 
427 	irtoy->dev = &intf->dev;
428 	irtoy->usbdev = usbdev;
429 	irtoy->rc = rc;
430 	irtoy->urb_out = urb;
431 	irtoy->pulse = true;
432 
433 	err = usb_submit_urb(irtoy->urb_in, GFP_KERNEL);
434 	if (err != 0) {
435 		dev_err(irtoy->dev, "fail to submit in urb: %d\n", err);
436 		return err;
437 	}
438 
439 	err = irtoy_setup(irtoy);
440 	if (err)
441 		goto free_rcdev;
442 
443 	dev_info(irtoy->dev, "version: hardware %u, firmware %u, protocol %u",
444 		 irtoy->hw_version, irtoy->sw_version, irtoy->proto_version);
445 
446 	if (irtoy->sw_version < MIN_FW_VERSION) {
447 		dev_err(irtoy->dev, "need firmware V%02u or higher",
448 			MIN_FW_VERSION);
449 		err = -ENODEV;
450 		goto free_rcdev;
451 	}
452 
453 	usb_make_path(usbdev, irtoy->phys, sizeof(irtoy->phys));
454 
455 	rc->device_name = "Infrared Toy";
456 	rc->driver_name = KBUILD_MODNAME;
457 	rc->input_phys = irtoy->phys;
458 	usb_to_input_id(usbdev, &rc->input_id);
459 	rc->dev.parent = &intf->dev;
460 	rc->priv = irtoy;
461 	rc->tx_ir = irtoy_tx;
462 	rc->s_tx_carrier = irtoy_tx_carrier;
463 	rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
464 	rc->map_name = RC_MAP_RC6_MCE;
465 	rc->rx_resolution = UNIT_US;
466 	rc->timeout = IR_DEFAULT_TIMEOUT;
467 
468 	/*
469 	 * end of transmission is detected by absence of a usb packet
470 	 * with more pulse/spaces. However, each usb packet sent can
471 	 * contain 32 pulse/spaces, which can be quite lengthy, so there
472 	 * can be a delay between usb packets. For example with nec there is a
473 	 * 17ms gap between packets.
474 	 *
475 	 * So, make timeout a largish minimum which works with most protocols.
476 	 */
477 	rc->min_timeout = MS_TO_US(40);
478 	rc->max_timeout = MAX_TIMEOUT_US;
479 
480 	err = rc_register_device(rc);
481 	if (err)
482 		goto free_rcdev;
483 
484 	usb_set_intfdata(intf, irtoy);
485 
486 	return 0;
487 
488 free_rcdev:
489 	usb_kill_urb(irtoy->urb_out);
490 	usb_free_urb(irtoy->urb_out);
491 	usb_kill_urb(irtoy->urb_in);
492 	usb_free_urb(irtoy->urb_in);
493 	rc_free_device(rc);
494 free_irtoy:
495 	kfree(irtoy->in);
496 	kfree(irtoy->out);
497 	kfree(irtoy);
498 	return err;
499 }
500 
501 static void irtoy_disconnect(struct usb_interface *intf)
502 {
503 	struct irtoy *ir = usb_get_intfdata(intf);
504 
505 	rc_unregister_device(ir->rc);
506 	usb_set_intfdata(intf, NULL);
507 	usb_kill_urb(ir->urb_out);
508 	usb_free_urb(ir->urb_out);
509 	usb_kill_urb(ir->urb_in);
510 	usb_free_urb(ir->urb_in);
511 	kfree(ir->in);
512 	kfree(ir->out);
513 	kfree(ir);
514 }
515 
516 static const struct usb_device_id irtoy_table[] = {
517 	{ USB_DEVICE_INTERFACE_CLASS(0x04d8, 0xfd08, USB_CLASS_CDC_DATA) },
518 	{ USB_DEVICE_INTERFACE_CLASS(0x04d8, 0xf58b, USB_CLASS_CDC_DATA) },
519 	{ }
520 };
521 
522 static struct usb_driver irtoy_driver = {
523 	.name = KBUILD_MODNAME,
524 	.probe = irtoy_probe,
525 	.disconnect = irtoy_disconnect,
526 	.id_table = irtoy_table,
527 };
528 
529 module_usb_driver(irtoy_driver);
530 
531 MODULE_AUTHOR("Sean Young <sean@mess.org>");
532 MODULE_DESCRIPTION("Infrared Toy and IR Droid driver");
533 MODULE_LICENSE("GPL");
534 MODULE_DEVICE_TABLE(usb, irtoy_table);
535