rc-main.c 31 KB

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