ir-functions.c 8.4 KB

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