GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / media / rc / ir_toy.c
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] = cpu_to_be16(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                 kfree(buf);
328                 return err;
329         }
330
331         err = irtoy_command(irtoy, COMMAND_SMODE_ENTER,
332                             sizeof(COMMAND_SMODE_ENTER), STATE_COMMAND);
333         if (err) {
334                 dev_err(irtoy->dev, "enter sample mode: %d\n", err);
335                 kfree(buf);
336                 return err;
337         }
338
339         err = irtoy_command(irtoy, COMMAND_TXSTART, sizeof(COMMAND_TXSTART),
340                             STATE_TX);
341         kfree(buf);
342
343         if (err) {
344                 dev_err(irtoy->dev, "failed to send tx start command: %d\n",
345                         err);
346                 // not sure what state the device is in, reset it
347                 irtoy_setup(irtoy);
348                 return err;
349         }
350
351         if (size != irtoy->emitted) {
352                 dev_err(irtoy->dev, "expected %u emitted, got %u\n", size,
353                         irtoy->emitted);
354                 // not sure what state the device is in, reset it
355                 irtoy_setup(irtoy);
356                 return -EINVAL;
357         }
358
359         return count;
360 }
361
362 static int irtoy_probe(struct usb_interface *intf,
363                        const struct usb_device_id *id)
364 {
365         struct usb_host_interface *idesc = intf->cur_altsetting;
366         struct usb_device *usbdev = interface_to_usbdev(intf);
367         struct usb_endpoint_descriptor *ep_in = NULL;
368         struct usb_endpoint_descriptor *ep_out = NULL;
369         struct usb_endpoint_descriptor *ep = NULL;
370         struct irtoy *irtoy;
371         struct rc_dev *rc;
372         struct urb *urb;
373         int i, pipe, err = -ENOMEM;
374
375         for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
376                 ep = &idesc->endpoint[i].desc;
377
378                 if (!ep_in && usb_endpoint_is_bulk_in(ep) &&
379                     usb_endpoint_maxp(ep) == MAX_PACKET)
380                         ep_in = ep;
381
382                 if (!ep_out && usb_endpoint_is_bulk_out(ep) &&
383                     usb_endpoint_maxp(ep) == MAX_PACKET)
384                         ep_out = ep;
385         }
386
387         if (!ep_in || !ep_out) {
388                 dev_err(&intf->dev, "required endpoints not found\n");
389                 return -ENODEV;
390         }
391
392         irtoy = kzalloc(sizeof(*irtoy), GFP_KERNEL);
393         if (!irtoy)
394                 return -ENOMEM;
395
396         irtoy->in = kmalloc(MAX_PACKET,  GFP_KERNEL);
397         if (!irtoy->in)
398                 goto free_irtoy;
399
400         irtoy->out = kmalloc(MAX_PACKET,  GFP_KERNEL);
401         if (!irtoy->out)
402                 goto free_irtoy;
403
404         rc = rc_allocate_device(RC_DRIVER_IR_RAW);
405         if (!rc)
406                 goto free_irtoy;
407
408         urb = usb_alloc_urb(0, GFP_KERNEL);
409         if (!urb)
410                 goto free_rcdev;
411
412         pipe = usb_rcvbulkpipe(usbdev, ep_in->bEndpointAddress);
413         usb_fill_bulk_urb(urb, usbdev, pipe, irtoy->in, MAX_PACKET,
414                           irtoy_in_callback, irtoy);
415         irtoy->urb_in = urb;
416
417         urb = usb_alloc_urb(0, GFP_KERNEL);
418         if (!urb)
419                 goto free_rcdev;
420
421         pipe = usb_sndbulkpipe(usbdev, ep_out->bEndpointAddress);
422         usb_fill_bulk_urb(urb, usbdev, pipe, irtoy->out, MAX_PACKET,
423                           irtoy_out_callback, irtoy);
424
425         irtoy->dev = &intf->dev;
426         irtoy->usbdev = usbdev;
427         irtoy->rc = rc;
428         irtoy->urb_out = urb;
429         irtoy->pulse = true;
430
431         err = usb_submit_urb(irtoy->urb_in, GFP_KERNEL);
432         if (err != 0) {
433                 dev_err(irtoy->dev, "fail to submit in urb: %d\n", err);
434                 goto free_rcdev;
435         }
436
437         err = irtoy_setup(irtoy);
438         if (err)
439                 goto free_rcdev;
440
441         dev_info(irtoy->dev, "version: hardware %u, firmware %u, protocol %u",
442                  irtoy->hw_version, irtoy->sw_version, irtoy->proto_version);
443
444         if (irtoy->sw_version < MIN_FW_VERSION) {
445                 dev_err(irtoy->dev, "need firmware V%02u or higher",
446                         MIN_FW_VERSION);
447                 err = -ENODEV;
448                 goto free_rcdev;
449         }
450
451         usb_make_path(usbdev, irtoy->phys, sizeof(irtoy->phys));
452
453         rc->device_name = "Infrared Toy";
454         rc->driver_name = KBUILD_MODNAME;
455         rc->input_phys = irtoy->phys;
456         usb_to_input_id(usbdev, &rc->input_id);
457         rc->dev.parent = &intf->dev;
458         rc->priv = irtoy;
459         rc->tx_ir = irtoy_tx;
460         rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
461         rc->map_name = RC_MAP_RC6_MCE;
462         rc->rx_resolution = UNIT_US;
463         rc->timeout = IR_DEFAULT_TIMEOUT;
464
465         /*
466          * end of transmission is detected by absence of a usb packet
467          * with more pulse/spaces. However, each usb packet sent can
468          * contain 32 pulse/spaces, which can be quite lengthy, so there
469          * can be a delay between usb packets. For example with nec there is a
470          * 17ms gap between packets.
471          *
472          * So, make timeout a largish minimum which works with most protocols.
473          */
474         rc->min_timeout = MS_TO_US(40);
475         rc->max_timeout = MAX_TIMEOUT_US;
476
477         err = rc_register_device(rc);
478         if (err)
479                 goto free_rcdev;
480
481         usb_set_intfdata(intf, irtoy);
482
483         return 0;
484
485 free_rcdev:
486         usb_kill_urb(irtoy->urb_out);
487         usb_free_urb(irtoy->urb_out);
488         usb_kill_urb(irtoy->urb_in);
489         usb_free_urb(irtoy->urb_in);
490         rc_free_device(rc);
491 free_irtoy:
492         kfree(irtoy->in);
493         kfree(irtoy->out);
494         kfree(irtoy);
495         return err;
496 }
497
498 static void irtoy_disconnect(struct usb_interface *intf)
499 {
500         struct irtoy *ir = usb_get_intfdata(intf);
501
502         rc_unregister_device(ir->rc);
503         usb_set_intfdata(intf, NULL);
504         usb_kill_urb(ir->urb_out);
505         usb_free_urb(ir->urb_out);
506         usb_kill_urb(ir->urb_in);
507         usb_free_urb(ir->urb_in);
508         kfree(ir->in);
509         kfree(ir->out);
510         kfree(ir);
511 }
512
513 static const struct usb_device_id irtoy_table[] = {
514         { USB_DEVICE_INTERFACE_CLASS(0x04d8, 0xfd08, USB_CLASS_CDC_DATA) },
515         { USB_DEVICE_INTERFACE_CLASS(0x04d8, 0xf58b, USB_CLASS_CDC_DATA) },
516         { }
517 };
518
519 static struct usb_driver irtoy_driver = {
520         .name = KBUILD_MODNAME,
521         .probe = irtoy_probe,
522         .disconnect = irtoy_disconnect,
523         .id_table = irtoy_table,
524 };
525
526 module_usb_driver(irtoy_driver);
527
528 MODULE_AUTHOR("Sean Young <sean@mess.org>");
529 MODULE_DESCRIPTION("Infrared Toy and IR Droid driver");
530 MODULE_LICENSE("GPL");
531 MODULE_DEVICE_TABLE(usb, irtoy_table);