rc-main.c 32 KB

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