Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * A driver for the Griffin Technology, Inc. "PowerMate" USB controller dial.
0004  *
0005  * v1.1, (c)2002 William R Sowerbutts <will@sowerbutts.com>
0006  *
0007  * This device is a anodised aluminium knob which connects over USB. It can measure
0008  * clockwise and anticlockwise rotation. The dial also acts as a pushbutton with
0009  * a spring for automatic release. The base contains a pair of LEDs which illuminate
0010  * the translucent base. It rotates without limit and reports its relative rotation
0011  * back to the host when polled by the USB controller.
0012  *
0013  * Testing with the knob I have has shown that it measures approximately 94 "clicks"
0014  * for one full rotation. Testing with my High Speed Rotation Actuator (ok, it was
0015  * a variable speed cordless electric drill) has shown that the device can measure
0016  * speeds of up to 7 clicks either clockwise or anticlockwise between pollings from
0017  * the host. If it counts more than 7 clicks before it is polled, it will wrap back
0018  * to zero and start counting again. This was at quite high speed, however, almost
0019  * certainly faster than the human hand could turn it. Griffin say that it loses a
0020  * pulse or two on a direction change; the granularity is so fine that I never
0021  * noticed this in practice.
0022  *
0023  * The device's microcontroller can be programmed to set the LED to either a constant
0024  * intensity, or to a rhythmic pulsing. Several patterns and speeds are available.
0025  *
0026  * Griffin were very happy to provide documentation and free hardware for development.
0027  *
0028  * Some userspace tools are available on the web: http://sowerbutts.com/powermate/
0029  *
0030  */
0031 
0032 #include <linux/kernel.h>
0033 #include <linux/slab.h>
0034 #include <linux/module.h>
0035 #include <linux/spinlock.h>
0036 #include <linux/usb/input.h>
0037 
0038 #define POWERMATE_VENDOR    0x077d  /* Griffin Technology, Inc. */
0039 #define POWERMATE_PRODUCT_NEW   0x0410  /* Griffin PowerMate */
0040 #define POWERMATE_PRODUCT_OLD   0x04AA  /* Griffin soundKnob */
0041 
0042 #define CONTOUR_VENDOR      0x05f3  /* Contour Design, Inc. */
0043 #define CONTOUR_JOG     0x0240  /* Jog and Shuttle */
0044 
0045 /* these are the command codes we send to the device */
0046 #define SET_STATIC_BRIGHTNESS  0x01
0047 #define SET_PULSE_ASLEEP       0x02
0048 #define SET_PULSE_AWAKE        0x03
0049 #define SET_PULSE_MODE         0x04
0050 
0051 /* these refer to bits in the powermate_device's requires_update field. */
0052 #define UPDATE_STATIC_BRIGHTNESS (1<<0)
0053 #define UPDATE_PULSE_ASLEEP      (1<<1)
0054 #define UPDATE_PULSE_AWAKE       (1<<2)
0055 #define UPDATE_PULSE_MODE        (1<<3)
0056 
0057 /* at least two versions of the hardware exist, with differing payload
0058    sizes. the first three bytes always contain the "interesting" data in
0059    the relevant format. */
0060 #define POWERMATE_PAYLOAD_SIZE_MAX 6
0061 #define POWERMATE_PAYLOAD_SIZE_MIN 3
0062 struct powermate_device {
0063     signed char *data;
0064     dma_addr_t data_dma;
0065     struct urb *irq, *config;
0066     struct usb_ctrlrequest *configcr;
0067     struct usb_device *udev;
0068     struct usb_interface *intf;
0069     struct input_dev *input;
0070     spinlock_t lock;
0071     int static_brightness;
0072     int pulse_speed;
0073     int pulse_table;
0074     int pulse_asleep;
0075     int pulse_awake;
0076     int requires_update; // physical settings which are out of sync
0077     char phys[64];
0078 };
0079 
0080 static char pm_name_powermate[] = "Griffin PowerMate";
0081 static char pm_name_soundknob[] = "Griffin SoundKnob";
0082 
0083 static void powermate_config_complete(struct urb *urb);
0084 
0085 /* Callback for data arriving from the PowerMate over the USB interrupt pipe */
0086 static void powermate_irq(struct urb *urb)
0087 {
0088     struct powermate_device *pm = urb->context;
0089     struct device *dev = &pm->intf->dev;
0090     int retval;
0091 
0092     switch (urb->status) {
0093     case 0:
0094         /* success */
0095         break;
0096     case -ECONNRESET:
0097     case -ENOENT:
0098     case -ESHUTDOWN:
0099         /* this urb is terminated, clean up */
0100         dev_dbg(dev, "%s - urb shutting down with status: %d\n",
0101             __func__, urb->status);
0102         return;
0103     default:
0104         dev_dbg(dev, "%s - nonzero urb status received: %d\n",
0105             __func__, urb->status);
0106         goto exit;
0107     }
0108 
0109     /* handle updates to device state */
0110     input_report_key(pm->input, BTN_0, pm->data[0] & 0x01);
0111     input_report_rel(pm->input, REL_DIAL, pm->data[1]);
0112     input_sync(pm->input);
0113 
0114 exit:
0115     retval = usb_submit_urb (urb, GFP_ATOMIC);
0116     if (retval)
0117         dev_err(dev, "%s - usb_submit_urb failed with result: %d\n",
0118             __func__, retval);
0119 }
0120 
0121 /* Decide if we need to issue a control message and do so. Must be called with pm->lock taken */
0122 static void powermate_sync_state(struct powermate_device *pm)
0123 {
0124     if (pm->requires_update == 0)
0125         return; /* no updates are required */
0126     if (pm->config->status == -EINPROGRESS)
0127         return; /* an update is already in progress; it'll issue this update when it completes */
0128 
0129     if (pm->requires_update & UPDATE_PULSE_ASLEEP){
0130         pm->configcr->wValue = cpu_to_le16( SET_PULSE_ASLEEP );
0131         pm->configcr->wIndex = cpu_to_le16( pm->pulse_asleep ? 1 : 0 );
0132         pm->requires_update &= ~UPDATE_PULSE_ASLEEP;
0133     }else if (pm->requires_update & UPDATE_PULSE_AWAKE){
0134         pm->configcr->wValue = cpu_to_le16( SET_PULSE_AWAKE );
0135         pm->configcr->wIndex = cpu_to_le16( pm->pulse_awake ? 1 : 0 );
0136         pm->requires_update &= ~UPDATE_PULSE_AWAKE;
0137     }else if (pm->requires_update & UPDATE_PULSE_MODE){
0138         int op, arg;
0139         /* the powermate takes an operation and an argument for its pulse algorithm.
0140            the operation can be:
0141            0: divide the speed
0142            1: pulse at normal speed
0143            2: multiply the speed
0144            the argument only has an effect for operations 0 and 2, and ranges between
0145            1 (least effect) to 255 (maximum effect).
0146 
0147            thus, several states are equivalent and are coalesced into one state.
0148 
0149            we map this onto a range from 0 to 510, with:
0150            0 -- 254    -- use divide (0 = slowest)
0151            255         -- use normal speed
0152            256 -- 510  -- use multiple (510 = fastest).
0153 
0154            Only values of 'arg' quite close to 255 are particularly useful/spectacular.
0155         */
0156         if (pm->pulse_speed < 255) {
0157             op = 0;                   // divide
0158             arg = 255 - pm->pulse_speed;
0159         } else if (pm->pulse_speed > 255) {
0160             op = 2;                   // multiply
0161             arg = pm->pulse_speed - 255;
0162         } else {
0163             op = 1;                   // normal speed
0164             arg = 0;                  // can be any value
0165         }
0166         pm->configcr->wValue = cpu_to_le16( (pm->pulse_table << 8) | SET_PULSE_MODE );
0167         pm->configcr->wIndex = cpu_to_le16( (arg << 8) | op );
0168         pm->requires_update &= ~UPDATE_PULSE_MODE;
0169     } else if (pm->requires_update & UPDATE_STATIC_BRIGHTNESS) {
0170         pm->configcr->wValue = cpu_to_le16( SET_STATIC_BRIGHTNESS );
0171         pm->configcr->wIndex = cpu_to_le16( pm->static_brightness );
0172         pm->requires_update &= ~UPDATE_STATIC_BRIGHTNESS;
0173     } else {
0174         printk(KERN_ERR "powermate: unknown update required");
0175         pm->requires_update = 0; /* fudge the bug */
0176         return;
0177     }
0178 
0179 /*  printk("powermate: %04x %04x\n", pm->configcr->wValue, pm->configcr->wIndex); */
0180 
0181     pm->configcr->bRequestType = 0x41; /* vendor request */
0182     pm->configcr->bRequest = 0x01;
0183     pm->configcr->wLength = 0;
0184 
0185     usb_fill_control_urb(pm->config, pm->udev, usb_sndctrlpipe(pm->udev, 0),
0186                  (void *) pm->configcr, NULL, 0,
0187                  powermate_config_complete, pm);
0188 
0189     if (usb_submit_urb(pm->config, GFP_ATOMIC))
0190         printk(KERN_ERR "powermate: usb_submit_urb(config) failed");
0191 }
0192 
0193 /* Called when our asynchronous control message completes. We may need to issue another immediately */
0194 static void powermate_config_complete(struct urb *urb)
0195 {
0196     struct powermate_device *pm = urb->context;
0197     unsigned long flags;
0198 
0199     if (urb->status)
0200         printk(KERN_ERR "powermate: config urb returned %d\n", urb->status);
0201 
0202     spin_lock_irqsave(&pm->lock, flags);
0203     powermate_sync_state(pm);
0204     spin_unlock_irqrestore(&pm->lock, flags);
0205 }
0206 
0207 /* Set the LED up as described and begin the sync with the hardware if required */
0208 static void powermate_pulse_led(struct powermate_device *pm, int static_brightness, int pulse_speed,
0209                 int pulse_table, int pulse_asleep, int pulse_awake)
0210 {
0211     unsigned long flags;
0212 
0213     if (pulse_speed < 0)
0214         pulse_speed = 0;
0215     if (pulse_table < 0)
0216         pulse_table = 0;
0217     if (pulse_speed > 510)
0218         pulse_speed = 510;
0219     if (pulse_table > 2)
0220         pulse_table = 2;
0221 
0222     pulse_asleep = !!pulse_asleep;
0223     pulse_awake = !!pulse_awake;
0224 
0225 
0226     spin_lock_irqsave(&pm->lock, flags);
0227 
0228     /* mark state updates which are required */
0229     if (static_brightness != pm->static_brightness) {
0230         pm->static_brightness = static_brightness;
0231         pm->requires_update |= UPDATE_STATIC_BRIGHTNESS;
0232     }
0233     if (pulse_asleep != pm->pulse_asleep) {
0234         pm->pulse_asleep = pulse_asleep;
0235         pm->requires_update |= (UPDATE_PULSE_ASLEEP | UPDATE_STATIC_BRIGHTNESS);
0236     }
0237     if (pulse_awake != pm->pulse_awake) {
0238         pm->pulse_awake = pulse_awake;
0239         pm->requires_update |= (UPDATE_PULSE_AWAKE | UPDATE_STATIC_BRIGHTNESS);
0240     }
0241     if (pulse_speed != pm->pulse_speed || pulse_table != pm->pulse_table) {
0242         pm->pulse_speed = pulse_speed;
0243         pm->pulse_table = pulse_table;
0244         pm->requires_update |= UPDATE_PULSE_MODE;
0245     }
0246 
0247     powermate_sync_state(pm);
0248 
0249     spin_unlock_irqrestore(&pm->lock, flags);
0250 }
0251 
0252 /* Callback from the Input layer when an event arrives from userspace to configure the LED */
0253 static int powermate_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int _value)
0254 {
0255     unsigned int command = (unsigned int)_value;
0256     struct powermate_device *pm = input_get_drvdata(dev);
0257 
0258     if (type == EV_MSC && code == MSC_PULSELED){
0259         /*
0260             bits  0- 7: 8 bits: LED brightness
0261             bits  8-16: 9 bits: pulsing speed modifier (0 ... 510); 0-254 = slower, 255 = standard, 256-510 = faster.
0262             bits 17-18: 2 bits: pulse table (0, 1, 2 valid)
0263             bit     19: 1 bit : pulse whilst asleep?
0264             bit     20: 1 bit : pulse constantly?
0265         */
0266         int static_brightness = command & 0xFF;   // bits 0-7
0267         int pulse_speed = (command >> 8) & 0x1FF; // bits 8-16
0268         int pulse_table = (command >> 17) & 0x3;  // bits 17-18
0269         int pulse_asleep = (command >> 19) & 0x1; // bit 19
0270         int pulse_awake  = (command >> 20) & 0x1; // bit 20
0271 
0272         powermate_pulse_led(pm, static_brightness, pulse_speed, pulse_table, pulse_asleep, pulse_awake);
0273     }
0274 
0275     return 0;
0276 }
0277 
0278 static int powermate_alloc_buffers(struct usb_device *udev, struct powermate_device *pm)
0279 {
0280     pm->data = usb_alloc_coherent(udev, POWERMATE_PAYLOAD_SIZE_MAX,
0281                       GFP_KERNEL, &pm->data_dma);
0282     if (!pm->data)
0283         return -1;
0284 
0285     pm->configcr = kmalloc(sizeof(*(pm->configcr)), GFP_KERNEL);
0286     if (!pm->configcr)
0287         return -ENOMEM;
0288 
0289     return 0;
0290 }
0291 
0292 static void powermate_free_buffers(struct usb_device *udev, struct powermate_device *pm)
0293 {
0294     usb_free_coherent(udev, POWERMATE_PAYLOAD_SIZE_MAX,
0295               pm->data, pm->data_dma);
0296     kfree(pm->configcr);
0297 }
0298 
0299 /* Called whenever a USB device matching one in our supported devices table is connected */
0300 static int powermate_probe(struct usb_interface *intf, const struct usb_device_id *id)
0301 {
0302     struct usb_device *udev = interface_to_usbdev (intf);
0303     struct usb_host_interface *interface;
0304     struct usb_endpoint_descriptor *endpoint;
0305     struct powermate_device *pm;
0306     struct input_dev *input_dev;
0307     int pipe, maxp;
0308     int error = -ENOMEM;
0309 
0310     interface = intf->cur_altsetting;
0311     if (interface->desc.bNumEndpoints < 1)
0312         return -EINVAL;
0313 
0314     endpoint = &interface->endpoint[0].desc;
0315     if (!usb_endpoint_is_int_in(endpoint))
0316         return -EIO;
0317 
0318     usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0319         0x0a, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
0320         0, interface->desc.bInterfaceNumber, NULL, 0,
0321         USB_CTRL_SET_TIMEOUT);
0322 
0323     pm = kzalloc(sizeof(struct powermate_device), GFP_KERNEL);
0324     input_dev = input_allocate_device();
0325     if (!pm || !input_dev)
0326         goto fail1;
0327 
0328     if (powermate_alloc_buffers(udev, pm))
0329         goto fail2;
0330 
0331     pm->irq = usb_alloc_urb(0, GFP_KERNEL);
0332     if (!pm->irq)
0333         goto fail2;
0334 
0335     pm->config = usb_alloc_urb(0, GFP_KERNEL);
0336     if (!pm->config)
0337         goto fail3;
0338 
0339     pm->udev = udev;
0340     pm->intf = intf;
0341     pm->input = input_dev;
0342 
0343     usb_make_path(udev, pm->phys, sizeof(pm->phys));
0344     strlcat(pm->phys, "/input0", sizeof(pm->phys));
0345 
0346     spin_lock_init(&pm->lock);
0347 
0348     switch (le16_to_cpu(udev->descriptor.idProduct)) {
0349     case POWERMATE_PRODUCT_NEW:
0350         input_dev->name = pm_name_powermate;
0351         break;
0352     case POWERMATE_PRODUCT_OLD:
0353         input_dev->name = pm_name_soundknob;
0354         break;
0355     default:
0356         input_dev->name = pm_name_soundknob;
0357         printk(KERN_WARNING "powermate: unknown product id %04x\n",
0358                le16_to_cpu(udev->descriptor.idProduct));
0359     }
0360 
0361     input_dev->phys = pm->phys;
0362     usb_to_input_id(udev, &input_dev->id);
0363     input_dev->dev.parent = &intf->dev;
0364 
0365     input_set_drvdata(input_dev, pm);
0366 
0367     input_dev->event = powermate_input_event;
0368 
0369     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) |
0370         BIT_MASK(EV_MSC);
0371     input_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
0372     input_dev->relbit[BIT_WORD(REL_DIAL)] = BIT_MASK(REL_DIAL);
0373     input_dev->mscbit[BIT_WORD(MSC_PULSELED)] = BIT_MASK(MSC_PULSELED);
0374 
0375     /* get a handle to the interrupt data pipe */
0376     pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
0377     maxp = usb_maxpacket(udev, pipe);
0378 
0379     if (maxp < POWERMATE_PAYLOAD_SIZE_MIN || maxp > POWERMATE_PAYLOAD_SIZE_MAX) {
0380         printk(KERN_WARNING "powermate: Expected payload of %d--%d bytes, found %d bytes!\n",
0381             POWERMATE_PAYLOAD_SIZE_MIN, POWERMATE_PAYLOAD_SIZE_MAX, maxp);
0382         maxp = POWERMATE_PAYLOAD_SIZE_MAX;
0383     }
0384 
0385     usb_fill_int_urb(pm->irq, udev, pipe, pm->data,
0386              maxp, powermate_irq,
0387              pm, endpoint->bInterval);
0388     pm->irq->transfer_dma = pm->data_dma;
0389     pm->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0390 
0391     /* register our interrupt URB with the USB system */
0392     if (usb_submit_urb(pm->irq, GFP_KERNEL)) {
0393         error = -EIO;
0394         goto fail4;
0395     }
0396 
0397     error = input_register_device(pm->input);
0398     if (error)
0399         goto fail5;
0400 
0401 
0402     /* force an update of everything */
0403     pm->requires_update = UPDATE_PULSE_ASLEEP | UPDATE_PULSE_AWAKE | UPDATE_PULSE_MODE | UPDATE_STATIC_BRIGHTNESS;
0404     powermate_pulse_led(pm, 0x80, 255, 0, 1, 0); // set default pulse parameters
0405 
0406     usb_set_intfdata(intf, pm);
0407     return 0;
0408 
0409  fail5: usb_kill_urb(pm->irq);
0410  fail4: usb_free_urb(pm->config);
0411  fail3: usb_free_urb(pm->irq);
0412  fail2: powermate_free_buffers(udev, pm);
0413  fail1: input_free_device(input_dev);
0414     kfree(pm);
0415     return error;
0416 }
0417 
0418 /* Called when a USB device we've accepted ownership of is removed */
0419 static void powermate_disconnect(struct usb_interface *intf)
0420 {
0421     struct powermate_device *pm = usb_get_intfdata (intf);
0422 
0423     usb_set_intfdata(intf, NULL);
0424     if (pm) {
0425         pm->requires_update = 0;
0426         usb_kill_urb(pm->irq);
0427         input_unregister_device(pm->input);
0428         usb_free_urb(pm->irq);
0429         usb_free_urb(pm->config);
0430         powermate_free_buffers(interface_to_usbdev(intf), pm);
0431 
0432         kfree(pm);
0433     }
0434 }
0435 
0436 static const struct usb_device_id powermate_devices[] = {
0437     { USB_DEVICE(POWERMATE_VENDOR, POWERMATE_PRODUCT_NEW) },
0438     { USB_DEVICE(POWERMATE_VENDOR, POWERMATE_PRODUCT_OLD) },
0439     { USB_DEVICE(CONTOUR_VENDOR, CONTOUR_JOG) },
0440     { } /* Terminating entry */
0441 };
0442 
0443 MODULE_DEVICE_TABLE (usb, powermate_devices);
0444 
0445 static struct usb_driver powermate_driver = {
0446         .name =         "powermate",
0447         .probe =        powermate_probe,
0448         .disconnect =   powermate_disconnect,
0449         .id_table =     powermate_devices,
0450 };
0451 
0452 module_usb_driver(powermate_driver);
0453 
0454 MODULE_AUTHOR( "William R Sowerbutts" );
0455 MODULE_DESCRIPTION( "Griffin Technology, Inc PowerMate driver" );
0456 MODULE_LICENSE("GPL");