appledisplay.c 9.7 KB

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