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