rc-main.c 39 KB

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