rc-main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. * accommodate 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. entry = &rc_map->scan[index];
  402. ke->index = index;
  403. ke->keycode = entry->keycode;
  404. ke->len = sizeof(entry->scancode);
  405. memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
  406. } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
  407. /*
  408. * We do not really know the valid range of scancodes
  409. * so let's respond with KEY_RESERVED to anything we
  410. * do not have mapping for [yet].
  411. */
  412. ke->index = index;
  413. ke->keycode = KEY_RESERVED;
  414. } else {
  415. retval = -EINVAL;
  416. goto out;
  417. }
  418. retval = 0;
  419. out:
  420. spin_unlock_irqrestore(&rc_map->lock, flags);
  421. return retval;
  422. }
  423. /**
  424. * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  425. * @dev: the struct rc_dev descriptor of the device
  426. * @scancode: the scancode to look for
  427. * @return: the corresponding keycode, or KEY_RESERVED
  428. *
  429. * This routine is used by drivers which need to convert a scancode to a
  430. * keycode. Normally it should not be used since drivers should have no
  431. * interest in keycodes.
  432. */
  433. u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
  434. {
  435. struct rc_map *rc_map = &dev->rc_map;
  436. unsigned int keycode;
  437. unsigned int index;
  438. unsigned long flags;
  439. spin_lock_irqsave(&rc_map->lock, flags);
  440. index = ir_lookup_by_scancode(rc_map, scancode);
  441. keycode = index < rc_map->len ?
  442. rc_map->scan[index].keycode : KEY_RESERVED;
  443. spin_unlock_irqrestore(&rc_map->lock, flags);
  444. if (keycode != KEY_RESERVED)
  445. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  446. dev->input_name, scancode, keycode);
  447. return keycode;
  448. }
  449. EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
  450. /**
  451. * ir_do_keyup() - internal function to signal the release of a keypress
  452. * @dev: the struct rc_dev descriptor of the device
  453. *
  454. * This function is used internally to release a keypress, it must be
  455. * called with keylock held.
  456. */
  457. static void ir_do_keyup(struct rc_dev *dev)
  458. {
  459. if (!dev->keypressed)
  460. return;
  461. IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
  462. input_report_key(dev->input_dev, dev->last_keycode, 0);
  463. input_sync(dev->input_dev);
  464. dev->keypressed = false;
  465. }
  466. /**
  467. * rc_keyup() - signals the release of a keypress
  468. * @dev: the struct rc_dev descriptor of the device
  469. *
  470. * This routine is used to signal that a key has been released on the
  471. * remote control.
  472. */
  473. void rc_keyup(struct rc_dev *dev)
  474. {
  475. unsigned long flags;
  476. spin_lock_irqsave(&dev->keylock, flags);
  477. ir_do_keyup(dev);
  478. spin_unlock_irqrestore(&dev->keylock, flags);
  479. }
  480. EXPORT_SYMBOL_GPL(rc_keyup);
  481. /**
  482. * ir_timer_keyup() - generates a keyup event after a timeout
  483. * @cookie: a pointer to the struct rc_dev for the device
  484. *
  485. * This routine will generate a keyup event some time after a keydown event
  486. * is generated when no further activity has been detected.
  487. */
  488. static void ir_timer_keyup(unsigned long cookie)
  489. {
  490. struct rc_dev *dev = (struct rc_dev *)cookie;
  491. unsigned long flags;
  492. /*
  493. * ir->keyup_jiffies is used to prevent a race condition if a
  494. * hardware interrupt occurs at this point and the keyup timer
  495. * event is moved further into the future as a result.
  496. *
  497. * The timer will then be reactivated and this function called
  498. * again in the future. We need to exit gracefully in that case
  499. * to allow the input subsystem to do its auto-repeat magic or
  500. * a keyup event might follow immediately after the keydown.
  501. */
  502. spin_lock_irqsave(&dev->keylock, flags);
  503. if (time_is_before_eq_jiffies(dev->keyup_jiffies))
  504. ir_do_keyup(dev);
  505. spin_unlock_irqrestore(&dev->keylock, flags);
  506. }
  507. /**
  508. * rc_repeat() - signals that a key is still pressed
  509. * @dev: the struct rc_dev descriptor of the device
  510. *
  511. * This routine is used by IR decoders when a repeat message which does
  512. * not include the necessary bits to reproduce the scancode has been
  513. * received.
  514. */
  515. void rc_repeat(struct rc_dev *dev)
  516. {
  517. unsigned long flags;
  518. spin_lock_irqsave(&dev->keylock, flags);
  519. input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
  520. if (!dev->keypressed)
  521. goto out;
  522. dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  523. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  524. out:
  525. spin_unlock_irqrestore(&dev->keylock, flags);
  526. }
  527. EXPORT_SYMBOL_GPL(rc_repeat);
  528. /**
  529. * ir_do_keydown() - internal function to process a keypress
  530. * @dev: the struct rc_dev descriptor of the device
  531. * @scancode: the scancode of the keypress
  532. * @keycode: the keycode of the keypress
  533. * @toggle: the toggle value of the keypress
  534. *
  535. * This function is used internally to register a keypress, it must be
  536. * called with keylock held.
  537. */
  538. static void ir_do_keydown(struct rc_dev *dev, int scancode,
  539. u32 keycode, u8 toggle)
  540. {
  541. input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
  542. /* Repeat event? */
  543. if (dev->keypressed &&
  544. dev->last_scancode == scancode &&
  545. dev->last_toggle == toggle)
  546. return;
  547. /* Release old keypress */
  548. ir_do_keyup(dev);
  549. dev->last_scancode = scancode;
  550. dev->last_toggle = toggle;
  551. dev->last_keycode = keycode;
  552. if (keycode == KEY_RESERVED)
  553. return;
  554. /* Register a keypress */
  555. dev->keypressed = true;
  556. IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
  557. dev->input_name, keycode, scancode);
  558. input_report_key(dev->input_dev, dev->last_keycode, 1);
  559. input_sync(dev->input_dev);
  560. }
  561. /**
  562. * rc_keydown() - generates input event for a key press
  563. * @dev: the struct rc_dev descriptor of the device
  564. * @scancode: the scancode that we're seeking
  565. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  566. * support toggle values, this should be set to zero)
  567. *
  568. * This routine is used to signal that a key has been pressed on the
  569. * remote control.
  570. */
  571. void rc_keydown(struct rc_dev *dev, int scancode, u8 toggle)
  572. {
  573. unsigned long flags;
  574. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  575. spin_lock_irqsave(&dev->keylock, flags);
  576. ir_do_keydown(dev, scancode, keycode, toggle);
  577. if (dev->keypressed) {
  578. dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  579. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  580. }
  581. spin_unlock_irqrestore(&dev->keylock, flags);
  582. }
  583. EXPORT_SYMBOL_GPL(rc_keydown);
  584. /**
  585. * rc_keydown_notimeout() - generates input event for a key press without
  586. * an automatic keyup event at a later time
  587. * @dev: the struct rc_dev descriptor of the device
  588. * @scancode: the scancode that we're seeking
  589. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  590. * support toggle values, this should be set to zero)
  591. *
  592. * This routine is used to signal that a key has been pressed on the
  593. * remote control. The driver must manually call rc_keyup() at a later stage.
  594. */
  595. void rc_keydown_notimeout(struct rc_dev *dev, int scancode, u8 toggle)
  596. {
  597. unsigned long flags;
  598. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  599. spin_lock_irqsave(&dev->keylock, flags);
  600. ir_do_keydown(dev, scancode, keycode, toggle);
  601. spin_unlock_irqrestore(&dev->keylock, flags);
  602. }
  603. EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
  604. static int ir_open(struct input_dev *idev)
  605. {
  606. struct rc_dev *rdev = input_get_drvdata(idev);
  607. return rdev->open(rdev);
  608. }
  609. static void ir_close(struct input_dev *idev)
  610. {
  611. struct rc_dev *rdev = input_get_drvdata(idev);
  612. if (rdev)
  613. rdev->close(rdev);
  614. }
  615. /* class for /sys/class/rc */
  616. static char *ir_devnode(struct device *dev, mode_t *mode)
  617. {
  618. return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
  619. }
  620. static struct class ir_input_class = {
  621. .name = "rc",
  622. .devnode = ir_devnode,
  623. };
  624. static struct {
  625. u64 type;
  626. char *name;
  627. } proto_names[] = {
  628. { RC_TYPE_UNKNOWN, "unknown" },
  629. { RC_TYPE_RC5, "rc-5" },
  630. { RC_TYPE_NEC, "nec" },
  631. { RC_TYPE_RC6, "rc-6" },
  632. { RC_TYPE_JVC, "jvc" },
  633. { RC_TYPE_SONY, "sony" },
  634. { RC_TYPE_RC5_SZ, "rc-5-sz" },
  635. { RC_TYPE_LIRC, "lirc" },
  636. { RC_TYPE_OTHER, "other" },
  637. };
  638. #define PROTO_NONE "none"
  639. /**
  640. * show_protocols() - shows the current IR protocol(s)
  641. * @device: the device descriptor
  642. * @mattr: the device attribute struct (unused)
  643. * @buf: a pointer to the output buffer
  644. *
  645. * This routine is a callback routine for input read the IR protocol type(s).
  646. * it is trigged by reading /sys/class/rc/rc?/protocols.
  647. * It returns the protocol names of supported protocols.
  648. * Enabled protocols are printed in brackets.
  649. */
  650. static ssize_t show_protocols(struct device *device,
  651. struct device_attribute *mattr, char *buf)
  652. {
  653. struct rc_dev *dev = to_rc_dev(device);
  654. u64 allowed, enabled;
  655. char *tmp = buf;
  656. int i;
  657. /* Device is being removed */
  658. if (!dev)
  659. return -EINVAL;
  660. if (dev->driver_type == RC_DRIVER_SCANCODE) {
  661. enabled = dev->rc_map.rc_type;
  662. allowed = dev->allowed_protos;
  663. } else {
  664. enabled = dev->raw->enabled_protocols;
  665. allowed = ir_raw_get_allowed_protocols();
  666. }
  667. IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
  668. (long long)allowed,
  669. (long long)enabled);
  670. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  671. if (allowed & enabled & proto_names[i].type)
  672. tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
  673. else if (allowed & proto_names[i].type)
  674. tmp += sprintf(tmp, "%s ", proto_names[i].name);
  675. }
  676. if (tmp != buf)
  677. tmp--;
  678. *tmp = '\n';
  679. return tmp + 1 - buf;
  680. }
  681. /**
  682. * store_protocols() - changes the current IR protocol(s)
  683. * @device: the device descriptor
  684. * @mattr: the device attribute struct (unused)
  685. * @buf: a pointer to the input buffer
  686. * @len: length of the input buffer
  687. *
  688. * This routine is for changing the IR protocol type.
  689. * It is trigged by writing to /sys/class/rc/rc?/protocols.
  690. * Writing "+proto" will add a protocol to the list of enabled protocols.
  691. * Writing "-proto" will remove a protocol from the list of enabled protocols.
  692. * Writing "proto" will enable only "proto".
  693. * Writing "none" will disable all protocols.
  694. * Returns -EINVAL if an invalid protocol combination or unknown protocol name
  695. * is used, otherwise @len.
  696. */
  697. static ssize_t store_protocols(struct device *device,
  698. struct device_attribute *mattr,
  699. const char *data,
  700. size_t len)
  701. {
  702. struct rc_dev *dev = to_rc_dev(device);
  703. bool enable, disable;
  704. const char *tmp;
  705. u64 type;
  706. u64 mask;
  707. int rc, i, count = 0;
  708. unsigned long flags;
  709. /* Device is being removed */
  710. if (!dev)
  711. return -EINVAL;
  712. if (dev->driver_type == RC_DRIVER_SCANCODE)
  713. type = dev->rc_map.rc_type;
  714. else if (dev->raw)
  715. type = dev->raw->enabled_protocols;
  716. else {
  717. IR_dprintk(1, "Protocol switching not supported\n");
  718. return -EINVAL;
  719. }
  720. while ((tmp = strsep((char **) &data, " \n")) != NULL) {
  721. if (!*tmp)
  722. break;
  723. if (*tmp == '+') {
  724. enable = true;
  725. disable = false;
  726. tmp++;
  727. } else if (*tmp == '-') {
  728. enable = false;
  729. disable = true;
  730. tmp++;
  731. } else {
  732. enable = false;
  733. disable = false;
  734. }
  735. if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
  736. tmp += sizeof(PROTO_NONE);
  737. mask = 0;
  738. count++;
  739. } else {
  740. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  741. if (!strcasecmp(tmp, proto_names[i].name)) {
  742. tmp += strlen(proto_names[i].name);
  743. mask = proto_names[i].type;
  744. break;
  745. }
  746. }
  747. if (i == ARRAY_SIZE(proto_names)) {
  748. IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
  749. return -EINVAL;
  750. }
  751. count++;
  752. }
  753. if (enable)
  754. type |= mask;
  755. else if (disable)
  756. type &= ~mask;
  757. else
  758. type = mask;
  759. }
  760. if (!count) {
  761. IR_dprintk(1, "Protocol not specified\n");
  762. return -EINVAL;
  763. }
  764. if (dev->change_protocol) {
  765. rc = dev->change_protocol(dev, type);
  766. if (rc < 0) {
  767. IR_dprintk(1, "Error setting protocols to 0x%llx\n",
  768. (long long)type);
  769. return -EINVAL;
  770. }
  771. }
  772. if (dev->driver_type == RC_DRIVER_SCANCODE) {
  773. spin_lock_irqsave(&dev->rc_map.lock, flags);
  774. dev->rc_map.rc_type = type;
  775. spin_unlock_irqrestore(&dev->rc_map.lock, flags);
  776. } else {
  777. dev->raw->enabled_protocols = type;
  778. }
  779. IR_dprintk(1, "Current protocol(s): 0x%llx\n",
  780. (long long)type);
  781. return len;
  782. }
  783. static void rc_dev_release(struct device *device)
  784. {
  785. struct rc_dev *dev = to_rc_dev(device);
  786. kfree(dev);
  787. module_put(THIS_MODULE);
  788. }
  789. #define ADD_HOTPLUG_VAR(fmt, val...) \
  790. do { \
  791. int err = add_uevent_var(env, fmt, val); \
  792. if (err) \
  793. return err; \
  794. } while (0)
  795. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  796. {
  797. struct rc_dev *dev = to_rc_dev(device);
  798. if (dev->rc_map.name)
  799. ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
  800. if (dev->driver_name)
  801. ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
  802. return 0;
  803. }
  804. /*
  805. * Static device attribute struct with the sysfs attributes for IR's
  806. */
  807. static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
  808. show_protocols, store_protocols);
  809. static struct attribute *rc_dev_attrs[] = {
  810. &dev_attr_protocols.attr,
  811. NULL,
  812. };
  813. static struct attribute_group rc_dev_attr_grp = {
  814. .attrs = rc_dev_attrs,
  815. };
  816. static const struct attribute_group *rc_dev_attr_groups[] = {
  817. &rc_dev_attr_grp,
  818. NULL
  819. };
  820. static struct device_type rc_dev_type = {
  821. .groups = rc_dev_attr_groups,
  822. .release = rc_dev_release,
  823. .uevent = rc_dev_uevent,
  824. };
  825. struct rc_dev *rc_allocate_device(void)
  826. {
  827. struct rc_dev *dev;
  828. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  829. if (!dev)
  830. return NULL;
  831. dev->input_dev = input_allocate_device();
  832. if (!dev->input_dev) {
  833. kfree(dev);
  834. return NULL;
  835. }
  836. dev->input_dev->getkeycode = ir_getkeycode;
  837. dev->input_dev->setkeycode = ir_setkeycode;
  838. input_set_drvdata(dev->input_dev, dev);
  839. spin_lock_init(&dev->rc_map.lock);
  840. spin_lock_init(&dev->keylock);
  841. setup_timer(&dev->timer_keyup, ir_timer_keyup, (unsigned long)dev);
  842. dev->dev.type = &rc_dev_type;
  843. dev->dev.class = &ir_input_class;
  844. device_initialize(&dev->dev);
  845. __module_get(THIS_MODULE);
  846. return dev;
  847. }
  848. EXPORT_SYMBOL_GPL(rc_allocate_device);
  849. void rc_free_device(struct rc_dev *dev)
  850. {
  851. if (dev) {
  852. input_free_device(dev->input_dev);
  853. put_device(&dev->dev);
  854. }
  855. }
  856. EXPORT_SYMBOL_GPL(rc_free_device);
  857. int rc_register_device(struct rc_dev *dev)
  858. {
  859. static atomic_t devno = ATOMIC_INIT(0);
  860. struct rc_map *rc_map;
  861. const char *path;
  862. int rc;
  863. if (!dev || !dev->map_name)
  864. return -EINVAL;
  865. rc_map = rc_map_get(dev->map_name);
  866. if (!rc_map)
  867. rc_map = rc_map_get(RC_MAP_EMPTY);
  868. if (!rc_map || !rc_map->scan || rc_map->size == 0)
  869. return -EINVAL;
  870. set_bit(EV_KEY, dev->input_dev->evbit);
  871. set_bit(EV_REP, dev->input_dev->evbit);
  872. set_bit(EV_MSC, dev->input_dev->evbit);
  873. set_bit(MSC_SCAN, dev->input_dev->mscbit);
  874. if (dev->open)
  875. dev->input_dev->open = ir_open;
  876. if (dev->close)
  877. dev->input_dev->close = ir_close;
  878. dev->devno = (unsigned long)(atomic_inc_return(&devno) - 1);
  879. dev_set_name(&dev->dev, "rc%ld", dev->devno);
  880. dev_set_drvdata(&dev->dev, dev);
  881. rc = device_add(&dev->dev);
  882. if (rc)
  883. return rc;
  884. rc = ir_setkeytable(dev, rc_map);
  885. if (rc)
  886. goto out_dev;
  887. dev->input_dev->dev.parent = &dev->dev;
  888. memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
  889. dev->input_dev->phys = dev->input_phys;
  890. dev->input_dev->name = dev->input_name;
  891. rc = input_register_device(dev->input_dev);
  892. if (rc)
  893. goto out_table;
  894. /*
  895. * Default delay of 250ms is too short for some protocols, especially
  896. * since the timeout is currently set to 250ms. Increase it to 500ms,
  897. * to avoid wrong repetition of the keycodes. Note that this must be
  898. * set after the call to input_register_device().
  899. */
  900. dev->input_dev->rep[REP_DELAY] = 500;
  901. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  902. printk(KERN_INFO "%s: %s as %s\n",
  903. dev_name(&dev->dev),
  904. dev->input_name ? dev->input_name : "Unspecified device",
  905. path ? path : "N/A");
  906. kfree(path);
  907. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  908. rc = ir_raw_event_register(dev);
  909. if (rc < 0)
  910. goto out_input;
  911. }
  912. if (dev->change_protocol) {
  913. rc = dev->change_protocol(dev, rc_map->rc_type);
  914. if (rc < 0)
  915. goto out_raw;
  916. }
  917. IR_dprintk(1, "Registered rc%ld (driver: %s, remote: %s, mode %s)\n",
  918. dev->devno,
  919. dev->driver_name ? dev->driver_name : "unknown",
  920. rc_map->name ? rc_map->name : "unknown",
  921. dev->driver_type == RC_DRIVER_IR_RAW ? "raw" : "cooked");
  922. return 0;
  923. out_raw:
  924. if (dev->driver_type == RC_DRIVER_IR_RAW)
  925. ir_raw_event_unregister(dev);
  926. out_input:
  927. input_unregister_device(dev->input_dev);
  928. dev->input_dev = NULL;
  929. out_table:
  930. ir_free_table(&dev->rc_map);
  931. out_dev:
  932. device_del(&dev->dev);
  933. return rc;
  934. }
  935. EXPORT_SYMBOL_GPL(rc_register_device);
  936. void rc_unregister_device(struct rc_dev *dev)
  937. {
  938. if (!dev)
  939. return;
  940. del_timer_sync(&dev->timer_keyup);
  941. if (dev->driver_type == RC_DRIVER_IR_RAW)
  942. ir_raw_event_unregister(dev);
  943. input_unregister_device(dev->input_dev);
  944. dev->input_dev = NULL;
  945. ir_free_table(&dev->rc_map);
  946. IR_dprintk(1, "Freed keycode table\n");
  947. device_unregister(&dev->dev);
  948. }
  949. EXPORT_SYMBOL_GPL(rc_unregister_device);
  950. /*
  951. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  952. */
  953. static int __init rc_core_init(void)
  954. {
  955. int rc = class_register(&ir_input_class);
  956. if (rc) {
  957. printk(KERN_ERR "rc_core: unable to register rc class\n");
  958. return rc;
  959. }
  960. /* Initialize/load the decoders/keymap code that will be used */
  961. ir_raw_init();
  962. rc_map_register(&empty_map);
  963. return 0;
  964. }
  965. static void __exit rc_core_exit(void)
  966. {
  967. class_unregister(&ir_input_class);
  968. rc_map_unregister(&empty_map);
  969. }
  970. module_init(rc_core_init);
  971. module_exit(rc_core_exit);
  972. int rc_core_debug; /* ir_debug level (0,1,2) */
  973. EXPORT_SYMBOL_GPL(rc_core_debug);
  974. module_param_named(debug, rc_core_debug, int, 0644);
  975. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  976. MODULE_LICENSE("GPL");