input.c 60 KB

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