blob: 159b5c307f25013fe25a4ed55b6df7d67ee00a71 [file] [log] [blame]
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 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>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 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
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080022 */
23
24#include <linux/module.h>
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080025#include <linux/init.h>
26#include <linux/delay.h>
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080027#include <linux/interrupt.h>
28#include <linux/input.h>
29#include <linux/usb.h>
30
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080031#include "em28xx.h"
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080032
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -030033#define EM28XX_SNAPSHOT_KEY KEY_CAMERA
34#define EM28XX_SBUTTON_QUERY_INTERVAL 500
35#define EM28XX_R0C_USBSUSP_SNAPSHOT 0x20
36
Mauro Carvalho Chehabc8793b02008-01-13 15:42:17 -030037static unsigned int ir_debug;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080038module_param(ir_debug, int, 0644);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030039MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080040
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030041#define i2cdprintk(fmt, arg...) \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030042 if (ir_debug) { \
43 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg); \
44 }
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080045
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030046#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
55struct em28xx_IR {
56 struct em28xx *dev;
57 struct input_dev *input;
58 struct ir_input_state ir;
59 char name[32];
60 char phys[32];
61
62 /* poll external decoder */
63 int polling;
64 struct work_struct work;
65 struct timer_list timer;
66 u32 last_gpio;
67 u32 mask_keycode;
68 u32 mask_keydown;
69 u32 mask_keyup;
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -030070 u32 mask_repeat;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030071
72 int (*get_key)(struct em28xx_IR *);
73};
74
75/**********************************************************
76 I2C IR based get keycodes - should be used with ir-kbd-i2c
77 **********************************************************/
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080078
Mauro Carvalho Chehabc8793b02008-01-13 15:42:17 -030079int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080080{
81 unsigned char b;
82
83 /* poll IR chip */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030084 if (1 != i2c_master_recv(&ir->c, &b, 1)) {
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030085 i2cdprintk("read error\n");
Markus Rechbergere43f14a2005-11-08 21:37:35 -080086 return -EIO;
87 }
88
89 /* it seems that 0xFE indicates that a button is still hold
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080090 down, while 0xff indicates that no button is hold
91 down. 0xfe sequences are sometimes interrupted by 0xFF */
Markus Rechbergere43f14a2005-11-08 21:37:35 -080092
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -030093 i2cdprintk("key %02x\n", b);
Markus Rechbergere43f14a2005-11-08 21:37:35 -080094
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080095 if (b == 0xff)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080096 return 0;
97
Mauro Carvalho Chehab4f9c05a2005-11-08 21:37:36 -080098 if (b == 0xfe)
Markus Rechbergere43f14a2005-11-08 21:37:35 -080099 /* keep old data */
100 return 1;
101
102 *ir_key = b;
103 *ir_raw = b;
104 return 1;
105}
106
Mauro Carvalho Chehabc8793b02008-01-13 15:42:17 -0300107int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800108{
109 unsigned char buf[2];
110 unsigned char code;
111
112 /* poll IR chip */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300113 if (2 != i2c_master_recv(&ir->c, buf, 2))
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800114 return -EIO;
115
116 /* Does eliminate repeated parity code */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300117 if (buf[1] == 0xff)
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800118 return 0;
119
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300120 ir->old = buf[1];
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800121
122 /* Rearranges bits to the right order */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300123 code = ((buf[0]&0x01)<<5) | /* 0010 0000 */
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800124 ((buf[0]&0x02)<<3) | /* 0001 0000 */
125 ((buf[0]&0x04)<<1) | /* 0000 1000 */
126 ((buf[0]&0x08)>>1) | /* 0000 0100 */
127 ((buf[0]&0x10)>>3) | /* 0000 0010 */
128 ((buf[0]&0x20)>>5); /* 0000 0001 */
129
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300130 i2cdprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x)\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300131 code, buf[0]);
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -0800132
133 /* return key */
134 *ir_key = code;
135 *ir_raw = code;
136 return 1;
137}
138
Mauro Carvalho Chehabc8793b02008-01-13 15:42:17 -0300139int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
140 u32 *ir_raw)
Markus Rechberger366cc642006-01-15 21:04:27 -0200141{
142 unsigned char buf[3];
143
144 /* poll IR chip */
145
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300146 if (3 != i2c_master_recv(&ir->c, buf, 3)) {
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300147 i2cdprintk("read error\n");
Markus Rechberger366cc642006-01-15 21:04:27 -0200148 return -EIO;
149 }
150
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300151 i2cdprintk("key %02x\n", buf[2]&0x3f);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300152 if (buf[0] != 0x00)
Markus Rechberger366cc642006-01-15 21:04:27 -0200153 return 0;
Markus Rechberger366cc642006-01-15 21:04:27 -0200154
155 *ir_key = buf[2]&0x3f;
156 *ir_raw = buf[2]&0x3f;
157
158 return 1;
159}
160
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300161/**********************************************************
162 Poll based get keycode functions
163 **********************************************************/
164
165static int default_polling_getkey(struct em28xx_IR *ir)
166{
167 struct em28xx *dev = ir->dev;
168 int rc;
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300169 u8 msg[4] = { 0, 0, 0, 0 };
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300170
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300171 /* Read key toggle, brand, and key code
172 on registers 0x45, 0x46 and 0x47
173 */
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300174 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300175 msg, sizeof(msg));
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300176 if (rc < 0)
177 return rc;
178
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300179 return (int)(le32_to_cpu(*(u32 *)msg));
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300180}
181
182/**********************************************************
183 Polling code for em28xx
184 **********************************************************/
185
186static void em28xx_ir_handle_key(struct em28xx_IR *ir)
187{
188 int gpio;
189 u32 data;
190
191 /* read gpio value */
192 gpio = ir->get_key(ir);
193 if (gpio < 0)
194 return;
195
196 if (gpio == ir->last_gpio)
197 return;
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300198
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300199 ir->last_gpio = gpio;
200
201 /* extract data */
202 data = ir_extract_bits(gpio, ir->mask_keycode);
203 dprintk("irq gpio=0x%x code=%d | poll%s%s\n",
204 gpio, data,
205 (gpio & ir->mask_keydown) ? " down" : "",
206 (gpio & ir->mask_keyup) ? " up" : "");
207
208 /* Generate keyup/keydown events */
209 if (ir->mask_keydown) {
210 /* bit set on keydown */
211 if (gpio & ir->mask_keydown)
212 ir_input_keydown(ir->input, &ir->ir, data, data);
213 else
214 ir_input_nokey(ir->input, &ir->ir);
215 } else if (ir->mask_keyup) {
216 /* bit cleared on keydown */
217 if (!(gpio & ir->mask_keyup))
218 ir_input_keydown(ir->input, &ir->ir, data, data);
219 else
220 ir_input_nokey(ir->input, &ir->ir);
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300221 } else if (ir->mask_repeat) {
222 int count = ir->mask_repeat & gpio;
223
224 /* Avoid keyboard bouncing */
225 if ((count == 1) || (count >= 5)) {
226 ir_input_keydown(ir->input, &ir->ir, data, data);
227 ir_input_nokey(ir->input, &ir->ir);
228 }
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300229 } else {
230 /* can't distinguish keydown/up :-/ */
231 ir_input_keydown(ir->input, &ir->ir, data, data);
232 ir_input_nokey(ir->input, &ir->ir);
233 }
234}
235
236static void ir_timer(unsigned long data)
237{
238 struct em28xx_IR *ir = (struct em28xx_IR *)data;
239
240 schedule_work(&ir->work);
241}
242
243static void em28xx_ir_work(struct work_struct *work)
244{
245 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work);
246
247 em28xx_ir_handle_key(ir);
248 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
249}
250
251void em28xx_ir_start(struct em28xx_IR *ir)
252{
253 setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
254 INIT_WORK(&ir->work, em28xx_ir_work);
255 schedule_work(&ir->work);
256}
257
258static void em28xx_ir_stop(struct em28xx_IR *ir)
259{
260 del_timer_sync(&ir->timer);
261 flush_scheduled_work();
262}
263
264int em28xx_ir_init(struct em28xx *dev)
265{
266 struct em28xx_IR *ir;
267 struct input_dev *input_dev;
268 IR_KEYTAB_TYPE *ir_codes = NULL;
269 int ir_type = IR_TYPE_OTHER;
270 int err = -ENOMEM;
271
272 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
273 input_dev = input_allocate_device();
274 if (!ir || !input_dev)
275 goto err_out_free;
276
277 ir->input = input_dev;
278
279 /* */
280 ir->get_key = default_polling_getkey;
281 ir->polling = 50; /* ms */
282
283 /* detect & configure */
284 switch (dev->model) {
Mauro Carvalho Chehab91812fa2008-11-12 14:05:46 -0300285 case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
286 ir_type = IR_TYPE_OTHER;
287 ir_codes = ir_codes_hauppauge_new;
288 ir->mask_keycode = 0x007f0000;
Mauro Carvalho Chehab0a6b8a82008-11-12 18:46:01 -0300289 ir->mask_repeat = 0x0000007f;
Mauro Carvalho Chehab91812fa2008-11-12 14:05:46 -0300290 break;
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300291 }
292
293 if (NULL == ir_codes) {
294 err = -ENODEV;
295 goto err_out_free;
296 }
297
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300298 /* init input device */
299 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)",
300 dev->name);
301
302 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
303 strlcat(ir->phys, "/input0", sizeof(ir->phys));
304
305 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
306 input_dev->name = ir->name;
307 input_dev->phys = ir->phys;
308 input_dev->id.bustype = BUS_USB;
309 input_dev->id.version = 1;
310 input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
311 input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
312
313 input_dev->dev.parent = &dev->udev->dev;
314 /* record handles to ourself */
315 ir->dev = dev;
316 dev->ir = ir;
317
Mauro Carvalho Chehab91812fa2008-11-12 14:05:46 -0300318 /* Get the current key status, to avoid adding an
319 unexistent key code */
320 ir->last_gpio = ir->get_key(ir);
321
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -0300322 em28xx_ir_start(ir);
323
324 /* all done */
325 err = input_register_device(ir->input);
326 if (err)
327 goto err_out_stop;
328
329 return 0;
330 err_out_stop:
331 em28xx_ir_stop(ir);
332 dev->ir = NULL;
333 err_out_free:
334 input_free_device(input_dev);
335 kfree(ir);
336 return err;
337}
338
339int em28xx_ir_fini(struct em28xx *dev)
340{
341 struct em28xx_IR *ir = dev->ir;
342
343 /* skip detach on non attached boards */
344 if (!ir)
345 return 0;
346
347 em28xx_ir_stop(ir);
348 input_unregister_device(ir->input);
349 kfree(ir);
350
351 /* done */
352 dev->ir = NULL;
353 return 0;
354}
355
356/**********************************************************
357 Handle Webcam snapshot button
358 **********************************************************/
359
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -0300360static void em28xx_query_sbutton(struct work_struct *work)
361{
362 /* Poll the register and see if the button is depressed */
363 struct em28xx *dev =
364 container_of(work, struct em28xx, sbutton_query_work.work);
365 int ret;
366
367 ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
368
369 if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
370 u8 cleared;
371 /* Button is depressed, clear the register */
372 cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT;
373 em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1);
374
375 /* Not emulate the keypress */
376 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
377 1);
378 /* Now unpress the key */
379 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
380 0);
381 }
382
383 /* Schedule next poll */
384 schedule_delayed_work(&dev->sbutton_query_work,
385 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
386}
387
388void em28xx_register_snapshot_button(struct em28xx *dev)
389{
390 struct input_dev *input_dev;
391 int err;
392
393 em28xx_info("Registering snapshot button...\n");
394 input_dev = input_allocate_device();
395 if (!input_dev) {
396 em28xx_errdev("input_allocate_device failed\n");
397 return;
398 }
399
400 usb_make_path(dev->udev, dev->snapshot_button_path,
401 sizeof(dev->snapshot_button_path));
402 strlcat(dev->snapshot_button_path, "/sbutton",
403 sizeof(dev->snapshot_button_path));
404 INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton);
405
406 input_dev->name = "em28xx snapshot button";
407 input_dev->phys = dev->snapshot_button_path;
408 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
409 set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit);
410 input_dev->keycodesize = 0;
411 input_dev->keycodemax = 0;
412 input_dev->id.bustype = BUS_USB;
413 input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
414 input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct);
415 input_dev->id.version = 1;
416 input_dev->dev.parent = &dev->udev->dev;
417
418 err = input_register_device(input_dev);
419 if (err) {
420 em28xx_errdev("input_register_device failed\n");
421 input_free_device(input_dev);
422 return;
423 }
424
425 dev->sbutton_input_dev = input_dev;
426 schedule_delayed_work(&dev->sbutton_query_work,
427 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
428 return;
429
430}
431
432void em28xx_deregister_snapshot_button(struct em28xx *dev)
433{
434 if (dev->sbutton_input_dev != NULL) {
435 em28xx_info("Deregistering snapshot button\n");
436 cancel_rearming_delayed_work(&dev->sbutton_query_work);
437 input_unregister_device(dev->sbutton_input_dev);
438 dev->sbutton_input_dev = NULL;
439 }
440 return;
441}