[media] rc-core: add separate defines for protocol bitmaps and numbers
[deliverable/linux.git] / drivers / media / usb / tm6000 / tm6000-input.c
CommitLineData
d064f960 1/*
d0058645
RP
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 * 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.
d064f960
SR
18 */
19
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/delay.h>
23
24#include <linux/input.h>
25#include <linux/usb.h>
26
6bda9644 27#include <media/rc-core.h>
d064f960
SR
28
29#include "tm6000.h"
30#include "tm6000-regs.h"
31
32static unsigned int ir_debug;
33module_param(ir_debug, int, 0644);
4a83b011 34MODULE_PARM_DESC(ir_debug, "debug message level");
d064f960
SR
35
36static unsigned int enable_ir = 1;
37module_param(enable_ir, int, 0644);
401ad278 38MODULE_PARM_DESC(enable_ir, "enable ir (default is enable)");
d064f960 39
4a83b011
MCC
40static unsigned int ir_clock_mhz = 12;
41module_param(ir_clock_mhz, int, 0644);
42MODULE_PARM_DESC(enable_ir, "ir clock, in MHz");
43
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 */
641d2116 46
d064f960
SR
47#undef dprintk
48
4a83b011
MCC
49#define dprintk(level, fmt, arg...) do {\
50 if (ir_debug >= level) \
d064f960 51 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
4a83b011 52 } while (0)
d064f960
SR
53
54struct tm6000_ir_poll_result {
1b376dac 55 u16 rc_data;
d064f960
SR
56};
57
58struct tm6000_IR {
59 struct tm6000_core *dev;
d8b4b582 60 struct rc_dev *rc;
d064f960
SR
61 char name[32];
62 char phys[32];
63
64 /* poll expernal decoder */
65 int polling;
66 struct delayed_work work;
67 u8 wait:1;
4a83b011
MCC
68 u8 pwled:2;
69 u8 submit_urb:1;
641d2116 70 u16 key_addr;
d064f960 71 struct urb *int_urb;
d064f960
SR
72
73 /* IR device properties */
52b66144 74 u64 rc_type;
d064f960
SR
75};
76
d064f960
SR
77void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
78{
79 struct tm6000_IR *ir = dev->ir;
80
81 if (!dev->ir)
82 return;
83
4a83b011
MCC
84 dprintk(2, "%s: %i\n",__func__, ir->wait);
85
d064f960
SR
86 if (state)
87 ir->wait = 1;
88 else
89 ir->wait = 0;
90}
91
d064f960
SR
92static int tm6000_ir_config(struct tm6000_IR *ir)
93{
94 struct tm6000_core *dev = ir->dev;
4a83b011
MCC
95 u32 pulse = 0, leader = 0;
96
97 dprintk(2, "%s\n",__func__);
98
99 /*
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
109 */
d064f960 110
641d2116 111 switch (ir->rc_type) {
c003ab1b 112 case RC_BIT_NEC:
4a83b011
MCC
113 leader = 900; /* ms */
114 pulse = 700; /* ms - the actual value would be 562 */
641d2116
DB
115 break;
116 default:
c003ab1b 117 case RC_BIT_RC5:
4a83b011
MCC
118 leader = 900; /* ms - from the NEC decoding */
119 pulse = 1780; /* ms - The actual value would be 1776 */
641d2116 120 break;
d064f960 121 }
641d2116 122
4a83b011
MCC
123 pulse = ir_clock_mhz * pulse;
124 leader = ir_clock_mhz * leader;
c003ab1b 125 if (ir->rc_type == RC_BIT_NEC)
4a83b011
MCC
126 leader = leader | 0x8000;
127
128 dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n",
129 __func__,
c003ab1b 130 (ir->rc_type == RC_BIT_NEC) ? "NEC" : "RC-5",
4a83b011
MCC
131 ir_clock_mhz, leader, pulse);
132
133 /* Remote WAKEUP = enable, normal mode, from IR decoder output */
134 tm6000_set_reg(dev, TM6010_REQ07_RE5_REMOTE_WAKEUP, 0xfe);
135
136 /* Enable IR reception on non-busrt mode */
137 tm6000_set_reg(dev, TM6010_REQ07_RD8_IR, 0x2f);
138
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);
143
144 tm6000_set_reg(dev, TM6010_REQ07_RDC_IR_LEADER1, leader >> 8);
145 tm6000_set_reg(dev, TM6010_REQ07_RDD_IR_LEADER0, leader);
146
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);
149
150 if (!ir->polling)
151 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
152 else
153 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
154 msleep(10);
155
156 /* Shows that IR is working via the LED */
157 tm6000_flash_led(dev, 0);
158 msleep(100);
159 tm6000_flash_led(dev, 1);
160 ir->pwled = 1;
161
d064f960
SR
162 return 0;
163}
164
165static void tm6000_ir_urb_received(struct urb *urb)
166{
167 struct tm6000_core *dev = urb->context;
168 struct tm6000_IR *ir = dev->ir;
4a83b011
MCC
169 struct tm6000_ir_poll_result poll_result;
170 char *buf;
02c71055 171
4a83b011
MCC
172 dprintk(2, "%s\n",__func__);
173 if (urb->status < 0 || urb->actual_length <= 0) {
174 printk(KERN_INFO "tm6000: IR URB failure: status: %i, length %i\n",
175 urb->status, urb->actual_length);
176 ir->submit_urb = 1;
177 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
178 return;
179 }
180 buf = urb->transfer_buffer;
d064f960 181
4a83b011
MCC
182 if (ir_debug)
183 print_hex_dump(KERN_DEBUG, "tm6000: IR data: ",
184 DUMP_PREFIX_OFFSET,16, 1,
185 buf, urb->actual_length, false);
d064f960 186
4a83b011
MCC
187 poll_result.rc_data = buf[0];
188 if (urb->actual_length > 1)
189 poll_result.rc_data |= buf[1] << 8;
190
191 dprintk(1, "%s, scancode: 0x%04x\n",__func__, poll_result.rc_data);
192 rc_keydown(ir->rc, poll_result.rc_data, 0);
d064f960 193
8173090a 194 usb_submit_urb(urb, GFP_ATOMIC);
4a83b011
MCC
195 /*
196 * Flash the led. We can't do it here, as it is running on IRQ context.
197 * So, use the scheduler to do it, in a few ms.
198 */
199 ir->pwled = 2;
200 schedule_delayed_work(&ir->work, msecs_to_jiffies(10));
d064f960
SR
201}
202
4a83b011 203static void tm6000_ir_handle_key(struct work_struct *work)
d064f960 204{
4a83b011 205 struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
d064f960 206 struct tm6000_core *dev = ir->dev;
4a83b011 207 struct tm6000_ir_poll_result poll_result;
d064f960
SR
208 int rc;
209 u8 buf[2];
210
4a83b011
MCC
211 if (ir->wait)
212 return;
1b376dac 213
4a83b011 214 dprintk(3, "%s\n",__func__);
1b376dac 215
4a83b011
MCC
216 rc = tm6000_read_write_usb(dev, USB_DIR_IN |
217 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
218 REQ_02_GET_IR_CODE, 0, 0, buf, 2);
219 if (rc < 0)
220 return;
d064f960 221
4a83b011
MCC
222 if (rc > 1)
223 poll_result.rc_data = buf[0] | buf[1] << 8;
224 else
225 poll_result.rc_data = buf[0];
02c71055 226
4a83b011
MCC
227 /* Check if something was read */
228 if ((poll_result.rc_data & 0xff) == 0xff) {
229 if (!ir->pwled) {
230 tm6000_flash_led(dev, 1);
231 ir->pwled = 1;
1b376dac 232 }
4a83b011 233 return;
d064f960 234 }
4a83b011
MCC
235
236 dprintk(1, "%s, scancode: 0x%04x\n",__func__, poll_result.rc_data);
237 rc_keydown(ir->rc, poll_result.rc_data, 0);
238 tm6000_flash_led(dev, 0);
239 ir->pwled = 0;
240
241 /* Re-schedule polling */
242 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
d064f960
SR
243}
244
4a83b011 245static void tm6000_ir_int_work(struct work_struct *work)
d064f960 246{
4a83b011 247 struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
641d2116 248 struct tm6000_core *dev = ir->dev;
4a83b011 249 int rc;
d064f960 250
4a83b011
MCC
251 dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb,
252 ir->pwled);
d064f960 253
4a83b011
MCC
254 if (ir->submit_urb) {
255 dprintk(3, "Resubmit urb\n");
256 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
257
258 rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC);
259 if (rc < 0) {
260 printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n",
261 rc);
262 /* Retry in 100 ms */
263 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
264 return;
265 }
266 ir->submit_urb = 0;
641d2116
DB
267 }
268
4a83b011
MCC
269 /* Led is enabled only if USB submit doesn't fail */
270 if (ir->pwled == 2) {
641d2116 271 tm6000_flash_led(dev, 0);
4a83b011
MCC
272 ir->pwled = 0;
273 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_INT_LED_DELAY));
274 } else if (!ir->pwled) {
275 tm6000_flash_led(dev, 1);
276 ir->pwled = 1;
d064f960 277 }
d064f960
SR
278}
279
d8b4b582 280static int tm6000_ir_start(struct rc_dev *rc)
d064f960 281{
d8b4b582 282 struct tm6000_IR *ir = rc->priv;
d064f960 283
4a83b011
MCC
284 dprintk(2, "%s\n",__func__);
285
d064f960
SR
286 schedule_delayed_work(&ir->work, 0);
287
288 return 0;
289}
290
d8b4b582 291static void tm6000_ir_stop(struct rc_dev *rc)
d064f960 292{
d8b4b582 293 struct tm6000_IR *ir = rc->priv;
d064f960 294
4a83b011
MCC
295 dprintk(2, "%s\n",__func__);
296
d064f960
SR
297 cancel_delayed_work_sync(&ir->work);
298}
299
c003ab1b 300static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 *rc_type)
d064f960 301{
d8b4b582 302 struct tm6000_IR *ir = rc->priv;
d064f960 303
641d2116
DB
304 if (!ir)
305 return 0;
306
4a83b011
MCC
307 dprintk(2, "%s\n",__func__);
308
c003ab1b 309 if ((rc->rc_map.scan) && (*rc_type == RC_BIT_NEC))
641d2116
DB
310 ir->key_addr = ((rc->rc_map.scan[0].scancode >> 8) & 0xffff);
311
c003ab1b 312 ir->rc_type = *rc_type;
d064f960
SR
313
314 tm6000_ir_config(ir);
315 /* TODO */
316 return 0;
317}
318
4a83b011 319static int __tm6000_ir_int_start(struct rc_dev *rc)
641d2116 320{
4a83b011 321 struct tm6000_IR *ir = rc->priv;
e839776f 322 struct tm6000_core *dev;
641d2116
DB
323 int pipe, size;
324 int err = -ENOMEM;
325
641d2116
DB
326 if (!ir)
327 return -ENODEV;
e839776f 328 dev = ir->dev;
641d2116 329
4a83b011
MCC
330 dprintk(2, "%s\n",__func__);
331
332 ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC);
2591418b
DC
333 if (!ir->int_urb)
334 return -ENOMEM;
641d2116
DB
335
336 pipe = usb_rcvintpipe(dev->udev,
337 dev->int_in.endp->desc.bEndpointAddress
338 & USB_ENDPOINT_NUMBER_MASK);
339
340 size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
4a83b011 341 dprintk(1, "IR max size: %d\n", size);
641d2116 342
4a83b011 343 ir->int_urb->transfer_buffer = kzalloc(size, GFP_ATOMIC);
641d2116
DB
344 if (ir->int_urb->transfer_buffer == NULL) {
345 usb_free_urb(ir->int_urb);
346 return err;
347 }
4a83b011
MCC
348 dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval);
349
641d2116
DB
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);
4a83b011
MCC
354
355 ir->submit_urb = 1;
356 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
641d2116
DB
357
358 return 0;
359}
360
4a83b011 361static void __tm6000_ir_int_stop(struct rc_dev *rc)
641d2116 362{
4a83b011 363 struct tm6000_IR *ir = rc->priv;
641d2116 364
4a83b011 365 if (!ir || !ir->int_urb)
641d2116
DB
366 return;
367
4a83b011
MCC
368 dprintk(2, "%s\n",__func__);
369
641d2116
DB
370 usb_kill_urb(ir->int_urb);
371 kfree(ir->int_urb->transfer_buffer);
372 usb_free_urb(ir->int_urb);
373 ir->int_urb = NULL;
4a83b011
MCC
374}
375
376int tm6000_ir_int_start(struct tm6000_core *dev)
377{
378 struct tm6000_IR *ir = dev->ir;
379
2a887d27 380 if (!ir)
56a8b9f1 381 return 0;
2a887d27 382
4a83b011
MCC
383 return __tm6000_ir_int_start(ir->rc);
384}
385
386void tm6000_ir_int_stop(struct tm6000_core *dev)
387{
388 struct tm6000_IR *ir = dev->ir;
389
2a887d27
MCC
390 if (!ir || !ir->rc)
391 return;
392
4a83b011 393 __tm6000_ir_int_stop(ir->rc);
641d2116
DB
394}
395
d064f960
SR
396int tm6000_ir_init(struct tm6000_core *dev)
397{
398 struct tm6000_IR *ir;
d8b4b582 399 struct rc_dev *rc;
d064f960 400 int err = -ENOMEM;
c003ab1b 401 u64 rc_type;
d064f960
SR
402
403 if (!enable_ir)
404 return -ENODEV;
405
406 if (!dev->caps.has_remote)
407 return 0;
408
409 if (!dev->ir_codes)
410 return 0;
411
4a83b011 412 ir = kzalloc(sizeof(*ir), GFP_ATOMIC);
d8b4b582 413 rc = rc_allocate_device();
b01627c6 414 if (!ir || !rc)
d8b4b582 415 goto out;
d064f960 416
b16ec6f7
DC
417 dprintk(2, "%s\n", __func__);
418
d064f960
SR
419 /* record handles to ourself */
420 ir->dev = dev;
421 dev->ir = ir;
d8b4b582 422 ir->rc = rc;
d064f960 423
4a83b011 424 /* input setup */
c003ab1b 425 rc->allowed_protos = RC_BIT_RC5 | RC_BIT_NEC;
9f711a80
MCC
426 /* Neded, in order to support NEC remotes with 24 or 32 bits */
427 rc->scanmask = 0xffff;
d8b4b582
DH
428 rc->priv = ir;
429 rc->change_protocol = tm6000_ir_change_protocol;
4be9c8fb 430 if (dev->int_in.endp) {
4a83b011
MCC
431 rc->open = __tm6000_ir_int_start;
432 rc->close = __tm6000_ir_int_stop;
433 INIT_DELAYED_WORK(&ir->work, tm6000_ir_int_work);
434 } else {
435 rc->open = tm6000_ir_start;
436 rc->close = tm6000_ir_stop;
437 ir->polling = 50;
438 INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key);
439 }
d8b4b582 440 rc->driver_type = RC_DRIVER_SCANCODE;
d064f960 441
d064f960
SR
442 snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
443 dev->name);
444
445 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
446 strlcat(ir->phys, "/input0", sizeof(ir->phys));
447
c003ab1b
DH
448 rc_type = RC_BIT_UNKNOWN;
449 tm6000_ir_change_protocol(rc, &rc_type);
d064f960 450
d8b4b582
DH
451 rc->input_name = ir->name;
452 rc->input_phys = ir->phys;
453 rc->input_id.bustype = BUS_USB;
454 rc->input_id.version = 1;
455 rc->input_id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
456 rc->input_id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
457 rc->map_name = dev->ir_codes;
458 rc->driver_name = "tm6000";
459 rc->dev.parent = &dev->udev->dev;
d064f960 460
d064f960 461 /* ir register */
d8b4b582 462 err = rc_register_device(rc);
d064f960 463 if (err)
d8b4b582 464 goto out;
d064f960
SR
465
466 return 0;
467
d8b4b582 468out:
d064f960 469 dev->ir = NULL;
d8b4b582 470 rc_free_device(rc);
d064f960
SR
471 kfree(ir);
472 return err;
473}
474
475int tm6000_ir_fini(struct tm6000_core *dev)
476{
477 struct tm6000_IR *ir = dev->ir;
478
479 /* skip detach on non attached board */
480
481 if (!ir)
482 return 0;
483
4a83b011
MCC
484 dprintk(2, "%s\n",__func__);
485
4a83b011
MCC
486 if (!ir->polling)
487 __tm6000_ir_int_stop(ir->rc);
488
489 tm6000_ir_stop(ir->rc);
490
491 /* Turn off the led */
492 tm6000_flash_led(dev, 0);
493 ir->pwled = 0;
494
c6593959 495 rc_unregister_device(ir->rc);
d064f960 496
d064f960
SR
497 kfree(ir);
498 dev->ir = NULL;
499
500 return 0;
501}
This page took 0.214831 seconds and 5 git commands to generate.