appledisplay.c 9.0 KB

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