ir-keytable.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 <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. static 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. /**
  72. * ir_copy_table() - copies a keytable, discarding the unused entries
  73. * @destin: destin table
  74. * @origin: origin table
  75. *
  76. * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED
  77. * Also copies table size and table protocol.
  78. * NOTE: It shouldn't copy the lock field
  79. */
  80. static int ir_copy_table(struct ir_scancode_table *destin,
  81. const struct ir_scancode_table *origin)
  82. {
  83. int i, j = 0;
  84. for (i = 0; i < origin->size; i++) {
  85. if (origin->scan[i].keycode == KEY_UNKNOWN ||
  86. origin->scan[i].keycode == KEY_RESERVED)
  87. continue;
  88. memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode));
  89. j++;
  90. }
  91. destin->size = j;
  92. destin->ir_type = origin->ir_type;
  93. IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size);
  94. return 0;
  95. }
  96. /**
  97. * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table
  98. * @dev: the struct input_dev device descriptor
  99. * @scancode: the desired scancode
  100. * @keycode: the keycode to be retorned.
  101. *
  102. * This routine is used to handle evdev EVIOCGKEY ioctl.
  103. * If the key is not found, returns -EINVAL, otherwise, returns 0.
  104. */
  105. static int ir_getkeycode(struct input_dev *dev,
  106. int scancode, int *keycode)
  107. {
  108. int elem;
  109. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  110. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  111. elem = ir_seek_table(rc_tab, scancode);
  112. if (elem >= 0) {
  113. *keycode = rc_tab->scan[elem].keycode;
  114. return 0;
  115. }
  116. /*
  117. * Scancode not found and table can't be expanded
  118. */
  119. if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE)
  120. return -EINVAL;
  121. /*
  122. * If is there extra space, returns KEY_RESERVED,
  123. * otherwise, input core won't let ir_setkeycode to work
  124. */
  125. *keycode = KEY_RESERVED;
  126. return 0;
  127. }
  128. /**
  129. * ir_is_resize_needed() - Check if the table needs rezise
  130. * @table: keycode table that may need to resize
  131. * @n_elems: minimum number of entries to store keycodes
  132. *
  133. * Considering that kmalloc uses power of two storage areas, this
  134. * routine detects if the real alloced size will change. If not, it
  135. * just returns without doing nothing. Otherwise, it will extend or
  136. * reduce the table size to meet the new needs.
  137. *
  138. * It returns 0 if no resize is needed, 1 otherwise.
  139. */
  140. static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems)
  141. {
  142. int cur_size = ir_roundup_tablesize(table->size);
  143. int new_size = ir_roundup_tablesize(n_elems);
  144. if (cur_size == new_size)
  145. return 0;
  146. /* Resize is needed */
  147. return 1;
  148. }
  149. /**
  150. * ir_delete_key() - remove a keycode from the table
  151. * @rc_tab: keycode table
  152. * @elem: element to be removed
  153. *
  154. */
  155. static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem)
  156. {
  157. unsigned long flags = 0;
  158. int newsize = rc_tab->size - 1;
  159. int resize = ir_is_resize_needed(rc_tab, newsize);
  160. struct ir_scancode *oldkeymap = rc_tab->scan;
  161. struct ir_scancode *newkeymap = NULL;
  162. if (resize)
  163. newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
  164. sizeof(*newkeymap), GFP_ATOMIC);
  165. /* There's no memory for resize. Keep the old table */
  166. if (!resize || !newkeymap) {
  167. newkeymap = oldkeymap;
  168. /* We'll modify the live table. Lock it */
  169. spin_lock_irqsave(&rc_tab->lock, flags);
  170. }
  171. /*
  172. * Copy the elements before the one that will be deleted
  173. * if (!resize), both oldkeymap and newkeymap points
  174. * to the same place, so, there's no need to copy
  175. */
  176. if (resize && elem > 0)
  177. memcpy(newkeymap, oldkeymap,
  178. elem * sizeof(*newkeymap));
  179. /*
  180. * Copy the other elements overwriting the element to be removed
  181. * This operation applies to both resize and non-resize case
  182. */
  183. if (elem < newsize)
  184. memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
  185. (newsize - elem) * sizeof(*newkeymap));
  186. if (resize) {
  187. /*
  188. * As the copy happened to a temporary table, only here
  189. * it needs to lock while replacing the table pointers
  190. * to use the new table
  191. */
  192. spin_lock_irqsave(&rc_tab->lock, flags);
  193. rc_tab->size = newsize;
  194. rc_tab->scan = newkeymap;
  195. spin_unlock_irqrestore(&rc_tab->lock, flags);
  196. /* Frees the old keytable */
  197. kfree(oldkeymap);
  198. } else {
  199. rc_tab->size = newsize;
  200. spin_unlock_irqrestore(&rc_tab->lock, flags);
  201. }
  202. }
  203. /**
  204. * ir_insert_key() - insert a keycode at the table
  205. * @rc_tab: keycode table
  206. * @scancode: the desired scancode
  207. * @keycode: the keycode to be retorned.
  208. *
  209. */
  210. static int ir_insert_key(struct ir_scancode_table *rc_tab,
  211. int scancode, int keycode)
  212. {
  213. unsigned long flags;
  214. int elem = rc_tab->size;
  215. int newsize = rc_tab->size + 1;
  216. int resize = ir_is_resize_needed(rc_tab, newsize);
  217. struct ir_scancode *oldkeymap = rc_tab->scan;
  218. struct ir_scancode *newkeymap;
  219. if (resize) {
  220. newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
  221. sizeof(*newkeymap), GFP_ATOMIC);
  222. if (!newkeymap)
  223. return -ENOMEM;
  224. memcpy(newkeymap, oldkeymap,
  225. rc_tab->size * sizeof(*newkeymap));
  226. } else
  227. newkeymap = oldkeymap;
  228. /* Stores the new code at the table */
  229. IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
  230. rc_tab->size, scancode, keycode);
  231. spin_lock_irqsave(&rc_tab->lock, flags);
  232. rc_tab->size = newsize;
  233. if (resize) {
  234. rc_tab->scan = newkeymap;
  235. kfree(oldkeymap);
  236. }
  237. newkeymap[elem].scancode = scancode;
  238. newkeymap[elem].keycode = keycode;
  239. spin_unlock_irqrestore(&rc_tab->lock, flags);
  240. return 0;
  241. }
  242. /**
  243. * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
  244. * @dev: the struct input_dev device descriptor
  245. * @scancode: the desired scancode
  246. * @keycode: the keycode to be retorned.
  247. *
  248. * This routine is used to handle evdev EVIOCSKEY ioctl.
  249. * There's one caveat here: how can we increase the size of the table?
  250. * If the key is not found, returns -EINVAL, otherwise, returns 0.
  251. */
  252. static int ir_setkeycode(struct input_dev *dev,
  253. int scancode, int keycode)
  254. {
  255. int rc = 0;
  256. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  257. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  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_input_dev *ir_dev = input_get_drvdata(dev);
  319. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  320. struct ir_scancode *keymap = rc_tab->scan;
  321. int elem;
  322. elem = ir_seek_table(rc_tab, scancode);
  323. if (elem >= 0) {
  324. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  325. dev->name, scancode, keymap[elem].keycode);
  326. return rc_tab->scan[elem].keycode;
  327. }
  328. printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
  329. dev->name, scancode);
  330. /* Reports userspace that an unknown keycode were got */
  331. return KEY_RESERVED;
  332. }
  333. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  334. /**
  335. * ir_input_register() - sets the IR keycode table and add the handlers
  336. * for keymap table get/set
  337. * @input_dev: the struct input_dev descriptor of the device
  338. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  339. *
  340. * This routine is used to initialize the input infrastructure
  341. * to work with an IR.
  342. * It will register the input/evdev interface for the device and
  343. * register the syfs code for IR class
  344. */
  345. int ir_input_register(struct input_dev *input_dev,
  346. const struct ir_scancode_table *rc_tab,
  347. const struct ir_dev_props *props)
  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(&ir_dev->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. kfree(ir_dev);
  363. return -ENOMEM;
  364. }
  365. IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n",
  366. ir_dev->rc_tab.size,
  367. ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan));
  368. ir_copy_table(&ir_dev->rc_tab, rc_tab);
  369. ir_dev->props = props;
  370. /* set the bits for the keys */
  371. IR_dprintk(1, "key map size: %d\n", rc_tab->size);
  372. for (i = 0; i < rc_tab->size; i++) {
  373. IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n",
  374. i, keymap[i].keycode);
  375. set_bit(keymap[i].keycode, input_dev->keybit);
  376. }
  377. clear_bit(0, input_dev->keybit);
  378. set_bit(EV_KEY, input_dev->evbit);
  379. input_dev->getkeycode = ir_getkeycode;
  380. input_dev->setkeycode = ir_setkeycode;
  381. input_set_drvdata(input_dev, ir_dev);
  382. rc = input_register_device(input_dev);
  383. if (rc < 0)
  384. goto err;
  385. rc = ir_register_class(input_dev);
  386. if (rc < 0) {
  387. input_unregister_device(input_dev);
  388. goto err;
  389. }
  390. return 0;
  391. err:
  392. kfree(rc_tab->scan);
  393. kfree(ir_dev);
  394. input_set_drvdata(input_dev, NULL);
  395. return rc;
  396. }
  397. EXPORT_SYMBOL_GPL(ir_input_register);
  398. /**
  399. * ir_input_unregister() - unregisters IR and frees resources
  400. * @input_dev: the struct input_dev descriptor of the device
  401. * This routine is used to free memory and de-register interfaces.
  402. */
  403. void ir_input_unregister(struct input_dev *dev)
  404. {
  405. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  406. struct ir_scancode_table *rc_tab;
  407. if (!ir_dev)
  408. return;
  409. IR_dprintk(1, "Freed keycode table\n");
  410. rc_tab = &ir_dev->rc_tab;
  411. rc_tab->size = 0;
  412. kfree(rc_tab->scan);
  413. rc_tab->scan = NULL;
  414. ir_unregister_class(dev);
  415. kfree(ir_dev);
  416. input_unregister_device(dev);
  417. }
  418. EXPORT_SYMBOL_GPL(ir_input_unregister);
  419. int ir_core_debug; /* ir_debug level (0,1,2) */
  420. EXPORT_SYMBOL_GPL(ir_core_debug);
  421. module_param_named(debug, ir_core_debug, int, 0644);
  422. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  423. MODULE_LICENSE("GPL");