appledisplay.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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(0x9222) },
  45. { APPLEDISPLAY_DEVICE(0x9226) },
  46. { APPLEDISPLAY_DEVICE(0x9236) },
  47. /* Terminating entry */
  48. { }
  49. };
  50. MODULE_DEVICE_TABLE(usb, appledisplay_table);
  51. /* Structure to hold all of our device specific stuff */
  52. struct appledisplay {
  53. struct usb_device *udev; /* usb device */
  54. struct urb *urb; /* usb request block */
  55. struct backlight_device *bd; /* backlight device */
  56. u8 *urbdata; /* interrupt URB data buffer */
  57. u8 *msgdata; /* control message data buffer */
  58. struct delayed_work work;
  59. int button_pressed;
  60. struct mutex sysfslock; /* concurrent read and write */
  61. };
  62. static atomic_t count_displays = ATOMIC_INIT(0);
  63. static void appledisplay_complete(struct urb *urb)
  64. {
  65. struct appledisplay *pdata = urb->context;
  66. struct device *dev = &pdata->udev->dev;
  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. fallthrough;
  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. switch(pdata->urbdata[1]) {
  91. case ACD_BTN_BRIGHT_UP:
  92. case ACD_BTN_BRIGHT_DOWN:
  93. pdata->button_pressed = 1;
  94. /*
  95. * there is a window during which no device
  96. * is registered
  97. */
  98. if (pdata->bd )
  99. schedule_delayed_work(&pdata->work, 0);
  100. break;
  101. case ACD_BTN_NONE:
  102. default:
  103. pdata->button_pressed = 0;
  104. break;
  105. }
  106. exit:
  107. retval = usb_submit_urb(pdata->urb, GFP_ATOMIC);
  108. if (retval) {
  109. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  110. __func__, retval);
  111. }
  112. }
  113. static int appledisplay_bl_update_status(struct backlight_device *bd)
  114. {
  115. struct appledisplay *pdata = bl_get_data(bd);
  116. int retval;
  117. mutex_lock(&pdata->sysfslock);
  118. pdata->msgdata[0] = 0x10;
  119. pdata->msgdata[1] = bd->props.brightness;
  120. retval = usb_control_msg(
  121. pdata->udev,
  122. usb_sndctrlpipe(pdata->udev, 0),
  123. USB_REQ_SET_REPORT,
  124. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  125. ACD_USB_BRIGHTNESS,
  126. 0,
  127. pdata->msgdata, 2,
  128. ACD_USB_TIMEOUT);
  129. mutex_unlock(&pdata->sysfslock);
  130. if (retval < 0)
  131. return retval;
  132. else
  133. return 0;
  134. }
  135. static int appledisplay_bl_get_brightness(struct backlight_device *bd)
  136. {
  137. struct appledisplay *pdata = bl_get_data(bd);
  138. int retval, brightness;
  139. mutex_lock(&pdata->sysfslock);
  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 < 2) {
  150. if (retval >= 0)
  151. retval = -EMSGSIZE;
  152. } else {
  153. brightness = pdata->msgdata[1];
  154. }
  155. mutex_unlock(&pdata->sysfslock);
  156. if (retval < 0)
  157. return retval;
  158. else
  159. return brightness;
  160. }
  161. static const struct backlight_ops appledisplay_bl_data = {
  162. .get_brightness = appledisplay_bl_get_brightness,
  163. .update_status = appledisplay_bl_update_status,
  164. };
  165. static void appledisplay_work(struct work_struct *work)
  166. {
  167. struct appledisplay *pdata =
  168. container_of(work, struct appledisplay, work.work);
  169. int retval;
  170. retval = appledisplay_bl_get_brightness(pdata->bd);
  171. if (retval >= 0)
  172. pdata->bd->props.brightness = retval;
  173. /* Poll again in about 125ms if there's still a button pressed */
  174. if (pdata->button_pressed)
  175. schedule_delayed_work(&pdata->work, HZ / 8);
  176. }
  177. static int appledisplay_probe(struct usb_interface *iface,
  178. const struct usb_device_id *id)
  179. {
  180. struct backlight_properties props;
  181. struct backlight_device *backlight;
  182. struct appledisplay *pdata;
  183. struct usb_device *udev = interface_to_usbdev(iface);
  184. struct usb_endpoint_descriptor *endpoint;
  185. int int_in_endpointAddr = 0;
  186. int retval, brightness;
  187. char bl_name[20];
  188. /* set up the endpoint information */
  189. /* use only the first interrupt-in endpoint */
  190. retval = usb_find_int_in_endpoint(iface->cur_altsetting, &endpoint);
  191. if (retval) {
  192. dev_err(&iface->dev, "Could not find int-in endpoint\n");
  193. return retval;
  194. }
  195. int_in_endpointAddr = endpoint->bEndpointAddress;
  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. goto error;
  201. }
  202. pdata->udev = udev;
  203. INIT_DELAYED_WORK(&pdata->work, appledisplay_work);
  204. mutex_init(&pdata->sysfslock);
  205. /* Allocate buffer for control messages */
  206. pdata->msgdata = kmalloc(ACD_MSG_BUFFER_LEN, GFP_KERNEL);
  207. if (!pdata->msgdata) {
  208. retval = -ENOMEM;
  209. goto error;
  210. }
  211. /* Allocate interrupt URB */
  212. pdata->urb = usb_alloc_urb(0, GFP_KERNEL);
  213. if (!pdata->urb) {
  214. retval = -ENOMEM;
  215. goto error;
  216. }
  217. /* Allocate buffer for interrupt data */
  218. pdata->urbdata = usb_alloc_coherent(pdata->udev, ACD_URB_BUFFER_LEN,
  219. GFP_KERNEL, &pdata->urb->transfer_dma);
  220. if (!pdata->urbdata) {
  221. retval = -ENOMEM;
  222. dev_err(&iface->dev, "Allocating URB buffer failed\n");
  223. goto error;
  224. }
  225. /* Configure interrupt URB */
  226. usb_fill_int_urb(pdata->urb, udev,
  227. usb_rcvintpipe(udev, int_in_endpointAddr),
  228. pdata->urbdata, ACD_URB_BUFFER_LEN, appledisplay_complete,
  229. pdata, 1);
  230. pdata->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  231. if (usb_submit_urb(pdata->urb, GFP_KERNEL)) {
  232. retval = -EIO;
  233. dev_err(&iface->dev, "Submitting URB failed\n");
  234. goto error;
  235. }
  236. /* Register backlight device */
  237. snprintf(bl_name, sizeof(bl_name), "appledisplay%d",
  238. atomic_inc_return(&count_displays) - 1);
  239. memset(&props, 0, sizeof(struct backlight_properties));
  240. props.type = BACKLIGHT_RAW;
  241. props.max_brightness = 0xff;
  242. backlight = backlight_device_register(bl_name, NULL, pdata,
  243. &appledisplay_bl_data, &props);
  244. if (IS_ERR(backlight)) {
  245. dev_err(&iface->dev, "Backlight registration failed\n");
  246. retval = PTR_ERR(backlight);
  247. goto error;
  248. }
  249. pdata->bd = backlight;
  250. /* Try to get brightness */
  251. brightness = appledisplay_bl_get_brightness(pdata->bd);
  252. if (brightness < 0) {
  253. retval = brightness;
  254. dev_err(&iface->dev,
  255. "Error while getting initial brightness: %d\n", retval);
  256. goto error;
  257. }
  258. /* Set brightness in backlight device */
  259. pdata->bd->props.brightness = brightness;
  260. /* save our data pointer in the interface device */
  261. usb_set_intfdata(iface, pdata);
  262. printk(KERN_INFO "appledisplay: Apple Cinema Display connected\n");
  263. return 0;
  264. error:
  265. if (pdata) {
  266. if (pdata->urb) {
  267. usb_kill_urb(pdata->urb);
  268. cancel_delayed_work_sync(&pdata->work);
  269. usb_free_coherent(pdata->udev, ACD_URB_BUFFER_LEN,
  270. pdata->urbdata, pdata->urb->transfer_dma);
  271. usb_free_urb(pdata->urb);
  272. }
  273. if (!IS_ERR(pdata->bd))
  274. backlight_device_unregister(pdata->bd);
  275. kfree(pdata->msgdata);
  276. }
  277. usb_set_intfdata(iface, NULL);
  278. kfree(pdata);
  279. return retval;
  280. }
  281. static void appledisplay_disconnect(struct usb_interface *iface)
  282. {
  283. struct appledisplay *pdata = usb_get_intfdata(iface);
  284. if (pdata) {
  285. usb_kill_urb(pdata->urb);
  286. cancel_delayed_work_sync(&pdata->work);
  287. backlight_device_unregister(pdata->bd);
  288. usb_free_coherent(pdata->udev, ACD_URB_BUFFER_LEN,
  289. pdata->urbdata, pdata->urb->transfer_dma);
  290. usb_free_urb(pdata->urb);
  291. kfree(pdata->msgdata);
  292. kfree(pdata);
  293. }
  294. printk(KERN_INFO "appledisplay: Apple Cinema Display disconnected\n");
  295. }
  296. static struct usb_driver appledisplay_driver = {
  297. .name = "appledisplay",
  298. .probe = appledisplay_probe,
  299. .disconnect = appledisplay_disconnect,
  300. .id_table = appledisplay_table,
  301. };
  302. module_usb_driver(appledisplay_driver);
  303. MODULE_AUTHOR("Michael Hanselmann");
  304. MODULE_DESCRIPTION("Apple Cinema Display driver");
  305. MODULE_LICENSE("GPL");