appledisplay.c 8.8 KB

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