ir-functions.c 8.4 KB

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