rc-main.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  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. ir_lirc_scancode_event(dev, &sc);
  626. spin_lock_irqsave(&dev->keylock, flags);
  627. input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
  628. input_sync(dev->input_dev);
  629. if (dev->keypressed) {
  630. dev->keyup_jiffies = jiffies + timeout;
  631. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  632. }
  633. spin_unlock_irqrestore(&dev->keylock, flags);
  634. }
  635. EXPORT_SYMBOL_GPL(rc_repeat);
  636. /**
  637. * ir_do_keydown() - internal function to process a keypress
  638. * @dev: the struct rc_dev descriptor of the device
  639. * @protocol: the protocol of the keypress
  640. * @scancode: the scancode of the keypress
  641. * @keycode: the keycode of the keypress
  642. * @toggle: the toggle value of the keypress
  643. *
  644. * This function is used internally to register a keypress, it must be
  645. * called with keylock held.
  646. */
  647. static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
  648. u32 scancode, u32 keycode, u8 toggle)
  649. {
  650. bool new_event = (!dev->keypressed ||
  651. dev->last_protocol != protocol ||
  652. dev->last_scancode != scancode ||
  653. dev->last_toggle != toggle);
  654. struct lirc_scancode sc = {
  655. .scancode = scancode, .rc_proto = protocol,
  656. .flags = toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0,
  657. .keycode = keycode
  658. };
  659. ir_lirc_scancode_event(dev, &sc);
  660. if (new_event && dev->keypressed)
  661. ir_do_keyup(dev, false);
  662. input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
  663. dev->last_protocol = protocol;
  664. dev->last_scancode = scancode;
  665. dev->last_toggle = toggle;
  666. dev->last_keycode = keycode;
  667. if (new_event && keycode != KEY_RESERVED) {
  668. /* Register a keypress */
  669. dev->keypressed = true;
  670. dev_dbg(&dev->dev, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
  671. dev->device_name, keycode, protocol, scancode);
  672. input_report_key(dev->input_dev, keycode, 1);
  673. led_trigger_event(led_feedback, LED_FULL);
  674. }
  675. /*
  676. * For CEC, start sending repeat messages as soon as the first
  677. * repeated message is sent, as long as REP_DELAY = 0 and REP_PERIOD
  678. * is non-zero. Otherwise, the input layer will generate repeat
  679. * messages.
  680. */
  681. if (!new_event && keycode != KEY_RESERVED &&
  682. dev->allowed_protocols == RC_PROTO_BIT_CEC &&
  683. !timer_pending(&dev->timer_repeat) &&
  684. dev->input_dev->rep[REP_PERIOD] &&
  685. !dev->input_dev->rep[REP_DELAY]) {
  686. input_event(dev->input_dev, EV_KEY, keycode, 2);
  687. mod_timer(&dev->timer_repeat, jiffies +
  688. msecs_to_jiffies(dev->input_dev->rep[REP_PERIOD]));
  689. }
  690. input_sync(dev->input_dev);
  691. }
  692. /**
  693. * rc_keydown() - generates input event for a key press
  694. * @dev: the struct rc_dev descriptor of the device
  695. * @protocol: the protocol for the keypress
  696. * @scancode: the scancode for the keypress
  697. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  698. * support toggle values, this should be set to zero)
  699. *
  700. * This routine is used to signal that a key has been pressed on the
  701. * remote control.
  702. */
  703. void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
  704. u8 toggle)
  705. {
  706. unsigned long flags;
  707. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  708. spin_lock_irqsave(&dev->keylock, flags);
  709. ir_do_keydown(dev, protocol, scancode, keycode, toggle);
  710. if (dev->keypressed) {
  711. dev->keyup_jiffies = jiffies + nsecs_to_jiffies(dev->timeout) +
  712. msecs_to_jiffies(repeat_period(protocol));
  713. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  714. }
  715. spin_unlock_irqrestore(&dev->keylock, flags);
  716. }
  717. EXPORT_SYMBOL_GPL(rc_keydown);
  718. /**
  719. * rc_keydown_notimeout() - generates input event for a key press without
  720. * an automatic keyup event at a later time
  721. * @dev: the struct rc_dev descriptor of the device
  722. * @protocol: the protocol for the keypress
  723. * @scancode: the scancode for the keypress
  724. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  725. * support toggle values, this should be set to zero)
  726. *
  727. * This routine is used to signal that a key has been pressed on the
  728. * remote control. The driver must manually call rc_keyup() at a later stage.
  729. */
  730. void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
  731. u32 scancode, u8 toggle)
  732. {
  733. unsigned long flags;
  734. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  735. spin_lock_irqsave(&dev->keylock, flags);
  736. ir_do_keydown(dev, protocol, scancode, keycode, toggle);
  737. spin_unlock_irqrestore(&dev->keylock, flags);
  738. }
  739. EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
  740. /**
  741. * rc_validate_scancode() - checks that a scancode is valid for a protocol.
  742. * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
  743. * @proto: protocol
  744. * @scancode: scancode
  745. */
  746. bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
  747. {
  748. switch (proto) {
  749. /*
  750. * NECX has a 16-bit address; if the lower 8 bits match the upper
  751. * 8 bits inverted, then the address would match regular nec.
  752. */
  753. case RC_PROTO_NECX:
  754. if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
  755. return false;
  756. break;
  757. /*
  758. * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
  759. * of the command match the upper 8 bits inverted, then it would
  760. * be either NEC or NECX.
  761. */
  762. case RC_PROTO_NEC32:
  763. if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0)
  764. return false;
  765. break;
  766. /*
  767. * If the customer code (top 32-bit) is 0x800f, it is MCE else it
  768. * is regular mode-6a 32 bit
  769. */
  770. case RC_PROTO_RC6_MCE:
  771. if ((scancode & 0xffff0000) != 0x800f0000)
  772. return false;
  773. break;
  774. case RC_PROTO_RC6_6A_32:
  775. if ((scancode & 0xffff0000) == 0x800f0000)
  776. return false;
  777. break;
  778. default:
  779. break;
  780. }
  781. return true;
  782. }
  783. /**
  784. * rc_validate_filter() - checks that the scancode and mask are valid and
  785. * provides sensible defaults
  786. * @dev: the struct rc_dev descriptor of the device
  787. * @filter: the scancode and mask
  788. *
  789. * return: 0 or -EINVAL if the filter is not valid
  790. */
  791. static int rc_validate_filter(struct rc_dev *dev,
  792. struct rc_scancode_filter *filter)
  793. {
  794. u32 mask, s = filter->data;
  795. enum rc_proto protocol = dev->wakeup_protocol;
  796. if (protocol >= ARRAY_SIZE(protocols))
  797. return -EINVAL;
  798. mask = protocols[protocol].scancode_bits;
  799. if (!rc_validate_scancode(protocol, s))
  800. return -EINVAL;
  801. filter->data &= mask;
  802. filter->mask &= mask;
  803. /*
  804. * If we have to raw encode the IR for wakeup, we cannot have a mask
  805. */
  806. if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
  807. return -EINVAL;
  808. return 0;
  809. }
  810. int rc_open(struct rc_dev *rdev)
  811. {
  812. int rval = 0;
  813. if (!rdev)
  814. return -EINVAL;
  815. mutex_lock(&rdev->lock);
  816. if (!rdev->registered) {
  817. rval = -ENODEV;
  818. } else {
  819. if (!rdev->users++ && rdev->open)
  820. rval = rdev->open(rdev);
  821. if (rval)
  822. rdev->users--;
  823. }
  824. mutex_unlock(&rdev->lock);
  825. return rval;
  826. }
  827. static int ir_open(struct input_dev *idev)
  828. {
  829. struct rc_dev *rdev = input_get_drvdata(idev);
  830. return rc_open(rdev);
  831. }
  832. void rc_close(struct rc_dev *rdev)
  833. {
  834. if (rdev) {
  835. mutex_lock(&rdev->lock);
  836. if (!--rdev->users && rdev->close && rdev->registered)
  837. rdev->close(rdev);
  838. mutex_unlock(&rdev->lock);
  839. }
  840. }
  841. static void ir_close(struct input_dev *idev)
  842. {
  843. struct rc_dev *rdev = input_get_drvdata(idev);
  844. rc_close(rdev);
  845. }
  846. /* class for /sys/class/rc */
  847. static char *rc_devnode(struct device *dev, umode_t *mode)
  848. {
  849. return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
  850. }
  851. static struct class rc_class = {
  852. .name = "rc",
  853. .devnode = rc_devnode,
  854. };
  855. /*
  856. * These are the protocol textual descriptions that are
  857. * used by the sysfs protocols file. Note that the order
  858. * of the entries is relevant.
  859. */
  860. static const struct {
  861. u64 type;
  862. const char *name;
  863. const char *module_name;
  864. } proto_names[] = {
  865. { RC_PROTO_BIT_NONE, "none", NULL },
  866. { RC_PROTO_BIT_OTHER, "other", NULL },
  867. { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
  868. { RC_PROTO_BIT_RC5 |
  869. RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
  870. { RC_PROTO_BIT_NEC |
  871. RC_PROTO_BIT_NECX |
  872. RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
  873. { RC_PROTO_BIT_RC6_0 |
  874. RC_PROTO_BIT_RC6_6A_20 |
  875. RC_PROTO_BIT_RC6_6A_24 |
  876. RC_PROTO_BIT_RC6_6A_32 |
  877. RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
  878. { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
  879. { RC_PROTO_BIT_SONY12 |
  880. RC_PROTO_BIT_SONY15 |
  881. RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
  882. { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
  883. { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
  884. { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
  885. { RC_PROTO_BIT_MCIR2_KBD |
  886. RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
  887. { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
  888. { RC_PROTO_BIT_CEC, "cec", NULL },
  889. { RC_PROTO_BIT_IMON, "imon", "ir-imon-decoder" },
  890. };
  891. /**
  892. * struct rc_filter_attribute - Device attribute relating to a filter type.
  893. * @attr: Device attribute.
  894. * @type: Filter type.
  895. * @mask: false for filter value, true for filter mask.
  896. */
  897. struct rc_filter_attribute {
  898. struct device_attribute attr;
  899. enum rc_filter_type type;
  900. bool mask;
  901. };
  902. #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
  903. #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
  904. struct rc_filter_attribute dev_attr_##_name = { \
  905. .attr = __ATTR(_name, _mode, _show, _store), \
  906. .type = (_type), \
  907. .mask = (_mask), \
  908. }
  909. /**
  910. * show_protocols() - shows the current IR protocol(s)
  911. * @device: the device descriptor
  912. * @mattr: the device attribute struct
  913. * @buf: a pointer to the output buffer
  914. *
  915. * This routine is a callback routine for input read the IR protocol type(s).
  916. * it is trigged by reading /sys/class/rc/rc?/protocols.
  917. * It returns the protocol names of supported protocols.
  918. * Enabled protocols are printed in brackets.
  919. *
  920. * dev->lock is taken to guard against races between
  921. * store_protocols and show_protocols.
  922. */
  923. static ssize_t show_protocols(struct device *device,
  924. struct device_attribute *mattr, char *buf)
  925. {
  926. struct rc_dev *dev = to_rc_dev(device);
  927. u64 allowed, enabled;
  928. char *tmp = buf;
  929. int i;
  930. mutex_lock(&dev->lock);
  931. enabled = dev->enabled_protocols;
  932. allowed = dev->allowed_protocols;
  933. if (dev->raw && !allowed)
  934. allowed = ir_raw_get_allowed_protocols();
  935. mutex_unlock(&dev->lock);
  936. dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
  937. __func__, (long long)allowed, (long long)enabled);
  938. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  939. if (allowed & enabled & proto_names[i].type)
  940. tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
  941. else if (allowed & proto_names[i].type)
  942. tmp += sprintf(tmp, "%s ", proto_names[i].name);
  943. if (allowed & proto_names[i].type)
  944. allowed &= ~proto_names[i].type;
  945. }
  946. #ifdef CONFIG_LIRC
  947. if (dev->driver_type == RC_DRIVER_IR_RAW)
  948. tmp += sprintf(tmp, "[lirc] ");
  949. #endif
  950. if (tmp != buf)
  951. tmp--;
  952. *tmp = '\n';
  953. return tmp + 1 - buf;
  954. }
  955. /**
  956. * parse_protocol_change() - parses a protocol change request
  957. * @dev: rc_dev device
  958. * @protocols: pointer to the bitmask of current protocols
  959. * @buf: pointer to the buffer with a list of changes
  960. *
  961. * Writing "+proto" will add a protocol to the protocol mask.
  962. * Writing "-proto" will remove a protocol from protocol mask.
  963. * Writing "proto" will enable only "proto".
  964. * Writing "none" will disable all protocols.
  965. * Returns the number of changes performed or a negative error code.
  966. */
  967. static int parse_protocol_change(struct rc_dev *dev, u64 *protocols,
  968. const char *buf)
  969. {
  970. const char *tmp;
  971. unsigned count = 0;
  972. bool enable, disable;
  973. u64 mask;
  974. int i;
  975. while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
  976. if (!*tmp)
  977. break;
  978. if (*tmp == '+') {
  979. enable = true;
  980. disable = false;
  981. tmp++;
  982. } else if (*tmp == '-') {
  983. enable = false;
  984. disable = true;
  985. tmp++;
  986. } else {
  987. enable = false;
  988. disable = false;
  989. }
  990. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  991. if (!strcasecmp(tmp, proto_names[i].name)) {
  992. mask = proto_names[i].type;
  993. break;
  994. }
  995. }
  996. if (i == ARRAY_SIZE(proto_names)) {
  997. if (!strcasecmp(tmp, "lirc"))
  998. mask = 0;
  999. else {
  1000. dev_dbg(&dev->dev, "Unknown protocol: '%s'\n",
  1001. tmp);
  1002. return -EINVAL;
  1003. }
  1004. }
  1005. count++;
  1006. if (enable)
  1007. *protocols |= mask;
  1008. else if (disable)
  1009. *protocols &= ~mask;
  1010. else
  1011. *protocols = mask;
  1012. }
  1013. if (!count) {
  1014. dev_dbg(&dev->dev, "Protocol not specified\n");
  1015. return -EINVAL;
  1016. }
  1017. return count;
  1018. }
  1019. void ir_raw_load_modules(u64 *protocols)
  1020. {
  1021. u64 available;
  1022. int i, ret;
  1023. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  1024. if (proto_names[i].type == RC_PROTO_BIT_NONE ||
  1025. proto_names[i].type & (RC_PROTO_BIT_OTHER |
  1026. RC_PROTO_BIT_UNKNOWN))
  1027. continue;
  1028. available = ir_raw_get_allowed_protocols();
  1029. if (!(*protocols & proto_names[i].type & ~available))
  1030. continue;
  1031. if (!proto_names[i].module_name) {
  1032. pr_err("Can't enable IR protocol %s\n",
  1033. proto_names[i].name);
  1034. *protocols &= ~proto_names[i].type;
  1035. continue;
  1036. }
  1037. ret = request_module("%s", proto_names[i].module_name);
  1038. if (ret < 0) {
  1039. pr_err("Couldn't load IR protocol module %s\n",
  1040. proto_names[i].module_name);
  1041. *protocols &= ~proto_names[i].type;
  1042. continue;
  1043. }
  1044. msleep(20);
  1045. available = ir_raw_get_allowed_protocols();
  1046. if (!(*protocols & proto_names[i].type & ~available))
  1047. continue;
  1048. pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
  1049. proto_names[i].module_name,
  1050. proto_names[i].name);
  1051. *protocols &= ~proto_names[i].type;
  1052. }
  1053. }
  1054. /**
  1055. * store_protocols() - changes the current/wakeup IR protocol(s)
  1056. * @device: the device descriptor
  1057. * @mattr: the device attribute struct
  1058. * @buf: a pointer to the input buffer
  1059. * @len: length of the input buffer
  1060. *
  1061. * This routine is for changing the IR protocol type.
  1062. * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
  1063. * See parse_protocol_change() for the valid commands.
  1064. * Returns @len on success or a negative error code.
  1065. *
  1066. * dev->lock is taken to guard against races between
  1067. * store_protocols and show_protocols.
  1068. */
  1069. static ssize_t store_protocols(struct device *device,
  1070. struct device_attribute *mattr,
  1071. const char *buf, size_t len)
  1072. {
  1073. struct rc_dev *dev = to_rc_dev(device);
  1074. u64 *current_protocols;
  1075. struct rc_scancode_filter *filter;
  1076. u64 old_protocols, new_protocols;
  1077. ssize_t rc;
  1078. dev_dbg(&dev->dev, "Normal protocol change requested\n");
  1079. current_protocols = &dev->enabled_protocols;
  1080. filter = &dev->scancode_filter;
  1081. if (!dev->change_protocol) {
  1082. dev_dbg(&dev->dev, "Protocol switching not supported\n");
  1083. return -EINVAL;
  1084. }
  1085. mutex_lock(&dev->lock);
  1086. old_protocols = *current_protocols;
  1087. new_protocols = old_protocols;
  1088. rc = parse_protocol_change(dev, &new_protocols, buf);
  1089. if (rc < 0)
  1090. goto out;
  1091. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1092. ir_raw_load_modules(&new_protocols);
  1093. rc = dev->change_protocol(dev, &new_protocols);
  1094. if (rc < 0) {
  1095. dev_dbg(&dev->dev, "Error setting protocols to 0x%llx\n",
  1096. (long long)new_protocols);
  1097. goto out;
  1098. }
  1099. if (new_protocols != old_protocols) {
  1100. *current_protocols = new_protocols;
  1101. dev_dbg(&dev->dev, "Protocols changed to 0x%llx\n",
  1102. (long long)new_protocols);
  1103. }
  1104. /*
  1105. * If a protocol change was attempted the filter may need updating, even
  1106. * if the actual protocol mask hasn't changed (since the driver may have
  1107. * cleared the filter).
  1108. * Try setting the same filter with the new protocol (if any).
  1109. * Fall back to clearing the filter.
  1110. */
  1111. if (dev->s_filter && filter->mask) {
  1112. if (new_protocols)
  1113. rc = dev->s_filter(dev, filter);
  1114. else
  1115. rc = -1;
  1116. if (rc < 0) {
  1117. filter->data = 0;
  1118. filter->mask = 0;
  1119. dev->s_filter(dev, filter);
  1120. }
  1121. }
  1122. rc = len;
  1123. out:
  1124. mutex_unlock(&dev->lock);
  1125. return rc;
  1126. }
  1127. /**
  1128. * show_filter() - shows the current scancode filter value or mask
  1129. * @device: the device descriptor
  1130. * @attr: the device attribute struct
  1131. * @buf: a pointer to the output buffer
  1132. *
  1133. * This routine is a callback routine to read a scancode filter value or mask.
  1134. * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
  1135. * It prints the current scancode filter value or mask of the appropriate filter
  1136. * type in hexadecimal into @buf and returns the size of the buffer.
  1137. *
  1138. * Bits of the filter value corresponding to set bits in the filter mask are
  1139. * compared against input scancodes and non-matching scancodes are discarded.
  1140. *
  1141. * dev->lock is taken to guard against races between
  1142. * store_filter and show_filter.
  1143. */
  1144. static ssize_t show_filter(struct device *device,
  1145. struct device_attribute *attr,
  1146. char *buf)
  1147. {
  1148. struct rc_dev *dev = to_rc_dev(device);
  1149. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1150. struct rc_scancode_filter *filter;
  1151. u32 val;
  1152. mutex_lock(&dev->lock);
  1153. if (fattr->type == RC_FILTER_NORMAL)
  1154. filter = &dev->scancode_filter;
  1155. else
  1156. filter = &dev->scancode_wakeup_filter;
  1157. if (fattr->mask)
  1158. val = filter->mask;
  1159. else
  1160. val = filter->data;
  1161. mutex_unlock(&dev->lock);
  1162. return sprintf(buf, "%#x\n", val);
  1163. }
  1164. /**
  1165. * store_filter() - changes the scancode filter value
  1166. * @device: the device descriptor
  1167. * @attr: the device attribute struct
  1168. * @buf: a pointer to the input buffer
  1169. * @len: length of the input buffer
  1170. *
  1171. * This routine is for changing a scancode filter value or mask.
  1172. * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
  1173. * Returns -EINVAL if an invalid filter value for the current protocol was
  1174. * specified or if scancode filtering is not supported by the driver, otherwise
  1175. * returns @len.
  1176. *
  1177. * Bits of the filter value corresponding to set bits in the filter mask are
  1178. * compared against input scancodes and non-matching scancodes are discarded.
  1179. *
  1180. * dev->lock is taken to guard against races between
  1181. * store_filter and show_filter.
  1182. */
  1183. static ssize_t store_filter(struct device *device,
  1184. struct device_attribute *attr,
  1185. const char *buf, size_t len)
  1186. {
  1187. struct rc_dev *dev = to_rc_dev(device);
  1188. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1189. struct rc_scancode_filter new_filter, *filter;
  1190. int ret;
  1191. unsigned long val;
  1192. int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
  1193. ret = kstrtoul(buf, 0, &val);
  1194. if (ret < 0)
  1195. return ret;
  1196. if (fattr->type == RC_FILTER_NORMAL) {
  1197. set_filter = dev->s_filter;
  1198. filter = &dev->scancode_filter;
  1199. } else {
  1200. set_filter = dev->s_wakeup_filter;
  1201. filter = &dev->scancode_wakeup_filter;
  1202. }
  1203. if (!set_filter)
  1204. return -EINVAL;
  1205. mutex_lock(&dev->lock);
  1206. new_filter = *filter;
  1207. if (fattr->mask)
  1208. new_filter.mask = val;
  1209. else
  1210. new_filter.data = val;
  1211. if (fattr->type == RC_FILTER_WAKEUP) {
  1212. /*
  1213. * Refuse to set a filter unless a protocol is enabled
  1214. * and the filter is valid for that protocol
  1215. */
  1216. if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
  1217. ret = rc_validate_filter(dev, &new_filter);
  1218. else
  1219. ret = -EINVAL;
  1220. if (ret != 0)
  1221. goto unlock;
  1222. }
  1223. if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
  1224. val) {
  1225. /* refuse to set a filter unless a protocol is enabled */
  1226. ret = -EINVAL;
  1227. goto unlock;
  1228. }
  1229. ret = set_filter(dev, &new_filter);
  1230. if (ret < 0)
  1231. goto unlock;
  1232. *filter = new_filter;
  1233. unlock:
  1234. mutex_unlock(&dev->lock);
  1235. return (ret < 0) ? ret : len;
  1236. }
  1237. /**
  1238. * show_wakeup_protocols() - shows the wakeup IR protocol
  1239. * @device: the device descriptor
  1240. * @mattr: the device attribute struct
  1241. * @buf: a pointer to the output buffer
  1242. *
  1243. * This routine is a callback routine for input read the IR protocol type(s).
  1244. * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
  1245. * It returns the protocol names of supported protocols.
  1246. * The enabled protocols are printed in brackets.
  1247. *
  1248. * dev->lock is taken to guard against races between
  1249. * store_wakeup_protocols and show_wakeup_protocols.
  1250. */
  1251. static ssize_t show_wakeup_protocols(struct device *device,
  1252. struct device_attribute *mattr,
  1253. char *buf)
  1254. {
  1255. struct rc_dev *dev = to_rc_dev(device);
  1256. u64 allowed;
  1257. enum rc_proto enabled;
  1258. char *tmp = buf;
  1259. int i;
  1260. mutex_lock(&dev->lock);
  1261. allowed = dev->allowed_wakeup_protocols;
  1262. enabled = dev->wakeup_protocol;
  1263. mutex_unlock(&dev->lock);
  1264. dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - %d\n",
  1265. __func__, (long long)allowed, enabled);
  1266. for (i = 0; i < ARRAY_SIZE(protocols); i++) {
  1267. if (allowed & (1ULL << i)) {
  1268. if (i == enabled)
  1269. tmp += sprintf(tmp, "[%s] ", protocols[i].name);
  1270. else
  1271. tmp += sprintf(tmp, "%s ", protocols[i].name);
  1272. }
  1273. }
  1274. if (tmp != buf)
  1275. tmp--;
  1276. *tmp = '\n';
  1277. return tmp + 1 - buf;
  1278. }
  1279. /**
  1280. * store_wakeup_protocols() - changes the wakeup IR protocol(s)
  1281. * @device: the device descriptor
  1282. * @mattr: the device attribute struct
  1283. * @buf: a pointer to the input buffer
  1284. * @len: length of the input buffer
  1285. *
  1286. * This routine is for changing the IR protocol type.
  1287. * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
  1288. * Returns @len on success or a negative error code.
  1289. *
  1290. * dev->lock is taken to guard against races between
  1291. * store_wakeup_protocols and show_wakeup_protocols.
  1292. */
  1293. static ssize_t store_wakeup_protocols(struct device *device,
  1294. struct device_attribute *mattr,
  1295. const char *buf, size_t len)
  1296. {
  1297. struct rc_dev *dev = to_rc_dev(device);
  1298. enum rc_proto protocol;
  1299. ssize_t rc;
  1300. u64 allowed;
  1301. int i;
  1302. mutex_lock(&dev->lock);
  1303. allowed = dev->allowed_wakeup_protocols;
  1304. if (sysfs_streq(buf, "none")) {
  1305. protocol = RC_PROTO_UNKNOWN;
  1306. } else {
  1307. for (i = 0; i < ARRAY_SIZE(protocols); i++) {
  1308. if ((allowed & (1ULL << i)) &&
  1309. sysfs_streq(buf, protocols[i].name)) {
  1310. protocol = i;
  1311. break;
  1312. }
  1313. }
  1314. if (i == ARRAY_SIZE(protocols)) {
  1315. rc = -EINVAL;
  1316. goto out;
  1317. }
  1318. if (dev->encode_wakeup) {
  1319. u64 mask = 1ULL << protocol;
  1320. ir_raw_load_modules(&mask);
  1321. if (!mask) {
  1322. rc = -EINVAL;
  1323. goto out;
  1324. }
  1325. }
  1326. }
  1327. if (dev->wakeup_protocol != protocol) {
  1328. dev->wakeup_protocol = protocol;
  1329. dev_dbg(&dev->dev, "Wakeup protocol changed to %d\n", protocol);
  1330. if (protocol == RC_PROTO_RC6_MCE)
  1331. dev->scancode_wakeup_filter.data = 0x800f0000;
  1332. else
  1333. dev->scancode_wakeup_filter.data = 0;
  1334. dev->scancode_wakeup_filter.mask = 0;
  1335. rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
  1336. if (rc == 0)
  1337. rc = len;
  1338. } else {
  1339. rc = len;
  1340. }
  1341. out:
  1342. mutex_unlock(&dev->lock);
  1343. return rc;
  1344. }
  1345. static void rc_dev_release(struct device *device)
  1346. {
  1347. struct rc_dev *dev = to_rc_dev(device);
  1348. kfree(dev);
  1349. }
  1350. #define ADD_HOTPLUG_VAR(fmt, val...) \
  1351. do { \
  1352. int err = add_uevent_var(env, fmt, val); \
  1353. if (err) \
  1354. return err; \
  1355. } while (0)
  1356. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  1357. {
  1358. struct rc_dev *dev = to_rc_dev(device);
  1359. if (dev->rc_map.name)
  1360. ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
  1361. if (dev->driver_name)
  1362. ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
  1363. if (dev->device_name)
  1364. ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
  1365. return 0;
  1366. }
  1367. /*
  1368. * Static device attribute struct with the sysfs attributes for IR's
  1369. */
  1370. static struct device_attribute dev_attr_ro_protocols =
  1371. __ATTR(protocols, 0444, show_protocols, NULL);
  1372. static struct device_attribute dev_attr_rw_protocols =
  1373. __ATTR(protocols, 0644, show_protocols, store_protocols);
  1374. static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
  1375. store_wakeup_protocols);
  1376. static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
  1377. show_filter, store_filter, RC_FILTER_NORMAL, false);
  1378. static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
  1379. show_filter, store_filter, RC_FILTER_NORMAL, true);
  1380. static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
  1381. show_filter, store_filter, RC_FILTER_WAKEUP, false);
  1382. static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
  1383. show_filter, store_filter, RC_FILTER_WAKEUP, true);
  1384. static struct attribute *rc_dev_rw_protocol_attrs[] = {
  1385. &dev_attr_rw_protocols.attr,
  1386. NULL,
  1387. };
  1388. static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
  1389. .attrs = rc_dev_rw_protocol_attrs,
  1390. };
  1391. static struct attribute *rc_dev_ro_protocol_attrs[] = {
  1392. &dev_attr_ro_protocols.attr,
  1393. NULL,
  1394. };
  1395. static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
  1396. .attrs = rc_dev_ro_protocol_attrs,
  1397. };
  1398. static struct attribute *rc_dev_filter_attrs[] = {
  1399. &dev_attr_filter.attr.attr,
  1400. &dev_attr_filter_mask.attr.attr,
  1401. NULL,
  1402. };
  1403. static const struct attribute_group rc_dev_filter_attr_grp = {
  1404. .attrs = rc_dev_filter_attrs,
  1405. };
  1406. static struct attribute *rc_dev_wakeup_filter_attrs[] = {
  1407. &dev_attr_wakeup_filter.attr.attr,
  1408. &dev_attr_wakeup_filter_mask.attr.attr,
  1409. &dev_attr_wakeup_protocols.attr,
  1410. NULL,
  1411. };
  1412. static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
  1413. .attrs = rc_dev_wakeup_filter_attrs,
  1414. };
  1415. static const struct device_type rc_dev_type = {
  1416. .release = rc_dev_release,
  1417. .uevent = rc_dev_uevent,
  1418. };
  1419. struct rc_dev *rc_allocate_device(enum rc_driver_type type)
  1420. {
  1421. struct rc_dev *dev;
  1422. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1423. if (!dev)
  1424. return NULL;
  1425. if (type != RC_DRIVER_IR_RAW_TX) {
  1426. dev->input_dev = input_allocate_device();
  1427. if (!dev->input_dev) {
  1428. kfree(dev);
  1429. return NULL;
  1430. }
  1431. dev->input_dev->getkeycode = ir_getkeycode;
  1432. dev->input_dev->setkeycode = ir_setkeycode;
  1433. input_set_drvdata(dev->input_dev, dev);
  1434. dev->timeout = IR_DEFAULT_TIMEOUT;
  1435. timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
  1436. timer_setup(&dev->timer_repeat, ir_timer_repeat, 0);
  1437. spin_lock_init(&dev->rc_map.lock);
  1438. spin_lock_init(&dev->keylock);
  1439. }
  1440. mutex_init(&dev->lock);
  1441. dev->dev.type = &rc_dev_type;
  1442. dev->dev.class = &rc_class;
  1443. device_initialize(&dev->dev);
  1444. dev->driver_type = type;
  1445. __module_get(THIS_MODULE);
  1446. return dev;
  1447. }
  1448. EXPORT_SYMBOL_GPL(rc_allocate_device);
  1449. void rc_free_device(struct rc_dev *dev)
  1450. {
  1451. if (!dev)
  1452. return;
  1453. input_free_device(dev->input_dev);
  1454. put_device(&dev->dev);
  1455. /* kfree(dev) will be called by the callback function
  1456. rc_dev_release() */
  1457. module_put(THIS_MODULE);
  1458. }
  1459. EXPORT_SYMBOL_GPL(rc_free_device);
  1460. static void devm_rc_alloc_release(struct device *dev, void *res)
  1461. {
  1462. rc_free_device(*(struct rc_dev **)res);
  1463. }
  1464. struct rc_dev *devm_rc_allocate_device(struct device *dev,
  1465. enum rc_driver_type type)
  1466. {
  1467. struct rc_dev **dr, *rc;
  1468. dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
  1469. if (!dr)
  1470. return NULL;
  1471. rc = rc_allocate_device(type);
  1472. if (!rc) {
  1473. devres_free(dr);
  1474. return NULL;
  1475. }
  1476. rc->dev.parent = dev;
  1477. rc->managed_alloc = true;
  1478. *dr = rc;
  1479. devres_add(dev, dr);
  1480. return rc;
  1481. }
  1482. EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
  1483. static int rc_prepare_rx_device(struct rc_dev *dev)
  1484. {
  1485. int rc;
  1486. struct rc_map *rc_map;
  1487. u64 rc_proto;
  1488. if (!dev->map_name)
  1489. return -EINVAL;
  1490. rc_map = rc_map_get(dev->map_name);
  1491. if (!rc_map)
  1492. rc_map = rc_map_get(RC_MAP_EMPTY);
  1493. if (!rc_map || !rc_map->scan || rc_map->size == 0)
  1494. return -EINVAL;
  1495. rc = ir_setkeytable(dev, rc_map);
  1496. if (rc)
  1497. return rc;
  1498. rc_proto = BIT_ULL(rc_map->rc_proto);
  1499. if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
  1500. dev->enabled_protocols = dev->allowed_protocols;
  1501. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1502. ir_raw_load_modules(&rc_proto);
  1503. if (dev->change_protocol) {
  1504. rc = dev->change_protocol(dev, &rc_proto);
  1505. if (rc < 0)
  1506. goto out_table;
  1507. dev->enabled_protocols = rc_proto;
  1508. }
  1509. set_bit(EV_KEY, dev->input_dev->evbit);
  1510. set_bit(EV_REP, dev->input_dev->evbit);
  1511. set_bit(EV_MSC, dev->input_dev->evbit);
  1512. set_bit(MSC_SCAN, dev->input_dev->mscbit);
  1513. if (dev->open)
  1514. dev->input_dev->open = ir_open;
  1515. if (dev->close)
  1516. dev->input_dev->close = ir_close;
  1517. dev->input_dev->dev.parent = &dev->dev;
  1518. memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
  1519. dev->input_dev->phys = dev->input_phys;
  1520. dev->input_dev->name = dev->device_name;
  1521. return 0;
  1522. out_table:
  1523. ir_free_table(&dev->rc_map);
  1524. return rc;
  1525. }
  1526. static int rc_setup_rx_device(struct rc_dev *dev)
  1527. {
  1528. int rc;
  1529. /* rc_open will be called here */
  1530. rc = input_register_device(dev->input_dev);
  1531. if (rc)
  1532. return rc;
  1533. /*
  1534. * Default delay of 250ms is too short for some protocols, especially
  1535. * since the timeout is currently set to 250ms. Increase it to 500ms,
  1536. * to avoid wrong repetition of the keycodes. Note that this must be
  1537. * set after the call to input_register_device().
  1538. */
  1539. if (dev->allowed_protocols == RC_PROTO_BIT_CEC)
  1540. dev->input_dev->rep[REP_DELAY] = 0;
  1541. else
  1542. dev->input_dev->rep[REP_DELAY] = 500;
  1543. /*
  1544. * As a repeat event on protocols like RC-5 and NEC take as long as
  1545. * 110/114ms, using 33ms as a repeat period is not the right thing
  1546. * to do.
  1547. */
  1548. dev->input_dev->rep[REP_PERIOD] = 125;
  1549. return 0;
  1550. }
  1551. static void rc_free_rx_device(struct rc_dev *dev)
  1552. {
  1553. if (!dev)
  1554. return;
  1555. if (dev->input_dev) {
  1556. input_unregister_device(dev->input_dev);
  1557. dev->input_dev = NULL;
  1558. }
  1559. ir_free_table(&dev->rc_map);
  1560. }
  1561. int rc_register_device(struct rc_dev *dev)
  1562. {
  1563. const char *path;
  1564. int attr = 0;
  1565. int minor;
  1566. int rc;
  1567. if (!dev)
  1568. return -EINVAL;
  1569. minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
  1570. if (minor < 0)
  1571. return minor;
  1572. dev->minor = minor;
  1573. dev_set_name(&dev->dev, "rc%u", dev->minor);
  1574. dev_set_drvdata(&dev->dev, dev);
  1575. dev->dev.groups = dev->sysfs_groups;
  1576. if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
  1577. dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
  1578. else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
  1579. dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
  1580. if (dev->s_filter)
  1581. dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
  1582. if (dev->s_wakeup_filter)
  1583. dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
  1584. dev->sysfs_groups[attr++] = NULL;
  1585. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  1586. rc = ir_raw_event_prepare(dev);
  1587. if (rc < 0)
  1588. goto out_minor;
  1589. }
  1590. if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
  1591. rc = rc_prepare_rx_device(dev);
  1592. if (rc)
  1593. goto out_raw;
  1594. }
  1595. rc = device_add(&dev->dev);
  1596. if (rc)
  1597. goto out_rx_free;
  1598. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1599. dev_info(&dev->dev, "%s as %s\n",
  1600. dev->device_name ?: "Unspecified device", path ?: "N/A");
  1601. kfree(path);
  1602. dev->registered = true;
  1603. if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
  1604. rc = rc_setup_rx_device(dev);
  1605. if (rc)
  1606. goto out_dev;
  1607. }
  1608. /* Ensure that the lirc kfifo is setup before we start the thread */
  1609. if (dev->allowed_protocols != RC_PROTO_BIT_CEC) {
  1610. rc = ir_lirc_register(dev);
  1611. if (rc < 0)
  1612. goto out_rx;
  1613. }
  1614. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  1615. rc = ir_raw_event_register(dev);
  1616. if (rc < 0)
  1617. goto out_lirc;
  1618. }
  1619. dev_dbg(&dev->dev, "Registered rc%u (driver: %s)\n", dev->minor,
  1620. dev->driver_name ? dev->driver_name : "unknown");
  1621. return 0;
  1622. out_lirc:
  1623. if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
  1624. ir_lirc_unregister(dev);
  1625. out_rx:
  1626. rc_free_rx_device(dev);
  1627. out_dev:
  1628. device_del(&dev->dev);
  1629. out_rx_free:
  1630. ir_free_table(&dev->rc_map);
  1631. out_raw:
  1632. ir_raw_event_free(dev);
  1633. out_minor:
  1634. ida_simple_remove(&rc_ida, minor);
  1635. return rc;
  1636. }
  1637. EXPORT_SYMBOL_GPL(rc_register_device);
  1638. static void devm_rc_release(struct device *dev, void *res)
  1639. {
  1640. rc_unregister_device(*(struct rc_dev **)res);
  1641. }
  1642. int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
  1643. {
  1644. struct rc_dev **dr;
  1645. int ret;
  1646. dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
  1647. if (!dr)
  1648. return -ENOMEM;
  1649. ret = rc_register_device(dev);
  1650. if (ret) {
  1651. devres_free(dr);
  1652. return ret;
  1653. }
  1654. *dr = dev;
  1655. devres_add(parent, dr);
  1656. return 0;
  1657. }
  1658. EXPORT_SYMBOL_GPL(devm_rc_register_device);
  1659. void rc_unregister_device(struct rc_dev *dev)
  1660. {
  1661. if (!dev)
  1662. return;
  1663. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1664. ir_raw_event_unregister(dev);
  1665. del_timer_sync(&dev->timer_keyup);
  1666. del_timer_sync(&dev->timer_repeat);
  1667. rc_free_rx_device(dev);
  1668. mutex_lock(&dev->lock);
  1669. dev->registered = false;
  1670. mutex_unlock(&dev->lock);
  1671. /*
  1672. * lirc device should be freed with dev->registered = false, so
  1673. * that userspace polling will get notified.
  1674. */
  1675. if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
  1676. ir_lirc_unregister(dev);
  1677. device_del(&dev->dev);
  1678. ida_simple_remove(&rc_ida, dev->minor);
  1679. if (!dev->managed_alloc)
  1680. rc_free_device(dev);
  1681. }
  1682. EXPORT_SYMBOL_GPL(rc_unregister_device);
  1683. /*
  1684. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  1685. */
  1686. static int __init rc_core_init(void)
  1687. {
  1688. int rc = class_register(&rc_class);
  1689. if (rc) {
  1690. pr_err("rc_core: unable to register rc class\n");
  1691. return rc;
  1692. }
  1693. rc = lirc_dev_init();
  1694. if (rc) {
  1695. pr_err("rc_core: unable to init lirc\n");
  1696. class_unregister(&rc_class);
  1697. return 0;
  1698. }
  1699. led_trigger_register_simple("rc-feedback", &led_feedback);
  1700. rc_map_register(&empty_map);
  1701. return 0;
  1702. }
  1703. static void __exit rc_core_exit(void)
  1704. {
  1705. lirc_dev_exit();
  1706. class_unregister(&rc_class);
  1707. led_trigger_unregister_simple(led_feedback);
  1708. rc_map_unregister(&empty_map);
  1709. }
  1710. subsys_initcall(rc_core_init);
  1711. module_exit(rc_core_exit);
  1712. MODULE_AUTHOR("Mauro Carvalho Chehab");
  1713. MODULE_LICENSE("GPL v2");