vserver 1.9.5.x5
[linux-2.6.git] / drivers / pnp / pnpbios / core.c
1 /*
2  * pnpbios -- PnP BIOS driver
3  *
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 <jdthood@mail.com>
16  *   Brian Gerst <bgerst@didntduck.org>
17  *
18  * Ported to the PnP Layer and several additional improvements (C) 2002
19  * by Adam Belay <ambx1@neo.rr.com>
20  *
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.
25  *
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.
30  *
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
34  */
35  
36 /* Change Log
37  *
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
46  */
47
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/pnpbios.h>
54 #include <linux/device.h>
55 #include <linux/pnp.h>
56 #include <linux/mm.h>
57 #include <linux/smp.h>
58 #include <linux/slab.h>
59 #include <linux/kobject_uevent.h>
60 #include <linux/completion.h>
61 #include <linux/spinlock.h>
62 #include <linux/dmi.h>
63 #include <linux/delay.h>
64 #include <linux/acpi.h>
65
66 #include <asm/page.h>
67 #include <asm/desc.h>
68 #include <asm/system.h>
69 #include <asm/byteorder.h>
70
71 #include "pnpbios.h"
72
73
74 /*
75  *
76  * PnP BIOS INTERFACE
77  *
78  */
79
80 static union pnp_bios_install_struct * pnp_bios_install = NULL;
81
82 int pnp_bios_present(void)
83 {
84         return (pnp_bios_install != NULL);
85 }
86
87 struct pnp_dev_node_info node_info;
88
89 void *pnpbios_kmalloc(size_t size, int f)
90 {
91         void *p = kmalloc( size, f );
92         if ( p == NULL )
93                 printk(KERN_ERR "PnPBIOS: kmalloc() failed\n");
94         else
95                 memset(p, 0, size);
96         return p;
97 }
98
99 /*
100  *
101  * DOCKING FUNCTIONS
102  *
103  */
104
105 #ifdef CONFIG_HOTPLUG
106
107 static int unloading = 0;
108 static struct completion unload_sem;
109
110 /*
111  * (Much of this belongs in a shared routine somewhere)
112  */
113  
114 static int pnp_dock_event(int dock, struct pnp_docking_station_info *info)
115 {
116         char *argv [3], **envp, *buf, *scratch;
117         int i = 0, value;
118
119         if (!hotplug_path [0])
120                 return -ENOENT;
121         if (!current->fs->root) {
122                 return -EAGAIN;
123         }
124         if (!(envp = (char **) pnpbios_kmalloc (20 * sizeof (char *), GFP_KERNEL))) {
125                 return -ENOMEM;
126         }
127         if (!(buf = pnpbios_kmalloc (256, GFP_KERNEL))) {
128                 kfree (envp);
129                 return -ENOMEM;
130         }
131
132         /* only one standardized param to hotplug command: type */
133         argv [0] = hotplug_path;
134         argv [1] = "dock";
135         argv [2] = NULL;
136
137         /* minimal command environment */
138         envp [i++] = "HOME=/";
139         envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
140
141 #ifdef  DEBUG
142         /* hint that policy agent should enter no-stdout debug mode */
143         envp [i++] = "DEBUG=kernel";
144 #endif
145         /* extensible set of named bus-specific parameters,
146          * supporting multiple driver selection algorithms.
147          */
148         scratch = buf;
149
150         /* action:  add, remove */
151         envp [i++] = scratch;
152         scratch += sprintf (scratch, "ACTION=%s", dock?"add":"remove") + 1;
153
154         /* Report the ident for the dock */
155         envp [i++] = scratch;
156         scratch += sprintf (scratch, "DOCK=%x/%x/%x",
157                 info->location_id, info->serial, info->capabilities);
158         envp[i] = NULL;
159         
160         value = call_usermodehelper (argv [0], argv, envp, 0);
161         kfree (buf);
162         kfree (envp);
163         return 0;
164 }
165
166 /*
167  * Poll the PnP docking at regular intervals
168  */
169 static int pnp_dock_thread(void * unused)
170 {
171         static struct pnp_docking_station_info now;
172         int docked = -1, d = 0;
173         daemonize("kpnpbiosd");
174         allow_signal(SIGKILL);
175         while(!unloading && !signal_pending(current))
176         {
177                 int status;
178                 
179                 /*
180                  * Poll every 2 seconds
181                  */
182                 msleep_interruptible(2000);
183                 if(signal_pending(current))
184                         break;
185
186                 status = pnp_bios_dock_station_info(&now);
187
188                 switch(status)
189                 {
190                         /*
191                          * No dock to manage
192                          */
193                         case PNP_FUNCTION_NOT_SUPPORTED:
194                                 complete_and_exit(&unload_sem, 0);
195                         case PNP_SYSTEM_NOT_DOCKED:
196                                 d = 0;
197                                 break;
198                         case PNP_SUCCESS:
199                                 d = 1;
200                                 break;
201                         default:
202                                 pnpbios_print_status( "pnp_dock_thread", status );
203                                 continue;
204                 }
205                 if(d != docked)
206                 {
207                         if(pnp_dock_event(d, &now)==0)
208                         {
209                                 docked = d;
210 #if 0
211                                 printk(KERN_INFO "PnPBIOS: Docking station %stached\n", docked?"at":"de");
212 #endif
213                         }
214                 }
215         }
216         complete_and_exit(&unload_sem, 0);
217 }
218
219 #endif   /* CONFIG_HOTPLUG */
220
221 static int pnpbios_get_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
222 {
223         u8 nodenum = dev->number;
224         struct pnp_bios_node * node;
225
226         /* just in case */
227         if(!pnpbios_is_dynamic(dev))
228                 return -EPERM;
229
230         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
231         if (!node)
232                 return -1;
233         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_DYNAMIC, node)) {
234                 kfree(node);
235                 return -ENODEV;
236         }
237         pnpbios_read_resources_from_node(res, node);
238         dev->active = pnp_is_active(dev);
239         kfree(node);
240         return 0;
241 }
242
243 static int pnpbios_set_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
244 {
245         u8 nodenum = dev->number;
246         struct pnp_bios_node * node;
247         int ret;
248
249         /* just in case */
250         if (!pnpbios_is_dynamic(dev))
251                 return -EPERM;
252
253         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
254         if (!node)
255                 return -1;
256         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_DYNAMIC, node)) {
257                 kfree(node);
258                 return -ENODEV;
259         }
260         if(pnpbios_write_resources_to_node(res, node)<0) {
261                 kfree(node);
262                 return -1;
263         }
264         ret = pnp_bios_set_dev_node(node->handle, (char)PNPMODE_DYNAMIC, node);
265         kfree(node);
266         if (ret > 0)
267                 ret = -1;
268         return ret;
269 }
270
271 static void pnpbios_zero_data_stream(struct pnp_bios_node * node)
272 {
273         unsigned char * p = (char *)node->data;
274         unsigned char * end = (char *)(node->data + node->size);
275         unsigned int len;
276         int i;
277         while ((char *)p < (char *)end) {
278                 if(p[0] & 0x80) { /* large tag */
279                         len = (p[2] << 8) | p[1];
280                         p += 3;
281                 } else {
282                         if (((p[0]>>3) & 0x0f) == 0x0f)
283                                 return;
284                         len = p[0] & 0x07;
285                         p += 1;
286                 }
287                 for (i = 0; i < len; i++)
288                         p[i] = 0;
289                 p += len;
290         }
291         printk(KERN_ERR "PnPBIOS: Resource structure did not contain an end tag.\n");
292 }
293
294 static int pnpbios_disable_resources(struct pnp_dev *dev)
295 {
296         struct pnp_bios_node * node;
297         u8 nodenum = dev->number;
298         int ret;
299
300         /* just in case */
301         if(dev->flags & PNPBIOS_NO_DISABLE || !pnpbios_is_dynamic(dev))
302                 return -EPERM;
303
304         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
305         if (!node)
306                 return -ENOMEM;
307
308         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_DYNAMIC, node)) {
309                 kfree(node);
310                 return -ENODEV;
311         }
312         pnpbios_zero_data_stream(node);
313
314         ret = pnp_bios_set_dev_node(dev->number, (char)PNPMODE_DYNAMIC, node);
315         kfree(node);
316         if (ret > 0)
317                 ret = -1;
318         return ret;
319 }
320
321 /* PnP Layer support */
322
323 struct pnp_protocol pnpbios_protocol = {
324         .name   = "Plug and Play BIOS",
325         .get    = pnpbios_get_resources,
326         .set    = pnpbios_set_resources,
327         .disable = pnpbios_disable_resources,
328 };
329
330 static int insert_device(struct pnp_dev *dev, struct pnp_bios_node * node)
331 {
332         struct list_head * pos;
333         struct pnp_dev * pnp_dev;
334         struct pnp_id *dev_id;
335         char id[8];
336
337         /* check if the device is already added */
338         dev->number = node->handle;
339         list_for_each (pos, &pnpbios_protocol.devices){
340                 pnp_dev = list_entry(pos, struct pnp_dev, protocol_list);
341                 if (dev->number == pnp_dev->number)
342                         return -1;
343         }
344
345         /* set the initial values for the PnP device */
346         dev_id = pnpbios_kmalloc(sizeof(struct pnp_id), GFP_KERNEL);
347         if (!dev_id)
348                 return -1;
349         pnpid32_to_pnpid(node->eisa_id,id);
350         memcpy(dev_id->id,id,7);
351         pnp_add_id(dev_id, dev);
352         pnpbios_parse_data_stream(dev, node);
353         dev->active = pnp_is_active(dev);
354         dev->flags = node->flags;
355         if (!(dev->flags & PNPBIOS_NO_CONFIG))
356                 dev->capabilities |= PNP_CONFIGURABLE;
357         if (!(dev->flags & PNPBIOS_NO_DISABLE))
358                 dev->capabilities |= PNP_DISABLE;
359         dev->capabilities |= PNP_READ;
360         if (pnpbios_is_dynamic(dev))
361                 dev->capabilities |= PNP_WRITE;
362         if (dev->flags & PNPBIOS_REMOVABLE)
363                 dev->capabilities |= PNP_REMOVABLE;
364         dev->protocol = &pnpbios_protocol;
365
366         /* clear out the damaged flags */
367         if (!dev->active)
368                 pnp_init_resource_table(&dev->res);
369
370         pnp_add_device(dev);
371         pnpbios_interface_attach_device(node);
372
373         return 0;
374 }
375
376 static void __init build_devlist(void)
377 {
378         u8 nodenum;
379         unsigned int nodes_got = 0;
380         unsigned int devs = 0;
381         struct pnp_bios_node *node;
382         struct pnp_dev *dev;
383
384         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
385         if (!node)
386                 return;
387
388         for(nodenum=0; nodenum<0xff; ) {
389                 u8 thisnodenum = nodenum;
390                 /* eventually we will want to use PNPMODE_STATIC here but for now
391                  * dynamic will help us catch buggy bioses to add to the blacklist.
392                  */
393                 if (!pnpbios_dont_use_current_config) {
394                         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_DYNAMIC, node))
395                                 break;
396                 } else {
397                         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_STATIC, node))
398                                 break;
399                 }
400                 nodes_got++;
401                 dev =  pnpbios_kmalloc(sizeof (struct pnp_dev), GFP_KERNEL);
402                 if (!dev)
403                         break;
404                 if(insert_device(dev,node)<0)
405                         kfree(dev);
406                 else
407                         devs++;
408                 if (nodenum <= thisnodenum) {
409                         printk(KERN_ERR "PnPBIOS: build_devlist: Node number 0x%x is out of sequence following node 0x%x. Aborting.\n", (unsigned int)nodenum, (unsigned int)thisnodenum);
410                         break;
411                 }
412         }
413         kfree(node);
414
415         printk(KERN_INFO "PnPBIOS: %i node%s reported by PnP BIOS; %i recorded by driver\n",
416                 nodes_got, nodes_got != 1 ? "s" : "", devs);
417 }
418
419 /*
420  *
421  * INIT AND EXIT
422  *
423  */
424
425 static int pnpbios_disabled; /* = 0 */
426 int pnpbios_dont_use_current_config; /* = 0 */
427
428 #ifndef MODULE
429 static int __init pnpbios_setup(char *str)
430 {
431         int invert;
432
433         while ((str != NULL) && (*str != '\0')) {
434                 if (strncmp(str, "off", 3) == 0)
435                         pnpbios_disabled=1;
436                 if (strncmp(str, "on", 2) == 0)
437                         pnpbios_disabled=0;
438                 invert = (strncmp(str, "no-", 3) == 0);
439                 if (invert)
440                         str += 3;
441                 if (strncmp(str, "curr", 4) == 0)
442                         pnpbios_dont_use_current_config = invert;
443                 str = strchr(str, ',');
444                 if (str != NULL)
445                         str += strspn(str, ", \t");
446         }
447
448         return 1;
449 }
450
451 __setup("pnpbios=", pnpbios_setup);
452 #endif
453
454 /* PnP BIOS signature: "$PnP" */
455 #define PNP_SIGNATURE   (('$' << 0) + ('P' << 8) + ('n' << 16) + ('P' << 24))
456
457 int __init pnpbios_probe_system(void)
458 {
459         union pnp_bios_install_struct *check;
460         u8 sum;
461         int length, i;
462
463         printk(KERN_INFO "PnPBIOS: Scanning system for PnP BIOS support...\n");
464
465         /*
466          * Search the defined area (0xf0000-0xffff0) for a valid PnP BIOS
467          * structure and, if one is found, sets up the selectors and
468          * entry points
469          */
470         for (check = (union pnp_bios_install_struct *) __va(0xf0000);
471              check < (union pnp_bios_install_struct *) __va(0xffff0);
472              check = (void *)check + 16) {
473                 if (check->fields.signature != PNP_SIGNATURE)
474                         continue;
475                 printk(KERN_INFO "PnPBIOS: Found PnP BIOS installation structure at 0x%p\n", check);
476                 length = check->fields.length;
477                 if (!length) {
478                         printk(KERN_ERR "PnPBIOS: installation structure is invalid, skipping\n");
479                         continue;
480                 }
481                 for (sum = 0, i = 0; i < length; i++)
482                         sum += check->chars[i];
483                 if (sum) {
484                         printk(KERN_ERR "PnPBIOS: installation structure is corrupted, skipping\n");
485                         continue;
486                 }
487                 if (check->fields.version < 0x10) {
488                         printk(KERN_WARNING "PnPBIOS: PnP BIOS version %d.%d is not supported\n",
489                                check->fields.version >> 4,
490                                check->fields.version & 15);
491                         continue;
492                 }
493                 printk(KERN_INFO "PnPBIOS: PnP BIOS version %d.%d, entry 0x%x:0x%x, dseg 0x%x\n",
494                        check->fields.version >> 4, check->fields.version & 15,
495                        check->fields.pm16cseg, check->fields.pm16offset,
496                        check->fields.pm16dseg);
497                 pnp_bios_install = check;
498                 return 1;
499         }
500
501         printk(KERN_INFO "PnPBIOS: PnP BIOS support was not detected.\n");
502         return 0;
503 }
504
505 static int __init exploding_pnp_bios(struct dmi_system_id *d)
506 {
507         printk(KERN_WARNING "%s detected. Disabling PnPBIOS\n", d->ident);
508         return 0;
509 }
510
511 static struct dmi_system_id pnpbios_dmi_table[] = {
512         {       /* PnPBIOS GPF on boot */
513                 .callback = exploding_pnp_bios,
514                 .ident = "Higraded P14H",
515                 .matches = {
516                         DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
517                         DMI_MATCH(DMI_BIOS_VERSION, "07.00T"),
518                         DMI_MATCH(DMI_SYS_VENDOR, "Higraded"),
519                         DMI_MATCH(DMI_PRODUCT_NAME, "P14H"),
520                 },
521         },
522         {       /* PnPBIOS GPF on boot */
523                 .callback = exploding_pnp_bios,
524                 .ident = "ASUS P4P800",
525                 .matches = {
526                         DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc."),
527                         DMI_MATCH(DMI_BOARD_NAME, "P4P800"),
528                 },
529         },
530         { }
531 };
532
533 int __init pnpbios_init(void)
534 {
535         int ret;
536
537         if (pnpbios_disabled || dmi_check_system(pnpbios_dmi_table)) {
538                 printk(KERN_INFO "PnPBIOS: Disabled\n");
539                 return -ENODEV;
540         }
541
542 #ifdef CONFIG_PNPACPI
543         if (!acpi_disabled && !pnpacpi_disabled) {
544                 pnpbios_disabled = 1;
545                 printk(KERN_INFO "PnPBIOS: Disabled by ACPI PNP\n");
546                 return -ENODEV;
547         }
548 #endif /* CONFIG_ACPI */
549
550         /* scan the system for pnpbios support */
551         if (!pnpbios_probe_system())
552                 return -ENODEV;
553
554         /* make preparations for bios calls */
555         pnpbios_calls_init(pnp_bios_install);
556
557         /* read the node info */
558         ret = pnp_bios_dev_node_info(&node_info);
559         if (ret) {
560                 printk(KERN_ERR "PnPBIOS: Unable to get node info.  Aborting.\n");
561                 return ret;
562         }
563
564         /* register with the pnp layer */
565         ret = pnp_register_protocol(&pnpbios_protocol);
566         if (ret) {
567                 printk(KERN_ERR "PnPBIOS: Unable to register driver.  Aborting.\n");
568                 return ret;
569         }
570
571         /* start the proc interface */
572         ret = pnpbios_proc_init();
573         if (ret)
574                 printk(KERN_ERR "PnPBIOS: Failed to create proc interface.\n");
575
576         /* scan for pnpbios devices */
577         build_devlist();
578
579         return 0;
580 }
581
582 subsys_initcall(pnpbios_init);
583
584 static int __init pnpbios_thread_init(void)
585 {
586         if (pnpbios_disabled)
587                 return 0;
588 #ifdef CONFIG_HOTPLUG
589         init_completion(&unload_sem);
590         if (kernel_thread(pnp_dock_thread, NULL, CLONE_KERNEL) > 0)
591                 unloading = 0;
592 #endif
593         return 0;
594 }
595
596 #ifndef MODULE
597
598 /* init/main.c calls pnpbios_init early */
599
600 /* Start the kernel thread later: */
601 module_init(pnpbios_thread_init);
602
603 #else
604
605 /*
606  * N.B.: Building pnpbios as a module hasn't been fully implemented
607  */
608
609 MODULE_LICENSE("GPL");
610
611 static int __init pnpbios_init_all(void)
612 {
613         int r;
614
615         r = pnpbios_init();
616         if (r)
617                 return r;
618         r = pnpbios_thread_init();
619         if (r)
620                 return r;
621         return 0;
622 }
623
624 static void __exit pnpbios_exit(void)
625 {
626 #ifdef CONFIG_HOTPLUG
627         unloading = 1;
628         wait_for_completion(&unload_sem);
629 #endif
630         pnpbios_proc_exit();
631         /* We ought to free resources here */
632         return;
633 }
634
635 module_init(pnpbios_init_all);
636 module_exit(pnpbios_exit);
637
638 #endif
639
640 EXPORT_SYMBOL(pnpbios_protocol);