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

USB: Put phidgets driver in a sysfs class

This patch creates a device class phidget and add the phidget drivers to 
them.
Signed-off-by: default avatarSean Young <sean@mess.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent d5176b41
......@@ -115,9 +115,16 @@ config USB_CYTHERM
To compile this driver as a module, choose M here: the
module will be called cytherm.
config USB_PHIDGET
tristate "USB Phidgets drivers"
depends on USB
help
Say Y here to enable the various drivers for devices from
Phidgets inc.
config USB_PHIDGETKIT
tristate "USB PhidgetInterfaceKit support"
depends on USB
depends on USB_PHIDGET
help
Say Y here if you want to connect a PhidgetInterfaceKit USB device
from Phidgets Inc.
......@@ -127,7 +134,7 @@ config USB_PHIDGETKIT
config USB_PHIDGETMOTORCONTROL
tristate "USB PhidgetMotorControl support"
depends on USB
depends on USB_PHIDGET
help
Say Y here if you want to connect a PhidgetMotorControl USB device
from Phidgets Inc.
......@@ -137,7 +144,7 @@ config USB_PHIDGETMOTORCONTROL
config USB_PHIDGETSERVO
tristate "USB PhidgetServo support"
depends on USB
depends on USB_PHIDGET
help
Say Y here if you want to connect an 1 or 4 Motor PhidgetServo
servo controller version 2.0 or 3.0.
......
......@@ -13,6 +13,7 @@ obj-$(CONFIG_USB_LCD) += usblcd.o
obj-$(CONFIG_USB_LD) += ldusb.o
obj-$(CONFIG_USB_LED) += usbled.o
obj-$(CONFIG_USB_LEGOTOWER) += legousbtower.o
obj-$(CONFIG_USB_PHIDGET) += phidget.o
obj-$(CONFIG_USB_PHIDGETKIT) += phidgetkit.o
obj-$(CONFIG_USB_PHIDGETMOTORCONTROL) += phidgetmotorcontrol.o
obj-$(CONFIG_USB_PHIDGETSERVO) += phidgetservo.o
......
/*
* USB Phidgets class
*
* Copyright (C) 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/device.h>
struct class *phidget_class;
static int __init init_phidget(void)
{
phidget_class = class_create(THIS_MODULE, "phidget");
if (IS_ERR(phidget_class))
return PTR_ERR(phidget_class);
return 0;
}
static void __exit cleanup_phidget(void)
{
class_destroy(phidget_class);
}
EXPORT_SYMBOL_GPL(phidget_class);
module_init(init_phidget);
module_exit(cleanup_phidget);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sean Young <sean@mess.org>");
MODULE_DESCRIPTION("Container module for phidget class");
/*
* USB Phidgets class
*
* Copyright (C) 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
extern struct class *phidget_class;
This diff is collapsed.
......@@ -15,6 +15,8 @@
#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"
......@@ -23,9 +25,13 @@
#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];
......@@ -162,14 +168,14 @@ static void do_notify(void *data)
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);
}
}
......@@ -181,11 +187,11 @@ static void do_notify(void *data)
}
#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; \
\
......@@ -202,11 +208,11 @@ static ssize_t set_speed##value(struct device *dev, \
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]); \
} \
......@@ -217,10 +223,10 @@ 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; \
\
......@@ -237,11 +243,11 @@ static ssize_t set_acceleration##value(struct device *dev, \
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]); \
} \
......@@ -251,11 +257,11 @@ 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]); \
} \
......@@ -265,11 +271,11 @@ 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]); \
} \
......@@ -287,6 +293,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;
interface = intf->cur_altsetting;
if (interface->desc.bNumEndpoints != 1)
......@@ -306,6 +313,7 @@ static int motorcontrol_probe(struct usb_interface *intf, const struct usb_devic
if (!mc)
goto out;
mc->dev_no = -1;
mc->data = usb_buffer_alloc(dev, URB_INT_SIZE, SLAB_ATOMIC, &mc->data_dma);
if (!mc->data)
goto out;
......@@ -326,26 +334,42 @@ static int motorcontrol_probe(struct usb_interface *intf, const struct usb_devic
usb_set_intfdata(intf, mc);
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;
}
dev_set_drvdata(mc->dev, mc);
if (usb_submit_urb(mc->irq, GFP_KERNEL)) {
rc = -EIO;
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(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(&intf->dev, &dev_attr_speed0);
device_create_file(&intf->dev, &dev_attr_speed1);
device_create_file(mc->dev, &dev_attr_speed0);
device_create_file(mc->dev, &dev_attr_speed1);
device_create_file(&intf->dev, &dev_attr_acceleration0);
device_create_file(&intf->dev, &dev_attr_acceleration1);
device_create_file(mc->dev, &dev_attr_acceleration0);
device_create_file(mc->dev, &dev_attr_acceleration1);
device_create_file(&intf->dev, &dev_attr_current0);
device_create_file(&intf->dev, &dev_attr_current1);
device_create_file(mc->dev, &dev_attr_current0);
device_create_file(mc->dev, &dev_attr_current1);
dev_info(&intf->dev, "USB Phidget MotorControl attached\n");
dev_info(&intf->dev, "USB PhidgetMotorControl attached\n");
return 0;
......@@ -355,6 +379,11 @@ static int motorcontrol_probe(struct usb_interface *intf, const struct usb_devic
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);
}
......@@ -376,24 +405,27 @@ static void motorcontrol_disconnect(struct usb_interface *interface)
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);
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(&interface->dev, &dev_attr_speed0);
device_remove_file(&interface->dev, &dev_attr_speed1);
device_remove_file(mc->dev, &dev_attr_speed0);
device_remove_file(mc->dev, &dev_attr_speed1);
device_remove_file(&interface->dev, &dev_attr_acceleration0);
device_remove_file(&interface->dev, &dev_attr_acceleration1);
device_remove_file(mc->dev, &dev_attr_acceleration0);
device_remove_file(mc->dev, &dev_attr_acceleration1);
device_remove_file(&interface->dev, &dev_attr_current0);
device_remove_file(&interface->dev, &dev_attr_current1);
device_remove_file(mc->dev, &dev_attr_current0);
device_remove_file(mc->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 = {
......
......@@ -15,14 +15,6 @@
*
* CAUTION: Generally you should use 0 < degrees < 180 as anything else
* is probably beyond the range of your servo and may damage it.
*
* Jun 16, 2004: Sean Young <sean@mess.org>
* - cleanups
* - was using memory after kfree()
* Aug 8, 2004: Sean Young <sean@mess.org>
* - set the highest angle as high as the hardware allows, there are
* some odd servos out there
*
*/
#include <linux/kernel.h>
......@@ -32,6 +24,8 @@
#include <linux/module.h>
#include <linux/usb.h>
#include "phidget.h"
#define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
#define DRIVER_DESC "USB PhidgetServo Driver"
......@@ -70,8 +64,12 @@ static struct usb_device_id id_table[] = {
MODULE_DEVICE_TABLE(usb, id_table);
static int unsigned long device_no;
struct phidget_servo {
struct usb_device *udev;
struct device *dev;
int dev_no;
ulong type;
int pulse[4];
int degrees[4];
......@@ -203,16 +201,16 @@ change_position_v20(struct phidget_servo *servo, int servo_no, int degrees,
}
#define show_set(value) \
static ssize_t set_servo##value (struct device *dev, struct device_attribute *attr, \
static ssize_t set_servo##value (struct device *dev, \
struct device_attribute *attr, \
const char *buf, size_t count) \
{ \
int degrees, minutes, retval; \
struct usb_interface *intf = to_usb_interface (dev); \
struct phidget_servo *servo = usb_get_intfdata (intf); \
struct phidget_servo *servo = dev_get_drvdata(dev); \
\
minutes = 0; \
/* must at least convert degrees */ \
if (sscanf (buf, "%d.%d", &degrees, &minutes) < 1) { \
if (sscanf(buf, "%d.%d", &degrees, &minutes) < 1) { \
return -EINVAL; \
} \
\
......@@ -220,21 +218,22 @@ static ssize_t set_servo##value (struct device *dev, struct device_attribute *at
return -EINVAL; \
\
if (servo->type & SERVO_VERSION_30) \
retval = change_position_v30 (servo, value, degrees, \
retval = change_position_v30(servo, value, degrees, \
minutes); \
else \
retval = change_position_v20 (servo, value, degrees, \
retval = change_position_v20(servo, value, degrees, \
minutes); \
\
return retval < 0 ? retval : count; \
} \
\
static ssize_t show_servo##value (struct device *dev, struct device_attribute *attr, char *buf) \
static ssize_t show_servo##value (struct device *dev, \
struct device_attribute *attr, \
char *buf) \
{ \
struct usb_interface *intf = to_usb_interface (dev); \
struct phidget_servo *servo = usb_get_intfdata (intf); \
struct phidget_servo *servo = dev_get_drvdata(dev); \
\
return sprintf (buf, "%d.%02d\n", servo->degrees[value], \
return sprintf(buf, "%d.%02d\n", servo->degrees[value], \
servo->minutes[value]); \
} \
static DEVICE_ATTR(servo##value, S_IWUGO | S_IRUGO, \
......@@ -250,6 +249,7 @@ 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;
dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL);
if (dev == NULL) {
......@@ -261,18 +261,33 @@ servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
dev->type = id->driver_info;
usb_set_intfdata(interface, dev);
device_create_file(&interface->dev, &dev_attr_servo0);
do {
bit = find_first_zero_bit(&device_no, sizeof(device_no));
value = test_and_set_bit(bit, &device_no);
} 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;
}
device_create_file(dev->dev, &dev_attr_servo0);
if (dev->type & SERVO_COUNT_QUAD) {
device_create_file(&interface->dev, &dev_attr_servo1);
device_create_file(&interface->dev, &dev_attr_servo2);
device_create_file(&interface->dev, &dev_attr_servo3);
device_create_file(dev->dev, &dev_attr_servo1);
device_create_file(dev->dev, &dev_attr_servo2);
device_create_file(dev->dev, &dev_attr_servo3);
}
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);
if(!(dev->type & SERVO_VERSION_30))
if (!(dev->type & SERVO_VERSION_30))
dev_info(&interface->dev,
"WARNING: v2.0 not tested! Please report if it works.\n");
......@@ -287,19 +302,21 @@ servo_disconnect(struct usb_interface *interface)
dev = usb_get_intfdata(interface);
usb_set_intfdata(interface, NULL);
device_remove_file(&interface->dev, &dev_attr_servo0);
device_remove_file(dev->dev, &dev_attr_servo0);
if (dev->type & SERVO_COUNT_QUAD) {
device_remove_file(&interface->dev, &dev_attr_servo1);
device_remove_file(&interface->dev, &dev_attr_servo2);
device_remove_file(&interface->dev, &dev_attr_servo3);
device_remove_file(dev->dev, &dev_attr_servo1);
device_remove_file(dev->dev, &dev_attr_servo2);
device_remove_file(dev->dev, &dev_attr_servo3);
}
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);
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