input.c 54 KB

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