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;
68 /* IR device properties */
72 void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
74 struct tm6000_IR *ir = dev->ir;
79 dprintk(2, "%s: %i\n",__func__, ir->wait);
87 static int tm6000_ir_config(struct tm6000_IR *ir)
89 struct tm6000_core *dev = ir->dev;
90 u32 pulse = 0, leader = 0;
92 dprintk(2, "%s\n",__func__);
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
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 */
112 case RC_PROTO_BIT_RC5:
113 leader = 900; /* ms - from the NEC decoding */
114 pulse = 1780; /* ms - The actual value would be 1776 */
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;
123 dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n",
125 (ir->rc_proto == RC_PROTO_BIT_NEC) ? "NEC" : "RC-5",
126 ir_clock_mhz, leader, pulse);
128 /* Remote WAKEUP = enable, normal mode, from IR decoder output */
129 tm6000_set_reg(dev, TM6010_REQ07_RE5_REMOTE_WAKEUP, 0xfe);
131 /* Enable IR reception on non-busrt mode */
132 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR, 0x2f);
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);
139 tm6000_set_reg(dev, TM6010_REQ07_RDC_IR_LEADER1, leader >> 8);
140 tm6000_set_reg(dev, TM6010_REQ07_RDD_IR_LEADER0, leader);
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);
146 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
148 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
151 /* Shows that IR is working via the LED */
152 tm6000_flash_led(dev, 0);
154 tm6000_flash_led(dev, 1);
160 static void tm6000_ir_keydown(struct tm6000_IR *ir,
161 const char *buf, unsigned int len)
165 enum rc_proto protocol;
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);
177 case RC_PROTO_BIT_NEC:
178 protocol = RC_PROTO_NEC;
179 scancode = RC_SCANCODE_NEC(device, command);
182 protocol = RC_PROTO_OTHER;
183 scancode = RC_SCANCODE_OTHER(device << 8 | command);
187 dprintk(1, "%s, protocol: 0x%04x, scancode: 0x%08x\n",
188 __func__, protocol, scancode);
189 rc_keydown(ir->rc, protocol, scancode, 0);
192 static void tm6000_ir_urb_received(struct urb *urb)
194 struct tm6000_core *dev = urb->context;
195 struct tm6000_IR *ir = dev->ir;
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);
203 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
206 buf = urb->transfer_buffer;
209 print_hex_dump(KERN_DEBUG, "tm6000: IR data: ",
210 DUMP_PREFIX_OFFSET,16, 1,
211 buf, urb->actual_length, false);
213 tm6000_ir_keydown(ir, urb->transfer_buffer, urb->actual_length);
215 usb_submit_urb(urb, GFP_ATOMIC);
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.
221 schedule_delayed_work(&ir->work, msecs_to_jiffies(10));
224 static void tm6000_ir_handle_key(struct work_struct *work)
226 struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
227 struct tm6000_core *dev = ir->dev;
234 dprintk(3, "%s\n",__func__);
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);
242 /* Check if something was read */
243 if ((buf[0] & 0xff) == 0xff) {
245 tm6000_flash_led(dev, 1);
251 tm6000_ir_keydown(ir, buf, rc);
252 tm6000_flash_led(dev, 0);
255 /* Re-schedule polling */
256 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
259 static void tm6000_ir_int_work(struct work_struct *work)
261 struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
262 struct tm6000_core *dev = ir->dev;
265 dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb,
268 if (ir->submit_urb) {
269 dprintk(3, "Resubmit urb\n");
270 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
272 rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC);
274 printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n",
276 /* Retry in 100 ms */
277 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
283 /* Led is enabled only if USB submit doesn't fail */
284 if (ir->pwled == 2) {
285 tm6000_flash_led(dev, 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);
294 static int tm6000_ir_start(struct rc_dev *rc)
296 struct tm6000_IR *ir = rc->priv;
298 dprintk(2, "%s\n",__func__);
300 schedule_delayed_work(&ir->work, 0);
305 static void tm6000_ir_stop(struct rc_dev *rc)
307 struct tm6000_IR *ir = rc->priv;
309 dprintk(2, "%s\n",__func__);
311 cancel_delayed_work_sync(&ir->work);
314 static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 *rc_proto)
316 struct tm6000_IR *ir = rc->priv;
321 dprintk(2, "%s\n",__func__);
323 ir->rc_proto = *rc_proto;
325 tm6000_ir_config(ir);
330 static int __tm6000_ir_int_start(struct rc_dev *rc)
332 struct tm6000_IR *ir = rc->priv;
333 struct tm6000_core *dev;
341 dprintk(2, "%s\n",__func__);
343 ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC);
347 pipe = usb_rcvintpipe(dev->udev,
348 dev->int_in.endp->desc.bEndpointAddress
349 & USB_ENDPOINT_NUMBER_MASK);
351 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
352 dprintk(1, "IR max size: %d\n", size);
354 ir->int_urb->transfer_buffer = kzalloc(size, GFP_ATOMIC);
355 if (!ir->int_urb->transfer_buffer) {
356 usb_free_urb(ir->int_urb);
359 dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval);
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);
367 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
372 static void __tm6000_ir_int_stop(struct rc_dev *rc)
374 struct tm6000_IR *ir = rc->priv;
376 if (!ir || !ir->int_urb)
379 dprintk(2, "%s\n",__func__);
381 usb_kill_urb(ir->int_urb);
382 kfree(ir->int_urb->transfer_buffer);
383 usb_free_urb(ir->int_urb);
387 int tm6000_ir_int_start(struct tm6000_core *dev)
389 struct tm6000_IR *ir = dev->ir;
394 return __tm6000_ir_int_start(ir->rc);
397 void tm6000_ir_int_stop(struct tm6000_core *dev)
399 struct tm6000_IR *ir = dev->ir;
404 __tm6000_ir_int_stop(ir->rc);
407 int tm6000_ir_init(struct tm6000_core *dev)
409 struct tm6000_IR *ir;
417 if (!dev->caps.has_remote)
423 ir = kzalloc(sizeof(*ir), GFP_ATOMIC);
424 rc = rc_allocate_device(RC_DRIVER_SCANCODE);
428 dprintk(2, "%s\n", __func__);
430 /* record handles to ourself */
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;
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);
446 rc->open = tm6000_ir_start;
447 rc->close = tm6000_ir_stop;
449 INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key);
452 snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
455 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
456 strlcat(ir->phys, "/input0", sizeof(ir->phys));
458 rc_proto = RC_PROTO_BIT_UNKNOWN;
459 tm6000_ir_change_protocol(rc, &rc_proto);
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;
472 err = rc_register_device(rc);
485 int tm6000_ir_fini(struct tm6000_core *dev)
487 struct tm6000_IR *ir = dev->ir;
489 /* skip detach on non attached board */
494 dprintk(2, "%s\n",__func__);
497 __tm6000_ir_int_stop(ir->rc);
499 tm6000_ir_stop(ir->rc);
501 /* Turn off the led */
502 tm6000_flash_led(dev, 0);
505 rc_unregister_device(ir->rc);