input.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  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. static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
  642. {
  643. if (mutex_lock_interruptible(&input_mutex))
  644. return NULL;
  645. return seq_list_start(&input_dev_list, *pos);
  646. }
  647. static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  648. {
  649. return seq_list_next(v, &input_dev_list, pos);
  650. }
  651. static void input_devices_seq_stop(struct seq_file *seq, void *v)
  652. {
  653. mutex_unlock(&input_mutex);
  654. }
  655. static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
  656. unsigned long *bitmap, int max)
  657. {
  658. int i;
  659. for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
  660. if (bitmap[i])
  661. break;
  662. seq_printf(seq, "B: %s=", name);
  663. for (; i >= 0; i--)
  664. seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
  665. seq_putc(seq, '\n');
  666. }
  667. static int input_devices_seq_show(struct seq_file *seq, void *v)
  668. {
  669. struct input_dev *dev = container_of(v, struct input_dev, node);
  670. const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  671. struct input_handle *handle;
  672. seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  673. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  674. seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  675. seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  676. seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
  677. seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
  678. seq_printf(seq, "H: Handlers=");
  679. list_for_each_entry(handle, &dev->h_list, d_node)
  680. seq_printf(seq, "%s ", handle->name);
  681. seq_putc(seq, '\n');
  682. input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
  683. if (test_bit(EV_KEY, dev->evbit))
  684. input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
  685. if (test_bit(EV_REL, dev->evbit))
  686. input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
  687. if (test_bit(EV_ABS, dev->evbit))
  688. input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
  689. if (test_bit(EV_MSC, dev->evbit))
  690. input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
  691. if (test_bit(EV_LED, dev->evbit))
  692. input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
  693. if (test_bit(EV_SND, dev->evbit))
  694. input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
  695. if (test_bit(EV_FF, dev->evbit))
  696. input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
  697. if (test_bit(EV_SW, dev->evbit))
  698. input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
  699. seq_putc(seq, '\n');
  700. kfree(path);
  701. return 0;
  702. }
  703. static const struct seq_operations input_devices_seq_ops = {
  704. .start = input_devices_seq_start,
  705. .next = input_devices_seq_next,
  706. .stop = input_devices_seq_stop,
  707. .show = input_devices_seq_show,
  708. };
  709. static int input_proc_devices_open(struct inode *inode, struct file *file)
  710. {
  711. return seq_open(file, &input_devices_seq_ops);
  712. }
  713. static const struct file_operations input_devices_fileops = {
  714. .owner = THIS_MODULE,
  715. .open = input_proc_devices_open,
  716. .poll = input_proc_devices_poll,
  717. .read = seq_read,
  718. .llseek = seq_lseek,
  719. .release = seq_release,
  720. };
  721. static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
  722. {
  723. if (mutex_lock_interruptible(&input_mutex))
  724. return NULL;
  725. seq->private = (void *)(unsigned long)*pos;
  726. return seq_list_start(&input_handler_list, *pos);
  727. }
  728. static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  729. {
  730. seq->private = (void *)(unsigned long)(*pos + 1);
  731. return seq_list_next(v, &input_handler_list, pos);
  732. }
  733. static void input_handlers_seq_stop(struct seq_file *seq, void *v)
  734. {
  735. mutex_unlock(&input_mutex);
  736. }
  737. static int input_handlers_seq_show(struct seq_file *seq, void *v)
  738. {
  739. struct input_handler *handler = container_of(v, struct input_handler, node);
  740. seq_printf(seq, "N: Number=%ld Name=%s",
  741. (unsigned long)seq->private, handler->name);
  742. if (handler->fops)
  743. seq_printf(seq, " Minor=%d", handler->minor);
  744. seq_putc(seq, '\n');
  745. return 0;
  746. }
  747. static const struct seq_operations input_handlers_seq_ops = {
  748. .start = input_handlers_seq_start,
  749. .next = input_handlers_seq_next,
  750. .stop = input_handlers_seq_stop,
  751. .show = input_handlers_seq_show,
  752. };
  753. static int input_proc_handlers_open(struct inode *inode, struct file *file)
  754. {
  755. return seq_open(file, &input_handlers_seq_ops);
  756. }
  757. static const struct file_operations input_handlers_fileops = {
  758. .owner = THIS_MODULE,
  759. .open = input_proc_handlers_open,
  760. .read = seq_read,
  761. .llseek = seq_lseek,
  762. .release = seq_release,
  763. };
  764. static int __init input_proc_init(void)
  765. {
  766. struct proc_dir_entry *entry;
  767. proc_bus_input_dir = proc_mkdir("bus/input", NULL);
  768. if (!proc_bus_input_dir)
  769. return -ENOMEM;
  770. entry = proc_create("devices", 0, proc_bus_input_dir,
  771. &input_devices_fileops);
  772. if (!entry)
  773. goto fail1;
  774. entry = proc_create("handlers", 0, proc_bus_input_dir,
  775. &input_handlers_fileops);
  776. if (!entry)
  777. goto fail2;
  778. return 0;
  779. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  780. fail1: remove_proc_entry("bus/input", NULL);
  781. return -ENOMEM;
  782. }
  783. static void input_proc_exit(void)
  784. {
  785. remove_proc_entry("devices", proc_bus_input_dir);
  786. remove_proc_entry("handlers", proc_bus_input_dir);
  787. remove_proc_entry("bus/input", NULL);
  788. }
  789. #else /* !CONFIG_PROC_FS */
  790. static inline void input_wakeup_procfs_readers(void) { }
  791. static inline int input_proc_init(void) { return 0; }
  792. static inline void input_proc_exit(void) { }
  793. #endif
  794. #define INPUT_DEV_STRING_ATTR_SHOW(name) \
  795. static ssize_t input_dev_show_##name(struct device *dev, \
  796. struct device_attribute *attr, \
  797. char *buf) \
  798. { \
  799. struct input_dev *input_dev = to_input_dev(dev); \
  800. \
  801. return scnprintf(buf, PAGE_SIZE, "%s\n", \
  802. input_dev->name ? input_dev->name : ""); \
  803. } \
  804. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
  805. INPUT_DEV_STRING_ATTR_SHOW(name);
  806. INPUT_DEV_STRING_ATTR_SHOW(phys);
  807. INPUT_DEV_STRING_ATTR_SHOW(uniq);
  808. static int input_print_modalias_bits(char *buf, int size,
  809. char name, unsigned long *bm,
  810. unsigned int min_bit, unsigned int max_bit)
  811. {
  812. int len = 0, i;
  813. len += snprintf(buf, max(size, 0), "%c", name);
  814. for (i = min_bit; i < max_bit; i++)
  815. if (bm[BIT_WORD(i)] & BIT_MASK(i))
  816. len += snprintf(buf + len, max(size - len, 0), "%X,", i);
  817. return len;
  818. }
  819. static int input_print_modalias(char *buf, int size, struct input_dev *id,
  820. int add_cr)
  821. {
  822. int len;
  823. len = snprintf(buf, max(size, 0),
  824. "input:b%04Xv%04Xp%04Xe%04X-",
  825. id->id.bustype, id->id.vendor,
  826. id->id.product, id->id.version);
  827. len += input_print_modalias_bits(buf + len, size - len,
  828. 'e', id->evbit, 0, EV_MAX);
  829. len += input_print_modalias_bits(buf + len, size - len,
  830. 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
  831. len += input_print_modalias_bits(buf + len, size - len,
  832. 'r', id->relbit, 0, REL_MAX);
  833. len += input_print_modalias_bits(buf + len, size - len,
  834. 'a', id->absbit, 0, ABS_MAX);
  835. len += input_print_modalias_bits(buf + len, size - len,
  836. 'm', id->mscbit, 0, MSC_MAX);
  837. len += input_print_modalias_bits(buf + len, size - len,
  838. 'l', id->ledbit, 0, LED_MAX);
  839. len += input_print_modalias_bits(buf + len, size - len,
  840. 's', id->sndbit, 0, SND_MAX);
  841. len += input_print_modalias_bits(buf + len, size - len,
  842. 'f', id->ffbit, 0, FF_MAX);
  843. len += input_print_modalias_bits(buf + len, size - len,
  844. 'w', id->swbit, 0, SW_MAX);
  845. if (add_cr)
  846. len += snprintf(buf + len, max(size - len, 0), "\n");
  847. return len;
  848. }
  849. static ssize_t input_dev_show_modalias(struct device *dev,
  850. struct device_attribute *attr,
  851. char *buf)
  852. {
  853. struct input_dev *id = to_input_dev(dev);
  854. ssize_t len;
  855. len = input_print_modalias(buf, PAGE_SIZE, id, 1);
  856. return min_t(int, len, PAGE_SIZE);
  857. }
  858. static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
  859. static struct attribute *input_dev_attrs[] = {
  860. &dev_attr_name.attr,
  861. &dev_attr_phys.attr,
  862. &dev_attr_uniq.attr,
  863. &dev_attr_modalias.attr,
  864. NULL
  865. };
  866. static struct attribute_group input_dev_attr_group = {
  867. .attrs = input_dev_attrs,
  868. };
  869. #define INPUT_DEV_ID_ATTR(name) \
  870. static ssize_t input_dev_show_id_##name(struct device *dev, \
  871. struct device_attribute *attr, \
  872. char *buf) \
  873. { \
  874. struct input_dev *input_dev = to_input_dev(dev); \
  875. return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
  876. } \
  877. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
  878. INPUT_DEV_ID_ATTR(bustype);
  879. INPUT_DEV_ID_ATTR(vendor);
  880. INPUT_DEV_ID_ATTR(product);
  881. INPUT_DEV_ID_ATTR(version);
  882. static struct attribute *input_dev_id_attrs[] = {
  883. &dev_attr_bustype.attr,
  884. &dev_attr_vendor.attr,
  885. &dev_attr_product.attr,
  886. &dev_attr_version.attr,
  887. NULL
  888. };
  889. static struct attribute_group input_dev_id_attr_group = {
  890. .name = "id",
  891. .attrs = input_dev_id_attrs,
  892. };
  893. static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
  894. int max, int add_cr)
  895. {
  896. int i;
  897. int len = 0;
  898. for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
  899. if (bitmap[i])
  900. break;
  901. for (; i >= 0; i--)
  902. len += snprintf(buf + len, max(buf_size - len, 0),
  903. "%lx%s", bitmap[i], i > 0 ? " " : "");
  904. if (add_cr)
  905. len += snprintf(buf + len, max(buf_size - len, 0), "\n");
  906. return len;
  907. }
  908. #define INPUT_DEV_CAP_ATTR(ev, bm) \
  909. static ssize_t input_dev_show_cap_##bm(struct device *dev, \
  910. struct device_attribute *attr, \
  911. char *buf) \
  912. { \
  913. struct input_dev *input_dev = to_input_dev(dev); \
  914. int len = input_print_bitmap(buf, PAGE_SIZE, \
  915. input_dev->bm##bit, ev##_MAX, 1); \
  916. return min_t(int, len, PAGE_SIZE); \
  917. } \
  918. static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
  919. INPUT_DEV_CAP_ATTR(EV, ev);
  920. INPUT_DEV_CAP_ATTR(KEY, key);
  921. INPUT_DEV_CAP_ATTR(REL, rel);
  922. INPUT_DEV_CAP_ATTR(ABS, abs);
  923. INPUT_DEV_CAP_ATTR(MSC, msc);
  924. INPUT_DEV_CAP_ATTR(LED, led);
  925. INPUT_DEV_CAP_ATTR(SND, snd);
  926. INPUT_DEV_CAP_ATTR(FF, ff);
  927. INPUT_DEV_CAP_ATTR(SW, sw);
  928. static struct attribute *input_dev_caps_attrs[] = {
  929. &dev_attr_ev.attr,
  930. &dev_attr_key.attr,
  931. &dev_attr_rel.attr,
  932. &dev_attr_abs.attr,
  933. &dev_attr_msc.attr,
  934. &dev_attr_led.attr,
  935. &dev_attr_snd.attr,
  936. &dev_attr_ff.attr,
  937. &dev_attr_sw.attr,
  938. NULL
  939. };
  940. static struct attribute_group input_dev_caps_attr_group = {
  941. .name = "capabilities",
  942. .attrs = input_dev_caps_attrs,
  943. };
  944. static const struct attribute_group *input_dev_attr_groups[] = {
  945. &input_dev_attr_group,
  946. &input_dev_id_attr_group,
  947. &input_dev_caps_attr_group,
  948. NULL
  949. };
  950. static void input_dev_release(struct device *device)
  951. {
  952. struct input_dev *dev = to_input_dev(device);
  953. input_ff_destroy(dev);
  954. kfree(dev);
  955. module_put(THIS_MODULE);
  956. }
  957. /*
  958. * Input uevent interface - loading event handlers based on
  959. * device bitfields.
  960. */
  961. static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
  962. const char *name, unsigned long *bitmap, int max)
  963. {
  964. int len;
  965. if (add_uevent_var(env, "%s=", name))
  966. return -ENOMEM;
  967. len = input_print_bitmap(&env->buf[env->buflen - 1],
  968. sizeof(env->buf) - env->buflen,
  969. bitmap, max, 0);
  970. if (len >= (sizeof(env->buf) - env->buflen))
  971. return -ENOMEM;
  972. env->buflen += len;
  973. return 0;
  974. }
  975. static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
  976. struct input_dev *dev)
  977. {
  978. int len;
  979. if (add_uevent_var(env, "MODALIAS="))
  980. return -ENOMEM;
  981. len = input_print_modalias(&env->buf[env->buflen - 1],
  982. sizeof(env->buf) - env->buflen,
  983. dev, 0);
  984. if (len >= (sizeof(env->buf) - env->buflen))
  985. return -ENOMEM;
  986. env->buflen += len;
  987. return 0;
  988. }
  989. #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
  990. do { \
  991. int err = add_uevent_var(env, fmt, val); \
  992. if (err) \
  993. return err; \
  994. } while (0)
  995. #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
  996. do { \
  997. int err = input_add_uevent_bm_var(env, name, bm, max); \
  998. if (err) \
  999. return err; \
  1000. } while (0)
  1001. #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
  1002. do { \
  1003. int err = input_add_uevent_modalias_var(env, dev); \
  1004. if (err) \
  1005. return err; \
  1006. } while (0)
  1007. static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  1008. {
  1009. struct input_dev *dev = to_input_dev(device);
  1010. INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
  1011. dev->id.bustype, dev->id.vendor,
  1012. dev->id.product, dev->id.version);
  1013. if (dev->name)
  1014. INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
  1015. if (dev->phys)
  1016. INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
  1017. if (dev->uniq)
  1018. INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
  1019. INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
  1020. if (test_bit(EV_KEY, dev->evbit))
  1021. INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
  1022. if (test_bit(EV_REL, dev->evbit))
  1023. INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
  1024. if (test_bit(EV_ABS, dev->evbit))
  1025. INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
  1026. if (test_bit(EV_MSC, dev->evbit))
  1027. INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
  1028. if (test_bit(EV_LED, dev->evbit))
  1029. INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
  1030. if (test_bit(EV_SND, dev->evbit))
  1031. INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
  1032. if (test_bit(EV_FF, dev->evbit))
  1033. INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
  1034. if (test_bit(EV_SW, dev->evbit))
  1035. INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
  1036. INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
  1037. return 0;
  1038. }
  1039. #define INPUT_DO_TOGGLE(dev, type, bits, on) \
  1040. do { \
  1041. int i; \
  1042. if (!test_bit(EV_##type, dev->evbit)) \
  1043. break; \
  1044. for (i = 0; i < type##_MAX; i++) { \
  1045. if (!test_bit(i, dev->bits##bit) || \
  1046. !test_bit(i, dev->bits)) \
  1047. continue; \
  1048. dev->event(dev, EV_##type, i, on); \
  1049. } \
  1050. } while (0)
  1051. static void input_dev_reset(struct input_dev *dev, bool activate)
  1052. {
  1053. if (!dev->event)
  1054. return;
  1055. INPUT_DO_TOGGLE(dev, LED, led, activate);
  1056. INPUT_DO_TOGGLE(dev, SND, snd, activate);
  1057. if (activate && test_bit(EV_REP, dev->evbit)) {
  1058. dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
  1059. dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
  1060. }
  1061. }
  1062. #ifdef CONFIG_PM
  1063. static int input_dev_suspend(struct device *dev)
  1064. {
  1065. struct input_dev *input_dev = to_input_dev(dev);
  1066. mutex_lock(&input_dev->mutex);
  1067. input_dev_reset(input_dev, false);
  1068. mutex_unlock(&input_dev->mutex);
  1069. return 0;
  1070. }
  1071. static int input_dev_resume(struct device *dev)
  1072. {
  1073. struct input_dev *input_dev = to_input_dev(dev);
  1074. mutex_lock(&input_dev->mutex);
  1075. input_dev_reset(input_dev, true);
  1076. mutex_unlock(&input_dev->mutex);
  1077. return 0;
  1078. }
  1079. static const struct dev_pm_ops input_dev_pm_ops = {
  1080. .suspend = input_dev_suspend,
  1081. .resume = input_dev_resume,
  1082. .poweroff = input_dev_suspend,
  1083. .restore = input_dev_resume,
  1084. };
  1085. #endif /* CONFIG_PM */
  1086. static struct device_type input_dev_type = {
  1087. .groups = input_dev_attr_groups,
  1088. .release = input_dev_release,
  1089. .uevent = input_dev_uevent,
  1090. #ifdef CONFIG_PM
  1091. .pm = &input_dev_pm_ops,
  1092. #endif
  1093. };
  1094. static char *input_devnode(struct device *dev, mode_t *mode)
  1095. {
  1096. return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
  1097. }
  1098. struct class input_class = {
  1099. .name = "input",
  1100. .devnode = input_devnode,
  1101. };
  1102. EXPORT_SYMBOL_GPL(input_class);
  1103. /**
  1104. * input_allocate_device - allocate memory for new input device
  1105. *
  1106. * Returns prepared struct input_dev or NULL.
  1107. *
  1108. * NOTE: Use input_free_device() to free devices that have not been
  1109. * registered; input_unregister_device() should be used for already
  1110. * registered devices.
  1111. */
  1112. struct input_dev *input_allocate_device(void)
  1113. {
  1114. struct input_dev *dev;
  1115. dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  1116. if (dev) {
  1117. dev->dev.type = &input_dev_type;
  1118. dev->dev.class = &input_class;
  1119. device_initialize(&dev->dev);
  1120. mutex_init(&dev->mutex);
  1121. spin_lock_init(&dev->event_lock);
  1122. INIT_LIST_HEAD(&dev->h_list);
  1123. INIT_LIST_HEAD(&dev->node);
  1124. __module_get(THIS_MODULE);
  1125. }
  1126. return dev;
  1127. }
  1128. EXPORT_SYMBOL(input_allocate_device);
  1129. /**
  1130. * input_free_device - free memory occupied by input_dev structure
  1131. * @dev: input device to free
  1132. *
  1133. * This function should only be used if input_register_device()
  1134. * was not called yet or if it failed. Once device was registered
  1135. * use input_unregister_device() and memory will be freed once last
  1136. * reference to the device is dropped.
  1137. *
  1138. * Device should be allocated by input_allocate_device().
  1139. *
  1140. * NOTE: If there are references to the input device then memory
  1141. * will not be freed until last reference is dropped.
  1142. */
  1143. void input_free_device(struct input_dev *dev)
  1144. {
  1145. if (dev)
  1146. input_put_device(dev);
  1147. }
  1148. EXPORT_SYMBOL(input_free_device);
  1149. /**
  1150. * input_set_capability - mark device as capable of a certain event
  1151. * @dev: device that is capable of emitting or accepting event
  1152. * @type: type of the event (EV_KEY, EV_REL, etc...)
  1153. * @code: event code
  1154. *
  1155. * In addition to setting up corresponding bit in appropriate capability
  1156. * bitmap the function also adjusts dev->evbit.
  1157. */
  1158. void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
  1159. {
  1160. switch (type) {
  1161. case EV_KEY:
  1162. __set_bit(code, dev->keybit);
  1163. break;
  1164. case EV_REL:
  1165. __set_bit(code, dev->relbit);
  1166. break;
  1167. case EV_ABS:
  1168. __set_bit(code, dev->absbit);
  1169. break;
  1170. case EV_MSC:
  1171. __set_bit(code, dev->mscbit);
  1172. break;
  1173. case EV_SW:
  1174. __set_bit(code, dev->swbit);
  1175. break;
  1176. case EV_LED:
  1177. __set_bit(code, dev->ledbit);
  1178. break;
  1179. case EV_SND:
  1180. __set_bit(code, dev->sndbit);
  1181. break;
  1182. case EV_FF:
  1183. __set_bit(code, dev->ffbit);
  1184. break;
  1185. case EV_PWR:
  1186. /* do nothing */
  1187. break;
  1188. default:
  1189. printk(KERN_ERR
  1190. "input_set_capability: unknown type %u (code %u)\n",
  1191. type, code);
  1192. dump_stack();
  1193. return;
  1194. }
  1195. __set_bit(type, dev->evbit);
  1196. }
  1197. EXPORT_SYMBOL(input_set_capability);
  1198. /**
  1199. * input_register_device - register device with input core
  1200. * @dev: device to be registered
  1201. *
  1202. * This function registers device with input core. The device must be
  1203. * allocated with input_allocate_device() and all it's capabilities
  1204. * set up before registering.
  1205. * If function fails the device must be freed with input_free_device().
  1206. * Once device has been successfully registered it can be unregistered
  1207. * with input_unregister_device(); input_free_device() should not be
  1208. * called in this case.
  1209. */
  1210. int input_register_device(struct input_dev *dev)
  1211. {
  1212. static atomic_t input_no = ATOMIC_INIT(0);
  1213. struct input_handler *handler;
  1214. const char *path;
  1215. int error;
  1216. __set_bit(EV_SYN, dev->evbit);
  1217. /*
  1218. * If delay and period are pre-set by the driver, then autorepeating
  1219. * is handled by the driver itself and we don't do it in input.c.
  1220. */
  1221. init_timer(&dev->timer);
  1222. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  1223. dev->timer.data = (long) dev;
  1224. dev->timer.function = input_repeat_key;
  1225. dev->rep[REP_DELAY] = 250;
  1226. dev->rep[REP_PERIOD] = 33;
  1227. }
  1228. if (!dev->getkeycode)
  1229. dev->getkeycode = input_default_getkeycode;
  1230. if (!dev->setkeycode)
  1231. dev->setkeycode = input_default_setkeycode;
  1232. dev_set_name(&dev->dev, "input%ld",
  1233. (unsigned long) atomic_inc_return(&input_no) - 1);
  1234. error = device_add(&dev->dev);
  1235. if (error)
  1236. return error;
  1237. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1238. printk(KERN_INFO "input: %s as %s\n",
  1239. dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
  1240. kfree(path);
  1241. error = mutex_lock_interruptible(&input_mutex);
  1242. if (error) {
  1243. device_del(&dev->dev);
  1244. return error;
  1245. }
  1246. list_add_tail(&dev->node, &input_dev_list);
  1247. list_for_each_entry(handler, &input_handler_list, node)
  1248. input_attach_handler(dev, handler);
  1249. input_wakeup_procfs_readers();
  1250. mutex_unlock(&input_mutex);
  1251. return 0;
  1252. }
  1253. EXPORT_SYMBOL(input_register_device);
  1254. /**
  1255. * input_unregister_device - unregister previously registered device
  1256. * @dev: device to be unregistered
  1257. *
  1258. * This function unregisters an input device. Once device is unregistered
  1259. * the caller should not try to access it as it may get freed at any moment.
  1260. */
  1261. void input_unregister_device(struct input_dev *dev)
  1262. {
  1263. struct input_handle *handle, *next;
  1264. input_disconnect_device(dev);
  1265. mutex_lock(&input_mutex);
  1266. list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
  1267. handle->handler->disconnect(handle);
  1268. WARN_ON(!list_empty(&dev->h_list));
  1269. del_timer_sync(&dev->timer);
  1270. list_del_init(&dev->node);
  1271. input_wakeup_procfs_readers();
  1272. mutex_unlock(&input_mutex);
  1273. device_unregister(&dev->dev);
  1274. }
  1275. EXPORT_SYMBOL(input_unregister_device);
  1276. /**
  1277. * input_register_handler - register a new input handler
  1278. * @handler: handler to be registered
  1279. *
  1280. * This function registers a new input handler (interface) for input
  1281. * devices in the system and attaches it to all input devices that
  1282. * are compatible with the handler.
  1283. */
  1284. int input_register_handler(struct input_handler *handler)
  1285. {
  1286. struct input_dev *dev;
  1287. int retval;
  1288. retval = mutex_lock_interruptible(&input_mutex);
  1289. if (retval)
  1290. return retval;
  1291. INIT_LIST_HEAD(&handler->h_list);
  1292. if (handler->fops != NULL) {
  1293. if (input_table[handler->minor >> 5]) {
  1294. retval = -EBUSY;
  1295. goto out;
  1296. }
  1297. input_table[handler->minor >> 5] = handler;
  1298. }
  1299. list_add_tail(&handler->node, &input_handler_list);
  1300. list_for_each_entry(dev, &input_dev_list, node)
  1301. input_attach_handler(dev, handler);
  1302. input_wakeup_procfs_readers();
  1303. out:
  1304. mutex_unlock(&input_mutex);
  1305. return retval;
  1306. }
  1307. EXPORT_SYMBOL(input_register_handler);
  1308. /**
  1309. * input_unregister_handler - unregisters an input handler
  1310. * @handler: handler to be unregistered
  1311. *
  1312. * This function disconnects a handler from its input devices and
  1313. * removes it from lists of known handlers.
  1314. */
  1315. void input_unregister_handler(struct input_handler *handler)
  1316. {
  1317. struct input_handle *handle, *next;
  1318. mutex_lock(&input_mutex);
  1319. list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
  1320. handler->disconnect(handle);
  1321. WARN_ON(!list_empty(&handler->h_list));
  1322. list_del_init(&handler->node);
  1323. if (handler->fops != NULL)
  1324. input_table[handler->minor >> 5] = NULL;
  1325. input_wakeup_procfs_readers();
  1326. mutex_unlock(&input_mutex);
  1327. }
  1328. EXPORT_SYMBOL(input_unregister_handler);
  1329. /**
  1330. * input_register_handle - register a new input handle
  1331. * @handle: handle to register
  1332. *
  1333. * This function puts a new input handle onto device's
  1334. * and handler's lists so that events can flow through
  1335. * it once it is opened using input_open_device().
  1336. *
  1337. * This function is supposed to be called from handler's
  1338. * connect() method.
  1339. */
  1340. int input_register_handle(struct input_handle *handle)
  1341. {
  1342. struct input_handler *handler = handle->handler;
  1343. struct input_dev *dev = handle->dev;
  1344. int error;
  1345. /*
  1346. * We take dev->mutex here to prevent race with
  1347. * input_release_device().
  1348. */
  1349. error = mutex_lock_interruptible(&dev->mutex);
  1350. if (error)
  1351. return error;
  1352. list_add_tail_rcu(&handle->d_node, &dev->h_list);
  1353. mutex_unlock(&dev->mutex);
  1354. /*
  1355. * Since we are supposed to be called from ->connect()
  1356. * which is mutually exclusive with ->disconnect()
  1357. * we can't be racing with input_unregister_handle()
  1358. * and so separate lock is not needed here.
  1359. */
  1360. list_add_tail(&handle->h_node, &handler->h_list);
  1361. if (handler->start)
  1362. handler->start(handle);
  1363. return 0;
  1364. }
  1365. EXPORT_SYMBOL(input_register_handle);
  1366. /**
  1367. * input_unregister_handle - unregister an input handle
  1368. * @handle: handle to unregister
  1369. *
  1370. * This function removes input handle from device's
  1371. * and handler's lists.
  1372. *
  1373. * This function is supposed to be called from handler's
  1374. * disconnect() method.
  1375. */
  1376. void input_unregister_handle(struct input_handle *handle)
  1377. {
  1378. struct input_dev *dev = handle->dev;
  1379. list_del_init(&handle->h_node);
  1380. /*
  1381. * Take dev->mutex to prevent race with input_release_device().
  1382. */
  1383. mutex_lock(&dev->mutex);
  1384. list_del_rcu(&handle->d_node);
  1385. mutex_unlock(&dev->mutex);
  1386. synchronize_rcu();
  1387. }
  1388. EXPORT_SYMBOL(input_unregister_handle);
  1389. static int input_open_file(struct inode *inode, struct file *file)
  1390. {
  1391. struct input_handler *handler;
  1392. const struct file_operations *old_fops, *new_fops = NULL;
  1393. int err;
  1394. lock_kernel();
  1395. /* No load-on-demand here? */
  1396. handler = input_table[iminor(inode) >> 5];
  1397. if (!handler || !(new_fops = fops_get(handler->fops))) {
  1398. err = -ENODEV;
  1399. goto out;
  1400. }
  1401. /*
  1402. * That's _really_ odd. Usually NULL ->open means "nothing special",
  1403. * not "no device". Oh, well...
  1404. */
  1405. if (!new_fops->open) {
  1406. fops_put(new_fops);
  1407. err = -ENODEV;
  1408. goto out;
  1409. }
  1410. old_fops = file->f_op;
  1411. file->f_op = new_fops;
  1412. err = new_fops->open(inode, file);
  1413. if (err) {
  1414. fops_put(file->f_op);
  1415. file->f_op = fops_get(old_fops);
  1416. }
  1417. fops_put(old_fops);
  1418. out:
  1419. unlock_kernel();
  1420. return err;
  1421. }
  1422. static const struct file_operations input_fops = {
  1423. .owner = THIS_MODULE,
  1424. .open = input_open_file,
  1425. };
  1426. static void __init input_init_abs_bypass(void)
  1427. {
  1428. const unsigned int *p;
  1429. for (p = input_abs_bypass_init_data; *p; p++)
  1430. input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
  1431. }
  1432. static int __init input_init(void)
  1433. {
  1434. int err;
  1435. input_init_abs_bypass();
  1436. err = class_register(&input_class);
  1437. if (err) {
  1438. printk(KERN_ERR "input: unable to register input_dev class\n");
  1439. return err;
  1440. }
  1441. err = input_proc_init();
  1442. if (err)
  1443. goto fail1;
  1444. err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
  1445. if (err) {
  1446. printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
  1447. goto fail2;
  1448. }
  1449. return 0;
  1450. fail2: input_proc_exit();
  1451. fail1: class_unregister(&input_class);
  1452. return err;
  1453. }
  1454. static void __exit input_exit(void)
  1455. {
  1456. input_proc_exit();
  1457. unregister_chrdev(INPUT_MAJOR, "input");
  1458. class_unregister(&input_class);
  1459. }
  1460. subsys_initcall(input_init);
  1461. module_exit(input_exit);