ir-keytable.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. #define IR_TAB_MIN_SIZE 32
  18. #define IR_TAB_MAX_SIZE 1024
  19. /**
  20. * ir_seek_table() - returns the element order on the table
  21. * @rc_tab: the ir_scancode_table with the keymap to be used
  22. * @scancode: the scancode that we're seeking
  23. *
  24. * This routine is used by the input routines when a key is pressed at the
  25. * IR. The scancode is received and needs to be converted into a keycode.
  26. * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
  27. * corresponding keycode from the table.
  28. */
  29. static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode)
  30. {
  31. int rc;
  32. unsigned long flags;
  33. struct ir_scancode *keymap = rc_tab->scan;
  34. spin_lock_irqsave(&rc_tab->lock, flags);
  35. /* FIXME: replace it by a binary search */
  36. for (rc = 0; rc < rc_tab->size; rc++)
  37. if (keymap[rc].scancode == scancode)
  38. goto exit;
  39. /* Not found */
  40. rc = -EINVAL;
  41. exit:
  42. spin_unlock_irqrestore(&rc_tab->lock, flags);
  43. return rc;
  44. }
  45. /**
  46. * ir_roundup_tablesize() - gets an optimum value for the table size
  47. * @n_elems: minimum number of entries to store keycodes
  48. *
  49. * This routine is used to choose the keycode table size.
  50. *
  51. * In order to have some empty space for new keycodes,
  52. * and knowing in advance that kmalloc allocates only power of two
  53. * segments, it optimizes the allocated space to have some spare space
  54. * for those new keycodes by using the maximum number of entries that
  55. * will be effectively be allocated by kmalloc.
  56. * In order to reduce the quantity of table resizes, it has a minimum
  57. * table size of IR_TAB_MIN_SIZE.
  58. */
  59. static int ir_roundup_tablesize(int n_elems)
  60. {
  61. size_t size;
  62. if (n_elems < IR_TAB_MIN_SIZE)
  63. n_elems = IR_TAB_MIN_SIZE;
  64. /*
  65. * As kmalloc only allocates sizes of power of two, get as
  66. * much entries as possible for the allocated memory segment
  67. */
  68. size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode));
  69. n_elems = size / sizeof(struct ir_scancode);
  70. return n_elems;
  71. }
  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. * Also copies table size and table protocol.
  79. * NOTE: It shouldn't copy the lock field
  80. */
  81. static int ir_copy_table(struct ir_scancode_table *destin,
  82. const struct ir_scancode_table *origin)
  83. {
  84. int i, j = 0;
  85. for (i = 0; i < origin->size; i++) {
  86. if (origin->scan[i].keycode == KEY_UNKNOWN ||
  87. origin->scan[i].keycode == KEY_RESERVED)
  88. continue;
  89. memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
  90. j++;
  91. }
  92. destin->size = j;
  93. destin->ir_type = origin->ir_type;
  94. IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
  95. return 0;
  96. }
  97. /**
  98. * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
  99. * @dev: the struct input_dev device descriptor
  100. * @scancode: the desired scancode
  101. * @keycode: the keycode to be retorned.
  102. *
  103. * This routine is used to handle evdev EVIOCGKEY ioctl.
  104. * If the key is not found, returns -EINVAL, otherwise, returns 0.
  105. */
  106. static int ir_getkeycode(struct input_dev *dev,
  107. unsigned int scancode, unsigned int *keycode)
  108. {
  109. int elem;
  110. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  111. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  112. elem = ir_seek_table(rc_tab, scancode);
  113. if (elem >= 0) {
  114. *keycode = rc_tab->scan[elem].keycode;
  115. return 0;
  116. }
  117. /*
  118. * Scancode not found and table can't be expanded
  119. */
  120. if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
  121. return -EINVAL;
  122. /*
  123. * If is there extra space, returns KEY_RESERVED,
  124. * otherwise, input core won't let ir_setkeycode to work
  125. */
  126. *keycode = KEY_RESERVED;
  127. return 0;
  128. }
  129. /**
  130. * ir_is_resize_needed() - Check if the table needs rezise
  131. * @table: keycode table that may need to resize
  132. * @n_elems: minimum number of entries to store keycodes
  133. *
  134. * Considering that kmalloc uses power of two storage areas, this
  135. * routine detects if the real alloced size will change. If not, it
  136. * just returns without doing nothing. Otherwise, it will extend or
  137. * reduce the table size to meet the new needs.
  138. *
  139. * It returns 0 if no resize is needed, 1 otherwise.
  140. */
  141. static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
  142. {
  143. int cur_size = ir_roundup_tablesize(table->size);
  144. int new_size = ir_roundup_tablesize(n_elems);
  145. if (cur_size == new_size)
  146. return 0;
  147. /* Resize is needed */
  148. return 1;
  149. }
  150. /**
  151. * ir_delete_key() - remove a keycode from the table
  152. * @rc_tab: keycode table
  153. * @elem: element to be removed
  154. *
  155. */
  156. static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
  157. {
  158. unsigned long flags = 0;
  159. int newsize = rc_tab->size - 1;
  160. int resize = ir_is_resize_needed(rc_tab, newsize);
  161. struct ir_scancode *oldkeymap = rc_tab->scan;
  162. struct ir_scancode *newkeymap = NULL;
  163. if (resize)
  164. newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
  165. sizeof(*newkeymap), GFP_ATOMIC);
  166. /* There's no memory for resize. Keep the old table */
  167. if (!resize || !newkeymap) {
  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. unsigned int scancode, unsigned int keycode)
  255. {
  256. int rc = 0;
  257. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  258. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  259. struct ir_scancode *keymap = rc_tab->scan;
  260. unsigned long flags;
  261. /*
  262. * Handle keycode table deletions
  263. *
  264. * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
  265. * deal as a trial to remove an existing scancode attribution
  266. * if table become too big, reduce it to save space
  267. */
  268. if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
  269. rc = ir_seek_table(rc_tab, scancode);
  270. if (rc < 0)
  271. return 0;
  272. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
  273. clear_bit(keymap[rc].keycode, dev->keybit);
  274. ir_delete_key(rc_tab, rc);
  275. return 0;
  276. }
  277. /*
  278. * Handle keycode replacements
  279. *
  280. * If the scancode exists, just replace by the new value
  281. */
  282. rc = ir_seek_table(rc_tab, scancode);
  283. if (rc >= 0) {
  284. IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
  285. rc, scancode, keycode);
  286. clear_bit(keymap[rc].keycode, dev->keybit);
  287. spin_lock_irqsave(&rc_tab->lock, flags);
  288. keymap[rc].keycode = keycode;
  289. spin_unlock_irqrestore(&rc_tab->lock, flags);
  290. set_bit(keycode, dev->keybit);
  291. return 0;
  292. }
  293. /*
  294. * Handle new scancode inserts
  295. *
  296. * reallocate table if needed and insert a new keycode
  297. */
  298. /* Avoid growing the table indefinitely */
  299. if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
  300. return -EINVAL;
  301. rc = ir_insert_key(rc_tab, scancode, keycode);
  302. if (rc < 0)
  303. return rc;
  304. set_bit(keycode, dev->keybit);
  305. return 0;
  306. }
  307. /**
  308. * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  309. * @input_dev: the struct input_dev descriptor of the device
  310. * @scancode: the scancode that we're seeking
  311. *
  312. * This routine is used by the input routines when a key is pressed at the
  313. * IR. The scancode is received and needs to be converted into a keycode.
  314. * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
  315. * corresponding keycode from the table.
  316. */
  317. u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
  318. {
  319. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  320. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  321. struct ir_scancode *keymap = rc_tab->scan;
  322. int elem;
  323. elem = ir_seek_table(rc_tab, scancode);
  324. if (elem >= 0) {
  325. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  326. dev->name, scancode, keymap[elem].keycode);
  327. return rc_tab->scan[elem].keycode;
  328. }
  329. printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
  330. dev->name, scancode);
  331. /* Reports userspace that an unknown keycode were got */
  332. return KEY_RESERVED;
  333. }
  334. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  335. /**
  336. * ir_input_register() - sets the IR keycode table and add the handlers
  337. * for keymap table get/set
  338. * @input_dev: the struct input_dev descriptor of the device
  339. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  340. *
  341. * This routine is used to initialize the input infrastructure
  342. * to work with an IR.
  343. * It will register the input/evdev interface for the device and
  344. * register the syfs code for IR class
  345. */
  346. int ir_input_register(struct input_dev *input_dev,
  347. const struct ir_scancode_table *rc_tab,
  348. const struct ir_dev_props *props)
  349. {
  350. struct ir_input_dev *ir_dev;
  351. struct ir_scancode *keymap = rc_tab->scan;
  352. int i, rc;
  353. if (rc_tab->scan == NULL || !rc_tab->size)
  354. return -EINVAL;
  355. ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
  356. if (!ir_dev)
  357. return -ENOMEM;
  358. spin_lock_init(&ir_dev->rc_tab.lock);
  359. ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size);
  360. ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size *
  361. sizeof(struct ir_scancode), GFP_KERNEL);
  362. if (!ir_dev->rc_tab.scan) {
  363. kfree(ir_dev);
  364. return -ENOMEM;
  365. }
  366. IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
  367. ir_dev->rc_tab.size,
  368. ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan));
  369. ir_copy_table(&ir_dev->rc_tab, rc_tab);
  370. ir_dev->props = props;
  371. /* set the bits for the keys */
  372. IR_dprintk(1, "key map size: %d\n", rc_tab->size);
  373. for (i = 0; i < rc_tab->size; i++) {
  374. IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
  375. i, keymap[i].keycode);
  376. set_bit(keymap[i].keycode, input_dev->keybit);
  377. }
  378. clear_bit(0, input_dev->keybit);
  379. set_bit(EV_KEY, input_dev->evbit);
  380. input_dev->getkeycode = ir_getkeycode;
  381. input_dev->setkeycode = ir_setkeycode;
  382. input_set_drvdata(input_dev, ir_dev);
  383. rc = input_register_device(input_dev);
  384. if (rc < 0)
  385. goto err;
  386. rc = ir_register_class(input_dev);
  387. if (rc < 0) {
  388. input_unregister_device(input_dev);
  389. goto err;
  390. }
  391. return 0;
  392. err:
  393. kfree(rc_tab->scan);
  394. kfree(ir_dev);
  395. input_set_drvdata(input_dev, NULL);
  396. return rc;
  397. }
  398. EXPORT_SYMBOL_GPL(ir_input_register);
  399. /**
  400. * ir_input_unregister() - unregisters IR and frees resources
  401. * @input_dev: the struct input_dev descriptor of the device
  402. * This routine is used to free memory and de-register interfaces.
  403. */
  404. void ir_input_unregister(struct input_dev *dev)
  405. {
  406. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  407. struct ir_scancode_table *rc_tab;
  408. if (!ir_dev)
  409. return;
  410. IR_dprintk(1, "Freed keycode table\n");
  411. rc_tab = &ir_dev->rc_tab;
  412. rc_tab->size = 0;
  413. kfree(rc_tab->scan);
  414. rc_tab->scan = NULL;
  415. ir_unregister_class(dev);
  416. kfree(ir_dev);
  417. input_unregister_device(dev);
  418. }
  419. EXPORT_SYMBOL_GPL(ir_input_unregister);
  420. int ir_core_debug; /* ir_debug level (0,1,2) */
  421. EXPORT_SYMBOL_GPL(ir_core_debug);
  422. module_param_named(debug, ir_core_debug, int, 0644);
  423. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  424. MODULE_LICENSE("GPL");