ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / input / powermate.c
1 /*
2  * A driver for the Griffin Technology, Inc. "PowerMate" USB controller dial.
3  *
4  * v1.1, (c)2002 William R Sowerbutts <will@sowerbutts.com>
5  *
6  * This device is a anodised aluminium knob which connects over USB. It can measure
7  * clockwise and anticlockwise rotation. The dial also acts as a pushbutton with
8  * a spring for automatic release. The base contains a pair of LEDs which illuminate
9  * the translucent base. It rotates without limit and reports its relative rotation
10  * back to the host when polled by the USB controller.
11  *
12  * Testing with the knob I have has shown that it measures approximately 94 "clicks"
13  * for one full rotation. Testing with my High Speed Rotation Actuator (ok, it was 
14  * a variable speed cordless electric drill) has shown that the device can measure
15  * speeds of up to 7 clicks either clockwise or anticlockwise between pollings from
16  * the host. If it counts more than 7 clicks before it is polled, it will wrap back
17  * to zero and start counting again. This was at quite high speed, however, almost
18  * certainly faster than the human hand could turn it. Griffin say that it loses a
19  * pulse or two on a direction change; the granularity is so fine that I never
20  * noticed this in practice.
21  *
22  * The device's microcontroller can be programmed to set the LED to either a constant
23  * intensity, or to a rhythmic pulsing. Several patterns and speeds are available.
24  *
25  * Griffin were very happy to provide documentation and free hardware for development.
26  *
27  * Some userspace tools are available on the web: http://sowerbutts.com/powermate/
28  *
29  */
30
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/input.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/usb.h>
37
38 #define POWERMATE_VENDOR        0x077d  /* Griffin Technology, Inc. */
39 #define POWERMATE_PRODUCT_NEW   0x0410  /* Griffin PowerMate */
40 #define POWERMATE_PRODUCT_OLD   0x04AA  /* Griffin soundKnob */
41
42 #define CONTOUR_VENDOR          0x05f3  /* Contour Design, Inc. */
43 #define CONTOUR_JOG             0x0240  /* Jog and Shuttle */
44
45 /* these are the command codes we send to the device */
46 #define SET_STATIC_BRIGHTNESS  0x01
47 #define SET_PULSE_ASLEEP       0x02
48 #define SET_PULSE_AWAKE        0x03
49 #define SET_PULSE_MODE         0x04
50
51 /* these refer to bits in the powermate_device's requires_update field. */
52 #define UPDATE_STATIC_BRIGHTNESS (1<<0)
53 #define UPDATE_PULSE_ASLEEP      (1<<1)
54 #define UPDATE_PULSE_AWAKE       (1<<2)
55 #define UPDATE_PULSE_MODE        (1<<3)
56
57 /* at least two versions of the hardware exist, with differing payload
58    sizes. the first three bytes always contain the "interesting" data in
59    the relevant format. */
60 #define POWERMATE_PAYLOAD_SIZE_MAX 6
61 #define POWERMATE_PAYLOAD_SIZE_MIN 3
62 struct powermate_device {
63         signed char *data;
64         dma_addr_t data_dma;
65         struct urb *irq, *config;
66         struct usb_ctrlrequest *configcr;
67         dma_addr_t configcr_dma;
68         struct usb_device *udev;
69         struct input_dev input;
70         struct semaphore lock;
71         int static_brightness;
72         int pulse_speed;
73         int pulse_table;
74         int pulse_asleep;
75         int pulse_awake;
76         int requires_update; // physical settings which are out of sync
77         char phys[64];
78 };
79
80 static char pm_name_powermate[] = "Griffin PowerMate";
81 static char pm_name_soundknob[] = "Griffin SoundKnob";
82
83 static void powermate_config_complete(struct urb *urb, struct pt_regs *regs);
84
85 /* Callback for data arriving from the PowerMate over the USB interrupt pipe */
86 static void powermate_irq(struct urb *urb, struct pt_regs *regs)
87 {
88         struct powermate_device *pm = urb->context;
89         int retval;
90
91         switch (urb->status) {
92         case 0:
93                 /* success */
94                 break;
95         case -ECONNRESET:
96         case -ENOENT:
97         case -ESHUTDOWN:
98                 /* this urb is terminated, clean up */
99                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
100                 return;
101         default:
102                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
103                 goto exit;
104         }
105
106         /* handle updates to device state */
107         input_regs(&pm->input, regs);
108         input_report_key(&pm->input, BTN_0, pm->data[0] & 0x01);
109         input_report_rel(&pm->input, REL_DIAL, pm->data[1]);
110         input_sync(&pm->input);
111
112 exit:
113         retval = usb_submit_urb (urb, GFP_ATOMIC);
114         if (retval)
115                 err ("%s - usb_submit_urb failed with result %d",
116                      __FUNCTION__, retval);
117 }
118
119 /* Decide if we need to issue a control message and do so. Must be called with pm->lock down */
120 static void powermate_sync_state(struct powermate_device *pm)
121 {
122         if (pm->requires_update == 0) 
123                 return; /* no updates are required */
124         if (pm->config->status == -EINPROGRESS) 
125                 return; /* an update is already in progress; it'll issue this update when it completes */
126
127         if (pm->requires_update & UPDATE_PULSE_ASLEEP){
128                 pm->configcr->wValue = cpu_to_le16( SET_PULSE_ASLEEP );
129                 pm->configcr->wIndex = cpu_to_le16( pm->pulse_asleep ? 1 : 0 );
130                 pm->requires_update &= ~UPDATE_PULSE_ASLEEP;
131         }else if (pm->requires_update & UPDATE_PULSE_AWAKE){
132                 pm->configcr->wValue = cpu_to_le16( SET_PULSE_AWAKE );
133                 pm->configcr->wIndex = cpu_to_le16( pm->pulse_awake ? 1 : 0 );
134                 pm->requires_update &= ~UPDATE_PULSE_AWAKE;
135         }else if (pm->requires_update & UPDATE_PULSE_MODE){
136                 int op, arg;
137                 /* the powermate takes an operation and an argument for its pulse algorithm.
138                    the operation can be:
139                    0: divide the speed
140                    1: pulse at normal speed
141                    2: multiply the speed
142                    the argument only has an effect for operations 0 and 2, and ranges between
143                    1 (least effect) to 255 (maximum effect).
144        
145                    thus, several states are equivalent and are coalesced into one state.
146
147                    we map this onto a range from 0 to 510, with:
148                    0 -- 254    -- use divide (0 = slowest)
149                    255         -- use normal speed
150                    256 -- 510  -- use multiple (510 = fastest).
151
152                    Only values of 'arg' quite close to 255 are particularly useful/spectacular.
153                 */    
154                 if (pm->pulse_speed < 255){
155                         op = 0;                   // divide
156                         arg = 255 - pm->pulse_speed;
157                 } else if (pm->pulse_speed > 255){
158                         op = 2;                   // multiply
159                         arg = pm->pulse_speed - 255;
160                 } else {
161                         op = 1;                   // normal speed
162                         arg = 0;                  // can be any value
163                 }
164                 pm->configcr->wValue = cpu_to_le16( (pm->pulse_table << 8) | SET_PULSE_MODE );
165                 pm->configcr->wIndex = cpu_to_le16( (arg << 8) | op );
166                 pm->requires_update &= ~UPDATE_PULSE_MODE;
167         }else if (pm->requires_update & UPDATE_STATIC_BRIGHTNESS){
168                 pm->configcr->wValue = cpu_to_le16( SET_STATIC_BRIGHTNESS );
169                 pm->configcr->wIndex = cpu_to_le16( pm->static_brightness );
170                 pm->requires_update &= ~UPDATE_STATIC_BRIGHTNESS;
171         }else{
172                 printk(KERN_ERR "powermate: unknown update required");
173                 pm->requires_update = 0; /* fudge the bug */
174                 return;
175         }
176
177 /*      printk("powermate: %04x %04x\n", pm->configcr->wValue, pm->configcr->wIndex); */
178
179         pm->configcr->bRequestType = 0x41; /* vendor request */
180         pm->configcr->bRequest = 0x01;
181         pm->configcr->wLength = 0;
182
183         usb_fill_control_urb(pm->config, pm->udev, usb_sndctrlpipe(pm->udev, 0),
184                              (void *) pm->configcr, 0, 0,
185                              powermate_config_complete, pm);
186         pm->config->setup_dma = pm->configcr_dma;
187         pm->config->transfer_flags |= URB_NO_SETUP_DMA_MAP;
188
189         if (usb_submit_urb(pm->config, GFP_ATOMIC))
190                 printk(KERN_ERR "powermate: usb_submit_urb(config) failed");
191 }
192
193 /* Called when our asynchronous control message completes. We may need to issue another immediately */
194 static void powermate_config_complete(struct urb *urb, struct pt_regs *regs)
195 {
196         struct powermate_device *pm = urb->context;
197
198         if (urb->status)
199                 printk(KERN_ERR "powermate: config urb returned %d\n", urb->status);
200         
201         down(&pm->lock);
202         powermate_sync_state(pm);
203         up(&pm->lock);
204 }
205
206 /* Set the LED up as described and begin the sync with the hardware if required */
207 static void powermate_pulse_led(struct powermate_device *pm, int static_brightness, int pulse_speed, 
208                                 int pulse_table, int pulse_asleep, int pulse_awake)
209 {
210         if (pulse_speed < 0)
211                 pulse_speed = 0;
212         if (pulse_table < 0)
213                 pulse_table = 0;
214         if (pulse_speed > 510)
215                 pulse_speed = 510;
216         if (pulse_table > 2)
217                 pulse_table = 2;
218
219         pulse_asleep = !!pulse_asleep;
220         pulse_awake = !!pulse_awake;
221
222         down(&pm->lock);
223
224         /* mark state updates which are required */
225         if (static_brightness != pm->static_brightness){
226                 pm->static_brightness = static_brightness;
227                 pm->requires_update |= UPDATE_STATIC_BRIGHTNESS;                
228         }
229         if (pulse_asleep != pm->pulse_asleep){
230                 pm->pulse_asleep = pulse_asleep;
231                 pm->requires_update |= (UPDATE_PULSE_ASLEEP | UPDATE_STATIC_BRIGHTNESS);
232         }
233         if (pulse_awake != pm->pulse_awake){
234                 pm->pulse_awake = pulse_awake;
235                 pm->requires_update |= (UPDATE_PULSE_AWAKE | UPDATE_STATIC_BRIGHTNESS);
236         }
237         if (pulse_speed != pm->pulse_speed || pulse_table != pm->pulse_table){
238                 pm->pulse_speed = pulse_speed;
239                 pm->pulse_table = pulse_table;
240                 pm->requires_update |= UPDATE_PULSE_MODE;
241         }
242
243         powermate_sync_state(pm);
244    
245         up(&pm->lock);
246 }
247
248 /* Callback from the Input layer when an event arrives from userspace to configure the LED */
249 static int powermate_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int _value)
250 {
251         unsigned int command = (unsigned int)_value;
252         struct powermate_device *pm = dev->private;
253
254         if (type == EV_MSC && code == MSC_PULSELED){
255                 /*  
256                     bits  0- 7: 8 bits: LED brightness
257                     bits  8-16: 9 bits: pulsing speed modifier (0 ... 510); 0-254 = slower, 255 = standard, 256-510 = faster.
258                     bits 17-18: 2 bits: pulse table (0, 1, 2 valid)
259                     bit     19: 1 bit : pulse whilst asleep?
260                     bit     20: 1 bit : pulse constantly?
261                 */  
262                 int static_brightness = command & 0xFF;   // bits 0-7
263                 int pulse_speed = (command >> 8) & 0x1FF; // bits 8-16
264                 int pulse_table = (command >> 17) & 0x3;  // bits 17-18
265                 int pulse_asleep = (command >> 19) & 0x1; // bit 19
266                 int pulse_awake  = (command >> 20) & 0x1; // bit 20
267   
268                 powermate_pulse_led(pm, static_brightness, pulse_speed, pulse_table, pulse_asleep, pulse_awake);
269         }
270
271         return 0;
272 }
273
274 static int powermate_alloc_buffers(struct usb_device *udev, struct powermate_device *pm)
275 {
276         pm->data = usb_buffer_alloc(udev, POWERMATE_PAYLOAD_SIZE_MAX,
277                                     SLAB_ATOMIC, &pm->data_dma);
278         if (!pm->data)
279                 return -1;
280         pm->configcr = usb_buffer_alloc(udev, sizeof(*(pm->configcr)),
281                                         SLAB_ATOMIC, &pm->configcr_dma);
282         if (!pm->configcr)
283                 return -1;
284
285         return 0;
286 }
287
288 static void powermate_free_buffers(struct usb_device *udev, struct powermate_device *pm)
289 {
290         if (pm->data)
291                 usb_buffer_free(udev, POWERMATE_PAYLOAD_SIZE_MAX,
292                                 pm->data, pm->data_dma);
293         if (pm->configcr)
294                 usb_buffer_free(udev, sizeof(*(pm->configcr)),
295                                 pm->configcr, pm->configcr_dma);
296 }
297
298 /* Called whenever a USB device matching one in our supported devices table is connected */
299 static int powermate_probe(struct usb_interface *intf, const struct usb_device_id *id)
300 {
301         struct usb_device *udev = interface_to_usbdev (intf);
302         struct usb_host_interface *interface;
303         struct usb_endpoint_descriptor *endpoint;
304         struct powermate_device *pm;
305         int pipe, maxp;
306         char path[64];
307
308         interface = intf->cur_altsetting;
309         endpoint = &interface->endpoint[0].desc;
310         if (!(endpoint->bEndpointAddress & 0x80))
311                 return -EIO;
312         if ((endpoint->bmAttributes & 3) != 3)
313                 return -EIO;
314
315         usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
316                 0x0a, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
317                 0, interface->desc.bInterfaceNumber, NULL, 0,
318                 HZ * USB_CTRL_SET_TIMEOUT);
319
320         if (!(pm = kmalloc(sizeof(struct powermate_device), GFP_KERNEL)))
321                 return -ENOMEM;
322
323         memset(pm, 0, sizeof(struct powermate_device));
324         pm->udev = udev;
325
326         if (powermate_alloc_buffers(udev, pm)) {
327                 powermate_free_buffers(udev, pm);
328                 kfree(pm);
329                 return -ENOMEM;
330         }
331
332         pm->irq = usb_alloc_urb(0, GFP_KERNEL);
333         if (!pm->irq) {
334                 powermate_free_buffers(udev, pm);
335                 kfree(pm);
336                 return -ENOMEM;
337         }
338
339         pm->config = usb_alloc_urb(0, GFP_KERNEL);
340         if (!pm->config) {
341                 usb_free_urb(pm->irq);
342                 powermate_free_buffers(udev, pm);
343                 kfree(pm);
344                 return -ENOMEM;
345         }
346
347         init_MUTEX(&pm->lock);
348         init_input_dev(&pm->input);
349
350         /* get a handle to the interrupt data pipe */
351         pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
352         maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
353
354         if(maxp < POWERMATE_PAYLOAD_SIZE_MIN || maxp > POWERMATE_PAYLOAD_SIZE_MAX){
355                 printk("powermate: Expected payload of %d--%d bytes, found %d bytes!\n",
356                         POWERMATE_PAYLOAD_SIZE_MIN, POWERMATE_PAYLOAD_SIZE_MAX, maxp);
357                 maxp = POWERMATE_PAYLOAD_SIZE_MAX;
358         }
359
360         usb_fill_int_urb(pm->irq, udev, pipe, pm->data,
361                          maxp, powermate_irq,
362                          pm, endpoint->bInterval);
363         pm->irq->transfer_dma = pm->data_dma;
364         pm->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
365
366         /* register our interrupt URB with the USB system */
367         if (usb_submit_urb(pm->irq, GFP_KERNEL)) {
368                 powermate_free_buffers(udev, pm);
369                 kfree(pm);
370                 return -EIO; /* failure */
371         }
372
373         switch (udev->descriptor.idProduct) {
374         case POWERMATE_PRODUCT_NEW: pm->input.name = pm_name_powermate; break;
375         case POWERMATE_PRODUCT_OLD: pm->input.name = pm_name_soundknob; break;
376         default: 
377           pm->input.name = pm_name_soundknob;
378           printk(KERN_WARNING "powermate: unknown product id %04x\n", udev->descriptor.idProduct);
379         }
380
381         pm->input.private = pm;
382         pm->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_MSC);
383         pm->input.keybit[LONG(BTN_0)] = BIT(BTN_0);
384         pm->input.relbit[LONG(REL_DIAL)] = BIT(REL_DIAL);
385         pm->input.mscbit[LONG(MSC_PULSELED)] = BIT(MSC_PULSELED);
386         pm->input.id.bustype = BUS_USB;
387         pm->input.id.vendor = udev->descriptor.idVendor;
388         pm->input.id.product = udev->descriptor.idProduct;
389         pm->input.id.version = udev->descriptor.bcdDevice;
390         pm->input.event = powermate_input_event;
391         pm->input.dev = &intf->dev;
392
393         input_register_device(&pm->input);
394
395         usb_make_path(udev, path, 64);
396         snprintf(pm->phys, 64, "%s/input0", path);
397         printk(KERN_INFO "input: %s on %s\n", pm->input.name, pm->input.phys);
398         
399         /* force an update of everything */
400         pm->requires_update = UPDATE_PULSE_ASLEEP | UPDATE_PULSE_AWAKE | UPDATE_PULSE_MODE | UPDATE_STATIC_BRIGHTNESS;
401         powermate_pulse_led(pm, 0x80, 255, 0, 1, 0); // set default pulse parameters
402   
403         usb_set_intfdata(intf, pm);
404         return 0;
405 }
406
407 /* Called when a USB device we've accepted ownership of is removed */
408 static void powermate_disconnect(struct usb_interface *intf)
409 {
410         struct powermate_device *pm = usb_get_intfdata (intf);
411
412         usb_set_intfdata(intf, NULL);
413         if (pm) {
414                 down(&pm->lock);
415                 pm->requires_update = 0;
416                 usb_unlink_urb(pm->irq);
417                 input_unregister_device(&pm->input);
418                 usb_free_urb(pm->irq);
419                 usb_free_urb(pm->config);
420                 powermate_free_buffers(interface_to_usbdev(intf), pm);
421
422                 kfree(pm);
423         }
424 }
425
426 static struct usb_device_id powermate_devices [] = {
427         { USB_DEVICE(POWERMATE_VENDOR, POWERMATE_PRODUCT_NEW) },
428         { USB_DEVICE(POWERMATE_VENDOR, POWERMATE_PRODUCT_OLD) },
429         { USB_DEVICE(CONTOUR_VENDOR, CONTOUR_JOG) },
430         { } /* Terminating entry */
431 };
432
433 MODULE_DEVICE_TABLE (usb, powermate_devices);
434
435 static struct usb_driver powermate_driver = {
436         .owner =        THIS_MODULE,
437         .name =         "powermate",
438         .probe =        powermate_probe,
439         .disconnect =   powermate_disconnect,
440         .id_table =     powermate_devices,
441 };
442
443 static int __init powermate_init(void)
444 {
445         return usb_register(&powermate_driver);
446 }
447
448 static void __exit powermate_cleanup(void)
449 {
450         usb_deregister(&powermate_driver);
451 }
452
453 module_init(powermate_init);
454 module_exit(powermate_cleanup);
455
456 MODULE_AUTHOR( "William R Sowerbutts" );
457 MODULE_DESCRIPTION( "Griffin Technology, Inc PowerMate driver" );
458 MODULE_LICENSE("GPL");