input.c 53 KB

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