| 1 | |
| 2 | |
| 3 | * A driver for the Griffin Technology, Inc. "PowerMate" USB controller dial.
|
| 4 | *
|
| 5 | * v1.1, (c)2002 William R Sowerbutts <[email protected]>
|
| 6 | *
|
| 7 | * This device is a anodised aluminium knob which connects over USB. It can measure
|
| 8 | * clockwise and anticlockwise rotation. The dial also acts as a pushbutton with
|
| 9 | * a spring for automatic release. The base contains a pair of LEDs which illuminate
|
| 10 | * the translucent base. It rotates without limit and reports its relative rotation
|
| 11 | * back to the host when polled by the USB controller.
|
| 12 | *
|
| 13 | * Testing with the knob I have has shown that it measures approximately 94 "clicks"
|
| 14 | * for one full rotation. Testing with my High Speed Rotation Actuator (ok, it was
|
| 15 | * a variable speed cordless electric drill) has shown that the device can measure
|
| 16 | * speeds of up to 7 clicks either clockwise or anticlockwise between pollings from
|
| 17 | * the host. If it counts more than 7 clicks before it is polled, it will wrap back
|
| 18 | * to zero and start counting again. This was at quite high speed, however, almost
|
| 19 | * certainly faster than the human hand could turn it. Griffin say that it loses a
|
| 20 | * pulse or two on a direction change; the granularity is so fine that I never
|
| 21 | * noticed this in practice.
|
| 22 | *
|
| 23 | * The device's microcontroller can be programmed to set the LED to either a constant
|
| 24 | * intensity, or to a rhythmic pulsing. Several patterns and speeds are available.
|
| 25 | *
|
| 26 | * Griffin were very happy to provide documentation and free hardware for development.
|
| 27 | *
|
| 28 | * Some userspace tools are available on the web: http://sowerbutts.com/powermate/
|
| 29 | *
|
| 30 |
|
| 31 |
|
| 32 | #include <linux/kernel.h>
|
| 33 | #include <linux/slab.h>
|
| 34 | #include <linux/module.h>
|
| 35 | #include <linux/spinlock.h>
|
| 36 | #include <linux/usb/input.h>
|
| 37 |
|
| 38 | #define POWERMATE_VENDOR 0x077d
|
| 39 | #define POWERMATE_PRODUCT_NEW 0x0410
|
| 40 | #define POWERMATE_PRODUCT_OLD 0x04AA
|
| 41 |
|
| 42 | #define CONTOUR_VENDOR 0x05f3
|
| 43 | #define CONTOUR_JOG 0x0240
|
| 44 |
|
| 45 |
|
| 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 |
|
| 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 | |
| 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 | struct usb_device *udev;
|
| 68 | struct usb_interface *intf;
|
| 69 | struct input_dev *input;
|
| 70 | spinlock_t 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; |
| 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);
|
| 84 |
|
| 85 |
|
| 86 | static void powermate_irq(struct urb *urb)
|
| 87 | {
|
| 88 | struct powermate_device *pm = urb->context;
|
| 89 | struct device *dev = &pm->intf->dev;
|
| 90 | int retval;
|
| 91 |
|
| 92 | switch (urb->status) {
|
| 93 | case 0:
|
| 94 |
|
| 95 | break;
|
| 96 | case -ECONNRESET:
|
| 97 | case -ENOENT:
|
| 98 | case -ESHUTDOWN:
|
| 99 |
|
| 100 | dev_dbg(dev, "%s - urb shutting down with status: %d\n",
|
| 101 | __func__, urb->status);
|
| 102 | return;
|
| 103 | default:
|
| 104 | dev_dbg(dev, "%s - nonzero urb status received: %d\n",
|
| 105 | __func__, urb->status);
|
| 106 | goto exit;
|
| 107 | }
|
| 108 |
|
| 109 |
|
| 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 | dev_err(dev, "%s - usb_submit_urb failed with result: %d\n",
|
| 118 | __func__, retval);
|
| 119 | }
|
| 120 |
|
| 121 |
|
| 122 | static void powermate_sync_state(struct powermate_device *pm)
|
| 123 | {
|
| 124 | if (pm->requires_update == 0)
|
| 125 | return;
|
| 126 | if (pm->config->status == -EINPROGRESS)
|
| 127 | return;
|
| 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 | |
| 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; |
| 158 | arg = 255 - pm->pulse_speed;
|
| 159 | } else if (pm->pulse_speed > 255) {
|
| 160 | op = 2; |
| 161 | arg = pm->pulse_speed - 255;
|
| 162 | } else {
|
| 163 | op = 1; |
| 164 | arg = 0; |
| 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;
|
| 176 | return;
|
| 177 | }
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 | pm->configcr->bRequestType = 0x41;
|
| 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 |
|
| 189 | if (usb_submit_urb(pm->config, GFP_ATOMIC))
|
| 190 | printk(KERN_ERR "powermate: usb_submit_urb(config) failed");
|
| 191 | }
|
| 192 |
|
| 193 |
|
| 194 | static void powermate_config_complete(struct urb *urb)
|
| 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 | guard(spinlock_irqsave)(&pm->lock);
|
| 202 | powermate_sync_state(pm);
|
| 203 | }
|
| 204 |
|
| 205 |
|
| 206 | static void powermate_pulse_led(struct powermate_device *pm, int static_brightness, int pulse_speed,
|
| 207 | int pulse_table, int pulse_asleep, int pulse_awake)
|
| 208 | {
|
| 209 | if (pulse_speed < 0)
|
| 210 | pulse_speed = 0;
|
| 211 | if (pulse_table < 0)
|
| 212 | pulse_table = 0;
|
| 213 | if (pulse_speed > 510)
|
| 214 | pulse_speed = 510;
|
| 215 | if (pulse_table > 2)
|
| 216 | pulse_table = 2;
|
| 217 |
|
| 218 | pulse_asleep = !!pulse_asleep;
|
| 219 | pulse_awake = !!pulse_awake;
|
| 220 |
|
| 221 | guard(spinlock_irqsave)(&pm->lock);
|
| 222 |
|
| 223 |
|
| 224 | if (static_brightness != pm->static_brightness) {
|
| 225 | pm->static_brightness = static_brightness;
|
| 226 | pm->requires_update |= UPDATE_STATIC_BRIGHTNESS;
|
| 227 | }
|
| 228 | if (pulse_asleep != pm->pulse_asleep) {
|
| 229 | pm->pulse_asleep = pulse_asleep;
|
| 230 | pm->requires_update |= (UPDATE_PULSE_ASLEEP | UPDATE_STATIC_BRIGHTNESS);
|
| 231 | }
|
| 232 | if (pulse_awake != pm->pulse_awake) {
|
| 233 | pm->pulse_awake = pulse_awake;
|
| 234 | pm->requires_update |= (UPDATE_PULSE_AWAKE | UPDATE_STATIC_BRIGHTNESS);
|
| 235 | }
|
| 236 | if (pulse_speed != pm->pulse_speed || pulse_table != pm->pulse_table) {
|
| 237 | pm->pulse_speed = pulse_speed;
|
| 238 | pm->pulse_table = pulse_table;
|
| 239 | pm->requires_update |= UPDATE_PULSE_MODE;
|
| 240 | }
|
| 241 |
|
| 242 | powermate_sync_state(pm);
|
| 243 | }
|
| 244 |
|
| 245 |
|
| 246 | static int powermate_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int _value)
|
| 247 | {
|
| 248 | unsigned int command = (unsigned int)_value;
|
| 249 | struct powermate_device *pm = input_get_drvdata(dev);
|
| 250 |
|
| 251 | if (type == EV_MSC && code == MSC_PULSELED){
|
| 252 | |
| 253 | bits 0- 7: 8 bits: LED brightness
|
| 254 | bits 8-16: 9 bits: pulsing speed modifier (0 ... 510); 0-254 = slower, 255 = standard, 256-510 = faster.
|
| 255 | bits 17-18: 2 bits: pulse table (0, 1, 2 valid)
|
| 256 | bit 19: 1 bit : pulse whilst asleep?
|
| 257 | bit 20: 1 bit : pulse constantly?
|
| 258 |
|
| 259 | int static_brightness = command & 0xFF; |
| 260 | int pulse_speed = (command >> 8) & 0x1FF; |
| 261 | int pulse_table = (command >> 17) & 0x3; |
| 262 | int pulse_asleep = (command >> 19) & 0x1; |
| 263 | int pulse_awake = (command >> 20) & 0x1; |
| 264 |
|
| 265 | powermate_pulse_led(pm, static_brightness, pulse_speed, pulse_table, pulse_asleep, pulse_awake);
|
| 266 | }
|
| 267 |
|
| 268 | return 0;
|
| 269 | }
|
| 270 |
|
| 271 | static int powermate_alloc_buffers(struct usb_device *udev, struct powermate_device *pm)
|
| 272 | {
|
| 273 | pm->data = usb_alloc_coherent(udev, POWERMATE_PAYLOAD_SIZE_MAX,
|
| 274 | GFP_KERNEL, &pm->data_dma);
|
| 275 | if (!pm->data)
|
| 276 | return -1;
|
| 277 |
|
| 278 | pm->configcr = kmalloc_obj(*(pm->configcr));
|
| 279 | if (!pm->configcr)
|
| 280 | return -ENOMEM;
|
| 281 |
|
| 282 | return 0;
|
| 283 | }
|
| 284 |
|
| 285 | static void powermate_free_buffers(struct usb_device *udev, struct powermate_device *pm)
|
| 286 | {
|
| 287 | usb_free_coherent(udev, POWERMATE_PAYLOAD_SIZE_MAX,
|
| 288 | pm->data, pm->data_dma);
|
| 289 | kfree(pm->configcr);
|
| 290 | }
|
| 291 |
|
| 292 |
|
| 293 | static int powermate_probe(struct usb_interface *intf, const struct usb_device_id *id)
|
| 294 | {
|
| 295 | struct usb_device *udev = interface_to_usbdev (intf);
|
| 296 | struct usb_host_interface *interface;
|
| 297 | struct usb_endpoint_descriptor *endpoint;
|
| 298 | struct powermate_device *pm;
|
| 299 | struct input_dev *input_dev;
|
| 300 | int pipe, maxp;
|
| 301 | int error = -ENOMEM;
|
| 302 |
|
| 303 | interface = intf->cur_altsetting;
|
| 304 | if (interface->desc.bNumEndpoints < 1)
|
| 305 | return -EINVAL;
|
| 306 |
|
| 307 | endpoint = &interface->endpoint[0].desc;
|
| 308 | if (!usb_endpoint_is_int_in(endpoint))
|
| 309 | return -EIO;
|
| 310 |
|
| 311 | usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
|
| 312 | 0x0a, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
|
| 313 | 0, interface->desc.bInterfaceNumber, NULL, 0,
|
| 314 | USB_CTRL_SET_TIMEOUT);
|
| 315 |
|
| 316 | pm = kzalloc_obj(*pm);
|
| 317 | input_dev = input_allocate_device();
|
| 318 | if (!pm || !input_dev)
|
| 319 | goto fail1;
|
| 320 |
|
| 321 | if (powermate_alloc_buffers(udev, pm))
|
| 322 | goto fail2;
|
| 323 |
|
| 324 | pm->irq = usb_alloc_urb(0, GFP_KERNEL);
|
| 325 | if (!pm->irq)
|
| 326 | goto fail2;
|
| 327 |
|
| 328 | pm->config = usb_alloc_urb(0, GFP_KERNEL);
|
| 329 | if (!pm->config)
|
| 330 | goto fail3;
|
| 331 |
|
| 332 | pm->udev = udev;
|
| 333 | pm->intf = intf;
|
| 334 | pm->input = input_dev;
|
| 335 |
|
| 336 | usb_make_path(udev, pm->phys, sizeof(pm->phys));
|
| 337 | strlcat(pm->phys, "/input0", sizeof(pm->phys));
|
| 338 |
|
| 339 | spin_lock_init(&pm->lock);
|
| 340 |
|
| 341 | switch (le16_to_cpu(udev->descriptor.idProduct)) {
|
| 342 | case POWERMATE_PRODUCT_NEW:
|
| 343 | input_dev->name = pm_name_powermate;
|
| 344 | break;
|
| 345 | case POWERMATE_PRODUCT_OLD:
|
| 346 | input_dev->name = pm_name_soundknob;
|
| 347 | break;
|
| 348 | default:
|
| 349 | input_dev->name = pm_name_soundknob;
|
| 350 | printk(KERN_WARNING "powermate: unknown product id %04x\n",
|
| 351 | le16_to_cpu(udev->descriptor.idProduct));
|
| 352 | }
|
| 353 |
|
| 354 | input_dev->phys = pm->phys;
|
| 355 | usb_to_input_id(udev, &input_dev->id);
|
| 356 | input_dev->dev.parent = &intf->dev;
|
| 357 |
|
| 358 | input_set_drvdata(input_dev, pm);
|
| 359 |
|
| 360 | input_dev->event = powermate_input_event;
|
| 361 |
|
| 362 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) |
|
| 363 | BIT_MASK(EV_MSC);
|
| 364 | input_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
|
| 365 | input_dev->relbit[BIT_WORD(REL_DIAL)] = BIT_MASK(REL_DIAL);
|
| 366 | input_dev->mscbit[BIT_WORD(MSC_PULSELED)] = BIT_MASK(MSC_PULSELED);
|
| 367 |
|
| 368 |
|
| 369 | pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
|
| 370 | maxp = usb_maxpacket(udev, pipe);
|
| 371 |
|
| 372 | if (maxp < POWERMATE_PAYLOAD_SIZE_MIN || maxp > POWERMATE_PAYLOAD_SIZE_MAX) {
|
| 373 | printk(KERN_WARNING "powermate: Expected payload of %d--%d bytes, found %d bytes!\n",
|
| 374 | POWERMATE_PAYLOAD_SIZE_MIN, POWERMATE_PAYLOAD_SIZE_MAX, maxp);
|
| 375 | maxp = POWERMATE_PAYLOAD_SIZE_MAX;
|
| 376 | }
|
| 377 |
|
| 378 | usb_fill_int_urb(pm->irq, udev, pipe, pm->data,
|
| 379 | maxp, powermate_irq,
|
| 380 | pm, endpoint->bInterval);
|
| 381 | pm->irq->transfer_dma = pm->data_dma;
|
| 382 | pm->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
|
| 383 |
|
| 384 |
|
| 385 | if (usb_submit_urb(pm->irq, GFP_KERNEL)) {
|
| 386 | error = -EIO;
|
| 387 | goto fail4;
|
| 388 | }
|
| 389 |
|
| 390 | error = input_register_device(pm->input);
|
| 391 | if (error)
|
| 392 | goto fail5;
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 | pm->requires_update = UPDATE_PULSE_ASLEEP | UPDATE_PULSE_AWAKE | UPDATE_PULSE_MODE | UPDATE_STATIC_BRIGHTNESS;
|
| 397 | powermate_pulse_led(pm, 0x80, 255, 0, 1, 0); |
| 398 |
|
| 399 | usb_set_intfdata(intf, pm);
|
| 400 | return 0;
|
| 401 |
|
| 402 | fail5: usb_kill_urb(pm->irq);
|
| 403 | fail4: usb_free_urb(pm->config);
|
| 404 | fail3: usb_free_urb(pm->irq);
|
| 405 | fail2: powermate_free_buffers(udev, pm);
|
| 406 | fail1: input_free_device(input_dev);
|
| 407 | kfree(pm);
|
| 408 | return error;
|
| 409 | }
|
| 410 |
|
| 411 |
|
| 412 | static void powermate_disconnect(struct usb_interface *intf)
|
| 413 | {
|
| 414 | struct powermate_device *pm = usb_get_intfdata (intf);
|
| 415 |
|
| 416 | usb_set_intfdata(intf, NULL);
|
| 417 | if (pm) {
|
| 418 | pm->requires_update = 0;
|
| 419 | usb_kill_urb(pm->irq);
|
| 420 | input_unregister_device(pm->input);
|
| 421 | usb_kill_urb(pm->config);
|
| 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 const 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 | { }
|
| 435 | };
|
| 436 |
|
| 437 | MODULE_DEVICE_TABLE (usb, powermate_devices);
|
| 438 |
|
| 439 | static struct usb_driver powermate_driver = {
|
| 440 | .name = "powermate",
|
| 441 | .probe = powermate_probe,
|
| 442 | .disconnect = powermate_disconnect,
|
| 443 | .id_table = powermate_devices,
|
| 444 | };
|
| 445 |
|
| 446 | module_usb_driver(powermate_driver);
|
| 447 |
|
| 448 | MODULE_AUTHOR( "William R Sowerbutts" );
|
| 449 | MODULE_DESCRIPTION( "Griffin Technology, Inc PowerMate driver" );
|
| 450 | MODULE_LICENSE("GPL");
|