input.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  1. /*
  2. * The input core
  3. *
  4. * Copyright (c) 1999-2002 Vojtech Pavlik
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <linux/input.h>
  14. #include <linux/module.h>
  15. #include <linux/random.h>
  16. #include <linux/major.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/poll.h>
  20. #include <linux/device.h>
  21. #include <linux/mutex.h>
  22. #include <linux/rcupdate.h>
  23. #include <linux/smp_lock.h>
  24. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  25. MODULE_DESCRIPTION("Input core");
  26. MODULE_LICENSE("GPL");
  27. #define INPUT_DEVICES 256
  28. /*
  29. * EV_ABS events which should not be cached are listed here.
  30. */
  31. static unsigned int input_abs_bypass_init_data[] __initdata = {
  32. ABS_MT_TOUCH_MAJOR,
  33. ABS_MT_TOUCH_MINOR,
  34. ABS_MT_WIDTH_MAJOR,
  35. ABS_MT_WIDTH_MINOR,
  36. ABS_MT_ORIENTATION,
  37. ABS_MT_POSITION_X,
  38. ABS_MT_POSITION_Y,
  39. ABS_MT_TOOL_TYPE,
  40. ABS_MT_BLOB_ID,
  41. ABS_MT_TRACKING_ID,
  42. 0
  43. };
  44. static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
  45. static LIST_HEAD(input_dev_list);
  46. static LIST_HEAD(input_handler_list);
  47. /*
  48. * input_mutex protects access to both input_dev_list and input_handler_list.
  49. * This also causes input_[un]register_device and input_[un]register_handler
  50. * be mutually exclusive which simplifies locking in drivers implementing
  51. * input handlers.
  52. */
  53. static DEFINE_MUTEX(input_mutex);
  54. static struct input_handler *input_table[8];
  55. static inline int is_event_supported(unsigned int code,
  56. unsigned long *bm, unsigned int max)
  57. {
  58. return code <= max && test_bit(code, bm);
  59. }
  60. static int input_defuzz_abs_event(int value, int old_val, int fuzz)
  61. {
  62. if (fuzz) {
  63. if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
  64. return old_val;
  65. if (value > old_val - fuzz && value < old_val + fuzz)
  66. return (old_val * 3 + value) / 4;
  67. if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
  68. return (old_val + value) / 2;
  69. }
  70. return value;
  71. }
  72. /*
  73. * Pass event through all open handles. This function is called with
  74. * dev->event_lock held and interrupts disabled.
  75. */
  76. static void input_pass_event(struct input_dev *dev,
  77. unsigned int type, unsigned int code, int value)
  78. {
  79. struct input_handle *handle;
  80. rcu_read_lock();
  81. handle = rcu_dereference(dev->grab);
  82. if (handle)
  83. handle->handler->event(handle, type, code, value);
  84. else
  85. list_for_each_entry_rcu(handle, &dev->h_list, d_node)
  86. if (handle->open)
  87. handle->handler->event(handle,
  88. type, code, value);
  89. rcu_read_unlock();
  90. }
  91. /*
  92. * Generate software autorepeat event. Note that we take
  93. * dev->event_lock here to avoid racing with input_event
  94. * which may cause keys get "stuck".
  95. */
  96. static void input_repeat_key(unsigned long data)
  97. {
  98. struct input_dev *dev = (void *) data;
  99. unsigned long flags;
  100. spin_lock_irqsave(&dev->event_lock, flags);
  101. if (test_bit(dev->repeat_key, dev->key) &&
  102. is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
  103. input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
  104. if (dev->sync) {
  105. /*
  106. * Only send SYN_REPORT if we are not in a middle
  107. * of driver parsing a new hardware packet.
  108. * Otherwise assume that the driver will send
  109. * SYN_REPORT once it's done.
  110. */
  111. input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
  112. }
  113. if (dev->rep[REP_PERIOD])
  114. mod_timer(&dev->timer, jiffies +
  115. msecs_to_jiffies(dev->rep[REP_PERIOD]));
  116. }
  117. spin_unlock_irqrestore(&dev->event_lock, flags);
  118. }
  119. static void input_start_autorepeat(struct input_dev *dev, int code)
  120. {
  121. if (test_bit(EV_REP, dev->evbit) &&
  122. dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
  123. dev->timer.data) {
  124. dev->repeat_key = code;
  125. mod_timer(&dev->timer,
  126. jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
  127. }
  128. }
  129. static void input_stop_autorepeat(struct input_dev *dev)
  130. {
  131. del_timer(&dev->timer);
  132. }
  133. #define INPUT_IGNORE_EVENT 0
  134. #define INPUT_PASS_TO_HANDLERS 1
  135. #define INPUT_PASS_TO_DEVICE 2
  136. #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
  137. static void input_handle_event(struct input_dev *dev,
  138. unsigned int type, unsigned int code, int value)
  139. {
  140. int disposition = INPUT_IGNORE_EVENT;
  141. switch (type) {
  142. case EV_SYN:
  143. switch (code) {
  144. case SYN_CONFIG:
  145. disposition = INPUT_PASS_TO_ALL;
  146. break;
  147. case SYN_REPORT:
  148. if (!dev->sync) {
  149. dev->sync = 1;
  150. disposition = INPUT_PASS_TO_HANDLERS;
  151. }
  152. break;
  153. case SYN_MT_REPORT:
  154. dev->sync = 0;
  155. disposition = INPUT_PASS_TO_HANDLERS;
  156. break;
  157. }
  158. break;
  159. case EV_KEY:
  160. if (is_event_supported(code, dev->keybit, KEY_MAX) &&
  161. !!test_bit(code, dev->key) != value) {
  162. if (value != 2) {
  163. __change_bit(code, dev->key);
  164. if (value)
  165. input_start_autorepeat(dev, code);
  166. else
  167. input_stop_autorepeat(dev);
  168. }
  169. disposition = INPUT_PASS_TO_HANDLERS;
  170. }
  171. break;
  172. case EV_SW:
  173. if (is_event_supported(code, dev->swbit, SW_MAX) &&
  174. !!test_bit(code, dev->sw) != value) {
  175. __change_bit(code, dev->sw);
  176. disposition = INPUT_PASS_TO_HANDLERS;
  177. }
  178. break;
  179. case EV_ABS:
  180. if (is_event_supported(code, dev->absbit, ABS_MAX)) {
  181. if (test_bit(code, input_abs_bypass)) {
  182. disposition = INPUT_PASS_TO_HANDLERS;
  183. break;
  184. }
  185. value = input_defuzz_abs_event(value,
  186. dev->abs[code], dev->absfuzz[code]);
  187. if (dev->abs[code] != value) {
  188. dev->abs[code] = value;
  189. disposition = INPUT_PASS_TO_HANDLERS;
  190. }
  191. }
  192. break;
  193. case EV_REL:
  194. if (is_event_supported(code, dev->relbit, REL_MAX) && value)
  195. disposition = INPUT_PASS_TO_HANDLERS;
  196. break;
  197. case EV_MSC:
  198. if (is_event_supported(code, dev->mscbit, MSC_MAX))
  199. disposition = INPUT_PASS_TO_ALL;
  200. break;
  201. case EV_LED:
  202. if (is_event_supported(code, dev->ledbit, LED_MAX) &&
  203. !!test_bit(code, dev->led) != value) {
  204. __change_bit(code, dev->led);
  205. disposition = INPUT_PASS_TO_ALL;
  206. }
  207. break;
  208. case EV_SND:
  209. if (is_event_supported(code, dev->sndbit, SND_MAX)) {
  210. if (!!test_bit(code, dev->snd) != !!value)
  211. __change_bit(code, dev->snd);
  212. disposition = INPUT_PASS_TO_ALL;
  213. }
  214. break;
  215. case EV_REP:
  216. if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
  217. dev->rep[code] = value;
  218. disposition = INPUT_PASS_TO_ALL;
  219. }
  220. break;
  221. case EV_FF:
  222. if (value >= 0)
  223. disposition = INPUT_PASS_TO_ALL;
  224. break;
  225. case EV_PWR:
  226. disposition = INPUT_PASS_TO_ALL;
  227. break;
  228. }
  229. if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
  230. dev->sync = 0;
  231. if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
  232. dev->event(dev, type, code, value);
  233. if (disposition & INPUT_PASS_TO_HANDLERS)
  234. input_pass_event(dev, type, code, value);
  235. }
  236. /**
  237. * input_event() - report new input event
  238. * @dev: device that generated the event
  239. * @type: type of the event
  240. * @code: event code
  241. * @value: value of the event
  242. *
  243. * This function should be used by drivers implementing various input
  244. * devices. See also input_inject_event().
  245. */
  246. void input_event(struct input_dev *dev,
  247. unsigned int type, unsigned int code, int value)
  248. {
  249. unsigned long flags;
  250. if (is_event_supported(type, dev->evbit, EV_MAX)) {
  251. spin_lock_irqsave(&dev->event_lock, flags);
  252. add_input_randomness(type, code, value);
  253. input_handle_event(dev, type, code, value);
  254. spin_unlock_irqrestore(&dev->event_lock, flags);
  255. }
  256. }
  257. EXPORT_SYMBOL(input_event);
  258. /**
  259. * input_inject_event() - send input event from input handler
  260. * @handle: input handle to send event through
  261. * @type: type of the event
  262. * @code: event code
  263. * @value: value of the event
  264. *
  265. * Similar to input_event() but will ignore event if device is
  266. * "grabbed" and handle injecting event is not the one that owns
  267. * the device.
  268. */
  269. void input_inject_event(struct input_handle *handle,
  270. unsigned int type, unsigned int code, int value)
  271. {
  272. struct input_dev *dev = handle->dev;
  273. struct input_handle *grab;
  274. unsigned long flags;
  275. if (is_event_supported(type, dev->evbit, EV_MAX)) {
  276. spin_lock_irqsave(&dev->event_lock, flags);
  277. rcu_read_lock();
  278. grab = rcu_dereference(dev->grab);
  279. if (!grab || grab == handle)
  280. input_handle_event(dev, type, code, value);
  281. rcu_read_unlock();
  282. spin_unlock_irqrestore(&dev->event_lock, flags);
  283. }
  284. }
  285. EXPORT_SYMBOL(input_inject_event);
  286. /**
  287. * input_grab_device - grabs device for exclusive use
  288. * @handle: input handle that wants to own the device
  289. *
  290. * When a device is grabbed by an input handle all events generated by
  291. * the device are delivered only to this handle. Also events injected
  292. * by other input handles are ignored while device is grabbed.
  293. */
  294. int input_grab_device(struct input_handle *handle)
  295. {
  296. struct input_dev *dev = handle->dev;
  297. int retval;
  298. retval = mutex_lock_interruptible(&dev->mutex);
  299. if (retval)
  300. return retval;
  301. if (dev->grab) {
  302. retval = -EBUSY;
  303. goto out;
  304. }
  305. rcu_assign_pointer(dev->grab, handle);
  306. synchronize_rcu();
  307. out:
  308. mutex_unlock(&dev->mutex);
  309. return retval;
  310. }
  311. EXPORT_SYMBOL(input_grab_device);
  312. static void __input_release_device(struct input_handle *handle)
  313. {
  314. struct input_dev *dev = handle->dev;
  315. if (dev->grab == handle) {
  316. rcu_assign_pointer(dev->grab, NULL);
  317. /* Make sure input_pass_event() notices that grab is gone */
  318. synchronize_rcu();
  319. list_for_each_entry(handle, &dev->h_list, d_node)
  320. if (handle->open && handle->handler->start)
  321. handle->handler->start(handle);
  322. }
  323. }
  324. /**
  325. * input_release_device - release previously grabbed device
  326. * @handle: input handle that owns the device
  327. *
  328. * Releases previously grabbed device so that other input handles can
  329. * start receiving input events. Upon release all handlers attached
  330. * to the device have their start() method called so they have a change
  331. * to synchronize device state with the rest of the system.
  332. */
  333. void input_release_device(struct input_handle *handle)
  334. {
  335. struct input_dev *dev = handle->dev;
  336. mutex_lock(&dev->mutex);
  337. __input_release_device(handle);
  338. mutex_unlock(&dev->mutex);
  339. }
  340. EXPORT_SYMBOL(input_release_device);
  341. /**
  342. * input_open_device - open input device
  343. * @handle: handle through which device is being accessed
  344. *
  345. * This function should be called by input handlers when they
  346. * want to start receive events from given input device.
  347. */
  348. int input_open_device(struct input_handle *handle)
  349. {
  350. struct input_dev *dev = handle->dev;
  351. int retval;
  352. retval = mutex_lock_interruptible(&dev->mutex);
  353. if (retval)
  354. return retval;
  355. if (dev->going_away) {
  356. retval = -ENODEV;
  357. goto out;
  358. }
  359. handle->open++;
  360. if (!dev->users++ && dev->open)
  361. retval = dev->open(dev);
  362. if (retval) {
  363. dev->users--;
  364. if (!--handle->open) {
  365. /*
  366. * Make sure we are not delivering any more events
  367. * through this handle
  368. */
  369. synchronize_rcu();
  370. }
  371. }
  372. out:
  373. mutex_unlock(&dev->mutex);
  374. return retval;
  375. }
  376. EXPORT_SYMBOL(input_open_device);
  377. int input_flush_device(struct input_handle *handle, struct file *file)
  378. {
  379. struct input_dev *dev = handle->dev;
  380. int retval;
  381. retval = mutex_lock_interruptible(&dev->mutex);
  382. if (retval)
  383. return retval;
  384. if (dev->flush)
  385. retval = dev->flush(dev, file);
  386. mutex_unlock(&dev->mutex);
  387. return retval;
  388. }
  389. EXPORT_SYMBOL(input_flush_device);
  390. /**
  391. * input_close_device - close input device
  392. * @handle: handle through which device is being accessed
  393. *
  394. * This function should be called by input handlers when they
  395. * want to stop receive events from given input device.
  396. */
  397. void input_close_device(struct input_handle *handle)
  398. {
  399. struct input_dev *dev = handle->dev;
  400. mutex_lock(&dev->mutex);
  401. __input_release_device(handle);
  402. if (!--dev->users && dev->close)
  403. dev->close(dev);
  404. if (!--handle->open) {
  405. /*
  406. * synchronize_rcu() makes sure that input_pass_event()
  407. * completed and that no more input events are delivered
  408. * through this handle
  409. */
  410. synchronize_rcu();
  411. }
  412. mutex_unlock(&dev->mutex);
  413. }
  414. EXPORT_SYMBOL(input_close_device);
  415. /*
  416. * Prepare device for unregistering
  417. */
  418. static void input_disconnect_device(struct input_dev *dev)
  419. {
  420. struct input_handle *handle;
  421. int code;
  422. /*
  423. * Mark device as going away. Note that we take dev->mutex here
  424. * not to protect access to dev->going_away but rather to ensure
  425. * that there are no threads in the middle of input_open_device()
  426. */
  427. mutex_lock(&dev->mutex);
  428. dev->going_away = true;
  429. mutex_unlock(&dev->mutex);
  430. spin_lock_irq(&dev->event_lock);
  431. /*
  432. * Simulate keyup events for all pressed keys so that handlers
  433. * are not left with "stuck" keys. The driver may continue
  434. * generate events even after we done here but they will not
  435. * reach any handlers.
  436. */
  437. if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
  438. for (code = 0; code <= KEY_MAX; code++) {
  439. if (is_event_supported(code, dev->keybit, KEY_MAX) &&
  440. __test_and_clear_bit(code, dev->key)) {
  441. input_pass_event(dev, EV_KEY, code, 0);
  442. }
  443. }
  444. input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
  445. }
  446. list_for_each_entry(handle, &dev->h_list, d_node)
  447. handle->open = 0;
  448. spin_unlock_irq(&dev->event_lock);
  449. }
  450. static int input_fetch_keycode(struct input_dev *dev, int scancode)
  451. {
  452. switch (dev->keycodesize) {
  453. case 1:
  454. return ((u8 *)dev->keycode)[scancode];
  455. case 2:
  456. return ((u16 *)dev->keycode)[scancode];
  457. default:
  458. return ((u32 *)dev->keycode)[scancode];
  459. }
  460. }
  461. static int input_default_getkeycode(struct input_dev *dev,
  462. int scancode, int *keycode)
  463. {
  464. if (!dev->keycodesize)
  465. return -EINVAL;
  466. if (scancode >= dev->keycodemax)
  467. return -EINVAL;
  468. *keycode = input_fetch_keycode(dev, scancode);
  469. return 0;
  470. }
  471. static int input_default_setkeycode(struct input_dev *dev,
  472. int scancode, int keycode)
  473. {
  474. int old_keycode;
  475. int i;
  476. if (scancode >= dev->keycodemax)
  477. return -EINVAL;
  478. if (!dev->keycodesize)
  479. return -EINVAL;
  480. if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
  481. return -EINVAL;
  482. switch (dev->keycodesize) {
  483. case 1: {
  484. u8 *k = (u8 *)dev->keycode;
  485. old_keycode = k[scancode];
  486. k[scancode] = keycode;
  487. break;
  488. }
  489. case 2: {
  490. u16 *k = (u16 *)dev->keycode;
  491. old_keycode = k[scancode];
  492. k[scancode] = keycode;
  493. break;
  494. }
  495. default: {
  496. u32 *k = (u32 *)dev->keycode;
  497. old_keycode = k[scancode];
  498. k[scancode] = keycode;
  499. break;
  500. }
  501. }
  502. clear_bit(old_keycode, dev->keybit);
  503. set_bit(keycode, dev->keybit);
  504. for (i = 0; i < dev->keycodemax; i++) {
  505. if (input_fetch_keycode(dev, i) == old_keycode) {
  506. set_bit(old_keycode, dev->keybit);
  507. break; /* Setting the bit twice is useless, so break */
  508. }
  509. }
  510. return 0;
  511. }
  512. /**
  513. * input_get_keycode - retrieve keycode currently mapped to a given scancode
  514. * @dev: input device which keymap is being queried
  515. * @scancode: scancode (or its equivalent for device in question) for which
  516. * keycode is needed
  517. * @keycode: result
  518. *
  519. * This function should be called by anyone interested in retrieving current
  520. * keymap. Presently keyboard and evdev handlers use it.
  521. */
  522. int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
  523. {
  524. if (scancode < 0)
  525. return -EINVAL;
  526. return dev->getkeycode(dev, scancode, keycode);
  527. }
  528. EXPORT_SYMBOL(input_get_keycode);
  529. /**
  530. * input_get_keycode - assign new keycode to a given scancode
  531. * @dev: input device which keymap is being updated
  532. * @scancode: scancode (or its equivalent for device in question)
  533. * @keycode: new keycode to be assigned to the scancode
  534. *
  535. * This function should be called by anyone needing to update current
  536. * keymap. Presently keyboard and evdev handlers use it.
  537. */
  538. int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
  539. {
  540. unsigned long flags;
  541. int old_keycode;
  542. int retval;
  543. if (scancode < 0)
  544. return -EINVAL;
  545. if (keycode < 0 || keycode > KEY_MAX)
  546. return -EINVAL;
  547. spin_lock_irqsave(&dev->event_lock, flags);
  548. retval = dev->getkeycode(dev, scancode, &old_keycode);
  549. if (retval)
  550. goto out;
  551. retval = dev->setkeycode(dev, scancode, keycode);
  552. if (retval)
  553. goto out;
  554. /*
  555. * Simulate keyup event if keycode is not present
  556. * in the keymap anymore
  557. */
  558. if (test_bit(EV_KEY, dev->evbit) &&
  559. !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
  560. __test_and_clear_bit(old_keycode, dev->key)) {
  561. input_pass_event(dev, EV_KEY, old_keycode, 0);
  562. if (dev->sync)
  563. input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
  564. }
  565. out:
  566. spin_unlock_irqrestore(&dev->event_lock, flags);
  567. return retval;
  568. }
  569. EXPORT_SYMBOL(input_set_keycode);
  570. #define MATCH_BIT(bit, max) \
  571. for (i = 0; i < BITS_TO_LONGS(max); i++) \
  572. if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
  573. break; \
  574. if (i != BITS_TO_LONGS(max)) \
  575. continue;
  576. static const struct input_device_id *input_match_device(const struct input_device_id *id,
  577. struct input_dev *dev)
  578. {
  579. int i;
  580. for (; id->flags || id->driver_info; id++) {
  581. if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  582. if (id->bustype != dev->id.bustype)
  583. continue;
  584. if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  585. if (id->vendor != dev->id.vendor)
  586. continue;
  587. if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  588. if (id->product != dev->id.product)
  589. continue;
  590. if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  591. if (id->version != dev->id.version)
  592. continue;
  593. MATCH_BIT(evbit, EV_MAX);
  594. MATCH_BIT(keybit, KEY_MAX);
  595. MATCH_BIT(relbit, REL_MAX);
  596. MATCH_BIT(absbit, ABS_MAX);
  597. MATCH_BIT(mscbit, MSC_MAX);
  598. MATCH_BIT(ledbit, LED_MAX);
  599. MATCH_BIT(sndbit, SND_MAX);
  600. MATCH_BIT(ffbit, FF_MAX);
  601. MATCH_BIT(swbit, SW_MAX);
  602. return id;
  603. }
  604. return NULL;
  605. }
  606. static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
  607. {
  608. const struct input_device_id *id;
  609. int error;
  610. if (handler->blacklist && input_match_device(handler->blacklist, dev))
  611. return -ENODEV;
  612. id = input_match_device(handler->id_table, dev);
  613. if (!id)
  614. return -ENODEV;
  615. error = handler->connect(handler, dev, id);
  616. if (error && error != -ENODEV)
  617. printk(KERN_ERR
  618. "input: failed to attach handler %s to device %s, "
  619. "error: %d\n",
  620. handler->name, kobject_name(&dev->dev.kobj), error);
  621. return error;
  622. }
  623. #ifdef CONFIG_PROC_FS
  624. static struct proc_dir_entry *proc_bus_input_dir;
  625. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  626. static int input_devices_state;
  627. static inline void input_wakeup_procfs_readers(void)
  628. {
  629. input_devices_state++;
  630. wake_up(&input_devices_poll_wait);
  631. }
  632. static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
  633. {
  634. poll_wait(file, &input_devices_poll_wait, wait);
  635. if (file->f_version != input_devices_state) {
  636. file->f_version = input_devices_state;
  637. return POLLIN | POLLRDNORM;
  638. }
  639. return 0;
  640. }
  641. union input_seq_state {
  642. struct {
  643. unsigned short pos;
  644. bool mutex_acquired;
  645. };
  646. void *p;
  647. };
  648. static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
  649. {
  650. union input_seq_state *state = (union input_seq_state *)&seq->private;
  651. int error;
  652. /* We need to fit into seq->private pointer */
  653. BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
  654. error = mutex_lock_interruptible(&input_mutex);
  655. if (error) {
  656. state->mutex_acquired = false;
  657. return ERR_PTR(error);
  658. }
  659. state->mutex_acquired = true;
  660. return seq_list_start(&input_dev_list, *pos);
  661. }
  662. static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  663. {
  664. return seq_list_next(v, &input_dev_list, pos);
  665. }
  666. static void input_seq_stop(struct seq_file *seq, void *v)
  667. {
  668. union input_seq_state *state = (union input_seq_state *)&seq->private;
  669. if (state->mutex_acquired)
  670. mutex_unlock(&input_mutex);
  671. }
  672. static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
  673. unsigned long *bitmap, int max)
  674. {
  675. int i;
  676. for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
  677. if (bitmap[i])
  678. break;
  679. seq_printf(seq, "B: %s=", name);
  680. for (; i >= 0; i--)
  681. seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
  682. seq_putc(seq, '\n');
  683. }
  684. static int input_devices_seq_show(struct seq_file *seq, void *v)
  685. {
  686. struct input_dev *dev = container_of(v, struct input_dev, node);
  687. const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  688. struct input_handle *handle;
  689. seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  690. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  691. seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  692. seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  693. seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
  694. seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
  695. seq_printf(seq, "H: Handlers=");
  696. list_for_each_entry(handle, &dev->h_list, d_node)
  697. seq_printf(seq, "%s ", handle->name);
  698. seq_putc(seq, '\n');
  699. input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
  700. if (test_bit(EV_KEY, dev->evbit))
  701. input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
  702. if (test_bit(EV_REL, dev->evbit))
  703. input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
  704. if (test_bit(EV_ABS, dev->evbit))
  705. input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
  706. if (test_bit(EV_MSC, dev->evbit))
  707. input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
  708. if (test_bit(EV_LED, dev->evbit))
  709. input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
  710. if (test_bit(EV_SND, dev->evbit))
  711. input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
  712. if (test_bit(EV_FF, dev->evbit))
  713. input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
  714. if (test_bit(EV_SW, dev->evbit))
  715. input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
  716. seq_putc(seq, '\n');
  717. kfree(path);
  718. return 0;
  719. }
  720. static const struct seq_operations input_devices_seq_ops = {
  721. .start = input_devices_seq_start,
  722. .next = input_devices_seq_next,
  723. .stop = input_seq_stop,
  724. .show = input_devices_seq_show,
  725. };
  726. static int input_proc_devices_open(struct inode *inode, struct file *file)
  727. {
  728. return seq_open(file, &input_devices_seq_ops);
  729. }
  730. static const struct file_operations input_devices_fileops = {
  731. .owner = THIS_MODULE,
  732. .open = input_proc_devices_open,
  733. .poll = input_proc_devices_poll,
  734. .read = seq_read,
  735. .llseek = seq_lseek,
  736. .release = seq_release,
  737. };
  738. static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
  739. {
  740. union input_seq_state *state = (union input_seq_state *)&seq->private;
  741. int error;
  742. /* We need to fit into seq->private pointer */
  743. BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
  744. error = mutex_lock_interruptible(&input_mutex);
  745. if (error) {
  746. state->mutex_acquired = false;
  747. return ERR_PTR(error);
  748. }
  749. state->mutex_acquired = true;
  750. state->pos = *pos;
  751. return seq_list_start(&input_handler_list, *pos);
  752. }
  753. static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  754. {
  755. union input_seq_state *state = (union input_seq_state *)&seq->private;
  756. state->pos = *pos + 1;
  757. return seq_list_next(v, &input_handler_list, pos);
  758. }
  759. static int input_handlers_seq_show(struct seq_file *seq, void *v)
  760. {
  761. struct input_handler *handler = container_of(v, struct input_handler, node);
  762. union input_seq_state *state = (union input_seq_state *)&seq->private;
  763. seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
  764. if (handler->fops)
  765. seq_printf(seq, " Minor=%d", handler->minor);
  766. seq_putc(seq, '\n');
  767. return 0;
  768. }
  769. static const struct seq_operations input_handlers_seq_ops = {
  770. .start = input_handlers_seq_start,
  771. .next = input_handlers_seq_next,
  772. .stop = input_seq_stop,
  773. .show = input_handlers_seq_show,
  774. };
  775. static int input_proc_handlers_open(struct inode *inode, struct file *file)
  776. {
  777. return seq_open(file, &input_handlers_seq_ops);
  778. }
  779. static const struct file_operations input_handlers_fileops = {
  780. .owner = THIS_MODULE,
  781. .open = input_proc_handlers_open,
  782. .read = seq_read,
  783. .llseek = seq_lseek,
  784. .release = seq_release,
  785. };
  786. static int __init input_proc_init(void)
  787. {
  788. struct proc_dir_entry *entry;
  789. proc_bus_input_dir = proc_mkdir("bus/input", NULL);
  790. if (!proc_bus_input_dir)
  791. return -ENOMEM;
  792. entry = proc_create("devices", 0, proc_bus_input_dir,
  793. &input_devices_fileops);
  794. if (!entry)
  795. goto fail1;
  796. entry = proc_create("handlers", 0, proc_bus_input_dir,
  797. &input_handlers_fileops);
  798. if (!entry)
  799. goto fail2;
  800. return 0;
  801. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  802. fail1: remove_proc_entry("bus/input", NULL);
  803. return -ENOMEM;
  804. }
  805. static void input_proc_exit(void)
  806. {
  807. remove_proc_entry("devices", proc_bus_input_dir);
  808. remove_proc_entry("handlers", proc_bus_input_dir);
  809. remove_proc_entry("bus/input", NULL);
  810. }
  811. #else /* !CONFIG_PROC_FS */
  812. static inline void input_wakeup_procfs_readers(void) { }
  813. static inline int input_proc_init(void) { return 0; }
  814. static inline void input_proc_exit(void) { }
  815. #endif
  816. #define INPUT_DEV_STRING_ATTR_SHOW(name) \
  817. static ssize_t input_dev_show_##name(struct device *dev, \
  818. struct device_attribute *attr, \
  819. char *buf) \
  820. { \
  821. struct input_dev *input_dev = to_input_dev(dev); \
  822. \
  823. return scnprintf(buf, PAGE_SIZE, "%s\n", \
  824. input_dev->name ? input_dev->name : ""); \
  825. } \
  826. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
  827. INPUT_DEV_STRING_ATTR_SHOW(name);
  828. INPUT_DEV_STRING_ATTR_SHOW(phys);
  829. INPUT_DEV_STRING_ATTR_SHOW(uniq);
  830. static int input_print_modalias_bits(char *buf, int size,
  831. char name, unsigned long *bm,
  832. unsigned int min_bit, unsigned int max_bit)
  833. {
  834. int len = 0, i;
  835. len += snprintf(buf, max(size, 0), "%c", name);
  836. for (i = min_bit; i < max_bit; i++)
  837. if (bm[BIT_WORD(i)] & BIT_MASK(i))
  838. len += snprintf(buf + len, max(size - len, 0), "%X,", i);
  839. return len;
  840. }
  841. static int input_print_modalias(char *buf, int size, struct input_dev *id,
  842. int add_cr)
  843. {
  844. int len;
  845. len = snprintf(buf, max(size, 0),
  846. "input:b%04Xv%04Xp%04Xe%04X-",
  847. id->id.bustype, id->id.vendor,
  848. id->id.product, id->id.version);
  849. len += input_print_modalias_bits(buf + len, size - len,
  850. 'e', id->evbit, 0, EV_MAX);
  851. len += input_print_modalias_bits(buf + len, size - len,
  852. 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
  853. len += input_print_modalias_bits(buf + len, size - len,
  854. 'r', id->relbit, 0, REL_MAX);
  855. len += input_print_modalias_bits(buf + len, size - len,
  856. 'a', id->absbit, 0, ABS_MAX);
  857. len += input_print_modalias_bits(buf + len, size - len,
  858. 'm', id->mscbit, 0, MSC_MAX);
  859. len += input_print_modalias_bits(buf + len, size - len,
  860. 'l', id->ledbit, 0, LED_MAX);
  861. len += input_print_modalias_bits(buf + len, size - len,
  862. 's', id->sndbit, 0, SND_MAX);
  863. len += input_print_modalias_bits(buf + len, size - len,
  864. 'f', id->ffbit, 0, FF_MAX);
  865. len += input_print_modalias_bits(buf + len, size - len,
  866. 'w', id->swbit, 0, SW_MAX);
  867. if (add_cr)
  868. len += snprintf(buf + len, max(size - len, 0), "\n");
  869. return len;
  870. }
  871. static ssize_t input_dev_show_modalias(struct device *dev,
  872. struct device_attribute *attr,
  873. char *buf)
  874. {
  875. struct input_dev *id = to_input_dev(dev);
  876. ssize_t len;
  877. len = input_print_modalias(buf, PAGE_SIZE, id, 1);
  878. return min_t(int, len, PAGE_SIZE);
  879. }
  880. static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
  881. static struct attribute *input_dev_attrs[] = {
  882. &dev_attr_name.attr,
  883. &dev_attr_phys.attr,
  884. &dev_attr_uniq.attr,
  885. &dev_attr_modalias.attr,
  886. NULL
  887. };
  888. static struct attribute_group input_dev_attr_group = {
  889. .attrs = input_dev_attrs,
  890. };
  891. #define INPUT_DEV_ID_ATTR(name) \
  892. static ssize_t input_dev_show_id_##name(struct device *dev, \
  893. struct device_attribute *attr, \
  894. char *buf) \
  895. { \
  896. struct input_dev *input_dev = to_input_dev(dev); \
  897. return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
  898. } \
  899. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
  900. INPUT_DEV_ID_ATTR(bustype);
  901. INPUT_DEV_ID_ATTR(vendor);
  902. INPUT_DEV_ID_ATTR(product);
  903. INPUT_DEV_ID_ATTR(version);
  904. static struct attribute *input_dev_id_attrs[] = {
  905. &dev_attr_bustype.attr,
  906. &dev_attr_vendor.attr,
  907. &dev_attr_product.attr,
  908. &dev_attr_version.attr,
  909. NULL
  910. };
  911. static struct attribute_group input_dev_id_attr_group = {
  912. .name = "id",
  913. .attrs = input_dev_id_attrs,
  914. };
  915. static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
  916. int max, int add_cr)
  917. {
  918. int i;
  919. int len = 0;
  920. for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
  921. if (bitmap[i])
  922. break;
  923. for (; i >= 0; i--)
  924. len += snprintf(buf + len, max(buf_size - len, 0),
  925. "%lx%s", bitmap[i], i > 0 ? " " : "");
  926. if (add_cr)
  927. len += snprintf(buf + len, max(buf_size - len, 0), "\n");
  928. return len;
  929. }
  930. #define INPUT_DEV_CAP_ATTR(ev, bm) \
  931. static ssize_t input_dev_show_cap_##bm(struct device *dev, \
  932. struct device_attribute *attr, \
  933. char *buf) \
  934. { \
  935. struct input_dev *input_dev = to_input_dev(dev); \
  936. int len = input_print_bitmap(buf, PAGE_SIZE, \
  937. input_dev->bm##bit, ev##_MAX, 1); \
  938. return min_t(int, len, PAGE_SIZE); \
  939. } \
  940. static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
  941. INPUT_DEV_CAP_ATTR(EV, ev);
  942. INPUT_DEV_CAP_ATTR(KEY, key);
  943. INPUT_DEV_CAP_ATTR(REL, rel);
  944. INPUT_DEV_CAP_ATTR(ABS, abs);
  945. INPUT_DEV_CAP_ATTR(MSC, msc);
  946. INPUT_DEV_CAP_ATTR(LED, led);
  947. INPUT_DEV_CAP_ATTR(SND, snd);
  948. INPUT_DEV_CAP_ATTR(FF, ff);
  949. INPUT_DEV_CAP_ATTR(SW, sw);
  950. static struct attribute *input_dev_caps_attrs[] = {
  951. &dev_attr_ev.attr,
  952. &dev_attr_key.attr,
  953. &dev_attr_rel.attr,
  954. &dev_attr_abs.attr,
  955. &dev_attr_msc.attr,
  956. &dev_attr_led.attr,
  957. &dev_attr_snd.attr,
  958. &dev_attr_ff.attr,
  959. &dev_attr_sw.attr,
  960. NULL
  961. };
  962. static struct attribute_group input_dev_caps_attr_group = {
  963. .name = "capabilities",
  964. .attrs = input_dev_caps_attrs,
  965. };
  966. static struct attribute_group *input_dev_attr_groups[] = {
  967. &input_dev_attr_group,
  968. &input_dev_id_attr_group,
  969. &input_dev_caps_attr_group,
  970. NULL
  971. };
  972. static void input_dev_release(struct device *device)
  973. {
  974. struct input_dev *dev = to_input_dev(device);
  975. input_ff_destroy(dev);
  976. kfree(dev);
  977. module_put(THIS_MODULE);
  978. }
  979. /*
  980. * Input uevent interface - loading event handlers based on
  981. * device bitfields.
  982. */
  983. static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
  984. const char *name, unsigned long *bitmap, int max)
  985. {
  986. int len;
  987. if (add_uevent_var(env, "%s=", name))
  988. return -ENOMEM;
  989. len = input_print_bitmap(&env->buf[env->buflen - 1],
  990. sizeof(env->buf) - env->buflen,
  991. bitmap, max, 0);
  992. if (len >= (sizeof(env->buf) - env->buflen))
  993. return -ENOMEM;
  994. env->buflen += len;
  995. return 0;
  996. }
  997. static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
  998. struct input_dev *dev)
  999. {
  1000. int len;
  1001. if (add_uevent_var(env, "MODALIAS="))
  1002. return -ENOMEM;
  1003. len = input_print_modalias(&env->buf[env->buflen - 1],
  1004. sizeof(env->buf) - env->buflen,
  1005. dev, 0);
  1006. if (len >= (sizeof(env->buf) - env->buflen))
  1007. return -ENOMEM;
  1008. env->buflen += len;
  1009. return 0;
  1010. }
  1011. #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
  1012. do { \
  1013. int err = add_uevent_var(env, fmt, val); \
  1014. if (err) \
  1015. return err; \
  1016. } while (0)
  1017. #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
  1018. do { \
  1019. int err = input_add_uevent_bm_var(env, name, bm, max); \
  1020. if (err) \
  1021. return err; \
  1022. } while (0)
  1023. #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
  1024. do { \
  1025. int err = input_add_uevent_modalias_var(env, dev); \
  1026. if (err) \
  1027. return err; \
  1028. } while (0)
  1029. static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  1030. {
  1031. struct input_dev *dev = to_input_dev(device);
  1032. INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
  1033. dev->id.bustype, dev->id.vendor,
  1034. dev->id.product, dev->id.version);
  1035. if (dev->name)
  1036. INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
  1037. if (dev->phys)
  1038. INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
  1039. if (dev->uniq)
  1040. INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
  1041. INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
  1042. if (test_bit(EV_KEY, dev->evbit))
  1043. INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
  1044. if (test_bit(EV_REL, dev->evbit))
  1045. INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
  1046. if (test_bit(EV_ABS, dev->evbit))
  1047. INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
  1048. if (test_bit(EV_MSC, dev->evbit))
  1049. INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
  1050. if (test_bit(EV_LED, dev->evbit))
  1051. INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
  1052. if (test_bit(EV_SND, dev->evbit))
  1053. INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
  1054. if (test_bit(EV_FF, dev->evbit))
  1055. INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
  1056. if (test_bit(EV_SW, dev->evbit))
  1057. INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
  1058. INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
  1059. return 0;
  1060. }
  1061. #define INPUT_DO_TOGGLE(dev, type, bits, on) \
  1062. do { \
  1063. int i; \
  1064. if (!test_bit(EV_##type, dev->evbit)) \
  1065. break; \
  1066. for (i = 0; i < type##_MAX; i++) { \
  1067. if (!test_bit(i, dev->bits##bit) || \
  1068. !test_bit(i, dev->bits)) \
  1069. continue; \
  1070. dev->event(dev, EV_##type, i, on); \
  1071. } \
  1072. } while (0)
  1073. static void input_dev_reset(struct input_dev *dev, bool activate)
  1074. {
  1075. if (!dev->event)
  1076. return;
  1077. INPUT_DO_TOGGLE(dev, LED, led, activate);
  1078. INPUT_DO_TOGGLE(dev, SND, snd, activate);
  1079. if (activate && test_bit(EV_REP, dev->evbit)) {
  1080. dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
  1081. dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
  1082. }
  1083. }
  1084. #ifdef CONFIG_PM
  1085. static int input_dev_suspend(struct device *dev)
  1086. {
  1087. struct input_dev *input_dev = to_input_dev(dev);
  1088. mutex_lock(&input_dev->mutex);
  1089. input_dev_reset(input_dev, false);
  1090. mutex_unlock(&input_dev->mutex);
  1091. return 0;
  1092. }
  1093. static int input_dev_resume(struct device *dev)
  1094. {
  1095. struct input_dev *input_dev = to_input_dev(dev);
  1096. mutex_lock(&input_dev->mutex);
  1097. input_dev_reset(input_dev, true);
  1098. mutex_unlock(&input_dev->mutex);
  1099. return 0;
  1100. }
  1101. static const struct dev_pm_ops input_dev_pm_ops = {
  1102. .suspend = input_dev_suspend,
  1103. .resume = input_dev_resume,
  1104. .poweroff = input_dev_suspend,
  1105. .restore = input_dev_resume,
  1106. };
  1107. #endif /* CONFIG_PM */
  1108. static struct device_type input_dev_type = {
  1109. .groups = input_dev_attr_groups,
  1110. .release = input_dev_release,
  1111. .uevent = input_dev_uevent,
  1112. #ifdef CONFIG_PM
  1113. .pm = &input_dev_pm_ops,
  1114. #endif
  1115. };
  1116. static char *input_nodename(struct device *dev)
  1117. {
  1118. return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
  1119. }
  1120. struct class input_class = {
  1121. .name = "input",
  1122. .nodename = input_nodename,
  1123. };
  1124. EXPORT_SYMBOL_GPL(input_class);
  1125. /**
  1126. * input_allocate_device - allocate memory for new input device
  1127. *
  1128. * Returns prepared struct input_dev or NULL.
  1129. *
  1130. * NOTE: Use input_free_device() to free devices that have not been
  1131. * registered; input_unregister_device() should be used for already
  1132. * registered devices.
  1133. */
  1134. struct input_dev *input_allocate_device(void)
  1135. {
  1136. struct input_dev *dev;
  1137. dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  1138. if (dev) {
  1139. dev->dev.type = &input_dev_type;
  1140. dev->dev.class = &input_class;
  1141. device_initialize(&dev->dev);
  1142. mutex_init(&dev->mutex);
  1143. spin_lock_init(&dev->event_lock);
  1144. INIT_LIST_HEAD(&dev->h_list);
  1145. INIT_LIST_HEAD(&dev->node);
  1146. __module_get(THIS_MODULE);
  1147. }
  1148. return dev;
  1149. }
  1150. EXPORT_SYMBOL(input_allocate_device);
  1151. /**
  1152. * input_free_device - free memory occupied by input_dev structure
  1153. * @dev: input device to free
  1154. *
  1155. * This function should only be used if input_register_device()
  1156. * was not called yet or if it failed. Once device was registered
  1157. * use input_unregister_device() and memory will be freed once last
  1158. * reference to the device is dropped.
  1159. *
  1160. * Device should be allocated by input_allocate_device().
  1161. *
  1162. * NOTE: If there are references to the input device then memory
  1163. * will not be freed until last reference is dropped.
  1164. */
  1165. void input_free_device(struct input_dev *dev)
  1166. {
  1167. if (dev)
  1168. input_put_device(dev);
  1169. }
  1170. EXPORT_SYMBOL(input_free_device);
  1171. /**
  1172. * input_set_capability - mark device as capable of a certain event
  1173. * @dev: device that is capable of emitting or accepting event
  1174. * @type: type of the event (EV_KEY, EV_REL, etc...)
  1175. * @code: event code
  1176. *
  1177. * In addition to setting up corresponding bit in appropriate capability
  1178. * bitmap the function also adjusts dev->evbit.
  1179. */
  1180. void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
  1181. {
  1182. switch (type) {
  1183. case EV_KEY:
  1184. __set_bit(code, dev->keybit);
  1185. break;
  1186. case EV_REL:
  1187. __set_bit(code, dev->relbit);
  1188. break;
  1189. case EV_ABS:
  1190. __set_bit(code, dev->absbit);
  1191. break;
  1192. case EV_MSC:
  1193. __set_bit(code, dev->mscbit);
  1194. break;
  1195. case EV_SW:
  1196. __set_bit(code, dev->swbit);
  1197. break;
  1198. case EV_LED:
  1199. __set_bit(code, dev->ledbit);
  1200. break;
  1201. case EV_SND:
  1202. __set_bit(code, dev->sndbit);
  1203. break;
  1204. case EV_FF:
  1205. __set_bit(code, dev->ffbit);
  1206. break;
  1207. case EV_PWR:
  1208. /* do nothing */
  1209. break;
  1210. default:
  1211. printk(KERN_ERR
  1212. "input_set_capability: unknown type %u (code %u)\n",
  1213. type, code);
  1214. dump_stack();
  1215. return;
  1216. }
  1217. __set_bit(type, dev->evbit);
  1218. }
  1219. EXPORT_SYMBOL(input_set_capability);
  1220. /**
  1221. * input_register_device - register device with input core
  1222. * @dev: device to be registered
  1223. *
  1224. * This function registers device with input core. The device must be
  1225. * allocated with input_allocate_device() and all it's capabilities
  1226. * set up before registering.
  1227. * If function fails the device must be freed with input_free_device().
  1228. * Once device has been successfully registered it can be unregistered
  1229. * with input_unregister_device(); input_free_device() should not be
  1230. * called in this case.
  1231. */
  1232. int input_register_device(struct input_dev *dev)
  1233. {
  1234. static atomic_t input_no = ATOMIC_INIT(0);
  1235. struct input_handler *handler;
  1236. const char *path;
  1237. int error;
  1238. __set_bit(EV_SYN, dev->evbit);
  1239. /*
  1240. * If delay and period are pre-set by the driver, then autorepeating
  1241. * is handled by the driver itself and we don't do it in input.c.
  1242. */
  1243. init_timer(&dev->timer);
  1244. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  1245. dev->timer.data = (long) dev;
  1246. dev->timer.function = input_repeat_key;
  1247. dev->rep[REP_DELAY] = 250;
  1248. dev->rep[REP_PERIOD] = 33;
  1249. }
  1250. if (!dev->getkeycode)
  1251. dev->getkeycode = input_default_getkeycode;
  1252. if (!dev->setkeycode)
  1253. dev->setkeycode = input_default_setkeycode;
  1254. dev_set_name(&dev->dev, "input%ld",
  1255. (unsigned long) atomic_inc_return(&input_no) - 1);
  1256. error = device_add(&dev->dev);
  1257. if (error)
  1258. return error;
  1259. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1260. printk(KERN_INFO "input: %s as %s\n",
  1261. dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
  1262. kfree(path);
  1263. error = mutex_lock_interruptible(&input_mutex);
  1264. if (error) {
  1265. device_del(&dev->dev);
  1266. return error;
  1267. }
  1268. list_add_tail(&dev->node, &input_dev_list);
  1269. list_for_each_entry(handler, &input_handler_list, node)
  1270. input_attach_handler(dev, handler);
  1271. input_wakeup_procfs_readers();
  1272. mutex_unlock(&input_mutex);
  1273. return 0;
  1274. }
  1275. EXPORT_SYMBOL(input_register_device);
  1276. /**
  1277. * input_unregister_device - unregister previously registered device
  1278. * @dev: device to be unregistered
  1279. *
  1280. * This function unregisters an input device. Once device is unregistered
  1281. * the caller should not try to access it as it may get freed at any moment.
  1282. */
  1283. void input_unregister_device(struct input_dev *dev)
  1284. {
  1285. struct input_handle *handle, *next;
  1286. input_disconnect_device(dev);
  1287. mutex_lock(&input_mutex);
  1288. list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
  1289. handle->handler->disconnect(handle);
  1290. WARN_ON(!list_empty(&dev->h_list));
  1291. del_timer_sync(&dev->timer);
  1292. list_del_init(&dev->node);
  1293. input_wakeup_procfs_readers();
  1294. mutex_unlock(&input_mutex);
  1295. device_unregister(&dev->dev);
  1296. }
  1297. EXPORT_SYMBOL(input_unregister_device);
  1298. /**
  1299. * input_register_handler - register a new input handler
  1300. * @handler: handler to be registered
  1301. *
  1302. * This function registers a new input handler (interface) for input
  1303. * devices in the system and attaches it to all input devices that
  1304. * are compatible with the handler.
  1305. */
  1306. int input_register_handler(struct input_handler *handler)
  1307. {
  1308. struct input_dev *dev;
  1309. int retval;
  1310. retval = mutex_lock_interruptible(&input_mutex);
  1311. if (retval)
  1312. return retval;
  1313. INIT_LIST_HEAD(&handler->h_list);
  1314. if (handler->fops != NULL) {
  1315. if (input_table[handler->minor >> 5]) {
  1316. retval = -EBUSY;
  1317. goto out;
  1318. }
  1319. input_table[handler->minor >> 5] = handler;
  1320. }
  1321. list_add_tail(&handler->node, &input_handler_list);
  1322. list_for_each_entry(dev, &input_dev_list, node)
  1323. input_attach_handler(dev, handler);
  1324. input_wakeup_procfs_readers();
  1325. out:
  1326. mutex_unlock(&input_mutex);
  1327. return retval;
  1328. }
  1329. EXPORT_SYMBOL(input_register_handler);
  1330. /**
  1331. * input_unregister_handler - unregisters an input handler
  1332. * @handler: handler to be unregistered
  1333. *
  1334. * This function disconnects a handler from its input devices and
  1335. * removes it from lists of known handlers.
  1336. */
  1337. void input_unregister_handler(struct input_handler *handler)
  1338. {
  1339. struct input_handle *handle, *next;
  1340. mutex_lock(&input_mutex);
  1341. list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
  1342. handler->disconnect(handle);
  1343. WARN_ON(!list_empty(&handler->h_list));
  1344. list_del_init(&handler->node);
  1345. if (handler->fops != NULL)
  1346. input_table[handler->minor >> 5] = NULL;
  1347. input_wakeup_procfs_readers();
  1348. mutex_unlock(&input_mutex);
  1349. }
  1350. EXPORT_SYMBOL(input_unregister_handler);
  1351. /**
  1352. * input_register_handle - register a new input handle
  1353. * @handle: handle to register
  1354. *
  1355. * This function puts a new input handle onto device's
  1356. * and handler's lists so that events can flow through
  1357. * it once it is opened using input_open_device().
  1358. *
  1359. * This function is supposed to be called from handler's
  1360. * connect() method.
  1361. */
  1362. int input_register_handle(struct input_handle *handle)
  1363. {
  1364. struct input_handler *handler = handle->handler;
  1365. struct input_dev *dev = handle->dev;
  1366. int error;
  1367. /*
  1368. * We take dev->mutex here to prevent race with
  1369. * input_release_device().
  1370. */
  1371. error = mutex_lock_interruptible(&dev->mutex);
  1372. if (error)
  1373. return error;
  1374. list_add_tail_rcu(&handle->d_node, &dev->h_list);
  1375. mutex_unlock(&dev->mutex);
  1376. /*
  1377. * Since we are supposed to be called from ->connect()
  1378. * which is mutually exclusive with ->disconnect()
  1379. * we can't be racing with input_unregister_handle()
  1380. * and so separate lock is not needed here.
  1381. */
  1382. list_add_tail(&handle->h_node, &handler->h_list);
  1383. if (handler->start)
  1384. handler->start(handle);
  1385. return 0;
  1386. }
  1387. EXPORT_SYMBOL(input_register_handle);
  1388. /**
  1389. * input_unregister_handle - unregister an input handle
  1390. * @handle: handle to unregister
  1391. *
  1392. * This function removes input handle from device's
  1393. * and handler's lists.
  1394. *
  1395. * This function is supposed to be called from handler's
  1396. * disconnect() method.
  1397. */
  1398. void input_unregister_handle(struct input_handle *handle)
  1399. {
  1400. struct input_dev *dev = handle->dev;
  1401. list_del_init(&handle->h_node);
  1402. /*
  1403. * Take dev->mutex to prevent race with input_release_device().
  1404. */
  1405. mutex_lock(&dev->mutex);
  1406. list_del_rcu(&handle->d_node);
  1407. mutex_unlock(&dev->mutex);
  1408. synchronize_rcu();
  1409. }
  1410. EXPORT_SYMBOL(input_unregister_handle);
  1411. static int input_open_file(struct inode *inode, struct file *file)
  1412. {
  1413. struct input_handler *handler;
  1414. const struct file_operations *old_fops, *new_fops = NULL;
  1415. int err;
  1416. lock_kernel();
  1417. /* No load-on-demand here? */
  1418. handler = input_table[iminor(inode) >> 5];
  1419. if (!handler || !(new_fops = fops_get(handler->fops))) {
  1420. err = -ENODEV;
  1421. goto out;
  1422. }
  1423. /*
  1424. * That's _really_ odd. Usually NULL ->open means "nothing special",
  1425. * not "no device". Oh, well...
  1426. */
  1427. if (!new_fops->open) {
  1428. fops_put(new_fops);
  1429. err = -ENODEV;
  1430. goto out;
  1431. }
  1432. old_fops = file->f_op;
  1433. file->f_op = new_fops;
  1434. err = new_fops->open(inode, file);
  1435. if (err) {
  1436. fops_put(file->f_op);
  1437. file->f_op = fops_get(old_fops);
  1438. }
  1439. fops_put(old_fops);
  1440. out:
  1441. unlock_kernel();
  1442. return err;
  1443. }
  1444. static const struct file_operations input_fops = {
  1445. .owner = THIS_MODULE,
  1446. .open = input_open_file,
  1447. };
  1448. static void __init input_init_abs_bypass(void)
  1449. {
  1450. const unsigned int *p;
  1451. for (p = input_abs_bypass_init_data; *p; p++)
  1452. input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
  1453. }
  1454. static int __init input_init(void)
  1455. {
  1456. int err;
  1457. input_init_abs_bypass();
  1458. err = class_register(&input_class);
  1459. if (err) {
  1460. printk(KERN_ERR "input: unable to register input_dev class\n");
  1461. return err;
  1462. }
  1463. err = input_proc_init();
  1464. if (err)
  1465. goto fail1;
  1466. err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
  1467. if (err) {
  1468. printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
  1469. goto fail2;
  1470. }
  1471. return 0;
  1472. fail2: input_proc_exit();
  1473. fail1: class_unregister(&input_class);
  1474. return err;
  1475. }
  1476. static void __exit input_exit(void)
  1477. {
  1478. input_proc_exit();
  1479. unregister_chrdev(INPUT_MAJOR, "input");
  1480. class_unregister(&input_class);
  1481. }
  1482. subsys_initcall(input_init);
  1483. module_exit(input_exit);