Input: mousedev - factor out psaux code to reduce #ifdefery
Subsystems (2)
Dmitry Torokhov
maintainerPATCH 1/4 Input: mousedev - factor out psaux code to reduce #ifdefery
---
This is the previously posted dynamic input devices patch with changes that has nothing to do with dynamic minors split into separate patches for easier reviewing. We also now preserving format of /proc/bus/input/handlers to ensure userspace ABI.
drivers/input/mousedev.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-)
drivers/input/mousedev.c +30 -17
@@ -25,9 +25,6 @@#include <linux/major.h> #include <linux/device.h> #include <linux/kernel.h> -#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX -#include <linux/miscdevice.h> -#endif MODULE_AUTHOR ("Vojtech Pavlik <[email protected]>" );MODULE_DESCRIPTION ("Mouse (ExplorerPS/2) device interfaces" );
@@ -1064,10 +1061,36 @@ static struct input_handler mousedev_handler = { };#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX +#include <linux/miscdevice.h> +static struct miscdevice psaux_mouse = { -PSMOUSE_MINOR ,"psaux" , &mousedev_fops + .minor =PSMOUSE_MINOR , + .name ="psaux" , + .fops = &mousedev_fops, }; -static int psaux_registered; + +static bool psaux_registered; + +static void __init mousedev_psaux_register (void ) +{ +int error; + + error =misc_register (&psaux_mouse); +if (error) +pr_warn ("could not register psaux device, error: %d\n" , + error); +else + psaux_registered = true; +} + +static void __exit mousedev_psaux_unregister(void) +{ +if (psaux_registered) +misc_deregister (&psaux_mouse); +} +#else +static inline void mousedev_psaux_register (void ) { } +static inline void mousedev_psaux_unregister (void ) { }#endif static int __init mousedev_init (void )
@@ -1084,14 +1107,7 @@ static int __init mousedev_init(void)return error; } -#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX - error =misc_register (&psaux_mouse); -if (error) -pr_warn ("could not register psaux device, error: %d\n" , - error); -else - psaux_registered =1 ; -#endif +mousedev_psaux_register ();pr_info ("PS/2 mouse device common for all mice\n" );
@@ -1100,10 +1116,7 @@ static int __init mousedev_init(void) static void __exit mousedev_exit(void) { -#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX -if (psaux_registered) -misc_deregister (&psaux_mouse); -#endif +mousedev_psaux_unregister ();input_unregister_handler (&mousedev_handler);mousedev_destroy (mousedev_mix); }
Signature · 1 line
1.7.11.4
Dmitry Torokhov
maintainerPATCH 2/4 Input: mousedev - reformat structure initializers
Reformat structure initializers to match the rest of input handlers
--- drivers/input/mousedev.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
drivers/input/mousedev.c +15 -15
@@ -782,14 +782,14 @@ static unsigned int mousedev_poll(struct file *file, poll_table *wait) }static const struct file_operations mousedev_fops = { - .owner =THIS_MODULE , - .read = mousedev_read, - .write = mousedev_write, - .poll = mousedev_poll, - .open = mousedev_open, - .release = mousedev_release, - .fasync = mousedev_fasync, - .llseek = noop_llseek, + .owner =THIS_MODULE , + .read = mousedev_read, + .write = mousedev_write, + .poll = mousedev_poll, + .open = mousedev_open, + .release = mousedev_release, + .fasync = mousedev_fasync, + .llseek = noop_llseek, };static int mousedev_install_chrdev (struct mousedev *mousedev)
@@ -1051,13 +1051,13 @@ static const struct input_device_id mousedev_ids[] = {MODULE_DEVICE_TABLE (input, mousedev_ids);static struct input_handler mousedev_handler = { - .event = mousedev_event, - .connect = mousedev_connect, - .disconnect = mousedev_disconnect, - .fops = &mousedev_fops, - .minor =MOUSEDEV_MINOR_BASE , - .name ="mousedev" , - .id_table = mousedev_ids, + .event = mousedev_event, + .connect = mousedev_connect, + .disconnect = mousedev_disconnect, + .fops = &mousedev_fops, + .minor =MOUSEDEV_MINOR_BASE , + .name ="mousedev" , + .id_table = mousedev_ids, };#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
Signature · 1 line
1.7.11.4
Dmitry Torokhov
maintainerPATCH 3/4 Input: mousedev - rename mixdev_open to opened_by_mixdev
In reply to Dmitry TorokhovDo the rename to make the logic more clear and also change it to bool while at it.
--- drivers/input/mousedev.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/input/mousedev.c +8 -8
@@ -68,7 +68,7 @@ struct mousedev {bool exist;struct list_head mixdev_node; -int mixdev_open; +bool opened_by_mixdev;struct mousedev_hw_data packet;unsigned int pkt_count;
@@ -469,11 +469,11 @@ static void mixdev_open_devices(void)return ;list_for_each_entry (mousedev, &mousedev_mix_list, mixdev_node) { -if (!mousedev->mixdev_open) { +if (!mousedev->opened_by_mixdev) {if (mousedev_open_device (mousedev))continue ; - mousedev->mixdev_open =1 ; + mousedev->opened_by_mixdev = true; } } }
@@ -491,8 +491,8 @@ static void mixdev_close_devices(void)return ;list_for_each_entry (mousedev, &mousedev_mix_list, mixdev_node) { -if (mousedev->mixdev_open) { - mousedev->mixdev_open =0 ; +if (mousedev->opened_by_mixdev) { + mousedev->opened_by_mixdev = false;mousedev_close_device (mousedev); } }
@@ -935,7 +935,7 @@ static int mixdev_add_device(struct mousedev *mousedev)if (retval) goto out; - mousedev->mixdev_open =1 ; + mousedev->opened_by_mixdev = true; }get_device (&mousedev->dev);
@@ -950,8 +950,8 @@ static void mixdev_remove_device(struct mousedev *mousedev) {mutex_lock (&mousedev_mix->mutex); -if (mousedev->mixdev_open) { - mousedev->mixdev_open =0 ; +if (mousedev->opened_by_mixdev) { + mousedev->opened_by_mixdev = false;mousedev_close_device (mousedev); }
Signature · 1 line
1.7.11.4
Dmitry Torokhov
maintainerPATCH 4/4 Input: extend the number of event (and other) devices
In reply to Dmitry TorokhovExtend the amount of character devices, such as eventX, mouseX and jsX, from a hard limit of 32 per input handler to about 1024 shared across all handlers.
To be compatible with legacy installations input handlers will start creating char devices with minors in their legacy range, however once legacy range is exhausted they will start allocating minors from the dynamic range 256-1024.
--- drivers/input/evdev.c | 99 ++++++++++++----------------------- drivers/input/input.c | 114 +++++++++++++++++++--------------------- drivers/input/joydev.c | 88 ++++++++++++------------------- drivers/input/mousedev.c | 133 ++++++++++++++++++++++------------------------- include/linux/input.h | 10 ++-- 5 files changed, 188 insertions(+), 256 deletions(-)
drivers/input/evdev.c +33 -66
@@ -23,11 +23,11 @@#include <linux/input/mt.h> #include <linux/major.h> #include <linux/device.h> +#include <linux/cdev.h> #include "input-compat.h" struct evdev {int open; -int minor;struct input_handle handle;wait_queue_head_t wait;struct evdev_client __rcu *grab;
@@ -35,6 +35,7 @@ struct evdev {spinlock_t client_lock;/* protects client_list */ struct mutex mutex;struct device dev; +struct cdev cdev;bool exist; };
@@ -51,9 +52,6 @@ struct evdev_client {struct input_event buffer[]; }; -static struct evdev *evdev_table[EVDEV_MINORS ]; -static DEFINE_MUTEX (evdev_table_mutex); -static void __pass_event (struct evdev_client *client,const struct input_event *event) {
@@ -310,35 +308,16 @@ static unsigned int evdev_compute_buffer_size(struct input_dev *dev)static int evdev_open (struct inode *inode,struct file *file) { -struct evdev *evdev; + struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev); +unsigned int bufsize =evdev_compute_buffer_size (evdev->handle.dev);struct evdev_client *client; -int i =iminor (inode) -EVDEV_MINOR_BASE ; -unsigned int bufsize;int error; -if (i >=EVDEV_MINORS ) -return -ENODEV ; - - error =mutex_lock_interruptible (&evdev_table_mutex); -if (error) -return error; - evdev = evdev_table[i]; -if (evdev) -get_device (&evdev->dev); -mutex_unlock (&evdev_table_mutex); - -if (!evdev) -return -ENODEV ; - - bufsize =evdev_compute_buffer_size (evdev->handle.dev); - client =kzalloc (sizeof (struct evdev_client ) + bufsize *sizeof (struct input_event ),GFP_KERNEL ); -if (!client) { - error = -ENOMEM ; - goto err_put_evdev; - } +if (!client) +return -ENOMEM ; client->bufsize = bufsize;spin_lock_init (&client->buffer_lock);
@@ -352,13 +331,12 @@ static int evdev_open(struct inode *inode, struct file *file) file->private_data = client;nonseekable_open (inode, file); +get_device (&evdev->dev);return 0 ; err_free_client:evdev_detach_client (evdev, client);kfree (client); - err_put_evdev: -put_device (&evdev->dev);return error; }
@@ -942,26 +920,6 @@ static const struct file_operations evdev_fops = { .llseek = no_llseek, }; -static int evdev_install_chrdev (struct evdev *evdev) -{ -/* -* No need to do any locking here as calls to connect and -* disconnect are serialized by the input core -*/ - evdev_table[evdev->minor] = evdev; -return 0 ; -} - -static void evdev_remove_chrdev (struct evdev *evdev) -{ -/* -* Lock evdev table to prevent race with evdev_open() -*/ -mutex_lock (&evdev_table_mutex); - evdev_table[evdev->minor] =NULL ; -mutex_unlock (&evdev_table_mutex); -} -/* * Mark device non-existent. This disables writes, ioctls and * prevents new users from opening the device. Already posted
@@ -980,7 +938,8 @@ static void evdev_cleanup(struct evdev *evdev)evdev_mark_dead (evdev);evdev_hangup (evdev); -evdev_remove_chrdev (evdev); + +cdev_del (&evdev->cdev);/* evdev is marked dead so no one else accesses evdev->open */ if (evdev->open) {
@@ -991,43 +950,47 @@ static void evdev_cleanup(struct evdev *evdev)/* * Create new evdev device. Note that input core serializes calls -* to connect and disconnect so we don't need to lock evdev_table here. +* to connect and disconnect. */ static int evdev_connect (struct input_handler *handler,struct input_dev *dev,const struct input_device_id *id) {struct evdev *evdev;int minor; +int dev_no;int error; -for (minor =0 ; minor <EVDEV_MINORS ; minor++) -if (!evdev_table[minor]) -break ; - -if (minor ==EVDEV_MINORS ) { -pr_err ("no more free evdev devices\n" ); -return -ENFILE ; + minor =input_get_new_minor (EVDEV_MINOR_BASE ,EVDEV_MINORS , true); +if (minor <0 ) { + error = minor; +pr_err ("failed to reserve new minor: %d\n" , error); +return error; } evdev =kzalloc (sizeof (struct evdev ),GFP_KERNEL ); -if (!evdev) -return -ENOMEM ; +if (!evdev) { + error = -ENOMEM ; + goto err_free_minor; + }INIT_LIST_HEAD (&evdev->client_list);spin_lock_init (&evdev->client_lock);mutex_init (&evdev->mutex);init_waitqueue_head (&evdev->wait); - -dev_set_name (&evdev->dev,"event%d" , minor); evdev->exist = true; - evdev->minor = minor; + + dev_no = minor; +/* Normalize device number if it falls into legacy range */ +if (dev_no <EVDEV_MINOR_BASE +EVDEV_MINORS ) + dev_no -=EVDEV_MINOR_BASE ; +dev_set_name (&evdev->dev,"event%d" , dev_no); evdev->handle.dev =input_get_device (dev); evdev->handle.name =dev_name (&evdev->dev); evdev->handle.handler = handler; evdev->handle.private = evdev; - evdev->dev.devt =MKDEV (INPUT_MAJOR ,EVDEV_MINOR_BASE + minor); + evdev->dev.devt =MKDEV (INPUT_MAJOR , minor); evdev->dev.class = &input_class; evdev->dev.parent = &dev->dev; evdev->dev.release = evdev_free;
@@ -1037,7 +1000,8 @@ static int evdev_connect(struct input_handler *handler, struct input_dev *dev,if (error) goto err_free_evdev; - error =evdev_install_chrdev (evdev); +cdev_init (&evdev->cdev, &evdev_fops); + error =cdev_add (&evdev->cdev, evdev->dev.devt,1 );if (error) goto err_unregister_handle;
@@ -1053,6 +1017,8 @@ static int evdev_connect(struct input_handler *handler, struct input_dev *dev,input_unregister_handle (&evdev->handle); err_free_evdev:put_device (&evdev->dev); + err_free_minor: +input_free_minor (minor);return error; }
@@ -1062,6 +1028,7 @@ static void evdev_disconnect(struct input_handle *handle)device_del (&evdev->dev);evdev_cleanup (evdev); +input_free_minor (MINOR (evdev->dev.devt));input_unregister_handle (handle);put_device (&evdev->dev); }
@@ -1078,7 +1045,7 @@ static struct input_handler evdev_handler = { .events = evdev_events, .connect = evdev_connect, .disconnect = evdev_disconnect, - .fops = &evdev_fops, + .legacy_minors = true, .minor = EVDEV_MINOR_BASE, .name = "evdev", .id_table = evdev_ids,
drivers/input/input.c +53 -61
186 lines not on this page · raw message
drivers/input/joydev.c +32 -56
192 lines not on this page · raw message
drivers/input/mousedev.c +63 -70
281 lines not on this page · raw message
include/linux/input.h +7 -3
@@ -1396,8 +1396,8 @@ struct input_handle;* @start: starts handler for given handle. This function is called by * input core right after connect() method and also when a process * that "grabbed" a device releases it -* @fops: file operations this driver implements -* @minor: beginning of range of 32 minors for devices this driver +* @legacy_minors: set to %true by drivers using legacy minor ranges +* @minor: beginning of range of 32 legacy minors for devices this driver * can provide * @name: name of the handler, to be shown in /proc/bus/input/handlers * @id_table: pointer to a table of input_device_ids this driver can
@@ -1431,7 +1431,7 @@ struct input_handler {void (*disconnect)(struct input_handle *handle);void (*start)(struct input_handle *handle); -const struct file_operations *fops; +bool legacy_minors;int minor;const char *name;
@@ -1499,6 +1499,10 @@ void input_reset_device(struct input_dev *);int __must_check input_register_handler (struct input_handler *);void input_unregister_handler (struct input_handler *); +int __must_check input_get_new_minor (int legacy_base,unsigned int legacy_num, +bool allow_dynamic); +void input_free_minor (unsigned int minor); +int input_handler_for_each_handle (struct input_handler *,void *data,int (*fn)(struct input_handle *,void *));
Signature · 1 line
1.7.11.4
Re: [PATCH 4/4] Input: extend the number of event (and other) devices
Hi Dmitry
On Wed, Oct 3, 2012 at 11:03 PM, Dmitry Torokhov
<[email protected]> wrote:
[snip]
Quoted 17 lines
drivers/input/evdev.c +2
-3@@ -991,43 +950,47 @@ static void evdev_cleanup(struct evdev *evdev)/* * Create new evdev device. Note that input core serializes calls -* to connect and disconnect so we don't need to lock evdev_table here. +* to connect and disconnect. */ static int evdev_connect (struct input_handler *handler,struct input_dev *dev,const struct input_device_id *id) {struct evdev *evdev;int minor; +int dev_no;int error; -for (minor =0 ; minor <EVDEV_MINORS ; minor++) -if (!evdev_table[minor])
drivers/input/evdev.c static void evdev_cleanup(struct evdev *evdev) · line 967
Quoted 10 lines
drivers/input/evdev.c +5
-5-break ; - -if (minor ==EVDEV_MINORS ) { -pr_err ("no more free evdev devices\n" ); -return -ENFILE ; + minor =input_get_new_minor (EVDEV_MINOR_BASE ,EVDEV_MINORS , true); +if (minor <0 ) { + error = minor; +pr_err ("failed to reserve new minor: %d\n" , error); +return error;
You could also do:
return minor;
So you can drop that "error = minor;" line.
[snip]
drivers/input/evdev.c static void evdev_disconnect(struct input_handle *handle) · line 1031
Quoted 5 lines
drivers/input/evdev.c +1
-0@@ -1062,6 +1028,7 @@ static void evdev_disconnect(struct input_handle *handle)device_del (&evdev->dev);evdev_cleanup (evdev); +input_free_minor (MINOR (evdev->dev.devt));
I was wondering whether we should free the minors in evdev_free() instead. Because if we free them here, we might end up with two user-space applications listening to the same cdev (based on major/minor) but to different input devices that drive the cdev.
The older of both cdevs would be already dead and I don't think this is a big issue, but I just wanted to mention it if others can think of corner cases where this would be bad.
[snip]
drivers/input/input.c EXPORT_SYMBOL(input_unregister_device); · line 2024
Quoted 13 lines
drivers/input/input.c +4
-4@@ -2016,22 +2017,14 @@ EXPORT_SYMBOL(input_unregister_device);int input_register_handler (struct input_handler *handler) {struct input_dev *dev; -int retval; +int error; - retval =mutex_lock_interruptible (&input_mutex); -if (retval) -return retval; + error =mutex_lock_interruptible (&input_mutex); +if (error) +return error;
Still wondering why you change that variable name here?
[snip]
drivers/input/mousedev.c static int mousedev_open(struct inode *inode, struct file *file) · line 559
Quoted 5 lines
drivers/input/mousedev.c +1
-0@@ -576,13 +556,14 @@ static int mousedev_open(struct inode *inode, struct file *file) goto err_free_client; file->private_data = client; +nonseekable_open (inode, file);
Ouh, seems like we never called this for mousedevs, isn't that a bug that should also go to stable? It just sets some flags but I am not sure what happends if we don't set them.
It isn't related to this patch (I think?).
[snip]
Sorry for the bikeshedding. The patch looks really good and all I found are just minor optional suggestions (or personal taste). Thanks a lot for fixing this! It looks much better than my first approach.
Regards
David
Re: [PATCH 4/4] Input: extend the number of event (and other) devices
In reply to Dmitry TorokhovHi Dmitry
Quoted 11 lines · Dmitry Torokhov
Extend the amount of character devices, such as eventX, mouseX and jsX, from a hard limit of 32 per input handler to about 1024 shared across all handlers.
To be compatible with legacy installations input handlers will start creating char devices with minors in their legacy range, however once legacy range is exhausted they will start allocating minors from the dynamic range 256-1024.
Signed-off-by Dmitry Torokhov <[email protected]>---
I just reviewed the other patches 1-3 as well. Thanks for factoring them out! Feel free to add my reviewed-by to those as well.
Regards
David
Dmitry Torokhov
maintainerRe: [PATCH 4/4] Input: extend the number of event (and other) devices
In reply to David HerrmannHi David,
Quoted 22 lines · David Herrmann
Hi Dmitry
On Wed, Oct 3, 2012 at 11:03 PM, Dmitry Torokhov
<[email protected]> wrote:[snip]
Quoted 15 lines
drivers/input/evdev.c +2
-1@@ -991,43 +950,47 @@ static void evdev_cleanup(struct evdev *evdev)/* * Create new evdev device. Note that input core serializes calls -* to connect and disconnect so we don't need to lock evdev_table here. +* to connect and disconnect. */ static int evdev_connect (struct input_handler *handler,struct input_dev *dev,const struct input_device_id *id) {struct evdev *evdev;int minor; +int dev_no;int error;
Quoted 16 lines
drivers/input/evdev.c static void evdev_cleanup(struct evdev *evdev) · line 967
Quoted 12 lines
drivers/input/evdev.c +5
-7-for (minor =0 ; minor <EVDEV_MINORS ; minor++) -if (!evdev_table[minor]) -break ; - -if (minor ==EVDEV_MINORS ) { -pr_err ("no more free evdev devices\n" ); -return -ENFILE ; + minor =input_get_new_minor (EVDEV_MINOR_BASE ,EVDEV_MINORS , true); +if (minor <0 ) { + error = minor; +pr_err ("failed to reserve new minor: %d\n" , error); +return error;You could also do:
return minor;
So you can drop that "error = minor;" line.
Right, but I prefer doing this to signify that the value in 'minor' is actually an error code.
Quoted 14 lines
[snip]
drivers/input/evdev.c static void evdev_disconnect(struct input_handle *handle) · line 1031
Quoted 5 lines
drivers/input/evdev.c +1
-0@@ -1062,6 +1028,7 @@ static void evdev_disconnect(struct input_handle *handle)device_del (&evdev->dev);evdev_cleanup (evdev); +input_free_minor (MINOR (evdev->dev.devt));I was wondering whether we should free the minors in
evdev_free()instead. Because if we free them here, we might end up with two user-space applications listening to the same cdev (based on major/minor) but to different input devices that drive the cdev.
Quoted 3 lines
The older of both cdevs would be already dead and I don't think this is a big issue, but I just wanted to mention it if others can think of corner cases where this would be bad.
I believe this is OK and how the rest of Unix objects work - they do not get freed until all users release them, but new users will get the new instance.
Quoted 10 lines
[snip]
Quoted 7 lines
drivers/input/input.c +1
-1@@ -2016,22 +2017,14 @@ EXPORT_SYMBOL(input_unregister_device);int input_register_handler (struct input_handler *handler) {struct input_dev *dev; -int retval; +int error;
Quoted 8 lines
drivers/input/input.c EXPORT_SYMBOL(input_unregister_device); · line 2024
Quoted 6 lines
drivers/input/input.c +3
-3- retval =mutex_lock_interruptible (&input_mutex); -if (retval) -return retval; + error =mutex_lock_interruptible (&input_mutex); +if (error) +return error;Still wondering why you change that variable name here?
My style preference: when the same variable is used to return error codes and success/real values I call it retval. If I have separate error handling path and explicit success path I use "return error;"/"return 0;". Here we transitioned to explicitly returning success, so I changed the name.
Quoted 6 lines
[snip]
Quoted 3 lines
drivers/input/mousedev.c +0
-0@@ -576,13 +556,14 @@ static int mousedev_open(struct inode *inode, struct file *file) goto err_free_client;
Quoted 7 lines
drivers/input/mousedev.c static int mousedev_open(struct inode *inode, struct file *file) · line 559
Quoted 2 lines
drivers/input/mousedev.c +1
-0file->private_data = client; +nonseekable_open (inode, file);Ouh, seems like we never called this for mousedevs, isn't that a bug that should also go to stable? It just sets some flags but I am not sure what happends if we don't set them.
It isn't related to this patch (I think?).
I split it out, but we do not need it for stable. We still were installing noop_llseek() fops handler so it should still work even without nonseekable_open().
Quoted 5 lines
[snip]
Sorry for the bikeshedding. The patch looks really good and all I found are just minor optional suggestions (or personal taste).
Thank you very mich for going over the code!
Quoted 6 lines
Thanks a lot for fixing this! It looks much better than my first approach.
Reviewed-by David Herrmann <[email protected]>Regards
David
Signature · 1 line
Dmitry