rc-main.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  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. /**
  630. * rc_validate_filter() - checks that the scancode and mask are valid and
  631. * provides sensible defaults
  632. * @dev: the struct rc_dev descriptor of the device
  633. * @filter: the scancode and mask
  634. * @return: 0 or -EINVAL if the filter is not valid
  635. */
  636. static int rc_validate_filter(struct rc_dev *dev,
  637. struct rc_scancode_filter *filter)
  638. {
  639. static u32 masks[] = {
  640. [RC_TYPE_RC5] = 0x1f7f,
  641. [RC_TYPE_RC5X_20] = 0x1f7f3f,
  642. [RC_TYPE_RC5_SZ] = 0x2fff,
  643. [RC_TYPE_SONY12] = 0x1f007f,
  644. [RC_TYPE_SONY15] = 0xff007f,
  645. [RC_TYPE_SONY20] = 0x1fff7f,
  646. [RC_TYPE_JVC] = 0xffff,
  647. [RC_TYPE_NEC] = 0xffff,
  648. [RC_TYPE_NECX] = 0xffffff,
  649. [RC_TYPE_NEC32] = 0xffffffff,
  650. [RC_TYPE_SANYO] = 0x1fffff,
  651. [RC_TYPE_MCIR2_KBD] = 0xffff,
  652. [RC_TYPE_MCIR2_MSE] = 0x1fffff,
  653. [RC_TYPE_RC6_0] = 0xffff,
  654. [RC_TYPE_RC6_6A_20] = 0xfffff,
  655. [RC_TYPE_RC6_6A_24] = 0xffffff,
  656. [RC_TYPE_RC6_6A_32] = 0xffffffff,
  657. [RC_TYPE_RC6_MCE] = 0xffff7fff,
  658. [RC_TYPE_SHARP] = 0x1fff,
  659. };
  660. u32 s = filter->data;
  661. enum rc_type protocol = dev->wakeup_protocol;
  662. switch (protocol) {
  663. case RC_TYPE_NECX:
  664. if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
  665. return -EINVAL;
  666. break;
  667. case RC_TYPE_NEC32:
  668. if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
  669. return -EINVAL;
  670. break;
  671. case RC_TYPE_RC6_MCE:
  672. if ((s & 0xffff0000) != 0x800f0000)
  673. return -EINVAL;
  674. break;
  675. case RC_TYPE_RC6_6A_32:
  676. if ((s & 0xffff0000) == 0x800f0000)
  677. return -EINVAL;
  678. break;
  679. default:
  680. break;
  681. }
  682. filter->data &= masks[protocol];
  683. filter->mask &= masks[protocol];
  684. /*
  685. * If we have to raw encode the IR for wakeup, we cannot have a mask
  686. */
  687. if (dev->encode_wakeup &&
  688. filter->mask != 0 && filter->mask != masks[protocol])
  689. return -EINVAL;
  690. return 0;
  691. }
  692. int rc_open(struct rc_dev *rdev)
  693. {
  694. int rval = 0;
  695. if (!rdev)
  696. return -EINVAL;
  697. mutex_lock(&rdev->lock);
  698. if (!rdev->users++ && rdev->open != NULL)
  699. rval = rdev->open(rdev);
  700. if (rval)
  701. rdev->users--;
  702. mutex_unlock(&rdev->lock);
  703. return rval;
  704. }
  705. EXPORT_SYMBOL_GPL(rc_open);
  706. static int ir_open(struct input_dev *idev)
  707. {
  708. struct rc_dev *rdev = input_get_drvdata(idev);
  709. return rc_open(rdev);
  710. }
  711. void rc_close(struct rc_dev *rdev)
  712. {
  713. if (rdev) {
  714. mutex_lock(&rdev->lock);
  715. if (!--rdev->users && rdev->close != NULL)
  716. rdev->close(rdev);
  717. mutex_unlock(&rdev->lock);
  718. }
  719. }
  720. EXPORT_SYMBOL_GPL(rc_close);
  721. static void ir_close(struct input_dev *idev)
  722. {
  723. struct rc_dev *rdev = input_get_drvdata(idev);
  724. rc_close(rdev);
  725. }
  726. /* class for /sys/class/rc */
  727. static char *rc_devnode(struct device *dev, umode_t *mode)
  728. {
  729. return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
  730. }
  731. static struct class rc_class = {
  732. .name = "rc",
  733. .devnode = rc_devnode,
  734. };
  735. /*
  736. * These are the protocol textual descriptions that are
  737. * used by the sysfs protocols file. Note that the order
  738. * of the entries is relevant.
  739. */
  740. static const struct {
  741. u64 type;
  742. const char *name;
  743. const char *module_name;
  744. } proto_names[] = {
  745. { RC_BIT_NONE, "none", NULL },
  746. { RC_BIT_OTHER, "other", NULL },
  747. { RC_BIT_UNKNOWN, "unknown", NULL },
  748. { RC_BIT_RC5 |
  749. RC_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
  750. { RC_BIT_NEC |
  751. RC_BIT_NECX |
  752. RC_BIT_NEC32, "nec", "ir-nec-decoder" },
  753. { RC_BIT_RC6_0 |
  754. RC_BIT_RC6_6A_20 |
  755. RC_BIT_RC6_6A_24 |
  756. RC_BIT_RC6_6A_32 |
  757. RC_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
  758. { RC_BIT_JVC, "jvc", "ir-jvc-decoder" },
  759. { RC_BIT_SONY12 |
  760. RC_BIT_SONY15 |
  761. RC_BIT_SONY20, "sony", "ir-sony-decoder" },
  762. { RC_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
  763. { RC_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
  764. { RC_BIT_SHARP, "sharp", "ir-sharp-decoder" },
  765. { RC_BIT_MCIR2_KBD |
  766. RC_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
  767. { RC_BIT_XMP, "xmp", "ir-xmp-decoder" },
  768. { RC_BIT_CEC, "cec", NULL },
  769. };
  770. /**
  771. * struct rc_filter_attribute - Device attribute relating to a filter type.
  772. * @attr: Device attribute.
  773. * @type: Filter type.
  774. * @mask: false for filter value, true for filter mask.
  775. */
  776. struct rc_filter_attribute {
  777. struct device_attribute attr;
  778. enum rc_filter_type type;
  779. bool mask;
  780. };
  781. #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
  782. #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
  783. struct rc_filter_attribute dev_attr_##_name = { \
  784. .attr = __ATTR(_name, _mode, _show, _store), \
  785. .type = (_type), \
  786. .mask = (_mask), \
  787. }
  788. static bool lirc_is_present(void)
  789. {
  790. #if defined(CONFIG_LIRC_MODULE)
  791. struct module *lirc;
  792. mutex_lock(&module_mutex);
  793. lirc = find_module("lirc_dev");
  794. mutex_unlock(&module_mutex);
  795. return lirc ? true : false;
  796. #elif defined(CONFIG_LIRC)
  797. return true;
  798. #else
  799. return false;
  800. #endif
  801. }
  802. /**
  803. * show_protocols() - shows the current IR protocol(s)
  804. * @device: the device descriptor
  805. * @mattr: the device attribute struct
  806. * @buf: a pointer to the output buffer
  807. *
  808. * This routine is a callback routine for input read the IR protocol type(s).
  809. * it is trigged by reading /sys/class/rc/rc?/protocols.
  810. * It returns the protocol names of supported protocols.
  811. * Enabled protocols are printed in brackets.
  812. *
  813. * dev->lock is taken to guard against races between device
  814. * registration, store_protocols and show_protocols.
  815. */
  816. static ssize_t show_protocols(struct device *device,
  817. struct device_attribute *mattr, char *buf)
  818. {
  819. struct rc_dev *dev = to_rc_dev(device);
  820. u64 allowed, enabled;
  821. char *tmp = buf;
  822. int i;
  823. /* Device is being removed */
  824. if (!dev)
  825. return -EINVAL;
  826. if (!atomic_read(&dev->initialized))
  827. return -ERESTARTSYS;
  828. mutex_lock(&dev->lock);
  829. enabled = dev->enabled_protocols;
  830. allowed = dev->allowed_protocols;
  831. if (dev->raw && !allowed)
  832. allowed = ir_raw_get_allowed_protocols();
  833. mutex_unlock(&dev->lock);
  834. IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
  835. __func__, (long long)allowed, (long long)enabled);
  836. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  837. if (allowed & enabled & proto_names[i].type)
  838. tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
  839. else if (allowed & proto_names[i].type)
  840. tmp += sprintf(tmp, "%s ", proto_names[i].name);
  841. if (allowed & proto_names[i].type)
  842. allowed &= ~proto_names[i].type;
  843. }
  844. if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
  845. tmp += sprintf(tmp, "[lirc] ");
  846. if (tmp != buf)
  847. tmp--;
  848. *tmp = '\n';
  849. return tmp + 1 - buf;
  850. }
  851. /**
  852. * parse_protocol_change() - parses a protocol change request
  853. * @protocols: pointer to the bitmask of current protocols
  854. * @buf: pointer to the buffer with a list of changes
  855. *
  856. * Writing "+proto" will add a protocol to the protocol mask.
  857. * Writing "-proto" will remove a protocol from protocol mask.
  858. * Writing "proto" will enable only "proto".
  859. * Writing "none" will disable all protocols.
  860. * Returns the number of changes performed or a negative error code.
  861. */
  862. static int parse_protocol_change(u64 *protocols, const char *buf)
  863. {
  864. const char *tmp;
  865. unsigned count = 0;
  866. bool enable, disable;
  867. u64 mask;
  868. int i;
  869. while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
  870. if (!*tmp)
  871. break;
  872. if (*tmp == '+') {
  873. enable = true;
  874. disable = false;
  875. tmp++;
  876. } else if (*tmp == '-') {
  877. enable = false;
  878. disable = true;
  879. tmp++;
  880. } else {
  881. enable = false;
  882. disable = false;
  883. }
  884. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  885. if (!strcasecmp(tmp, proto_names[i].name)) {
  886. mask = proto_names[i].type;
  887. break;
  888. }
  889. }
  890. if (i == ARRAY_SIZE(proto_names)) {
  891. if (!strcasecmp(tmp, "lirc"))
  892. mask = 0;
  893. else {
  894. IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
  895. return -EINVAL;
  896. }
  897. }
  898. count++;
  899. if (enable)
  900. *protocols |= mask;
  901. else if (disable)
  902. *protocols &= ~mask;
  903. else
  904. *protocols = mask;
  905. }
  906. if (!count) {
  907. IR_dprintk(1, "Protocol not specified\n");
  908. return -EINVAL;
  909. }
  910. return count;
  911. }
  912. static void ir_raw_load_modules(u64 *protocols)
  913. {
  914. u64 available;
  915. int i, ret;
  916. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  917. if (proto_names[i].type == RC_BIT_NONE ||
  918. proto_names[i].type & (RC_BIT_OTHER | RC_BIT_UNKNOWN))
  919. continue;
  920. available = ir_raw_get_allowed_protocols();
  921. if (!(*protocols & proto_names[i].type & ~available))
  922. continue;
  923. if (!proto_names[i].module_name) {
  924. pr_err("Can't enable IR protocol %s\n",
  925. proto_names[i].name);
  926. *protocols &= ~proto_names[i].type;
  927. continue;
  928. }
  929. ret = request_module("%s", proto_names[i].module_name);
  930. if (ret < 0) {
  931. pr_err("Couldn't load IR protocol module %s\n",
  932. proto_names[i].module_name);
  933. *protocols &= ~proto_names[i].type;
  934. continue;
  935. }
  936. msleep(20);
  937. available = ir_raw_get_allowed_protocols();
  938. if (!(*protocols & proto_names[i].type & ~available))
  939. continue;
  940. pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
  941. proto_names[i].module_name,
  942. proto_names[i].name);
  943. *protocols &= ~proto_names[i].type;
  944. }
  945. }
  946. /**
  947. * store_protocols() - changes the current/wakeup IR protocol(s)
  948. * @device: the device descriptor
  949. * @mattr: the device attribute struct
  950. * @buf: a pointer to the input buffer
  951. * @len: length of the input buffer
  952. *
  953. * This routine is for changing the IR protocol type.
  954. * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
  955. * See parse_protocol_change() for the valid commands.
  956. * Returns @len on success or a negative error code.
  957. *
  958. * dev->lock is taken to guard against races between device
  959. * registration, store_protocols and show_protocols.
  960. */
  961. static ssize_t store_protocols(struct device *device,
  962. struct device_attribute *mattr,
  963. const char *buf, size_t len)
  964. {
  965. struct rc_dev *dev = to_rc_dev(device);
  966. u64 *current_protocols;
  967. struct rc_scancode_filter *filter;
  968. u64 old_protocols, new_protocols;
  969. ssize_t rc;
  970. /* Device is being removed */
  971. if (!dev)
  972. return -EINVAL;
  973. if (!atomic_read(&dev->initialized))
  974. return -ERESTARTSYS;
  975. IR_dprintk(1, "Normal protocol change requested\n");
  976. current_protocols = &dev->enabled_protocols;
  977. filter = &dev->scancode_filter;
  978. if (!dev->change_protocol) {
  979. IR_dprintk(1, "Protocol switching not supported\n");
  980. return -EINVAL;
  981. }
  982. mutex_lock(&dev->lock);
  983. old_protocols = *current_protocols;
  984. new_protocols = old_protocols;
  985. rc = parse_protocol_change(&new_protocols, buf);
  986. if (rc < 0)
  987. goto out;
  988. rc = dev->change_protocol(dev, &new_protocols);
  989. if (rc < 0) {
  990. IR_dprintk(1, "Error setting protocols to 0x%llx\n",
  991. (long long)new_protocols);
  992. goto out;
  993. }
  994. if (dev->driver_type == RC_DRIVER_IR_RAW)
  995. ir_raw_load_modules(&new_protocols);
  996. if (new_protocols != old_protocols) {
  997. *current_protocols = new_protocols;
  998. IR_dprintk(1, "Protocols changed to 0x%llx\n",
  999. (long long)new_protocols);
  1000. }
  1001. /*
  1002. * If a protocol change was attempted the filter may need updating, even
  1003. * if the actual protocol mask hasn't changed (since the driver may have
  1004. * cleared the filter).
  1005. * Try setting the same filter with the new protocol (if any).
  1006. * Fall back to clearing the filter.
  1007. */
  1008. if (dev->s_filter && filter->mask) {
  1009. if (new_protocols)
  1010. rc = dev->s_filter(dev, filter);
  1011. else
  1012. rc = -1;
  1013. if (rc < 0) {
  1014. filter->data = 0;
  1015. filter->mask = 0;
  1016. dev->s_filter(dev, filter);
  1017. }
  1018. }
  1019. rc = len;
  1020. out:
  1021. mutex_unlock(&dev->lock);
  1022. return rc;
  1023. }
  1024. /**
  1025. * show_filter() - shows the current scancode filter value or mask
  1026. * @device: the device descriptor
  1027. * @attr: the device attribute struct
  1028. * @buf: a pointer to the output buffer
  1029. *
  1030. * This routine is a callback routine to read a scancode filter value or mask.
  1031. * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
  1032. * It prints the current scancode filter value or mask of the appropriate filter
  1033. * type in hexadecimal into @buf and returns the size of the buffer.
  1034. *
  1035. * Bits of the filter value corresponding to set bits in the filter mask are
  1036. * compared against input scancodes and non-matching scancodes are discarded.
  1037. *
  1038. * dev->lock is taken to guard against races between device registration,
  1039. * store_filter and show_filter.
  1040. */
  1041. static ssize_t show_filter(struct device *device,
  1042. struct device_attribute *attr,
  1043. char *buf)
  1044. {
  1045. struct rc_dev *dev = to_rc_dev(device);
  1046. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1047. struct rc_scancode_filter *filter;
  1048. u32 val;
  1049. /* Device is being removed */
  1050. if (!dev)
  1051. return -EINVAL;
  1052. if (!atomic_read(&dev->initialized))
  1053. return -ERESTARTSYS;
  1054. mutex_lock(&dev->lock);
  1055. if (fattr->type == RC_FILTER_NORMAL)
  1056. filter = &dev->scancode_filter;
  1057. else
  1058. filter = &dev->scancode_wakeup_filter;
  1059. if (fattr->mask)
  1060. val = filter->mask;
  1061. else
  1062. val = filter->data;
  1063. mutex_unlock(&dev->lock);
  1064. return sprintf(buf, "%#x\n", val);
  1065. }
  1066. /**
  1067. * store_filter() - changes the scancode filter value
  1068. * @device: the device descriptor
  1069. * @attr: the device attribute struct
  1070. * @buf: a pointer to the input buffer
  1071. * @len: length of the input buffer
  1072. *
  1073. * This routine is for changing a scancode filter value or mask.
  1074. * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
  1075. * Returns -EINVAL if an invalid filter value for the current protocol was
  1076. * specified or if scancode filtering is not supported by the driver, otherwise
  1077. * returns @len.
  1078. *
  1079. * Bits of the filter value corresponding to set bits in the filter mask are
  1080. * compared against input scancodes and non-matching scancodes are discarded.
  1081. *
  1082. * dev->lock is taken to guard against races between device registration,
  1083. * store_filter and show_filter.
  1084. */
  1085. static ssize_t store_filter(struct device *device,
  1086. struct device_attribute *attr,
  1087. const char *buf, size_t len)
  1088. {
  1089. struct rc_dev *dev = to_rc_dev(device);
  1090. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1091. struct rc_scancode_filter new_filter, *filter;
  1092. int ret;
  1093. unsigned long val;
  1094. int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
  1095. /* Device is being removed */
  1096. if (!dev)
  1097. return -EINVAL;
  1098. if (!atomic_read(&dev->initialized))
  1099. return -ERESTARTSYS;
  1100. ret = kstrtoul(buf, 0, &val);
  1101. if (ret < 0)
  1102. return ret;
  1103. if (fattr->type == RC_FILTER_NORMAL) {
  1104. set_filter = dev->s_filter;
  1105. filter = &dev->scancode_filter;
  1106. } else {
  1107. set_filter = dev->s_wakeup_filter;
  1108. filter = &dev->scancode_wakeup_filter;
  1109. }
  1110. if (!set_filter)
  1111. return -EINVAL;
  1112. mutex_lock(&dev->lock);
  1113. new_filter = *filter;
  1114. if (fattr->mask)
  1115. new_filter.mask = val;
  1116. else
  1117. new_filter.data = val;
  1118. if (fattr->type == RC_FILTER_WAKEUP) {
  1119. /*
  1120. * Refuse to set a filter unless a protocol is enabled
  1121. * and the filter is valid for that protocol
  1122. */
  1123. if (dev->wakeup_protocol != RC_TYPE_UNKNOWN)
  1124. ret = rc_validate_filter(dev, &new_filter);
  1125. else
  1126. ret = -EINVAL;
  1127. if (ret != 0)
  1128. goto unlock;
  1129. }
  1130. if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
  1131. val) {
  1132. /* refuse to set a filter unless a protocol is enabled */
  1133. ret = -EINVAL;
  1134. goto unlock;
  1135. }
  1136. ret = set_filter(dev, &new_filter);
  1137. if (ret < 0)
  1138. goto unlock;
  1139. *filter = new_filter;
  1140. unlock:
  1141. mutex_unlock(&dev->lock);
  1142. return (ret < 0) ? ret : len;
  1143. }
  1144. /*
  1145. * This is the list of all variants of all protocols, which is used by
  1146. * the wakeup_protocols sysfs entry. In the protocols sysfs entry some
  1147. * some protocols are grouped together (e.g. nec = nec + necx + nec32).
  1148. *
  1149. * For wakeup we need to know the exact protocol variant so the hardware
  1150. * can be programmed exactly what to expect.
  1151. */
  1152. static const char * const proto_variant_names[] = {
  1153. [RC_TYPE_UNKNOWN] = "unknown",
  1154. [RC_TYPE_OTHER] = "other",
  1155. [RC_TYPE_RC5] = "rc-5",
  1156. [RC_TYPE_RC5X_20] = "rc-5x-20",
  1157. [RC_TYPE_RC5_SZ] = "rc-5-sz",
  1158. [RC_TYPE_JVC] = "jvc",
  1159. [RC_TYPE_SONY12] = "sony-12",
  1160. [RC_TYPE_SONY15] = "sony-15",
  1161. [RC_TYPE_SONY20] = "sony-20",
  1162. [RC_TYPE_NEC] = "nec",
  1163. [RC_TYPE_NECX] = "nec-x",
  1164. [RC_TYPE_NEC32] = "nec-32",
  1165. [RC_TYPE_SANYO] = "sanyo",
  1166. [RC_TYPE_MCIR2_KBD] = "mcir2-kbd",
  1167. [RC_TYPE_MCIR2_MSE] = "mcir2-mse",
  1168. [RC_TYPE_RC6_0] = "rc-6-0",
  1169. [RC_TYPE_RC6_6A_20] = "rc-6-6a-20",
  1170. [RC_TYPE_RC6_6A_24] = "rc-6-6a-24",
  1171. [RC_TYPE_RC6_6A_32] = "rc-6-6a-32",
  1172. [RC_TYPE_RC6_MCE] = "rc-6-mce",
  1173. [RC_TYPE_SHARP] = "sharp",
  1174. [RC_TYPE_XMP] = "xmp",
  1175. [RC_TYPE_CEC] = "cec",
  1176. };
  1177. /**
  1178. * show_wakeup_protocols() - shows the wakeup IR protocol
  1179. * @device: the device descriptor
  1180. * @mattr: the device attribute struct
  1181. * @buf: a pointer to the output buffer
  1182. *
  1183. * This routine is a callback routine for input read the IR protocol type(s).
  1184. * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
  1185. * It returns the protocol names of supported protocols.
  1186. * The enabled protocols are printed in brackets.
  1187. *
  1188. * dev->lock is taken to guard against races between device
  1189. * registration, store_protocols and show_protocols.
  1190. */
  1191. static ssize_t show_wakeup_protocols(struct device *device,
  1192. struct device_attribute *mattr,
  1193. char *buf)
  1194. {
  1195. struct rc_dev *dev = to_rc_dev(device);
  1196. u64 allowed;
  1197. enum rc_type enabled;
  1198. char *tmp = buf;
  1199. int i;
  1200. /* Device is being removed */
  1201. if (!dev)
  1202. return -EINVAL;
  1203. if (!atomic_read(&dev->initialized))
  1204. return -ERESTARTSYS;
  1205. mutex_lock(&dev->lock);
  1206. allowed = dev->allowed_wakeup_protocols;
  1207. enabled = dev->wakeup_protocol;
  1208. mutex_unlock(&dev->lock);
  1209. IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
  1210. __func__, (long long)allowed, enabled);
  1211. for (i = 0; i < ARRAY_SIZE(proto_variant_names); i++) {
  1212. if (allowed & (1ULL << i)) {
  1213. if (i == enabled)
  1214. tmp += sprintf(tmp, "[%s] ",
  1215. proto_variant_names[i]);
  1216. else
  1217. tmp += sprintf(tmp, "%s ",
  1218. proto_variant_names[i]);
  1219. }
  1220. }
  1221. if (tmp != buf)
  1222. tmp--;
  1223. *tmp = '\n';
  1224. return tmp + 1 - buf;
  1225. }
  1226. /**
  1227. * store_wakeup_protocols() - changes the wakeup IR protocol(s)
  1228. * @device: the device descriptor
  1229. * @mattr: the device attribute struct
  1230. * @buf: a pointer to the input buffer
  1231. * @len: length of the input buffer
  1232. *
  1233. * This routine is for changing the IR protocol type.
  1234. * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
  1235. * Returns @len on success or a negative error code.
  1236. *
  1237. * dev->lock is taken to guard against races between device
  1238. * registration, store_protocols and show_protocols.
  1239. */
  1240. static ssize_t store_wakeup_protocols(struct device *device,
  1241. struct device_attribute *mattr,
  1242. const char *buf, size_t len)
  1243. {
  1244. struct rc_dev *dev = to_rc_dev(device);
  1245. enum rc_type protocol;
  1246. ssize_t rc;
  1247. u64 allowed;
  1248. int i;
  1249. /* Device is being removed */
  1250. if (!dev)
  1251. return -EINVAL;
  1252. if (!atomic_read(&dev->initialized))
  1253. return -ERESTARTSYS;
  1254. mutex_lock(&dev->lock);
  1255. allowed = dev->allowed_wakeup_protocols;
  1256. if (sysfs_streq(buf, "none")) {
  1257. protocol = RC_TYPE_UNKNOWN;
  1258. } else {
  1259. for (i = 0; i < ARRAY_SIZE(proto_variant_names); i++) {
  1260. if ((allowed & (1ULL << i)) &&
  1261. sysfs_streq(buf, proto_variant_names[i])) {
  1262. protocol = i;
  1263. break;
  1264. }
  1265. }
  1266. if (i == ARRAY_SIZE(proto_variant_names)) {
  1267. rc = -EINVAL;
  1268. goto out;
  1269. }
  1270. if (dev->encode_wakeup) {
  1271. u64 mask = 1ULL << protocol;
  1272. ir_raw_load_modules(&mask);
  1273. if (!mask) {
  1274. rc = -EINVAL;
  1275. goto out;
  1276. }
  1277. }
  1278. }
  1279. if (dev->wakeup_protocol != protocol) {
  1280. dev->wakeup_protocol = protocol;
  1281. IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
  1282. if (protocol == RC_TYPE_RC6_MCE)
  1283. dev->scancode_wakeup_filter.data = 0x800f0000;
  1284. else
  1285. dev->scancode_wakeup_filter.data = 0;
  1286. dev->scancode_wakeup_filter.mask = 0;
  1287. rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
  1288. if (rc == 0)
  1289. rc = len;
  1290. } else {
  1291. rc = len;
  1292. }
  1293. out:
  1294. mutex_unlock(&dev->lock);
  1295. return rc;
  1296. }
  1297. static void rc_dev_release(struct device *device)
  1298. {
  1299. struct rc_dev *dev = to_rc_dev(device);
  1300. kfree(dev);
  1301. }
  1302. #define ADD_HOTPLUG_VAR(fmt, val...) \
  1303. do { \
  1304. int err = add_uevent_var(env, fmt, val); \
  1305. if (err) \
  1306. return err; \
  1307. } while (0)
  1308. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  1309. {
  1310. struct rc_dev *dev = to_rc_dev(device);
  1311. if (dev->rc_map.name)
  1312. ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
  1313. if (dev->driver_name)
  1314. ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
  1315. return 0;
  1316. }
  1317. /*
  1318. * Static device attribute struct with the sysfs attributes for IR's
  1319. */
  1320. static DEVICE_ATTR(protocols, 0644, show_protocols, store_protocols);
  1321. static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
  1322. store_wakeup_protocols);
  1323. static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
  1324. show_filter, store_filter, RC_FILTER_NORMAL, false);
  1325. static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
  1326. show_filter, store_filter, RC_FILTER_NORMAL, true);
  1327. static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
  1328. show_filter, store_filter, RC_FILTER_WAKEUP, false);
  1329. static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
  1330. show_filter, store_filter, RC_FILTER_WAKEUP, true);
  1331. static struct attribute *rc_dev_protocol_attrs[] = {
  1332. &dev_attr_protocols.attr,
  1333. NULL,
  1334. };
  1335. static struct attribute_group rc_dev_protocol_attr_grp = {
  1336. .attrs = rc_dev_protocol_attrs,
  1337. };
  1338. static struct attribute *rc_dev_filter_attrs[] = {
  1339. &dev_attr_filter.attr.attr,
  1340. &dev_attr_filter_mask.attr.attr,
  1341. NULL,
  1342. };
  1343. static struct attribute_group rc_dev_filter_attr_grp = {
  1344. .attrs = rc_dev_filter_attrs,
  1345. };
  1346. static struct attribute *rc_dev_wakeup_filter_attrs[] = {
  1347. &dev_attr_wakeup_filter.attr.attr,
  1348. &dev_attr_wakeup_filter_mask.attr.attr,
  1349. &dev_attr_wakeup_protocols.attr,
  1350. NULL,
  1351. };
  1352. static struct attribute_group rc_dev_wakeup_filter_attr_grp = {
  1353. .attrs = rc_dev_wakeup_filter_attrs,
  1354. };
  1355. static struct device_type rc_dev_type = {
  1356. .release = rc_dev_release,
  1357. .uevent = rc_dev_uevent,
  1358. };
  1359. struct rc_dev *rc_allocate_device(enum rc_driver_type type)
  1360. {
  1361. struct rc_dev *dev;
  1362. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1363. if (!dev)
  1364. return NULL;
  1365. if (type != RC_DRIVER_IR_RAW_TX) {
  1366. dev->input_dev = input_allocate_device();
  1367. if (!dev->input_dev) {
  1368. kfree(dev);
  1369. return NULL;
  1370. }
  1371. dev->input_dev->getkeycode = ir_getkeycode;
  1372. dev->input_dev->setkeycode = ir_setkeycode;
  1373. input_set_drvdata(dev->input_dev, dev);
  1374. setup_timer(&dev->timer_keyup, ir_timer_keyup,
  1375. (unsigned long)dev);
  1376. spin_lock_init(&dev->rc_map.lock);
  1377. spin_lock_init(&dev->keylock);
  1378. }
  1379. mutex_init(&dev->lock);
  1380. dev->dev.type = &rc_dev_type;
  1381. dev->dev.class = &rc_class;
  1382. device_initialize(&dev->dev);
  1383. dev->driver_type = type;
  1384. __module_get(THIS_MODULE);
  1385. return dev;
  1386. }
  1387. EXPORT_SYMBOL_GPL(rc_allocate_device);
  1388. void rc_free_device(struct rc_dev *dev)
  1389. {
  1390. if (!dev)
  1391. return;
  1392. input_free_device(dev->input_dev);
  1393. put_device(&dev->dev);
  1394. /* kfree(dev) will be called by the callback function
  1395. rc_dev_release() */
  1396. module_put(THIS_MODULE);
  1397. }
  1398. EXPORT_SYMBOL_GPL(rc_free_device);
  1399. static void devm_rc_alloc_release(struct device *dev, void *res)
  1400. {
  1401. rc_free_device(*(struct rc_dev **)res);
  1402. }
  1403. struct rc_dev *devm_rc_allocate_device(struct device *dev,
  1404. enum rc_driver_type type)
  1405. {
  1406. struct rc_dev **dr, *rc;
  1407. dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
  1408. if (!dr)
  1409. return NULL;
  1410. rc = rc_allocate_device(type);
  1411. if (!rc) {
  1412. devres_free(dr);
  1413. return NULL;
  1414. }
  1415. rc->dev.parent = dev;
  1416. rc->managed_alloc = true;
  1417. *dr = rc;
  1418. devres_add(dev, dr);
  1419. return rc;
  1420. }
  1421. EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
  1422. static int rc_setup_rx_device(struct rc_dev *dev)
  1423. {
  1424. int rc;
  1425. struct rc_map *rc_map;
  1426. u64 rc_type;
  1427. if (!dev->map_name)
  1428. return -EINVAL;
  1429. rc_map = rc_map_get(dev->map_name);
  1430. if (!rc_map)
  1431. rc_map = rc_map_get(RC_MAP_EMPTY);
  1432. if (!rc_map || !rc_map->scan || rc_map->size == 0)
  1433. return -EINVAL;
  1434. rc = ir_setkeytable(dev, rc_map);
  1435. if (rc)
  1436. return rc;
  1437. rc_type = BIT_ULL(rc_map->rc_type);
  1438. if (dev->change_protocol) {
  1439. rc = dev->change_protocol(dev, &rc_type);
  1440. if (rc < 0)
  1441. goto out_table;
  1442. dev->enabled_protocols = rc_type;
  1443. }
  1444. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1445. ir_raw_load_modules(&rc_type);
  1446. set_bit(EV_KEY, dev->input_dev->evbit);
  1447. set_bit(EV_REP, dev->input_dev->evbit);
  1448. set_bit(EV_MSC, dev->input_dev->evbit);
  1449. set_bit(MSC_SCAN, dev->input_dev->mscbit);
  1450. if (dev->open)
  1451. dev->input_dev->open = ir_open;
  1452. if (dev->close)
  1453. dev->input_dev->close = ir_close;
  1454. /*
  1455. * Default delay of 250ms is too short for some protocols, especially
  1456. * since the timeout is currently set to 250ms. Increase it to 500ms,
  1457. * to avoid wrong repetition of the keycodes. Note that this must be
  1458. * set after the call to input_register_device().
  1459. */
  1460. dev->input_dev->rep[REP_DELAY] = 500;
  1461. /*
  1462. * As a repeat event on protocols like RC-5 and NEC take as long as
  1463. * 110/114ms, using 33ms as a repeat period is not the right thing
  1464. * to do.
  1465. */
  1466. dev->input_dev->rep[REP_PERIOD] = 125;
  1467. dev->input_dev->dev.parent = &dev->dev;
  1468. memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
  1469. dev->input_dev->phys = dev->input_phys;
  1470. dev->input_dev->name = dev->input_name;
  1471. /* rc_open will be called here */
  1472. rc = input_register_device(dev->input_dev);
  1473. if (rc)
  1474. goto out_table;
  1475. return 0;
  1476. out_table:
  1477. ir_free_table(&dev->rc_map);
  1478. return rc;
  1479. }
  1480. static void rc_free_rx_device(struct rc_dev *dev)
  1481. {
  1482. if (!dev || dev->driver_type == RC_DRIVER_IR_RAW_TX)
  1483. return;
  1484. ir_free_table(&dev->rc_map);
  1485. input_unregister_device(dev->input_dev);
  1486. dev->input_dev = NULL;
  1487. }
  1488. int rc_register_device(struct rc_dev *dev)
  1489. {
  1490. static bool raw_init; /* 'false' default value, raw decoders loaded? */
  1491. const char *path;
  1492. int attr = 0;
  1493. int minor;
  1494. int rc;
  1495. if (!dev)
  1496. return -EINVAL;
  1497. minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
  1498. if (minor < 0)
  1499. return minor;
  1500. dev->minor = minor;
  1501. dev_set_name(&dev->dev, "rc%u", dev->minor);
  1502. dev_set_drvdata(&dev->dev, dev);
  1503. atomic_set(&dev->initialized, 0);
  1504. dev->dev.groups = dev->sysfs_groups;
  1505. if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
  1506. dev->sysfs_groups[attr++] = &rc_dev_protocol_attr_grp;
  1507. if (dev->s_filter)
  1508. dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
  1509. if (dev->s_wakeup_filter)
  1510. dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
  1511. dev->sysfs_groups[attr++] = NULL;
  1512. rc = device_add(&dev->dev);
  1513. if (rc)
  1514. goto out_unlock;
  1515. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1516. dev_info(&dev->dev, "%s as %s\n",
  1517. dev->input_name ?: "Unspecified device", path ?: "N/A");
  1518. kfree(path);
  1519. if (dev->driver_type == RC_DRIVER_IR_RAW ||
  1520. dev->driver_type == RC_DRIVER_IR_RAW_TX) {
  1521. if (!raw_init) {
  1522. request_module_nowait("ir-lirc-codec");
  1523. raw_init = true;
  1524. }
  1525. rc = ir_raw_event_register(dev);
  1526. if (rc < 0)
  1527. goto out_dev;
  1528. }
  1529. if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
  1530. rc = rc_setup_rx_device(dev);
  1531. if (rc)
  1532. goto out_raw;
  1533. }
  1534. /* Allow the RC sysfs nodes to be accessible */
  1535. atomic_set(&dev->initialized, 1);
  1536. IR_dprintk(1, "Registered rc%u (driver: %s)\n",
  1537. dev->minor,
  1538. dev->driver_name ? dev->driver_name : "unknown");
  1539. return 0;
  1540. out_raw:
  1541. ir_raw_event_unregister(dev);
  1542. out_dev:
  1543. device_del(&dev->dev);
  1544. out_unlock:
  1545. ida_simple_remove(&rc_ida, minor);
  1546. return rc;
  1547. }
  1548. EXPORT_SYMBOL_GPL(rc_register_device);
  1549. static void devm_rc_release(struct device *dev, void *res)
  1550. {
  1551. rc_unregister_device(*(struct rc_dev **)res);
  1552. }
  1553. int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
  1554. {
  1555. struct rc_dev **dr;
  1556. int ret;
  1557. dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
  1558. if (!dr)
  1559. return -ENOMEM;
  1560. ret = rc_register_device(dev);
  1561. if (ret) {
  1562. devres_free(dr);
  1563. return ret;
  1564. }
  1565. *dr = dev;
  1566. devres_add(parent, dr);
  1567. return 0;
  1568. }
  1569. EXPORT_SYMBOL_GPL(devm_rc_register_device);
  1570. void rc_unregister_device(struct rc_dev *dev)
  1571. {
  1572. if (!dev)
  1573. return;
  1574. del_timer_sync(&dev->timer_keyup);
  1575. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1576. ir_raw_event_unregister(dev);
  1577. rc_free_rx_device(dev);
  1578. device_del(&dev->dev);
  1579. ida_simple_remove(&rc_ida, dev->minor);
  1580. if (!dev->managed_alloc)
  1581. rc_free_device(dev);
  1582. }
  1583. EXPORT_SYMBOL_GPL(rc_unregister_device);
  1584. /*
  1585. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  1586. */
  1587. static int __init rc_core_init(void)
  1588. {
  1589. int rc = class_register(&rc_class);
  1590. if (rc) {
  1591. pr_err("rc_core: unable to register rc class\n");
  1592. return rc;
  1593. }
  1594. led_trigger_register_simple("rc-feedback", &led_feedback);
  1595. rc_map_register(&empty_map);
  1596. return 0;
  1597. }
  1598. static void __exit rc_core_exit(void)
  1599. {
  1600. class_unregister(&rc_class);
  1601. led_trigger_unregister_simple(led_feedback);
  1602. rc_map_unregister(&empty_map);
  1603. }
  1604. subsys_initcall(rc_core_init);
  1605. module_exit(rc_core_exit);
  1606. int rc_core_debug; /* ir_debug level (0,1,2) */
  1607. EXPORT_SYMBOL_GPL(rc_core_debug);
  1608. module_param_named(debug, rc_core_debug, int, 0644);
  1609. MODULE_AUTHOR("Mauro Carvalho Chehab");
  1610. MODULE_LICENSE("GPL");