appledisplay.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Apple Cinema Display driver
  3. *
  4. * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
  5. *
  6. * Thanks to Caskey L. Dickson for his work with acdctl.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. */
  22. #include <linux/config.h>
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/usb.h>
  28. #include <linux/backlight.h>
  29. #include <linux/timer.h>
  30. #include <linux/workqueue.h>
  31. #include <asm/atomic.h>
  32. #include <asm/semaphore.h>
  33. #define APPLE_VENDOR_ID 0x05AC
  34. #define USB_REQ_GET_REPORT 0x01
  35. #define USB_REQ_SET_REPORT 0x09
  36. #define ACD_USB_TIMEOUT 250
  37. #define ACD_USB_EDID 0x0302
  38. #define ACD_USB_BRIGHTNESS 0x0310
  39. #define ACD_BTN_NONE 0
  40. #define ACD_BTN_BRIGHT_UP 3
  41. #define ACD_BTN_BRIGHT_DOWN 4
  42. #define ACD_URB_BUFFER_LEN 2
  43. #define ACD_MSG_BUFFER_LEN 2
  44. #define APPLEDISPLAY_DEVICE(prod) \
  45. .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
  46. USB_DEVICE_ID_MATCH_INT_CLASS | \
  47. USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
  48. .idVendor = APPLE_VENDOR_ID, \
  49. .idProduct = (prod), \
  50. .bInterfaceClass = USB_CLASS_HID, \
  51. .bInterfaceProtocol = 0x00
  52. /* table of devices that work with this driver */
  53. static struct usb_device_id appledisplay_table [] = {
  54. { APPLEDISPLAY_DEVICE(0x9218) },
  55. { APPLEDISPLAY_DEVICE(0x9219) },
  56. { APPLEDISPLAY_DEVICE(0x921d) },
  57. /* Terminating entry */
  58. { }
  59. };
  60. MODULE_DEVICE_TABLE(usb, appledisplay_table);
  61. /* Structure to hold all of our device specific stuff */
  62. struct appledisplay {
  63. struct usb_device *udev; /* usb device */
  64. struct urb *urb; /* usb request block */
  65. struct backlight_device *bd; /* backlight device */
  66. char *urbdata; /* interrupt URB data buffer */
  67. char *msgdata; /* control message data buffer */
  68. struct work_struct work;
  69. int button_pressed;
  70. spinlock_t lock;
  71. };
  72. static atomic_t count_displays = ATOMIC_INIT(0);
  73. static struct workqueue_struct *wq;
  74. static void appledisplay_complete(struct urb *urb, struct pt_regs *regs)
  75. {
  76. struct appledisplay *pdata = urb->context;
  77. unsigned long flags;
  78. int retval;
  79. switch (urb->status) {
  80. case 0:
  81. /* success */
  82. break;
  83. case -EOVERFLOW:
  84. printk(KERN_ERR "appletouch: OVERFLOW with data "
  85. "length %d, actual length is %d\n",
  86. ACD_URB_BUFFER_LEN, pdata->urb->actual_length);
  87. case -ECONNRESET:
  88. case -ENOENT:
  89. case -ESHUTDOWN:
  90. /* This urb is terminated, clean up */
  91. dbg("%s - urb shutting down with status: %d",
  92. __FUNCTION__, urb->status);
  93. return;
  94. default:
  95. dbg("%s - nonzero urb status received: %d",
  96. __FUNCTION__, urb->status);
  97. goto exit;
  98. }
  99. spin_lock_irqsave(&pdata->lock, flags);
  100. switch(pdata->urbdata[1]) {
  101. case ACD_BTN_BRIGHT_UP:
  102. case ACD_BTN_BRIGHT_DOWN:
  103. pdata->button_pressed = 1;
  104. queue_work(wq, &pdata->work);
  105. break;
  106. case ACD_BTN_NONE:
  107. default:
  108. pdata->button_pressed = 0;
  109. break;
  110. }
  111. spin_unlock_irqrestore(&pdata->lock, flags);
  112. exit:
  113. retval = usb_submit_urb(pdata->urb, GFP_ATOMIC);
  114. if (retval) {
  115. err("%s - usb_submit_urb failed with result %d",
  116. __FUNCTION__, retval);
  117. }
  118. }
  119. static int appledisplay_bl_update_status(struct backlight_device *bd)
  120. {
  121. struct appledisplay *pdata = class_get_devdata(&bd->class_dev);
  122. int retval;
  123. pdata->msgdata[0] = 0x10;
  124. pdata->msgdata[1] = bd->props->brightness;
  125. retval = usb_control_msg(
  126. pdata->udev,
  127. usb_sndctrlpipe(pdata->udev, 0),
  128. USB_REQ_SET_REPORT,
  129. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  130. ACD_USB_BRIGHTNESS,
  131. 0,
  132. pdata->msgdata, 2,
  133. ACD_USB_TIMEOUT);
  134. return retval;
  135. }
  136. static int appledisplay_bl_get_brightness(struct backlight_device *bd)
  137. {
  138. struct appledisplay *pdata = class_get_devdata(&bd->class_dev);
  139. int retval;
  140. retval = usb_control_msg(
  141. pdata->udev,
  142. usb_rcvctrlpipe(pdata->udev, 0),
  143. USB_REQ_GET_REPORT,
  144. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  145. ACD_USB_BRIGHTNESS,
  146. 0,
  147. pdata->msgdata, 2,
  148. ACD_USB_TIMEOUT);
  149. if (retval < 0)
  150. return retval;
  151. else
  152. return pdata->msgdata[1];
  153. }
  154. static struct backlight_properties appledisplay_bl_data = {
  155. .owner = THIS_MODULE,
  156. .get_brightness = appledisplay_bl_get_brightness,
  157. .update_status = appledisplay_bl_update_status,
  158. .max_brightness = 0xFF
  159. };
  160. static void appledisplay_work(void *private)
  161. {
  162. struct appledisplay *pdata = private;
  163. int retval;
  164. up(&pdata->bd->sem);
  165. retval = appledisplay_bl_get_brightness(pdata->bd);
  166. if (retval >= 0)
  167. pdata->bd->props->brightness = retval;
  168. down(&pdata->bd->sem);
  169. /* Poll again in about 125ms if there's still a button pressed */
  170. if (pdata->button_pressed)
  171. schedule_delayed_work(&pdata->work, HZ / 8);
  172. }
  173. static int appledisplay_probe(struct usb_interface *iface,
  174. const struct usb_device_id *id)
  175. {
  176. struct appledisplay *pdata;
  177. struct usb_device *udev = interface_to_usbdev(iface);
  178. struct usb_host_interface *iface_desc;
  179. struct usb_endpoint_descriptor *endpoint;
  180. int int_in_endpointAddr = 0;
  181. int i, retval = -ENOMEM, brightness;
  182. char bl_name[20];
  183. /* set up the endpoint information */
  184. /* use only the first interrupt-in endpoint */
  185. iface_desc = iface->cur_altsetting;
  186. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  187. endpoint = &iface_desc->endpoint[i].desc;
  188. if (!int_in_endpointAddr &&
  189. (endpoint->bEndpointAddress & USB_DIR_IN) &&
  190. ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  191. USB_ENDPOINT_XFER_INT)) {
  192. /* we found an interrupt in endpoint */
  193. int_in_endpointAddr = endpoint->bEndpointAddress;
  194. break;
  195. }
  196. }
  197. if (!int_in_endpointAddr) {
  198. err("Could not find int-in endpoint");
  199. return -EIO;
  200. }
  201. /* allocate memory for our device state and initialize it */
  202. pdata = kzalloc(sizeof(struct appledisplay), GFP_KERNEL);
  203. if (!pdata) {
  204. retval = -ENOMEM;
  205. err("Out of memory");
  206. goto error;
  207. }
  208. pdata->udev = udev;
  209. spin_lock_init(&pdata->lock);
  210. INIT_WORK(&pdata->work, appledisplay_work, pdata);
  211. /* Allocate buffer for control messages */
  212. pdata->msgdata = kmalloc(ACD_MSG_BUFFER_LEN, GFP_KERNEL);
  213. if (!pdata->msgdata) {
  214. retval = -ENOMEM;
  215. err("appledisplay: Allocating buffer for control messages "
  216. "failed");
  217. goto error;
  218. }
  219. /* Allocate interrupt URB */
  220. pdata->urb = usb_alloc_urb(0, GFP_KERNEL);
  221. if (!pdata->urb) {
  222. retval = -ENOMEM;
  223. err("appledisplay: Allocating URB failed");
  224. goto error;
  225. }
  226. /* Allocate buffer for interrupt data */
  227. pdata->urbdata = usb_buffer_alloc(pdata->udev, ACD_URB_BUFFER_LEN,
  228. GFP_KERNEL, &pdata->urb->transfer_dma);
  229. if (!pdata->urbdata) {
  230. retval = -ENOMEM;
  231. err("appledisplay: Allocating URB buffer failed");
  232. goto error;
  233. }
  234. /* Configure interrupt URB */
  235. usb_fill_int_urb(pdata->urb, udev,
  236. usb_rcvintpipe(udev, int_in_endpointAddr),
  237. pdata->urbdata, ACD_URB_BUFFER_LEN, appledisplay_complete,
  238. pdata, 1);
  239. if (usb_submit_urb(pdata->urb, GFP_KERNEL)) {
  240. retval = -EIO;
  241. err("appledisplay: Submitting URB failed");
  242. goto error;
  243. }
  244. /* Register backlight device */
  245. snprintf(bl_name, sizeof(bl_name), "appledisplay%d",
  246. atomic_inc_return(&count_displays) - 1);
  247. pdata->bd = backlight_device_register(bl_name, pdata,
  248. &appledisplay_bl_data);
  249. if (IS_ERR(pdata->bd)) {
  250. err("appledisplay: Backlight registration failed");
  251. goto error;
  252. }
  253. /* Try to get brightness */
  254. up(&pdata->bd->sem);
  255. brightness = appledisplay_bl_get_brightness(pdata->bd);
  256. down(&pdata->bd->sem);
  257. if (brightness < 0) {
  258. retval = brightness;
  259. err("appledisplay: Error while getting initial brightness: %d", retval);
  260. goto error;
  261. }
  262. /* Set brightness in backlight device */
  263. up(&pdata->bd->sem);
  264. pdata->bd->props->brightness = brightness;
  265. down(&pdata->bd->sem);
  266. /* save our data pointer in the interface device */
  267. usb_set_intfdata(iface, pdata);
  268. printk(KERN_INFO "appledisplay: Apple Cinema Display connected\n");
  269. return 0;
  270. error:
  271. if (pdata) {
  272. if (pdata->urb) {
  273. usb_kill_urb(pdata->urb);
  274. if (pdata->urbdata)
  275. usb_buffer_free(pdata->udev, ACD_URB_BUFFER_LEN,
  276. pdata->urbdata, pdata->urb->transfer_dma);
  277. usb_free_urb(pdata->urb);
  278. }
  279. if (pdata->bd)
  280. backlight_device_unregister(pdata->bd);
  281. kfree(pdata->msgdata);
  282. }
  283. usb_set_intfdata(iface, NULL);
  284. kfree(pdata);
  285. return retval;
  286. }
  287. static void appledisplay_disconnect(struct usb_interface *iface)
  288. {
  289. struct appledisplay *pdata = usb_get_intfdata(iface);
  290. if (pdata) {
  291. usb_kill_urb(pdata->urb);
  292. cancel_delayed_work(&pdata->work);
  293. backlight_device_unregister(pdata->bd);
  294. usb_buffer_free(pdata->udev, ACD_URB_BUFFER_LEN,
  295. pdata->urbdata, pdata->urb->transfer_dma);
  296. usb_free_urb(pdata->urb);
  297. kfree(pdata->msgdata);
  298. kfree(pdata);
  299. }
  300. printk(KERN_INFO "appledisplay: Apple Cinema Display disconnected\n");
  301. }
  302. static struct usb_driver appledisplay_driver = {
  303. .name = "appledisplay",
  304. .probe = appledisplay_probe,
  305. .disconnect = appledisplay_disconnect,
  306. .id_table = appledisplay_table,
  307. };
  308. static int __init appledisplay_init(void)
  309. {
  310. wq = create_singlethread_workqueue("appledisplay");
  311. if (!wq) {
  312. err("Could not create work queue\n");
  313. return -ENOMEM;
  314. }
  315. return usb_register(&appledisplay_driver);
  316. }
  317. static void __exit appledisplay_exit(void)
  318. {
  319. flush_workqueue(wq);
  320. destroy_workqueue(wq);
  321. usb_deregister(&appledisplay_driver);
  322. }
  323. MODULE_AUTHOR("Michael Hanselmann");
  324. MODULE_DESCRIPTION("Apple Cinema Display driver");
  325. MODULE_LICENSE("GPL");
  326. module_init(appledisplay_init);
  327. module_exit(appledisplay_exit);