input.c 54 KB

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