xref: /openbmc/linux/drivers/pnp/pnpbios/core.c (revision b8265621)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * pnpbios -- PnP BIOS driver
4  *
5  * This driver provides access to Plug-'n'-Play services provided by
6  * the PnP BIOS firmware, described in the following documents:
7  *   Plug and Play BIOS Specification, Version 1.0A, 5 May 1994
8  *   Plug and Play BIOS Clarification Paper, 6 October 1994
9  *     Compaq Computer Corporation, Phoenix Technologies Ltd., Intel Corp.
10  *
11  * Originally (C) 1998 Christian Schmidt <schmidt@digadd.de>
12  * Modifications (C) 1998 Tom Lees <tom@lpsg.demon.co.uk>
13  * Minor reorganizations by David Hinds <dahinds@users.sourceforge.net>
14  * Further modifications (C) 2001, 2002 by:
15  *   Alan Cox <alan@redhat.com>
16  *   Thomas Hood
17  *   Brian Gerst <bgerst@didntduck.org>
18  *
19  * Ported to the PnP Layer and several additional improvements (C) 2002
20  * by Adam Belay <ambx1@neo.rr.com>
21  */
22 
23 /* Change Log
24  *
25  * Adam Belay - <ambx1@neo.rr.com> - March 16, 2003
26  * rev 1.01	Only call pnp_bios_dev_node_info once
27  *		Added pnpbios_print_status
28  *		Added several new error messages and info messages
29  *		Added pnpbios_interface_attach_device
30  *		integrated core and proc init system
31  *		Introduced PNPMODE flags
32  *		Removed some useless includes
33  */
34 
35 #include <linux/types.h>
36 #include <linux/init.h>
37 #include <linux/linkage.h>
38 #include <linux/kernel.h>
39 #include <linux/device.h>
40 #include <linux/pnp.h>
41 #include <linux/mm.h>
42 #include <linux/smp.h>
43 #include <linux/slab.h>
44 #include <linux/completion.h>
45 #include <linux/spinlock.h>
46 #include <linux/dmi.h>
47 #include <linux/delay.h>
48 #include <linux/acpi.h>
49 #include <linux/freezer.h>
50 #include <linux/kmod.h>
51 #include <linux/kthread.h>
52 
53 #include <asm/page.h>
54 #include <asm/desc.h>
55 #include <asm/byteorder.h>
56 
57 #include "../base.h"
58 #include "pnpbios.h"
59 
60 /*
61  *
62  * PnP BIOS INTERFACE
63  *
64  */
65 
66 static union pnp_bios_install_struct *pnp_bios_install = NULL;
67 
68 int pnp_bios_present(void)
69 {
70 	return (pnp_bios_install != NULL);
71 }
72 
73 struct pnp_dev_node_info node_info;
74 
75 /*
76  *
77  * DOCKING FUNCTIONS
78  *
79  */
80 
81 static struct completion unload_sem;
82 
83 /*
84  * (Much of this belongs in a shared routine somewhere)
85  */
86 static int pnp_dock_event(int dock, struct pnp_docking_station_info *info)
87 {
88 	static char const sbin_pnpbios[] = "/sbin/pnpbios";
89 	char *argv[3], **envp, *buf, *scratch;
90 	int i = 0, value;
91 
92 	if (!(envp = kcalloc(20, sizeof(char *), GFP_KERNEL)))
93 		return -ENOMEM;
94 	if (!(buf = kzalloc(256, GFP_KERNEL))) {
95 		kfree(envp);
96 		return -ENOMEM;
97 	}
98 
99 	/* FIXME: if there are actual users of this, it should be
100 	 * integrated into the driver core and use the usual infrastructure
101 	 * like sysfs and uevents
102 	 */
103 	argv[0] = (char *)sbin_pnpbios;
104 	argv[1] = "dock";
105 	argv[2] = NULL;
106 
107 	/* minimal command environment */
108 	envp[i++] = "HOME=/";
109 	envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
110 
111 #ifdef	DEBUG
112 	/* hint that policy agent should enter no-stdout debug mode */
113 	envp[i++] = "DEBUG=kernel";
114 #endif
115 	/* extensible set of named bus-specific parameters,
116 	 * supporting multiple driver selection algorithms.
117 	 */
118 	scratch = buf;
119 
120 	/* action:  add, remove */
121 	envp[i++] = scratch;
122 	scratch += sprintf(scratch, "ACTION=%s", dock ? "add" : "remove") + 1;
123 
124 	/* Report the ident for the dock */
125 	envp[i++] = scratch;
126 	scratch += sprintf(scratch, "DOCK=%x/%x/%x",
127 			   info->location_id, info->serial, info->capabilities);
128 	envp[i] = NULL;
129 
130 	value = call_usermodehelper(sbin_pnpbios, argv, envp, UMH_WAIT_EXEC);
131 	kfree(buf);
132 	kfree(envp);
133 	return 0;
134 }
135 
136 /*
137  * Poll the PnP docking at regular intervals
138  */
139 static int pnp_dock_thread(void *unused)
140 {
141 	static struct pnp_docking_station_info now;
142 	int docked = -1, d = 0;
143 
144 	set_freezable();
145 	while (1) {
146 		int status;
147 
148 		/*
149 		 * Poll every 2 seconds
150 		 */
151 		msleep_interruptible(2000);
152 
153 		if (try_to_freeze())
154 			continue;
155 
156 		status = pnp_bios_dock_station_info(&now);
157 
158 		switch (status) {
159 			/*
160 			 * No dock to manage
161 			 */
162 		case PNP_FUNCTION_NOT_SUPPORTED:
163 			complete_and_exit(&unload_sem, 0);
164 		case PNP_SYSTEM_NOT_DOCKED:
165 			d = 0;
166 			break;
167 		case PNP_SUCCESS:
168 			d = 1;
169 			break;
170 		default:
171 			pnpbios_print_status("pnp_dock_thread", status);
172 			printk(KERN_WARNING "PnPBIOS: disabling dock monitoring.\n");
173 			complete_and_exit(&unload_sem, 0);
174 		}
175 		if (d != docked) {
176 			if (pnp_dock_event(d, &now) == 0) {
177 				docked = d;
178 #if 0
179 				printk(KERN_INFO
180 				       "PnPBIOS: Docking station %stached\n",
181 				       docked ? "at" : "de");
182 #endif
183 			}
184 		}
185 	}
186 	complete_and_exit(&unload_sem, 0);
187 }
188 
189 static int pnpbios_get_resources(struct pnp_dev *dev)
190 {
191 	u8 nodenum = dev->number;
192 	struct pnp_bios_node *node;
193 
194 	if (!pnpbios_is_dynamic(dev))
195 		return -EPERM;
196 
197 	pnp_dbg(&dev->dev, "get resources\n");
198 	node = kzalloc(node_info.max_node_size, GFP_KERNEL);
199 	if (!node)
200 		return -1;
201 	if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
202 		kfree(node);
203 		return -ENODEV;
204 	}
205 	pnpbios_read_resources_from_node(dev, node);
206 	dev->active = pnp_is_active(dev);
207 	kfree(node);
208 	return 0;
209 }
210 
211 static int pnpbios_set_resources(struct pnp_dev *dev)
212 {
213 	u8 nodenum = dev->number;
214 	struct pnp_bios_node *node;
215 	int ret;
216 
217 	if (!pnpbios_is_dynamic(dev))
218 		return -EPERM;
219 
220 	pnp_dbg(&dev->dev, "set resources\n");
221 	node = kzalloc(node_info.max_node_size, GFP_KERNEL);
222 	if (!node)
223 		return -1;
224 	if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
225 		kfree(node);
226 		return -ENODEV;
227 	}
228 	if (pnpbios_write_resources_to_node(dev, node) < 0) {
229 		kfree(node);
230 		return -1;
231 	}
232 	ret = pnp_bios_set_dev_node(node->handle, (char)PNPMODE_DYNAMIC, node);
233 	kfree(node);
234 	if (ret > 0)
235 		ret = -1;
236 	return ret;
237 }
238 
239 static void pnpbios_zero_data_stream(struct pnp_bios_node *node)
240 {
241 	unsigned char *p = (char *)node->data;
242 	unsigned char *end = (char *)(node->data + node->size);
243 	unsigned int len;
244 	int i;
245 
246 	while ((char *)p < (char *)end) {
247 		if (p[0] & 0x80) {	/* large tag */
248 			len = (p[2] << 8) | p[1];
249 			p += 3;
250 		} else {
251 			if (((p[0] >> 3) & 0x0f) == 0x0f)
252 				return;
253 			len = p[0] & 0x07;
254 			p += 1;
255 		}
256 		for (i = 0; i < len; i++)
257 			p[i] = 0;
258 		p += len;
259 	}
260 	printk(KERN_ERR
261 	       "PnPBIOS: Resource structure did not contain an end tag.\n");
262 }
263 
264 static int pnpbios_disable_resources(struct pnp_dev *dev)
265 {
266 	struct pnp_bios_node *node;
267 	u8 nodenum = dev->number;
268 	int ret;
269 
270 	if (dev->flags & PNPBIOS_NO_DISABLE || !pnpbios_is_dynamic(dev))
271 		return -EPERM;
272 
273 	node = kzalloc(node_info.max_node_size, GFP_KERNEL);
274 	if (!node)
275 		return -ENOMEM;
276 
277 	if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
278 		kfree(node);
279 		return -ENODEV;
280 	}
281 	pnpbios_zero_data_stream(node);
282 
283 	ret = pnp_bios_set_dev_node(dev->number, (char)PNPMODE_DYNAMIC, node);
284 	kfree(node);
285 	if (ret > 0)
286 		ret = -1;
287 	return ret;
288 }
289 
290 /* PnP Layer support */
291 
292 struct pnp_protocol pnpbios_protocol = {
293 	.name = "Plug and Play BIOS",
294 	.get = pnpbios_get_resources,
295 	.set = pnpbios_set_resources,
296 	.disable = pnpbios_disable_resources,
297 };
298 
299 static int __init insert_device(struct pnp_bios_node *node)
300 {
301 	struct list_head *pos;
302 	struct pnp_dev *dev;
303 	char id[8];
304 	int error;
305 
306 	/* check if the device is already added */
307 	list_for_each(pos, &pnpbios_protocol.devices) {
308 		dev = list_entry(pos, struct pnp_dev, protocol_list);
309 		if (dev->number == node->handle)
310 			return -EEXIST;
311 	}
312 
313 	pnp_eisa_id_to_string(node->eisa_id & PNP_EISA_ID_MASK, id);
314 	dev = pnp_alloc_dev(&pnpbios_protocol, node->handle, id);
315 	if (!dev)
316 		return -ENOMEM;
317 
318 	pnpbios_parse_data_stream(dev, node);
319 	dev->active = pnp_is_active(dev);
320 	dev->flags = node->flags;
321 	if (!(dev->flags & PNPBIOS_NO_CONFIG))
322 		dev->capabilities |= PNP_CONFIGURABLE;
323 	if (!(dev->flags & PNPBIOS_NO_DISABLE) && pnpbios_is_dynamic(dev))
324 		dev->capabilities |= PNP_DISABLE;
325 	dev->capabilities |= PNP_READ;
326 	if (pnpbios_is_dynamic(dev))
327 		dev->capabilities |= PNP_WRITE;
328 	if (dev->flags & PNPBIOS_REMOVABLE)
329 		dev->capabilities |= PNP_REMOVABLE;
330 
331 	/* clear out the damaged flags */
332 	if (!dev->active)
333 		pnp_init_resources(dev);
334 
335 	error = pnp_add_device(dev);
336 	if (error) {
337 		put_device(&dev->dev);
338 		return error;
339 	}
340 
341 	pnpbios_interface_attach_device(node);
342 
343 	return 0;
344 }
345 
346 static void __init build_devlist(void)
347 {
348 	u8 nodenum;
349 	unsigned int nodes_got = 0;
350 	unsigned int devs = 0;
351 	struct pnp_bios_node *node;
352 
353 	node = kzalloc(node_info.max_node_size, GFP_KERNEL);
354 	if (!node)
355 		return;
356 
357 	for (nodenum = 0; nodenum < 0xff;) {
358 		u8 thisnodenum = nodenum;
359 		/* eventually we will want to use PNPMODE_STATIC here but for now
360 		 * dynamic will help us catch buggy bioses to add to the blacklist.
361 		 */
362 		if (!pnpbios_dont_use_current_config) {
363 			if (pnp_bios_get_dev_node
364 			    (&nodenum, (char)PNPMODE_DYNAMIC, node))
365 				break;
366 		} else {
367 			if (pnp_bios_get_dev_node
368 			    (&nodenum, (char)PNPMODE_STATIC, node))
369 				break;
370 		}
371 		nodes_got++;
372 		if (insert_device(node) == 0)
373 			devs++;
374 		if (nodenum <= thisnodenum) {
375 			printk(KERN_ERR
376 			       "PnPBIOS: build_devlist: Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
377 			       (unsigned int)nodenum,
378 			       (unsigned int)thisnodenum);
379 			break;
380 		}
381 	}
382 	kfree(node);
383 
384 	printk(KERN_INFO
385 	       "PnPBIOS: %i node%s reported by PnP BIOS; %i recorded by driver\n",
386 	       nodes_got, nodes_got != 1 ? "s" : "", devs);
387 }
388 
389 /*
390  *
391  * INIT AND EXIT
392  *
393  */
394 
395 static int pnpbios_disabled;
396 int pnpbios_dont_use_current_config;
397 
398 static int __init pnpbios_setup(char *str)
399 {
400 	int invert;
401 
402 	while ((str != NULL) && (*str != '\0')) {
403 		if (strncmp(str, "off", 3) == 0)
404 			pnpbios_disabled = 1;
405 		if (strncmp(str, "on", 2) == 0)
406 			pnpbios_disabled = 0;
407 		invert = (strncmp(str, "no-", 3) == 0);
408 		if (invert)
409 			str += 3;
410 		if (strncmp(str, "curr", 4) == 0)
411 			pnpbios_dont_use_current_config = invert;
412 		str = strchr(str, ',');
413 		if (str != NULL)
414 			str += strspn(str, ", \t");
415 	}
416 
417 	return 1;
418 }
419 
420 __setup("pnpbios=", pnpbios_setup);
421 
422 /* PnP BIOS signature: "$PnP" */
423 #define PNP_SIGNATURE   (('$' << 0) + ('P' << 8) + ('n' << 16) + ('P' << 24))
424 
425 static int __init pnpbios_probe_system(void)
426 {
427 	union pnp_bios_install_struct *check;
428 	u8 sum;
429 	int length, i;
430 
431 	printk(KERN_INFO "PnPBIOS: Scanning system for PnP BIOS support...\n");
432 
433 	/*
434 	 * Search the defined area (0xf0000-0xffff0) for a valid PnP BIOS
435 	 * structure and, if one is found, sets up the selectors and
436 	 * entry points
437 	 */
438 	for (check = (union pnp_bios_install_struct *)__va(0xf0000);
439 	     check < (union pnp_bios_install_struct *)__va(0xffff0);
440 	     check = (void *)check + 16) {
441 		if (check->fields.signature != PNP_SIGNATURE)
442 			continue;
443 		printk(KERN_INFO
444 		       "PnPBIOS: Found PnP BIOS installation structure at 0x%p\n",
445 		       check);
446 		length = check->fields.length;
447 		if (!length) {
448 			printk(KERN_ERR
449 			       "PnPBIOS: installation structure is invalid, skipping\n");
450 			continue;
451 		}
452 		for (sum = 0, i = 0; i < length; i++)
453 			sum += check->chars[i];
454 		if (sum) {
455 			printk(KERN_ERR
456 			       "PnPBIOS: installation structure is corrupted, skipping\n");
457 			continue;
458 		}
459 		if (check->fields.version < 0x10) {
460 			printk(KERN_WARNING
461 			       "PnPBIOS: PnP BIOS version %d.%d is not supported\n",
462 			       check->fields.version >> 4,
463 			       check->fields.version & 15);
464 			continue;
465 		}
466 		printk(KERN_INFO
467 		       "PnPBIOS: PnP BIOS version %d.%d, entry 0x%x:0x%x, dseg 0x%x\n",
468 		       check->fields.version >> 4, check->fields.version & 15,
469 		       check->fields.pm16cseg, check->fields.pm16offset,
470 		       check->fields.pm16dseg);
471 		pnp_bios_install = check;
472 		return 1;
473 	}
474 
475 	printk(KERN_INFO "PnPBIOS: PnP BIOS support was not detected.\n");
476 	return 0;
477 }
478 
479 static int __init exploding_pnp_bios(const struct dmi_system_id *d)
480 {
481 	printk(KERN_WARNING "%s detected. Disabling PnPBIOS\n", d->ident);
482 	return 0;
483 }
484 
485 static const struct dmi_system_id pnpbios_dmi_table[] __initconst = {
486 	{			/* PnPBIOS GPF on boot */
487 	 .callback = exploding_pnp_bios,
488 	 .ident = "Higraded P14H",
489 	 .matches = {
490 		     DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
491 		     DMI_MATCH(DMI_BIOS_VERSION, "07.00T"),
492 		     DMI_MATCH(DMI_SYS_VENDOR, "Higraded"),
493 		     DMI_MATCH(DMI_PRODUCT_NAME, "P14H"),
494 		     },
495 	 },
496 	{			/* PnPBIOS GPF on boot */
497 	 .callback = exploding_pnp_bios,
498 	 .ident = "ASUS P4P800",
499 	 .matches = {
500 		     DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc."),
501 		     DMI_MATCH(DMI_BOARD_NAME, "P4P800"),
502 		     },
503 	 },
504 	{}
505 };
506 
507 static int __init pnpbios_init(void)
508 {
509 	int ret;
510 
511 	if (pnpbios_disabled || dmi_check_system(pnpbios_dmi_table) ||
512 	    arch_pnpbios_disabled()) {
513 		printk(KERN_INFO "PnPBIOS: Disabled\n");
514 		return -ENODEV;
515 	}
516 
517 #ifdef CONFIG_PNPACPI
518 	if (!acpi_disabled && !pnpacpi_disabled) {
519 		pnpbios_disabled = 1;
520 		printk(KERN_INFO "PnPBIOS: Disabled by ACPI PNP\n");
521 		return -ENODEV;
522 	}
523 #endif				/* CONFIG_ACPI */
524 
525 	/* scan the system for pnpbios support */
526 	if (!pnpbios_probe_system())
527 		return -ENODEV;
528 
529 	/* make preparations for bios calls */
530 	pnpbios_calls_init(pnp_bios_install);
531 
532 	/* read the node info */
533 	ret = pnp_bios_dev_node_info(&node_info);
534 	if (ret) {
535 		printk(KERN_ERR
536 		       "PnPBIOS: Unable to get node info.  Aborting.\n");
537 		return ret;
538 	}
539 
540 	/* register with the pnp layer */
541 	ret = pnp_register_protocol(&pnpbios_protocol);
542 	if (ret) {
543 		printk(KERN_ERR
544 		       "PnPBIOS: Unable to register driver.  Aborting.\n");
545 		return ret;
546 	}
547 
548 	/* start the proc interface */
549 	ret = pnpbios_proc_init();
550 	if (ret)
551 		printk(KERN_ERR "PnPBIOS: Failed to create proc interface.\n");
552 
553 	/* scan for pnpbios devices */
554 	build_devlist();
555 
556 	pnp_platform_devices = 1;
557 	return 0;
558 }
559 
560 fs_initcall(pnpbios_init);
561 
562 static int __init pnpbios_thread_init(void)
563 {
564 	struct task_struct *task;
565 
566 	if (pnpbios_disabled)
567 		return 0;
568 
569 	init_completion(&unload_sem);
570 	task = kthread_run(pnp_dock_thread, NULL, "kpnpbiosd");
571 	return PTR_ERR_OR_ZERO(task);
572 }
573 
574 /* Start the kernel thread later: */
575 device_initcall(pnpbios_thread_init);
576 
577 EXPORT_SYMBOL(pnpbios_protocol);
578