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