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 | |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 37 | /* -------------------------------------------------------------------------- */ |
| 38 | |
| 39 | static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir) |
| 40 | { |
| 41 | if (KEY_RESERVED == ir->keycode) { |
Mauro Carvalho Chehab | 8573b74 | 2009-11-27 22:40:22 -0300 | [diff] [blame] | 42 | printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n", |
| 43 | dev->name, ir->ir_key, ir->keypressed); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 44 | return; |
| 45 | } |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 46 | IR_dprintk(1,"%s: key event code=%d down=%d\n", |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 47 | dev->name,ir->keycode,ir->keypressed); |
| 48 | input_report_key(dev,ir->keycode,ir->keypressed); |
| 49 | input_sync(dev); |
| 50 | } |
| 51 | |
| 52 | /* -------------------------------------------------------------------------- */ |
| 53 | |
Mauro Carvalho Chehab | 055cd55 | 2009-11-29 08:19:59 -0300 | [diff] [blame] | 54 | int ir_input_init(struct input_dev *dev, struct ir_input_state *ir, |
Mauro Carvalho Chehab | 971e829 | 2009-12-14 13:53:37 -0300 | [diff] [blame] | 55 | const u64 ir_type) |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 56 | { |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 57 | ir->ir_type = ir_type; |
Mauro Carvalho Chehab | 715a223 | 2009-08-29 14:15:55 -0300 | [diff] [blame] | 58 | |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 59 | if (repeat) |
| 60 | set_bit(EV_REP, dev->evbit); |
Mauro Carvalho Chehab | 055cd55 | 2009-11-29 08:19:59 -0300 | [diff] [blame] | 61 | |
| 62 | return 0; |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 63 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 64 | EXPORT_SYMBOL_GPL(ir_input_init); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 65 | |
Mauro Carvalho Chehab | 055cd55 | 2009-11-29 08:19:59 -0300 | [diff] [blame] | 66 | |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 67 | void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir) |
| 68 | { |
| 69 | if (ir->keypressed) { |
| 70 | ir->keypressed = 0; |
| 71 | ir_input_key_event(dev,ir); |
| 72 | } |
| 73 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(ir_input_nokey); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 75 | |
| 76 | 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] | 77 | u32 ir_key) |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 78 | { |
Mauro Carvalho Chehab | 8573b74 | 2009-11-27 22:40:22 -0300 | [diff] [blame] | 79 | u32 keycode = ir_g_keycode_from_table(dev, ir_key); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 80 | |
| 81 | if (ir->keypressed && ir->keycode != keycode) { |
| 82 | ir->keypressed = 0; |
| 83 | ir_input_key_event(dev,ir); |
| 84 | } |
| 85 | if (!ir->keypressed) { |
| 86 | ir->ir_key = ir_key; |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 87 | ir->keycode = keycode; |
| 88 | ir->keypressed = 1; |
| 89 | ir_input_key_event(dev,ir); |
| 90 | } |
| 91 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 92 | EXPORT_SYMBOL_GPL(ir_input_keydown); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 93 | |
| 94 | /* -------------------------------------------------------------------------- */ |
Trent Piepho | d67be61 | 2007-07-11 20:28:44 -0300 | [diff] [blame] | 95 | /* extract mask bits out of data and pack them into the result */ |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 96 | u32 ir_extract_bits(u32 data, u32 mask) |
| 97 | { |
Trent Piepho | d67be61 | 2007-07-11 20:28:44 -0300 | [diff] [blame] | 98 | u32 vbit = 1, value = 0; |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 99 | |
Trent Piepho | d67be61 | 2007-07-11 20:28:44 -0300 | [diff] [blame] | 100 | do { |
| 101 | if (mask&1) { |
| 102 | if (data&1) |
| 103 | value |= vbit; |
| 104 | vbit<<=1; |
| 105 | } |
| 106 | data>>=1; |
| 107 | } while (mask>>=1); |
| 108 | |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 109 | return value; |
| 110 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 111 | EXPORT_SYMBOL_GPL(ir_extract_bits); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 112 | |
| 113 | static int inline getbit(u32 *samples, int bit) |
| 114 | { |
| 115 | return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0; |
| 116 | } |
| 117 | |
| 118 | /* sump raw samples for visual debugging ;) */ |
| 119 | int ir_dump_samples(u32 *samples, int count) |
| 120 | { |
| 121 | int i, bit, start; |
| 122 | |
| 123 | printk(KERN_DEBUG "ir samples: "); |
| 124 | start = 0; |
| 125 | for (i = 0; i < count * 32; i++) { |
| 126 | bit = getbit(samples,i); |
| 127 | if (bit) |
| 128 | start = 1; |
| 129 | if (0 == start) |
| 130 | continue; |
| 131 | printk("%s", bit ? "#" : "_"); |
| 132 | } |
| 133 | printk("\n"); |
| 134 | return 0; |
| 135 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 136 | EXPORT_SYMBOL_GPL(ir_dump_samples); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 137 | |
| 138 | /* decode raw samples, pulse distance coding used by NEC remotes */ |
| 139 | int ir_decode_pulsedistance(u32 *samples, int count, int low, int high) |
| 140 | { |
| 141 | int i,last,bit,len; |
| 142 | u32 curBit; |
| 143 | u32 value; |
| 144 | |
| 145 | /* find start burst */ |
| 146 | for (i = len = 0; i < count * 32; i++) { |
| 147 | bit = getbit(samples,i); |
| 148 | if (bit) { |
| 149 | len++; |
| 150 | } else { |
| 151 | if (len >= 29) |
| 152 | break; |
| 153 | len = 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* start burst to short */ |
| 158 | if (len < 29) |
| 159 | return 0xffffffff; |
| 160 | |
| 161 | /* find start silence */ |
| 162 | for (len = 0; i < count * 32; i++) { |
| 163 | bit = getbit(samples,i); |
| 164 | if (bit) { |
| 165 | break; |
| 166 | } else { |
| 167 | len++; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* silence to short */ |
| 172 | if (len < 7) |
| 173 | return 0xffffffff; |
| 174 | |
| 175 | /* go decoding */ |
| 176 | len = 0; |
| 177 | last = 1; |
| 178 | value = 0; curBit = 1; |
| 179 | for (; i < count * 32; i++) { |
| 180 | bit = getbit(samples,i); |
| 181 | if (last) { |
| 182 | if(bit) { |
| 183 | continue; |
| 184 | } else { |
| 185 | len = 1; |
| 186 | } |
| 187 | } else { |
| 188 | if (bit) { |
| 189 | if (len > (low + high) /2) |
| 190 | value |= curBit; |
| 191 | curBit <<= 1; |
| 192 | if (curBit == 1) |
| 193 | break; |
| 194 | } else { |
| 195 | len++; |
| 196 | } |
| 197 | } |
| 198 | last = bit; |
| 199 | } |
| 200 | |
| 201 | return value; |
| 202 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 203 | EXPORT_SYMBOL_GPL(ir_decode_pulsedistance); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 204 | |
| 205 | /* decode raw samples, biphase coding, used by rc5 for example */ |
| 206 | int ir_decode_biphase(u32 *samples, int count, int low, int high) |
| 207 | { |
| 208 | int i,last,bit,len,flips; |
| 209 | u32 value; |
| 210 | |
| 211 | /* find start bit (1) */ |
| 212 | for (i = 0; i < 32; i++) { |
| 213 | bit = getbit(samples,i); |
| 214 | if (bit) |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | /* go decoding */ |
| 219 | len = 0; |
| 220 | flips = 0; |
| 221 | value = 1; |
| 222 | for (; i < count * 32; i++) { |
| 223 | if (len > high) |
| 224 | break; |
| 225 | if (flips > 1) |
| 226 | break; |
| 227 | last = bit; |
| 228 | bit = getbit(samples,i); |
| 229 | if (last == bit) { |
| 230 | len++; |
| 231 | continue; |
| 232 | } |
| 233 | if (len < low) { |
| 234 | len++; |
| 235 | flips++; |
| 236 | continue; |
| 237 | } |
| 238 | value <<= 1; |
| 239 | value |= bit; |
| 240 | flips = 0; |
| 241 | len = 1; |
| 242 | } |
| 243 | return value; |
| 244 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 245 | EXPORT_SYMBOL_GPL(ir_decode_biphase); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 246 | |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 247 | /* RC5 decoding stuff, moved from bttv-input.c to share it with |
| 248 | * saa7134 */ |
| 249 | |
| 250 | /* decode raw bit pattern to RC5 code */ |
Andy Walls | 1d23a00 | 2009-09-27 20:05:23 -0300 | [diff] [blame] | 251 | u32 ir_rc5_decode(unsigned int code) |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 252 | { |
| 253 | unsigned int org_code = code; |
| 254 | unsigned int pair; |
| 255 | unsigned int rc5 = 0; |
| 256 | int i; |
| 257 | |
| 258 | for (i = 0; i < 14; ++i) { |
| 259 | pair = code & 0x3; |
| 260 | code >>= 2; |
| 261 | |
| 262 | rc5 <<= 1; |
| 263 | switch (pair) { |
| 264 | case 0: |
| 265 | case 2: |
| 266 | break; |
| 267 | case 1: |
| 268 | rc5 |= 1; |
| 269 | break; |
| 270 | case 3: |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 271 | 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] | 272 | return 0; |
| 273 | } |
| 274 | } |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 275 | 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] | 276 | "instr=%x\n", rc5, org_code, RC5_START(rc5), |
| 277 | RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5)); |
| 278 | return rc5; |
| 279 | } |
Andy Walls | 1d23a00 | 2009-09-27 20:05:23 -0300 | [diff] [blame] | 280 | EXPORT_SYMBOL_GPL(ir_rc5_decode); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 281 | |
| 282 | void ir_rc5_timer_end(unsigned long data) |
| 283 | { |
| 284 | struct card_ir *ir = (struct card_ir *)data; |
| 285 | struct timeval tv; |
| 286 | unsigned long current_jiffies, timeout; |
| 287 | u32 gap; |
| 288 | u32 rc5 = 0; |
| 289 | |
| 290 | /* get time */ |
| 291 | current_jiffies = jiffies; |
| 292 | do_gettimeofday(&tv); |
| 293 | |
| 294 | /* avoid overflow with gap >1s */ |
| 295 | if (tv.tv_sec - ir->base_time.tv_sec > 1) { |
| 296 | gap = 200000; |
| 297 | } else { |
| 298 | gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) + |
| 299 | tv.tv_usec - ir->base_time.tv_usec; |
| 300 | } |
| 301 | |
Vincent Penne | 726cf56 | 2007-03-25 11:58:23 -0300 | [diff] [blame] | 302 | /* signal we're ready to start a new code */ |
| 303 | ir->active = 0; |
| 304 | |
| 305 | /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */ |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 306 | if (gap < 28000) { |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 307 | IR_dprintk(1, "ir-common: spurious timer_end\n"); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 311 | if (ir->last_bit < 20) { |
| 312 | /* ignore spurious codes (caused by light/other remotes) */ |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 313 | IR_dprintk(1, "ir-common: short code: %x\n", ir->code); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 314 | } else { |
| 315 | ir->code = (ir->code << ir->shift_by) | 1; |
| 316 | rc5 = ir_rc5_decode(ir->code); |
| 317 | |
| 318 | /* two start bits? */ |
| 319 | if (RC5_START(rc5) != ir->start) { |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 320 | 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] | 321 | |
| 322 | /* right address? */ |
| 323 | } else if (RC5_ADDR(rc5) == ir->addr) { |
| 324 | u32 toggle = RC5_TOGGLE(rc5); |
| 325 | u32 instr = RC5_INSTR(rc5); |
| 326 | |
| 327 | /* Good code, decide if repeat/repress */ |
| 328 | if (toggle != RC5_TOGGLE(ir->last_rc5) || |
| 329 | instr != RC5_INSTR(ir->last_rc5)) { |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 330 | IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr, |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 331 | toggle); |
| 332 | ir_input_nokey(ir->dev, &ir->ir); |
Mauro Carvalho Chehab | 8573b74 | 2009-11-27 22:40:22 -0300 | [diff] [blame] | 333 | ir_input_keydown(ir->dev, &ir->ir, instr); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Set/reset key-up timer */ |
Mauro Carvalho Chehab | f7518bd | 2007-07-17 16:27:30 -0300 | [diff] [blame] | 337 | timeout = current_jiffies + |
| 338 | msecs_to_jiffies(ir->rc5_key_timeout); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 339 | mod_timer(&ir->timer_keyup, timeout); |
| 340 | |
| 341 | /* Save code for repeat test */ |
| 342 | ir->last_rc5 = rc5; |
| 343 | } |
| 344 | } |
| 345 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 346 | EXPORT_SYMBOL_GPL(ir_rc5_timer_end); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 347 | |
| 348 | void ir_rc5_timer_keyup(unsigned long data) |
| 349 | { |
| 350 | struct card_ir *ir = (struct card_ir *)data; |
| 351 | |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 352 | IR_dprintk(1, "ir-common: key released\n"); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 353 | ir_input_nokey(ir->dev, &ir->ir); |
| 354 | } |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 355 | EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup); |