2 * USB PhidgetServo driver 1.0
4 * Copyright (C) 2004 Sean Young <sean@mess.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This is a driver for the USB PhidgetServo version 2.0 and 3.0 servo
12 * controllers available at: http://www.phidgets.com/
14 * Note that the driver takes input as: degrees.minutes
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.
19 * Jun 16, 2004: Sean Young <sean@mess.org>
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
28 #include <linux/config.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/module.h>
34 #include <linux/usb.h>
36 #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
37 #define DRIVER_DESC "USB PhidgetServo Driver"
39 #define VENDOR_ID_GLAB 0x06c2
40 #define DEVICE_ID_GLAB_PHIDGETSERVO_QUAD 0x0038
41 #define DEVICE_ID_GLAB_PHIDGETSERVO_UNI 0x0039
43 #define VENDOR_ID_WISEGROUP 0x0925
44 #define VENDOR_ID_WISEGROUP_PHIDGETSERVO_QUAD 0x8101
45 #define VENDOR_ID_WISEGROUP_PHIDGETSERVO_UNI 0x8104
47 #define SERVO_VERSION_30 0x01
48 #define SERVO_COUNT_QUAD 0x02
50 static struct usb_device_id id_table[] = {
52 USB_DEVICE(VENDOR_ID_GLAB, DEVICE_ID_GLAB_PHIDGETSERVO_QUAD),
53 .driver_info = SERVO_VERSION_30 | SERVO_COUNT_QUAD
56 USB_DEVICE(VENDOR_ID_GLAB, DEVICE_ID_GLAB_PHIDGETSERVO_UNI),
57 .driver_info = SERVO_VERSION_30
60 USB_DEVICE(VENDOR_ID_WISEGROUP,
61 VENDOR_ID_WISEGROUP_PHIDGETSERVO_QUAD),
62 .driver_info = SERVO_COUNT_QUAD
65 USB_DEVICE(VENDOR_ID_WISEGROUP,
66 VENDOR_ID_WISEGROUP_PHIDGETSERVO_UNI),
72 MODULE_DEVICE_TABLE(usb, id_table);
74 struct phidget_servo {
75 struct usb_device *udev;
83 change_position_v30(struct phidget_servo *servo, int servo_no, int degrees,
87 unsigned char *buffer;
89 if (degrees < -23 || degrees > 362)
92 buffer = kmalloc(6, GFP_KERNEL);
94 dev_err(&servo->udev->dev, "%s - out of memory\n",
101 * angle = 0 - 180 degrees
103 * pulse = angle * 10.6 + 243.8
105 servo->pulse[servo_no] = ((degrees*60 + minutes)*106 + 2438*60)/600;
106 servo->degrees[servo_no]= degrees;
107 servo->minutes[servo_no]= minutes;
110 * The PhidgetServo v3.0 is controlled by sending 6 bytes,
111 * 4 * 12 bits for each servo.
113 * low = lower 8 bits pulse
114 * high = higher 4 bits pulse
117 * +---+-----------------+
119 * +---+--------+--------+
120 * | 1 | high 1 | high 0 |
121 * +---+--------+--------+
123 * +---+-----------------+
125 * +---+--------+--------+
126 * | 4 | high 3 | high 2 |
127 * +---+--------+--------+
129 * +---+-----------------+
132 buffer[0] = servo->pulse[0] & 0xff;
133 buffer[1] = (servo->pulse[0] >> 8 & 0x0f)
134 | (servo->pulse[1] >> 4 & 0xf0);
135 buffer[2] = servo->pulse[1] & 0xff;
136 buffer[3] = servo->pulse[2] & 0xff;
137 buffer[4] = (servo->pulse[2] >> 8 & 0x0f)
138 | (servo->pulse[3] >> 4 & 0xf0);
139 buffer[5] = servo->pulse[3] & 0xff;
141 dev_dbg(&servo->udev->dev,
142 "data: %02x %02x %02x %02x %02x %02x\n",
143 buffer[0], buffer[1], buffer[2],
144 buffer[3], buffer[4], buffer[5]);
146 retval = usb_control_msg(servo->udev,
147 usb_sndctrlpipe(servo->udev, 0),
148 0x09, 0x21, 0x0200, 0x0000, buffer, 6, 2000);
156 change_position_v20(struct phidget_servo *servo, int servo_no, int degrees,
160 unsigned char *buffer;
162 if (degrees < -23 || degrees > 278)
165 buffer = kmalloc(2, GFP_KERNEL);
167 dev_err(&servo->udev->dev, "%s - out of memory\n",
173 * angle = 0 - 180 degrees
176 servo->pulse[servo_no]= degrees + 23;
177 servo->degrees[servo_no]= degrees;
178 servo->minutes[servo_no]= 0;
181 * The PhidgetServo v2.0 is controlled by sending two bytes. The
182 * first byte is the servo number xor'ed with 2:
189 * The second byte is the position.
192 buffer[0] = servo_no ^ 2;
193 buffer[1] = servo->pulse[servo_no];
195 dev_dbg(&servo->udev->dev, "data: %02x %02x\n", buffer[0], buffer[1]);
197 retval = usb_control_msg(servo->udev,
198 usb_sndctrlpipe(servo->udev, 0),
199 0x09, 0x21, 0x0200, 0x0000, buffer, 2, 2000);
206 #define show_set(value) \
207 static ssize_t set_servo##value (struct device *dev, struct device_attribute *attr, \
208 const char *buf, size_t count) \
210 int degrees, minutes, retval; \
211 struct usb_interface *intf = to_usb_interface (dev); \
212 struct phidget_servo *servo = usb_get_intfdata (intf); \
215 /* must at least convert degrees */ \
216 if (sscanf (buf, "%d.%d", °rees, &minutes) < 1) { \
220 if (minutes < 0 || minutes > 59) \
223 if (servo->type & SERVO_VERSION_30) \
224 retval = change_position_v30 (servo, value, degrees, \
227 retval = change_position_v20 (servo, value, degrees, \
230 return retval < 0 ? retval : count; \
233 static ssize_t show_servo##value (struct device *dev, struct device_attribute *attr, char *buf) \
235 struct usb_interface *intf = to_usb_interface (dev); \
236 struct phidget_servo *servo = usb_get_intfdata (intf); \
238 return sprintf (buf, "%d.%02d\n", servo->degrees[value], \
239 servo->minutes[value]); \
241 static DEVICE_ATTR(servo##value, S_IWUGO | S_IRUGO, \
242 show_servo##value, set_servo##value);
250 servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
252 struct usb_device *udev = interface_to_usbdev(interface);
253 struct phidget_servo *dev;
255 dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL);
257 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
261 dev->udev = usb_get_dev(udev);
262 dev->type = id->driver_info;
263 usb_set_intfdata(interface, dev);
265 device_create_file(&interface->dev, &dev_attr_servo0);
266 if (dev->type & SERVO_COUNT_QUAD) {
267 device_create_file(&interface->dev, &dev_attr_servo1);
268 device_create_file(&interface->dev, &dev_attr_servo2);
269 device_create_file(&interface->dev, &dev_attr_servo3);
272 dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 attached\n",
273 dev->type & SERVO_COUNT_QUAD ? 4 : 1,
274 dev->type & SERVO_VERSION_30 ? 3 : 2);
276 if(!(dev->type & SERVO_VERSION_30))
277 dev_info(&interface->dev,
278 "WARNING: v2.0 not tested! Please report if it works.\n");
284 servo_disconnect(struct usb_interface *interface)
286 struct phidget_servo *dev;
288 dev = usb_get_intfdata(interface);
289 usb_set_intfdata(interface, NULL);
291 device_remove_file(&interface->dev, &dev_attr_servo0);
292 if (dev->type & SERVO_COUNT_QUAD) {
293 device_remove_file(&interface->dev, &dev_attr_servo1);
294 device_remove_file(&interface->dev, &dev_attr_servo2);
295 device_remove_file(&interface->dev, &dev_attr_servo3);
298 usb_put_dev(dev->udev);
300 dev_info(&interface->dev, "USB %d-Motor PhidgetServo v%d.0 detached\n",
301 dev->type & SERVO_COUNT_QUAD ? 4 : 1,
302 dev->type & SERVO_VERSION_30 ? 3 : 2);
307 static struct usb_driver servo_driver = {
308 .name = "phidgetservo",
309 .probe = servo_probe,
310 .disconnect = servo_disconnect,
315 phidget_servo_init(void)
319 retval = usb_register(&servo_driver);
321 err("usb_register failed. Error number %d", retval);
327 phidget_servo_exit(void)
329 usb_deregister(&servo_driver);
332 module_init(phidget_servo_init);
333 module_exit(phidget_servo_exit);
335 MODULE_AUTHOR(DRIVER_AUTHOR);
336 MODULE_DESCRIPTION(DRIVER_DESC);
337 MODULE_LICENSE("GPL");