Linux Audio

Check our new training course

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