GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / media / usb / tm6000 / tm6000-input.c
1 /*
2  *  tm6000-input.c - driver for TM5600/TM6000/TM6010 USB video capture devices
3  *
4  *  Copyright (C) 2010 Stefan Ringel <stefan.ringel@arcor.de>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation version 2
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  */
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19
20 #include <linux/input.h>
21 #include <linux/usb.h>
22
23 #include <media/rc-core.h>
24
25 #include "tm6000.h"
26 #include "tm6000-regs.h"
27
28 static unsigned int ir_debug;
29 module_param(ir_debug, int, 0644);
30 MODULE_PARM_DESC(ir_debug, "debug message level");
31
32 static unsigned int enable_ir = 1;
33 module_param(enable_ir, int, 0644);
34 MODULE_PARM_DESC(enable_ir, "enable ir (default is enable)");
35
36 static unsigned int ir_clock_mhz = 12;
37 module_param(ir_clock_mhz, int, 0644);
38 MODULE_PARM_DESC(ir_clock_mhz, "ir clock, in MHz");
39
40 #define URB_SUBMIT_DELAY        100     /* ms - Delay to submit an URB request on retrial and init */
41 #define URB_INT_LED_DELAY       100     /* ms - Delay to turn led on again on int mode */
42
43 #undef dprintk
44
45 #define dprintk(level, fmt, arg...) do {\
46         if (ir_debug >= level) \
47                 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
48         } while (0)
49
50 struct tm6000_ir_poll_result {
51         u16 rc_data;
52 };
53
54 struct tm6000_IR {
55         struct tm6000_core      *dev;
56         struct rc_dev           *rc;
57         char                    name[32];
58         char                    phys[32];
59
60         /* poll expernal decoder */
61         int                     polling;
62         struct delayed_work     work;
63         u8                      wait:1;
64         u8                      pwled:2;
65         u8                      submit_urb:1;
66         struct urb              *int_urb;
67
68         /* IR device properties */
69         u64                     rc_proto;
70 };
71
72 void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
73 {
74         struct tm6000_IR *ir = dev->ir;
75
76         if (!dev->ir)
77                 return;
78
79         dprintk(2, "%s: %i\n",__func__, ir->wait);
80
81         if (state)
82                 ir->wait = 1;
83         else
84                 ir->wait = 0;
85 }
86
87 static int tm6000_ir_config(struct tm6000_IR *ir)
88 {
89         struct tm6000_core *dev = ir->dev;
90         u32 pulse = 0, leader = 0;
91
92         dprintk(2, "%s\n",__func__);
93
94         /*
95          * The IR decoder supports RC-5 or NEC, with a configurable timing.
96          * The timing configuration there is not that accurate, as it uses
97          * approximate values. The NEC spec mentions a 562.5 unit period,
98          * and RC-5 uses a 888.8 period.
99          * Currently, driver assumes a clock provided by a 12 MHz XTAL, but
100          * a modprobe parameter can adjust it.
101          * Adjustments are required for other timings.
102          * It seems that the 900ms timing for NEC is used to detect a RC-5
103          * IR, in order to discard such decoding
104          */
105
106         switch (ir->rc_proto) {
107         case RC_PROTO_BIT_NEC:
108                 leader = 900;   /* ms */
109                 pulse  = 700;   /* ms - the actual value would be 562 */
110                 break;
111         default:
112         case RC_PROTO_BIT_RC5:
113                 leader = 900;   /* ms - from the NEC decoding */
114                 pulse  = 1780;  /* ms - The actual value would be 1776 */
115                 break;
116         }
117
118         pulse = ir_clock_mhz * pulse;
119         leader = ir_clock_mhz * leader;
120         if (ir->rc_proto == RC_PROTO_BIT_NEC)
121                 leader = leader | 0x8000;
122
123         dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n",
124                 __func__,
125                 (ir->rc_proto == RC_PROTO_BIT_NEC) ? "NEC" : "RC-5",
126                 ir_clock_mhz, leader, pulse);
127
128         /* Remote WAKEUP = enable, normal mode, from IR decoder output */
129         tm6000_set_reg(dev, TM6010_REQ07_RE5_REMOTE_WAKEUP, 0xfe);
130
131         /* Enable IR reception on non-busrt mode */
132         tm6000_set_reg(dev, TM6010_REQ07_RD8_IR, 0x2f);
133
134         /* IR_WKUP_SEL = Low byte in decoded IR data */
135         tm6000_set_reg(dev, TM6010_REQ07_RDA_IR_WAKEUP_SEL, 0xff);
136         /* IR_WKU_ADD code */
137         tm6000_set_reg(dev, TM6010_REQ07_RDB_IR_WAKEUP_ADD, 0xff);
138
139         tm6000_set_reg(dev, TM6010_REQ07_RDC_IR_LEADER1, leader >> 8);
140         tm6000_set_reg(dev, TM6010_REQ07_RDD_IR_LEADER0, leader);
141
142         tm6000_set_reg(dev, TM6010_REQ07_RDE_IR_PULSE_CNT1, pulse >> 8);
143         tm6000_set_reg(dev, TM6010_REQ07_RDF_IR_PULSE_CNT0, pulse);
144
145         if (!ir->polling)
146                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
147         else
148                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
149         msleep(10);
150
151         /* Shows that IR is working via the LED */
152         tm6000_flash_led(dev, 0);
153         msleep(100);
154         tm6000_flash_led(dev, 1);
155         ir->pwled = 1;
156
157         return 0;
158 }
159
160 static void tm6000_ir_keydown(struct tm6000_IR *ir,
161                               const char *buf, unsigned int len)
162 {
163         u8 device, command;
164         u32 scancode;
165         enum rc_proto protocol;
166
167         if (len < 1)
168                 return;
169
170         command = buf[0];
171         device = (len > 1 ? buf[1] : 0x0);
172         switch (ir->rc_proto) {
173         case RC_PROTO_BIT_RC5:
174                 protocol = RC_PROTO_RC5;
175                 scancode = RC_SCANCODE_RC5(device, command);
176                 break;
177         case RC_PROTO_BIT_NEC:
178                 protocol = RC_PROTO_NEC;
179                 scancode = RC_SCANCODE_NEC(device, command);
180                 break;
181         default:
182                 protocol = RC_PROTO_OTHER;
183                 scancode = RC_SCANCODE_OTHER(device << 8 | command);
184                 break;
185         }
186
187         dprintk(1, "%s, protocol: 0x%04x, scancode: 0x%08x\n",
188                 __func__, protocol, scancode);
189         rc_keydown(ir->rc, protocol, scancode, 0);
190 }
191
192 static void tm6000_ir_urb_received(struct urb *urb)
193 {
194         struct tm6000_core *dev = urb->context;
195         struct tm6000_IR *ir = dev->ir;
196         char *buf;
197
198         dprintk(2, "%s\n",__func__);
199         if (urb->status < 0 || urb->actual_length <= 0) {
200                 printk(KERN_INFO "tm6000: IR URB failure: status: %i, length %i\n",
201                        urb->status, urb->actual_length);
202                 ir->submit_urb = 1;
203                 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
204                 return;
205         }
206         buf = urb->transfer_buffer;
207
208         if (ir_debug)
209                 print_hex_dump(KERN_DEBUG, "tm6000: IR data: ",
210                                DUMP_PREFIX_OFFSET,16, 1,
211                                buf, urb->actual_length, false);
212
213         tm6000_ir_keydown(ir, urb->transfer_buffer, urb->actual_length);
214
215         usb_submit_urb(urb, GFP_ATOMIC);
216         /*
217          * Flash the led. We can't do it here, as it is running on IRQ context.
218          * So, use the scheduler to do it, in a few ms.
219          */
220         ir->pwled = 2;
221         schedule_delayed_work(&ir->work, msecs_to_jiffies(10));
222 }
223
224 static void tm6000_ir_handle_key(struct work_struct *work)
225 {
226         struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
227         struct tm6000_core *dev = ir->dev;
228         int rc;
229         u8 buf[2];
230
231         if (ir->wait)
232                 return;
233
234         dprintk(3, "%s\n",__func__);
235
236         rc = tm6000_read_write_usb(dev, USB_DIR_IN |
237                 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
238                 REQ_02_GET_IR_CODE, 0, 0, buf, 2);
239         if (rc < 0)
240                 return;
241
242         /* Check if something was read */
243         if ((buf[0] & 0xff) == 0xff) {
244                 if (!ir->pwled) {
245                         tm6000_flash_led(dev, 1);
246                         ir->pwled = 1;
247                 }
248                 return;
249         }
250
251         tm6000_ir_keydown(ir, buf, rc);
252         tm6000_flash_led(dev, 0);
253         ir->pwled = 0;
254
255         /* Re-schedule polling */
256         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
257 }
258
259 static void tm6000_ir_int_work(struct work_struct *work)
260 {
261         struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
262         struct tm6000_core *dev = ir->dev;
263         int rc;
264
265         dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb,
266                 ir->pwled);
267
268         if (ir->submit_urb) {
269                 dprintk(3, "Resubmit urb\n");
270                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
271
272                 rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC);
273                 if (rc < 0) {
274                         printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n",
275                                rc);
276                         /* Retry in 100 ms */
277                         schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
278                         return;
279                 }
280                 ir->submit_urb = 0;
281         }
282
283         /* Led is enabled only if USB submit doesn't fail */
284         if (ir->pwled == 2) {
285                 tm6000_flash_led(dev, 0);
286                 ir->pwled = 0;
287                 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_INT_LED_DELAY));
288         } else if (!ir->pwled) {
289                 tm6000_flash_led(dev, 1);
290                 ir->pwled = 1;
291         }
292 }
293
294 static int tm6000_ir_start(struct rc_dev *rc)
295 {
296         struct tm6000_IR *ir = rc->priv;
297
298         dprintk(2, "%s\n",__func__);
299
300         schedule_delayed_work(&ir->work, 0);
301
302         return 0;
303 }
304
305 static void tm6000_ir_stop(struct rc_dev *rc)
306 {
307         struct tm6000_IR *ir = rc->priv;
308
309         dprintk(2, "%s\n",__func__);
310
311         cancel_delayed_work_sync(&ir->work);
312 }
313
314 static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
315 {
316         struct tm6000_IR *ir = rc->priv;
317
318         if (!ir)
319                 return 0;
320
321         dprintk(2, "%s\n",__func__);
322
323         ir->rc_proto = *rc_proto;
324
325         tm6000_ir_config(ir);
326         /* TODO */
327         return 0;
328 }
329
330 static int __tm6000_ir_int_start(struct rc_dev *rc)
331 {
332         struct tm6000_IR *ir = rc->priv;
333         struct tm6000_core *dev;
334         int pipe, size;
335         int err = -ENOMEM;
336
337         if (!ir)
338                 return -ENODEV;
339         dev = ir->dev;
340
341         dprintk(2, "%s\n",__func__);
342
343         ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC);
344         if (!ir->int_urb)
345                 return -ENOMEM;
346
347         pipe = usb_rcvintpipe(dev->udev,
348                 dev->int_in.endp->desc.bEndpointAddress
349                 & USB_ENDPOINT_NUMBER_MASK);
350
351         size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
352         dprintk(1, "IR max size: %d\n", size);
353
354         ir->int_urb->transfer_buffer = kzalloc(size, GFP_ATOMIC);
355         if (ir->int_urb->transfer_buffer == NULL) {
356                 usb_free_urb(ir->int_urb);
357                 return err;
358         }
359         dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval);
360
361         usb_fill_int_urb(ir->int_urb, dev->udev, pipe,
362                 ir->int_urb->transfer_buffer, size,
363                 tm6000_ir_urb_received, dev,
364                 dev->int_in.endp->desc.bInterval);
365
366         ir->submit_urb = 1;
367         schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
368
369         return 0;
370 }
371
372 static void __tm6000_ir_int_stop(struct rc_dev *rc)
373 {
374         struct tm6000_IR *ir = rc->priv;
375
376         if (!ir || !ir->int_urb)
377                 return;
378
379         dprintk(2, "%s\n",__func__);
380
381         usb_kill_urb(ir->int_urb);
382         kfree(ir->int_urb->transfer_buffer);
383         usb_free_urb(ir->int_urb);
384         ir->int_urb = NULL;
385 }
386
387 int tm6000_ir_int_start(struct tm6000_core *dev)
388 {
389         struct tm6000_IR *ir = dev->ir;
390
391         if (!ir)
392                 return 0;
393
394         return __tm6000_ir_int_start(ir->rc);
395 }
396
397 void tm6000_ir_int_stop(struct tm6000_core *dev)
398 {
399         struct tm6000_IR *ir = dev->ir;
400
401         if (!ir || !ir->rc)
402                 return;
403
404         __tm6000_ir_int_stop(ir->rc);
405 }
406
407 int tm6000_ir_init(struct tm6000_core *dev)
408 {
409         struct tm6000_IR *ir;
410         struct rc_dev *rc;
411         int err = -ENOMEM;
412         u64 rc_proto;
413
414         if (!enable_ir)
415                 return -ENODEV;
416
417         if (!dev->caps.has_remote)
418                 return 0;
419
420         if (!dev->ir_codes)
421                 return 0;
422
423         ir = kzalloc(sizeof(*ir), GFP_ATOMIC);
424         rc = rc_allocate_device(RC_DRIVER_SCANCODE);
425         if (!ir || !rc)
426                 goto out;
427
428         dprintk(2, "%s\n", __func__);
429
430         /* record handles to ourself */
431         ir->dev = dev;
432         dev->ir = ir;
433         ir->rc = rc;
434
435         /* input setup */
436         rc->allowed_protocols = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_NEC;
437         /* Needed, in order to support NEC remotes with 24 or 32 bits */
438         rc->scancode_mask = 0xffff;
439         rc->priv = ir;
440         rc->change_protocol = tm6000_ir_change_protocol;
441         if (dev->int_in.endp) {
442                 rc->open    = __tm6000_ir_int_start;
443                 rc->close   = __tm6000_ir_int_stop;
444                 INIT_DELAYED_WORK(&ir->work, tm6000_ir_int_work);
445         } else {
446                 rc->open  = tm6000_ir_start;
447                 rc->close = tm6000_ir_stop;
448                 ir->polling = 50;
449                 INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key);
450         }
451
452         snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
453                                                 dev->name);
454
455         usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
456         strlcat(ir->phys, "/input0", sizeof(ir->phys));
457
458         rc_proto = RC_PROTO_BIT_UNKNOWN;
459         tm6000_ir_change_protocol(rc, &rc_proto);
460
461         rc->device_name = ir->name;
462         rc->input_phys = ir->phys;
463         rc->input_id.bustype = BUS_USB;
464         rc->input_id.version = 1;
465         rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
466         rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
467         rc->map_name = dev->ir_codes;
468         rc->driver_name = "tm6000";
469         rc->dev.parent = &dev->udev->dev;
470
471         /* ir register */
472         err = rc_register_device(rc);
473         if (err)
474                 goto out;
475
476         return 0;
477
478 out:
479         dev->ir = NULL;
480         rc_free_device(rc);
481         kfree(ir);
482         return err;
483 }
484
485 int tm6000_ir_fini(struct tm6000_core *dev)
486 {
487         struct tm6000_IR *ir = dev->ir;
488
489         /* skip detach on non attached board */
490
491         if (!ir)
492                 return 0;
493
494         dprintk(2, "%s\n",__func__);
495
496         if (!ir->polling)
497                 __tm6000_ir_int_stop(ir->rc);
498
499         tm6000_ir_stop(ir->rc);
500
501         /* Turn off the led */
502         tm6000_flash_led(dev, 0);
503         ir->pwled = 0;
504
505         rc_unregister_device(ir->rc);
506
507         kfree(ir);
508         dev->ir = NULL;
509
510         return 0;
511 }