input.c 55 KB

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