powermate.c 15 KB

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