rc-main.c 31 KB

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