input.c 70 KB

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