Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 1 | /* |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame^] | 2 | * some common functions to handle infrared remote protocol decoding for |
| 3 | * drivers which have not yet been (or can't be) converted to use the |
| 4 | * regular protocol decoders... |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 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> |
Mauro Carvalho Chehab | 3f113e3 | 2010-04-08 15:10:27 -0300 | [diff] [blame] | 27 | #include "ir-core-priv.h" |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 28 | |
| 29 | /* -------------------------------------------------------------------------- */ |
| 30 | |
| 31 | MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 34 | /* -------------------------------------------------------------------------- */ |
Trent Piepho | d67be61 | 2007-07-11 20:28:44 -0300 | [diff] [blame] | 35 | /* extract mask bits out of data and pack them into the result */ |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 36 | u32 ir_extract_bits(u32 data, u32 mask) |
| 37 | { |
Trent Piepho | d67be61 | 2007-07-11 20:28:44 -0300 | [diff] [blame] | 38 | u32 vbit = 1, value = 0; |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 39 | |
Trent Piepho | d67be61 | 2007-07-11 20:28:44 -0300 | [diff] [blame] | 40 | do { |
| 41 | if (mask&1) { |
| 42 | if (data&1) |
| 43 | value |= vbit; |
| 44 | vbit<<=1; |
| 45 | } |
| 46 | data>>=1; |
| 47 | } while (mask>>=1); |
| 48 | |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 49 | return value; |
| 50 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 51 | EXPORT_SYMBOL_GPL(ir_extract_bits); |
Ricardo Cerqueira | d15549a | 2006-03-03 12:13:42 -0300 | [diff] [blame] | 52 | |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 53 | /* RC5 decoding stuff, moved from bttv-input.c to share it with |
| 54 | * saa7134 */ |
| 55 | |
| 56 | /* decode raw bit pattern to RC5 code */ |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame^] | 57 | static u32 ir_rc5_decode(unsigned int code) |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 58 | { |
| 59 | unsigned int org_code = code; |
| 60 | unsigned int pair; |
| 61 | unsigned int rc5 = 0; |
| 62 | int i; |
| 63 | |
| 64 | for (i = 0; i < 14; ++i) { |
| 65 | pair = code & 0x3; |
| 66 | code >>= 2; |
| 67 | |
| 68 | rc5 <<= 1; |
| 69 | switch (pair) { |
| 70 | case 0: |
| 71 | case 2: |
| 72 | break; |
| 73 | case 1: |
| 74 | rc5 |= 1; |
| 75 | break; |
| 76 | case 3: |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 77 | 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] | 78 | return 0; |
| 79 | } |
| 80 | } |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 81 | 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] | 82 | "instr=%x\n", rc5, org_code, RC5_START(rc5), |
| 83 | RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5)); |
| 84 | return rc5; |
| 85 | } |
| 86 | |
| 87 | void ir_rc5_timer_end(unsigned long data) |
| 88 | { |
| 89 | struct card_ir *ir = (struct card_ir *)data; |
| 90 | struct timeval tv; |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame^] | 91 | unsigned long current_jiffies; |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 92 | u32 gap; |
| 93 | u32 rc5 = 0; |
| 94 | |
| 95 | /* get time */ |
| 96 | current_jiffies = jiffies; |
| 97 | do_gettimeofday(&tv); |
| 98 | |
| 99 | /* avoid overflow with gap >1s */ |
| 100 | if (tv.tv_sec - ir->base_time.tv_sec > 1) { |
| 101 | gap = 200000; |
| 102 | } else { |
| 103 | gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) + |
| 104 | tv.tv_usec - ir->base_time.tv_usec; |
| 105 | } |
| 106 | |
Vincent Penne | 726cf56 | 2007-03-25 11:58:23 -0300 | [diff] [blame] | 107 | /* signal we're ready to start a new code */ |
| 108 | ir->active = 0; |
| 109 | |
| 110 | /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */ |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 111 | if (gap < 28000) { |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 112 | IR_dprintk(1, "ir-common: spurious timer_end\n"); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 116 | if (ir->last_bit < 20) { |
| 117 | /* ignore spurious codes (caused by light/other remotes) */ |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 118 | IR_dprintk(1, "ir-common: short code: %x\n", ir->code); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 119 | } else { |
| 120 | ir->code = (ir->code << ir->shift_by) | 1; |
| 121 | rc5 = ir_rc5_decode(ir->code); |
| 122 | |
| 123 | /* two start bits? */ |
| 124 | if (RC5_START(rc5) != ir->start) { |
Mauro Carvalho Chehab | 4e89217 | 2009-11-27 21:54:41 -0300 | [diff] [blame] | 125 | 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] | 126 | |
| 127 | /* right address? */ |
| 128 | } else if (RC5_ADDR(rc5) == ir->addr) { |
| 129 | u32 toggle = RC5_TOGGLE(rc5); |
| 130 | u32 instr = RC5_INSTR(rc5); |
| 131 | |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame^] | 132 | /* Good code */ |
| 133 | ir_keydown(ir->dev, instr, toggle); |
| 134 | IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", |
| 135 | instr, toggle); |
Hermann Pitton | 9160723 | 2006-12-07 21:45:28 -0300 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
Mauro Carvalho Chehab | 4db16db | 2008-07-17 22:28:56 -0300 | [diff] [blame] | 139 | EXPORT_SYMBOL_GPL(ir_rc5_timer_end); |