ir-keytable.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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_create_table() - initializes a scancode table
  24. * @rc_tab: the ir_scancode_table to initialize
  25. * @name: name to assign to the table
  26. * @ir_type: ir type to assign to the new table
  27. * @size: initial size of the table
  28. * @return: zero on success or a negative error code
  29. *
  30. * This routine will initialize the ir_scancode_table and will allocate
  31. * memory to hold at least the specified number elements.
  32. */
  33. static int ir_create_table(struct ir_scancode_table *rc_tab,
  34. const char *name, u64 ir_type, size_t size)
  35. {
  36. rc_tab->name = name;
  37. rc_tab->ir_type = ir_type;
  38. rc_tab->alloc = roundup_pow_of_two(size * sizeof(struct ir_scancode));
  39. rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
  40. rc_tab->scan = kmalloc(rc_tab->alloc, GFP_KERNEL);
  41. if (!rc_tab->scan)
  42. return -ENOMEM;
  43. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  44. rc_tab->size, rc_tab->alloc);
  45. return 0;
  46. }
  47. /**
  48. * ir_free_table() - frees memory allocated by a scancode table
  49. * @rc_tab: the table whose mappings need to be freed
  50. *
  51. * This routine will free memory alloctaed for key mappings used by given
  52. * scancode table.
  53. */
  54. static void ir_free_table(struct ir_scancode_table *rc_tab)
  55. {
  56. rc_tab->size = 0;
  57. kfree(rc_tab->scan);
  58. rc_tab->scan = NULL;
  59. }
  60. /**
  61. * ir_resize_table() - resizes a scancode table if necessary
  62. * @rc_tab: the ir_scancode_table to resize
  63. * @gfp_flags: gfp flags to use when allocating memory
  64. * @return: zero on success or a negative error code
  65. *
  66. * This routine will shrink the ir_scancode_table if it has lots of
  67. * unused entries and grow it if it is full.
  68. */
  69. static int ir_resize_table(struct ir_scancode_table *rc_tab, gfp_t gfp_flags)
  70. {
  71. unsigned int oldalloc = rc_tab->alloc;
  72. unsigned int newalloc = oldalloc;
  73. struct ir_scancode *oldscan = rc_tab->scan;
  74. struct ir_scancode *newscan;
  75. if (rc_tab->size == rc_tab->len) {
  76. /* All entries in use -> grow keytable */
  77. if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
  78. return -ENOMEM;
  79. newalloc *= 2;
  80. IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
  81. }
  82. if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
  83. /* Less than 1/3 of entries in use -> shrink keytable */
  84. newalloc /= 2;
  85. IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
  86. }
  87. if (newalloc == oldalloc)
  88. return 0;
  89. newscan = kmalloc(newalloc, gfp_flags);
  90. if (!newscan) {
  91. IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
  92. return -ENOMEM;
  93. }
  94. memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
  95. rc_tab->scan = newscan;
  96. rc_tab->alloc = newalloc;
  97. rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
  98. kfree(oldscan);
  99. return 0;
  100. }
  101. /**
  102. * ir_update_mapping() - set a keycode in the scancode->keycode table
  103. * @dev: the struct input_dev device descriptor
  104. * @rc_tab: scancode table to be adjusted
  105. * @index: index of the mapping that needs to be updated
  106. * @keycode: the desired keycode
  107. * @return: previous keycode assigned to the mapping
  108. *
  109. * This routine is used to update scancode->keycopde mapping at given
  110. * position.
  111. */
  112. static unsigned int ir_update_mapping(struct input_dev *dev,
  113. struct ir_scancode_table *rc_tab,
  114. unsigned int index,
  115. unsigned int new_keycode)
  116. {
  117. int old_keycode = rc_tab->scan[index].keycode;
  118. int i;
  119. /* Did the user wish to remove the mapping? */
  120. if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
  121. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
  122. index, rc_tab->scan[index].scancode);
  123. rc_tab->len--;
  124. memmove(&rc_tab->scan[index], &rc_tab->scan[index+ 1],
  125. (rc_tab->len - index) * sizeof(struct ir_scancode));
  126. } else {
  127. IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
  128. index,
  129. old_keycode == KEY_RESERVED ? "New" : "Replacing",
  130. rc_tab->scan[index].scancode, new_keycode);
  131. rc_tab->scan[index].keycode = new_keycode;
  132. __set_bit(new_keycode, dev->keybit);
  133. }
  134. if (old_keycode != KEY_RESERVED) {
  135. /* A previous mapping was updated... */
  136. __clear_bit(old_keycode, dev->keybit);
  137. /* ... but another scancode might use the same keycode */
  138. for (i = 0; i < rc_tab->len; i++) {
  139. if (rc_tab->scan[i].keycode == old_keycode) {
  140. __set_bit(old_keycode, dev->keybit);
  141. break;
  142. }
  143. }
  144. /* Possibly shrink the keytable, failure is not a problem */
  145. ir_resize_table(rc_tab, GFP_ATOMIC);
  146. }
  147. return old_keycode;
  148. }
  149. /**
  150. * ir_locate_scancode() - set a keycode in the scancode->keycode table
  151. * @ir_dev: the struct ir_input_dev device descriptor
  152. * @rc_tab: scancode table to be searched
  153. * @scancode: the desired scancode
  154. * @resize: controls whether we allowed to resize the table to
  155. * accomodate not yet present scancodes
  156. * @return: index of the mapping containing scancode in question
  157. * or -1U in case of failure.
  158. *
  159. * This routine is used to locate given scancode in ir_scancode_table.
  160. * If scancode is not yet present the routine will allocate a new slot
  161. * for it.
  162. */
  163. static unsigned int ir_establish_scancode(struct ir_input_dev *ir_dev,
  164. struct ir_scancode_table *rc_tab,
  165. unsigned int scancode,
  166. bool resize)
  167. {
  168. unsigned int i;
  169. /*
  170. * Unfortunately, some hardware-based IR decoders don't provide
  171. * all bits for the complete IR code. In general, they provide only
  172. * the command part of the IR code. Yet, as it is possible to replace
  173. * the provided IR with another one, it is needed to allow loading
  174. * IR tables from other remotes. So,
  175. */
  176. if (ir_dev->props && ir_dev->props->scanmask)
  177. scancode &= ir_dev->props->scanmask;
  178. /* First check if we already have a mapping for this ir command */
  179. for (i = 0; i < rc_tab->len; i++) {
  180. if (rc_tab->scan[i].scancode == scancode)
  181. return i;
  182. /* Keytable is sorted from lowest to highest scancode */
  183. if (rc_tab->scan[i].scancode >= scancode)
  184. break;
  185. }
  186. /* No previous mapping found, we might need to grow the table */
  187. if (rc_tab->size == rc_tab->len) {
  188. if (!resize || ir_resize_table(rc_tab, GFP_ATOMIC))
  189. return -1U;
  190. }
  191. /* i is the proper index to insert our new keycode */
  192. if (i < rc_tab->len)
  193. memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
  194. (rc_tab->len - i) * sizeof(struct ir_scancode));
  195. rc_tab->scan[i].scancode = scancode;
  196. rc_tab->scan[i].keycode = KEY_RESERVED;
  197. rc_tab->len++;
  198. return i;
  199. }
  200. /**
  201. * ir_setkeycode() - set a keycode in the scancode->keycode table
  202. * @dev: the struct input_dev device descriptor
  203. * @scancode: the desired scancode
  204. * @keycode: result
  205. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  206. *
  207. * This routine is used to handle evdev EVIOCSKEY ioctl.
  208. */
  209. static int ir_setkeycode(struct input_dev *dev,
  210. const struct input_keymap_entry *ke,
  211. unsigned int *old_keycode)
  212. {
  213. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  214. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  215. unsigned int index;
  216. unsigned int scancode;
  217. int retval;
  218. unsigned long flags;
  219. spin_lock_irqsave(&rc_tab->lock, flags);
  220. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  221. index = ke->index;
  222. if (index >= rc_tab->len) {
  223. retval = -EINVAL;
  224. goto out;
  225. }
  226. } else {
  227. retval = input_scancode_to_scalar(ke, &scancode);
  228. if (retval)
  229. goto out;
  230. index = ir_establish_scancode(ir_dev, rc_tab, scancode, true);
  231. if (index >= rc_tab->len) {
  232. retval = -ENOMEM;
  233. goto out;
  234. }
  235. }
  236. *old_keycode = ir_update_mapping(dev, rc_tab, index, ke->keycode);
  237. out:
  238. spin_unlock_irqrestore(&rc_tab->lock, flags);
  239. return retval;
  240. }
  241. /**
  242. * ir_setkeytable() - sets several entries in the scancode->keycode table
  243. * @dev: the struct input_dev device descriptor
  244. * @to: the struct ir_scancode_table to copy entries to
  245. * @from: the struct ir_scancode_table to copy entries from
  246. * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
  247. *
  248. * This routine is used to handle table initialization.
  249. */
  250. static int ir_setkeytable(struct ir_input_dev *ir_dev,
  251. const struct ir_scancode_table *from)
  252. {
  253. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  254. unsigned int i, index;
  255. int rc;
  256. rc = ir_create_table(&ir_dev->rc_tab,
  257. from->name, from->ir_type, from->size);
  258. if (rc)
  259. return rc;
  260. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  261. rc_tab->size, rc_tab->alloc);
  262. for (i = 0; i < from->size; i++) {
  263. index = ir_establish_scancode(ir_dev, rc_tab,
  264. from->scan[i].scancode, false);
  265. if (index >= rc_tab->len) {
  266. rc = -ENOMEM;
  267. break;
  268. }
  269. ir_update_mapping(ir_dev->input_dev, rc_tab, index,
  270. from->scan[i].keycode);
  271. }
  272. if (rc)
  273. ir_free_table(rc_tab);
  274. return rc;
  275. }
  276. /**
  277. * ir_lookup_by_scancode() - locate mapping by scancode
  278. * @rc_tab: the &struct ir_scancode_table to search
  279. * @scancode: scancode to look for in the table
  280. * @return: index in the table, -1U if not found
  281. *
  282. * This routine performs binary search in RC keykeymap table for
  283. * given scancode.
  284. */
  285. static unsigned int ir_lookup_by_scancode(const struct ir_scancode_table *rc_tab,
  286. unsigned int scancode)
  287. {
  288. int start = 0;
  289. int end = rc_tab->len - 1;
  290. int mid;
  291. while (start <= end) {
  292. mid = (start + end) / 2;
  293. if (rc_tab->scan[mid].scancode < scancode)
  294. start = mid + 1;
  295. else if (rc_tab->scan[mid].scancode > scancode)
  296. end = mid - 1;
  297. else
  298. return mid;
  299. }
  300. return -1U;
  301. }
  302. /**
  303. * ir_getkeycode() - get a keycode from the scancode->keycode table
  304. * @dev: the struct input_dev device descriptor
  305. * @scancode: the desired scancode
  306. * @keycode: used to return the keycode, if found, or KEY_RESERVED
  307. * @return: always returns zero.
  308. *
  309. * This routine is used to handle evdev EVIOCGKEY ioctl.
  310. */
  311. static int ir_getkeycode(struct input_dev *dev,
  312. struct input_keymap_entry *ke)
  313. {
  314. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  315. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  316. struct ir_scancode *entry;
  317. unsigned long flags;
  318. unsigned int index;
  319. unsigned int scancode;
  320. int retval;
  321. spin_lock_irqsave(&rc_tab->lock, flags);
  322. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  323. index = ke->index;
  324. } else {
  325. retval = input_scancode_to_scalar(ke, &scancode);
  326. if (retval)
  327. goto out;
  328. index = ir_lookup_by_scancode(rc_tab, scancode);
  329. }
  330. if (index >= rc_tab->len) {
  331. if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
  332. IR_dprintk(1, "unknown key for scancode 0x%04x\n",
  333. scancode);
  334. retval = -EINVAL;
  335. goto out;
  336. }
  337. entry = &rc_tab->scan[index];
  338. ke->index = index;
  339. ke->keycode = entry->keycode;
  340. ke->len = sizeof(entry->scancode);
  341. memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
  342. retval = 0;
  343. out:
  344. spin_unlock_irqrestore(&rc_tab->lock, flags);
  345. return retval;
  346. }
  347. /**
  348. * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  349. * @input_dev: the struct input_dev descriptor of the device
  350. * @scancode: the scancode that we're seeking
  351. *
  352. * This routine is used by the input routines when a key is pressed at the
  353. * IR. The scancode is received and needs to be converted into a keycode.
  354. * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
  355. * corresponding keycode from the table.
  356. */
  357. u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
  358. {
  359. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  360. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  361. unsigned int keycode;
  362. unsigned int index;
  363. unsigned long flags;
  364. spin_lock_irqsave(&rc_tab->lock, flags);
  365. index = ir_lookup_by_scancode(rc_tab, scancode);
  366. keycode = index < rc_tab->len ?
  367. rc_tab->scan[index].keycode : KEY_RESERVED;
  368. spin_unlock_irqrestore(&rc_tab->lock, flags);
  369. if (keycode != KEY_RESERVED)
  370. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  371. dev->name, scancode, keycode);
  372. return keycode;
  373. }
  374. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  375. /**
  376. * ir_do_keyup() - internal function to signal the release of a keypress
  377. * @ir: the struct ir_input_dev descriptor of the device
  378. *
  379. * This function is used internally to release a keypress, it must be
  380. * called with keylock held.
  381. */
  382. static void ir_do_keyup(struct ir_input_dev *ir)
  383. {
  384. if (!ir->keypressed)
  385. return;
  386. IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
  387. input_report_key(ir->input_dev, ir->last_keycode, 0);
  388. input_sync(ir->input_dev);
  389. ir->keypressed = false;
  390. }
  391. /**
  392. * ir_keyup() - generates input event to signal the release of a keypress
  393. * @dev: the struct input_dev descriptor of the device
  394. *
  395. * This routine is used to signal that a key has been released on the
  396. * remote control.
  397. */
  398. void ir_keyup(struct input_dev *dev)
  399. {
  400. unsigned long flags;
  401. struct ir_input_dev *ir = input_get_drvdata(dev);
  402. spin_lock_irqsave(&ir->keylock, flags);
  403. ir_do_keyup(ir);
  404. spin_unlock_irqrestore(&ir->keylock, flags);
  405. }
  406. EXPORT_SYMBOL_GPL(ir_keyup);
  407. /**
  408. * ir_timer_keyup() - generates a keyup event after a timeout
  409. * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
  410. *
  411. * This routine will generate a keyup event some time after a keydown event
  412. * is generated when no further activity has been detected.
  413. */
  414. static void ir_timer_keyup(unsigned long cookie)
  415. {
  416. struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
  417. unsigned long flags;
  418. /*
  419. * ir->keyup_jiffies is used to prevent a race condition if a
  420. * hardware interrupt occurs at this point and the keyup timer
  421. * event is moved further into the future as a result.
  422. *
  423. * The timer will then be reactivated and this function called
  424. * again in the future. We need to exit gracefully in that case
  425. * to allow the input subsystem to do its auto-repeat magic or
  426. * a keyup event might follow immediately after the keydown.
  427. */
  428. spin_lock_irqsave(&ir->keylock, flags);
  429. if (time_is_before_eq_jiffies(ir->keyup_jiffies))
  430. ir_do_keyup(ir);
  431. spin_unlock_irqrestore(&ir->keylock, flags);
  432. }
  433. /**
  434. * ir_repeat() - notifies the IR core that a key is still pressed
  435. * @dev: the struct input_dev descriptor of the device
  436. *
  437. * This routine is used by IR decoders when a repeat message which does
  438. * not include the necessary bits to reproduce the scancode has been
  439. * received.
  440. */
  441. void ir_repeat(struct input_dev *dev)
  442. {
  443. unsigned long flags;
  444. struct ir_input_dev *ir = input_get_drvdata(dev);
  445. spin_lock_irqsave(&ir->keylock, flags);
  446. input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
  447. if (!ir->keypressed)
  448. goto out;
  449. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  450. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  451. out:
  452. spin_unlock_irqrestore(&ir->keylock, flags);
  453. }
  454. EXPORT_SYMBOL_GPL(ir_repeat);
  455. /**
  456. * ir_do_keydown() - internal function to process a keypress
  457. * @dev: the struct input_dev descriptor of the device
  458. * @scancode: the scancode of the keypress
  459. * @keycode: the keycode of the keypress
  460. * @toggle: the toggle value of the keypress
  461. *
  462. * This function is used internally to register a keypress, it must be
  463. * called with keylock held.
  464. */
  465. static void ir_do_keydown(struct input_dev *dev, int scancode,
  466. u32 keycode, u8 toggle)
  467. {
  468. struct ir_input_dev *ir = input_get_drvdata(dev);
  469. input_event(dev, EV_MSC, MSC_SCAN, scancode);
  470. /* Repeat event? */
  471. if (ir->keypressed &&
  472. ir->last_scancode == scancode &&
  473. ir->last_toggle == toggle)
  474. return;
  475. /* Release old keypress */
  476. ir_do_keyup(ir);
  477. ir->last_scancode = scancode;
  478. ir->last_toggle = toggle;
  479. ir->last_keycode = keycode;
  480. if (keycode == KEY_RESERVED)
  481. return;
  482. /* Register a keypress */
  483. ir->keypressed = true;
  484. IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
  485. dev->name, keycode, scancode);
  486. input_report_key(dev, ir->last_keycode, 1);
  487. input_sync(dev);
  488. }
  489. /**
  490. * ir_keydown() - generates input event for a key press
  491. * @dev: the struct input_dev descriptor of the device
  492. * @scancode: the scancode that we're seeking
  493. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  494. * support toggle values, this should be set to zero)
  495. *
  496. * This routine is used by the input routines when a key is pressed at the
  497. * IR. It gets the keycode for a scancode and reports an input event via
  498. * input_report_key().
  499. */
  500. void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
  501. {
  502. unsigned long flags;
  503. struct ir_input_dev *ir = input_get_drvdata(dev);
  504. u32 keycode = ir_g_keycode_from_table(dev, scancode);
  505. spin_lock_irqsave(&ir->keylock, flags);
  506. ir_do_keydown(dev, scancode, keycode, toggle);
  507. if (ir->keypressed) {
  508. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  509. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  510. }
  511. spin_unlock_irqrestore(&ir->keylock, flags);
  512. }
  513. EXPORT_SYMBOL_GPL(ir_keydown);
  514. /**
  515. * ir_keydown_notimeout() - generates input event for a key press without
  516. * an automatic keyup event at a later time
  517. * @dev: the struct input_dev descriptor of the device
  518. * @scancode: the scancode that we're seeking
  519. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  520. * support toggle values, this should be set to zero)
  521. *
  522. * This routine is used by the input routines when a key is pressed at the
  523. * IR. It gets the keycode for a scancode and reports an input event via
  524. * input_report_key(). The driver must manually call ir_keyup() at a later
  525. * stage.
  526. */
  527. void ir_keydown_notimeout(struct input_dev *dev, int scancode, u8 toggle)
  528. {
  529. unsigned long flags;
  530. struct ir_input_dev *ir = input_get_drvdata(dev);
  531. u32 keycode = ir_g_keycode_from_table(dev, scancode);
  532. spin_lock_irqsave(&ir->keylock, flags);
  533. ir_do_keydown(dev, scancode, keycode, toggle);
  534. spin_unlock_irqrestore(&ir->keylock, flags);
  535. }
  536. EXPORT_SYMBOL_GPL(ir_keydown_notimeout);
  537. static int ir_open(struct input_dev *input_dev)
  538. {
  539. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  540. return ir_dev->props->open(ir_dev->props->priv);
  541. }
  542. static void ir_close(struct input_dev *input_dev)
  543. {
  544. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  545. ir_dev->props->close(ir_dev->props->priv);
  546. }
  547. /**
  548. * __ir_input_register() - sets the IR keycode table and add the handlers
  549. * for keymap table get/set
  550. * @input_dev: the struct input_dev descriptor of the device
  551. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  552. *
  553. * This routine is used to initialize the input infrastructure
  554. * to work with an IR.
  555. * It will register the input/evdev interface for the device and
  556. * register the syfs code for IR class
  557. */
  558. int __ir_input_register(struct input_dev *input_dev,
  559. const struct ir_scancode_table *rc_tab,
  560. struct ir_dev_props *props,
  561. const char *driver_name)
  562. {
  563. struct ir_input_dev *ir_dev;
  564. int rc;
  565. if (rc_tab->scan == NULL || !rc_tab->size)
  566. return -EINVAL;
  567. ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
  568. if (!ir_dev)
  569. return -ENOMEM;
  570. ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
  571. if (!ir_dev->driver_name) {
  572. rc = -ENOMEM;
  573. goto out_dev;
  574. }
  575. input_dev->getkeycode_new = ir_getkeycode;
  576. input_dev->setkeycode_new = ir_setkeycode;
  577. input_set_drvdata(input_dev, ir_dev);
  578. ir_dev->input_dev = input_dev;
  579. spin_lock_init(&ir_dev->rc_tab.lock);
  580. spin_lock_init(&ir_dev->keylock);
  581. setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
  582. if (props) {
  583. ir_dev->props = props;
  584. if (props->open)
  585. input_dev->open = ir_open;
  586. if (props->close)
  587. input_dev->close = ir_close;
  588. }
  589. set_bit(EV_KEY, input_dev->evbit);
  590. set_bit(EV_REP, input_dev->evbit);
  591. set_bit(EV_MSC, input_dev->evbit);
  592. set_bit(MSC_SCAN, input_dev->mscbit);
  593. rc = ir_setkeytable(ir_dev, rc_tab);
  594. if (rc)
  595. goto out_name;
  596. rc = ir_register_class(input_dev);
  597. if (rc < 0)
  598. goto out_table;
  599. if (ir_dev->props)
  600. if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
  601. rc = ir_raw_event_register(input_dev);
  602. if (rc < 0)
  603. goto out_event;
  604. }
  605. rc = ir_register_input(input_dev);
  606. if (rc < 0)
  607. goto out_event;
  608. IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
  609. driver_name, rc_tab->name,
  610. (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
  611. " in raw mode" : "");
  612. /*
  613. * Default delay of 250ms is too short for some protocols, expecially
  614. * since the timeout is currently set to 250ms. Increase it to 500ms,
  615. * to avoid wrong repetition of the keycodes.
  616. */
  617. input_dev->rep[REP_DELAY] = 500;
  618. return 0;
  619. out_event:
  620. ir_unregister_class(input_dev);
  621. out_table:
  622. ir_free_table(&ir_dev->rc_tab);
  623. out_name:
  624. kfree(ir_dev->driver_name);
  625. out_dev:
  626. kfree(ir_dev);
  627. return rc;
  628. }
  629. EXPORT_SYMBOL_GPL(__ir_input_register);
  630. /**
  631. * ir_input_unregister() - unregisters IR and frees resources
  632. * @input_dev: the struct input_dev descriptor of the device
  633. * This routine is used to free memory and de-register interfaces.
  634. */
  635. void ir_input_unregister(struct input_dev *input_dev)
  636. {
  637. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  638. if (!ir_dev)
  639. return;
  640. IR_dprintk(1, "Freed keycode table\n");
  641. del_timer_sync(&ir_dev->timer_keyup);
  642. if (ir_dev->props)
  643. if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
  644. ir_raw_event_unregister(input_dev);
  645. ir_free_table(&ir_dev->rc_tab);
  646. ir_unregister_class(input_dev);
  647. kfree(ir_dev->driver_name);
  648. kfree(ir_dev);
  649. }
  650. EXPORT_SYMBOL_GPL(ir_input_unregister);
  651. int ir_core_debug; /* ir_debug level (0,1,2) */
  652. EXPORT_SYMBOL_GPL(ir_core_debug);
  653. module_param_named(debug, ir_core_debug, int, 0644);
  654. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  655. MODULE_LICENSE("GPL");