rc-main.c 41 KB

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