input.c 53 KB

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