Skip to content

Commit

Permalink
Input: rework handle creation code
Browse files Browse the repository at this point in the history
 - consolidate code for binding handlers to a device
 - return error codes from handlers connect() methods back to input
   core and log failures

Signed-off-by: Dmitry Torokhov <[email protected]>
  • Loading branch information
Dmitry Torokhov committed Apr 12, 2007
1 parent 6e78258 commit 5b2a082
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 125 deletions.
28 changes: 20 additions & 8 deletions drivers/char/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include <linux/input.h>
#include <linux/reboot.h>

static void kbd_disconnect(struct input_handle *handle);
extern void ctrl_alt_del(void);

/*
Expand Down Expand Up @@ -1260,36 +1259,49 @@ static void kbd_event(struct input_handle *handle, unsigned int event_type,
* likes it, it can open it and get events from it. In this (kbd_connect)
* function, we should decide which VT to bind that keyboard to initially.
*/
static struct input_handle *kbd_connect(struct input_handler *handler,
struct input_dev *dev,
const struct input_device_id *id)
static int kbd_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
{
struct input_handle *handle;
int error;
int i;

for (i = KEY_RESERVED; i < BTN_MISC; i++)
if (test_bit(i, dev->keybit))
break;

if (i == BTN_MISC && !test_bit(EV_SND, dev->evbit))
return NULL;
return -ENODEV;

handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
if (!handle)
return NULL;
return -ENOMEM;

handle->dev = dev;
handle->handler = handler;
handle->name = "kbd";

input_open_device(handle);
error = input_register_handle(handle);
if (error)
goto err_free_handle;

error = input_open_device(handle);
if (error)
goto err_unregister_handle;

return 0;

return handle;
err_unregister_handle:
input_unregister_handle(handle);
err_free_handle:
kfree(handle);
return error;
}

static void kbd_disconnect(struct input_handle *handle)
{
input_close_device(handle);
input_unregister_handle(handle);
kfree(handle);
}

Expand Down
32 changes: 22 additions & 10 deletions drivers/input/evbug.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,51 @@ MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");
MODULE_DESCRIPTION("Input driver event debug module");
MODULE_LICENSE("GPL");

static char evbug_name[] = "evbug";

static void evbug_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
{
printk(KERN_DEBUG "evbug.c: Event. Dev: %s, Type: %d, Code: %d, Value: %d\n",
handle->dev->phys, type, code, value);
}

static struct input_handle *evbug_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
static int evbug_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
{
struct input_handle *handle;
int error;

if (!(handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL)))
return NULL;
handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
if (!handle)
return -ENOMEM;

handle->dev = dev;
handle->handler = handler;
handle->name = evbug_name;
handle->name = "evbug";

error = input_register_handle(handle);
if (error)
goto err_free_handle;

input_open_device(handle);
error = input_open_device(handle);
if (error)
goto err_unregister_handle;

printk(KERN_DEBUG "evbug.c: Connected device: \"%s\", %s\n", dev->name, dev->phys);

return handle;
return 0;

err_unregister_handle:
input_unregister_handle(handle);
err_free_handle:
kfree(handle);
return error;
}

static void evbug_disconnect(struct input_handle *handle)
{
printk(KERN_DEBUG "evbug.c: Disconnected device: %s\n", handle->dev->phys);

input_close_device(handle);

input_unregister_handle(handle);
kfree(handle);
}

Expand Down
47 changes: 36 additions & 11 deletions drivers/input/evdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,21 +605,24 @@ static const struct file_operations evdev_fops = {
.flush = evdev_flush
};

static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
{
struct evdev *evdev;
struct class_device *cdev;
dev_t devt;
int minor;
int error;

for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
if (minor == EVDEV_MINORS) {
printk(KERN_ERR "evdev: no more free evdev devices\n");
return NULL;
return -ENFILE;
}

if (!(evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL)))
return NULL;
evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
if (!evdev)
return -ENOMEM;

INIT_LIST_HEAD(&evdev->list);
init_waitqueue_head(&evdev->wait);
Expand All @@ -634,22 +637,44 @@ static struct input_handle *evdev_connect(struct input_handler *handler, struct

evdev_table[minor] = evdev;

cdev = class_device_create(&input_class, &dev->cdev,
MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
dev->cdev.dev, evdev->name);
devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),

cdev = class_device_create(&input_class, &dev->cdev, devt,
dev->cdev.dev, evdev->name);
if (IS_ERR(cdev)) {
error = PTR_ERR(cdev);
goto err_free_evdev;
}

/* temporary symlink to keep userspace happy */
sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
evdev->name);
error = sysfs_create_link(&input_class.subsys.kset.kobj,
&cdev->kobj, evdev->name);
if (error)
goto err_cdev_destroy;

error = input_register_handle(&evdev->handle);
if (error)
goto err_remove_link;

return &evdev->handle;
return 0;

