hwmon: (ams) Fix locking issues

Use a separate mutex to serialize input device creation/removal,
otheriwse we deadlock if we try to remove input device while it is
being polled. Also do not take ams_info.lock when it is not needed.

Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Signed-off-by: Jean Delvare <khali@linux-fr.org>
This commit is contained in:
Dmitry Torokhov 2008-10-17 17:51:12 +02:00 committed by Jean Delvare
parent 0a02002268
commit ee4cd32ee8
4 changed files with 56 additions and 65 deletions

View file

@ -223,34 +223,28 @@ int __init ams_init(void)
void ams_exit(void) void ams_exit(void)
{ {
mutex_lock(&ams_info.lock); /* Remove input device */
ams_input_exit();
if (ams_info.has_device) { /* Remove attributes */
/* Remove input device */ device_remove_file(&ams_info.of_dev->dev, &dev_attr_current);
ams_input_exit();
/* Shut down implementation */ /* Shut down implementation */
ams_info.exit(); ams_info.exit();
/* Flush interrupt worker /* Flush interrupt worker
* *
* We do this after ams_info.exit(), because an interrupt might * We do this after ams_info.exit(), because an interrupt might
* have arrived before disabling them. * have arrived before disabling them.
*/ */
flush_scheduled_work(); flush_scheduled_work();
/* Remove attributes */ /* Remove device */
device_remove_file(&ams_info.of_dev->dev, &dev_attr_current); of_device_unregister(ams_info.of_dev);
/* Remove device */ /* Remove handler */
of_device_unregister(ams_info.of_dev); pmf_unregister_irq_client(&ams_shock_client);
pmf_unregister_irq_client(&ams_freefall_client);
/* Remove handler */
pmf_unregister_irq_client(&ams_shock_client);
pmf_unregister_irq_client(&ams_freefall_client);
}
mutex_unlock(&ams_info.lock);
} }
MODULE_AUTHOR("Stelian Pop, Michael Hanselmann"); MODULE_AUTHOR("Stelian Pop, Michael Hanselmann");

View file

@ -261,8 +261,6 @@ int __init ams_i2c_init(struct device_node *np)
{ {
int result; int result;
mutex_lock(&ams_info.lock);
/* Set implementation stuff */ /* Set implementation stuff */
ams_info.of_node = np; ams_info.of_node = np;
ams_info.exit = ams_i2c_exit; ams_info.exit = ams_i2c_exit;
@ -273,7 +271,5 @@ int __init ams_i2c_init(struct device_node *np)
result = i2c_add_driver(&ams_i2c_driver); result = i2c_add_driver(&ams_i2c_driver);
mutex_unlock(&ams_info.lock);
return result; return result;
} }

View file

