perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / usb / tm6000 / tm6000-input.c
blob397990afe00bb05b9bc784852e88d508848397a2
1 /*
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>
25 #include "tm6000.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 */
43 #undef dprintk
45 #define dprintk(level, fmt, arg...) do {\
46 if (ir_debug >= level) \
47 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
48 } while (0)
50 struct tm6000_ir_poll_result {
51 u16 rc_data;
54 struct tm6000_IR {
55 struct tm6000_core *dev;
56 struct rc_dev *rc;
57 char name[32];
58 char phys[32];
60 /* poll expernal decoder */
61 int polling;
62 struct delayed_work work;
63 u8 wait:1;
64 u8 pwled:2;
65 u8 submit_urb:1;
66 struct urb *int_urb;
68 /* IR device properties */
69 u64 rc_proto;
72 void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
74 struct tm6000_IR *ir = dev->ir;
76 if (!dev->ir)
77 return;
79 dprintk(2, "%s: %i\n",__func__, ir->wait);
81 if (state)
82 ir->wait = 1;
83 else
84 ir->wait = 0;
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 */
110 break;
111 default:
112 case RC_PROTO_BIT_RC5:
113 leader = 900; /* ms - from the NEC decoding */
114 pulse = 1780; /* ms - The actual value would be 1776 */
115 break;
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",
124 __func__,
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);
145 if (!ir->polling)
146 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
147 else
148 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
149 msleep(10);
151 /* Shows that IR is working via the LED */
152 tm6000_flash_led(dev, 0);
153 msleep(100);
154 tm6000_flash_led(dev, 1);
155 ir->pwled = 1;
157 return 0;
160 static void tm6000_ir_keydown(struct tm6000_IR *ir,
161 const char *buf, unsigned int len)
163 u8 device, command;
164 u32 scancode;
165 enum rc_proto protocol;
167 if (len < 1)
168 return;
170 command = buf[0];
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);
176 break;
177 case RC_PROTO_BIT_NEC:
178 protocol = RC_PROTO_NEC;
179 scancode = RC_SCANCODE_NEC(device, command);
180 break;
181 default:
182 protocol = RC_PROTO_OTHER;
183 scancode = RC_SCANCODE_OTHER(device << 8 | command);
184 break;
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;
196 char *buf;
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);
202 ir->submit_urb = 1;
203 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
204 return;
206 buf = urb->transfer_buffer;
208 if (ir_debug)
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.
220 ir->pwled = 2;
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;
228 int rc;
229 u8 buf[2];
231 if (ir->wait)
232 return;
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);
239 if (rc < 0)
240 return;
242 /* Check if something was read */
243 if ((buf[0] & 0xff) == 0xff) {
244 if (!ir->pwled) {
245 tm6000_flash_led(dev, 1);
246 ir->pwled = 1;
248 return;
251 tm6000_ir_keydown(ir, buf, rc);
252 tm6000_flash_led(dev, 0);
253 ir->pwled = 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;
263 int rc;
265 dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb,
266 ir->pwled);
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);
273 if (rc < 0) {
274 printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n",
275 rc);
276 /* Retry in 100 ms */
277 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
278 return;
280 ir->submit_urb = 0;
283 /* Led is enabled only if USB submit doesn't fail */
284 if (ir->pwled == 2) {
285 tm6000_flash_led(dev, 0);
286 ir->pwled = 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);
290 ir->pwled = 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);
302 return 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;
318 if (!ir)
319 return 0;
321 dprintk(2, "%s\n",__func__);
323 ir->rc_proto = *rc_proto;
325 tm6000_ir_config(ir);
326 /* TODO */
327 return 0;
330 static int __tm6000_ir_int_start(struct rc_dev *rc)
332 struct tm6000_IR *ir = rc->priv;
333 struct tm6000_core *dev;
334 int pipe, size;
335 int err = -ENOMEM;
337 if (!ir)
338 return -ENODEV;
339 dev = ir->dev;
341 dprintk(2, "%s\n",__func__);
343 ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC);
344 if (!ir->int_urb)
345 return -ENOMEM;
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);
357 return err;
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);
366 ir->submit_urb = 1;
367 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
369 return 0;
372 static void __tm6000_ir_int_stop(struct rc_dev *rc)
374 struct tm6000_IR *ir = rc->priv;
376 if (!ir || !ir->int_urb)
377 return;
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);
384 ir->int_urb = NULL;
387 int tm6000_ir_int_start(struct tm6000_core *dev)
389 struct tm6000_IR *ir = dev->ir;
391 if (!ir)
392 return 0;
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;
401 if (!ir || !ir->rc)
402 return;
404 __tm6000_ir_int_stop(ir->rc);
407 int tm6000_ir_init(struct tm6000_core *dev)
409 struct tm6000_IR *ir;
410 struct rc_dev *rc;
411 int err = -ENOMEM;
412 u64 rc_proto;
414 if (!enable_ir)
415 return -ENODEV;
417 if (!dev->caps.has_remote)
418 return 0;
420 if (!dev->ir_codes)
421 return 0;
423 ir = kzalloc(sizeof(*ir), GFP_ATOMIC);
424 rc = rc_allocate_device(RC_DRIVER_SCANCODE);
425 if (!ir || !rc)
426 goto out;
428 dprintk(2, "%s\n", __func__);
430 /* record handles to ourself */
431 ir->dev = dev;
432 dev->ir = ir;
433 ir->rc = rc;
435 /* input setup */
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;
439 rc->priv = ir;
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);
445 } else {
446 rc->open = tm6000_ir_start;
447 rc->close = tm6000_ir_stop;
448 ir->polling = 50;
449 INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key);
452 snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
453 dev->name);
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;
471 /* ir register */
472 err = rc_register_device(rc);
473 if (err)
474 goto out;
476 return 0;
478 out:
479 dev->ir = NULL;
480 rc_free_device(rc);
481 kfree(ir);
482 return err;
485 int tm6000_ir_fini(struct tm6000_core *dev)
487 struct tm6000_IR *ir = dev->ir;
489 /* skip detach on non attached board */
491 if (!ir)
492 return 0;
494 dprintk(2, "%s\n",__func__);
496 if (!ir->polling)
497 __tm6000_ir_int_stop(ir->rc);
499 tm6000_ir_stop(ir->rc);
501 /* Turn off the led */
502 tm6000_flash_led(dev, 0);
503 ir->pwled = 0;
505 rc_unregister_device(ir->rc);
507 kfree(ir);
508 dev->ir = NULL;
510 return 0;