rc-main.c 52 KB

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