input.c 29 KB

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