1 // SPDX-License-Identifier: GPL-2.0-only
3 * tm6000-input.c - driver for TM5600/TM6000/TM6010 USB video capture devices
5 * Copyright (C) 2010 Stefan Ringel <stefan.ringel@arcor.de>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/delay.h>
12 #include <linux/input.h>
13 #include <linux/usb.h>
15 #include <media/rc-core.h>
18 #include "tm6000-regs.h"
20 static unsigned int ir_debug
;
21 module_param(ir_debug
, int, 0644);
22 MODULE_PARM_DESC(ir_debug
, "debug message level");
24 static unsigned int enable_ir
= 1;
25 module_param(enable_ir
, int, 0644);
26 MODULE_PARM_DESC(enable_ir
, "enable ir (default is enable)");
28 static unsigned int ir_clock_mhz
= 12;
29 module_param(ir_clock_mhz
, int, 0644);
30 MODULE_PARM_DESC(ir_clock_mhz
, "ir clock, in MHz");
32 #define URB_SUBMIT_DELAY 100 /* ms - Delay to submit an URB request on retrial and init */
33 #define URB_INT_LED_DELAY 100 /* ms - Delay to turn led on again on int mode */
37 #define dprintk(level, fmt, arg...) do {\
38 if (ir_debug >= level) \
39 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
42 struct tm6000_ir_poll_result
{
47 struct tm6000_core
*dev
;
52 /* poll expernal decoder */
54 struct delayed_work work
;
60 /* IR device properties */
64 void tm6000_ir_wait(struct tm6000_core
*dev
, u8 state
)
66 struct tm6000_IR
*ir
= dev
->ir
;
71 dprintk(2, "%s: %i\n",__func__
, ir
->wait
);
79 static int tm6000_ir_config(struct tm6000_IR
*ir
)
81 struct tm6000_core
*dev
= ir
->dev
;
82 u32 pulse
= 0, leader
= 0;
84 dprintk(2, "%s\n",__func__
);
87 * The IR decoder supports RC-5 or NEC, with a configurable timing.
88 * The timing configuration there is not that accurate, as it uses
89 * approximate values. The NEC spec mentions a 562.5 unit period,
90 * and RC-5 uses a 888.8 period.
91 * Currently, driver assumes a clock provided by a 12 MHz XTAL, but
92 * a modprobe parameter can adjust it.
93 * Adjustments are required for other timings.
94 * It seems that the 900ms timing for NEC is used to detect a RC-5
95 * IR, in order to discard such decoding
98 switch (ir
->rc_proto
) {
99 case RC_PROTO_BIT_NEC
:
100 leader
= 900; /* ms */
101 pulse
= 700; /* ms - the actual value would be 562 */
104 case RC_PROTO_BIT_RC5
:
105 leader
= 900; /* ms - from the NEC decoding */
106 pulse
= 1780; /* ms - The actual value would be 1776 */
110 pulse
= ir_clock_mhz
* pulse
;
111 leader
= ir_clock_mhz
* leader
;
112 if (ir
->rc_proto
== RC_PROTO_BIT_NEC
)
113 leader
= leader
| 0x8000;
115 dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n",
117 (ir
->rc_proto
== RC_PROTO_BIT_NEC
) ? "NEC" : "RC-5",
118 ir_clock_mhz
, leader
, pulse
);
120 /* Remote WAKEUP = enable, normal mode, from IR decoder output */
121 tm6000_set_reg(dev
, TM6010_REQ07_RE5_REMOTE_WAKEUP
, 0xfe);
123 /* Enable IR reception on non-busrt mode */
124 tm6000_set_reg(dev
, TM6010_REQ07_RD8_IR
, 0x2f);
126 /* IR_WKUP_SEL = Low byte in decoded IR data */
127 tm6000_set_reg(dev
, TM6010_REQ07_RDA_IR_WAKEUP_SEL
, 0xff);
128 /* IR_WKU_ADD code */
129 tm6000_set_reg(dev
, TM6010_REQ07_RDB_IR_WAKEUP_ADD
, 0xff);
131 tm6000_set_reg(dev
, TM6010_REQ07_RDC_IR_LEADER1
, leader
>> 8);
132 tm6000_set_reg(dev
, TM6010_REQ07_RDD_IR_LEADER0
, leader
);
134 tm6000_set_reg(dev
, TM6010_REQ07_RDE_IR_PULSE_CNT1
, pulse
>> 8);
135 tm6000_set_reg(dev
, TM6010_REQ07_RDF_IR_PULSE_CNT0
, pulse
);
138 tm6000_set_reg(dev
, REQ_04_EN_DISABLE_MCU_INT
, 2, 0);
140 tm6000_set_reg(dev
, REQ_04_EN_DISABLE_MCU_INT
, 2, 1);
143 /* Shows that IR is working via the LED */
144 tm6000_flash_led(dev
, 0);
146 tm6000_flash_led(dev
, 1);
152 static void tm6000_ir_keydown(struct tm6000_IR
*ir
,
153 const char *buf
, unsigned int len
)
157 enum rc_proto protocol
;
163 device
= (len
> 1 ? buf
[1] : 0x0);
164 switch (ir
->rc_proto
) {
165 case RC_PROTO_BIT_RC5
:
166 protocol
= RC_PROTO_RC5
;
167 scancode
= RC_SCANCODE_RC5(device
, command
);
169 case RC_PROTO_BIT_NEC
:
170 protocol
= RC_PROTO_NEC
;
171 scancode
= RC_SCANCODE_NEC(device
, command
);
174 protocol
= RC_PROTO_OTHER
;
175 scancode
= RC_SCANCODE_OTHER(device
<< 8 | command
);
179 dprintk(1, "%s, protocol: 0x%04x, scancode: 0x%08x\n",
180 __func__
, protocol
, scancode
);
181 rc_keydown(ir
->rc
, protocol
, scancode
, 0);
184 static void tm6000_ir_urb_received(struct urb
*urb
)
186 struct tm6000_core
*dev
= urb
->context
;
187 struct tm6000_IR
*ir
= dev
->ir
;
190 dprintk(2, "%s\n",__func__
);
191 if (urb
->status
< 0 || urb
->actual_length
<= 0) {
192 printk(KERN_INFO
"tm6000: IR URB failure: status: %i, length %i\n",
193 urb
->status
, urb
->actual_length
);
195 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(URB_SUBMIT_DELAY
));
198 buf
= urb
->transfer_buffer
;
201 print_hex_dump(KERN_DEBUG
, "tm6000: IR data: ",
202 DUMP_PREFIX_OFFSET
,16, 1,
203 buf
, urb
->actual_length
, false);
205 tm6000_ir_keydown(ir
, urb
->transfer_buffer
, urb
->actual_length
);
207 usb_submit_urb(urb
, GFP_ATOMIC
);
209 * Flash the led. We can't do it here, as it is running on IRQ context.
210 * So, use the scheduler to do it, in a few ms.
213 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(10));
216 static void tm6000_ir_handle_key(struct work_struct
*work
)
218 struct tm6000_IR
*ir
= container_of(work
, struct tm6000_IR
, work
.work
);
219 struct tm6000_core
*dev
= ir
->dev
;
226 dprintk(3, "%s\n",__func__
);
228 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
|
229 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
230 REQ_02_GET_IR_CODE
, 0, 0, buf
, 2);
234 /* Check if something was read */
235 if ((buf
[0] & 0xff) == 0xff) {
237 tm6000_flash_led(dev
, 1);
243 tm6000_ir_keydown(ir
, buf
, rc
);
244 tm6000_flash_led(dev
, 0);
247 /* Re-schedule polling */
248 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(ir
->polling
));
251 static void tm6000_ir_int_work(struct work_struct
*work
)
253 struct tm6000_IR
*ir
= container_of(work
, struct tm6000_IR
, work
.work
);
254 struct tm6000_core
*dev
= ir
->dev
;
257 dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__
, ir
->submit_urb
,
260 if (ir
->submit_urb
) {
261 dprintk(3, "Resubmit urb\n");
262 tm6000_set_reg(dev
, REQ_04_EN_DISABLE_MCU_INT
, 2, 0);
264 rc
= usb_submit_urb(ir
->int_urb
, GFP_ATOMIC
);
266 printk(KERN_ERR
"tm6000: Can't submit an IR interrupt. Error %i\n",
268 /* Retry in 100 ms */
269 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(URB_SUBMIT_DELAY
));
275 /* Led is enabled only if USB submit doesn't fail */
276 if (ir
->pwled
== 2) {
277 tm6000_flash_led(dev
, 0);
279 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(URB_INT_LED_DELAY
));
280 } else if (!ir
->pwled
) {
281 tm6000_flash_led(dev
, 1);
286 static int tm6000_ir_start(struct rc_dev
*rc
)
288 struct tm6000_IR
*ir
= rc
->priv
;
290 dprintk(2, "%s\n",__func__
);
292 schedule_delayed_work(&ir
->work
, 0);
297 static void tm6000_ir_stop(struct rc_dev
*rc
)
299 struct tm6000_IR
*ir
= rc
->priv
;
301 dprintk(2, "%s\n",__func__
);
303 cancel_delayed_work_sync(&ir
->work
);
306 static int tm6000_ir_change_protocol(struct rc_dev
*rc
, u64
*rc_proto
)
308 struct tm6000_IR
*ir
= rc
->priv
;
313 dprintk(2, "%s\n",__func__
);
315 ir
->rc_proto
= *rc_proto
;
317 tm6000_ir_config(ir
);
322 static int __tm6000_ir_int_start(struct rc_dev
*rc
)
324 struct tm6000_IR
*ir
= rc
->priv
;
325 struct tm6000_core
*dev
;
333 dprintk(2, "%s\n",__func__
);
335 ir
->int_urb
= usb_alloc_urb(0, GFP_ATOMIC
);
339 pipe
= usb_rcvintpipe(dev
->udev
,
340 dev
->int_in
.endp
->desc
.bEndpointAddress
341 & USB_ENDPOINT_NUMBER_MASK
);
343 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
344 dprintk(1, "IR max size: %d\n", size
);
346 ir
->int_urb
->transfer_buffer
= kzalloc(size
, GFP_ATOMIC
);
347 if (!ir
->int_urb
->transfer_buffer
) {
348 usb_free_urb(ir
->int_urb
);
351 dprintk(1, "int interval: %d\n", dev
->int_in
.endp
->desc
.bInterval
);
353 usb_fill_int_urb(ir
->int_urb
, dev
->udev
, pipe
,
354 ir
->int_urb
->transfer_buffer
, size
,
355 tm6000_ir_urb_received
, dev
,
356 dev
->int_in
.endp
->desc
.bInterval
);
359 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(URB_SUBMIT_DELAY
));
364 static void __tm6000_ir_int_stop(struct rc_dev
*rc
)
366 struct tm6000_IR
*ir
= rc
->priv
;
368 if (!ir
|| !ir
->int_urb
)
371 dprintk(2, "%s\n",__func__
);
373 usb_kill_urb(ir
->int_urb
);
374 kfree(ir
->int_urb
->transfer_buffer
);
375 usb_free_urb(ir
->int_urb
);
379 int tm6000_ir_int_start(struct tm6000_core
*dev
)
381 struct tm6000_IR
*ir
= dev
->ir
;
386 return __tm6000_ir_int_start(ir
->rc
);
389 void tm6000_ir_int_stop(struct tm6000_core
*dev
)
391 struct tm6000_IR
*ir
= dev
->ir
;
396 __tm6000_ir_int_stop(ir
->rc
);
399 int tm6000_ir_init(struct tm6000_core
*dev
)
401 struct tm6000_IR
*ir
;
409 if (!dev
->caps
.has_remote
)
415 ir
= kzalloc(sizeof(*ir
), GFP_ATOMIC
);
416 rc
= rc_allocate_device(RC_DRIVER_SCANCODE
);
420 dprintk(2, "%s\n", __func__
);
422 /* record handles to ourself */
428 rc
->allowed_protocols
= RC_PROTO_BIT_RC5
| RC_PROTO_BIT_NEC
;
429 /* Needed, in order to support NEC remotes with 24 or 32 bits */
430 rc
->scancode_mask
= 0xffff;
432 rc
->change_protocol
= tm6000_ir_change_protocol
;
433 if (dev
->int_in
.endp
) {
434 rc
->open
= __tm6000_ir_int_start
;
435 rc
->close
= __tm6000_ir_int_stop
;
436 INIT_DELAYED_WORK(&ir
->work
, tm6000_ir_int_work
);
438 rc
->open
= tm6000_ir_start
;
439 rc
->close
= tm6000_ir_stop
;
441 INIT_DELAYED_WORK(&ir
->work
, tm6000_ir_handle_key
);
444 snprintf(ir
->name
, sizeof(ir
->name
), "tm5600/60x0 IR (%s)",
447 usb_make_path(dev
->udev
, ir
->phys
, sizeof(ir
->phys
));
448 strlcat(ir
->phys
, "/input0", sizeof(ir
->phys
));
450 rc_proto
= RC_PROTO_BIT_UNKNOWN
;
451 tm6000_ir_change_protocol(rc
, &rc_proto
);
453 rc
->device_name
= ir
->name
;
454 rc
->input_phys
= ir
->phys
;
455 rc
->input_id
.bustype
= BUS_USB
;
456 rc
->input_id
.version
= 1;
457 rc
->input_id
.vendor
= le16_to_cpu(dev
->udev
->descriptor
.idVendor
);
458 rc
->input_id
.product
= le16_to_cpu(dev
->udev
->descriptor
.idProduct
);
459 rc
->map_name
= dev
->ir_codes
;
460 rc
->driver_name
= "tm6000";
461 rc
->dev
.parent
= &dev
->udev
->dev
;
464 err
= rc_register_device(rc
);
477 int tm6000_ir_fini(struct tm6000_core
*dev
)
479 struct tm6000_IR
*ir
= dev
->ir
;
481 /* skip detach on non attached board */
486 dprintk(2, "%s\n",__func__
);
489 __tm6000_ir_int_stop(ir
->rc
);
491 tm6000_ir_stop(ir
->rc
);
493 /* Turn off the led */
494 tm6000_flash_led(dev
, 0);
497 rc_unregister_device(ir
->rc
);