input.c 67 KB

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