Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 13 | * for one full rotation. Testing with my High Speed Rotation Actuator (ok, it was |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/spinlock.h> |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 35 | #include <linux/usb/input.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #define POWERMATE_VENDOR 0x077d /* Griffin Technology, Inc. */ |
| 38 | #define POWERMATE_PRODUCT_NEW 0x0410 /* Griffin PowerMate */ |
| 39 | #define POWERMATE_PRODUCT_OLD 0x04AA /* Griffin soundKnob */ |
| 40 | |
| 41 | #define CONTOUR_VENDOR 0x05f3 /* Contour Design, Inc. */ |
| 42 | #define CONTOUR_JOG 0x0240 /* Jog and Shuttle */ |
| 43 | |
| 44 | /* these are the command codes we send to the device */ |
| 45 | #define SET_STATIC_BRIGHTNESS 0x01 |
| 46 | #define SET_PULSE_ASLEEP 0x02 |
| 47 | #define SET_PULSE_AWAKE 0x03 |
| 48 | #define SET_PULSE_MODE 0x04 |
| 49 | |
| 50 | /* these refer to bits in the powermate_device's requires_update field. */ |
| 51 | #define UPDATE_STATIC_BRIGHTNESS (1<<0) |
| 52 | #define UPDATE_PULSE_ASLEEP (1<<1) |
| 53 | #define UPDATE_PULSE_AWAKE (1<<2) |
| 54 | #define UPDATE_PULSE_MODE (1<<3) |
| 55 | |
| 56 | /* at least two versions of the hardware exist, with differing payload |
| 57 | sizes. the first three bytes always contain the "interesting" data in |
| 58 | the relevant format. */ |
| 59 | #define POWERMATE_PAYLOAD_SIZE_MAX 6 |
| 60 | #define POWERMATE_PAYLOAD_SIZE_MIN 3 |
| 61 | struct powermate_device { |
| 62 | signed char *data; |
| 63 | dma_addr_t data_dma; |
| 64 | struct urb *irq, *config; |
| 65 | struct usb_ctrlrequest *configcr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | struct usb_device *udev; |
Greg Kroah-Hartman | c25e647 | 2012-05-04 15:33:00 -0700 | [diff] [blame] | 67 | struct usb_interface *intf; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 68 | struct input_dev *input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | spinlock_t lock; |
| 70 | int static_brightness; |
| 71 | int pulse_speed; |
| 72 | int pulse_table; |
| 73 | int pulse_asleep; |
| 74 | int pulse_awake; |
| 75 | int requires_update; // physical settings which are out of sync |
| 76 | char phys[64]; |
| 77 | }; |
| 78 | |
| 79 | static char pm_name_powermate[] = "Griffin PowerMate"; |
| 80 | static char pm_name_soundknob[] = "Griffin SoundKnob"; |
| 81 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 82 | static void powermate_config_complete(struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* Callback for data arriving from the PowerMate over the USB interrupt pipe */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 85 | static void powermate_irq(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | struct powermate_device *pm = urb->context; |
Greg Kroah-Hartman | c25e647 | 2012-05-04 15:33:00 -0700 | [diff] [blame] | 88 | struct device *dev = &pm->intf->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Greg Kroah-Hartman | 8b0725c | 2012-05-01 21:33:03 -0700 | [diff] [blame] | 99 | dev_dbg(dev, "%s - urb shutting down with status: %d\n", |
| 100 | __func__, urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return; |
| 102 | default: |
Greg Kroah-Hartman | 8b0725c | 2012-05-01 21:33:03 -0700 | [diff] [blame] | 103 | dev_dbg(dev, "%s - nonzero urb status received: %d\n", |
| 104 | __func__, urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | goto exit; |
| 106 | } |
| 107 | |
| 108 | /* handle updates to device state */ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | exit: |
| 114 | retval = usb_submit_urb (urb, GFP_ATOMIC); |
| 115 | if (retval) |
Greg Kroah-Hartman | 8b0725c | 2012-05-01 21:33:03 -0700 | [diff] [blame] | 116 | dev_err(dev, "%s - usb_submit_urb failed with result: %d\n", |
Greg Kroah-Hartman | 2385f3c | 2012-04-25 14:48:30 -0700 | [diff] [blame] | 117 | __func__, retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 123 | if (pm->requires_update == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return; /* no updates are required */ |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 125 | if (pm->config->status == -EINPROGRESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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). |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 154 | */ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 155 | if (pm->pulse_speed < 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | op = 0; // divide |
| 157 | arg = 255 - pm->pulse_speed; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 158 | } else if (pm->pulse_speed > 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 168 | } else if (pm->requires_update & UPDATE_STATIC_BRIGHTNESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 172 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | if (usb_submit_urb(pm->config, GFP_ATOMIC)) |
| 189 | printk(KERN_ERR "powermate: usb_submit_urb(config) failed"); |
| 190 | } |
| 191 | |
| 192 | /* Called when our asynchronous control message completes. We may need to issue another immediately */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 193 | static void powermate_config_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
| 195 | struct powermate_device *pm = urb->context; |
| 196 | unsigned long flags; |
| 197 | |
| 198 | if (urb->status) |
| 199 | printk(KERN_ERR "powermate: config urb returned %d\n", urb->status); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | spin_lock_irqsave(&pm->lock, flags); |
| 202 | powermate_sync_state(pm); |
| 203 | spin_unlock_irqrestore(&pm->lock, flags); |
| 204 | } |
| 205 | |
| 206 | /* Set the LED up as described and begin the sync with the hardware if required */ |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 207 | static void powermate_pulse_led(struct powermate_device *pm, int static_brightness, int pulse_speed, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | int pulse_table, int pulse_asleep, int pulse_awake) |
| 209 | { |
| 210 | unsigned long flags; |
| 211 | |
| 212 | if (pulse_speed < 0) |
| 213 | pulse_speed = 0; |
| 214 | if (pulse_table < 0) |
| 215 | pulse_table = 0; |
| 216 | if (pulse_speed > 510) |
| 217 | pulse_speed = 510; |
| 218 | if (pulse_table > 2) |
| 219 | pulse_table = 2; |
| 220 | |
| 221 | pulse_asleep = !!pulse_asleep; |
| 222 | pulse_awake = !!pulse_awake; |
| 223 | |
| 224 | |
| 225 | spin_lock_irqsave(&pm->lock, flags); |
| 226 | |
| 227 | /* mark state updates which are required */ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 228 | if (static_brightness != pm->static_brightness) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | pm->static_brightness = static_brightness; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 230 | pm->requires_update |= UPDATE_STATIC_BRIGHTNESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 232 | if (pulse_asleep != pm->pulse_asleep) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | pm->pulse_asleep = pulse_asleep; |
| 234 | pm->requires_update |= (UPDATE_PULSE_ASLEEP | UPDATE_STATIC_BRIGHTNESS); |
| 235 | } |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 236 | if (pulse_awake != pm->pulse_awake) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | pm->pulse_awake = pulse_awake; |
| 238 | pm->requires_update |= (UPDATE_PULSE_AWAKE | UPDATE_STATIC_BRIGHTNESS); |
| 239 | } |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 240 | if (pulse_speed != pm->pulse_speed || pulse_table != pm->pulse_table) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | pm->pulse_speed = pulse_speed; |
| 242 | pm->pulse_table = pulse_table; |
| 243 | pm->requires_update |= UPDATE_PULSE_MODE; |
| 244 | } |
| 245 | |
| 246 | powermate_sync_state(pm); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | spin_unlock_irqrestore(&pm->lock, flags); |
| 249 | } |
| 250 | |
| 251 | /* Callback from the Input layer when an event arrives from userspace to configure the LED */ |
| 252 | static int powermate_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int _value) |
| 253 | { |
| 254 | unsigned int command = (unsigned int)_value; |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 255 | struct powermate_device *pm = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | if (type == EV_MSC && code == MSC_PULSELED){ |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 258 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | bits 0- 7: 8 bits: LED brightness |
| 260 | bits 8-16: 9 bits: pulsing speed modifier (0 ... 510); 0-254 = slower, 255 = standard, 256-510 = faster. |
| 261 | bits 17-18: 2 bits: pulse table (0, 1, 2 valid) |
| 262 | bit 19: 1 bit : pulse whilst asleep? |
| 263 | bit 20: 1 bit : pulse constantly? |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 264 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | int static_brightness = command & 0xFF; // bits 0-7 |
| 266 | int pulse_speed = (command >> 8) & 0x1FF; // bits 8-16 |
| 267 | int pulse_table = (command >> 17) & 0x3; // bits 17-18 |
| 268 | int pulse_asleep = (command >> 19) & 0x1; // bit 19 |
| 269 | int pulse_awake = (command >> 20) & 0x1; // bit 20 |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | powermate_pulse_led(pm, static_brightness, pulse_speed, pulse_table, pulse_asleep, pulse_awake); |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int powermate_alloc_buffers(struct usb_device *udev, struct powermate_device *pm) |
| 278 | { |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 279 | pm->data = usb_alloc_coherent(udev, POWERMATE_PAYLOAD_SIZE_MAX, |
| 280 | GFP_ATOMIC, &pm->data_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if (!pm->data) |
| 282 | return -1; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 283 | |
Alan Stern | 0ede76f | 2010-03-05 15:10:17 -0500 | [diff] [blame] | 284 | pm->configcr = kmalloc(sizeof(*(pm->configcr)), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (!pm->configcr) |
Davidlohr Bueso | 6792cbb | 2010-09-29 18:53:35 -0700 | [diff] [blame] | 286 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static void powermate_free_buffers(struct usb_device *udev, struct powermate_device *pm) |
| 292 | { |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 293 | usb_free_coherent(udev, POWERMATE_PAYLOAD_SIZE_MAX, |
| 294 | pm->data, pm->data_dma); |
Alan Stern | 0ede76f | 2010-03-05 15:10:17 -0500 | [diff] [blame] | 295 | kfree(pm->configcr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 305 | struct input_dev *input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | int pipe, maxp; |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 307 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | interface = intf->cur_altsetting; |
Josh Boyer | 9c6ba45 | 2016-03-14 09:33:40 -0700 | [diff] [blame] | 310 | if (interface->desc.bNumEndpoints < 1) |
| 311 | return -EINVAL; |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | endpoint = &interface->endpoint[0].desc; |
Luiz Fernando N. Capitulino | 60ca126 | 2006-09-27 11:58:53 -0700 | [diff] [blame] | 314 | if (!usb_endpoint_is_int_in(endpoint)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return -EIO; |
| 316 | |
| 317 | usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 318 | 0x0a, USB_TYPE_CLASS | USB_RECIP_INTERFACE, |
| 319 | 0, interface->desc.bInterfaceNumber, NULL, 0, |
| 320 | USB_CTRL_SET_TIMEOUT); |
| 321 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 322 | pm = kzalloc(sizeof(struct powermate_device), GFP_KERNEL); |
| 323 | input_dev = input_allocate_device(); |
| 324 | if (!pm || !input_dev) |
| 325 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 327 | if (powermate_alloc_buffers(udev, pm)) |
| 328 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 330 | pm->irq = usb_alloc_urb(0, GFP_KERNEL); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 331 | if (!pm->irq) |
| 332 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | pm->config = usb_alloc_urb(0, GFP_KERNEL); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 335 | if (!pm->config) |
| 336 | goto fail3; |
| 337 | |
| 338 | pm->udev = udev; |
Greg Kroah-Hartman | c25e647 | 2012-05-04 15:33:00 -0700 | [diff] [blame] | 339 | pm->intf = intf; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 340 | pm->input = input_dev; |
| 341 | |
| 342 | usb_make_path(udev, pm->phys, sizeof(pm->phys)); |
Márton Németh | 6236dfa | 2009-11-23 08:26:38 -0800 | [diff] [blame] | 343 | strlcat(pm->phys, "/input0", sizeof(pm->phys)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | spin_lock_init(&pm->lock); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 346 | |
| 347 | switch (le16_to_cpu(udev->descriptor.idProduct)) { |
| 348 | case POWERMATE_PRODUCT_NEW: |
| 349 | input_dev->name = pm_name_powermate; |
| 350 | break; |
| 351 | case POWERMATE_PRODUCT_OLD: |
| 352 | input_dev->name = pm_name_soundknob; |
| 353 | break; |
| 354 | default: |
| 355 | input_dev->name = pm_name_soundknob; |
| 356 | printk(KERN_WARNING "powermate: unknown product id %04x\n", |
| 357 | le16_to_cpu(udev->descriptor.idProduct)); |
| 358 | } |
| 359 | |
| 360 | input_dev->phys = pm->phys; |
| 361 | usb_to_input_id(udev, &input_dev->id); |
Dmitry Torokhov | c0f82d5 | 2007-04-12 01:35:03 -0400 | [diff] [blame] | 362 | input_dev->dev.parent = &intf->dev; |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 363 | |
| 364 | input_set_drvdata(input_dev, pm); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 365 | |
| 366 | input_dev->event = powermate_input_event; |
| 367 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 368 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) | |
| 369 | BIT_MASK(EV_MSC); |
| 370 | input_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0); |
| 371 | input_dev->relbit[BIT_WORD(REL_DIAL)] = BIT_MASK(REL_DIAL); |
| 372 | input_dev->mscbit[BIT_WORD(MSC_PULSELED)] = BIT_MASK(MSC_PULSELED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | /* get a handle to the interrupt data pipe */ |
| 375 | pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress); |
| 376 | maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe)); |
| 377 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 378 | if (maxp < POWERMATE_PAYLOAD_SIZE_MIN || maxp > POWERMATE_PAYLOAD_SIZE_MAX) { |
| 379 | printk(KERN_WARNING "powermate: Expected payload of %d--%d bytes, found %d bytes!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | POWERMATE_PAYLOAD_SIZE_MIN, POWERMATE_PAYLOAD_SIZE_MAX, maxp); |
| 381 | maxp = POWERMATE_PAYLOAD_SIZE_MAX; |
| 382 | } |
| 383 | |
| 384 | usb_fill_int_urb(pm->irq, udev, pipe, pm->data, |
| 385 | maxp, powermate_irq, |
| 386 | pm, endpoint->bInterval); |
| 387 | pm->irq->transfer_dma = pm->data_dma; |
| 388 | pm->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 389 | |
| 390 | /* register our interrupt URB with the USB system */ |
| 391 | if (usb_submit_urb(pm->irq, GFP_KERNEL)) { |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 392 | error = -EIO; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 393 | goto fail4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 396 | error = input_register_device(pm->input); |
| 397 | if (error) |
| 398 | goto fail5; |
| 399 | |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* force an update of everything */ |
| 402 | pm->requires_update = UPDATE_PULSE_ASLEEP | UPDATE_PULSE_AWAKE | UPDATE_PULSE_MODE | UPDATE_STATIC_BRIGHTNESS; |
| 403 | powermate_pulse_led(pm, 0x80, 255, 0, 1, 0); // set default pulse parameters |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | usb_set_intfdata(intf, pm); |
| 406 | return 0; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 407 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 408 | fail5: usb_kill_urb(pm->irq); |
| 409 | fail4: usb_free_urb(pm->config); |
| 410 | fail3: usb_free_urb(pm->irq); |
| 411 | fail2: powermate_free_buffers(udev, pm); |
| 412 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 413 | kfree(pm); |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 414 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* Called when a USB device we've accepted ownership of is removed */ |
| 418 | static void powermate_disconnect(struct usb_interface *intf) |
| 419 | { |
| 420 | struct powermate_device *pm = usb_get_intfdata (intf); |
| 421 | |
| 422 | usb_set_intfdata(intf, NULL); |
| 423 | if (pm) { |
| 424 | pm->requires_update = 0; |
| 425 | usb_kill_urb(pm->irq); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 426 | input_unregister_device(pm->input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | usb_free_urb(pm->irq); |
| 428 | usb_free_urb(pm->config); |
| 429 | powermate_free_buffers(interface_to_usbdev(intf), pm); |
| 430 | |
| 431 | kfree(pm); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | static struct usb_device_id powermate_devices [] = { |
| 436 | { USB_DEVICE(POWERMATE_VENDOR, POWERMATE_PRODUCT_NEW) }, |
| 437 | { USB_DEVICE(POWERMATE_VENDOR, POWERMATE_PRODUCT_OLD) }, |
| 438 | { USB_DEVICE(CONTOUR_VENDOR, CONTOUR_JOG) }, |
| 439 | { } /* Terminating entry */ |
| 440 | }; |
| 441 | |
| 442 | MODULE_DEVICE_TABLE (usb, powermate_devices); |
| 443 | |
| 444 | static struct usb_driver powermate_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | .name = "powermate", |
| 446 | .probe = powermate_probe, |
| 447 | .disconnect = powermate_disconnect, |
| 448 | .id_table = powermate_devices, |
| 449 | }; |
| 450 | |
Greg Kroah-Hartman | 08642e7 | 2011-11-18 09:48:31 -0800 | [diff] [blame] | 451 | module_usb_driver(powermate_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | MODULE_AUTHOR( "William R Sowerbutts" ); |
| 454 | MODULE_DESCRIPTION( "Griffin Technology, Inc PowerMate driver" ); |
| 455 | MODULE_LICENSE("GPL"); |