rc-main.c 40 KB

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