input.c 46 KB

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