V4L/DVB (13641): Properly update the driver representation for the protocol
[safe/jmp/linux-2.6] / drivers / media / video / em28xx / em28xx-input.c
1 /*
2   handle em28xx IR remotes via linux kernel input layer.
3
4    Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5                       Markus Rechberger <mrechberger@gmail.com>
6                       Mauro Carvalho Chehab <mchehab@infradead.org>
7                       Sascha Sommer <saschasommer@freenet.de>
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/input.h>
29 #include <linux/usb.h>
30
31 #include "em28xx.h"
32
33 #define EM28XX_SNAPSHOT_KEY KEY_CAMERA
34 #define EM28XX_SBUTTON_QUERY_INTERVAL 500
35 #define EM28XX_R0C_USBSUSP_SNAPSHOT 0x20
36
37 static unsigned int ir_debug;
38 module_param(ir_debug, int, 0644);
39 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
40
41 #define i2cdprintk(fmt, arg...) \
42         if (ir_debug) { \
43                 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
44         }
45
46 #define dprintk(fmt, arg...) \
47         if (ir_debug) { \
48                 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
49         }
50
51 /**********************************************************
52  Polling structure used by em28xx IR's
53  **********************************************************/
54
55 struct em28xx_ir_poll_result {
56         unsigned int toggle_bit:1;
57         unsigned int read_count:7;
58         u8 rc_address;
59         u8 rc_data[4]; /* 1 byte on em2860/2880, 4 on em2874 */
60 };
61
62 struct em28xx_IR {
63         struct em28xx *dev;
64         struct input_dev *input;
65         struct ir_input_state ir;
66         char name[32];
67         char phys[32];
68
69         /* poll external decoder */
70         int polling;
71         struct delayed_work work;
72         unsigned int last_toggle:1;
73         unsigned int full_code:1;
74         unsigned int last_readcount;
75         unsigned int repeat_interval;
76
77         int  (*get_key)(struct em28xx_IR *, struct em28xx_ir_poll_result *);
78
79         /* IR device properties */
80
81         struct ir_dev_props props;
82 };
83
84 /**********************************************************
85  I2C IR based get keycodes - should be used with ir-kbd-i2c
86  **********************************************************/
87
88 int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
89 {
90         unsigned char b;
91
92         /* poll IR chip */
93         if (1 != i2c_master_recv(ir->c, &b, 1)) {
94                 i2cdprintk("read error\n");
95                 return -EIO;
96         }
97
98         /* it seems that 0xFE indicates that a button is still hold
99            down, while 0xff indicates that no button is hold
100            down. 0xfe sequences are sometimes interrupted by 0xFF */
101
102         i2cdprintk("key %02x\n", b);
103
104         if (b == 0xff)
105                 return 0;
106
107         if (b == 0xfe)
108                 /* keep old data */
109                 return 1;
110
111         *ir_key = b;
112         *ir_raw = b;
113         return 1;
114 }
115
116 int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
117 {
118         unsigned char buf[2];
119         u16 code;
120         int size;
121
122         /* poll IR chip */
123         size = i2c_master_recv(ir->c, buf, sizeof(buf));
124
125         if (size != 2)
126                 return -EIO;
127
128         /* Does eliminate repeated parity code */
129         if (buf[1] == 0xff)
130                 return 0;
131
132         ir->old = buf[1];
133
134         /*
135          * Rearranges bits to the right order.
136          * The bit order were determined experimentally by using
137          * The original Hauppauge Grey IR and another RC5 that uses addr=0x08
138          * The RC5 code has 14 bits, but we've experimentally determined
139          * the meaning for only 11 bits.
140          * So, the code translation is not complete. Yet, it is enough to
141          * work with the provided RC5 IR.
142          */
143         code =
144                  ((buf[0] & 0x01) ? 0x0020 : 0) | /*            0010 0000 */
145                  ((buf[0] & 0x02) ? 0x0010 : 0) | /*            0001 0000 */
146                  ((buf[0] & 0x04) ? 0x0008 : 0) | /*            0000 1000 */
147                  ((buf[0] & 0x08) ? 0x0004 : 0) | /*            0000 0100 */
148                  ((buf[0] & 0x10) ? 0x0002 : 0) | /*            0000 0010 */
149                  ((buf[0] & 0x20) ? 0x0001 : 0) | /*            0000 0001 */
150                  ((buf[1] & 0x08) ? 0x1000 : 0) | /* 0001 0000            */
151                  ((buf[1] & 0x10) ? 0x0800 : 0) | /* 0000 1000            */
152                  ((buf[1] & 0x20) ? 0x0400 : 0) | /* 0000 0100            */
153                  ((buf[1] & 0x40) ? 0x0200 : 0) | /* 0000 0010            */
154                  ((buf[1] & 0x80) ? 0x0100 : 0);  /* 0000 0001            */
155
156         i2cdprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x%02x)\n",
157                         code, buf[1], buf[0]);
158
159         /* return key */
160         *ir_key = code;
161         *ir_raw = code;
162         return 1;
163 }
164
165 int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
166                                      u32 *ir_raw)
167 {
168         unsigned char buf[3];
169
170         /* poll IR chip */
171
172         if (3 != i2c_master_recv(ir->c, buf, 3)) {
173                 i2cdprintk("read error\n");
174                 return -EIO;
175         }
176
177         i2cdprintk("key %02x\n", buf[2]&0x3f);
178         if (buf[0] != 0x00)
179                 return 0;
180
181         *ir_key = buf[2]&0x3f;
182         *ir_raw = buf[2]&0x3f;
183
184         return 1;
185 }
186
187 /**********************************************************
188  Poll based get keycode functions
189  **********************************************************/
190
191 /* This is for the em2860/em2880 */
192 static int default_polling_getkey(struct em28xx_IR *ir,
193                                   struct em28xx_ir_poll_result *poll_result)
194 {
195         struct em28xx *dev = ir->dev;
196         int rc;
197         u8 msg[3] = { 0, 0, 0 };
198
199         /* Read key toggle, brand, and key code
200            on registers 0x45, 0x46 and 0x47
201          */
202         rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
203                                           msg, sizeof(msg));
204         if (rc < 0)
205                 return rc;
206
207         /* Infrared toggle (Reg 0x45[7]) */
208         poll_result->toggle_bit = (msg[0] >> 7);
209
210         /* Infrared read count (Reg 0x45[6:0] */
211         poll_result->read_count = (msg[0] & 0x7f);
212
213         /* Remote Control Address (Reg 0x46) */
214         poll_result->rc_address = msg[1];
215
216         /* Remote Control Data (Reg 0x47) */
217         poll_result->rc_data[0] = msg[2];
218
219         return 0;
220 }
221
222 static int em2874_polling_getkey(struct em28xx_IR *ir,
223                                  struct em28xx_ir_poll_result *poll_result)
224 {
225         struct em28xx *dev = ir->dev;
226         int rc;
227         u8 msg[5] = { 0, 0, 0, 0, 0 };
228
229         /* Read key toggle, brand, and key code
230            on registers 0x51-55
231          */
232         rc = dev->em28xx_read_reg_req_len(dev, 0, EM2874_R51_IR,
233                                           msg, sizeof(msg));
234         if (rc < 0)
235                 return rc;
236
237         /* Infrared toggle (Reg 0x51[7]) */
238         poll_result->toggle_bit = (msg[0] >> 7);
239
240         /* Infrared read count (Reg 0x51[6:0] */
241         poll_result->read_count = (msg[0] & 0x7f);
242
243         /* Remote Control Address (Reg 0x52) */
244         poll_result->rc_address = msg[1];
245
246         /* Remote Control Data (Reg 0x53-55) */
247         poll_result->rc_data[0] = msg[2];
248         poll_result->rc_data[1] = msg[3];
249         poll_result->rc_data[2] = msg[4];
250
251         return 0;
252 }
253
254 /**********************************************************
255  Polling code for em28xx
256  **********************************************************/
257
258 static void em28xx_ir_handle_key(struct em28xx_IR *ir)
259 {
260         int result;
261         int do_sendkey = 0;
262         struct em28xx_ir_poll_result poll_result;
263
264         /* read the registers containing the IR status */
265         result = ir->get_key(ir, &poll_result);
266         if (result < 0) {
267                 dprintk("ir->get_key() failed %d\n", result);
268                 return;
269         }
270
271         dprintk("ir->get_key result tb=%02x rc=%02x lr=%02x data=%02x%02x\n",
272                 poll_result.toggle_bit, poll_result.read_count,
273                 ir->last_readcount, poll_result.rc_address,
274                 poll_result.rc_data[0]);
275
276         if (ir->dev->chip_id == CHIP_ID_EM2874) {
277                 /* The em2874 clears the readcount field every time the
278                    register is read.  The em2860/2880 datasheet says that it
279                    is supposed to clear the readcount, but it doesn't.  So with
280                    the em2874, we are looking for a non-zero read count as
281                    opposed to a readcount that is incrementing */
282                 ir->last_readcount = 0;
283         }
284
285         if (poll_result.read_count == 0) {
286                 /* The button has not been pressed since the last read */
287         } else if (ir->last_toggle != poll_result.toggle_bit) {
288                 /* A button has been pressed */
289                 dprintk("button has been pressed\n");
290                 ir->last_toggle = poll_result.toggle_bit;
291                 ir->repeat_interval = 0;
292                 do_sendkey = 1;
293         } else if (poll_result.toggle_bit == ir->last_toggle &&
294                    poll_result.read_count > 0 &&
295                    poll_result.read_count != ir->last_readcount) {
296                 /* The button is still being held down */
297                 dprintk("button being held down\n");
298
299                 /* Debouncer for first keypress */
300                 if (ir->repeat_interval++ > 9) {
301                         /* Start repeating after 1 second */
302                         do_sendkey = 1;
303                 }
304         }
305
306         if (do_sendkey) {
307                 dprintk("sending keypress\n");
308
309                 if (ir->full_code)
310                         ir_input_keydown(ir->input, &ir->ir,
311                                          poll_result.rc_address << 8 |
312                                          poll_result.rc_data[0]);
313                 else
314                         ir_input_keydown(ir->input, &ir->ir,
315                                          poll_result.rc_data[0]);
316
317                 ir_input_nokey(ir->input, &ir->ir);
318         }
319
320         ir->last_readcount = poll_result.read_count;
321         return;
322 }
323
324 static void em28xx_ir_work(struct work_struct *work)
325 {
326         struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work.work);
327
328         em28xx_ir_handle_key(ir);
329         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
330 }
331
332 static void em28xx_ir_start(struct em28xx_IR *ir)
333 {
334         INIT_DELAYED_WORK(&ir->work, em28xx_ir_work);
335         schedule_delayed_work(&ir->work, 0);
336 }
337
338 static void em28xx_ir_stop(struct em28xx_IR *ir)
339 {
340         cancel_delayed_work_sync(&ir->work);
341 }
342
343 int em28xx_ir_change_protocol(void *priv, enum ir_type ir_type)
344 {
345         int rc = 0;
346         struct em28xx_IR *ir = priv;
347         struct em28xx *dev = ir->dev;
348         u8 ir_config = EM2874_IR_RC5;
349
350         /* Adjust xclk based o IR table for RC5/NEC tables */
351
352         dev->board.ir_codes->ir_type = IR_TYPE_OTHER;
353         if (ir_type == IR_TYPE_RC5) {
354                 dev->board.xclk |= EM28XX_XCLK_IR_RC5_MODE;
355                 ir->full_code = 1;
356         } else if (ir_type == IR_TYPE_NEC) {
357                 dev->board.xclk &= ~EM28XX_XCLK_IR_RC5_MODE;
358                 ir_config = EM2874_IR_NEC;
359                 ir->full_code = 1;
360         } else
361                 rc = -EINVAL;
362
363         dev->board.ir_codes->ir_type = ir_type;
364
365         em28xx_write_reg_bits(dev, EM28XX_R0F_XCLK, dev->board.xclk,
366                               EM28XX_XCLK_IR_RC5_MODE);
367
368         /* Setup the proper handler based on the chip */
369         switch (dev->chip_id) {
370         case CHIP_ID_EM2860:
371         case CHIP_ID_EM2883:
372                 ir->get_key = default_polling_getkey;
373                 break;
374         case CHIP_ID_EM2874:
375                 ir->get_key = em2874_polling_getkey;
376                 em28xx_write_regs(dev, EM2874_R50_IR_CONFIG, &ir_config, 1);
377                 break;
378         default:
379                 printk("Unrecognized em28xx chip id: IR not supported\n");
380                 rc = -EINVAL;
381         }
382
383         return rc;
384 }
385
386 int em28xx_ir_init(struct em28xx *dev)
387 {
388         struct em28xx_IR *ir;
389         struct input_dev *input_dev;
390         int err = -ENOMEM;
391
392         if (dev->board.ir_codes == NULL) {
393                 /* No remote control support */
394                 return 0;
395         }
396
397         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
398         input_dev = input_allocate_device();
399         if (!ir || !input_dev)
400                 goto err_out_free;
401
402         /* record handles to ourself */
403         ir->dev = dev;
404         dev->ir = ir;
405
406         ir->input = input_dev;
407
408         /*
409          * em2874 supports more protocols. For now, let's just announce
410          * the two protocols that were already tested
411          */
412         ir->props.allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
413         ir->props.priv = ir;
414         ir->props.change_protocol = em28xx_ir_change_protocol;
415
416         /* This is how often we ask the chip for IR information */
417         ir->polling = 100; /* ms */
418
419         /* init input device */
420         snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)",
421                                                 dev->name);
422
423         usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
424         strlcat(ir->phys, "/input0", sizeof(ir->phys));
425
426         /* Set IR protocol */
427         em28xx_ir_change_protocol(ir, dev->board.ir_codes->ir_type);
428         err = ir_input_init(input_dev, &ir->ir, IR_TYPE_OTHER);
429         if (err < 0)
430                 goto err_out_free;
431
432         input_dev->name = ir->name;
433         input_dev->phys = ir->phys;
434         input_dev->id.bustype = BUS_USB;
435         input_dev->id.version = 1;
436         input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
437         input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
438
439         input_dev->dev.parent = &dev->udev->dev;
440
441
442         em28xx_ir_start(ir);
443
444         /* all done */
445         err = ir_input_register(ir->input, dev->board.ir_codes,
446                                 &ir->props);
447         if (err)
448                 goto err_out_stop;
449
450         return 0;
451  err_out_stop:
452         em28xx_ir_stop(ir);
453         dev->ir = NULL;
454  err_out_free:
455         kfree(ir);
456         return err;
457 }
458
459 int em28xx_ir_fini(struct em28xx *dev)
460 {
461         struct em28xx_IR *ir = dev->ir;
462
463         /* skip detach on non attached boards */
464         if (!ir)
465                 return 0;
466
467         em28xx_ir_stop(ir);
468         ir_input_unregister(ir->input);
469         kfree(ir);
470
471         /* done */
472         dev->ir = NULL;
473         return 0;
474 }
475
476 /**********************************************************
477  Handle Webcam snapshot button
478  **********************************************************/
479
480 static void em28xx_query_sbutton(struct work_struct *work)
481 {
482         /* Poll the register and see if the button is depressed */
483         struct em28xx *dev =
484                 container_of(work, struct em28xx, sbutton_query_work.work);
485         int ret;
486
487         ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
488
489         if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
490                 u8 cleared;
491                 /* Button is depressed, clear the register */
492                 cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
493                 em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
494
495                 /* Not emulate the keypress */
496                 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
497                                  1);
498                 /* Now unpress the key */
499                 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
500                                  0);
501         }
502
503         /* Schedule next poll */
504         schedule_delayed_work(&dev->sbutton_query_work,
505                               msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
506 }
507
508 void em28xx_register_snapshot_button(struct em28xx *dev)
509 {
510         struct input_dev *input_dev;
511         int err;
512
513         em28xx_info("Registering snapshot button...\n");
514         input_dev = input_allocate_device();
515         if (!input_dev) {
516                 em28xx_errdev("input_allocate_device failed\n");
517                 return;
518         }
519
520         usb_make_path(dev->udev, dev->snapshot_button_path,
521                       sizeof(dev->snapshot_button_path));
522         strlcat(dev->snapshot_button_path, "/sbutton",
523                 sizeof(dev->snapshot_button_path));
524         INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
525
526         input_dev->name = "em28xx snapshot button";
527         input_dev->phys = dev->snapshot_button_path;
528         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
529         set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
530         input_dev->keycodesize = 0;
531         input_dev->keycodemax = 0;
532         input_dev->id.bustype = BUS_USB;
533         input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
534         input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
535         input_dev->id.version = 1;
536         input_dev->dev.parent = &dev->udev->dev;
537
538         err = input_register_device(input_dev);
539         if (err) {
540                 em28xx_errdev("input_register_device failed\n");
541                 input_free_device(input_dev);
542                 return;
543         }
544
545         dev->sbutton_input_dev = input_dev;
546         schedule_delayed_work(&dev->sbutton_query_work,
547                               msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
548         return;
549
550 }
551
552 void em28xx_deregister_snapshot_button(struct em28xx *dev)
553 {
554         if (dev->sbutton_input_dev != NULL) {
555                 em28xx_info("Deregistering snapshot button\n");
556                 cancel_rearming_delayed_work(&dev->sbutton_query_work);
557                 input_unregister_device(dev->sbutton_input_dev);
558                 dev->sbutton_input_dev = NULL;
559         }
560         return;
561 }