rc-main.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. /* rc-main.c - Remote Controller core module
  2. *
  3. * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <media/rc-core.h>
  16. #include <linux/atomic.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/delay.h>
  19. #include <linux/input.h>
  20. #include <linux/leds.h>
  21. #include <linux/slab.h>
  22. #include <linux/idr.h>
  23. #include <linux/device.h>
  24. #include <linux/module.h>
  25. #include "rc-core-priv.h"
  26. /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
  27. #define IR_TAB_MIN_SIZE 256
  28. #define IR_TAB_MAX_SIZE 8192
  29. #define RC_DEV_MAX 256
  30. /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
  31. #define IR_KEYPRESS_TIMEOUT 250
  32. /* Used to keep track of known keymaps */
  33. static LIST_HEAD(rc_map_list);
  34. static DEFINE_SPINLOCK(rc_map_lock);
  35. static struct led_trigger *led_feedback;
  36. /* Used to keep track of rc devices */
  37. static DEFINE_IDA(rc_ida);
  38. static struct rc_map_list *seek_rc_map(const char *name)
  39. {
  40. struct rc_map_list *map = NULL;
  41. spin_lock(&rc_map_lock);
  42. list_for_each_entry(map, &rc_map_list, list) {
  43. if (!strcmp(name, map->map.name)) {
  44. spin_unlock(&rc_map_lock);
  45. return map;
  46. }
  47. }
  48. spin_unlock(&rc_map_lock);
  49. return NULL;
  50. }
  51. struct rc_map *rc_map_get(const char *name)
  52. {
  53. struct rc_map_list *map;
  54. map = seek_rc_map(name);
  55. #ifdef CONFIG_MODULES
  56. if (!map) {
  57. int rc = request_module("%s", name);
  58. if (rc < 0) {
  59. pr_err("Couldn't load IR keymap %s\n", name);
  60. return NULL;
  61. }
  62. msleep(20); /* Give some time for IR to register */
  63. map = seek_rc_map(name);
  64. }
  65. #endif
  66. if (!map) {
  67. pr_err("IR keymap %s not found\n", name);
  68. return NULL;
  69. }
  70. printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
  71. return &map->map;
  72. }
  73. EXPORT_SYMBOL_GPL(rc_map_get);
  74. int rc_map_register(struct rc_map_list *map)
  75. {
  76. spin_lock(&rc_map_lock);
  77. list_add_tail(&map->list, &rc_map_list);
  78. spin_unlock(&rc_map_lock);
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_GPL(rc_map_register);
  82. void rc_map_unregister(struct rc_map_list *map)
  83. {
  84. spin_lock(&rc_map_lock);
  85. list_del(&map->list);
  86. spin_unlock(&rc_map_lock);
  87. }
  88. EXPORT_SYMBOL_GPL(rc_map_unregister);
  89. static struct rc_map_table empty[] = {
  90. { 0x2a, KEY_COFFEE },
  91. };
  92. static struct rc_map_list empty_map = {
  93. .map = {
  94. .scan = empty,
  95. .size = ARRAY_SIZE(empty),
  96. .rc_type = RC_TYPE_UNKNOWN, /* Legacy IR type */
  97. .name = RC_MAP_EMPTY,
  98. }
  99. };
  100. /**
  101. * ir_create_table() - initializes a scancode table
  102. * @rc_map: the rc_map to initialize
  103. * @name: name to assign to the table
  104. * @rc_type: ir type to assign to the new table
  105. * @size: initial size of the table
  106. * @return: zero on success or a negative error code
  107. *
  108. * This routine will initialize the rc_map and will allocate
  109. * memory to hold at least the specified number of elements.
  110. */
  111. static int ir_create_table(struct rc_map *rc_map,
  112. const char *name, u64 rc_type, size_t size)
  113. {
  114. rc_map->name = kstrdup(name, GFP_KERNEL);
  115. if (!rc_map->name)
  116. return -ENOMEM;
  117. rc_map->rc_type = rc_type;
  118. rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
  119. rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
  120. rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
  121. if (!rc_map->scan) {
  122. kfree(rc_map->name);
  123. rc_map->name = NULL;
  124. return -ENOMEM;
  125. }
  126. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  127. rc_map->size, rc_map->alloc);
  128. return 0;
  129. }
  130. /**
  131. * ir_free_table() - frees memory allocated by a scancode table
  132. * @rc_map: the table whose mappings need to be freed
  133. *
  134. * This routine will free memory alloctaed for key mappings used by given
  135. * scancode table.
  136. */
  137. static void ir_free_table(struct rc_map *rc_map)
  138. {
  139. rc_map->size = 0;
  140. kfree(rc_map->name);
  141. rc_map->name = NULL;
  142. kfree(rc_map->scan);
  143. rc_map->scan = NULL;
  144. }
  145. /**
  146. * ir_resize_table() - resizes a scancode table if necessary
  147. * @rc_map: the rc_map to resize
  148. * @gfp_flags: gfp flags to use when allocating memory
  149. * @return: zero on success or a negative error code
  150. *
  151. * This routine will shrink the rc_map if it has lots of
  152. * unused entries and grow it if it is full.
  153. */
  154. static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
  155. {
  156. unsigned int oldalloc = rc_map->alloc;
  157. unsigned int newalloc = oldalloc;
  158. struct rc_map_table *oldscan = rc_map->scan;
  159. struct rc_map_table *newscan;
  160. if (rc_map->size == rc_map->len) {
  161. /* All entries in use -> grow keytable */
  162. if (rc_map->alloc >= IR_TAB_MAX_SIZE)
  163. return -ENOMEM;
  164. newalloc *= 2;
  165. IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
  166. }
  167. if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
  168. /* Less than 1/3 of entries in use -> shrink keytable */
  169. newalloc /= 2;
  170. IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
  171. }
  172. if (newalloc == oldalloc)
  173. return 0;
  174. newscan = kmalloc(newalloc, gfp_flags);
  175. if (!newscan) {
  176. IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
  177. return -ENOMEM;
  178. }
  179. memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
  180. rc_map->scan = newscan;
  181. rc_map->alloc = newalloc;
  182. rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
  183. kfree(oldscan);
  184. return 0;
  185. }
  186. /**
  187. * ir_update_mapping() - set a keycode in the scancode->keycode table
  188. * @dev: the struct rc_dev device descriptor
  189. * @rc_map: scancode table to be adjusted
  190. * @index: index of the mapping that needs to be updated
  191. * @keycode: the desired keycode
  192. * @return: previous keycode assigned to the mapping
  193. *
  194. * This routine is used to update scancode->keycode mapping at given
  195. * position.
  196. */
  197. static unsigned int ir_update_mapping(struct rc_dev *dev,
  198. struct rc_map *rc_map,
  199. unsigned int index,
  200. unsigned int new_keycode)
  201. {
  202. int old_keycode = rc_map->scan[index].keycode;
  203. int i;
  204. /* Did the user wish to remove the mapping? */
  205. if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
  206. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
  207. index, rc_map->scan[index].scancode);
  208. rc_map->len--;
  209. memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
  210. (rc_map->len - index) * sizeof(struct rc_map_table));
  211. } else {
  212. IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
  213. index,
  214. old_keycode == KEY_RESERVED ? "New" : "Replacing",
  215. rc_map->scan[index].scancode, new_keycode);
  216. rc_map->scan[index].keycode = new_keycode;
  217. __set_bit(new_keycode, dev->input_dev->keybit);
  218. }
  219. if (old_keycode != KEY_RESERVED) {
  220. /* A previous mapping was updated... */
  221. __clear_bit(old_keycode, dev->input_dev->keybit);
  222. /* ... but another scancode might use the same keycode */
  223. for (i = 0; i < rc_map->len; i++) {
  224. if (rc_map->scan[i].keycode == old_keycode) {
  225. __set_bit(old_keycode, dev->input_dev->keybit);
  226. break;
  227. }
  228. }
  229. /* Possibly shrink the keytable, failure is not a problem */
  230. ir_resize_table(rc_map, GFP_ATOMIC);
  231. }
  232. return old_keycode;
  233. }
  234. /**
  235. * ir_establish_scancode() - set a keycode in the scancode->keycode table
  236. * @dev: the struct rc_dev device descriptor
  237. * @rc_map: scancode table to be searched
  238. * @scancode: the desired scancode
  239. * @resize: controls whether we allowed to resize the table to
  240. * accommodate not yet present scancodes
  241. * @return: index of the mapping containing scancode in question
  242. * or -1U in case of failure.
  243. *
  244. * This routine is used to locate given scancode in rc_map.
  245. * If scancode is not yet present the routine will allocate a new slot
  246. * for it.
  247. */
  248. static unsigned int ir_establish_scancode(struct rc_dev *dev,
  249. struct rc_map *rc_map,
  250. unsigned int scancode,
  251. bool resize)
  252. {
  253. unsigned int i;
  254. /*
  255. * Unfortunately, some hardware-based IR decoders don't provide
  256. * all bits for the complete IR code. In general, they provide only
  257. * the command part of the IR code. Yet, as it is possible to replace
  258. * the provided IR with another one, it is needed to allow loading
  259. * IR tables from other remotes. So, we support specifying a mask to
  260. * indicate the valid bits of the scancodes.
  261. */
  262. if (dev->scancode_mask)
  263. scancode &= dev->scancode_mask;
  264. /* First check if we already have a mapping for this ir command */
  265. for (i = 0; i < rc_map->len; i++) {
  266. if (rc_map->scan[i].scancode == scancode)
  267. return i;
  268. /* Keytable is sorted from lowest to highest scancode */
  269. if (rc_map->scan[i].scancode >= scancode)
  270. break;
  271. }
  272. /* No previous mapping found, we might need to grow the table */
  273. if (rc_map->size == rc_map->len) {
  274. if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
  275. return -1U;
  276. }
  277. /* i is the proper index to insert our new keycode */
  278. if (i < rc_map->len)
  279. memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
  280. (rc_map->len - i) * sizeof(struct rc_map_table));
  281. rc_map->scan[i].scancode = scancode;
  282. rc_map->scan[i].keycode = KEY_RESERVED;
  283. rc_map->len++;
  284. return i;
  285. }
  286. /**
  287. * ir_setkeycode() - set a keycode in the scancode->keycode table
  288. * @idev: the struct input_dev device descriptor
  289. * @scancode: the desired scancode
  290. * @keycode: result
  291. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  292. *
  293. * This routine is used to handle evdev EVIOCSKEY ioctl.
  294. */
  295. static int ir_setkeycode(struct input_dev *idev,
  296. const struct input_keymap_entry *ke,
  297. unsigned int *old_keycode)
  298. {
  299. struct rc_dev *rdev = input_get_drvdata(idev);
  300. struct rc_map *rc_map = &rdev->rc_map;
  301. unsigned int index;
  302. unsigned int scancode;
  303. int retval = 0;
  304. unsigned long flags;
  305. spin_lock_irqsave(&rc_map->lock, flags);
  306. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  307. index = ke->index;
  308. if (index >= rc_map->len) {
  309. retval = -EINVAL;
  310. goto out;
  311. }
  312. } else {
  313. retval = input_scancode_to_scalar(ke, &scancode);
  314. if (retval)
  315. goto out;
  316. index = ir_establish_scancode(rdev, rc_map, scancode, true);
  317. if (index >= rc_map->len) {
  318. retval = -ENOMEM;
  319. goto out;
  320. }
  321. }
  322. *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
  323. out:
  324. spin_unlock_irqrestore(&rc_map->lock, flags);
  325. return retval;
  326. }
  327. /**
  328. * ir_setkeytable() - sets several entries in the scancode->keycode table
  329. * @dev: the struct rc_dev device descriptor
  330. * @to: the struct rc_map to copy entries to
  331. * @from: the struct rc_map to copy entries from
  332. * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
  333. *
  334. * This routine is used to handle table initialization.
  335. */
  336. static int ir_setkeytable(struct rc_dev *dev,
  337. const struct rc_map *from)
  338. {
  339. struct rc_map *rc_map = &dev->rc_map;
  340. unsigned int i, index;
  341. int rc;
  342. rc = ir_create_table(rc_map, from->name,
  343. from->rc_type, from->size);
  344. if (rc)
  345. return rc;
  346. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  347. rc_map->size, rc_map->alloc);
  348. for (i = 0; i < from->size; i++) {
  349. index = ir_establish_scancode(dev, rc_map,
  350. from->scan[i].scancode, false);
  351. if (index >= rc_map->len) {
  352. rc = -ENOMEM;
  353. break;
  354. }
  355. ir_update_mapping(dev, rc_map, index,
  356. from->scan[i].keycode);
  357. }
  358. if (rc)
  359. ir_free_table(rc_map);
  360. return rc;
  361. }
  362. /**
  363. * ir_lookup_by_scancode() - locate mapping by scancode
  364. * @rc_map: the struct rc_map to search
  365. * @scancode: scancode to look for in the table
  366. * @return: index in the table, -1U if not found
  367. *
  368. * This routine performs binary search in RC keykeymap table for
  369. * given scancode.
  370. */
  371. static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
  372. unsigned int scancode)
  373. {
  374. int start = 0;
  375. int end = rc_map->len - 1;
  376. int mid;
  377. while (start <= end) {
  378. mid = (start + end) / 2;
  379. if (rc_map->scan[mid].scancode < scancode)
  380. start = mid + 1;
  381. else if (rc_map->scan[mid].scancode > scancode)
  382. end = mid - 1;
  383. else
  384. return mid;
  385. }
  386. return -1U;
  387. }
  388. /**
  389. * ir_getkeycode() - get a keycode from the scancode->keycode table
  390. * @idev: the struct input_dev device descriptor
  391. * @scancode: the desired scancode
  392. * @keycode: used to return the keycode, if found, or KEY_RESERVED
  393. * @return: always returns zero.
  394. *
  395. * This routine is used to handle evdev EVIOCGKEY ioctl.
  396. */
  397. static int ir_getkeycode(struct input_dev *idev,
  398. struct input_keymap_entry *ke)
  399. {
  400. struct rc_dev *rdev = input_get_drvdata(idev);
  401. struct rc_map *rc_map = &rdev->rc_map;
  402. struct rc_map_table *entry;
  403. unsigned long flags;
  404. unsigned int index;
  405. unsigned int scancode;
  406. int retval;
  407. spin_lock_irqsave(&rc_map->lock, flags);
  408. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  409. index = ke->index;
  410. } else {
  411. retval = input_scancode_to_scalar(ke, &scancode);
  412. if (retval)
  413. goto out;
  414. index = ir_lookup_by_scancode(rc_map, scancode);
  415. }
  416. if (index < rc_map->len) {
  417. entry = &rc_map->scan[index];
  418. ke->index = index;
  419. ke->keycode = entry->keycode;
  420. ke->len = sizeof(entry->scancode);
  421. memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
  422. } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
  423. /*
  424. * We do not really know the valid range of scancodes
  425. * so let's respond with KEY_RESERVED to anything we
  426. * do not have mapping for [yet].
  427. */
  428. ke->index = index;
  429. ke->keycode = KEY_RESERVED;
  430. } else {
  431. retval = -EINVAL;
  432. goto out;
  433. }
  434. retval = 0;
  435. out:
  436. spin_unlock_irqrestore(&rc_map->lock, flags);
  437. return retval;
  438. }
  439. /**
  440. * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  441. * @dev: the struct rc_dev descriptor of the device
  442. * @scancode: the scancode to look for
  443. * @return: the corresponding keycode, or KEY_RESERVED
  444. *
  445. * This routine is used by drivers which need to convert a scancode to a
  446. * keycode. Normally it should not be used since drivers should have no
  447. * interest in keycodes.
  448. */
  449. u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
  450. {
  451. struct rc_map *rc_map = &dev->rc_map;
  452. unsigned int keycode;
  453. unsigned int index;
  454. unsigned long flags;
  455. spin_lock_irqsave(&rc_map->lock, flags);
  456. index = ir_lookup_by_scancode(rc_map, scancode);
  457. keycode = index < rc_map->len ?
  458. rc_map->scan[index].keycode : KEY_RESERVED;
  459. spin_unlock_irqrestore(&rc_map->lock, flags);
  460. if (keycode != KEY_RESERVED)
  461. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  462. dev->input_name, scancode, keycode);
  463. return keycode;
  464. }
  465. EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
  466. /**
  467. * ir_do_keyup() - internal function to signal the release of a keypress
  468. * @dev: the struct rc_dev descriptor of the device
  469. * @sync: whether or not to call input_sync
  470. *
  471. * This function is used internally to release a keypress, it must be
  472. * called with keylock held.
  473. */
  474. static void ir_do_keyup(struct rc_dev *dev, bool sync)
  475. {
  476. if (!dev->keypressed)
  477. return;
  478. IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
  479. input_report_key(dev->input_dev, dev->last_keycode, 0);
  480. led_trigger_event(led_feedback, LED_OFF);
  481. if (sync)
  482. input_sync(dev->input_dev);
  483. dev->keypressed = false;
  484. }
  485. /**
  486. * rc_keyup() - signals the release of a keypress
  487. * @dev: the struct rc_dev descriptor of the device
  488. *
  489. * This routine is used to signal that a key has been released on the
  490. * remote control.
  491. */
  492. void rc_keyup(struct rc_dev *dev)
  493. {
  494. unsigned long flags;
  495. spin_lock_irqsave(&dev->keylock, flags);
  496. ir_do_keyup(dev, true);
  497. spin_unlock_irqrestore(&dev->keylock, flags);
  498. }
  499. EXPORT_SYMBOL_GPL(rc_keyup);
  500. /**
  501. * ir_timer_keyup() - generates a keyup event after a timeout
  502. * @cookie: a pointer to the struct rc_dev for the device
  503. *
  504. * This routine will generate a keyup event some time after a keydown event
  505. * is generated when no further activity has been detected.
  506. */
  507. static void ir_timer_keyup(unsigned long cookie)
  508. {
  509. struct rc_dev *dev = (struct rc_dev *)cookie;
  510. unsigned long flags;
  511. /*
  512. * ir->keyup_jiffies is used to prevent a race condition if a
  513. * hardware interrupt occurs at this point and the keyup timer
  514. * event is moved further into the future as a result.
  515. *
  516. * The timer will then be reactivated and this function called
  517. * again in the future. We need to exit gracefully in that case
  518. * to allow the input subsystem to do its auto-repeat magic or
  519. * a keyup event might follow immediately after the keydown.
  520. */
  521. spin_lock_irqsave(&dev->keylock, flags);
  522. if (time_is_before_eq_jiffies(dev->keyup_jiffies))
  523. ir_do_keyup(dev, true);
  524. spin_unlock_irqrestore(&dev->keylock, flags);
  525. }
  526. /**
  527. * rc_repeat() - signals that a key is still pressed
  528. * @dev: the struct rc_dev descriptor of the device
  529. *
  530. * This routine is used by IR decoders when a repeat message which does
  531. * not include the necessary bits to reproduce the scancode has been
  532. * received.
  533. */
  534. void rc_repeat(struct rc_dev *dev)
  535. {
  536. unsigned long flags;
  537. spin_lock_irqsave(&dev->keylock, flags);
  538. input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
  539. input_sync(dev->input_dev);
  540. if (!dev->keypressed)
  541. goto out;
  542. dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  543. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  544. out:
  545. spin_unlock_irqrestore(&dev->keylock, flags);
  546. }
  547. EXPORT_SYMBOL_GPL(rc_repeat);
  548. /**
  549. * ir_do_keydown() - internal function to process a keypress
  550. * @dev: the struct rc_dev descriptor of the device
  551. * @protocol: the protocol of the keypress
  552. * @scancode: the scancode of the keypress
  553. * @keycode: the keycode of the keypress
  554. * @toggle: the toggle value of the keypress
  555. *
  556. * This function is used internally to register a keypress, it must be
  557. * called with keylock held.
  558. */
  559. static void ir_do_keydown(struct rc_dev *dev, enum rc_type protocol,
  560. u32 scancode, u32 keycode, u8 toggle)
  561. {
  562. bool new_event = (!dev->keypressed ||
  563. dev->last_protocol != protocol ||
  564. dev->last_scancode != scancode ||
  565. dev->last_toggle != toggle);
  566. if (new_event && dev->keypressed)
  567. ir_do_keyup(dev, false);
  568. input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
  569. if (new_event && keycode != KEY_RESERVED) {
  570. /* Register a keypress */
  571. dev->keypressed = true;
  572. dev->last_protocol = protocol;
  573. dev->last_scancode = scancode;
  574. dev->last_toggle = toggle;
  575. dev->last_keycode = keycode;
  576. IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
  577. dev->input_name, keycode, protocol, scancode);
  578. input_report_key(dev->input_dev, keycode, 1);
  579. led_trigger_event(led_feedback, LED_FULL);
  580. }
  581. input_sync(dev->input_dev);
  582. }
  583. /**
  584. * rc_keydown() - generates input event for a key press
  585. * @dev: the struct rc_dev descriptor of the device
  586. * @protocol: the protocol for the keypress
  587. * @scancode: the scancode for the keypress
  588. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  589. * support toggle values, this should be set to zero)
  590. *
  591. * This routine is used to signal that a key has been pressed on the
  592. * remote control.
  593. */
  594. void rc_keydown(struct rc_dev *dev, enum rc_type protocol, u32 scancode, u8 toggle)
  595. {
  596. unsigned long flags;
  597. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  598. spin_lock_irqsave(&dev->keylock, flags);
  599. ir_do_keydown(dev, protocol, scancode, keycode, toggle);
  600. if (dev->keypressed) {
  601. dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  602. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  603. }
  604. spin_unlock_irqrestore(&dev->keylock, flags);
  605. }
  606. EXPORT_SYMBOL_GPL(rc_keydown);
  607. /**
  608. * rc_keydown_notimeout() - generates input event for a key press without
  609. * an automatic keyup event at a later time
  610. * @dev: the struct rc_dev descriptor of the device
  611. * @protocol: the protocol for the keypress
  612. * @scancode: the scancode for the keypress
  613. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  614. * support toggle values, this should be set to zero)
  615. *
  616. * This routine is used to signal that a key has been pressed on the
  617. * remote control. The driver must manually call rc_keyup() at a later stage.
  618. */
  619. void rc_keydown_notimeout(struct rc_dev *dev, enum rc_type protocol,
  620. u32 scancode, u8 toggle)
  621. {
  622. unsigned long flags;
  623. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  624. spin_lock_irqsave(&dev->keylock, flags);
  625. ir_do_keydown(dev, protocol, scancode, keycode, toggle);
  626. spin_unlock_irqrestore(&dev->keylock, flags);
  627. }
  628. EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
  629. int rc_open(struct rc_dev *rdev)
  630. {
  631. int rval = 0;
  632. if (!rdev)
  633. return -EINVAL;
  634. mutex_lock(&rdev->lock);
  635. if (!rdev->users++ && rdev->open != NULL)
  636. rval = rdev->open(rdev);
  637. if (rval)
  638. rdev->users--;
  639. mutex_unlock(&rdev->lock);
  640. return rval;
  641. }
  642. EXPORT_SYMBOL_GPL(rc_open);
  643. static int ir_open(struct input_dev *idev)
  644. {
  645. struct rc_dev *rdev = input_get_drvdata(idev);
  646. return rc_open(rdev);
  647. }
  648. void rc_close(struct rc_dev *rdev)
  649. {
  650. if (rdev) {
  651. mutex_lock(&rdev->lock);
  652. if (!--rdev->users && rdev->close != NULL)
  653. rdev->close(rdev);
  654. mutex_unlock(&rdev->lock);
  655. }
  656. }
  657. EXPORT_SYMBOL_GPL(rc_close);
  658. static void ir_close(struct input_dev *idev)
  659. {
  660. struct rc_dev *rdev = input_get_drvdata(idev);
  661. rc_close(rdev);
  662. }
  663. /* class for /sys/class/rc */
  664. static char *rc_devnode(struct device *dev, umode_t *mode)
  665. {
  666. return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
  667. }
  668. static struct class rc_class = {
  669. .name = "rc",
  670. .devnode = rc_devnode,
  671. };
  672. /*
  673. * These are the protocol textual descriptions that are
  674. * used by the sysfs protocols file. Note that the order
  675. * of the entries is relevant.
  676. */
  677. static const struct {
  678. u64 type;
  679. const char *name;
  680. const char *module_name;
  681. } proto_names[] = {
  682. { RC_BIT_NONE, "none", NULL },
  683. { RC_BIT_OTHER, "other", NULL },
  684. { RC_BIT_UNKNOWN, "unknown", NULL },
  685. { RC_BIT_RC5 |
  686. RC_BIT_RC5X, "rc-5", "ir-rc5-decoder" },
  687. { RC_BIT_NEC |
  688. RC_BIT_NECX |
  689. RC_BIT_NEC32, "nec", "ir-nec-decoder" },
  690. { RC_BIT_RC6_0 |
  691. RC_BIT_RC6_6A_20 |
  692. RC_BIT_RC6_6A_24 |
  693. RC_BIT_RC6_6A_32 |
  694. RC_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
  695. { RC_BIT_JVC, "jvc", "ir-jvc-decoder" },
  696. { RC_BIT_SONY12 |
  697. RC_BIT_SONY15 |
  698. RC_BIT_SONY20, "sony", "ir-sony-decoder" },
  699. { RC_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
  700. { RC_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
  701. { RC_BIT_SHARP, "sharp", "ir-sharp-decoder" },
  702. { RC_BIT_MCE_KBD, "mce_kbd", "ir-mce_kbd-decoder" },
  703. { RC_BIT_XMP, "xmp", "ir-xmp-decoder" },
  704. { RC_BIT_CEC, "cec", NULL },
  705. };
  706. /**
  707. * struct rc_filter_attribute - Device attribute relating to a filter type.
  708. * @attr: Device attribute.
  709. * @type: Filter type.
  710. * @mask: false for filter value, true for filter mask.
  711. */
  712. struct rc_filter_attribute {
  713. struct device_attribute attr;
  714. enum rc_filter_type type;
  715. bool mask;
  716. };
  717. #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
  718. #define RC_PROTO_ATTR(_name, _mode, _show, _store, _type) \
  719. struct rc_filter_attribute dev_attr_##_name = { \
  720. .attr = __ATTR(_name, _mode, _show, _store), \
  721. .type = (_type), \
  722. }
  723. #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
  724. struct rc_filter_attribute dev_attr_##_name = { \
  725. .attr = __ATTR(_name, _mode, _show, _store), \
  726. .type = (_type), \
  727. .mask = (_mask), \
  728. }
  729. static bool lirc_is_present(void)
  730. {
  731. #if defined(CONFIG_LIRC_MODULE)
  732. struct module *lirc;
  733. mutex_lock(&module_mutex);
  734. lirc = find_module("lirc_dev");
  735. mutex_unlock(&module_mutex);
  736. return lirc ? true : false;
  737. #elif defined(CONFIG_LIRC)
  738. return true;
  739. #else
  740. return false;
  741. #endif
  742. }
  743. /**
  744. * show_protocols() - shows the current/wakeup IR protocol(s)
  745. * @device: the device descriptor
  746. * @mattr: the device attribute struct
  747. * @buf: a pointer to the output buffer
  748. *
  749. * This routine is a callback routine for input read the IR protocol type(s).
  750. * it is trigged by reading /sys/class/rc/rc?/[wakeup_]protocols.
  751. * It returns the protocol names of supported protocols.
  752. * Enabled protocols are printed in brackets.
  753. *
  754. * dev->lock is taken to guard against races between device
  755. * registration, store_protocols and show_protocols.
  756. */
  757. static ssize_t show_protocols(struct device *device,
  758. struct device_attribute *mattr, char *buf)
  759. {
  760. struct rc_dev *dev = to_rc_dev(device);
  761. struct rc_filter_attribute *fattr = to_rc_filter_attr(mattr);
  762. u64 allowed, enabled;
  763. char *tmp = buf;
  764. int i;
  765. /* Device is being removed */
  766. if (!dev)
  767. return -EINVAL;
  768. if (!atomic_read(&dev->initialized))
  769. return -ERESTARTSYS;
  770. mutex_lock(&dev->lock);
  771. if (fattr->type == RC_FILTER_NORMAL) {
  772. enabled = dev->enabled_protocols;
  773. allowed = dev->allowed_protocols;
  774. if (dev->raw && !allowed)
  775. allowed = ir_raw_get_allowed_protocols();
  776. } else {
  777. enabled = dev->enabled_wakeup_protocols;
  778. allowed = dev->allowed_wakeup_protocols;
  779. }
  780. mutex_unlock(&dev->lock);
  781. IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
  782. __func__, (long long)allowed, (long long)enabled);
  783. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  784. if (allowed & enabled & proto_names[i].type)
  785. tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
  786. else if (allowed & proto_names[i].type)
  787. tmp += sprintf(tmp, "%s ", proto_names[i].name);
  788. if (allowed & proto_names[i].type)
  789. allowed &= ~proto_names[i].type;
  790. }
  791. if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
  792. tmp += sprintf(tmp, "[lirc] ");
  793. if (tmp != buf)
  794. tmp--;
  795. *tmp = '\n';
  796. return tmp + 1 - buf;
  797. }
  798. /**
  799. * parse_protocol_change() - parses a protocol change request
  800. * @protocols: pointer to the bitmask of current protocols
  801. * @buf: pointer to the buffer with a list of changes
  802. *
  803. * Writing "+proto" will add a protocol to the protocol mask.
  804. * Writing "-proto" will remove a protocol from protocol mask.
  805. * Writing "proto" will enable only "proto".
  806. * Writing "none" will disable all protocols.
  807. * Returns the number of changes performed or a negative error code.
  808. */
  809. static int parse_protocol_change(u64 *protocols, const char *buf)
  810. {
  811. const char *tmp;
  812. unsigned count = 0;
  813. bool enable, disable;
  814. u64 mask;
  815. int i;
  816. while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
  817. if (!*tmp)
  818. break;
  819. if (*tmp == '+') {
  820. enable = true;
  821. disable = false;
  822. tmp++;
  823. } else if (*tmp == '-') {
  824. enable = false;
  825. disable = true;
  826. tmp++;
  827. } else {
  828. enable = false;
  829. disable = false;
  830. }
  831. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  832. if (!strcasecmp(tmp, proto_names[i].name)) {
  833. mask = proto_names[i].type;
  834. break;
  835. }
  836. }
  837. if (i == ARRAY_SIZE(proto_names)) {
  838. if (!strcasecmp(tmp, "lirc"))
  839. mask = 0;
  840. else {
  841. IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
  842. return -EINVAL;
  843. }
  844. }
  845. count++;
  846. if (enable)
  847. *protocols |= mask;
  848. else if (disable)
  849. *protocols &= ~mask;
  850. else
  851. *protocols = mask;
  852. }
  853. if (!count) {
  854. IR_dprintk(1, "Protocol not specified\n");
  855. return -EINVAL;
  856. }
  857. return count;
  858. }
  859. static void ir_raw_load_modules(u64 *protocols)
  860. {
  861. u64 available;
  862. int i, ret;
  863. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  864. if (proto_names[i].type == RC_BIT_NONE ||
  865. proto_names[i].type & (RC_BIT_OTHER | RC_BIT_UNKNOWN))
  866. continue;
  867. available = ir_raw_get_allowed_protocols();
  868. if (!(*protocols & proto_names[i].type & ~available))
  869. continue;
  870. if (!proto_names[i].module_name) {
  871. pr_err("Can't enable IR protocol %s\n",
  872. proto_names[i].name);
  873. *protocols &= ~proto_names[i].type;
  874. continue;
  875. }
  876. ret = request_module("%s", proto_names[i].module_name);
  877. if (ret < 0) {
  878. pr_err("Couldn't load IR protocol module %s\n",
  879. proto_names[i].module_name);
  880. *protocols &= ~proto_names[i].type;
  881. continue;
  882. }
  883. msleep(20);
  884. available = ir_raw_get_allowed_protocols();
  885. if (!(*protocols & proto_names[i].type & ~available))
  886. continue;
  887. pr_err("Loaded IR protocol module %s, \
  888. but protocol %s still not available\n",
  889. proto_names[i].module_name,
  890. proto_names[i].name);
  891. *protocols &= ~proto_names[i].type;
  892. }
  893. }
  894. /**
  895. * store_protocols() - changes the current/wakeup IR protocol(s)
  896. * @device: the device descriptor
  897. * @mattr: the device attribute struct
  898. * @buf: a pointer to the input buffer
  899. * @len: length of the input buffer
  900. *
  901. * This routine is for changing the IR protocol type.
  902. * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
  903. * See parse_protocol_change() for the valid commands.
  904. * Returns @len on success or a negative error code.
  905. *
  906. * dev->lock is taken to guard against races between device
  907. * registration, store_protocols and show_protocols.
  908. */
  909. static ssize_t store_protocols(struct device *device,
  910. struct device_attribute *mattr,
  911. const char *buf, size_t len)
  912. {
  913. struct rc_dev *dev = to_rc_dev(device);
  914. struct rc_filter_attribute *fattr = to_rc_filter_attr(mattr);
  915. u64 *current_protocols;
  916. int (*change_protocol)(struct rc_dev *dev, u64 *rc_type);
  917. struct rc_scancode_filter *filter;
  918. int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
  919. u64 old_protocols, new_protocols;
  920. ssize_t rc;
  921. /* Device is being removed */
  922. if (!dev)
  923. return -EINVAL;
  924. if (!atomic_read(&dev->initialized))
  925. return -ERESTARTSYS;
  926. if (fattr->type == RC_FILTER_NORMAL) {
  927. IR_dprintk(1, "Normal protocol change requested\n");
  928. current_protocols = &dev->enabled_protocols;
  929. change_protocol = dev->change_protocol;
  930. filter = &dev->scancode_filter;
  931. set_filter = dev->s_filter;
  932. } else {
  933. IR_dprintk(1, "Wakeup protocol change requested\n");
  934. current_protocols = &dev->enabled_wakeup_protocols;
  935. change_protocol = dev->change_wakeup_protocol;
  936. filter = &dev->scancode_wakeup_filter;
  937. set_filter = dev->s_wakeup_filter;
  938. }
  939. if (!change_protocol) {
  940. IR_dprintk(1, "Protocol switching not supported\n");
  941. return -EINVAL;
  942. }
  943. mutex_lock(&dev->lock);
  944. old_protocols = *current_protocols;
  945. new_protocols = old_protocols;
  946. rc = parse_protocol_change(&new_protocols, buf);
  947. if (rc < 0)
  948. goto out;
  949. rc = change_protocol(dev, &new_protocols);
  950. if (rc < 0) {
  951. IR_dprintk(1, "Error setting protocols to 0x%llx\n",
  952. (long long)new_protocols);
  953. goto out;
  954. }
  955. if (dev->driver_type == RC_DRIVER_IR_RAW)
  956. ir_raw_load_modules(&new_protocols);
  957. if (new_protocols != old_protocols) {
  958. *current_protocols = new_protocols;
  959. IR_dprintk(1, "Protocols changed to 0x%llx\n",
  960. (long long)new_protocols);
  961. }
  962. /*
  963. * If a protocol change was attempted the filter may need updating, even
  964. * if the actual protocol mask hasn't changed (since the driver may have
  965. * cleared the filter).
  966. * Try setting the same filter with the new protocol (if any).
  967. * Fall back to clearing the filter.
  968. */
  969. if (set_filter && filter->mask) {
  970. if (new_protocols)
  971. rc = set_filter(dev, filter);
  972. else
  973. rc = -1;
  974. if (rc < 0) {
  975. filter->data = 0;
  976. filter->mask = 0;
  977. set_filter(dev, filter);
  978. }
  979. }
  980. rc = len;
  981. out:
  982. mutex_unlock(&dev->lock);
  983. return rc;
  984. }
  985. /**
  986. * show_filter() - shows the current scancode filter value or mask
  987. * @device: the device descriptor
  988. * @attr: the device attribute struct
  989. * @buf: a pointer to the output buffer
  990. *
  991. * This routine is a callback routine to read a scancode filter value or mask.
  992. * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
  993. * It prints the current scancode filter value or mask of the appropriate filter
  994. * type in hexadecimal into @buf and returns the size of the buffer.
  995. *
  996. * Bits of the filter value corresponding to set bits in the filter mask are
  997. * compared against input scancodes and non-matching scancodes are discarded.
  998. *
  999. * dev->lock is taken to guard against races between device registration,
  1000. * store_filter and show_filter.
  1001. */
  1002. static ssize_t show_filter(struct device *device,
  1003. struct device_attribute *attr,
  1004. char *buf)
  1005. {
  1006. struct rc_dev *dev = to_rc_dev(device);
  1007. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1008. struct rc_scancode_filter *filter;
  1009. u32 val;
  1010. /* Device is being removed */
  1011. if (!dev)
  1012. return -EINVAL;
  1013. if (!atomic_read(&dev->initialized))
  1014. return -ERESTARTSYS;
  1015. mutex_lock(&dev->lock);
  1016. if (fattr->type == RC_FILTER_NORMAL)
  1017. filter = &dev->scancode_filter;
  1018. else
  1019. filter = &dev->scancode_wakeup_filter;
  1020. if (fattr->mask)
  1021. val = filter->mask;
  1022. else
  1023. val = filter->data;
  1024. mutex_unlock(&dev->lock);
  1025. return sprintf(buf, "%#x\n", val);
  1026. }
  1027. /**
  1028. * store_filter() - changes the scancode filter value
  1029. * @device: the device descriptor
  1030. * @attr: the device attribute struct
  1031. * @buf: a pointer to the input buffer
  1032. * @len: length of the input buffer
  1033. *
  1034. * This routine is for changing a scancode filter value or mask.
  1035. * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
  1036. * Returns -EINVAL if an invalid filter value for the current protocol was
  1037. * specified or if scancode filtering is not supported by the driver, otherwise
  1038. * returns @len.
  1039. *
  1040. * Bits of the filter value corresponding to set bits in the filter mask are
  1041. * compared against input scancodes and non-matching scancodes are discarded.
  1042. *
  1043. * dev->lock is taken to guard against races between device registration,
  1044. * store_filter and show_filter.
  1045. */
  1046. static ssize_t store_filter(struct device *device,
  1047. struct device_attribute *attr,
  1048. const char *buf, size_t len)
  1049. {
  1050. struct rc_dev *dev = to_rc_dev(device);
  1051. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1052. struct rc_scancode_filter new_filter, *filter;
  1053. int ret;
  1054. unsigned long val;
  1055. int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
  1056. u64 *enabled_protocols;
  1057. /* Device is being removed */
  1058. if (!dev)
  1059. return -EINVAL;
  1060. if (!atomic_read(&dev->initialized))
  1061. return -ERESTARTSYS;
  1062. ret = kstrtoul(buf, 0, &val);
  1063. if (ret < 0)
  1064. return ret;
  1065. if (fattr->type == RC_FILTER_NORMAL) {
  1066. set_filter = dev->s_filter;
  1067. enabled_protocols = &dev->enabled_protocols;
  1068. filter = &dev->scancode_filter;
  1069. } else {
  1070. set_filter = dev->s_wakeup_filter;
  1071. enabled_protocols = &dev->enabled_wakeup_protocols;
  1072. filter = &dev->scancode_wakeup_filter;
  1073. }
  1074. if (!set_filter)
  1075. return -EINVAL;
  1076. mutex_lock(&dev->lock);
  1077. new_filter = *filter;
  1078. if (fattr->mask)
  1079. new_filter.mask = val;
  1080. else
  1081. new_filter.data = val;
  1082. if (!*enabled_protocols && val) {
  1083. /* refuse to set a filter unless a protocol is enabled */
  1084. ret = -EINVAL;
  1085. goto unlock;
  1086. }
  1087. ret = set_filter(dev, &new_filter);
  1088. if (ret < 0)
  1089. goto unlock;
  1090. *filter = new_filter;
  1091. unlock:
  1092. mutex_unlock(&dev->lock);
  1093. return (ret < 0) ? ret : len;
  1094. }
  1095. static void rc_dev_release(struct device *device)
  1096. {
  1097. struct rc_dev *dev = to_rc_dev(device);
  1098. kfree(dev);
  1099. }
  1100. #define ADD_HOTPLUG_VAR(fmt, val...) \
  1101. do { \
  1102. int err = add_uevent_var(env, fmt, val); \
  1103. if (err) \
  1104. return err; \
  1105. } while (0)
  1106. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  1107. {
  1108. struct rc_dev *dev = to_rc_dev(device);
  1109. if (dev->rc_map.name)
  1110. ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
  1111. if (dev->driver_name)
  1112. ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
  1113. return 0;
  1114. }
  1115. /*
  1116. * Static device attribute struct with the sysfs attributes for IR's
  1117. */
  1118. static RC_PROTO_ATTR(protocols, S_IRUGO | S_IWUSR,
  1119. show_protocols, store_protocols, RC_FILTER_NORMAL);
  1120. static RC_PROTO_ATTR(wakeup_protocols, S_IRUGO | S_IWUSR,
  1121. show_protocols, store_protocols, RC_FILTER_WAKEUP);
  1122. static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
  1123. show_filter, store_filter, RC_FILTER_NORMAL, false);
  1124. static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
  1125. show_filter, store_filter, RC_FILTER_NORMAL, true);
  1126. static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
  1127. show_filter, store_filter, RC_FILTER_WAKEUP, false);
  1128. static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
  1129. show_filter, store_filter, RC_FILTER_WAKEUP, true);
  1130. static struct attribute *rc_dev_protocol_attrs[] = {
  1131. &dev_attr_protocols.attr.attr,
  1132. NULL,
  1133. };
  1134. static struct attribute_group rc_dev_protocol_attr_grp = {
  1135. .attrs = rc_dev_protocol_attrs,
  1136. };
  1137. static struct attribute *rc_dev_wakeup_protocol_attrs[] = {
  1138. &dev_attr_wakeup_protocols.attr.attr,
  1139. NULL,
  1140. };
  1141. static struct attribute_group rc_dev_wakeup_protocol_attr_grp = {
  1142. .attrs = rc_dev_wakeup_protocol_attrs,
  1143. };
  1144. static struct attribute *rc_dev_filter_attrs[] = {
  1145. &dev_attr_filter.attr.attr,
  1146. &dev_attr_filter_mask.attr.attr,
  1147. NULL,
  1148. };
  1149. static struct attribute_group rc_dev_filter_attr_grp = {
  1150. .attrs = rc_dev_filter_attrs,
  1151. };
  1152. static struct attribute *rc_dev_wakeup_filter_attrs[] = {
  1153. &dev_attr_wakeup_filter.attr.attr,
  1154. &dev_attr_wakeup_filter_mask.attr.attr,
  1155. NULL,
  1156. };
  1157. static struct attribute_group rc_dev_wakeup_filter_attr_grp = {
  1158. .attrs = rc_dev_wakeup_filter_attrs,
  1159. };
  1160. static struct device_type rc_dev_type = {
  1161. .release = rc_dev_release,
  1162. .uevent = rc_dev_uevent,
  1163. };
  1164. struct rc_dev *rc_allocate_device(void)
  1165. {
  1166. struct rc_dev *dev;
  1167. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1168. if (!dev)
  1169. return NULL;
  1170. dev->input_dev = input_allocate_device();
  1171. if (!dev->input_dev) {
  1172. kfree(dev);
  1173. return NULL;
  1174. }
  1175. dev->input_dev->getkeycode = ir_getkeycode;
  1176. dev->input_dev->setkeycode = ir_setkeycode;
  1177. input_set_drvdata(dev->input_dev, dev);
  1178. spin_lock_init(&dev->rc_map.lock);
  1179. spin_lock_init(&dev->keylock);
  1180. mutex_init(&dev->lock);
  1181. setup_timer(&dev->timer_keyup, ir_timer_keyup, (unsigned long)dev);
  1182. dev->dev.type = &rc_dev_type;
  1183. dev->dev.class = &rc_class;
  1184. device_initialize(&dev->dev);
  1185. __module_get(THIS_MODULE);
  1186. return dev;
  1187. }
  1188. EXPORT_SYMBOL_GPL(rc_allocate_device);
  1189. void rc_free_device(struct rc_dev *dev)
  1190. {
  1191. if (!dev)
  1192. return;
  1193. input_free_device(dev->input_dev);
  1194. put_device(&dev->dev);
  1195. /* kfree(dev) will be called by the callback function
  1196. rc_dev_release() */
  1197. module_put(THIS_MODULE);
  1198. }
  1199. EXPORT_SYMBOL_GPL(rc_free_device);
  1200. static void devm_rc_alloc_release(struct device *dev, void *res)
  1201. {
  1202. rc_free_device(*(struct rc_dev **)res);
  1203. }
  1204. struct rc_dev *devm_rc_allocate_device(struct device *dev)
  1205. {
  1206. struct rc_dev **dr, *rc;
  1207. dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
  1208. if (!dr)
  1209. return NULL;
  1210. rc = rc_allocate_device();
  1211. if (!rc) {
  1212. devres_free(dr);
  1213. return NULL;
  1214. }
  1215. rc->dev.parent = dev;
  1216. rc->managed_alloc = true;
  1217. *dr = rc;
  1218. devres_add(dev, dr);
  1219. return rc;
  1220. }
  1221. EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
  1222. int rc_register_device(struct rc_dev *dev)
  1223. {
  1224. static bool raw_init = false; /* raw decoders loaded? */
  1225. struct rc_map *rc_map;
  1226. const char *path;
  1227. int attr = 0;
  1228. int minor;
  1229. int rc;
  1230. if (!dev || !dev->map_name)
  1231. return -EINVAL;
  1232. rc_map = rc_map_get(dev->map_name);
  1233. if (!rc_map)
  1234. rc_map = rc_map_get(RC_MAP_EMPTY);
  1235. if (!rc_map || !rc_map->scan || rc_map->size == 0)
  1236. return -EINVAL;
  1237. set_bit(EV_KEY, dev->input_dev->evbit);
  1238. set_bit(EV_REP, dev->input_dev->evbit);
  1239. set_bit(EV_MSC, dev->input_dev->evbit);
  1240. set_bit(MSC_SCAN, dev->input_dev->mscbit);
  1241. if (dev->open)
  1242. dev->input_dev->open = ir_open;
  1243. if (dev->close)
  1244. dev->input_dev->close = ir_close;
  1245. minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
  1246. if (minor < 0)
  1247. return minor;
  1248. dev->minor = minor;
  1249. dev_set_name(&dev->dev, "rc%u", dev->minor);
  1250. dev_set_drvdata(&dev->dev, dev);
  1251. atomic_set(&dev->initialized, 0);
  1252. dev->dev.groups = dev->sysfs_groups;
  1253. dev->sysfs_groups[attr++] = &rc_dev_protocol_attr_grp;
  1254. if (dev->s_filter)
  1255. dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
  1256. if (dev->s_wakeup_filter)
  1257. dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
  1258. if (dev->change_wakeup_protocol)
  1259. dev->sysfs_groups[attr++] = &rc_dev_wakeup_protocol_attr_grp;
  1260. dev->sysfs_groups[attr++] = NULL;
  1261. rc = device_add(&dev->dev);
  1262. if (rc)
  1263. goto out_unlock;
  1264. rc = ir_setkeytable(dev, rc_map);
  1265. if (rc)
  1266. goto out_dev;
  1267. dev->input_dev->dev.parent = &dev->dev;
  1268. memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
  1269. dev->input_dev->phys = dev->input_phys;
  1270. dev->input_dev->name = dev->input_name;
  1271. rc = input_register_device(dev->input_dev);
  1272. if (rc)
  1273. goto out_table;
  1274. /*
  1275. * Default delay of 250ms is too short for some protocols, especially
  1276. * since the timeout is currently set to 250ms. Increase it to 500ms,
  1277. * to avoid wrong repetition of the keycodes. Note that this must be
  1278. * set after the call to input_register_device().
  1279. */
  1280. dev->input_dev->rep[REP_DELAY] = 500;
  1281. /*
  1282. * As a repeat event on protocols like RC-5 and NEC take as long as
  1283. * 110/114ms, using 33ms as a repeat period is not the right thing
  1284. * to do.
  1285. */
  1286. dev->input_dev->rep[REP_PERIOD] = 125;
  1287. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1288. dev_info(&dev->dev, "%s as %s\n",
  1289. dev->input_name ?: "Unspecified device", path ?: "N/A");
  1290. kfree(path);
  1291. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  1292. if (!raw_init) {
  1293. request_module_nowait("ir-lirc-codec");
  1294. raw_init = true;
  1295. }
  1296. rc = ir_raw_event_register(dev);
  1297. if (rc < 0)
  1298. goto out_input;
  1299. }
  1300. if (dev->change_protocol) {
  1301. u64 rc_type = (1ll << rc_map->rc_type);
  1302. rc = dev->change_protocol(dev, &rc_type);
  1303. if (rc < 0)
  1304. goto out_raw;
  1305. dev->enabled_protocols = rc_type;
  1306. }
  1307. /* Allow the RC sysfs nodes to be accessible */
  1308. atomic_set(&dev->initialized, 1);
  1309. IR_dprintk(1, "Registered rc%u (driver: %s, remote: %s, mode %s)\n",
  1310. dev->minor,
  1311. dev->driver_name ? dev->driver_name : "unknown",
  1312. rc_map->name ? rc_map->name : "unknown",
  1313. dev->driver_type == RC_DRIVER_IR_RAW ? "raw" : "cooked");
  1314. return 0;
  1315. out_raw:
  1316. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1317. ir_raw_event_unregister(dev);
  1318. out_input:
  1319. input_unregister_device(dev->input_dev);
  1320. dev->input_dev = NULL;
  1321. out_table:
  1322. ir_free_table(&dev->rc_map);
  1323. out_dev:
  1324. device_del(&dev->dev);
  1325. out_unlock:
  1326. ida_simple_remove(&rc_ida, minor);
  1327. return rc;
  1328. }
  1329. EXPORT_SYMBOL_GPL(rc_register_device);
  1330. static void devm_rc_release(struct device *dev, void *res)
  1331. {
  1332. rc_unregister_device(*(struct rc_dev **)res);
  1333. }
  1334. int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
  1335. {
  1336. struct rc_dev **dr;
  1337. int ret;
  1338. dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
  1339. if (!dr)
  1340. return -ENOMEM;
  1341. ret = rc_register_device(dev);
  1342. if (ret) {
  1343. devres_free(dr);
  1344. return ret;
  1345. }
  1346. *dr = dev;
  1347. devres_add(parent, dr);
  1348. return 0;
  1349. }
  1350. EXPORT_SYMBOL_GPL(devm_rc_register_device);
  1351. void rc_unregister_device(struct rc_dev *dev)
  1352. {
  1353. if (!dev)
  1354. return;
  1355. del_timer_sync(&dev->timer_keyup);
  1356. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1357. ir_raw_event_unregister(dev);
  1358. /* Freeing the table should also call the stop callback */
  1359. ir_free_table(&dev->rc_map);
  1360. IR_dprintk(1, "Freed keycode table\n");
  1361. input_unregister_device(dev->input_dev);
  1362. dev->input_dev = NULL;
  1363. device_del(&dev->dev);
  1364. ida_simple_remove(&rc_ida, dev->minor);
  1365. if (!dev->managed_alloc)
  1366. rc_free_device(dev);
  1367. }
  1368. EXPORT_SYMBOL_GPL(rc_unregister_device);
  1369. /*
  1370. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  1371. */
  1372. static int __init rc_core_init(void)
  1373. {
  1374. int rc = class_register(&rc_class);
  1375. if (rc) {
  1376. pr_err("rc_core: unable to register rc class\n");
  1377. return rc;
  1378. }
  1379. led_trigger_register_simple("rc-feedback", &led_feedback);
  1380. rc_map_register(&empty_map);
  1381. return 0;
  1382. }
  1383. static void __exit rc_core_exit(void)
  1384. {
  1385. class_unregister(&rc_class);
  1386. led_trigger_unregister_simple(led_feedback);
  1387. rc_map_unregister(&empty_map);
  1388. }
  1389. subsys_initcall(rc_core_init);
  1390. module_exit(rc_core_exit);
  1391. int rc_core_debug; /* ir_debug level (0,1,2) */
  1392. EXPORT_SYMBOL_GPL(rc_core_debug);
  1393. module_param_named(debug, rc_core_debug, int, 0644);
  1394. MODULE_AUTHOR("Mauro Carvalho Chehab");
  1395. MODULE_LICENSE("GPL");