appledisplay.c 10 KB

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