rc-main.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  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. old_protocols = *current_protocols;
  1089. new_protocols = old_protocols;
  1090. rc = parse_protocol_change(dev, &new_protocols, buf);
  1091. if (rc < 0)
  1092. goto out;
  1093. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1094. ir_raw_load_modules(&new_protocols);
  1095. rc = dev->change_protocol(dev, &new_protocols);
  1096. if (rc < 0) {
  1097. dev_dbg(&dev->dev, "Error setting protocols to 0x%llx\n",
  1098. (long long)new_protocols);
  1099. goto out;
  1100. }
  1101. if (new_protocols != old_protocols) {
  1102. *current_protocols = new_protocols;
  1103. dev_dbg(&dev->dev, "Protocols changed to 0x%llx\n",
  1104. (long long)new_protocols);
  1105. }
  1106. /*
  1107. * If a protocol change was attempted the filter may need updating, even
  1108. * if the actual protocol mask hasn't changed (since the driver may have
  1109. * cleared the filter).
  1110. * Try setting the same filter with the new protocol (if any).
  1111. * Fall back to clearing the filter.
  1112. */
  1113. if (dev->s_filter && filter->mask) {
  1114. if (new_protocols)
  1115. rc = dev->s_filter(dev, filter);
  1116. else
  1117. rc = -1;
  1118. if (rc < 0) {
  1119. filter->data = 0;
  1120. filter->mask = 0;
  1121. dev->s_filter(dev, filter);
  1122. }
  1123. }
  1124. rc = len;
  1125. out:
  1126. mutex_unlock(&dev->lock);
  1127. return rc;
  1128. }
  1129. /**
  1130. * show_filter() - shows the current scancode filter value or mask
  1131. * @device: the device descriptor
  1132. * @attr: the device attribute struct
  1133. * @buf: a pointer to the output buffer
  1134. *
  1135. * This routine is a callback routine to read a scancode filter value or mask.
  1136. * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
  1137. * It prints the current scancode filter value or mask of the appropriate filter
  1138. * type in hexadecimal into @buf and returns the size of the buffer.
  1139. *
  1140. * Bits of the filter value corresponding to set bits in the filter mask are
  1141. * compared against input scancodes and non-matching scancodes are discarded.
  1142. *
  1143. * dev->lock is taken to guard against races between
  1144. * store_filter and show_filter.
  1145. */
  1146. static ssize_t show_filter(struct device *device,
  1147. struct device_attribute *attr,
  1148. char *buf)
  1149. {
  1150. struct rc_dev *dev = to_rc_dev(device);
  1151. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1152. struct rc_scancode_filter *filter;
  1153. u32 val;
  1154. mutex_lock(&dev->lock);
  1155. if (fattr->type == RC_FILTER_NORMAL)
  1156. filter = &dev->scancode_filter;
  1157. else
  1158. filter = &dev->scancode_wakeup_filter;
  1159. if (fattr->mask)
  1160. val = filter->mask;
  1161. else
  1162. val = filter->data;
  1163. mutex_unlock(&dev->lock);
  1164. return sprintf(buf, "%#x\n", val);
  1165. }
  1166. /**
  1167. * store_filter() - changes the scancode filter value
  1168. * @device: the device descriptor
  1169. * @attr: the device attribute struct
  1170. * @buf: a pointer to the input buffer
  1171. * @len: length of the input buffer
  1172. *
  1173. * This routine is for changing a scancode filter value or mask.
  1174. * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
  1175. * Returns -EINVAL if an invalid filter value for the current protocol was
  1176. * specified or if scancode filtering is not supported by the driver, otherwise
  1177. * returns @len.
  1178. *
  1179. * Bits of the filter value corresponding to set bits in the filter mask are
  1180. * compared against input scancodes and non-matching scancodes are discarded.
  1181. *
  1182. * dev->lock is taken to guard against races between
  1183. * store_filter and show_filter.
  1184. */
  1185. static ssize_t store_filter(struct device *device,
  1186. struct device_attribute *attr,
  1187. const char *buf, size_t len)
  1188. {
  1189. struct rc_dev *dev = to_rc_dev(device);
  1190. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  1191. struct rc_scancode_filter new_filter, *filter;
  1192. int ret;
  1193. unsigned long val;
  1194. int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
  1195. ret = kstrtoul(buf, 0, &val);
  1196. if (ret < 0)
  1197. return ret;
  1198. if (fattr->type == RC_FILTER_NORMAL) {
  1199. set_filter = dev->s_filter;
  1200. filter = &dev->scancode_filter;
  1201. } else {
  1202. set_filter = dev->s_wakeup_filter;
  1203. filter = &dev->scancode_wakeup_filter;
  1204. }
  1205. if (!set_filter)
  1206. return -EINVAL;
  1207. mutex_lock(&dev->lock);
  1208. new_filter = *filter;
  1209. if (fattr->mask)
  1210. new_filter.mask = val;
  1211. else
  1212. new_filter.data = val;
  1213. if (fattr->type == RC_FILTER_WAKEUP) {
  1214. /*
  1215. * Refuse to set a filter unless a protocol is enabled
  1216. * and the filter is valid for that protocol
  1217. */
  1218. if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
  1219. ret = rc_validate_filter(dev, &new_filter);
  1220. else
  1221. ret = -EINVAL;
  1222. if (ret != 0)
  1223. goto unlock;
  1224. }
  1225. if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
  1226. val) {
  1227. /* refuse to set a filter unless a protocol is enabled */
  1228. ret = -EINVAL;
  1229. goto unlock;
  1230. }
  1231. ret = set_filter(dev, &new_filter);
  1232. if (ret < 0)
  1233. goto unlock;
  1234. *filter = new_filter;
  1235. unlock:
  1236. mutex_unlock(&dev->lock);
  1237. return (ret < 0) ? ret : len;
  1238. }
  1239. /**
  1240. * show_wakeup_protocols() - shows the wakeup IR protocol
  1241. * @device: the device descriptor
  1242. * @mattr: the device attribute struct
  1243. * @buf: a pointer to the output buffer
  1244. *
  1245. * This routine is a callback routine for input read the IR protocol type(s).
  1246. * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
  1247. * It returns the protocol names of supported protocols.
  1248. * The enabled protocols are printed in brackets.
  1249. *
  1250. * dev->lock is taken to guard against races between
  1251. * store_wakeup_protocols and show_wakeup_protocols.
  1252. */
  1253. static ssize_t show_wakeup_protocols(struct device *device,
  1254. struct device_attribute *mattr,
  1255. char *buf)
  1256. {
  1257. struct rc_dev *dev = to_rc_dev(device);
  1258. u64 allowed;
  1259. enum rc_proto enabled;
  1260. char *tmp = buf;
  1261. int i;
  1262. mutex_lock(&dev->lock);
  1263. allowed = dev->allowed_wakeup_protocols;
  1264. enabled = dev->wakeup_protocol;
  1265. mutex_unlock(&dev->lock);
  1266. dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - %d\n",
  1267. __func__, (long long)allowed, enabled);
  1268. for (i = 0; i < ARRAY_SIZE(protocols); i++) {
  1269. if (allowed & (1ULL << i)) {
  1270. if (i == enabled)
  1271. tmp += sprintf(tmp, "[%s] ", protocols[i].name);
  1272. else
  1273. tmp += sprintf(tmp, "%s ", protocols[i].name);
  1274. }
  1275. }
  1276. if (tmp != buf)
  1277. tmp--;
  1278. *tmp = '\n';
  1279. return tmp + 1 - buf;
  1280. }
  1281. /**
  1282. * store_wakeup_protocols() - changes the wakeup IR protocol(s)
  1283. * @device: the device descriptor
  1284. * @mattr: the device attribute struct
  1285. * @buf: a pointer to the input buffer
  1286. * @len: length of the input buffer
  1287. *
  1288. * This routine is for changing the IR protocol type.
  1289. * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
  1290. * Returns @len on success or a negative error code.
  1291. *
  1292. * dev->lock is taken to guard against races between
  1293. * store_wakeup_protocols and show_wakeup_protocols.
  1294. */
  1295. static ssize_t store_wakeup_protocols(struct device *device,
  1296. struct device_attribute *mattr,
  1297. const char *buf, size_t len)
  1298. {
  1299. struct rc_dev *dev = to_rc_dev(device);
  1300. enum rc_proto protocol;
  1301. ssize_t rc;
  1302. u64 allowed;
  1303. int i;
  1304. mutex_lock(&dev->lock);
  1305. allowed = dev->allowed_wakeup_protocols;
  1306. if (sysfs_streq(buf, "none")) {
  1307. protocol = RC_PROTO_UNKNOWN;
  1308. } else {
  1309. for (i = 0; i < ARRAY_SIZE(protocols); i++) {
  1310. if ((allowed & (1ULL << i)) &&
  1311. sysfs_streq(buf, protocols[i].name)) {
  1312. protocol = i;
  1313. break;
  1314. }
  1315. }
  1316. if (i == ARRAY_SIZE(protocols)) {
  1317. rc = -EINVAL;
  1318. goto out;
  1319. }
  1320. if (dev->encode_wakeup) {
  1321. u64 mask = 1ULL << protocol;
  1322. ir_raw_load_modules(&mask);
  1323. if (!mask) {
  1324. rc = -EINVAL;
  1325. goto out;
  1326. }
  1327. }
  1328. }
  1329. if (dev->wakeup_protocol != protocol) {
  1330. dev->wakeup_protocol = protocol;
  1331. dev_dbg(&dev->dev, "Wakeup protocol changed to %d\n", protocol);
  1332. if (protocol == RC_PROTO_RC6_MCE)
  1333. dev->scancode_wakeup_filter.data = 0x800f0000;
  1334. else
  1335. dev->scancode_wakeup_filter.data = 0;
  1336. dev->scancode_wakeup_filter.mask = 0;
  1337. rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
  1338. if (rc == 0)
  1339. rc = len;
  1340. } else {
  1341. rc = len;
  1342. }
  1343. out:
  1344. mutex_unlock(&dev->lock);
  1345. return rc;
  1346. }
  1347. static void rc_dev_release(struct device *device)
  1348. {
  1349. struct rc_dev *dev = to_rc_dev(device);
  1350. kfree(dev);
  1351. }
  1352. #define ADD_HOTPLUG_VAR(fmt, val...) \
  1353. do { \
  1354. int err = add_uevent_var(env, fmt, val); \
  1355. if (err) \
  1356. return err; \
  1357. } while (0)
  1358. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  1359. {
  1360. struct rc_dev *dev = to_rc_dev(device);
  1361. if (dev->rc_map.name)
  1362. ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
  1363. if (dev->driver_name)
  1364. ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
  1365. if (dev->device_name)
  1366. ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
  1367. return 0;
  1368. }
  1369. /*
  1370. * Static device attribute struct with the sysfs attributes for IR's
  1371. */
  1372. static struct device_attribute dev_attr_ro_protocols =
  1373. __ATTR(protocols, 0444, show_protocols, NULL);
  1374. static struct device_attribute dev_attr_rw_protocols =
  1375. __ATTR(protocols, 0644, show_protocols, store_protocols);
  1376. static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
  1377. store_wakeup_protocols);
  1378. static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
  1379. show_filter, store_filter, RC_FILTER_NORMAL, false);
  1380. static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
  1381. show_filter, store_filter, RC_FILTER_NORMAL, true);
  1382. static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
  1383. show_filter, store_filter, RC_FILTER_WAKEUP, false);
  1384. static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
  1385. show_filter, store_filter, RC_FILTER_WAKEUP, true);
  1386. static struct attribute *rc_dev_rw_protocol_attrs[] = {
  1387. &dev_attr_rw_protocols.attr,
  1388. NULL,
  1389. };
  1390. static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
  1391. .attrs = rc_dev_rw_protocol_attrs,
  1392. };
  1393. static struct attribute *rc_dev_ro_protocol_attrs[] = {
  1394. &dev_attr_ro_protocols.attr,
  1395. NULL,
  1396. };
  1397. static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
  1398. .attrs = rc_dev_ro_protocol_attrs,
  1399. };
  1400. static struct attribute *rc_dev_filter_attrs[] = {
  1401. &dev_attr_filter.attr.attr,
  1402. &dev_attr_filter_mask.attr.attr,
  1403. NULL,
  1404. };
  1405. static const struct attribute_group rc_dev_filter_attr_grp = {
  1406. .attrs = rc_dev_filter_attrs,
  1407. };
  1408. static struct attribute *rc_dev_wakeup_filter_attrs[] = {
  1409. &dev_attr_wakeup_filter.attr.attr,
  1410. &dev_attr_wakeup_filter_mask.attr.attr,
  1411. &dev_attr_wakeup_protocols.attr,
  1412. NULL,
  1413. };
  1414. static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
  1415. .attrs = rc_dev_wakeup_filter_attrs,
  1416. };
  1417. static const struct device_type rc_dev_type = {
  1418. .release = rc_dev_release,
  1419. .uevent = rc_dev_uevent,
  1420. };
  1421. struct rc_dev *rc_allocate_device(enum rc_driver_type type)
  1422. {
  1423. struct rc_dev *dev;
  1424. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1425. if (!dev)
  1426. return NULL;
  1427. if (type != RC_DRIVER_IR_RAW_TX) {
  1428. dev->input_dev = input_allocate_device();
  1429. if (!dev->input_dev) {
  1430. kfree(dev);
  1431. return NULL;
  1432. }
  1433. dev->input_dev->getkeycode = ir_getkeycode;
  1434. dev->input_dev->setkeycode = ir_setkeycode;
  1435. input_set_drvdata(dev->input_dev, dev);
  1436. dev->timeout = IR_DEFAULT_TIMEOUT;
  1437. timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
  1438. timer_setup(&dev->timer_repeat, ir_timer_repeat, 0);
  1439. spin_lock_init(&dev->rc_map.lock);
  1440. spin_lock_init(&dev->keylock);
  1441. }
  1442. mutex_init(&dev->lock);
  1443. dev->dev.type = &rc_dev_type;
  1444. dev->dev.class = &rc_class;
  1445. device_initialize(&dev->dev);
  1446. dev->driver_type = type;
  1447. __module_get(THIS_MODULE);
  1448. return dev;
  1449. }
  1450. EXPORT_SYMBOL_GPL(rc_allocate_device);
  1451. void rc_free_device(struct rc_dev *dev)
  1452. {
  1453. if (!dev)
  1454. return;
  1455. input_free_device(dev->input_dev);
  1456. put_device(&dev->dev);
  1457. /* kfree(dev) will be called by the callback function
  1458. rc_dev_release() */
  1459. module_put(THIS_MODULE);
  1460. }
  1461. EXPORT_SYMBOL_GPL(rc_free_device);
  1462. static void devm_rc_alloc_release(struct device *dev, void *res)
  1463. {
  1464. rc_free_device(*(struct rc_dev **)res);
  1465. }
  1466. struct rc_dev *devm_rc_allocate_device(struct device *dev,
  1467. enum rc_driver_type type)
  1468. {
  1469. struct rc_dev **dr, *rc;
  1470. dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
  1471. if (!dr)
  1472. return NULL;
  1473. rc = rc_allocate_device(type);
  1474. if (!rc) {
  1475. devres_free(dr);
  1476. return NULL;
  1477. }
  1478. rc->dev.parent = dev;
  1479. rc->managed_alloc = true;
  1480. *dr = rc;
  1481. devres_add(dev, dr);
  1482. return rc;
  1483. }
  1484. EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
  1485. static int rc_prepare_rx_device(struct rc_dev *dev)
  1486. {
  1487. int rc;
  1488. struct rc_map *rc_map;
  1489. u64 rc_proto;
  1490. if (!dev->map_name)
  1491. return -EINVAL;
  1492. rc_map = rc_map_get(dev->map_name);
  1493. if (!rc_map)
  1494. rc_map = rc_map_get(RC_MAP_EMPTY);
  1495. if (!rc_map || !rc_map->scan || rc_map->size == 0)
  1496. return -EINVAL;
  1497. rc = ir_setkeytable(dev, rc_map);
  1498. if (rc)
  1499. return rc;
  1500. rc_proto = BIT_ULL(rc_map->rc_proto);
  1501. if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
  1502. dev->enabled_protocols = dev->allowed_protocols;
  1503. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1504. ir_raw_load_modules(&rc_proto);
  1505. if (dev->change_protocol) {
  1506. rc = dev->change_protocol(dev, &rc_proto);
  1507. if (rc < 0)
  1508. goto out_table;
  1509. dev->enabled_protocols = rc_proto;
  1510. }
  1511. set_bit(EV_KEY, dev->input_dev->evbit);
  1512. set_bit(EV_REP, dev->input_dev->evbit);
  1513. set_bit(EV_MSC, dev->input_dev->evbit);
  1514. set_bit(MSC_SCAN, dev->input_dev->mscbit);
  1515. if (dev->open)
  1516. dev->input_dev->open = ir_open;
  1517. if (dev->close)
  1518. dev->input_dev->close = ir_close;
  1519. dev->input_dev->dev.parent = &dev->dev;
  1520. memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
  1521. dev->input_dev->phys = dev->input_phys;
  1522. dev->input_dev->name = dev->device_name;
  1523. return 0;
  1524. out_table:
  1525. ir_free_table(&dev->rc_map);
  1526. return rc;
  1527. }
  1528. static int rc_setup_rx_device(struct rc_dev *dev)
  1529. {
  1530. int rc;
  1531. /* rc_open will be called here */
  1532. rc = input_register_device(dev->input_dev);
  1533. if (rc)
  1534. return rc;
  1535. /*
  1536. * Default delay of 250ms is too short for some protocols, especially
  1537. * since the timeout is currently set to 250ms. Increase it to 500ms,
  1538. * to avoid wrong repetition of the keycodes. Note that this must be
  1539. * set after the call to input_register_device().
  1540. */
  1541. if (dev->allowed_protocols == RC_PROTO_BIT_CEC)
  1542. dev->input_dev->rep[REP_DELAY] = 0;
  1543. else
  1544. dev->input_dev->rep[REP_DELAY] = 500;
  1545. /*
  1546. * As a repeat event on protocols like RC-5 and NEC take as long as
  1547. * 110/114ms, using 33ms as a repeat period is not the right thing
  1548. * to do.
  1549. */
  1550. dev->input_dev->rep[REP_PERIOD] = 125;
  1551. return 0;
  1552. }
  1553. static void rc_free_rx_device(struct rc_dev *dev)
  1554. {
  1555. if (!dev)
  1556. return;
  1557. if (dev->input_dev) {
  1558. input_unregister_device(dev->input_dev);
  1559. dev->input_dev = NULL;
  1560. }
  1561. ir_free_table(&dev->rc_map);
  1562. }
  1563. int rc_register_device(struct rc_dev *dev)
  1564. {
  1565. const char *path;
  1566. int attr = 0;
  1567. int minor;
  1568. int rc;
  1569. if (!dev)
  1570. return -EINVAL;
  1571. minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
  1572. if (minor < 0)
  1573. return minor;
  1574. dev->minor = minor;
  1575. dev_set_name(&dev->dev, "rc%u", dev->minor);
  1576. dev_set_drvdata(&dev->dev, dev);
  1577. dev->dev.groups = dev->sysfs_groups;
  1578. if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
  1579. dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
  1580. else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
  1581. dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
  1582. if (dev->s_filter)
  1583. dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
  1584. if (dev->s_wakeup_filter)
  1585. dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
  1586. dev->sysfs_groups[attr++] = NULL;
  1587. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  1588. rc = ir_raw_event_prepare(dev);
  1589. if (rc < 0)
  1590. goto out_minor;
  1591. }
  1592. if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
  1593. rc = rc_prepare_rx_device(dev);
  1594. if (rc)
  1595. goto out_raw;
  1596. }
  1597. rc = device_add(&dev->dev);
  1598. if (rc)
  1599. goto out_rx_free;
  1600. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1601. dev_info(&dev->dev, "%s as %s\n",
  1602. dev->device_name ?: "Unspecified device", path ?: "N/A");
  1603. kfree(path);
  1604. dev->registered = true;
  1605. /*
  1606. * once the the input device is registered in rc_setup_rx_device,
  1607. * userspace can open the input device and rc_open() will be called
  1608. * as a result. This results in driver code being allowed to submit
  1609. * keycodes with rc_keydown, so lirc must be registered first.
  1610. */
  1611. if (dev->allowed_protocols != RC_PROTO_BIT_CEC) {
  1612. rc = ir_lirc_register(dev);
  1613. if (rc < 0)
  1614. goto out_dev;
  1615. }
  1616. if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
  1617. rc = rc_setup_rx_device(dev);
  1618. if (rc)
  1619. goto out_lirc;
  1620. }
  1621. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  1622. rc = ir_raw_event_register(dev);
  1623. if (rc < 0)
  1624. goto out_rx;
  1625. }
  1626. dev_dbg(&dev->dev, "Registered rc%u (driver: %s)\n", dev->minor,
  1627. dev->driver_name ? dev->driver_name : "unknown");
  1628. return 0;
  1629. out_rx:
  1630. rc_free_rx_device(dev);
  1631. out_lirc:
  1632. if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
  1633. ir_lirc_unregister(dev);
  1634. out_dev:
  1635. device_del(&dev->dev);
  1636. out_rx_free:
  1637. ir_free_table(&dev->rc_map);
  1638. out_raw:
  1639. ir_raw_event_free(dev);
  1640. out_minor:
  1641. ida_simple_remove(&rc_ida, minor);
  1642. return rc;
  1643. }
  1644. EXPORT_SYMBOL_GPL(rc_register_device);
  1645. static void devm_rc_release(struct device *dev, void *res)
  1646. {
  1647. rc_unregister_device(*(struct rc_dev **)res);
  1648. }
  1649. int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
  1650. {
  1651. struct rc_dev **dr;
  1652. int ret;
  1653. dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
  1654. if (!dr)
  1655. return -ENOMEM;
  1656. ret = rc_register_device(dev);
  1657. if (ret) {
  1658. devres_free(dr);
  1659. return ret;
  1660. }
  1661. *dr = dev;
  1662. devres_add(parent, dr);
  1663. return 0;
  1664. }
  1665. EXPORT_SYMBOL_GPL(devm_rc_register_device);
  1666. void rc_unregister_device(struct rc_dev *dev)
  1667. {
  1668. if (!dev)
  1669. return;
  1670. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1671. ir_raw_event_unregister(dev);
  1672. del_timer_sync(&dev->timer_keyup);
  1673. del_timer_sync(&dev->timer_repeat);
  1674. rc_free_rx_device(dev);
  1675. mutex_lock(&dev->lock);
  1676. if (dev->users && dev->close)
  1677. dev->close(dev);
  1678. dev->registered = false;
  1679. mutex_unlock(&dev->lock);
  1680. /*
  1681. * lirc device should be freed with dev->registered = false, so
  1682. * that userspace polling will get notified.
  1683. */
  1684. if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
  1685. ir_lirc_unregister(dev);
  1686. device_del(&dev->dev);
  1687. ida_simple_remove(&rc_ida, dev->minor);
  1688. if (!dev->managed_alloc)
  1689. rc_free_device(dev);
  1690. }
  1691. EXPORT_SYMBOL_GPL(rc_unregister_device);
  1692. /*
  1693. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  1694. */
  1695. static int __init rc_core_init(void)
  1696. {
  1697. int rc = class_register(&rc_class);
  1698. if (rc) {
  1699. pr_err("rc_core: unable to register rc class\n");
  1700. return rc;
  1701. }
  1702. rc = lirc_dev_init();
  1703. if (rc) {
  1704. pr_err("rc_core: unable to init lirc\n");
  1705. class_unregister(&rc_class);
  1706. return 0;
  1707. }
  1708. led_trigger_register_simple("rc-feedback", &led_feedback);
  1709. rc_map_register(&empty_map);
  1710. return 0;
  1711. }
  1712. static void __exit rc_core_exit(void)
  1713. {
  1714. lirc_dev_exit();
  1715. class_unregister(&rc_class);
  1716. led_trigger_unregister_simple(led_feedback);
  1717. rc_map_unregister(&empty_map);
  1718. }
  1719. subsys_initcall(rc_core_init);
  1720. module_exit(rc_core_exit);
  1721. MODULE_AUTHOR("Mauro Carvalho Chehab");
  1722. MODULE_LICENSE("GPL v2");