input.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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 int input_fetch_keycode(struct input_dev *dev, int scancode)
  223. {
  224. switch (dev->keycodesize) {
  225. case 1:
  226. return ((u8 *)dev->keycode)[scancode];
  227. case 2:
  228. return ((u16 *)dev->keycode)[scancode];
  229. default:
  230. return ((u32 *)dev->keycode)[scancode];
  231. }
  232. }
  233. static int input_default_getkeycode(struct input_dev *dev,
  234. int scancode, int *keycode)
  235. {
  236. if (!dev->keycodesize)
  237. return -EINVAL;
  238. if (scancode < 0 || scancode >= dev->keycodemax)
  239. return -EINVAL;
  240. *keycode = input_fetch_keycode(dev, scancode);
  241. return 0;
  242. }
  243. static int input_default_setkeycode(struct input_dev *dev,
  244. int scancode, int keycode)
  245. {
  246. int old_keycode;
  247. int i;
  248. if (scancode < 0 || scancode >= dev->keycodemax)
  249. return -EINVAL;
  250. if (keycode < 0 || keycode > KEY_MAX)
  251. return -EINVAL;
  252. if (!dev->keycodesize)
  253. return -EINVAL;
  254. if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
  255. return -EINVAL;
  256. switch (dev->keycodesize) {
  257. case 1: {
  258. u8 *k = (u8 *)dev->keycode;
  259. old_keycode = k[scancode];
  260. k[scancode] = keycode;
  261. break;
  262. }
  263. case 2: {
  264. u16 *k = (u16 *)dev->keycode;
  265. old_keycode = k[scancode];
  266. k[scancode] = keycode;
  267. break;
  268. }
  269. default: {
  270. u32 *k = (u32 *)dev->keycode;
  271. old_keycode = k[scancode];
  272. k[scancode] = keycode;
  273. break;
  274. }
  275. }
  276. clear_bit(old_keycode, dev->keybit);
  277. set_bit(keycode, dev->keybit);
  278. for (i = 0; i < dev->keycodemax; i++) {
  279. if (input_fetch_keycode(dev, i) == old_keycode) {
  280. set_bit(old_keycode, dev->keybit);
  281. break; /* Setting the bit twice is useless, so break */
  282. }
  283. }
  284. return 0;
  285. }
  286. #define MATCH_BIT(bit, max) \
  287. for (i = 0; i < NBITS(max); i++) \
  288. if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
  289. break; \
  290. if (i != NBITS(max)) \
  291. continue;
  292. static const struct input_device_id *input_match_device(const struct input_device_id *id,
  293. struct input_dev *dev)
  294. {
  295. int i;
  296. for (; id->flags || id->driver_info; id++) {
  297. if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  298. if (id->bustype != dev->id.bustype)
  299. continue;
  300. if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  301. if (id->vendor != dev->id.vendor)
  302. continue;
  303. if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  304. if (id->product != dev->id.product)
  305. continue;
  306. if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  307. if (id->version != dev->id.version)
  308. continue;
  309. MATCH_BIT(evbit, EV_MAX);
  310. MATCH_BIT(keybit, KEY_MAX);
  311. MATCH_BIT(relbit, REL_MAX);
  312. MATCH_BIT(absbit, ABS_MAX);
  313. MATCH_BIT(mscbit, MSC_MAX);
  314. MATCH_BIT(ledbit, LED_MAX);
  315. MATCH_BIT(sndbit, SND_MAX);
  316. MATCH_BIT(ffbit, FF_MAX);
  317. MATCH_BIT(swbit, SW_MAX);
  318. return id;
  319. }
  320. return NULL;
  321. }
  322. static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
  323. {
  324. const struct input_device_id *id;
  325. int error;
  326. if (handler->blacklist && input_match_device(handler->blacklist, dev))
  327. return -ENODEV;
  328. id = input_match_device(handler->id_table, dev);
  329. if (!id)
  330. return -ENODEV;
  331. error = handler->connect(handler, dev, id);
  332. if (error && error != -ENODEV)
  333. printk(KERN_ERR
  334. "input: failed to attach handler %s to device %s, "
  335. "error: %d\n",
  336. handler->name, kobject_name(&dev->cdev.kobj), error);
  337. return error;
  338. }
  339. #ifdef CONFIG_PROC_FS
  340. static struct proc_dir_entry *proc_bus_input_dir;
  341. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  342. static int input_devices_state;
  343. static inline void input_wakeup_procfs_readers(void)
  344. {
  345. input_devices_state++;
  346. wake_up(&input_devices_poll_wait);
  347. }
  348. static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
  349. {
  350. int state = input_devices_state;
  351. poll_wait(file, &input_devices_poll_wait, wait);
  352. if (state != input_devices_state)
  353. return POLLIN | POLLRDNORM;
  354. return 0;
  355. }
  356. static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
  357. {
  358. struct list_head *node;
  359. loff_t i = 0;
  360. list_for_each(node, list)
  361. if (i++ == *pos)
  362. return node;
  363. return NULL;
  364. }
  365. static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
  366. {
  367. if (element->next == list)
  368. return NULL;
  369. ++(*pos);
  370. return element->next;
  371. }
  372. static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
  373. {
  374. /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
  375. return list_get_nth_element(&input_dev_list, pos);
  376. }
  377. static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  378. {
  379. return list_get_next_element(&input_dev_list, v, pos);
  380. }
  381. static void input_devices_seq_stop(struct seq_file *seq, void *v)
  382. {
  383. /* release lock here */
  384. }
  385. static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
  386. unsigned long *bitmap, int max)
  387. {
  388. int i;
  389. for (i = NBITS(max) - 1; i > 0; i--)
  390. if (bitmap[i])
  391. break;
  392. seq_printf(seq, "B: %s=", name);
  393. for (; i >= 0; i--)
  394. seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
  395. seq_putc(seq, '\n');
  396. }
  397. static int input_devices_seq_show(struct seq_file *seq, void *v)
  398. {
  399. struct input_dev *dev = container_of(v, struct input_dev, node);
  400. const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
  401. struct input_handle *handle;
  402. seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  403. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  404. seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  405. seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  406. seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
  407. seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
  408. seq_printf(seq, "H: Handlers=");
  409. list_for_each_entry(handle, &dev->h_list, d_node)
  410. seq_printf(seq, "%s ", handle->name);
  411. seq_putc(seq, '\n');
  412. input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
  413. if (test_bit(EV_KEY, dev->evbit))
  414. input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
  415. if (test_bit(EV_REL, dev->evbit))
  416. input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
  417. if (test_bit(EV_ABS, dev->evbit))
  418. input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
  419. if (test_bit(EV_MSC, dev->evbit))
  420. input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
  421. if (test_bit(EV_LED, dev->evbit))
  422. input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
  423. if (test_bit(EV_SND, dev->evbit))
  424. input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
  425. if (test_bit(EV_FF, dev->evbit))
  426. input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
  427. if (test_bit(EV_SW, dev->evbit))
  428. input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
  429. seq_putc(seq, '\n');
  430. kfree(path);
  431. return 0;
  432. }
  433. static struct seq_operations input_devices_seq_ops = {
  434. .start = input_devices_seq_start,
  435. .next = input_devices_seq_next,
  436. .stop = input_devices_seq_stop,
  437. .show = input_devices_seq_show,
  438. };
  439. static int input_proc_devices_open(struct inode *inode, struct file *file)
  440. {
  441. return seq_open(file, &input_devices_seq_ops);
  442. }
  443. static const struct file_operations input_devices_fileops = {
  444. .owner = THIS_MODULE,
  445. .open = input_proc_devices_open,
  446. .poll = input_proc_devices_poll,
  447. .read = seq_read,
  448. .llseek = seq_lseek,
  449. .release = seq_release,
  450. };
  451. static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
  452. {
  453. /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
  454. seq->private = (void *)(unsigned long)*pos;
  455. return list_get_nth_element(&input_handler_list, pos);
  456. }
  457. static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  458. {
  459. seq->private = (void *)(unsigned long)(*pos + 1);
  460. return list_get_next_element(&input_handler_list, v, pos);
  461. }
  462. static void input_handlers_seq_stop(struct seq_file *seq, void *v)
  463. {
  464. /* release lock here */
  465. }
  466. static int input_handlers_seq_show(struct seq_file *seq, void *v)
  467. {
  468. struct input_handler *handler = container_of(v, struct input_handler, node);
  469. seq_printf(seq, "N: Number=%ld Name=%s",
  470. (unsigned long)seq->private, handler->name);
  471. if (handler->fops)
  472. seq_printf(seq, " Minor=%d", handler->minor);
  473. seq_putc(seq, '\n');
  474. return 0;
  475. }
  476. static struct seq_operations input_handlers_seq_ops = {
  477. .start = input_handlers_seq_start,
  478. .next = input_handlers_seq_next,
  479. .stop = input_handlers_seq_stop,
  480. .show = input_handlers_seq_show,
  481. };
  482. static int input_proc_handlers_open(struct inode *inode, struct file *file)
  483. {
  484. return seq_open(file, &input_handlers_seq_ops);
  485. }
  486. static const struct file_operations input_handlers_fileops = {
  487. .owner = THIS_MODULE,
  488. .open = input_proc_handlers_open,
  489. .read = seq_read,
  490. .llseek = seq_lseek,
  491. .release = seq_release,
  492. };
  493. static int __init input_proc_init(void)
  494. {
  495. struct proc_dir_entry *entry;
  496. proc_bus_input_dir = proc_mkdir("input", proc_bus);
  497. if (!proc_bus_input_dir)
  498. return -ENOMEM;
  499. proc_bus_input_dir->owner = THIS_MODULE;
  500. entry = create_proc_entry("devices", 0, proc_bus_input_dir);
  501. if (!entry)
  502. goto fail1;
  503. entry->owner = THIS_MODULE;
  504. entry->proc_fops = &input_devices_fileops;
  505. entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
  506. if (!entry)
  507. goto fail2;
  508. entry->owner = THIS_MODULE;
  509. entry->proc_fops = &input_handlers_fileops;
  510. return 0;
  511. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  512. fail1: remove_proc_entry("input", proc_bus);
  513. return -ENOMEM;
  514. }
  515. static void input_proc_exit(void)
  516. {
  517. remove_proc_entry("devices", proc_bus_input_dir);
  518. remove_proc_entry("handlers", proc_bus_input_dir);
  519. remove_proc_entry("input", proc_bus);
  520. }
  521. #else /* !CONFIG_PROC_FS */
  522. static inline void input_wakeup_procfs_readers(void) { }
  523. static inline int input_proc_init(void) { return 0; }
  524. static inline void input_proc_exit(void) { }
  525. #endif
  526. #define INPUT_DEV_STRING_ATTR_SHOW(name) \
  527. static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
  528. { \
  529. struct input_dev *input_dev = to_input_dev(dev); \
  530. \
  531. return scnprintf(buf, PAGE_SIZE, "%s\n", \
  532. input_dev->name ? input_dev->name : ""); \
  533. } \
  534. static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
  535. INPUT_DEV_STRING_ATTR_SHOW(name);
  536. INPUT_DEV_STRING_ATTR_SHOW(phys);
  537. INPUT_DEV_STRING_ATTR_SHOW(uniq);
  538. static int input_print_modalias_bits(char *buf, int size,
  539. char name, unsigned long *bm,
  540. unsigned int min_bit, unsigned int max_bit)
  541. {
  542. int len = 0, i;
  543. len += snprintf(buf, max(size, 0), "%c", name);
  544. for (i = min_bit; i < max_bit; i++)
  545. if (bm[LONG(i)] & BIT(i))
  546. len += snprintf(buf + len, max(size - len, 0), "%X,", i);
  547. return len;
  548. }
  549. static int input_print_modalias(char *buf, int size, struct input_dev *id,
  550. int add_cr)
  551. {
  552. int len;
  553. len = snprintf(buf, max(size, 0),
  554. "input:b%04Xv%04Xp%04Xe%04X-",
  555. id->id.bustype, id->id.vendor,
  556. id->id.product, id->id.version);
  557. len += input_print_modalias_bits(buf + len, size - len,
  558. 'e', id->evbit, 0, EV_MAX);
  559. len += input_print_modalias_bits(buf + len, size - len,
  560. 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
  561. len += input_print_modalias_bits(buf + len, size - len,
  562. 'r', id->relbit, 0, REL_MAX);
  563. len += input_print_modalias_bits(buf + len, size - len,
  564. 'a', id->absbit, 0, ABS_MAX);
  565. len += input_print_modalias_bits(buf + len, size - len,
  566. 'm', id->mscbit, 0, MSC_MAX);
  567. len += input_print_modalias_bits(buf + len, size - len,
  568. 'l', id->ledbit, 0, LED_MAX);
  569. len += input_print_modalias_bits(buf + len, size - len,
  570. 's', id->sndbit, 0, SND_MAX);
  571. len += input_print_modalias_bits(buf + len, size - len,
  572. 'f', id->ffbit, 0, FF_MAX);
  573. len += input_print_modalias_bits(buf + len, size - len,
  574. 'w', id->swbit, 0, SW_MAX);
  575. if (add_cr)
  576. len += snprintf(buf + len, max(size - len, 0), "\n");
  577. return len;
  578. }
  579. static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
  580. {
  581. struct input_dev *id = to_input_dev(dev);
  582. ssize_t len;
  583. len = input_print_modalias(buf, PAGE_SIZE, id, 1);
  584. return min_t(int, len, PAGE_SIZE);
  585. }
  586. static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
  587. static struct attribute *input_dev_attrs[] = {
  588. &class_device_attr_name.attr,
  589. &class_device_attr_phys.attr,
  590. &class_device_attr_uniq.attr,
  591. &class_device_attr_modalias.attr,
  592. NULL
  593. };
  594. static struct attribute_group input_dev_attr_group = {
  595. .attrs = input_dev_attrs,
  596. };
  597. #define INPUT_DEV_ID_ATTR(name) \
  598. static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
  599. { \
  600. struct input_dev *input_dev = to_input_dev(dev); \
  601. return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
  602. } \
  603. static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
  604. INPUT_DEV_ID_ATTR(bustype);
  605. INPUT_DEV_ID_ATTR(vendor);
  606. INPUT_DEV_ID_ATTR(product);
  607. INPUT_DEV_ID_ATTR(version);
  608. static struct attribute *input_dev_id_attrs[] = {
  609. &class_device_attr_bustype.attr,
  610. &class_device_attr_vendor.attr,
  611. &class_device_attr_product.attr,
  612. &class_device_attr_version.attr,
  613. NULL
  614. };
  615. static struct attribute_group input_dev_id_attr_group = {
  616. .name = "id",
  617. .attrs = input_dev_id_attrs,
  618. };
  619. static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
  620. int max, int add_cr)
  621. {
  622. int i;
  623. int len = 0;
  624. for (i = NBITS(max) - 1; i > 0; i--)
  625. if (bitmap[i])
  626. break;
  627. for (; i >= 0; i--)
  628. len += snprintf(buf + len, max(buf_size - len, 0),
  629. "%lx%s", bitmap[i], i > 0 ? " " : "");
  630. if (add_cr)
  631. len += snprintf(buf + len, max(buf_size - len, 0), "\n");
  632. return len;
  633. }
  634. #define INPUT_DEV_CAP_ATTR(ev, bm) \
  635. static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
  636. { \
  637. struct input_dev *input_dev = to_input_dev(dev); \
  638. int len = input_print_bitmap(buf, PAGE_SIZE, \
  639. input_dev->bm##bit, ev##_MAX, 1); \
  640. return min_t(int, len, PAGE_SIZE); \
  641. } \
  642. static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
  643. INPUT_DEV_CAP_ATTR(EV, ev);
  644. INPUT_DEV_CAP_ATTR(KEY, key);
  645. INPUT_DEV_CAP_ATTR(REL, rel);
  646. INPUT_DEV_CAP_ATTR(ABS, abs);
  647. INPUT_DEV_CAP_ATTR(MSC, msc);
  648. INPUT_DEV_CAP_ATTR(LED, led);
  649. INPUT_DEV_CAP_ATTR(SND, snd);
  650. INPUT_DEV_CAP_ATTR(FF, ff);
  651. INPUT_DEV_CAP_ATTR(SW, sw);
  652. static struct attribute *input_dev_caps_attrs[] = {
  653. &class_device_attr_ev.attr,
  654. &class_device_attr_key.attr,
  655. &class_device_attr_rel.attr,
  656. &class_device_attr_abs.attr,
  657. &class_device_attr_msc.attr,
  658. &class_device_attr_led.attr,
  659. &class_device_attr_snd.attr,
  660. &class_device_attr_ff.attr,
  661. &class_device_attr_sw.attr,
  662. NULL
  663. };
  664. static struct attribute_group input_dev_caps_attr_group = {
  665. .name = "capabilities",
  666. .attrs = input_dev_caps_attrs,
  667. };
  668. static struct attribute_group *input_dev_attr_groups[] = {
  669. &input_dev_attr_group,
  670. &input_dev_id_attr_group,
  671. &input_dev_caps_attr_group,
  672. NULL
  673. };
  674. static void input_dev_release(struct class_device *class_dev)
  675. {
  676. struct input_dev *dev = to_input_dev(class_dev);
  677. input_ff_destroy(dev);
  678. kfree(dev);
  679. module_put(THIS_MODULE);
  680. }
  681. /*
  682. * Input uevent interface - loading event handlers based on
  683. * device bitfields.
  684. */
  685. static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
  686. char *buffer, int buffer_size, int *cur_len,
  687. const char *name, unsigned long *bitmap, int max)
  688. {
  689. if (*cur_index >= num_envp - 1)
  690. return -ENOMEM;
  691. envp[*cur_index] = buffer + *cur_len;
  692. *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
  693. if (*cur_len >= buffer_size)
  694. return -ENOMEM;
  695. *cur_len += input_print_bitmap(buffer + *cur_len,
  696. max(buffer_size - *cur_len, 0),
  697. bitmap, max, 0) + 1;
  698. if (*cur_len > buffer_size)
  699. return -ENOMEM;
  700. (*cur_index)++;
  701. return 0;
  702. }
  703. static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
  704. char *buffer, int buffer_size, int *cur_len,
  705. struct input_dev *dev)
  706. {
  707. if (*cur_index >= num_envp - 1)
  708. return -ENOMEM;
  709. envp[*cur_index] = buffer + *cur_len;
  710. *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
  711. "MODALIAS=");
  712. if (*cur_len >= buffer_size)
  713. return -ENOMEM;
  714. *cur_len += input_print_modalias(buffer + *cur_len,
  715. max(buffer_size - *cur_len, 0),
  716. dev, 0) + 1;
  717. if (*cur_len > buffer_size)
  718. return -ENOMEM;
  719. (*cur_index)++;
  720. return 0;
  721. }
  722. #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
  723. do { \
  724. int err = add_uevent_var(envp, num_envp, &i, \
  725. buffer, buffer_size, &len, \
  726. fmt, val); \
  727. if (err) \
  728. return err; \
  729. } while (0)
  730. #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
  731. do { \
  732. int err = input_add_uevent_bm_var(envp, num_envp, &i, \
  733. buffer, buffer_size, &len, \
  734. name, bm, max); \
  735. if (err) \
  736. return err; \
  737. } while (0)
  738. #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
  739. do { \
  740. int err = input_add_uevent_modalias_var(envp, \
  741. num_envp, &i, \
  742. buffer, buffer_size, &len, \
  743. dev); \
  744. if (err) \
  745. return err; \
  746. } while (0)
  747. static int input_dev_uevent(struct class_device *cdev, char **envp,
  748. int num_envp, char *buffer, int buffer_size)
  749. {
  750. struct input_dev *dev = to_input_dev(cdev);
  751. int i = 0;
  752. int len = 0;
  753. INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
  754. dev->id.bustype, dev->id.vendor,
  755. dev->id.product, dev->id.version);
  756. if (dev->name)
  757. INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
  758. if (dev->phys)
  759. INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
  760. if (dev->uniq)
  761. INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
  762. INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
  763. if (test_bit(EV_KEY, dev->evbit))
  764. INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
  765. if (test_bit(EV_REL, dev->evbit))
  766. INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
  767. if (test_bit(EV_ABS, dev->evbit))
  768. INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
  769. if (test_bit(EV_MSC, dev->evbit))
  770. INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
  771. if (test_bit(EV_LED, dev->evbit))
  772. INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
  773. if (test_bit(EV_SND, dev->evbit))
  774. INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
  775. if (test_bit(EV_FF, dev->evbit))
  776. INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
  777. if (test_bit(EV_SW, dev->evbit))
  778. INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
  779. INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
  780. envp[i] = NULL;
  781. return 0;
  782. }
  783. struct class input_class = {
  784. .name = "input",
  785. .release = input_dev_release,
  786. .uevent = input_dev_uevent,
  787. };
  788. EXPORT_SYMBOL_GPL(input_class);
  789. /**
  790. * input_allocate_device - allocate memory for new input device
  791. *
  792. * Returns prepared struct input_dev or NULL.
  793. *
  794. * NOTE: Use input_free_device() to free devices that have not been
  795. * registered; input_unregister_device() should be used for already
  796. * registered devices.
  797. */
  798. struct input_dev *input_allocate_device(void)
  799. {
  800. struct input_dev *dev;
  801. dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  802. if (dev) {
  803. dev->cdev.class = &input_class;
  804. dev->cdev.groups = input_dev_attr_groups;
  805. class_device_initialize(&dev->cdev);
  806. mutex_init(&dev->mutex);
  807. INIT_LIST_HEAD(&dev->h_list);
  808. INIT_LIST_HEAD(&dev->node);
  809. __module_get(THIS_MODULE);
  810. }
  811. return dev;
  812. }
  813. EXPORT_SYMBOL(input_allocate_device);
  814. /**
  815. * input_free_device - free memory occupied by input_dev structure
  816. * @dev: input device to free
  817. *
  818. * This function should only be used if input_register_device()
  819. * was not called yet or if it failed. Once device was registered
  820. * use input_unregister_device() and memory will be freed once last
  821. * refrence to the device is dropped.
  822. *
  823. * Device should be allocated by input_allocate_device().
  824. *
  825. * NOTE: If there are references to the input device then memory
  826. * will not be freed until last reference is dropped.
  827. */
  828. void input_free_device(struct input_dev *dev)
  829. {
  830. if (dev)
  831. input_put_device(dev);
  832. }
  833. EXPORT_SYMBOL(input_free_device);
  834. int input_register_device(struct input_dev *dev)
  835. {
  836. static atomic_t input_no = ATOMIC_INIT(0);
  837. struct input_handler *handler;
  838. const char *path;
  839. int error;
  840. set_bit(EV_SYN, dev->evbit);
  841. /*
  842. * If delay and period are pre-set by the driver, then autorepeating
  843. * is handled by the driver itself and we don't do it in input.c.
  844. */
  845. init_timer(&dev->timer);
  846. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  847. dev->timer.data = (long) dev;
  848. dev->timer.function = input_repeat_key;
  849. dev->rep[REP_DELAY] = 250;
  850. dev->rep[REP_PERIOD] = 33;
  851. }
  852. if (!dev->getkeycode)
  853. dev->getkeycode = input_default_getkeycode;
  854. if (!dev->setkeycode)
  855. dev->setkeycode = input_default_setkeycode;
  856. list_add_tail(&dev->node, &input_dev_list);
  857. snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
  858. "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
  859. error = class_device_add(&dev->cdev);
  860. if (error)
  861. return error;
  862. path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
  863. printk(KERN_INFO "input: %s as %s\n",
  864. dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
  865. kfree(path);
  866. list_for_each_entry(handler, &input_handler_list, node)
  867. input_attach_handler(dev, handler);
  868. input_wakeup_procfs_readers();
  869. return 0;
  870. }
  871. EXPORT_SYMBOL(input_register_device);
  872. void input_unregister_device(struct input_dev *dev)
  873. {
  874. struct input_handle *handle, *next;
  875. int code;
  876. for (code = 0; code <= KEY_MAX; code++)
  877. if (test_bit(code, dev->key))
  878. input_report_key(dev, code, 0);
  879. input_sync(dev);
  880. del_timer_sync(&dev->timer);
  881. list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
  882. handle->handler->disconnect(handle);
  883. WARN_ON(!list_empty(&dev->h_list));
  884. list_del_init(&dev->node);
  885. class_device_unregister(&dev->cdev);
  886. input_wakeup_procfs_readers();
  887. }
  888. EXPORT_SYMBOL(input_unregister_device);
  889. int input_register_handler(struct input_handler *handler)
  890. {
  891. struct input_dev *dev;
  892. INIT_LIST_HEAD(&handler->h_list);
  893. if (handler->fops != NULL) {
  894. if (input_table[handler->minor >> 5])
  895. return -EBUSY;
  896. input_table[handler->minor >> 5] = handler;
  897. }
  898. list_add_tail(&handler->node, &input_handler_list);
  899. list_for_each_entry(dev, &input_dev_list, node)
  900. input_attach_handler(dev, handler);
  901. input_wakeup_procfs_readers();
  902. return 0;
  903. }
  904. EXPORT_SYMBOL(input_register_handler);
  905. void input_unregister_handler(struct input_handler *handler)
  906. {
  907. struct input_handle *handle, *next;
  908. list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
  909. handler->disconnect(handle);
  910. WARN_ON(!list_empty(&handler->h_list));
  911. list_del_init(&handler->node);
  912. if (handler->fops != NULL)
  913. input_table[handler->minor >> 5] = NULL;
  914. input_wakeup_procfs_readers();
  915. }
  916. EXPORT_SYMBOL(input_unregister_handler);
  917. int input_register_handle(struct input_handle *handle)
  918. {
  919. struct input_handler *handler = handle->handler;
  920. list_add_tail(&handle->d_node, &handle->dev->h_list);
  921. list_add_tail(&handle->h_node, &handler->h_list);
  922. if (handler->start)
  923. handler->start(handle);
  924. return 0;
  925. }
  926. EXPORT_SYMBOL(input_register_handle);
  927. void input_unregister_handle(struct input_handle *handle)
  928. {
  929. list_del_init(&handle->h_node);
  930. list_del_init(&handle->d_node);
  931. }
  932. EXPORT_SYMBOL(input_unregister_handle);
  933. static int input_open_file(struct inode *inode, struct file *file)
  934. {
  935. struct input_handler *handler = input_table[iminor(inode) >> 5];
  936. const struct file_operations *old_fops, *new_fops = NULL;
  937. int err;
  938. /* No load-on-demand here? */
  939. if (!handler || !(new_fops = fops_get(handler->fops)))
  940. return -ENODEV;
  941. /*
  942. * That's _really_ odd. Usually NULL ->open means "nothing special",
  943. * not "no device". Oh, well...
  944. */
  945. if (!new_fops->open) {
  946. fops_put(new_fops);
  947. return -ENODEV;
  948. }
  949. old_fops = file->f_op;
  950. file->f_op = new_fops;
  951. err = new_fops->open(inode, file);
  952. if (err) {
  953. fops_put(file->f_op);
  954. file->f_op = fops_get(old_fops);
  955. }
  956. fops_put(old_fops);
  957. return err;
  958. }
  959. static const struct file_operations input_fops = {
  960. .owner = THIS_MODULE,
  961. .open = input_open_file,
  962. };
  963. static int __init input_init(void)
  964. {
  965. int err;
  966. err = class_register(&input_class);
  967. if (err) {
  968. printk(KERN_ERR "input: unable to register input_dev class\n");
  969. return err;
  970. }
  971. err = input_proc_init();
  972. if (err)
  973. goto fail1;
  974. err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
  975. if (err) {
  976. printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
  977. goto fail2;
  978. }
  979. return 0;
  980. fail2: input_proc_exit();
  981. fail1: class_unregister(&input_class);
  982. return err;
  983. }
  984. static void __exit input_exit(void)
  985. {
  986. input_proc_exit();
  987. unregister_chrdev(INPUT_MAJOR, "input");
  988. class_unregister(&input_class);
  989. }
  990. subsys_initcall(input_init);
  991. module_exit(input_exit);