/* * The input core * * Copyright (c) 1999-2002 Vojtech Pavlik */ /* * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by * the Free Software Foundation. */ #include <linux/init.h>#include <linux/input.h>#include <linux/module.h>#include <linux/random.h>#include <linux/major.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/interrupt.h>#include <linux/poll.h>#include <linux/device.h>#include <linux/mutex.h> MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");MODULE_DESCRIPTION("Input core");MODULE_LICENSE("GPL"); #define INPUT_DEVICES 256 static LIST_HEAD(input_dev_list);static LIST_HEAD(input_handler_list); static struct input_handler *input_table[8]; /** * input_event() - report new input event * @dev: device that generated the event * @type: type of the event * @code: event code * @value: value of the event * * This function should be used by drivers implementing various input devices * See also input_inject_event() */void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value){ struct input_handle *handle; if (type > EV_MAX || !test_bit(type, dev->evbit)) return; add_input_randomness(type, code, value); switch (type) { case EV_SYN: switch (code) { case SYN_CONFIG: if (dev->event) dev->event(dev, type, code, value); break; case SYN_REPORT: if (dev->sync) return; dev->sync = 1; break; } break; case EV_KEY: if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) return; if (value == 2) break; change_bit(code, dev->key); if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) { dev->repeat_key = code; mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); } break; case EV_SW: if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value) return; change_bit(code, dev->sw); break; case EV_ABS: if (code > ABS_MAX || !test_bit(code, dev->absbit)) return; if (dev->absfuzz[code]) { if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) && (value < dev->abs[code] + (dev->absfuzz[code] >> 1))) return; if ((value > dev->abs[code] - dev->absfuzz[code]) && (value < dev->abs[code] + dev->absfuzz[code])) value = (dev->abs[code] * 3 + value) >> 2; if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) && (value < dev->abs[code] + (dev->absfuzz[code] << 1))) value = (dev->abs[code] + value) >> 1; } if (dev->abs[code] == value) return; dev->abs[code] = value; break; case EV_REL: if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0)) return; break; case EV_MSC: if (code > MSC_MAX || !test_bit(code, dev->mscbit)) return; if (dev->event) dev->event(dev, type, code, value); break; case EV_LED: if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value) return; change_bit(code, dev->led); if (dev->event) dev->event(dev, type, code, value); break; case EV_SND: if (code > SND_MAX || !test_bit(code, dev->sndbit)) return; if (!!test_bit(code, dev->snd) != !!value) change_bit(code, dev->snd); if (dev->event) dev->event(dev, type, code, value); break; case EV_REP: if (code > REP_MAX || value < 0 || dev->rep[code] == value) return; dev->rep[code] = value; if (dev->event) dev->event(dev, type, code, value); break; case EV_FF: if (value < 0) return; if (dev->event) dev->event(dev, type, code, value); break; } if (type != EV_SYN) dev->sync = 0; if (dev->grab) dev->grab->handler->event(dev->grab, type, code, value); else list_for_each_entry(handle, &dev->h_list, d_node) if (handle->open) handle->handler->event(handle, type, code, value);}EXPORT_SYMBOL(input_event); /** * input_inject_event() - send input event from input handler * @handle: input handle to send event through * @type: type of the event * @code: event code * @value: value of the event * * Similar to input_event() but will ignore event if device is "grabbed" and handle * injecting event is not the one that owns the device. */void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value){ if (!handle->dev->grab || handle->dev->grab == handle) input_event(handle->dev, type, code, value);}EXPORT_SYMBOL(input_inject_event); static void input_repeat_key(unsigned long data){ struct input_dev *dev = (void *) data; if (!test_bit(dev->repeat_key, dev->key)) return; input_event(dev, EV_KEY, dev->repeat_key, 2); input_sync(dev); if (dev->rep[REP_PERIOD]) mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));} int input_grab_device(struct input_handle *handle){ if (handle->dev->grab) return -EBUSY; handle->dev->grab = handle; return 0;}EXPORT_SYMBOL(input_grab_device); void input_release_device(struct input_handle *handle){ struct input_dev *dev = handle->dev; if (dev->grab == handle) { dev->grab = NULL; list_for_each_entry(handle, &dev->h_list, d_node) if (handle->handler->start) handle->handler->start(handle); }}EXPORT_SYMBOL(input_release_device); int input_open_device(struct input_handle *handle){ struct input_dev *dev = handle->dev; int err; err = mutex_lock_interruptible(&dev->mutex); if (err) return err; handle->open++; if (!dev->users++ && dev->open) err = dev->open(dev); if (err) handle->open--; mutex_unlock(&dev->mutex); return err;}EXPORT_SYMBOL(input_open_device); int input_flush_device(struct input_handle* handle, struct file* file){ if (handle->dev->flush) return handle->dev->flush(handle->dev, file); return 0;}EXPORT_SYMBOL(input_flush_device); void input_close_device(struct input_handle *handle){ struct input_dev *dev = handle->dev; input_release_device(handle); mutex_lock(&dev->mutex); if (!--dev->users && dev->close) dev->close(dev); handle->open--; mutex_unlock(&dev->mutex);}EXPORT_SYMBOL(input_close_device); static int input_fetch_keycode(struct input_dev *dev, int scancode){ switch (dev->keycodesize) { case 1: return ((u8 *)dev->keycode)[scancode]; case 2: return ((u16 *)dev->keycode)[scancode]; default: return ((u32 *)dev->keycode)[scancode]; }} static int input_default_getkeycode(struct input_dev *dev, int scancode, int *keycode){ if (!dev->keycodesize) return -EINVAL; if (scancode < 0 || scancode >= dev->keycodemax) return -EINVAL; *keycode = input_fetch_keycode(dev, scancode); return 0;} static int input_default_setkeycode(struct input_dev *dev, int scancode, int keycode){ int old_keycode; int i; if (scancode < 0 || scancode >= dev->keycodemax) return -EINVAL; if (keycode < 0 || keycode > KEY_MAX) return -EINVAL; if (!dev->keycodesize) return -EINVAL; if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8))) return -EINVAL; switch (dev->keycodesize) { case 1: { u8 *k = (u8 *)dev->keycode; old_keycode = k[scancode]; k[scancode] = keycode; break; } case 2: { u16 *k = (u16 *)dev->keycode; old_keycode = k[scancode]; k[scancode] = keycode; break; } default: { u32 *k = (u32 *)dev->keycode; old_keycode = k[scancode]; k[scancode] = keycode; break; } } clear_bit(old_keycode, dev->keybit); set_bit(keycode, dev->keybit); for (i = 0; i < dev->keycodemax; i++) { if (input_fetch_keycode(dev, i) == old_keycode) { set_bit(old_keycode, dev->keybit); break; /* Setting the bit twice is useless, so break */ } } return 0;} #define MATCH_BIT(bit, max) \ for (i = 0; i < NBITS(max); i++) \ if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ break; \ if (i != NBITS(max)) \ continue; static const struct input_device_id *input_match_device(const struct input_device_id *id, struct input_dev *dev){ int i; for (; id->flags || id->driver_info; id++) { if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) if (id->bustype != dev->id.bustype) continue; if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) if (id->vendor != dev->id.vendor) continue; if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) if (id->product != dev->id.product) continue; if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) if (id->version != dev->id.version) continue; MATCH_BIT(evbit, EV_MAX); MATCH_BIT(keybit, KEY_MAX); MATCH_BIT(relbit, REL_MAX); MATCH_BIT(absbit, ABS_MAX); MATCH_BIT(mscbit, MSC_MAX); MATCH_BIT(ledbit, LED_MAX); MATCH_BIT(sndbit, SND_MAX); MATCH_BIT(ffbit, FF_MAX); MATCH_BIT(swbit, SW_MAX); return id; } return NULL;} static int input_attach_handler(struct input_dev *dev, struct input_handler *handler){ const struct input_device_id *id; int error; if (handler->blacklist && input_match_device(handler->blacklist, dev)) return -ENODEV; id = input_match_device(handler->id_table, dev); if (!id) return -ENODEV; error = handler->connect(handler, dev, id); if (error && error != -ENODEV) printk(KERN_ERR "input: failed to attach handler %s to device %s, " "error: %d\n", handler->name, kobject_name(&dev->dev.kobj), error); return error;} #ifdef CONFIG_PROC_FS static struct proc_dir_entry *proc_bus_input_dir;static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);static int input_devices_state; static inline void input_wakeup_procfs_readers(void){ input_devices_state++; wake_up(&input_devices_poll_wait);} static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait){ int state = input_devices_state; poll_wait(file, &input_devices_poll_wait, wait); if (state != input_devices_state) return POLLIN | POLLRDNORM; return 0;} static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos){ /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ return seq_list_start(&input_dev_list, *pos);} static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos){ return seq_list_next(v, &input_dev_list, pos);} static void input_devices_seq_stop(struct seq_file *seq, void *v){ /* release lock here */} static void input_seq_print_bitmap(struct seq_file *seq, const char *name, unsigned long *bitmap, int max){ int i; for (i = NBITS(max) - 1; i > 0; i--) if (bitmap[i]) break; seq_printf(seq, "B: %s=", name);Lines 1–500 of 1305. Load more
/* * The input core * * Copyright (c) 1999-2002 Vojtech Pavlik */ /* * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by * the Free Software Foundation. */ #include <linux/init.h> #include <linux/input.h> #include <linux/module.h> #include <linux/random.h> #include <linux/major.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/interrupt.h> #include <linux/poll.h> #include <linux/device.h> #include <linux/mutex.h> MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); MODULE_DESCRIPTION("Input core"); MODULE_LICENSE("GPL"); #define INPUT_DEVICES 256 static LIST_HEAD(input_dev_list); static LIST_HEAD(input_handler_list); static struct input_handler *input_table[8]; /** * input_event() - report new input event * @dev: device that generated the event * @type: type of the event * @code: event code * @value: value of the event * * This function should be used by drivers implementing various input devices * See also input_inject_event() */ void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) { struct input_handle *handle; if (type > EV_MAX || !test_bit(type, dev->evbit)) return; add_input_randomness(type, code, value); switch (type) { case EV_SYN: switch (code) { case SYN_CONFIG: if (dev->event) dev->event(dev, type, code, value); break; case SYN_REPORT: if (dev->sync) return; dev->sync = 1; break; } break; case EV_KEY: if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) return; if (value == 2) break; change_bit(code, dev->key); if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) { dev->repeat_key = code; mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); } break; case EV_SW: if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value) return; change_bit(code, dev->sw); break; case EV_ABS: if (code > ABS_MAX || !test_bit(code, dev->absbit)) return; if (dev->absfuzz[code]) { if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) && (value < dev->abs[code] + (dev->absfuzz[code] >> 1))) return; if ((value > dev->abs[code] - dev->absfuzz[code]) && (value < dev->abs[code] + dev->absfuzz[code])) value = (dev->abs[code] * 3 + value) >> 2; if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) && (value < dev->abs[code] + (dev->absfuzz[code] << 1))) value = (dev->abs[code] + value) >> 1; } if (dev->abs[code] == value) return; dev->abs[code] = value; break; case EV_REL: if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0)) return; break; case EV_MSC: if (code > MSC_MAX || !test_bit(code, dev->mscbit)) return; if (dev->event) dev->event(dev, type, code, value); break; case EV_LED: if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value) return; change_bit(code, dev->led); if (dev->event) dev->event(dev, type, code, value); break; case EV_SND: if (code > SND_MAX || !test_bit(code, dev->sndbit)) return; if (!!test_bit(code, dev->snd) != !!value) change_bit(code, dev->snd); if (dev->event) dev->event(dev, type, code, value); break; case EV_REP: if (code > REP_MAX || value < 0 || dev->rep[code] == value) return; dev->rep[code] = value; if (dev->event) dev->event(dev, type, code, value); break; case EV_FF: if (value < 0) return; if (dev->event) dev->event(dev, type, code, value); break; } if (type != EV_SYN) dev->sync = 0; if (dev->grab) dev->grab->handler->event(dev->grab, type, code, value); else list_for_each_entry(handle, &dev->h_list, d_node) if (handle->open) handle->handler->event(handle, type, code, value); } EXPORT_SYMBOL(input_event); /** * input_inject_event() - send input event from input handler * @handle: input handle to send event through * @type: type of the event * @code: event code * @value: value of the event * * Similar to input_event() but will ignore event if device is "grabbed" and handle * injecting event is not the one that owns the device. */ void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) { if (!handle->dev->grab || handle->dev->grab == handle) input_event(handle->dev, type, code, value); } EXPORT_SYMBOL(input_inject_event); static void input_repeat_key(unsigned long data) { struct input_dev *dev = (void *) data; if (!test_bit(dev->repeat_key, dev->key)) return; input_event(dev, EV_KEY, dev->repeat_key, 2); input_sync(dev); if (dev->rep[REP_PERIOD]) mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD])); } int input_grab_device(struct input_handle *handle) { if (handle->dev->grab) return -EBUSY; handle->dev->grab = handle; return 0; } EXPORT_SYMBOL(input_grab_device); void input_release_device(struct input_handle *handle) { struct input_dev *dev = handle->dev; if (dev->grab == handle) { dev->grab = NULL; list_for_each_entry(handle, &dev->h_list, d_node) if (handle->handler->start) handle->handler->start(handle); } } EXPORT_SYMBOL(input_release_device); int input_open_device(struct input_handle *handle) { struct input_dev *dev = handle->dev; int err; err = mutex_lock_interruptible(&dev->mutex); if (err) return err; handle->open++; if (!dev->users++ && dev->open) err = dev->open(dev); if (err) handle->open--; mutex_unlock(&dev->mutex); return err; } EXPORT_SYMBOL(input_open_device); int input_flush_device(struct input_handle* handle, struct file* file) { if (handle->dev->flush) return handle->dev->flush(handle->dev, file); return 0; } EXPORT_SYMBOL(input_flush_device); void input_close_device(struct input_handle *handle) { struct input_dev *dev = handle->dev; input_release_device(handle); mutex_lock(&dev->mutex); if (!--dev->users && dev->close) dev->close(dev); handle->open--; mutex_unlock(&dev->mutex); } EXPORT_SYMBOL(input_close_device); static int input_fetch_keycode(struct input_dev *dev, int scancode) { switch (dev->keycodesize) { case 1: return ((u8 *)dev->keycode)[scancode]; case 2: return ((u16 *)dev->keycode)[scancode]; default: return ((u32 *)dev->keycode)[scancode]; } } static int input_default_getkeycode(struct input_dev *dev, int scancode, int *keycode) { if (!dev->keycodesize) return -EINVAL; if (scancode < 0 || scancode >= dev->keycodemax) return -EINVAL; *keycode = input_fetch_keycode(dev, scancode); return 0; } static int input_default_setkeycode(struct input_dev *dev, int scancode, int keycode) { int old_keycode; int i; if (scancode < 0 || scancode >= dev->keycodemax) return -EINVAL; if (keycode < 0 || keycode > KEY_MAX) return -EINVAL; if (!dev->keycodesize) return -EINVAL; if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8))) return -EINVAL; switch (dev->keycodesize) { case 1: { u8 *k = (u8 *)dev->keycode; old_keycode = k[scancode]; k[scancode] = keycode; break; } case 2: { u16 *k = (u16 *)dev->keycode; old_keycode = k[scancode]; k[scancode] = keycode; break; } default: { u32 *k = (u32 *)dev->keycode; old_keycode = k[scancode]; k[scancode] = keycode; break; } } clear_bit(old_keycode, dev->keybit); set_bit(keycode, dev->keybit); for (i = 0; i < dev->keycodemax; i++) { if (input_fetch_keycode(dev, i) == old_keycode) { set_bit(old_keycode, dev->keybit); break; /* Setting the bit twice is useless, so break */ } } return 0; } #define MATCH_BIT(bit, max) \ for (i = 0; i < NBITS(max); i++) \ if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ break; \ if (i != NBITS(max)) \ continue; static const struct input_device_id *input_match_device(const struct input_device_id *id, struct input_dev *dev) { int i; for (; id->flags || id->driver_info; id++) { if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) if (id->bustype != dev->id.bustype) continue; if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) if (id->vendor != dev->id.vendor) continue; if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) if (id->product != dev->id.product) continue; if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) if (id->version != dev->id.version) continue; MATCH_BIT(evbit, EV_MAX); MATCH_BIT(keybit, KEY_MAX); MATCH_BIT(relbit, REL_MAX); MATCH_BIT(absbit, ABS_MAX); MATCH_BIT(mscbit, MSC_MAX); MATCH_BIT(ledbit, LED_MAX); MATCH_BIT(sndbit, SND_MAX); MATCH_BIT(ffbit, FF_MAX); MATCH_BIT(swbit, SW_MAX); return id; } return NULL; } static int input_attach_handler(struct input_dev *dev, struct input_handler *handler) { const struct input_device_id *id; int error; if (handler->blacklist && input_match_device(handler->blacklist, dev)) return -ENODEV; id = input_match_device(handler->id_table, dev); if (!id) return -ENODEV; error = handler->connect(handler, dev, id); if (error && error != -ENODEV) printk(KERN_ERR "input: failed to attach handler %s to device %s, " "error: %d\n", handler->name, kobject_name(&dev->dev.kobj), error); return error; } #ifdef CONFIG_PROC_FS static struct proc_dir_entry *proc_bus_input_dir; static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait); static int input_devices_state; static inline void input_wakeup_procfs_readers(void) { input_devices_state++; wake_up(&input_devices_poll_wait); } static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait) { int state = input_devices_state; poll_wait(file, &input_devices_poll_wait, wait); if (state != input_devices_state) return POLLIN | POLLRDNORM; return 0; } static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos) { /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ return seq_list_start(&input_dev_list, *pos); } static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos) { return seq_list_next(v, &input_dev_list, pos); } static void input_devices_seq_stop(struct seq_file *seq, void *v) { /* release lock here */ } static void input_seq_print_bitmap(struct seq_file *seq, const char *name, unsigned long *bitmap, int max) { int i; for (i = NBITS(max) - 1; i > 0; i--) if (bitmap[i]) break; seq_printf(seq, "B: %s=", name);