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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
24 #include <linux/input.h>
25 #include <linux/usb.h>
27 #include <media/rc-core.h>
30 #include "tm6000-regs.h"
32 static unsigned int ir_debug
;
33 module_param(ir_debug
, int, 0644);
34 MODULE_PARM_DESC(ir_debug
, "debug message level");
36 static unsigned int enable_ir
= 1;
37 module_param(enable_ir
, int, 0644);
38 MODULE_PARM_DESC(enable_ir
, "enable ir (default is enable)");
40 static unsigned int ir_clock_mhz
= 12;
41 module_param(ir_clock_mhz
, int, 0644);
42 MODULE_PARM_DESC(enable_ir
, "ir clock, in MHz");
44 #define URB_SUBMIT_DELAY 100 /* ms - Delay to submit an URB request on retrial and init */
45 #define URB_INT_LED_DELAY 100 /* ms - Delay to turn led on again on int mode */
49 #define dprintk(level, fmt, arg...) do {\
50 if (ir_debug >= level) \
51 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
54 struct tm6000_ir_poll_result
{
59 struct tm6000_core
*dev
;
64 /* poll expernal decoder */
66 struct delayed_work work
;
73 /* IR device properties */
77 void tm6000_ir_wait(struct tm6000_core
*dev
, u8 state
)
79 struct tm6000_IR
*ir
= dev
->ir
;
84 dprintk(2, "%s: %i\n",__func__
, ir
->wait
);
92 static int tm6000_ir_config(struct tm6000_IR
*ir
)
94 struct tm6000_core
*dev
= ir
->dev
;
95 u32 pulse
= 0, leader
= 0;
97 dprintk(2, "%s\n",__func__
);
100 * The IR decoder supports RC-5 or NEC, with a configurable timing.
101 * The timing configuration there is not that accurate, as it uses
102 * approximate values. The NEC spec mentions a 562.5 unit period,
103 * and RC-5 uses a 888.8 period.
104 * Currently, driver assumes a clock provided by a 12 MHz XTAL, but
105 * a modprobe parameter can adjust it.
106 * Adjustments are required for other timings.
107 * It seems that the 900ms timing for NEC is used to detect a RC-5
108 * IR, in order to discard such decoding
111 switch (ir
->rc_type
) {
113 leader
= 900; /* ms */
114 pulse
= 700; /* ms - the actual value would be 562 */
118 leader
= 900; /* ms - from the NEC decoding */
119 pulse
= 1780; /* ms - The actual value would be 1776 */
123 pulse
= ir_clock_mhz
* pulse
;
124 leader
= ir_clock_mhz
* leader
;
125 if (ir
->rc_type
== RC_TYPE_NEC
)
126 leader
= leader
| 0x8000;
128 dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n",
130 (ir
->rc_type
== RC_TYPE_NEC
) ? "NEC" : "RC-5",
131 ir_clock_mhz
, leader
, pulse
);
133 /* Remote WAKEUP = enable, normal mode, from IR decoder output */
134 tm6000_set_reg(dev
, TM6010_REQ07_RE5_REMOTE_WAKEUP
, 0xfe);
136 /* Enable IR reception on non-busrt mode */
137 tm6000_set_reg(dev
, TM6010_REQ07_RD8_IR
, 0x2f);
139 /* IR_WKUP_SEL = Low byte in decoded IR data */
140 tm6000_set_reg(dev
, TM6010_REQ07_RDA_IR_WAKEUP_SEL
, 0xff);
141 /* IR_WKU_ADD code */
142 tm6000_set_reg(dev
, TM6010_REQ07_RDB_IR_WAKEUP_ADD
, 0xff);
144 tm6000_set_reg(dev
, TM6010_REQ07_RDC_IR_LEADER1
, leader
>> 8);
145 tm6000_set_reg(dev
, TM6010_REQ07_RDD_IR_LEADER0
, leader
);
147 tm6000_set_reg(dev
, TM6010_REQ07_RDE_IR_PULSE_CNT1
, pulse
>> 8);
148 tm6000_set_reg(dev
, TM6010_REQ07_RDF_IR_PULSE_CNT0
, pulse
);
151 tm6000_set_reg(dev
, REQ_04_EN_DISABLE_MCU_INT
, 2, 0);
153 tm6000_set_reg(dev
, REQ_04_EN_DISABLE_MCU_INT
, 2, 1);
156 /* Shows that IR is working via the LED */
157 tm6000_flash_led(dev
, 0);
159 tm6000_flash_led(dev
, 1);
165 static void tm6000_ir_urb_received(struct urb
*urb
)
167 struct tm6000_core
*dev
= urb
->context
;
168 struct tm6000_IR
*ir
= dev
->ir
;
169 struct tm6000_ir_poll_result poll_result
;
173 dprintk(2, "%s\n",__func__
);
174 if (urb
->status
< 0 || urb
->actual_length
<= 0) {
175 printk(KERN_INFO
"tm6000: IR URB failure: status: %i, length %i\n",
176 urb
->status
, urb
->actual_length
);
178 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(URB_SUBMIT_DELAY
));
181 buf
= urb
->transfer_buffer
;
184 print_hex_dump(KERN_DEBUG
, "tm6000: IR data: ",
185 DUMP_PREFIX_OFFSET
,16, 1,
186 buf
, urb
->actual_length
, false);
188 poll_result
.rc_data
= buf
[0];
189 if (urb
->actual_length
> 1)
190 poll_result
.rc_data
|= buf
[1] << 8;
192 dprintk(1, "%s, scancode: 0x%04x\n",__func__
, poll_result
.rc_data
);
193 rc_keydown(ir
->rc
, poll_result
.rc_data
, 0);
195 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
197 * Flash the led. We can't do it here, as it is running on IRQ context.
198 * So, use the scheduler to do it, in a few ms.
201 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(10));
204 static void tm6000_ir_handle_key(struct work_struct
*work
)
206 struct tm6000_IR
*ir
= container_of(work
, struct tm6000_IR
, work
.work
);
207 struct tm6000_core
*dev
= ir
->dev
;
208 struct tm6000_ir_poll_result poll_result
;
215 dprintk(3, "%s\n",__func__
);
217 rc
= tm6000_read_write_usb(dev
, USB_DIR_IN
|
218 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
219 REQ_02_GET_IR_CODE
, 0, 0, buf
, 2);
224 poll_result
.rc_data
= buf
[0] | buf
[1] << 8;
226 poll_result
.rc_data
= buf
[0];
228 /* Check if something was read */
229 if ((poll_result
.rc_data
& 0xff) == 0xff) {
231 tm6000_flash_led(dev
, 1);
237 dprintk(1, "%s, scancode: 0x%04x\n",__func__
, poll_result
.rc_data
);
238 rc_keydown(ir
->rc
, poll_result
.rc_data
, 0);
239 tm6000_flash_led(dev
, 0);
242 /* Re-schedule polling */
243 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(ir
->polling
));
246 static void tm6000_ir_int_work(struct work_struct
*work
)
248 struct tm6000_IR
*ir
= container_of(work
, struct tm6000_IR
, work
.work
);
249 struct tm6000_core
*dev
= ir
->dev
;
252 dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__
, ir
->submit_urb
,
255 if (ir
->submit_urb
) {
256 dprintk(3, "Resubmit urb\n");
257 tm6000_set_reg(dev
, REQ_04_EN_DISABLE_MCU_INT
, 2, 0);
259 rc
= usb_submit_urb(ir
->int_urb
, GFP_ATOMIC
);
261 printk(KERN_ERR
"tm6000: Can't submit an IR interrupt. Error %i\n",
263 /* Retry in 100 ms */
264 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(URB_SUBMIT_DELAY
));
270 /* Led is enabled only if USB submit doesn't fail */
271 if (ir
->pwled
== 2) {
272 tm6000_flash_led(dev
, 0);
274 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(URB_INT_LED_DELAY
));
275 } else if (!ir
->pwled
) {
276 tm6000_flash_led(dev
, 1);
281 static int tm6000_ir_start(struct rc_dev
*rc
)
283 struct tm6000_IR
*ir
= rc
->priv
;
285 dprintk(2, "%s\n",__func__
);
287 schedule_delayed_work(&ir
->work
, 0);
292 static void tm6000_ir_stop(struct rc_dev
*rc
)
294 struct tm6000_IR
*ir
= rc
->priv
;
296 dprintk(2, "%s\n",__func__
);
298 cancel_delayed_work_sync(&ir
->work
);
301 static int tm6000_ir_change_protocol(struct rc_dev
*rc
, u64 rc_type
)
303 struct tm6000_IR
*ir
= rc
->priv
;
308 dprintk(2, "%s\n",__func__
);
310 if ((rc
->rc_map
.scan
) && (rc_type
== RC_TYPE_NEC
))
311 ir
->key_addr
= ((rc
->rc_map
.scan
[0].scancode
>> 8) & 0xffff);
313 ir
->rc_type
= rc_type
;
315 tm6000_ir_config(ir
);
320 static int __tm6000_ir_int_start(struct rc_dev
*rc
)
322 struct tm6000_IR
*ir
= rc
->priv
;
323 struct tm6000_core
*dev
= ir
->dev
;
330 dprintk(2, "%s\n",__func__
);
332 ir
->int_urb
= usb_alloc_urb(0, GFP_ATOMIC
);
336 pipe
= usb_rcvintpipe(dev
->udev
,
337 dev
->int_in
.endp
->desc
.bEndpointAddress
338 & USB_ENDPOINT_NUMBER_MASK
);
340 size
= usb_maxpacket(dev
->udev
, pipe
, usb_pipeout(pipe
));
341 dprintk(1, "IR max size: %d\n", size
);
343 ir
->int_urb
->transfer_buffer
= kzalloc(size
, GFP_ATOMIC
);
344 if (ir
->int_urb
->transfer_buffer
== NULL
) {
345 usb_free_urb(ir
->int_urb
);
348 dprintk(1, "int interval: %d\n", dev
->int_in
.endp
->desc
.bInterval
);
350 usb_fill_int_urb(ir
->int_urb
, dev
->udev
, pipe
,
351 ir
->int_urb
->transfer_buffer
, size
,
352 tm6000_ir_urb_received
, dev
,
353 dev
->int_in
.endp
->desc
.bInterval
);
356 schedule_delayed_work(&ir
->work
, msecs_to_jiffies(URB_SUBMIT_DELAY
));
361 static void __tm6000_ir_int_stop(struct rc_dev
*rc
)
363 struct tm6000_IR
*ir
= rc
->priv
;
365 if (!ir
|| !ir
->int_urb
)
368 dprintk(2, "%s\n",__func__
);
370 usb_kill_urb(ir
->int_urb
);
371 kfree(ir
->int_urb
->transfer_buffer
);
372 usb_free_urb(ir
->int_urb
);
376 int tm6000_ir_int_start(struct tm6000_core
*dev
)
378 struct tm6000_IR
*ir
= dev
->ir
;
383 return __tm6000_ir_int_start(ir
->rc
);
386 void tm6000_ir_int_stop(struct tm6000_core
*dev
)
388 struct tm6000_IR
*ir
= dev
->ir
;
393 __tm6000_ir_int_stop(ir
->rc
);
396 int tm6000_ir_init(struct tm6000_core
*dev
)
398 struct tm6000_IR
*ir
;
405 if (!dev
->caps
.has_remote
)
411 ir
= kzalloc(sizeof(*ir
), GFP_ATOMIC
);
412 rc
= rc_allocate_device();
416 dprintk(2, "%s\n", __func__
);
418 /* record handles to ourself */
424 rc
->allowed_protos
= RC_TYPE_RC5
| RC_TYPE_NEC
;
425 /* Neded, in order to support NEC remotes with 24 or 32 bits */
426 rc
->scanmask
= 0xffff;
428 rc
->change_protocol
= tm6000_ir_change_protocol
;
429 if (dev
->int_in
.endp
) {
430 rc
->open
= __tm6000_ir_int_start
;
431 rc
->close
= __tm6000_ir_int_stop
;
432 INIT_DELAYED_WORK(&ir
->work
, tm6000_ir_int_work
);
434 rc
->open
= tm6000_ir_start
;
435 rc
->close
= tm6000_ir_stop
;
437 INIT_DELAYED_WORK(&ir
->work
, tm6000_ir_handle_key
);
439 rc
->driver_type
= RC_DRIVER_SCANCODE
;
441 snprintf(ir
->name
, sizeof(ir
->name
), "tm5600/60x0 IR (%s)",
444 usb_make_path(dev
->udev
, ir
->phys
, sizeof(ir
->phys
));
445 strlcat(ir
->phys
, "/input0", sizeof(ir
->phys
));
447 tm6000_ir_change_protocol(rc
, RC_TYPE_UNKNOWN
);
449 rc
->input_name
= ir
->name
;
450 rc
->input_phys
= ir
->phys
;
451 rc
->input_id
.bustype
= BUS_USB
;
452 rc
->input_id
.version
= 1;
453 rc
->input_id
.vendor
= le16_to_cpu(dev
->udev
->descriptor
.idVendor
);
454 rc
->input_id
.product
= le16_to_cpu(dev
->udev
->descriptor
.idProduct
);
455 rc
->map_name
= dev
->ir_codes
;
456 rc
->driver_name
= "tm6000";
457 rc
->dev
.parent
= &dev
->udev
->dev
;
460 err
= rc_register_device(rc
);
473 int tm6000_ir_fini(struct tm6000_core
*dev
)
475 struct tm6000_IR
*ir
= dev
->ir
;
477 /* skip detach on non attached board */
482 dprintk(2, "%s\n",__func__
);
484 rc_unregister_device(ir
->rc
);
487 __tm6000_ir_int_stop(ir
->rc
);
489 tm6000_ir_stop(ir
->rc
);
491 /* Turn off the led */
492 tm6000_flash_led(dev
, 0);