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