Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * some common structs and functions to handle infrared remotes via |
| 4 | * input layer ... |
| 5 | * |
| 6 | * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs] |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 24 | #include <linux/string.h> |
Mauro Carvalho Chehab | 0b778a5 | 2006-12-27 14:04:09 -0200 | [diff] [blame] | 25 | #include <linux/jiffies.h> |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 26 | #include <media/ir-common.h> |
| 27 | |
| 28 | /* -------------------------------------------------------------------------- */ |
| 29 | |
| 30 | MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]"); |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | |
| 33 | static int repeat = 1; |
| 34 | module_param(repeat, int, 0444); |
| 35 | MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)"); |
| 36 | |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 37 | int media_ir_debug; /* media_ir_debug level (0,1,2) */ |
| 38 | module_param_named(debug, media_ir_debug, int, 0644); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 39 | |
| 40 | /* -------------------------------------------------------------------------- */ |
| 41 | |
| 42 | static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir) |
| 43 | { |
| 44 | if (KEY_RESERVED == ir->keycode) { |
Mauro Carvalho Chehab | 8573b74 | 2009-11-27 22:40:22 -0300 | [diff] [blame] | 45 | printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n", |
| 46 | dev->name, ir->ir_key, ir->keypressed); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 47 | return; |
| 48 | } |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 49 | IR_dprintk(1,"%s: key event code=%d down=%d\n", |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 50 | dev->name,ir->keycode,ir->keypressed); |
| 51 | input_report_key(dev,ir->keycode,ir->keypressed); |
| 52 | input_sync(dev); |
| 53 | } |
| 54 | |
| 55 | /* -------------------------------------------------------------------------- */ |
| 56 | |
Mauro Carvalho Chehab | 055cd55 | 2009-11-29 08:19:59 -0300 | [diff] [blame^] | 57 | int ir_input_init(struct input_dev *dev, struct ir_input_state *ir, |
Mauro Carvalho Chehab | 715a223 | 2009-08-29 14:15:55 -0300 | [diff] [blame] | 58 | int ir_type, struct ir_scancode_table *ir_codes) |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 59 | { |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 60 | ir->ir_type = ir_type; |
Mauro Carvalho Chehab | 715a223 | 2009-08-29 14:15:55 -0300 | [diff] [blame] | 61 | |
Mauro Carvalho Chehab | 055cd55 | 2009-11-29 08:19:59 -0300 | [diff] [blame^] | 62 | /* FIXME: Add the proper code to dynamically allocate IR table */ |
| 63 | |
Mauro Carvalho Chehab | 8573b74 | 2009-11-27 22:40:22 -0300 | [diff] [blame] | 64 | ir_set_keycode_table(dev, ir_codes); |
Mauro Carvalho Chehab | 715a223 | 2009-08-29 14:15:55 -0300 | [diff] [blame] | 65 | |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 66 | clear_bit(0, dev->keybit); |
| 67 | |
| 68 | set_bit(EV_KEY, dev->evbit); |
| 69 | if (repeat) |
| 70 | set_bit(EV_REP, dev->evbit); |
Mauro Carvalho Chehab | 055cd55 | 2009-11-29 08:19:59 -0300 | [diff] [blame^] | 71 | |
| 72 | return 0; |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 73 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(ir_input_init); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 75 | |
Mauro Carvalho Chehab | 055cd55 | 2009-11-29 08:19:59 -0300 | [diff] [blame^] | 76 | void ir_input_free(struct input_dev *input_dev) |
| 77 | { |
| 78 | /* FIXME: Add the proper code to free allocated resources */ |
| 79 | } |
| 80 | EXPORT_SYMBOL_GPL(ir_input_free); |
| 81 | |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 82 | void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir) |
| 83 | { |
| 84 | if (ir->keypressed) { |
| 85 | ir->keypressed = 0; |
| 86 | ir_input_key_event(dev,ir); |
| 87 | } |
| 88 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 89 | EXPORT_SYMBOL_GPL(ir_input_nokey); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 90 | |
| 91 | void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir, |
Mauro Carvalho Chehab | 8573b74 | 2009-11-27 22:40:22 -0300 | [diff] [blame] | 92 | u32 ir_key) |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 93 | { |
Mauro Carvalho Chehab | 8573b74 | 2009-11-27 22:40:22 -0300 | [diff] [blame] | 94 | u32 keycode = ir_g_keycode_from_table(dev, ir_key); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 95 | |
| 96 | if (ir->keypressed && ir->keycode != keycode) { |
| 97 | ir->keypressed = 0; |
| 98 | ir_input_key_event(dev,ir); |
| 99 | } |
| 100 | if (!ir->keypressed) { |
| 101 | ir->ir_key = ir_key; |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 102 | ir->keycode = keycode; |
| 103 | ir->keypressed = 1; |
| 104 | ir_input_key_event(dev,ir); |
| 105 | } |
| 106 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 107 | EXPORT_SYMBOL_GPL(ir_input_keydown); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 108 | |
| 109 | /* -------------------------------------------------------------------------- */ |
Trent Piepho | d67be61 | 2007-07-11 20:28:44 -0300 | [diff] [blame] | 110 | /* extract mask bits out of data and pack them into the result */ |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 111 | u32 ir_extract_bits(u32 data, u32 mask) |
| 112 | { |
Trent Piepho | d67be61 | 2007-07-11 20:28:44 -0300 | [diff] [blame] | 113 | u32 vbit = 1, value = 0; |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 114 | |
Trent Piepho | d67be61 | 2007-07-11 20:28:44 -0300 | [diff] [blame] | 115 | do { |
| 116 | if (mask&1) { |
| 117 | if (data&1) |
| 118 | value |= vbit; |
| 119 | vbit<<=1; |
| 120 | } |
| 121 | data>>=1; |
| 122 | } while (mask>>=1); |
| 123 | |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 124 | return value; |
| 125 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 126 | EXPORT_SYMBOL_GPL(ir_extract_bits); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 127 | |
| 128 | static int inline getbit(u32 *samples, int bit) |
| 129 | { |
| 130 | return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0; |
| 131 | } |
| 132 | |
| 133 | /* sump raw samples for visual debugging ;) */ |
| 134 | int ir_dump_samples(u32 *samples, int count) |
| 135 | { |
| 136 | int i, bit, start; |
| 137 | |
| 138 | printk(KERN_DEBUG "ir samples: "); |
| 139 | start = 0; |
| 140 | for (i = 0; i < count * 32; i++) { |
| 141 | bit = getbit(samples,i); |
| 142 | if (bit) |
| 143 | start = 1; |
| 144 | if (0 == start) |
| 145 | continue; |
| 146 | printk("%s", bit ? "#" : "_"); |
| 147 | } |
| 148 | printk("\n"); |
| 149 | return 0; |
| 150 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 151 | EXPORT_SYMBOL_GPL(ir_dump_samples); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 152 | |
| 153 | /* decode raw samples, pulse distance coding used by NEC remotes */ |
| 154 | int ir_decode_pulsedistance(u32 *samples, int count, int low, int high) |
| 155 | { |
| 156 | int i,last,bit,len; |
| 157 | u32 curBit; |
| 158 | u32 value; |
| 159 | |
| 160 | /* find start burst */ |
| 161 | for (i = len = 0; i < count * 32; i++) { |
| 162 | bit = getbit(samples,i); |
| 163 | if (bit) { |
| 164 | len++; |
| 165 | } else { |
| 166 | if (len >= 29) |
| 167 | break; |
| 168 | len = 0; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* start burst to short */ |
| 173 | if (len < 29) |
| 174 | return 0xffffffff; |
| 175 | |
| 176 | /* find start silence */ |
| 177 | for (len = 0; i < count * 32; i++) { |
| 178 | bit = getbit(samples,i); |
| 179 | if (bit) { |
| 180 | break; |
| 181 | } else { |
| 182 | len++; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* silence to short */ |
| 187 | if (len < 7) |
| 188 | return 0xffffffff; |
| 189 | |
| 190 | /* go decoding */ |
| 191 | len = 0; |
| 192 | last = 1; |
| 193 | value = 0; curBit = 1; |
| 194 | for (; i < count * 32; i++) { |
| 195 | bit = getbit(samples,i); |
| 196 | if (last) { |
| 197 | if(bit) { |
| 198 | continue; |
| 199 | } else { |
| 200 | len = 1; |
| 201 | } |
| 202 | } else { |
| 203 | if (bit) { |
| 204 | if (len > (low + high) /2) |
| 205 | value |= curBit; |
| 206 | curBit <<= 1; |
| 207 | if (curBit == 1) |
| 208 | break; |
| 209 | } else { |
| 210 | len++; |
| 211 | } |
| 212 | } |
| 213 | last = bit; |
| 214 | } |
| 215 | |
| 216 | return value; |
| 217 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 218 | EXPORT_SYMBOL_GPL(ir_decode_pulsedistance); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 219 | |
| 220 | /* decode raw samples, biphase coding, used by rc5 for example */ |
| 221 | int ir_decode_biphase(u32 *samples, int count, int low, int high) |
| 222 | { |
| 223 | int i,last,bit,len,flips; |
| 224 | u32 value; |
| 225 | |
| 226 | /* find start bit (1) */ |
| 227 | for (i = 0; i < 32; i++) { |
| 228 | bit = getbit(samples,i); |
| 229 | if (bit) |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | /* go decoding */ |
| 234 | len = 0; |
| 235 | flips = 0; |
| 236 | value = 1; |
| 237 | for (; i < count * 32; i++) { |
| 238 | if (len > high) |
| 239 | break; |
| 240 | if (flips > 1) |
| 241 | break; |
| 242 | last = bit; |
| 243 | bit = getbit(samples,i); |
| 244 | if (last == bit) { |
| 245 | len++; |
| 246 | continue; |
| 247 | } |
| 248 | if (len < low) { |
| 249 | len++; |
| 250 | flips++; |
| 251 | continue; |
| 252 | } |
| 253 | value <<= 1; |
| 254 | value |= bit; |
| 255 | flips = 0; |
| 256 | len = 1; |
| 257 | } |
| 258 | return value; |
| 259 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 260 | EXPORT_SYMBOL_GPL(ir_decode_biphase); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 261 | |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 262 | /* RC5 decoding stuff, moved from bttv-input.c to share it with |
| 263 | * saa7134 */ |
| 264 | |
| 265 | /* decode raw bit pattern to RC5 code */ |
Andy Walls | 1d23a00 | 2009-09-27 20:05:23 -0300 | [diff] [blame] | 266 | u32 ir_rc5_decode(unsigned int code) |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 267 | { |
| 268 | unsigned int org_code = code; |
| 269 | unsigned int pair; |
| 270 | unsigned int rc5 = 0; |
| 271 | int i; |
| 272 | |
| 273 | for (i = 0; i < 14; ++i) { |
| 274 | pair = code & 0x3; |
| 275 | code >>= 2; |
| 276 | |
| 277 | rc5 <<= 1; |
| 278 | switch (pair) { |
| 279 | case 0: |
| 280 | case 2: |
| 281 | break; |
| 282 | case 1: |
| 283 | rc5 |= 1; |
| 284 | break; |
| 285 | case 3: |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 286 | IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | } |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 290 | IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, " |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 291 | "instr=%x\n", rc5, org_code, RC5_START(rc5), |
| 292 | RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5)); |
| 293 | return rc5; |
| 294 | } |
Andy Walls | 1d23a00 | 2009-09-27 20:05:23 -0300 | [diff] [blame] | 295 | EXPORT_SYMBOL_GPL(ir_rc5_decode); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 296 | |
| 297 | void ir_rc5_timer_end(unsigned long data) |
| 298 | { |
| 299 | struct card_ir *ir = (struct card_ir *)data; |
| 300 | struct timeval tv; |
| 301 | unsigned long current_jiffies, timeout; |
| 302 | u32 gap; |
| 303 | u32 rc5 = 0; |
| 304 | |
| 305 | /* get time */ |
| 306 | current_jiffies = jiffies; |
| 307 | do_gettimeofday(&tv); |
| 308 | |
| 309 | /* avoid overflow with gap >1s */ |
| 310 | if (tv.tv_sec - ir->base_time.tv_sec > 1) { |
| 311 | gap = 200000; |
| 312 | } else { |
| 313 | gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) + |
| 314 | tv.tv_usec - ir->base_time.tv_usec; |
| 315 | } |
| 316 | |
Vincent Penne | 726cf56 | 2007-03-25 11:58:23 -0300 | [diff] [blame] | 317 | /* signal we're ready to start a new code */ |
| 318 | ir->active = 0; |
| 319 | |
| 320 | /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */ |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 321 | if (gap < 28000) { |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 322 | IR_dprintk(1, "ir-common: spurious timer_end\n"); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 323 | return; |
| 324 | } |
| 325 | |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 326 | if (ir->last_bit < 20) { |
| 327 | /* ignore spurious codes (caused by light/other remotes) */ |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 328 | IR_dprintk(1, "ir-common: short code: %x\n", ir->code); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 329 | } else { |
| 330 | ir->code = (ir->code << ir->shift_by) | 1; |
| 331 | rc5 = ir_rc5_decode(ir->code); |
| 332 | |
| 333 | /* two start bits? */ |
| 334 | if (RC5_START(rc5) != ir->start) { |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 335 | IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5)); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 336 | |
| 337 | /* right address? */ |
| 338 | } else if (RC5_ADDR(rc5) == ir->addr) { |
| 339 | u32 toggle = RC5_TOGGLE(rc5); |
| 340 | u32 instr = RC5_INSTR(rc5); |
| 341 | |
| 342 | /* Good code, decide if repeat/repress */ |
| 343 | if (toggle != RC5_TOGGLE(ir->last_rc5) || |
| 344 | instr != RC5_INSTR(ir->last_rc5)) { |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 345 | IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr, |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 346 | toggle); |
| 347 | ir_input_nokey(ir->dev, &ir->ir); |
Mauro Carvalho Chehab | 8573b74 | 2009-11-27 22:40:22 -0300 | [diff] [blame] | 348 | ir_input_keydown(ir->dev, &ir->ir, instr); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* Set/reset key-up timer */ |
Mauro Carvalho Chehab | f7518bd | 2007-07-17 16:27:30 -0300 | [diff] [blame] | 352 | timeout = current_jiffies + |
| 353 | msecs_to_jiffies(ir->rc5_key_timeout); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 354 | mod_timer(&ir->timer_keyup, timeout); |
| 355 | |
| 356 | /* Save code for repeat test */ |
| 357 | ir->last_rc5 = rc5; |
| 358 | } |
| 359 | } |
| 360 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 361 | EXPORT_SYMBOL_GPL(ir_rc5_timer_end); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 362 | |
| 363 | void ir_rc5_timer_keyup(unsigned long data) |
| 364 | { |
| 365 | struct card_ir *ir = (struct card_ir *)data; |
| 366 | |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 367 | IR_dprintk(1, "ir-common: key released\n"); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 368 | ir_input_nokey(ir->dev, &ir->ir); |
| 369 | } |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 370 | EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup); |