rc-main.c 31 KB

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