input.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * The input core
  4. *
  5. * Copyright (c) 1999-2002 Vojtech Pavlik
  6. */
  7. #define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
  8. #include <linux/init.h>
  9. #include <linux/types.h>
  10. #include <linux/idr.h>
  11. #include <linux/input/mt.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/random.h>
  15. #include <linux/major.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/sched.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/pm.h>
  20. #include <linux/poll.h>
  21. #include <linux/device.h>
  22. #include <linux/kstrtox.h>
  23. #include <linux/mutex.h>
  24. #include <linux/rcupdate.h>
  25. #include "input-compat.h"
  26. #include "input-core-private.h"
  27. #include "input-poller.h"
  28. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  29. MODULE_DESCRIPTION("Input core");
  30. MODULE_LICENSE("GPL");
  31. #define INPUT_MAX_CHAR_DEVICES 1024
  32. #define INPUT_FIRST_DYNAMIC_DEV 256
  33. static DEFINE_IDA(input_ida);
  34. static LIST_HEAD(input_dev_list);
  35. static LIST_HEAD(input_handler_list);
  36. /*
  37. * input_mutex protects access to both input_dev_list and input_handler_list.
  38. * This also causes input_[un]register_device and input_[un]register_handler
  39. * be mutually exclusive which simplifies locking in drivers implementing
  40. * input handlers.
  41. */
  42. static DEFINE_MUTEX(input_mutex);
  43. static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 };
  44. static const unsigned int input_max_code[EV_CNT] = {
  45. [EV_KEY] = KEY_MAX,
  46. [EV_REL] = REL_MAX,
  47. [EV_ABS] = ABS_MAX,
  48. [EV_MSC] = MSC_MAX,
  49. [EV_SW] = SW_MAX,
  50. [EV_LED] = LED_MAX,
  51. [EV_SND] = SND_MAX,
  52. [EV_FF] = FF_MAX,
  53. };
  54. static inline int is_event_supported(unsigned int code,
  55. unsigned long *bm, unsigned int max)
  56. {
  57. return code <= max && test_bit(code, bm);
  58. }
  59. static int input_defuzz_abs_event(int value, int old_val, int fuzz)
  60. {
  61. if (fuzz) {
  62. if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
  63. return old_val;
  64. if (value > old_val - fuzz && value < old_val + fuzz)
  65. return (old_val * 3 + value) / 4;
  66. if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
  67. return (old_val + value) / 2;
  68. }
  69. return value;
  70. }
  71. static void input_start_autorepeat(struct input_dev *dev, int code)
  72. {
  73. if (test_bit(EV_REP, dev->evbit) &&
  74. dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
  75. dev->timer.function) {
  76. dev->repeat_key = code;
  77. mod_timer(&dev->timer,
  78. jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
  79. }
  80. }
  81. static void input_stop_autorepeat(struct input_dev *dev)
  82. {
  83. del_timer(&dev->timer);
  84. }
  85. /*
  86. * Pass values first through all filters and then, if event has not been
  87. * filtered out, through all open handles. This order is achieved by placing
  88. * filters at the head of the list of handles attached to the device, and
  89. * placing regular handles at the tail of the list.
  90. *
  91. * This function is called with dev->event_lock held and interrupts disabled.
  92. */
  93. static void input_pass_values(struct input_dev *dev,
  94. struct input_value *vals, unsigned int count)
  95. {
  96. struct input_handle *handle;
  97. struct input_value *v;
  98. lockdep_assert_held(&dev->event_lock);
  99. rcu_read_lock();
  100. handle = rcu_dereference(dev->grab);
  101. if (handle) {
  102. count = handle->handle_events(handle, vals, count);
  103. } else {
  104. list_for_each_entry_rcu(handle, &dev->h_list, d_node)
  105. if (handle->open) {
  106. count = handle->handle_events(handle, vals,
  107. count);
  108. if (!count)
  109. break;
  110. }
  111. }
  112. rcu_read_unlock();
  113. /* trigger auto repeat for key events */
  114. if (test_bit(EV_REP, dev->evbit) && test_bit(EV_KEY, dev->evbit)) {
  115. for (v = vals; v != vals + count; v++) {
  116. if (v->type == EV_KEY && v->value != 2) {
  117. if (v->value)
  118. input_start_autorepeat(dev, v->code);
  119. else
  120. input_stop_autorepeat(dev);
  121. }
  122. }
  123. }
  124. }
  125. #define INPUT_IGNORE_EVENT 0
  126. #define INPUT_PASS_TO_HANDLERS 1
  127. #define INPUT_PASS_TO_DEVICE 2
  128. #define INPUT_SLOT 4
  129. #define INPUT_FLUSH 8
  130. #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
  131. static int input_handle_abs_event(struct input_dev *dev,
  132. unsigned int code, int *pval)
  133. {
  134. struct input_mt *mt = dev->mt;
  135. bool is_new_slot = false;
  136. bool is_mt_event;
  137. int *pold;
  138. if (code == ABS_MT_SLOT) {
  139. /*
  140. * "Stage" the event; we'll flush it later, when we
  141. * get actual touch data.
  142. */
  143. if (mt && *pval >= 0 && *pval < mt->num_slots)
  144. mt->slot = *pval;
  145. return INPUT_IGNORE_EVENT;
  146. }
  147. is_mt_event = input_is_mt_value(code);
  148. if (!is_mt_event) {
  149. pold = &dev->absinfo[code].value;
  150. } else if (mt) {
  151. pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST];
  152. is_new_slot = mt->slot != dev->absinfo[ABS_MT_SLOT].value;
  153. } else {
  154. /*
  155. * Bypass filtering for multi-touch events when
  156. * not employing slots.
  157. */
  158. pold = NULL;
  159. }
  160. if (pold) {
  161. *pval = input_defuzz_abs_event(*pval, *pold,
  162. dev->absinfo[code].fuzz);
  163. if (*pold == *pval)
  164. return INPUT_IGNORE_EVENT;
  165. *pold = *pval;
  166. }
  167. /* Flush pending "slot" event */
  168. if (is_new_slot) {
  169. dev->absinfo[ABS_MT_SLOT].value = mt->slot;
  170. return INPUT_PASS_TO_HANDLERS | INPUT_SLOT;
  171. }
  172. return INPUT_PASS_TO_HANDLERS;
  173. }
  174. static int input_get_disposition(struct input_dev *dev,
  175. unsigned int type, unsigned int code, int *pval)
  176. {
  177. int disposition = INPUT_IGNORE_EVENT;
  178. int value = *pval;
  179. /* filter-out events from inhibited devices */
  180. if (dev->inhibited)
  181. return INPUT_IGNORE_EVENT;
  182. switch (type) {
  183. case EV_SYN:
  184. switch (code) {
  185. case SYN_CONFIG:
  186. disposition = INPUT_PASS_TO_ALL;
  187. break;
  188. case SYN_REPORT:
  189. disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;
  190. break;
  191. case SYN_MT_REPORT:
  192. disposition = INPUT_PASS_TO_HANDLERS;
  193. break;
  194. }
  195. break;
  196. case EV_KEY:
  197. if (is_event_supported(code, dev->keybit, KEY_MAX)) {
  198. /* auto-repeat bypasses state updates */
  199. if (value == 2) {
  200. disposition = INPUT_PASS_TO_HANDLERS;
  201. break;
  202. }
  203. if (!!test_bit(code, dev->key) != !!value) {
  204. __change_bit(code, dev->key);
  205. disposition = INPUT_PASS_TO_HANDLERS;
  206. }
  207. }
  208. break;
  209. case EV_SW:
  210. if (is_event_supported(code, dev->swbit, SW_MAX) &&
  211. !!test_bit(code, dev->sw) != !!value) {
  212. __change_bit(code, dev->sw);
  213. disposition = INPUT_PASS_TO_HANDLERS;
  214. }
  215. break;
  216. case EV_ABS:
  217. if (is_event_supported(code, dev->absbit, ABS_MAX))
  218. disposition = input_handle_abs_event(dev, code, &value);
  219. break;
  220. case EV_REL:
  221. if (is_event_supported(code, dev->relbit, REL_MAX) && value)
  222. disposition = INPUT_PASS_TO_HANDLERS;
  223. break;
  224. case EV_MSC:
  225. if (is_event_supported(code, dev->mscbit, MSC_MAX))
  226. disposition = INPUT_PASS_TO_ALL;
  227. break;
  228. case EV_LED:
  229. if (is_event_supported(code, dev->ledbit, LED_MAX) &&
  230. !!test_bit(code, dev->led) != !!value) {
  231. __change_bit(code, dev->led);
  232. disposition = INPUT_PASS_TO_ALL;
  233. }
  234. break;
  235. case EV_SND:
  236. if (is_event_supported(code, dev->sndbit, SND_MAX)) {
  237. if (!!test_bit(code, dev->snd) != !!value)
  238. __change_bit(code, dev->snd);
  239. disposition = INPUT_PASS_TO_ALL;
  240. }
  241. break;
  242. case EV_REP:
  243. if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
  244. dev->rep[code] = value;
  245. disposition = INPUT_PASS_TO_ALL;
  246. }
  247. break;
  248. case EV_FF:
  249. if (value >= 0)
  250. disposition = INPUT_PASS_TO_ALL;
  251. break;
  252. case EV_PWR:
  253. disposition = INPUT_PASS_TO_ALL;
  254. break;
  255. }
  256. *pval = value;
  257. return disposition;
  258. }
  259. static void input_event_dispose(struct input_dev *dev, int disposition,
  260. unsigned int type, unsigned int code, int value)
  261. {
  262. if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
  263. dev->event(dev, type, code, value);
  264. if (disposition & INPUT_PASS_TO_HANDLERS) {
  265. struct input_value *v;
  266. if (disposition & INPUT_SLOT) {
  267. v = &dev->vals[dev->num_vals++];
  268. v->type = EV_ABS;
  269. v->code = ABS_MT_SLOT;
  270. v->value = dev->mt->slot;
  271. }
  272. v = &dev->vals[dev->num_vals++];
  273. v->type = type;
  274. v->code = code;
  275. v->value = value;
  276. }
  277. if (disposition & INPUT_FLUSH) {
  278. if (dev->num_vals >= 2)
  279. input_pass_values(dev, dev->vals, dev->num_vals);
  280. dev->num_vals = 0;
  281. /*
  282. * Reset the timestamp on flush so we won't end up
  283. * with a stale one. Note we only need to reset the
  284. * monolithic one as we use its presence when deciding
  285. * whether to generate a synthetic timestamp.
  286. */
  287. dev->timestamp[INPUT_CLK_MONO] = ktime_set(0, 0);
  288. } else if (dev->num_vals >= dev->max_vals - 2) {
  289. dev->vals[dev->num_vals++] = input_value_sync;
  290. input_pass_values(dev, dev->vals, dev->num_vals);
  291. dev->num_vals = 0;
  292. }
  293. }
  294. void input_handle_event(struct input_dev *dev,
  295. unsigned int type, unsigned int code, int value)
  296. {
  297. int disposition;
  298. lockdep_assert_held(&dev->event_lock);
  299. disposition = input_get_disposition(dev, type, code, &value);
  300. if (disposition != INPUT_IGNORE_EVENT) {
  301. if (type != EV_SYN)
  302. add_input_randomness(type, code, value);
  303. input_event_dispose(dev, disposition, type, code, value);
  304. }
  305. }
  306. /**
  307. * input_event() - report new input event
  308. * @dev: device that generated the event
  309. * @type: type of the event
  310. * @code: event code
  311. * @value: value of the event
  312. *
  313. * This function should be used by drivers implementing various input
  314. * devices to report input events. See also input_inject_event().
  315. *
  316. * NOTE: input_event() may be safely used right after input device was
  317. * allocated with input_allocate_device(), even before it is registered
  318. * with input_register_device(), but the event will not reach any of the
  319. * input handlers. Such early invocation of input_event() may be used
  320. * to 'seed' initial state of a switch or initial position of absolute
  321. * axis, etc.
  322. */
  323. void input_event(struct input_dev *dev,
  324. unsigned int type, unsigned int code, int value)
  325. {
  326. unsigned long flags;
  327. if (is_event_supported(type, dev->evbit, EV_MAX)) {
  328. spin_lock_irqsave(&dev->event_lock, flags);
  329. input_handle_event(dev, type, code, value);
  330. spin_unlock_irqrestore(&dev->event_lock, flags);
  331. }
  332. }
  333. EXPORT_SYMBOL(input_event);
  334. /**
  335. * input_inject_event() - send input event from input handler
  336. * @handle: input handle to send event through
  337. * @type: type of the event
  338. * @code: event code
  339. * @value: value of the event
  340. *
  341. * Similar to input_event() but will ignore event if device is
  342. * "grabbed" and handle injecting event is not the one that owns
  343. * the device.
  344. */
  345. void input_inject_event(struct input_handle *handle,
  346. unsigned int type, unsigned int code, int value)
  347. {
  348. struct input_dev *dev = handle->dev;
  349. struct input_handle *grab;
  350. unsigned long flags;
  351. if (is_event_supported(type, dev->evbit, EV_MAX)) {
  352. spin_lock_irqsave(&dev->event_lock, flags);
  353. rcu_read_lock();
  354. grab = rcu_dereference(dev->grab);
  355. if (!grab || grab == handle)
  356. input_handle_event(dev, type, code, value);
  357. rcu_read_unlock();
  358. spin_unlock_irqrestore(&dev->event_lock, flags);
  359. }
  360. }
  361. EXPORT_SYMBOL(input_inject_event);
  362. /**
  363. * input_alloc_absinfo - allocates array of input_absinfo structs
  364. * @dev: the input device emitting absolute events
  365. *
  366. * If the absinfo struct the caller asked for is already allocated, this
  367. * functions will not do anything.
  368. */
  369. void input_alloc_absinfo(struct input_dev *dev)
  370. {
  371. if (dev->absinfo)
  372. return;
  373. dev->absinfo = kcalloc(ABS_CNT, sizeof(*dev->absinfo), GFP_KERNEL);
  374. if (!dev->absinfo) {
  375. dev_err(dev->dev.parent ?: &dev->dev,
  376. "%s: unable to allocate memory\n", __func__);
  377. /*
  378. * We will handle this allocation failure in
  379. * input_register_device() when we refuse to register input
  380. * device with ABS bits but without absinfo.
  381. */
  382. }
  383. }
  384. EXPORT_SYMBOL(input_alloc_absinfo);
  385. void input_set_abs_params(struct input_dev *dev, unsigned int axis,
  386. int min, int max, int fuzz, int flat)
  387. {
  388. struct input_absinfo *absinfo;
  389. __set_bit(EV_ABS, dev->evbit);
  390. __set_bit(axis, dev->absbit);
  391. input_alloc_absinfo(dev);
  392. if (!dev->absinfo)
  393. return;
  394. absinfo = &dev->absinfo[axis];
  395. absinfo->minimum = min;
  396. absinfo->maximum = max;
  397. absinfo->fuzz = fuzz;
  398. absinfo->flat = flat;
  399. }
  400. EXPORT_SYMBOL(input_set_abs_params);
  401. /**
  402. * input_copy_abs - Copy absinfo from one input_dev to another
  403. * @dst: Destination input device to copy the abs settings to
  404. * @dst_axis: ABS_* value selecting the destination axis
  405. * @src: Source input device to copy the abs settings from
  406. * @src_axis: ABS_* value selecting the source axis
  407. *
  408. * Set absinfo for the selected destination axis by copying it from
  409. * the specified source input device's source axis.
  410. * This is useful to e.g. setup a pen/stylus input-device for combined
  411. * touchscreen/pen hardware where the pen uses the same coordinates as
  412. * the touchscreen.
  413. */
  414. void input_copy_abs(struct input_dev *dst, unsigned int dst_axis,
  415. const struct input_dev *src, unsigned int src_axis)
  416. {
  417. /* src must have EV_ABS and src_axis set */
  418. if (WARN_ON(!(test_bit(EV_ABS, src->evbit) &&
  419. test_bit(src_axis, src->absbit))))
  420. return;
  421. /*
  422. * input_alloc_absinfo() may have failed for the source. Our caller is
  423. * expected to catch this when registering the input devices, which may
  424. * happen after the input_copy_abs() call.
  425. */
  426. if (!src->absinfo)
  427. return;
  428. input_set_capability(dst, EV_ABS, dst_axis);
  429. if (!dst->absinfo)
  430. return;
  431. dst->absinfo[dst_axis] = src->absinfo[src_axis];
  432. }
  433. EXPORT_SYMBOL(input_copy_abs);
  434. /**
  435. * input_grab_device - grabs device for exclusive use
  436. * @handle: input handle that wants to own the device
  437. *
  438. * When a device is grabbed by an input handle all events generated by
  439. * the device are delivered only to this handle. Also events injected
  440. * by other input handles are ignored while device is grabbed.
  441. */
  442. int input_grab_device(struct input_handle *handle)
  443. {
  444. struct input_dev *dev = handle->dev;
  445. int retval;
  446. retval = mutex_lock_interruptible(&dev->mutex);
  447. if (retval)
  448. return retval;
  449. if (dev->grab) {
  450. retval = -EBUSY;
  451. goto out;
  452. }
  453. rcu_assign_pointer(dev->grab, handle);
  454. out:
  455. mutex_unlock(&dev->mutex);
  456. return retval;
  457. }
  458. EXPORT_SYMBOL(input_grab_device);
  459. static void __input_release_device(struct input_handle *handle)
  460. {
  461. struct input_dev *dev = handle->dev;
  462. struct input_handle *grabber;
  463. grabber = rcu_dereference_protected(dev->grab,
  464. lockdep_is_held(&dev->mutex));
  465. if (grabber == handle) {
  466. rcu_assign_pointer(dev->grab, NULL);
  467. /* Make sure input_pass_values() notices that grab is gone */
  468. synchronize_rcu();
  469. list_for_each_entry(handle, &dev->h_list, d_node)
  470. if (handle->open && handle->handler->start)
  471. handle->handler->start(handle);
  472. }
  473. }
  474. /**
  475. * input_release_device - release previously grabbed device
  476. * @handle: input handle that owns the device
  477. *
  478. * Releases previously grabbed device so that other input handles can
  479. * start receiving input events. Upon release all handlers attached
  480. * to the device have their start() method called so they have a change
  481. * to synchronize device state with the rest of the system.
  482. */
  483. void input_release_device(struct input_handle *handle)
  484. {
  485. struct input_dev *dev = handle->dev;
  486. mutex_lock(&dev->mutex);
  487. __input_release_device(handle);
  488. mutex_unlock(&dev->mutex);
  489. }
  490. EXPORT_SYMBOL(input_release_device);
  491. /**
  492. * input_open_device - open input device
  493. * @handle: handle through which device is being accessed
  494. *
  495. * This function should be called by input handlers when they
  496. * want to start receive events from given input device.
  497. */
  498. int input_open_device(struct input_handle *handle)
  499. {
  500. struct input_dev *dev = handle->dev;
  501. int retval;
  502. retval = mutex_lock_interruptible(&dev->mutex);
  503. if (retval)
  504. return retval;
  505. if (dev->going_away) {
  506. retval = -ENODEV;
  507. goto out;
  508. }
  509. handle->open++;
  510. if (dev->users++ || dev->inhibited) {
  511. /*
  512. * Device is already opened and/or inhibited,
  513. * so we can exit immediately and report success.
  514. */
  515. goto out;
  516. }
  517. if (dev->open) {
  518. retval = dev->open(dev);
  519. if (retval) {
  520. dev->users--;
  521. handle->open--;
  522. /*
  523. * Make sure we are not delivering any more events
  524. * through this handle
  525. */
  526. synchronize_rcu();
  527. goto out;
  528. }
  529. }
  530. if (dev->poller)
  531. input_dev_poller_start(dev->poller);
  532. out:
  533. mutex_unlock(&dev->mutex);
  534. return retval;
  535. }
  536. EXPORT_SYMBOL(input_open_device);
  537. int input_flush_device(struct input_handle *handle, struct file *file)
  538. {
  539. struct input_dev *dev = handle->dev;
  540. int retval;
  541. retval = mutex_lock_interruptible(&dev->mutex);
  542. if (retval)
  543. return retval;
  544. if (dev->flush)
  545. retval = dev->flush(dev, file);
  546. mutex_unlock(&dev->mutex);
  547. return retval;
  548. }
  549. EXPORT_SYMBOL(input_flush_device);
  550. /**
  551. * input_close_device - close input device
  552. * @handle: handle through which device is being accessed
  553. *
  554. * This function should be called by input handlers when they
  555. * want to stop receive events from given input device.
  556. */
  557. void input_close_device(struct input_handle *handle)
  558. {
  559. struct input_dev *dev = handle->dev;
  560. mutex_lock(&dev->mutex);
  561. __input_release_device(handle);
  562. if (!--dev->users && !dev->inhibited) {
  563. if (dev->poller)
  564. input_dev_poller_stop(dev->poller);
  565. if (dev->close)
  566. dev->close(dev);
  567. }
  568. if (!--handle->open) {
  569. /*
  570. * synchronize_rcu() makes sure that input_pass_values()
  571. * completed and that no more input events are delivered
  572. * through this handle
  573. */
  574. synchronize_rcu();
  575. }
  576. mutex_unlock(&dev->mutex);
  577. }
  578. EXPORT_SYMBOL(input_close_device);
  579. /*
  580. * Simulate keyup events for all keys that are marked as pressed.
  581. * The function must be called with dev->event_lock held.
  582. */
  583. static bool input_dev_release_keys(struct input_dev *dev)
  584. {
  585. bool need_sync = false;
  586. int code;
  587. lockdep_assert_held(&dev->event_lock);
  588. if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
  589. for_each_set_bit(code, dev->key, KEY_CNT) {
  590. input_handle_event(dev, EV_KEY, code, 0);
  591. need_sync = true;
  592. }
  593. }
  594. return need_sync;
  595. }
  596. /*
  597. * Prepare device for unregistering
  598. */
  599. static void input_disconnect_device(struct input_dev *dev)
  600. {
  601. struct input_handle *handle;
  602. /*
  603. * Mark device as going away. Note that we take dev->mutex here
  604. * not to protect access to dev->going_away but rather to ensure
  605. * that there are no threads in the middle of input_open_device()
  606. */
  607. mutex_lock(&dev->mutex);
  608. dev->going_away = true;
  609. mutex_unlock(&dev->mutex);
  610. spin_lock_irq(&dev->event_lock);
  611. /*
  612. * Simulate keyup events for all pressed keys so that handlers
  613. * are not left with "stuck" keys. The driver may continue
  614. * generate events even after we done here but they will not
  615. * reach any handlers.
  616. */
  617. if (input_dev_release_keys(dev))
  618. input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
  619. list_for_each_entry(handle, &dev->h_list, d_node)
  620. handle->open = 0;
  621. spin_unlock_irq(&dev->event_lock);
  622. }
  623. /**
  624. * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry
  625. * @ke: keymap entry containing scancode to be converted.
  626. * @scancode: pointer to the location where converted scancode should
  627. * be stored.
  628. *
  629. * This function is used to convert scancode stored in &struct keymap_entry
  630. * into scalar form understood by legacy keymap handling methods. These
  631. * methods expect scancodes to be represented as 'unsigned int'.
  632. */
  633. int input_scancode_to_scalar(const struct input_keymap_entry *ke,
  634. unsigned int *scancode)
  635. {
  636. switch (ke->len) {
  637. case 1:
  638. *scancode = *((u8 *)ke->scancode);
  639. break;
  640. case 2:
  641. *scancode = *((u16 *)ke->scancode);
  642. break;
  643. case 4:
  644. *scancode = *((u32 *)ke->scancode);
  645. break;
  646. default:
  647. return -EINVAL;
  648. }
  649. return 0;
  650. }
  651. EXPORT_SYMBOL(input_scancode_to_scalar);
  652. /*
  653. * Those routines handle the default case where no [gs]etkeycode() is
  654. * defined. In this case, an array indexed by the scancode is used.
  655. */
  656. static unsigned int input_fetch_keycode(struct input_dev *dev,
  657. unsigned int index)
  658. {
  659. switch (dev->keycodesize) {
  660. case 1:
  661. return ((u8 *)dev->keycode)[index];
  662. case 2:
  663. return ((u16 *)dev->keycode)[index];
  664. default:
  665. return ((u32 *)dev->keycode)[index];
  666. }
  667. }
  668. static int input_default_getkeycode(struct input_dev *dev,
  669. struct input_keymap_entry *ke)
  670. {
  671. unsigned int index;
  672. int error;
  673. if (!dev->keycodesize)
  674. return -EINVAL;
  675. if (ke->flags & INPUT_KEYMAP_BY_INDEX)
  676. index = ke->index;
  677. else {
  678. error = input_scancode_to_scalar(ke, &index);
  679. if (error)
  680. return error;
  681. }
  682. if (index >= dev->keycodemax)
  683. return -EINVAL;
  684. ke->keycode = input_fetch_keycode(dev, index);
  685. ke->index = index;
  686. ke->len = sizeof(index);
  687. memcpy(ke->scancode, &index, sizeof(index));
  688. return 0;
  689. }
  690. static int input_default_setkeycode(struct input_dev *dev,
  691. const struct input_keymap_entry *ke,
  692. unsigned int *old_keycode)
  693. {
  694. unsigned int index;
  695. int error;
  696. int i;
  697. if (!dev->keycodesize)
  698. return -EINVAL;
  699. if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
  700. index = ke->index;
  701. } else {
  702. error = input_scancode_to_scalar(ke, &index);
  703. if (error)
  704. return error;
  705. }
  706. if (index >= dev->keycodemax)
  707. return -EINVAL;
  708. if (dev->keycodesize < sizeof(ke->keycode) &&
  709. (ke->keycode >> (dev->keycodesize * 8)))
  710. return -EINVAL;
  711. switch (dev->keycodesize) {
  712. case 1: {
  713. u8 *k = (u8 *)dev->keycode;
  714. *old_keycode = k[index];
  715. k[index] = ke->keycode;
  716. break;
  717. }
  718. case 2: {
  719. u16 *k = (u16 *)dev->keycode;
  720. *old_keycode = k[index];
  721. k[index] = ke->keycode;
  722. break;
  723. }
  724. default: {
  725. u32 *k = (u32 *)dev->keycode;
  726. *old_keycode = k[index];
  727. k[index] = ke->keycode;
  728. break;
  729. }
  730. }
  731. if (*old_keycode <= KEY_MAX) {
  732. __clear_bit(*old_keycode, dev->keybit);
  733. for (i = 0; i < dev->keycodemax; i++) {
  734. if (input_fetch_keycode(dev, i) == *old_keycode) {
  735. __set_bit(*old_keycode, dev->keybit);
  736. /* Setting the bit twice is useless, so break */
  737. break;
  738. }
  739. }
  740. }
  741. __set_bit(ke->keycode, dev->keybit);
  742. return 0;
  743. }
  744. /**
  745. * input_get_keycode - retrieve keycode currently mapped to a given scancode
  746. * @dev: input device which keymap is being queried
  747. * @ke: keymap entry
  748. *
  749. * This function should be called by anyone interested in retrieving current
  750. * keymap. Presently evdev handlers use it.
  751. */
  752. int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)
  753. {
  754. unsigned long flags;
  755. int retval;
  756. spin_lock_irqsave(&dev->event_lock, flags);
  757. retval = dev->getkeycode(dev, ke);
  758. spin_unlock_irqrestore(&dev->event_lock, flags);
  759. return retval;
  760. }
  761. EXPORT_SYMBOL(input_get_keycode);
  762. /**
  763. * input_set_keycode - attribute a keycode to a given scancode
  764. * @dev: input device which keymap is being updated
  765. * @ke: new keymap entry
  766. *
  767. * This function should be called by anyone needing to update current
  768. * keymap. Presently keyboard and evdev handlers use it.
  769. */
  770. int input_set_keycode(struct input_dev *dev,
  771. const struct input_keymap_entry *ke)
  772. {
  773. unsigned long flags;
  774. unsigned int old_keycode;
  775. int retval;
  776. if (ke->keycode > KEY_MAX)
  777. return -EINVAL;
  778. spin_lock_irqsave(&dev->event_lock, flags);
  779. retval = dev->setkeycode(dev, ke, &old_keycode);
  780. if (retval)
  781. goto out;
  782. /* Make sure KEY_RESERVED did not get enabled. */
  783. __clear_bit(KEY_RESERVED, dev->keybit);
  784. /*
  785. * Simulate keyup event if keycode is not present
  786. * in the keymap anymore
  787. */
  788. if (old_keycode > KEY_MAX) {
  789. dev_warn(dev->dev.parent ?: &dev->dev,
  790. "%s: got too big old keycode %#x\n",
  791. __func__, old_keycode);
  792. } else if (test_bit(EV_KEY, dev->evbit) &&
  793. !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
  794. __test_and_clear_bit(old_keycode, dev->key)) {
  795. /*
  796. * We have to use input_event_dispose() here directly instead
  797. * of input_handle_event() because the key we want to release
  798. * here is considered no longer supported by the device and
  799. * input_handle_event() will ignore it.
  800. */
  801. input_event_dispose(dev, INPUT_PASS_TO_HANDLERS,
  802. EV_KEY, old_keycode, 0);
  803. input_event_dispose(dev, INPUT_PASS_TO_HANDLERS | INPUT_FLUSH,
  804. EV_SYN, SYN_REPORT, 1);
  805. }
  806. out:
  807. spin_unlock_irqrestore(&dev->event_lock, flags);
  808. return retval;
  809. }
  810. EXPORT_SYMBOL(input_set_keycode);
  811. bool input_match_device_id(const struct input_dev *dev,
  812. const struct input_device_id *id)
  813. {
  814. if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  815. if (id->bustype != dev->id.bustype)
  816. return false;
  817. if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  818. if (id->vendor != dev->id.vendor)
  819. return false;
  820. if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  821. if (id->product != dev->id.product)
  822. return false;
  823. if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  824. if (id->version != dev->id.version)
  825. return false;
  826. if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX) ||
  827. !bitmap_subset(id->keybit, dev->keybit, KEY_MAX) ||
  828. !bitmap_subset(id->relbit, dev->relbit, REL_MAX) ||
  829. !bitmap_subset(id->absbit, dev->absbit, ABS_MAX) ||
  830. !bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX) ||
  831. !bitmap_subset(id->ledbit, dev->ledbit, LED_MAX) ||
  832. !bitmap_subset(id->sndbit, dev->sndbit, SND_MAX) ||
  833. !bitmap_subset(id->ffbit, dev->ffbit, FF_MAX) ||
  834. !bitmap_subset(id->swbit, dev->swbit, SW_MAX) ||
  835. !bitmap_subset(id->propbit, dev->propbit, INPUT_PROP_MAX)) {
  836. return false;
  837. }
  838. return true;
  839. }
  840. EXPORT_SYMBOL(input_match_device_id);
  841. static const struct input_device_id *input_match_device(struct input_handler *handler,
  842. struct input_dev *dev)
  843. {
  844. const struct input_device_id *id;
  845. for (id = handler->id_table; id->flags || id->driver_info; id++) {
  846. if (input_match_device_id(dev, id) &&
  847. (!handler->match || handler->match(handler, dev))) {
  848. return id;
  849. }
  850. }
  851. return NULL;
  852. }
  853. static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
  854. {
  855. const struct input_device_id *id;
  856. int error;
  857. id = input_match_device(handler, dev);
  858. if (!id)
  859. return -ENODEV;
  860. error = handler->connect(handler, dev, id);
  861. if (error && error != -ENODEV)
  862. pr_err("failed to attach handler %s to device %s, error: %d\n",
  863. handler->name, kobject_name(&dev->dev.kobj), error);
  864. return error;
  865. }
  866. #ifdef CONFIG_COMPAT
  867. static int input_bits_to_string(char *buf, int buf_size,
  868. unsigned long bits, bool skip_empty)
  869. {
  870. int len = 0;
  871. if (in_compat_syscall()) {
  872. u32 dword = bits >> 32;
  873. if (dword || !skip_empty)
  874. len += snprintf(buf, buf_size, "%x ", dword);
  875. dword = bits & 0xffffffffUL;
  876. if (dword || !skip_empty || len)
  877. len += snprintf(buf + len, max(buf_size - len, 0),
  878. "%x", dword);
  879. } else {
  880. if (bits || !skip_empty)
  881. len += snprintf(buf, buf_size, "%lx", bits);
  882. }
  883. return len;
  884. }
  885. #else /* !CONFIG_COMPAT */
  886. static int input_bits_to_string(char *buf, int buf_size,
  887. unsigned long bits, bool skip_empty)
  888. {
  889. return bits || !skip_empty ?
  890. snprintf(buf, buf_size, "%lx", bits) : 0;
  891. }
  892. #endif
  893. #ifdef CONFIG_PROC_FS
  894. static struct proc_dir_entry *proc_bus_input_dir;
  895. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  896. static int input_devices_state;
  897. static inline void input_wakeup_procfs_readers(void)
  898. {
  899. input_devices_state++;
  900. wake_up(&input_devices_poll_wait);
  901. }
  902. struct input_seq_state {
  903. unsigned short pos;
  904. bool mutex_acquired;
  905. int input_devices_state;
  906. };
  907. static __poll_t input_proc_devices_poll(struct file *file, poll_table *wait)
  908. {
  909. struct seq_file *seq = file->private_data;
  910. struct input_seq_state *state = seq->private;
  911. poll_wait(file, &input_devices_poll_wait, wait);
  912. if (state->input_devices_state != input_devices_state) {
  913. state->input_devices_state = input_devices_state;
  914. return EPOLLIN | EPOLLRDNORM;
  915. }
  916. return 0;
  917. }
  918. static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
  919. {
  920. struct input_seq_state *state = seq->private;
  921. int error;
  922. error = mutex_lock_interruptible(&input_mutex);
  923. if (error) {
  924. state->mutex_acquired = false;
  925. return ERR_PTR(error);
  926. }
  927. state->mutex_acquired = true;
  928. return seq_list_start(&input_dev_list, *pos);
  929. }
  930. static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  931. {
  932. return seq_list_next(v, &input_dev_list, pos);
  933. }
  934. static void input_seq_stop(struct seq_file *seq, void *v)
  935. {
  936. struct input_seq_state *state = seq->private;
  937. if (state->mutex_acquired)
  938. mutex_unlock(&input_mutex);
  939. }
  940. static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
  941. unsigned long *bitmap, int max)
  942. {
  943. int i;
  944. bool skip_empty = true;
  945. char buf[18];
  946. seq_printf(seq, "B: %s=", name);
  947. for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
  948. if (input_bits_to_string(buf, sizeof(buf),
  949. bitmap[i], skip_empty)) {
  950. skip_empty = false;
  951. seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
  952. }
  953. }
  954. /*
  955. * If no output was produced print a single 0.
  956. */
  957. if (skip_empty)
  958. seq_putc(seq, '0');
  959. seq_putc(seq, '\n');
  960. }
  961. static int input_devices_seq_show(struct seq_file *seq, void *v)
  962. {
  963. struct input_dev *dev = container_of(v, struct input_dev, node);
  964. const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  965. struct input_handle *handle;
  966. seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  967. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  968. seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  969. seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  970. seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
  971. seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
  972. seq_puts(seq, "H: Handlers=");
  973. list_for_each_entry(handle, &dev->h_list, d_node)
  974. seq_printf(seq, "%s ", handle->name);
  975. seq_putc(seq, '\n');
  976. input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX);
  977. input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
  978. if (test_bit(EV_KEY, dev->evbit))
  979. input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
  980. if (test_bit(EV_REL, dev->evbit))
  981. input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
  982. if (test_bit(EV_ABS, dev->evbit))
  983. input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
  984. if (test_bit(EV_MSC, dev->evbit))
  985. input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
  986. if (test_bit(EV_LED, dev->evbit))
  987. input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
  988. if (test_bit(EV_SND, dev->evbit))
  989. input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
  990. if (test_bit(EV_FF, dev->evbit))
  991. input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
  992. if (test_bit(EV_SW, dev->evbit))
  993. input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
  994. seq_putc(seq, '\n');
  995. kfree(path);
  996. return 0;
  997. }
  998. static const struct seq_operations input_devices_seq_ops = {
  999. .start = input_devices_seq_start,
  1000. .next = input_devices_seq_next,
  1001. .stop = input_seq_stop,
  1002. .show = input_devices_seq_show,
  1003. };
  1004. static int input_proc_devices_open(struct inode *inode, struct file *file)
  1005. {
  1006. return seq_open_private(file, &input_devices_seq_ops,
  1007. sizeof(struct input_seq_state));
  1008. }
  1009. static const struct proc_ops input_devices_proc_ops = {
  1010. .proc_open = input_proc_devices_open,
  1011. .proc_poll = input_proc_devices_poll,
  1012. .proc_read = seq_read,
  1013. .proc_lseek = seq_lseek,
  1014. .proc_release = seq_release_private,
  1015. };
  1016. static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
  1017. {
  1018. struct input_seq_state *state = seq->private;
  1019. int error;
  1020. error = mutex_lock_interruptible(&input_mutex);
  1021. if (error) {
  1022. state->mutex_acquired = false;
  1023. return ERR_PTR(error);
  1024. }
  1025. state->mutex_acquired = true;
  1026. state->pos = *pos;
  1027. return seq_list_start(&input_handler_list, *pos);
  1028. }
  1029. static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1030. {
  1031. struct input_seq_state *state = seq->private;
  1032. state->pos = *pos + 1;
  1033. return seq_list_next(v, &input_handler_list, pos);
  1034. }
  1035. static int input_handlers_seq_show(struct seq_file *seq, void *v)
  1036. {
  1037. struct input_handler *handler = container_of(v, struct input_handler, node);
  1038. struct input_seq_state *state = seq->private;
  1039. seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
  1040. if (handler->filter)
  1041. seq_puts(seq, " (filter)");
  1042. if (handler->legacy_minors)
  1043. seq_printf(seq, " Minor=%d", handler->minor);
  1044. seq_putc(seq, '\n');
  1045. return 0;
  1046. }
  1047. static const struct seq_operations input_handlers_seq_ops = {
  1048. .start = input_handlers_seq_start,
  1049. .next = input_handlers_seq_next,
  1050. .stop = input_seq_stop,
  1051. .show = input_handlers_seq_show,
  1052. };
  1053. static int input_proc_handlers_open(struct inode *inode, struct file *file)
  1054. {
  1055. return seq_open_private(file, &input_handlers_seq_ops,
  1056. sizeof(struct input_seq_state));
  1057. }
  1058. static const struct proc_ops input_handlers_proc_ops = {
  1059. .proc_open = input_proc_handlers_open,
  1060. .proc_read = seq_read,
  1061. .proc_lseek = seq_lseek,
  1062. .proc_release = seq_release_private,
  1063. };
  1064. static int __init input_proc_init(void)
  1065. {
  1066. struct proc_dir_entry *entry;
  1067. proc_bus_input_dir = proc_mkdir("bus/input", NULL);
  1068. if (!proc_bus_input_dir)
  1069. return -ENOMEM;
  1070. entry = proc_create("devices", 0, proc_bus_input_dir,
  1071. &input_devices_proc_ops);
  1072. if (!entry)
  1073. goto fail1;
  1074. entry = proc_create("handlers", 0, proc_bus_input_dir,
  1075. &input_handlers_proc_ops);
  1076. if (!entry)
  1077. goto fail2;
  1078. return 0;
  1079. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  1080. fail1: remove_proc_entry("bus/input", NULL);
  1081. return -ENOMEM;
  1082. }
  1083. static void input_proc_exit(void)
  1084. {
  1085. remove_proc_entry("devices", proc_bus_input_dir);
  1086. remove_proc_entry("handlers", proc_bus_input_dir);
  1087. remove_proc_entry("bus/input", NULL);
  1088. }
  1089. #else /* !CONFIG_PROC_FS */
  1090. static inline void input_wakeup_procfs_readers(void) { }
  1091. static inline int input_proc_init(void) { return 0; }
  1092. static inline void input_proc_exit(void) { }
  1093. #endif
  1094. #define INPUT_DEV_STRING_ATTR_SHOW(name) \
  1095. static ssize_t input_dev_show_##name(struct device *dev, \
  1096. struct device_attribute *attr, \
  1097. char *buf) \
  1098. { \
  1099. struct input_dev *input_dev = to_input_dev(dev); \
  1100. \
  1101. return sysfs_emit(buf, "%s\n", \
  1102. input_dev->name ? input_dev->name : ""); \
  1103. } \
  1104. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
  1105. INPUT_DEV_STRING_ATTR_SHOW(name);
  1106. INPUT_DEV_STRING_ATTR_SHOW(phys);
  1107. INPUT_DEV_STRING_ATTR_SHOW(uniq);
  1108. static int input_print_modalias_bits(char *buf, int size,
  1109. char name, const unsigned long *bm,
  1110. unsigned int min_bit, unsigned int max_bit)
  1111. {
  1112. int bit = min_bit;
  1113. int len = 0;
  1114. len += snprintf(buf, max(size, 0), "%c", name);
  1115. for_each_set_bit_from(bit, bm, max_bit)
  1116. len += snprintf(buf + len, max(size - len, 0), "%X,", bit);
  1117. return len;
  1118. }
  1119. static int input_print_modalias_parts(char *buf, int size, int full_len,
  1120. const struct input_dev *id)
  1121. {
  1122. int len, klen, remainder, space;
  1123. len = snprintf(buf, max(size, 0),
  1124. "input:b%04Xv%04Xp%04Xe%04X-",
  1125. id->id.bustype, id->id.vendor,
  1126. id->id.product, id->id.version);
  1127. len += input_print_modalias_bits(buf + len, size - len,
  1128. 'e', id->evbit, 0, EV_MAX);
  1129. /*
  1130. * Calculate the remaining space in the buffer making sure we
  1131. * have place for the terminating 0.
  1132. */
  1133. space = max(size - (len + 1), 0);
  1134. klen = input_print_modalias_bits(buf + len, size - len,
  1135. 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
  1136. len += klen;
  1137. /*
  1138. * If we have more data than we can fit in the buffer, check
  1139. * if we can trim key data to fit in the rest. We will indicate
  1140. * that key data is incomplete by adding "+" sign at the end, like
  1141. * this: * "k1,2,3,45,+,".
  1142. *
  1143. * Note that we shortest key info (if present) is "k+," so we
  1144. * can only try to trim if key data is longer than that.
  1145. */
  1146. if (full_len && size < full_len + 1 && klen > 3) {
  1147. remainder = full_len - len;
  1148. /*
  1149. * We can only trim if we have space for the remainder
  1150. * and also for at least "k+," which is 3 more characters.
  1151. */
  1152. if (remainder <= space - 3) {
  1153. /*
  1154. * We are guaranteed to have 'k' in the buffer, so
  1155. * we need at least 3 additional bytes for storing
  1156. * "+," in addition to the remainder.
  1157. */
  1158. for (int i = size - 1 - remainder - 3; i >= 0; i--) {
  1159. if (buf[i] == 'k' || buf[i] == ',') {
  1160. strcpy(buf + i + 1, "+,");
  1161. len = i + 3; /* Not counting '\0' */
  1162. break;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. len += input_print_modalias_bits(buf + len, size - len,
  1168. 'r', id->relbit, 0, REL_MAX);
  1169. len += input_print_modalias_bits(buf + len, size - len,
  1170. 'a', id->absbit, 0, ABS_MAX);
  1171. len += input_print_modalias_bits(buf + len, size - len,
  1172. 'm', id->mscbit, 0, MSC_MAX);
  1173. len += input_print_modalias_bits(buf + len, size - len,
  1174. 'l', id->ledbit, 0, LED_MAX);
  1175. len += input_print_modalias_bits(buf + len, size - len,
  1176. 's', id->sndbit, 0, SND_MAX);
  1177. len += input_print_modalias_bits(buf + len, size - len,
  1178. 'f', id->ffbit, 0, FF_MAX);
  1179. len += input_print_modalias_bits(buf + len, size - len,
  1180. 'w', id->swbit, 0, SW_MAX);
  1181. return len;
  1182. }
  1183. static int input_print_modalias(char *buf, int size, const struct input_dev *id)
  1184. {
  1185. int full_len;
  1186. /*
  1187. * Printing is done in 2 passes: first one figures out total length
  1188. * needed for the modalias string, second one will try to trim key
  1189. * data in case when buffer is too small for the entire modalias.
  1190. * If the buffer is too small regardless, it will fill as much as it
  1191. * can (without trimming key data) into the buffer and leave it to
  1192. * the caller to figure out what to do with the result.
  1193. */
  1194. full_len = input_print_modalias_parts(NULL, 0, 0, id);
  1195. return input_print_modalias_parts(buf, size, full_len, id);
  1196. }
  1197. static ssize_t input_dev_show_modalias(struct device *dev,
  1198. struct device_attribute *attr,
  1199. char *buf)
  1200. {
  1201. struct input_dev *id = to_input_dev(dev);
  1202. ssize_t len;
  1203. len = input_print_modalias(buf, PAGE_SIZE, id);
  1204. if (len < PAGE_SIZE - 2)
  1205. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  1206. return min_t(int, len, PAGE_SIZE);
  1207. }
  1208. static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
  1209. static int input_print_bitmap(char *buf, int buf_size, const unsigned long *bitmap,
  1210. int max, int add_cr);
  1211. static ssize_t input_dev_show_properties(struct device *dev,
  1212. struct device_attribute *attr,
  1213. char *buf)
  1214. {
  1215. struct input_dev *input_dev = to_input_dev(dev);
  1216. int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit,
  1217. INPUT_PROP_MAX, true);
  1218. return min_t(int, len, PAGE_SIZE);
  1219. }
  1220. static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL);
  1221. static int input_inhibit_device(struct input_dev *dev);
  1222. static int input_uninhibit_device(struct input_dev *dev);
  1223. static ssize_t inhibited_show(struct device *dev,
  1224. struct device_attribute *attr,
  1225. char *buf)
  1226. {
  1227. struct input_dev *input_dev = to_input_dev(dev);
  1228. return sysfs_emit(buf, "%d\n", input_dev->inhibited);
  1229. }
  1230. static ssize_t inhibited_store(struct device *dev,
  1231. struct device_attribute *attr, const char *buf,
  1232. size_t len)
  1233. {
  1234. struct input_dev *input_dev = to_input_dev(dev);
  1235. ssize_t rv;
  1236. bool inhibited;
  1237. if (kstrtobool(buf, &inhibited))
  1238. return -EINVAL;
  1239. if (inhibited)
  1240. rv = input_inhibit_device(input_dev);
  1241. else
  1242. rv = input_uninhibit_device(input_dev);
  1243. if (rv != 0)
  1244. return rv;
  1245. return len;
  1246. }
  1247. static DEVICE_ATTR_RW(inhibited);
  1248. static struct attribute *input_dev_attrs[] = {
  1249. &dev_attr_name.attr,
  1250. &dev_attr_phys.attr,
  1251. &dev_attr_uniq.attr,
  1252. &dev_attr_modalias.attr,
  1253. &dev_attr_properties.attr,
  1254. &dev_attr_inhibited.attr,
  1255. NULL
  1256. };
  1257. static const struct attribute_group input_dev_attr_group = {
  1258. .attrs = input_dev_attrs,
  1259. };
  1260. #define INPUT_DEV_ID_ATTR(name) \
  1261. static ssize_t input_dev_show_id_##name(struct device *dev, \
  1262. struct device_attribute *attr, \
  1263. char *buf) \
  1264. { \
  1265. struct input_dev *input_dev = to_input_dev(dev); \
  1266. return sysfs_emit(buf, "%04x\n", input_dev->id.name); \
  1267. } \
  1268. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
  1269. INPUT_DEV_ID_ATTR(bustype);
  1270. INPUT_DEV_ID_ATTR(vendor);
  1271. INPUT_DEV_ID_ATTR(product);
  1272. INPUT_DEV_ID_ATTR(version);
  1273. static struct attribute *input_dev_id_attrs[] = {
  1274. &dev_attr_bustype.attr,
  1275. &dev_attr_vendor.attr,
  1276. &dev_attr_product.attr,
  1277. &dev_attr_version.attr,
  1278. NULL
  1279. };
  1280. static const struct attribute_group input_dev_id_attr_group = {
  1281. .name = "id",
  1282. .attrs = input_dev_id_attrs,
  1283. };
  1284. static int input_print_bitmap(char *buf, int buf_size, const unsigned long *bitmap,
  1285. int max, int add_cr)
  1286. {
  1287. int i;
  1288. int len = 0;
  1289. bool skip_empty = true;
  1290. for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
  1291. len += input_bits_to_string(buf + len, max(buf_size - len, 0),
  1292. bitmap[i], skip_empty);
  1293. if (len) {
  1294. skip_empty = false;
  1295. if (i > 0)
  1296. len += snprintf(buf + len, max(buf_size - len, 0), " ");
  1297. }
  1298. }
  1299. /*
  1300. * If no output was produced print a single 0.
  1301. */
  1302. if (len == 0)
  1303. len = snprintf(buf, buf_size, "%d", 0);
  1304. if (add_cr)
  1305. len += snprintf(buf + len, max(buf_size - len, 0), "\n");
  1306. return len;
  1307. }
  1308. #define INPUT_DEV_CAP_ATTR(ev, bm) \
  1309. static ssize_t input_dev_show_cap_##bm(struct device *dev, \
  1310. struct device_attribute *attr, \
  1311. char *buf) \
  1312. { \
  1313. struct input_dev *input_dev = to_input_dev(dev); \
  1314. int len = input_print_bitmap(buf, PAGE_SIZE, \
  1315. input_dev->bm##bit, ev##_MAX, \
  1316. true); \
  1317. return min_t(int, len, PAGE_SIZE); \
  1318. } \
  1319. static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
  1320. INPUT_DEV_CAP_ATTR(EV, ev);
  1321. INPUT_DEV_CAP_ATTR(KEY, key);
  1322. INPUT_DEV_CAP_ATTR(REL, rel);
  1323. INPUT_DEV_CAP_ATTR(ABS, abs);
  1324. INPUT_DEV_CAP_ATTR(MSC, msc);
  1325. INPUT_DEV_CAP_ATTR(LED, led);
  1326. INPUT_DEV_CAP_ATTR(SND, snd);
  1327. INPUT_DEV_CAP_ATTR(FF, ff);
  1328. INPUT_DEV_CAP_ATTR(SW, sw);
  1329. static struct attribute *input_dev_caps_attrs[] = {
  1330. &dev_attr_ev.attr,
  1331. &dev_attr_key.attr,
  1332. &dev_attr_rel.attr,
  1333. &dev_attr_abs.attr,
  1334. &dev_attr_msc.attr,
  1335. &dev_attr_led.attr,
  1336. &dev_attr_snd.attr,
  1337. &dev_attr_ff.attr,
  1338. &dev_attr_sw.attr,
  1339. NULL
  1340. };
  1341. static const struct attribute_group input_dev_caps_attr_group = {
  1342. .name = "capabilities",
  1343. .attrs = input_dev_caps_attrs,
  1344. };
  1345. static const struct attribute_group *input_dev_attr_groups[] = {
  1346. &input_dev_attr_group,
  1347. &input_dev_id_attr_group,
  1348. &input_dev_caps_attr_group,
  1349. &input_poller_attribute_group,
  1350. NULL
  1351. };
  1352. static void input_dev_release(struct device *device)
  1353. {
  1354. struct input_dev *dev = to_input_dev(device);
  1355. input_ff_destroy(dev);
  1356. input_mt_destroy_slots(dev);
  1357. kfree(dev->poller);
  1358. kfree(dev->absinfo);
  1359. kfree(dev->vals);
  1360. kfree(dev);
  1361. module_put(THIS_MODULE);
  1362. }
  1363. /*
  1364. * Input uevent interface - loading event handlers based on
  1365. * device bitfields.
  1366. */
  1367. static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
  1368. const char *name, const unsigned long *bitmap, int max)
  1369. {
  1370. int len;
  1371. if (add_uevent_var(env, "%s", name))
  1372. return -ENOMEM;
  1373. len = input_print_bitmap(&env->buf[env->buflen - 1],
  1374. sizeof(env->buf) - env->buflen,
  1375. bitmap, max, false);
  1376. if (len >= (sizeof(env->buf) - env->buflen))
  1377. return -ENOMEM;
  1378. env->buflen += len;
  1379. return 0;
  1380. }
  1381. /*
  1382. * This is a pretty gross hack. When building uevent data the driver core
  1383. * may try adding more environment variables to kobj_uevent_env without
  1384. * telling us, so we have no idea how much of the buffer we can use to
  1385. * avoid overflows/-ENOMEM elsewhere. To work around this let's artificially
  1386. * reduce amount of memory we will use for the modalias environment variable.
  1387. *
  1388. * The potential additions are:
  1389. *
  1390. * SEQNUM=18446744073709551615 - (%llu - 28 bytes)
  1391. * HOME=/ (6 bytes)
  1392. * PATH=/sbin:/bin:/usr/sbin:/usr/bin (34 bytes)
  1393. *
  1394. * 68 bytes total. Allow extra buffer - 96 bytes
  1395. */
  1396. #define UEVENT_ENV_EXTRA_LEN 96
  1397. static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
  1398. const struct input_dev *dev)
  1399. {
  1400. int len;
  1401. if (add_uevent_var(env, "MODALIAS="))
  1402. return -ENOMEM;
  1403. len = input_print_modalias(&env->buf[env->buflen - 1],
  1404. (int)sizeof(env->buf) - env->buflen -
  1405. UEVENT_ENV_EXTRA_LEN,
  1406. dev);
  1407. if (len >= ((int)sizeof(env->buf) - env->buflen -
  1408. UEVENT_ENV_EXTRA_LEN))
  1409. return -ENOMEM;
  1410. env->buflen += len;
  1411. return 0;
  1412. }
  1413. #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
  1414. do { \
  1415. int err = add_uevent_var(env, fmt, val); \
  1416. if (err) \
  1417. return err; \
  1418. } while (0)
  1419. #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
  1420. do { \
  1421. int err = input_add_uevent_bm_var(env, name, bm, max); \
  1422. if (err) \
  1423. return err; \
  1424. } while (0)
  1425. #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
  1426. do { \
  1427. int err = input_add_uevent_modalias_var(env, dev); \
  1428. if (err) \
  1429. return err; \
  1430. } while (0)
  1431. static int input_dev_uevent(const struct device *device, struct kobj_uevent_env *env)
  1432. {
  1433. const struct input_dev *dev = to_input_dev(device);
  1434. INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
  1435. dev->id.bustype, dev->id.vendor,
  1436. dev->id.product, dev->id.version);
  1437. if (dev->name)
  1438. INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
  1439. if (dev->phys)
  1440. INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
  1441. if (dev->uniq)
  1442. INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
  1443. INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX);
  1444. INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
  1445. if (test_bit(EV_KEY, dev->evbit))
  1446. INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
  1447. if (test_bit(EV_REL, dev->evbit))
  1448. INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
  1449. if (test_bit(EV_ABS, dev->evbit))
  1450. INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
  1451. if (test_bit(EV_MSC, dev->evbit))
  1452. INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
  1453. if (test_bit(EV_LED, dev->evbit))
  1454. INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
  1455. if (test_bit(EV_SND, dev->evbit))
  1456. INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
  1457. if (test_bit(EV_FF, dev->evbit))
  1458. INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
  1459. if (test_bit(EV_SW, dev->evbit))
  1460. INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
  1461. INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
  1462. return 0;
  1463. }
  1464. #define INPUT_DO_TOGGLE(dev, type, bits, on) \
  1465. do { \
  1466. int i; \
  1467. bool active; \
  1468. \
  1469. if (!test_bit(EV_##type, dev->evbit)) \
  1470. break; \
  1471. \
  1472. for_each_set_bit(i, dev->bits##bit, type##_CNT) { \
  1473. active = test_bit(i, dev->bits); \
  1474. if (!active && !on) \
  1475. continue; \
  1476. \
  1477. dev->event(dev, EV_##type, i, on ? active : 0); \
  1478. } \
  1479. } while (0)
  1480. static void input_dev_toggle(struct input_dev *dev, bool activate)
  1481. {
  1482. if (!dev->event)
  1483. return;
  1484. INPUT_DO_TOGGLE(dev, LED, led, activate);
  1485. INPUT_DO_TOGGLE(dev, SND, snd, activate);
  1486. if (activate && test_bit(EV_REP, dev->evbit)) {
  1487. dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
  1488. dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
  1489. }
  1490. }
  1491. /**
  1492. * input_reset_device() - reset/restore the state of input device
  1493. * @dev: input device whose state needs to be reset
  1494. *
  1495. * This function tries to reset the state of an opened input device and
  1496. * bring internal state and state if the hardware in sync with each other.
  1497. * We mark all keys as released, restore LED state, repeat rate, etc.
  1498. */
  1499. void input_reset_device(struct input_dev *dev)
  1500. {
  1501. unsigned long flags;
  1502. mutex_lock(&dev->mutex);
  1503. spin_lock_irqsave(&dev->event_lock, flags);
  1504. input_dev_toggle(dev, true);
  1505. if (input_dev_release_keys(dev))
  1506. input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
  1507. spin_unlock_irqrestore(&dev->event_lock, flags);
  1508. mutex_unlock(&dev->mutex);
  1509. }
  1510. EXPORT_SYMBOL(input_reset_device);
  1511. static int input_inhibit_device(struct input_dev *dev)
  1512. {
  1513. mutex_lock(&dev->mutex);
  1514. if (dev->inhibited)
  1515. goto out;
  1516. if (dev->users) {
  1517. if (dev->close)
  1518. dev->close(dev);
  1519. if (dev->poller)
  1520. input_dev_poller_stop(dev->poller);
  1521. }
  1522. spin_lock_irq(&dev->event_lock);
  1523. input_mt_release_slots(dev);
  1524. input_dev_release_keys(dev);
  1525. input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
  1526. input_dev_toggle(dev, false);
  1527. spin_unlock_irq(&dev->event_lock);
  1528. dev->inhibited = true;
  1529. out:
  1530. mutex_unlock(&dev->mutex);
  1531. return 0;
  1532. }
  1533. static int input_uninhibit_device(struct input_dev *dev)
  1534. {
  1535. int ret = 0;
  1536. mutex_lock(&dev->mutex);
  1537. if (!dev->inhibited)
  1538. goto out;
  1539. if (dev->users) {
  1540. if (dev->open) {
  1541. ret = dev->open(dev);
  1542. if (ret)
  1543. goto out;
  1544. }
  1545. if (dev->poller)
  1546. input_dev_poller_start(dev->poller);
  1547. }
  1548. dev->inhibited = false;
  1549. spin_lock_irq(&dev->event_lock);
  1550. input_dev_toggle(dev, true);
  1551. spin_unlock_irq(&dev->event_lock);
  1552. out:
  1553. mutex_unlock(&dev->mutex);
  1554. return ret;
  1555. }
  1556. static int input_dev_suspend(struct device *dev)
  1557. {
  1558. struct input_dev *input_dev = to_input_dev(dev);
  1559. spin_lock_irq(&input_dev->event_lock);
  1560. /*
  1561. * Keys that are pressed now are unlikely to be
  1562. * still pressed when we resume.
  1563. */
  1564. if (input_dev_release_keys(input_dev))
  1565. input_handle_event(input_dev, EV_SYN, SYN_REPORT, 1);
  1566. /* Turn off LEDs and sounds, if any are active. */
  1567. input_dev_toggle(input_dev, false);
  1568. spin_unlock_irq(&input_dev->event_lock);
  1569. return 0;
  1570. }
  1571. static int input_dev_resume(struct device *dev)
  1572. {
  1573. struct input_dev *input_dev = to_input_dev(dev);
  1574. spin_lock_irq(&input_dev->event_lock);
  1575. /* Restore state of LEDs and sounds, if any were active. */
  1576. input_dev_toggle(input_dev, true);
  1577. spin_unlock_irq(&input_dev->event_lock);
  1578. return 0;
  1579. }
  1580. static int input_dev_freeze(struct device *dev)
  1581. {
  1582. struct input_dev *input_dev = to_input_dev(dev);
  1583. spin_lock_irq(&input_dev->event_lock);
  1584. /*
  1585. * Keys that are pressed now are unlikely to be
  1586. * still pressed when we resume.
  1587. */
  1588. if (input_dev_release_keys(input_dev))
  1589. input_handle_event(input_dev, EV_SYN, SYN_REPORT, 1);
  1590. spin_unlock_irq(&input_dev->event_lock);
  1591. return 0;
  1592. }
  1593. static int input_dev_poweroff(struct device *dev)
  1594. {
  1595. struct input_dev *input_dev = to_input_dev(dev);
  1596. spin_lock_irq(&input_dev->event_lock);
  1597. /* Turn off LEDs and sounds, if any are active. */
  1598. input_dev_toggle(input_dev, false);
  1599. spin_unlock_irq(&input_dev->event_lock);
  1600. return 0;
  1601. }
  1602. static const struct dev_pm_ops input_dev_pm_ops = {
  1603. .suspend = input_dev_suspend,
  1604. .resume = input_dev_resume,
  1605. .freeze = input_dev_freeze,
  1606. .poweroff = input_dev_poweroff,
  1607. .restore = input_dev_resume,
  1608. };
  1609. static const struct device_type input_dev_type = {
  1610. .groups = input_dev_attr_groups,
  1611. .release = input_dev_release,
  1612. .uevent = input_dev_uevent,
  1613. .pm = pm_sleep_ptr(&input_dev_pm_ops),
  1614. };
  1615. static char *input_devnode(const struct device *dev, umode_t *mode)
  1616. {
  1617. return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
  1618. }
  1619. const struct class input_class = {
  1620. .name = "input",
  1621. .devnode = input_devnode,
  1622. };
  1623. EXPORT_SYMBOL_GPL(input_class);
  1624. /**
  1625. * input_allocate_device - allocate memory for new input device
  1626. *
  1627. * Returns prepared struct input_dev or %NULL.
  1628. *
  1629. * NOTE: Use input_free_device() to free devices that have not been
  1630. * registered; input_unregister_device() should be used for already
  1631. * registered devices.
  1632. */
  1633. struct input_dev *input_allocate_device(void)
  1634. {
  1635. static atomic_t input_no = ATOMIC_INIT(-1);
  1636. struct input_dev *dev;
  1637. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1638. if (!dev)
  1639. return NULL;
  1640. /*
  1641. * Start with space for SYN_REPORT + 7 EV_KEY/EV_MSC events + 2 spare,
  1642. * see input_estimate_events_per_packet(). We will tune the number
  1643. * when we register the device.
  1644. */
  1645. dev->max_vals = 10;
  1646. dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
  1647. if (!dev->vals) {
  1648. kfree(dev);
  1649. return NULL;
  1650. }
  1651. mutex_init(&dev->mutex);
  1652. spin_lock_init(&dev->event_lock);
  1653. timer_setup(&dev->timer, NULL, 0);
  1654. INIT_LIST_HEAD(&dev->h_list);
  1655. INIT_LIST_HEAD(&dev->node);
  1656. dev->dev.type = &input_dev_type;
  1657. dev->dev.class = &input_class;
  1658. device_initialize(&dev->dev);
  1659. /*
  1660. * From this point on we can no longer simply "kfree(dev)", we need
  1661. * to use input_free_device() so that device core properly frees its
  1662. * resources associated with the input device.
  1663. */
  1664. dev_set_name(&dev->dev, "input%lu",
  1665. (unsigned long)atomic_inc_return(&input_no));
  1666. __module_get(THIS_MODULE);
  1667. return dev;
  1668. }
  1669. EXPORT_SYMBOL(input_allocate_device);
  1670. struct input_devres {
  1671. struct input_dev *input;
  1672. };
  1673. static int devm_input_device_match(struct device *dev, void *res, void *data)
  1674. {
  1675. struct input_devres *devres = res;
  1676. return devres->input == data;
  1677. }
  1678. static void devm_input_device_release(struct device *dev, void *res)
  1679. {
  1680. struct input_devres *devres = res;
  1681. struct input_dev *input = devres->input;
  1682. dev_dbg(dev, "%s: dropping reference to %s\n",
  1683. __func__, dev_name(&input->dev));
  1684. input_put_device(input);
  1685. }
  1686. /**
  1687. * devm_input_allocate_device - allocate managed input device
  1688. * @dev: device owning the input device being created
  1689. *
  1690. * Returns prepared struct input_dev or %NULL.
  1691. *
  1692. * Managed input devices do not need to be explicitly unregistered or
  1693. * freed as it will be done automatically when owner device unbinds from
  1694. * its driver (or binding fails). Once managed input device is allocated,
  1695. * it is ready to be set up and registered in the same fashion as regular
  1696. * input device. There are no special devm_input_device_[un]register()
  1697. * variants, regular ones work with both managed and unmanaged devices,
  1698. * should you need them. In most cases however, managed input device need
  1699. * not be explicitly unregistered or freed.
  1700. *
  1701. * NOTE: the owner device is set up as parent of input device and users
  1702. * should not override it.
  1703. */
  1704. struct input_dev *devm_input_allocate_device(struct device *dev)
  1705. {
  1706. struct input_dev *input;
  1707. struct input_devres *devres;
  1708. devres = devres_alloc(devm_input_device_release,
  1709. sizeof(*devres), GFP_KERNEL);
  1710. if (!devres)
  1711. return NULL;
  1712. input = input_allocate_device();
  1713. if (!input) {
  1714. devres_free(devres);
  1715. return NULL;
  1716. }
  1717. input->dev.parent = dev;
  1718. input->devres_managed = true;
  1719. devres->input = input;
  1720. devres_add(dev, devres);
  1721. return input;
  1722. }
  1723. EXPORT_SYMBOL(devm_input_allocate_device);
  1724. /**
  1725. * input_free_device - free memory occupied by input_dev structure
  1726. * @dev: input device to free
  1727. *
  1728. * This function should only be used if input_register_device()
  1729. * was not called yet or if it failed. Once device was registered
  1730. * use input_unregister_device() and memory will be freed once last
  1731. * reference to the device is dropped.
  1732. *
  1733. * Device should be allocated by input_allocate_device().
  1734. *
  1735. * NOTE: If there are references to the input device then memory
  1736. * will not be freed until last reference is dropped.
  1737. */
  1738. void input_free_device(struct input_dev *dev)
  1739. {
  1740. if (dev) {
  1741. if (dev->devres_managed)
  1742. WARN_ON(devres_destroy(dev->dev.parent,
  1743. devm_input_device_release,
  1744. devm_input_device_match,
  1745. dev));
  1746. input_put_device(dev);
  1747. }
  1748. }
  1749. EXPORT_SYMBOL(input_free_device);
  1750. /**
  1751. * input_set_timestamp - set timestamp for input events
  1752. * @dev: input device to set timestamp for
  1753. * @timestamp: the time at which the event has occurred
  1754. * in CLOCK_MONOTONIC
  1755. *
  1756. * This function is intended to provide to the input system a more
  1757. * accurate time of when an event actually occurred. The driver should
  1758. * call this function as soon as a timestamp is acquired ensuring
  1759. * clock conversions in input_set_timestamp are done correctly.
  1760. *
  1761. * The system entering suspend state between timestamp acquisition and
  1762. * calling input_set_timestamp can result in inaccurate conversions.
  1763. */
  1764. void input_set_timestamp(struct input_dev *dev, ktime_t timestamp)
  1765. {
  1766. dev->timestamp[INPUT_CLK_MONO] = timestamp;
  1767. dev->timestamp[INPUT_CLK_REAL] = ktime_mono_to_real(timestamp);
  1768. dev->timestamp[INPUT_CLK_BOOT] = ktime_mono_to_any(timestamp,
  1769. TK_OFFS_BOOT);
  1770. }
  1771. EXPORT_SYMBOL(input_set_timestamp);
  1772. /**
  1773. * input_get_timestamp - get timestamp for input events
  1774. * @dev: input device to get timestamp from
  1775. *
  1776. * A valid timestamp is a timestamp of non-zero value.
  1777. */
  1778. ktime_t *input_get_timestamp(struct input_dev *dev)
  1779. {
  1780. const ktime_t invalid_timestamp = ktime_set(0, 0);
  1781. if (!ktime_compare(dev->timestamp[INPUT_CLK_MONO], invalid_timestamp))
  1782. input_set_timestamp(dev, ktime_get());
  1783. return dev->timestamp;
  1784. }
  1785. EXPORT_SYMBOL(input_get_timestamp);
  1786. /**
  1787. * input_set_capability - mark device as capable of a certain event
  1788. * @dev: device that is capable of emitting or accepting event
  1789. * @type: type of the event (EV_KEY, EV_REL, etc...)
  1790. * @code: event code
  1791. *
  1792. * In addition to setting up corresponding bit in appropriate capability
  1793. * bitmap the function also adjusts dev->evbit.
  1794. */
  1795. void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
  1796. {
  1797. if (type < EV_CNT && input_max_code[type] &&
  1798. code > input_max_code[type]) {
  1799. pr_err("%s: invalid code %u for type %u\n", __func__, code,
  1800. type);
  1801. dump_stack();
  1802. return;
  1803. }
  1804. switch (type) {
  1805. case EV_KEY:
  1806. __set_bit(code, dev->keybit);
  1807. break;
  1808. case EV_REL:
  1809. __set_bit(code, dev->relbit);
  1810. break;
  1811. case EV_ABS:
  1812. input_alloc_absinfo(dev);
  1813. __set_bit(code, dev->absbit);
  1814. break;
  1815. case EV_MSC:
  1816. __set_bit(code, dev->mscbit);
  1817. break;
  1818. case EV_SW:
  1819. __set_bit(code, dev->swbit);
  1820. break;
  1821. case EV_LED:
  1822. __set_bit(code, dev->ledbit);
  1823. break;
  1824. case EV_SND:
  1825. __set_bit(code, dev->sndbit);
  1826. break;
  1827. case EV_FF:
  1828. __set_bit(code, dev->ffbit);
  1829. break;
  1830. case EV_PWR:
  1831. /* do nothing */
  1832. break;
  1833. default:
  1834. pr_err("%s: unknown type %u (code %u)\n", __func__, type, code);
  1835. dump_stack();
  1836. return;
  1837. }
  1838. __set_bit(type, dev->evbit);
  1839. }
  1840. EXPORT_SYMBOL(input_set_capability);
  1841. static unsigned int input_estimate_events_per_packet(struct input_dev *dev)
  1842. {
  1843. int mt_slots;
  1844. int i;
  1845. unsigned int events;
  1846. if (dev->mt) {
  1847. mt_slots = dev->mt->num_slots;
  1848. } else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) {
  1849. mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum -
  1850. dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1;
  1851. mt_slots = clamp(mt_slots, 2, 32);
  1852. } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
  1853. mt_slots = 2;
  1854. } else {
  1855. mt_slots = 0;
  1856. }
  1857. events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */
  1858. if (test_bit(EV_ABS, dev->evbit))
  1859. for_each_set_bit(i, dev->absbit, ABS_CNT)
  1860. events += input_is_mt_axis(i) ? mt_slots : 1;
  1861. if (test_bit(EV_REL, dev->evbit))
  1862. events += bitmap_weight(dev->relbit, REL_CNT);
  1863. /* Make room for KEY and MSC events */
  1864. events += 7;
  1865. return events;
  1866. }
  1867. #define INPUT_CLEANSE_BITMASK(dev, type, bits) \
  1868. do { \
  1869. if (!test_bit(EV_##type, dev->evbit)) \
  1870. memset(dev->bits##bit, 0, \
  1871. sizeof(dev->bits##bit)); \
  1872. } while (0)
  1873. static void input_cleanse_bitmasks(struct input_dev *dev)
  1874. {
  1875. INPUT_CLEANSE_BITMASK(dev, KEY, key);
  1876. INPUT_CLEANSE_BITMASK(dev, REL, rel);
  1877. INPUT_CLEANSE_BITMASK(dev, ABS, abs);
  1878. INPUT_CLEANSE_BITMASK(dev, MSC, msc);
  1879. INPUT_CLEANSE_BITMASK(dev, LED, led);
  1880. INPUT_CLEANSE_BITMASK(dev, SND, snd);
  1881. INPUT_CLEANSE_BITMASK(dev, FF, ff);
  1882. INPUT_CLEANSE_BITMASK(dev, SW, sw);
  1883. }
  1884. static void __input_unregister_device(struct input_dev *dev)
  1885. {
  1886. struct input_handle *handle, *next;
  1887. input_disconnect_device(dev);
  1888. mutex_lock(&input_mutex);
  1889. list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
  1890. handle->handler->disconnect(handle);
  1891. WARN_ON(!list_empty(&dev->h_list));
  1892. del_timer_sync(&dev->timer);
  1893. list_del_init(&dev->node);
  1894. input_wakeup_procfs_readers();
  1895. mutex_unlock(&input_mutex);
  1896. device_del(&dev->dev);
  1897. }
  1898. static void devm_input_device_unregister(struct device *dev, void *res)
  1899. {
  1900. struct input_devres *devres = res;
  1901. struct input_dev *input = devres->input;
  1902. dev_dbg(dev, "%s: unregistering device %s\n",
  1903. __func__, dev_name(&input->dev));
  1904. __input_unregister_device(input);
  1905. }
  1906. /*
  1907. * Generate software autorepeat event. Note that we take
  1908. * dev->event_lock here to avoid racing with input_event
  1909. * which may cause keys get "stuck".
  1910. */
  1911. static void input_repeat_key(struct timer_list *t)
  1912. {
  1913. struct input_dev *dev = from_timer(dev, t, timer);
  1914. unsigned long flags;
  1915. spin_lock_irqsave(&dev->event_lock, flags);
  1916. if (!dev->inhibited &&
  1917. test_bit(dev->repeat_key, dev->key) &&
  1918. is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
  1919. input_set_timestamp(dev, ktime_get());
  1920. input_handle_event(dev, EV_KEY, dev->repeat_key, 2);
  1921. input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
  1922. if (dev->rep[REP_PERIOD])
  1923. mod_timer(&dev->timer, jiffies +
  1924. msecs_to_jiffies(dev->rep[REP_PERIOD]));
  1925. }
  1926. spin_unlock_irqrestore(&dev->event_lock, flags);
  1927. }
  1928. /**
  1929. * input_enable_softrepeat - enable software autorepeat
  1930. * @dev: input device
  1931. * @delay: repeat delay
  1932. * @period: repeat period
  1933. *
  1934. * Enable software autorepeat on the input device.
  1935. */
  1936. void input_enable_softrepeat(struct input_dev *dev, int delay, int period)
  1937. {
  1938. dev->timer.function = input_repeat_key;
  1939. dev->rep[REP_DELAY] = delay;
  1940. dev->rep[REP_PERIOD] = period;
  1941. }
  1942. EXPORT_SYMBOL(input_enable_softrepeat);
  1943. bool input_device_enabled(struct input_dev *dev)
  1944. {
  1945. lockdep_assert_held(&dev->mutex);
  1946. return !dev->inhibited && dev->users > 0;
  1947. }
  1948. EXPORT_SYMBOL_GPL(input_device_enabled);
  1949. static int input_device_tune_vals(struct input_dev *dev)
  1950. {
  1951. struct input_value *vals;
  1952. unsigned int packet_size;
  1953. unsigned int max_vals;
  1954. packet_size = input_estimate_events_per_packet(dev);
  1955. if (dev->hint_events_per_packet < packet_size)
  1956. dev->hint_events_per_packet = packet_size;
  1957. max_vals = dev->hint_events_per_packet + 2;
  1958. if (dev->max_vals >= max_vals)
  1959. return 0;
  1960. vals = kcalloc(max_vals, sizeof(*vals), GFP_KERNEL);
  1961. if (!vals)
  1962. return -ENOMEM;
  1963. spin_lock_irq(&dev->event_lock);
  1964. dev->max_vals = max_vals;
  1965. swap(dev->vals, vals);
  1966. spin_unlock_irq(&dev->event_lock);
  1967. /* Because of swap() above, this frees the old vals memory */
  1968. kfree(vals);
  1969. return 0;
  1970. }
  1971. /**
  1972. * input_register_device - register device with input core
  1973. * @dev: device to be registered
  1974. *
  1975. * This function registers device with input core. The device must be
  1976. * allocated with input_allocate_device() and all it's capabilities
  1977. * set up before registering.
  1978. * If function fails the device must be freed with input_free_device().
  1979. * Once device has been successfully registered it can be unregistered
  1980. * with input_unregister_device(); input_free_device() should not be
  1981. * called in this case.
  1982. *
  1983. * Note that this function is also used to register managed input devices
  1984. * (ones allocated with devm_input_allocate_device()). Such managed input
  1985. * devices need not be explicitly unregistered or freed, their tear down
  1986. * is controlled by the devres infrastructure. It is also worth noting
  1987. * that tear down of managed input devices is internally a 2-step process:
  1988. * registered managed input device is first unregistered, but stays in
  1989. * memory and can still handle input_event() calls (although events will
  1990. * not be delivered anywhere). The freeing of managed input device will
  1991. * happen later, when devres stack is unwound to the point where device
  1992. * allocation was made.
  1993. */
  1994. int input_register_device(struct input_dev *dev)
  1995. {
  1996. struct input_devres *devres = NULL;
  1997. struct input_handler *handler;
  1998. const char *path;
  1999. int error;
  2000. if (test_bit(EV_ABS, dev->evbit) && !dev->absinfo) {
  2001. dev_err(&dev->dev,
  2002. "Absolute device without dev->absinfo, refusing to register\n");
  2003. return -EINVAL;
  2004. }
  2005. if (dev->devres_managed) {
  2006. devres = devres_alloc(devm_input_device_unregister,
  2007. sizeof(*devres), GFP_KERNEL);
  2008. if (!devres)
  2009. return -ENOMEM;
  2010. devres->input = dev;
  2011. }
  2012. /* Every input device generates EV_SYN/SYN_REPORT events. */
  2013. __set_bit(EV_SYN, dev->evbit);
  2014. /* KEY_RESERVED is not supposed to be transmitted to userspace. */
  2015. __clear_bit(KEY_RESERVED, dev->keybit);
  2016. /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
  2017. input_cleanse_bitmasks(dev);
  2018. error = input_device_tune_vals(dev);
  2019. if (error)
  2020. goto err_devres_free;
  2021. /*
  2022. * If delay and period are pre-set by the driver, then autorepeating
  2023. * is handled by the driver itself and we don't do it in input.c.
  2024. */
  2025. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD])
  2026. input_enable_softrepeat(dev, 250, 33);
  2027. if (!dev->getkeycode)
  2028. dev->getkeycode = input_default_getkeycode;
  2029. if (!dev->setkeycode)
  2030. dev->setkeycode = input_default_setkeycode;
  2031. if (dev->poller)
  2032. input_dev_poller_finalize(dev->poller);
  2033. error = device_add(&dev->dev);
  2034. if (error)
  2035. goto err_devres_free;
  2036. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  2037. pr_info("%s as %s\n",
  2038. dev->name ? dev->name : "Unspecified device",
  2039. path ? path : "N/A");
  2040. kfree(path);
  2041. error = mutex_lock_interruptible(&input_mutex);
  2042. if (error)
  2043. goto err_device_del;
  2044. list_add_tail(&dev->node, &input_dev_list);
  2045. list_for_each_entry(handler, &input_handler_list, node)
  2046. input_attach_handler(dev, handler);
  2047. input_wakeup_procfs_readers();
  2048. mutex_unlock(&input_mutex);
  2049. if (dev->devres_managed) {
  2050. dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
  2051. __func__, dev_name(&dev->dev));
  2052. devres_add(dev->dev.parent, devres);
  2053. }
  2054. return 0;
  2055. err_device_del:
  2056. device_del(&dev->dev);
  2057. err_devres_free:
  2058. devres_free(devres);
  2059. return error;
  2060. }
  2061. EXPORT_SYMBOL(input_register_device);
  2062. /**
  2063. * input_unregister_device - unregister previously registered device
  2064. * @dev: device to be unregistered
  2065. *
  2066. * This function unregisters an input device. Once device is unregistered
  2067. * the caller should not try to access it as it may get freed at any moment.
  2068. */
  2069. void input_unregister_device(struct input_dev *dev)
  2070. {
  2071. if (dev->devres_managed) {
  2072. WARN_ON(devres_destroy(dev->dev.parent,
  2073. devm_input_device_unregister,
  2074. devm_input_device_match,
  2075. dev));
  2076. __input_unregister_device(dev);
  2077. /*
  2078. * We do not do input_put_device() here because it will be done
  2079. * when 2nd devres fires up.
  2080. */
  2081. } else {
  2082. __input_unregister_device(dev);
  2083. input_put_device(dev);
  2084. }
  2085. }
  2086. EXPORT_SYMBOL(input_unregister_device);
  2087. static int input_handler_check_methods(const struct input_handler *handler)
  2088. {
  2089. int count = 0;
  2090. if (handler->filter)
  2091. count++;
  2092. if (handler->events)
  2093. count++;
  2094. if (handler->event)
  2095. count++;
  2096. if (count > 1) {
  2097. pr_err("%s: only one event processing method can be defined (%s)\n",
  2098. __func__, handler->name);
  2099. return -EINVAL;
  2100. }
  2101. return 0;
  2102. }
  2103. /**
  2104. * input_register_handler - register a new input handler
  2105. * @handler: handler to be registered
  2106. *
  2107. * This function registers a new input handler (interface) for input
  2108. * devices in the system and attaches it to all input devices that
  2109. * are compatible with the handler.
  2110. */
  2111. int input_register_handler(struct input_handler *handler)
  2112. {
  2113. struct input_dev *dev;
  2114. int error;
  2115. error = input_handler_check_methods(handler);
  2116. if (error)
  2117. return error;
  2118. INIT_LIST_HEAD(&handler->h_list);
  2119. error = mutex_lock_interruptible(&input_mutex);
  2120. if (error)
  2121. return error;
  2122. list_add_tail(&handler->node, &input_handler_list);
  2123. list_for_each_entry(dev, &input_dev_list, node)
  2124. input_attach_handler(dev, handler);
  2125. input_wakeup_procfs_readers();
  2126. mutex_unlock(&input_mutex);
  2127. return 0;
  2128. }
  2129. EXPORT_SYMBOL(input_register_handler);
  2130. /**
  2131. * input_unregister_handler - unregisters an input handler
  2132. * @handler: handler to be unregistered
  2133. *
  2134. * This function disconnects a handler from its input devices and
  2135. * removes it from lists of known handlers.
  2136. */
  2137. void input_unregister_handler(struct input_handler *handler)
  2138. {
  2139. struct input_handle *handle, *next;
  2140. mutex_lock(&input_mutex);
  2141. list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
  2142. handler->disconnect(handle);
  2143. WARN_ON(!list_empty(&handler->h_list));
  2144. list_del_init(&handler->node);
  2145. input_wakeup_procfs_readers();
  2146. mutex_unlock(&input_mutex);
  2147. }
  2148. EXPORT_SYMBOL(input_unregister_handler);
  2149. /**
  2150. * input_handler_for_each_handle - handle iterator
  2151. * @handler: input handler to iterate
  2152. * @data: data for the callback
  2153. * @fn: function to be called for each handle
  2154. *
  2155. * Iterate over @bus's list of devices, and call @fn for each, passing
  2156. * it @data and stop when @fn returns a non-zero value. The function is
  2157. * using RCU to traverse the list and therefore may be using in atomic
  2158. * contexts. The @fn callback is invoked from RCU critical section and
  2159. * thus must not sleep.
  2160. */
  2161. int input_handler_for_each_handle(struct input_handler *handler, void *data,
  2162. int (*fn)(struct input_handle *, void *))
  2163. {
  2164. struct input_handle *handle;
  2165. int retval = 0;
  2166. rcu_read_lock();
  2167. list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
  2168. retval = fn(handle, data);
  2169. if (retval)
  2170. break;
  2171. }
  2172. rcu_read_unlock();
  2173. return retval;
  2174. }
  2175. EXPORT_SYMBOL(input_handler_for_each_handle);
  2176. /*
  2177. * An implementation of input_handle's handle_events() method that simply
  2178. * invokes handler->event() method for each event one by one.
  2179. */
  2180. static unsigned int input_handle_events_default(struct input_handle *handle,
  2181. struct input_value *vals,
  2182. unsigned int count)
  2183. {
  2184. struct input_handler *handler = handle->handler;
  2185. struct input_value *v;
  2186. for (v = vals; v != vals + count; v++)
  2187. handler->event(handle, v->type, v->code, v->value);
  2188. return count;
  2189. }
  2190. /*
  2191. * An implementation of input_handle's handle_events() method that invokes
  2192. * handler->filter() method for each event one by one and removes events
  2193. * that were filtered out from the "vals" array.
  2194. */
  2195. static unsigned int input_handle_events_filter(struct input_handle *handle,
  2196. struct input_value *vals,
  2197. unsigned int count)
  2198. {
  2199. struct input_handler *handler = handle->handler;
  2200. struct input_value *end = vals;
  2201. struct input_value *v;
  2202. for (v = vals; v != vals + count; v++) {
  2203. if (handler->filter(handle, v->type, v->code, v->value))
  2204. continue;
  2205. if (end != v)
  2206. *end = *v;
  2207. end++;
  2208. }
  2209. return end - vals;
  2210. }
  2211. /*
  2212. * An implementation of input_handle's handle_events() method that does nothing.
  2213. */
  2214. static unsigned int input_handle_events_null(struct input_handle *handle,
  2215. struct input_value *vals,
  2216. unsigned int count)
  2217. {
  2218. return count;
  2219. }
  2220. /*
  2221. * Sets up appropriate handle->event_handler based on the input_handler
  2222. * associated with the handle.
  2223. */
  2224. static void input_handle_setup_event_handler(struct input_handle *handle)
  2225. {
  2226. struct input_handler *handler = handle->handler;
  2227. if (handler->filter)
  2228. handle->handle_events = input_handle_events_filter;
  2229. else if (handler->event)
  2230. handle->handle_events = input_handle_events_default;
  2231. else if (handler->events)
  2232. handle->handle_events = handler->events;
  2233. else
  2234. handle->handle_events = input_handle_events_null;
  2235. }
  2236. /**
  2237. * input_register_handle - register a new input handle
  2238. * @handle: handle to register
  2239. *
  2240. * This function puts a new input handle onto device's
  2241. * and handler's lists so that events can flow through
  2242. * it once it is opened using input_open_device().
  2243. *
  2244. * This function is supposed to be called from handler's
  2245. * connect() method.
  2246. */
  2247. int input_register_handle(struct input_handle *handle)
  2248. {
  2249. struct input_handler *handler = handle->handler;
  2250. struct input_dev *dev = handle->dev;
  2251. int error;
  2252. input_handle_setup_event_handler(handle);
  2253. /*
  2254. * We take dev->mutex here to prevent race with
  2255. * input_release_device().
  2256. */
  2257. error = mutex_lock_interruptible(&dev->mutex);
  2258. if (error)
  2259. return error;
  2260. /*
  2261. * Filters go to the head of the list, normal handlers
  2262. * to the tail.
  2263. */
  2264. if (handler->filter)
  2265. list_add_rcu(&handle->d_node, &dev->h_list);
  2266. else
  2267. list_add_tail_rcu(&handle->d_node, &dev->h_list);
  2268. mutex_unlock(&dev->mutex);
  2269. /*
  2270. * Since we are supposed to be called from ->connect()
  2271. * which is mutually exclusive with ->disconnect()
  2272. * we can't be racing with input_unregister_handle()
  2273. * and so separate lock is not needed here.
  2274. */
  2275. list_add_tail_rcu(&handle->h_node, &handler->h_list);
  2276. if (handler->start)
  2277. handler->start(handle);
  2278. return 0;
  2279. }
  2280. EXPORT_SYMBOL(input_register_handle);
  2281. /**
  2282. * input_unregister_handle - unregister an input handle
  2283. * @handle: handle to unregister
  2284. *
  2285. * This function removes input handle from device's
  2286. * and handler's lists.
  2287. *
  2288. * This function is supposed to be called from handler's
  2289. * disconnect() method.
  2290. */
  2291. void input_unregister_handle(struct input_handle *handle)
  2292. {
  2293. struct input_dev *dev = handle->dev;
  2294. list_del_rcu(&handle->h_node);
  2295. /*
  2296. * Take dev->mutex to prevent race with input_release_device().
  2297. */
  2298. mutex_lock(&dev->mutex);
  2299. list_del_rcu(&handle->d_node);
  2300. mutex_unlock(&dev->mutex);
  2301. synchronize_rcu();
  2302. }
  2303. EXPORT_SYMBOL(input_unregister_handle);
  2304. /**
  2305. * input_get_new_minor - allocates a new input minor number
  2306. * @legacy_base: beginning or the legacy range to be searched
  2307. * @legacy_num: size of legacy range
  2308. * @allow_dynamic: whether we can also take ID from the dynamic range
  2309. *
  2310. * This function allocates a new device minor for from input major namespace.
  2311. * Caller can request legacy minor by specifying @legacy_base and @legacy_num
  2312. * parameters and whether ID can be allocated from dynamic range if there are
  2313. * no free IDs in legacy range.
  2314. */
  2315. int input_get_new_minor(int legacy_base, unsigned int legacy_num,
  2316. bool allow_dynamic)
  2317. {
  2318. /*
  2319. * This function should be called from input handler's ->connect()
  2320. * methods, which are serialized with input_mutex, so no additional
  2321. * locking is needed here.
  2322. */
  2323. if (legacy_base >= 0) {
  2324. int minor = ida_alloc_range(&input_ida, legacy_base,
  2325. legacy_base + legacy_num - 1,
  2326. GFP_KERNEL);
  2327. if (minor >= 0 || !allow_dynamic)
  2328. return minor;
  2329. }
  2330. return ida_alloc_range(&input_ida, INPUT_FIRST_DYNAMIC_DEV,
  2331. INPUT_MAX_CHAR_DEVICES - 1, GFP_KERNEL);
  2332. }
  2333. EXPORT_SYMBOL(input_get_new_minor);
  2334. /**
  2335. * input_free_minor - release previously allocated minor
  2336. * @minor: minor to be released
  2337. *
  2338. * This function releases previously allocated input minor so that it can be
  2339. * reused later.
  2340. */
  2341. void input_free_minor(unsigned int minor)
  2342. {
  2343. ida_free(&input_ida, minor);
  2344. }
  2345. EXPORT_SYMBOL(input_free_minor);
  2346. static int __init input_init(void)
  2347. {
  2348. int err;
  2349. err = class_register(&input_class);
  2350. if (err) {
  2351. pr_err("unable to register input_dev class\n");
  2352. return err;
  2353. }
  2354. err = input_proc_init();
  2355. if (err)
  2356. goto fail1;
  2357. err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),
  2358. INPUT_MAX_CHAR_DEVICES, "input");
  2359. if (err) {
  2360. pr_err("unable to register char major %d", INPUT_MAJOR);
  2361. goto fail2;
  2362. }
  2363. return 0;
  2364. fail2: input_proc_exit();
  2365. fail1: class_unregister(&input_class);
  2366. return err;
  2367. }
  2368. static void __exit input_exit(void)
  2369. {
  2370. input_proc_exit();
  2371. unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0),
  2372. INPUT_MAX_CHAR_DEVICES);
  2373. class_unregister(&input_class);
  2374. }
  2375. subsys_initcall(input_init);
  2376. module_exit(input_exit);