input.c 54 KB

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