input.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  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. if (*old_keycode <= KEY_MAX) {
  712. __clear_bit(*old_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. /* Setting the bit twice is useless, so break */
  717. break;
  718. }
  719. }
  720. }
  721. __set_bit(ke->keycode, dev->keybit);
  722. return 0;
  723. }
  724. /**
  725. * input_get_keycode - retrieve keycode currently mapped to a given scancode
  726. * @dev: input device which keymap is being queried
  727. * @ke: keymap entry
  728. *
  729. * This function should be called by anyone interested in retrieving current
  730. * keymap. Presently evdev handlers use it.
  731. */
  732. int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)
  733. {
  734. unsigned long flags;
  735. int retval;
  736. spin_lock_irqsave(&dev->event_lock, flags);
  737. retval = dev->getkeycode(dev, ke);
  738. spin_unlock_irqrestore(&dev->event_lock, flags);
  739. return retval;
  740. }
  741. EXPORT_SYMBOL(input_get_keycode);
  742. /**
  743. * input_set_keycode - attribute a keycode to a given scancode
  744. * @dev: input device which keymap is being updated
  745. * @ke: new keymap entry
  746. *
  747. * This function should be called by anyone needing to update current
  748. * keymap. Presently keyboard and evdev handlers use it.
  749. */
  750. int input_set_keycode(struct input_dev *dev,
  751. const struct input_keymap_entry *ke)
  752. {
  753. unsigned long flags;
  754. unsigned int old_keycode;
  755. int retval;
  756. if (ke->keycode > KEY_MAX)
  757. return -EINVAL;
  758. spin_lock_irqsave(&dev->event_lock, flags);
  759. retval = dev->setkeycode(dev, ke, &old_keycode);
  760. if (retval)
  761. goto out;
  762. /* Make sure KEY_RESERVED did not get enabled. */
  763. __clear_bit(KEY_RESERVED, dev->keybit);
  764. /*
  765. * Simulate keyup event if keycode is not present
  766. * in the keymap anymore
  767. */
  768. if (old_keycode > KEY_MAX) {
  769. dev_warn(dev->dev.parent ?: &dev->dev,
  770. "%s: got too big old keycode %#x\n",
  771. __func__, old_keycode);
  772. } else if (test_bit(EV_KEY, dev->evbit) &&
  773. !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
  774. __test_and_clear_bit(old_keycode, dev->key)) {
  775. struct input_value vals[] = {
  776. { EV_KEY, old_keycode, 0 },
  777. input_value_sync
  778. };
  779. input_pass_values(dev, vals, ARRAY_SIZE(vals));
  780. }
  781. out:
  782. spin_unlock_irqrestore(&dev->event_lock, flags);
  783. return retval;
  784. }
  785. EXPORT_SYMBOL(input_set_keycode);
  786. bool input_match_device_id(const struct input_dev *dev,
  787. const struct input_device_id *id)
  788. {
  789. if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  790. if (id->bustype != dev->id.bustype)
  791. return false;
  792. if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  793. if (id->vendor != dev->id.vendor)
  794. return false;
  795. if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  796. if (id->product != dev->id.product)
  797. return false;
  798. if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  799. if (id->version != dev->id.version)
  800. return false;
  801. if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX) ||
  802. !bitmap_subset(id->keybit, dev->keybit, KEY_MAX) ||
  803. !bitmap_subset(id->relbit, dev->relbit, REL_MAX) ||
  804. !bitmap_subset(id->absbit, dev->absbit, ABS_MAX) ||
  805. !bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX) ||
  806. !bitmap_subset(id->ledbit, dev->ledbit, LED_MAX) ||
  807. !bitmap_subset(id->sndbit, dev->sndbit, SND_MAX) ||
  808. !bitmap_subset(id->ffbit, dev->ffbit, FF_MAX) ||
  809. !bitmap_subset(id->swbit, dev->swbit, SW_MAX) ||
  810. !bitmap_subset(id->propbit, dev->propbit, INPUT_PROP_MAX)) {
  811. return false;
  812. }
  813. return true;
  814. }
  815. EXPORT_SYMBOL(input_match_device_id);
  816. static const struct input_device_id *input_match_device(struct input_handler *handler,
  817. struct input_dev *dev)
  818. {
  819. const struct input_device_id *id;
  820. for (id = handler->id_table; id->flags || id->driver_info; id++) {
  821. if (input_match_device_id(dev, id) &&
  822. (!handler->match || handler->match(handler, dev))) {
  823. return id;
  824. }
  825. }
  826. return NULL;
  827. }
  828. static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
  829. {
  830. const struct input_device_id *id;
  831. int error;
  832. id = input_match_device(handler, dev);
  833. if (!id)
  834. return -ENODEV;
  835. error = handler->connect(handler, dev, id);
  836. if (error && error != -ENODEV)
  837. pr_err("failed to attach handler %s to device %s, error: %d\n",
  838. handler->name, kobject_name(&dev->dev.kobj), error);
  839. return error;
  840. }
  841. #ifdef CONFIG_COMPAT
  842. static int input_bits_to_string(char *buf, int buf_size,
  843. unsigned long bits, bool skip_empty)
  844. {
  845. int len = 0;
  846. if (in_compat_syscall()) {
  847. u32 dword = bits >> 32;
  848. if (dword || !skip_empty)
  849. len += snprintf(buf, buf_size, "%x ", dword);
  850. dword = bits & 0xffffffffUL;
  851. if (dword || !skip_empty || len)
  852. len += snprintf(buf + len, max(buf_size - len, 0),
  853. "%x", dword);
  854. } else {
  855. if (bits || !skip_empty)
  856. len += snprintf(buf, buf_size, "%lx", bits);
  857. }
  858. return len;
  859. }
  860. #else /* !CONFIG_COMPAT */
  861. static int input_bits_to_string(char *buf, int buf_size,
  862. unsigned long bits, bool skip_empty)
  863. {
  864. return bits || !skip_empty ?
  865. snprintf(buf, buf_size, "%lx", bits) : 0;
  866. }
  867. #endif
  868. #ifdef CONFIG_PROC_FS
  869. static struct proc_dir_entry *proc_bus_input_dir;
  870. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  871. static int input_devices_state;
  872. static inline void input_wakeup_procfs_readers(void)
  873. {
  874. input_devices_state++;
  875. wake_up(&input_devices_poll_wait);
  876. }
  877. static __poll_t input_proc_devices_poll(struct file *file, poll_table *wait)
  878. {
  879. poll_wait(file, &input_devices_poll_wait, wait);
  880. if (file->f_version != input_devices_state) {
  881. file->f_version = input_devices_state;
  882. return EPOLLIN | EPOLLRDNORM;
  883. }
  884. return 0;
  885. }
  886. union input_seq_state {
  887. struct {
  888. unsigned short pos;
  889. bool mutex_acquired;
  890. };
  891. void *p;
  892. };
  893. static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
  894. {
  895. union input_seq_state *state = (union input_seq_state *)&seq->private;
  896. int error;
  897. /* We need to fit into seq->private pointer */
  898. BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
  899. error = mutex_lock_interruptible(&input_mutex);
  900. if (error) {
  901. state->mutex_acquired = false;
  902. return ERR_PTR(error);
  903. }
  904. state->mutex_acquired = true;
  905. return seq_list_start(&input_dev_list, *pos);
  906. }
  907. static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  908. {
  909. return seq_list_next(v, &input_dev_list, pos);
  910. }
  911. static void input_seq_stop(struct seq_file *seq, void *v)
  912. {
  913. union input_seq_state *state = (union input_seq_state *)&seq->private;
  914. if (state->mutex_acquired)
  915. mutex_unlock(&input_mutex);
  916. }
  917. static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
  918. unsigned long *bitmap, int max)
  919. {
  920. int i;
  921. bool skip_empty = true;
  922. char buf[18];
  923. seq_printf(seq, "B: %s=", name);
  924. for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
  925. if (input_bits_to_string(buf, sizeof(buf),
  926. bitmap[i], skip_empty)) {
  927. skip_empty = false;
  928. seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
  929. }
  930. }
  931. /*
  932. * If no output was produced print a single 0.
  933. */
  934. if (skip_empty)
  935. seq_putc(seq, '0');
  936. seq_putc(seq, '\n');
  937. }
  938. static int input_devices_seq_show(struct seq_file *seq, void *v)
  939. {
  940. struct input_dev *dev = container_of(v, struct input_dev, node);
  941. const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  942. struct input_handle *handle;
  943. seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  944. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  945. seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  946. seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  947. seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
  948. seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
  949. seq_puts(seq, "H: Handlers=");
  950. list_for_each_entry(handle, &dev->h_list, d_node)
  951. seq_printf(seq, "%s ", handle->name);
  952. seq_putc(seq, '\n');
  953. input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX);
  954. input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
  955. if (test_bit(EV_KEY, dev->evbit))
  956. input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
  957. if (test_bit(EV_REL, dev->evbit))
  958. input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
  959. if (test_bit(EV_ABS, dev->evbit))
  960. input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
  961. if (test_bit(EV_MSC, dev->evbit))
  962. input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
  963. if (test_bit(EV_LED, dev->evbit))
  964. input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
  965. if (test_bit(EV_SND, dev->evbit))
  966. input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
  967. if (test_bit(EV_FF, dev->evbit))
  968. input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
  969. if (test_bit(EV_SW, dev->evbit))
  970. input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
  971. seq_putc(seq, '\n');
  972. kfree(path);
  973. return 0;
  974. }
  975. static const struct seq_operations input_devices_seq_ops = {
  976. .start = input_devices_seq_start,
  977. .next = input_devices_seq_next,
  978. .stop = input_seq_stop,
  979. .show = input_devices_seq_show,
  980. };
  981. static int input_proc_devices_open(struct inode *inode, struct file *file)
  982. {
  983. return seq_open(file, &input_devices_seq_ops);
  984. }
  985. static const struct file_operations input_devices_fileops = {
  986. .owner = THIS_MODULE,
  987. .open = input_proc_devices_open,
  988. .poll = input_proc_devices_poll,
  989. .read = seq_read,
  990. .llseek = seq_lseek,
  991. .release = seq_release,
  992. };
  993. static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
  994. {
  995. union input_seq_state *state = (union input_seq_state *)&seq->private;
  996. int error;
  997. /* We need to fit into seq->private pointer */
  998. BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
  999. error = mutex_lock_interruptible(&input_mutex);
  1000. if (error) {
  1001. state->mutex_acquired = false;
  1002. return ERR_PTR(error);
  1003. }
  1004. state->mutex_acquired = true;
  1005. state->pos = *pos;
  1006. return seq_list_start(&input_handler_list, *pos);
  1007. }
  1008. static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1009. {
  1010. union input_seq_state *state = (union input_seq_state *)&seq->private;
  1011. state->pos = *pos + 1;
  1012. return seq_list_next(v, &input_handler_list, pos);
  1013. }
  1014. static int input_handlers_seq_show(struct seq_file *seq, void *v)
  1015. {
  1016. struct input_handler *handler = container_of(v, struct input_handler, node);
  1017. union input_seq_state *state = (union input_seq_state *)&seq->private;
  1018. seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
  1019. if (handler->filter)
  1020. seq_puts(seq, " (filter)");
  1021. if (handler->legacy_minors)
  1022. seq_printf(seq, " Minor=%d", handler->minor);
  1023. seq_putc(seq, '\n');
  1024. return 0;
  1025. }
  1026. static const struct seq_operations input_handlers_seq_ops = {
  1027. .start = input_handlers_seq_start,
  1028. .next = input_handlers_seq_next,
  1029. .stop = input_seq_stop,
  1030. .show = input_handlers_seq_show,
  1031. };
  1032. static int input_proc_handlers_open(struct inode *inode, struct file *file)
  1033. {
  1034. return seq_open(file, &input_handlers_seq_ops);
  1035. }
  1036. static const struct file_operations input_handlers_fileops = {
  1037. .owner = THIS_MODULE,
  1038. .open = input_proc_handlers_open,
  1039. .read = seq_read,
  1040. .llseek = seq_lseek,
  1041. .release = seq_release,
  1042. };
  1043. static int __init input_proc_init(void)
  1044. {
  1045. struct proc_dir_entry *entry;
  1046. proc_bus_input_dir = proc_mkdir("bus/input", NULL);
  1047. if (!proc_bus_input_dir)
  1048. return -ENOMEM;
  1049. entry = proc_create("devices", 0, proc_bus_input_dir,
  1050. &input_devices_fileops);
  1051. if (!entry)
  1052. goto fail1;
  1053. entry = proc_create("handlers", 0, proc_bus_input_dir,
  1054. &input_handlers_fileops);
  1055. if (!entry)
  1056. goto fail2;
  1057. return 0;
  1058. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  1059. fail1: remove_proc_entry("bus/input", NULL);
  1060. return -ENOMEM;
  1061. }
  1062. static void input_proc_exit(void)
  1063. {
  1064. remove_proc_entry("devices", proc_bus_input_dir);
  1065. remove_proc_entry("handlers", proc_bus_input_dir);
  1066. remove_proc_entry("bus/input", NULL);
  1067. }
  1068. #else /* !CONFIG_PROC_FS */
  1069. static inline void input_wakeup_procfs_readers(void) { }
  1070. static inline int input_proc_init(void) { return 0; }
  1071. static inline void input_proc_exit(void) { }
  1072. #endif
  1073. #define INPUT_DEV_STRING_ATTR_SHOW(name) \
  1074. static ssize_t input_dev_show_##name(struct device *dev, \
  1075. struct device_attribute *attr, \
  1076. char *buf) \
  1077. { \
  1078. struct input_dev *input_dev = to_input_dev(dev); \
  1079. \
  1080. return scnprintf(buf, PAGE_SIZE, "%s\n", \
  1081. input_dev->name ? input_dev->name : ""); \
  1082. } \
  1083. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
  1084. INPUT_DEV_STRING_ATTR_SHOW(name);
  1085. INPUT_DEV_STRING_ATTR_SHOW(phys);
  1086. INPUT_DEV_STRING_ATTR_SHOW(uniq);
  1087. static int input_print_modalias_bits(char *buf, int size,
  1088. char name, unsigned long *bm,
  1089. unsigned int min_bit, unsigned int max_bit)
  1090. {
  1091. int len = 0, i;
  1092. len += snprintf(buf, max(size, 0), "%c", name);
  1093. for (i = min_bit; i < max_bit; i++)
  1094. if (bm[BIT_WORD(i)] & BIT_MASK(i))
  1095. len += snprintf(buf + len, max(size - len, 0), "%X,", i);
  1096. return len;
  1097. }
  1098. static int input_print_modalias(char *buf, int size, struct input_dev *id,
  1099. int add_cr)
  1100. {
  1101. int len;
  1102. len = snprintf(buf, max(size, 0),
  1103. "input:b%04Xv%04Xp%04Xe%04X-",
  1104. id->id.bustype, id->id.vendor,
  1105. id->id.product, id->id.version);
  1106. len += input_print_modalias_bits(buf + len, size - len,
  1107. 'e', id->evbit, 0, EV_MAX);
  1108. len += input_print_modalias_bits(buf + len, size - len,
  1109. 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
  1110. len += input_print_modalias_bits(buf + len, size - len,
  1111. 'r', id->relbit, 0, REL_MAX);
  1112. len += input_print_modalias_bits(buf + len, size - len,
  1113. 'a', id->absbit, 0, ABS_MAX);
  1114. len += input_print_modalias_bits(buf + len, size - len,
  1115. 'm', id->mscbit, 0, MSC_MAX);
  1116. len += input_print_modalias_bits(buf + len, size - len,
  1117. 'l', id->ledbit, 0, LED_MAX);
  1118. len += input_print_modalias_bits(buf + len, size - len,
  1119. 's', id->sndbit, 0, SND_MAX);
  1120. len += input_print_modalias_bits(buf + len, size - len,
  1121. 'f', id->ffbit, 0, FF_MAX);
  1122. len += input_print_modalias_bits(buf + len, size - len,
  1123. 'w', id->swbit, 0, SW_MAX);
  1124. if (add_cr)
  1125. len += snprintf(buf + len, max(size - len, 0), "\n");
  1126. return len;
  1127. }
  1128. static ssize_t input_dev_show_modalias(struct device *dev,
  1129. struct device_attribute *attr,
  1130. char *buf)
  1131. {
  1132. struct input_dev *id = to_input_dev(dev);
  1133. ssize_t len;
  1134. len = input_print_modalias(buf, PAGE_SIZE, id, 1);
  1135. return min_t(int, len, PAGE_SIZE);
  1136. }
  1137. static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
  1138. static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
  1139. int max, int add_cr);
  1140. static ssize_t input_dev_show_properties(struct device *dev,
  1141. struct device_attribute *attr,
  1142. char *buf)
  1143. {
  1144. struct input_dev *input_dev = to_input_dev(dev);
  1145. int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit,
  1146. INPUT_PROP_MAX, true);
  1147. return min_t(int, len, PAGE_SIZE);
  1148. }
  1149. static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL);
  1150. static struct attribute *input_dev_attrs[] = {
  1151. &dev_attr_name.attr,
  1152. &dev_attr_phys.attr,
  1153. &dev_attr_uniq.attr,
  1154. &dev_attr_modalias.attr,
  1155. &dev_attr_properties.attr,
  1156. NULL
  1157. };
  1158. static const struct attribute_group input_dev_attr_group = {
  1159. .attrs = input_dev_attrs,
  1160. };
  1161. #define INPUT_DEV_ID_ATTR(name) \
  1162. static ssize_t input_dev_show_id_##name(struct device *dev, \
  1163. struct device_attribute *attr, \
  1164. char *buf) \
  1165. { \
  1166. struct input_dev *input_dev = to_input_dev(dev); \
  1167. return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
  1168. } \
  1169. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
  1170. INPUT_DEV_ID_ATTR(bustype);
  1171. INPUT_DEV_ID_ATTR(vendor);
  1172. INPUT_DEV_ID_ATTR(product);
  1173. INPUT_DEV_ID_ATTR(version);
  1174. static struct attribute *input_dev_id_attrs[] = {
  1175. &dev_attr_bustype.attr,
  1176. &dev_attr_vendor.attr,
  1177. &dev_attr_product.attr,
  1178. &dev_attr_version.attr,
  1179. NULL
  1180. };
  1181. static const struct attribute_group input_dev_id_attr_group = {
  1182. .name = "id",
  1183. .attrs = input_dev_id_attrs,
  1184. };
  1185. static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
  1186. int max, int add_cr)
  1187. {
  1188. int i;
  1189. int len = 0;
  1190. bool skip_empty = true;
  1191. for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
  1192. len += input_bits_to_string(buf + len, max(buf_size - len, 0),
  1193. bitmap[i], skip_empty);
  1194. if (len) {
  1195. skip_empty = false;
  1196. if (i > 0)
  1197. len += snprintf(buf + len, max(buf_size - len, 0), " ");
  1198. }
  1199. }
  1200. /*
  1201. * If no output was produced print a single 0.
  1202. */
  1203. if (len == 0)
  1204. len = snprintf(buf, buf_size, "%d", 0);
  1205. if (add_cr)
  1206. len += snprintf(buf + len, max(buf_size - len, 0), "\n");
  1207. return len;
  1208. }
  1209. #define INPUT_DEV_CAP_ATTR(ev, bm) \
  1210. static ssize_t input_dev_show_cap_##bm(struct device *dev, \
  1211. struct device_attribute *attr, \
  1212. char *buf) \
  1213. { \
  1214. struct input_dev *input_dev = to_input_dev(dev); \
  1215. int len = input_print_bitmap(buf, PAGE_SIZE, \
  1216. input_dev->bm##bit, ev##_MAX, \
  1217. true); \
  1218. return min_t(int, len, PAGE_SIZE); \
  1219. } \
  1220. static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
  1221. INPUT_DEV_CAP_ATTR(EV, ev);
  1222. INPUT_DEV_CAP_ATTR(KEY, key);
  1223. INPUT_DEV_CAP_ATTR(REL, rel);
  1224. INPUT_DEV_CAP_ATTR(ABS, abs);
  1225. INPUT_DEV_CAP_ATTR(MSC, msc);
  1226. INPUT_DEV_CAP_ATTR(LED, led);
  1227. INPUT_DEV_CAP_ATTR(SND, snd);
  1228. INPUT_DEV_CAP_ATTR(FF, ff);
  1229. INPUT_DEV_CAP_ATTR(SW, sw);
  1230. static struct attribute *input_dev_caps_attrs[] = {
  1231. &dev_attr_ev.attr,
  1232. &dev_attr_key.attr,
  1233. &dev_attr_rel.attr,
  1234. &dev_attr_abs.attr,
  1235. &dev_attr_msc.attr,
  1236. &dev_attr_led.attr,
  1237. &dev_attr_snd.attr,
  1238. &dev_attr_ff.attr,
  1239. &dev_attr_sw.attr,
  1240. NULL
  1241. };
  1242. static const struct attribute_group input_dev_caps_attr_group = {
  1243. .name = "capabilities",
  1244. .attrs = input_dev_caps_attrs,
  1245. };
  1246. static const struct attribute_group *input_dev_attr_groups[] = {
  1247. &input_dev_attr_group,
  1248. &input_dev_id_attr_group,
  1249. &input_dev_caps_attr_group,
  1250. NULL
  1251. };
  1252. static void input_dev_release(struct device *device)
  1253. {
  1254. struct input_dev *dev = to_input_dev(device);
  1255. input_ff_destroy(dev);
  1256. input_mt_destroy_slots(dev);
  1257. kfree(dev->absinfo);
  1258. kfree(dev->vals);
  1259. kfree(dev);
  1260. module_put(THIS_MODULE);
  1261. }
  1262. /*
  1263. * Input uevent interface - loading event handlers based on
  1264. * device bitfields.
  1265. */
  1266. static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
  1267. const char *name, unsigned long *bitmap, int max)
  1268. {
  1269. int len;
  1270. if (add_uevent_var(env, "%s", name))
  1271. return -ENOMEM;
  1272. len = input_print_bitmap(&env->buf[env->buflen - 1],
  1273. sizeof(env->buf) - env->buflen,
  1274. bitmap, max, false);
  1275. if (len >= (sizeof(env->buf) - env->buflen))
  1276. return -ENOMEM;
  1277. env->buflen += len;
  1278. return 0;
  1279. }
  1280. static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
  1281. struct input_dev *dev)
  1282. {
  1283. int len;
  1284. if (add_uevent_var(env, "MODALIAS="))
  1285. return -ENOMEM;
  1286. len = input_print_modalias(&env->buf[env->buflen - 1],
  1287. sizeof(env->buf) - env->buflen,
  1288. dev, 0);
  1289. if (len >= (sizeof(env->buf) - env->buflen))
  1290. return -ENOMEM;
  1291. env->buflen += len;
  1292. return 0;
  1293. }
  1294. #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
  1295. do { \
  1296. int err = add_uevent_var(env, fmt, val); \
  1297. if (err) \
  1298. return err; \
  1299. } while (0)
  1300. #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
  1301. do { \
  1302. int err = input_add_uevent_bm_var(env, name, bm, max); \
  1303. if (err) \
  1304. return err; \
  1305. } while (0)
  1306. #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
  1307. do { \
  1308. int err = input_add_uevent_modalias_var(env, dev); \
  1309. if (err) \
  1310. return err; \
  1311. } while (0)
  1312. static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  1313. {
  1314. struct input_dev *dev = to_input_dev(device);
  1315. INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
  1316. dev->id.bustype, dev->id.vendor,
  1317. dev->id.product, dev->id.version);
  1318. if (dev->name)
  1319. INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
  1320. if (dev->phys)
  1321. INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
  1322. if (dev->uniq)
  1323. INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
  1324. INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX);
  1325. INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
  1326. if (test_bit(EV_KEY, dev->evbit))
  1327. INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
  1328. if (test_bit(EV_REL, dev->evbit))
  1329. INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
  1330. if (test_bit(EV_ABS, dev->evbit))
  1331. INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
  1332. if (test_bit(EV_MSC, dev->evbit))
  1333. INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
  1334. if (test_bit(EV_LED, dev->evbit))
  1335. INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
  1336. if (test_bit(EV_SND, dev->evbit))
  1337. INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
  1338. if (test_bit(EV_FF, dev->evbit))
  1339. INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
  1340. if (test_bit(EV_SW, dev->evbit))
  1341. INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
  1342. INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
  1343. return 0;
  1344. }
  1345. #define INPUT_DO_TOGGLE(dev, type, bits, on) \
  1346. do { \
  1347. int i; \
  1348. bool active; \
  1349. \
  1350. if (!test_bit(EV_##type, dev->evbit)) \
  1351. break; \
  1352. \
  1353. for_each_set_bit(i, dev->bits##bit, type##_CNT) { \
  1354. active = test_bit(i, dev->bits); \
  1355. if (!active && !on) \
  1356. continue; \
  1357. \
  1358. dev->event(dev, EV_##type, i, on ? active : 0); \
  1359. } \
  1360. } while (0)
  1361. static void input_dev_toggle(struct input_dev *dev, bool activate)
  1362. {
  1363. if (!dev->event)
  1364. return;
  1365. INPUT_DO_TOGGLE(dev, LED, led, activate);
  1366. INPUT_DO_TOGGLE(dev, SND, snd, activate);
  1367. if (activate && test_bit(EV_REP, dev->evbit)) {
  1368. dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
  1369. dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
  1370. }
  1371. }
  1372. /**
  1373. * input_reset_device() - reset/restore the state of input device
  1374. * @dev: input device whose state needs to be reset
  1375. *
  1376. * This function tries to reset the state of an opened input device and
  1377. * bring internal state and state if the hardware in sync with each other.
  1378. * We mark all keys as released, restore LED state, repeat rate, etc.
  1379. */
  1380. void input_reset_device(struct input_dev *dev)
  1381. {
  1382. unsigned long flags;
  1383. mutex_lock(&dev->mutex);
  1384. spin_lock_irqsave(&dev->event_lock, flags);
  1385. input_dev_toggle(dev, true);
  1386. input_dev_release_keys(dev);
  1387. spin_unlock_irqrestore(&dev->event_lock, flags);
  1388. mutex_unlock(&dev->mutex);
  1389. }
  1390. EXPORT_SYMBOL(input_reset_device);
  1391. #ifdef CONFIG_PM_SLEEP
  1392. static int input_dev_suspend(struct device *dev)
  1393. {
  1394. struct input_dev *input_dev = to_input_dev(dev);
  1395. spin_lock_irq(&input_dev->event_lock);
  1396. /*
  1397. * Keys that are pressed now are unlikely to be
  1398. * still pressed when we resume.
  1399. */
  1400. input_dev_release_keys(input_dev);
  1401. /* Turn off LEDs and sounds, if any are active. */
  1402. input_dev_toggle(input_dev, false);
  1403. spin_unlock_irq(&input_dev->event_lock);
  1404. return 0;
  1405. }
  1406. static int input_dev_resume(struct device *dev)
  1407. {
  1408. struct input_dev *input_dev = to_input_dev(dev);
  1409. spin_lock_irq(&input_dev->event_lock);
  1410. /* Restore state of LEDs and sounds, if any were active. */
  1411. input_dev_toggle(input_dev, true);
  1412. spin_unlock_irq(&input_dev->event_lock);
  1413. return 0;
  1414. }
  1415. static int input_dev_freeze(struct device *dev)
  1416. {
  1417. struct input_dev *input_dev = to_input_dev(dev);
  1418. spin_lock_irq(&input_dev->event_lock);
  1419. /*
  1420. * Keys that are pressed now are unlikely to be
  1421. * still pressed when we resume.
  1422. */
  1423. input_dev_release_keys(input_dev);
  1424. spin_unlock_irq(&input_dev->event_lock);
  1425. return 0;
  1426. }
  1427. static int input_dev_poweroff(struct device *dev)
  1428. {
  1429. struct input_dev *input_dev = to_input_dev(dev);
  1430. spin_lock_irq(&input_dev->event_lock);
  1431. /* Turn off LEDs and sounds, if any are active. */
  1432. input_dev_toggle(input_dev, false);
  1433. spin_unlock_irq(&input_dev->event_lock);
  1434. return 0;
  1435. }
  1436. static const struct dev_pm_ops input_dev_pm_ops = {
  1437. .suspend = input_dev_suspend,
  1438. .resume = input_dev_resume,
  1439. .freeze = input_dev_freeze,
  1440. .poweroff = input_dev_poweroff,
  1441. .restore = input_dev_resume,
  1442. };
  1443. #endif /* CONFIG_PM */
  1444. static const struct device_type input_dev_type = {
  1445. .groups = input_dev_attr_groups,
  1446. .release = input_dev_release,
  1447. .uevent = input_dev_uevent,
  1448. #ifdef CONFIG_PM_SLEEP
  1449. .pm = &input_dev_pm_ops,
  1450. #endif
  1451. };
  1452. static char *input_devnode(struct device *dev, umode_t *mode)
  1453. {
  1454. return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
  1455. }
  1456. struct class input_class = {
  1457. .name = "input",
  1458. .devnode = input_devnode,
  1459. };
  1460. EXPORT_SYMBOL_GPL(input_class);
  1461. /**
  1462. * input_allocate_device - allocate memory for new input device
  1463. *
  1464. * Returns prepared struct input_dev or %NULL.
  1465. *
  1466. * NOTE: Use input_free_device() to free devices that have not been
  1467. * registered; input_unregister_device() should be used for already
  1468. * registered devices.
  1469. */
  1470. struct input_dev *input_allocate_device(void)
  1471. {
  1472. static atomic_t input_no = ATOMIC_INIT(-1);
  1473. struct input_dev *dev;
  1474. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1475. if (dev) {
  1476. dev->dev.type = &input_dev_type;
  1477. dev->dev.class = &input_class;
  1478. device_initialize(&dev->dev);
  1479. mutex_init(&dev->mutex);
  1480. spin_lock_init(&dev->event_lock);
  1481. timer_setup(&dev->timer, NULL, 0);
  1482. INIT_LIST_HEAD(&dev->h_list);
  1483. INIT_LIST_HEAD(&dev->node);
  1484. dev_set_name(&dev->dev, "input%lu",
  1485. (unsigned long)atomic_inc_return(&input_no));
  1486. __module_get(THIS_MODULE);
  1487. }
  1488. return dev;
  1489. }
  1490. EXPORT_SYMBOL(input_allocate_device);
  1491. struct input_devres {
  1492. struct input_dev *input;
  1493. };
  1494. static int devm_input_device_match(struct device *dev, void *res, void *data)
  1495. {
  1496. struct input_devres *devres = res;
  1497. return devres->input == data;
  1498. }
  1499. static void devm_input_device_release(struct device *dev, void *res)
  1500. {
  1501. struct input_devres *devres = res;
  1502. struct input_dev *input = devres->input;
  1503. dev_dbg(dev, "%s: dropping reference to %s\n",
  1504. __func__, dev_name(&input->dev));
  1505. input_put_device(input);
  1506. }
  1507. /**
  1508. * devm_input_allocate_device - allocate managed input device
  1509. * @dev: device owning the input device being created
  1510. *
  1511. * Returns prepared struct input_dev or %NULL.
  1512. *
  1513. * Managed input devices do not need to be explicitly unregistered or
  1514. * freed as it will be done automatically when owner device unbinds from
  1515. * its driver (or binding fails). Once managed input device is allocated,
  1516. * it is ready to be set up and registered in the same fashion as regular
  1517. * input device. There are no special devm_input_device_[un]register()
  1518. * variants, regular ones work with both managed and unmanaged devices,
  1519. * should you need them. In most cases however, managed input device need
  1520. * not be explicitly unregistered or freed.
  1521. *
  1522. * NOTE: the owner device is set up as parent of input device and users
  1523. * should not override it.
  1524. */
  1525. struct input_dev *devm_input_allocate_device(struct device *dev)
  1526. {
  1527. struct input_dev *input;
  1528. struct input_devres *devres;
  1529. devres = devres_alloc(devm_input_device_release,
  1530. sizeof(*devres), GFP_KERNEL);
  1531. if (!devres)
  1532. return NULL;
  1533. input = input_allocate_device();
  1534. if (!input) {
  1535. devres_free(devres);
  1536. return NULL;
  1537. }
  1538. input->dev.parent = dev;
  1539. input->devres_managed = true;
  1540. devres->input = input;
  1541. devres_add(dev, devres);
  1542. return input;
  1543. }
  1544. EXPORT_SYMBOL(devm_input_allocate_device);
  1545. /**
  1546. * input_free_device - free memory occupied by input_dev structure
  1547. * @dev: input device to free
  1548. *
  1549. * This function should only be used if input_register_device()
  1550. * was not called yet or if it failed. Once device was registered
  1551. * use input_unregister_device() and memory will be freed once last
  1552. * reference to the device is dropped.
  1553. *
  1554. * Device should be allocated by input_allocate_device().
  1555. *
  1556. * NOTE: If there are references to the input device then memory
  1557. * will not be freed until last reference is dropped.
  1558. */
  1559. void input_free_device(struct input_dev *dev)
  1560. {
  1561. if (dev) {
  1562. if (dev->devres_managed)
  1563. WARN_ON(devres_destroy(dev->dev.parent,
  1564. devm_input_device_release,
  1565. devm_input_device_match,
  1566. dev));
  1567. input_put_device(dev);
  1568. }
  1569. }
  1570. EXPORT_SYMBOL(input_free_device);
  1571. /**
  1572. * input_set_capability - mark device as capable of a certain event
  1573. * @dev: device that is capable of emitting or accepting event
  1574. * @type: type of the event (EV_KEY, EV_REL, etc...)
  1575. * @code: event code
  1576. *
  1577. * In addition to setting up corresponding bit in appropriate capability
  1578. * bitmap the function also adjusts dev->evbit.
  1579. */
  1580. void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
  1581. {
  1582. switch (type) {
  1583. case EV_KEY:
  1584. __set_bit(code, dev->keybit);
  1585. break;
  1586. case EV_REL:
  1587. __set_bit(code, dev->relbit);
  1588. break;
  1589. case EV_ABS:
  1590. input_alloc_absinfo(dev);
  1591. if (!dev->absinfo)
  1592. return;
  1593. __set_bit(code, dev->absbit);
  1594. break;
  1595. case EV_MSC:
  1596. __set_bit(code, dev->mscbit);
  1597. break;
  1598. case EV_SW:
  1599. __set_bit(code, dev->swbit);
  1600. break;
  1601. case EV_LED:
  1602. __set_bit(code, dev->ledbit);
  1603. break;
  1604. case EV_SND:
  1605. __set_bit(code, dev->sndbit);
  1606. break;
  1607. case EV_FF:
  1608. __set_bit(code, dev->ffbit);
  1609. break;
  1610. case EV_PWR:
  1611. /* do nothing */
  1612. break;
  1613. default:
  1614. pr_err("%s: unknown type %u (code %u)\n", __func__, type, code);
  1615. dump_stack();
  1616. return;
  1617. }
  1618. __set_bit(type, dev->evbit);
  1619. }
  1620. EXPORT_SYMBOL(input_set_capability);
  1621. static unsigned int input_estimate_events_per_packet(struct input_dev *dev)
  1622. {
  1623. int mt_slots;
  1624. int i;
  1625. unsigned int events;
  1626. if (dev->mt) {
  1627. mt_slots = dev->mt->num_slots;
  1628. } else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) {
  1629. mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum -
  1630. dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1,
  1631. mt_slots = clamp(mt_slots, 2, 32);
  1632. } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
  1633. mt_slots = 2;
  1634. } else {
  1635. mt_slots = 0;
  1636. }
  1637. events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */
  1638. if (test_bit(EV_ABS, dev->evbit))
  1639. for_each_set_bit(i, dev->absbit, ABS_CNT)
  1640. events += input_is_mt_axis(i) ? mt_slots : 1;
  1641. if (test_bit(EV_REL, dev->evbit))
  1642. events += bitmap_weight(dev->relbit, REL_CNT);
  1643. /* Make room for KEY and MSC events */
  1644. events += 7;
  1645. return events;
  1646. }
  1647. #define INPUT_CLEANSE_BITMASK(dev, type, bits) \
  1648. do { \
  1649. if (!test_bit(EV_##type, dev->evbit)) \
  1650. memset(dev->bits##bit, 0, \
  1651. sizeof(dev->bits##bit)); \
  1652. } while (0)
  1653. static void input_cleanse_bitmasks(struct input_dev *dev)
  1654. {
  1655. INPUT_CLEANSE_BITMASK(dev, KEY, key);
  1656. INPUT_CLEANSE_BITMASK(dev, REL, rel);
  1657. INPUT_CLEANSE_BITMASK(dev, ABS, abs);
  1658. INPUT_CLEANSE_BITMASK(dev, MSC, msc);
  1659. INPUT_CLEANSE_BITMASK(dev, LED, led);
  1660. INPUT_CLEANSE_BITMASK(dev, SND, snd);
  1661. INPUT_CLEANSE_BITMASK(dev, FF, ff);
  1662. INPUT_CLEANSE_BITMASK(dev, SW, sw);
  1663. }
  1664. static void __input_unregister_device(struct input_dev *dev)
  1665. {
  1666. struct input_handle *handle, *next;
  1667. input_disconnect_device(dev);
  1668. mutex_lock(&input_mutex);
  1669. list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
  1670. handle->handler->disconnect(handle);
  1671. WARN_ON(!list_empty(&dev->h_list));
  1672. del_timer_sync(&dev->timer);
  1673. list_del_init(&dev->node);
  1674. input_wakeup_procfs_readers();
  1675. mutex_unlock(&input_mutex);
  1676. device_del(&dev->dev);
  1677. }
  1678. static void devm_input_device_unregister(struct device *dev, void *res)
  1679. {
  1680. struct input_devres *devres = res;
  1681. struct input_dev *input = devres->input;
  1682. dev_dbg(dev, "%s: unregistering device %s\n",
  1683. __func__, dev_name(&input->dev));
  1684. __input_unregister_device(input);
  1685. }
  1686. /**
  1687. * input_enable_softrepeat - enable software autorepeat
  1688. * @dev: input device
  1689. * @delay: repeat delay
  1690. * @period: repeat period
  1691. *
  1692. * Enable software autorepeat on the input device.
  1693. */
  1694. void input_enable_softrepeat(struct input_dev *dev, int delay, int period)
  1695. {
  1696. dev->timer.function = input_repeat_key;
  1697. dev->rep[REP_DELAY] = delay;
  1698. dev->rep[REP_PERIOD] = period;
  1699. }
  1700. EXPORT_SYMBOL(input_enable_softrepeat);
  1701. /**
  1702. * input_register_device - register device with input core
  1703. * @dev: device to be registered
  1704. *
  1705. * This function registers device with input core. The device must be
  1706. * allocated with input_allocate_device() and all it's capabilities
  1707. * set up before registering.
  1708. * If function fails the device must be freed with input_free_device().
  1709. * Once device has been successfully registered it can be unregistered
  1710. * with input_unregister_device(); input_free_device() should not be
  1711. * called in this case.
  1712. *
  1713. * Note that this function is also used to register managed input devices
  1714. * (ones allocated with devm_input_allocate_device()). Such managed input
  1715. * devices need not be explicitly unregistered or freed, their tear down
  1716. * is controlled by the devres infrastructure. It is also worth noting
  1717. * that tear down of managed input devices is internally a 2-step process:
  1718. * registered managed input device is first unregistered, but stays in
  1719. * memory and can still handle input_event() calls (although events will
  1720. * not be delivered anywhere). The freeing of managed input device will
  1721. * happen later, when devres stack is unwound to the point where device
  1722. * allocation was made.
  1723. */
  1724. int input_register_device(struct input_dev *dev)
  1725. {
  1726. struct input_devres *devres = NULL;
  1727. struct input_handler *handler;
  1728. unsigned int packet_size;
  1729. const char *path;
  1730. int error;
  1731. if (test_bit(EV_ABS, dev->evbit) && !dev->absinfo) {
  1732. dev_err(&dev->dev,
  1733. "Absolute device without dev->absinfo, refusing to register\n");
  1734. return -EINVAL;
  1735. }
  1736. if (dev->devres_managed) {
  1737. devres = devres_alloc(devm_input_device_unregister,
  1738. sizeof(*devres), GFP_KERNEL);
  1739. if (!devres)
  1740. return -ENOMEM;
  1741. devres->input = dev;
  1742. }
  1743. /* Every input device generates EV_SYN/SYN_REPORT events. */
  1744. __set_bit(EV_SYN, dev->evbit);
  1745. /* KEY_RESERVED is not supposed to be transmitted to userspace. */
  1746. __clear_bit(KEY_RESERVED, dev->keybit);
  1747. /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
  1748. input_cleanse_bitmasks(dev);
  1749. packet_size = input_estimate_events_per_packet(dev);
  1750. if (dev->hint_events_per_packet < packet_size)
  1751. dev->hint_events_per_packet = packet_size;
  1752. dev->max_vals = dev->hint_events_per_packet + 2;
  1753. dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
  1754. if (!dev->vals) {
  1755. error = -ENOMEM;
  1756. goto err_devres_free;
  1757. }
  1758. /*
  1759. * If delay and period are pre-set by the driver, then autorepeating
  1760. * is handled by the driver itself and we don't do it in input.c.
  1761. */
  1762. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD])
  1763. input_enable_softrepeat(dev, 250, 33);
  1764. if (!dev->getkeycode)
  1765. dev->getkeycode = input_default_getkeycode;
  1766. if (!dev->setkeycode)
  1767. dev->setkeycode = input_default_setkeycode;
  1768. error = device_add(&dev->dev);
  1769. if (error)
  1770. goto err_free_vals;
  1771. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1772. pr_info("%s as %s\n",
  1773. dev->name ? dev->name : "Unspecified device",
  1774. path ? path : "N/A");
  1775. kfree(path);
  1776. error = mutex_lock_interruptible(&input_mutex);
  1777. if (error)
  1778. goto err_device_del;
  1779. list_add_tail(&dev->node, &input_dev_list);
  1780. list_for_each_entry(handler, &input_handler_list, node)
  1781. input_attach_handler(dev, handler);
  1782. input_wakeup_procfs_readers();
  1783. mutex_unlock(&input_mutex);
  1784. if (dev->devres_managed) {
  1785. dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
  1786. __func__, dev_name(&dev->dev));
  1787. devres_add(dev->dev.parent, devres);
  1788. }
  1789. return 0;
  1790. err_device_del:
  1791. device_del(&dev->dev);
  1792. err_free_vals:
  1793. kfree(dev->vals);
  1794. dev->vals = NULL;
  1795. err_devres_free:
  1796. devres_free(devres);
  1797. return error;
  1798. }
  1799. EXPORT_SYMBOL(input_register_device);
  1800. /**
  1801. * input_unregister_device - unregister previously registered device
  1802. * @dev: device to be unregistered
  1803. *
  1804. * This function unregisters an input device. Once device is unregistered
  1805. * the caller should not try to access it as it may get freed at any moment.
  1806. */
  1807. void input_unregister_device(struct input_dev *dev)
  1808. {
  1809. if (dev->devres_managed) {
  1810. WARN_ON(devres_destroy(dev->dev.parent,
  1811. devm_input_device_unregister,
  1812. devm_input_device_match,
  1813. dev));
  1814. __input_unregister_device(dev);
  1815. /*
  1816. * We do not do input_put_device() here because it will be done
  1817. * when 2nd devres fires up.
  1818. */
  1819. } else {
  1820. __input_unregister_device(dev);
  1821. input_put_device(dev);
  1822. }
  1823. }
  1824. EXPORT_SYMBOL(input_unregister_device);
  1825. /**
  1826. * input_register_handler - register a new input handler
  1827. * @handler: handler to be registered
  1828. *
  1829. * This function registers a new input handler (interface) for input
  1830. * devices in the system and attaches it to all input devices that
  1831. * are compatible with the handler.
  1832. */
  1833. int input_register_handler(struct input_handler *handler)
  1834. {
  1835. struct input_dev *dev;
  1836. int error;
  1837. error = mutex_lock_interruptible(&input_mutex);
  1838. if (error)
  1839. return error;
  1840. INIT_LIST_HEAD(&handler->h_list);
  1841. list_add_tail(&handler->node, &input_handler_list);
  1842. list_for_each_entry(dev, &input_dev_list, node)
  1843. input_attach_handler(dev, handler);
  1844. input_wakeup_procfs_readers();
  1845. mutex_unlock(&input_mutex);
  1846. return 0;
  1847. }
  1848. EXPORT_SYMBOL(input_register_handler);
  1849. /**
  1850. * input_unregister_handler - unregisters an input handler
  1851. * @handler: handler to be unregistered
  1852. *
  1853. * This function disconnects a handler from its input devices and
  1854. * removes it from lists of known handlers.
  1855. */
  1856. void input_unregister_handler(struct input_handler *handler)
  1857. {
  1858. struct input_handle *handle, *next;
  1859. mutex_lock(&input_mutex);
  1860. list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
  1861. handler->disconnect(handle);
  1862. WARN_ON(!list_empty(&handler->h_list));
  1863. list_del_init(&handler->node);
  1864. input_wakeup_procfs_readers();
  1865. mutex_unlock(&input_mutex);
  1866. }
  1867. EXPORT_SYMBOL(input_unregister_handler);
  1868. /**
  1869. * input_handler_for_each_handle - handle iterator
  1870. * @handler: input handler to iterate
  1871. * @data: data for the callback
  1872. * @fn: function to be called for each handle
  1873. *
  1874. * Iterate over @bus's list of devices, and call @fn for each, passing
  1875. * it @data and stop when @fn returns a non-zero value. The function is
  1876. * using RCU to traverse the list and therefore may be using in atomic
  1877. * contexts. The @fn callback is invoked from RCU critical section and
  1878. * thus must not sleep.
  1879. */
  1880. int input_handler_for_each_handle(struct input_handler *handler, void *data,
  1881. int (*fn)(struct input_handle *, void *))
  1882. {
  1883. struct input_handle *handle;
  1884. int retval = 0;
  1885. rcu_read_lock();
  1886. list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
  1887. retval = fn(handle, data);
  1888. if (retval)
  1889. break;
  1890. }
  1891. rcu_read_unlock();
  1892. return retval;
  1893. }
  1894. EXPORT_SYMBOL(input_handler_for_each_handle);
  1895. /**
  1896. * input_register_handle - register a new input handle
  1897. * @handle: handle to register
  1898. *
  1899. * This function puts a new input handle onto device's
  1900. * and handler's lists so that events can flow through
  1901. * it once it is opened using input_open_device().
  1902. *
  1903. * This function is supposed to be called from handler's
  1904. * connect() method.
  1905. */
  1906. int input_register_handle(struct input_handle *handle)
  1907. {
  1908. struct input_handler *handler = handle->handler;
  1909. struct input_dev *dev = handle->dev;
  1910. int error;
  1911. /*
  1912. * We take dev->mutex here to prevent race with
  1913. * input_release_device().
  1914. */
  1915. error = mutex_lock_interruptible(&dev->mutex);
  1916. if (error)
  1917. return error;
  1918. /*
  1919. * Filters go to the head of the list, normal handlers
  1920. * to the tail.
  1921. */
  1922. if (handler->filter)
  1923. list_add_rcu(&handle->d_node, &dev->h_list);
  1924. else
  1925. list_add_tail_rcu(&handle->d_node, &dev->h_list);
  1926. mutex_unlock(&dev->mutex);
  1927. /*
  1928. * Since we are supposed to be called from ->connect()
  1929. * which is mutually exclusive with ->disconnect()
  1930. * we can't be racing with input_unregister_handle()
  1931. * and so separate lock is not needed here.
  1932. */
  1933. list_add_tail_rcu(&handle->h_node, &handler->h_list);
  1934. if (handler->start)
  1935. handler->start(handle);
  1936. return 0;
  1937. }
  1938. EXPORT_SYMBOL(input_register_handle);
  1939. /**
  1940. * input_unregister_handle - unregister an input handle
  1941. * @handle: handle to unregister
  1942. *
  1943. * This function removes input handle from device's
  1944. * and handler's lists.
  1945. *
  1946. * This function is supposed to be called from handler's
  1947. * disconnect() method.
  1948. */
  1949. void input_unregister_handle(struct input_handle *handle)
  1950. {
  1951. struct input_dev *dev = handle->dev;
  1952. list_del_rcu(&handle->h_node);
  1953. /*
  1954. * Take dev->mutex to prevent race with input_release_device().
  1955. */
  1956. mutex_lock(&dev->mutex);
  1957. list_del_rcu(&handle->d_node);
  1958. mutex_unlock(&dev->mutex);
  1959. synchronize_rcu();
  1960. }
  1961. EXPORT_SYMBOL(input_unregister_handle);
  1962. /**
  1963. * input_get_new_minor - allocates a new input minor number
  1964. * @legacy_base: beginning or the legacy range to be searched
  1965. * @legacy_num: size of legacy range
  1966. * @allow_dynamic: whether we can also take ID from the dynamic range
  1967. *
  1968. * This function allocates a new device minor for from input major namespace.
  1969. * Caller can request legacy minor by specifying @legacy_base and @legacy_num
  1970. * parameters and whether ID can be allocated from dynamic range if there are
  1971. * no free IDs in legacy range.
  1972. */
  1973. int input_get_new_minor(int legacy_base, unsigned int legacy_num,
  1974. bool allow_dynamic)
  1975. {
  1976. /*
  1977. * This function should be called from input handler's ->connect()
  1978. * methods, which are serialized with input_mutex, so no additional
  1979. * locking is needed here.
  1980. */
  1981. if (legacy_base >= 0) {
  1982. int minor = ida_simple_get(&input_ida,
  1983. legacy_base,
  1984. legacy_base + legacy_num,
  1985. GFP_KERNEL);
  1986. if (minor >= 0 || !allow_dynamic)
  1987. return minor;
  1988. }
  1989. return ida_simple_get(&input_ida,
  1990. INPUT_FIRST_DYNAMIC_DEV, INPUT_MAX_CHAR_DEVICES,
  1991. GFP_KERNEL);
  1992. }
  1993. EXPORT_SYMBOL(input_get_new_minor);
  1994. /**
  1995. * input_free_minor - release previously allocated minor
  1996. * @minor: minor to be released
  1997. *
  1998. * This function releases previously allocated input minor so that it can be
  1999. * reused later.
  2000. */
  2001. void input_free_minor(unsigned int minor)
  2002. {
  2003. ida_simple_remove(&input_ida, minor);
  2004. }
  2005. EXPORT_SYMBOL(input_free_minor);
  2006. static int __init input_init(void)
  2007. {
  2008. int err;
  2009. err = class_register(&input_class);
  2010. if (err) {
  2011. pr_err("unable to register input_dev class\n");
  2012. return err;
  2013. }
  2014. err = input_proc_init();
  2015. if (err)
  2016. goto fail1;
  2017. err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),
  2018. INPUT_MAX_CHAR_DEVICES, "input");
  2019. if (err) {
  2020. pr_err("unable to register char major %d", INPUT_MAJOR);
  2021. goto fail2;
  2022. }
  2023. return 0;
  2024. fail2: input_proc_exit();
  2025. fail1: class_unregister(&input_class);
  2026. return err;
  2027. }
  2028. static void __exit input_exit(void)
  2029. {
  2030. input_proc_exit();
  2031. unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0),
  2032. INPUT_MAX_CHAR_DEVICES);
  2033. class_unregister(&input_class);
  2034. }
  2035. subsys_initcall(input_init);
  2036. module_exit(input_exit);