#include <linux/module.h>
#include <linux/usb.h>
+#include "phidget.h"
+
#define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
#define DRIVER_DESC "USB PhidgetMotorControl Driver"
#define URB_INT_SIZE 8
+static unsigned long device_no;
+
struct motorcontrol {
struct usb_device *udev;
struct usb_interface *intf;
+ struct device *dev;
+ int dev_no;
u8 inputs[4];
s8 desired_speed[2];
s8 speed[2];
unsigned char *data;
dma_addr_t data_dma;
- struct work_struct do_notify;
+ struct delayed_work do_notify;
unsigned long input_events;
unsigned long speed_events;
unsigned long exceed_events;
return retval < 0 ? retval : 0;
}
-static void motorcontrol_irq(struct urb *urb, struct pt_regs *regs)
+static void motorcontrol_irq(struct urb *urb)
{
struct motorcontrol *mc = urb->context;
unsigned char *buffer = mc->data;
set_bit(1, &mc->exceed_events);
if (mc->input_events || mc->exceed_events || mc->speed_events)
- schedule_work(&mc->do_notify);
+ schedule_delayed_work(&mc->do_notify, 0);
resubmit:
- status = usb_submit_urb(urb, SLAB_ATOMIC);
+ status = usb_submit_urb(urb, GFP_ATOMIC);
if (status)
dev_err(&mc->intf->dev,
"can't resubmit intr, %s-%s/motorcontrol0, status %d",
mc->udev->devpath, status);
}
-static void do_notify(void *data)
+static void do_notify(struct work_struct *work)
{
- struct motorcontrol *mc = data;
+ struct motorcontrol *mc =
+ container_of(work, struct motorcontrol, do_notify.work);
int i;
char sysfs_file[8];
for (i=0; i<4; i++) {
if (test_and_clear_bit(i, &mc->input_events)) {
sprintf(sysfs_file, "input%d", i);
- sysfs_notify(&mc->intf->dev.kobj, NULL, sysfs_file);
+ sysfs_notify(&mc->dev->kobj, NULL, sysfs_file);
}
}
for (i=0; i<2; i++) {
if (test_and_clear_bit(i, &mc->speed_events)) {
sprintf(sysfs_file, "speed%d", i);
- sysfs_notify(&mc->intf->dev.kobj, NULL, sysfs_file);
+ sysfs_notify(&mc->dev->kobj, NULL, sysfs_file);
}
}
}
#define show_set_speed(value) \
-static ssize_t set_speed##value(struct device *dev, \
- struct device_attribute *attr, const char *buf, size_t count) \
+static ssize_t set_speed##value(struct device *dev, \
+ struct device_attribute *attr, \
+ const char *buf, size_t count) \
{ \
- struct usb_interface *intf = to_usb_interface(dev); \
- struct motorcontrol *mc = usb_get_intfdata(intf); \
+ struct motorcontrol *mc = dev_get_drvdata(dev); \
int speed; \
int retval; \
\
return retval ? retval : count; \
} \
\
-static ssize_t show_speed##value(struct device *dev, \
- struct device_attribute *attr, char *buf) \
+static ssize_t show_speed##value(struct device *dev, \
+ struct device_attribute *attr, \
+ char *buf) \
{ \
- struct usb_interface *intf = to_usb_interface(dev); \
- struct motorcontrol *mc = usb_get_intfdata(intf); \
+ 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);
#define show_set_acceleration(value) \
static ssize_t set_acceleration##value(struct device *dev, \
- struct device_attribute *attr, const char *buf, size_t count) \
+ struct device_attribute *attr, \
+ const char *buf, size_t count) \
{ \
- struct usb_interface *intf = to_usb_interface(dev); \
- struct motorcontrol *mc = usb_get_intfdata(intf); \
+ struct motorcontrol *mc = dev_get_drvdata(dev); \
int acceleration; \
int retval; \
\
return retval ? retval : count; \
} \
\
-static ssize_t show_acceleration##value(struct device *dev, \
- struct device_attribute *attr, char *buf) \
+static ssize_t show_acceleration##value(struct device *dev, \
+ struct device_attribute *attr, \
+ char *buf) \
{ \
- struct usb_interface *intf = to_usb_interface(dev); \
- struct motorcontrol *mc = usb_get_intfdata(intf); \
+ 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);
#define show_current(value) \
-static ssize_t show_current##value(struct device *dev, \
- struct device_attribute *attr, char *buf) \
+static ssize_t show_current##value(struct device *dev, \
+ struct device_attribute *attr, \
+ char *buf) \
{ \
- struct usb_interface *intf = to_usb_interface(dev); \
- struct motorcontrol *mc = usb_get_intfdata(intf); \
+ 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);
#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 usb_interface *intf = to_usb_interface(dev); \
- struct motorcontrol *mc = usb_get_intfdata(intf); \
+ 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);
struct usb_endpoint_descriptor *endpoint;
struct motorcontrol *mc;
int pipe, maxp, rc = -ENOMEM;
+ int bit, value, i;
interface = intf->cur_altsetting;
if (interface->desc.bNumEndpoints != 1)
return -ENODEV;
endpoint = &interface->endpoint[0].desc;
- if (!(endpoint->bEndpointAddress & 0x80))
+ if (!usb_endpoint_dir_in(endpoint))
return -ENODEV;
/*
if (!mc)
goto out;
- mc->data = usb_buffer_alloc(dev, URB_INT_SIZE, SLAB_ATOMIC, &mc->data_dma);
+ mc->dev_no = -1;
+ mc->data = usb_buffer_alloc(dev, URB_INT_SIZE, GFP_ATOMIC, &mc->data_dma);
if (!mc->data)
goto out;
mc->udev = usb_get_dev(dev);
mc->intf = intf;
mc->acceleration[0] = mc->acceleration[1] = 10;
- INIT_WORK(&mc->do_notify, do_notify, mc);
+ INIT_DELAYED_WORK(&mc->do_notify, do_notify);
usb_fill_int_urb(mc->irq, mc->udev, pipe, mc->data,
maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
motorcontrol_irq, mc, endpoint->bInterval);
usb_set_intfdata(intf, mc);
- if (usb_submit_urb(mc->irq, GFP_KERNEL)) {
- rc = -EIO;
+ do {
+ bit = find_first_zero_bit(&device_no, sizeof(device_no));
+ value = test_and_set_bit(bit, &device_no);
+ } while(value);
+ mc->dev_no = bit;
+
+ mc->dev = device_create(phidget_class, &mc->udev->dev, 0,
+ "motorcontrol%d", mc->dev_no);
+ if (IS_ERR(mc->dev)) {
+ rc = PTR_ERR(mc->dev);
+ mc->dev = NULL;
goto out;
}
- device_create_file(&intf->dev, &dev_attr_input0);
- device_create_file(&intf->dev, &dev_attr_input1);
- device_create_file(&intf->dev, &dev_attr_input2);
- device_create_file(&intf->dev, &dev_attr_input3);
-
- device_create_file(&intf->dev, &dev_attr_speed0);
- device_create_file(&intf->dev, &dev_attr_speed1);
+ dev_set_drvdata(mc->dev, mc);
- device_create_file(&intf->dev, &dev_attr_acceleration0);
- device_create_file(&intf->dev, &dev_attr_acceleration1);
+ if (usb_submit_urb(mc->irq, GFP_KERNEL)) {
+ rc = -EIO;
+ goto out;
+ }
- device_create_file(&intf->dev, &dev_attr_current0);
- device_create_file(&intf->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 Phidget MotorControl attached\n");
+ 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)
- usb_free_urb(mc->irq);
+ usb_free_urb(mc->irq);
if (mc->data)
usb_buffer_free(dev, URB_INT_SIZE, mc->data, mc->data_dma);
+ if (mc->dev)
+ device_unregister(mc->dev);
+ if (mc->dev_no >= 0)
+ clear_bit(mc->dev_no, &device_no);
+
kfree(mc);
}
static void motorcontrol_disconnect(struct usb_interface *interface)
{
struct motorcontrol *mc;
+ int i;
mc = usb_get_intfdata(interface);
usb_set_intfdata(interface, NULL);
cancel_delayed_work(&mc->do_notify);
- device_remove_file(&interface->dev, &dev_attr_input0);
- device_remove_file(&interface->dev, &dev_attr_input1);
- device_remove_file(&interface->dev, &dev_attr_input2);
- device_remove_file(&interface->dev, &dev_attr_input3);
+ for (i=0; i<ARRAY_SIZE(dev_attrs); i++)
+ device_remove_file(mc->dev, &dev_attrs[i]);
- device_remove_file(&interface->dev, &dev_attr_speed0);
- device_remove_file(&interface->dev, &dev_attr_speed1);
-
- device_remove_file(&interface->dev, &dev_attr_acceleration0);
- device_remove_file(&interface->dev, &dev_attr_acceleration1);
-
- device_remove_file(&interface->dev, &dev_attr_current0);
- device_remove_file(&interface->dev, &dev_attr_current1);
-
- dev_info(&interface->dev, "USB Phidget MotorControl disconnected\n");
+ device_unregister(mc->dev);
usb_put_dev(mc->udev);
+ clear_bit(mc->dev_no, &device_no);
kfree(mc);
+
+ dev_info(&interface->dev, "USB PhidgetMotorControl detached\n");
}
static struct usb_driver motorcontrol_driver = {