rc-main.c 53 KB

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