appledisplay.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/usb.h>
  27. #include <linux/backlight.h>
  28. #include <linux/timer.h>
  29. #include <linux/workqueue.h>
  30. #include <asm/atomic.h>
  31. #define APPLE_VENDOR_ID 0x05AC
  32. #define USB_REQ_GET_REPORT 0x01
  33. #define USB_REQ_SET_REPORT 0x09
  34. #define ACD_USB_TIMEOUT 250
  35. #define ACD_USB_EDID 0x0302
  36. #define ACD_USB_BRIGHTNESS 0x0310
  37. #define ACD_BTN_NONE 0
  38. #define ACD_BTN_BRIGHT_UP 3
  39. #define ACD_BTN_BRIGHT_DOWN 4
  40. #define ACD_URB_BUFFER_LEN 2
  41. #define ACD_MSG_BUFFER_LEN 2
  42. #define APPLEDISPLAY_DEVICE(prod) \
  43. .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
  44. USB_DEVICE_ID_MATCH_INT_CLASS | \
  45. USB_DEVICE_ID_MATCH_INT_PROTOCOL, \
  46. .idVendor = APPLE_VENDOR_ID, \
  47. .idProduct = (prod), \
  48. .bInterfaceClass = USB_CLASS_HID, \
  49. .bInterfaceProtocol = 0x00
  50. /* table of devices that work with this driver */
  51. static struct usb_device_id appledisplay_table [] = {
  52. { APPLEDISPLAY_DEVICE(0x9218) },
  53. { APPLEDISPLAY_DEVICE(0x9219) },
  54. { APPLEDISPLAY_DEVICE(0x921c) },
  55. { APPLEDISPLAY_DEVICE(0x921d) },
  56. /* Terminating entry */
  57. { }
  58. };
  59. MODULE_DEVICE_TABLE(usb, appledisplay_table);
  60. /* Structure to hold all of our device specific stuff */
  61. struct appledisplay {
  62. struct usb_device *udev; /* usb device */
  63. struct urb *urb; /* usb request block */
  64. struct backlight_device *bd; /* backlight device */
  65. u8 *urbdata; /* interrupt URB data buffer */
  66. u8 *msgdata; /* control message data buffer */
  67. struct delayed_work work;
  68. int button_pressed;
  69. spinlock_t lock;
  70. };
  71. static atomic_t count_displays = ATOMIC_INIT(0);
  72. static struct workqueue_struct *wq;
  73. static void appledisplay_complete(struct urb *urb)
  74. {
  75. struct appledisplay *pdata = urb->context;
  76. unsigned long flags;
  77. int status = urb->status;
  78. int retval;
  79. switch (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 shuttingdown with status: %d",
  92. __func__, status);
  93. return;
  94. default:
  95. dbg("%s - nonzero urb status received: %d",
  96. __func__, 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_delayed_work(wq, &pdata->work, 0);
  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. dev_err(&pdata->udev->dev,
  116. "%s - usb_submit_urb failed with result %d\n",
  117. __func__, retval);
  118. }
  119. }
  120. static int appledisplay_bl_update_status(struct backlight_device *bd)
  121. {
  122. struct appledisplay *pdata = bl_get_data(bd);
  123. int retval;
  124. pdata->msgdata[0] = 0x10;
  125. pdata->msgdata[1] = bd->props.brightness;
  126. retval = usb_control_msg(
  127. pdata->udev,
  128. usb_sndctrlpipe(pdata->udev, 0),
  129. USB_REQ_SET_REPORT,
  130. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  131. ACD_USB_BRIGHTNESS,
  132. 0,
  133. pdata->msgdata, 2,
  134. ACD_USB_TIMEOUT);
  135. return retval;
  136. }
  137. static int appledisplay_bl_get_brightness(struct backlight_device *bd)
  138. {
  139. struct appledisplay *pdata = bl_get_data(bd);
  140. int retval;
  141. retval = usb_control_msg(
  142. pdata->udev,
  143. usb_rcvctrlpipe(pdata->udev, 0),
  144. USB_REQ_GET_REPORT,
  145. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  146. ACD_USB_BRIGHTNESS,
  147. 0,
  148. pdata->msgdata, 2,
  149. ACD_USB_TIMEOUT);
  150. if (retval < 0)
  151. return retval;
  152. else
  153. return pdata->msgdata[1];
  154. }
  155. static struct backlight_ops appledisplay_bl_data = {
  156. .get_brightness = appledisplay_bl_get_brightness,
  157. .update_status = appledisplay_bl_update_status,
  158. };
  159. static void appledisplay_work(struct work_struct *work)
  160. {
  161. struct appledisplay *pdata =
  162. container_of(work, struct appledisplay, work.work);
  163. int retval;
  164. retval = appledisplay_bl_get_brightness(pdata->bd);
  165. if (retval >= 0)
  166. pdata->bd->props.brightness = retval;
  167. /* Poll again in about 125ms if there's still a button pressed */
  168. if (pdata->button_pressed)
  169. schedule_delayed_work(&pdata->work, HZ / 8);
  170. }
  171. static int appledisplay_probe(struct usb_interface *iface,
  172. const struct usb_device_id *id)
  173. {
  174. struct appledisplay *pdata;
  175. struct usb_device *udev = interface_to_usbdev(iface);
  176. struct usb_host_interface *iface_desc;
  177. struct usb_endpoint_descriptor *endpoint;
  178. int int_in_endpointAddr = 0;
  179. int i, retval = -ENOMEM, brightness;
  180. char bl_name[20];
  181. /* set up the endpoint information */
  182. /* use only the first interrupt-in endpoint */
  183. iface_desc = iface->cur_altsetting;
  184. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  185. endpoint = &iface_desc->endpoint[i].desc;
  186. if (!int_in_endpointAddr && usb_endpoint_is_int_in(endpoint)) {
  187. /* we found an interrupt in endpoint */
  188. int_in_endpointAddr = endpoint->bEndpointAddress;
  189. break;
  190. }
  191. }
  192. if (!int_in_endpointAddr) {
  193. dev_err(&iface->dev, "Could not find int-in endpoint\n");
  194. return -EIO;
  195. }
  196. /* allocate memory for our device state and initialize it */
  197. pdata = kzalloc(sizeof(struct appledisplay), GFP_KERNEL);
  198. if (!pdata) {
  199. retval = -ENOMEM;
  200. dev_err(&iface->dev, "Out of memory\n");
  201. goto error;
  202. }
  203. pdata->udev = udev;
  204. spin_lock_init(&pdata->lock);
  205. INIT_DELAYED_WORK(&pdata->work, appledisplay_work);
  206. /* Allocate buffer for control messages */
  207. pdata->msgdata = kmalloc(ACD_MSG_BUFFER_LEN, GFP_KERNEL);
  208. if (!pdata->msgdata) {
  209. retval = -ENOMEM;
  210. dev_err(&iface->dev,
  211. "Allocating buffer for control messages failed\n");
  212. goto error;
  213. }
  214. /* Allocate interrupt URB */
  215. pdata->urb = usb_alloc_urb(0, GFP_KERNEL);
  216. if (!pdata->urb) {
  217. retval = -ENOMEM;
  218. dev_err(&iface->dev, "Allocating URB failed\n");
  219. goto error;
  220. }
  221. /* Allocate buffer for interrupt data */
  222. pdata->urbdata = usb_buffer_alloc(pdata->udev, ACD_URB_BUFFER_LEN,
  223. GFP_KERNEL, &pdata->urb->transfer_dma);
  224. if (!pdata->urbdata) {
  225. retval = -ENOMEM;
  226. dev_err(&iface->dev, "Allocating URB buffer failed\n");
  227. goto error;
  228. }
  229. /* Configure interrupt URB */
  230. usb_fill_int_urb(pdata->urb, udev,
  231. usb_rcvintpipe(udev, int_in_endpointAddr),
  232. pdata->urbdata, ACD_URB_BUFFER_LEN, appledisplay_complete,
  233. pdata, 1);
  234. if (usb_submit_urb(pdata->urb, GFP_KERNEL)) {
  235. retval = -EIO;
  236. dev_err(&iface->dev, "Submitting URB failed\n");
  237. goto error;
  238. }
  239. /* Register backlight device */
  240. snprintf(bl_name, sizeof(bl_name), "appledisplay%d",
  241. atomic_inc_return(&count_displays) - 1);
  242. pdata->bd = backlight_device_register(bl_name, NULL, pdata,
  243. &appledisplay_bl_data);
  244. if (IS_ERR(pdata->bd)) {
  245. dev_err(&iface->dev, "Backlight registration failed\n");
  246. goto error;
  247. }
  248. pdata->bd->props.max_brightness = 0xff;
  249. /* Try to get brightness */
  250. brightness = appledisplay_bl_get_brightness(pdata->bd);
  251. if (brightness < 0) {
  252. retval = brightness;
  253. dev_err(&iface->dev,
  254. "Error while getting initial brightness: %d\n", retval);
  255. goto error;
  256. }
  257. /* Set brightness in backlight device */
  258. pdata->bd->props.brightness = brightness;
  259. /* save our data pointer in the interface device */
  260. usb_set_intfdata(iface, pdata);
  261. printk(KERN_INFO "appledisplay: Apple Cinema Display connected\n");
  262. return 0;
  263. error:
  264. if (pdata) {
  265. if (pdata->urb) {
  266. usb_kill_urb(pdata->urb);
  267. if (pdata->urbdata)
  268. usb_buffer_free(pdata->udev, ACD_URB_BUFFER_LEN,
  269. pdata->urbdata, pdata->urb->transfer_dma);
  270. usb_free_urb(pdata->urb);
  271. }
  272. if (pdata->bd && !IS_ERR(pdata->bd))
  273. backlight_device_unregister(pdata->bd);
  274. kfree(pdata->msgdata);
  275. }
  276. usb_set_intfdata(iface, NULL);
  277. kfree(pdata);
  278. return retval;
  279. }
  280. static void appledisplay_disconnect(struct usb_interface *iface)
  281. {
  282. struct appledisplay *pdata = usb_get_intfdata(iface);
  283. if (pdata) {
  284. usb_kill_urb(pdata->urb);
  285. cancel_delayed_work(&pdata->work);
  286. backlight_device_unregister(pdata->bd);
  287. usb_buffer_free(pdata->udev, ACD_URB_BUFFER_LEN,
  288. pdata->urbdata, pdata->urb->transfer_dma);
  289. usb_free_urb(pdata->urb);
  290. kfree(pdata->msgdata);
  291. kfree(pdata);
  292. }
  293. printk(KERN_INFO "appledisplay: Apple Cinema Display disconnected\n");
  294. }
  295. static struct usb_driver appledisplay_driver = {
  296. .name = "appledisplay",
  297. .probe = appledisplay_probe,
  298. .disconnect = appledisplay_disconnect,
  299. .id_table = appledisplay_table,
  300. };
  301. static int __init appledisplay_init(void)
  302. {
  303. wq = create_singlethread_workqueue("appledisplay");
  304. if (!wq) {
  305. printk(KERN_ERR "appledisplay: Could not create work queue\n");
  306. return -ENOMEM;
  307. }
  308. return usb_register(&appledisplay_driver);
  309. }
  310. static void __exit appledisplay_exit(void)
  311. {
  312. flush_workqueue(wq);
  313. destroy_workqueue(wq);
  314. usb_deregister(&appledisplay_driver);
  315. }
  316. MODULE_AUTHOR("Michael Hanselmann");
  317. MODULE_DESCRIPTION("Apple Cinema Display driver");
  318. MODULE_LICENSE("GPL");
  319. module_init(appledisplay_init);
  320. module_exit(appledisplay_exit);