ir-keytable.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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/usb/input.h>
  15. #include <media/ir-common.h>
  16. #define IR_TAB_MIN_SIZE 32
  17. #define IR_TAB_MAX_SIZE 1024
  18. /**
  19. * ir_seek_table() - returns the element order on the table
  20. * @rc_tab: the ir_scancode_table with the keymap to be used
  21. * @scancode: the scancode that we're seeking
  22. *
  23. * This routine is used by the input routines when a key is pressed at the
  24. * IR. The scancode is received and needs to be converted into a keycode.
  25. * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
  26. * corresponding keycode from the table.
  27. */
  28. static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode)
  29. {
  30. int rc;
  31. unsigned long flags;
  32. struct ir_scancode *keymap = rc_tab->scan;
  33. spin_lock_irqsave(&rc_tab->lock, flags);
  34. /* FIXME: replace it by a binary search */
  35. for (rc = 0; rc < rc_tab->size; rc++)
  36. if (keymap[rc].scancode == scancode)
  37. goto exit;
  38. /* Not found */
  39. rc = -EINVAL;
  40. exit:
  41. spin_unlock_irqrestore(&rc_tab->lock, flags);
  42. return rc;
  43. }
  44. /**
  45. * ir_roundup_tablesize() - gets an optimum value for the table size
  46. * @n_elems: minimum number of entries to store keycodes
  47. *
  48. * This routine is used to choose the keycode table size.
  49. *
  50. * In order to have some empty space for new keycodes,
  51. * and knowing in advance that kmalloc allocates only power of two
  52. * segments, it optimizes the allocated space to have some spare space
  53. * for those new keycodes by using the maximum number of entries that
  54. * will be effectively be allocated by kmalloc.
  55. * In order to reduce the quantity of table resizes, it has a minimum
  56. * table size of IR_TAB_MIN_SIZE.
  57. */
  58. int ir_roundup_tablesize(int n_elems)
  59. {
  60. size_t size;
  61. if (n_elems < IR_TAB_MIN_SIZE)
  62. n_elems = IR_TAB_MIN_SIZE;
  63. /*
  64. * As kmalloc only allocates sizes of power of two, get as
  65. * much entries as possible for the allocated memory segment
  66. */
  67. size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
  68. n_elems = size / sizeof(struct ir_scancode);
  69. return n_elems;
  70. }
  71. EXPORT_SYMBOL_GPL(ir_roundup_tablesize);
  72. /**
  73. * ir_copy_table() - copies a keytable, discarding the unused entries
  74. * @destin: destin table
  75. * @origin: origin table
  76. *
  77. * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
  78. */
  79. int ir_copy_table(struct ir_scancode_table *destin,
  80. const struct ir_scancode_table *origin)
  81. {
  82. int i, j = 0;
  83. for (i = 0; i < origin->size; i++) {
  84. if (origin->scan[i].keycode == KEY_UNKNOWN ||
  85. origin->scan[i].keycode == KEY_RESERVED)
  86. continue;
  87. memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
  88. j++;
  89. }
  90. destin->size = j;
  91. IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
  92. return 0;
  93. }
  94. EXPORT_SYMBOL_GPL(ir_copy_table);
  95. /**
  96. * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
  97. * @dev: the struct input_dev device descriptor
  98. * @scancode: the desired scancode
  99. * @keycode: the keycode to be retorned.
  100. *
  101. * This routine is used to handle evdev EVIOCGKEY ioctl.
  102. * If the key is not found, returns -EINVAL, otherwise, returns 0.
  103. */
  104. static int ir_getkeycode(struct input_dev *dev,
  105. int scancode, int *keycode)
  106. {
  107. int elem;
  108. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  109. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  110. elem = ir_seek_table(rc_tab, scancode);
  111. if (elem >= 0) {
  112. *keycode = rc_tab->scan[elem].keycode;
  113. return 0;
  114. }
  115. /*
  116. * Scancode not found and table can't be expanded
  117. */
  118. if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
  119. return -EINVAL;
  120. /*
  121. * If is there extra space, returns KEY_RESERVED,
  122. * otherwise, input core won't let ir_setkeycode to work
  123. */
  124. *keycode = KEY_RESERVED;
  125. return 0;
  126. }
  127. /**
  128. * ir_is_resize_needed() - Check if the table needs rezise
  129. * @table: keycode table that may need to resize
  130. * @n_elems: minimum number of entries to store keycodes
  131. *
  132. * Considering that kmalloc uses power of two storage areas, this
  133. * routine detects if the real alloced size will change. If not, it
  134. * just returns without doing nothing. Otherwise, it will extend or
  135. * reduce the table size to meet the new needs.
  136. *
  137. * It returns 0 if no resize is needed, 1 otherwise.
  138. */
  139. static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
  140. {
  141. int cur_size = ir_roundup_tablesize(table->size);
  142. int new_size = ir_roundup_tablesize(n_elems);
  143. if (cur_size == new_size)
  144. return 0;
  145. /* Resize is needed */
  146. return 1;
  147. }
  148. /**
  149. * ir_delete_key() - remove a keycode from the table
  150. * @rc_tab: keycode table
  151. * @elem: element to be removed
  152. *
  153. */
  154. static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
  155. {
  156. unsigned long flags = 0;
  157. int newsize = rc_tab->size - 1;
  158. int resize = ir_is_resize_needed(rc_tab, newsize);
  159. struct ir_scancode *oldkeymap = rc_tab->scan;
  160. struct ir_scancode *newkeymap;
  161. if (resize) {
  162. newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
  163. sizeof(*newkeymap), GFP_ATOMIC);
  164. /* There's no memory for resize. Keep the old table */
  165. if (!newkeymap)
  166. resize = 0;
  167. }
  168. if (!resize) {
  169. newkeymap = oldkeymap;
  170. /* We'll modify the live table. Lock it */
  171. spin_lock_irqsave(&rc_tab->lock, flags);
  172. }
  173. /*
  174. * Copy the elements before the one that will be deleted
  175. * if (!resize), both oldkeymap and newkeymap points
  176. * to the same place, so, there's no need to copy
  177. */
  178. if (resize && elem > 0)
  179. memcpy(newkeymap, oldkeymap,
  180. elem * sizeof(*newkeymap));
  181. /*
  182. * Copy the other elements overwriting the element to be removed
  183. * This operation applies to both resize and non-resize case
  184. */
  185. if (elem < newsize)
  186. memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
  187. (newsize - elem) * sizeof(*newkeymap));
  188. if (resize) {
  189. /*
  190. * As the copy happened to a temporary table, only here
  191. * it needs to lock while replacing the table pointers
  192. * to use the new table
  193. */
  194. spin_lock_irqsave(&rc_tab->lock, flags);
  195. rc_tab->size = newsize;
  196. rc_tab->scan = newkeymap;
  197. spin_unlock_irqrestore(&rc_tab->lock, flags);
  198. /* Frees the old keytable */
  199. kfree(oldkeymap);
  200. } else {
  201. rc_tab->size = newsize;
  202. spin_unlock_irqrestore(&rc_tab->lock, flags);
  203. }
  204. }
  205. /**
  206. * ir_insert_key() - insert a keycode at the table
  207. * @rc_tab: keycode table
  208. * @scancode: the desired scancode
  209. * @keycode: the keycode to be retorned.
  210. *
  211. */
  212. static int ir_insert_key(struct ir_scancode_table *rc_tab,
  213. int scancode, int keycode)
  214. {
  215. unsigned long flags;
  216. int elem = rc_tab->size;
  217. int newsize = rc_tab->size + 1;
  218. int resize = ir_is_resize_needed(rc_tab, newsize);
  219. struct ir_scancode *oldkeymap = rc_tab->scan;
  220. struct ir_scancode *newkeymap;
  221. if (resize) {
  222. newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
  223. sizeof(*newkeymap), GFP_ATOMIC);
  224. if (!newkeymap)
  225. return -ENOMEM;
  226. memcpy(newkeymap, oldkeymap,
  227. rc_tab->size * sizeof(*newkeymap));
  228. } else
  229. newkeymap = oldkeymap;
  230. /* Stores the new code at the table */
  231. IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
  232. rc_tab->size, scancode, keycode);
  233. spin_lock_irqsave(&rc_tab->lock, flags);
  234. rc_tab->size = newsize;
  235. if (resize) {
  236. rc_tab->scan = newkeymap;
  237. kfree(oldkeymap);
  238. }
  239. newkeymap[elem].scancode = scancode;
  240. newkeymap[elem].keycode = keycode;
  241. spin_unlock_irqrestore(&rc_tab->lock, flags);
  242. return 0;
  243. }
  244. /**
  245. * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
  246. * @dev: the struct input_dev device descriptor
  247. * @scancode: the desired scancode
  248. * @keycode: the keycode to be retorned.
  249. *
  250. * This routine is used to handle evdev EVIOCSKEY ioctl.
  251. * There's one caveat here: how can we increase the size of the table?
  252. * If the key is not found, returns -EINVAL, otherwise, returns 0.
  253. */
  254. static int ir_setkeycode(struct input_dev *dev,
  255. int scancode, int keycode)
  256. {
  257. int rc = 0;
  258. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  259. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  260. struct ir_scancode *keymap = rc_tab->scan;
  261. unsigned long flags;
  262. /*
  263. * Handle keycode table deletions
  264. *
  265. * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
  266. * deal as a trial to remove an existing scancode attribution
  267. * if table become too big, reduce it to save space
  268. */
  269. if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
  270. rc = ir_seek_table(rc_tab, scancode);
  271. if (rc < 0)
  272. return 0;
  273. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
  274. clear_bit(keymap[rc].keycode, dev->keybit);
  275. ir_delete_key(rc_tab, rc);
  276. return 0;
  277. }
  278. /*
  279. * Handle keycode replacements
  280. *
  281. * If the scancode exists, just replace by the new value
  282. */
  283. rc = ir_seek_table(rc_tab, scancode);
  284. if (rc >= 0) {
  285. IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
  286. rc, scancode, keycode);
  287. clear_bit(keymap[rc].keycode, dev->keybit);
  288. spin_lock_irqsave(&rc_tab->lock, flags);
  289. keymap[rc].keycode = keycode;
  290. spin_unlock_irqrestore(&rc_tab->lock, flags);
  291. set_bit(keycode, dev->keybit);
  292. return 0;
  293. }
  294. /*
  295. * Handle new scancode inserts
  296. *
  297. * reallocate table if needed and insert a new keycode
  298. */
  299. /* Avoid growing the table indefinitely */
  300. if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
  301. return -EINVAL;
  302. rc = ir_insert_key(rc_tab, scancode, keycode);
  303. if (rc < 0)
  304. return rc;
  305. set_bit(keycode, dev->keybit);
  306. return 0;
  307. }
  308. /**
  309. * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  310. * @input_dev: the struct input_dev descriptor of the device
  311. * @scancode: the scancode that we're seeking
  312. *
  313. * This routine is used by the input routines when a key is pressed at the
  314. * IR. The scancode is received and needs to be converted into a keycode.
  315. * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
  316. * corresponding keycode from the table.
  317. */
  318. u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
  319. {
  320. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  321. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  322. struct ir_scancode *keymap = rc_tab->scan;
  323. int elem;
  324. elem = ir_seek_table(rc_tab, scancode);
  325. if (elem >= 0) {
  326. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  327. dev->name, scancode, keymap[elem].keycode);
  328. return rc_tab->scan[elem].keycode;
  329. }
  330. printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
  331. dev->name, scancode);
  332. /* Reports userspace that an unknown keycode were got */
  333. return KEY_RESERVED;
  334. }
  335. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  336. /**
  337. * ir_input_register() - sets the IR keycode table and add the handlers
  338. * for keymap table get/set
  339. * @input_dev: the struct input_dev descriptor of the device
  340. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  341. *
  342. * This routine is used to initialize the input infrastructure to work with
  343. * an IR.
  344. * It should be called before registering the IR device.
  345. */
  346. int ir_input_register(struct input_dev *input_dev,
  347. struct ir_scancode_table *rc_tab)
  348. {
  349. struct ir_input_dev *ir_dev;
  350. struct ir_scancode *keymap = rc_tab->scan;
  351. int i, rc;
  352. if (rc_tab->scan == NULL || !rc_tab->size)
  353. return -EINVAL;
  354. ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
  355. if (!ir_dev)
  356. return -ENOMEM;
  357. spin_lock_init(&rc_tab->lock);
  358. ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size);
  359. ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size *
  360. sizeof(struct ir_scancode), GFP_KERNEL);
  361. if (!ir_dev->rc_tab.scan)
  362. return -ENOMEM;
  363. IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
  364. ir_dev->rc_tab.size,
  365. ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan));
  366. ir_copy_table(&ir_dev->rc_tab, rc_tab);
  367. /* set the bits for the keys */
  368. IR_dprintk(1, "key map size: %d\n", rc_tab->size);
  369. for (i = 0; i < rc_tab->size; i++) {
  370. IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
  371. i, keymap[i].keycode);
  372. set_bit(keymap[i].keycode, input_dev->keybit);
  373. }
  374. clear_bit(0, input_dev->keybit);
  375. set_bit(EV_KEY, input_dev->evbit);
  376. input_dev->getkeycode = ir_getkeycode;
  377. input_dev->setkeycode = ir_setkeycode;
  378. input_set_drvdata(input_dev, ir_dev);
  379. rc = input_register_device(input_dev);
  380. if (rc < 0) {
  381. kfree(rc_tab->scan);
  382. kfree(ir_dev);
  383. input_set_drvdata(input_dev, NULL);
  384. }
  385. return rc;
  386. }
  387. EXPORT_SYMBOL_GPL(ir_input_register);
  388. void ir_input_unregister(struct input_dev *dev)
  389. {
  390. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  391. struct ir_scancode_table *rc_tab;
  392. if (!ir_dev)
  393. return;
  394. IR_dprintk(1, "Freed keycode table\n");
  395. rc_tab = &ir_dev->rc_tab;
  396. rc_tab->size = 0;
  397. kfree(rc_tab->scan);
  398. rc_tab->scan = NULL;
  399. kfree(ir_dev);
  400. input_unregister_device(dev);
  401. }
  402. EXPORT_SYMBOL_GPL(ir_input_unregister);
  403. int ir_core_debug; /* ir_debug level (0,1,2) */
  404. EXPORT_SYMBOL_GPL(ir_core_debug);
  405. module_param_named(debug, ir_core_debug, int, 0644);
  406. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  407. MODULE_LICENSE("GPL");