input.c 53 KB

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