]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/usb/tm6000/tm6000-input.c
[media] tm6000: fix typo in parameter description
[karo-tx-linux.git] / drivers / media / usb / tm6000 / tm6000-input.c
1 /*
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
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19
20 #include <linux/input.h>
21 #include <linux/usb.h>
22
23 #include <media/rc-core.h>
24
25 #include "tm6000.h"
26 #include "tm6000-regs.h"
27
28 static unsigned int ir_debug;
29 module_param(ir_debug, int, 0644);
30 MODULE_PARM_DESC(ir_debug, "debug message level");
31
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)");
35
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");
39
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 */
42
43 #undef dprintk
44
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)
49
50 struct tm6000_ir_poll_result {
51         u16 rc_data;
52 };
53
54 struct tm6000_IR {
55         struct tm6000_core      *dev;
56         struct rc_dev           *rc;
57         char                    name[32];
58         char                    phys[32];
59
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         u16                     key_addr;
67         struct urb              *int_urb;
68
69         /* IR device properties */
70         u64                     rc_type;
71 };
72
73 void tm6000_ir_wait(struct tm6000_core *dev, u8 state)
74 {
75         struct tm6000_IR *ir = dev->ir;
76
77         if (!dev->ir)
78                 return;
79
80         dprintk(2, "%s: %i\n",__func__, ir->wait);
81
82         if (state)
83                 ir->wait = 1;
84         else
85                 ir->wait = 0;
86 }
87
88 static int tm6000_ir_config(struct tm6000_IR *ir)
89 {
90         struct tm6000_core *dev = ir->dev;
91         u32 pulse = 0, leader = 0;
92
93         dprintk(2, "%s\n",__func__);
94
95         /*
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
105          */
106
107         switch (ir->rc_type) {
108         case RC_BIT_NEC:
109                 leader = 900;   /* ms */
110                 pulse  = 700;   /* ms - the actual value would be 562 */
111                 break;
112         default:
113         case RC_BIT_RC5:
114                 leader = 900;   /* ms - from the NEC decoding */
115                 pulse  = 1780;  /* ms - The actual value would be 1776 */
116                 break;
117         }
118
119         pulse = ir_clock_mhz * pulse;
120         leader = ir_clock_mhz * leader;
121         if (ir->rc_type == RC_BIT_NEC)
122                 leader = leader | 0x8000;
123
124         dprintk(2, "%s: %s, %d MHz, leader = 0x%04x, pulse = 0x%06x \n",
125                 __func__,
126                 (ir->rc_type == RC_BIT_NEC) ? "NEC" : "RC-5",
127                 ir_clock_mhz, leader, pulse);
128
129         /* Remote WAKEUP = enable, normal mode, from IR decoder output */
130         tm6000_set_reg(dev, TM6010_REQ07_RE5_REMOTE_WAKEUP, 0xfe);
131
132         /* Enable IR reception on non-busrt mode */
133         tm6000_set_reg(dev, TM6010_REQ07_RD8_IR, 0x2f);
134
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);
139
140         tm6000_set_reg(dev, TM6010_REQ07_RDC_IR_LEADER1, leader >> 8);
141         tm6000_set_reg(dev, TM6010_REQ07_RDD_IR_LEADER0, leader);
142
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);
145
146         if (!ir->polling)
147                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
148         else
149                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 1);
150         msleep(10);
151
152         /* Shows that IR is working via the LED */
153         tm6000_flash_led(dev, 0);
154         msleep(100);
155         tm6000_flash_led(dev, 1);
156         ir->pwled = 1;
157
158         return 0;
159 }
160
161 static void tm6000_ir_keydown(struct tm6000_IR *ir,
162                               const char *buf, unsigned int len)
163 {
164         u8 device, command;
165         u32 scancode;
166         enum rc_type protocol;
167
168         if (len < 1)
169                 return;
170
171         command = buf[0];
172         device = (len > 1 ? buf[1] : 0x0);
173         switch (ir->rc_type) {
174         case RC_BIT_RC5:
175                 protocol = RC_TYPE_RC5;
176                 scancode = RC_SCANCODE_RC5(device, command);
177                 break;
178         case RC_BIT_NEC:
179                 protocol = RC_TYPE_NEC;
180                 scancode = RC_SCANCODE_NEC(device, command);
181                 break;
182         default:
183                 protocol = RC_TYPE_OTHER;
184                 scancode = RC_SCANCODE_OTHER(device << 8 | command);
185                 break;
186         }
187
188         dprintk(1, "%s, protocol: 0x%04x, scancode: 0x%08x\n",
189                 __func__, protocol, scancode);
190         rc_keydown(ir->rc, protocol, scancode, 0);
191 }
192
193 static void tm6000_ir_urb_received(struct urb *urb)
194 {
195         struct tm6000_core *dev = urb->context;
196         struct tm6000_IR *ir = dev->ir;
197         char *buf;
198
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);
203                 ir->submit_urb = 1;
204                 schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
205                 return;
206         }
207         buf = urb->transfer_buffer;
208
209         if (ir_debug)
210                 print_hex_dump(KERN_DEBUG, "tm6000: IR data: ",
211                                DUMP_PREFIX_OFFSET,16, 1,
212                                buf, urb->actual_length, false);
213
214         tm6000_ir_keydown(ir, urb->transfer_buffer, urb->actual_length);
215
216         usb_submit_urb(urb, GFP_ATOMIC);
217         /*
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.
220          */
221         ir->pwled = 2;
222         schedule_delayed_work(&ir->work, msecs_to_jiffies(10));
223 }
224
225 static void tm6000_ir_handle_key(struct work_struct *work)
226 {
227         struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
228         struct tm6000_core *dev = ir->dev;
229         int rc;
230         u8 buf[2];
231
232         if (ir->wait)
233                 return;
234
235         dprintk(3, "%s\n",__func__);
236
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);
240         if (rc < 0)
241                 return;
242
243         /* Check if something was read */
244         if ((buf[0] & 0xff) == 0xff) {
245                 if (!ir->pwled) {
246                         tm6000_flash_led(dev, 1);
247                         ir->pwled = 1;
248                 }
249                 return;
250         }
251
252         tm6000_ir_keydown(ir, buf, rc);
253         tm6000_flash_led(dev, 0);
254         ir->pwled = 0;
255
256         /* Re-schedule polling */
257         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
258 }
259
260 static void tm6000_ir_int_work(struct work_struct *work)
261 {
262         struct tm6000_IR *ir = container_of(work, struct tm6000_IR, work.work);
263         struct tm6000_core *dev = ir->dev;
264         int rc;
265
266         dprintk(3, "%s, submit_urb = %d, pwled = %d\n",__func__, ir->submit_urb,
267                 ir->pwled);
268
269         if (ir->submit_urb) {
270                 dprintk(3, "Resubmit urb\n");
271                 tm6000_set_reg(dev, REQ_04_EN_DISABLE_MCU_INT, 2, 0);
272
273                 rc = usb_submit_urb(ir->int_urb, GFP_ATOMIC);
274                 if (rc < 0) {
275                         printk(KERN_ERR "tm6000: Can't submit an IR interrupt. Error %i\n",
276                                rc);
277                         /* Retry in 100 ms */
278                         schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
279                         return;
280                 }
281                 ir->submit_urb = 0;
282         }
283
284         /* Led is enabled only if USB submit doesn't fail */
285         if (ir->pwled == 2) {
286                 tm6000_flash_led(dev, 0);
287                 ir->pwled = 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);
291                 ir->pwled = 1;
292         }
293 }
294
295 static int tm6000_ir_start(struct rc_dev *rc)
296 {
297         struct tm6000_IR *ir = rc->priv;
298
299         dprintk(2, "%s\n",__func__);
300
301         schedule_delayed_work(&ir->work, 0);
302
303         return 0;
304 }
305
306 static void tm6000_ir_stop(struct rc_dev *rc)
307 {
308         struct tm6000_IR *ir = rc->priv;
309
310         dprintk(2, "%s\n",__func__);
311
312         cancel_delayed_work_sync(&ir->work);
313 }
314
315 static int tm6000_ir_change_protocol(struct rc_dev *rc, u64 *rc_type)
316 {
317         struct tm6000_IR *ir = rc->priv;
318
319         if (!ir)
320                 return 0;
321
322         dprintk(2, "%s\n",__func__);
323
324         if ((rc->rc_map.scan) && (*rc_type == RC_BIT_NEC))
325                 ir->key_addr = ((rc->rc_map.scan[0].scancode >> 8) & 0xffff);
326
327         ir->rc_type = *rc_type;
328
329         tm6000_ir_config(ir);
330         /* TODO */
331         return 0;
332 }
333
334 static int __tm6000_ir_int_start(struct rc_dev *rc)
335 {
336         struct tm6000_IR *ir = rc->priv;
337         struct tm6000_core *dev;
338         int pipe, size;
339         int err = -ENOMEM;
340
341         if (!ir)
342                 return -ENODEV;
343         dev = ir->dev;
344
345         dprintk(2, "%s\n",__func__);
346
347         ir->int_urb = usb_alloc_urb(0, GFP_ATOMIC);
348         if (!ir->int_urb)
349                 return -ENOMEM;
350
351         pipe = usb_rcvintpipe(dev->udev,
352                 dev->int_in.endp->desc.bEndpointAddress
353                 & USB_ENDPOINT_NUMBER_MASK);
354
355         size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
356         dprintk(1, "IR max size: %d\n", size);
357
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);
361                 return err;
362         }
363         dprintk(1, "int interval: %d\n", dev->int_in.endp->desc.bInterval);
364
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);
369
370         ir->submit_urb = 1;
371         schedule_delayed_work(&ir->work, msecs_to_jiffies(URB_SUBMIT_DELAY));
372
373         return 0;
374 }
375
376 static void __tm6000_ir_int_stop(struct rc_dev *rc)
377 {
378         struct tm6000_IR *ir = rc->priv;
379
380         if (!ir || !ir->int_urb)
381                 return;
382
383         dprintk(2, "%s\n",__func__);
384
385         usb_kill_urb(ir->int_urb);
386         kfree(ir->int_urb->transfer_buffer);
387         usb_free_urb(ir->int_urb);
388         ir->int_urb = NULL;
389 }
390
391 int tm6000_ir_int_start(struct tm6000_core *dev)
392 {
393         struct tm6000_IR *ir = dev->ir;
394
395         if (!ir)
396                 return 0;
397
398         return __tm6000_ir_int_start(ir->rc);
399 }
400
401 void tm6000_ir_int_stop(struct tm6000_core *dev)
402 {
403         struct tm6000_IR *ir = dev->ir;
404
405         if (!ir || !ir->rc)
406                 return;
407
408         __tm6000_ir_int_stop(ir->rc);
409 }
410
411 int tm6000_ir_init(struct tm6000_core *dev)
412 {
413         struct tm6000_IR *ir;
414         struct rc_dev *rc;
415         int err = -ENOMEM;
416         u64 rc_type;
417
418         if (!enable_ir)
419                 return -ENODEV;
420
421         if (!dev->caps.has_remote)
422                 return 0;
423
424         if (!dev->ir_codes)
425                 return 0;
426
427         ir = kzalloc(sizeof(*ir), GFP_ATOMIC);
428         rc = rc_allocate_device(RC_DRIVER_SCANCODE);
429         if (!ir || !rc)
430                 goto out;
431
432         dprintk(2, "%s\n", __func__);
433
434         /* record handles to ourself */
435         ir->dev = dev;
436         dev->ir = ir;
437         ir->rc = rc;
438
439         /* input setup */
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;
443         rc->priv = ir;
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);
449         } else {
450                 rc->open  = tm6000_ir_start;
451                 rc->close = tm6000_ir_stop;
452                 ir->polling = 50;
453                 INIT_DELAYED_WORK(&ir->work, tm6000_ir_handle_key);
454         }
455
456         snprintf(ir->name, sizeof(ir->name), "tm5600/60x0 IR (%s)",
457                                                 dev->name);
458
459         usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
460         strlcat(ir->phys, "/input0", sizeof(ir->phys));
461
462         rc_type = RC_BIT_UNKNOWN;
463         tm6000_ir_change_protocol(rc, &rc_type);
464
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;
474
475         /* ir register */
476         err = rc_register_device(rc);
477         if (err)
478                 goto out;
479
480         return 0;
481
482 out:
483         dev->ir = NULL;
484         rc_free_device(rc);
485         kfree(ir);
486         return err;
487 }
488
489 int tm6000_ir_fini(struct tm6000_core *dev)
490 {
491         struct tm6000_IR *ir = dev->ir;
492
493         /* skip detach on non attached board */
494
495         if (!ir)
496                 return 0;
497
498         dprintk(2, "%s\n",__func__);
499
500         if (!ir->polling)
501                 __tm6000_ir_int_stop(ir->rc);
502
503         tm6000_ir_stop(ir->rc);
504
505         /* Turn off the led */
506         tm6000_flash_led(dev, 0);
507         ir->pwled = 0;
508
509         rc_unregister_device(ir->rc);
510
511         kfree(ir);
512         dev->ir = NULL;
513
514         return 0;
515 }