ir-keytable.c 16 KB

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