ir-keytable.c 11 KB

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