1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Infrared Toy and IR Droid RC core driver
6 * Copyright (C) 2020 Sean Young <sean@mess.org>
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>
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>
21 #include <media/rc-core.h>
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 };
30 #define REPLY_XMITCOUNT 't'
31 #define REPLY_XMITSUCCESS 'C'
32 #define REPLY_VERSION 'V'
33 #define REPLY_SAMPLEMODEPROTO 'S'
39 #define LEN_SAMPLEMODEPROTO 3
41 #define MIN_FW_VERSION 20
43 #define MAX_TIMEOUT_US (UNIT_US * U16_MAX)
56 struct usb_device *usbdev;
59 struct urb *urb_in, *urb_out;
63 struct completion command_done;
79 static void irtoy_response(struct irtoy *irtoy, u32 len)
81 switch (irtoy->state) {
83 if (len == LEN_VERSION && irtoy->in[0] == REPLY_VERSION) {
86 irtoy->in[LEN_VERSION] = 0;
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);
94 dev_dbg(irtoy->dev, "version %s\n", irtoy->in);
96 irtoy->hw_version = version / 100;
97 irtoy->sw_version = version % 100;
99 irtoy->state = STATE_IRDATA;
100 complete(&irtoy->command_done);
101 } else if (len == LEN_SAMPLEMODEPROTO &&
102 irtoy->in[0] == REPLY_SAMPLEMODEPROTO) {
105 irtoy->in[LEN_SAMPLEMODEPROTO] = 0;
107 if (kstrtouint(irtoy->in + 1, 10, &version)) {
108 dev_err(irtoy->dev, "invalid sample mode response %*phN",
109 LEN_SAMPLEMODEPROTO, irtoy->in);
113 dev_dbg(irtoy->dev, "protocol %s\n", irtoy->in);
115 irtoy->proto_version = version;
117 irtoy->state = STATE_IRDATA;
118 complete(&irtoy->command_done);
120 dev_err(irtoy->dev, "unexpected response to command: %*phN\n",
125 struct ir_raw_event rawir = { .pulse = irtoy->pulse };
126 __be16 *in = (__be16 *)irtoy->in;
129 for (i = 0; i < len / sizeof(__be16); i++) {
130 u16 v = be16_to_cpu(in[i]);
135 rawir.duration = v * UNIT_US;
136 ir_raw_event_store_with_timeout(irtoy->rc,
140 rawir.pulse = !rawir.pulse;
143 irtoy->pulse = rawir.pulse;
145 ir_raw_event_handle(irtoy->rc);
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);
154 dev_dbg(irtoy->dev, "emitted:%u\n", emitted);
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);
163 // send next part of tx buffer
164 uint space = irtoy->in[0];
168 if (len != 1 || space > MAX_PACKET || space == 0) {
169 dev_err(irtoy->dev, "packet length expected: %*phN\n",
171 irtoy->state = STATE_IRDATA;
172 complete(&irtoy->command_done);
176 buf_len = min(space, irtoy->tx_len);
178 dev_dbg(irtoy->dev, "remaining:%u sending:%u\n",
179 irtoy->tx_len, buf_len);
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);
185 dev_err(irtoy->dev, "fail to submit tx buf urb: %d\n",
187 irtoy->state = STATE_IRDATA;
188 complete(&irtoy->command_done);
192 irtoy->tx_buf += buf_len;
193 irtoy->tx_len -= buf_len;
197 dev_err(irtoy->dev, "unexpected response to reset: %*phN\n",
202 static void irtoy_out_callback(struct urb *urb)
204 struct irtoy *irtoy = urb->context;
206 if (urb->status == 0) {
207 if (irtoy->state == STATE_RESET)
208 complete(&irtoy->command_done);
210 dev_warn(irtoy->dev, "out urb status: %d\n", urb->status);
214 static void irtoy_in_callback(struct urb *urb)
216 struct irtoy *irtoy = urb->context;
219 if (urb->status == 0)
220 irtoy_response(irtoy, urb->actual_length);
222 dev_dbg(irtoy->dev, "in urb status: %d\n", urb->status);
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);
229 static int irtoy_command(struct irtoy *irtoy, const u8 *cmd, int cmd_len,
234 init_completion(&irtoy->command_done);
236 irtoy->state = state;
238 memcpy(irtoy->out, cmd, cmd_len);
239 irtoy->urb_out->transfer_buffer_length = cmd_len;
241 err = usb_submit_urb(irtoy->urb_out, GFP_KERNEL);
245 if (!wait_for_completion_timeout(&irtoy->command_done,
246 msecs_to_jiffies(TIMEOUT))) {
247 usb_kill_urb(irtoy->urb_out);
254 static int irtoy_setup(struct irtoy *irtoy)
258 err = irtoy_command(irtoy, COMMAND_RESET, sizeof(COMMAND_RESET),
261 dev_err(irtoy->dev, "could not write reset command: %d\n",
266 usleep_range(50, 50);
269 err = irtoy_command(irtoy, COMMAND_VERSION, sizeof(COMMAND_VERSION),
272 dev_err(irtoy->dev, "could not write version command: %d\n",
278 err = irtoy_command(irtoy, COMMAND_SMODE_ENTER,
279 sizeof(COMMAND_SMODE_ENTER), STATE_COMMAND);
281 dev_err(irtoy->dev, "could not write sample command: %d\n",
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.
293 static int irtoy_tx(struct rc_dev *rc, uint *txbuf, uint count)
295 struct irtoy *irtoy = rc->priv;
296 unsigned int i, size;
300 size = sizeof(u16) * (count + 1);
301 buf = kmalloc(size, GFP_KERNEL);
305 for (i = 0; i < count; i++) {
306 u16 v = DIV_ROUND_CLOSEST(txbuf[i], UNIT_US);
310 buf[i] = cpu_to_be16(v);
313 buf[count] = cpu_to_be16(0xffff);
316 irtoy->tx_len = size;
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);
326 dev_err(irtoy->dev, "exit sample mode: %d\n", err);
330 err = irtoy_command(irtoy, COMMAND_SMODE_ENTER,
331 sizeof(COMMAND_SMODE_ENTER), STATE_COMMAND);
333 dev_err(irtoy->dev, "enter sample mode: %d\n", err);
337 err = irtoy_command(irtoy, COMMAND_TXSTART, sizeof(COMMAND_TXSTART),
342 dev_err(irtoy->dev, "failed to send tx start command: %d\n",
344 // not sure what state the device is in, reset it
349 if (size != irtoy->emitted) {
350 dev_err(irtoy->dev, "expected %u emitted, got %u\n", size,
352 // not sure what state the device is in, reset it
360 static int irtoy_probe(struct usb_interface *intf,
361 const struct usb_device_id *id)
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;
371 int i, pipe, err = -ENOMEM;
373 for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
374 ep = &idesc->endpoint[i].desc;
376 if (!ep_in && usb_endpoint_is_bulk_in(ep) &&
377 usb_endpoint_maxp(ep) == MAX_PACKET)
380 if (!ep_out && usb_endpoint_is_bulk_out(ep) &&
381 usb_endpoint_maxp(ep) == MAX_PACKET)
385 if (!ep_in || !ep_out) {
386 dev_err(&intf->dev, "required endpoints not found\n");
390 irtoy = kzalloc(sizeof(*irtoy), GFP_KERNEL);
394 irtoy->in = kmalloc(MAX_PACKET, GFP_KERNEL);
398 irtoy->out = kmalloc(MAX_PACKET, GFP_KERNEL);
402 rc = rc_allocate_device(RC_DRIVER_IR_RAW);
406 urb = usb_alloc_urb(0, GFP_KERNEL);
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);
415 urb = usb_alloc_urb(0, GFP_KERNEL);
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);
423 irtoy->dev = &intf->dev;
424 irtoy->usbdev = usbdev;
426 irtoy->urb_out = urb;
429 err = usb_submit_urb(irtoy->urb_in, GFP_KERNEL);
431 dev_err(irtoy->dev, "fail to submit in urb: %d\n", err);
435 err = irtoy_setup(irtoy);
439 dev_info(irtoy->dev, "version: hardware %u, firmware %u, protocol %u",
440 irtoy->hw_version, irtoy->sw_version, irtoy->proto_version);
442 if (irtoy->sw_version < MIN_FW_VERSION) {
443 dev_err(irtoy->dev, "need firmware V%02u or higher",
449 usb_make_path(usbdev, irtoy->phys, sizeof(irtoy->phys));
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;
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;
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.
470 * So, make timeout a largish minimum which works with most protocols.
472 rc->min_timeout = MS_TO_US(40);
473 rc->max_timeout = MAX_TIMEOUT_US;
475 err = rc_register_device(rc);
479 usb_set_intfdata(intf, irtoy);
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);
496 static void irtoy_disconnect(struct usb_interface *intf)
498 struct irtoy *ir = usb_get_intfdata(intf);
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);
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) },
517 static struct usb_driver irtoy_driver = {
518 .name = KBUILD_MODNAME,
519 .probe = irtoy_probe,
520 .disconnect = irtoy_disconnect,
521 .id_table = irtoy_table,
524 module_usb_driver(irtoy_driver);
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);