input.c 60 KB

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