mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-21 00:10:09 +00:00
USB: Phidgets should check create_device_file() return value
device_create_file() could fail, add proper error paths for this condition. Signed-off-by: Sean Young <sean@mess.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
This commit is contained in:
parent
912b24c333
commit
da308e8da7
3 changed files with 199 additions and 196 deletions
|
@ -193,8 +193,11 @@ static ssize_t lcd_line_##number(struct device *dev, \
|
||||||
struct interfacekit *kit = dev_get_drvdata(dev); \
|
struct interfacekit *kit = dev_get_drvdata(dev); \
|
||||||
change_string(kit, buf, number - 1); \
|
change_string(kit, buf, number - 1); \
|
||||||
return count; \
|
return count; \
|
||||||
} \
|
}
|
||||||
static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
|
|
||||||
|
#define lcd_line_attr(number) \
|
||||||
|
__ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number)
|
||||||
|
|
||||||
set_lcd_line(1);
|
set_lcd_line(1);
|
||||||
set_lcd_line(2);
|
set_lcd_line(2);
|
||||||
|
|
||||||
|
@ -232,15 +235,22 @@ static ssize_t set_backlight(struct device *dev, struct device_attribute *attr,
|
||||||
kfree(buffer);
|
kfree(buffer);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
static DEVICE_ATTR(backlight, S_IWUGO, NULL, set_backlight);
|
|
||||||
|
static struct device_attribute dev_lcd_line_attrs[] = {
|
||||||
|
lcd_line_attr(1),
|
||||||
|
lcd_line_attr(2),
|
||||||
|
__ATTR(backlight, S_IWUGO, NULL, set_backlight)
|
||||||
|
};
|
||||||
|
|
||||||
static void remove_lcd_files(struct interfacekit *kit)
|
static void remove_lcd_files(struct interfacekit *kit)
|
||||||
{
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
if (kit->lcd_files_on) {
|
if (kit->lcd_files_on) {
|
||||||
dev_dbg(&kit->udev->dev, "Removing lcd files\n");
|
dev_dbg(&kit->udev->dev, "Removing lcd files\n");
|
||||||
device_remove_file(kit->dev, &dev_attr_lcd_line_1);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_lcd_line_2);
|
for (i=0; i<ARRAY_SIZE(dev_lcd_line_attrs); i++)
|
||||||
device_remove_file(kit->dev, &dev_attr_backlight);
|
device_remove_file(kit->dev, &dev_lcd_line_attrs[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,6 +258,7 @@ static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *att
|
||||||
{
|
{
|
||||||
struct interfacekit *kit = dev_get_drvdata(dev);
|
struct interfacekit *kit = dev_get_drvdata(dev);
|
||||||
int enable;
|
int enable;
|
||||||
|
int i, rc;
|
||||||
|
|
||||||
if (kit->ifkit->has_lcd == 0)
|
if (kit->ifkit->has_lcd == 0)
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
|
@ -258,9 +269,12 @@ static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *att
|
||||||
if (enable) {
|
if (enable) {
|
||||||
if (!kit->lcd_files_on) {
|
if (!kit->lcd_files_on) {
|
||||||
dev_dbg(&kit->udev->dev, "Adding lcd files\n");
|
dev_dbg(&kit->udev->dev, "Adding lcd files\n");
|
||||||
device_create_file(kit->dev, &dev_attr_lcd_line_1);
|
for (i=0; i<ARRAY_SIZE(dev_lcd_line_attrs); i++) {
|
||||||
device_create_file(kit->dev, &dev_attr_lcd_line_2);
|
rc = device_create_file(kit->dev,
|
||||||
device_create_file(kit->dev, &dev_attr_backlight);
|
&dev_lcd_line_attrs[i]);
|
||||||
|
if (rc)
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
kit->lcd_files_on = 1;
|
kit->lcd_files_on = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -271,7 +285,13 @@ static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *att
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
out:
|
||||||
|
while (i-- > 0)
|
||||||
|
device_remove_file(kit->dev, &dev_lcd_line_attrs[i]);
|
||||||
|
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
|
static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
|
||||||
|
|
||||||
static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
|
static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
|
||||||
|
@ -403,9 +423,12 @@ static ssize_t show_output##value(struct device *dev, \
|
||||||
struct interfacekit *kit = dev_get_drvdata(dev); \
|
struct interfacekit *kit = dev_get_drvdata(dev); \
|
||||||
\
|
\
|
||||||
return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
|
return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
|
||||||
} \
|
}
|
||||||
static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO, \
|
|
||||||
show_output##value, set_output##value);
|
#define output_attr(value) \
|
||||||
|
__ATTR(output##value, S_IWUGO | S_IRUGO, \
|
||||||
|
show_output##value, set_output##value)
|
||||||
|
|
||||||
show_set_output(1);
|
show_set_output(1);
|
||||||
show_set_output(2);
|
show_set_output(2);
|
||||||
show_set_output(3);
|
show_set_output(3);
|
||||||
|
@ -423,14 +446,24 @@ show_set_output(14);
|
||||||
show_set_output(15);
|
show_set_output(15);
|
||||||
show_set_output(16);
|
show_set_output(16);
|
||||||
|
|
||||||
|
static struct device_attribute dev_output_attrs[] = {
|
||||||
|
output_attr(1), output_attr(2), output_attr(3), output_attr(4),
|
||||||
|
output_attr(5), output_attr(6), output_attr(7), output_attr(8),
|
||||||
|
output_attr(9), output_attr(10), output_attr(11), output_attr(12),
|
||||||
|
output_attr(13), output_attr(14), output_attr(15), output_attr(16)
|
||||||
|
};
|
||||||
|
|
||||||
#define show_input(value) \
|
#define show_input(value) \
|
||||||
static ssize_t show_input##value(struct device *dev, struct device_attribute *attr, char *buf) \
|
static ssize_t show_input##value(struct device *dev, \
|
||||||
|
struct device_attribute *attr, char *buf) \
|
||||||
{ \
|
{ \
|
||||||
struct interfacekit *kit = dev_get_drvdata(dev); \
|
struct interfacekit *kit = dev_get_drvdata(dev); \
|
||||||
\
|
\
|
||||||
return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]); \
|
return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]); \
|
||||||
} \
|
}
|
||||||
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(1);
|
show_input(1);
|
||||||
show_input(2);
|
show_input(2);
|
||||||
|
@ -449,6 +482,13 @@ show_input(14);
|
||||||
show_input(15);
|
show_input(15);
|
||||||
show_input(16);
|
show_input(16);
|
||||||
|
|
||||||
|
static struct device_attribute dev_input_attrs[] = {
|
||||||
|
input_attr(1), input_attr(2), input_attr(3), input_attr(4),
|
||||||
|
input_attr(5), input_attr(6), input_attr(7), input_attr(8),
|
||||||
|
input_attr(9), input_attr(10), input_attr(11), input_attr(12),
|
||||||
|
input_attr(13), input_attr(14), input_attr(15), input_attr(16)
|
||||||
|
};
|
||||||
|
|
||||||
#define show_sensor(value) \
|
#define show_sensor(value) \
|
||||||
static ssize_t show_sensor##value(struct device *dev, \
|
static ssize_t show_sensor##value(struct device *dev, \
|
||||||
struct device_attribute *attr, \
|
struct device_attribute *attr, \
|
||||||
|
@ -457,8 +497,10 @@ static ssize_t show_sensor##value(struct device *dev, \
|
||||||
struct interfacekit *kit = dev_get_drvdata(dev); \
|
struct interfacekit *kit = dev_get_drvdata(dev); \
|
||||||
\
|
\
|
||||||
return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]); \
|
return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]); \
|
||||||
} \
|
}
|
||||||
static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);
|
|
||||||
|
#define sensor_attr(value) \
|
||||||
|
__ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL)
|
||||||
|
|
||||||
show_sensor(1);
|
show_sensor(1);
|
||||||
show_sensor(2);
|
show_sensor(2);
|
||||||
|
@ -469,6 +511,11 @@ show_sensor(6);
|
||||||
show_sensor(7);
|
show_sensor(7);
|
||||||
show_sensor(8);
|
show_sensor(8);
|
||||||
|
|
||||||
|
static struct device_attribute dev_sensor_attrs[] = {
|
||||||
|
sensor_attr(1), sensor_attr(2), sensor_attr(3), sensor_attr(4),
|
||||||
|
sensor_attr(5), sensor_attr(6), sensor_attr(7), sensor_attr(8)
|
||||||
|
};
|
||||||
|
|
||||||
static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
|
static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
|
||||||
{
|
{
|
||||||
struct usb_device *dev = interface_to_usbdev(intf);
|
struct usb_device *dev = interface_to_usbdev(intf);
|
||||||
|
@ -477,7 +524,7 @@ static int interfacekit_probe(struct usb_interface *intf, const struct usb_devic
|
||||||
struct interfacekit *kit;
|
struct interfacekit *kit;
|
||||||
struct driver_interfacekit *ifkit;
|
struct driver_interfacekit *ifkit;
|
||||||
int pipe, maxp, rc = -ENOMEM;
|
int pipe, maxp, rc = -ENOMEM;
|
||||||
int bit, value;
|
int bit, value, i;
|
||||||
|
|
||||||
ifkit = (struct driver_interfacekit *)id->driver_info;
|
ifkit = (struct driver_interfacekit *)id->driver_info;
|
||||||
if (!ifkit)
|
if (!ifkit)
|
||||||
|
@ -541,74 +588,49 @@ static int interfacekit_probe(struct usb_interface *intf, const struct usb_devic
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ifkit->outputs >= 4) {
|
for (i=0; i<ifkit->outputs; i++ ) {
|
||||||
device_create_file(kit->dev, &dev_attr_output1);
|
rc = device_create_file(kit->dev, &dev_output_attrs[i]);
|
||||||
device_create_file(kit->dev, &dev_attr_output2);
|
if (rc)
|
||||||
device_create_file(kit->dev, &dev_attr_output3);
|
goto out2;
|
||||||
device_create_file(kit->dev, &dev_attr_output4);
|
|
||||||
}
|
|
||||||
if (ifkit->outputs >= 8) {
|
|
||||||
device_create_file(kit->dev, &dev_attr_output5);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output6);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output7);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output8);
|
|
||||||
}
|
|
||||||
if (ifkit->outputs == 16) {
|
|
||||||
device_create_file(kit->dev, &dev_attr_output9);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output10);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output11);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output12);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output13);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output14);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output15);
|
|
||||||
device_create_file(kit->dev, &dev_attr_output16);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ifkit->inputs >= 4) {
|
for (i=0; i<ifkit->inputs; i++ ) {
|
||||||
device_create_file(kit->dev, &dev_attr_input1);
|
rc = device_create_file(kit->dev, &dev_input_attrs[i]);
|
||||||
device_create_file(kit->dev, &dev_attr_input2);
|
if (rc)
|
||||||
device_create_file(kit->dev, &dev_attr_input3);
|
goto out3;
|
||||||
device_create_file(kit->dev, &dev_attr_input4);
|
|
||||||
}
|
|
||||||
if (ifkit->inputs >= 8) {
|
|
||||||
device_create_file(kit->dev, &dev_attr_input5);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input6);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input7);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input8);
|
|
||||||
}
|
|
||||||
if (ifkit->inputs == 16) {
|
|
||||||
device_create_file(kit->dev, &dev_attr_input9);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input10);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input11);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input12);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input13);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input14);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input15);
|
|
||||||
device_create_file(kit->dev, &dev_attr_input16);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ifkit->sensors >= 4) {
|
for (i=0; i<ifkit->sensors; i++ ) {
|
||||||
device_create_file(kit->dev, &dev_attr_sensor1);
|
rc = device_create_file(kit->dev, &dev_sensor_attrs[i]);
|
||||||
device_create_file(kit->dev, &dev_attr_sensor2);
|
if (rc)
|
||||||
device_create_file(kit->dev, &dev_attr_sensor3);
|
goto out4;
|
||||||
device_create_file(kit->dev, &dev_attr_sensor4);
|
|
||||||
}
|
}
|
||||||
if (ifkit->sensors >= 7) {
|
|
||||||
device_create_file(kit->dev, &dev_attr_sensor5);
|
|
||||||
device_create_file(kit->dev, &dev_attr_sensor6);
|
|
||||||
device_create_file(kit->dev, &dev_attr_sensor7);
|
|
||||||
}
|
|
||||||
if (ifkit->sensors == 8)
|
|
||||||
device_create_file(kit->dev, &dev_attr_sensor8);
|
|
||||||
|
|
||||||
if (ifkit->has_lcd)
|
if (ifkit->has_lcd) {
|
||||||
device_create_file(kit->dev, &dev_attr_lcd);
|
rc = device_create_file(kit->dev, &dev_attr_lcd);
|
||||||
|
if (rc)
|
||||||
|
goto out4;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
|
dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
|
||||||
ifkit->sensors, ifkit->inputs, ifkit->outputs);
|
ifkit->sensors, ifkit->inputs, ifkit->outputs);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
out4:
|
||||||
|
while (i-- > 0)
|
||||||
|
device_remove_file(kit->dev, &dev_sensor_attrs[i]);
|
||||||
|
|
||||||
|
i = ifkit->inputs;
|
||||||
|
out3:
|
||||||
|
while (i-- > 0)
|
||||||
|
device_remove_file(kit->dev, &dev_input_attrs[i]);
|
||||||
|
|
||||||
|
i = ifkit->outputs;
|
||||||
|
out2:
|
||||||
|
while (i-- > 0)
|
||||||
|
device_remove_file(kit->dev, &dev_output_attrs[i]);
|
||||||
out:
|
out:
|
||||||
if (kit) {
|
if (kit) {
|
||||||
if (kit->irq)
|
if (kit->irq)
|
||||||
|
@ -629,6 +651,7 @@ static int interfacekit_probe(struct usb_interface *intf, const struct usb_devic
|
||||||
static void interfacekit_disconnect(struct usb_interface *interface)
|
static void interfacekit_disconnect(struct usb_interface *interface)
|
||||||
{
|
{
|
||||||
struct interfacekit *kit;
|
struct interfacekit *kit;
|
||||||
|
int i;
|
||||||
|
|
||||||
kit = usb_get_intfdata(interface);
|
kit = usb_get_intfdata(interface);
|
||||||
usb_set_intfdata(interface, NULL);
|
usb_set_intfdata(interface, NULL);
|
||||||
|
@ -641,68 +664,19 @@ static void interfacekit_disconnect(struct usb_interface *interface)
|
||||||
|
|
||||||
cancel_delayed_work(&kit->do_notify);
|
cancel_delayed_work(&kit->do_notify);
|
||||||
|
|
||||||
if (kit->ifkit->outputs >= 4) {
|
for (i=0; i<kit->ifkit->outputs; i++)
|
||||||
device_remove_file(kit->dev, &dev_attr_output1);
|
device_remove_file(kit->dev, &dev_output_attrs[i]);
|
||||||
device_remove_file(kit->dev, &dev_attr_output2);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output3);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output4);
|
|
||||||
}
|
|
||||||
if (kit->ifkit->outputs >= 8) {
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output5);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output6);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output7);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output8);
|
|
||||||
}
|
|
||||||
if (kit->ifkit->outputs == 16) {
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output9);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output10);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output11);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output12);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output13);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output14);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output15);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_output16);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (kit->ifkit->inputs >= 4) {
|
for (i=0; i<kit->ifkit->inputs; i++)
|
||||||
device_remove_file(kit->dev, &dev_attr_input1);
|
device_remove_file(kit->dev, &dev_input_attrs[i]);
|
||||||
device_remove_file(kit->dev, &dev_attr_input2);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input3);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input4);
|
|
||||||
}
|
|
||||||
if (kit->ifkit->inputs >= 8) {
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input5);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input6);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input7);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input8);
|
|
||||||
}
|
|
||||||
if (kit->ifkit->inputs == 16) {
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input9);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input10);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input11);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input12);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input13);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input14);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input15);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_input16);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (kit->ifkit->sensors >= 4) {
|
for (i=0; i<kit->ifkit->sensors; i++)
|
||||||
device_remove_file(kit->dev, &dev_attr_sensor1);
|
device_remove_file(kit->dev, &dev_sensor_attrs[i]);
|
||||||
device_remove_file(kit->dev, &dev_attr_sensor2);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_sensor3);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_sensor4);
|
|
||||||
}
|
|
||||||
if (kit->ifkit->sensors >= 7) {
|
|
||||||
device_remove_file(kit->dev, &dev_attr_sensor5);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_sensor6);
|
|
||||||
device_remove_file(kit->dev, &dev_attr_sensor7);
|
|
||||||
}
|
|
||||||
if (kit->ifkit->sensors == 8)
|
|
||||||
device_remove_file(kit->dev, &dev_attr_sensor8);
|
|
||||||
|
|
||||||
if (kit->ifkit->has_lcd)
|
if (kit->ifkit->has_lcd) {
|
||||||
device_remove_file(kit->dev, &dev_attr_lcd);
|
device_remove_file(kit->dev, &dev_attr_lcd);
|
||||||
|
remove_lcd_files(kit);
|
||||||
|
}
|
||||||
|
|
||||||
device_unregister(kit->dev);
|
device_unregister(kit->dev);
|
||||||
|
|
||||||
|
|
|
@ -215,9 +215,12 @@ static ssize_t show_speed##value(struct device *dev, \
|
||||||
struct motorcontrol *mc = dev_get_drvdata(dev); \
|
struct motorcontrol *mc = dev_get_drvdata(dev); \
|
||||||
\
|
\
|
||||||
return sprintf(buf, "%d\n", mc->speed[value]); \
|
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(0);
|
||||||
show_set_speed(1);
|
show_set_speed(1);
|
||||||
|
|
||||||
|
@ -250,9 +253,12 @@ static ssize_t show_acceleration##value(struct device *dev, \
|
||||||
struct motorcontrol *mc = dev_get_drvdata(dev); \
|
struct motorcontrol *mc = dev_get_drvdata(dev); \
|
||||||
\
|
\
|
||||||
return sprintf(buf, "%d\n", mc->acceleration[value]); \
|
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(0);
|
||||||
show_set_acceleration(1);
|
show_set_acceleration(1);
|
||||||
|
|
||||||
|
@ -264,8 +270,10 @@ static ssize_t show_current##value(struct device *dev, \
|
||||||
struct motorcontrol *mc = dev_get_drvdata(dev); \
|
struct motorcontrol *mc = dev_get_drvdata(dev); \
|
||||||
\
|
\
|
||||||
return sprintf(buf, "%dmA\n", (int)mc->_current[value]); \
|
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(0);
|
||||||
show_current(1);
|
show_current(1);
|
||||||
|
@ -278,14 +286,29 @@ static ssize_t show_input##value(struct device *dev, \
|
||||||
struct motorcontrol *mc = dev_get_drvdata(dev); \
|
struct motorcontrol *mc = dev_get_drvdata(dev); \
|
||||||
\
|
\
|
||||||
return sprintf(buf, "%d\n", (int)mc->inputs[value]); \
|
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(0);
|
||||||
show_input(1);
|
show_input(1);
|
||||||
show_input(2);
|
show_input(2);
|
||||||
show_input(3);
|
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)
|
static int motorcontrol_probe(struct usb_interface *intf, const struct usb_device_id *id)
|
||||||
{
|
{
|
||||||
struct usb_device *dev = interface_to_usbdev(intf);
|
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 usb_endpoint_descriptor *endpoint;
|
||||||
struct motorcontrol *mc;
|
struct motorcontrol *mc;
|
||||||
int pipe, maxp, rc = -ENOMEM;
|
int pipe, maxp, rc = -ENOMEM;
|
||||||
int bit, value;
|
int bit, value, i;
|
||||||
|
|
||||||
interface = intf->cur_altsetting;
|
interface = intf->cur_altsetting;
|
||||||
if (interface->desc.bNumEndpoints != 1)
|
if (interface->desc.bNumEndpoints != 1)
|
||||||
|
@ -355,24 +378,18 @@ static int motorcontrol_probe(struct usb_interface *intf, const struct usb_devic
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
device_create_file(mc->dev, &dev_attr_input0);
|
for (i=0; i<ARRAY_SIZE(dev_attrs); i++) {
|
||||||
device_create_file(mc->dev, &dev_attr_input1);
|
rc = device_create_file(mc->dev, &dev_attrs[i]);
|
||||||
device_create_file(mc->dev, &dev_attr_input2);
|
if (rc)
|
||||||
device_create_file(mc->dev, &dev_attr_input3);
|
goto out2;
|
||||||
|
}
|
||||||
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);
|
|
||||||
|
|
||||||
dev_info(&intf->dev, "USB PhidgetMotorControl attached\n");
|
dev_info(&intf->dev, "USB PhidgetMotorControl attached\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
out2:
|
||||||
|
while (i-- > 0)
|
||||||
|
device_remove_file(mc->dev, &dev_attrs[i]);
|
||||||
out:
|
out:
|
||||||
if (mc) {
|
if (mc) {
|
||||||
if (mc->irq)
|
if (mc->irq)
|
||||||
|
@ -393,6 +410,7 @@ static int motorcontrol_probe(struct usb_interface *intf, const struct usb_devic
|
||||||
static void motorcontrol_disconnect(struct usb_interface *interface)
|
static void motorcontrol_disconnect(struct usb_interface *interface)
|
||||||
{
|
{
|
||||||
struct motorcontrol *mc;
|
struct motorcontrol *mc;
|
||||||
|
int i;
|
||||||
|
|
||||||
mc = usb_get_intfdata(interface);
|
mc = usb_get_intfdata(interface);
|
||||||
usb_set_intfdata(interface, NULL);
|
usb_set_intfdata(interface, NULL);
|
||||||
|
@ -405,19 +423,8 @@ static void motorcontrol_disconnect(struct usb_interface *interface)
|
||||||
|
|
||||||
cancel_delayed_work(&mc->do_notify);
|
cancel_delayed_work(&mc->do_notify);
|
||||||
|
|
||||||
device_remove_file(mc->dev, &dev_attr_input0);
|
for (i=0; i<ARRAY_SIZE(dev_attrs); i++)
|
||||||
device_remove_file(mc->dev, &dev_attr_input1);
|
device_remove_file(mc->dev, &dev_attrs[i]);
|
||||||
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);
|
|
||||||
|
|
||||||
device_unregister(mc->dev);
|
device_unregister(mc->dev);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* USB PhidgetServo driver 1.0
|
* 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
|
* 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
|
* 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], \
|
return sprintf(buf, "%d.%02d\n", servo->degrees[value], \
|
||||||
servo->minutes[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(0);
|
||||||
show_set(1);
|
show_set(1);
|
||||||
show_set(2);
|
show_set(2);
|
||||||
show_set(3);
|
show_set(3);
|
||||||
|
|
||||||
|
static struct device_attribute dev_attrs[] = {
|
||||||
|
servo_attr(0), servo_attr(1), servo_attr(2), servo_attr(3)
|
||||||
|
};
|
||||||
|
|
||||||
static int
|
static int
|
||||||
servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
|
servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
|
||||||
{
|
{
|
||||||
struct usb_device *udev = interface_to_usbdev(interface);
|
struct usb_device *udev = interface_to_usbdev(interface);
|
||||||
struct phidget_servo *dev;
|
struct phidget_servo *dev;
|
||||||
int bit, value;
|
int bit, value, rc;
|
||||||
|
int servo_count, i;
|
||||||
|
|
||||||
dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL);
|
dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL);
|
||||||
if (dev == NULL) {
|
if (dev == NULL) {
|
||||||
dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
|
dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
|
||||||
return -ENOMEM;
|
rc = -ENOMEM;
|
||||||
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
dev->udev = usb_get_dev(udev);
|
dev->udev = usb_get_dev(udev);
|
||||||
dev->type = id->driver_info;
|
dev->type = id->driver_info;
|
||||||
|
dev->dev_no = -1;
|
||||||
usb_set_intfdata(interface, dev);
|
usb_set_intfdata(interface, dev);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
bit = find_first_zero_bit(&device_no, sizeof(device_no));
|
bit = find_first_zero_bit(&device_no, sizeof(device_no));
|
||||||
value = test_and_set_bit(bit, &device_no);
|
value = test_and_set_bit(bit, &device_no);
|
||||||
} while(value);
|
} while (value);
|
||||||
dev->dev_no = bit;
|
dev->dev_no = bit;
|
||||||
|
|
||||||
dev->dev = device_create(phidget_class, &dev->udev->dev, 0,
|
dev->dev = device_create(phidget_class, &dev->udev->dev, 0,
|
||||||
"servo%d", dev->dev_no);
|
"servo%d", dev->dev_no);
|
||||||
if (IS_ERR(dev->dev)) {
|
if (IS_ERR(dev->dev)) {
|
||||||
int rc = PTR_ERR(dev->dev);
|
rc = PTR_ERR(dev->dev);
|
||||||
clear_bit(dev->dev_no, &device_no);
|
dev->dev = NULL;
|
||||||
kfree(dev);
|
goto out;
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
device_create_file(dev->dev, &dev_attr_servo0);
|
servo_count = dev->type & SERVO_COUNT_QUAD ? 4 : 1;
|
||||||
if (dev->type & SERVO_COUNT_QUAD) {
|
|
||||||
device_create_file(dev->dev, &dev_attr_servo1);
|
for (i=0; i<servo_count; i++) {
|
||||||
device_create_file(dev->dev, &dev_attr_servo2);
|
rc = device_create_file(dev->dev, &dev_attrs[i]);
|
||||||
device_create_file(dev->dev, &dev_attr_servo3);
|
if (rc)
|
||||||
|
goto out2;
|
||||||
}
|
}
|
||||||
|
|
||||||
dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 attached\n",
|
dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 attached\n",
|
||||||
dev->type & SERVO_COUNT_QUAD ? 4 : 1,
|
servo_count, dev->type & SERVO_VERSION_30 ? 3 : 2);
|
||||||
dev->type & SERVO_VERSION_30 ? 3 : 2);
|
|
||||||
|
|
||||||
if (!(dev->type & SERVO_VERSION_30))
|
if (!(dev->type & SERVO_VERSION_30))
|
||||||
dev_info(&interface->dev,
|
dev_info(&interface->dev,
|
||||||
"WARNING: v2.0 not tested! Please report if it works.\n");
|
"WARNING: v2.0 not tested! Please report if it works.\n");
|
||||||
|
|
||||||
return 0;
|
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
|
static void
|
||||||
servo_disconnect(struct usb_interface *interface)
|
servo_disconnect(struct usb_interface *interface)
|
||||||
{
|
{
|
||||||
struct phidget_servo *dev;
|
struct phidget_servo *dev;
|
||||||
|
int servo_count, i;
|
||||||
|
|
||||||
dev = usb_get_intfdata(interface);
|
dev = usb_get_intfdata(interface);
|
||||||
usb_set_intfdata(interface, NULL);
|
usb_set_intfdata(interface, NULL);
|
||||||
|
|
||||||
device_remove_file(dev->dev, &dev_attr_servo0);
|
if (!dev)
|
||||||
if (dev->type & SERVO_COUNT_QUAD) {
|
return;
|
||||||
device_remove_file(dev->dev, &dev_attr_servo1);
|
|
||||||
device_remove_file(dev->dev, &dev_attr_servo2);
|
servo_count = dev->type & SERVO_COUNT_QUAD ? 4 : 1;
|
||||||
device_remove_file(dev->dev, &dev_attr_servo3);
|
|
||||||
}
|
for (i=0; i<servo_count; i++)
|
||||||
|
device_remove_file(dev->dev, &dev_attrs[i]);
|
||||||
|
|
||||||
device_unregister(dev->dev);
|
device_unregister(dev->dev);
|
||||||
usb_put_dev(dev->udev);
|
usb_put_dev(dev->udev);
|
||||||
|
|
||||||
dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 detached\n",
|
dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 detached\n",
|
||||||
dev->type & SERVO_COUNT_QUAD ? 4 : 1,
|
servo_count, dev->type & SERVO_VERSION_30 ? 3 : 2);
|
||||||
dev->type & SERVO_VERSION_30 ? 3 : 2);
|
|
||||||
|
|
||||||
clear_bit(dev->dev_no, &device_no);
|
clear_bit(dev->dev_no, &device_no);
|
||||||
kfree(dev);
|
kfree(dev);
|
||||||
|
|
Loading…
Reference in a new issue