Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 1 | /* |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 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> |
Mauro Carvalho Chehab | 2e7c6dc | 2006-04-03 07:53:40 -0300 | [diff] [blame] | 6 | Mauro Carvalho Chehab <mchehab@infradead.org> |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 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 |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 25 | #include <linux/init.h> |
| 26 | #include <linux/delay.h> |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/input.h> |
| 29 | #include <linux/usb.h> |
| 30 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 31 | #include "em28xx.h" |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 32 | |
Devin Heitmueller | a9fc52b | 2008-06-28 08:57:06 -0300 | [diff] [blame^] | 33 | #define EM28XX_SNAPSHOT_KEY KEY_CAMERA |
| 34 | #define EM28XX_SBUTTON_QUERY_INTERVAL 500 |
| 35 | #define EM28XX_R0C_USBSUSP_SNAPSHOT 0x20 |
| 36 | |
Mauro Carvalho Chehab | c8793b0 | 2008-01-13 15:42:17 -0300 | [diff] [blame] | 37 | static unsigned int ir_debug; |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 38 | module_param(ir_debug, int, 0644); |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 39 | MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]"); |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 40 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 41 | #define dprintk(fmt, arg...) \ |
| 42 | if (ir_debug) { \ |
| 43 | printk(KERN_DEBUG "%s/ir: " fmt, ir->c.name , ## arg); \ |
| 44 | } |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 45 | |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 46 | /* ----------------------------------------------------------------------- */ |
| 47 | |
Mauro Carvalho Chehab | c8793b0 | 2008-01-13 15:42:17 -0300 | [diff] [blame] | 48 | int em28xx_get_key_terratec(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) |
Markus Rechberger | e43f14a | 2005-11-08 21:37:35 -0800 | [diff] [blame] | 49 | { |
| 50 | unsigned char b; |
| 51 | |
| 52 | /* poll IR chip */ |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 53 | if (1 != i2c_master_recv(&ir->c, &b, 1)) { |
Markus Rechberger | e43f14a | 2005-11-08 21:37:35 -0800 | [diff] [blame] | 54 | dprintk("read error\n"); |
| 55 | return -EIO; |
| 56 | } |
| 57 | |
| 58 | /* it seems that 0xFE indicates that a button is still hold |
Mauro Carvalho Chehab | 4f9c05a | 2005-11-08 21:37:36 -0800 | [diff] [blame] | 59 | down, while 0xff indicates that no button is hold |
| 60 | down. 0xfe sequences are sometimes interrupted by 0xFF */ |
Markus Rechberger | e43f14a | 2005-11-08 21:37:35 -0800 | [diff] [blame] | 61 | |
| 62 | dprintk("key %02x\n", b); |
| 63 | |
Mauro Carvalho Chehab | 4f9c05a | 2005-11-08 21:37:36 -0800 | [diff] [blame] | 64 | if (b == 0xff) |
Markus Rechberger | e43f14a | 2005-11-08 21:37:35 -0800 | [diff] [blame] | 65 | return 0; |
| 66 | |
Mauro Carvalho Chehab | 4f9c05a | 2005-11-08 21:37:36 -0800 | [diff] [blame] | 67 | if (b == 0xfe) |
Markus Rechberger | e43f14a | 2005-11-08 21:37:35 -0800 | [diff] [blame] | 68 | /* keep old data */ |
| 69 | return 1; |
| 70 | |
| 71 | *ir_key = b; |
| 72 | *ir_raw = b; |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | |
Mauro Carvalho Chehab | c8793b0 | 2008-01-13 15:42:17 -0300 | [diff] [blame] | 77 | int em28xx_get_key_em_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw) |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 78 | { |
| 79 | unsigned char buf[2]; |
| 80 | unsigned char code; |
| 81 | |
| 82 | /* poll IR chip */ |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 83 | if (2 != i2c_master_recv(&ir->c, buf, 2)) |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 84 | return -EIO; |
| 85 | |
| 86 | /* Does eliminate repeated parity code */ |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 87 | if (buf[1] == 0xff) |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 88 | return 0; |
| 89 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 90 | ir->old = buf[1]; |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 91 | |
| 92 | /* Rearranges bits to the right order */ |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 93 | code = ((buf[0]&0x01)<<5) | /* 0010 0000 */ |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 94 | ((buf[0]&0x02)<<3) | /* 0001 0000 */ |
| 95 | ((buf[0]&0x04)<<1) | /* 0000 1000 */ |
| 96 | ((buf[0]&0x08)>>1) | /* 0000 0100 */ |
| 97 | ((buf[0]&0x10)>>3) | /* 0000 0010 */ |
| 98 | ((buf[0]&0x20)>>5); /* 0000 0001 */ |
| 99 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 100 | dprintk("ir hauppauge (em2840): code=0x%02x (rcv=0x%02x)\n", |
| 101 | code, buf[0]); |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 102 | |
| 103 | /* return key */ |
| 104 | *ir_key = code; |
| 105 | *ir_raw = code; |
| 106 | return 1; |
| 107 | } |
| 108 | |
Mauro Carvalho Chehab | c8793b0 | 2008-01-13 15:42:17 -0300 | [diff] [blame] | 109 | int em28xx_get_key_pinnacle_usb_grey(struct IR_i2c *ir, u32 *ir_key, |
| 110 | u32 *ir_raw) |
Markus Rechberger | 366cc64 | 2006-01-15 21:04:27 -0200 | [diff] [blame] | 111 | { |
| 112 | unsigned char buf[3]; |
| 113 | |
| 114 | /* poll IR chip */ |
| 115 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 116 | if (3 != i2c_master_recv(&ir->c, buf, 3)) { |
Markus Rechberger | 366cc64 | 2006-01-15 21:04:27 -0200 | [diff] [blame] | 117 | dprintk("read error\n"); |
| 118 | return -EIO; |
| 119 | } |
| 120 | |
| 121 | dprintk("key %02x\n", buf[2]&0x3f); |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 122 | if (buf[0] != 0x00) |
Markus Rechberger | 366cc64 | 2006-01-15 21:04:27 -0200 | [diff] [blame] | 123 | return 0; |
Markus Rechberger | 366cc64 | 2006-01-15 21:04:27 -0200 | [diff] [blame] | 124 | |
| 125 | *ir_key = buf[2]&0x3f; |
| 126 | *ir_raw = buf[2]&0x3f; |
| 127 | |
| 128 | return 1; |
| 129 | } |
| 130 | |
Devin Heitmueller | a9fc52b | 2008-06-28 08:57:06 -0300 | [diff] [blame^] | 131 | static void em28xx_query_sbutton(struct work_struct *work) |
| 132 | { |
| 133 | /* Poll the register and see if the button is depressed */ |
| 134 | struct em28xx *dev = |
| 135 | container_of(work, struct em28xx, sbutton_query_work.work); |
| 136 | int ret; |
| 137 | |
| 138 | ret = em28xx_read_reg(dev, EM28XX_R0C_USBSUSP); |
| 139 | |
| 140 | if (ret & EM28XX_R0C_USBSUSP_SNAPSHOT) { |
| 141 | u8 cleared; |
| 142 | /* Button is depressed, clear the register */ |
| 143 | cleared = ((u8) ret) & ~EM28XX_R0C_USBSUSP_SNAPSHOT; |
| 144 | em28xx_write_regs(dev, EM28XX_R0C_USBSUSP, &cleared, 1); |
| 145 | |
| 146 | /* Not emulate the keypress */ |
| 147 | input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY, |
| 148 | 1); |
| 149 | /* Now unpress the key */ |
| 150 | input_report_key(dev->sbutton_input_dev, EM28XX_SNAPSHOT_KEY, |
| 151 | 0); |
| 152 | } |
| 153 | |
| 154 | /* Schedule next poll */ |
| 155 | schedule_delayed_work(&dev->sbutton_query_work, |
| 156 | msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL)); |
| 157 | } |
| 158 | |
| 159 | void em28xx_register_snapshot_button(struct em28xx *dev) |
| 160 | { |
| 161 | struct input_dev *input_dev; |
| 162 | int err; |
| 163 | |
| 164 | em28xx_info("Registering snapshot button...\n"); |
| 165 | input_dev = input_allocate_device(); |
| 166 | if (!input_dev) { |
| 167 | em28xx_errdev("input_allocate_device failed\n"); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | usb_make_path(dev->udev, dev->snapshot_button_path, |
| 172 | sizeof(dev->snapshot_button_path)); |
| 173 | strlcat(dev->snapshot_button_path, "/sbutton", |
| 174 | sizeof(dev->snapshot_button_path)); |
| 175 | INIT_DELAYED_WORK(&dev->sbutton_query_work, em28xx_query_sbutton); |
| 176 | |
| 177 | input_dev->name = "em28xx snapshot button"; |
| 178 | input_dev->phys = dev->snapshot_button_path; |
| 179 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); |
| 180 | set_bit(EM28XX_SNAPSHOT_KEY, input_dev->keybit); |
| 181 | input_dev->keycodesize = 0; |
| 182 | input_dev->keycodemax = 0; |
| 183 | input_dev->id.bustype = BUS_USB; |
| 184 | input_dev->id.vendor = le16_to_cpu(dev->udev->descriptor.idVendor); |
| 185 | input_dev->id.product = le16_to_cpu(dev->udev->descriptor.idProduct); |
| 186 | input_dev->id.version = 1; |
| 187 | input_dev->dev.parent = &dev->udev->dev; |
| 188 | |
| 189 | err = input_register_device(input_dev); |
| 190 | if (err) { |
| 191 | em28xx_errdev("input_register_device failed\n"); |
| 192 | input_free_device(input_dev); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | dev->sbutton_input_dev = input_dev; |
| 197 | schedule_delayed_work(&dev->sbutton_query_work, |
| 198 | msecs_to_jiffies(EM28XX_SBUTTON_QUERY_INTERVAL)); |
| 199 | return; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | void em28xx_deregister_snapshot_button(struct em28xx *dev) |
| 204 | { |
| 205 | if (dev->sbutton_input_dev != NULL) { |
| 206 | em28xx_info("Deregistering snapshot button\n"); |
| 207 | cancel_rearming_delayed_work(&dev->sbutton_query_work); |
| 208 | input_unregister_device(dev->sbutton_input_dev); |
| 209 | dev->sbutton_input_dev = NULL; |
| 210 | } |
| 211 | return; |
| 212 | } |
| 213 | |
Mauro Carvalho Chehab | d5e5265 | 2005-11-08 21:37:32 -0800 | [diff] [blame] | 214 | /* ---------------------------------------------------------------------- |
| 215 | * Local variables: |
| 216 | * c-basic-offset: 8 |
| 217 | * End: |
| 218 | */ |