aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/misc/phidgetservo.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/misc/phidgetservo.c')
-rw-r--r--drivers/usb/misc/phidgetservo.c71
1 files changed, 44 insertions, 27 deletions
diff --git a/drivers/usb/misc/phidgetservo.c b/drivers/usb/misc/phidgetservo.c
index c0df79c96538..66be9513fd6f 100644
--- a/drivers/usb/misc/phidgetservo.c
+++ b/drivers/usb/misc/phidgetservo.c
@@ -15,14 +15,6 @@
15 * 15 *
16 * CAUTION: Generally you should use 0 < degrees < 180 as anything else 16 * CAUTION: Generally you should use 0 < degrees < 180 as anything else
17 * is probably beyond the range of your servo and may damage it. 17 * is probably beyond the range of your servo and may damage it.
18 *
19 * Jun 16, 2004: Sean Young <sean@mess.org>
20 * - cleanups
21 * - was using memory after kfree()
22 * Aug 8, 2004: Sean Young <sean@mess.org>
23 * - set the highest angle as high as the hardware allows, there are
24 * some odd servos out there
25 *
26 */ 18 */
27 19
28#include <linux/kernel.h> 20#include <linux/kernel.h>
@@ -32,6 +24,8 @@
32#include <linux/module.h> 24#include <linux/module.h>
33#include <linux/usb.h> 25#include <linux/usb.h>
34 26
27#include "phidget.h"
28
35#define DRIVER_AUTHOR "Sean Young <sean@mess.org>" 29#define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
36#define DRIVER_DESC "USB PhidgetServo Driver" 30#define DRIVER_DESC "USB PhidgetServo Driver"
37 31
@@ -70,8 +64,12 @@ static struct usb_device_id id_table[] = {
70 64
71MODULE_DEVICE_TABLE(usb, id_table); 65MODULE_DEVICE_TABLE(usb, id_table);
72 66
67static int unsigned long device_no;
68
73struct phidget_servo { 69struct phidget_servo {
74 struct usb_device *udev; 70 struct usb_device *udev;
71 struct device *dev;
72 int dev_no;
75 ulong type; 73 ulong type;
76 int pulse[4]; 74 int pulse[4];
77 int degrees[4]; 75 int degrees[4];
@@ -203,16 +201,16 @@ change_position_v20(struct phidget_servo *servo, int servo_no, int degrees,
203} 201}
204 202
205#define show_set(value) \ 203#define show_set(value) \
206static ssize_t set_servo##value (struct device *dev, struct device_attribute *attr, \ 204static ssize_t set_servo##value (struct device *dev, \
205 struct device_attribute *attr, \
207 const char *buf, size_t count) \ 206 const char *buf, size_t count) \
208{ \ 207{ \
209 int degrees, minutes, retval; \ 208 int degrees, minutes, retval; \
210 struct usb_interface *intf = to_usb_interface (dev); \ 209 struct phidget_servo *servo = dev_get_drvdata(dev); \
211 struct phidget_servo *servo = usb_get_intfdata (intf); \
212 \ 210 \
213 minutes = 0; \ 211 minutes = 0; \
214 /* must at least convert degrees */ \ 212 /* must at least convert degrees */ \
215 if (sscanf (buf, "%d.%d", &degrees, &minutes) < 1) { \ 213 if (sscanf(buf, "%d.%d", &degrees, &minutes) < 1) { \
216 return -EINVAL; \ 214 return -EINVAL; \
217 } \ 215 } \
218 \ 216 \
@@ -220,21 +218,22 @@ static ssize_t set_servo##value (struct device *dev, struct device_attribute *at
220 return -EINVAL; \ 218 return -EINVAL; \
221 \ 219 \
222 if (servo->type & SERVO_VERSION_30) \ 220 if (servo->type & SERVO_VERSION_30) \
223 retval = change_position_v30 (servo, value, degrees, \ 221 retval = change_position_v30(servo, value, degrees, \
224 minutes); \ 222 minutes); \
225 else \ 223 else \
226 retval = change_position_v20 (servo, value, degrees, \ 224 retval = change_position_v20(servo, value, degrees, \
227 minutes); \ 225 minutes); \
228 \ 226 \
229 return retval < 0 ? retval : count; \ 227 return retval < 0 ? retval : count; \
230} \ 228} \
231 \ 229 \
232static ssize_t show_servo##value (struct device *dev, struct device_attribute *attr, char *buf) \ 230static ssize_t show_servo##value (struct device *dev, \
231 struct device_attribute *attr, \
232 char *buf) \
233{ \ 233{ \
234 struct usb_interface *intf = to_usb_interface (dev); \ 234 struct phidget_servo *servo = dev_get_drvdata(dev); \
235 struct phidget_servo *servo = usb_get_intfdata (intf); \
236 \ 235 \
237 return sprintf (buf, "%d.%02d\n", servo->degrees[value], \ 236 return sprintf(buf, "%d.%02d\n", servo->degrees[value], \
238 servo->minutes[value]); \ 237 servo->minutes[value]); \
239} \ 238} \
240static DEVICE_ATTR(servo##value, S_IWUGO | S_IRUGO, \ 239static DEVICE_ATTR(servo##value, S_IWUGO | S_IRUGO, \
@@ -250,6 +249,7 @@ servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
250{ 249{
251 struct usb_device *udev = interface_to_usbdev(interface); 250 struct usb_device *udev = interface_to_usbdev(interface);
252 struct phidget_servo *dev; 251 struct phidget_servo *dev;
252 int bit, value;
253 253
254 dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL); 254 dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL);
255 if (dev == NULL) { 255 if (dev == NULL) {
@@ -261,18 +261,33 @@ servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
261 dev->type = id->driver_info; 261 dev->type = id->driver_info;
262 usb_set_intfdata(interface, dev); 262 usb_set_intfdata(interface, dev);
263 263
264 device_create_file(&interface->dev, &dev_attr_servo0); 264 do {
265 bit = find_first_zero_bit(&device_no, sizeof(device_no));
266 value = test_and_set_bit(bit, &device_no);
267 } while(value);
268 dev->dev_no = bit;
269
270 dev->dev = device_create(phidget_class, &dev->udev->dev, 0,
271 "servo%d", dev->dev_no);
272 if (IS_ERR(dev->dev)) {
273 int rc = PTR_ERR(dev->dev);
274 clear_bit(dev->dev_no, &device_no);
275 kfree(dev);
276 return rc;
277 }
278
279 device_create_file(dev->dev, &dev_attr_servo0);
265 if (dev->type & SERVO_COUNT_QUAD) { 280 if (dev->type & SERVO_COUNT_QUAD) {
266 device_create_file(&interface->dev, &dev_attr_servo1); 281 device_create_file(dev->dev, &dev_attr_servo1);
267 device_create_file(&interface->dev, &dev_attr_servo2); 282 device_create_file(dev->dev, &dev_attr_servo2);
268 device_create_file(&interface->dev, &dev_attr_servo3); 283 device_create_file(dev->dev, &dev_attr_servo3);
269 } 284 }
270 285
271 dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 attached\n", 286 dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 attached\n",
272 dev->type & SERVO_COUNT_QUAD ? 4 : 1, 287 dev->type & SERVO_COUNT_QUAD ? 4 : 1,
273 dev->type & SERVO_VERSION_30 ? 3 : 2); 288 dev->type & SERVO_VERSION_30 ? 3 : 2);
274 289
275 if(!(dev->type & SERVO_VERSION_30)) 290 if (!(dev->type & SERVO_VERSION_30))
276 dev_info(&interface->dev, 291 dev_info(&interface->dev,
277 "WARNING: v2.0 not tested! Please report if it works.\n"); 292 "WARNING: v2.0 not tested! Please report if it works.\n");
278 293
@@ -287,19 +302,21 @@ servo_disconnect(struct usb_interface *interface)
287 dev = usb_get_intfdata(interface); 302 dev = usb_get_intfdata(interface);
288 usb_set_intfdata(interface, NULL); 303 usb_set_intfdata(interface, NULL);
289 304
290 device_remove_file(&interface->dev, &dev_attr_servo0); 305 device_remove_file(dev->dev, &dev_attr_servo0);
291 if (dev->type & SERVO_COUNT_QUAD) { 306 if (dev->type & SERVO_COUNT_QUAD) {
292 device_remove_file(&interface->dev, &dev_attr_servo1); 307 device_remove_file(dev->dev, &dev_attr_servo1);
293 device_remove_file(&interface->dev, &dev_attr_servo2); 308 device_remove_file(dev->dev, &dev_attr_servo2);
294 device_remove_file(&interface->dev, &dev_attr_servo3); 309 device_remove_file(dev->dev, &dev_attr_servo3);
295 } 310 }
296 311
312 device_unregister(dev->dev);
297 usb_put_dev(dev->udev); 313 usb_put_dev(dev->udev);
298 314
299 dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 detached\n", 315 dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 detached\n",
300 dev->type & SERVO_COUNT_QUAD ? 4 : 1, 316 dev->type & SERVO_COUNT_QUAD ? 4 : 1,
301 dev->type & SERVO_VERSION_30 ? 3 : 2); 317 dev->type & SERVO_VERSION_30 ? 3 : 2);
302 318
319 clear_bit(dev->dev_no, &device_no);
303 kfree(dev); 320 kfree(dev);
304} 321}
305 322