input.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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/input.h>
  13. #include <linux/module.h>
  14. #include <linux/random.h>
  15. #include <linux/major.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/poll.h>
  20. #include <linux/device.h>
  21. #include <linux/mutex.h>
  22. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  23. MODULE_DESCRIPTION("Input core");
  24. MODULE_LICENSE("GPL");
  25. #define INPUT_DEVICES 256
  26. static LIST_HEAD(input_dev_list);
  27. static LIST_HEAD(input_handler_list);
  28. static struct input_handler *input_table[8];
  29. /**
  30. * input_event() - report new input event
  31. * @dev: device that generated the event
  32. * @type: type of the event
  33. * @code: event code
  34. * @value: value of the event
  35. *
  36. * This function should be used by drivers implementing various input devices
  37. * See also input_inject_event()
  38. */
  39. void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  40. {
  41. struct input_handle *handle;
  42. if (type > EV_MAX || !test_bit(type, dev->evbit))
  43. return;
  44. add_input_randomness(type, code, value);
  45. switch (type) {
  46. case EV_SYN:
  47. switch (code) {
  48. case SYN_CONFIG:
  49. if (dev->event)
  50. dev->event(dev, type, code, value);
  51. break;
  52. case SYN_REPORT:
  53. if (dev->sync)
  54. return;
  55. dev->sync = 1;
  56. break;
  57. }
  58. break;
  59. case EV_KEY:
  60. if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
  61. return;
  62. if (value == 2)
  63. break;
  64. change_bit(code, dev->key);
  65. if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
  66. dev->repeat_key = code;
  67. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
  68. }
  69. break;
  70. case EV_SW:
  71. if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
  72. return;
  73. change_bit(code, dev->sw);
  74. break;
  75. case EV_ABS:
  76. if (code > ABS_MAX || !test_bit(code, dev->absbit))
  77. return;
  78. if (dev->absfuzz[code]) {
  79. if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
  80. (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
  81. return;
  82. if ((value > dev->abs[code] - dev->absfuzz[code]) &&
  83. (value < dev->abs[code] + dev->absfuzz[code]))
  84. value = (dev->abs[code] * 3 + value) >> 2;
  85. if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
  86. (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
  87. value = (dev->abs[code] + value) >> 1;
  88. }
  89. if (dev->abs[code] == value)
  90. return;
  91. dev->abs[code] = value;
  92. break;
  93. case EV_REL:
  94. if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
  95. return;
  96. break;
  97. case EV_MSC:
  98. if (code > MSC_MAX || !test_bit(code, dev->mscbit))
  99. return;
  100. if (dev->event)
  101. dev->event(dev, type, code, value);
  102. break;
  103. case EV_LED:
  104. if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
  105. return;
  106. change_bit(code, dev->led);
  107. if (dev->event)
  108. dev->event(dev, type, code, value);
  109. break;
  110. case EV_SND:
  111. if (code > SND_MAX || !test_bit(code, dev->sndbit))
  112. return;
  113. if (!!test_bit(code, dev->snd) != !!value)
  114. change_bit(code, dev->snd);
  115. if (dev->event)
  116. dev->event(dev, type, code, value);
  117. break;
  118. case EV_REP:
  119. if (code > REP_MAX || value < 0 || dev->rep[code] == value)
  120. return;
  121. dev->rep[code] = value;
  122. if (dev->event)
  123. dev->event(dev, type, code, value);
  124. break;
  125. case EV_FF:
  126. if (value < 0)
  127. return;
  128. if (dev->event)
  129. dev->event(dev, type, code, value);
  130. break;
  131. }
  132. if (type != EV_SYN)
  133. dev->sync = 0;
  134. if (dev->grab)
  135. dev->grab->handler->event(dev->grab, type, code, value);
  136. else
  137. list_for_each_entry(handle, &dev->h_list, d_node)
  138. if (handle->open)
  139. handle->handler->event(handle, type, code, value);
  140. }
  141. EXPORT_SYMBOL(input_event);
  142. /**
  143. * input_inject_event() - send input event from input handler
  144. * @handle: input handle to send event through
  145. * @type: type of the event
  146. * @code: event code
  147. * @value: value of the event
  148. *
  149. * Similar to input_event() but will ignore event if device is "grabbed" and handle
  150. * injecting event is not the one that owns the device.
  151. */
  152. void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
  153. {
  154. if (!handle->dev->grab || handle->dev->grab == handle)
  155. input_event(handle->dev, type, code, value);
  156. }
  157. EXPORT_SYMBOL(input_inject_event);
  158. static void input_repeat_key(unsigned long data)
  159. {
  160. struct input_dev *dev = (void *) data;
  161. if (!test_bit(dev->repeat_key, dev->key))
  162. return;
  163. input_event(dev, EV_KEY, dev->repeat_key, 2);
  164. input_sync(dev);
  165. if (dev->rep[REP_PERIOD])
  166. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
  167. }
  168. int input_grab_device(struct input_handle *handle)
  169. {
  170. if (handle->dev->grab)
  171. return -EBUSY;
  172. handle->dev->grab = handle;
  173. return 0;
  174. }
  175. EXPORT_SYMBOL(input_grab_device);
  176. void input_release_device(struct input_handle *handle)
  177. {
  178. struct input_dev *dev = handle->dev;
  179. if (dev->grab == handle) {
  180. dev->grab = NULL;
  181. list_for_each_entry(handle, &dev->h_list, d_node)
  182. if (handle->handler->start)
  183. handle->handler->start(handle);
  184. }
  185. }
  186. EXPORT_SYMBOL(input_release_device);
  187. int input_open_device(struct input_handle *handle)
  188. {
  189. struct input_dev *dev = handle->dev;
  190. int err;
  191. err = mutex_lock_interruptible(&dev->mutex);
  192. if (err)
  193. return err;
  194. handle->open++;
  195. if (!dev->users++ && dev->open)
  196. err = dev->open(dev);
  197. if (err)
  198. handle->open--;
  199. mutex_unlock(&dev->mutex);
  200. return err;
  201. }
  202. EXPORT_SYMBOL(input_open_device);
  203. int input_flush_device(struct input_handle* handle, struct file* file)
  204. {
  205. if (handle->dev->flush)
  206. return handle->dev->flush(handle->dev, file);
  207. return 0;
  208. }
  209. EXPORT_SYMBOL(input_flush_device);
  210. void input_close_device(struct input_handle *handle)
  211. {
  212. struct input_dev *dev = handle->dev;
  213. input_release_device(handle);
  214. mutex_lock(&dev->mutex);
  215. if (!--dev->users && dev->close)
  216. dev->close(dev);
  217. handle->open--;
  218. mutex_unlock(&dev->mutex);
  219. }
  220. EXPORT_SYMBOL(input_close_device);
  221. static int input_fetch_keycode(struct input_dev *dev, int scancode)
  222. {
  223. switch (dev->keycodesize) {
  224. case 1:
  225. return ((u8 *)dev->keycode)[scancode];
  226. case 2:
  227. return ((u16 *)dev->keycode)[scancode];
  228. default:
  229. return ((u32 *)dev->keycode)[scancode];
  230. }
  231. }
  232. static int input_default_getkeycode(struct input_dev *dev,
  233. int scancode, int *keycode)
  234. {
  235. if (!dev->keycodesize)
  236. return -EINVAL;
  237. if (scancode < 0 || scancode >= dev->keycodemax)
  238. return -EINVAL;
  239. *keycode = input_fetch_keycode(dev, scancode);
  240. return 0;
  241. }
  242. static int input_default_setkeycode(struct input_dev *dev,
  243. int scancode, int keycode)
  244. {
  245. int old_keycode;
  246. int i;
  247. if (scancode < 0 || scancode >= dev->keycodemax)
  248. return -EINVAL;
  249. if (keycode < 0 || keycode > KEY_MAX)
  250. return -EINVAL;
  251. if (!dev->keycodesize)
  252. return -EINVAL;
  253. if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
  254. return -EINVAL;
  255. switch (dev->keycodesize) {
  256. case 1: {
  257. u8 *k = (u8 *)dev->keycode;
  258. old_keycode = k[scancode];
  259. k[scancode] = keycode;
  260. break;
  261. }
  262. case 2: {
  263. u16 *k = (u16 *)dev->keycode;
  264. old_keycode = k[scancode];
  265. k[scancode] = keycode;
  266. break;
  267. }
  268. default: {
  269. u32 *k = (u32 *)dev->keycode;
  270. old_keycode = k[scancode];
  271. k[scancode] = keycode;
  272. break;
  273. }
  274. }
  275. clear_bit(old_keycode, dev->keybit);
  276. set_bit(keycode, dev->keybit);
  277. for (i = 0; i < dev->keycodemax; i++) {
  278. if (input_fetch_keycode(dev, i) == old_keycode) {
  279. set_bit(old_keycode, dev->keybit);
  280. break; /* Setting the bit twice is useless, so break */
  281. }
  282. }
  283. return 0;
  284. }
  285. #define MATCH_BIT(bit, max) \
  286. for (i = 0; i < NBITS(max); i++) \
  287. if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
  288. break; \
  289. if (i != NBITS(max)) \
  290. continue;
  291. static const struct input_device_id *input_match_device(const struct input_device_id *id,
  292. struct input_dev *dev)
  293. {
  294. int i;
  295. for (; id->flags || id->driver_info; id++) {
  296. if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  297. if (id->bustype != dev->id.bustype)
  298. continue;
  299. if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  300. if (id->vendor != dev->id.vendor)
  301. continue;
  302. if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  303. if (id->product != dev->id.product)
  304. continue;
  305. if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  306. if (id->version != dev->id.version)
  307. continue;
  308. MATCH_BIT(evbit, EV_MAX);
  309. MATCH_BIT(keybit, KEY_MAX);
  310. MATCH_BIT(relbit, REL_MAX);
  311. MATCH_BIT(absbit, ABS_MAX);
  312. MATCH_BIT(mscbit, MSC_MAX);
  313. MATCH_BIT(ledbit, LED_MAX);
  314. MATCH_BIT(sndbit, SND_MAX);
  315. MATCH_BIT(ffbit, FF_MAX);
  316. MATCH_BIT(swbit, SW_MAX);
  317. return id;
  318. }
  319. return NULL;
  320. }
  321. static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
  322. {
  323. const struct input_device_id *id;
  324. int error;
  325. if (handler->blacklist && input_match_device(handler->blacklist, dev))
  326. return -ENODEV;
  327. id = input_match_device(handler->id_table, dev);
  328. if (!id)
  329. return -ENODEV;
  330. error = handler->connect(handler, dev, id);
  331. if (error && error != -ENODEV)
  332. printk(KERN_ERR
  333. "input: failed to attach handler %s to device %s, "
  334. "error: %d\n",
  335. handler->name, kobject_name(&dev->dev.kobj), error);
  336. return error;
  337. }
  338. #ifdef CONFIG_PROC_FS
  339. static struct proc_dir_entry *proc_bus_input_dir;
  340. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  341. static int input_devices_state;
  342. static inline void input_wakeup_procfs_readers(void)
  343. {
  344. input_devices_state++;
  345. wake_up(&input_devices_poll_wait);
  346. }
  347. static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
  348. {
  349. int state = input_devices_state;
  350. poll_wait(file, &input_devices_poll_wait, wait);
  351. if (state != input_devices_state)
  352. return POLLIN | POLLRDNORM;
  353. return 0;
  354. }
  355. static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
  356. {
  357. /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
  358. return seq_list_start(&input_dev_list, *pos);
  359. }
  360. static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  361. {
  362. return seq_list_next(v, &input_dev_list, pos);
  363. }
  364. static void input_devices_seq_stop(struct seq_file *seq, void *v)
  365. {
  366. /* release lock here */
  367. }
  368. static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
  369. unsigned long *bitmap, int max)
  370. {
  371. int i;
  372. for (i = NBITS(max) - 1; i > 0; i--)
  373. if (bitmap[i])
  374. break;
  375. seq_printf(seq, "B: %s=", name);
  376. for (; i >= 0; i--)
  377. seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
  378. seq_putc(seq, '\n');
  379. }
  380. static int input_devices_seq_show(struct seq_file *seq, void *v)
  381. {
  382. struct input_dev *dev = container_of(v, struct input_dev, node);
  383. const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  384. struct input_handle *handle;
  385. seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  386. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  387. seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  388. seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  389. seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
  390. seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
  391. seq_printf(seq, "H: Handlers=");
  392. list_for_each_entry(handle, &dev->h_list, d_node)
  393. seq_printf(seq, "%s ", handle->name);
  394. seq_putc(seq, '\n');
  395. input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
  396. if (test_bit(EV_KEY, dev->evbit))
  397. input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
  398. if (test_bit(EV_REL, dev->evbit))
  399. input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
  400. if (test_bit(EV_ABS, dev->evbit))
  401. input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
  402. if (test_bit(EV_MSC, dev->evbit))
  403. input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
  404. if (test_bit(EV_LED, dev->evbit))
  405. input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
  406. if (test_bit(EV_SND, dev->evbit))
  407. input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
  408. if (test_bit(EV_FF, dev->evbit))
  409. input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
  410. if (test_bit(EV_SW, dev->evbit))
  411. input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
  412. seq_putc(seq, '\n');
  413. kfree(path);
  414. return 0;
  415. }
  416. static struct seq_operations input_devices_seq_ops = {
  417. .start = input_devices_seq_start,
  418. .next = input_devices_seq_next,
  419. .stop = input_devices_seq_stop,
  420. .show = input_devices_seq_show,
  421. };
  422. static int input_proc_devices_open(struct inode *inode, struct file *file)
  423. {
  424. return seq_open(file, &input_devices_seq_ops);
  425. }
  426. static const struct file_operations input_devices_fileops = {
  427. .owner = THIS_MODULE,
  428. .open = input_proc_devices_open,
  429. .poll = input_proc_devices_poll,
  430. .read = seq_read,
  431. .llseek = seq_lseek,
  432. .release = seq_release,
  433. };
  434. static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
  435. {
  436. /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
  437. seq->private = (void *)(unsigned long)*pos;
  438. return seq_list_start(&input_handler_list, *pos);
  439. }
  440. static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  441. {
  442. seq->private = (void *)(unsigned long)(*pos + 1);
  443. return seq_list_next(v, &input_handler_list, pos);
  444. }
  445. static void input_handlers_seq_stop(struct seq_file *seq, void *v)
  446. {
  447. /* release lock here */
  448. }
  449. static int input_handlers_seq_show(struct seq_file *seq, void *v)
  450. {
  451. struct input_handler *handler = container_of(v, struct input_handler, node);
  452. seq_printf(seq, "N: Number=%ld Name=%s",
  453. (unsigned long)seq->private, handler->name);
  454. if (handler->fops)
  455. seq_printf(seq, " Minor=%d", handler->minor);
  456. seq_putc(seq, '\n');
  457. return 0;
  458. }
  459. static struct seq_operations input_handlers_seq_ops = {
  460. .start = input_handlers_seq_start,
  461. .next = input_handlers_seq_next,
  462. .stop = input_handlers_seq_stop,
  463. .show = input_handlers_seq_show,
  464. };
  465. static int input_proc_handlers_open(struct inode *inode, struct file *file)
  466. {
  467. return seq_open(file, &input_handlers_seq_ops);
  468. }
  469. static const struct file_operations input_handlers_fileops = {
  470. .owner = THIS_MODULE,
  471. .open = input_proc_handlers_open,
  472. .read = seq_read,
  473. .llseek = seq_lseek,
  474. .release = seq_release,
  475. };
  476. static int __init input_proc_init(void)
  477. {
  478. struct proc_dir_entry *entry;
  479. proc_bus_input_dir = proc_mkdir("input", proc_bus);
  480. if (!proc_bus_input_dir)
  481. return -ENOMEM;
  482. proc_bus_input_dir->owner = THIS_MODULE;
  483. entry = create_proc_entry("devices", 0, proc_bus_input_dir);
  484. if (!entry)
  485. goto fail1;
  486. entry->owner = THIS_MODULE;
  487. entry->proc_fops = &input_devices_fileops;
  488. entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
  489. if (!entry)
  490. goto fail2;
  491. entry->owner = THIS_MODULE;
  492. entry->proc_fops = &input_handlers_fileops;
  493. return 0;
  494. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  495. fail1: remove_proc_entry("input", proc_bus);
  496. return -ENOMEM;
  497. }
  498. static void input_proc_exit(void)
  499. {
  500. remove_proc_entry("devices", proc_bus_input_dir);
  501. remove_proc_entry("handlers", proc_bus_input_dir);
  502. remove_proc_entry("input", proc_bus);
  503. }
  504. #else /* !CONFIG_PROC_FS */
  505. static inline void input_wakeup_procfs_readers(void) { }
  506. static inline int input_proc_init(void) { return 0; }
  507. static inline void input_proc_exit(void) { }
  508. #endif
  509. #define INPUT_DEV_STRING_ATTR_SHOW(name) \
  510. static ssize_t input_dev_show_##name(struct device *dev, \
  511. struct device_attribute *attr, \
  512. char *buf) \
  513. { \
  514. struct input_dev *input_dev = to_input_dev(dev); \
  515. \
  516. return scnprintf(buf, PAGE_SIZE, "%s\n", \
  517. input_dev->name ? input_dev->name : ""); \
  518. } \
  519. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
  520. INPUT_DEV_STRING_ATTR_SHOW(name);
  521. INPUT_DEV_STRING_ATTR_SHOW(phys);
  522. INPUT_DEV_STRING_ATTR_SHOW(uniq);
  523. static int input_print_modalias_bits(char *buf, int size,
  524. char name, unsigned long *bm,
  525. unsigned int min_bit, unsigned int max_bit)
  526. {
  527. int len = 0, i;
  528. len += snprintf(buf, max(size, 0), "%c", name);
  529. for (i = min_bit; i < max_bit; i++)
  530. if (bm[LONG(i)] & BIT(i))
  531. len += snprintf(buf + len, max(size - len, 0), "%X,", i);
  532. return len;
  533. }
  534. static int input_print_modalias(char *buf, int size, struct input_dev *id,
  535. int add_cr)
  536. {
  537. int len;
  538. len = snprintf(buf, max(size, 0),
  539. "input:b%04Xv%04Xp%04Xe%04X-",
  540. id->id.bustype, id->id.vendor,
  541. id->id.product, id->id.version);
  542. len += input_print_modalias_bits(buf + len, size - len,
  543. 'e', id->evbit, 0, EV_MAX);
  544. len += input_print_modalias_bits(buf + len, size - len,
  545. 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
  546. len += input_print_modalias_bits(buf + len, size - len,
  547. 'r', id->relbit, 0, REL_MAX);
  548. len += input_print_modalias_bits(buf + len, size - len,
  549. 'a', id->absbit, 0, ABS_MAX);
  550. len += input_print_modalias_bits(buf + len, size - len,
  551. 'm', id->mscbit, 0, MSC_MAX);
  552. len += input_print_modalias_bits(buf + len, size - len,
  553. 'l', id->ledbit, 0, LED_MAX);
  554. len += input_print_modalias_bits(buf + len, size - len,
  555. 's', id->sndbit, 0, SND_MAX);
  556. len += input_print_modalias_bits(buf + len, size - len,
  557. 'f', id->ffbit, 0, FF_MAX);
  558. len += input_print_modalias_bits(buf + len, size - len,
  559. 'w', id->swbit, 0, SW_MAX);
  560. if (add_cr)
  561. len += snprintf(buf + len, max(size - len, 0), "\n");
  562. return len;
  563. }
  564. static ssize_t input_dev_show_modalias(struct device *dev,
  565. struct device_attribute *attr,
  566. char *buf)
  567. {
  568. struct input_dev *id = to_input_dev(dev);
  569. ssize_t len;
  570. len = input_print_modalias(buf, PAGE_SIZE, id, 1);
  571. return min_t(int, len, PAGE_SIZE);
  572. }
  573. static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
  574. static struct attribute *input_dev_attrs[] = {
  575. &dev_attr_name.attr,
  576. &dev_attr_phys.attr,
  577. &dev_attr_uniq.attr,
  578. &dev_attr_modalias.attr,
  579. NULL
  580. };
  581. static struct attribute_group input_dev_attr_group = {
  582. .attrs = input_dev_attrs,
  583. };
  584. #define INPUT_DEV_ID_ATTR(name) \
  585. static ssize_t input_dev_show_id_##name(struct device *dev, \
  586. struct device_attribute *attr, \
  587. char *buf) \
  588. { \
  589. struct input_dev *input_dev = to_input_dev(dev); \
  590. return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
  591. } \
  592. static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
  593. INPUT_DEV_ID_ATTR(bustype);
  594. INPUT_DEV_ID_ATTR(vendor);
  595. INPUT_DEV_ID_ATTR(product);
  596. INPUT_DEV_ID_ATTR(version);
  597. static struct attribute *input_dev_id_attrs[] = {
  598. &dev_attr_bustype.attr,
  599. &dev_attr_vendor.attr,
  600. &dev_attr_product.attr,
  601. &dev_attr_version.attr,
  602. NULL
  603. };
  604. static struct attribute_group input_dev_id_attr_group = {
  605. .name = "id",
  606. .attrs = input_dev_id_attrs,
  607. };
  608. static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
  609. int max, int add_cr)
  610. {
  611. int i;
  612. int len = 0;
  613. for (i = NBITS(max) - 1; i > 0; i--)
  614. if (bitmap[i])
  615. break;
  616. for (; i >= 0; i--)
  617. len += snprintf(buf + len, max(buf_size - len, 0),
  618. "%lx%s", bitmap[i], i > 0 ? " " : "");
  619. if (add_cr)
  620. len += snprintf(buf + len, max(buf_size - len, 0), "\n");
  621. return len;
  622. }
  623. #define INPUT_DEV_CAP_ATTR(ev, bm) \
  624. static ssize_t input_dev_show_cap_##bm(struct device *dev, \
  625. struct device_attribute *attr, \
  626. char *buf) \
  627. { \
  628. struct input_dev *input_dev = to_input_dev(dev); \
  629. int len = input_print_bitmap(buf, PAGE_SIZE, \
  630. input_dev->bm##bit, ev##_MAX, 1); \
  631. return min_t(int, len, PAGE_SIZE); \
  632. } \
  633. static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
  634. INPUT_DEV_CAP_ATTR(EV, ev);
  635. INPUT_DEV_CAP_ATTR(KEY, key);
  636. INPUT_DEV_CAP_ATTR(REL, rel);
  637. INPUT_DEV_CAP_ATTR(ABS, abs);
  638. INPUT_DEV_CAP_ATTR(MSC, msc);
  639. INPUT_DEV_CAP_ATTR(LED, led);
  640. INPUT_DEV_CAP_ATTR(SND, snd);
  641. INPUT_DEV_CAP_ATTR(FF, ff);
  642. INPUT_DEV_CAP_ATTR(SW, sw);
  643. static struct attribute *input_dev_caps_attrs[] = {
  644. &dev_attr_ev.attr,
  645. &dev_attr_key.attr,
  646. &dev_attr_rel.attr,
  647. &dev_attr_abs.attr,
  648. &dev_attr_msc.attr,
  649. &dev_attr_led.attr,
  650. &dev_attr_snd.attr,
  651. &dev_attr_ff.attr,
  652. &dev_attr_sw.attr,
  653. NULL
  654. };
  655. static struct attribute_group input_dev_caps_attr_group = {
  656. .name = "capabilities",
  657. .attrs = input_dev_caps_attrs,
  658. };
  659. static struct attribute_group *input_dev_attr_groups[] = {
  660. &input_dev_attr_group,
  661. &input_dev_id_attr_group,
  662. &input_dev_caps_attr_group,
  663. NULL
  664. };
  665. static void input_dev_release(struct device *device)
  666. {
  667. struct input_dev *dev = to_input_dev(device);
  668. input_ff_destroy(dev);
  669. kfree(dev);
  670. module_put(THIS_MODULE);
  671. }
  672. /*
  673. * Input uevent interface - loading event handlers based on
  674. * device bitfields.
  675. */
  676. static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
  677. const char *name, unsigned long *bitmap, int max)
  678. {
  679. int len;
  680. if (add_uevent_var(env, "%s=", name))
  681. return -ENOMEM;
  682. len = input_print_bitmap(&env->buf[env->buflen - 1],
  683. sizeof(env->buf) - env->buflen,
  684. bitmap, max, 0);
  685. if (len >= (sizeof(env->buf) - env->buflen))
  686. return -ENOMEM;
  687. env->buflen += len;
  688. return 0;
  689. }
  690. static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
  691. struct input_dev *dev)
  692. {
  693. int len;
  694. if (add_uevent_var(env, "MODALIAS="))
  695. return -ENOMEM;
  696. len = input_print_modalias(&env->buf[env->buflen - 1],
  697. sizeof(env->buf) - env->buflen,
  698. dev, 0);
  699. if (len >= (sizeof(env->buf) - env->buflen))
  700. return -ENOMEM;
  701. env->buflen += len;
  702. return 0;
  703. }
  704. #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
  705. do { \
  706. int err = add_uevent_var(env, fmt, val); \
  707. if (err) \
  708. return err; \
  709. } while (0)
  710. #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
  711. do { \
  712. int err = input_add_uevent_bm_var(env, name, bm, max); \
  713. if (err) \
  714. return err; \
  715. } while (0)
  716. #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
  717. do { \
  718. int err = input_add_uevent_modalias_var(env, dev); \
  719. if (err) \
  720. return err; \
  721. } while (0)
  722. static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  723. {
  724. struct input_dev *dev = to_input_dev(device);
  725. INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
  726. dev->id.bustype, dev->id.vendor,
  727. dev->id.product, dev->id.version);
  728. if (dev->name)
  729. INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
  730. if (dev->phys)
  731. INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
  732. if (dev->uniq)
  733. INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
  734. INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
  735. if (test_bit(EV_KEY, dev->evbit))
  736. INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
  737. if (test_bit(EV_REL, dev->evbit))
  738. INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
  739. if (test_bit(EV_ABS, dev->evbit))
  740. INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
  741. if (test_bit(EV_MSC, dev->evbit))
  742. INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
  743. if (test_bit(EV_LED, dev->evbit))
  744. INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
  745. if (test_bit(EV_SND, dev->evbit))
  746. INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
  747. if (test_bit(EV_FF, dev->evbit))
  748. INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
  749. if (test_bit(EV_SW, dev->evbit))
  750. INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
  751. INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
  752. return 0;
  753. }
  754. static struct device_type input_dev_type = {
  755. .groups = input_dev_attr_groups,
  756. .release = input_dev_release,
  757. .uevent = input_dev_uevent,
  758. };
  759. struct class input_class = {
  760. .name = "input",
  761. };
  762. EXPORT_SYMBOL_GPL(input_class);
  763. /**
  764. * input_allocate_device - allocate memory for new input device
  765. *
  766. * Returns prepared struct input_dev or NULL.
  767. *
  768. * NOTE: Use input_free_device() to free devices that have not been
  769. * registered; input_unregister_device() should be used for already
  770. * registered devices.
  771. */
  772. struct input_dev *input_allocate_device(void)
  773. {
  774. struct input_dev *dev;
  775. dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  776. if (dev) {
  777. dev->dev.type = &input_dev_type;
  778. dev->dev.class = &input_class;
  779. device_initialize(&dev->dev);
  780. mutex_init(&dev->mutex);
  781. INIT_LIST_HEAD(&dev->h_list);
  782. INIT_LIST_HEAD(&dev->node);
  783. __module_get(THIS_MODULE);
  784. }
  785. return dev;
  786. }
  787. EXPORT_SYMBOL(input_allocate_device);
  788. /**
  789. * input_free_device - free memory occupied by input_dev structure
  790. * @dev: input device to free
  791. *
  792. * This function should only be used if input_register_device()
  793. * was not called yet or if it failed. Once device was registered
  794. * use input_unregister_device() and memory will be freed once last
  795. * refrence to the device is dropped.
  796. *
  797. * Device should be allocated by input_allocate_device().
  798. *
  799. * NOTE: If there are references to the input device then memory
  800. * will not be freed until last reference is dropped.
  801. */
  802. void input_free_device(struct input_dev *dev)
  803. {
  804. if (dev)
  805. input_put_device(dev);
  806. }
  807. EXPORT_SYMBOL(input_free_device);
  808. /**
  809. * input_set_capability - mark device as capable of a certain event
  810. * @dev: device that is capable of emitting or accepting event
  811. * @type: type of the event (EV_KEY, EV_REL, etc...)
  812. * @code: event code
  813. *
  814. * In addition to setting up corresponding bit in appropriate capability
  815. * bitmap the function also adjusts dev->evbit.
  816. */
  817. void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
  818. {
  819. switch (type) {
  820. case EV_KEY:
  821. __set_bit(code, dev->keybit);
  822. break;
  823. case EV_REL:
  824. __set_bit(code, dev->relbit);
  825. break;
  826. case EV_ABS:
  827. __set_bit(code, dev->absbit);
  828. break;
  829. case EV_MSC:
  830. __set_bit(code, dev->mscbit);
  831. break;
  832. case EV_SW:
  833. __set_bit(code, dev->swbit);
  834. break;
  835. case EV_LED:
  836. __set_bit(code, dev->ledbit);
  837. break;
  838. case EV_SND:
  839. __set_bit(code, dev->sndbit);
  840. break;
  841. case EV_FF:
  842. __set_bit(code, dev->ffbit);
  843. break;
  844. default:
  845. printk(KERN_ERR
  846. "input_set_capability: unknown type %u (code %u)\n",
  847. type, code);
  848. dump_stack();
  849. return;
  850. }
  851. __set_bit(type, dev->evbit);
  852. }
  853. EXPORT_SYMBOL(input_set_capability);
  854. int input_register_device(struct input_dev *dev)
  855. {
  856. static atomic_t input_no = ATOMIC_INIT(0);
  857. struct input_handler *handler;
  858. const char *path;
  859. int error;
  860. set_bit(EV_SYN, dev->evbit);
  861. /*
  862. * If delay and period are pre-set by the driver, then autorepeating
  863. * is handled by the driver itself and we don't do it in input.c.
  864. */
  865. init_timer(&dev->timer);
  866. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  867. dev->timer.data = (long) dev;
  868. dev->timer.function = input_repeat_key;
  869. dev->rep[REP_DELAY] = 250;
  870. dev->rep[REP_PERIOD] = 33;
  871. }
  872. if (!dev->getkeycode)
  873. dev->getkeycode = input_default_getkeycode;
  874. if (!dev->setkeycode)
  875. dev->setkeycode = input_default_setkeycode;
  876. list_add_tail(&dev->node, &input_dev_list);
  877. snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
  878. "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
  879. if (dev->cdev.dev)
  880. dev->dev.parent = dev->cdev.dev;
  881. error = device_add(&dev->dev);
  882. if (error)
  883. return error;
  884. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  885. printk(KERN_INFO "input: %s as %s\n",
  886. dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
  887. kfree(path);
  888. list_for_each_entry(handler, &input_handler_list, node)
  889. input_attach_handler(dev, handler);
  890. input_wakeup_procfs_readers();
  891. return 0;
  892. }
  893. EXPORT_SYMBOL(input_register_device);
  894. void input_unregister_device(struct input_dev *dev)
  895. {
  896. struct input_handle *handle, *next;
  897. int code;
  898. for (code = 0; code <= KEY_MAX; code++)
  899. if (test_bit(code, dev->key))
  900. input_report_key(dev, code, 0);
  901. input_sync(dev);
  902. del_timer_sync(&dev->timer);
  903. list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
  904. handle->handler->disconnect(handle);
  905. WARN_ON(!list_empty(&dev->h_list));
  906. list_del_init(&dev->node);
  907. device_unregister(&dev->dev);
  908. input_wakeup_procfs_readers();
  909. }
  910. EXPORT_SYMBOL(input_unregister_device);
  911. int input_register_handler(struct input_handler *handler)
  912. {
  913. struct input_dev *dev;
  914. INIT_LIST_HEAD(&handler->h_list);
  915. if (handler->fops != NULL) {
  916. if (input_table[handler->minor >> 5])
  917. return -EBUSY;
  918. input_table[handler->minor >> 5] = handler;
  919. }
  920. list_add_tail(&handler->node, &input_handler_list);
  921. list_for_each_entry(dev, &input_dev_list, node)
  922. input_attach_handler(dev, handler);
  923. input_wakeup_procfs_readers();
  924. return 0;
  925. }
  926. EXPORT_SYMBOL(input_register_handler);
  927. void input_unregister_handler(struct input_handler *handler)
  928. {
  929. struct input_handle *handle, *next;
  930. list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
  931. handler->disconnect(handle);
  932. WARN_ON(!list_empty(&handler->h_list));
  933. list_del_init(&handler->node);
  934. if (handler->fops != NULL)
  935. input_table[handler->minor >> 5] = NULL;
  936. input_wakeup_procfs_readers();
  937. }
  938. EXPORT_SYMBOL(input_unregister_handler);
  939. int input_register_handle(struct input_handle *handle)
  940. {
  941. struct input_handler *handler = handle->handler;
  942. list_add_tail(&handle->d_node, &handle->dev->h_list);
  943. list_add_tail(&handle->h_node, &handler->h_list);
  944. if (handler->start)
  945. handler->start(handle);
  946. return 0;
  947. }
  948. EXPORT_SYMBOL(input_register_handle);
  949. void input_unregister_handle(struct input_handle *handle)
  950. {
  951. list_del_init(&handle->h_node);
  952. list_del_init(&handle->d_node);
  953. }
  954. EXPORT_SYMBOL(input_unregister_handle);
  955. static int input_open_file(struct inode *inode, struct file *file)
  956. {
  957. struct input_handler *handler = input_table[iminor(inode) >> 5];
  958. const struct file_operations *old_fops, *new_fops = NULL;
  959. int err;
  960. /* No load-on-demand here? */
  961. if (!handler || !(new_fops = fops_get(handler->fops)))
  962. return -ENODEV;
  963. /*
  964. * That's _really_ odd. Usually NULL ->open means "nothing special",
  965. * not "no device". Oh, well...
  966. */
  967. if (!new_fops->open) {
  968. fops_put(new_fops);
  969. return -ENODEV;
  970. }
  971. old_fops = file->f_op;
  972. file->f_op = new_fops;
  973. err = new_fops->open(inode, file);
  974. if (err) {
  975. fops_put(file->f_op);
  976. file->f_op = fops_get(old_fops);
  977. }
  978. fops_put(old_fops);
  979. return err;
  980. }
  981. static const struct file_operations input_fops = {
  982. .owner = THIS_MODULE,
  983. .open = input_open_file,
  984. };
  985. static int __init input_init(void)
  986. {
  987. int err;
  988. err = class_register(&input_class);
  989. if (err) {
  990. printk(KERN_ERR "input: unable to register input_dev class\n");
  991. return err;
  992. }
  993. err = input_proc_init();
  994. if (err)
  995. goto fail1;
  996. err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
  997. if (err) {
  998. printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
  999. goto fail2;
  1000. }
  1001. return 0;
  1002. fail2: input_proc_exit();
  1003. fail1: class_unregister(&input_class);
  1004. return err;
  1005. }
  1006. static void __exit input_exit(void)
  1007. {
  1008. input_proc_exit();
  1009. unregister_chrdev(INPUT_MAJOR, "input");
  1010. class_unregister(&input_class);
  1011. }
  1012. subsys_initcall(input_init);
  1013. module_exit(input_exit);