rc-main.c 51 KB

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