rc-main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /* rc-main.c - Remote Controller core module
  2. *
  3. * Copyright (C) 2009-2010 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 <media/rc-core.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/delay.h>
  17. #include <linux/input.h>
  18. #include <linux/slab.h>
  19. #include <linux/device.h>
  20. #include "rc-core-priv.h"
  21. /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
  22. #define IR_TAB_MIN_SIZE 256
  23. #define IR_TAB_MAX_SIZE 8192
  24. /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
  25. #define IR_KEYPRESS_TIMEOUT 250
  26. /* Used to keep track of known keymaps */
  27. static LIST_HEAD(rc_map_list);
  28. static DEFINE_SPINLOCK(rc_map_lock);
  29. static struct rc_map_list *seek_rc_map(const char *name)
  30. {
  31. struct rc_map_list *map = NULL;
  32. spin_lock(&rc_map_lock);
  33. list_for_each_entry(map, &rc_map_list, list) {
  34. if (!strcmp(name, map->map.name)) {
  35. spin_unlock(&rc_map_lock);
  36. return map;
  37. }
  38. }
  39. spin_unlock(&rc_map_lock);
  40. return NULL;
  41. }
  42. struct rc_map *rc_map_get(const char *name)
  43. {
  44. struct rc_map_list *map;
  45. map = seek_rc_map(name);
  46. #ifdef MODULE
  47. if (!map) {
  48. int rc = request_module(name);
  49. if (rc < 0) {
  50. printk(KERN_ERR "Couldn't load IR keymap %s\n", name);
  51. return NULL;
  52. }
  53. msleep(20); /* Give some time for IR to register */
  54. map = seek_rc_map(name);
  55. }
  56. #endif
  57. if (!map) {
  58. printk(KERN_ERR "IR keymap %s not found\n", name);
  59. return NULL;
  60. }
  61. printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
  62. return &map->map;
  63. }
  64. EXPORT_SYMBOL_GPL(rc_map_get);
  65. int rc_map_register(struct rc_map_list *map)
  66. {
  67. spin_lock(&rc_map_lock);
  68. list_add_tail(&map->list, &rc_map_list);
  69. spin_unlock(&rc_map_lock);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL_GPL(rc_map_register);
  73. void rc_map_unregister(struct rc_map_list *map)
  74. {
  75. spin_lock(&rc_map_lock);
  76. list_del(&map->list);
  77. spin_unlock(&rc_map_lock);
  78. }
  79. EXPORT_SYMBOL_GPL(rc_map_unregister);
  80. static struct rc_map_table empty[] = {
  81. { 0x2a, KEY_COFFEE },
  82. };
  83. static struct rc_map_list empty_map = {
  84. .map = {
  85. .scan = empty,
  86. .size = ARRAY_SIZE(empty),
  87. .rc_type = RC_TYPE_UNKNOWN, /* Legacy IR type */
  88. .name = RC_MAP_EMPTY,
  89. }
  90. };
  91. /**
  92. * ir_create_table() - initializes a scancode table
  93. * @rc_map: the rc_map to initialize
  94. * @name: name to assign to the table
  95. * @rc_type: ir type to assign to the new table
  96. * @size: initial size of the table
  97. * @return: zero on success or a negative error code
  98. *
  99. * This routine will initialize the rc_map and will allocate
  100. * memory to hold at least the specified number of elements.
  101. */
  102. static int ir_create_table(struct rc_map *rc_map,
  103. const char *name, u64 rc_type, size_t size)
  104. {
  105. rc_map->name = name;
  106. rc_map->rc_type = rc_type;
  107. rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
  108. rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
  109. rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
  110. if (!rc_map->scan)
  111. return -ENOMEM;
  112. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  113. rc_map->size, rc_map->alloc);
  114. return 0;
  115. }
  116. /**
  117. * ir_free_table() - frees memory allocated by a scancode table
  118. * @rc_map: the table whose mappings need to be freed
  119. *
  120. * This routine will free memory alloctaed for key mappings used by given
  121. * scancode table.
  122. */
  123. static void ir_free_table(struct rc_map *rc_map)
  124. {
  125. rc_map->size = 0;
  126. kfree(rc_map->scan);
  127. rc_map->scan = NULL;
  128. }
  129. /**
  130. * ir_resize_table() - resizes a scancode table if necessary
  131. * @rc_map: the rc_map to resize
  132. * @gfp_flags: gfp flags to use when allocating memory
  133. * @return: zero on success or a negative error code
  134. *
  135. * This routine will shrink the rc_map if it has lots of
  136. * unused entries and grow it if it is full.
  137. */
  138. static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
  139. {
  140. unsigned int oldalloc = rc_map->alloc;
  141. unsigned int newalloc = oldalloc;
  142. struct rc_map_table *oldscan = rc_map->scan;
  143. struct rc_map_table *newscan;
  144. if (rc_map->size == rc_map->len) {
  145. /* All entries in use -> grow keytable */
  146. if (rc_map->alloc >= IR_TAB_MAX_SIZE)
  147. return -ENOMEM;
  148. newalloc *= 2;
  149. IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
  150. }
  151. if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
  152. /* Less than 1/3 of entries in use -> shrink keytable */
  153. newalloc /= 2;
  154. IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
  155. }
  156. if (newalloc == oldalloc)
  157. return 0;
  158. newscan = kmalloc(newalloc, gfp_flags);
  159. if (!newscan) {
  160. IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
  161. return -ENOMEM;
  162. }
  163. memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
  164. rc_map->scan = newscan;
  165. rc_map->alloc = newalloc;
  166. rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
  167. kfree(oldscan);
  168. return 0;
  169. }
  170. /**
  171. * ir_update_mapping() - set a keycode in the scancode->keycode table
  172. * @dev: the struct rc_dev device descriptor
  173. * @rc_map: scancode table to be adjusted
  174. * @index: index of the mapping that needs to be updated
  175. * @keycode: the desired keycode
  176. * @return: previous keycode assigned to the mapping
  177. *
  178. * This routine is used to update scancode->keycode mapping at given
  179. * position.
  180. */
  181. static unsigned int ir_update_mapping(struct rc_dev *dev,
  182. struct rc_map *rc_map,
  183. unsigned int index,
  184. unsigned int new_keycode)
  185. {
  186. int old_keycode = rc_map->scan[index].keycode;
  187. int i;
  188. /* Did the user wish to remove the mapping? */
  189. if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
  190. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
  191. index, rc_map->scan[index].scancode);
  192. rc_map->len--;
  193. memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
  194. (rc_map->len - index) * sizeof(struct rc_map_table));
  195. } else {
  196. IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
  197. index,
  198. old_keycode == KEY_RESERVED ? "New" : "Replacing",
  199. rc_map->scan[index].scancode, new_keycode);
  200. rc_map->scan[index].keycode = new_keycode;
  201. __set_bit(new_keycode, dev->input_dev->keybit);
  202. }
  203. if (old_keycode != KEY_RESERVED) {
  204. /* A previous mapping was updated... */
  205. __clear_bit(old_keycode, dev->input_dev->keybit);
  206. /* ... but another scancode might use the same keycode */
  207. for (i = 0; i < rc_map->len; i++) {
  208. if (rc_map->scan[i].keycode == old_keycode) {
  209. __set_bit(old_keycode, dev->input_dev->keybit);
  210. break;
  211. }
  212. }
  213. /* Possibly shrink the keytable, failure is not a problem */
  214. ir_resize_table(rc_map, GFP_ATOMIC);
  215. }
  216. return old_keycode;
  217. }
  218. /**
  219. * ir_establish_scancode() - set a keycode in the scancode->keycode table
  220. * @dev: the struct rc_dev device descriptor
  221. * @rc_map: scancode table to be searched
  222. * @scancode: the desired scancode
  223. * @resize: controls whether we allowed to resize the table to
  224. * accomodate not yet present scancodes
  225. * @return: index of the mapping containing scancode in question
  226. * or -1U in case of failure.
  227. *
  228. * This routine is used to locate given scancode in rc_map.
  229. * If scancode is not yet present the routine will allocate a new slot
  230. * for it.
  231. */
  232. static unsigned int ir_establish_scancode(struct rc_dev *dev,
  233. struct rc_map *rc_map,
  234. unsigned int scancode,
  235. bool resize)
  236. {
  237. unsigned int i;
  238. /*
  239. * Unfortunately, some hardware-based IR decoders don't provide
  240. * all bits for the complete IR code. In general, they provide only
  241. * the command part of the IR code. Yet, as it is possible to replace
  242. * the provided IR with another one, it is needed to allow loading
  243. * IR tables from other remotes. So, we support specifying a mask to
  244. * indicate the valid bits of the scancodes.
  245. */
  246. if (dev->scanmask)
  247. scancode &= dev->scanmask;
  248. /* First check if we already have a mapping for this ir command */
  249. for (i = 0; i < rc_map->len; i++) {
  250. if (rc_map->scan[i].scancode == scancode)
  251. return i;
  252. /* Keytable is sorted from lowest to highest scancode */
  253. if (rc_map->scan[i].scancode >= scancode)
  254. break;
  255. }
  256. /* No previous mapping found, we might need to grow the table */
  257. if (rc_map->size == rc_map->len) {
  258. if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
  259. return -1U;
  260. }
  261. /* i is the proper index to insert our new keycode */
  262. if (i < rc_map->len)
  263. memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
  264. (rc_map->len - i) * sizeof(struct rc_map_table));
  265. rc_map->scan[i].scancode = scancode;
  266. rc_map->scan[i].keycode = KEY_RESERVED;
  267. rc_map->len++;
  268. return i;
  269. }
  270. /**
  271. * ir_setkeycode() - set a keycode in the scancode->keycode table
  272. * @idev: the struct input_dev device descriptor
  273. * @scancode: the desired scancode
  274. * @keycode: result
  275. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  276. *
  277. * This routine is used to handle evdev EVIOCSKEY ioctl.
  278. */
  279. static int ir_setkeycode(struct input_dev *idev,
  280. const struct input_keymap_entry *ke,
  281. unsigned int *old_keycode)
  282. {
  283. struct rc_dev *rdev = input_get_drvdata(idev);
  284. struct rc_map *rc_map = &rdev->rc_map;
  285. unsigned int index;
  286. unsigned int scancode;
  287. int retval = 0;
  288. unsigned long flags;
  289. spin_lock_irqsave(&rc_map->lock, flags);
  290. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  291. index = ke->index;
  292. if (index >= rc_map->len) {
  293. retval = -EINVAL;
  294. goto out;
  295. }
  296. } else {
  297. retval = input_scancode_to_scalar(ke, &scancode);
  298. if (retval)
  299. goto out;
  300. index = ir_establish_scancode(rdev, rc_map, scancode, true);
  301. if (index >= rc_map->len) {
  302. retval = -ENOMEM;
  303. goto out;
  304. }
  305. }
  306. *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
  307. out:
  308. spin_unlock_irqrestore(&rc_map->lock, flags);
  309. return retval;
  310. }
  311. /**
  312. * ir_setkeytable() - sets several entries in the scancode->keycode table
  313. * @dev: the struct rc_dev device descriptor
  314. * @to: the struct rc_map to copy entries to
  315. * @from: the struct rc_map to copy entries from
  316. * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
  317. *
  318. * This routine is used to handle table initialization.
  319. */
  320. static int ir_setkeytable(struct rc_dev *dev,
  321. const struct rc_map *from)
  322. {
  323. struct rc_map *rc_map = &dev->rc_map;
  324. unsigned int i, index;
  325. int rc;
  326. rc = ir_create_table(rc_map, from->name,
  327. from->rc_type, from->size);
  328. if (rc)
  329. return rc;
  330. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  331. rc_map->size, rc_map->alloc);
  332. for (i = 0; i < from->size; i++) {
  333. index = ir_establish_scancode(dev, rc_map,
  334. from->scan[i].scancode, false);
  335. if (index >= rc_map->len) {
  336. rc = -ENOMEM;
  337. break;
  338. }
  339. ir_update_mapping(dev, rc_map, index,
  340. from->scan[i].keycode);
  341. }
  342. if (rc)
  343. ir_free_table(rc_map);
  344. return rc;
  345. }
  346. /**
  347. * ir_lookup_by_scancode() - locate mapping by scancode
  348. * @rc_map: the struct rc_map to search
  349. * @scancode: scancode to look for in the table
  350. * @return: index in the table, -1U if not found
  351. *
  352. * This routine performs binary search in RC keykeymap table for
  353. * given scancode.
  354. */
  355. static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
  356. unsigned int scancode)
  357. {
  358. int start = 0;
  359. int end = rc_map->len - 1;
  360. int mid;
  361. while (start <= end) {
  362. mid = (start + end) / 2;
  363. if (rc_map->scan[mid].scancode < scancode)
  364. start = mid + 1;
  365. else if (rc_map->scan[mid].scancode > scancode)
  366. end = mid - 1;
  367. else
  368. return mid;
  369. }
  370. return -1U;
  371. }
  372. /**
  373. * ir_getkeycode() - get a keycode from the scancode->keycode table
  374. * @idev: the struct input_dev device descriptor
  375. * @scancode: the desired scancode
  376. * @keycode: used to return the keycode, if found, or KEY_RESERVED
  377. * @return: always returns zero.
  378. *
  379. * This routine is used to handle evdev EVIOCGKEY ioctl.
  380. */
  381. static int ir_getkeycode(struct input_dev *idev,
  382. struct input_keymap_entry *ke)
  383. {
  384. struct rc_dev *rdev = input_get_drvdata(idev);
  385. struct rc_map *rc_map = &rdev->rc_map;
  386. struct rc_map_table *entry;
  387. unsigned long flags;
  388. unsigned int index;
  389. unsigned int scancode;
  390. int retval;
  391. spin_lock_irqsave(&rc_map->lock, flags);
  392. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  393. index = ke->index;
  394. } else {
  395. retval = input_scancode_to_scalar(ke, &scancode);
  396. if (retval)
  397. goto out;
  398. index = ir_lookup_by_scancode(rc_map, scancode);
  399. }
  400. if (index >= rc_map->len) {
  401. if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
  402. IR_dprintk(1, "unknown key for scancode 0x%04x\n",
  403. scancode);
  404. retval = -EINVAL;
  405. goto out;
  406. }
  407. entry = &rc_map->scan[index];
  408. ke->index = index;
  409. ke->keycode = entry->keycode;
  410. ke->len = sizeof(entry->scancode);
  411. memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
  412. retval = 0;
  413. out:
  414. spin_unlock_irqrestore(&rc_map->lock, flags);
  415. return retval;
  416. }
  417. /**
  418. * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  419. * @dev: the struct rc_dev descriptor of the device
  420. * @scancode: the scancode to look for
  421. * @return: the corresponding keycode, or KEY_RESERVED
  422. *
  423. * This routine is used by drivers which need to convert a scancode to a
  424. * keycode. Normally it should not be used since drivers should have no
  425. * interest in keycodes.
  426. */
  427. u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
  428. {
  429. struct rc_map *rc_map = &dev->rc_map;
  430. unsigned int keycode;
  431. unsigned int index;
  432. unsigned long flags;
  433. spin_lock_irqsave(&rc_map->lock, flags);
  434. index = ir_lookup_by_scancode(rc_map, scancode);
  435. keycode = index < rc_map->len ?
  436. rc_map->scan[index].keycode : KEY_RESERVED;
  437. spin_unlock_irqrestore(&rc_map->lock, flags);
  438. if (keycode != KEY_RESERVED)
  439. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  440. dev->input_name, scancode, keycode);
  441. return keycode;
  442. }
  443. EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
  444. /**
  445. * ir_do_keyup() - internal function to signal the release of a keypress
  446. * @dev: the struct rc_dev descriptor of the device
  447. *
  448. * This function is used internally to release a keypress, it must be
  449. * called with keylock held.
  450. */
  451. static void ir_do_keyup(struct rc_dev *dev)
  452. {
  453. if (!dev->keypressed)
  454. return;
  455. IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
  456. input_report_key(dev->input_dev, dev->last_keycode, 0);
  457. input_sync(dev->input_dev);
  458. dev->keypressed = false;
  459. }
  460. /**
  461. * rc_keyup() - signals the release of a keypress
  462. * @dev: the struct rc_dev descriptor of the device
  463. *
  464. * This routine is used to signal that a key has been released on the
  465. * remote control.
  466. */
  467. void rc_keyup(struct rc_dev *dev)
  468. {
  469. unsigned long flags;
  470. spin_lock_irqsave(&dev->keylock, flags);
  471. ir_do_keyup(dev);
  472. spin_unlock_irqrestore(&dev->keylock, flags);
  473. }
  474. EXPORT_SYMBOL_GPL(rc_keyup);
  475. /**
  476. * ir_timer_keyup() - generates a keyup event after a timeout
  477. * @cookie: a pointer to the struct rc_dev for the device
  478. *
  479. * This routine will generate a keyup event some time after a keydown event
  480. * is generated when no further activity has been detected.
  481. */
  482. static void ir_timer_keyup(unsigned long cookie)
  483. {
  484. struct rc_dev *dev = (struct rc_dev *)cookie;
  485. unsigned long flags;
  486. /*
  487. * ir->keyup_jiffies is used to prevent a race condition if a
  488. * hardware interrupt occurs at this point and the keyup timer
  489. * event is moved further into the future as a result.
  490. *
  491. * The timer will then be reactivated and this function called
  492. * again in the future. We need to exit gracefully in that case
  493. * to allow the input subsystem to do its auto-repeat magic or
  494. * a keyup event might follow immediately after the keydown.
  495. */
  496. spin_lock_irqsave(&dev->keylock, flags);
  497. if (time_is_before_eq_jiffies(dev->keyup_jiffies))
  498. ir_do_keyup(dev);
  499. spin_unlock_irqrestore(&dev->keylock, flags);
  500. }
  501. /**
  502. * rc_repeat() - signals that a key is still pressed
  503. * @dev: the struct rc_dev descriptor of the device
  504. *
  505. * This routine is used by IR decoders when a repeat message which does
  506. * not include the necessary bits to reproduce the scancode has been
  507. * received.
  508. */
  509. void rc_repeat(struct rc_dev *dev)
  510. {
  511. unsigned long flags;
  512. spin_lock_irqsave(&dev->keylock, flags);
  513. input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
  514. if (!dev->keypressed)
  515. goto out;
  516. dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  517. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  518. out:
  519. spin_unlock_irqrestore(&dev->keylock, flags);
  520. }
  521. EXPORT_SYMBOL_GPL(rc_repeat);
  522. /**
  523. * ir_do_keydown() - internal function to process a keypress
  524. * @dev: the struct rc_dev descriptor of the device
  525. * @scancode: the scancode of the keypress
  526. * @keycode: the keycode of the keypress
  527. * @toggle: the toggle value of the keypress
  528. *
  529. * This function is used internally to register a keypress, it must be
  530. * called with keylock held.
  531. */
  532. static void ir_do_keydown(struct rc_dev *dev, int scancode,
  533. u32 keycode, u8 toggle)
  534. {
  535. input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
  536. /* Repeat event? */
  537. if (dev->keypressed &&
  538. dev->last_scancode == scancode &&
  539. dev->last_toggle == toggle)
  540. return;
  541. /* Release old keypress */
  542. ir_do_keyup(dev);
  543. dev->last_scancode = scancode;
  544. dev->last_toggle = toggle;
  545. dev->last_keycode = keycode;
  546. if (keycode == KEY_RESERVED)
  547. return;
  548. /* Register a keypress */
  549. dev->keypressed = true;
  550. IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
  551. dev->input_name, keycode, scancode);
  552. input_report_key(dev->input_dev, dev->last_keycode, 1);
  553. input_sync(dev->input_dev);
  554. }
  555. /**
  556. * rc_keydown() - generates input event for a key press
  557. * @dev: the struct rc_dev descriptor of the device
  558. * @scancode: the scancode that we're seeking
  559. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  560. * support toggle values, this should be set to zero)
  561. *
  562. * This routine is used to signal that a key has been pressed on the
  563. * remote control.
  564. */
  565. void rc_keydown(struct rc_dev *dev, int scancode, u8 toggle)
  566. {
  567. unsigned long flags;
  568. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  569. spin_lock_irqsave(&dev->keylock, flags);
  570. ir_do_keydown(dev, scancode, keycode, toggle);
  571. if (dev->keypressed) {
  572. dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  573. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  574. }
  575. spin_unlock_irqrestore(&dev->keylock, flags);
  576. }
  577. EXPORT_SYMBOL_GPL(rc_keydown);
  578. /**
  579. * rc_keydown_notimeout() - generates input event for a key press without
  580. * an automatic keyup event at a later time
  581. * @dev: the struct rc_dev descriptor of the device
  582. * @scancode: the scancode that we're seeking
  583. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  584. * support toggle values, this should be set to zero)
  585. *
  586. * This routine is used to signal that a key has been pressed on the
  587. * remote control. The driver must manually call rc_keyup() at a later stage.
  588. */
  589. void rc_keydown_notimeout(struct rc_dev *dev, int scancode, u8 toggle)
  590. {
  591. unsigned long flags;
  592. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  593. spin_lock_irqsave(&dev->keylock, flags);
  594. ir_do_keydown(dev, scancode, keycode, toggle);
  595. spin_unlock_irqrestore(&dev->keylock, flags);
  596. }
  597. EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
  598. static int ir_open(struct input_dev *idev)
  599. {
  600. struct rc_dev *rdev = input_get_drvdata(idev);
  601. return rdev->open(rdev);
  602. }
  603. static void ir_close(struct input_dev *idev)
  604. {
  605. struct rc_dev *rdev = input_get_drvdata(idev);
  606. rdev->close(rdev);
  607. }
  608. /* class for /sys/class/rc */
  609. static char *ir_devnode(struct device *dev, mode_t *mode)
  610. {
  611. return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
  612. }
  613. static struct class ir_input_class = {
  614. .name = "rc",
  615. .devnode = ir_devnode,
  616. };
  617. static struct {
  618. u64 type;
  619. char *name;
  620. } proto_names[] = {
  621. { RC_TYPE_UNKNOWN, "unknown" },
  622. { RC_TYPE_RC5, "rc-5" },
  623. { RC_TYPE_NEC, "nec" },
  624. { RC_TYPE_RC6, "rc-6" },
  625. { RC_TYPE_JVC, "jvc" },
  626. { RC_TYPE_SONY, "sony" },
  627. { RC_TYPE_RC5_SZ, "rc-5-sz" },
  628. { RC_TYPE_LIRC, "lirc" },
  629. };
  630. #define PROTO_NONE "none"
  631. /**
  632. * show_protocols() - shows the current IR protocol(s)
  633. * @device: the device descriptor
  634. * @mattr: the device attribute struct (unused)
  635. * @buf: a pointer to the output buffer
  636. *
  637. * This routine is a callback routine for input read the IR protocol type(s).
  638. * it is trigged by reading /sys/class/rc/rc?/protocols.
  639. * It returns the protocol names of supported protocols.
  640. * Enabled protocols are printed in brackets.
  641. */
  642. static ssize_t show_protocols(struct device *device,
  643. struct device_attribute *mattr, char *buf)
  644. {
  645. struct rc_dev *dev = to_rc_dev(device);
  646. u64 allowed, enabled;
  647. char *tmp = buf;
  648. int i;
  649. /* Device is being removed */
  650. if (!dev)
  651. return -EINVAL;
  652. if (dev->driver_type == RC_DRIVER_SCANCODE) {
  653. enabled = dev->rc_map.rc_type;
  654. allowed = dev->allowed_protos;
  655. } else {
  656. enabled = dev->raw->enabled_protocols;
  657. allowed = ir_raw_get_allowed_protocols();
  658. }
  659. IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
  660. (long long)allowed,
  661. (long long)enabled);
  662. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  663. if (allowed & enabled & proto_names[i].type)
  664. tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
  665. else if (allowed & proto_names[i].type)
  666. tmp += sprintf(tmp, "%s ", proto_names[i].name);
  667. }
  668. if (tmp != buf)
  669. tmp--;
  670. *tmp = '\n';
  671. return tmp + 1 - buf;
  672. }
  673. /**
  674. * store_protocols() - changes the current IR protocol(s)
  675. * @device: the device descriptor
  676. * @mattr: the device attribute struct (unused)
  677. * @buf: a pointer to the input buffer
  678. * @len: length of the input buffer
  679. *
  680. * This routine is for changing the IR protocol type.
  681. * It is trigged by writing to /sys/class/rc/rc?/protocols.
  682. * Writing "+proto" will add a protocol to the list of enabled protocols.
  683. * Writing "-proto" will remove a protocol from the list of enabled protocols.
  684. * Writing "proto" will enable only "proto".
  685. * Writing "none" will disable all protocols.
  686. * Returns -EINVAL if an invalid protocol combination or unknown protocol name
  687. * is used, otherwise @len.
  688. */
  689. static ssize_t store_protocols(struct device *device,
  690. struct device_attribute *mattr,
  691. const char *data,
  692. size_t len)
  693. {
  694. struct rc_dev *dev = to_rc_dev(device);
  695. bool enable, disable;
  696. const char *tmp;
  697. u64 type;
  698. u64 mask;
  699. int rc, i, count = 0;
  700. unsigned long flags;
  701. /* Device is being removed */
  702. if (!dev)
  703. return -EINVAL;
  704. if (dev->driver_type == RC_DRIVER_SCANCODE)
  705. type = dev->rc_map.rc_type;
  706. else if (dev->raw)
  707. type = dev->raw->enabled_protocols;
  708. else {
  709. IR_dprintk(1, "Protocol switching not supported\n");
  710. return -EINVAL;
  711. }
  712. while ((tmp = strsep((char **) &data, " \n")) != NULL) {
  713. if (!*tmp)
  714. break;
  715. if (*tmp == '+') {
  716. enable = true;
  717. disable = false;
  718. tmp++;
  719. } else if (*tmp == '-') {
  720. enable = false;
  721. disable = true;
  722. tmp++;
  723. } else {
  724. enable = false;
  725. disable = false;
  726. }
  727. if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
  728. tmp += sizeof(PROTO_NONE);
  729. mask = 0;
  730. count++;
  731. } else {
  732. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  733. if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
  734. tmp += strlen(proto_names[i].name);
  735. mask = proto_names[i].type;
  736. break;
  737. }
  738. }
  739. if (i == ARRAY_SIZE(proto_names)) {
  740. IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
  741. return -EINVAL;
  742. }
  743. count++;
  744. }
  745. if (enable)
  746. type |= mask;
  747. else if (disable)
  748. type &= ~mask;
  749. else
  750. type = mask;
  751. }
  752. if (!count) {
  753. IR_dprintk(1, "Protocol not specified\n");
  754. return -EINVAL;
  755. }
  756. if (dev->change_protocol) {
  757. rc = dev->change_protocol(dev, type);
  758. if (rc < 0) {
  759. IR_dprintk(1, "Error setting protocols to 0x%llx\n",
  760. (long long)type);
  761. return -EINVAL;
  762. }
  763. }
  764. if (dev->driver_type == RC_DRIVER_SCANCODE) {
  765. spin_lock_irqsave(&dev->rc_map.lock, flags);
  766. dev->rc_map.rc_type = type;
  767. spin_unlock_irqrestore(&dev->rc_map.lock, flags);
  768. } else {
  769. dev->raw->enabled_protocols = type;
  770. }
  771. IR_dprintk(1, "Current protocol(s): 0x%llx\n",
  772. (long long)type);
  773. return len;
  774. }
  775. static void rc_dev_release(struct device *device)
  776. {
  777. struct rc_dev *dev = to_rc_dev(device);
  778. kfree(dev);
  779. module_put(THIS_MODULE);
  780. }
  781. #define ADD_HOTPLUG_VAR(fmt, val...) \
  782. do { \
  783. int err = add_uevent_var(env, fmt, val); \
  784. if (err) \
  785. return err; \
  786. } while (0)
  787. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  788. {
  789. struct rc_dev *dev = to_rc_dev(device);
  790. if (dev->rc_map.name)
  791. ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
  792. if (dev->driver_name)
  793. ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
  794. return 0;
  795. }
  796. /*
  797. * Static device attribute struct with the sysfs attributes for IR's
  798. */
  799. static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
  800. show_protocols, store_protocols);
  801. static struct attribute *rc_dev_attrs[] = {
  802. &dev_attr_protocols.attr,
  803. NULL,
  804. };
  805. static struct attribute_group rc_dev_attr_grp = {
  806. .attrs = rc_dev_attrs,
  807. };
  808. static const struct attribute_group *rc_dev_attr_groups[] = {
  809. &rc_dev_attr_grp,
  810. NULL
  811. };
  812. static struct device_type rc_dev_type = {
  813. .groups = rc_dev_attr_groups,
  814. .release = rc_dev_release,
  815. .uevent = rc_dev_uevent,
  816. };
  817. struct rc_dev *rc_allocate_device(void)
  818. {
  819. struct rc_dev *dev;
  820. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  821. if (!dev)
  822. return NULL;
  823. dev->input_dev = input_allocate_device();
  824. if (!dev->input_dev) {
  825. kfree(dev);
  826. return NULL;
  827. }
  828. dev->input_dev->getkeycode_new = ir_getkeycode;
  829. dev->input_dev->setkeycode_new = ir_setkeycode;
  830. input_set_drvdata(dev->input_dev, dev);
  831. spin_lock_init(&dev->rc_map.lock);
  832. spin_lock_init(&dev->keylock);
  833. setup_timer(&dev->timer_keyup, ir_timer_keyup, (unsigned long)dev);
  834. dev->dev.type = &rc_dev_type;
  835. dev->dev.class = &ir_input_class;
  836. device_initialize(&dev->dev);
  837. __module_get(THIS_MODULE);
  838. return dev;
  839. }
  840. EXPORT_SYMBOL_GPL(rc_allocate_device);
  841. void rc_free_device(struct rc_dev *dev)
  842. {
  843. if (dev) {
  844. input_free_device(dev->input_dev);
  845. put_device(&dev->dev);
  846. }
  847. }
  848. EXPORT_SYMBOL_GPL(rc_free_device);
  849. int rc_register_device(struct rc_dev *dev)
  850. {
  851. static atomic_t devno = ATOMIC_INIT(0);
  852. struct rc_map *rc_map;
  853. const char *path;
  854. int rc;
  855. if (!dev || !dev->map_name)
  856. return -EINVAL;
  857. rc_map = rc_map_get(dev->map_name);
  858. if (!rc_map)
  859. rc_map = rc_map_get(RC_MAP_EMPTY);
  860. if (!rc_map || !rc_map->scan || rc_map->size == 0)
  861. return -EINVAL;
  862. set_bit(EV_KEY, dev->input_dev->evbit);
  863. set_bit(EV_REP, dev->input_dev->evbit);
  864. set_bit(EV_MSC, dev->input_dev->evbit);
  865. set_bit(MSC_SCAN, dev->input_dev->mscbit);
  866. if (dev->open)
  867. dev->input_dev->open = ir_open;
  868. if (dev->close)
  869. dev->input_dev->close = ir_close;
  870. dev->devno = (unsigned long)(atomic_inc_return(&devno) - 1);
  871. dev_set_name(&dev->dev, "rc%ld", dev->devno);
  872. dev_set_drvdata(&dev->dev, dev);
  873. rc = device_add(&dev->dev);
  874. if (rc)
  875. return rc;
  876. rc = ir_setkeytable(dev, rc_map);
  877. if (rc)
  878. goto out_dev;
  879. dev->input_dev->dev.parent = &dev->dev;
  880. memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
  881. dev->input_dev->phys = dev->input_phys;
  882. dev->input_dev->name = dev->input_name;
  883. rc = input_register_device(dev->input_dev);
  884. if (rc)
  885. goto out_table;
  886. /*
  887. * Default delay of 250ms is too short for some protocols, expecially
  888. * since the timeout is currently set to 250ms. Increase it to 500ms,
  889. * to avoid wrong repetition of the keycodes. Note that this must be
  890. * set after the call to input_register_device().
  891. */
  892. dev->input_dev->rep[REP_DELAY] = 500;
  893. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  894. printk(KERN_INFO "%s: %s as %s\n",
  895. dev_name(&dev->dev),
  896. dev->input_name ? dev->input_name : "Unspecified device",
  897. path ? path : "N/A");
  898. kfree(path);
  899. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  900. rc = ir_raw_event_register(dev);
  901. if (rc < 0)
  902. goto out_input;
  903. }
  904. if (dev->change_protocol) {
  905. rc = dev->change_protocol(dev, rc_map->rc_type);
  906. if (rc < 0)
  907. goto out_raw;
  908. }
  909. IR_dprintk(1, "Registered rc%ld (driver: %s, remote: %s, mode %s)\n",
  910. dev->devno,
  911. dev->driver_name ? dev->driver_name : "unknown",
  912. rc_map->name ? rc_map->name : "unknown",
  913. dev->driver_type == RC_DRIVER_IR_RAW ? "raw" : "cooked");
  914. return 0;
  915. out_raw:
  916. if (dev->driver_type == RC_DRIVER_IR_RAW)
  917. ir_raw_event_unregister(dev);
  918. out_input:
  919. input_unregister_device(dev->input_dev);
  920. dev->input_dev = NULL;
  921. out_table:
  922. ir_free_table(&dev->rc_map);
  923. out_dev:
  924. device_del(&dev->dev);
  925. return rc;
  926. }
  927. EXPORT_SYMBOL_GPL(rc_register_device);
  928. void rc_unregister_device(struct rc_dev *dev)
  929. {
  930. if (!dev)
  931. return;
  932. del_timer_sync(&dev->timer_keyup);
  933. if (dev->driver_type == RC_DRIVER_IR_RAW)
  934. ir_raw_event_unregister(dev);
  935. input_unregister_device(dev->input_dev);
  936. dev->input_dev = NULL;
  937. ir_free_table(&dev->rc_map);
  938. IR_dprintk(1, "Freed keycode table\n");
  939. device_unregister(&dev->dev);
  940. }
  941. EXPORT_SYMBOL_GPL(rc_unregister_device);
  942. /*
  943. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  944. */
  945. static int __init rc_core_init(void)
  946. {
  947. int rc = class_register(&ir_input_class);
  948. if (rc) {
  949. printk(KERN_ERR "rc_core: unable to register rc class\n");
  950. return rc;
  951. }
  952. /* Initialize/load the decoders/keymap code that will be used */
  953. ir_raw_init();
  954. rc_map_register(&empty_map);
  955. return 0;
  956. }
  957. static void __exit rc_core_exit(void)
  958. {
  959. class_unregister(&ir_input_class);
  960. rc_map_unregister(&empty_map);
  961. }
  962. module_init(rc_core_init);
  963. module_exit(rc_core_exit);
  964. int rc_core_debug; /* ir_debug level (0,1,2) */
  965. EXPORT_SYMBOL_GPL(rc_core_debug);
  966. module_param_named(debug, rc_core_debug, int, 0644);
  967. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  968. MODULE_LICENSE("GPL");