input.c 60 KB

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