/kern_oII/drivers/input/misc/powermate.c

http://omnia2droid.googlecode.com/ · C · 464 lines · 320 code · 71 blank · 73 comment · 50 complexity · 5606877170bd624f2d33118008a72527 MD5 · raw file

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