ir-keytable.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* ir-register.c - handle IR scancode->keycode tables
  2. *
  3. * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/input.h>
  15. #include <linux/slab.h>
  16. #include <media/ir-common.h>
  17. /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
  18. #define IR_TAB_MIN_SIZE 256
  19. #define IR_TAB_MAX_SIZE 8192
  20. /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
  21. #define IR_KEYPRESS_TIMEOUT 250
  22. /**
  23. * ir_resize_table() - resizes a scancode table if necessary
  24. * @rc_tab: the ir_scancode_table to resize
  25. * @return: zero on success or a negative error code
  26. *
  27. * This routine will shrink the ir_scancode_table if it has lots of
  28. * unused entries and grow it if it is full.
  29. */
  30. static int ir_resize_table(struct ir_scancode_table *rc_tab)
  31. {
  32. unsigned int oldalloc = rc_tab->alloc;
  33. unsigned int newalloc = oldalloc;
  34. struct ir_scancode *oldscan = rc_tab->scan;
  35. struct ir_scancode *newscan;
  36. if (rc_tab->size == rc_tab->len) {
  37. /* All entries in use -> grow keytable */
  38. if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
  39. return -ENOMEM;
  40. newalloc *= 2;
  41. IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
  42. }
  43. if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
  44. /* Less than 1/3 of entries in use -> shrink keytable */
  45. newalloc /= 2;
  46. IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
  47. }
  48. if (newalloc == oldalloc)
  49. return 0;
  50. newscan = kmalloc(newalloc, GFP_ATOMIC);
  51. if (!newscan) {
  52. IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
  53. return -ENOMEM;
  54. }
  55. memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
  56. rc_tab->scan = newscan;
  57. rc_tab->alloc = newalloc;
  58. rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
  59. kfree(oldscan);
  60. return 0;
  61. }
  62. /**
  63. * ir_do_setkeycode() - internal function to set a keycode in the
  64. * scancode->keycode table
  65. * @dev: the struct input_dev device descriptor
  66. * @rc_tab: the struct ir_scancode_table to set the keycode in
  67. * @scancode: the scancode for the ir command
  68. * @keycode: the keycode for the ir command
  69. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  70. *
  71. * This routine is used internally to manipulate the scancode->keycode table.
  72. * The caller has to hold @rc_tab->lock.
  73. */
  74. static int ir_do_setkeycode(struct input_dev *dev,
  75. struct ir_scancode_table *rc_tab,
  76. unsigned scancode, unsigned keycode)
  77. {
  78. unsigned int i;
  79. int old_keycode = KEY_RESERVED;
  80. /* First check if we already have a mapping for this ir command */
  81. for (i = 0; i < rc_tab->len; i++) {
  82. /* Keytable is sorted from lowest to highest scancode */
  83. if (rc_tab->scan[i].scancode > scancode)
  84. break;
  85. else if (rc_tab->scan[i].scancode < scancode)
  86. continue;
  87. old_keycode = rc_tab->scan[i].keycode;
  88. rc_tab->scan[i].keycode = keycode;
  89. /* Did the user wish to remove the mapping? */
  90. if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) {
  91. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
  92. i, scancode);
  93. rc_tab->len--;
  94. memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1],
  95. (rc_tab->len - i) * sizeof(struct ir_scancode));
  96. }
  97. /* Possibly shrink the keytable, failure is not a problem */
  98. ir_resize_table(rc_tab);
  99. break;
  100. }
  101. if (old_keycode == KEY_RESERVED) {
  102. /* No previous mapping found, we might need to grow the table */
  103. if (ir_resize_table(rc_tab))
  104. return -ENOMEM;
  105. IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
  106. i, scancode, keycode);
  107. /* i is the proper index to insert our new keycode */
  108. memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
  109. (rc_tab->len - i) * sizeof(struct ir_scancode));
  110. rc_tab->scan[i].scancode = scancode;
  111. rc_tab->scan[i].keycode = keycode;
  112. rc_tab->len++;
  113. set_bit(keycode, dev->keybit);
  114. } else {
  115. IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
  116. i, scancode, keycode);
  117. /* A previous mapping was updated... */
  118. clear_bit(old_keycode, dev->keybit);
  119. /* ...but another scancode might use the same keycode */
  120. for (i = 0; i < rc_tab->len; i++) {
  121. if (rc_tab->scan[i].keycode == old_keycode) {
  122. set_bit(old_keycode, dev->keybit);
  123. break;
  124. }
  125. }
  126. }
  127. return 0;
  128. }
  129. /**
  130. * ir_setkeycode() - set a keycode in the scancode->keycode table
  131. * @dev: the struct input_dev device descriptor
  132. * @scancode: the desired scancode
  133. * @keycode: result
  134. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  135. *
  136. * This routine is used to handle evdev EVIOCSKEY ioctl.
  137. */
  138. static int ir_setkeycode(struct input_dev *dev,
  139. unsigned int scancode, unsigned int keycode)
  140. {
  141. int rc;
  142. unsigned long flags;
  143. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  144. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  145. spin_lock_irqsave(&rc_tab->lock, flags);
  146. rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode);
  147. spin_unlock_irqrestore(&rc_tab->lock, flags);
  148. return rc;
  149. }
  150. /**
  151. * ir_setkeytable() - sets several entries in the scancode->keycode table
  152. * @dev: the struct input_dev device descriptor
  153. * @to: the struct ir_scancode_table to copy entries to
  154. * @from: the struct ir_scancode_table to copy entries from
  155. * @return: -EINVAL if all keycodes could not be inserted, otherwise zero.
  156. *
  157. * This routine is used to handle table initialization.
  158. */
  159. static int ir_setkeytable(struct input_dev *dev,
  160. struct ir_scancode_table *to,
  161. const struct ir_scancode_table *from)
  162. {
  163. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  164. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  165. unsigned long flags;
  166. unsigned int i;
  167. int rc = 0;
  168. spin_lock_irqsave(&rc_tab->lock, flags);
  169. for (i = 0; i < from->size; i++) {
  170. rc = ir_do_setkeycode(dev, to, from->scan[i].scancode,
  171. from->scan[i].keycode);
  172. if (rc)
  173. break;
  174. }
  175. spin_unlock_irqrestore(&rc_tab->lock, flags);
  176. return rc;
  177. }
  178. /**
  179. * ir_getkeycode() - get a keycode from the scancode->keycode table
  180. * @dev: the struct input_dev device descriptor
  181. * @scancode: the desired scancode
  182. * @keycode: used to return the keycode, if found, or KEY_RESERVED
  183. * @return: always returns zero.
  184. *
  185. * This routine is used to handle evdev EVIOCGKEY ioctl.
  186. */
  187. static int ir_getkeycode(struct input_dev *dev,
  188. unsigned int scancode, unsigned int *keycode)
  189. {
  190. int start, end, mid;
  191. unsigned long flags;
  192. int key = KEY_RESERVED;
  193. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  194. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  195. spin_lock_irqsave(&rc_tab->lock, flags);
  196. start = 0;
  197. end = rc_tab->len - 1;
  198. while (start <= end) {
  199. mid = (start + end) / 2;
  200. if (rc_tab->scan[mid].scancode < scancode)
  201. start = mid + 1;
  202. else if (rc_tab->scan[mid].scancode > scancode)
  203. end = mid - 1;
  204. else {
  205. key = rc_tab->scan[mid].keycode;
  206. break;
  207. }
  208. }
  209. spin_unlock_irqrestore(&rc_tab->lock, flags);
  210. if (key == KEY_RESERVED)
  211. IR_dprintk(1, "unknown key for scancode 0x%04x\n",
  212. scancode);
  213. *keycode = key;
  214. return 0;
  215. }
  216. /**
  217. * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  218. * @input_dev: the struct input_dev descriptor of the device
  219. * @scancode: the scancode that we're seeking
  220. *
  221. * This routine is used by the input routines when a key is pressed at the
  222. * IR. The scancode is received and needs to be converted into a keycode.
  223. * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
  224. * corresponding keycode from the table.
  225. */
  226. u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
  227. {
  228. int keycode;
  229. ir_getkeycode(dev, scancode, &keycode);
  230. if (keycode != KEY_RESERVED)
  231. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  232. dev->name, scancode, keycode);
  233. return keycode;
  234. }
  235. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  236. /**
  237. * ir_keyup() - generates input event to cleanup a key press
  238. * @ir: the struct ir_input_dev descriptor of the device
  239. *
  240. * This routine is used to signal that a key has been released on the
  241. * remote control. It reports a keyup input event via input_report_key().
  242. */
  243. static void ir_keyup(struct ir_input_dev *ir)
  244. {
  245. if (!ir->keypressed)
  246. return;
  247. IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
  248. input_report_key(ir->input_dev, ir->last_keycode, 0);
  249. input_sync(ir->input_dev);
  250. ir->keypressed = false;
  251. }
  252. /**
  253. * ir_timer_keyup() - generates a keyup event after a timeout
  254. * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
  255. *
  256. * This routine will generate a keyup event some time after a keydown event
  257. * is generated when no further activity has been detected.
  258. */
  259. static void ir_timer_keyup(unsigned long cookie)
  260. {
  261. struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
  262. unsigned long flags;
  263. /*
  264. * ir->keyup_jiffies is used to prevent a race condition if a
  265. * hardware interrupt occurs at this point and the keyup timer
  266. * event is moved further into the future as a result.
  267. *
  268. * The timer will then be reactivated and this function called
  269. * again in the future. We need to exit gracefully in that case
  270. * to allow the input subsystem to do its auto-repeat magic or
  271. * a keyup event might follow immediately after the keydown.
  272. */
  273. spin_lock_irqsave(&ir->keylock, flags);
  274. if (time_is_after_eq_jiffies(ir->keyup_jiffies))
  275. ir_keyup(ir);
  276. spin_unlock_irqrestore(&ir->keylock, flags);
  277. }
  278. /**
  279. * ir_repeat() - notifies the IR core that a key is still pressed
  280. * @dev: the struct input_dev descriptor of the device
  281. *
  282. * This routine is used by IR decoders when a repeat message which does
  283. * not include the necessary bits to reproduce the scancode has been
  284. * received.
  285. */
  286. void ir_repeat(struct input_dev *dev)
  287. {
  288. unsigned long flags;
  289. struct ir_input_dev *ir = input_get_drvdata(dev);
  290. spin_lock_irqsave(&ir->keylock, flags);
  291. if (!ir->keypressed)
  292. goto out;
  293. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  294. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  295. out:
  296. spin_unlock_irqrestore(&ir->keylock, flags);
  297. }
  298. EXPORT_SYMBOL_GPL(ir_repeat);
  299. /**
  300. * ir_keydown() - generates input event for a key press
  301. * @dev: the struct input_dev descriptor of the device
  302. * @scancode: the scancode that we're seeking
  303. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  304. * support toggle values, this should be set to zero)
  305. *
  306. * This routine is used by the input routines when a key is pressed at the
  307. * IR. It gets the keycode for a scancode and reports an input event via
  308. * input_report_key().
  309. */
  310. void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
  311. {
  312. unsigned long flags;
  313. struct ir_input_dev *ir = input_get_drvdata(dev);
  314. u32 keycode = ir_g_keycode_from_table(dev, scancode);
  315. spin_lock_irqsave(&ir->keylock, flags);
  316. /* Repeat event? */
  317. if (ir->keypressed &&
  318. ir->last_scancode == scancode &&
  319. ir->last_toggle == toggle)
  320. goto set_timer;
  321. /* Release old keypress */
  322. ir_keyup(ir);
  323. ir->last_scancode = scancode;
  324. ir->last_toggle = toggle;
  325. ir->last_keycode = keycode;
  326. if (keycode == KEY_RESERVED)
  327. goto out;
  328. /* Register a keypress */
  329. ir->keypressed = true;
  330. IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
  331. dev->name, keycode, scancode);
  332. input_report_key(dev, ir->last_keycode, 1);
  333. input_sync(dev);
  334. set_timer:
  335. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  336. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  337. out:
  338. spin_unlock_irqrestore(&ir->keylock, flags);
  339. }
  340. EXPORT_SYMBOL_GPL(ir_keydown);
  341. static int ir_open(struct input_dev *input_dev)
  342. {
  343. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  344. return ir_dev->props->open(ir_dev->props->priv);
  345. }
  346. static void ir_close(struct input_dev *input_dev)
  347. {
  348. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  349. ir_dev->props->close(ir_dev->props->priv);
  350. }
  351. /**
  352. * __ir_input_register() - sets the IR keycode table and add the handlers
  353. * for keymap table get/set
  354. * @input_dev: the struct input_dev descriptor of the device
  355. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  356. *
  357. * This routine is used to initialize the input infrastructure
  358. * to work with an IR.
  359. * It will register the input/evdev interface for the device and
  360. * register the syfs code for IR class
  361. */
  362. int __ir_input_register(struct input_dev *input_dev,
  363. const struct ir_scancode_table *rc_tab,
  364. const struct ir_dev_props *props,
  365. const char *driver_name)
  366. {
  367. struct ir_input_dev *ir_dev;
  368. int rc;
  369. if (rc_tab->scan == NULL || !rc_tab->size)
  370. return -EINVAL;
  371. ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
  372. if (!ir_dev)
  373. return -ENOMEM;
  374. ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
  375. if (!ir_dev->driver_name) {
  376. rc = -ENOMEM;
  377. goto out_dev;
  378. }
  379. input_dev->getkeycode = ir_getkeycode;
  380. input_dev->setkeycode = ir_setkeycode;
  381. input_set_drvdata(input_dev, ir_dev);
  382. ir_dev->input_dev = input_dev;
  383. spin_lock_init(&ir_dev->rc_tab.lock);
  384. spin_lock_init(&ir_dev->keylock);
  385. setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
  386. ir_dev->rc_tab.name = rc_tab->name;
  387. ir_dev->rc_tab.ir_type = rc_tab->ir_type;
  388. ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size *
  389. sizeof(struct ir_scancode));
  390. ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL);
  391. ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode);
  392. if (!ir_dev->rc_tab.scan) {
  393. rc = -ENOMEM;
  394. goto out_name;
  395. }
  396. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  397. ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
  398. set_bit(EV_KEY, input_dev->evbit);
  399. set_bit(EV_REP, input_dev->evbit);
  400. if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
  401. rc = -ENOMEM;
  402. goto out_table;
  403. }
  404. ir_dev->props = props;
  405. if (props && props->open)
  406. input_dev->open = ir_open;
  407. if (props && props->close)
  408. input_dev->close = ir_close;
  409. rc = ir_register_class(input_dev);
  410. if (rc < 0)
  411. goto out_table;
  412. IR_dprintk(1, "Registered input device on %s for %s remote.\n",
  413. driver_name, rc_tab->name);
  414. return 0;
  415. out_table:
  416. kfree(ir_dev->rc_tab.scan);
  417. out_name:
  418. kfree(ir_dev->driver_name);
  419. out_dev:
  420. kfree(ir_dev);
  421. return rc;
  422. }
  423. EXPORT_SYMBOL_GPL(__ir_input_register);
  424. /**
  425. * ir_input_unregister() - unregisters IR and frees resources
  426. * @input_dev: the struct input_dev descriptor of the device
  427. * This routine is used to free memory and de-register interfaces.
  428. */
  429. void ir_input_unregister(struct input_dev *dev)
  430. {
  431. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  432. struct ir_scancode_table *rc_tab;
  433. if (!ir_dev)
  434. return;
  435. IR_dprintk(1, "Freed keycode table\n");
  436. del_timer_sync(&ir_dev->timer_keyup);
  437. rc_tab = &ir_dev->rc_tab;
  438. rc_tab->size = 0;
  439. kfree(rc_tab->scan);
  440. rc_tab->scan = NULL;
  441. ir_unregister_class(dev);
  442. kfree(ir_dev->driver_name);
  443. kfree(ir_dev);
  444. }
  445. EXPORT_SYMBOL_GPL(ir_input_unregister);
  446. int ir_core_debug; /* ir_debug level (0,1,2) */
  447. EXPORT_SYMBOL_GPL(ir_core_debug);
  448. module_param_named(debug, ir_core_debug, int, 0644);
  449. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  450. MODULE_LICENSE("GPL");