Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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