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