2 handle em28xx IR remotes via linux kernel input layer.
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>
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.
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.
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
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>
33 #define EM28XX_SNAPSHOT_KEY KEY_CAMERA
34 #define EM28XX_SBUTTON_QUERY_INTERVAL 500
35 #define EM28XX_R0C_USBSUSP_SNAPSHOT 0x20
37 static unsigned int ir_debug;
38 module_param(ir_debug, int, 0644);
39 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
41 #define i2cdprintk(fmt, arg...) \
43 printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg); \
46 #define dprintk(fmt, arg...) \
48 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg); \
51 /**********************************************************
52 Polling structure used by em28xx IR's
53 **********************************************************/
57 struct input_dev *input;
58 struct ir_input_state ir;
62 /* poll external decoder */
64 struct work_struct work;
65 struct timer_list timer;
72 int (*get_key)(struct em28xx_IR *);
75 /**********************************************************
76 I2C IR based get keycodes - should be used with ir-kbd-i2c
77 **********************************************************/
79 int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
84 if (1 != i2c_master_recv(&ir->c, &b, 1)) {
85 i2cdprintk("read error\n");
89 /* it seems that 0xFE indicates that a button is still hold
90 down, while 0xff indicates that no button is hold
91 down. 0xfe sequences are sometimes interrupted by 0xFF */
93 i2cdprintk("key %02x\n", b);
107 int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
109 unsigned char buf[2];
113 if (2 != i2c_master_recv(&ir->c, buf, 2))
116 /* Does eliminate repeated parity code */
122 /* Rearranges bits to the right order */
123 code = ((buf[0]&0x01)<<5) | /* 0010 0000 */
124 ((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 */
130 i2cdprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x)\n",
139 int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key,
142 unsigned char buf[3];
146 if (3 != i2c_master_recv(&ir->c, buf, 3)) {
147 i2cdprintk("read error\n");
151 i2cdprintk("key %02x\n", buf[2]&0x3f);
155 *ir_key = buf[2]&0x3f;
156 *ir_raw = buf[2]&0x3f;
161 /**********************************************************
162 Poll based get keycode functions
163 **********************************************************/
165 static int default_polling_getkey(struct em28xx_IR *ir)
167 struct em28xx *dev = ir->dev;
169 u8 msg[4] = { 0, 0, 0, 0 };
171 /* Read key toggle, brand, and key code
172 on registers 0x45, 0x46 and 0x47
174 rc = dev->em28xx_read_reg_req_len(dev, 0, EM28XX_R45_IR,
179 return (int)(le32_to_cpu(*(u32 *)msg));
182 /**********************************************************
183 Polling code for em28xx
184 **********************************************************/
186 static void em28xx_ir_handle_key(struct em28xx_IR *ir)
191 /* read gpio value */
192 gpio = ir->get_key(ir);
196 if (gpio == ir->last_gpio)
199 ir->last_gpio = gpio;
202 data = ir_extract_bits(gpio, ir->mask_keycode);
203 dprintk("irq gpio=0x%x code=%d | poll%s%s\n",
205 (gpio & ir->mask_keydown) ? " down" : "",
206 (gpio & ir->mask_keyup) ? " up" : "");
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);
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);
220 ir_input_nokey(ir->input, &ir->ir);
221 } else if (ir->mask_repeat) {
222 int count = ir->mask_repeat & gpio;
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);
230 /* can't distinguish keydown/up :-/ */
231 ir_input_keydown(ir->input, &ir->ir, data, data);
232 ir_input_nokey(ir->input, &ir->ir);
236 static void ir_timer(unsigned long data)
238 struct em28xx_IR *ir = (struct em28xx_IR *)data;
240 schedule_work(&ir->work);
243 static void em28xx_ir_work(struct work_struct *work)
245 struct em28xx_IR *ir = container_of(work, struct em28xx_IR, work);
247 em28xx_ir_handle_key(ir);
248 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
251 void em28xx_ir_start(struct em28xx_IR *ir)
253 setup_timer(&ir->timer, ir_timer, (unsigned long)ir);
254 INIT_WORK(&ir->work, em28xx_ir_work);
255 schedule_work(&ir->work);
258 static void em28xx_ir_stop(struct em28xx_IR *ir)
260 del_timer_sync(&ir->timer);
261 flush_scheduled_work();
264 int em28xx_ir_init(struct em28xx *dev)
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;
272 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
273 input_dev = input_allocate_device();
274 if (!ir || !input_dev)
277 ir->input = input_dev;
280 ir->get_key = default_polling_getkey;
281 ir->polling = 50; /* ms */
283 /* detect & configure */
284 switch (dev->model) {
285 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;
289 ir->mask_repeat = 0x0000007f;
293 if (NULL == ir_codes) {
298 /* init input device */
299 snprintf(ir->name, sizeof(ir->name), "em28xx IR (%s)",
302 usb_make_path(dev->udev, ir->phys, sizeof(ir->phys));
303 strlcat(ir->phys, "/input0", sizeof(ir->phys));
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);
313 input_dev->dev.parent = &dev->udev->dev;
314 /* record handles to ourself */
318 /* Get the current key status, to avoid adding an
319 unexistent key code */
320 ir->last_gpio = ir->get_key(ir);
325 err = input_register_device(ir->input);
334 input_free_device(input_dev);
339 int em28xx_ir_fini(struct em28xx *dev)
341 struct em28xx_IR *ir = dev->ir;
343 /* skip detach on non attached boards */
348 input_unregister_device(ir->input);
356 /**********************************************************
357 Handle Webcam snapshot button
358 **********************************************************/
360 static void em28xx_query_sbutton(struct work_struct *work)
362 /* Poll the register and see if the button is depressed */
364 container_of(work, struct em28xx, sbutton_query_work.work);
367 ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP);
369 if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) {
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);
375 /* Not emulate the keypress */
376 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
378 /* Now unpress the key */
379 input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY,
383 /* Schedule next poll */
384 schedule_delayed_work(&dev->sbutton_query_work,
385 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
388 void em28xx_register_snapshot_button(struct em28xx *dev)
390 struct input_dev *input_dev;
393 em28xx_info("Registering snapshot button...\n");
394 input_dev = input_allocate_device();
396 em28xx_errdev("input_allocate_device failed\n");
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);
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;
418 err = input_register_device(input_dev);
420 em28xx_errdev("input_register_device failed\n");
421 input_free_device(input_dev);
425 dev->sbutton_input_dev = input_dev;
426 schedule_delayed_work(&dev->sbutton_query_work,
427 msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL));
432 void em28xx_deregister_snapshot_button(struct em28xx *dev)
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;