err_remove_link:
sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
err_cdev_destroy:
class_device_destroy(&input_class, devt);
err_free_evdev:
kfree(evdev);
evdev_table[minor] = NULL;
return error;
}

static void evdev_disconnect(struct input_handle *handle)
{
struct evdev *evdev = handle->private;
struct evdev_list *list;

input_unregister_handle(handle);

sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
class_device_destroy(&input_class,
MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
Expand Down
88 changes: 52 additions & 36 deletions drivers/input/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,6 @@ static int input_default_setkeycode(struct input_dev *dev,
}


static void input_link_handle(struct input_handle *handle)
{
list_add_tail(&handle->d_node, &handle->dev->h_list);
list_add_tail(&handle->h_node, &handle->handler->h_list);
}

#define MATCH_BIT(bit, max) \
for (i = 0; i < NBITS(max); i++) \
if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
Expand Down Expand Up @@ -432,6 +426,29 @@ static const struct input_device_id *input_match_device(const struct input_devic
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->cdev.kobj), error);

return error;
}


#ifdef CONFIG_PROC_FS

static struct proc_dir_entry *proc_bus_input_dir;
Expand Down Expand Up @@ -1032,9 +1049,7 @@ EXPORT_SYMBOL(input_free_device);
int input_register_device(struct input_dev *dev)
{
static atomic_t input_no = ATOMIC_INIT(0);
struct input_handle *handle;
struct input_handler *handler;
const struct input_device_id *id;
const char *path;
int error;

Expand Down Expand Up @@ -1074,13 +1089,7 @@ int input_register_device(struct input_dev *dev)
kfree(path);

list_for_each_entry(handler, &input_handler_list, node)
if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
if ((id = input_match_device(handler->id_table, dev)))
if ((handle = handler->connect(handler, dev, id))) {
input_link_handle(handle);
if (handler->start)
handler->start(handle);
}
input_attach_handler(dev, handler);

input_wakeup_procfs_readers();

Expand All @@ -1090,7 +1099,7 @@ EXPORT_SYMBOL(input_register_device);

void input_unregister_device(struct input_dev *dev)
{
struct list_head *node, *next;
struct input_handle *handle, *next;
int code;

for (code = 0; code <= KEY_MAX; code++)
Expand All @@ -1100,12 +1109,9 @@ void input_unregister_device(struct input_dev *dev)

del_timer_sync(&dev->timer);

list_for_each_safe(node, next, &dev->h_list) {
struct input_handle * handle = to_handle(node);
list_del_init(&handle->d_node);
list_del_init(&handle->h_node);
list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
handle->handler->disconnect(handle);
}
WARN_ON(!list_empty(&dev->h_list));

list_del_init(&dev->node);

Expand All @@ -1118,8 +1124,6 @@ EXPORT_SYMBOL(input_unregister_device);
int input_register_handler(struct input_handler *handler)
{
struct input_dev *dev;
struct input_handle *handle;
const struct input_device_id *id;

INIT_LIST_HEAD(&handler->h_list);

Expand All @@ -1133,13 +1137,7 @@ int input_register_handler(struct input_handler *handler)
list_add_tail(&handler->node, &input_handler_list);

list_for_each_entry(dev, &input_dev_list, node)
if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
if ((id = input_match_device(handler->id_table, dev)))
if ((handle = handler->connect(handler, dev, id))) {
input_link_handle(handle);
if (handler->start)
handler->start(handle);
}
input_attach_handler(dev, handler);

input_wakeup_procfs_readers();
return 0;
Expand All @@ -1148,14 +1146,11 @@ EXPORT_SYMBOL(input_register_handler);

void input_unregister_handler(struct input_handler *handler)
{
struct list_head *node, *next;
struct input_handle *handle, *next;

list_for_each_safe(node, next, &handler->h_list) {
struct input_handle * handle = to_handle_h(node);
list_del_init(&handle->h_node);
list_del_init(&handle->d_node);
list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
handler->disconnect(handle);
}
WARN_ON(!list_empty(&handler->h_list));

list_del_init(&handler->node);

Expand All @@ -1166,6 +1161,27 @@ void input_unregister_handler(struct input_handler *handler)
}
EXPORT_SYMBOL(input_unregister_handler);

int input_register_handle(struct input_handle *handle)
{
struct input_handler *handler = handle->handler;

list_add_tail(&handle->d_node, &handle->dev->h_list);
list_add_tail(&handle->h_node, &handler->h_list);

if (handler->start)
handler->start(handle);

return 0;
}
EXPORT_SYMBOL(input_register_handle);

void input_unregister_handle(struct input_handle *handle)
{
list_del_init(&handle->h_node);
list_del_init(&handle->d_node);
}
EXPORT_SYMBOL(input_unregister_handle);

static int input_open_file(struct inode *inode, struct file *file)
{
struct input_handler *handler = input_table[iminor(inode) >> 5];
Expand Down
Loading

0 comments on commit 5b2a082

Please sign in to comment.