input.c 61 KB

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