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