2 * USB PhidgetServo driver 1.0
4 * Copyright (C) 2004, 2006 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.
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/usb.h>
29 #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
30 #define DRIVER_DESC "USB PhidgetServo Driver"
32 #define VENDOR_ID_GLAB 0x06c2
33 #define DEVICE_ID_GLAB_PHIDGETSERVO_QUAD 0x0038
34 #define DEVICE_ID_GLAB_PHIDGETSERVO_UNI 0x0039
36 #define VENDOR_ID_WISEGROUP 0x0925
37 #define VENDOR_ID_WISEGROUP_PHIDGETSERVO_QUAD 0x8101
38 #define VENDOR_ID_WISEGROUP_PHIDGETSERVO_UNI 0x8104
40 #define SERVO_VERSION_30 0x01
41 #define SERVO_COUNT_QUAD 0x02
43 static struct usb_device_id id_table
[] = {
45 USB_DEVICE(VENDOR_ID_GLAB
, DEVICE_ID_GLAB_PHIDGETSERVO_QUAD
),
46 .driver_info
= SERVO_VERSION_30
| SERVO_COUNT_QUAD
49 USB_DEVICE(VENDOR_ID_GLAB
, DEVICE_ID_GLAB_PHIDGETSERVO_UNI
),
50 .driver_info
= SERVO_VERSION_30
53 USB_DEVICE(VENDOR_ID_WISEGROUP
,
54 VENDOR_ID_WISEGROUP_PHIDGETSERVO_QUAD
),
55 .driver_info
= SERVO_COUNT_QUAD
58 USB_DEVICE(VENDOR_ID_WISEGROUP
,
59 VENDOR_ID_WISEGROUP_PHIDGETSERVO_UNI
),
65 MODULE_DEVICE_TABLE(usb
, id_table
);
67 static int unsigned long device_no
;
69 struct phidget_servo
{
70 struct usb_device
*udev
;
80 change_position_v30(struct phidget_servo
*servo
, int servo_no
, int degrees
,
84 unsigned char *buffer
;
86 if (degrees
< -23 || degrees
> 362)
89 buffer
= kmalloc(6, GFP_KERNEL
);
91 dev_err(&servo
->udev
->dev
, "%s - out of memory\n",
98 * angle = 0 - 180 degrees
100 * pulse = angle * 10.6 + 243.8
102 servo
->pulse
[servo_no
] = ((degrees
*60 + minutes
)*106 + 2438*60)/600;
103 servo
->degrees
[servo_no
]= degrees
;
104 servo
->minutes
[servo_no
]= minutes
;
107 * The PhidgetServo v3.0 is controlled by sending 6 bytes,
108 * 4 * 12 bits for each servo.
110 * low = lower 8 bits pulse
111 * high = higher 4 bits pulse
114 * +---+-----------------+
116 * +---+--------+--------+
117 * | 1 | high 1 | high 0 |
118 * +---+--------+--------+
120 * +---+-----------------+
122 * +---+--------+--------+
123 * | 4 | high 3 | high 2 |
124 * +---+--------+--------+
126 * +---+-----------------+
129 buffer
[0] = servo
->pulse
[0] & 0xff;
130 buffer
[1] = (servo
->pulse
[0] >> 8 & 0x0f)
131 | (servo
->pulse
[1] >> 4 & 0xf0);
132 buffer
[2] = servo
->pulse
[1] & 0xff;
133 buffer
[3] = servo
->pulse
[2] & 0xff;
134 buffer
[4] = (servo
->pulse
[2] >> 8 & 0x0f)
135 | (servo
->pulse
[3] >> 4 & 0xf0);
136 buffer
[5] = servo
->pulse
[3] & 0xff;
138 dev_dbg(&servo
->udev
->dev
,
139 "data: %02x %02x %02x %02x %02x %02x\n",
140 buffer
[0], buffer
[1], buffer
[2],
141 buffer
[3], buffer
[4], buffer
[5]);
143 retval
= usb_control_msg(servo
->udev
,
144 usb_sndctrlpipe(servo
->udev
, 0),
145 0x09, 0x21, 0x0200, 0x0000, buffer
, 6, 2000);
153 change_position_v20(struct phidget_servo
*servo
, int servo_no
, int degrees
,
157 unsigned char *buffer
;
159 if (degrees
< -23 || degrees
> 278)
162 buffer
= kmalloc(2, GFP_KERNEL
);
164 dev_err(&servo
->udev
->dev
, "%s - out of memory\n",
170 * angle = 0 - 180 degrees
173 servo
->pulse
[servo_no
]= degrees
+ 23;
174 servo
->degrees
[servo_no
]= degrees
;
175 servo
->minutes
[servo_no
]= 0;
178 * The PhidgetServo v2.0 is controlled by sending two bytes. The
179 * first byte is the servo number xor'ed with 2:
186 * The second byte is the position.
189 buffer
[0] = servo_no
^ 2;
190 buffer
[1] = servo
->pulse
[servo_no
];
192 dev_dbg(&servo
->udev
->dev
, "data: %02x %02x\n", buffer
[0], buffer
[1]);
194 retval
= usb_control_msg(servo
->udev
,
195 usb_sndctrlpipe(servo
->udev
, 0),
196 0x09, 0x21, 0x0200, 0x0000, buffer
, 2, 2000);
203 #define show_set(value) \
204 static ssize_t set_servo##value (struct device *dev, \
205 struct device_attribute *attr, \
206 const char *buf, size_t count) \
208 int degrees, minutes, retval; \
209 struct phidget_servo *servo = dev_get_drvdata(dev); \
212 /* must at least convert degrees */ \
213 if (sscanf(buf, "%d.%d", °rees, &minutes) < 1) { \
217 if (minutes < 0 || minutes > 59) \
220 if (servo->type & SERVO_VERSION_30) \
221 retval = change_position_v30(servo, value, degrees, \
224 retval = change_position_v20(servo, value, degrees, \
227 return retval < 0 ? retval : count; \
230 static ssize_t show_servo##value (struct device *dev, \
231 struct device_attribute *attr, \
234 struct phidget_servo *servo = dev_get_drvdata(dev); \
236 return sprintf(buf, "%d.%02d\n", servo->degrees[value], \
237 servo->minutes[value]); \
240 #define servo_attr(value) \
241 __ATTR(servo##value, S_IWUGO | S_IRUGO, \
242 show_servo##value, set_servo##value)
248 static struct device_attribute dev_attrs
[] = {
249 servo_attr(0), servo_attr(1), servo_attr(2), servo_attr(3)
253 servo_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
255 struct usb_device
*udev
= interface_to_usbdev(interface
);
256 struct phidget_servo
*dev
;
260 dev
= kzalloc(sizeof (struct phidget_servo
), GFP_KERNEL
);
262 dev_err(&interface
->dev
, "%s - out of memory\n", __FUNCTION__
);
267 dev
->udev
= usb_get_dev(udev
);
268 dev
->type
= id
->driver_info
;
270 usb_set_intfdata(interface
, dev
);
273 bit
= find_first_zero_bit(&device_no
, sizeof(device_no
));
274 value
= test_and_set_bit(bit
, &device_no
);
278 dev
->dev
= device_create(phidget_class
, &dev
->udev
->dev
, 0,
279 "servo%d", dev
->dev_no
);
280 if (IS_ERR(dev
->dev
)) {
281 rc
= PTR_ERR(dev
->dev
);
285 dev_set_drvdata(dev
->dev
, dev
);
287 servo_count
= dev
->type
& SERVO_COUNT_QUAD
? 4 : 1;
289 for (i
=0; i
<servo_count
; i
++) {
290 rc
= device_create_file(dev
->dev
, &dev_attrs
[i
]);
295 dev_info(&interface
->dev
, "USB %d-Motor PhidgetServo v%d.0 attached\n",
296 servo_count
, dev
->type
& SERVO_VERSION_30
? 3 : 2);
298 if (!(dev
->type
& SERVO_VERSION_30
))
299 dev_info(&interface
->dev
,
300 "WARNING: v2.0 not tested! Please report if it works.\n");
305 device_remove_file(dev
->dev
, &dev_attrs
[i
]);
309 device_unregister(dev
->dev
);
310 if (dev
->dev_no
>= 0)
311 clear_bit(dev
->dev_no
, &device_no
);
320 servo_disconnect(struct usb_interface
*interface
)
322 struct phidget_servo
*dev
;
325 dev
= usb_get_intfdata(interface
);
326 usb_set_intfdata(interface
, NULL
);
331 servo_count
= dev
->type
& SERVO_COUNT_QUAD
? 4 : 1;
333 for (i
=0; i
<servo_count
; i
++)
334 device_remove_file(dev
->dev
, &dev_attrs
[i
]);
336 device_unregister(dev
->dev
);
337 usb_put_dev(dev
->udev
);
339 dev_info(&interface
->dev
, "USB %d-Motor PhidgetServo v%d.0 detached\n",
340 servo_count
, dev
->type
& SERVO_VERSION_30
? 3 : 2);
342 clear_bit(dev
->dev_no
, &device_no
);
346 static struct usb_driver servo_driver
= {
347 .name
= "phidgetservo",
348 .probe
= servo_probe
,
349 .disconnect
= servo_disconnect
,
354 phidget_servo_init(void)
358 retval
= usb_register(&servo_driver
);
360 err("usb_register failed. Error number %d", retval
);
366 phidget_servo_exit(void)
368 usb_deregister(&servo_driver
);
371 module_init(phidget_servo_init
);
372 module_exit(phidget_servo_exit
);
374 MODULE_AUTHOR(DRIVER_AUTHOR
);
375 MODULE_DESCRIPTION(DRIVER_DESC
);
376 MODULE_LICENSE("GPL");