ir-keytable.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* ir-keytable.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 "ir-core-priv.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. /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
  21. #define IR_KEYPRESS_TIMEOUT 250
  22. /**
  23. * ir_resize_table() - resizes a scancode table if necessary
  24. * @rc_tab: the ir_scancode_table to resize
  25. * @return: zero on success or a negative error code
  26. *
  27. * This routine will shrink the ir_scancode_table if it has lots of
  28. * unused entries and grow it if it is full.
  29. */
  30. static int ir_resize_table(struct ir_scancode_table *rc_tab)
  31. {
  32. unsigned int oldalloc = rc_tab->alloc;
  33. unsigned int newalloc = oldalloc;
  34. struct ir_scancode *oldscan = rc_tab->scan;
  35. struct ir_scancode *newscan;
  36. if (rc_tab->size == rc_tab->len) {
  37. /* All entries in use -> grow keytable */
  38. if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
  39. return -ENOMEM;
  40. newalloc *= 2;
  41. IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
  42. }
  43. if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
  44. /* Less than 1/3 of entries in use -> shrink keytable */
  45. newalloc /= 2;
  46. IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
  47. }
  48. if (newalloc == oldalloc)
  49. return 0;
  50. newscan = kmalloc(newalloc, GFP_ATOMIC);
  51. if (!newscan) {
  52. IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
  53. return -ENOMEM;
  54. }
  55. memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
  56. rc_tab->scan = newscan;
  57. rc_tab->alloc = newalloc;
  58. rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
  59. kfree(oldscan);
  60. return 0;
  61. }
  62. /**
  63. * ir_do_setkeycode() - internal function to set a keycode in the
  64. * scancode->keycode table
  65. * @dev: the struct input_dev device descriptor
  66. * @rc_tab: the struct ir_scancode_table to set the keycode in
  67. * @scancode: the scancode for the ir command
  68. * @keycode: the keycode for the ir command
  69. * @resize: whether the keytable may be shrunk
  70. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  71. *
  72. * This routine is used internally to manipulate the scancode->keycode table.
  73. * The caller has to hold @rc_tab->lock.
  74. */
  75. static int ir_do_setkeycode(struct input_dev *dev,
  76. struct ir_scancode_table *rc_tab,
  77. unsigned scancode, unsigned keycode,
  78. bool resize)
  79. {
  80. unsigned int i;
  81. int old_keycode = KEY_RESERVED;
  82. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  83. /*
  84. * Unfortunately, some hardware-based IR decoders don't provide
  85. * all bits for the complete IR code. In general, they provide only
  86. * the command part of the IR code. Yet, as it is possible to replace
  87. * the provided IR with another one, it is needed to allow loading
  88. * IR tables from other remotes. So,
  89. */
  90. if (ir_dev->props && ir_dev->props->scanmask) {
  91. scancode &= ir_dev->props->scanmask;
  92. }
  93. /* First check if we already have a mapping for this ir command */
  94. for (i = 0; i < rc_tab->len; i++) {
  95. /* Keytable is sorted from lowest to highest scancode */
  96. if (rc_tab->scan[i].scancode > scancode)
  97. break;
  98. else if (rc_tab->scan[i].scancode < scancode)
  99. continue;
  100. old_keycode = rc_tab->scan[i].keycode;
  101. rc_tab->scan[i].keycode = keycode;
  102. /* Did the user wish to remove the mapping? */
  103. if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) {
  104. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
  105. i, scancode);
  106. rc_tab->len--;
  107. memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1],
  108. (rc_tab->len - i) * sizeof(struct ir_scancode));
  109. }
  110. /* Possibly shrink the keytable, failure is not a problem */
  111. ir_resize_table(rc_tab);
  112. break;
  113. }
  114. if (old_keycode == KEY_RESERVED && keycode != KEY_RESERVED) {
  115. /* No previous mapping found, we might need to grow the table */
  116. if (resize && ir_resize_table(rc_tab))
  117. return -ENOMEM;
  118. IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n",
  119. i, scancode, keycode);
  120. /* i is the proper index to insert our new keycode */
  121. memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
  122. (rc_tab->len - i) * sizeof(struct ir_scancode));
  123. rc_tab->scan[i].scancode = scancode;
  124. rc_tab->scan[i].keycode = keycode;
  125. rc_tab->len++;
  126. set_bit(keycode, dev->keybit);
  127. } else {
  128. IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n",
  129. i, scancode, keycode);
  130. /* A previous mapping was updated... */
  131. clear_bit(old_keycode, dev->keybit);
  132. /* ...but another scancode might use the same keycode */
  133. for (i = 0; i < rc_tab->len; i++) {
  134. if (rc_tab->scan[i].keycode == old_keycode) {
  135. set_bit(old_keycode, dev->keybit);
  136. break;
  137. }
  138. }
  139. }
  140. return 0;
  141. }
  142. /**
  143. * ir_setkeycode() - set a keycode in the scancode->keycode table
  144. * @dev: the struct input_dev device descriptor
  145. * @scancode: the desired scancode
  146. * @keycode: result
  147. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  148. *
  149. * This routine is used to handle evdev EVIOCSKEY ioctl.
  150. */
  151. static int ir_setkeycode(struct input_dev *dev,
  152. unsigned int scancode, unsigned int keycode)
  153. {
  154. int rc;
  155. unsigned long flags;
  156. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  157. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  158. spin_lock_irqsave(&rc_tab->lock, flags);
  159. rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode, true);
  160. spin_unlock_irqrestore(&rc_tab->lock, flags);
  161. return rc;
  162. }
  163. /**
  164. * ir_setkeytable() - sets several entries in the scancode->keycode table
  165. * @dev: the struct input_dev device descriptor
  166. * @to: the struct ir_scancode_table to copy entries to
  167. * @from: the struct ir_scancode_table to copy entries from
  168. * @return: -EINVAL if all keycodes could not be inserted, otherwise zero.
  169. *
  170. * This routine is used to handle table initialization.
  171. */
  172. static int ir_setkeytable(struct input_dev *dev,
  173. struct ir_scancode_table *to,
  174. const struct ir_scancode_table *from)
  175. {
  176. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  177. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  178. unsigned long flags;
  179. unsigned int i;
  180. int rc = 0;
  181. spin_lock_irqsave(&rc_tab->lock, flags);
  182. for (i = 0; i < from->size; i++) {
  183. rc = ir_do_setkeycode(dev, to, from->scan[i].scancode,
  184. from->scan[i].keycode, false);
  185. if (rc)
  186. break;
  187. }
  188. spin_unlock_irqrestore(&rc_tab->lock, flags);
  189. return rc;
  190. }
  191. /**
  192. * ir_getkeycode() - get a keycode from the scancode->keycode table
  193. * @dev: the struct input_dev device descriptor
  194. * @scancode: the desired scancode
  195. * @keycode: used to return the keycode, if found, or KEY_RESERVED
  196. * @return: always returns zero.
  197. *
  198. * This routine is used to handle evdev EVIOCGKEY ioctl.
  199. */
  200. static int ir_getkeycode(struct input_dev *dev,
  201. unsigned int scancode, unsigned int *keycode)
  202. {
  203. int start, end, mid;
  204. unsigned long flags;
  205. int key = KEY_RESERVED;
  206. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  207. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  208. spin_lock_irqsave(&rc_tab->lock, flags);
  209. start = 0;
  210. end = rc_tab->len - 1;
  211. while (start <= end) {
  212. mid = (start + end) / 2;
  213. if (rc_tab->scan[mid].scancode < scancode)
  214. start = mid + 1;
  215. else if (rc_tab->scan[mid].scancode > scancode)
  216. end = mid - 1;
  217. else {
  218. key = rc_tab->scan[mid].keycode;
  219. break;
  220. }
  221. }
  222. spin_unlock_irqrestore(&rc_tab->lock, flags);
  223. if (key == KEY_RESERVED)
  224. IR_dprintk(1, "unknown key for scancode 0x%04x\n",
  225. scancode);
  226. *keycode = key;
  227. return 0;
  228. }
  229. /**
  230. * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  231. * @input_dev: the struct input_dev descriptor of the device
  232. * @scancode: the scancode that we're seeking
  233. *
  234. * This routine is used by the input routines when a key is pressed at the
  235. * IR. The scancode is received and needs to be converted into a keycode.
  236. * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
  237. * corresponding keycode from the table.
  238. */
  239. u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
  240. {
  241. int keycode;
  242. ir_getkeycode(dev, scancode, &keycode);
  243. if (keycode != KEY_RESERVED)
  244. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  245. dev->name, scancode, keycode);
  246. return keycode;
  247. }
  248. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  249. /**
  250. * ir_keyup() - generates input event to cleanup a key press
  251. * @ir: the struct ir_input_dev descriptor of the device
  252. *
  253. * This routine is used to signal that a key has been released on the
  254. * remote control. It reports a keyup input event via input_report_key().
  255. */
  256. static void ir_keyup(struct ir_input_dev *ir)
  257. {
  258. if (!ir->keypressed)
  259. return;
  260. IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
  261. input_report_key(ir->input_dev, ir->last_keycode, 0);
  262. input_sync(ir->input_dev);
  263. ir->keypressed = false;
  264. }
  265. /**
  266. * ir_timer_keyup() - generates a keyup event after a timeout
  267. * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
  268. *
  269. * This routine will generate a keyup event some time after a keydown event
  270. * is generated when no further activity has been detected.
  271. */
  272. static void ir_timer_keyup(unsigned long cookie)
  273. {
  274. struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
  275. unsigned long flags;
  276. /*
  277. * ir->keyup_jiffies is used to prevent a race condition if a
  278. * hardware interrupt occurs at this point and the keyup timer
  279. * event is moved further into the future as a result.
  280. *
  281. * The timer will then be reactivated and this function called
  282. * again in the future. We need to exit gracefully in that case
  283. * to allow the input subsystem to do its auto-repeat magic or
  284. * a keyup event might follow immediately after the keydown.
  285. */
  286. spin_lock_irqsave(&ir->keylock, flags);
  287. if (time_is_after_eq_jiffies(ir->keyup_jiffies))
  288. ir_keyup(ir);
  289. spin_unlock_irqrestore(&ir->keylock, flags);
  290. }
  291. /**
  292. * ir_repeat() - notifies the IR core that a key is still pressed
  293. * @dev: the struct input_dev descriptor of the device
  294. *
  295. * This routine is used by IR decoders when a repeat message which does
  296. * not include the necessary bits to reproduce the scancode has been
  297. * received.
  298. */
  299. void ir_repeat(struct input_dev *dev)
  300. {
  301. unsigned long flags;
  302. struct ir_input_dev *ir = input_get_drvdata(dev);
  303. spin_lock_irqsave(&ir->keylock, flags);
  304. if (!ir->keypressed)
  305. goto out;
  306. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  307. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  308. out:
  309. spin_unlock_irqrestore(&ir->keylock, flags);
  310. }
  311. EXPORT_SYMBOL_GPL(ir_repeat);
  312. /**
  313. * ir_keydown() - generates input event for a key press
  314. * @dev: the struct input_dev descriptor of the device
  315. * @scancode: the scancode that we're seeking
  316. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  317. * support toggle values, this should be set to zero)
  318. *
  319. * This routine is used by the input routines when a key is pressed at the
  320. * IR. It gets the keycode for a scancode and reports an input event via
  321. * input_report_key().
  322. */
  323. void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
  324. {
  325. unsigned long flags;
  326. struct ir_input_dev *ir = input_get_drvdata(dev);
  327. u32 keycode = ir_g_keycode_from_table(dev, scancode);
  328. spin_lock_irqsave(&ir->keylock, flags);
  329. /* Repeat event? */
  330. if (ir->keypressed &&
  331. ir->last_scancode == scancode &&
  332. ir->last_toggle == toggle)
  333. goto set_timer;
  334. /* Release old keypress */
  335. ir_keyup(ir);
  336. ir->last_scancode = scancode;
  337. ir->last_toggle = toggle;
  338. ir->last_keycode = keycode;
  339. if (keycode == KEY_RESERVED)
  340. goto out;
  341. /* Register a keypress */
  342. ir->keypressed = true;
  343. IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
  344. dev->name, keycode, scancode);
  345. input_report_key(dev, ir->last_keycode, 1);
  346. input_sync(dev);
  347. set_timer:
  348. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  349. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  350. out:
  351. spin_unlock_irqrestore(&ir->keylock, flags);
  352. }
  353. EXPORT_SYMBOL_GPL(ir_keydown);
  354. static int ir_open(struct input_dev *input_dev)
  355. {
  356. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  357. return ir_dev->props->open(ir_dev->props->priv);
  358. }
  359. static void ir_close(struct input_dev *input_dev)
  360. {
  361. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  362. ir_dev->props->close(ir_dev->props->priv);
  363. }
  364. /**
  365. * __ir_input_register() - sets the IR keycode table and add the handlers
  366. * for keymap table get/set
  367. * @input_dev: the struct input_dev descriptor of the device
  368. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  369. *
  370. * This routine is used to initialize the input infrastructure
  371. * to work with an IR.
  372. * It will register the input/evdev interface for the device and
  373. * register the syfs code for IR class
  374. */
  375. int __ir_input_register(struct input_dev *input_dev,
  376. const struct ir_scancode_table *rc_tab,
  377. const struct ir_dev_props *props,
  378. const char *driver_name)
  379. {
  380. struct ir_input_dev *ir_dev;
  381. int rc;
  382. if (rc_tab->scan == NULL || !rc_tab->size)
  383. return -EINVAL;
  384. ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
  385. if (!ir_dev)
  386. return -ENOMEM;
  387. ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
  388. if (!ir_dev->driver_name) {
  389. rc = -ENOMEM;
  390. goto out_dev;
  391. }
  392. input_dev->getkeycode = ir_getkeycode;
  393. input_dev->setkeycode = ir_setkeycode;
  394. input_set_drvdata(input_dev, ir_dev);
  395. ir_dev->input_dev = input_dev;
  396. spin_lock_init(&ir_dev->rc_tab.lock);
  397. spin_lock_init(&ir_dev->keylock);
  398. setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
  399. ir_dev->rc_tab.name = rc_tab->name;
  400. ir_dev->rc_tab.ir_type = rc_tab->ir_type;
  401. ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size *
  402. sizeof(struct ir_scancode));
  403. ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL);
  404. ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode);
  405. if (props) {
  406. ir_dev->props = props;
  407. if (props->open)
  408. input_dev->open = ir_open;
  409. if (props->close)
  410. input_dev->close = ir_close;
  411. }
  412. if (!ir_dev->rc_tab.scan) {
  413. rc = -ENOMEM;
  414. goto out_name;
  415. }
  416. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  417. ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
  418. set_bit(EV_KEY, input_dev->evbit);
  419. set_bit(EV_REP, input_dev->evbit);
  420. if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
  421. rc = -ENOMEM;
  422. goto out_table;
  423. }
  424. rc = ir_register_class(input_dev);
  425. if (rc < 0)
  426. goto out_table;
  427. if (ir_dev->props)
  428. if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
  429. rc = ir_raw_event_register(input_dev);
  430. if (rc < 0)
  431. goto out_event;
  432. }
  433. IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
  434. driver_name, rc_tab->name,
  435. ir_dev->props->driver_type == RC_DRIVER_IR_RAW ? " in raw mode" : "");
  436. return 0;
  437. out_event:
  438. ir_unregister_class(input_dev);
  439. out_table:
  440. kfree(ir_dev->rc_tab.scan);
  441. out_name:
  442. kfree(ir_dev->driver_name);
  443. out_dev:
  444. kfree(ir_dev);
  445. return rc;
  446. }
  447. EXPORT_SYMBOL_GPL(__ir_input_register);
  448. /**
  449. * ir_input_unregister() - unregisters IR and frees resources
  450. * @input_dev: the struct input_dev descriptor of the device
  451. * This routine is used to free memory and de-register interfaces.
  452. */
  453. void ir_input_unregister(struct input_dev *input_dev)
  454. {
  455. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  456. struct ir_scancode_table *rc_tab;
  457. if (!ir_dev)
  458. return;
  459. IR_dprintk(1, "Freed keycode table\n");
  460. del_timer_sync(&ir_dev->timer_keyup);
  461. if (ir_dev->props)
  462. if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
  463. ir_raw_event_unregister(input_dev);
  464. rc_tab = &ir_dev->rc_tab;
  465. rc_tab->size = 0;
  466. kfree(rc_tab->scan);
  467. rc_tab->scan = NULL;
  468. ir_unregister_class(input_dev);
  469. kfree(ir_dev->driver_name);
  470. kfree(ir_dev);
  471. }
  472. EXPORT_SYMBOL_GPL(ir_input_unregister);
  473. int ir_core_debug; /* ir_debug level (0,1,2) */
  474. EXPORT_SYMBOL_GPL(ir_core_debug);
  475. module_param_named(debug, ir_core_debug, int, 0644);
  476. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  477. MODULE_LICENSE("GPL");