ir-keytable.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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;
  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 (!newkeymap)
  167. resize = 0;
  168. }
  169. if (!resize) {
  170. newkeymap = oldkeymap;
  171. /* We'll modify the live table. Lock it */
  172. spin_lock_irqsave(&rc_tab->lock, flags);
  173. }
  174. /*
  175. * Copy the elements before the one that will be deleted
  176. * if (!resize), both oldkeymap and newkeymap points
  177. * to the same place, so, there's no need to copy
  178. */
  179. if (resize && elem > 0)
  180. memcpy(newkeymap, oldkeymap,
  181. elem * sizeof(*newkeymap));
  182. /*
  183. * Copy the other elements overwriting the element to be removed
  184. * This operation applies to both resize and non-resize case
  185. */
  186. if (elem < newsize)
  187. memcpy(&newkeymap[elem], &oldkeymap[elem + 1],
  188. (newsize - elem) * sizeof(*newkeymap));
  189. if (resize) {
  190. /*
  191. * As the copy happened to a temporary table, only here
  192. * it needs to lock while replacing the table pointers
  193. * to use the new table
  194. */
  195. spin_lock_irqsave(&rc_tab->lock, flags);
  196. rc_tab->size = newsize;
  197. rc_tab->scan = newkeymap;
  198. spin_unlock_irqrestore(&rc_tab->lock, flags);
  199. /* Frees the old keytable */
  200. kfree(oldkeymap);
  201. } else {
  202. rc_tab->size = newsize;
  203. spin_unlock_irqrestore(&rc_tab->lock, flags);
  204. }
  205. }
  206. /**
  207. * ir_insert_key() - insert a keycode at the table
  208. * @rc_tab: keycode table
  209. * @scancode: the desired scancode
  210. * @keycode: the keycode to be retorned.
  211. *
  212. */
  213. static int ir_insert_key(struct ir_scancode_table *rc_tab,
  214. int scancode, int keycode)
  215. {
  216. unsigned long flags;
  217. int elem = rc_tab->size;
  218. int newsize = rc_tab->size + 1;
  219. int resize = ir_is_resize_needed(rc_tab, newsize);
  220. struct ir_scancode *oldkeymap = rc_tab->scan;
  221. struct ir_scancode *newkeymap;
  222. if (resize) {
  223. newkeymap = kzalloc(ir_roundup_tablesize(newsize) *
  224. sizeof(*newkeymap), GFP_ATOMIC);
  225. if (!newkeymap)
  226. return -ENOMEM;
  227. memcpy(newkeymap, oldkeymap,
  228. rc_tab->size * sizeof(*newkeymap));
  229. } else
  230. newkeymap = oldkeymap;
  231. /* Stores the new code at the table */
  232. IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
  233. rc_tab->size, scancode, keycode);
  234. spin_lock_irqsave(&rc_tab->lock, flags);
  235. rc_tab->size = newsize;
  236. if (resize) {
  237. rc_tab->scan = newkeymap;
  238. kfree(oldkeymap);
  239. }
  240. newkeymap[elem].scancode = scancode;
  241. newkeymap[elem].keycode = keycode;
  242. spin_unlock_irqrestore(&rc_tab->lock, flags);
  243. return 0;
  244. }
  245. /**
  246. * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table
  247. * @dev: the struct input_dev device descriptor
  248. * @scancode: the desired scancode
  249. * @keycode: the keycode to be retorned.
  250. *
  251. * This routine is used to handle evdev EVIOCSKEY ioctl.
  252. * There's one caveat here: how can we increase the size of the table?
  253. * If the key is not found, returns -EINVAL, otherwise, returns 0.
  254. */
  255. static int ir_setkeycode(struct input_dev *dev,
  256. int scancode, int keycode)
  257. {
  258. int rc = 0;
  259. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  260. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  261. struct ir_scancode *keymap = rc_tab->scan;
  262. unsigned long flags;
  263. /*
  264. * Handle keycode table deletions
  265. *
  266. * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED,
  267. * deal as a trial to remove an existing scancode attribution
  268. * if table become too big, reduce it to save space
  269. */
  270. if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) {
  271. rc = ir_seek_table(rc_tab, scancode);
  272. if (rc < 0)
  273. return 0;
  274. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode);
  275. clear_bit(keymap[rc].keycode, dev->keybit);
  276. ir_delete_key(rc_tab, rc);
  277. return 0;
  278. }
  279. /*
  280. * Handle keycode replacements
  281. *
  282. * If the scancode exists, just replace by the new value
  283. */
  284. rc = ir_seek_table(rc_tab, scancode);
  285. if (rc >= 0) {
  286. IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
  287. rc, scancode, keycode);
  288. clear_bit(keymap[rc].keycode, dev->keybit);
  289. spin_lock_irqsave(&rc_tab->lock, flags);
  290. keymap[rc].keycode = keycode;
  291. spin_unlock_irqrestore(&rc_tab->lock, flags);
  292. set_bit(keycode, dev->keybit);
  293. return 0;
  294. }
  295. /*
  296. * Handle new scancode inserts
  297. *
  298. * reallocate table if needed and insert a new keycode
  299. */
  300. /* Avoid growing the table indefinitely */
  301. if (rc_tab->size + 1 > IR_TAB_MAX_SIZE)
  302. return -EINVAL;
  303. rc = ir_insert_key(rc_tab, scancode, keycode);
  304. if (rc < 0)
  305. return rc;
  306. set_bit(keycode, dev->keybit);
  307. return 0;
  308. }
  309. /**
  310. * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  311. * @input_dev: the struct input_dev descriptor of the device
  312. * @scancode: the scancode that we're seeking
  313. *
  314. * This routine is used by the input routines when a key is pressed at the
  315. * IR. The scancode is received and needs to be converted into a keycode.
  316. * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the
  317. * corresponding keycode from the table.
  318. */
  319. u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
  320. {
  321. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  322. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  323. struct ir_scancode *keymap = rc_tab->scan;
  324. int elem;
  325. elem = ir_seek_table(rc_tab, scancode);
  326. if (elem >= 0) {
  327. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  328. dev->name, scancode, keymap[elem].keycode);
  329. return rc_tab->scan[elem].keycode;
  330. }
  331. printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n",
  332. dev->name, scancode);
  333. /* Reports userspace that an unknown keycode were got */
  334. return KEY_RESERVED;
  335. }
  336. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  337. /**
  338. * ir_input_register() - sets the IR keycode table and add the handlers
  339. * for keymap table get/set
  340. * @input_dev: the struct input_dev descriptor of the device
  341. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  342. *
  343. * This routine is used to initialize the input infrastructure
  344. * to work with an IR.
  345. * It will register the input/evdev interface for the device and
  346. * register the syfs code for IR class
  347. */
  348. int ir_input_register(struct input_dev *input_dev,
  349. const struct ir_scancode_table *rc_tab,
  350. const struct ir_dev_props *props)
  351. {
  352. struct ir_input_dev *ir_dev;
  353. struct ir_scancode *keymap = rc_tab->scan;
  354. int i, rc;
  355. if (rc_tab->scan == NULL || !rc_tab->size)
  356. return -EINVAL;
  357. ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
  358. if (!ir_dev)
  359. return -ENOMEM;
  360. spin_lock_init(&ir_dev->rc_tab.lock);
  361. ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size);
  362. ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size *
  363. sizeof(struct ir_scancode), GFP_KERNEL);
  364. if (!ir_dev->rc_tab.scan)
  365. return -ENOMEM;
  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");