rc-main.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. /* rc-main.c - Remote Controller core module
  2. *
  3. * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <media/rc-core.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/delay.h>
  17. #include <linux/input.h>
  18. #include <linux/leds.h>
  19. #include <linux/slab.h>
  20. #include <linux/device.h>
  21. #include <linux/module.h>
  22. #include "rc-core-priv.h"
  23. /* Bitmap to store allocated device numbers from 0 to IRRCV_NUM_DEVICES - 1 */
  24. #define IRRCV_NUM_DEVICES 256
  25. static DECLARE_BITMAP(ir_core_dev_number, IRRCV_NUM_DEVICES);
  26. /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
  27. #define IR_TAB_MIN_SIZE 256
  28. #define IR_TAB_MAX_SIZE 8192
  29. /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
  30. #define IR_KEYPRESS_TIMEOUT 250
  31. /* Used to keep track of known keymaps */
  32. static LIST_HEAD(rc_map_list);
  33. static DEFINE_SPINLOCK(rc_map_lock);
  34. static struct led_trigger *led_feedback;
  35. static struct rc_map_list *seek_rc_map(const char *name)
  36. {
  37. struct rc_map_list *map = NULL;
  38. spin_lock(&rc_map_lock);
  39. list_for_each_entry(map, &rc_map_list, list) {
  40. if (!strcmp(name, map->map.name)) {
  41. spin_unlock(&rc_map_lock);
  42. return map;
  43. }
  44. }
  45. spin_unlock(&rc_map_lock);
  46. return NULL;
  47. }
  48. struct rc_map *rc_map_get(const char *name)
  49. {
  50. struct rc_map_list *map;
  51. map = seek_rc_map(name);
  52. #ifdef MODULE
  53. if (!map) {
  54. int rc = request_module("%s", name);
  55. if (rc < 0) {
  56. printk(KERN_ERR "Couldn't load IR keymap %s\n", name);
  57. return NULL;
  58. }
  59. msleep(20); /* Give some time for IR to register */
  60. map = seek_rc_map(name);
  61. }
  62. #endif
  63. if (!map) {
  64. printk(KERN_ERR "IR keymap %s not found\n", name);
  65. return NULL;
  66. }
  67. printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
  68. return &map->map;
  69. }
  70. EXPORT_SYMBOL_GPL(rc_map_get);
  71. int rc_map_register(struct rc_map_list *map)
  72. {
  73. spin_lock(&rc_map_lock);
  74. list_add_tail(&map->list, &rc_map_list);
  75. spin_unlock(&rc_map_lock);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_GPL(rc_map_register);
  79. void rc_map_unregister(struct rc_map_list *map)
  80. {
  81. spin_lock(&rc_map_lock);
  82. list_del(&map->list);
  83. spin_unlock(&rc_map_lock);
  84. }
  85. EXPORT_SYMBOL_GPL(rc_map_unregister);
  86. static struct rc_map_table empty[] = {
  87. { 0x2a, KEY_COFFEE },
  88. };
  89. static struct rc_map_list empty_map = {
  90. .map = {
  91. .scan = empty,
  92. .size = ARRAY_SIZE(empty),
  93. .rc_type = RC_TYPE_UNKNOWN, /* Legacy IR type */
  94. .name = RC_MAP_EMPTY,
  95. }
  96. };
  97. /**
  98. * ir_create_table() - initializes a scancode table
  99. * @rc_map: the rc_map to initialize
  100. * @name: name to assign to the table
  101. * @rc_type: ir type to assign to the new table
  102. * @size: initial size of the table
  103. * @return: zero on success or a negative error code
  104. *
  105. * This routine will initialize the rc_map and will allocate
  106. * memory to hold at least the specified number of elements.
  107. */
  108. static int ir_create_table(struct rc_map *rc_map,
  109. const char *name, u64 rc_type, size_t size)
  110. {
  111. rc_map->name = name;
  112. rc_map->rc_type = rc_type;
  113. rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
  114. rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
  115. rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
  116. if (!rc_map->scan)
  117. return -ENOMEM;
  118. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  119. rc_map->size, rc_map->alloc);
  120. return 0;
  121. }
  122. /**
  123. * ir_free_table() - frees memory allocated by a scancode table
  124. * @rc_map: the table whose mappings need to be freed
  125. *
  126. * This routine will free memory alloctaed for key mappings used by given
  127. * scancode table.
  128. */
  129. static void ir_free_table(struct rc_map *rc_map)
  130. {
  131. rc_map->size = 0;
  132. kfree(rc_map->scan);
  133. rc_map->scan = NULL;
  134. }
  135. /**
  136. * ir_resize_table() - resizes a scancode table if necessary
  137. * @rc_map: the rc_map to resize
  138. * @gfp_flags: gfp flags to use when allocating memory
  139. * @return: zero on success or a negative error code
  140. *
  141. * This routine will shrink the rc_map if it has lots of
  142. * unused entries and grow it if it is full.
  143. */
  144. static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
  145. {
  146. unsigned int oldalloc = rc_map->alloc;
  147. unsigned int newalloc = oldalloc;
  148. struct rc_map_table *oldscan = rc_map->scan;
  149. struct rc_map_table *newscan;
  150. if (rc_map->size == rc_map->len) {
  151. /* All entries in use -> grow keytable */
  152. if (rc_map->alloc >= IR_TAB_MAX_SIZE)
  153. return -ENOMEM;
  154. newalloc *= 2;
  155. IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
  156. }
  157. if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
  158. /* Less than 1/3 of entries in use -> shrink keytable */
  159. newalloc /= 2;
  160. IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
  161. }
  162. if (newalloc == oldalloc)
  163. return 0;
  164. newscan = kmalloc(newalloc, gfp_flags);
  165. if (!newscan) {
  166. IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
  167. return -ENOMEM;
  168. }
  169. memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
  170. rc_map->scan = newscan;
  171. rc_map->alloc = newalloc;
  172. rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
  173. kfree(oldscan);
  174. return 0;
  175. }
  176. /**
  177. * ir_update_mapping() - set a keycode in the scancode->keycode table
  178. * @dev: the struct rc_dev device descriptor
  179. * @rc_map: scancode table to be adjusted
  180. * @index: index of the mapping that needs to be updated
  181. * @keycode: the desired keycode
  182. * @return: previous keycode assigned to the mapping
  183. *
  184. * This routine is used to update scancode->keycode mapping at given
  185. * position.
  186. */
  187. static unsigned int ir_update_mapping(struct rc_dev *dev,
  188. struct rc_map *rc_map,
  189. unsigned int index,
  190. unsigned int new_keycode)
  191. {
  192. int old_keycode = rc_map->scan[index].keycode;
  193. int i;
  194. /* Did the user wish to remove the mapping? */
  195. if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
  196. IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
  197. index, rc_map->scan[index].scancode);
  198. rc_map->len--;
  199. memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
  200. (rc_map->len - index) * sizeof(struct rc_map_table));
  201. } else {
  202. IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
  203. index,
  204. old_keycode == KEY_RESERVED ? "New" : "Replacing",
  205. rc_map->scan[index].scancode, new_keycode);
  206. rc_map->scan[index].keycode = new_keycode;
  207. __set_bit(new_keycode, dev->input_dev->keybit);
  208. }
  209. if (old_keycode != KEY_RESERVED) {
  210. /* A previous mapping was updated... */
  211. __clear_bit(old_keycode, dev->input_dev->keybit);
  212. /* ... but another scancode might use the same keycode */
  213. for (i = 0; i < rc_map->len; i++) {
  214. if (rc_map->scan[i].keycode == old_keycode) {
  215. __set_bit(old_keycode, dev->input_dev->keybit);
  216. break;
  217. }
  218. }
  219. /* Possibly shrink the keytable, failure is not a problem */
  220. ir_resize_table(rc_map, GFP_ATOMIC);
  221. }
  222. return old_keycode;
  223. }
  224. /**
  225. * ir_establish_scancode() - set a keycode in the scancode->keycode table
  226. * @dev: the struct rc_dev device descriptor
  227. * @rc_map: scancode table to be searched
  228. * @scancode: the desired scancode
  229. * @resize: controls whether we allowed to resize the table to
  230. * accommodate not yet present scancodes
  231. * @return: index of the mapping containing scancode in question
  232. * or -1U in case of failure.
  233. *
  234. * This routine is used to locate given scancode in rc_map.
  235. * If scancode is not yet present the routine will allocate a new slot
  236. * for it.
  237. */
  238. static unsigned int ir_establish_scancode(struct rc_dev *dev,
  239. struct rc_map *rc_map,
  240. unsigned int scancode,
  241. bool resize)
  242. {
  243. unsigned int i;
  244. /*
  245. * Unfortunately, some hardware-based IR decoders don't provide
  246. * all bits for the complete IR code. In general, they provide only
  247. * the command part of the IR code. Yet, as it is possible to replace
  248. * the provided IR with another one, it is needed to allow loading
  249. * IR tables from other remotes. So, we support specifying a mask to
  250. * indicate the valid bits of the scancodes.
  251. */
  252. if (dev->scancode_mask)
  253. scancode &= dev->scancode_mask;
  254. /* First check if we already have a mapping for this ir command */
  255. for (i = 0; i < rc_map->len; i++) {
  256. if (rc_map->scan[i].scancode == scancode)
  257. return i;
  258. /* Keytable is sorted from lowest to highest scancode */
  259. if (rc_map->scan[i].scancode >= scancode)
  260. break;
  261. }
  262. /* No previous mapping found, we might need to grow the table */
  263. if (rc_map->size == rc_map->len) {
  264. if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
  265. return -1U;
  266. }
  267. /* i is the proper index to insert our new keycode */
  268. if (i < rc_map->len)
  269. memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
  270. (rc_map->len - i) * sizeof(struct rc_map_table));
  271. rc_map->scan[i].scancode = scancode;
  272. rc_map->scan[i].keycode = KEY_RESERVED;
  273. rc_map->len++;
  274. return i;
  275. }
  276. /**
  277. * ir_setkeycode() - set a keycode in the scancode->keycode table
  278. * @idev: the struct input_dev device descriptor
  279. * @scancode: the desired scancode
  280. * @keycode: result
  281. * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
  282. *
  283. * This routine is used to handle evdev EVIOCSKEY ioctl.
  284. */
  285. static int ir_setkeycode(struct input_dev *idev,
  286. const struct input_keymap_entry *ke,
  287. unsigned int *old_keycode)
  288. {
  289. struct rc_dev *rdev = input_get_drvdata(idev);
  290. struct rc_map *rc_map = &rdev->rc_map;
  291. unsigned int index;
  292. unsigned int scancode;
  293. int retval = 0;
  294. unsigned long flags;
  295. spin_lock_irqsave(&rc_map->lock, flags);
  296. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  297. index = ke->index;
  298. if (index >= rc_map->len) {
  299. retval = -EINVAL;
  300. goto out;
  301. }
  302. } else {
  303. retval = input_scancode_to_scalar(ke, &scancode);
  304. if (retval)
  305. goto out;
  306. index = ir_establish_scancode(rdev, rc_map, scancode, true);
  307. if (index >= rc_map->len) {
  308. retval = -ENOMEM;
  309. goto out;
  310. }
  311. }
  312. *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
  313. out:
  314. spin_unlock_irqrestore(&rc_map->lock, flags);
  315. return retval;
  316. }
  317. /**
  318. * ir_setkeytable() - sets several entries in the scancode->keycode table
  319. * @dev: the struct rc_dev device descriptor
  320. * @to: the struct rc_map to copy entries to
  321. * @from: the struct rc_map to copy entries from
  322. * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
  323. *
  324. * This routine is used to handle table initialization.
  325. */
  326. static int ir_setkeytable(struct rc_dev *dev,
  327. const struct rc_map *from)
  328. {
  329. struct rc_map *rc_map = &dev->rc_map;
  330. unsigned int i, index;
  331. int rc;
  332. rc = ir_create_table(rc_map, from->name,
  333. from->rc_type, from->size);
  334. if (rc)
  335. return rc;
  336. IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
  337. rc_map->size, rc_map->alloc);
  338. for (i = 0; i < from->size; i++) {
  339. index = ir_establish_scancode(dev, rc_map,
  340. from->scan[i].scancode, false);
  341. if (index >= rc_map->len) {
  342. rc = -ENOMEM;
  343. break;
  344. }
  345. ir_update_mapping(dev, rc_map, index,
  346. from->scan[i].keycode);
  347. }
  348. if (rc)
  349. ir_free_table(rc_map);
  350. return rc;
  351. }
  352. /**
  353. * ir_lookup_by_scancode() - locate mapping by scancode
  354. * @rc_map: the struct rc_map to search
  355. * @scancode: scancode to look for in the table
  356. * @return: index in the table, -1U if not found
  357. *
  358. * This routine performs binary search in RC keykeymap table for
  359. * given scancode.
  360. */
  361. static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
  362. unsigned int scancode)
  363. {
  364. int start = 0;
  365. int end = rc_map->len - 1;
  366. int mid;
  367. while (start <= end) {
  368. mid = (start + end) / 2;
  369. if (rc_map->scan[mid].scancode < scancode)
  370. start = mid + 1;
  371. else if (rc_map->scan[mid].scancode > scancode)
  372. end = mid - 1;
  373. else
  374. return mid;
  375. }
  376. return -1U;
  377. }
  378. /**
  379. * ir_getkeycode() - get a keycode from the scancode->keycode table
  380. * @idev: the struct input_dev device descriptor
  381. * @scancode: the desired scancode
  382. * @keycode: used to return the keycode, if found, or KEY_RESERVED
  383. * @return: always returns zero.
  384. *
  385. * This routine is used to handle evdev EVIOCGKEY ioctl.
  386. */
  387. static int ir_getkeycode(struct input_dev *idev,
  388. struct input_keymap_entry *ke)
  389. {
  390. struct rc_dev *rdev = input_get_drvdata(idev);
  391. struct rc_map *rc_map = &rdev->rc_map;
  392. struct rc_map_table *entry;
  393. unsigned long flags;
  394. unsigned int index;
  395. unsigned int scancode;
  396. int retval;
  397. spin_lock_irqsave(&rc_map->lock, flags);
  398. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  399. index = ke->index;
  400. } else {
  401. retval = input_scancode_to_scalar(ke, &scancode);
  402. if (retval)
  403. goto out;
  404. index = ir_lookup_by_scancode(rc_map, scancode);
  405. }
  406. if (index < rc_map->len) {
  407. entry = &rc_map->scan[index];
  408. ke->index = index;
  409. ke->keycode = entry->keycode;
  410. ke->len = sizeof(entry->scancode);
  411. memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
  412. } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
  413. /*
  414. * We do not really know the valid range of scancodes
  415. * so let's respond with KEY_RESERVED to anything we
  416. * do not have mapping for [yet].
  417. */
  418. ke->index = index;
  419. ke->keycode = KEY_RESERVED;
  420. } else {
  421. retval = -EINVAL;
  422. goto out;
  423. }
  424. retval = 0;
  425. out:
  426. spin_unlock_irqrestore(&rc_map->lock, flags);
  427. return retval;
  428. }
  429. /**
  430. * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
  431. * @dev: the struct rc_dev descriptor of the device
  432. * @scancode: the scancode to look for
  433. * @return: the corresponding keycode, or KEY_RESERVED
  434. *
  435. * This routine is used by drivers which need to convert a scancode to a
  436. * keycode. Normally it should not be used since drivers should have no
  437. * interest in keycodes.
  438. */
  439. u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
  440. {
  441. struct rc_map *rc_map = &dev->rc_map;
  442. unsigned int keycode;
  443. unsigned int index;
  444. unsigned long flags;
  445. spin_lock_irqsave(&rc_map->lock, flags);
  446. index = ir_lookup_by_scancode(rc_map, scancode);
  447. keycode = index < rc_map->len ?
  448. rc_map->scan[index].keycode : KEY_RESERVED;
  449. spin_unlock_irqrestore(&rc_map->lock, flags);
  450. if (keycode != KEY_RESERVED)
  451. IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
  452. dev->input_name, scancode, keycode);
  453. return keycode;
  454. }
  455. EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
  456. /**
  457. * ir_do_keyup() - internal function to signal the release of a keypress
  458. * @dev: the struct rc_dev descriptor of the device
  459. * @sync: whether or not to call input_sync
  460. *
  461. * This function is used internally to release a keypress, it must be
  462. * called with keylock held.
  463. */
  464. static void ir_do_keyup(struct rc_dev *dev, bool sync)
  465. {
  466. if (!dev->keypressed)
  467. return;
  468. IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
  469. input_report_key(dev->input_dev, dev->last_keycode, 0);
  470. led_trigger_event(led_feedback, LED_OFF);
  471. if (sync)
  472. input_sync(dev->input_dev);
  473. dev->keypressed = false;
  474. }
  475. /**
  476. * rc_keyup() - signals the release of a keypress
  477. * @dev: the struct rc_dev descriptor of the device
  478. *
  479. * This routine is used to signal that a key has been released on the
  480. * remote control.
  481. */
  482. void rc_keyup(struct rc_dev *dev)
  483. {
  484. unsigned long flags;
  485. spin_lock_irqsave(&dev->keylock, flags);
  486. ir_do_keyup(dev, true);
  487. spin_unlock_irqrestore(&dev->keylock, flags);
  488. }
  489. EXPORT_SYMBOL_GPL(rc_keyup);
  490. /**
  491. * ir_timer_keyup() - generates a keyup event after a timeout
  492. * @cookie: a pointer to the struct rc_dev for the device
  493. *
  494. * This routine will generate a keyup event some time after a keydown event
  495. * is generated when no further activity has been detected.
  496. */
  497. static void ir_timer_keyup(unsigned long cookie)
  498. {
  499. struct rc_dev *dev = (struct rc_dev *)cookie;
  500. unsigned long flags;
  501. /*
  502. * ir->keyup_jiffies is used to prevent a race condition if a
  503. * hardware interrupt occurs at this point and the keyup timer
  504. * event is moved further into the future as a result.
  505. *
  506. * The timer will then be reactivated and this function called
  507. * again in the future. We need to exit gracefully in that case
  508. * to allow the input subsystem to do its auto-repeat magic or
  509. * a keyup event might follow immediately after the keydown.
  510. */
  511. spin_lock_irqsave(&dev->keylock, flags);
  512. if (time_is_before_eq_jiffies(dev->keyup_jiffies))
  513. ir_do_keyup(dev, true);
  514. spin_unlock_irqrestore(&dev->keylock, flags);
  515. }
  516. /**
  517. * rc_repeat() - signals that a key is still pressed
  518. * @dev: the struct rc_dev descriptor of the device
  519. *
  520. * This routine is used by IR decoders when a repeat message which does
  521. * not include the necessary bits to reproduce the scancode has been
  522. * received.
  523. */
  524. void rc_repeat(struct rc_dev *dev)
  525. {
  526. unsigned long flags;
  527. spin_lock_irqsave(&dev->keylock, flags);
  528. input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
  529. input_sync(dev->input_dev);
  530. if (!dev->keypressed)
  531. goto out;
  532. dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  533. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  534. out:
  535. spin_unlock_irqrestore(&dev->keylock, flags);
  536. }
  537. EXPORT_SYMBOL_GPL(rc_repeat);
  538. /**
  539. * ir_do_keydown() - internal function to process a keypress
  540. * @dev: the struct rc_dev descriptor of the device
  541. * @protocol: the protocol of the keypress
  542. * @scancode: the scancode of the keypress
  543. * @keycode: the keycode of the keypress
  544. * @toggle: the toggle value of the keypress
  545. *
  546. * This function is used internally to register a keypress, it must be
  547. * called with keylock held.
  548. */
  549. static void ir_do_keydown(struct rc_dev *dev, enum rc_type protocol,
  550. u32 scancode, u32 keycode, u8 toggle)
  551. {
  552. bool new_event = (!dev->keypressed ||
  553. dev->last_protocol != protocol ||
  554. dev->last_scancode != scancode ||
  555. dev->last_toggle != toggle);
  556. if (new_event && dev->keypressed)
  557. ir_do_keyup(dev, false);
  558. input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
  559. if (new_event && keycode != KEY_RESERVED) {
  560. /* Register a keypress */
  561. dev->keypressed = true;
  562. dev->last_protocol = protocol;
  563. dev->last_scancode = scancode;
  564. dev->last_toggle = toggle;
  565. dev->last_keycode = keycode;
  566. IR_dprintk(1, "%s: key down event, "
  567. "key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
  568. dev->input_name, keycode, protocol, scancode);
  569. input_report_key(dev->input_dev, keycode, 1);
  570. led_trigger_event(led_feedback, LED_FULL);
  571. }
  572. input_sync(dev->input_dev);
  573. }
  574. /**
  575. * rc_keydown() - generates input event for a key press
  576. * @dev: the struct rc_dev descriptor of the device
  577. * @protocol: the protocol for the keypress
  578. * @scancode: the scancode for the keypress
  579. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  580. * support toggle values, this should be set to zero)
  581. *
  582. * This routine is used to signal that a key has been pressed on the
  583. * remote control.
  584. */
  585. void rc_keydown(struct rc_dev *dev, enum rc_type protocol, u32 scancode, u8 toggle)
  586. {
  587. unsigned long flags;
  588. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  589. spin_lock_irqsave(&dev->keylock, flags);
  590. ir_do_keydown(dev, protocol, scancode, keycode, toggle);
  591. if (dev->keypressed) {
  592. dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
  593. mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
  594. }
  595. spin_unlock_irqrestore(&dev->keylock, flags);
  596. }
  597. EXPORT_SYMBOL_GPL(rc_keydown);
  598. /**
  599. * rc_keydown_notimeout() - generates input event for a key press without
  600. * an automatic keyup event at a later time
  601. * @dev: the struct rc_dev descriptor of the device
  602. * @protocol: the protocol for the keypress
  603. * @scancode: the scancode for the keypress
  604. * @toggle: the toggle value (protocol dependent, if the protocol doesn't
  605. * support toggle values, this should be set to zero)
  606. *
  607. * This routine is used to signal that a key has been pressed on the
  608. * remote control. The driver must manually call rc_keyup() at a later stage.
  609. */
  610. void rc_keydown_notimeout(struct rc_dev *dev, enum rc_type protocol,
  611. u32 scancode, u8 toggle)
  612. {
  613. unsigned long flags;
  614. u32 keycode = rc_g_keycode_from_table(dev, scancode);
  615. spin_lock_irqsave(&dev->keylock, flags);
  616. ir_do_keydown(dev, protocol, scancode, keycode, toggle);
  617. spin_unlock_irqrestore(&dev->keylock, flags);
  618. }
  619. EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
  620. int rc_open(struct rc_dev *rdev)
  621. {
  622. int rval = 0;
  623. if (!rdev)
  624. return -EINVAL;
  625. mutex_lock(&rdev->lock);
  626. if (!rdev->users++ && rdev->open != NULL)
  627. rval = rdev->open(rdev);
  628. if (rval)
  629. rdev->users--;
  630. mutex_unlock(&rdev->lock);
  631. return rval;
  632. }
  633. EXPORT_SYMBOL_GPL(rc_open);
  634. static int ir_open(struct input_dev *idev)
  635. {
  636. struct rc_dev *rdev = input_get_drvdata(idev);
  637. return rc_open(rdev);
  638. }
  639. void rc_close(struct rc_dev *rdev)
  640. {
  641. if (rdev) {
  642. mutex_lock(&rdev->lock);
  643. if (!--rdev->users && rdev->close != NULL)
  644. rdev->close(rdev);
  645. mutex_unlock(&rdev->lock);
  646. }
  647. }
  648. EXPORT_SYMBOL_GPL(rc_close);
  649. static void ir_close(struct input_dev *idev)
  650. {
  651. struct rc_dev *rdev = input_get_drvdata(idev);
  652. rc_close(rdev);
  653. }
  654. /* class for /sys/class/rc */
  655. static char *rc_devnode(struct device *dev, umode_t *mode)
  656. {
  657. return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
  658. }
  659. static struct class rc_class = {
  660. .name = "rc",
  661. .devnode = rc_devnode,
  662. };
  663. /*
  664. * These are the protocol textual descriptions that are
  665. * used by the sysfs protocols file. Note that the order
  666. * of the entries is relevant.
  667. */
  668. static struct {
  669. u64 type;
  670. char *name;
  671. } proto_names[] = {
  672. { RC_BIT_NONE, "none" },
  673. { RC_BIT_OTHER, "other" },
  674. { RC_BIT_UNKNOWN, "unknown" },
  675. { RC_BIT_RC5 |
  676. RC_BIT_RC5X, "rc-5" },
  677. { RC_BIT_NEC, "nec" },
  678. { RC_BIT_RC6_0 |
  679. RC_BIT_RC6_6A_20 |
  680. RC_BIT_RC6_6A_24 |
  681. RC_BIT_RC6_6A_32 |
  682. RC_BIT_RC6_MCE, "rc-6" },
  683. { RC_BIT_JVC, "jvc" },
  684. { RC_BIT_SONY12 |
  685. RC_BIT_SONY15 |
  686. RC_BIT_SONY20, "sony" },
  687. { RC_BIT_RC5_SZ, "rc-5-sz" },
  688. { RC_BIT_SANYO, "sanyo" },
  689. { RC_BIT_SHARP, "sharp" },
  690. { RC_BIT_MCE_KBD, "mce_kbd" },
  691. { RC_BIT_LIRC, "lirc" },
  692. { RC_BIT_XMP, "xmp" },
  693. };
  694. /**
  695. * struct rc_filter_attribute - Device attribute relating to a filter type.
  696. * @attr: Device attribute.
  697. * @type: Filter type.
  698. * @mask: false for filter value, true for filter mask.
  699. */
  700. struct rc_filter_attribute {
  701. struct device_attribute attr;
  702. enum rc_filter_type type;
  703. bool mask;
  704. };
  705. #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
  706. #define RC_PROTO_ATTR(_name, _mode, _show, _store, _type) \
  707. struct rc_filter_attribute dev_attr_##_name = { \
  708. .attr = __ATTR(_name, _mode, _show, _store), \
  709. .type = (_type), \
  710. }
  711. #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
  712. struct rc_filter_attribute dev_attr_##_name = { \
  713. .attr = __ATTR(_name, _mode, _show, _store), \
  714. .type = (_type), \
  715. .mask = (_mask), \
  716. }
  717. /**
  718. * show_protocols() - shows the current/wakeup IR protocol(s)
  719. * @device: the device descriptor
  720. * @mattr: the device attribute struct
  721. * @buf: a pointer to the output buffer
  722. *
  723. * This routine is a callback routine for input read the IR protocol type(s).
  724. * it is trigged by reading /sys/class/rc/rc?/[wakeup_]protocols.
  725. * It returns the protocol names of supported protocols.
  726. * Enabled protocols are printed in brackets.
  727. *
  728. * dev->lock is taken to guard against races between device
  729. * registration, store_protocols and show_protocols.
  730. */
  731. static ssize_t show_protocols(struct device *device,
  732. struct device_attribute *mattr, char *buf)
  733. {
  734. struct rc_dev *dev = to_rc_dev(device);
  735. struct rc_filter_attribute *fattr = to_rc_filter_attr(mattr);
  736. u64 allowed, enabled;
  737. char *tmp = buf;
  738. int i;
  739. /* Device is being removed */
  740. if (!dev)
  741. return -EINVAL;
  742. mutex_lock(&dev->lock);
  743. if (fattr->type == RC_FILTER_NORMAL) {
  744. enabled = dev->enabled_protocols;
  745. allowed = dev->allowed_protocols;
  746. if (dev->raw && !allowed)
  747. allowed = ir_raw_get_allowed_protocols();
  748. } else {
  749. enabled = dev->enabled_wakeup_protocols;
  750. allowed = dev->allowed_wakeup_protocols;
  751. }
  752. mutex_unlock(&dev->lock);
  753. IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
  754. __func__, (long long)allowed, (long long)enabled);
  755. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  756. if (allowed & enabled & proto_names[i].type)
  757. tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
  758. else if (allowed & proto_names[i].type)
  759. tmp += sprintf(tmp, "%s ", proto_names[i].name);
  760. if (allowed & proto_names[i].type)
  761. allowed &= ~proto_names[i].type;
  762. }
  763. if (tmp != buf)
  764. tmp--;
  765. *tmp = '\n';
  766. return tmp + 1 - buf;
  767. }
  768. /**
  769. * parse_protocol_change() - parses a protocol change request
  770. * @protocols: pointer to the bitmask of current protocols
  771. * @buf: pointer to the buffer with a list of changes
  772. *
  773. * Writing "+proto" will add a protocol to the protocol mask.
  774. * Writing "-proto" will remove a protocol from protocol mask.
  775. * Writing "proto" will enable only "proto".
  776. * Writing "none" will disable all protocols.
  777. * Returns the number of changes performed or a negative error code.
  778. */
  779. static int parse_protocol_change(u64 *protocols, const char *buf)
  780. {
  781. const char *tmp;
  782. unsigned count = 0;
  783. bool enable, disable;
  784. u64 mask;
  785. int i;
  786. while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
  787. if (!*tmp)
  788. break;
  789. if (*tmp == '+') {
  790. enable = true;
  791. disable = false;
  792. tmp++;
  793. } else if (*tmp == '-') {
  794. enable = false;
  795. disable = true;
  796. tmp++;
  797. } else {
  798. enable = false;
  799. disable = false;
  800. }
  801. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  802. if (!strcasecmp(tmp, proto_names[i].name)) {
  803. mask = proto_names[i].type;
  804. break;
  805. }
  806. }
  807. if (i == ARRAY_SIZE(proto_names)) {
  808. IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
  809. return -EINVAL;
  810. }
  811. count++;
  812. if (enable)
  813. *protocols |= mask;
  814. else if (disable)
  815. *protocols &= ~mask;
  816. else
  817. *protocols = mask;
  818. }
  819. if (!count) {
  820. IR_dprintk(1, "Protocol not specified\n");
  821. return -EINVAL;
  822. }
  823. return count;
  824. }
  825. /**
  826. * store_protocols() - changes the current/wakeup IR protocol(s)
  827. * @device: the device descriptor
  828. * @mattr: the device attribute struct
  829. * @buf: a pointer to the input buffer
  830. * @len: length of the input buffer
  831. *
  832. * This routine is for changing the IR protocol type.
  833. * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
  834. * See parse_protocol_change() for the valid commands.
  835. * Returns @len on success or a negative error code.
  836. *
  837. * dev->lock is taken to guard against races between device
  838. * registration, store_protocols and show_protocols.
  839. */
  840. static ssize_t store_protocols(struct device *device,
  841. struct device_attribute *mattr,
  842. const char *buf, size_t len)
  843. {
  844. struct rc_dev *dev = to_rc_dev(device);
  845. struct rc_filter_attribute *fattr = to_rc_filter_attr(mattr);
  846. u64 *current_protocols;
  847. int (*change_protocol)(struct rc_dev *dev, u64 *rc_type);
  848. struct rc_scancode_filter *filter;
  849. int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
  850. u64 old_protocols, new_protocols;
  851. ssize_t rc;
  852. /* Device is being removed */
  853. if (!dev)
  854. return -EINVAL;
  855. if (fattr->type == RC_FILTER_NORMAL) {
  856. IR_dprintk(1, "Normal protocol change requested\n");
  857. current_protocols = &dev->enabled_protocols;
  858. change_protocol = dev->change_protocol;
  859. filter = &dev->scancode_filter;
  860. set_filter = dev->s_filter;
  861. } else {
  862. IR_dprintk(1, "Wakeup protocol change requested\n");
  863. current_protocols = &dev->enabled_wakeup_protocols;
  864. change_protocol = dev->change_wakeup_protocol;
  865. filter = &dev->scancode_wakeup_filter;
  866. set_filter = dev->s_wakeup_filter;
  867. }
  868. if (!change_protocol) {
  869. IR_dprintk(1, "Protocol switching not supported\n");
  870. return -EINVAL;
  871. }
  872. mutex_lock(&dev->lock);
  873. old_protocols = *current_protocols;
  874. new_protocols = old_protocols;
  875. rc = parse_protocol_change(&new_protocols, buf);
  876. if (rc < 0)
  877. goto out;
  878. rc = change_protocol(dev, &new_protocols);
  879. if (rc < 0) {
  880. IR_dprintk(1, "Error setting protocols to 0x%llx\n",
  881. (long long)new_protocols);
  882. goto out;
  883. }
  884. if (new_protocols != old_protocols) {
  885. *current_protocols = new_protocols;
  886. IR_dprintk(1, "Protocols changed to 0x%llx\n",
  887. (long long)new_protocols);
  888. }
  889. /*
  890. * If a protocol change was attempted the filter may need updating, even
  891. * if the actual protocol mask hasn't changed (since the driver may have
  892. * cleared the filter).
  893. * Try setting the same filter with the new protocol (if any).
  894. * Fall back to clearing the filter.
  895. */
  896. if (set_filter && filter->mask) {
  897. if (new_protocols)
  898. rc = set_filter(dev, filter);
  899. else
  900. rc = -1;
  901. if (rc < 0) {
  902. filter->data = 0;
  903. filter->mask = 0;
  904. set_filter(dev, filter);
  905. }
  906. }
  907. rc = len;
  908. out:
  909. mutex_unlock(&dev->lock);
  910. return rc;
  911. }
  912. /**
  913. * show_filter() - shows the current scancode filter value or mask
  914. * @device: the device descriptor
  915. * @attr: the device attribute struct
  916. * @buf: a pointer to the output buffer
  917. *
  918. * This routine is a callback routine to read a scancode filter value or mask.
  919. * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
  920. * It prints the current scancode filter value or mask of the appropriate filter
  921. * type in hexadecimal into @buf and returns the size of the buffer.
  922. *
  923. * Bits of the filter value corresponding to set bits in the filter mask are
  924. * compared against input scancodes and non-matching scancodes are discarded.
  925. *
  926. * dev->lock is taken to guard against races between device registration,
  927. * store_filter and show_filter.
  928. */
  929. static ssize_t show_filter(struct device *device,
  930. struct device_attribute *attr,
  931. char *buf)
  932. {
  933. struct rc_dev *dev = to_rc_dev(device);
  934. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  935. struct rc_scancode_filter *filter;
  936. u32 val;
  937. /* Device is being removed */
  938. if (!dev)
  939. return -EINVAL;
  940. if (fattr->type == RC_FILTER_NORMAL)
  941. filter = &dev->scancode_filter;
  942. else
  943. filter = &dev->scancode_wakeup_filter;
  944. mutex_lock(&dev->lock);
  945. if (fattr->mask)
  946. val = filter->mask;
  947. else
  948. val = filter->data;
  949. mutex_unlock(&dev->lock);
  950. return sprintf(buf, "%#x\n", val);
  951. }
  952. /**
  953. * store_filter() - changes the scancode filter value
  954. * @device: the device descriptor
  955. * @attr: the device attribute struct
  956. * @buf: a pointer to the input buffer
  957. * @len: length of the input buffer
  958. *
  959. * This routine is for changing a scancode filter value or mask.
  960. * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
  961. * Returns -EINVAL if an invalid filter value for the current protocol was
  962. * specified or if scancode filtering is not supported by the driver, otherwise
  963. * returns @len.
  964. *
  965. * Bits of the filter value corresponding to set bits in the filter mask are
  966. * compared against input scancodes and non-matching scancodes are discarded.
  967. *
  968. * dev->lock is taken to guard against races between device registration,
  969. * store_filter and show_filter.
  970. */
  971. static ssize_t store_filter(struct device *device,
  972. struct device_attribute *attr,
  973. const char *buf, size_t len)
  974. {
  975. struct rc_dev *dev = to_rc_dev(device);
  976. struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
  977. struct rc_scancode_filter new_filter, *filter;
  978. int ret;
  979. unsigned long val;
  980. int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
  981. u64 *enabled_protocols;
  982. /* Device is being removed */
  983. if (!dev)
  984. return -EINVAL;
  985. ret = kstrtoul(buf, 0, &val);
  986. if (ret < 0)
  987. return ret;
  988. if (fattr->type == RC_FILTER_NORMAL) {
  989. set_filter = dev->s_filter;
  990. enabled_protocols = &dev->enabled_protocols;
  991. filter = &dev->scancode_filter;
  992. } else {
  993. set_filter = dev->s_wakeup_filter;
  994. enabled_protocols = &dev->enabled_wakeup_protocols;
  995. filter = &dev->scancode_wakeup_filter;
  996. }
  997. if (!set_filter)
  998. return -EINVAL;
  999. mutex_lock(&dev->lock);
  1000. new_filter = *filter;
  1001. if (fattr->mask)
  1002. new_filter.mask = val;
  1003. else
  1004. new_filter.data = val;
  1005. if (!*enabled_protocols && val) {
  1006. /* refuse to set a filter unless a protocol is enabled */
  1007. ret = -EINVAL;
  1008. goto unlock;
  1009. }
  1010. ret = set_filter(dev, &new_filter);
  1011. if (ret < 0)
  1012. goto unlock;
  1013. *filter = new_filter;
  1014. unlock:
  1015. mutex_unlock(&dev->lock);
  1016. return (ret < 0) ? ret : len;
  1017. }
  1018. static void rc_dev_release(struct device *device)
  1019. {
  1020. }
  1021. #define ADD_HOTPLUG_VAR(fmt, val...) \
  1022. do { \
  1023. int err = add_uevent_var(env, fmt, val); \
  1024. if (err) \
  1025. return err; \
  1026. } while (0)
  1027. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  1028. {
  1029. struct rc_dev *dev = to_rc_dev(device);
  1030. if (!dev || !dev->input_dev)
  1031. return -ENODEV;
  1032. if (dev->rc_map.name)
  1033. ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
  1034. if (dev->driver_name)
  1035. ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
  1036. return 0;
  1037. }
  1038. /*
  1039. * Static device attribute struct with the sysfs attributes for IR's
  1040. */
  1041. static RC_PROTO_ATTR(protocols, S_IRUGO | S_IWUSR,
  1042. show_protocols, store_protocols, RC_FILTER_NORMAL);
  1043. static RC_PROTO_ATTR(wakeup_protocols, S_IRUGO | S_IWUSR,
  1044. show_protocols, store_protocols, RC_FILTER_WAKEUP);
  1045. static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
  1046. show_filter, store_filter, RC_FILTER_NORMAL, false);
  1047. static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
  1048. show_filter, store_filter, RC_FILTER_NORMAL, true);
  1049. static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
  1050. show_filter, store_filter, RC_FILTER_WAKEUP, false);
  1051. static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
  1052. show_filter, store_filter, RC_FILTER_WAKEUP, true);
  1053. static struct attribute *rc_dev_protocol_attrs[] = {
  1054. &dev_attr_protocols.attr.attr,
  1055. NULL,
  1056. };
  1057. static struct attribute_group rc_dev_protocol_attr_grp = {
  1058. .attrs = rc_dev_protocol_attrs,
  1059. };
  1060. static struct attribute *rc_dev_wakeup_protocol_attrs[] = {
  1061. &dev_attr_wakeup_protocols.attr.attr,
  1062. NULL,
  1063. };
  1064. static struct attribute_group rc_dev_wakeup_protocol_attr_grp = {
  1065. .attrs = rc_dev_wakeup_protocol_attrs,
  1066. };
  1067. static struct attribute *rc_dev_filter_attrs[] = {
  1068. &dev_attr_filter.attr.attr,
  1069. &dev_attr_filter_mask.attr.attr,
  1070. NULL,
  1071. };
  1072. static struct attribute_group rc_dev_filter_attr_grp = {
  1073. .attrs = rc_dev_filter_attrs,
  1074. };
  1075. static struct attribute *rc_dev_wakeup_filter_attrs[] = {
  1076. &dev_attr_wakeup_filter.attr.attr,
  1077. &dev_attr_wakeup_filter_mask.attr.attr,
  1078. NULL,
  1079. };
  1080. static struct attribute_group rc_dev_wakeup_filter_attr_grp = {
  1081. .attrs = rc_dev_wakeup_filter_attrs,
  1082. };
  1083. static struct device_type rc_dev_type = {
  1084. .release = rc_dev_release,
  1085. .uevent = rc_dev_uevent,
  1086. };
  1087. struct rc_dev *rc_allocate_device(void)
  1088. {
  1089. struct rc_dev *dev;
  1090. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1091. if (!dev)
  1092. return NULL;
  1093. dev->input_dev = input_allocate_device();
  1094. if (!dev->input_dev) {
  1095. kfree(dev);
  1096. return NULL;
  1097. }
  1098. dev->input_dev->getkeycode = ir_getkeycode;
  1099. dev->input_dev->setkeycode = ir_setkeycode;
  1100. input_set_drvdata(dev->input_dev, dev);
  1101. spin_lock_init(&dev->rc_map.lock);
  1102. spin_lock_init(&dev->keylock);
  1103. mutex_init(&dev->lock);
  1104. setup_timer(&dev->timer_keyup, ir_timer_keyup, (unsigned long)dev);
  1105. dev->dev.type = &rc_dev_type;
  1106. dev->dev.class = &rc_class;
  1107. device_initialize(&dev->dev);
  1108. __module_get(THIS_MODULE);
  1109. return dev;
  1110. }
  1111. EXPORT_SYMBOL_GPL(rc_allocate_device);
  1112. void rc_free_device(struct rc_dev *dev)
  1113. {
  1114. if (!dev)
  1115. return;
  1116. if (dev->input_dev)
  1117. input_free_device(dev->input_dev);
  1118. put_device(&dev->dev);
  1119. kfree(dev);
  1120. module_put(THIS_MODULE);
  1121. }
  1122. EXPORT_SYMBOL_GPL(rc_free_device);
  1123. int rc_register_device(struct rc_dev *dev)
  1124. {
  1125. static bool raw_init = false; /* raw decoders loaded? */
  1126. struct rc_map *rc_map;
  1127. const char *path;
  1128. int rc, devno, attr = 0;
  1129. if (!dev || !dev->map_name)
  1130. return -EINVAL;
  1131. rc_map = rc_map_get(dev->map_name);
  1132. if (!rc_map)
  1133. rc_map = rc_map_get(RC_MAP_EMPTY);
  1134. if (!rc_map || !rc_map->scan || rc_map->size == 0)
  1135. return -EINVAL;
  1136. set_bit(EV_KEY, dev->input_dev->evbit);
  1137. set_bit(EV_REP, dev->input_dev->evbit);
  1138. set_bit(EV_MSC, dev->input_dev->evbit);
  1139. set_bit(MSC_SCAN, dev->input_dev->mscbit);
  1140. if (dev->open)
  1141. dev->input_dev->open = ir_open;
  1142. if (dev->close)
  1143. dev->input_dev->close = ir_close;
  1144. do {
  1145. devno = find_first_zero_bit(ir_core_dev_number,
  1146. IRRCV_NUM_DEVICES);
  1147. /* No free device slots */
  1148. if (devno >= IRRCV_NUM_DEVICES)
  1149. return -ENOMEM;
  1150. } while (test_and_set_bit(devno, ir_core_dev_number));
  1151. dev->dev.groups = dev->sysfs_groups;
  1152. dev->sysfs_groups[attr++] = &rc_dev_protocol_attr_grp;
  1153. if (dev->s_filter)
  1154. dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
  1155. if (dev->s_wakeup_filter)
  1156. dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
  1157. if (dev->change_wakeup_protocol)
  1158. dev->sysfs_groups[attr++] = &rc_dev_wakeup_protocol_attr_grp;
  1159. dev->sysfs_groups[attr++] = NULL;
  1160. /*
  1161. * Take the lock here, as the device sysfs node will appear
  1162. * when device_add() is called, which may trigger an ir-keytable udev
  1163. * rule, which will in turn call show_protocols and access
  1164. * dev->enabled_protocols before it has been initialized.
  1165. */
  1166. mutex_lock(&dev->lock);
  1167. dev->devno = devno;
  1168. dev_set_name(&dev->dev, "rc%ld", dev->devno);
  1169. dev_set_drvdata(&dev->dev, dev);
  1170. rc = device_add(&dev->dev);
  1171. if (rc)
  1172. goto out_unlock;
  1173. rc = ir_setkeytable(dev, rc_map);
  1174. if (rc)
  1175. goto out_dev;
  1176. dev->input_dev->dev.parent = &dev->dev;
  1177. memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
  1178. dev->input_dev->phys = dev->input_phys;
  1179. dev->input_dev->name = dev->input_name;
  1180. /* input_register_device can call ir_open, so unlock mutex here */
  1181. mutex_unlock(&dev->lock);
  1182. rc = input_register_device(dev->input_dev);
  1183. mutex_lock(&dev->lock);
  1184. if (rc)
  1185. goto out_table;
  1186. /*
  1187. * Default delay of 250ms is too short for some protocols, especially
  1188. * since the timeout is currently set to 250ms. Increase it to 500ms,
  1189. * to avoid wrong repetition of the keycodes. Note that this must be
  1190. * set after the call to input_register_device().
  1191. */
  1192. dev->input_dev->rep[REP_DELAY] = 500;
  1193. /*
  1194. * As a repeat event on protocols like RC-5 and NEC take as long as
  1195. * 110/114ms, using 33ms as a repeat period is not the right thing
  1196. * to do.
  1197. */
  1198. dev->input_dev->rep[REP_PERIOD] = 125;
  1199. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1200. printk(KERN_INFO "%s: %s as %s\n",
  1201. dev_name(&dev->dev),
  1202. dev->input_name ? dev->input_name : "Unspecified device",
  1203. path ? path : "N/A");
  1204. kfree(path);
  1205. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  1206. /* Load raw decoders, if they aren't already */
  1207. if (!raw_init) {
  1208. IR_dprintk(1, "Loading raw decoders\n");
  1209. ir_raw_init();
  1210. raw_init = true;
  1211. }
  1212. rc = ir_raw_event_register(dev);
  1213. if (rc < 0)
  1214. goto out_input;
  1215. }
  1216. if (dev->change_protocol) {
  1217. u64 rc_type = (1 << rc_map->rc_type);
  1218. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1219. rc_type |= RC_BIT_LIRC;
  1220. rc = dev->change_protocol(dev, &rc_type);
  1221. if (rc < 0)
  1222. goto out_raw;
  1223. dev->enabled_protocols = rc_type;
  1224. }
  1225. mutex_unlock(&dev->lock);
  1226. IR_dprintk(1, "Registered rc%ld (driver: %s, remote: %s, mode %s)\n",
  1227. dev->devno,
  1228. dev->driver_name ? dev->driver_name : "unknown",
  1229. rc_map->name ? rc_map->name : "unknown",
  1230. dev->driver_type == RC_DRIVER_IR_RAW ? "raw" : "cooked");
  1231. return 0;
  1232. out_raw:
  1233. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1234. ir_raw_event_unregister(dev);
  1235. out_input:
  1236. input_unregister_device(dev->input_dev);
  1237. dev->input_dev = NULL;
  1238. out_table:
  1239. ir_free_table(&dev->rc_map);
  1240. out_dev:
  1241. device_del(&dev->dev);
  1242. out_unlock:
  1243. mutex_unlock(&dev->lock);
  1244. clear_bit(dev->devno, ir_core_dev_number);
  1245. return rc;
  1246. }
  1247. EXPORT_SYMBOL_GPL(rc_register_device);
  1248. void rc_unregister_device(struct rc_dev *dev)
  1249. {
  1250. if (!dev)
  1251. return;
  1252. del_timer_sync(&dev->timer_keyup);
  1253. clear_bit(dev->devno, ir_core_dev_number);
  1254. if (dev->driver_type == RC_DRIVER_IR_RAW)
  1255. ir_raw_event_unregister(dev);
  1256. /* Freeing the table should also call the stop callback */
  1257. ir_free_table(&dev->rc_map);
  1258. IR_dprintk(1, "Freed keycode table\n");
  1259. input_unregister_device(dev->input_dev);
  1260. dev->input_dev = NULL;
  1261. device_del(&dev->dev);
  1262. rc_free_device(dev);
  1263. }
  1264. EXPORT_SYMBOL_GPL(rc_unregister_device);
  1265. /*
  1266. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  1267. */
  1268. static int __init rc_core_init(void)
  1269. {
  1270. int rc = class_register(&rc_class);
  1271. if (rc) {
  1272. printk(KERN_ERR "rc_core: unable to register rc class\n");
  1273. return rc;
  1274. }
  1275. led_trigger_register_simple("rc-feedback", &led_feedback);
  1276. rc_map_register(&empty_map);
  1277. return 0;
  1278. }
  1279. static void __exit rc_core_exit(void)
  1280. {
  1281. class_unregister(&rc_class);
  1282. led_trigger_unregister_simple(led_feedback);
  1283. rc_map_unregister(&empty_map);
  1284. }
  1285. subsys_initcall(rc_core_init);
  1286. module_exit(rc_core_exit);
  1287. int rc_core_debug; /* ir_debug level (0,1,2) */
  1288. EXPORT_SYMBOL_GPL(rc_core_debug);
  1289. module_param_named(debug, rc_core_debug, int, 0644);
  1290. MODULE_AUTHOR("Mauro Carvalho Chehab");
  1291. MODULE_LICENSE("GPL");