1/*
2 *  Copyright (C) 2015       Red Hat Inc.
3 *                           Hans de Goede <hdegoede@redhat.com>
4 *  Copyright (C) 2008       SuSE Linux Products GmbH
5 *                           Thomas Renninger <trenn@suse.de>
6 *
7 *  May be copied or modified under the terms of the GNU General Public License
8 *
9 * video_detect.c:
10 * After PCI devices are glued with ACPI devices
11 * acpi_get_pci_dev() can be called to identify ACPI graphics
12 * devices for which a real graphics card is plugged in
13 *
14 * Depending on whether ACPI graphics extensions (cmp. ACPI spec Appendix B)
15 * are available, video.ko should be used to handle the device.
16 *
17 * Otherwise vendor specific drivers like thinkpad_acpi, asus-laptop,
18 * sony_acpi,... can take care about backlight brightness.
19 *
20 * Backlight drivers can use acpi_video_get_backlight_type() to determine
21 * which driver should handle the backlight.
22 *
23 * If CONFIG_ACPI_VIDEO is neither set as "compiled in" (y) nor as a module (m)
24 * this file will not be compiled and acpi_video_get_backlight_type() will
25 * always return acpi_backlight_vendor.
26 */
27
28#include <linux/export.h>
29#include <linux/acpi.h>
30#include <linux/backlight.h>
31#include <linux/dmi.h>
32#include <linux/module.h>
33#include <linux/pci.h>
34#include <linux/types.h>
35#include <linux/workqueue.h>
36#include <acpi/video.h>
37
38ACPI_MODULE_NAME("video");
39#define _COMPONENT		ACPI_VIDEO_COMPONENT
40
41void acpi_video_unregister_backlight(void);
42
43static bool backlight_notifier_registered;
44static struct notifier_block backlight_nb;
45static struct work_struct backlight_notify_work;
46
47static enum acpi_backlight_type acpi_backlight_cmdline = acpi_backlight_undef;
48static enum acpi_backlight_type acpi_backlight_dmi = acpi_backlight_undef;
49
50static void acpi_video_parse_cmdline(void)
51{
52	if (!strcmp("vendor", acpi_video_backlight_string))
53		acpi_backlight_cmdline = acpi_backlight_vendor;
54	if (!strcmp("video", acpi_video_backlight_string))
55		acpi_backlight_cmdline = acpi_backlight_video;
56	if (!strcmp("native", acpi_video_backlight_string))
57		acpi_backlight_cmdline = acpi_backlight_native;
58	if (!strcmp("none", acpi_video_backlight_string))
59		acpi_backlight_cmdline = acpi_backlight_none;
60}
61
62static acpi_status
63find_video(acpi_handle handle, u32 lvl, void *context, void **rv)
64{
65	long *cap = context;
66	struct pci_dev *dev;
67	struct acpi_device *acpi_dev;
68
69	static const struct acpi_device_id video_ids[] = {
70		{ACPI_VIDEO_HID, 0},
71		{"", 0},
72	};
73	if (acpi_bus_get_device(handle, &acpi_dev))
74		return AE_OK;
75
76	if (!acpi_match_device_ids(acpi_dev, video_ids)) {
77		dev = acpi_get_pci_dev(handle);
78		if (!dev)
79			return AE_OK;
80		pci_dev_put(dev);
81		*cap |= acpi_is_video_device(handle);
82	}
83	return AE_OK;
84}
85
86/* Force to use vendor driver when the ACPI device is known to be
87 * buggy */
88static int video_detect_force_vendor(const struct dmi_system_id *d)
89{
90	acpi_backlight_dmi = acpi_backlight_vendor;
91	return 0;
92}
93
94static int video_detect_force_video(const struct dmi_system_id *d)
95{
96	acpi_backlight_dmi = acpi_backlight_video;
97	return 0;
98}
99
100static int video_detect_force_native(const struct dmi_system_id *d)
101{
102	acpi_backlight_dmi = acpi_backlight_native;
103	return 0;
104}
105
106static const struct dmi_system_id video_detect_dmi_table[] = {
107	/* On Samsung X360, the BIOS will set a flag (VDRV) if generic
108	 * ACPI backlight device is used. This flag will definitively break
109	 * the backlight interface (even the vendor interface) untill next
110	 * reboot. It's why we should prevent video.ko from being used here
111	 * and we can't rely on a later call to acpi_video_unregister().
112	 */
113	{
114	 .callback = video_detect_force_vendor,
115	 .ident = "X360",
116	 .matches = {
117		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
118		DMI_MATCH(DMI_PRODUCT_NAME, "X360"),
119		DMI_MATCH(DMI_BOARD_NAME, "X360"),
120		},
121	},
122	{
123	.callback = video_detect_force_vendor,
124	.ident = "Asus UL30VT",
125	.matches = {
126		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
127		DMI_MATCH(DMI_PRODUCT_NAME, "UL30VT"),
128		},
129	},
130	{
131	.callback = video_detect_force_vendor,
132	.ident = "Asus UL30A",
133	.matches = {
134		DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
135		DMI_MATCH(DMI_PRODUCT_NAME, "UL30A"),
136		},
137	},
138
139	/*
140	 * These models have a working acpi_video backlight control, and using
141	 * native backlight causes a regression where backlight does not work
142	 * when userspace is not handling brightness key events. Disable
143	 * native_backlight on these to fix this:
144	 * https://bugzilla.kernel.org/show_bug.cgi?id=81691
145	 */
146	{
147	 .callback = video_detect_force_video,
148	 .ident = "ThinkPad T420",
149	 .matches = {
150		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
151		DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T420"),
152		},
153	},
154	{
155	 .callback = video_detect_force_video,
156	 .ident = "ThinkPad T520",
157	 .matches = {
158		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
159		DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad T520"),
160		},
161	},
162	{
163	 .callback = video_detect_force_video,
164	 .ident = "ThinkPad X201s",
165	 .matches = {
166		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
167		DMI_MATCH(DMI_PRODUCT_VERSION, "ThinkPad X201s"),
168		},
169	},
170
171	/* The native backlight controls do not work on some older machines */
172	{
173	 /* https://bugs.freedesktop.org/show_bug.cgi?id=81515 */
174	 .callback = video_detect_force_video,
175	 .ident = "HP ENVY 15 Notebook",
176	 .matches = {
177		DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
178		DMI_MATCH(DMI_PRODUCT_NAME, "HP ENVY 15 Notebook PC"),
179		},
180	},
181	{
182	 .callback = video_detect_force_video,
183	 .ident = "SAMSUNG 870Z5E/880Z5E/680Z5E",
184	 .matches = {
185		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
186		DMI_MATCH(DMI_PRODUCT_NAME, "870Z5E/880Z5E/680Z5E"),
187		},
188	},
189	{
190	 .callback = video_detect_force_video,
191	 .ident = "SAMSUNG 370R4E/370R4V/370R5E/3570RE/370R5V",
192	 .matches = {
193		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
194		DMI_MATCH(DMI_PRODUCT_NAME,
195			  "370R4E/370R4V/370R5E/3570RE/370R5V"),
196		},
197	},
198	{
199	 /* https://bugzilla.redhat.com/show_bug.cgi?id=1186097 */
200	 .callback = video_detect_force_video,
201	 .ident = "SAMSUNG 3570R/370R/470R/450R/510R/4450RV",
202	 .matches = {
203		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
204		DMI_MATCH(DMI_PRODUCT_NAME,
205			  "3570R/370R/470R/450R/510R/4450RV"),
206		},
207	},
208	{
209	 /* https://bugzilla.redhat.com/show_bug.cgi?id=1094948 */
210	 .callback = video_detect_force_video,
211	 .ident = "SAMSUNG 730U3E/740U3E",
212	 .matches = {
213		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
214		DMI_MATCH(DMI_PRODUCT_NAME, "730U3E/740U3E"),
215		},
216	},
217	{
218	 /* https://bugs.freedesktop.org/show_bug.cgi?id=87286 */
219	 .callback = video_detect_force_video,
220	 .ident = "SAMSUNG 900X3C/900X3D/900X3E/900X4C/900X4D",
221	 .matches = {
222		DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."),
223		DMI_MATCH(DMI_PRODUCT_NAME,
224			  "900X3C/900X3D/900X3E/900X4C/900X4D"),
225		},
226	},
227	{
228	 /* https://bugzilla.redhat.com/show_bug.cgi?id=1272633 */
229	 .callback = video_detect_force_video,
230	 .ident = "Dell XPS14 L421X",
231	 .matches = {
232		DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
233		DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
234		},
235	},
236	{
237	 /* https://bugzilla.redhat.com/show_bug.cgi?id=1163574 */
238	 .callback = video_detect_force_video,
239	 .ident = "Dell XPS15 L521X",
240	 .matches = {
241		DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
242		DMI_MATCH(DMI_PRODUCT_NAME, "XPS L521X"),
243		},
244	},
245
246	/* Non win8 machines which need native backlight nevertheless */
247	{
248	 /* https://bugzilla.redhat.com/show_bug.cgi?id=1201530 */
249	 .callback = video_detect_force_native,
250	 .ident = "Lenovo Ideapad S405",
251	 .matches = {
252		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
253		DMI_MATCH(DMI_BOARD_NAME, "Lenovo IdeaPad S405"),
254		},
255	},
256	{
257	 /* https://bugzilla.redhat.com/show_bug.cgi?id=1187004 */
258	 .callback = video_detect_force_native,
259	 .ident = "Lenovo Ideapad Z570",
260	 .matches = {
261		DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
262		DMI_MATCH(DMI_PRODUCT_NAME, "102434U"),
263		},
264	},
265	{
266	 /* https://bugzilla.redhat.com/show_bug.cgi?id=1217249 */
267	 .callback = video_detect_force_native,
268	 .ident = "Apple MacBook Pro 12,1",
269	 .matches = {
270		DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
271		DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro12,1"),
272		},
273	},
274	{ },
275};
276
277/* This uses a workqueue to avoid various locking ordering issues */
278static void acpi_video_backlight_notify_work(struct work_struct *work)
279{
280	if (acpi_video_get_backlight_type() != acpi_backlight_video)
281		acpi_video_unregister_backlight();
282}
283
284static int acpi_video_backlight_notify(struct notifier_block *nb,
285				       unsigned long val, void *bd)
286{
287	struct backlight_device *backlight = bd;
288
289	/* A raw bl registering may change video -> native */
290	if (backlight->props.type == BACKLIGHT_RAW &&
291	    val == BACKLIGHT_REGISTERED)
292		schedule_work(&backlight_notify_work);
293
294	return NOTIFY_OK;
295}
296
297/*
298 * Determine which type of backlight interface to use on this system,
299 * First check cmdline, then dmi quirks, then do autodetect.
300 *
301 * The autodetect order is:
302 * 1) Is the acpi-video backlight interface supported ->
303 *  no, use a vendor interface
304 * 2) Is this a win8 "ready" BIOS and do we have a native interface ->
305 *  yes, use a native interface
306 * 3) Else use the acpi-video interface
307 *
308 * Arguably the native on win8 check should be done first, but that would
309 * be a behavior change, which may causes issues.
310 */
311enum acpi_backlight_type acpi_video_get_backlight_type(void)
312{
313	static DEFINE_MUTEX(init_mutex);
314	static bool init_done;
315	static long video_caps;
316
317	/* Parse cmdline, dmi and acpi only once */
318	mutex_lock(&init_mutex);
319	if (!init_done) {
320		acpi_video_parse_cmdline();
321		dmi_check_system(video_detect_dmi_table);
322		acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
323				    ACPI_UINT32_MAX, find_video, NULL,
324				    &video_caps, NULL);
325		INIT_WORK(&backlight_notify_work,
326			  acpi_video_backlight_notify_work);
327		backlight_nb.notifier_call = acpi_video_backlight_notify;
328		backlight_nb.priority = 0;
329		if (backlight_register_notifier(&backlight_nb) == 0)
330			backlight_notifier_registered = true;
331		init_done = true;
332	}
333	mutex_unlock(&init_mutex);
334
335	if (acpi_backlight_cmdline != acpi_backlight_undef)
336		return acpi_backlight_cmdline;
337
338	if (acpi_backlight_dmi != acpi_backlight_undef)
339		return acpi_backlight_dmi;
340
341	if (!(video_caps & ACPI_VIDEO_BACKLIGHT))
342		return acpi_backlight_vendor;
343
344	if (acpi_osi_is_win8() && backlight_device_registered(BACKLIGHT_RAW))
345		return acpi_backlight_native;
346
347	return acpi_backlight_video;
348}
349EXPORT_SYMBOL(acpi_video_get_backlight_type);
350
351/*
352 * Set the preferred backlight interface type based on DMI info.
353 * This function allows DMI blacklists to be implemented by external
354 * platform drivers instead of putting a big blacklist in video_detect.c
355 */
356void acpi_video_set_dmi_backlight_type(enum acpi_backlight_type type)
357{
358	acpi_backlight_dmi = type;
359	/* Remove acpi-video backlight interface if it is no longer desired */
360	if (acpi_video_get_backlight_type() != acpi_backlight_video)
361		acpi_video_unregister_backlight();
362}
363EXPORT_SYMBOL(acpi_video_set_dmi_backlight_type);
364
365void __exit acpi_video_detect_exit(void)
366{
367	if (backlight_notifier_registered)
368		backlight_unregister_notifier(&backlight_nb);
369}
370