input.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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/sched.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/input.h>
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/major.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/kobject_uevent.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/poll.h>
  22. #include <linux/device.h>
  23. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  24. MODULE_DESCRIPTION("Input core");
  25. MODULE_LICENSE("GPL");
  26. EXPORT_SYMBOL(input_allocate_device);
  27. EXPORT_SYMBOL(input_register_device);
  28. EXPORT_SYMBOL(input_unregister_device);
  29. EXPORT_SYMBOL(input_register_handler);
  30. EXPORT_SYMBOL(input_unregister_handler);
  31. EXPORT_SYMBOL(input_grab_device);
  32. EXPORT_SYMBOL(input_release_device);
  33. EXPORT_SYMBOL(input_open_device);
  34. EXPORT_SYMBOL(input_close_device);
  35. EXPORT_SYMBOL(input_accept_process);
  36. EXPORT_SYMBOL(input_flush_device);
  37. EXPORT_SYMBOL(input_event);
  38. EXPORT_SYMBOL_GPL(input_class);
  39. #define INPUT_DEVICES 256
  40. static LIST_HEAD(input_dev_list);
  41. static LIST_HEAD(input_handler_list);
  42. static struct input_handler *input_table[8];
  43. void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  44. {
  45. struct input_handle *handle;
  46. if (type > EV_MAX || !test_bit(type, dev->evbit))
  47. return;
  48. add_input_randomness(type, code, value);
  49. switch (type) {
  50. case EV_SYN:
  51. switch (code) {
  52. case SYN_CONFIG:
  53. if (dev->event) dev->event(dev, type, code, value);
  54. break;
  55. case SYN_REPORT:
  56. if (dev->sync) return;
  57. dev->sync = 1;
  58. break;
  59. }
  60. break;
  61. case EV_KEY:
  62. if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
  63. return;
  64. if (value == 2)
  65. break;
  66. change_bit(code, dev->key);
  67. if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
  68. dev->repeat_key = code;
  69. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
  70. }
  71. break;
  72. case EV_SW:
  73. if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
  74. return;
  75. change_bit(code, dev->sw);
  76. break;
  77. case EV_ABS:
  78. if (code > ABS_MAX || !test_bit(code, dev->absbit))
  79. return;
  80. if (dev->absfuzz[code]) {
  81. if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
  82. (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
  83. return;
  84. if ((value > dev->abs[code] - dev->absfuzz[code]) &&
  85. (value < dev->abs[code] + dev->absfuzz[code]))
  86. value = (dev->abs[code] * 3 + value) >> 2;
  87. if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
  88. (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
  89. value = (dev->abs[code] + value) >> 1;
  90. }
  91. if (dev->abs[code] == value)
  92. return;
  93. dev->abs[code] = value;
  94. break;
  95. case EV_REL:
  96. if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
  97. return;
  98. break;
  99. case EV_MSC:
  100. if (code > MSC_MAX || !test_bit(code, dev->mscbit))
  101. return;
  102. if (dev->event) 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) 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 (dev->event) dev->event(dev, type, code, value);
  114. break;
  115. case EV_REP:
  116. if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
  117. dev->rep[code] = value;
  118. if (dev->event) dev->event(dev, type, code, value);
  119. break;
  120. case EV_FF:
  121. if (dev->event) dev->event(dev, type, code, value);
  122. break;
  123. }
  124. if (type != EV_SYN)
  125. dev->sync = 0;
  126. if (dev->grab)
  127. dev->grab->handler->event(dev->grab, type, code, value);
  128. else
  129. list_for_each_entry(handle, &dev->h_list, d_node)
  130. if (handle->open)
  131. handle->handler->event(handle, type, code, value);
  132. }
  133. static void input_repeat_key(unsigned long data)
  134. {
  135. struct input_dev *dev = (void *) data;
  136. if (!test_bit(dev->repeat_key, dev->key))
  137. return;
  138. input_event(dev, EV_KEY, dev->repeat_key, 2);
  139. input_sync(dev);
  140. if (dev->rep[REP_PERIOD])
  141. mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
  142. }
  143. int input_accept_process(struct input_handle *handle, struct file *file)
  144. {
  145. if (handle->dev->accept)
  146. return handle->dev->accept(handle->dev, file);
  147. return 0;
  148. }
  149. int input_grab_device(struct input_handle *handle)
  150. {
  151. if (handle->dev->grab)
  152. return -EBUSY;
  153. handle->dev->grab = handle;
  154. return 0;
  155. }
  156. void input_release_device(struct input_handle *handle)
  157. {
  158. if (handle->dev->grab == handle)
  159. handle->dev->grab = NULL;
  160. }
  161. int input_open_device(struct input_handle *handle)
  162. {
  163. struct input_dev *dev = handle->dev;
  164. int err;
  165. err = down_interruptible(&dev->sem);
  166. if (err)
  167. return err;
  168. handle->open++;
  169. if (!dev->users++ && dev->open)
  170. err = dev->open(dev);
  171. if (err)
  172. handle->open--;
  173. up(&dev->sem);
  174. return err;
  175. }
  176. int input_flush_device(struct input_handle* handle, struct file* file)
  177. {
  178. if (handle->dev->flush)
  179. return handle->dev->flush(handle->dev, file);
  180. return 0;
  181. }
  182. void input_close_device(struct input_handle *handle)
  183. {
  184. struct input_dev *dev = handle->dev;
  185. input_release_device(handle);
  186. down(&dev->sem);
  187. if (!--dev->users && dev->close)
  188. dev->close(dev);
  189. handle->open--;
  190. up(&dev->sem);
  191. }
  192. static void input_link_handle(struct input_handle *handle)
  193. {
  194. list_add_tail(&handle->d_node, &handle->dev->h_list);
  195. list_add_tail(&handle->h_node, &handle->handler->h_list);
  196. }
  197. #define MATCH_BIT(bit, max) \
  198. for (i = 0; i < NBITS(max); i++) \
  199. if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
  200. break; \
  201. if (i != NBITS(max)) \
  202. continue;
  203. static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
  204. {
  205. int i;
  206. for (; id->flags || id->driver_info; id++) {
  207. if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  208. if (id->id.bustype != dev->id.bustype)
  209. continue;
  210. if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  211. if (id->id.vendor != dev->id.vendor)
  212. continue;
  213. if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  214. if (id->id.product != dev->id.product)
  215. continue;
  216. if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  217. if (id->id.version != dev->id.version)
  218. continue;
  219. MATCH_BIT(evbit, EV_MAX);
  220. MATCH_BIT(keybit, KEY_MAX);
  221. MATCH_BIT(relbit, REL_MAX);
  222. MATCH_BIT(absbit, ABS_MAX);
  223. MATCH_BIT(mscbit, MSC_MAX);
  224. MATCH_BIT(ledbit, LED_MAX);
  225. MATCH_BIT(sndbit, SND_MAX);
  226. MATCH_BIT(ffbit, FF_MAX);
  227. MATCH_BIT(swbit, SW_MAX);
  228. return id;
  229. }
  230. return NULL;
  231. }
  232. static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap, int max)
  233. {
  234. int i;
  235. int len = 0;
  236. for (i = NBITS(max) - 1; i > 0; i--)
  237. if (bitmap[i])
  238. break;
  239. for (; i >= 0; i--)
  240. len += snprintf(buf + len, max(buf_size - len, 0),
  241. "%lx%s", bitmap[i], i > 0 ? " " : "");
  242. return len;
  243. }
  244. #ifdef CONFIG_PROC_FS
  245. static struct proc_dir_entry *proc_bus_input_dir;
  246. static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
  247. static int input_devices_state;
  248. static inline void input_wakeup_procfs_readers(void)
  249. {
  250. input_devices_state++;
  251. wake_up(&input_devices_poll_wait);
  252. }
  253. static unsigned int input_devices_poll(struct file *file, poll_table *wait)
  254. {
  255. int state = input_devices_state;
  256. poll_wait(file, &input_devices_poll_wait, wait);
  257. if (state != input_devices_state)
  258. return POLLIN | POLLRDNORM;
  259. return 0;
  260. }
  261. #define SPRINTF_BIT(ev, bm) \
  262. do { \
  263. len += sprintf(buf + len, "B: %s=", #ev); \
  264. len += input_print_bitmap(buf + len, INT_MAX, \
  265. dev->bm##bit, ev##_MAX); \
  266. len += sprintf(buf + len, "\n"); \
  267. } while (0)
  268. #define TEST_AND_SPRINTF_BIT(ev, bm) \
  269. do { \
  270. if (test_bit(EV_##ev, dev->evbit)) \
  271. SPRINTF_BIT(ev, bm); \
  272. } while (0)
  273. static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
  274. {
  275. struct input_dev *dev;
  276. struct input_handle *handle;
  277. const char *path;
  278. off_t at = 0;
  279. int len, cnt = 0;
  280. list_for_each_entry(dev, &input_dev_list, node) {
  281. path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
  282. len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
  283. dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
  284. len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
  285. len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
  286. len += sprintf(buf + len, "S: Sysfs=%s\n", path ? path : "");
  287. len += sprintf(buf + len, "H: Handlers=");
  288. list_for_each_entry(handle, &dev->h_list, d_node)
  289. len += sprintf(buf + len, "%s ", handle->name);
  290. len += sprintf(buf + len, "\n");
  291. SPRINTF_BIT(EV, ev);
  292. TEST_AND_SPRINTF_BIT(KEY, key);
  293. TEST_AND_SPRINTF_BIT(REL, rel);
  294. TEST_AND_SPRINTF_BIT(ABS, abs);
  295. TEST_AND_SPRINTF_BIT(MSC, msc);
  296. TEST_AND_SPRINTF_BIT(LED, led);
  297. TEST_AND_SPRINTF_BIT(SND, snd);
  298. TEST_AND_SPRINTF_BIT(FF, ff);
  299. TEST_AND_SPRINTF_BIT(SW, sw);
  300. len += sprintf(buf + len, "\n");
  301. at += len;
  302. if (at >= pos) {
  303. if (!*start) {
  304. *start = buf + (pos - (at - len));
  305. cnt = at - pos;
  306. } else cnt += len;
  307. buf += len;
  308. if (cnt >= count)
  309. break;
  310. }
  311. kfree(path);
  312. }
  313. if (&dev->node == &input_dev_list)
  314. *eof = 1;
  315. return (count > cnt) ? cnt : count;
  316. }
  317. static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
  318. {
  319. struct input_handler *handler;
  320. off_t at = 0;
  321. int len = 0, cnt = 0;
  322. int i = 0;
  323. list_for_each_entry(handler, &input_handler_list, node) {
  324. if (handler->fops)
  325. len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
  326. i++, handler->name, handler->minor);
  327. else
  328. len = sprintf(buf, "N: Number=%d Name=%s\n",
  329. i++, handler->name);
  330. at += len;
  331. if (at >= pos) {
  332. if (!*start) {
  333. *start = buf + (pos - (at - len));
  334. cnt = at - pos;
  335. } else cnt += len;
  336. buf += len;
  337. if (cnt >= count)
  338. break;
  339. }
  340. }
  341. if (&handler->node == &input_handler_list)
  342. *eof = 1;
  343. return (count > cnt) ? cnt : count;
  344. }
  345. static struct file_operations input_fileops;
  346. static int __init input_proc_init(void)
  347. {
  348. struct proc_dir_entry *entry;
  349. proc_bus_input_dir = proc_mkdir("input", proc_bus);
  350. if (!proc_bus_input_dir)
  351. return -ENOMEM;
  352. proc_bus_input_dir->owner = THIS_MODULE;
  353. entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
  354. if (!entry)
  355. goto fail1;
  356. entry->owner = THIS_MODULE;
  357. input_fileops = *entry->proc_fops;
  358. entry->proc_fops = &input_fileops;
  359. entry->proc_fops->poll = input_devices_poll;
  360. entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
  361. if (!entry)
  362. goto fail2;
  363. entry->owner = THIS_MODULE;
  364. return 0;
  365. fail2: remove_proc_entry("devices", proc_bus_input_dir);
  366. fail1: remove_proc_entry("input", proc_bus);
  367. return -ENOMEM;
  368. }
  369. static void input_proc_exit(void)
  370. {
  371. remove_proc_entry("devices", proc_bus_input_dir);
  372. remove_proc_entry("handlers", proc_bus_input_dir);
  373. remove_proc_entry("input", proc_bus);
  374. }
  375. #else /* !CONFIG_PROC_FS */
  376. static inline void input_wakeup_procfs_readers(void) { }
  377. static inline int input_proc_init(void) { return 0; }
  378. static inline void input_proc_exit(void) { }
  379. #endif
  380. #define INPUT_DEV_STRING_ATTR_SHOW(name) \
  381. static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
  382. { \
  383. struct input_dev *input_dev = to_input_dev(dev); \
  384. int retval; \
  385. \
  386. retval = down_interruptible(&input_dev->sem); \
  387. if (retval) \
  388. return retval; \
  389. \
  390. retval = sprintf(buf, "%s\n", input_dev->name ? input_dev->name : ""); \
  391. \
  392. up(&input_dev->sem); \
  393. \
  394. return retval; \
  395. } \
  396. static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
  397. INPUT_DEV_STRING_ATTR_SHOW(name);
  398. INPUT_DEV_STRING_ATTR_SHOW(phys);
  399. INPUT_DEV_STRING_ATTR_SHOW(uniq);
  400. static struct attribute *input_dev_attrs[] = {
  401. &class_device_attr_name.attr,
  402. &class_device_attr_phys.attr,
  403. &class_device_attr_uniq.attr,
  404. NULL
  405. };
  406. static struct attribute_group input_dev_attr_group = {
  407. .attrs = input_dev_attrs,
  408. };
  409. #define INPUT_DEV_ID_ATTR(name) \
  410. static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
  411. { \
  412. struct input_dev *input_dev = to_input_dev(dev); \
  413. return sprintf(buf, "%04x\n", input_dev->id.name); \
  414. } \
  415. static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
  416. INPUT_DEV_ID_ATTR(bustype);
  417. INPUT_DEV_ID_ATTR(vendor);
  418. INPUT_DEV_ID_ATTR(product);
  419. INPUT_DEV_ID_ATTR(version);
  420. static struct attribute *input_dev_id_attrs[] = {
  421. &class_device_attr_bustype.attr,
  422. &class_device_attr_vendor.attr,
  423. &class_device_attr_product.attr,
  424. &class_device_attr_version.attr,
  425. NULL
  426. };
  427. static struct attribute_group input_dev_id_attr_group = {
  428. .name = "id",
  429. .attrs = input_dev_id_attrs,
  430. };
  431. #define INPUT_DEV_CAP_ATTR(ev, bm) \
  432. static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
  433. { \
  434. struct input_dev *input_dev = to_input_dev(dev); \
  435. return input_print_bitmap(buf, PAGE_SIZE, input_dev->bm##bit, ev##_MAX);\
  436. } \
  437. static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
  438. INPUT_DEV_CAP_ATTR(EV, ev);
  439. INPUT_DEV_CAP_ATTR(KEY, key);
  440. INPUT_DEV_CAP_ATTR(REL, rel);
  441. INPUT_DEV_CAP_ATTR(ABS, abs);
  442. INPUT_DEV_CAP_ATTR(MSC, msc);
  443. INPUT_DEV_CAP_ATTR(LED, led);
  444. INPUT_DEV_CAP_ATTR(SND, snd);
  445. INPUT_DEV_CAP_ATTR(FF, ff);
  446. INPUT_DEV_CAP_ATTR(SW, sw);
  447. static struct attribute *input_dev_caps_attrs[] = {
  448. &class_device_attr_ev.attr,
  449. &class_device_attr_key.attr,
  450. &class_device_attr_rel.attr,
  451. &class_device_attr_abs.attr,
  452. &class_device_attr_msc.attr,
  453. &class_device_attr_led.attr,
  454. &class_device_attr_snd.attr,
  455. &class_device_attr_ff.attr,
  456. &class_device_attr_sw.attr,
  457. NULL
  458. };
  459. static struct attribute_group input_dev_caps_attr_group = {
  460. .name = "capabilities",
  461. .attrs = input_dev_caps_attrs,
  462. };
  463. static void input_dev_release(struct class_device *class_dev)
  464. {
  465. struct input_dev *dev = to_input_dev(class_dev);
  466. kfree(dev);
  467. module_put(THIS_MODULE);
  468. }
  469. /*
  470. * Input hotplugging interface - loading event handlers based on
  471. * device bitfields.
  472. */
  473. static int input_add_hotplug_bm_var(char **envp, int num_envp, int *cur_index,
  474. char *buffer, int buffer_size, int *cur_len,
  475. const char *name, unsigned long *bitmap, int max)
  476. {
  477. if (*cur_index >= num_envp - 1)
  478. return -ENOMEM;
  479. envp[*cur_index] = buffer + *cur_len;
  480. *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
  481. if (*cur_len > buffer_size)
  482. return -ENOMEM;
  483. *cur_len += input_print_bitmap(buffer + *cur_len,
  484. max(buffer_size - *cur_len, 0),
  485. bitmap, max) + 1;
  486. if (*cur_len > buffer_size)
  487. return -ENOMEM;
  488. (*cur_index)++;
  489. return 0;
  490. }
  491. #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
  492. do { \
  493. int err = add_hotplug_env_var(envp, num_envp, &i, \
  494. buffer, buffer_size, &len, \
  495. fmt, val); \
  496. if (err) \
  497. return err; \
  498. } while (0)
  499. #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
  500. do { \
  501. int err = input_add_hotplug_bm_var(envp, num_envp, &i, \
  502. buffer, buffer_size, &len, \
  503. name, bm, max); \
  504. if (err) \
  505. return err; \
  506. } while (0)
  507. static int input_dev_hotplug(struct class_device *cdev, char **envp,
  508. int num_envp, char *buffer, int buffer_size)
  509. {
  510. struct input_dev *dev = to_input_dev(cdev);
  511. int i = 0;
  512. int len = 0;
  513. INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
  514. dev->id.bustype, dev->id.vendor,
  515. dev->id.product, dev->id.version);
  516. if (dev->name)
  517. INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
  518. if (dev->phys)
  519. INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
  520. if (dev->uniq)
  521. INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
  522. INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
  523. if (test_bit(EV_KEY, dev->evbit))
  524. INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
  525. if (test_bit(EV_REL, dev->evbit))
  526. INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
  527. if (test_bit(EV_ABS, dev->evbit))
  528. INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
  529. if (test_bit(EV_MSC, dev->evbit))
  530. INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
  531. if (test_bit(EV_LED, dev->evbit))
  532. INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
  533. if (test_bit(EV_SND, dev->evbit))
  534. INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
  535. if (test_bit(EV_FF, dev->evbit))
  536. INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
  537. if (test_bit(EV_SW, dev->evbit))
  538. INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
  539. envp[i] = NULL;
  540. return 0;
  541. }
  542. struct class input_class = {
  543. .name = "input",
  544. .release = input_dev_release,
  545. .hotplug = input_dev_hotplug,
  546. };
  547. struct input_dev *input_allocate_device(void)
  548. {
  549. struct input_dev *dev;
  550. dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
  551. if (dev) {
  552. dev->dynalloc = 1;
  553. dev->cdev.class = &input_class;
  554. class_device_initialize(&dev->cdev);
  555. INIT_LIST_HEAD(&dev->h_list);
  556. INIT_LIST_HEAD(&dev->node);
  557. }
  558. return dev;
  559. }
  560. int input_register_device(struct input_dev *dev)
  561. {
  562. static atomic_t input_no = ATOMIC_INIT(0);
  563. struct input_handle *handle;
  564. struct input_handler *handler;
  565. struct input_device_id *id;
  566. const char *path;
  567. int error;
  568. if (!dev->dynalloc) {
  569. printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
  570. "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
  571. dev->name ? dev->name : "<Unknown>");
  572. return -EINVAL;
  573. }
  574. init_MUTEX(&dev->sem);
  575. set_bit(EV_SYN, dev->evbit);
  576. /*
  577. * If delay and period are pre-set by the driver, then autorepeating
  578. * is handled by the driver itself and we don't do it in input.c.
  579. */
  580. init_timer(&dev->timer);
  581. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  582. dev->timer.data = (long) dev;
  583. dev->timer.function = input_repeat_key;
  584. dev->rep[REP_DELAY] = 250;
  585. dev->rep[REP_PERIOD] = 33;
  586. }
  587. INIT_LIST_HEAD(&dev->h_list);
  588. list_add_tail(&dev->node, &input_dev_list);
  589. dev->cdev.class = &input_class;
  590. snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
  591. "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
  592. error = class_device_add(&dev->cdev);
  593. if (error)
  594. return error;
  595. error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
  596. if (error)
  597. goto fail1;
  598. error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
  599. if (error)
  600. goto fail2;
  601. error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
  602. if (error)
  603. goto fail3;
  604. __module_get(THIS_MODULE);
  605. path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
  606. printk(KERN_INFO "input: %s as %s\n",
  607. dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
  608. kfree(path);
  609. list_for_each_entry(handler, &input_handler_list, node)
  610. if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
  611. if ((id = input_match_device(handler->id_table, dev)))
  612. if ((handle = handler->connect(handler, dev, id)))
  613. input_link_handle(handle);
  614. input_wakeup_procfs_readers();
  615. return 0;
  616. fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
  617. fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
  618. fail1: class_device_del(&dev->cdev);
  619. return error;
  620. }
  621. void input_unregister_device(struct input_dev *dev)
  622. {
  623. struct list_head * node, * next;
  624. if (!dev) return;
  625. del_timer_sync(&dev->timer);
  626. list_for_each_safe(node, next, &dev->h_list) {
  627. struct input_handle * handle = to_handle(node);
  628. list_del_init(&handle->d_node);
  629. list_del_init(&handle->h_node);
  630. handle->handler->disconnect(handle);
  631. }
  632. list_del_init(&dev->node);
  633. sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
  634. sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
  635. sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
  636. class_device_unregister(&dev->cdev);
  637. input_wakeup_procfs_readers();
  638. }
  639. void input_register_handler(struct input_handler *handler)
  640. {
  641. struct input_dev *dev;
  642. struct input_handle *handle;
  643. struct input_device_id *id;
  644. if (!handler) return;
  645. INIT_LIST_HEAD(&handler->h_list);
  646. if (handler->fops != NULL)
  647. input_table[handler->minor >> 5] = handler;
  648. list_add_tail(&handler->node, &input_handler_list);
  649. list_for_each_entry(dev, &input_dev_list, node)
  650. if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
  651. if ((id = input_match_device(handler->id_table, dev)))
  652. if ((handle = handler->connect(handler, dev, id)))
  653. input_link_handle(handle);
  654. input_wakeup_procfs_readers();
  655. }
  656. void input_unregister_handler(struct input_handler *handler)
  657. {
  658. struct list_head * node, * next;
  659. list_for_each_safe(node, next, &handler->h_list) {
  660. struct input_handle * handle = to_handle_h(node);
  661. list_del_init(&handle->h_node);
  662. list_del_init(&handle->d_node);
  663. handler->disconnect(handle);
  664. }
  665. list_del_init(&handler->node);
  666. if (handler->fops != NULL)
  667. input_table[handler->minor >> 5] = NULL;
  668. input_wakeup_procfs_readers();
  669. }
  670. static int input_open_file(struct inode *inode, struct file *file)
  671. {
  672. struct input_handler *handler = input_table[iminor(inode) >> 5];
  673. struct file_operations *old_fops, *new_fops = NULL;
  674. int err;
  675. /* No load-on-demand here? */
  676. if (!handler || !(new_fops = fops_get(handler->fops)))
  677. return -ENODEV;
  678. /*
  679. * That's _really_ odd. Usually NULL ->open means "nothing special",
  680. * not "no device". Oh, well...
  681. */
  682. if (!new_fops->open) {
  683. fops_put(new_fops);
  684. return -ENODEV;
  685. }
  686. old_fops = file->f_op;
  687. file->f_op = new_fops;
  688. err = new_fops->open(inode, file);
  689. if (err) {
  690. fops_put(file->f_op);
  691. file->f_op = fops_get(old_fops);
  692. }
  693. fops_put(old_fops);
  694. return err;
  695. }
  696. static struct file_operations input_fops = {
  697. .owner = THIS_MODULE,
  698. .open = input_open_file,
  699. };
  700. static int __init input_init(void)
  701. {
  702. int err;
  703. err = class_register(&input_class);
  704. if (err) {
  705. printk(KERN_ERR "input: unable to register input_dev class\n");
  706. return err;
  707. }
  708. err = input_proc_init();
  709. if (err)
  710. goto fail1;
  711. err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
  712. if (err) {
  713. printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
  714. goto fail2;
  715. }
  716. return 0;
  717. fail2: input_proc_exit();
  718. fail1: class_unregister(&input_class);
  719. return err;
  720. }
  721. static void __exit input_exit(void)
  722. {
  723. input_proc_exit();
  724. unregister_chrdev(INPUT_MAJOR, "input");
  725. class_unregister(&input_class);
  726. }
  727. subsys_initcall(input_init);
  728. module_exit(input_exit);