2 * tm6000-input.c - driver for TM5600/TM6000/TM6010 USB video capture devices
4 * Copyright (C) 2010 Stefan Ringel <stefan.ringel@arcor.de>
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
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.
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <linux/input.h>
21 #include <linux/usb.h>
23 #include <media/rc-core.h>
26 #include "tm6000-regs.h"
28 static unsigned int ir_debug;
29 module_param(ir_debug, int, 0644);
30 MODULE_PARM_DESC(ir_debug, "debug message level");
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)");
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");
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 */
45 #define dprintk(level, fmt, arg...) do {\
46 if (ir_debug >= level) \
47 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
50 struct tm6000_ir_poll_result {
55 struct tm6000_core *dev;
60 /* poll expernal decoder */
62 struct delayed_work work;
69 /* IR device properties */
73 void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
75 struct tm6000_IR *ir = dev->ir;
80 dprintk(2, "%s: %i\n",__func__, ir->wait);
88 static int tm6000_ir_config(struct tm6000_IR *ir)
90 struct tm6000_core *dev = ir->dev;
91 u32 pulse = 0, leader = 0;
93 dprintk(2, "%s\n",__func__);
96 * The IR decoder supports RC-5 or NEC, with a configurable timing.
97 * The timing configuration there is not that accurate, as it uses
98 * approximate values. The NEC spec mentions a 562.5 unit period,
99 * and RC-5 uses a 888.8 period.
100 * Currently, driver assumes a clock provided by a 12 MHz XTAL, but
101 * a modprobe parameter can adjust it.
102 * Adjustments are required for other timings.
103 * It seems that the 900ms timing for NEC is used to detect a RC-5
104 * IR, in order to discard such decoding
107 switch (ir->rc_type) {
109 leader = 900; /* ms */
110 pulse = 700; /* ms - the actual value would be 562 */
114 leader = 900; /* ms - from the NEC decoding */
115 pulse = 1780; /* ms - The actual value would be 1776 */
119 pulse = ir_clock_mhz * pulse;
120 leader = ir_clock_mhz * leader;
121 if (ir->rc_type == RC_BIT_NEC)
122 leader = leader | 0x8000;
124 dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n",
126 (ir->rc_type == RC_BIT_NEC) ? "NEC" : "RC-5",
127 ir_clock_mhz, leader, pulse);
129 /* Remote WAKEUP = enable, normal mode, from IR decoder output */
130 tm6000_set_reg(dev, TM6010_REQ07_RE5_REMOTE_WAKEUP, 0xfe);
132 /* Enable IR reception on non-busrt mode */
133 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR, 0x2f);
135 /* IR_WKUP_SEL = Low byte in decoded IR data */
136 tm6000_set_reg(dev, TM6010_REQ07_RDA_IR_WAKEUP_SEL, 0xff);
137 /* IR_WKU_ADD code */
138 tm6000_set_reg(dev, TM6010_REQ07_RDB_IR_WAKEUP_ADD, 0xff);
140 tm6000_set_reg(dev, TM6010_REQ07_RDC_IR_LEADER1, leader >> 8);
141 tm6000_set_reg(dev, TM6010_REQ07_RDD_IR_LEADER0, leader);
143 tm6000_set_reg(dev, TM6010_REQ07_RDE_IR_PULSE_CNT1, pulse >> 8);
144 tm6000_set_reg(dev, TM6010_REQ07_RDF_IR_PULSE_CNT0, pulse);
147 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
149 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
152 /* Shows that IR is working via the LED */
153 tm6000_flash_led(dev, 0);
155 tm6000_flash_led(dev, 1);
161 static void tm6000_ir_keydown(struct tm6000_IR *ir,
162 const char *buf, unsigned int len)
166 enum rc_type protocol;
172 device = (len > 1 ? buf[1] : 0x0);
173 switch (ir->rc_type) {
175 protocol = RC_TYPE_RC5;
176 scancode = RC_SCANCODE_RC5(device, command);
179 protocol = RC_TYPE_NEC;
180 scancode = RC_SCANCODE_NEC(device, command);
183 protocol = RC_TYPE_OTHER;
184 scancode = RC_SCANCODE_OTHER(device << 8 | command);
188 dprintk(1, "%s, protocol: 0x%04x, scancode: 0x%08x\n",
189 __func__, protocol, scancode);
190 rc_keydown(ir->rc, protocol, scancode, 0);
193 static void tm6000_ir_urb_received(struct urb *urb)
195 struct tm6000_core *dev = urb->context;
196 struct tm6000_IR *ir = dev->ir;
199 dprintk(2, "%s\n",__func__);
200 if (urb->status < 0 || urb->actual_length <= 0) {
201 printk(KERN_INFO "tm6000: IR URB failure: status: %i, length %i\n",
202 urb->status, urb->actual_length);
204 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
207 buf = urb->transfer_buffer;
210 print_hex_dump(KERN_DEBUG, "tm6000: IR data: ",
211 DUMP_PREFIX_OFFSET,16, 1,
212 buf, urb->actual_length, false);
214 tm6000_ir_keydown(ir, urb->transfer_buffer, urb->actual_length);
216 usb_submit_urb(urb, GFP_ATOMIC);
218 * Flash the led. We can't do it here, as it is running on IRQ context.
219 * So, use the scheduler to do it, in a few ms.
222 schedule_delayed_work(&ir->work, msecs_to_jiffies(10));
225 static void tm6000_ir_handle_key(struct work_struct *work)
227 struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
228 struct tm6000_core *dev = ir->dev;
235 dprintk(3, "%s\n",__func__);
237 rc = tm6000_read_write_usb(dev, USB_DIR_IN |
238 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
239 REQ_02_GET_IR_CODE, 0, 0, buf, 2);
243 /* Check if something was read */
244 if ((buf[0] & 0xff) == 0xff) {
246 tm6000_flash_led(dev, 1);
252 tm6000_ir_keydown(ir, buf, rc);
253 tm6000_flash_led(dev, 0);
256 /* Re-schedule polling */
257 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
260 static void tm6000_ir_int_work(struct work_struct *work)
262 struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
263 struct tm6000_core *dev = ir->dev;
266 dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb,
269 if (ir->submit_urb) {
270 dprintk(3, "Resubmit urb\n");
271 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
273 rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC);
275 printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n",
277 /* Retry in 100 ms */
278 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
284 /* Led is enabled only if USB submit doesn't fail */
285 if (ir->pwled == 2) {
286 tm6000_flash_led(dev, 0);
288 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_INT_LED_DELAY));
289 } else if (!ir->pwled) {
290 tm6000_flash_led(dev, 1);
295 static int tm6000_ir_start(struct rc_dev *rc)
297 struct tm6000_IR *ir = rc->priv;
299 dprintk(2, "%s\n",__func__);
301 schedule_delayed_work(&ir->work, 0);
306 static void tm6000_ir_stop(struct rc_dev *rc)
308 struct tm6000_IR *ir = rc->priv;
310 dprintk(2, "%s\n",__func__);
312 cancel_delayed_work_sync(&ir->work);
315 static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 *rc_type)
317 struct tm6000_IR *ir = rc->priv;
322 dprintk(2, "%s\n",__func__);
324 if ((rc->rc_map.scan) && (*rc_type == RC_BIT_NEC))
325 ir->key_addr = ((rc->rc_map.scan[0].scancode >> 8) & 0xffff);
327 ir->rc_type = *rc_type;
329 tm6000_ir_config(ir);
334 static int __tm6000_ir_int_start(struct rc_dev *rc)
336 struct tm6000_IR *ir = rc->priv;
337 struct tm6000_core *dev;
345 dprintk(2, "%s\n",__func__);
347 ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC);
351 pipe = usb_rcvintpipe(dev->udev,
352 dev->int_in.endp->desc.bEndpointAddress
353 & USB_ENDPOINT_NUMBER_MASK);
355 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
356 dprintk(1, "IR max size: %d\n", size);
358 ir->int_urb->transfer_buffer = kzalloc(size, GFP_ATOMIC);
359 if (ir->int_urb->transfer_buffer == NULL) {
360 usb_free_urb(ir->int_urb);
363 dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval);
365 usb_fill_int_urb(ir->int_urb, dev->udev, pipe,
366 ir->int_urb->transfer_buffer, size,
367 tm6000_ir_urb_received, dev,
368 dev->int_in.endp->desc.bInterval);
371 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
376 static void __tm6000_ir_int_stop(struct rc_dev *rc)
378 struct tm6000_IR *ir = rc->priv;
380 if (!ir || !ir->int_urb)
383 dprintk(2, "%s\n",__func__);
385 usb_kill_urb(ir->int_urb);
386 kfree(ir->int_urb->transfer_buffer);
387 usb_free_urb(ir->int_urb);
391 int tm6000_ir_int_start(struct tm6000_core *dev)
393 struct tm6000_IR *ir = dev->ir;
398 return __tm6000_ir_int_start(ir->rc);
401 void tm6000_ir_int_stop(struct tm6000_core *dev)
403 struct tm6000_IR *ir = dev->ir;
408 __tm6000_ir_int_stop(ir->rc);
411 int tm6000_ir_init(struct tm6000_core *dev)
413 struct tm6000_IR *ir;
421 if (!dev->caps.has_remote)
427 ir = kzalloc(sizeof(*ir), GFP_ATOMIC);
428 rc = rc_allocate_device(RC_DRIVER_SCANCODE);
432 dprintk(2, "%s\n", __func__);
434 /* record handles to ourself */
440 rc->allowed_protocols = RC_BIT_RC5 | RC_BIT_NEC;
441 /* Neded, in order to support NEC remotes with 24 or 32 bits */
442 rc->scancode_mask = 0xffff;
444 rc->change_protocol = tm6000_ir_change_protocol;
445 if (dev->int_in.endp) {
446 rc->open = __tm6000_ir_int_start;
447 rc->close = __tm6000_ir_int_stop;
448 INIT_DELAYED_WORK(&ir->work, tm6000_ir_int_work);
450 rc->open = tm6000_ir_start;
451 rc->close = tm6000_ir_stop;
453 INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key);
456 snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
459 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
460 strlcat(ir->phys, "/input0", sizeof(ir->phys));
462 rc_type = RC_BIT_UNKNOWN;
463 tm6000_ir_change_protocol(rc, &rc_type);
465 rc->input_name = ir->name;
466 rc->input_phys = ir->phys;
467 rc->input_id.bustype = BUS_USB;
468 rc->input_id.version = 1;
469 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
470 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
471 rc->map_name = dev->ir_codes;
472 rc->driver_name = "tm6000";
473 rc->dev.parent = &dev->udev->dev;
476 err = rc_register_device(rc);
489 int tm6000_ir_fini(struct tm6000_core *dev)
491 struct tm6000_IR *ir = dev->ir;
493 /* skip detach on non attached board */
498 dprintk(2, "%s\n",__func__);
501 __tm6000_ir_int_stop(ir->rc);
503 tm6000_ir_stop(ir->rc);
505 /* Turn off the led */
506 tm6000_flash_led(dev, 0);
509 rc_unregister_device(ir->rc);