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