ir-functions.c 7.8 KB

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