1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pnpbios -- PnP BIOS driver
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.
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>
17 * Brian Gerst <bgerst@didntduck.org>
19 * Ported to the PnP Layer and several additional improvements (C) 2002
20 * by Adam Belay <ambx1@neo.rr.com>
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
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>
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>
55 #include <asm/byteorder.h>
66 static union pnp_bios_install_struct
*pnp_bios_install
= NULL
;
68 int pnp_bios_present(void)
70 return (pnp_bios_install
!= NULL
);
73 struct pnp_dev_node_info node_info
;
81 static struct completion unload_sem
;
84 * (Much of this belongs in a shared routine somewhere)
86 static int pnp_dock_event(int dock
, struct pnp_docking_station_info
*info
)
88 static char const sbin_pnpbios
[] = "/sbin/pnpbios";
89 char *argv
[3], **envp
, *buf
, *scratch
;
92 if (!(envp
= kcalloc(20, sizeof(char *), GFP_KERNEL
)))
94 if (!(buf
= kzalloc(256, GFP_KERNEL
))) {
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
103 argv
[0] = (char *)sbin_pnpbios
;
107 /* minimal command environment */
108 envp
[i
++] = "HOME=/";
109 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
112 /* hint that policy agent should enter no-stdout debug mode */
113 envp
[i
++] = "DEBUG=kernel";
115 /* extensible set of named bus-specific parameters,
116 * supporting multiple driver selection algorithms.
120 /* action: add, remove */
122 scratch
+= sprintf(scratch
, "ACTION=%s", dock
? "add" : "remove") + 1;
124 /* Report the ident for the dock */
126 scratch
+= sprintf(scratch
, "DOCK=%x/%x/%x",
127 info
->location_id
, info
->serial
, info
->capabilities
);
130 value
= call_usermodehelper(sbin_pnpbios
, argv
, envp
, UMH_WAIT_EXEC
);
137 * Poll the PnP docking at regular intervals
139 static int pnp_dock_thread(void *unused
)
141 static struct pnp_docking_station_info now
;
142 int docked
= -1, d
= 0;
149 * Poll every 2 seconds
151 msleep_interruptible(2000);
156 status
= pnp_bios_dock_station_info(&now
);
162 case PNP_FUNCTION_NOT_SUPPORTED
:
163 kthread_complete_and_exit(&unload_sem
, 0);
164 case PNP_SYSTEM_NOT_DOCKED
:
171 pnpbios_print_status("pnp_dock_thread", status
);
172 printk(KERN_WARNING
"PnPBIOS: disabling dock monitoring.\n");
173 kthread_complete_and_exit(&unload_sem
, 0);
176 if (pnp_dock_event(d
, &now
) == 0) {
180 "PnPBIOS: Docking station %stached\n",
181 docked
? "at" : "de");
186 kthread_complete_and_exit(&unload_sem
, 0);
189 static int pnpbios_get_resources(struct pnp_dev
*dev
)
191 u8 nodenum
= dev
->number
;
192 struct pnp_bios_node
*node
;
194 if (!pnpbios_is_dynamic(dev
))
197 pnp_dbg(&dev
->dev
, "get resources\n");
198 node
= kzalloc(node_info
.max_node_size
, GFP_KERNEL
);
201 if (pnp_bios_get_dev_node(&nodenum
, (char)PNPMODE_DYNAMIC
, node
)) {
205 pnpbios_read_resources_from_node(dev
, node
);
206 dev
->active
= pnp_is_active(dev
);
211 static int pnpbios_set_resources(struct pnp_dev
*dev
)
213 u8 nodenum
= dev
->number
;
214 struct pnp_bios_node
*node
;
217 if (!pnpbios_is_dynamic(dev
))
220 pnp_dbg(&dev
->dev
, "set resources\n");
221 node
= kzalloc(node_info
.max_node_size
, GFP_KERNEL
);
224 if (pnp_bios_get_dev_node(&nodenum
, (char)PNPMODE_DYNAMIC
, node
)) {
228 if (pnpbios_write_resources_to_node(dev
, node
) < 0) {
232 ret
= pnp_bios_set_dev_node(node
->handle
, (char)PNPMODE_DYNAMIC
, node
);
239 static void pnpbios_zero_data_stream(struct pnp_bios_node
*node
)
241 unsigned char *p
= (char *)node
->data
;
242 unsigned char *end
= (char *)(node
->data
+ node
->size
);
246 while ((char *)p
< (char *)end
) {
247 if (p
[0] & 0x80) { /* large tag */
248 len
= (p
[2] << 8) | p
[1];
251 if (((p
[0] >> 3) & 0x0f) == 0x0f)
256 for (i
= 0; i
< len
; i
++)
261 "PnPBIOS: Resource structure did not contain an end tag.\n");
264 static int pnpbios_disable_resources(struct pnp_dev
*dev
)
266 struct pnp_bios_node
*node
;
267 u8 nodenum
= dev
->number
;
270 if (dev
->flags
& PNPBIOS_NO_DISABLE
|| !pnpbios_is_dynamic(dev
))
273 node
= kzalloc(node_info
.max_node_size
, GFP_KERNEL
);
277 if (pnp_bios_get_dev_node(&nodenum
, (char)PNPMODE_DYNAMIC
, node
)) {
281 pnpbios_zero_data_stream(node
);
283 ret
= pnp_bios_set_dev_node(dev
->number
, (char)PNPMODE_DYNAMIC
, node
);
290 /* PnP Layer support */
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
,
299 static int __init
insert_device(struct pnp_bios_node
*node
)
305 /* check if the device is already added */
306 list_for_each_entry(dev
, &pnpbios_protocol
.devices
, protocol_list
) {
307 if (dev
->number
== node
->handle
)
311 pnp_eisa_id_to_string(node
->eisa_id
& PNP_EISA_ID_MASK
, id
);
312 dev
= pnp_alloc_dev(&pnpbios_protocol
, node
->handle
, id
);
316 pnpbios_parse_data_stream(dev
, node
);
317 dev
->active
= pnp_is_active(dev
);
318 dev
->flags
= node
->flags
;
319 if (!(dev
->flags
& PNPBIOS_NO_CONFIG
))
320 dev
->capabilities
|= PNP_CONFIGURABLE
;
321 if (!(dev
->flags
& PNPBIOS_NO_DISABLE
) && pnpbios_is_dynamic(dev
))
322 dev
->capabilities
|= PNP_DISABLE
;
323 dev
->capabilities
|= PNP_READ
;
324 if (pnpbios_is_dynamic(dev
))
325 dev
->capabilities
|= PNP_WRITE
;
326 if (dev
->flags
& PNPBIOS_REMOVABLE
)
327 dev
->capabilities
|= PNP_REMOVABLE
;
329 /* clear out the damaged flags */
331 pnp_init_resources(dev
);
333 error
= pnp_add_device(dev
);
335 put_device(&dev
->dev
);
339 pnpbios_interface_attach_device(node
);
344 static void __init
build_devlist(void)
347 unsigned int nodes_got
= 0;
348 unsigned int devs
= 0;
349 struct pnp_bios_node
*node
;
351 node
= kzalloc(node_info
.max_node_size
, GFP_KERNEL
);
355 for (nodenum
= 0; nodenum
< 0xff;) {
356 u8 thisnodenum
= nodenum
;
357 /* eventually we will want to use PNPMODE_STATIC here but for now
358 * dynamic will help us catch buggy bioses to add to the blacklist.
360 if (!pnpbios_dont_use_current_config
) {
361 if (pnp_bios_get_dev_node
362 (&nodenum
, (char)PNPMODE_DYNAMIC
, node
))
365 if (pnp_bios_get_dev_node
366 (&nodenum
, (char)PNPMODE_STATIC
, node
))
370 if (insert_device(node
) == 0)
372 if (nodenum
<= thisnodenum
) {
374 "PnPBIOS: build_devlist: Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
375 (unsigned int)nodenum
,
376 (unsigned int)thisnodenum
);
383 "PnPBIOS: %i node%s reported by PnP BIOS; %i recorded by driver\n",
384 nodes_got
, nodes_got
!= 1 ? "s" : "", devs
);
393 static int pnpbios_disabled
;
394 int pnpbios_dont_use_current_config
;
396 static int __init
pnpbios_setup(char *str
)
400 while ((str
!= NULL
) && (*str
!= '\0')) {
401 if (strncmp(str
, "off", 3) == 0)
402 pnpbios_disabled
= 1;
403 if (strncmp(str
, "on", 2) == 0)
404 pnpbios_disabled
= 0;
405 invert
= (strncmp(str
, "no-", 3) == 0);
408 if (strncmp(str
, "curr", 4) == 0)
409 pnpbios_dont_use_current_config
= invert
;
410 str
= strchr(str
, ',');
412 str
+= strspn(str
, ", \t");
418 __setup("pnpbios=", pnpbios_setup
);
420 /* PnP BIOS signature: "$PnP" */
421 #define PNP_SIGNATURE (('$' << 0) + ('P' << 8) + ('n' << 16) + ('P' << 24))
423 static int __init
pnpbios_probe_system(void)
425 union pnp_bios_install_struct
*check
;
429 printk(KERN_INFO
"PnPBIOS: Scanning system for PnP BIOS support...\n");
432 * Search the defined area (0xf0000-0xffff0) for a valid PnP BIOS
433 * structure and, if one is found, sets up the selectors and
436 for (check
= (union pnp_bios_install_struct
*)__va(0xf0000);
437 check
< (union pnp_bios_install_struct
*)__va(0xffff0);
438 check
= (void *)check
+ 16) {
439 if (check
->fields
.signature
!= PNP_SIGNATURE
)
442 "PnPBIOS: Found PnP BIOS installation structure at 0x%p\n",
444 length
= check
->fields
.length
;
447 "PnPBIOS: installation structure is invalid, skipping\n");
450 for (sum
= 0, i
= 0; i
< length
; i
++)
451 sum
+= check
->chars
[i
];
454 "PnPBIOS: installation structure is corrupted, skipping\n");
457 if (check
->fields
.version
< 0x10) {
459 "PnPBIOS: PnP BIOS version %d.%d is not supported\n",
460 check
->fields
.version
>> 4,
461 check
->fields
.version
& 15);
465 "PnPBIOS: PnP BIOS version %d.%d, entry 0x%x:0x%x, dseg 0x%x\n",
466 check
->fields
.version
>> 4, check
->fields
.version
& 15,
467 check
->fields
.pm16cseg
, check
->fields
.pm16offset
,
468 check
->fields
.pm16dseg
);
469 pnp_bios_install
= check
;
473 printk(KERN_INFO
"PnPBIOS: PnP BIOS support was not detected.\n");
477 static int __init
exploding_pnp_bios(const struct dmi_system_id
*d
)
479 printk(KERN_WARNING
"%s detected. Disabling PnPBIOS\n", d
->ident
);
483 static const struct dmi_system_id pnpbios_dmi_table
[] __initconst
= {
484 { /* PnPBIOS GPF on boot */
485 .callback
= exploding_pnp_bios
,
486 .ident
= "Higraded P14H",
488 DMI_MATCH(DMI_BIOS_VENDOR
, "American Megatrends Inc."),
489 DMI_MATCH(DMI_BIOS_VERSION
, "07.00T"),
490 DMI_MATCH(DMI_SYS_VENDOR
, "Higraded"),
491 DMI_MATCH(DMI_PRODUCT_NAME
, "P14H"),
494 { /* PnPBIOS GPF on boot */
495 .callback
= exploding_pnp_bios
,
496 .ident
= "ASUS P4P800",
498 DMI_MATCH(DMI_BOARD_VENDOR
, "ASUSTeK Computer Inc."),
499 DMI_MATCH(DMI_BOARD_NAME
, "P4P800"),
505 static int __init
pnpbios_init(void)
509 if (pnpbios_disabled
|| dmi_check_system(pnpbios_dmi_table
) ||
510 arch_pnpbios_disabled()) {
511 printk(KERN_INFO
"PnPBIOS: Disabled\n");
515 #ifdef CONFIG_PNPACPI
516 if (!acpi_disabled
&& !pnpacpi_disabled
) {
517 pnpbios_disabled
= 1;
518 printk(KERN_INFO
"PnPBIOS: Disabled by ACPI PNP\n");
521 #endif /* CONFIG_ACPI */
523 /* scan the system for pnpbios support */
524 if (!pnpbios_probe_system())
527 /* make preparations for bios calls */
528 pnpbios_calls_init(pnp_bios_install
);
530 /* read the node info */
531 ret
= pnp_bios_dev_node_info(&node_info
);
534 "PnPBIOS: Unable to get node info. Aborting.\n");
538 /* register with the pnp layer */
539 ret
= pnp_register_protocol(&pnpbios_protocol
);
542 "PnPBIOS: Unable to register driver. Aborting.\n");
546 /* start the proc interface */
547 ret
= pnpbios_proc_init();
549 printk(KERN_ERR
"PnPBIOS: Failed to create proc interface.\n");
551 /* scan for pnpbios devices */
554 pnp_platform_devices
= 1;
558 fs_initcall(pnpbios_init
);
560 static int __init
pnpbios_thread_init(void)
562 struct task_struct
*task
;
564 if (pnpbios_disabled
)
567 init_completion(&unload_sem
);
568 task
= kthread_run(pnp_dock_thread
, NULL
, "kpnpbiosd");
569 return PTR_ERR_OR_ZERO(task
);
572 /* Start the kernel thread later: */
573 device_initcall(pnpbios_thread_init
);
575 EXPORT_SYMBOL(pnpbios_protocol
);