Merge ../linux-2.6
[linux-2.6] / drivers / usb / misc / phidgetservo.c
1 /*
2  * USB PhidgetServo driver 1.0
3  *
4  * Copyright (C) 2004 Sean Young <sean@mess.org>
5  *
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.
10  *
11  * This is a driver for the USB PhidgetServo version 2.0 and 3.0 servo 
12  * controllers available at: http://www.phidgets.com/ 
13  *
14  * Note that the driver takes input as: degrees.minutes
15  *
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.
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  */
27
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>
35
36 #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
37 #define DRIVER_DESC "USB PhidgetServo Driver"
38
39 #define VENDOR_ID_GLAB                          0x06c2
40 #define DEVICE_ID_GLAB_PHIDGETSERVO_QUAD        0x0038
41 #define DEVICE_ID_GLAB_PHIDGETSERVO_UNI         0x0039
42
43 #define VENDOR_ID_WISEGROUP                     0x0925
44 #define VENDOR_ID_WISEGROUP_PHIDGETSERVO_QUAD   0x8101
45 #define VENDOR_ID_WISEGROUP_PHIDGETSERVO_UNI    0x8104
46
47 #define SERVO_VERSION_30                        0x01
48 #define SERVO_COUNT_QUAD                        0x02
49
50 static struct usb_device_id id_table[] = {
51         {
52                 USB_DEVICE(VENDOR_ID_GLAB, DEVICE_ID_GLAB_PHIDGETSERVO_QUAD), 
53                 .driver_info = SERVO_VERSION_30 | SERVO_COUNT_QUAD 
54         },
55         {
56                 USB_DEVICE(VENDOR_ID_GLAB, DEVICE_ID_GLAB_PHIDGETSERVO_UNI),
57                 .driver_info = SERVO_VERSION_30 
58         },
59         {
60                 USB_DEVICE(VENDOR_ID_WISEGROUP, 
61                                 VENDOR_ID_WISEGROUP_PHIDGETSERVO_QUAD),
62                 .driver_info = SERVO_COUNT_QUAD 
63         },
64         {
65                 USB_DEVICE(VENDOR_ID_WISEGROUP, 
66                                 VENDOR_ID_WISEGROUP_PHIDGETSERVO_UNI),
67                 .driver_info = 0
68         },
69         {}
70 };
71
72 MODULE_DEVICE_TABLE(usb, id_table);
73
74 struct phidget_servo {
75         struct usb_device *udev;
76         ulong type;
77         int pulse[4];
78         int degrees[4];
79         int minutes[4];
80 };
81
82 static int
83 change_position_v30(struct phidget_servo *servo, int servo_no, int degrees, 
84                                                                 int minutes)
85 {
86         int retval;
87         unsigned char *buffer;
88
89         if (degrees < -23 || degrees > 362)
90                 return -EINVAL;
91
92         buffer = kmalloc(6, GFP_KERNEL);
93         if (!buffer) {
94                 dev_err(&servo->udev->dev, "%s - out of memory\n",
95                         __FUNCTION__);
96                 return -ENOMEM;
97         }
98
99         /*
100          * pulse = 0 - 4095
101          * angle = 0 - 180 degrees
102          *
103          * pulse = angle * 10.6 + 243.8 
104          */
105         servo->pulse[servo_no] = ((degrees*60 + minutes)*106 + 2438*60)/600;    
106         servo->degrees[servo_no]= degrees;
107         servo->minutes[servo_no]= minutes;      
108
109         /* 
110          * The PhidgetServo v3.0 is controlled by sending 6 bytes,
111          * 4 * 12 bits for each servo.
112          *
113          * low = lower 8 bits pulse
114          * high = higher 4 bits pulse
115          *
116          * offset     bits
117          * +---+-----------------+
118          * | 0 |      low 0      |
119          * +---+--------+--------+
120          * | 1 | high 1 | high 0 |
121          * +---+--------+--------+
122          * | 2 |      low 1      |
123          * +---+-----------------+
124          * | 3 |      low 2      |
125          * +---+--------+--------+
126          * | 4 | high 3 | high 2 |
127          * +---+--------+--------+
128          * | 5 |      low 3      |
129          * +---+-----------------+
130          */
131
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;
140
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]);
145
146         retval = usb_control_msg(servo->udev,
147                                  usb_sndctrlpipe(servo->udev, 0),
148                                  0x09, 0x21, 0x0200, 0x0000, buffer, 6, 2000);
149
150         kfree(buffer);
151
152         return retval;
153 }
154
155 static int
156 change_position_v20(struct phidget_servo *servo, int servo_no, int degrees,
157                                                                 int minutes)
158 {
159         int retval;
160         unsigned char *buffer;
161
162         if (degrees < -23 || degrees > 278)
163                 return -EINVAL;
164
165         buffer = kmalloc(2, GFP_KERNEL);
166         if (!buffer) {
167                 dev_err(&servo->udev->dev, "%s - out of memory\n",
168                         __FUNCTION__);
169                 return -ENOMEM;
170         }
171
172         /*
173          * angle = 0 - 180 degrees
174          * pulse = angle + 23
175          */
176         servo->pulse[servo_no]= degrees + 23;
177         servo->degrees[servo_no]= degrees;
178         servo->minutes[servo_no]= 0;
179
180         /*
181          * The PhidgetServo v2.0 is controlled by sending two bytes. The
182          * first byte is the servo number xor'ed with 2:
183          *
184          * servo 0 = 2
185          * servo 1 = 3
186          * servo 2 = 0
187          * servo 3 = 1
188          *
189          * The second byte is the position.
190          */
191
192         buffer[0] = servo_no ^ 2;
193         buffer[1] = servo->pulse[servo_no];
194
195         dev_dbg(&servo->udev->dev, "data: %02x %02x\n", buffer[0], buffer[1]);
196
197         retval = usb_control_msg(servo->udev,
198                                  usb_sndctrlpipe(servo->udev, 0),
199                                  0x09, 0x21, 0x0200, 0x0000, buffer, 2, 2000);
200
201         kfree(buffer);
202
203         return retval;
204 }
205
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)  \
209 {                                                                       \
210         int degrees, minutes, retval;                                   \
211         struct usb_interface *intf = to_usb_interface (dev);            \
212         struct phidget_servo *servo = usb_get_intfdata (intf);          \
213                                                                         \
214         minutes = 0;                                                    \
215         /* must at least convert degrees */                             \
216         if (sscanf (buf, "%d.%d", &degrees, &minutes) < 1) {            \
217                 return -EINVAL;                                         \
218         }                                                               \
219                                                                         \
220         if (minutes < 0 || minutes > 59)                                \
221                 return -EINVAL;                                         \
222                                                                         \
223         if (servo->type & SERVO_VERSION_30)                             \
224                 retval = change_position_v30 (servo, value, degrees,    \
225                                                         minutes);       \
226         else                                                            \
227                 retval = change_position_v20 (servo, value, degrees,    \
228                                                         minutes);       \
229                                                                         \
230         return retval < 0 ? retval : count;                             \
231 }                                                                       \
232                                                                         \
233 static ssize_t show_servo##value (struct device *dev, struct device_attribute *attr, char *buf)         \
234 {                                                                       \
235         struct usb_interface *intf = to_usb_interface (dev);            \
236         struct phidget_servo *servo = usb_get_intfdata (intf);          \
237                                                                         \
238         return sprintf (buf, "%d.%02d\n", servo->degrees[value],        \
239                                 servo->minutes[value]);                 \
240 }                                                                       \
241 static DEVICE_ATTR(servo##value, S_IWUGO | S_IRUGO,                     \
242           show_servo##value, set_servo##value);
243
244 show_set(0);
245 show_set(1);
246 show_set(2);
247 show_set(3);
248
249 static int
250 servo_probe(struct usb_interface *interface, const struct usb_device_id *id)
251 {
252         struct usb_device *udev = interface_to_usbdev(interface);
253         struct phidget_servo *dev;
254
255         dev = kzalloc(sizeof (struct phidget_servo), GFP_KERNEL);
256         if (dev == NULL) {
257                 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
258                 return -ENOMEM;
259         }
260
261         dev->udev = usb_get_dev(udev);
262         dev->type = id->driver_info;
263         usb_set_intfdata(interface, dev);
264
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);
270         }
271
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);
275
276         if(!(dev->type & SERVO_VERSION_30))
277                 dev_info(&interface->dev,
278                          "WARNING: v2.0 not tested! Please report if it works.\n");
279
280         return 0;
281 }
282
283 static void
284 servo_disconnect(struct usb_interface *interface)
285 {
286         struct phidget_servo *dev;
287
288         dev = usb_get_intfdata(interface);
289         usb_set_intfdata(interface, NULL);
290
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);
296         }
297
298         usb_put_dev(dev->udev);
299
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);
303
304         kfree(dev);
305 }
306
307 static struct usb_driver servo_driver = {
308         .name = "phidgetservo",
309         .probe = servo_probe,
310         .disconnect = servo_disconnect,
311         .id_table = id_table
312 };
313
314 static int __init
315 phidget_servo_init(void)
316 {
317         int retval;
318
319         retval = usb_register(&servo_driver);
320         if (retval)
321                 err("usb_register failed. Error number %d", retval);
322
323         return retval;
324 }
325
326 static void __exit
327 phidget_servo_exit(void)
328 {
329         usb_deregister(&servo_driver);
330 }
331
332 module_init(phidget_servo_init);
333 module_exit(phidget_servo_exit);
334
335 MODULE_AUTHOR(DRIVER_AUTHOR);
336 MODULE_DESCRIPTION(DRIVER_DESC);
337 MODULE_LICENSE("GPL");