ir-keytable.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
  305. if (!ir->keypressed)
  306. goto out;
  307. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  308. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  309. out:
  310. spin_unlock_irqrestore(&ir->keylock, flags);
  311. }
  312. EXPORT_SYMBOL_GPL(ir_repeat);
  313. /**
  314. * ir_keydown() - generates input event for a key press
  315. * @dev: the struct input_dev descriptor of the device
  316. * @scancode: the scancode that we're seeking
  317. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  318. * support toggle values, this should be set to zero)
  319. *
  320. * This routine is used by the input routines when a key is pressed at the
  321. * IR. It gets the keycode for a scancode and reports an input event via
  322. * input_report_key().
  323. */
  324. void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
  325. {
  326. unsigned long flags;
  327. struct ir_input_dev *ir = input_get_drvdata(dev);
  328. u32 keycode = ir_g_keycode_from_table(dev, scancode);
  329. spin_lock_irqsave(&ir->keylock, flags);
  330. input_event(dev, EV_MSC, MSC_SCAN, scancode);
  331. /* Repeat event? */
  332. if (ir->keypressed &&
  333. ir->last_scancode == scancode &&
  334. ir->last_toggle == toggle)
  335. goto set_timer;
  336. /* Release old keypress */
  337. ir_keyup(ir);
  338. ir->last_scancode = scancode;
  339. ir->last_toggle = toggle;
  340. ir->last_keycode = keycode;
  341. if (keycode == KEY_RESERVED)
  342. goto out;
  343. /* Register a keypress */
  344. ir->keypressed = true;
  345. IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
  346. dev->name, keycode, scancode);
  347. input_report_key(dev, ir->last_keycode, 1);
  348. input_sync(dev);
  349. set_timer:
  350. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  351. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  352. out:
  353. spin_unlock_irqrestore(&ir->keylock, flags);
  354. }
  355. EXPORT_SYMBOL_GPL(ir_keydown);
  356. static int ir_open(struct input_dev *input_dev)
  357. {
  358. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  359. return ir_dev->props->open(ir_dev->props->priv);
  360. }
  361. static void ir_close(struct input_dev *input_dev)
  362. {
  363. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  364. ir_dev->props->close(ir_dev->props->priv);
  365. }
  366. /**
  367. * __ir_input_register() - sets the IR keycode table and add the handlers
  368. * for keymap table get/set
  369. * @input_dev: the struct input_dev descriptor of the device
  370. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  371. *
  372. * This routine is used to initialize the input infrastructure
  373. * to work with an IR.
  374. * It will register the input/evdev interface for the device and
  375. * register the syfs code for IR class
  376. */
  377. int __ir_input_register(struct input_dev *input_dev,
  378. const struct ir_scancode_table *rc_tab,
  379. struct ir_dev_props *props,
  380. const char *driver_name)
  381. {
  382. struct ir_input_dev *ir_dev;
  383. int rc;
  384. if (rc_tab->scan == NULL || !rc_tab->size)
  385. return -EINVAL;
  386. ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
  387. if (!ir_dev)
  388. return -ENOMEM;
  389. ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
  390. if (!ir_dev->driver_name) {
  391. rc = -ENOMEM;
  392. goto out_dev;
  393. }
  394. input_dev->getkeycode = ir_getkeycode;
  395. input_dev->setkeycode = ir_setkeycode;
  396. input_set_drvdata(input_dev, ir_dev);
  397. ir_dev->input_dev = input_dev;
  398. spin_lock_init(&ir_dev->rc_tab.lock);
  399. spin_lock_init(&ir_dev->keylock);
  400. setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
  401. ir_dev->rc_tab.name = rc_tab->name;
  402. ir_dev->rc_tab.ir_type = rc_tab->ir_type;
  403. ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size *
  404. sizeof(struct ir_scancode));
  405. ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL);
  406. ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode);
  407. if (props) {
  408. ir_dev->props = props;
  409. if (props->open)
  410. input_dev->open = ir_open;
  411. if (props->close)
  412. input_dev->close = ir_close;
  413. }
  414. if (!ir_dev->rc_tab.scan) {
  415. rc = -ENOMEM;
  416. goto out_name;
  417. }
  418. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  419. ir_dev->rc_tab.size, ir_dev->rc_tab.alloc);
  420. set_bit(EV_KEY, input_dev->evbit);
  421. set_bit(EV_REP, input_dev->evbit);
  422. set_bit(EV_MSC, input_dev->evbit);
  423. set_bit(MSC_SCAN, input_dev->mscbit);
  424. if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) {
  425. rc = -ENOMEM;
  426. goto out_table;
  427. }
  428. rc = ir_register_class(input_dev);
  429. if (rc < 0)
  430. goto out_table;
  431. if (ir_dev->props)
  432. if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
  433. rc = ir_raw_event_register(input_dev);
  434. if (rc < 0)
  435. goto out_event;
  436. }
  437. IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
  438. driver_name, rc_tab->name,
  439. (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
  440. " in raw mode" : "");
  441. return 0;
  442. out_event:
  443. ir_unregister_class(input_dev);
  444. out_table:
  445. kfree(ir_dev->rc_tab.scan);
  446. out_name:
  447. kfree(ir_dev->driver_name);
  448. out_dev:
  449. kfree(ir_dev);
  450. return rc;
  451. }
  452. EXPORT_SYMBOL_GPL(__ir_input_register);
  453. /**
  454. * ir_input_unregister() - unregisters IR and frees resources
  455. * @input_dev: the struct input_dev descriptor of the device
  456. * This routine is used to free memory and de-register interfaces.
  457. */
  458. void ir_input_unregister(struct input_dev *input_dev)
  459. {
  460. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  461. struct ir_scancode_table *rc_tab;
  462. if (!ir_dev)
  463. return;
  464. IR_dprintk(1, "Freed keycode table\n");
  465. del_timer_sync(&ir_dev->timer_keyup);
  466. if (ir_dev->props)
  467. if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
  468. ir_raw_event_unregister(input_dev);
  469. rc_tab = &ir_dev->rc_tab;
  470. rc_tab->size = 0;
  471. kfree(rc_tab->scan);
  472. rc_tab->scan = NULL;
  473. ir_unregister_class(input_dev);
  474. kfree(ir_dev->driver_name);
  475. kfree(ir_dev);
  476. }
  477. EXPORT_SYMBOL_GPL(ir_input_unregister);
  478. int ir_core_debug; /* ir_debug level (0,1,2) */
  479. EXPORT_SYMBOL_GPL(ir_core_debug);
  480. module_param_named(debug, ir_core_debug, int, 0644);
  481. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  482. MODULE_LICENSE("GPL");