RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / pnp / pnpbios / core.c
blobf6a450beea48c5454a8cef090901f409e92bbdbf
1 /*
2 * pnpbios -- PnP BIOS driver
4 * This driver provides access to Plug-'n'-Play services provided by
5 * the PnP BIOS firmware, described in the following documents:
6 * Plug and Play BIOS Specification, Version 1.0A, 5 May 1994
7 * Plug and Play BIOS Clarification Paper, 6 October 1994
8 * Compaq Computer Corporation, Phoenix Technologies Ltd., Intel Corp.
9 *
10 * Originally (C) 1998 Christian Schmidt <schmidt@digadd.de>
11 * Modifications (C) 1998 Tom Lees <tom@lpsg.demon.co.uk>
12 * Minor reorganizations by David Hinds <dahinds@users.sourceforge.net>
13 * Further modifications (C) 2001, 2002 by:
14 * Alan Cox <alan@redhat.com>
15 * Thomas Hood
16 * Brian Gerst <bgerst@didntduck.org>
18 * Ported to the PnP Layer and several additional improvements (C) 2002
19 * by Adam Belay <ambx1@neo.rr.com>
21 * This program is free software; you can redistribute it and/or modify it
22 * under the terms of the GNU General Public License as published by the
23 * Free Software Foundation; either version 2, or (at your option) any
24 * later version.
26 * This program is distributed in the hope that it will be useful, but
27 * WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 /* Change Log
38 * Adam Belay - <ambx1@neo.rr.com> - March 16, 2003
39 * rev 1.01 Only call pnp_bios_dev_node_info once
40 * Added pnpbios_print_status
41 * Added several new error messages and info messages
42 * Added pnpbios_interface_attach_device
43 * integrated core and proc init system
44 * Introduced PNPMODE flags
45 * Removed some useless includes
48 #include <linux/types.h>
49 #include <linux/module.h>
50 #include <linux/init.h>
51 #include <linux/linkage.h>
52 #include <linux/kernel.h>
53 #include <linux/device.h>
54 #include <linux/pnp.h>
55 #include <linux/mm.h>
56 #include <linux/smp.h>
57 #include <linux/slab.h>
58 #include <linux/completion.h>
59 #include <linux/spinlock.h>
60 #include <linux/dmi.h>
61 #include <linux/delay.h>
62 #include <linux/acpi.h>
63 #include <linux/freezer.h>
64 #include <linux/kthread.h>
66 #include <asm/page.h>
67 #include <asm/desc.h>
68 #include <asm/system.h>
69 #include <asm/byteorder.h>
71 #include "../base.h"
72 #include "pnpbios.h"
76 * PnP BIOS INTERFACE
80 static union pnp_bios_install_struct *pnp_bios_install = NULL;
82 int pnp_bios_present(void)
84 return (pnp_bios_install != NULL);
87 struct pnp_dev_node_info node_info;
91 * DOCKING FUNCTIONS
95 #ifdef CONFIG_HOTPLUG
97 static struct completion unload_sem;
100 * (Much of this belongs in a shared routine somewhere)
102 static int pnp_dock_event(int dock, struct pnp_docking_station_info *info)
104 char *argv[3], **envp, *buf, *scratch;
105 int i = 0, value;
107 if (!(envp = kcalloc(20, sizeof(char *), GFP_KERNEL)))
108 return -ENOMEM;
109 if (!(buf = kzalloc(256, GFP_KERNEL))) {
110 kfree(envp);
111 return -ENOMEM;
114 argv[0] = "/sbin/pnpbios";
115 argv[1] = "dock";
116 argv[2] = NULL;
118 /* minimal command environment */
119 envp[i++] = "HOME=/";
120 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
122 #ifdef DEBUG
123 /* hint that policy agent should enter no-stdout debug mode */
124 envp[i++] = "DEBUG=kernel";
125 #endif
126 /* extensible set of named bus-specific parameters,
127 * supporting multiple driver selection algorithms.
129 scratch = buf;
131 /* action: add, remove */
132 envp[i++] = scratch;
133 scratch += sprintf(scratch, "ACTION=%s", dock ? "add" : "remove") + 1;
135 /* Report the ident for the dock */
136 envp[i++] = scratch;
137 scratch += sprintf(scratch, "DOCK=%x/%x/%x",
138 info->location_id, info->serial, info->capabilities);
139 envp[i] = NULL;
141 value = call_usermodehelper(argv [0], argv, envp, UMH_WAIT_EXEC);
142 kfree(buf);
143 kfree(envp);
144 return 0;
148 * Poll the PnP docking at regular intervals
150 static int pnp_dock_thread(void *unused)
152 static struct pnp_docking_station_info now;
153 int docked = -1, d = 0;
155 set_freezable();
156 while (1) {
157 int status;
160 * Poll every 2 seconds
162 msleep_interruptible(2000);
164 if (try_to_freeze())
165 continue;
167 status = pnp_bios_dock_station_info(&now);
169 switch (status) {
171 * No dock to manage
173 case PNP_FUNCTION_NOT_SUPPORTED:
174 complete_and_exit(&unload_sem, 0);
175 case PNP_SYSTEM_NOT_DOCKED:
176 d = 0;
177 break;
178 case PNP_SUCCESS:
179 d = 1;
180 break;
181 default:
182 pnpbios_print_status("pnp_dock_thread", status);
183 continue;
185 if (d != docked) {
186 if (pnp_dock_event(d, &now) == 0) {
187 docked = d;
191 complete_and_exit(&unload_sem, 0);
194 #endif /* CONFIG_HOTPLUG */
196 static int pnpbios_get_resources(struct pnp_dev *dev)
198 u8 nodenum = dev->number;
199 struct pnp_bios_node *node;
201 if (!pnpbios_is_dynamic(dev))
202 return -EPERM;
204 pnp_dbg(&dev->dev, "get resources\n");
205 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
206 if (!node)
207 return -1;
208 if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
209 kfree(node);
210 return -ENODEV;
212 pnpbios_read_resources_from_node(dev, node);
213 dev->active = pnp_is_active(dev);
214 kfree(node);
215 return 0;
218 static int pnpbios_set_resources(struct pnp_dev *dev)
220 u8 nodenum = dev->number;
221 struct pnp_bios_node *node;
222 int ret;
224 if (!pnpbios_is_dynamic(dev))
225 return -EPERM;
227 pnp_dbg(&dev->dev, "set resources\n");
228 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
229 if (!node)
230 return -1;
231 if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
232 kfree(node);
233 return -ENODEV;
235 if (pnpbios_write_resources_to_node(dev, node) < 0) {
236 kfree(node);
237 return -1;
239 ret = pnp_bios_set_dev_node(node->handle, (char)PNPMODE_DYNAMIC, node);
240 kfree(node);
241 if (ret > 0)
242 ret = -1;
243 return ret;
246 static void pnpbios_zero_data_stream(struct pnp_bios_node *node)
248 unsigned char *p = (char *)node->data;
249 unsigned char *end = (char *)(node->data + node->size);
250 unsigned int len;
251 int i;
253 while ((char *)p < (char *)end) {
254 if (p[0] & 0x80) { /* large tag */
255 len = (p[2] << 8) | p[1];
256 p += 3;
257 } else {
258 if (((p[0] >> 3) & 0x0f) == 0x0f)
259 return;
260 len = p[0] & 0x07;
261 p += 1;
263 for (i = 0; i < len; i++)
264 p[i] = 0;
265 p += len;
267 printk(KERN_ERR
268 "PnPBIOS: Resource structure did not contain an end tag.\n");
271 static int pnpbios_disable_resources(struct pnp_dev *dev)
273 struct pnp_bios_node *node;
274 u8 nodenum = dev->number;
275 int ret;
277 if (dev->flags & PNPBIOS_NO_DISABLE || !pnpbios_is_dynamic(dev))
278 return -EPERM;
280 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
281 if (!node)
282 return -ENOMEM;
284 if (pnp_bios_get_dev_node(&nodenum, (char)PNPMODE_DYNAMIC, node)) {
285 kfree(node);
286 return -ENODEV;
288 pnpbios_zero_data_stream(node);
290 ret = pnp_bios_set_dev_node(dev->number, (char)PNPMODE_DYNAMIC, node);
291 kfree(node);
292 if (ret > 0)
293 ret = -1;
294 return ret;
297 /* PnP Layer support */
299 struct pnp_protocol pnpbios_protocol = {
300 .name = "Plug and Play BIOS",
301 .get = pnpbios_get_resources,
302 .set = pnpbios_set_resources,
303 .disable = pnpbios_disable_resources,
306 static int __init insert_device(struct pnp_bios_node *node)
308 struct list_head *pos;
309 struct pnp_dev *dev;
310 char id[8];
312 /* check if the device is already added */
313 list_for_each(pos, &pnpbios_protocol.devices) {
314 dev = list_entry(pos, struct pnp_dev, protocol_list);
315 if (dev->number == node->handle)
316 return -1;
319 pnp_eisa_id_to_string(node->eisa_id & PNP_EISA_ID_MASK, id);
320 dev = pnp_alloc_dev(&pnpbios_protocol, node->handle, id);
321 if (!dev)
322 return -1;
324 pnpbios_parse_data_stream(dev, node);
325 dev->active = pnp_is_active(dev);
326 dev->flags = node->flags;
327 if (!(dev->flags & PNPBIOS_NO_CONFIG))
328 dev->capabilities |= PNP_CONFIGURABLE;
329 if (!(dev->flags & PNPBIOS_NO_DISABLE) && pnpbios_is_dynamic(dev))
330 dev->capabilities |= PNP_DISABLE;
331 dev->capabilities |= PNP_READ;
332 if (pnpbios_is_dynamic(dev))
333 dev->capabilities |= PNP_WRITE;
334 if (dev->flags & PNPBIOS_REMOVABLE)
335 dev->capabilities |= PNP_REMOVABLE;
337 /* clear out the damaged flags */
338 if (!dev->active)
339 pnp_init_resources(dev);
341 pnp_add_device(dev);
342 pnpbios_interface_attach_device(node);
344 return 0;
347 static void __init build_devlist(void)
349 u8 nodenum;
350 unsigned int nodes_got = 0;
351 unsigned int devs = 0;
352 struct pnp_bios_node *node;
354 node = kzalloc(node_info.max_node_size, GFP_KERNEL);
355 if (!node)
356 return;
358 for (nodenum = 0; nodenum < 0xff;) {
359 u8 thisnodenum = nodenum;
360 /* eventually we will want to use PNPMODE_STATIC here but for now
361 * dynamic will help us catch buggy bioses to add to the blacklist.
363 if (!pnpbios_dont_use_current_config) {
364 if (pnp_bios_get_dev_node
365 (&nodenum, (char)PNPMODE_DYNAMIC, node))
366 break;
367 } else {
368 if (pnp_bios_get_dev_node
369 (&nodenum, (char)PNPMODE_STATIC, node))
370 break;
372 nodes_got++;
373 if (insert_device(node) == 0)
374 devs++;
375 if (nodenum <= thisnodenum) {
376 printk(KERN_ERR
377 "PnPBIOS: build_devlist: Node number 0x%x is out of sequence following node 0x%x. Aborting.\n",
378 (unsigned int)nodenum,
379 (unsigned int)thisnodenum);
380 break;
383 kfree(node);
385 printk(KERN_INFO
386 "PnPBIOS: %i node%s reported by PnP BIOS; %i recorded by driver\n",
387 nodes_got, nodes_got != 1 ? "s" : "", devs);
392 * INIT AND EXIT
396 static int pnpbios_disabled;
397 int pnpbios_dont_use_current_config;
399 static int __init pnpbios_setup(char *str)
401 int invert;
403 while ((str != NULL) && (*str != '\0')) {
404 if (strncmp(str, "off", 3) == 0)
405 pnpbios_disabled = 1;
406 if (strncmp(str, "on", 2) == 0)
407 pnpbios_disabled = 0;
408 invert = (strncmp(str, "no-", 3) == 0);
409 if (invert)
410 str += 3;
411 if (strncmp(str, "curr", 4) == 0)
412 pnpbios_dont_use_current_config = invert;
413 str = strchr(str, ',');
414 if (str != NULL)
415 str += strspn(str, ", \t");
418 return 1;
421 __setup("pnpbios=", pnpbios_setup);
423 /* PnP BIOS signature: "$PnP" */
424 #define PNP_SIGNATURE (('$' << 0) + ('P' << 8) + ('n' << 16) + ('P' << 24))
426 static int __init pnpbios_probe_system(void)
428 union pnp_bios_install_struct *check;
429 u8 sum;
430 int length, i;
432 printk(KERN_INFO "PnPBIOS: Scanning system for PnP BIOS support...\n");
435 * Search the defined area (0xf0000-0xffff0) for a valid PnP BIOS
436 * structure and, if one is found, sets up the selectors and
437 * entry points
439 for (check = (union pnp_bios_install_struct *)__va(0xf0000);
440 check < (union pnp_bios_install_struct *)__va(0xffff0);
441 check = (void *)check + 16) {
442 if (check->fields.signature != PNP_SIGNATURE)
443 continue;
444 printk(KERN_INFO
445 "PnPBIOS: Found PnP BIOS installation structure at 0x%p\n",
446 check);
447 length = check->fields.length;
448 if (!length) {
449 printk(KERN_ERR
450 "PnPBIOS: installation structure is invalid, skipping\n");
451 continue;
453 for (sum = 0, i = 0; i < length; i++)
454 sum += check->chars[i];
455 if (sum) {
456 printk(KERN_ERR
457 "PnPBIOS: installation structure is corrupted, skipping\n");
458 continue;
460 if (check->fields.version < 0x10) {
461 printk(KERN_WARNING
462 "PnPBIOS: PnP BIOS version %d.%d is not supported\n",
463 check->fields.version >> 4,
464 check->fields.version & 15);
465 continue;
467 printk(KERN_INFO
468 "PnPBIOS: PnP BIOS version %d.%d, entry 0x%x:0x%x, dseg 0x%x\n",
469 check->fields.version >> 4, check->fields.version & 15,
470 check->fields.pm16cseg, check->fields.pm16offset,
471 check->fields.pm16dseg);
472 pnp_bios_install = check;
473 return 1;
476 printk(KERN_INFO "PnPBIOS: PnP BIOS support was not detected.\n");
477 return 0;
480 static int __init exploding_pnp_bios(const struct dmi_system_id *d)
482 printk(KERN_WARNING "%s detected. Disabling PnPBIOS\n", d->ident);
483 return 0;
486 static struct dmi_system_id pnpbios_dmi_table[] __initdata = {
487 { /* PnPBIOS GPF on boot */
488 .callback = exploding_pnp_bios,
489 .ident = "Higraded P14H",
490 .matches = {
491 DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
492 DMI_MATCH(DMI_BIOS_VERSION, "07.00T"),
493 DMI_MATCH(DMI_SYS_VENDOR, "Higraded"),
494 DMI_MATCH(DMI_PRODUCT_NAME, "P14H"),
497 { /* PnPBIOS GPF on boot */
498 .callback = exploding_pnp_bios,
499 .ident = "ASUS P4P800",
500 .matches = {
501 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc."),
502 DMI_MATCH(DMI_BOARD_NAME, "P4P800"),
508 static int __init pnpbios_init(void)
510 int ret;
512 #if defined(CONFIG_PPC)
513 if (check_legacy_ioport(PNPBIOS_BASE))
514 return -ENODEV;
515 #endif
516 if (pnpbios_disabled || dmi_check_system(pnpbios_dmi_table) ||
517 paravirt_enabled()) {
518 printk(KERN_INFO "PnPBIOS: Disabled\n");
519 return -ENODEV;
521 #ifdef CONFIG_PNPACPI
522 if (!acpi_disabled && !pnpacpi_disabled) {
523 pnpbios_disabled = 1;
524 printk(KERN_INFO "PnPBIOS: Disabled by ACPI PNP\n");
525 return -ENODEV;
527 #endif /* CONFIG_ACPI */
529 /* scan the system for pnpbios support */
530 if (!pnpbios_probe_system())
531 return -ENODEV;
533 /* make preparations for bios calls */
534 pnpbios_calls_init(pnp_bios_install);
536 /* read the node info */
537 ret = pnp_bios_dev_node_info(&node_info);
538 if (ret) {
539 printk(KERN_ERR
540 "PnPBIOS: Unable to get node info. Aborting.\n");
541 return ret;
544 /* register with the pnp layer */
545 ret = pnp_register_protocol(&pnpbios_protocol);
546 if (ret) {
547 printk(KERN_ERR
548 "PnPBIOS: Unable to register driver. Aborting.\n");
549 return ret;
552 /* start the proc interface */
553 ret = pnpbios_proc_init();
554 if (ret)
555 printk(KERN_ERR "PnPBIOS: Failed to create proc interface.\n");
557 /* scan for pnpbios devices */
558 build_devlist();
560 pnp_platform_devices = 1;
561 return 0;
564 fs_initcall(pnpbios_init);
566 static int __init pnpbios_thread_init(void)
568 #if defined(CONFIG_PPC)
569 if (check_legacy_ioport(PNPBIOS_BASE))
570 return 0;
571 #endif
572 if (pnpbios_disabled)
573 return 0;
574 #ifdef CONFIG_HOTPLUG
576 struct task_struct *task;
577 init_completion(&unload_sem);
578 task = kthread_run(pnp_dock_thread, NULL, "kpnpbiosd");
579 if (IS_ERR(task))
580 return PTR_ERR(task);
582 #endif
583 return 0;
586 /* Start the kernel thread later: */
587 module_init(pnpbios_thread_init);
589 EXPORT_SYMBOL(pnpbios_protocol);