ir-functions.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include <linux/module.h>
  23. #include <linux/string.h>
  24. #include <linux/jiffies.h>
  25. #include <media/ir-common.h>
  26. /* -------------------------------------------------------------------------- */
  27. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  28. MODULE_LICENSE("GPL");
  29. static int repeat = 1;
  30. module_param(repeat, int, 0444);
  31. MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
  32. static int debug; /* debug level (0,1,2) */
  33. module_param(debug, int, 0644);
  34. #define dprintk(level, fmt, arg...) if (debug >= level) \
  35. printk(KERN_DEBUG fmt , ## arg)
  36. /* -------------------------------------------------------------------------- */
  37. static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
  38. {
  39. if (KEY_RESERVED == ir->keycode) {
  40. printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
  41. dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
  42. return;
  43. }
  44. dprintk(1,"%s: key event code=%d down=%d\n",
  45. dev->name,ir->keycode,ir->keypressed);
  46. input_report_key(dev,ir->keycode,ir->keypressed);
  47. input_sync(dev);
  48. }
  49. /* -------------------------------------------------------------------------- */
  50. void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
  51. int ir_type, struct ir_scancode_table *ir_codes)
  52. {
  53. int i;
  54. ir->ir_type = ir_type;
  55. memset(ir->ir_codes, sizeof(ir->ir_codes), 0);
  56. /*
  57. * FIXME: This is a temporary workaround to use the new IR tables
  58. * with the old approach. Later patches will replace this to a
  59. * proper method
  60. */
  61. if (ir_codes)
  62. for (i = 0; i < ir_codes->size; i++)
  63. if (ir_codes->scan[i].scancode < IR_KEYTAB_SIZE)
  64. ir->ir_codes[ir_codes->scan[i].scancode] = ir_codes->scan[i].keycode;
  65. dev->keycode = ir->ir_codes;
  66. dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
  67. dev->keycodemax = IR_KEYTAB_SIZE;
  68. for (i = 0; i < IR_KEYTAB_SIZE; i++)
  69. set_bit(ir->ir_codes[i], dev->keybit);
  70. clear_bit(0, dev->keybit);
  71. set_bit(EV_KEY, dev->evbit);
  72. if (repeat)
  73. set_bit(EV_REP, dev->evbit);
  74. }
  75. EXPORT_SYMBOL_GPL(ir_input_init);
  76. void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
  77. {
  78. if (ir->keypressed) {
  79. ir->keypressed = 0;
  80. ir_input_key_event(dev,ir);
  81. }
  82. }
  83. EXPORT_SYMBOL_GPL(ir_input_nokey);
  84. void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
  85. u32 ir_key, u32 ir_raw)
  86. {
  87. u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
  88. if (ir->keypressed && ir->keycode != keycode) {
  89. ir->keypressed = 0;
  90. ir_input_key_event(dev,ir);
  91. }
  92. if (!ir->keypressed) {
  93. ir->ir_key = ir_key;
  94. ir->ir_raw = ir_raw;
  95. ir->keycode = keycode;
  96. ir->keypressed = 1;
  97. ir_input_key_event(dev,ir);
  98. }
  99. }
  100. EXPORT_SYMBOL_GPL(ir_input_keydown);
  101. /* -------------------------------------------------------------------------- */
  102. /* extract mask bits out of data and pack them into the result */
  103. u32 ir_extract_bits(u32 data, u32 mask)
  104. {
  105. u32 vbit = 1, value = 0;
  106. do {
  107. if (mask&1) {
  108. if (data&1)
  109. value |= vbit;
  110. vbit<<=1;
  111. }
  112. data>>=1;
  113. } while (mask>>=1);
  114. return value;
  115. }
  116. EXPORT_SYMBOL_GPL(ir_extract_bits);
  117. static int inline getbit(u32 *samples, int bit)
  118. {
  119. return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
  120. }
  121. /* sump raw samples for visual debugging ;) */
  122. int ir_dump_samples(u32 *samples, int count)
  123. {
  124. int i, bit, start;
  125. printk(KERN_DEBUG "ir samples: ");
  126. start = 0;
  127. for (i = 0; i < count * 32; i++) {
  128. bit = getbit(samples,i);
  129. if (bit)
  130. start = 1;
  131. if (0 == start)
  132. continue;
  133. printk("%s", bit ? "#" : "_");
  134. }
  135. printk("\n");
  136. return 0;
  137. }
  138. EXPORT_SYMBOL_GPL(ir_dump_samples);
  139. /* decode raw samples, pulse distance coding used by NEC remotes */
  140. int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
  141. {
  142. int i,last,bit,len;
  143. u32 curBit;
  144. u32 value;
  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. /* start burst to short */
  157. if (len < 29)
  158. return 0xffffffff;
  159. /* find start silence */
  160. for (len = 0; i < count * 32; i++) {
  161. bit = getbit(samples,i);
  162. if (bit) {
  163. break;
  164. } else {
  165. len++;
  166. }
  167. }
  168. /* silence to short */
  169. if (len < 7)
  170. return 0xffffffff;
  171. /* go decoding */
  172. len = 0;
  173. last = 1;
  174. value = 0; curBit = 1;
  175. for (; i < count * 32; i++) {
  176. bit = getbit(samples,i);
  177. if (last) {
  178. if(bit) {
  179. continue;
  180. } else {
  181. len = 1;
  182. }
  183. } else {
  184. if (bit) {
  185. if (len > (low + high) /2)
  186. value |= curBit;
  187. curBit <<= 1;
  188. if (curBit == 1)
  189. break;
  190. } else {
  191. len++;
  192. }
  193. }
  194. last = bit;
  195. }
  196. return value;
  197. }
  198. EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
  199. /* decode raw samples, biphase coding, used by rc5 for example */
  200. int ir_decode_biphase(u32 *samples, int count, int low, int high)
  201. {
  202. int i,last,bit,len,flips;
  203. u32 value;
  204. /* find start bit (1) */
  205. for (i = 0; i < 32; i++) {
  206. bit = getbit(samples,i);
  207. if (bit)
  208. break;
  209. }
  210. /* go decoding */
  211. len = 0;
  212. flips = 0;
  213. value = 1;
  214. for (; i < count * 32; i++) {
  215. if (len > high)
  216. break;
  217. if (flips > 1)
  218. break;
  219. last = bit;
  220. bit = getbit(samples,i);
  221. if (last == bit) {
  222. len++;
  223. continue;
  224. }
  225. if (len < low) {
  226. len++;
  227. flips++;
  228. continue;
  229. }
  230. value <<= 1;
  231. value |= bit;
  232. flips = 0;
  233. len = 1;
  234. }
  235. return value;
  236. }
  237. EXPORT_SYMBOL_GPL(ir_decode_biphase);
  238. /* RC5 decoding stuff, moved from bttv-input.c to share it with
  239. * saa7134 */
  240. /* decode raw bit pattern to RC5 code */
  241. static u32 ir_rc5_decode(unsigned int code)
  242. {
  243. unsigned int org_code = code;
  244. unsigned int pair;
  245. unsigned int rc5 = 0;
  246. int i;
  247. for (i = 0; i < 14; ++i) {
  248. pair = code & 0x3;
  249. code >>= 2;
  250. rc5 <<= 1;
  251. switch (pair) {
  252. case 0:
  253. case 2:
  254. break;
  255. case 1:
  256. rc5 |= 1;
  257. break;
  258. case 3:
  259. dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
  260. return 0;
  261. }
  262. }
  263. dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
  264. "instr=%x\n", rc5, org_code, RC5_START(rc5),
  265. RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
  266. return rc5;
  267. }
  268. void ir_rc5_timer_end(unsigned long data)
  269. {
  270. struct card_ir *ir = (struct card_ir *)data;
  271. struct timeval tv;
  272. unsigned long current_jiffies, timeout;
  273. u32 gap;
  274. u32 rc5 = 0;
  275. /* get time */
  276. current_jiffies = jiffies;
  277. do_gettimeofday(&tv);
  278. /* avoid overflow with gap >1s */
  279. if (tv.tv_sec - ir->base_time.tv_sec > 1) {
  280. gap = 200000;
  281. } else {
  282. gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
  283. tv.tv_usec - ir->base_time.tv_usec;
  284. }
  285. /* signal we're ready to start a new code */
  286. ir->active = 0;
  287. /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
  288. if (gap < 28000) {
  289. dprintk(1, "ir-common: spurious timer_end\n");
  290. return;
  291. }
  292. if (ir->last_bit < 20) {
  293. /* ignore spurious codes (caused by light/other remotes) */
  294. dprintk(1, "ir-common: short code: %x\n", ir->code);
  295. } else {
  296. ir->code = (ir->code << ir->shift_by) | 1;
  297. rc5 = ir_rc5_decode(ir->code);
  298. /* two start bits? */
  299. if (RC5_START(rc5) != ir->start) {
  300. dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
  301. /* right address? */
  302. } else if (RC5_ADDR(rc5) == ir->addr) {
  303. u32 toggle = RC5_TOGGLE(rc5);
  304. u32 instr = RC5_INSTR(rc5);
  305. /* Good code, decide if repeat/repress */
  306. if (toggle != RC5_TOGGLE(ir->last_rc5) ||
  307. instr != RC5_INSTR(ir->last_rc5)) {
  308. dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
  309. toggle);
  310. ir_input_nokey(ir->dev, &ir->ir);
  311. ir_input_keydown(ir->dev, &ir->ir, instr,
  312. instr);
  313. }
  314. /* Set/reset key-up timer */
  315. timeout = current_jiffies +
  316. msecs_to_jiffies(ir->rc5_key_timeout);
  317. mod_timer(&ir->timer_keyup, timeout);
  318. /* Save code for repeat test */
  319. ir->last_rc5 = rc5;
  320. }
  321. }
  322. }
  323. EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
  324. void ir_rc5_timer_keyup(unsigned long data)
  325. {
  326. struct card_ir *ir = (struct card_ir *)data;
  327. dprintk(1, "ir-common: key released\n");
  328. ir_input_nokey(ir->dev, &ir->ir);
  329. }
  330. EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);