input.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436
  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. 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. add_input_randomness(vals->type, vals->code, vals->value);
  131. /* trigger auto repeat for key events */
  132. if (test_bit(EV_REP, dev->evbit) && test_bit(EV_KEY, dev->evbit)) {
  133. for (v = vals; v != vals + count; v++) {
  134. if (v->type == EV_KEY && v->value != 2) {
  135. if (v->value)
  136. input_start_autorepeat(dev, v->code);
  137. else
  138. input_stop_autorepeat(dev);
  139. }
  140. }
  141. }
  142. }
  143. static void input_pass_event(struct input_dev *dev,
  144. unsigned int type, unsigned int code, int value)
  145. {
  146. struct input_value vals[] = { { type, code, value } };
  147. input_pass_values(dev, vals, ARRAY_SIZE(vals));
  148. }
  149. /*
  150. * Generate software autorepeat event. Note that we take
  151. * dev->event_lock here to avoid racing with input_event
  152. * which may cause keys get "stuck".
  153. */
  154. static void input_repeat_key(unsigned long data)
  155. {
  156. struct input_dev *dev = (void *) data;
  157. unsigned long flags;
  158. spin_lock_irqsave(&dev->event_lock, flags);
  159. if (test_bit(dev->repeat_key, dev->key) &&
  160. is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
  161. struct input_value vals[] = {
  162. { EV_KEY, dev->repeat_key, 2 },
  163. input_value_sync
  164. };
  165. input_pass_values(dev, vals, ARRAY_SIZE(vals));
  166. if (dev->rep[REP_PERIOD])
  167. mod_timer(&dev->timer, jiffies +
  168. msecs_to_jiffies(dev->rep[REP_PERIOD]));
  169. }
  170. spin_unlock_irqrestore(&dev->event_lock, flags);
  171. }
  172. #define INPUT_IGNORE_EVENT 0
  173. #define INPUT_PASS_TO_HANDLERS 1
  174. #define INPUT_PASS_TO_DEVICE 2
  175. #define INPUT_SLOT 4
  176. #define INPUT_FLUSH 8
  177. #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
  178. static int input_handle_abs_event(struct input_dev *dev,
  179. unsigned int code, int *pval)
  180. {
  181. struct input_mt *mt = dev->mt;
  182. bool is_mt_event;
  183. int *pold;
  184. if (code == ABS_MT_SLOT) {
  185. /*
  186. * "Stage" the event; we'll flush it later, when we
  187. * get actual touch data.
  188. */
  189. if (mt && *pval >= 0 && *pval < mt->num_slots)
  190. mt->slot = *pval;
  191. return INPUT_IGNORE_EVENT;
  192. }
  193. is_mt_event = input_is_mt_value(code);
  194. if (!is_mt_event) {
  195. pold = &dev->absinfo[code].value;
  196. } else if (mt) {
  197. pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST];
  198. } else {
  199. /*
  200. * Bypass filtering for multi-touch events when
  201. * not employing slots.
  202. */
  203. pold = NULL;
  204. }
  205. if (pold) {
  206. *pval = input_defuzz_abs_event(*pval, *pold,
  207. dev->absinfo[code].fuzz);
  208. if (*pold == *pval)
  209. return INPUT_IGNORE_EVENT;
  210. *pold = *pval;
  211. }
  212. /* Flush pending "slot" event */
  213. if (is_mt_event && mt && mt->slot != input_abs_get_val(dev, ABS_MT_SLOT)) {
  214. input_abs_set_val(dev, ABS_MT_SLOT, mt->slot);
  215. return INPUT_PASS_TO_HANDLERS | INPUT_SLOT;
  216. }
  217. return INPUT_PASS_TO_HANDLERS;
  218. }
  219. static int input_get_disposition(struct input_dev *dev,
  220. unsigned int type, unsigned int code, int *pval)
  221. {
  222. int disposition = INPUT_IGNORE_EVENT;
  223. int value = *pval;
  224. switch (type) {
  225. case EV_SYN:
  226. switch (code) {
  227. case SYN_CONFIG:
  228. disposition = INPUT_PASS_TO_ALL;
  229. break;
  230. case SYN_REPORT:
  231. disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;
  232. break;
  233. case SYN_MT_REPORT:
  234. disposition = INPUT_PASS_TO_HANDLERS;
  235. break;
  236. }
  237. break;
  238. case EV_KEY:
  239. if (is_event_supported(code, dev->keybit, KEY_MAX)) {
  240. /* auto-repeat bypasses state updates */
  241. if (value == 2) {
  242. disposition = INPUT_PASS_TO_HANDLERS;
  243. break;
  244. }
  245. if (!!test_bit(code, dev->key) != !!value) {
  246. __change_bit(code, dev->key);
  247. disposition = INPUT_PASS_TO_HANDLERS;
  248. }
  249. }
  250. break;
  251. case EV_SW:
  252. if (is_event_supported(code, dev->swbit, SW_MAX) &&
  253. !!test_bit(code, dev->sw) != !!value) {
  254. __change_bit(code, dev->sw);
  255. disposition = INPUT_PASS_TO_HANDLERS;
  256. }
  257. break;
  258. case EV_ABS:
  259. if (is_event_supported(code, dev->absbit, ABS_MAX))
  260. disposition = input_handle_abs_event(dev, code, &value);
  261. break;
  262. case EV_REL:
  263. if (is_event_supported(code, dev->relbit, REL_MAX) && value)
  264. disposition = INPUT_PASS_TO_HANDLERS;
  265. break;
  266. case EV_MSC:
  267. if (is_event_supported(code, dev->mscbit, MSC_MAX))
  268. disposition = INPUT_PASS_TO_ALL;
  269. break;
  270. case EV_LED:
  271. if (is_event_supported(code, dev->ledbit, LED_MAX) &&
  272. !!test_bit(code, dev->led) != !!value) {
  273. __change_bit(code, dev->led);
  274. disposition = INPUT_PASS_TO_ALL;
  275. }
  276. break;
  277. case EV_SND:
  278. if (is_event_supported(code, dev->sndbit, SND_MAX)) {
  279. if (!!test_bit(code, dev->snd) != !!value)
  280. __change_bit(code, dev->snd);
  281. disposition = INPUT_PASS_TO_ALL;
  282. }
  283. break;
  284. case EV_REP:
  285. if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
  286. dev->rep[code] = value;
  287. disposition = INPUT_PASS_TO_ALL;
  288. }
  289. break;
  290. case EV_FF:
  291. if (value >= 0)
  292. disposition = INPUT_PASS_TO_ALL;
  293. break;
  294. case EV_PWR:
  295. disposition = INPUT_PASS_TO_ALL;
  296. break;
  297. }
  298. *pval = value;
  299. return disposition;
  300. }
  301. static void input_handle_event(struct input_dev *dev,
  302. unsigned int type, unsigned int code, int value)
  303. {
  304. int disposition;
  305. disposition = input_get_disposition(dev, 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. dev->absinfo = kcalloc(ABS_CNT, sizeof(struct input_absinfo),
  400. GFP_KERNEL);
  401. WARN(!dev->absinfo, "%s(): kcalloc() failed?\n", __func__);
  402. }
  403. EXPORT_SYMBOL(input_alloc_absinfo);
  404. void input_set_abs_params(struct input_dev *dev, unsigned int axis,
  405. int min, int max, int fuzz, int flat)
  406. {
  407. struct input_absinfo *absinfo;
  408. input_alloc_absinfo(dev);
  409. if (!dev->absinfo)
  410. return;
  411. absinfo = &dev->absinfo[axis];
  412. absinfo->minimum = min;
  413. absinfo->maximum = max;
  414. absinfo->fuzz = fuzz;
  415. absinfo->flat = flat;
  416. __set_bit(EV_ABS, dev->evbit);
  417. __set_bit(axis, dev->absbit);
  418. }
  419. EXPORT_SYMBOL(input_set_abs_params);
  420. /**
  421. * input_grab_device - grabs device for exclusive use
  422. * @handle: input handle that wants to own the device
  423. *
  424. * When a device is grabbed by an input handle all events generated by
  425. * the device are delivered only to this handle. Also events injected
  426. * by other input handles are ignored while device is grabbed.
  427. */
  428. int input_grab_device(struct input_handle *handle)
  429. {
  430. struct input_dev *dev = handle->dev;
  431. int retval;
  432. retval = mutex_lock_interruptible(&dev->mutex);
  433. if (retval)
  434. return retval;
  435. if (dev->grab) {
  436. retval = -EBUSY;
  437. goto out;
  438. }
  439. rcu_assign_pointer(dev->grab, handle);
  440. out:
  441. mutex_unlock(&dev->mutex);
  442. return retval;
  443. }
  444. EXPORT_SYMBOL(input_grab_device);
  445. static void __input_release_device(struct input_handle *handle)
  446. {
  447. struct input_dev *dev = handle->dev;
  448. struct input_handle *grabber;
  449. grabber = rcu_dereference_protected(dev->grab,
  450. lockdep_is_held(&dev->mutex));
  451. if (grabber == handle) {
  452. rcu_assign_pointer(dev->grab, NULL);
  453. /* Make sure input_pass_event() notices that grab is gone */
  454. synchronize_rcu();
  455. list_for_each_entry(handle, &dev->h_list, d_node)
  456. if (handle->open && handle->handler->start)
  457. handle->handler->start(handle);
  458. }
  459. }
  460. /**
  461. * input_release_device - release previously grabbed device
  462. * @handle: input handle that owns the device
  463. *
  464. * Releases previously grabbed device so that other input handles can
  465. * start receiving input events. Upon release all handlers attached
  466. * to the device have their start() method called so they have a change
  467. * to synchronize device state with the rest of the system.
  468. */
  469. void input_release_device(struct input_handle *handle)
  470. {
  471. struct input_dev *dev = handle->dev;
  472. mutex_lock(&dev->mutex);
  473. __input_release_device(handle);
  474. mutex_unlock(&dev->mutex);
  475. }
  476. EXPORT_SYMBOL(input_release_device);
  477. /**
  478. * input_open_device - open input device
  479. * @handle: handle through which device is being accessed
  480. *
  481. * This function should be called by input handlers when they
  482. * want to start receive events from given input device.
  483. */
  484. int input_open_device(struct input_handle *handle)
  485. {
  486. struct input_dev *dev = handle->dev;
  487. int retval;
  488. retval = mutex_lock_interruptible(&dev->mutex);
  489. if (retval)
  490. return retval;
  491. if (dev->going_away) {
  492. retval = -ENODEV;
  493. goto out;
  494. }
  495. handle->open++;
  496. if (!dev->users++ && dev->open)
  497. retval = dev->open(dev);
  498. if (retval) {
  499. dev->users--;
  500. if (!--handle->open) {
  501. /*
  502. * Make sure we are not delivering any more events
  503. * through this handle
  504. */
  505. synchronize_rcu();
  506. }
  507. }
  508. out:
  509. mutex_unlock(&dev->mutex);
  510. return retval;
  511. }
  512. EXPORT_SYMBOL(input_open_device);
  513. int input_flush_device(struct input_handle *handle, struct file *file)
  514. {
  515. struct input_dev *dev = handle->dev;
  516. int retval;
  517. retval = mutex_lock_interruptible(&dev->mutex);
  518. if (retval)
  519. return retval;
  520. if (dev->flush)
  521. retval = dev->flush(dev, file);
  522. mutex_unlock(&dev->mutex);
  523. return retval;
  524. }
  525. EXPORT_SYMBOL(input_flush_device);
  526. /**
  527. * input_close_device - close input device
  528. * @handle: handle through which device is being accessed
  529. *
  530. * This function should be called by input handlers when they
  531. * want to stop receive events from given input device.
  532. */
  533. void input_close_device(struct input_handle *handle)
  534. {
  535. struct input_dev *dev = handle->dev;
  536. mutex_lock(&dev->mutex);
  537. __input_release_device(handle);
  538. if (!--dev->users && dev->close)
  539. dev->close(dev);
  540. if (!--handle->open) {
  541. /*
  542. * synchronize_rcu() makes sure that input_pass_event()
  543. * completed and that no more input events are delivered
  544. * through this handle
  545. */
  546. synchronize_rcu();
  547. }
  548. mutex_unlock(&dev->mutex);
  549. }
  550. EXPORT_SYMBOL(input_close_device);
  551. /*
  552. * Simulate keyup events for all keys that are marked as pressed.
  553. * The function must be called with dev->event_lock held.
  554. */
  555. static void input_dev_release_keys(struct input_dev *dev)
  556. {
  557. int code;
  558. if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
  559. for_each_set_bit(code, dev->key, KEY_CNT)
  560. input_pass_event(dev, EV_KEY, code, 0);
  561. memset(dev->key, 0, sizeof(dev->key));
  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_each_set_bit(i, dev->bits##bit, type##_CNT) { \
  1333. active = test_bit(i, dev->bits); \
  1334. if (!active && !on) \
  1335. continue; \
  1336. \
  1337. dev->event(dev, EV_##type, i, on ? active : 0); \
  1338. } \
  1339. } while (0)
  1340. static void input_dev_toggle(struct input_dev *dev, bool activate)
  1341. {
  1342. if (!dev->event)
  1343. return;
  1344. INPUT_DO_TOGGLE(dev, LED, led, activate);
  1345. INPUT_DO_TOGGLE(dev, SND, snd, activate);
  1346. if (activate && test_bit(EV_REP, dev->evbit)) {
  1347. dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
  1348. dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
  1349. }
  1350. }
  1351. /**
  1352. * input_reset_device() - reset/restore the state of input device
  1353. * @dev: input device whose state needs to be reset
  1354. *
  1355. * This function tries to reset the state of an opened input device and
  1356. * bring internal state and state if the hardware in sync with each other.
  1357. * We mark all keys as released, restore LED state, repeat rate, etc.
  1358. */
  1359. void input_reset_device(struct input_dev *dev)
  1360. {
  1361. unsigned long flags;
  1362. mutex_lock(&dev->mutex);
  1363. spin_lock_irqsave(&dev->event_lock, flags);
  1364. input_dev_toggle(dev, true);
  1365. input_dev_release_keys(dev);
  1366. spin_unlock_irqrestore(&dev->event_lock, flags);
  1367. mutex_unlock(&dev->mutex);
  1368. }
  1369. EXPORT_SYMBOL(input_reset_device);
  1370. #ifdef CONFIG_PM_SLEEP
  1371. static int input_dev_suspend(struct device *dev)
  1372. {
  1373. struct input_dev *input_dev = to_input_dev(dev);
  1374. spin_lock_irq(&input_dev->event_lock);
  1375. /*
  1376. * Keys that are pressed now are unlikely to be
  1377. * still pressed when we resume.
  1378. */
  1379. input_dev_release_keys(input_dev);
  1380. /* Turn off LEDs and sounds, if any are active. */
  1381. input_dev_toggle(input_dev, false);
  1382. spin_unlock_irq(&input_dev->event_lock);
  1383. return 0;
  1384. }
  1385. static int input_dev_resume(struct device *dev)
  1386. {
  1387. struct input_dev *input_dev = to_input_dev(dev);
  1388. spin_lock_irq(&input_dev->event_lock);
  1389. /* Restore state of LEDs and sounds, if any were active. */
  1390. input_dev_toggle(input_dev, true);
  1391. spin_unlock_irq(&input_dev->event_lock);
  1392. return 0;
  1393. }
  1394. static int input_dev_freeze(struct device *dev)
  1395. {
  1396. struct input_dev *input_dev = to_input_dev(dev);
  1397. spin_lock_irq(&input_dev->event_lock);
  1398. /*
  1399. * Keys that are pressed now are unlikely to be
  1400. * still pressed when we resume.
  1401. */
  1402. input_dev_release_keys(input_dev);
  1403. spin_unlock_irq(&input_dev->event_lock);
  1404. return 0;
  1405. }
  1406. static int input_dev_poweroff(struct device *dev)
  1407. {
  1408. struct input_dev *input_dev = to_input_dev(dev);
  1409. spin_lock_irq(&input_dev->event_lock);
  1410. /* Turn off LEDs and sounds, if any are active. */
  1411. input_dev_toggle(input_dev, false);
  1412. spin_unlock_irq(&input_dev->event_lock);
  1413. return 0;
  1414. }
  1415. static const struct dev_pm_ops input_dev_pm_ops = {
  1416. .suspend = input_dev_suspend,
  1417. .resume = input_dev_resume,
  1418. .freeze = input_dev_freeze,
  1419. .poweroff = input_dev_poweroff,
  1420. .restore = input_dev_resume,
  1421. };
  1422. #endif /* CONFIG_PM */
  1423. static struct device_type input_dev_type = {
  1424. .groups = input_dev_attr_groups,
  1425. .release = input_dev_release,
  1426. .uevent = input_dev_uevent,
  1427. #ifdef CONFIG_PM_SLEEP
  1428. .pm = &input_dev_pm_ops,
  1429. #endif
  1430. };
  1431. static char *input_devnode(struct device *dev, umode_t *mode)
  1432. {
  1433. return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
  1434. }
  1435. struct class input_class = {
  1436. .name = "input",
  1437. .devnode = input_devnode,
  1438. };
  1439. EXPORT_SYMBOL_GPL(input_class);
  1440. /**
  1441. * input_allocate_device - allocate memory for new input device
  1442. *
  1443. * Returns prepared struct input_dev or %NULL.
  1444. *
  1445. * NOTE: Use input_free_device() to free devices that have not been
  1446. * registered; input_unregister_device() should be used for already
  1447. * registered devices.
  1448. */
  1449. struct input_dev *input_allocate_device(void)
  1450. {
  1451. static atomic_t input_no = ATOMIC_INIT(-1);
  1452. struct input_dev *dev;
  1453. dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  1454. if (dev) {
  1455. dev->dev.type = &input_dev_type;
  1456. dev->dev.class = &input_class;
  1457. device_initialize(&dev->dev);
  1458. mutex_init(&dev->mutex);
  1459. spin_lock_init(&dev->event_lock);
  1460. init_timer(&dev->timer);
  1461. INIT_LIST_HEAD(&dev->h_list);
  1462. INIT_LIST_HEAD(&dev->node);
  1463. dev_set_name(&dev->dev, "input%lu",
  1464. (unsigned long)atomic_inc_return(&input_no));
  1465. __module_get(THIS_MODULE);
  1466. }
  1467. return dev;
  1468. }
  1469. EXPORT_SYMBOL(input_allocate_device);
  1470. struct input_devres {
  1471. struct input_dev *input;
  1472. };
  1473. static int devm_input_device_match(struct device *dev, void *res, void *data)
  1474. {
  1475. struct input_devres *devres = res;
  1476. return devres->input == data;
  1477. }
  1478. static void devm_input_device_release(struct device *dev, void *res)
  1479. {
  1480. struct input_devres *devres = res;
  1481. struct input_dev *input = devres->input;
  1482. dev_dbg(dev, "%s: dropping reference to %s\n",
  1483. __func__, dev_name(&input->dev));
  1484. input_put_device(input);
  1485. }
  1486. /**
  1487. * devm_input_allocate_device - allocate managed input device
  1488. * @dev: device owning the input device being created
  1489. *
  1490. * Returns prepared struct input_dev or %NULL.
  1491. *
  1492. * Managed input devices do not need to be explicitly unregistered or
  1493. * freed as it will be done automatically when owner device unbinds from
  1494. * its driver (or binding fails). Once managed input device is allocated,
  1495. * it is ready to be set up and registered in the same fashion as regular
  1496. * input device. There are no special devm_input_device_[un]register()
  1497. * variants, regular ones work with both managed and unmanaged devices,
  1498. * should you need them. In most cases however, managed input device need
  1499. * not be explicitly unregistered or freed.
  1500. *
  1501. * NOTE: the owner device is set up as parent of input device and users
  1502. * should not override it.
  1503. */
  1504. struct input_dev *devm_input_allocate_device(struct device *dev)
  1505. {
  1506. struct input_dev *input;
  1507. struct input_devres *devres;
  1508. devres = devres_alloc(devm_input_device_release,
  1509. sizeof(struct input_devres), GFP_KERNEL);
  1510. if (!devres)
  1511. return NULL;
  1512. input = input_allocate_device();
  1513. if (!input) {
  1514. devres_free(devres);
  1515. return NULL;
  1516. }
  1517. input->dev.parent = dev;
  1518. input->devres_managed = true;
  1519. devres->input = input;
  1520. devres_add(dev, devres);
  1521. return input;
  1522. }
  1523. EXPORT_SYMBOL(devm_input_allocate_device);
  1524. /**
  1525. * input_free_device - free memory occupied by input_dev structure
  1526. * @dev: input device to free
  1527. *
  1528. * This function should only be used if input_register_device()
  1529. * was not called yet or if it failed. Once device was registered
  1530. * use input_unregister_device() and memory will be freed once last
  1531. * reference to the device is dropped.
  1532. *
  1533. * Device should be allocated by input_allocate_device().
  1534. *
  1535. * NOTE: If there are references to the input device then memory
  1536. * will not be freed until last reference is dropped.
  1537. */
  1538. void input_free_device(struct input_dev *dev)
  1539. {
  1540. if (dev) {
  1541. if (dev->devres_managed)
  1542. WARN_ON(devres_destroy(dev->dev.parent,
  1543. devm_input_device_release,
  1544. devm_input_device_match,
  1545. dev));
  1546. input_put_device(dev);
  1547. }
  1548. }
  1549. EXPORT_SYMBOL(input_free_device);
  1550. /**
  1551. * input_set_capability - mark device as capable of a certain event
  1552. * @dev: device that is capable of emitting or accepting event
  1553. * @type: type of the event (EV_KEY, EV_REL, etc...)
  1554. * @code: event code
  1555. *
  1556. * In addition to setting up corresponding bit in appropriate capability
  1557. * bitmap the function also adjusts dev->evbit.
  1558. */
  1559. void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
  1560. {
  1561. switch (type) {
  1562. case EV_KEY:
  1563. __set_bit(code, dev->keybit);
  1564. break;
  1565. case EV_REL:
  1566. __set_bit(code, dev->relbit);
  1567. break;
  1568. case EV_ABS:
  1569. input_alloc_absinfo(dev);
  1570. if (!dev->absinfo)
  1571. return;
  1572. __set_bit(code, dev->absbit);
  1573. break;
  1574. case EV_MSC:
  1575. __set_bit(code, dev->mscbit);
  1576. break;
  1577. case EV_SW:
  1578. __set_bit(code, dev->swbit);
  1579. break;
  1580. case EV_LED:
  1581. __set_bit(code, dev->ledbit);
  1582. break;
  1583. case EV_SND:
  1584. __set_bit(code, dev->sndbit);
  1585. break;
  1586. case EV_FF:
  1587. __set_bit(code, dev->ffbit);
  1588. break;
  1589. case EV_PWR:
  1590. /* do nothing */
  1591. break;
  1592. default:
  1593. pr_err("input_set_capability: unknown type %u (code %u)\n",
  1594. type, code);
  1595. dump_stack();
  1596. return;
  1597. }
  1598. __set_bit(type, dev->evbit);
  1599. }
  1600. EXPORT_SYMBOL(input_set_capability);
  1601. static unsigned int input_estimate_events_per_packet(struct input_dev *dev)
  1602. {
  1603. int mt_slots;
  1604. int i;
  1605. unsigned int events;
  1606. if (dev->mt) {
  1607. mt_slots = dev->mt->num_slots;
  1608. } else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) {
  1609. mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum -
  1610. dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1,
  1611. mt_slots = clamp(mt_slots, 2, 32);
  1612. } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
  1613. mt_slots = 2;
  1614. } else {
  1615. mt_slots = 0;
  1616. }
  1617. events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */
  1618. if (test_bit(EV_ABS, dev->evbit))
  1619. for_each_set_bit(i, dev->absbit, ABS_CNT)
  1620. events += input_is_mt_axis(i) ? mt_slots : 1;
  1621. if (test_bit(EV_REL, dev->evbit))
  1622. events += bitmap_weight(dev->relbit, REL_CNT);
  1623. /* Make room for KEY and MSC events */
  1624. events += 7;
  1625. return events;
  1626. }
  1627. #define INPUT_CLEANSE_BITMASK(dev, type, bits) \
  1628. do { \
  1629. if (!test_bit(EV_##type, dev->evbit)) \
  1630. memset(dev->bits##bit, 0, \
  1631. sizeof(dev->bits##bit)); \
  1632. } while (0)
  1633. static void input_cleanse_bitmasks(struct input_dev *dev)
  1634. {
  1635. INPUT_CLEANSE_BITMASK(dev, KEY, key);
  1636. INPUT_CLEANSE_BITMASK(dev, REL, rel);
  1637. INPUT_CLEANSE_BITMASK(dev, ABS, abs);
  1638. INPUT_CLEANSE_BITMASK(dev, MSC, msc);
  1639. INPUT_CLEANSE_BITMASK(dev, LED, led);
  1640. INPUT_CLEANSE_BITMASK(dev, SND, snd);
  1641. INPUT_CLEANSE_BITMASK(dev, FF, ff);
  1642. INPUT_CLEANSE_BITMASK(dev, SW, sw);
  1643. }
  1644. static void __input_unregister_device(struct input_dev *dev)
  1645. {
  1646. struct input_handle *handle, *next;
  1647. input_disconnect_device(dev);
  1648. mutex_lock(&input_mutex);
  1649. list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
  1650. handle->handler->disconnect(handle);
  1651. WARN_ON(!list_empty(&dev->h_list));
  1652. del_timer_sync(&dev->timer);
  1653. list_del_init(&dev->node);
  1654. input_wakeup_procfs_readers();
  1655. mutex_unlock(&input_mutex);
  1656. device_del(&dev->dev);
  1657. }
  1658. static void devm_input_device_unregister(struct device *dev, void *res)
  1659. {
  1660. struct input_devres *devres = res;
  1661. struct input_dev *input = devres->input;
  1662. dev_dbg(dev, "%s: unregistering device %s\n",
  1663. __func__, dev_name(&input->dev));
  1664. __input_unregister_device(input);
  1665. }
  1666. /**
  1667. * input_register_device - register device with input core
  1668. * @dev: device to be registered
  1669. *
  1670. * This function registers device with input core. The device must be
  1671. * allocated with input_allocate_device() and all it's capabilities
  1672. * set up before registering.
  1673. * If function fails the device must be freed with input_free_device().
  1674. * Once device has been successfully registered it can be unregistered
  1675. * with input_unregister_device(); input_free_device() should not be
  1676. * called in this case.
  1677. *
  1678. * Note that this function is also used to register managed input devices
  1679. * (ones allocated with devm_input_allocate_device()). Such managed input
  1680. * devices need not be explicitly unregistered or freed, their tear down
  1681. * is controlled by the devres infrastructure. It is also worth noting
  1682. * that tear down of managed input devices is internally a 2-step process:
  1683. * registered managed input device is first unregistered, but stays in
  1684. * memory and can still handle input_event() calls (although events will
  1685. * not be delivered anywhere). The freeing of managed input device will
  1686. * happen later, when devres stack is unwound to the point where device
  1687. * allocation was made.
  1688. */
  1689. int input_register_device(struct input_dev *dev)
  1690. {
  1691. struct input_devres *devres = NULL;
  1692. struct input_handler *handler;
  1693. unsigned int packet_size;
  1694. const char *path;
  1695. int error;
  1696. if (dev->devres_managed) {
  1697. devres = devres_alloc(devm_input_device_unregister,
  1698. sizeof(struct input_devres), GFP_KERNEL);
  1699. if (!devres)
  1700. return -ENOMEM;
  1701. devres->input = dev;
  1702. }
  1703. /* Every input device generates EV_SYN/SYN_REPORT events. */
  1704. __set_bit(EV_SYN, dev->evbit);
  1705. /* KEY_RESERVED is not supposed to be transmitted to userspace. */
  1706. __clear_bit(KEY_RESERVED, dev->keybit);
  1707. /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
  1708. input_cleanse_bitmasks(dev);
  1709. packet_size = input_estimate_events_per_packet(dev);
  1710. if (dev->hint_events_per_packet < packet_size)
  1711. dev->hint_events_per_packet = packet_size;
  1712. dev->max_vals = dev->hint_events_per_packet + 2;
  1713. dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
  1714. if (!dev->vals) {
  1715. error = -ENOMEM;
  1716. goto err_devres_free;
  1717. }
  1718. /*
  1719. * If delay and period are pre-set by the driver, then autorepeating
  1720. * is handled by the driver itself and we don't do it in input.c.
  1721. */
  1722. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  1723. dev->timer.data = (long) dev;
  1724. dev->timer.function = input_repeat_key;
  1725. dev->rep[REP_DELAY] = 250;
  1726. dev->rep[REP_PERIOD] = 33;
  1727. }
  1728. if (!dev->getkeycode)
  1729. dev->getkeycode = input_default_getkeycode;
  1730. if (!dev->setkeycode)
  1731. dev->setkeycode = input_default_setkeycode;
  1732. error = device_add(&dev->dev);
  1733. if (error)
  1734. goto err_free_vals;
  1735. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  1736. pr_info("%s as %s\n",
  1737. dev->name ? dev->name : "Unspecified device",
  1738. path ? path : "N/A");
  1739. kfree(path);
  1740. error = mutex_lock_interruptible(&input_mutex);
  1741. if (error)
  1742. goto err_device_del;
  1743. list_add_tail(&dev->node, &input_dev_list);
  1744. list_for_each_entry(handler, &input_handler_list, node)
  1745. input_attach_handler(dev, handler);
  1746. input_wakeup_procfs_readers();
  1747. mutex_unlock(&input_mutex);
  1748. if (dev->devres_managed) {
  1749. dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
  1750. __func__, dev_name(&dev->dev));
  1751. devres_add(dev->dev.parent, devres);
  1752. }
  1753. return 0;
  1754. err_device_del:
  1755. device_del(&dev->dev);
  1756. err_free_vals:
  1757. kfree(dev->vals);
  1758. dev->vals = NULL;
  1759. err_devres_free:
  1760. devres_free(devres);
  1761. return error;
  1762. }
  1763. EXPORT_SYMBOL(input_register_device);
  1764. /**
  1765. * input_unregister_device - unregister previously registered device
  1766. * @dev: device to be unregistered
  1767. *
  1768. * This function unregisters an input device. Once device is unregistered
  1769. * the caller should not try to access it as it may get freed at any moment.
  1770. */
  1771. void input_unregister_device(struct input_dev *dev)
  1772. {
  1773. if (dev->devres_managed) {
  1774. WARN_ON(devres_destroy(dev->dev.parent,
  1775. devm_input_device_unregister,
  1776. devm_input_device_match,
  1777. dev));
  1778. __input_unregister_device(dev);
  1779. /*
  1780. * We do not do input_put_device() here because it will be done
  1781. * when 2nd devres fires up.
  1782. */
  1783. } else {
  1784. __input_unregister_device(dev);
  1785. input_put_device(dev);
  1786. }
  1787. }
  1788. EXPORT_SYMBOL(input_unregister_device);
  1789. /**
  1790. * input_register_handler - register a new input handler
  1791. * @handler: handler to be registered
  1792. *
  1793. * This function registers a new input handler (interface) for input
  1794. * devices in the system and attaches it to all input devices that
  1795. * are compatible with the handler.
  1796. */
  1797. int input_register_handler(struct input_handler *handler)
  1798. {
  1799. struct input_dev *dev;
  1800. int error;
  1801. error = mutex_lock_interruptible(&input_mutex);
  1802. if (error)
  1803. return error;
  1804. INIT_LIST_HEAD(&handler->h_list);
  1805. list_add_tail(&handler->node, &input_handler_list);
  1806. list_for_each_entry(dev, &input_dev_list, node)
  1807. input_attach_handler(dev, handler);
  1808. input_wakeup_procfs_readers();
  1809. mutex_unlock(&input_mutex);
  1810. return 0;
  1811. }
  1812. EXPORT_SYMBOL(input_register_handler);
  1813. /**
  1814. * input_unregister_handler - unregisters an input handler
  1815. * @handler: handler to be unregistered
  1816. *
  1817. * This function disconnects a handler from its input devices and
  1818. * removes it from lists of known handlers.
  1819. */
  1820. void input_unregister_handler(struct input_handler *handler)
  1821. {
  1822. struct input_handle *handle, *next;
  1823. mutex_lock(&input_mutex);
  1824. list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
  1825. handler->disconnect(handle);
  1826. WARN_ON(!list_empty(&handler->h_list));
  1827. list_del_init(&handler->node);
  1828. input_wakeup_procfs_readers();
  1829. mutex_unlock(&input_mutex);
  1830. }
  1831. EXPORT_SYMBOL(input_unregister_handler);
  1832. /**
  1833. * input_handler_for_each_handle - handle iterator
  1834. * @handler: input handler to iterate
  1835. * @data: data for the callback
  1836. * @fn: function to be called for each handle
  1837. *
  1838. * Iterate over @bus's list of devices, and call @fn for each, passing
  1839. * it @data and stop when @fn returns a non-zero value. The function is
  1840. * using RCU to traverse the list and therefore may be using in atomic
  1841. * contexts. The @fn callback is invoked from RCU critical section and
  1842. * thus must not sleep.
  1843. */
  1844. int input_handler_for_each_handle(struct input_handler *handler, void *data,
  1845. int (*fn)(struct input_handle *, void *))
  1846. {
  1847. struct input_handle *handle;
  1848. int retval = 0;
  1849. rcu_read_lock();
  1850. list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
  1851. retval = fn(handle, data);
  1852. if (retval)
  1853. break;
  1854. }
  1855. rcu_read_unlock();
  1856. return retval;
  1857. }
  1858. EXPORT_SYMBOL(input_handler_for_each_handle);
  1859. /**
  1860. * input_register_handle - register a new input handle
  1861. * @handle: handle to register
  1862. *
  1863. * This function puts a new input handle onto device's
  1864. * and handler's lists so that events can flow through
  1865. * it once it is opened using input_open_device().
  1866. *
  1867. * This function is supposed to be called from handler's
  1868. * connect() method.
  1869. */
  1870. int input_register_handle(struct input_handle *handle)
  1871. {
  1872. struct input_handler *handler = handle->handler;
  1873. struct input_dev *dev = handle->dev;
  1874. int error;
  1875. /*
  1876. * We take dev->mutex here to prevent race with
  1877. * input_release_device().
  1878. */
  1879. error = mutex_lock_interruptible(&dev->mutex);
  1880. if (error)
  1881. return error;
  1882. /*
  1883. * Filters go to the head of the list, normal handlers
  1884. * to the tail.
  1885. */
  1886. if (handler->filter)
  1887. list_add_rcu(&handle->d_node, &dev->h_list);
  1888. else
  1889. list_add_tail_rcu(&handle->d_node, &dev->h_list);
  1890. mutex_unlock(&dev->mutex);
  1891. /*
  1892. * Since we are supposed to be called from ->connect()
  1893. * which is mutually exclusive with ->disconnect()
  1894. * we can't be racing with input_unregister_handle()
  1895. * and so separate lock is not needed here.
  1896. */
  1897. list_add_tail_rcu(&handle->h_node, &handler->h_list);
  1898. if (handler->start)
  1899. handler->start(handle);
  1900. return 0;
  1901. }
  1902. EXPORT_SYMBOL(input_register_handle);
  1903. /**
  1904. * input_unregister_handle - unregister an input handle
  1905. * @handle: handle to unregister
  1906. *
  1907. * This function removes input handle from device's
  1908. * and handler's lists.
  1909. *
  1910. * This function is supposed to be called from handler's
  1911. * disconnect() method.
  1912. */
  1913. void input_unregister_handle(struct input_handle *handle)
  1914. {
  1915. struct input_dev *dev = handle->dev;
  1916. list_del_rcu(&handle->h_node);
  1917. /*
  1918. * Take dev->mutex to prevent race with input_release_device().
  1919. */
  1920. mutex_lock(&dev->mutex);
  1921. list_del_rcu(&handle->d_node);
  1922. mutex_unlock(&dev->mutex);
  1923. synchronize_rcu();
  1924. }
  1925. EXPORT_SYMBOL(input_unregister_handle);
  1926. /**
  1927. * input_get_new_minor - allocates a new input minor number
  1928. * @legacy_base: beginning or the legacy range to be searched
  1929. * @legacy_num: size of legacy range
  1930. * @allow_dynamic: whether we can also take ID from the dynamic range
  1931. *
  1932. * This function allocates a new device minor for from input major namespace.
  1933. * Caller can request legacy minor by specifying @legacy_base and @legacy_num
  1934. * parameters and whether ID can be allocated from dynamic range if there are
  1935. * no free IDs in legacy range.
  1936. */
  1937. int input_get_new_minor(int legacy_base, unsigned int legacy_num,
  1938. bool allow_dynamic)
  1939. {
  1940. /*
  1941. * This function should be called from input handler's ->connect()
  1942. * methods, which are serialized with input_mutex, so no additional
  1943. * locking is needed here.
  1944. */
  1945. if (legacy_base >= 0) {
  1946. int minor = ida_simple_get(&input_ida,
  1947. legacy_base,
  1948. legacy_base + legacy_num,
  1949. GFP_KERNEL);
  1950. if (minor >= 0 || !allow_dynamic)
  1951. return minor;
  1952. }
  1953. return ida_simple_get(&input_ida,
  1954. INPUT_FIRST_DYNAMIC_DEV, INPUT_MAX_CHAR_DEVICES,
  1955. GFP_KERNEL);
  1956. }
  1957. EXPORT_SYMBOL(input_get_new_minor);
  1958. /**
  1959. * input_free_minor - release previously allocated minor
  1960. * @minor: minor to be released
  1961. *
  1962. * This function releases previously allocated input minor so that it can be
  1963. * reused later.
  1964. */
  1965. void input_free_minor(unsigned int minor)
  1966. {
  1967. ida_simple_remove(&input_ida, minor);
  1968. }
  1969. EXPORT_SYMBOL(input_free_minor);
  1970. static int __init input_init(void)
  1971. {
  1972. int err;
  1973. err = class_register(&input_class);
  1974. if (err) {
  1975. pr_err("unable to register input_dev class\n");
  1976. return err;
  1977. }
  1978. err = input_proc_init();
  1979. if (err)
  1980. goto fail1;
  1981. err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),
  1982. INPUT_MAX_CHAR_DEVICES, "input");
  1983. if (err) {
  1984. pr_err("unable to register char major %d", INPUT_MAJOR);
  1985. goto fail2;
  1986. }
  1987. return 0;
  1988. fail2: input_proc_exit();
  1989. fail1: class_unregister(&input_class);
  1990. return err;
  1991. }
  1992. static void __exit input_exit(void)
  1993. {
  1994. input_proc_exit();
  1995. unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0),
  1996. INPUT_MAX_CHAR_DEVICES);
  1997. class_unregister(&input_class);
  1998. }
  1999. subsys_initcall(input_init);
  2000. module_exit(input_exit);