Merge /spare/repo/linux-2.6/
[linux-2.6] / 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
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
184                 if(signal_pending(current)) {
185                         if (try_to_freeze())
186                                 continue;
187                         break;
188                 }
189
190                 status = pnp_bios_dock_station_info(&now);
191
192                 switch(status)
193                 {
194                         /*
195                          * No dock to manage
196                          */
197                         case PNP_FUNCTION_NOT_SUPPORTED:
198                                 complete_and_exit(&unload_sem, 0);
199                         case PNP_SYSTEM_NOT_DOCKED:
200                                 d = 0;
201                                 break;
202                         case PNP_SUCCESS:
203                                 d = 1;
204                                 break;
205                         default:
206                                 pnpbios_print_status( "pnp_dock_thread", status );
207                                 continue;
208                 }
209                 if(d != docked)
210                 {
211                         if(pnp_dock_event(d, &now)==0)
212                         {
213                                 docked = d;
214 #if 0
215                                 printk(KERN_INFO "PnPBIOS: Docking station %stached\n", docked?"at":"de");
216 #endif
217                         }
218                 }
219         }
220         complete_and_exit(&unload_sem, 0);
221 }
222
223 #endif   /* CONFIG_HOTPLUG */
224
225 static int pnpbios_get_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
226 {
227         u8 nodenum = dev->number;
228         struct pnp_bios_node * node;
229
230         /* just in case */
231         if(!pnpbios_is_dynamic(dev))
232                 return -EPERM;
233
234         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
235         if (!node)
236                 return -1;
237         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_DYNAMIC, node)) {
238                 kfree(node);
239                 return -ENODEV;
240         }
241         pnpbios_read_resources_from_node(res, node);
242         dev->active = pnp_is_active(dev);
243         kfree(node);
244         return 0;
245 }
246
247 static int pnpbios_set_resources(struct pnp_dev * dev, struct pnp_resource_table * res)
248 {
249         u8 nodenum = dev->number;
250         struct pnp_bios_node * node;
251         int ret;
252
253         /* just in case */
254         if (!pnpbios_is_dynamic(dev))
255                 return -EPERM;
256
257         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
258         if (!node)
259                 return -1;
260         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_DYNAMIC, node)) {
261                 kfree(node);
262                 return -ENODEV;
263         }
264         if(pnpbios_write_resources_to_node(res, node)<0) {
265                 kfree(node);
266                 return -1;
267         }
268         ret = pnp_bios_set_dev_node(node->handle, (char)PNPMODE_DYNAMIC, node);
269         kfree(node);
270         if (ret > 0)
271                 ret = -1;
272         return ret;
273 }
274
275 static void pnpbios_zero_data_stream(struct pnp_bios_node * node)
276 {
277         unsigned char * p = (char *)node->data;
278         unsigned char * end = (char *)(node->data + node->size);
279         unsigned int len;
280         int i;
281         while ((char *)p < (char *)end) {
282                 if(p[0] & 0x80) { /* large tag */
283                         len = (p[2] << 8) | p[1];
284                         p += 3;
285                 } else {
286                         if (((p[0]>>3) & 0x0f) == 0x0f)
287                                 return;
288                         len = p[0] & 0x07;
289                         p += 1;
290                 }
291                 for (i = 0; i < len; i++)
292                         p[i] = 0;
293                 p += len;
294         }
295         printk(KERN_ERR "PnPBIOS: Resource structure did not contain an end tag.\n");
296 }
297
298 static int pnpbios_disable_resources(struct pnp_dev *dev)
299 {
300         struct pnp_bios_node * node;
301         u8 nodenum = dev->number;
302         int ret;
303
304         /* just in case */
305         if(dev->flags & PNPBIOS_NO_DISABLE || !pnpbios_is_dynamic(dev))
306                 return -EPERM;
307
308         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
309         if (!node)
310                 return -ENOMEM;
311
312         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_DYNAMIC, node)) {
313                 kfree(node);
314                 return -ENODEV;
315         }
316         pnpbios_zero_data_stream(node);
317
318         ret = pnp_bios_set_dev_node(dev->number, (char)PNPMODE_DYNAMIC, node);
319         kfree(node);
320         if (ret > 0)
321                 ret = -1;
322         return ret;
323 }
324
325 /* PnP Layer support */
326
327 struct pnp_protocol pnpbios_protocol = {
328         .name   = "Plug and Play BIOS",
329         .get    = pnpbios_get_resources,
330         .set    = pnpbios_set_resources,
331         .disable = pnpbios_disable_resources,
332 };
333
334 static int insert_device(struct pnp_dev *dev, struct pnp_bios_node * node)
335 {
336         struct list_head * pos;
337         struct pnp_dev * pnp_dev;
338         struct pnp_id *dev_id;
339         char id[8];
340
341         /* check if the device is already added */
342         dev->number = node->handle;
343         list_for_each (pos, &pnpbios_protocol.devices){
344                 pnp_dev = list_entry(pos, struct pnp_dev, protocol_list);
345                 if (dev->number == pnp_dev->number)
346                         return -1;
347         }
348
349         /* set the initial values for the PnP device */
350         dev_id = pnpbios_kmalloc(sizeof(struct pnp_id), GFP_KERNEL);
351         if (!dev_id)
352                 return -1;
353         pnpid32_to_pnpid(node->eisa_id,id);
354         memcpy(dev_id->id,id,7);
355         pnp_add_id(dev_id, dev);
356         pnpbios_parse_data_stream(dev, node);
357         dev->active = pnp_is_active(dev);
358         dev->flags = node->flags;
359         if (!(dev->flags & PNPBIOS_NO_CONFIG))
360                 dev->capabilities |= PNP_CONFIGURABLE;
361         if (!(dev->flags & PNPBIOS_NO_DISABLE))
362                 dev->capabilities |= PNP_DISABLE;
363         dev->capabilities |= PNP_READ;
364         if (pnpbios_is_dynamic(dev))
365                 dev->capabilities |= PNP_WRITE;
366         if (dev->flags & PNPBIOS_REMOVABLE)
367                 dev->capabilities |= PNP_REMOVABLE;
368         dev->protocol = &pnpbios_protocol;
369
370         /* clear out the damaged flags */
371         if (!dev->active)
372                 pnp_init_resource_table(&dev->res);
373
374         pnp_add_device(dev);
375         pnpbios_interface_attach_device(node);
376
377         return 0;
378 }
379
380 static void __init build_devlist(void)
381 {
382         u8 nodenum;
383         unsigned int nodes_got = 0;
384         unsigned int devs = 0;
385         struct pnp_bios_node *node;
386         struct pnp_dev *dev;
387
388         node = pnpbios_kmalloc(node_info.max_node_size, GFP_KERNEL);
389         if (!node)
390                 return;
391
392         for(nodenum=0; nodenum<0xff; ) {
393                 u8 thisnodenum = nodenum;
394                 /* eventually we will want to use PNPMODE_STATIC here but for now
395                  * dynamic will help us catch buggy bioses to add to the blacklist.
396                  */
397                 if (!pnpbios_dont_use_current_config) {
398                         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_DYNAMIC, node))
399                                 break;
400                 } else {
401                         if (pnp_bios_get_dev_node(&nodenum, (char )PNPMODE_STATIC, node))
402                                 break;
403                 }
404                 nodes_got++;
405                 dev =  pnpbios_kmalloc(sizeof (struct pnp_dev), GFP_KERNEL);
406                 if (!dev)
407                         break;
408                 if(insert_device(dev,node)<0)
409                         kfree(dev);
410                 else
411                         devs++;
412                 if (nodenum <= thisnodenum) {
413                         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);
414                         break;
415                 }
416         }
417         kfree(node);
418
419         printk(KERN_INFO "PnPBIOS: %i node%s reported by PnP BIOS; %i recorded by driver\n",
420                 nodes_got, nodes_got != 1 ? "s" : "", devs);
421 }
422
423 /*
424  *
425  * INIT AND EXIT
426  *
427  */
428
429 static int pnpbios_disabled; /* = 0 */
430 int pnpbios_dont_use_current_config; /* = 0 */
431
432 #ifndef MODULE
433 static int __init pnpbios_setup(char *str)
434 {
435         int invert;
436
437         while ((str != NULL) && (*str != '\0')) {
438                 if (strncmp(str, "off", 3) == 0)
439                         pnpbios_disabled=1;
440                 if (strncmp(str, "on", 2) == 0)
441                         pnpbios_disabled=0;
442                 invert = (strncmp(str, "no-", 3) == 0);
443                 if (invert)
444                         str += 3;
445                 if (strncmp(str, "curr", 4) == 0)
446                         pnpbios_dont_use_current_config = invert;
447                 str = strchr(str, ',');
448                 if (str != NULL)
449                         str += strspn(str, ", \t");
450         }
451
452         return 1;
453 }
454
455 __setup("pnpbios=", pnpbios_setup);
456 #endif
457
458 /* PnP BIOS signature: "$PnP" */
459 #define PNP_SIGNATURE   (('$' << 0) + ('P' << 8) + ('n' << 16) + ('P' << 24))
460
461 static int __init pnpbios_probe_system(void)
462 {
463         union pnp_bios_install_struct *check;
464         u8 sum;
465         int length, i;
466
467         printk(KERN_INFO "PnPBIOS: Scanning system for PnP BIOS support...\n");
468
469         /*
470          * Search the defined area (0xf0000-0xffff0) for a valid PnP BIOS
471          * structure and, if one is found, sets up the selectors and
472          * entry points
473          */
474         for (check = (union pnp_bios_install_struct *) __va(0xf0000);
475              check < (union pnp_bios_install_struct *) __va(0xffff0);
476              check = (void *)check + 16) {
477                 if (check->fields.signature != PNP_SIGNATURE)
478                         continue;
479                 printk(KERN_INFO "PnPBIOS: Found PnP BIOS installation structure at 0x%p\n", check);
480                 length = check->fields.length;
481                 if (!length) {
482                         printk(KERN_ERR "PnPBIOS: installation structure is invalid, skipping\n");
483                         continue;
484                 }
485                 for (sum = 0, i = 0; i < length; i++)
486                         sum += check->chars[i];
487                 if (sum) {
488                         printk(KERN_ERR "PnPBIOS: installation structure is corrupted, skipping\n");
489                         continue;
490                 }
491                 if (check->fields.version < 0x10) {
492                         printk(KERN_WARNING "PnPBIOS: PnP BIOS version %d.%d is not supported\n",
493                                check->fields.version >> 4,
494                                check->fields.version & 15);
495                         continue;
496                 }
497                 printk(KERN_INFO "PnPBIOS: PnP BIOS version %d.%d, entry 0x%x:0x%x, dseg 0x%x\n",
498                        check->fields.version >> 4, check->fields.version & 15,
499                        check->fields.pm16cseg, check->fields.pm16offset,
500                        check->fields.pm16dseg);
501                 pnp_bios_install = check;
502                 return 1;
503         }
504
505         printk(KERN_INFO "PnPBIOS: PnP BIOS support was not detected.\n");
506         return 0;
507 }
508
509 static int __init exploding_pnp_bios(struct dmi_system_id *d)
510 {
511         printk(KERN_WARNING "%s detected. Disabling PnPBIOS\n", d->ident);
512         return 0;
513 }
514
515 static struct dmi_system_id pnpbios_dmi_table[] __initdata = {
516         {       /* PnPBIOS GPF on boot */
517                 .callback = exploding_pnp_bios,
518                 .ident = "Higraded P14H",
519                 .matches = {
520                         DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
521                         DMI_MATCH(DMI_BIOS_VERSION, "07.00T"),
522                         DMI_MATCH(DMI_SYS_VENDOR, "Higraded"),
523                         DMI_MATCH(DMI_PRODUCT_NAME, "P14H"),
524                 },
525         },
526         {       /* PnPBIOS GPF on boot */
527                 .callback = exploding_pnp_bios,
528                 .ident = "ASUS P4P800",
529                 .matches = {
530                         DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc."),
531                         DMI_MATCH(DMI_BOARD_NAME, "P4P800"),
532                 },
533         },
534         { }
535 };
536
537 static int __init pnpbios_init(void)
538 {
539         int ret;
540
541         if (pnpbios_disabled || dmi_check_system(pnpbios_dmi_table)) {
542                 printk(KERN_INFO "PnPBIOS: Disabled\n");
543                 return -ENODEV;
544         }
545
546 #ifdef CONFIG_PNPACPI
547         if (!acpi_disabled && !pnpacpi_disabled) {
548                 pnpbios_disabled = 1;
549                 printk(KERN_INFO "PnPBIOS: Disabled by ACPI PNP\n");
550                 return -ENODEV;
551         }
552 #endif /* CONFIG_ACPI */
553
554         /* scan the system for pnpbios support */
555         if (!pnpbios_probe_system())
556                 return -ENODEV;
557
558         /* make preparations for bios calls */
559         pnpbios_calls_init(pnp_bios_install);
560
561         /* read the node info */
562         ret = pnp_bios_dev_node_info(&node_info);
563         if (ret) {
564                 printk(KERN_ERR "PnPBIOS: Unable to get node info.  Aborting.\n");
565                 return ret;
566         }
567
568         /* register with the pnp layer */
569         ret = pnp_register_protocol(&pnpbios_protocol);
570         if (ret) {
571                 printk(KERN_ERR "PnPBIOS: Unable to register driver.  Aborting.\n");
572                 return ret;
573         }
574
575         /* start the proc interface */
576         ret = pnpbios_proc_init();
577         if (ret)
578                 printk(KERN_ERR "PnPBIOS: Failed to create proc interface.\n");
579
580         /* scan for pnpbios devices */
581         build_devlist();
582
583         return 0;
584 }
585
586 subsys_initcall(pnpbios_init);
587
588 static int __init pnpbios_thread_init(void)
589 {
590         if (pnpbios_disabled)
591                 return 0;
592 #ifdef CONFIG_HOTPLUG
593         init_completion(&unload_sem);
594         if (kernel_thread(pnp_dock_thread, NULL, CLONE_KERNEL) > 0)
595                 unloading = 0;
596 #endif
597         return 0;
598 }
599
600 #ifndef MODULE
601
602 /* init/main.c calls pnpbios_init early */
603
604 /* Start the kernel thread later: */
605 module_init(pnpbios_thread_init);
606
607 #else
608
609 /*
610  * N.B.: Building pnpbios as a module hasn't been fully implemented
611  */
612
613 MODULE_LICENSE("GPL");
614
615 static int __init pnpbios_init_all(void)
616 {
617         int r;
618
619         r = pnpbios_init();
620         if (r)
621                 return r;
622         r = pnpbios_thread_init();
623         if (r)
624                 return r;
625         return 0;
626 }
627
628 static void __exit pnpbios_exit(void)
629 {
630 #ifdef CONFIG_HOTPLUG
631         unloading = 1;
632         wait_for_completion(&unload_sem);
633 #endif
634         pnpbios_proc_exit();
635         /* We ought to free resources here */
636         return;
637 }
638
639 module_init(pnpbios_init_all);
640 module_exit(pnpbios_exit);
641
642 #endif
643
644 EXPORT_SYMBOL(pnpbios_protocol);