ir-keytable.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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_scancode_table *rc_tab = input_get_drvdata(dev);
  109. elem = ir_seek_table(rc_tab, scancode);
  110. if (elem >= 0) {
  111. *keycode = rc_tab->scan[elem].keycode;
  112. return 0;
  113. }
  114. /*
  115. * Scancode not found and table can't be expanded
  116. */
  117. if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
  118. return -EINVAL;
  119. /*
  120. * If is there extra space, returns KEY_RESERVED,
  121. * otherwise, input core won't let ir_setkeycode to work
  122. */
  123. *keycode = KEY_RESERVED;
  124. return 0;
  125. }
  126. /**
  127. * ir_is_resize_needed() - Check if the table needs rezise
  128. * @table: keycode table that may need to resize
  129. * @n_elems: minimum number of entries to store keycodes
  130. *
  131. * Considering that kmalloc uses power of two storage areas, this
  132. * routine detects if the real alloced size will change. If not, it
  133. * just returns without doing nothing. Otherwise, it will extend or
  134. * reduce the table size to meet the new needs.
  135. *
  136. * It returns 0 if no resize is needed, 1 otherwise.
  137. */
  138. static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
  139. {
  140. int cur_size = ir_roundup_tablesize(table->size);
  141. int new_size = ir_roundup_tablesize(n_elems);
  142. if (cur_size == new_size)
  143. return 0;
  144. /* Resize is needed */
  145. return 1;
  146. }
  147. /**
  148. * ir_delete_key() - remove a keycode from the table
  149. * @rc_tab: keycode table
  150. * @elem: element to be removed
  151. *
  152. */
  153. static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
  154. {
  155. unsigned long flags = 0;
  156. int newsize = rc_tab->size - 1;
  157. int resize = ir_is_resize_needed(rc_tab, newsize);
  158. struct ir_scancode *oldkeymap = rc_tab->scan;
  159. struct ir_scancode *newkeymap;
  160. if (resize) {
  161. newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
  162. sizeof(*newkeymap), GFP_ATOMIC);
  163. /* There's no memory for resize. Keep the old table */
  164. if (!newkeymap)
  165. resize = 0;
  166. }
  167. if (!resize) {
  168. newkeymap = oldkeymap;
  169. /* We'll modify the live table. Lock it */
  170. spin_lock_irqsave(&rc_tab->lock, flags);
  171. }
  172. /*
  173. * Copy the elements before the one that will be deleted
  174. * if (!resize), both oldkeymap and newkeymap points
  175. * to the same place, so, there's no need to copy
  176. */
  177. if (resize && elem > 0)
  178. memcpy(newkeymap, oldkeymap,
  179. elem * sizeof(*newkeymap));
  180. /*
  181. * Copy the other elements overwriting the element to be removed
  182. * This operation applies to both resize and non-resize case
  183. */
  184. if (elem < newsize)
  185. memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
  186. (newsize - elem) * sizeof(*newkeymap));
  187. if (resize) {
  188. /*
  189. * As the copy happened to a temporary table, only here
  190. * it needs to lock while replacing the table pointers
  191. * to use the new table
  192. */
  193. spin_lock_irqsave(&rc_tab->lock, flags);
  194. rc_tab->size = newsize;
  195. rc_tab->scan = newkeymap;
  196. spin_unlock_irqrestore(&rc_tab->lock, flags);
  197. /* Frees the old keytable */
  198. kfree(oldkeymap);
  199. } else {
  200. rc_tab->size = newsize;
  201. spin_unlock_irqrestore(&rc_tab->lock, flags);
  202. }
  203. }
  204. /**
  205. * ir_insert_key() - insert a keycode at the table
  206. * @rc_tab: keycode table
  207. * @scancode: the desired scancode
  208. * @keycode: the keycode to be retorned.
  209. *
  210. */
  211. static int ir_insert_key(struct ir_scancode_table *rc_tab,
  212. int scancode, int keycode)
  213. {
  214. unsigned long flags;
  215. int elem = rc_tab->size;
  216. int newsize = rc_tab->size + 1;
  217. int resize = ir_is_resize_needed(rc_tab, newsize);
  218. struct ir_scancode *oldkeymap = rc_tab->scan;
  219. struct ir_scancode *newkeymap;
  220. if (resize) {
  221. newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
  222. sizeof(*newkeymap), GFP_ATOMIC);
  223. if (!newkeymap)
  224. return -ENOMEM;
  225. memcpy(newkeymap, oldkeymap,
  226. rc_tab->size * sizeof(*newkeymap));
  227. } else
  228. newkeymap = oldkeymap;
  229. /* Stores the new code at the table */
  230. IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
  231. rc_tab->size, scancode, keycode);
  232. spin_lock_irqsave(&rc_tab->lock, flags);
  233. rc_tab->size = newsize;
  234. if (resize) {
  235. rc_tab->scan = newkeymap;
  236. kfree(oldkeymap);
  237. }
  238. newkeymap[elem].scancode = scancode;
  239. newkeymap[elem].keycode = keycode;
  240. spin_unlock_irqrestore(&rc_tab->lock, flags);
  241. return 0;
  242. }
  243. /**
  244. * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
  245. * @dev: the struct input_dev device descriptor
  246. * @scancode: the desired scancode
  247. * @keycode: the keycode to be retorned.
  248. *
  249. * This routine is used to handle evdev EVIOCSKEY ioctl.
  250. * There's one caveat here: how can we increase the size of the table?
  251. * If the key is not found, returns -EINVAL, otherwise, returns 0.
  252. */
  253. static int ir_setkeycode(struct input_dev *dev,
  254. int scancode, int keycode)
  255. {
  256. int rc = 0;
  257. struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
  258. struct ir_scancode *keymap = rc_tab->scan;
  259. unsigned long flags;
  260. /*
  261. * Handle keycode table deletions
  262. *
  263. * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
  264. * deal as a trial to remove an existing scancode attribution
  265. * if table become too big, reduce it to save space
  266. */
  267. if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
  268. rc = ir_seek_table(rc_tab, scancode);
  269. if (rc < 0)
  270. return 0;
  271. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
  272. clear_bit(keymap[rc].keycode, dev->keybit);
  273. ir_delete_key(rc_tab, rc);
  274. return 0;
  275. }
  276. /*
  277. * Handle keycode replacements
  278. *
  279. * If the scancode exists, just replace by the new value
  280. */
  281. rc = ir_seek_table(rc_tab, scancode);
  282. if (rc >= 0) {
  283. IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
  284. rc, scancode, keycode);
  285. clear_bit(keymap[rc].keycode, dev->keybit);
  286. spin_lock_irqsave(&rc_tab->lock, flags);
  287. keymap[rc].keycode = keycode;
  288. spin_unlock_irqrestore(&rc_tab->lock, flags);
  289. set_bit(keycode, dev->keybit);
  290. return 0;
  291. }
  292. /*
  293. * Handle new scancode inserts
  294. *
  295. * reallocate table if needed and insert a new keycode
  296. */
  297. /* Avoid growing the table indefinitely */
  298. if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
  299. return -EINVAL;
  300. rc = ir_insert_key(rc_tab, scancode, keycode);
  301. if (rc < 0)
  302. return rc;
  303. set_bit(keycode, dev->keybit);
  304. return 0;
  305. }
  306. /**
  307. * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  308. * @input_dev: the struct input_dev descriptor of the device
  309. * @scancode: the scancode that we're seeking
  310. *
  311. * This routine is used by the input routines when a key is pressed at the
  312. * IR. The scancode is received and needs to be converted into a keycode.
  313. * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
  314. * corresponding keycode from the table.
  315. */
  316. u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
  317. {
  318. struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
  319. struct ir_scancode *keymap = rc_tab->scan;
  320. int elem;
  321. elem = ir_seek_table(rc_tab, scancode);
  322. if (elem >= 0) {
  323. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  324. dev->name, scancode, keymap[elem].keycode);
  325. return rc_tab->scan[elem].keycode;
  326. }
  327. printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
  328. dev->name, scancode);
  329. /* Reports userspace that an unknown keycode were got */
  330. return KEY_RESERVED;
  331. }
  332. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  333. /**
  334. * ir_set_keycode_table() - sets the IR keycode table and add the handlers
  335. * for keymap table get/set
  336. * @input_dev: the struct input_dev descriptor of the device
  337. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  338. *
  339. * This routine is used to initialize the input infrastructure to work with
  340. * an IR.
  341. * It should be called before registering the IR device.
  342. */
  343. int ir_set_keycode_table(struct input_dev *input_dev,
  344. struct ir_scancode_table *rc_tab)
  345. {
  346. struct ir_scancode *keymap = rc_tab->scan;
  347. int i;
  348. spin_lock_init(&rc_tab->lock);
  349. if (rc_tab->scan == NULL || !rc_tab->size)
  350. return -EINVAL;
  351. /* set the bits for the keys */
  352. IR_dprintk(1, "key map size: %d\n", rc_tab->size);
  353. for (i = 0; i < rc_tab->size; i++) {
  354. IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
  355. i, keymap[i].keycode);
  356. set_bit(keymap[i].keycode, input_dev->keybit);
  357. }
  358. input_dev->getkeycode = ir_getkeycode;
  359. input_dev->setkeycode = ir_setkeycode;
  360. input_set_drvdata(input_dev, rc_tab);
  361. return 0;
  362. }
  363. EXPORT_SYMBOL_GPL(ir_set_keycode_table);
  364. void ir_input_free(struct input_dev *dev)
  365. {
  366. struct ir_scancode_table *rc_tab = input_get_drvdata(dev);
  367. if (!rc_tab)
  368. return;
  369. IR_dprintk(1, "Freed keycode table\n");
  370. rc_tab->size = 0;
  371. kfree(rc_tab->scan);
  372. rc_tab->scan = NULL;
  373. }
  374. EXPORT_SYMBOL_GPL(ir_input_free);
  375. int ir_core_debug; /* ir_debug level (0,1,2) */
  376. EXPORT_SYMBOL_GPL(ir_core_debug);
  377. module_param_named(debug, ir_core_debug, int, 0644);
  378. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  379. MODULE_LICENSE("GPL");