powermate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * A driver for the Griffin Technology, Inc. "PowerMate" USB controller dial.
  4. *
  5. * v1.1, (c)2002 William R Sowerbutts <[email protected]>
  6. *
  7. * This device is a anodised aluminium knob which connects over USB. It can measure
  8. * clockwise and anticlockwise rotation. The dial also acts as a pushbutton with
  9. * a spring for automatic release. The base contains a pair of LEDs which illuminate
  10. * the translucent base. It rotates without limit and reports its relative rotation
  11. * back to the host when polled by the USB controller.
  12. *
  13. * Testing with the knob I have has shown that it measures approximately 94 "clicks"
  14. * for one full rotation. Testing with my High Speed Rotation Actuator (ok, it was
  15. * a variable speed cordless electric drill) has shown that the device can measure
  16. * speeds of up to 7 clicks either clockwise or anticlockwise between pollings from
  17. * the host. If it counts more than 7 clicks before it is polled, it will wrap back
  18. * to zero and start counting again. This was at quite high speed, however, almost
  19. * certainly faster than the human hand could turn it. Griffin say that it loses a
  20. * pulse or two on a direction change; the granularity is so fine that I never
  21. * noticed this in practice.
  22. *
  23. * The device's microcontroller can be programmed to set the LED to either a constant
  24. * intensity, or to a rhythmic pulsing. Several patterns and speeds are available.
  25. *
  26. * Griffin were very happy to provide documentation and free hardware for development.
  27. *
  28. * Some userspace tools are available on the web: http://sowerbutts.com/powermate/
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/usb/input.h>
  36. #define POWERMATE_VENDOR 0x077d /* Griffin Technology, Inc. */
  37. #define POWERMATE_PRODUCT_NEW 0x0410 /* Griffin PowerMate */
  38. #define POWERMATE_PRODUCT_OLD 0x04AA /* Griffin soundKnob */
  39. #define CONTOUR_VENDOR 0x05f3 /* Contour Design, Inc. */
  40. #define CONTOUR_JOG 0x0240 /* Jog and Shuttle */
  41. /* these are the command codes we send to the device */
  42. #define SET_STATIC_BRIGHTNESS 0x01
  43. #define SET_PULSE_ASLEEP 0x02
  44. #define SET_PULSE_AWAKE 0x03
  45. #define SET_PULSE_MODE 0x04
  46. /* these refer to bits in the powermate_device's requires_update field. */
  47. #define UPDATE_STATIC_BRIGHTNESS (1<<0)
  48. #define UPDATE_PULSE_ASLEEP (1<<1)
  49. #define UPDATE_PULSE_AWAKE (1<<2)
  50. #define UPDATE_PULSE_MODE (1<<3)
  51. /* at least two versions of the hardware exist, with differing payload
  52. sizes. the first three bytes always contain the "interesting" data in
  53. the relevant format. */
  54. #define POWERMATE_PAYLOAD_SIZE_MAX 6
  55. #define POWERMATE_PAYLOAD_SIZE_MIN 3
  56. struct powermate_device {
  57. signed char *data;
  58. dma_addr_t data_dma;
  59. struct urb *irq, *config;
  60. struct usb_ctrlrequest *configcr;
  61. struct usb_device *udev;
  62. struct usb_interface *intf;
  63. struct input_dev *input;
  64. spinlock_t lock;
  65. int static_brightness;
  66. int pulse_speed;
  67. int pulse_table;
  68. int pulse_asleep;
  69. int pulse_awake;
  70. int requires_update; // physical settings which are out of sync
  71. char phys[64];
  72. };
  73. static char pm_name_powermate[] = "Griffin PowerMate";
  74. static char pm_name_soundknob[] = "Griffin SoundKnob";
  75. static void powermate_config_complete(struct urb *urb);
  76. /* Callback for data arriving from the PowerMate over the USB interrupt pipe */
  77. static void powermate_irq(struct urb *urb)
  78. {
  79. struct powermate_device *pm = urb->context;
  80. struct device *dev = &pm->intf->dev;
  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. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  91. __func__, urb->status);
  92. return;
  93. default:
  94. dev_dbg(dev, "%s - nonzero urb status received: %d\n",
  95. __func__, urb->status);
  96. goto exit;
  97. }
  98. /* handle updates to device state */
  99. input_report_key(pm->input, BTN_0, pm->data[0] & 0x01);
  100. input_report_rel(pm->input, REL_DIAL, pm->data[1]);
  101. input_sync(pm->input);
  102. exit:
  103. retval = usb_submit_urb (urb, GFP_ATOMIC);
  104. if (retval)
  105. dev_err(dev, "%s - usb_submit_urb failed with result: %d\n",
  106. __func__, retval);
  107. }
  108. /* Decide if we need to issue a control message and do so. Must be called with pm->lock taken */
  109. static void powermate_sync_state(struct powermate_device *pm)
  110. {
  111. if (pm->requires_update == 0)
  112. return; /* no updates are required */
  113. if (pm->config->status == -EINPROGRESS)
  114. return; /* an update is already in progress; it'll issue this update when it completes */
  115. if (pm->requires_update & UPDATE_PULSE_ASLEEP){
  116. pm->configcr->wValue = cpu_to_le16( SET_PULSE_ASLEEP );
  117. pm->configcr->wIndex = cpu_to_le16( pm->pulse_asleep ? 1 : 0 );
  118. pm->requires_update &= ~UPDATE_PULSE_ASLEEP;
  119. }else if (pm->requires_update & UPDATE_PULSE_AWAKE){
  120. pm->configcr->wValue = cpu_to_le16( SET_PULSE_AWAKE );
  121. pm->configcr->wIndex = cpu_to_le16( pm->pulse_awake ? 1 : 0 );
  122. pm->requires_update &= ~UPDATE_PULSE_AWAKE;
  123. }else if (pm->requires_update & UPDATE_PULSE_MODE){
  124. int op, arg;
  125. /* the powermate takes an operation and an argument for its pulse algorithm.
  126. the operation can be:
  127. 0: divide the speed
  128. 1: pulse at normal speed
  129. 2: multiply the speed
  130. the argument only has an effect for operations 0 and 2, and ranges between
  131. 1 (least effect) to 255 (maximum effect).
  132. thus, several states are equivalent and are coalesced into one state.
  133. we map this onto a range from 0 to 510, with:
  134. 0 -- 254 -- use divide (0 = slowest)
  135. 255 -- use normal speed
  136. 256 -- 510 -- use multiple (510 = fastest).
  137. Only values of 'arg' quite close to 255 are particularly useful/spectacular.
  138. */
  139. if (pm->pulse_speed < 255) {
  140. op = 0; // divide
  141. arg = 255 - pm->pulse_speed;
  142. } else if (pm->pulse_speed > 255) {
  143. op = 2; // multiply
  144. arg = pm->pulse_speed - 255;
  145. } else {
  146. op = 1; // normal speed
  147. arg = 0; // can be any value
  148. }
  149. pm->configcr->wValue = cpu_to_le16( (pm->pulse_table << 8) | SET_PULSE_MODE );
  150. pm->configcr->wIndex = cpu_to_le16( (arg << 8) | op );
  151. pm->requires_update &= ~UPDATE_PULSE_MODE;
  152. } else if (pm->requires_update & UPDATE_STATIC_BRIGHTNESS) {
  153. pm->configcr->wValue = cpu_to_le16( SET_STATIC_BRIGHTNESS );
  154. pm->configcr->wIndex = cpu_to_le16( pm->static_brightness );
  155. pm->requires_update &= ~UPDATE_STATIC_BRIGHTNESS;
  156. } else {
  157. printk(KERN_ERR "powermate: unknown update required");
  158. pm->requires_update = 0; /* fudge the bug */
  159. return;
  160. }
  161. /* printk("powermate: %04x %04x\n", pm->configcr->wValue, pm->configcr->wIndex); */
  162. pm->configcr->bRequestType = 0x41; /* vendor request */
  163. pm->configcr->bRequest = 0x01;
  164. pm->configcr->wLength = 0;
  165. usb_fill_control_urb(pm->config, pm->udev, usb_sndctrlpipe(pm->udev, 0),
  166. (void *) pm->configcr, NULL, 0,
  167. powermate_config_complete, pm);
  168. if (usb_submit_urb(pm->config, GFP_ATOMIC))
  169. printk(KERN_ERR "powermate: usb_submit_urb(config) failed");
  170. }
  171. /* Called when our asynchronous control message completes. We may need to issue another immediately */
  172. static void powermate_config_complete(struct urb *urb)
  173. {
  174. struct powermate_device *pm = urb->context;
  175. unsigned long flags;
  176. if (urb->status)
  177. printk(KERN_ERR "powermate: config urb returned %d\n", urb->status);
  178. spin_lock_irqsave(&pm->lock, flags);
  179. powermate_sync_state(pm);
  180. spin_unlock_irqrestore(&pm->lock, flags);
  181. }
  182. /* Set the LED up as described and begin the sync with the hardware if required */
  183. static void powermate_pulse_led(struct powermate_device *pm, int static_brightness, int pulse_speed,
  184. int pulse_table, int pulse_asleep, int pulse_awake)
  185. {
  186. unsigned long flags;
  187. if (pulse_speed < 0)
  188. pulse_speed = 0;
  189. if (pulse_table < 0)
  190. pulse_table = 0;
  191. if (pulse_speed > 510)
  192. pulse_speed = 510;
  193. if (pulse_table > 2)
  194. pulse_table = 2;
  195. pulse_asleep = !!pulse_asleep;
  196. pulse_awake = !!pulse_awake;
  197. spin_lock_irqsave(&pm->lock, flags);
  198. /* mark state updates which are required */
  199. if (static_brightness != pm->static_brightness) {
  200. pm->static_brightness = static_brightness;
  201. pm->requires_update |= UPDATE_STATIC_BRIGHTNESS;
  202. }
  203. if (pulse_asleep != pm->pulse_asleep) {
  204. pm->pulse_asleep = pulse_asleep;
  205. pm->requires_update |= (UPDATE_PULSE_ASLEEP | UPDATE_STATIC_BRIGHTNESS);
  206. }
  207. if (pulse_awake != pm->pulse_awake) {
  208. pm->pulse_awake = pulse_awake;
  209. pm->requires_update |= (UPDATE_PULSE_AWAKE | UPDATE_STATIC_BRIGHTNESS);
  210. }
  211. if (pulse_speed != pm->pulse_speed || pulse_table != pm->pulse_table) {
  212. pm->pulse_speed = pulse_speed;
  213. pm->pulse_table = pulse_table;
  214. pm->requires_update |= UPDATE_PULSE_MODE;
  215. }
  216. powermate_sync_state(pm);
  217. spin_unlock_irqrestore(&pm->lock, flags);
  218. }
  219. /* Callback from the Input layer when an event arrives from userspace to configure the LED */
  220. static int powermate_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int _value)
  221. {
  222. unsigned int command = (unsigned int)_value;
  223. struct powermate_device *pm = input_get_drvdata(dev);
  224. if (type == EV_MSC && code == MSC_PULSELED){
  225. /*
  226. bits 0- 7: 8 bits: LED brightness
  227. bits 8-16: 9 bits: pulsing speed modifier (0 ... 510); 0-254 = slower, 255 = standard, 256-510 = faster.
  228. bits 17-18: 2 bits: pulse table (0, 1, 2 valid)
  229. bit 19: 1 bit : pulse whilst asleep?
  230. bit 20: 1 bit : pulse constantly?
  231. */
  232. int static_brightness = command & 0xFF; // bits 0-7
  233. int pulse_speed = (command >> 8) & 0x1FF; // bits 8-16
  234. int pulse_table = (command >> 17) & 0x3; // bits 17-18
  235. int pulse_asleep = (command >> 19) & 0x1; // bit 19
  236. int pulse_awake = (command >> 20) & 0x1; // bit 20
  237. powermate_pulse_led(pm, static_brightness, pulse_speed, pulse_table, pulse_asleep, pulse_awake);
  238. }
  239. return 0;
  240. }
  241. static int powermate_alloc_buffers(struct usb_device *udev, struct powermate_device *pm)
  242. {
  243. pm->data = usb_alloc_coherent(udev, POWERMATE_PAYLOAD_SIZE_MAX,
  244. GFP_KERNEL, &pm->data_dma);
  245. if (!pm->data)
  246. return -1;
  247. pm->configcr = kmalloc(sizeof(*(pm->configcr)), GFP_KERNEL);
  248. if (!pm->configcr)
  249. return -ENOMEM;
  250. return 0;
  251. }
  252. static void powermate_free_buffers(struct usb_device *udev, struct powermate_device *pm)
  253. {
  254. usb_free_coherent(udev, POWERMATE_PAYLOAD_SIZE_MAX,
  255. pm->data, pm->data_dma);
  256. kfree(pm->configcr);
  257. }
  258. /* Called whenever a USB device matching one in our supported devices table is connected */
  259. static int powermate_probe(struct usb_interface *intf, const struct usb_device_id *id)
  260. {
  261. struct usb_device *udev = interface_to_usbdev (intf);
  262. struct usb_host_interface *interface;
  263. struct usb_endpoint_descriptor *endpoint;
  264. struct powermate_device *pm;
  265. struct input_dev *input_dev;
  266. int pipe, maxp;
  267. int error = -ENOMEM;
  268. interface = intf->cur_altsetting;
  269. if (interface->desc.bNumEndpoints < 1)
  270. return -EINVAL;
  271. endpoint = &interface->endpoint[0].desc;
  272. if (!usb_endpoint_is_int_in(endpoint))
  273. return -EIO;
  274. usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  275. 0x0a, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  276. 0, interface->desc.bInterfaceNumber, NULL, 0,
  277. USB_CTRL_SET_TIMEOUT);
  278. pm = kzalloc(sizeof(struct powermate_device), GFP_KERNEL);
  279. input_dev = input_allocate_device();
  280. if (!pm || !input_dev)
  281. goto fail1;
  282. if (powermate_alloc_buffers(udev, pm))
  283. goto fail2;
  284. pm->irq = usb_alloc_urb(0, GFP_KERNEL);
  285. if (!pm->irq)
  286. goto fail2;
  287. pm->config = usb_alloc_urb(0, GFP_KERNEL);
  288. if (!pm->config)
  289. goto fail3;
  290. pm->udev = udev;
  291. pm->intf = intf;
  292. pm->input = input_dev;
  293. usb_make_path(udev, pm->phys, sizeof(pm->phys));
  294. strlcat(pm->phys, "/input0", sizeof(pm->phys));
  295. spin_lock_init(&pm->lock);
  296. switch (le16_to_cpu(udev->descriptor.idProduct)) {
  297. case POWERMATE_PRODUCT_NEW:
  298. input_dev->name = pm_name_powermate;
  299. break;
  300. case POWERMATE_PRODUCT_OLD:
  301. input_dev->name = pm_name_soundknob;
  302. break;
  303. default:
  304. input_dev->name = pm_name_soundknob;
  305. printk(KERN_WARNING "powermate: unknown product id %04x\n",
  306. le16_to_cpu(udev->descriptor.idProduct));
  307. }
  308. input_dev->phys = pm->phys;
  309. usb_to_input_id(udev, &input_dev->id);
  310. input_dev->dev.parent = &intf->dev;
  311. input_set_drvdata(input_dev, pm);
  312. input_dev->event = powermate_input_event;
  313. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) |
  314. BIT_MASK(EV_MSC);
  315. input_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
  316. input_dev->relbit[BIT_WORD(REL_DIAL)] = BIT_MASK(REL_DIAL);
  317. input_dev->mscbit[BIT_WORD(MSC_PULSELED)] = BIT_MASK(MSC_PULSELED);
  318. /* get a handle to the interrupt data pipe */
  319. pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
  320. maxp = usb_maxpacket(udev, pipe);
  321. if (maxp < POWERMATE_PAYLOAD_SIZE_MIN || maxp > POWERMATE_PAYLOAD_SIZE_MAX) {
  322. printk(KERN_WARNING "powermate: Expected payload of %d--%d bytes, found %d bytes!\n",
  323. POWERMATE_PAYLOAD_SIZE_MIN, POWERMATE_PAYLOAD_SIZE_MAX, maxp);
  324. maxp = POWERMATE_PAYLOAD_SIZE_MAX;
  325. }
  326. usb_fill_int_urb(pm->irq, udev, pipe, pm->data,
  327. maxp, powermate_irq,
  328. pm, endpoint->bInterval);
  329. pm->irq->transfer_dma = pm->data_dma;
  330. pm->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  331. /* register our interrupt URB with the USB system */
  332. if (usb_submit_urb(pm->irq, GFP_KERNEL)) {
  333. error = -EIO;
  334. goto fail4;
  335. }
  336. error = input_register_device(pm->input);
  337. if (error)
  338. goto fail5;
  339. /* force an update of everything */
  340. pm->requires_update = UPDATE_PULSE_ASLEEP | UPDATE_PULSE_AWAKE | UPDATE_PULSE_MODE | UPDATE_STATIC_BRIGHTNESS;
  341. powermate_pulse_led(pm, 0x80, 255, 0, 1, 0); // set default pulse parameters
  342. usb_set_intfdata(intf, pm);
  343. return 0;
  344. fail5: usb_kill_urb(pm->irq);
  345. fail4: usb_free_urb(pm->config);
  346. fail3: usb_free_urb(pm->irq);
  347. fail2: powermate_free_buffers(udev, pm);
  348. fail1: input_free_device(input_dev);
  349. kfree(pm);
  350. return error;
  351. }
  352. /* Called when a USB device we've accepted ownership of is removed */
  353. static void powermate_disconnect(struct usb_interface *intf)
  354. {
  355. struct powermate_device *pm = usb_get_intfdata (intf);
  356. usb_set_intfdata(intf, NULL);
  357. if (pm) {
  358. pm->requires_update = 0;
  359. usb_kill_urb(pm->irq);
  360. input_unregister_device(pm->input);
  361. usb_kill_urb(pm->config);
  362. usb_free_urb(pm->irq);
  363. usb_free_urb(pm->config);
  364. powermate_free_buffers(interface_to_usbdev(intf), pm);
  365. kfree(pm);
  366. }
  367. }
  368. static const struct usb_device_id powermate_devices[] = {
  369. { USB_DEVICE(POWERMATE_VENDOR, POWERMATE_PRODUCT_NEW) },
  370. { USB_DEVICE(POWERMATE_VENDOR, POWERMATE_PRODUCT_OLD) },
  371. { USB_DEVICE(CONTOUR_VENDOR, CONTOUR_JOG) },
  372. { } /* Terminating entry */
  373. };
  374. MODULE_DEVICE_TABLE (usb, powermate_devices);
  375. static struct usb_driver powermate_driver = {
  376. .name = "powermate",
  377. .probe = powermate_probe,
  378. .disconnect = powermate_disconnect,
  379. .id_table = powermate_devices,
  380. };
  381. module_usb_driver(powermate_driver);
  382. MODULE_AUTHOR( "William R Sowerbutts" );
  383. MODULE_DESCRIPTION( "Griffin Technology, Inc PowerMate driver" );
  384. MODULE_LICENSE("GPL");