Commit da308e8d authored by Sean Young's avatar Sean Young Committed by Greg Kroah-Hartman

USB: Phidgets should check create_device_file() return value

device_create_file() could fail, add proper error paths for this condition.
Signed-off-by: default avatarSean Young <sean@mess.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 912b24c3
This diff is collapsed.
......@@ -215,9 +215,12 @@ static ssize_t show_speed##value(struct device *dev, \
struct motorcontrol *mc = dev_get_drvdata(dev); \
\
return sprintf(buf, "%d\n", mc->speed[value]); \
} \
static DEVICE_ATTR(speed##value, S_IWUGO | S_IRUGO, \
show_speed##value, set_speed##value);
}
#define speed_attr(value) \
__ATTR(speed##value, S_IWUGO | S_IRUGO, \
show_speed##value, set_speed##value)
show_set_speed(0);
show_set_speed(1);
......@@ -250,9 +253,12 @@ static ssize_t show_acceleration##value(struct device *dev, \
struct motorcontrol *mc = dev_get_drvdata(dev); \
\
return sprintf(buf, "%d\n", mc->acceleration[value]); \
} \
static DEVICE_ATTR(acceleration##value, S_IWUGO | S_IRUGO, \
show_acceleration##value, set_acceleration##value);
}
#define acceleration_attr(value) \
__ATTR(acceleration##value, S_IWUGO | S_IRUGO, \
show_acceleration##value, set_acceleration##value)
show_set_acceleration(0);
show_set_acceleration(1);
......@@ -264,8 +270,10 @@ static ssize_t show_current##value(struct device *dev, \
struct motorcontrol *mc = dev_get_drvdata(dev); \
\
return sprintf(buf, "%dmA\n", (int)mc->_current[value]); \
} \
static DEVICE_ATTR(current##value, S_IRUGO, show_current##value, NULL);
}
#define current_attr(value) \
__ATTR(current##value, S_IRUGO, show_current##value, NULL)
show_current(0);
show_current(1);
......@@ -278,14 +286,29 @@ static ssize_t show_input##value(struct device *dev, \
struct motorcontrol *mc = dev_get_drvdata(dev); \
\
return sprintf(buf, "%d\n", (int)mc->inputs[value]); \
} \
static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);
}
#define input_attr(value) \
__ATTR(input##value, S_IRUGO, show_input##value, NULL)
show_input(0);
show_input(1);
show_input(2);
show_input(3);
static struct device_attribute dev_attrs[] = {
input_attr(0),
input_attr(1),
input_attr(2),
input_attr(3),
speed_attr(0),
speed_attr(1),
acceleration_attr(0),
acceleration_attr(1),
current_attr(0),
current_attr(1)
};
static int motorcontrol_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
struct usb_device *dev = interface_to_usbdev(intf);
......@@ -293,7 +316,7 @@ static int motorcontrol_probe(struct usb_interface *intf, const struct usb_devic
struct usb_endpoint_descriptor *endpoint;
struct motorcontrol *mc;
int pipe, maxp, rc = -ENOMEM;
int bit, value;
int bit, value, i;
interface = intf->cur_altsetting;
if (interface->desc.bNumEndpoints != 1)
......@@ -355,24 +378,18 @@ static int motorcontrol_probe(struct usb_interface *intf, const struct usb_devic
goto out;
}
device_create_file(mc->dev, &dev_attr_input0);
device_create_file(mc->dev, &dev_attr_input1);
device_create_file(mc->dev, &dev_attr_input2);
device_create_file(mc->dev, &dev_attr_input3);
device_create_file(mc->dev, &dev_attr_speed0);
device_create_file(mc->dev, &dev_attr_speed1);
device_create_file(mc->dev, &dev_attr_acceleration0);
device_create_file(mc->dev, &dev_attr_acceleration1);
device_create_file(mc->dev, &dev_attr_current0);
device_create_file(mc->dev, &dev_attr_current1);
for (i=0; i<ARRAY_SIZE(dev_attrs); i++) {
rc = device_create_file(mc->dev, &dev_attrs[i]);
if (rc)
goto out2;
}
dev_info(&intf->dev, "USB PhidgetMotorControl attached\n");
return 0;
out2:
while (i-- > 0)
device_remove_file(mc->dev, &dev_attrs[i]);
out:
if (mc) {
if (mc->irq)
......@@ -393,6 +410,7 @@ out:
static void motorcontrol_disconnect(struct usb_interface *interface)
{
struct motorcontrol *mc;
int i;
mc = usb_get_intfdata(interface);
usb_set_intfdata(interface, NULL);
......@@ -405,19 +423,8 @@ static void motorcontrol_disconnect(struct usb_interface *interface)
cancel_delayed_work(&mc->do_notify);
device_remove_file(mc->dev, &dev_attr_input0);
device_remove_file(mc->dev, &dev_attr_input1);
device_remove_file(mc->dev, &dev_attr_input2);
device_remove_file(mc->dev, &dev_attr_input3);
device_remove_file(mc->dev, &dev_attr_speed0);
device_remove_file(mc->dev, &dev_attr_speed1);
device_remove_file(mc->dev, &dev_attr_acceleration0);
device_remove_file(mc->dev, &dev_attr_acceleration1);
device_remove_file(mc->dev, &dev_attr_current0);
device_remove_file(mc->dev, &dev_attr_current1);
for (i=0; i<ARRAY_SIZE(dev_attrs); i++)
device_remove_file(mc->dev, &dev_attrs[i]);
device_unregister(mc->dev);
......
/*
* USB PhidgetServo driver 1.0
*
* Copyright (C) 2004 Sean Young <sean@mess.org>
* Copyright (C) 2004, 2006 Sean Young <sean@mess.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -235,86 +235,108 @@ static ssize_t show_servo##value (struct device *dev, \
\
return sprintf(buf, "%d.%02d\n", servo->degrees[value], \
servo->minutes[value]); \
} \
static DEVICE_ATTR(servo##value, S_IWUGO | S_IRUGO, \
show_servo##value, set_servo##value);
}
#define servo_attr(value) \
__ATTR(servo##value, S_IWUGO | S_IRUGO, \
show_servo##value, set_servo##value)
show_set(0);
show_set(1);
show_set(2);
show_set(3);
static struct device_attribute dev_attrs[] = {
servo_attr(0), servo_attr(1), servo_attr(2), servo_attr(3)
};
static int
servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
struct usb_device *udev = interface_to_usbdev(interface);
struct phidget_servo *dev;
int bit, value;
int bit, value, rc;
int servo_count, i;
dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL);
if (dev == NULL) {
dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
return -ENOMEM;
rc = -ENOMEM;
goto out;
}
dev->udev = usb_get_dev(udev);
dev->type = id->driver_info;
dev->dev_no = -1;
usb_set_intfdata(interface, dev);
do {
bit = find_first_zero_bit(&device_no, sizeof(device_no));
value = test_and_set_bit(bit, &device_no);
} while(value);
} while (value);
dev->dev_no = bit;
dev->dev = device_create(phidget_class, &dev->udev->dev, 0,
"servo%d", dev->dev_no);
if (IS_ERR(dev->dev)) {
int rc = PTR_ERR(dev->dev);
clear_bit(dev->dev_no, &device_no);
kfree(dev);
return rc;
rc = PTR_ERR(dev->dev);
dev->dev = NULL;
goto out;
}
device_create_file(dev->dev, &dev_attr_servo0);
if (dev->type & SERVO_COUNT_QUAD) {
device_create_file(dev->dev, &dev_attr_servo1);
device_create_file(dev->dev, &dev_attr_servo2);
device_create_file(dev->dev, &dev_attr_servo3);
servo_count = dev->type & SERVO_COUNT_QUAD ? 4 : 1;
for (i=0; i<servo_count; i++) {
rc = device_create_file(dev->dev, &dev_attrs[i]);
if (rc)
goto out2;
}
dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 attached\n",
dev->type & SERVO_COUNT_QUAD ? 4 : 1,
dev->type & SERVO_VERSION_30 ? 3 : 2);
servo_count, dev->type & SERVO_VERSION_30 ? 3 : 2);
if (!(dev->type & SERVO_VERSION_30))
dev_info(&interface->dev,
"WARNING: v2.0 not tested! Please report if it works.\n");
return 0;
out2:
while (i-- > 0)
device_remove_file(dev->dev, &dev_attrs[i]);
out:
if (dev) {
if (dev->dev)
device_unregister(dev->dev);
if (dev->dev_no >= 0)
clear_bit(dev->dev_no, &device_no);
kfree(dev);
}
return rc;
}
static void
servo_disconnect(struct usb_interface *interface)
{
struct phidget_servo *dev;
int servo_count, i;
dev = usb_get_intfdata(interface);
usb_set_intfdata(interface, NULL);
device_remove_file(dev->dev, &dev_attr_servo0);
if (dev->type & SERVO_COUNT_QUAD) {
device_remove_file(dev->dev, &dev_attr_servo1);
device_remove_file(dev->dev, &dev_attr_servo2);
device_remove_file(dev->dev, &dev_attr_servo3);
}
if (!dev)
return;
servo_count = dev->type & SERVO_COUNT_QUAD ? 4 : 1;
for (i=0; i<servo_count; i++)
device_remove_file(dev->dev, &dev_attrs[i]);
device_unregister(dev->dev);
usb_put_dev(dev->udev);
dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 detached\n",
dev->type & SERVO_COUNT_QUAD ? 4 : 1,
dev->type & SERVO_VERSION_30 ? 3 : 2);
servo_count, dev->type & SERVO_VERSION_30 ? 3 : 2);
clear_bit(dev->dev_no, &device_no);
kfree(dev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment