ir-keytable.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. unsigned int start = 0;
  289. unsigned int end = rc_tab->len - 1;
  290. unsigned 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. out:
  343. spin_unlock_irqrestore(&rc_tab->lock, flags);
  344. return retval;
  345. }
  346. /**
  347. * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  348. * @input_dev: the struct input_dev descriptor of the device
  349. * @scancode: the scancode that we're seeking
  350. *
  351. * This routine is used by the input routines when a key is pressed at the
  352. * IR. The scancode is received and needs to be converted into a keycode.
  353. * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
  354. * corresponding keycode from the table.
  355. */
  356. u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
  357. {
  358. struct ir_input_dev *ir_dev = input_get_drvdata(dev);
  359. struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
  360. unsigned int keycode;
  361. unsigned int index;
  362. unsigned long flags;
  363. spin_lock_irqsave(&rc_tab->lock, flags);
  364. index = ir_lookup_by_scancode(rc_tab, scancode);
  365. keycode = index < rc_tab->len ?
  366. rc_tab->scan[index].keycode : KEY_RESERVED;
  367. spin_unlock_irqrestore(&rc_tab->lock, flags);
  368. if (keycode != KEY_RESERVED)
  369. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  370. dev->name, scancode, keycode);
  371. return keycode;
  372. }
  373. EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
  374. /**
  375. * ir_keyup() - generates input event to cleanup a key press
  376. * @ir: the struct ir_input_dev descriptor of the device
  377. *
  378. * This routine is used to signal that a key has been released on the
  379. * remote control. It reports a keyup input event via input_report_key().
  380. */
  381. static void ir_keyup(struct ir_input_dev *ir)
  382. {
  383. if (!ir->keypressed)
  384. return;
  385. IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
  386. input_report_key(ir->input_dev, ir->last_keycode, 0);
  387. input_sync(ir->input_dev);
  388. ir->keypressed = false;
  389. }
  390. /**
  391. * ir_timer_keyup() - generates a keyup event after a timeout
  392. * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
  393. *
  394. * This routine will generate a keyup event some time after a keydown event
  395. * is generated when no further activity has been detected.
  396. */
  397. static void ir_timer_keyup(unsigned long cookie)
  398. {
  399. struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
  400. unsigned long flags;
  401. /*
  402. * ir->keyup_jiffies is used to prevent a race condition if a
  403. * hardware interrupt occurs at this point and the keyup timer
  404. * event is moved further into the future as a result.
  405. *
  406. * The timer will then be reactivated and this function called
  407. * again in the future. We need to exit gracefully in that case
  408. * to allow the input subsystem to do its auto-repeat magic or
  409. * a keyup event might follow immediately after the keydown.
  410. */
  411. spin_lock_irqsave(&ir->keylock, flags);
  412. if (time_is_after_eq_jiffies(ir->keyup_jiffies))
  413. ir_keyup(ir);
  414. spin_unlock_irqrestore(&ir->keylock, flags);
  415. }
  416. /**
  417. * ir_repeat() - notifies the IR core that a key is still pressed
  418. * @dev: the struct input_dev descriptor of the device
  419. *
  420. * This routine is used by IR decoders when a repeat message which does
  421. * not include the necessary bits to reproduce the scancode has been
  422. * received.
  423. */
  424. void ir_repeat(struct input_dev *dev)
  425. {
  426. unsigned long flags;
  427. struct ir_input_dev *ir = input_get_drvdata(dev);
  428. spin_lock_irqsave(&ir->keylock, flags);
  429. input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
  430. if (!ir->keypressed)
  431. goto out;
  432. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  433. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  434. out:
  435. spin_unlock_irqrestore(&ir->keylock, flags);
  436. }
  437. EXPORT_SYMBOL_GPL(ir_repeat);
  438. /**
  439. * ir_keydown() - generates input event for a key press
  440. * @dev: the struct input_dev descriptor of the device
  441. * @scancode: the scancode that we're seeking
  442. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  443. * support toggle values, this should be set to zero)
  444. *
  445. * This routine is used by the input routines when a key is pressed at the
  446. * IR. It gets the keycode for a scancode and reports an input event via
  447. * input_report_key().
  448. */
  449. void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
  450. {
  451. unsigned long flags;
  452. struct ir_input_dev *ir = input_get_drvdata(dev);
  453. u32 keycode = ir_g_keycode_from_table(dev, scancode);
  454. spin_lock_irqsave(&ir->keylock, flags);
  455. input_event(dev, EV_MSC, MSC_SCAN, scancode);
  456. /* Repeat event? */
  457. if (ir->keypressed &&
  458. ir->last_scancode == scancode &&
  459. ir->last_toggle == toggle)
  460. goto set_timer;
  461. /* Release old keypress */
  462. ir_keyup(ir);
  463. ir->last_scancode = scancode;
  464. ir->last_toggle = toggle;
  465. ir->last_keycode = keycode;
  466. if (keycode == KEY_RESERVED)
  467. goto out;
  468. /* Register a keypress */
  469. ir->keypressed = true;
  470. IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
  471. dev->name, keycode, scancode);
  472. input_report_key(dev, ir->last_keycode, 1);
  473. input_sync(dev);
  474. set_timer:
  475. ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  476. mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
  477. out:
  478. spin_unlock_irqrestore(&ir->keylock, flags);
  479. }
  480. EXPORT_SYMBOL_GPL(ir_keydown);
  481. static int ir_open(struct input_dev *input_dev)
  482. {
  483. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  484. return ir_dev->props->open(ir_dev->props->priv);
  485. }
  486. static void ir_close(struct input_dev *input_dev)
  487. {
  488. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  489. ir_dev->props->close(ir_dev->props->priv);
  490. }
  491. /**
  492. * __ir_input_register() - sets the IR keycode table and add the handlers
  493. * for keymap table get/set
  494. * @input_dev: the struct input_dev descriptor of the device
  495. * @rc_tab: the struct ir_scancode_table table of scancode/keymap
  496. *
  497. * This routine is used to initialize the input infrastructure
  498. * to work with an IR.
  499. * It will register the input/evdev interface for the device and
  500. * register the syfs code for IR class
  501. */
  502. int __ir_input_register(struct input_dev *input_dev,
  503. const struct ir_scancode_table *rc_tab,
  504. struct ir_dev_props *props,
  505. const char *driver_name)
  506. {
  507. struct ir_input_dev *ir_dev;
  508. int rc;
  509. if (rc_tab->scan == NULL || !rc_tab->size)
  510. return -EINVAL;
  511. ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
  512. if (!ir_dev)
  513. return -ENOMEM;
  514. ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
  515. if (!ir_dev->driver_name) {
  516. rc = -ENOMEM;
  517. goto out_dev;
  518. }
  519. input_dev->getkeycode_new = ir_getkeycode;
  520. input_dev->setkeycode_new = ir_setkeycode;
  521. input_set_drvdata(input_dev, ir_dev);
  522. ir_dev->input_dev = input_dev;
  523. spin_lock_init(&ir_dev->rc_tab.lock);
  524. spin_lock_init(&ir_dev->keylock);
  525. setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
  526. if (props) {
  527. ir_dev->props = props;
  528. if (props->open)
  529. input_dev->open = ir_open;
  530. if (props->close)
  531. input_dev->close = ir_close;
  532. }
  533. set_bit(EV_KEY, input_dev->evbit);
  534. set_bit(EV_REP, input_dev->evbit);
  535. set_bit(EV_MSC, input_dev->evbit);
  536. set_bit(MSC_SCAN, input_dev->mscbit);
  537. rc = ir_setkeytable(ir_dev, rc_tab);
  538. if (rc)
  539. goto out_name;
  540. rc = ir_register_class(input_dev);
  541. if (rc < 0)
  542. goto out_table;
  543. if (ir_dev->props)
  544. if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
  545. rc = ir_raw_event_register(input_dev);
  546. if (rc < 0)
  547. goto out_event;
  548. }
  549. IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
  550. driver_name, rc_tab->name,
  551. (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
  552. " in raw mode" : "");
  553. return 0;
  554. out_event:
  555. ir_unregister_class(input_dev);
  556. out_table:
  557. ir_free_table(&ir_dev->rc_tab);
  558. out_name:
  559. kfree(ir_dev->driver_name);
  560. out_dev:
  561. kfree(ir_dev);
  562. return rc;
  563. }
  564. EXPORT_SYMBOL_GPL(__ir_input_register);
  565. /**
  566. * ir_input_unregister() - unregisters IR and frees resources
  567. * @input_dev: the struct input_dev descriptor of the device
  568. * This routine is used to free memory and de-register interfaces.
  569. */
  570. void ir_input_unregister(struct input_dev *input_dev)
  571. {
  572. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  573. if (!ir_dev)
  574. return;
  575. IR_dprintk(1, "Freed keycode table\n");
  576. del_timer_sync(&ir_dev->timer_keyup);
  577. if (ir_dev->props)
  578. if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
  579. ir_raw_event_unregister(input_dev);
  580. ir_free_table(&ir_dev->rc_tab);
  581. ir_unregister_class(input_dev);
  582. kfree(ir_dev->driver_name);
  583. kfree(ir_dev);
  584. }
  585. EXPORT_SYMBOL_GPL(ir_input_unregister);
  586. int ir_core_debug; /* ir_debug level (0,1,2) */
  587. EXPORT_SYMBOL_GPL(ir_core_debug);
  588. module_param_named(debug, ir_core_debug, int, 0644);
  589. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  590. MODULE_LICENSE("GPL");