ir-functions.c 7.8 KB

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