input.c 53 KB

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