ir-keytable.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
  18. #define IR_TAB_MIN_SIZE 256
  19. #define IR_TAB_MAX_SIZE 8192
  20. /**
  21. * ir_resize_table() - resizes a scancode table if necessary
  22. * @rc_tab: the ir_scancode_table to resize
  23. * @return: zero on success or a negative error code
  24. *
  25. * This routine will shrink the ir_scancode_table if it has lots of
  26. * unused entries and grow it if it is full.
  27. */
  28. static int ir_resize_table(struct ir_scancode_table *rc_tab)
  29. {
  30. unsigned int oldalloc = rc_tab->alloc;
  31. unsigned int newalloc = oldalloc;
  32. struct ir_scancode *oldscan = rc_tab->scan;
  33. struct ir_scancode *newscan;
  34. if (rc_tab->size == rc_tab->len) {
  35. /* All entries in use -> grow keytable */
  36. if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
  37. return -ENOMEM;
  38. newalloc *= 2;
  39. IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
  40. }
  41. if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
  42. /* Less than 1/3 of entries in use -> shrink keytable */
  43. newalloc /= 2;
  44. IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
  45. }
  46. if (newalloc == oldalloc)
  47. return 0;
  48. newscan = kmalloc(newalloc, GFP_ATOMIC);
  49. if (!newscan) {
  50. IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
  51. return -ENOMEM;
  52. }
  53. memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
  54. rc_tab->scan = newscan;
  55. rc_tab->alloc = newalloc;
  56. rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
  57. kfree(oldscan);
  58. return 0;
  59. }
  60. /**
  61. * ir_do_setkeycode() - internal function to set a keycode in the
  62. * scancode->keycode table
  63. * @dev: the struct input_dev device descriptor
  64. * @rc_tab: the struct ir_scancode_table to set the keycode in
  65. * @scancode: the scancode for the ir command
  66. * @keycode: the keycode for the ir command
  67. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  68. *
  69. * This routine is used internally to manipulate the scancode->keycode table.
  70. * The caller has to hold @rc_tab->lock.
  71. */
  72. static int ir_do_setkeycode(struct input_dev *dev,
  73. struct ir_scancode_table *rc_tab,
  74. unsigned scancode, unsigned keycode)
  75. {
  76. unsigned int i;
  77. int old_keycode = KEY_RESERVED;
  78. /* First check if we already have a mapping for this ir command */
  79. for (i = 0; i < rc_tab->len; i++) {
  80. /* Keytable is sorted from lowest to highest scancode */
  81. if (rc_tab->scan[i].scancode > scancode)
  82. break;
  83. else if (rc_tab->scan[i].scancode < scancode)
  84. continue;
  85. old_keycode = rc_tab->scan[i].keycode;
  86. rc_tab->scan[i].keycode = keycode;
  87. /* Did the user wish to remove the mapping? */
  88. if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) {
  89. rc_tab->len--;
  90. memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1],
  91. (rc_tab->len - i) * sizeof(struct ir_scancode));
  92. }
  93. /* Possibly shrink the keytable, failure is not a problem */
  94. ir_resize_table(rc_tab);
  95. break;
  96. }
  97. if (old_keycode == KEY_RESERVED) {
  98. /* No previous mapping found, we might need to grow the table */
  99. if (ir_resize_table(rc_tab))
  100. return -ENOMEM;
  101. /* i is the proper index to insert our new keycode */
  102. memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
  103. (rc_tab->len - i) * sizeof(struct ir_scancode));
  104. rc_tab->scan[i].scancode = scancode;
  105. rc_tab->scan[i].keycode = keycode;
  106. rc_tab->len++;
  107. set_bit(keycode, dev->keybit);
  108. } else {
  109. /* A previous mapping was updated... */
  110. clear_bit(old_keycode, dev->keybit);
  111. /* ...but another scancode might use the same keycode */
  112. for (i = 0; i < rc_tab->len; i++) {
  113. if (rc_tab->scan[i].keycode == old_keycode) {
  114. set_bit(old_keycode, dev->keybit);
  115. break;
  116. }
  117. }
  118. }
  119. return 0;
  120. }
  121. /**
  122. * ir_setkeycode() - set a keycode in the scancode->keycode table
  123. * @dev: the struct input_dev device descriptor
  124. * @scancode: the desired scancode
  125. * @keycode: result
  126. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  127. *
  128. * This routine is used to handle evdev EVIOCSKEY ioctl.
  129. */
  130. static int ir_setkeycode(struct input_dev *dev,
  131. unsigned int scancode, unsigned int keycode)
  132. {
  133. int rc;
  134. unsigned long flags;
  135. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  136. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  137. spin_lock_irqsave(&rc_tab->lock, flags);
  138. rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode);
  139. spin_unlock_irqrestore(&rc_tab->lock, flags);
  140. return rc;
  141. }
  142. /**
  143. * ir_setkeytable() - sets several entries in the scancode->keycode table
  144. * @dev: the struct input_dev device descriptor
  145. * @to: the struct ir_scancode_table to copy entries to
  146. * @from: the struct ir_scancode_table to copy entries from
  147. * @return: -EINVAL if all keycodes could not be inserted, otherwise zero.
  148. *
  149. * This routine is used to handle table initialization.
  150. */
  151. static int ir_setkeytable(struct input_dev *dev,
  152. struct ir_scancode_table *to,
  153. const struct ir_scancode_table *from)
  154. {
  155. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  156. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  157. unsigned long flags;
  158. unsigned int i;
  159. int rc = 0;
  160. spin_lock_irqsave(&rc_tab->lock, flags);
  161. for (i = 0; i < from->size; i++) {
  162. rc = ir_do_setkeycode(dev, to, from->scan[i].scancode,
  163. from->scan[i].keycode);
  164. if (rc)
  165. break;
  166. }
  167. spin_unlock_irqrestore(&rc_tab->lock, flags);
  168. return rc;
  169. }
  170. /**
  171. * ir_getkeycode() - get a keycode from the scancode->keycode table
  172. * @dev: the struct input_dev device descriptor
  173. * @scancode: the desired scancode
  174. * @keycode: used to return the keycode, if found, or KEY_RESERVED
  175. * @return: always returns zero.
  176. *
  177. * This routine is used to handle evdev EVIOCGKEY ioctl.
  178. */
  179. static int ir_getkeycode(struct input_dev *dev,
  180. unsigned int scancode, unsigned int *keycode)
  181. {
  182. int start, end, mid;
  183. unsigned long flags;
  184. int key = KEY_RESERVED;
  185. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  186. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  187. spin_lock_irqsave(&rc_tab->lock, flags);
  188. start = 0;
  189. end = rc_tab->len - 1;
  190. while (start <= end) {
  191. mid = (start + end) / 2;
  192. if (rc_tab->scan[mid].scancode < scancode)
  193. start = mid + 1;
  194. else if (rc_tab->scan[mid].scancode > scancode)
  195. end = mid - 1;
  196. else {
  197. key = rc_tab->scan[mid].keycode;
  198. break;
  199. }
  200. }
  201. spin_unlock_irqrestore(&rc_tab->lock, flags);
  202. *keycode = key;
  203. return 0;
  204. }
  205. /**
  206. * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  207. * @input_dev: the struct input_dev descriptor of the device
  208. * @scancode: the scancode that we're seeking
  209. *
  210. * This routine is used by the input routines when a key is pressed at the
  211. * IR. The scancode is received and needs to be converted into a keycode.
  212. * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
  213. * corresponding keycode from the table.
  214. */
  215. u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
  216. {
  217. int keycode;
  218. ir_getkeycode(dev, scancode, &keycode);
  219. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  220. dev->name, scancode, keycode);
  221. return keycode;
  222. }
  223. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  224. /**
  225. * ir_keyup() - generates input event to cleanup a key press
  226. * @input_dev: the struct input_dev descriptor of the device
  227. *
  228. * This routine is used by the input routines when a key is pressed at the
  229. * IR. It reports a keyup input event via input_report_key().
  230. */
  231. void ir_keyup(struct input_dev *dev)
  232. {
  233. struct ir_input_dev *ir = input_get_drvdata(dev);
  234. if (!ir->keypressed)
  235. return;
  236. IR_dprintk(1, "keyup key 0x%04x\n", ir->keycode);
  237. input_report_key(dev, ir->keycode, 0);
  238. input_sync(dev);
  239. ir->keypressed = 0;
  240. }
  241. EXPORT_SYMBOL_GPL(ir_keyup);
  242. /**
  243. * ir_keydown() - generates input event for a key press
  244. * @input_dev: the struct input_dev descriptor of the device
  245. * @scancode: the scancode that we're seeking
  246. *
  247. * This routine is used by the input routines when a key is pressed at the
  248. * IR. It gets the keycode for a scancode and reports an input event via
  249. * input_report_key().
  250. */
  251. void ir_keydown(struct input_dev *dev, int scancode)
  252. {
  253. struct ir_input_dev *ir = input_get_drvdata(dev);
  254. u32 keycode = ir_g_keycode_from_table(dev, scancode);
  255. /* If already sent a keydown, do a keyup */
  256. if (ir->keypressed)
  257. ir_keyup(dev);
  258. if (KEY_RESERVED == keycode)
  259. return;
  260. ir->keycode = keycode;
  261. ir->keypressed = 1;
  262. IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
  263. dev->name, keycode, scancode);
  264. input_report_key(dev, ir->keycode, 1);
  265. input_sync(dev);
  266. }
  267. EXPORT_SYMBOL_GPL(ir_keydown);
  268. static int ir_open(struct input_dev *input_dev)
  269. {
  270. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  271. return ir_dev->props->open(ir_dev->props->priv);
  272. }
  273. static void ir_close(struct input_dev *input_dev)
  274. {
  275. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  276. ir_dev->props->close(ir_dev->props->priv);
  277. }
  278. /**
  279. * __ir_input_register() - sets the IR keycode table and add the handlers
  280. * for keymap table get/set
  281. * @input_dev: the struct input_dev descriptor of the device
  282. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  283. *
  284. * This routine is used to initialize the input infrastructure
  285. * to work with an IR.
  286. * It will register the input/evdev interface for the device and
  287. * register the syfs code for IR class
  288. */
  289. int __ir_input_register(struct input_dev *input_dev,
  290. const struct ir_scancode_table *rc_tab,
  291. const struct ir_dev_props *props,
  292. const char *driver_name)
  293. {
  294. struct ir_input_dev *ir_dev;
  295. int rc;
  296. if (rc_tab->scan == NULL || !rc_tab->size)
  297. return -EINVAL;
  298. ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
  299. if (!ir_dev)
  300. return -ENOMEM;
  301. ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
  302. if (!ir_dev->driver_name) {
  303. rc = -ENOMEM;
  304. goto out_dev;
  305. }
  306. input_dev->getkeycode = ir_getkeycode;
  307. input_dev->setkeycode = ir_setkeycode;
  308. input_set_drvdata(input_dev, ir_dev);
  309. spin_lock_init(&ir_dev->rc_tab.lock);
  310. ir_dev->rc_tab.name = rc_tab->name;
  311. ir_dev->rc_tab.ir_type = rc_tab->ir_type;
  312. ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size *
  313. sizeof(struct ir_scancode));
  314. ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL);
  315. ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode);
  316. if (!ir_dev->rc_tab.scan) {
  317. rc = -ENOMEM;
  318. goto out_name;
  319. }
  320. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  321. ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
  322. set_bit(EV_KEY, input_dev->evbit);
  323. if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
  324. rc = -ENOMEM;
  325. goto out_table;
  326. }
  327. ir_dev->props = props;
  328. if (props && props->open)
  329. input_dev->open = ir_open;
  330. if (props && props->close)
  331. input_dev->close = ir_close;
  332. rc = ir_register_class(input_dev);
  333. if (rc < 0)
  334. goto out_table;
  335. return 0;
  336. out_table:
  337. kfree(ir_dev->rc_tab.scan);
  338. out_name:
  339. kfree(ir_dev->driver_name);
  340. out_dev:
  341. kfree(ir_dev);
  342. return rc;
  343. }
  344. EXPORT_SYMBOL_GPL(__ir_input_register);
  345. /**
  346. * ir_input_unregister() - unregisters IR and frees resources
  347. * @input_dev: the struct input_dev descriptor of the device
  348. * This routine is used to free memory and de-register interfaces.
  349. */
  350. void ir_input_unregister(struct input_dev *dev)
  351. {
  352. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  353. struct ir_scancode_table *rc_tab;
  354. if (!ir_dev)
  355. return;
  356. IR_dprintk(1, "Freed keycode table\n");
  357. rc_tab = &ir_dev->rc_tab;
  358. rc_tab->size = 0;
  359. kfree(rc_tab->scan);
  360. rc_tab->scan = NULL;
  361. ir_unregister_class(dev);
  362. kfree(ir_dev->driver_name);
  363. kfree(ir_dev);
  364. }
  365. EXPORT_SYMBOL_GPL(ir_input_unregister);
  366. int ir_core_debug; /* ir_debug level (0,1,2) */
  367. EXPORT_SYMBOL_GPL(ir_core_debug);
  368. module_param_named(debug, ir_core_debug, int, 0644);
  369. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  370. MODULE_LICENSE("GPL");