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 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 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 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
)
301 struct list_head
*pos
;
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
)
313 pnp_eisa_id_to_string(node
->eisa_id
& PNP_EISA_ID_MASK
, id
);
314 dev
= pnp_alloc_dev(&pnpbios_protocol
, node
->handle
, id
);
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
;
331 /* clear out the damaged flags */
333 pnp_init_resources(dev
);
335 error
= pnp_add_device(dev
);
337 put_device(&dev
->dev
);
341 pnpbios_interface_attach_device(node
);
346 static void __init
build_devlist(void)
349 unsigned int nodes_got
= 0;
350 unsigned int devs
= 0;
351 struct pnp_bios_node
*node
;
353 node
= kzalloc(node_info
.max_node_size
, GFP_KERNEL
);
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.
362 if (!pnpbios_dont_use_current_config
) {
363 if (pnp_bios_get_dev_node
364 (&nodenum
, (char)PNPMODE_DYNAMIC
, node
))
367 if (pnp_bios_get_dev_node
368 (&nodenum
, (char)PNPMODE_STATIC
, node
))
372 if (insert_device(node
) == 0)
374 if (nodenum
<= thisnodenum
) {
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
);
385 "PnPBIOS: %i node%s reported by PnP BIOS; %i recorded by driver\n",
386 nodes_got
, nodes_got
!= 1 ? "s" : "", devs
);
395 static int pnpbios_disabled
;
396 int pnpbios_dont_use_current_config
;
398 static int __init
pnpbios_setup(char *str
)
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);
410 if (strncmp(str
, "curr", 4) == 0)
411 pnpbios_dont_use_current_config
= invert
;
412 str
= strchr(str
, ',');
414 str
+= strspn(str
, ", \t");
420 __setup("pnpbios=", pnpbios_setup
);
422 /* PnP BIOS signature: "$PnP" */
423 #define PNP_SIGNATURE (('$' << 0) + ('P' << 8) + ('n' << 16) + ('P' << 24))
425 static int __init
pnpbios_probe_system(void)
427 union pnp_bios_install_struct
*check
;
431 printk(KERN_INFO
"PnPBIOS: Scanning system for PnP BIOS support...\n");
434 * Search the defined area (0xf0000-0xffff0) for a valid PnP BIOS
435 * structure and, if one is found, sets up the selectors and
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
)
444 "PnPBIOS: Found PnP BIOS installation structure at 0x%p\n",
446 length
= check
->fields
.length
;
449 "PnPBIOS: installation structure is invalid, skipping\n");
452 for (sum
= 0, i
= 0; i
< length
; i
++)
453 sum
+= check
->chars
[i
];
456 "PnPBIOS: installation structure is corrupted, skipping\n");
459 if (check
->fields
.version
< 0x10) {
461 "PnPBIOS: PnP BIOS version %d.%d is not supported\n",
462 check
->fields
.version
>> 4,
463 check
->fields
.version
& 15);
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
;
475 printk(KERN_INFO
"PnPBIOS: PnP BIOS support was not detected.\n");
479 static int __init
exploding_pnp_bios(const struct dmi_system_id
*d
)
481 printk(KERN_WARNING
"%s detected. Disabling PnPBIOS\n", d
->ident
);
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",
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"),
496 { /* PnPBIOS GPF on boot */
497 .callback
= exploding_pnp_bios
,
498 .ident
= "ASUS P4P800",
500 DMI_MATCH(DMI_BOARD_VENDOR
, "ASUSTeK Computer Inc."),
501 DMI_MATCH(DMI_BOARD_NAME
, "P4P800"),
507 static int __init
pnpbios_init(void)
511 if (pnpbios_disabled
|| dmi_check_system(pnpbios_dmi_table
) ||
512 arch_pnpbios_disabled()) {
513 printk(KERN_INFO
"PnPBIOS: Disabled\n");
517 #ifdef CONFIG_PNPACPI
518 if (!acpi_disabled
&& !pnpacpi_disabled
) {
519 pnpbios_disabled
= 1;
520 printk(KERN_INFO
"PnPBIOS: Disabled by ACPI PNP\n");
523 #endif /* CONFIG_ACPI */
525 /* scan the system for pnpbios support */
526 if (!pnpbios_probe_system())
529 /* make preparations for bios calls */
530 pnpbios_calls_init(pnp_bios_install
);
532 /* read the node info */
533 ret
= pnp_bios_dev_node_info(&node_info
);
536 "PnPBIOS: Unable to get node info. Aborting.\n");
540 /* register with the pnp layer */
541 ret
= pnp_register_protocol(&pnpbios_protocol
);
544 "PnPBIOS: Unable to register driver. Aborting.\n");
548 /* start the proc interface */
549 ret
= pnpbios_proc_init();
551 printk(KERN_ERR
"PnPBIOS: Failed to create proc interface.\n");
553 /* scan for pnpbios devices */
556 pnp_platform_devices
= 1;
560 fs_initcall(pnpbios_init
);
562 static int __init
pnpbios_thread_init(void)
564 struct task_struct
*task
;
566 if (pnpbios_disabled
)
569 init_completion(&unload_sem
);
570 task
= kthread_run(pnp_dock_thread
, NULL
, "kpnpbiosd");
571 return PTR_ERR_OR_ZERO(task
);
574 /* Start the kernel thread later: */
575 device_initcall(pnpbios_thread_init
);
577 EXPORT_SYMBOL(pnpbios_protocol
);