@ -27,6 +27,8 @@ static unsigned int invert;
module_param(invert, bool, S_IWUSR | S_IRUGO); module_param(invert, bool, S_IWUSR | S_IRUGO);
MODULE_PARM_DESC(invert, "Invert input data on X and Y axis"); MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
static DEFINE_MUTEX(ams_input_mutex);
static void ams_idev_poll(struct input_polled_dev *dev) static void ams_idev_poll(struct input_polled_dev *dev)
{ {
struct input_dev *idev = dev->input; struct input_dev *idev = dev->input;
@ -50,13 +52,11 @@ static void ams_idev_poll(struct input_polled_dev *dev)
} }
/* Call with ams_info.lock held! */ /* Call with ams_info.lock held! */
static void ams_input_enable(void) static int ams_input_enable(void)
{ {
struct input_dev *input; struct input_dev *input;
s8 x, y, z; s8 x, y, z;
int error;
if (ams_info.idev)
return;
ams_sensors(&x, &y, &z); ams_sensors(&x, &y, &z);
ams_info.xcalib = x; ams_info.xcalib = x;
@ -65,7 +65,7 @@ static void ams_input_enable(void)
ams_info.idev = input_allocate_polled_device(); ams_info.idev = input_allocate_polled_device();
if (!ams_info.idev) if (!ams_info.idev)
return; return -ENOMEM;
ams_info.idev->poll = ams_idev_poll; ams_info.idev->poll = ams_idev_poll;
ams_info.idev->poll_interval = 25; ams_info.idev->poll_interval = 25;
@ -84,14 +84,18 @@ static void ams_input_enable(void)
set_bit(EV_KEY, input->evbit); set_bit(EV_KEY, input->evbit);
set_bit(BTN_TOUCH, input->keybit); set_bit(BTN_TOUCH, input->keybit);
if (input_register_polled_device(ams_info.idev)) { error = input_register_polled_device(ams_info.idev);
if (error) {
input_free_polled_device(ams_info.idev); input_free_polled_device(ams_info.idev);
ams_info.idev = NULL; ams_info.idev = NULL;
return; return error;
} }
joystick = 1;
return 0;
} }
/* Call with ams_info.lock held! */
static void ams_input_disable(void) static void ams_input_disable(void)
{ {
if (ams_info.idev) { if (ams_info.idev) {
@ -99,6 +103,8 @@ static void ams_input_disable(void)
input_free_polled_device(ams_info.idev); input_free_polled_device(ams_info.idev);
ams_info.idev = NULL; ams_info.idev = NULL;
} }
joystick = 0;
} }
static ssize_t ams_input_show_joystick(struct device *dev, static ssize_t ams_input_show_joystick(struct device *dev,
@ -110,39 +116,42 @@ static ssize_t ams_input_show_joystick(struct device *dev,
static ssize_t ams_input_store_joystick(struct device *dev, static ssize_t ams_input_store_joystick(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count) struct device_attribute *attr, const char *buf, size_t count)
{ {
if (sscanf(buf, "%d\n", &joystick) != 1) unsigned long enable;
int error = 0;
if (strict_strtoul(buf, 0, &enable) || enable > 1)
return -EINVAL; return -EINVAL;
mutex_lock(&ams_info.lock); mutex_lock(&ams_input_mutex);
if (joystick) if (enable != joystick) {
ams_input_enable(); if (enable)
else error = ams_input_enable();
ams_input_disable(); else
ams_input_disable();
}
mutex_unlock(&ams_info.lock); mutex_unlock(&ams_input_mutex);
return count; return error ? error : count;
} }
static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR, static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
ams_input_show_joystick, ams_input_store_joystick); ams_input_show_joystick, ams_input_store_joystick);
/* Call with ams_info.lock held! */
int ams_input_init(void) int ams_input_init(void)
{ {
int result; if (joystick)
result = device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
if (!result && joystick)
ams_input_enable(); ams_input_enable();
return result;
return device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
} }
/* Call with ams_info.lock held! */
void ams_input_exit(void) void ams_input_exit(void)
{ {
ams_input_disable();
device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick); device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);
mutex_lock(&ams_input_mutex);
ams_input_disable();
mutex_unlock(&ams_input_mutex);
} }

View file

@ -149,8 +149,6 @@ int __init ams_pmu_init(struct device_node *np)
const u32 *prop; const u32 *prop;
int result; int result;
mutex_lock(&ams_info.lock);
/* Set implementation stuff */ /* Set implementation stuff */
ams_info.of_node = np; ams_info.of_node = np;
ams_info.exit = ams_pmu_exit; ams_info.exit = ams_pmu_exit;
@ -161,10 +159,9 @@ int __init ams_pmu_init(struct device_node *np)
/* Get PMU command, should be 0x4e, but we can never know */ /* Get PMU command, should be 0x4e, but we can never know */
prop = of_get_property(ams_info.of_node, "reg", NULL); prop = of_get_property(ams_info.of_node, "reg", NULL);
if (!prop) { if (!prop)
result = -ENODEV; return -ENODEV;
goto exit;
}
ams_pmu_cmd = ((*prop) >> 8) & 0xff; ams_pmu_cmd = ((*prop) >> 8) & 0xff;
/* Disable interrupts */ /* Disable interrupts */
@ -175,7 +172,7 @@ int __init ams_pmu_init(struct device_node *np)
result = ams_sensor_attach(); result = ams_sensor_attach();
if (result < 0) if (result < 0)
goto exit; return result;
/* Set default values */ /* Set default values */
ams_pmu_set_register(AMS_FF_LOW_LIMIT, 0x15); ams_pmu_set_register(AMS_FF_LOW_LIMIT, 0x15);
@ -198,10 +195,5 @@ int __init ams_pmu_init(struct device_node *np)
printk(KERN_INFO "ams: Found PMU based motion sensor\n"); printk(KERN_INFO "ams: Found PMU based motion sensor\n");
result = 0; return 0;
exit:
mutex_unlock(&ams_info.lock);
return result;
} }