rc-main.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. // SPDX-License-Identifier: GPL-2.0
  2. // rc-main.c - Remote Controller core module
  3. //
  4. // Copyright (C) 2009-2010 by Mauro Carvalho Chehab
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <media/rc-core.h>
  7. #include <linux/bsearch.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/delay.h>
  10. #include <linux/input.h>
  11. #include <linux/leds.h>
  12. #include <linux/slab.h>
  13. #include <linux/idr.h>
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include "rc-core-priv.h"
  17. /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
  18. #define IR_TAB_MIN_SIZE 256
  19. #define IR_TAB_MAX_SIZE 8192
  20. static const struct {
  21. const char *name;
  22. unsigned int repeat_period;
  23. unsigned int scancode_bits;
  24. } protocols[] = {
  25. [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 125 },
  26. [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 125 },
  27. [RC_PROTO_RC5] = { .name = "rc-5",
  28. .scancode_bits = 0x1f7f, .repeat_period = 114 },
  29. [RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
  30. .scancode_bits = 0x1f7f3f, .repeat_period = 114 },
  31. [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
  32. .scancode_bits = 0x2fff, .repeat_period = 114 },
  33. [RC_PROTO_JVC] = { .name = "jvc",
  34. .scancode_bits = 0xffff, .repeat_period = 125 },
  35. [RC_PROTO_SONY12] = { .name = "sony-12",
  36. .scancode_bits = 0x1f007f, .repeat_period = 100 },
  37. [RC_PROTO_SONY15] = { .name = "sony-15",
  38. .scancode_bits = 0xff007f, .repeat_period = 100 },
  39. [RC_PROTO_SONY20] = { .name = "sony-20",
  40. .scancode_bits = 0x1fff7f, .repeat_period = 100 },
  41. [RC_PROTO_NEC] = { .name = "nec",
  42. .scancode_bits = 0xffff, .repeat_period = 110 },
  43. [RC_PROTO_NECX] = { .name = "nec-x",
  44. .scancode_bits = 0xffffff, .repeat_period = 110 },
  45. [RC_PROTO_NEC32] = { .name = "nec-32",
  46. .scancode_bits = 0xffffffff, .repeat_period = 110 },
  47. [RC_PROTO_SANYO] = { .name = "sanyo",
  48. .scancode_bits = 0x1fffff, .repeat_period = 125 },
  49. [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
  50. .scancode_bits = 0xffffff, .repeat_period = 100 },
  51. [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
  52. .scancode_bits = 0x1fffff, .repeat_period = 100 },
  53. [RC_PROTO_RC6_0] = { .name = "rc-6-0",
  54. .scancode_bits = 0xffff, .repeat_period = 114 },
  55. [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
  56. .scancode_bits = 0xfffff, .repeat_period = 114 },
  57. [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
  58. .scancode_bits = 0xffffff, .repeat_period = 114 },
  59. [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
  60. .scancode_bits = 0xffffffff, .repeat_period = 114 },
  61. [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
  62. .scancode_bits = 0xffff7fff, .repeat_period = 114 },
  63. [RC_PROTO_SHARP] = { .name = "sharp",
  64. .scancode_bits = 0x1fff, .repeat_period = 125 },
  65. [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 125 },
  66. [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 0 },
  67. [RC_PROTO_IMON] = { .name = "imon",
  68. .scancode_bits = 0x7fffffff, .repeat_period = 114 },
  69. [RC_PROTO_RCMM12] = { .name = "rc-mm-12",
  70. .scancode_bits = 0x00000fff, .repeat_period = 114 },
  71. [RC_PROTO_RCMM24] = { .name = "rc-mm-24",
  72. .scancode_bits = 0x00ffffff, .repeat_period = 114 },
  73. [RC_PROTO_RCMM32] = { .name = "rc-mm-32",
  74. .scancode_bits = 0xffffffff, .repeat_period = 114 },
  75. [RC_PROTO_XBOX_DVD] = { .name = "xbox-dvd", .repeat_period = 64 },
  76. };
  77. /* Used to keep track of known keymaps */
  78. static LIST_HEAD(rc_map_list);
  79. static DEFINE_SPINLOCK(rc_map_lock);
  80. static struct led_trigger *led_feedback;
  81. /* Used to keep track of rc devices */
  82. static DEFINE_IDA(rc_ida);
  83. static struct rc_map_list *seek_rc_map(const char *name)
  84. {
  85. struct rc_map_list *map = NULL;
  86. spin_lock(&rc_map_lock);
  87. list_for_each_entry(map, &rc_map_list, list) {
  88. if (!strcmp(name, map->map.name)) {
  89. spin_unlock(&rc_map_lock);
  90. return map;
  91. }
  92. }
  93. spin_unlock(&rc_map_lock);
  94. return NULL;
  95. }
  96. struct rc_map *rc_map_get(const char *name)
  97. {
  98. struct rc_map_list *map;
  99. map = seek_rc_map(name);
  100. #ifdef CONFIG_MODULES
  101. if (!map) {
  102. int rc = request_module("%s", name);
  103. if (rc < 0) {
  104. pr_err("Couldn't load IR keymap %s\n", name);
  105. return NULL;
  106. }
  107. msleep(20); /* Give some time for IR to register */
  108. map = seek_rc_map(name);
  109. }
  110. #endif
  111. if (!map) {
  112. pr_err("IR keymap %s not found\n", name);
  113. return NULL;
  114. }
  115. printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
  116. return &map->map;
  117. }
  118. EXPORT_SYMBOL_GPL(rc_map_get);
  119. int rc_map_register(struct rc_map_list *map)
  120. {
  121. spin_lock(&rc_map_lock);
  122. list_add_tail(&map->list, &rc_map_list);
  123. spin_unlock(&rc_map_lock);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL_GPL(rc_map_register);
  127. void rc_map_unregister(struct rc_map_list *map)
  128. {
  129. spin_lock(&rc_map_lock);
  130. list_del(&map->list);
  131. spin_unlock(&rc_map_lock);
  132. }
  133. EXPORT_SYMBOL_GPL(rc_map_unregister);
  134. static struct rc_map_table empty[] = {
  135. { 0x2a, KEY_COFFEE },
  136. };
  137. static struct rc_map_list empty_map = {
  138. .map = {
  139. .scan = empty,
  140. .size = ARRAY_SIZE(empty),
  141. .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
  142. .name = RC_MAP_EMPTY,
  143. }
  144. };
  145. /**
  146. * scancode_to_u64() - converts scancode in &struct input_keymap_entry
  147. * @ke: keymap entry containing scancode to be converted.
  148. * @scancode: pointer to the location where converted scancode should
  149. * be stored.
  150. *
  151. * This function is a version of input_scancode_to_scalar specialized for
  152. * rc-core.
  153. */
  154. static int scancode_to_u64(const struct input_keymap_entry *ke, u64 *scancode)
  155. {
  156. switch (ke->len) {
  157. case 1:
  158. *scancode = *((u8 *)ke->scancode);
  159. break;
  160. case 2:
  161. *scancode = *((u16 *)ke->scancode);
  162. break;
  163. case 4:
  164. *scancode = *((u32 *)ke->scancode);
  165. break;
  166. case 8:
  167. *scancode = *((u64 *)ke->scancode);
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. return 0;
  173. }
  174. /**
  175. * ir_create_table() - initializes a scancode table
  176. * @dev: the rc_dev device
  177. * @rc_map: the rc_map to initialize
  178. * @name: name to assign to the table
  179. * @rc_proto: ir type to assign to the new table
  180. * @size: initial size of the table
  181. *
  182. * This routine will initialize the rc_map and will allocate
  183. * memory to hold at least the specified number of elements.
  184. *
  185. * return: zero on success or a negative error code
  186. */
  187. static int ir_create_table(struct rc_dev *dev, struct rc_map *rc_map,
  188. const char *name, u64 rc_proto, size_t size)
  189. {
  190. rc_map->name = kstrdup(name, GFP_KERNEL);
  191. if (!rc_map->name)
  192. return -ENOMEM;
  193. rc_map->rc_proto = rc_proto;
  194. rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
  195. rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
  196. rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
  197. if (!rc_map->scan) {
  198. kfree(rc_map->name);
  199. rc_map->name = NULL;
  200. return -ENOMEM;
  201. }
  202. dev_dbg(&dev->dev, "Allocated space for %u keycode entries (%u bytes)\n",
  203. rc_map->size, rc_map->alloc);
  204. return 0;
  205. }
  206. /**
  207. * ir_free_table() - frees memory allocated by a scancode table
  208. * @rc_map: the table whose mappings need to be freed
  209. *
  210. * This routine will free memory alloctaed for key mappings used by given
  211. * scancode table.
  212. */
  213. static void ir_free_table(struct rc_map *rc_map)
  214. {
  215. rc_map->size = 0;
  216. kfree(rc_map->name);
  217. rc_map->name = NULL;
  218. kfree(rc_map->scan);
  219. rc_map->scan = NULL;
  220. }
  221. /**
  222. * ir_resize_table() - resizes a scancode table if necessary
  223. * @dev: the rc_dev device
  224. * @rc_map: the rc_map to resize
  225. * @gfp_flags: gfp flags to use when allocating memory
  226. *
  227. * This routine will shrink the rc_map if it has lots of
  228. * unused entries and grow it if it is full.
  229. *
  230. * return: zero on success or a negative error code
  231. */
  232. static int ir_resize_table(struct rc_dev *dev, struct rc_map *rc_map,
  233. gfp_t gfp_flags)
  234. {
  235. unsigned int oldalloc = rc_map->alloc;
  236. unsigned int newalloc = oldalloc;
  237. struct rc_map_table *oldscan = rc_map->scan;
  238. struct rc_map_table *newscan;
  239. if (rc_map->size == rc_map->len) {
  240. /* All entries in use -> grow keytable */
  241. if (rc_map->alloc >= IR_TAB_MAX_SIZE)
  242. return -ENOMEM;
  243. newalloc *= 2;
  244. dev_dbg(&dev->dev, "Growing table to %u bytes\n", newalloc);
  245. }
  246. if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
  247. /* Less than 1/3 of entries in use -> shrink keytable */
  248. newalloc /= 2;
  249. dev_dbg(&dev->dev, "Shrinking table to %u bytes\n", newalloc);
  250. }
  251. if (newalloc == oldalloc)
  252. return 0;
  253. newscan = kmalloc(newalloc, gfp_flags);
  254. if (!newscan)
  255. return -ENOMEM;
  256. memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
  257. rc_map->scan = newscan;
  258. rc_map->alloc = newalloc;
  259. rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
  260. kfree(oldscan);
  261. return 0;
  262. }
  263. /**
  264. * ir_update_mapping() - set a keycode in the scancode->keycode table
  265. * @dev: the struct rc_dev device descriptor
  266. * @rc_map: scancode table to be adjusted
  267. * @index: index of the mapping that needs to be updated
  268. * @new_keycode: the desired keycode
  269. *
  270. * This routine is used to update scancode->keycode mapping at given
  271. * position.
  272. *
  273. * return: previous keycode assigned to the mapping
  274. *
  275. */
  276. static unsigned int ir_update_mapping(struct rc_dev *dev,
  277. struct rc_map *rc_map,
  278. unsigned int index,
  279. unsigned int new_keycode)
  280. {
  281. int old_keycode = rc_map->scan[index].keycode;
  282. int i;
  283. /* Did the user wish to remove the mapping? */
  284. if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
  285. dev_dbg(&dev->dev, "#%d: Deleting scan 0x%04llx\n",
  286. index, rc_map->scan[index].scancode);
  287. rc_map->len--;
  288. memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
  289. (rc_map->len - index) * sizeof(struct rc_map_table));
  290. } else {
  291. dev_dbg(&dev->dev, "#%d: %s scan 0x%04llx with key 0x%04x\n",
  292. index,
  293. old_keycode == KEY_RESERVED ? "New" : "Replacing",
  294. rc_map->scan[index].scancode, new_keycode);
  295. rc_map->scan[index].keycode = new_keycode;
  296. __set_bit(new_keycode, dev->input_dev->keybit);
  297. }
  298. if (old_keycode != KEY_RESERVED) {
  299. /* A previous mapping was updated... */
  300. __clear_bit(old_keycode, dev->input_dev->keybit);
  301. /* ... but another scancode might use the same keycode */
  302. for (i = 0; i < rc_map->len; i++) {
  303. if (rc_map->scan[i].keycode == old_keycode) {
  304. __set_bit(old_keycode, dev->input_dev->keybit);
  305. break;
  306. }
  307. }
  308. /* Possibly shrink the keytable, failure is not a problem */
  309. ir_resize_table(dev, rc_map, GFP_ATOMIC);
  310. }
  311. return old_keycode;
  312. }
  313. /**
  314. * ir_establish_scancode() - set a keycode in the scancode->keycode table
  315. * @dev: the struct rc_dev device descriptor
  316. * @rc_map: scancode table to be searched
  317. * @scancode: the desired scancode
  318. * @resize: controls whether we allowed to resize the table to
  319. * accommodate not yet present scancodes
  320. *
  321. * This routine is used to locate given scancode in rc_map.
  322. * If scancode is not yet present the routine will allocate a new slot
  323. * for it.
  324. *
  325. * return: index of the mapping containing scancode in question
  326. * or -1U in case of failure.
  327. */
  328. static unsigned int ir_establish_scancode(struct rc_dev *dev,
  329. struct rc_map *rc_map,
  330. u64 scancode, bool resize)
  331. {
  332. unsigned int i;
  333. /*
  334. * Unfortunately, some hardware-based IR decoders don't provide
  335. * all bits for the complete IR code. In general, they provide only
  336. * the command part of the IR code. Yet, as it is possible to replace
  337. * the provided IR with another one, it is needed to allow loading
  338. * IR tables from other remotes. So, we support specifying a mask to
  339. * indicate the valid bits of the scancodes.
  340. */
  341. if (dev->scancode_mask)
  342. scancode &= dev->scancode_mask;
  343. /* First check if we already have a mapping for this ir command */
  344. for (i = 0; i < rc_map->len; i++) {
  345. if (rc_map->scan[i].scancode == scancode)
  346. return i;
  347. /* Keytable is sorted from lowest to highest scancode */
  348. if (rc_map->scan[i].scancode >= scancode)
  349. break;
  350. }
  351. /* No previous mapping found, we might need to grow the table */
  352. if (rc_map->size == rc_map->len) {
  353. if (!resize || ir_resize_table(dev, rc_map, GFP_ATOMIC))
  354. return -1U;
  355. }
  356. /* i is the proper index to insert our new keycode */
  357. if (i < rc_map->len)
  358. memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
  359. (rc_map->len - i) * sizeof(struct rc_map_table));
  360. rc_map->scan[i].scancode = scancode;
  361. rc_map->scan[i].keycode = KEY_RESERVED;
  362. rc_map->len++;
  363. return i;
  364. }
  365. /**
  366. * ir_setkeycode() - set a keycode in the scancode->keycode table
  367. * @idev: the struct input_dev device descriptor
  368. * @ke: Input keymap entry
  369. * @old_keycode: result
  370. *
  371. * This routine is used to handle evdev EVIOCSKEY ioctl.
  372. *
  373. * return: -EINVAL if the keycode could not be inserted, otherwise zero.
  374. */
  375. static int ir_setkeycode(struct input_dev *idev,
  376. const struct input_keymap_entry *ke,
  377. unsigned int *old_keycode)
  378. {
  379. struct rc_dev *rdev = input_get_drvdata(idev);
  380. struct rc_map *rc_map = &rdev->rc_map;
  381. unsigned int index;
  382. u64 scancode;
  383. int retval = 0;
  384. unsigned long flags;
  385. spin_lock_irqsave(&rc_map->lock, flags);
  386. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  387. index = ke->index;
  388. if (index >= rc_map->len) {
  389. retval = -EINVAL;
  390. goto out;
  391. }
  392. } else {
  393. retval = scancode_to_u64(ke, &scancode);
  394. if (retval)
  395. goto out;
  396. index = ir_establish_scancode(rdev, rc_map, scancode, true);
  397. if (index >= rc_map->len) {
  398. retval = -ENOMEM;
  399. goto out;
  400. }
  401. }
  402. *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
  403. out:
  404. spin_unlock_irqrestore(&rc_map->lock, flags);
  405. return retval;
  406. }
  407. /**
  408. * ir_setkeytable() - sets several entries in the scancode->keycode table
  409. * @dev: the struct rc_dev device descriptor
  410. * @from: the struct rc_map to copy entries from
  411. *
  412. * This routine is used to handle table initialization.
  413. *
  414. * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
  415. */
  416. static int ir_setkeytable(struct rc_dev *dev, const struct rc_map *from)
  417. {
  418. struct rc_map *rc_map = &dev->rc_map;
  419. unsigned int i, index;
  420. int rc;
  421. rc = ir_create_table(dev, rc_map, from->name, from->rc_proto,
  422. from->size);
  423. if (rc)
  424. return rc;
  425. for (i = 0; i < from->size; i++) {
  426. index = ir_establish_scancode(dev, rc_map,
  427. from->scan[i].scancode, false);
  428. if (index >= rc_map->len) {
  429. rc = -ENOMEM;
  430. break;
  431. }
  432. ir_update_mapping(dev, rc_map, index,
  433. from->scan[i].keycode);
  434. }
  435. if (rc)
  436. ir_free_table(rc_map);
  437. return rc;
  438. }
  439. static int rc_map_cmp(const void *key, const void *elt)
  440. {
  441. const u64 *scancode = key;
  442. const struct rc_map_table *e = elt;
  443. if (*scancode < e->scancode)
  444. return -1;
  445. else if (*scancode > e->scancode)
  446. return 1;
  447. return 0;
  448. }
  449. /**
  450. * ir_lookup_by_scancode() - locate mapping by scancode
  451. * @rc_map: the struct rc_map to search
  452. * @scancode: scancode to look for in the table
  453. *
  454. * This routine performs binary search in RC keykeymap table for
  455. * given scancode.
  456. *
  457. * return: index in the table, -1U if not found
  458. */
  459. static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
  460. u64 scancode)
  461. {
  462. struct rc_map_table *res;
  463. res = bsearch(&scancode, rc_map->scan, rc_map->len,
  464. sizeof(struct rc_map_table), rc_map_cmp);
  465. if (!res)
  466. return -1U;
  467. else
  468. return res - rc_map->scan;
  469. }
  470. /**
  471. * ir_getkeycode() - get a keycode from the scancode->keycode table
  472. * @idev: the struct input_dev device descriptor
  473. * @ke: Input keymap entry
  474. *
  475. * This routine is used to handle evdev EVIOCGKEY ioctl.
  476. *
  477. * return: always returns zero.
  478. */
  479. static int ir_getkeycode(struct input_dev *idev,
  480. struct input_keymap_entry *ke)
  481. {
  482. struct rc_dev *rdev = input_get_drvdata(idev);
  483. struct rc_map *rc_map = &rdev->rc_map;
  484. struct rc_map_table *entry;
  485. unsigned long flags;
  486. unsigned int index;
  487. u64 scancode;
  488. int retval;
  489. spin_lock_irqsave(&rc_map->lock, flags);
  490. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  491. index = ke->index;
  492. } else {
  493. retval = scancode_to_u64(ke, &scancode);
  494. if (retval)
  495. goto out;
  496. index = ir_lookup_by_scancode(rc_map, scancode);
  497. }
  498. if (index < rc_map->len) {
  499. entry = &rc_map->scan[index];
  500. ke->index = index;
  501. ke->keycode = entry->keycode;
  502. ke->len = sizeof(entry->scancode);
  503. memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
  504. } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
  505. /*
  506. * We do not really know the valid range of scancodes
  507. * so let's respond with KEY_RESERVED to anything we
  508. * do not have mapping for [yet].
  509. */
  510. ke->index = index;
  511. ke->keycode = KEY_RESERVED;
  512. } else {
  513. retval = -EINVAL;
  514. goto out;
  515. }
  516. retval = 0;
  517. out:
  518. spin_unlock_irqrestore(&rc_map->lock, flags);
  519. return retval;
  520. }
  521. /**
  522. * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  523. * @dev: the struct rc_dev descriptor of the device
  524. * @scancode: the scancode to look for
  525. *
  526. * This routine is used by drivers which need to convert a scancode to a
  527. * keycode. Normally it should not be used since drivers should have no
  528. * interest in keycodes.
  529. *
  530. * return: the corresponding keycode, or KEY_RESERVED
  531. */
  532. u32 rc_g_keycode_from_table(struct rc_dev *dev, u64 scancode)
  533. {
  534. struct rc_map *rc_map = &dev->rc_map;
  535. unsigned int keycode;
  536. unsigned int index;
  537. unsigned long flags;
  538. spin_lock_irqsave(&rc_map->lock, flags);
  539. index = ir_lookup_by_scancode(rc_map, scancode);
  540. keycode = index < rc_map->len ?
  541. rc_map->scan[index].keycode : KEY_RESERVED;
  542. spin_unlock_irqrestore(&rc_map->lock, flags);
  543. if (keycode != KEY_RESERVED)
  544. dev_dbg(&dev->dev, "%s: scancode 0x%04llx keycode 0x%02x\n",
  545. dev->device_name, scancode, keycode);
  546. return keycode;
  547. }
  548. EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
  549. /**
  550. * ir_do_keyup() - internal function to signal the release of a keypress
  551. * @dev: the struct rc_dev descriptor of the device
  552. * @sync: whether or not to call input_sync
  553. *
  554. * This function is used internally to release a keypress, it must be
  555. * called with keylock held.
  556. */
  557. static void ir_do_keyup(struct rc_dev *dev, bool sync)
  558. {
  559. if (!dev->keypressed)
  560. return;
  561. dev_dbg(&dev->dev, "keyup key 0x%04x\n", dev->last_keycode);
  562. del_timer(&dev->timer_repeat);
  563. input_report_key(dev->input_dev, dev->last_keycode, 0);
  564. led_trigger_event(led_feedback, LED_OFF);
  565. if (sync)
  566. input_sync(dev->input_dev);
  567. dev->keypressed = false;
  568. }
  569. /**
  570. * rc_keyup() - signals the release of a keypress
  571. * @dev: the struct rc_dev descriptor of the device
  572. *
  573. * This routine is used to signal that a key has been released on the
  574. * remote control.
  575. */
  576. void rc_keyup(struct rc_dev *dev)
  577. {
  578. unsigned long flags;
  579. spin_lock_irqsave(&dev->keylock, flags);
  580. ir_do_keyup(dev, true);
  581. spin_unlock_irqrestore(&dev->keylock, flags);
  582. }
  583. EXPORT_SYMBOL_GPL(rc_keyup);
  584. /**
  585. * ir_timer_keyup() - generates a keyup event after a timeout
  586. *
  587. * @t: a pointer to the struct timer_list
  588. *
  589. * This routine will generate a keyup event some time after a keydown event
  590. * is generated when no further activity has been detected.
  591. */
  592. static void ir_timer_keyup(struct timer_list *t)
  593. {
  594. struct rc_dev *dev = from_timer(dev, t, timer_keyup);
  595. unsigned long flags;
  596. /*
  597. * ir->keyup_jiffies is used to prevent a race condition if a
  598. * hardware interrupt occurs at this point and the keyup timer
  599. * event is moved further into the future as a result.
  600. *
  601. * The timer will then be reactivated and this function called
  602. * again in the future. We need to exit gracefully in that case
  603. * to allow the input subsystem to do its auto-repeat magic or
  604. * a keyup event might follow immediately after the keydown.
  605. */
  606. spin_lock_irqsave(&dev->keylock, flags);
  607. if (time_is_before_eq_jiffies(dev->keyup_jiffies))
  608. ir_do_keyup(dev, true);
  609. spin_unlock_irqrestore(&dev->keylock, flags);
  610. }
  611. /**
  612. * ir_timer_repeat() - generates a repeat event after a timeout
  613. *
  614. * @t: a pointer to the struct timer_list
  615. *
  616. * This routine will generate a soft repeat event every REP_PERIOD
  617. * milliseconds.
  618. */
  619. static void ir_timer_repeat(struct timer_list *t)
  620. {
  621. struct rc_dev *dev = from_timer(dev, t, timer_repeat);
  622. struct input_dev *input = dev->input_dev;
  623. unsigned long flags;
  624. spin_lock_irqsave(&dev->keylock, flags);
  625. if (dev->keypressed) {
  626. input_event(input, EV_KEY, dev->last_keycode, 2);
  627. input_sync(input);
  628. if (input->rep[REP_PERIOD])
  629. mod_timer(&dev->timer_repeat, jiffies +
  630. msecs_to_jiffies(input->rep[REP_PERIOD]));
  631. }
  632. spin_unlock_irqrestore(&dev->keylock, flags);
  633. }
  634. static unsigned int repeat_period(int protocol)
  635. {
  636. if (protocol >= ARRAY_SIZE(protocols))
  637. return 100;
  638. return protocols[protocol].repeat_period;
  639. }
  640. /**
  641. * rc_repeat() - signals that a key is still pressed
  642. * @dev: the struct rc_dev descriptor of the device
  643. *
  644. * This routine is used by IR decoders when a repeat message which does
  645. * not include the necessary bits to reproduce the scancode has been
  646. * received.
  647. */
  648. void rc_repeat(struct rc_dev *dev)
  649. {
  650. unsigned long flags;
  651. unsigned int timeout = usecs_to_jiffies(dev->timeout) +
  652. msecs_to_jiffies(repeat_period(dev->last_protocol));
  653. struct lirc_scancode sc = {
  654. .scancode = dev->last_scancode, .rc_proto = dev->last_protocol,
  655. .keycode = dev->keypressed ? dev->last_keycode : KEY_RESERVED,
  656. .flags = LIRC_SCANCODE_FLAG_REPEAT |
  657. (dev->last_toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0)
  658. };
  659. if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
  660. lirc_scancode_event(dev, &sc);
  661. spin_lock_irqsave(&dev->keylock, flags);
  662. if (dev->last_scancode <= U32_MAX) {
  663. input_event(dev->input_dev, EV_MSC, MSC_SCAN,
  664. dev->last_scancode);
  665. input_sync(dev->input_dev);
  666. }
  667. if (dev->keypressed) {
  668. dev->keyup_jiffies = jiffies + timeout;
  669. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  670. }
  671. spin_unlock_irqrestore(&dev->keylock, flags);
  672. }
  673. EXPORT_SYMBOL_GPL(rc_repeat);
  674. /**
  675. * ir_do_keydown() - internal function to process a keypress
  676. * @dev: the struct rc_dev descriptor of the device
  677. * @protocol: the protocol of the keypress
  678. * @scancode: the scancode of the keypress
  679. * @keycode: the keycode of the keypress
  680. * @toggle: the toggle value of the keypress
  681. *
  682. * This function is used internally to register a keypress, it must be
  683. * called with keylock held.
  684. */
  685. static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
  686. u64 scancode, u32 keycode, u8 toggle)
  687. {
  688. bool new_event = (!dev->keypressed ||
  689. dev->last_protocol != protocol ||
  690. dev->last_scancode != scancode ||
  691. dev->last_toggle != toggle);
  692. struct lirc_scancode sc = {
  693. .scancode = scancode, .rc_proto = protocol,
  694. .flags = (toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0) |
  695. (!new_event ? LIRC_SCANCODE_FLAG_REPEAT : 0),
  696. .keycode = keycode
  697. };
  698. if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
  699. lirc_scancode_event(dev, &sc);
  700. if (new_event && dev->keypressed)
  701. ir_do_keyup(dev, false);
  702. if (scancode <= U32_MAX)
  703. input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
  704. dev->last_protocol = protocol;
  705. dev->last_scancode = scancode;
  706. dev->last_toggle = toggle;
  707. dev->last_keycode = keycode;
  708. if (new_event && keycode != KEY_RESERVED) {
  709. /* Register a keypress */
  710. dev->keypressed = true;
  711. dev_dbg(&dev->dev, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08llx\n",
  712. dev->device_name, keycode, protocol, scancode);
  713. input_report_key(dev->input_dev, keycode, 1);
  714. led_trigger_event(led_feedback, LED_FULL);
  715. }
  716. /*
  717. * For CEC, start sending repeat messages as soon as the first
  718. * repeated message is sent, as long as REP_DELAY = 0 and REP_PERIOD
  719. * is non-zero. Otherwise, the input layer will generate repeat
  720. * messages.
  721. */
  722. if (!new_event && keycode != KEY_RESERVED &&
  723. dev->allowed_protocols == RC_PROTO_BIT_CEC &&
  724. !timer_pending(&dev->timer_repeat) &&
  725. dev->input_dev->rep[REP_PERIOD] &&
  726. !dev->input_dev->rep[REP_DELAY]) {
  727. input_event(dev->input_dev, EV_KEY, keycode, 2);
  728. mod_timer(&dev->timer_repeat, jiffies +
  729. msecs_to_jiffies(dev->input_dev->rep[REP_PERIOD]));
  730. }
  731. input_sync(dev->input_dev);
  732. }
  733. /**
  734. * rc_keydown() - generates input event for a key press
  735. * @dev: the struct rc_dev descriptor of the device
  736. * @protocol: the protocol for the keypress
  737. * @scancode: the scancode for the keypress
  738. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  739. * support toggle values, this should be set to zero)
  740. *
  741. * This routine is used to signal that a key has been pressed on the
  742. * remote control.
  743. */
  744. void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u64 scancode,
  745. u8 toggle)
  746. {
  747. unsigned long flags;
  748. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  749. spin_lock_irqsave(&dev->keylock, flags);
  750. ir_do_keydown(dev, protocol, scancode, keycode, toggle);
  751. if (dev->keypressed) {
  752. dev->keyup_jiffies = jiffies + usecs_to_jiffies(dev->timeout) +
  753. msecs_to_jiffies(repeat_period(protocol));
  754. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  755. }
  756. spin_unlock_irqrestore(&dev->keylock, flags);
  757. }
  758. EXPORT_SYMBOL_GPL(rc_keydown);
  759. /**
  760. * rc_keydown_notimeout() - generates input event for a key press without
  761. * an automatic keyup event at a later time
  762. * @dev: the struct rc_dev descriptor of the device
  763. * @protocol: the protocol for the keypress
  764. * @scancode: the scancode for the keypress
  765. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  766. * support toggle values, this should be set to zero)
  767. *
  768. * This routine is used to signal that a key has been pressed on the
  769. * remote control. The driver must manually call rc_keyup() at a later stage.
  770. */
  771. void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
  772. u64 scancode, u8 toggle)
  773. {
  774. unsigned long flags;
  775. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  776. spin_lock_irqsave(&dev->keylock, flags);
  777. ir_do_keydown(dev, protocol, scancode, keycode, toggle);
  778. spin_unlock_irqrestore(&dev->keylock, flags);
  779. }
  780. EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
  781. /**
  782. * rc_validate_scancode() - checks that a scancode is valid for a protocol.
  783. * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
  784. * @proto: protocol
  785. * @scancode: scancode
  786. */
  787. bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
  788. {
  789. switch (proto) {
  790. /*
  791. * NECX has a 16-bit address; if the lower 8 bits match the upper
  792. * 8 bits inverted, then the address would match regular nec.
  793. */
  794. case RC_PROTO_NECX:
  795. if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
  796. return false;
  797. break;
  798. /*
  799. * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
  800. * of the command match the upper 8 bits inverted, then it would
  801. * be either NEC or NECX.
  802. */
  803. case RC_PROTO_NEC32:
  804. if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0)
  805. return false;
  806. break;
  807. /*
  808. * If the customer code (top 32-bit) is 0x800f, it is MCE else it
  809. * is regular mode-6a 32 bit
  810. */
  811. case RC_PROTO_RC6_MCE:
  812. if ((scancode & 0xffff0000) != 0x800f0000)
  813. return false;
  814. break;
  815. case RC_PROTO_RC6_6A_32:
  816. if ((scancode & 0xffff0000) == 0x800f0000)
  817. return false;
  818. break;
  819. default:
  820. break;
  821. }
  822. return true;
  823. }
  824. /**
  825. * rc_validate_filter() - checks that the scancode and mask are valid and
  826. * provides sensible defaults
  827. * @dev: the struct rc_dev descriptor of the device
  828. * @filter: the scancode and mask
  829. *
  830. * return: 0 or -EINVAL if the filter is not valid
  831. */
  832. static int rc_validate_filter(struct rc_dev *dev,
  833. struct rc_scancode_filter *filter)
  834. {
  835. u32 mask, s = filter->data;
  836. enum rc_proto protocol = dev->wakeup_protocol;
  837. if (protocol >= ARRAY_SIZE(protocols))
  838. return -EINVAL;
  839. mask = protocols[protocol].scancode_bits;
  840. if (!rc_validate_scancode(protocol, s))
  841. return -EINVAL;
  842. filter->data &= mask;
  843. filter->mask &= mask;
  844. /*
  845. * If we have to raw encode the IR for wakeup, we cannot have a mask
  846. */
  847. if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
  848. return -EINVAL;
  849. return 0;
  850. }
  851. int rc_open(struct rc_dev *rdev)
  852. {
  853. int rval = 0;
  854. if (!rdev)
  855. return -EINVAL;
  856. mutex_lock(&rdev->lock);
  857. if (!rdev->registered) {
  858. rval = -ENODEV;
  859. } else {
  860. if (!rdev->users++ && rdev->open)
  861. rval = rdev->open(rdev);
  862. if (rval)
  863. rdev->users--;
  864. }
  865. mutex_unlock(&rdev->lock);
  866. return rval;
  867. }
  868. static int ir_open(struct input_dev *idev)
  869. {
  870. struct rc_dev *rdev = input_get_drvdata(idev);
  871. return rc_open(rdev);
  872. }
  873. void rc_close(struct rc_dev *rdev)
  874. {
  875. if (rdev) {
  876. mutex_lock(&rdev->lock);
  877. if (!--rdev->users && rdev->close && rdev->registered)
  878. rdev->close(rdev);
  879. mutex_unlock(&rdev->lock);
  880. }
  881. }
  882. static void ir_close(struct input_dev *idev)
  883. {
  884. struct rc_dev *rdev = input_get_drvdata(idev);
  885. rc_close(rdev);
  886. }
  887. /* class for /sys/class/rc */
  888. static char *rc_devnode(struct device *dev, umode_t *mode)
  889. {
  890. return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
  891. }
  892. static struct class rc_class = {
  893. .name = "rc",
  894. .devnode = rc_devnode,
  895. };
  896. /*
  897. * These are the protocol textual descriptions that are
  898. * used by the sysfs protocols file. Note that the order
  899. * of the entries is relevant.
  900. */
  901. static const struct {
  902. u64 type;
  903. const char *name;
  904. const char *module_name;
  905. } proto_names[] = {
  906. { RC_PROTO_BIT_NONE, "none", NULL },
  907. { RC_PROTO_BIT_OTHER, "other", NULL },
  908. { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
  909. { RC_PROTO_BIT_RC5 |
  910. RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
  911. { RC_PROTO_BIT_NEC |
  912. RC_PROTO_BIT_NECX |
  913. RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
  914. { RC_PROTO_BIT_RC6_0 |
  915. RC_PROTO_BIT_RC6_6A_20 |
  916. RC_PROTO_BIT_RC6_6A_24 |
  917. RC_PROTO_BIT_RC6_6A_32 |
  918. RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
  919. { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
  920. { RC_PROTO_BIT_SONY12 |
  921. RC_PROTO_BIT_SONY15 |
  922. RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
  923. { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
  924. { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
  925. { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
  926. { RC_PROTO_BIT_MCIR2_KBD |
  927. RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
  928. { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
  929. { RC_PROTO_BIT_CEC, "cec", NULL },
  930. { RC_PROTO_BIT_IMON, "imon", "ir-imon-decoder" },
  931. { RC_PROTO_BIT_RCMM12 |
  932. RC_PROTO_BIT_RCMM24 |
  933. RC_PROTO_BIT_RCMM32, "rc-mm", "ir-rcmm-decoder" },
  934. { RC_PROTO_BIT_XBOX_DVD, "xbox-dvd", NULL },
  935. };
  936. /**
  937. * struct rc_filter_attribute - Device attribute relating to a filter type.
  938. * @attr: Device attribute.
  939. * @type: Filter type.
  940. * @mask: false for filter value, true for filter mask.
  941. */
  942. struct rc_filter_attribute {
  943. struct device_attribute attr;
  944. enum rc_filter_type type;
  945. bool mask;
  946. };
  947. #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
  948. #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
  949. struct rc_filter_attribute dev_attr_##_name = { \
  950. .attr = __ATTR(_name, _mode, _show, _store), \
  951. .type = (_type), \
  952. .mask = (_mask), \
  953. }
  954. /**
  955. * show_protocols() - shows the current IR protocol(s)
  956. * @device: the device descriptor
  957. * @mattr: the device attribute struct
  958. * @buf: a pointer to the output buffer
  959. *
  960. * This routine is a callback routine for input read the IR protocol type(s).
  961. * it is triggered by reading /sys/class/rc/rc?/protocols.
  962. * It returns the protocol names of supported protocols.
  963. * Enabled protocols are printed in brackets.
  964. *
  965. * dev->lock is taken to guard against races between
  966. * store_protocols and show_protocols.
  967. */
  968. static ssize_t show_protocols(struct device *device,
  969. struct device_attribute *mattr, char *buf)
  970. {
  971. struct rc_dev *dev = to_rc_dev(device);
  972. u64 allowed, enabled;
  973. char *tmp = buf;
  974. int i;
  975. mutex_lock(&dev->lock);
  976. enabled = dev->enabled_protocols;
  977. allowed = dev->allowed_protocols;
  978. if (dev->raw && !allowed)
  979. allowed = ir_raw_get_allowed_protocols();
  980. mutex_unlock(&dev->lock);
  981. dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
  982. __func__, (long long)allowed, (long long)enabled);
  983. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  984. if (allowed & enabled & proto_names[i].type)
  985. tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
  986. else if (allowed & proto_names[i].type)
  987. tmp += sprintf(tmp, "%s ", proto_names[i].name);
  988. if (allowed & proto_names[i].type)
  989. allowed &= ~proto_names[i].type;
  990. }
  991. #ifdef CONFIG_LIRC
  992. if (dev->driver_type == RC_DRIVER_IR_RAW)
  993. tmp += sprintf(tmp, "[lirc] ");
  994. #endif
  995. if (tmp != buf)
  996. tmp--;
  997. *tmp = '\n';
  998. return tmp + 1 - buf;
  999. }
  1000. /**
  1001. * parse_protocol_change() - parses a protocol change request
  1002. * @dev: rc_dev device
  1003. * @protocols: pointer to the bitmask of current protocols
  1004. * @buf: pointer to the buffer with a list of changes
  1005. *
  1006. * Writing "+proto" will add a protocol to the protocol mask.
  1007. * Writing "-proto" will remove a protocol from protocol mask.
  1008. * Writing "proto" will enable only "proto".
  1009. * Writing "none" will disable all protocols.
  1010. * Returns the number of changes performed or a negative error code.
  1011. */
  1012. static int parse_protocol_change(struct rc_dev *dev, u64 *protocols,
  1013. const char *buf)
  1014. {
  1015. const char *tmp;
  1016. unsigned count = 0;
  1017. bool enable, disable;
  1018. u64 mask;
  1019. int i;
  1020. while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
  1021. if (!*tmp)
  1022. break;
  1023. if (*tmp == '+') {
  1024. enable = true;
  1025. disable = false;
  1026. tmp++;
  1027. } else if (*tmp == '-') {
  1028. enable = false;
  1029. disable = true;
  1030. tmp++;
  1031. } else {
  1032. enable = false;
  1033. disable = false;
  1034. }
  1035. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  1036. if (!strcasecmp(tmp, proto_names[i].name)) {
  1037. mask = proto_names[i].type;
  1038. break;
  1039. }
  1040. }
  1041. if (i == ARRAY_SIZE(proto_names)) {
  1042. if (!strcasecmp(tmp, "lirc"))
  1043. mask = 0;
  1044. else {
  1045. dev_dbg(&dev->dev, "Unknown protocol: '%s'\n",
  1046. tmp);
  1047. return -EINVAL;
  1048. }
  1049. }
  1050. count++;
  1051. if (enable)
  1052. *protocols |= mask;
  1053. else if (disable)
  1054. *protocols &= ~mask;
  1055. else
  1056. *protocols = mask;
  1057. }
  1058. if (!count) {
  1059. dev_dbg(&dev->dev, "Protocol not specified\n");
  1060. return -EINVAL;
  1061. }
  1062. return count;
  1063. }
  1064. void ir_raw_load_modules(u64 *protocols)
  1065. {
  1066. u64 available;
  1067. int i, ret;
  1068. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  1069. if (proto_names[i].type == RC_PROTO_BIT_NONE ||
  1070. proto_names[i].type & (RC_PROTO_BIT_OTHER |
  1071. RC_PROTO_BIT_UNKNOWN))
  1072. continue;
  1073. available = ir_raw_get_allowed_protocols();
  1074. if (!(*protocols & proto_names[i].type & ~available))
  1075. continue;
  1076. if (!proto_names[i].module_name) {
  1077. pr_err("Can't enable IR protocol %s\n",
  1078. proto_names[i].name);
  1079. *protocols &= ~proto_names[i].type;
  1080. continue;
  1081. }
  1082. ret = request_module("%s", proto_names[i].module_name);
  1083. if (ret < 0) {
  1084. pr_err("Couldn't load IR protocol module %s\n",
  1085. proto_names[i].module_name);
  1086. *protocols &= ~proto_names[i].type;
  1087. continue;
  1088. }
  1089. msleep(20);
  1090. available = ir_raw_get_allowed_protocols();
  1091. if (!(*protocols & proto_names[i].type & ~available))
  1092. continue;
  1093. pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
  1094. proto_names[i].module_name,
  1095. proto_names[i].name);
  1096. *protocols &= ~proto_names[i].type;
  1097. }
  1098. }
  1099. /**
  1100. * store_protocols() - changes the current/wakeup IR protocol(s)
  1101. * @device: the device descriptor
  1102. * @mattr: the device attribute struct
  1103. * @buf: a pointer to the input buffer
  1104. * @len: length of the input buffer
  1105. *
  1106. * This routine is for changing the IR protocol type.
  1107. * It is triggered by writing to /sys/class/rc/rc?/[wakeup_]protocols.
  1108. * See parse_protocol_change() for the valid commands.
  1109. * Returns @len on success or a negative error code.
  1110. *
  1111. * dev->lock is taken to guard against races between
  1112. * store_protocols and show_protocols.
  1113. */
  1114. static ssize_t store_protocols(struct device *device,
  1115. struct device_attribute *mattr,
  1116. const char *buf, size_t len)
  1117. {
  1118. struct rc_dev *dev = to_rc_dev(device);
  1119. u64 *current_protocols;
  1120. struct rc_scancode_filter *filter;
  1121. u64 old_protocols, new_protocols;
  1122. ssize_t rc;
  1123. dev_dbg(&dev->dev, "Normal protocol change requested\n");
  1124. current_protocols = &dev->enabled_protocols;
  1125. filter = &dev->scancode_filter;
  1126. if (!dev->change_protocol) {
  1127. dev_dbg(&dev->dev, "Protocol switching not supported\n");
  1128. return -EINVAL;
  1129. }
  1130. mutex_lock(&dev->lock);
  1131. if (!dev->registered) {
  1132. mutex_unlock(&dev->lock);
  1133. return -ENODEV;
  1134. }
  1135. old_protocols = *current_protocols;
  1136. new_protocols = old_protocols;
  1137. rc = parse_protocol_change(dev, &new_protocols, buf);
  1138. if (rc < 0)
  1139. goto out;
  1140. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1141. ir_raw_load_modules(&new_protocols);
  1142. rc = dev->change_protocol(dev, &new_protocols);
  1143. if (rc < 0) {
  1144. dev_dbg(&dev->dev, "Error setting protocols to 0x%llx\n",
  1145. (long long)new_protocols);
  1146. goto out;
  1147. }
  1148. if (new_protocols != old_protocols) {
  1149. *current_protocols = new_protocols;
  1150. dev_dbg(&dev->dev, "Protocols changed to 0x%llx\n",
  1151. (long long)new_protocols);
  1152. }
  1153. /*
  1154. * If a protocol change was attempted the filter may need updating, even
  1155. * if the actual protocol mask hasn't changed (since the driver may have
  1156. * cleared the filter).
  1157. * Try setting the same filter with the new protocol (if any).
  1158. * Fall back to clearing the filter.
  1159. */
  1160. if (dev->s_filter && filter->mask) {
  1161. if (new_protocols)
  1162. rc = dev->s_filter(dev, filter);
  1163. else
  1164. rc = -1;
  1165. if (rc < 0) {
  1166. filter->data = 0;
  1167. filter->mask = 0;
  1168. dev->s_filter(dev, filter);
  1169. }
  1170. }
  1171. rc = len;
  1172. out:
  1173. mutex_unlock(&dev->lock);
  1174. return rc;
  1175. }
  1176. /**
  1177. * show_filter() - shows the current scancode filter value or mask
  1178. * @device: the device descriptor
  1179. * @attr: the device attribute struct
  1180. * @buf: a pointer to the output buffer
  1181. *
  1182. * This routine is a callback routine to read a scancode filter value or mask.
  1183. * It is triggered by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
  1184. * It prints the current scancode filter value or mask of the appropriate filter
  1185. * type in hexadecimal into @buf and returns the size of the buffer.
  1186. *
  1187. * Bits of the filter value corresponding to set bits in the filter mask are
  1188. * compared against input scancodes and non-matching scancodes are discarded.
  1189. *
  1190. * dev->lock is taken to guard against races between
  1191. * store_filter and show_filter.
  1192. */
  1193. static ssize_t show_filter(struct device *device,
  1194. struct device_attribute *attr,
  1195. char *buf)
  1196. {
  1197. struct rc_dev *dev = to_rc_dev(device);
  1198. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1199. struct rc_scancode_filter *filter;
  1200. u32 val;
  1201. mutex_lock(&dev->lock);
  1202. if (fattr->type == RC_FILTER_NORMAL)
  1203. filter = &dev->scancode_filter;
  1204. else
  1205. filter = &dev->scancode_wakeup_filter;
  1206. if (fattr->mask)
  1207. val = filter->mask;
  1208. else
  1209. val = filter->data;
  1210. mutex_unlock(&dev->lock);
  1211. return sprintf(buf, "%#x\n", val);
  1212. }
  1213. /**
  1214. * store_filter() - changes the scancode filter value
  1215. * @device: the device descriptor
  1216. * @attr: the device attribute struct
  1217. * @buf: a pointer to the input buffer
  1218. * @len: length of the input buffer
  1219. *
  1220. * This routine is for changing a scancode filter value or mask.
  1221. * It is triggered by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
  1222. * Returns -EINVAL if an invalid filter value for the current protocol was
  1223. * specified or if scancode filtering is not supported by the driver, otherwise
  1224. * returns @len.
  1225. *
  1226. * Bits of the filter value corresponding to set bits in the filter mask are
  1227. * compared against input scancodes and non-matching scancodes are discarded.
  1228. *
  1229. * dev->lock is taken to guard against races between
  1230. * store_filter and show_filter.
  1231. */
  1232. static ssize_t store_filter(struct device *device,
  1233. struct device_attribute *attr,
  1234. const char *buf, size_t len)
  1235. {
  1236. struct rc_dev *dev = to_rc_dev(device);
  1237. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1238. struct rc_scancode_filter new_filter, *filter;
  1239. int ret;
  1240. unsigned long val;
  1241. int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
  1242. ret = kstrtoul(buf, 0, &val);
  1243. if (ret < 0)
  1244. return ret;
  1245. if (fattr->type == RC_FILTER_NORMAL) {
  1246. set_filter = dev->s_filter;
  1247. filter = &dev->scancode_filter;
  1248. } else {
  1249. set_filter = dev->s_wakeup_filter;
  1250. filter = &dev->scancode_wakeup_filter;
  1251. }
  1252. if (!set_filter)
  1253. return -EINVAL;
  1254. mutex_lock(&dev->lock);
  1255. if (!dev->registered) {
  1256. mutex_unlock(&dev->lock);
  1257. return -ENODEV;
  1258. }
  1259. new_filter = *filter;
  1260. if (fattr->mask)
  1261. new_filter.mask = val;
  1262. else
  1263. new_filter.data = val;
  1264. if (fattr->type == RC_FILTER_WAKEUP) {
  1265. /*
  1266. * Refuse to set a filter unless a protocol is enabled
  1267. * and the filter is valid for that protocol
  1268. */
  1269. if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
  1270. ret = rc_validate_filter(dev, &new_filter);
  1271. else
  1272. ret = -EINVAL;
  1273. if (ret != 0)
  1274. goto unlock;
  1275. }
  1276. if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
  1277. val) {
  1278. /* refuse to set a filter unless a protocol is enabled */
  1279. ret = -EINVAL;
  1280. goto unlock;
  1281. }
  1282. ret = set_filter(dev, &new_filter);
  1283. if (ret < 0)
  1284. goto unlock;
  1285. *filter = new_filter;
  1286. unlock:
  1287. mutex_unlock(&dev->lock);
  1288. return (ret < 0) ? ret : len;
  1289. }
  1290. /**
  1291. * show_wakeup_protocols() - shows the wakeup IR protocol
  1292. * @device: the device descriptor
  1293. * @mattr: the device attribute struct
  1294. * @buf: a pointer to the output buffer
  1295. *
  1296. * This routine is a callback routine for input read the IR protocol type(s).
  1297. * it is triggered by reading /sys/class/rc/rc?/wakeup_protocols.
  1298. * It returns the protocol names of supported protocols.
  1299. * The enabled protocols are printed in brackets.
  1300. *
  1301. * dev->lock is taken to guard against races between
  1302. * store_wakeup_protocols and show_wakeup_protocols.
  1303. */
  1304. static ssize_t show_wakeup_protocols(struct device *device,
  1305. struct device_attribute *mattr,
  1306. char *buf)
  1307. {
  1308. struct rc_dev *dev = to_rc_dev(device);
  1309. u64 allowed;
  1310. enum rc_proto enabled;
  1311. char *tmp = buf;
  1312. int i;
  1313. mutex_lock(&dev->lock);
  1314. allowed = dev->allowed_wakeup_protocols;
  1315. enabled = dev->wakeup_protocol;
  1316. mutex_unlock(&dev->lock);
  1317. dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - %d\n",
  1318. __func__, (long long)allowed, enabled);
  1319. for (i = 0; i < ARRAY_SIZE(protocols); i++) {
  1320. if (allowed & (1ULL << i)) {
  1321. if (i == enabled)
  1322. tmp += sprintf(tmp, "[%s] ", protocols[i].name);
  1323. else
  1324. tmp += sprintf(tmp, "%s ", protocols[i].name);
  1325. }
  1326. }
  1327. if (tmp != buf)
  1328. tmp--;
  1329. *tmp = '\n';
  1330. return tmp + 1 - buf;
  1331. }
  1332. /**
  1333. * store_wakeup_protocols() - changes the wakeup IR protocol(s)
  1334. * @device: the device descriptor
  1335. * @mattr: the device attribute struct
  1336. * @buf: a pointer to the input buffer
  1337. * @len: length of the input buffer
  1338. *
  1339. * This routine is for changing the IR protocol type.
  1340. * It is triggered by writing to /sys/class/rc/rc?/wakeup_protocols.
  1341. * Returns @len on success or a negative error code.
  1342. *
  1343. * dev->lock is taken to guard against races between
  1344. * store_wakeup_protocols and show_wakeup_protocols.
  1345. */
  1346. static ssize_t store_wakeup_protocols(struct device *device,
  1347. struct device_attribute *mattr,
  1348. const char *buf, size_t len)
  1349. {
  1350. struct rc_dev *dev = to_rc_dev(device);
  1351. enum rc_proto protocol = RC_PROTO_UNKNOWN;
  1352. ssize_t rc;
  1353. u64 allowed;
  1354. int i;
  1355. mutex_lock(&dev->lock);
  1356. if (!dev->registered) {
  1357. mutex_unlock(&dev->lock);
  1358. return -ENODEV;
  1359. }
  1360. allowed = dev->allowed_wakeup_protocols;
  1361. if (!sysfs_streq(buf, "none")) {
  1362. for (i = 0; i < ARRAY_SIZE(protocols); i++) {
  1363. if ((allowed & (1ULL << i)) &&
  1364. sysfs_streq(buf, protocols[i].name)) {
  1365. protocol = i;
  1366. break;
  1367. }
  1368. }
  1369. if (i == ARRAY_SIZE(protocols)) {
  1370. rc = -EINVAL;
  1371. goto out;
  1372. }
  1373. if (dev->encode_wakeup) {
  1374. u64 mask = 1ULL << protocol;
  1375. ir_raw_load_modules(&mask);
  1376. if (!mask) {
  1377. rc = -EINVAL;
  1378. goto out;
  1379. }
  1380. }
  1381. }
  1382. if (dev->wakeup_protocol != protocol) {
  1383. dev->wakeup_protocol = protocol;
  1384. dev_dbg(&dev->dev, "Wakeup protocol changed to %d\n", protocol);
  1385. if (protocol == RC_PROTO_RC6_MCE)
  1386. dev->scancode_wakeup_filter.data = 0x800f0000;
  1387. else
  1388. dev->scancode_wakeup_filter.data = 0;
  1389. dev->scancode_wakeup_filter.mask = 0;
  1390. rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
  1391. if (rc == 0)
  1392. rc = len;
  1393. } else {
  1394. rc = len;
  1395. }
  1396. out:
  1397. mutex_unlock(&dev->lock);
  1398. return rc;
  1399. }
  1400. static void rc_dev_release(struct device *device)
  1401. {
  1402. struct rc_dev *dev = to_rc_dev(device);
  1403. kfree(dev);
  1404. }
  1405. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  1406. {
  1407. struct rc_dev *dev = to_rc_dev(device);
  1408. int ret = 0;
  1409. mutex_lock(&dev->lock);
  1410. if (!dev->registered)
  1411. ret = -ENODEV;
  1412. if (ret == 0 && dev->rc_map.name)
  1413. ret = add_uevent_var(env, "NAME=%s", dev->rc_map.name);
  1414. if (ret == 0 && dev->driver_name)
  1415. ret = add_uevent_var(env, "DRV_NAME=%s", dev->driver_name);
  1416. if (ret == 0 && dev->device_name)
  1417. ret = add_uevent_var(env, "DEV_NAME=%s", dev->device_name);
  1418. mutex_unlock(&dev->lock);
  1419. return ret;
  1420. }
  1421. /*
  1422. * Static device attribute struct with the sysfs attributes for IR's
  1423. */
  1424. static struct device_attribute dev_attr_ro_protocols =
  1425. __ATTR(protocols, 0444, show_protocols, NULL);
  1426. static struct device_attribute dev_attr_rw_protocols =
  1427. __ATTR(protocols, 0644, show_protocols, store_protocols);
  1428. static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
  1429. store_wakeup_protocols);
  1430. static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
  1431. show_filter, store_filter, RC_FILTER_NORMAL, false);
  1432. static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
  1433. show_filter, store_filter, RC_FILTER_NORMAL, true);
  1434. static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
  1435. show_filter, store_filter, RC_FILTER_WAKEUP, false);
  1436. static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
  1437. show_filter, store_filter, RC_FILTER_WAKEUP, true);
  1438. static struct attribute *rc_dev_rw_protocol_attrs[] = {
  1439. &dev_attr_rw_protocols.attr,
  1440. NULL,
  1441. };
  1442. static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
  1443. .attrs = rc_dev_rw_protocol_attrs,
  1444. };
  1445. static struct attribute *rc_dev_ro_protocol_attrs[] = {
  1446. &dev_attr_ro_protocols.attr,
  1447. NULL,
  1448. };
  1449. static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
  1450. .attrs = rc_dev_ro_protocol_attrs,
  1451. };
  1452. static struct attribute *rc_dev_filter_attrs[] = {
  1453. &dev_attr_filter.attr.attr,
  1454. &dev_attr_filter_mask.attr.attr,
  1455. NULL,
  1456. };
  1457. static const struct attribute_group rc_dev_filter_attr_grp = {
  1458. .attrs = rc_dev_filter_attrs,
  1459. };
  1460. static struct attribute *rc_dev_wakeup_filter_attrs[] = {
  1461. &dev_attr_wakeup_filter.attr.attr,
  1462. &dev_attr_wakeup_filter_mask.attr.attr,
  1463. &dev_attr_wakeup_protocols.attr,
  1464. NULL,
  1465. };
  1466. static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
  1467. .attrs = rc_dev_wakeup_filter_attrs,
  1468. };
  1469. static const struct device_type rc_dev_type = {
  1470. .release = rc_dev_release,
  1471. .uevent = rc_dev_uevent,
  1472. };
  1473. struct rc_dev *rc_allocate_device(enum rc_driver_type type)
  1474. {
  1475. struct rc_dev *dev;
  1476. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1477. if (!dev)
  1478. return NULL;
  1479. if (type != RC_DRIVER_IR_RAW_TX) {
  1480. dev->input_dev = input_allocate_device();
  1481. if (!dev->input_dev) {
  1482. kfree(dev);
  1483. return NULL;
  1484. }
  1485. dev->input_dev->getkeycode = ir_getkeycode;
  1486. dev->input_dev->setkeycode = ir_setkeycode;
  1487. input_set_drvdata(dev->input_dev, dev);
  1488. dev->timeout = IR_DEFAULT_TIMEOUT;
  1489. timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
  1490. timer_setup(&dev->timer_repeat, ir_timer_repeat, 0);
  1491. spin_lock_init(&dev->rc_map.lock);
  1492. spin_lock_init(&dev->keylock);
  1493. }
  1494. mutex_init(&dev->lock);
  1495. dev->dev.type = &rc_dev_type;
  1496. dev->dev.class = &rc_class;
  1497. device_initialize(&dev->dev);
  1498. dev->driver_type = type;
  1499. __module_get(THIS_MODULE);
  1500. return dev;
  1501. }
  1502. EXPORT_SYMBOL_GPL(rc_allocate_device);
  1503. void rc_free_device(struct rc_dev *dev)
  1504. {
  1505. if (!dev)
  1506. return;
  1507. input_free_device(dev->input_dev);
  1508. put_device(&dev->dev);
  1509. /* kfree(dev) will be called by the callback function
  1510. rc_dev_release() */
  1511. module_put(THIS_MODULE);
  1512. }
  1513. EXPORT_SYMBOL_GPL(rc_free_device);
  1514. static void devm_rc_alloc_release(struct device *dev, void *res)
  1515. {
  1516. rc_free_device(*(struct rc_dev **)res);
  1517. }
  1518. struct rc_dev *devm_rc_allocate_device(struct device *dev,
  1519. enum rc_driver_type type)
  1520. {
  1521. struct rc_dev **dr, *rc;
  1522. dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
  1523. if (!dr)
  1524. return NULL;
  1525. rc = rc_allocate_device(type);
  1526. if (!rc) {
  1527. devres_free(dr);
  1528. return NULL;
  1529. }
  1530. rc->dev.parent = dev;
  1531. rc->managed_alloc = true;
  1532. *dr = rc;
  1533. devres_add(dev, dr);
  1534. return rc;
  1535. }
  1536. EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
  1537. static int rc_prepare_rx_device(struct rc_dev *dev)
  1538. {
  1539. int rc;
  1540. struct rc_map *rc_map;
  1541. u64 rc_proto;
  1542. if (!dev->map_name)
  1543. return -EINVAL;
  1544. rc_map = rc_map_get(dev->map_name);
  1545. if (!rc_map)
  1546. rc_map = rc_map_get(RC_MAP_EMPTY);
  1547. if (!rc_map || !rc_map->scan || rc_map->size == 0)
  1548. return -EINVAL;
  1549. rc = ir_setkeytable(dev, rc_map);
  1550. if (rc)
  1551. return rc;
  1552. rc_proto = BIT_ULL(rc_map->rc_proto);
  1553. if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
  1554. dev->enabled_protocols = dev->allowed_protocols;
  1555. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1556. ir_raw_load_modules(&rc_proto);
  1557. if (dev->change_protocol) {
  1558. rc = dev->change_protocol(dev, &rc_proto);
  1559. if (rc < 0)
  1560. goto out_table;
  1561. dev->enabled_protocols = rc_proto;
  1562. }
  1563. /* Keyboard events */
  1564. set_bit(EV_KEY, dev->input_dev->evbit);
  1565. set_bit(EV_REP, dev->input_dev->evbit);
  1566. set_bit(EV_MSC, dev->input_dev->evbit);
  1567. set_bit(MSC_SCAN, dev->input_dev->mscbit);
  1568. /* Pointer/mouse events */
  1569. set_bit(INPUT_PROP_POINTING_STICK, dev->input_dev->propbit);
  1570. set_bit(EV_REL, dev->input_dev->evbit);
  1571. set_bit(REL_X, dev->input_dev->relbit);
  1572. set_bit(REL_Y, dev->input_dev->relbit);
  1573. if (dev->open)
  1574. dev->input_dev->open = ir_open;
  1575. if (dev->close)
  1576. dev->input_dev->close = ir_close;
  1577. dev->input_dev->dev.parent = &dev->dev;
  1578. memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
  1579. dev->input_dev->phys = dev->input_phys;
  1580. dev->input_dev->name = dev->device_name;
  1581. return 0;
  1582. out_table:
  1583. ir_free_table(&dev->rc_map);
  1584. return rc;
  1585. }
  1586. static int rc_setup_rx_device(struct rc_dev *dev)
  1587. {
  1588. int rc;
  1589. /* rc_open will be called here */
  1590. rc = input_register_device(dev->input_dev);
  1591. if (rc)
  1592. return rc;
  1593. /*
  1594. * Default delay of 250ms is too short for some protocols, especially
  1595. * since the timeout is currently set to 250ms. Increase it to 500ms,
  1596. * to avoid wrong repetition of the keycodes. Note that this must be
  1597. * set after the call to input_register_device().
  1598. */
  1599. if (dev->allowed_protocols == RC_PROTO_BIT_CEC)
  1600. dev->input_dev->rep[REP_DELAY] = 0;
  1601. else
  1602. dev->input_dev->rep[REP_DELAY] = 500;
  1603. /*
  1604. * As a repeat event on protocols like RC-5 and NEC take as long as
  1605. * 110/114ms, using 33ms as a repeat period is not the right thing
  1606. * to do.
  1607. */
  1608. dev->input_dev->rep[REP_PERIOD] = 125;
  1609. return 0;
  1610. }
  1611. static void rc_free_rx_device(struct rc_dev *dev)
  1612. {
  1613. if (!dev)
  1614. return;
  1615. if (dev->input_dev) {
  1616. input_unregister_device(dev->input_dev);
  1617. dev->input_dev = NULL;
  1618. }
  1619. ir_free_table(&dev->rc_map);
  1620. }
  1621. int rc_register_device(struct rc_dev *dev)
  1622. {
  1623. const char *path;
  1624. int attr = 0;
  1625. int minor;
  1626. int rc;
  1627. if (!dev)
  1628. return -EINVAL;
  1629. minor = ida_alloc_max(&rc_ida, RC_DEV_MAX - 1, GFP_KERNEL);
  1630. if (minor < 0)
  1631. return minor;
  1632. dev->minor = minor;
  1633. dev_set_name(&dev->dev, "rc%u", dev->minor);
  1634. dev_set_drvdata(&dev->dev, dev);
  1635. dev->dev.groups = dev->sysfs_groups;
  1636. if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
  1637. dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
  1638. else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
  1639. dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
  1640. if (dev->s_filter)
  1641. dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
  1642. if (dev->s_wakeup_filter)
  1643. dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
  1644. dev->sysfs_groups[attr++] = NULL;
  1645. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  1646. rc = ir_raw_event_prepare(dev);
  1647. if (rc < 0)
  1648. goto out_minor;
  1649. }
  1650. if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
  1651. rc = rc_prepare_rx_device(dev);
  1652. if (rc)
  1653. goto out_raw;
  1654. }
  1655. dev->registered = true;
  1656. rc = device_add(&dev->dev);
  1657. if (rc)
  1658. goto out_rx_free;
  1659. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1660. dev_info(&dev->dev, "%s as %s\n",
  1661. dev->device_name ?: "Unspecified device", path ?: "N/A");
  1662. kfree(path);
  1663. /*
  1664. * once the input device is registered in rc_setup_rx_device,
  1665. * userspace can open the input device and rc_open() will be called
  1666. * as a result. This results in driver code being allowed to submit
  1667. * keycodes with rc_keydown, so lirc must be registered first.
  1668. */
  1669. if (dev->allowed_protocols != RC_PROTO_BIT_CEC) {
  1670. rc = lirc_register(dev);
  1671. if (rc < 0)
  1672. goto out_dev;
  1673. }
  1674. if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
  1675. rc = rc_setup_rx_device(dev);
  1676. if (rc)
  1677. goto out_lirc;
  1678. }
  1679. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  1680. rc = ir_raw_event_register(dev);
  1681. if (rc < 0)
  1682. goto out_rx;
  1683. }
  1684. dev_dbg(&dev->dev, "Registered rc%u (driver: %s)\n", dev->minor,
  1685. dev->driver_name ? dev->driver_name : "unknown");
  1686. return 0;
  1687. out_rx:
  1688. rc_free_rx_device(dev);
  1689. out_lirc:
  1690. if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
  1691. lirc_unregister(dev);
  1692. out_dev:
  1693. device_del(&dev->dev);
  1694. out_rx_free:
  1695. ir_free_table(&dev->rc_map);
  1696. out_raw:
  1697. ir_raw_event_free(dev);
  1698. out_minor:
  1699. ida_free(&rc_ida, minor);
  1700. return rc;
  1701. }
  1702. EXPORT_SYMBOL_GPL(rc_register_device);
  1703. static void devm_rc_release(struct device *dev, void *res)
  1704. {
  1705. rc_unregister_device(*(struct rc_dev **)res);
  1706. }
  1707. int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
  1708. {
  1709. struct rc_dev **dr;
  1710. int ret;
  1711. dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
  1712. if (!dr)
  1713. return -ENOMEM;
  1714. ret = rc_register_device(dev);
  1715. if (ret) {
  1716. devres_free(dr);
  1717. return ret;
  1718. }
  1719. *dr = dev;
  1720. devres_add(parent, dr);
  1721. return 0;
  1722. }
  1723. EXPORT_SYMBOL_GPL(devm_rc_register_device);
  1724. void rc_unregister_device(struct rc_dev *dev)
  1725. {
  1726. if (!dev)
  1727. return;
  1728. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1729. ir_raw_event_unregister(dev);
  1730. del_timer_sync(&dev->timer_keyup);
  1731. del_timer_sync(&dev->timer_repeat);
  1732. mutex_lock(&dev->lock);
  1733. if (dev->users && dev->close)
  1734. dev->close(dev);
  1735. dev->registered = false;
  1736. mutex_unlock(&dev->lock);
  1737. rc_free_rx_device(dev);
  1738. /*
  1739. * lirc device should be freed with dev->registered = false, so
  1740. * that userspace polling will get notified.
  1741. */
  1742. if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
  1743. lirc_unregister(dev);
  1744. device_del(&dev->dev);
  1745. ida_free(&rc_ida, dev->minor);
  1746. if (!dev->managed_alloc)
  1747. rc_free_device(dev);
  1748. }
  1749. EXPORT_SYMBOL_GPL(rc_unregister_device);
  1750. /*
  1751. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  1752. */
  1753. static int __init rc_core_init(void)
  1754. {
  1755. int rc = class_register(&rc_class);
  1756. if (rc) {
  1757. pr_err("rc_core: unable to register rc class\n");
  1758. return rc;
  1759. }
  1760. rc = lirc_dev_init();
  1761. if (rc) {
  1762. pr_err("rc_core: unable to init lirc\n");
  1763. class_unregister(&rc_class);
  1764. return rc;
  1765. }
  1766. led_trigger_register_simple("rc-feedback", &led_feedback);
  1767. rc_map_register(&empty_map);
  1768. #ifdef CONFIG_MEDIA_CEC_RC
  1769. rc_map_register(&cec_map);
  1770. #endif
  1771. return 0;
  1772. }
  1773. static void __exit rc_core_exit(void)
  1774. {
  1775. lirc_dev_exit();
  1776. class_unregister(&rc_class);
  1777. led_trigger_unregister_simple(led_feedback);
  1778. #ifdef CONFIG_MEDIA_CEC_RC
  1779. rc_map_unregister(&cec_map);
  1780. #endif
  1781. rc_map_unregister(&empty_map);
  1782. }
  1783. subsys_initcall(rc_core_init);
  1784. module_exit(rc_core_exit);
  1785. MODULE_AUTHOR("Mauro Carvalho Chehab");
  1786. MODULE_LICENSE("GPL v2");