appledisplay.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Apple Cinema Display driver
  4. *
  5. * Copyright (C) 2006 Michael Hanselmann ([email protected])
  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. schedule_delayed_work(&pdata->work, 0);
  95. break;
  96. case ACD_BTN_NONE:
  97. default:
  98. pdata->button_pressed = 0;
  99. break;
  100. }
  101. exit:
  102. retval = usb_submit_urb(pdata->urb, GFP_ATOMIC);
  103. if (retval) {
  104. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  105. __func__, retval);
  106. }
  107. }
  108. static int appledisplay_bl_update_status(struct backlight_device *bd)
  109. {
  110. struct appledisplay *pdata = bl_get_data(bd);
  111. int retval;
  112. mutex_lock(&pdata->sysfslock);
  113. pdata->msgdata[0] = 0x10;
  114. pdata->msgdata[1] = bd->props.brightness;
  115. retval = usb_control_msg(
  116. pdata->udev,
  117. usb_sndctrlpipe(pdata->udev, 0),
  118. USB_REQ_SET_REPORT,
  119. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  120. ACD_USB_BRIGHTNESS,
  121. 0,
  122. pdata->msgdata, 2,
  123. ACD_USB_TIMEOUT);
  124. mutex_unlock(&pdata->sysfslock);
  125. if (retval < 0)
  126. return retval;
  127. else
  128. return 0;
  129. }
  130. static int appledisplay_bl_get_brightness(struct backlight_device *bd)
  131. {
  132. struct appledisplay *pdata = bl_get_data(bd);
  133. int retval, brightness;
  134. mutex_lock(&pdata->sysfslock);
  135. retval = usb_control_msg(
  136. pdata->udev,
  137. usb_rcvctrlpipe(pdata->udev, 0),
  138. USB_REQ_GET_REPORT,
  139. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  140. ACD_USB_BRIGHTNESS,
  141. 0,
  142. pdata->msgdata, 2,
  143. ACD_USB_TIMEOUT);
  144. if (retval < 2) {
  145. if (retval >= 0)
  146. retval = -EMSGSIZE;
  147. } else {
  148. brightness = pdata->msgdata[1];
  149. }
  150. mutex_unlock(&pdata->sysfslock);
  151. if (retval < 0)
  152. return retval;
  153. else
  154. return brightness;
  155. }
  156. static const struct backlight_ops appledisplay_bl_data = {
  157. .get_brightness = appledisplay_bl_get_brightness,
  158. .update_status = appledisplay_bl_update_status,
  159. };
  160. static void appledisplay_work(struct work_struct *work)
  161. {
  162. struct appledisplay *pdata =
  163. container_of(work, struct appledisplay, work.work);
  164. int retval;
  165. retval = appledisplay_bl_get_brightness(pdata->bd);
  166. if (retval >= 0)
  167. pdata->bd->props.brightness = retval;
  168. /* Poll again in about 125ms if there's still a button pressed */
  169. if (pdata->button_pressed)
  170. schedule_delayed_work(&pdata->work, HZ / 8);
  171. }
  172. static int appledisplay_probe(struct usb_interface *iface,
  173. const struct usb_device_id *id)
  174. {
  175. struct backlight_properties props;
  176. struct appledisplay *pdata;
  177. struct usb_device *udev = interface_to_usbdev(iface);
  178. struct usb_endpoint_descriptor *endpoint;
  179. int int_in_endpointAddr = 0;
  180. int retval, brightness;
  181. char bl_name[20];
  182. /* set up the endpoint information */
  183. /* use only the first interrupt-in endpoint */
  184. retval = usb_find_int_in_endpoint(iface->cur_altsetting, &endpoint);
  185. if (retval) {
  186. dev_err(&iface->dev, "Could not find int-in endpoint\n");
  187. return retval;
  188. }
  189. int_in_endpointAddr = endpoint->bEndpointAddress;
  190. /* allocate memory for our device state and initialize it */
  191. pdata = kzalloc(sizeof(struct appledisplay), GFP_KERNEL);
  192. if (!pdata) {
  193. retval = -ENOMEM;
  194. goto error;
  195. }
  196. pdata->udev = udev;
  197. INIT_DELAYED_WORK(&pdata->work, appledisplay_work);
  198. mutex_init(&pdata->sysfslock);
  199. /* Allocate buffer for control messages */
  200. pdata->msgdata = kmalloc(ACD_MSG_BUFFER_LEN, GFP_KERNEL);
  201. if (!pdata->msgdata) {
  202. retval = -ENOMEM;
  203. goto error;
  204. }
  205. /* Allocate interrupt URB */
  206. pdata->urb = usb_alloc_urb(0, GFP_KERNEL);
  207. if (!pdata->urb) {
  208. retval = -ENOMEM;
  209. goto error;
  210. }
  211. /* Allocate buffer for interrupt data */
  212. pdata->urbdata = usb_alloc_coherent(pdata->udev, ACD_URB_BUFFER_LEN,
  213. GFP_KERNEL, &pdata->urb->transfer_dma);
  214. if (!pdata->urbdata) {
  215. retval = -ENOMEM;
  216. dev_err(&iface->dev, "Allocating URB buffer failed\n");
  217. goto error;
  218. }
  219. /* Configure interrupt URB */
  220. usb_fill_int_urb(pdata->urb, udev,
  221. usb_rcvintpipe(udev, int_in_endpointAddr),
  222. pdata->urbdata, ACD_URB_BUFFER_LEN, appledisplay_complete,
  223. pdata, 1);
  224. pdata->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
  225. if (usb_submit_urb(pdata->urb, GFP_KERNEL)) {
  226. retval = -EIO;
  227. dev_err(&iface->dev, "Submitting URB failed\n");
  228. goto error;
  229. }
  230. /* Register backlight device */
  231. snprintf(bl_name, sizeof(bl_name), "appledisplay%d",
  232. atomic_inc_return(&count_displays) - 1);
  233. memset(&props, 0, sizeof(struct backlight_properties));
  234. props.type = BACKLIGHT_RAW;
  235. props.max_brightness = 0xff;
  236. pdata->bd = backlight_device_register(bl_name, NULL, pdata,
  237. &appledisplay_bl_data, &props);
  238. if (IS_ERR(pdata->bd)) {
  239. dev_err(&iface->dev, "Backlight registration failed\n");
  240. retval = PTR_ERR(pdata->bd);
  241. goto error;
  242. }
  243. /* Try to get brightness */
  244. brightness = appledisplay_bl_get_brightness(pdata->bd);
  245. if (brightness < 0) {
  246. retval = brightness;
  247. dev_err(&iface->dev,
  248. "Error while getting initial brightness: %d\n", retval);
  249. goto error;
  250. }
  251. /* Set brightness in backlight device */
  252. pdata->bd->props.brightness = brightness;
  253. /* save our data pointer in the interface device */
  254. usb_set_intfdata(iface, pdata);
  255. printk(KERN_INFO "appledisplay: Apple Cinema Display connected\n");
  256. return 0;
  257. error:
  258. if (pdata) {
  259. if (pdata->urb) {
  260. usb_kill_urb(pdata->urb);
  261. cancel_delayed_work_sync(&pdata->work);
  262. usb_free_coherent(pdata->udev, ACD_URB_BUFFER_LEN,
  263. pdata->urbdata, pdata->urb->transfer_dma);
  264. usb_free_urb(pdata->urb);
  265. }
  266. if (!IS_ERR(pdata->bd))
  267. backlight_device_unregister(pdata->bd);
  268. kfree(pdata->msgdata);
  269. }
  270. usb_set_intfdata(iface, NULL);
  271. kfree(pdata);
  272. return retval;
  273. }
  274. static void appledisplay_disconnect(struct usb_interface *iface)
  275. {
  276. struct appledisplay *pdata = usb_get_intfdata(iface);
  277. if (pdata) {
  278. usb_kill_urb(pdata->urb);
  279. cancel_delayed_work_sync(&pdata->work);
  280. backlight_device_unregister(pdata->bd);
  281. usb_free_coherent(pdata->udev, ACD_URB_BUFFER_LEN,
  282. pdata->urbdata, pdata->urb->transfer_dma);
  283. usb_free_urb(pdata->urb);
  284. kfree(pdata->msgdata);
  285. kfree(pdata);
  286. }
  287. printk(KERN_INFO "appledisplay: Apple Cinema Display disconnected\n");
  288. }
  289. static struct usb_driver appledisplay_driver = {
  290. .name = "appledisplay",
  291. .probe = appledisplay_probe,
  292. .disconnect = appledisplay_disconnect,
  293. .id_table = appledisplay_table,
  294. };
  295. module_usb_driver(appledisplay_driver);
  296. MODULE_AUTHOR("Michael Hanselmann");
  297. MODULE_DESCRIPTION("Apple Cinema Display driver");
  298. MODULE_LICENSE("GPL");