[PATCH] USB: new SiS device id
[linux-2.6] / drivers / acpi / pci_link.c
1 /*
2  *  pci_link.c - ACPI PCI Interrupt Link Device Driver ($Revision: 34 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *  Copyright (C) 2002       Dominik Brodowski <devel@brodo.de>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or (at
13  *  your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  *
26  * TBD: 
27  *      1. Support more than one IRQ resource entry per link device (index).
28  *      2. Implement start/stop mechanism and use ACPI Bus Driver facilities
29  *         for IRQ management (e.g. start()->_SRS).
30  */
31
32 #include <linux/sysdev.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/types.h>
37 #include <linux/proc_fs.h>
38 #include <linux/spinlock.h>
39 #include <linux/pm.h>
40 #include <linux/pci.h>
41
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
44
45
46 #define _COMPONENT              ACPI_PCI_COMPONENT
47 ACPI_MODULE_NAME                ("pci_link")
48
49 #define ACPI_PCI_LINK_CLASS             "pci_irq_routing"
50 #define ACPI_PCI_LINK_HID               "PNP0C0F"
51 #define ACPI_PCI_LINK_DRIVER_NAME       "ACPI PCI Interrupt Link Driver"
52 #define ACPI_PCI_LINK_DEVICE_NAME       "PCI Interrupt Link"
53 #define ACPI_PCI_LINK_FILE_INFO         "info"
54 #define ACPI_PCI_LINK_FILE_STATUS       "state"
55
56 #define ACPI_PCI_LINK_MAX_POSSIBLE 16
57
58 static int acpi_pci_link_add (struct acpi_device *device);
59 static int acpi_pci_link_remove (struct acpi_device *device, int type);
60
61 static struct acpi_driver acpi_pci_link_driver = {
62         .name =         ACPI_PCI_LINK_DRIVER_NAME,
63         .class =        ACPI_PCI_LINK_CLASS,
64         .ids =          ACPI_PCI_LINK_HID,
65         .ops =          {
66                                 .add =    acpi_pci_link_add,
67                                 .remove = acpi_pci_link_remove,
68                         },
69 };
70
71 struct acpi_pci_link_irq {
72         u8                      active;                 /* Current IRQ */
73         u8                      edge_level;             /* All IRQs */
74         u8                      active_high_low;        /* All IRQs */
75         u8                      initialized;
76         u8                      resource_type;
77         u8                      possible_count;
78         u8                      possible[ACPI_PCI_LINK_MAX_POSSIBLE];
79 };
80
81 struct acpi_pci_link {
82         struct list_head        node;
83         struct acpi_device      *device;
84         acpi_handle             handle;
85         struct acpi_pci_link_irq irq;
86 };
87
88 static struct {
89         int                     count;
90         struct list_head        entries;
91 }                               acpi_link;
92
93
94 /* --------------------------------------------------------------------------
95                             PCI Link Device Management
96    -------------------------------------------------------------------------- */
97
98 /*
99  * set context (link) possible list from resource list
100  */
101 static acpi_status
102 acpi_pci_link_check_possible (
103         struct acpi_resource    *resource,
104         void                    *context)
105 {
106         struct acpi_pci_link    *link = (struct acpi_pci_link *) context;
107         u32                     i = 0;
108
109         ACPI_FUNCTION_TRACE("acpi_pci_link_check_possible");
110
111         switch (resource->id) {
112         case ACPI_RSTYPE_START_DPF:
113                 return_ACPI_STATUS(AE_OK);
114         case ACPI_RSTYPE_IRQ:
115         {
116                 struct acpi_resource_irq *p = &resource->data.irq;
117                 if (!p || !p->number_of_interrupts) {
118                         ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Blank IRQ resource\n"));
119                         return_ACPI_STATUS(AE_OK);
120                 }
121                 for (i = 0; (i<p->number_of_interrupts && i<ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
122                         if (!p->interrupts[i]) {
123                                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid IRQ %d\n", p->interrupts[i]));
124                                 continue;
125                         }
126                         link->irq.possible[i] = p->interrupts[i];
127                         link->irq.possible_count++;
128                 }
129                 link->irq.edge_level = p->edge_level;
130                 link->irq.active_high_low = p->active_high_low;
131                 link->irq.resource_type = ACPI_RSTYPE_IRQ;
132                 break;
133         }
134         case ACPI_RSTYPE_EXT_IRQ:
135         {
136                 struct acpi_resource_ext_irq *p = &resource->data.extended_irq;
137                 if (!p || !p->number_of_interrupts) {
138                         ACPI_DEBUG_PRINT((ACPI_DB_WARN, 
139                                 "Blank EXT IRQ resource\n"));
140                         return_ACPI_STATUS(AE_OK);
141                 }
142                 for (i = 0; (i<p->number_of_interrupts && i<ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
143                         if (!p->interrupts[i]) {
144                                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid IRQ %d\n", p->interrupts[i]));
145                                 continue;
146                         }
147                         link->irq.possible[i] = p->interrupts[i];
148                         link->irq.possible_count++;
149                 }
150                 link->irq.edge_level = p->edge_level;
151                 link->irq.active_high_low = p->active_high_low;
152                 link->irq.resource_type = ACPI_RSTYPE_EXT_IRQ;
153                 break;
154         }
155         default:
156                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
157                         "Resource is not an IRQ entry\n"));
158                 return_ACPI_STATUS(AE_OK);
159         }
160
161         return_ACPI_STATUS(AE_CTRL_TERMINATE);
162 }
163
164
165 static int
166 acpi_pci_link_get_possible (
167         struct acpi_pci_link    *link)
168 {
169         acpi_status             status;
170
171         ACPI_FUNCTION_TRACE("acpi_pci_link_get_possible");
172
173         if (!link)
174                 return_VALUE(-EINVAL);
175
176         status = acpi_walk_resources(link->handle, METHOD_NAME__PRS,
177                         acpi_pci_link_check_possible, link);
178         if (ACPI_FAILURE(status)) {
179                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRS\n"));
180                 return_VALUE(-ENODEV);
181         }
182
183         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
184                 "Found %d possible IRQs\n", link->irq.possible_count));
185
186         return_VALUE(0);
187 }
188
189
190 static acpi_status
191 acpi_pci_link_check_current (
192         struct acpi_resource    *resource,
193         void                    *context)
194 {
195         int                     *irq = (int *) context;
196
197         ACPI_FUNCTION_TRACE("acpi_pci_link_check_current");
198
199         switch (resource->id) {
200         case ACPI_RSTYPE_IRQ:
201         {
202                 struct acpi_resource_irq *p = &resource->data.irq;
203                 if (!p || !p->number_of_interrupts) {
204                         /*
205                          * IRQ descriptors may have no IRQ# bits set,
206                          * particularly those those w/ _STA disabled
207                          */
208                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
209                                 "Blank IRQ resource\n")); 
210                         return_ACPI_STATUS(AE_OK);
211                 }
212                 *irq = p->interrupts[0];
213                 break;
214         }
215         case ACPI_RSTYPE_EXT_IRQ:
216         {
217                 struct acpi_resource_ext_irq *p = &resource->data.extended_irq;
218                 if (!p || !p->number_of_interrupts) {
219                         /*
220                          * extended IRQ descriptors must
221                          * return at least 1 IRQ
222                          */
223                         ACPI_DEBUG_PRINT((ACPI_DB_WARN,
224                                 "Blank EXT IRQ resource\n"));
225                         return_ACPI_STATUS(AE_OK);
226                 }
227                 *irq = p->interrupts[0];
228                 break;
229         }
230         default:
231                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
232                         "Resource isn't an IRQ\n"));
233                 return_ACPI_STATUS(AE_OK);
234         }
235         return_ACPI_STATUS(AE_CTRL_TERMINATE);
236 }
237
238 /*
239  * Run _CRS and set link->irq.active
240  *
241  * return value:
242  * 0 - success
243  * !0 - failure
244  */
245 static int
246 acpi_pci_link_get_current (
247         struct acpi_pci_link    *link)
248 {
249         int                     result = 0;
250         acpi_status             status = AE_OK;
251         int                     irq = 0;
252
253         ACPI_FUNCTION_TRACE("acpi_pci_link_get_current");
254
255         if (!link || !link->handle)
256                 return_VALUE(-EINVAL);
257
258         link->irq.active = 0;
259
260         /* in practice, status disabled is meaningless, ignore it */
261         if (acpi_strict) {
262                 /* Query _STA, set link->device->status */
263                 result = acpi_bus_get_status(link->device);
264                 if (result) {
265                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to read status\n"));
266                         goto end;
267                 }
268
269                 if (!link->device->status.enabled) {
270                         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
271                         return_VALUE(0);
272                 }
273         }
274
275         /* 
276          * Query and parse _CRS to get the current IRQ assignment. 
277          */
278
279         status = acpi_walk_resources(link->handle, METHOD_NAME__CRS,
280                         acpi_pci_link_check_current, &irq);
281         if (ACPI_FAILURE(status)) {
282                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _CRS\n"));
283                 result = -ENODEV;
284                 goto end;
285         }
286
287         if (acpi_strict && !irq) {
288                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "_CRS returned 0\n"));
289                 result = -ENODEV;
290         }
291
292         link->irq.active = irq;
293
294         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
295
296 end:
297         return_VALUE(result);
298 }
299
300 static int
301 acpi_pci_link_set (
302         struct acpi_pci_link    *link,
303         int                     irq)
304 {
305         int                     result = 0;
306         acpi_status             status = AE_OK;
307         struct {
308                 struct acpi_resource    res;
309                 struct acpi_resource    end;
310         }    *resource;
311         struct acpi_buffer      buffer = {0, NULL};
312
313         ACPI_FUNCTION_TRACE("acpi_pci_link_set");
314
315         if (!link || !irq)
316                 return_VALUE(-EINVAL);
317
318         resource = kmalloc( sizeof(*resource)+1, GFP_KERNEL);
319         if(!resource)
320                 return_VALUE(-ENOMEM);
321
322         memset(resource, 0, sizeof(*resource)+1);
323         buffer.length = sizeof(*resource) +1;
324         buffer.pointer = resource;
325
326         switch(link->irq.resource_type) {
327         case ACPI_RSTYPE_IRQ:
328                 resource->res.id = ACPI_RSTYPE_IRQ;
329                 resource->res.length = sizeof(struct acpi_resource);
330                 resource->res.data.irq.edge_level = link->irq.edge_level;
331                 resource->res.data.irq.active_high_low = link->irq.active_high_low;
332                 if (link->irq.edge_level == ACPI_EDGE_SENSITIVE)
333                         resource->res.data.irq.shared_exclusive = ACPI_EXCLUSIVE;
334                 else
335                         resource->res.data.irq.shared_exclusive = ACPI_SHARED;
336                 resource->res.data.irq.number_of_interrupts = 1;
337                 resource->res.data.irq.interrupts[0] = irq;
338                 break;
339            
340         case ACPI_RSTYPE_EXT_IRQ:
341                 resource->res.id = ACPI_RSTYPE_EXT_IRQ;
342                 resource->res.length = sizeof(struct acpi_resource);
343                 resource->res.data.extended_irq.producer_consumer = ACPI_CONSUMER;
344                 resource->res.data.extended_irq.edge_level = link->irq.edge_level;
345                 resource->res.data.extended_irq.active_high_low = link->irq.active_high_low;
346                 if (link->irq.edge_level == ACPI_EDGE_SENSITIVE)
347                         resource->res.data.irq.shared_exclusive = ACPI_EXCLUSIVE;
348                 else
349                         resource->res.data.irq.shared_exclusive = ACPI_SHARED;
350                 resource->res.data.extended_irq.number_of_interrupts = 1;
351                 resource->res.data.extended_irq.interrupts[0] = irq;
352                 /* ignore resource_source, it's optional */
353                 break;
354         default:
355                 printk("ACPI BUG: resource_type %d\n", link->irq.resource_type);
356                 result = -EINVAL;
357                 goto end;
358
359         }
360         resource->end.id = ACPI_RSTYPE_END_TAG;
361
362         /* Attempt to set the resource */
363         status = acpi_set_current_resources(link->handle, &buffer);
364
365         /* check for total failure */
366         if (ACPI_FAILURE(status)) {
367                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SRS\n"));
368                 result = -ENODEV;
369                 goto end;
370         }
371
372         /* Query _STA, set device->status */
373         result = acpi_bus_get_status(link->device);
374         if (result) {
375                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to read status\n"));
376                 goto end;
377         }
378         if (!link->device->status.enabled) {
379                 printk(KERN_WARNING PREFIX
380                         "%s [%s] disabled and referenced, BIOS bug.\n",
381                         acpi_device_name(link->device),
382                         acpi_device_bid(link->device));
383         }
384
385         /* Query _CRS, set link->irq.active */
386         result = acpi_pci_link_get_current(link);
387         if (result) {
388                 goto end;
389         }
390
391         /*
392          * Is current setting not what we set?
393          * set link->irq.active
394          */
395         if (link->irq.active != irq) {
396                 /*
397                  * policy: when _CRS doesn't return what we just _SRS
398                  * assume _SRS worked and override _CRS value.
399                  */
400                 printk(KERN_WARNING PREFIX 
401                         "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
402                         acpi_device_name(link->device),
403                         acpi_device_bid(link->device),
404                         link->irq.active, irq);
405                 link->irq.active = irq;
406         }
407
408         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
409         
410 end:
411         kfree(resource);
412         return_VALUE(result);
413 }
414
415
416 /* --------------------------------------------------------------------------
417                             PCI Link IRQ Management
418    -------------------------------------------------------------------------- */
419
420 /*
421  * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
422  * Link Devices to move the PIRQs around to minimize sharing.
423  * 
424  * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
425  * that the BIOS has already set to active.  This is necessary because
426  * ACPI has no automatic means of knowing what ISA IRQs are used.  Note that
427  * if the BIOS doesn't set a Link Device active, ACPI needs to program it
428  * even if acpi_irq_nobalance is set.
429  *
430  * A tables of penalties avoids directing PCI interrupts to well known
431  * ISA IRQs. Boot params are available to over-ride the default table:
432  *
433  * List interrupts that are free for PCI use.
434  * acpi_irq_pci=n[,m]
435  *
436  * List interrupts that should not be used for PCI:
437  * acpi_irq_isa=n[,m]
438  *
439  * Note that PCI IRQ routers have a list of possible IRQs,
440  * which may not include the IRQs this table says are available.
441  * 
442  * Since this heuristic can't tell the difference between a link
443  * that no device will attach to, vs. a link which may be shared
444  * by multiple active devices -- it is not optimal.
445  *
446  * If interrupt performance is that important, get an IO-APIC system
447  * with a pin dedicated to each device.  Or for that matter, an MSI
448  * enabled system.
449  */
450
451 #define ACPI_MAX_IRQS           256
452 #define ACPI_MAX_ISA_IRQ        16
453
454 #define PIRQ_PENALTY_PCI_AVAILABLE      (0)
455 #define PIRQ_PENALTY_PCI_POSSIBLE       (16*16)
456 #define PIRQ_PENALTY_PCI_USING          (16*16*16)
457 #define PIRQ_PENALTY_ISA_TYPICAL        (16*16*16*16)
458 #define PIRQ_PENALTY_ISA_USED           (16*16*16*16*16)
459 #define PIRQ_PENALTY_ISA_ALWAYS         (16*16*16*16*16*16)
460
461 static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
462         PIRQ_PENALTY_ISA_ALWAYS,        /* IRQ0 timer */
463         PIRQ_PENALTY_ISA_ALWAYS,        /* IRQ1 keyboard */
464         PIRQ_PENALTY_ISA_ALWAYS,        /* IRQ2 cascade */
465         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ3 serial */
466         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ4 serial */
467         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ5 sometimes SoundBlaster */
468         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ6 */
469         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ7 parallel, spurious */
470         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ8 rtc, sometimes */
471         PIRQ_PENALTY_PCI_AVAILABLE,     /* IRQ9  PCI, often acpi */
472         PIRQ_PENALTY_PCI_AVAILABLE,     /* IRQ10 PCI */
473         PIRQ_PENALTY_PCI_AVAILABLE,     /* IRQ11 PCI */
474         PIRQ_PENALTY_ISA_USED,  /* IRQ12 mouse */
475         PIRQ_PENALTY_ISA_USED,  /* IRQ13 fpe, sometimes */
476         PIRQ_PENALTY_ISA_USED,  /* IRQ14 ide0 */
477         PIRQ_PENALTY_ISA_USED,  /* IRQ15 ide1 */
478                         /* >IRQ15 */
479 };
480
481 int __init
482 acpi_irq_penalty_init(void)
483 {
484         struct list_head        *node = NULL;
485         struct acpi_pci_link    *link = NULL;
486         int                     i = 0;
487
488         ACPI_FUNCTION_TRACE("acpi_irq_penalty_init");
489
490         /*
491          * Update penalties to facilitate IRQ balancing.
492          */
493         list_for_each(node, &acpi_link.entries) {
494
495                 link = list_entry(node, struct acpi_pci_link, node);
496                 if (!link) {
497                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
498                         continue;
499                 }
500
501                 /*
502                  * reflect the possible and active irqs in the penalty table --
503                  * useful for breaking ties.
504                  */
505                 if (link->irq.possible_count) {
506                         int penalty = PIRQ_PENALTY_PCI_POSSIBLE / link->irq.possible_count;
507
508                         for (i = 0; i < link->irq.possible_count; i++) {
509                                 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
510                                         acpi_irq_penalty[link->irq.possible[i]] += penalty;
511                         }
512
513                 } else if (link->irq.active) {
514                         acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_POSSIBLE;
515                 }
516         }
517         /* Add a penalty for the SCI */
518         acpi_irq_penalty[acpi_fadt.sci_int] += PIRQ_PENALTY_PCI_USING;
519
520         return_VALUE(0);
521 }
522
523 static int acpi_irq_balance;    /* 0: static, 1: balance */
524
525 static int acpi_pci_link_allocate(
526         struct acpi_pci_link    *link)
527 {
528         int                     irq;
529         int                     i;
530
531         ACPI_FUNCTION_TRACE("acpi_pci_link_allocate");
532
533         if (link->irq.initialized)
534                 return_VALUE(0);
535
536         /*
537          * search for active IRQ in list of possible IRQs.
538          */
539         for (i = 0; i < link->irq.possible_count; ++i) {
540                 if (link->irq.active == link->irq.possible[i])
541                         break;
542         }
543         /*
544          * forget active IRQ that is not in possible list
545          */
546         if (i == link->irq.possible_count) {
547                 if (acpi_strict)
548                         printk(KERN_WARNING PREFIX "_CRS %d not found"
549                                 " in _PRS\n", link->irq.active);
550                 link->irq.active = 0;
551         }
552
553         /*
554          * if active found, use it; else pick entry from end of possible list.
555          */
556         if (link->irq.active) {
557                 irq = link->irq.active;
558         } else {
559                 irq = link->irq.possible[link->irq.possible_count - 1];
560         }
561
562         if (acpi_irq_balance || !link->irq.active) {
563                 /*
564                  * Select the best IRQ.  This is done in reverse to promote
565                  * the use of IRQs 9, 10, 11, and >15.
566                  */
567                 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
568                         if (acpi_irq_penalty[irq] > acpi_irq_penalty[link->irq.possible[i]])
569                                 irq = link->irq.possible[i];
570                 }
571         }
572
573         /* Attempt to enable the link device at this IRQ. */
574         if (acpi_pci_link_set(link, irq)) {
575                 printk(PREFIX "Unable to set IRQ for %s [%s] (likely buggy ACPI BIOS).\n"
576                                 "Try pci=noacpi or acpi=off\n",
577                         acpi_device_name(link->device),
578                         acpi_device_bid(link->device));
579                 return_VALUE(-ENODEV);
580         } else {
581                 acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
582                 printk(PREFIX "%s [%s] enabled at IRQ %d\n", 
583                         acpi_device_name(link->device),
584                         acpi_device_bid(link->device), link->irq.active);
585         }
586
587         link->irq.initialized = 1;
588
589         return_VALUE(0);
590 }
591
592 /*
593  * acpi_pci_link_get_irq
594  * success: return IRQ >= 0
595  * failure: return -1
596  */
597
598 int
599 acpi_pci_link_get_irq (
600         acpi_handle             handle,
601         int                     index,
602         int                     *edge_level,
603         int                     *active_high_low,
604         char                    **name)
605 {
606         int                     result = 0;
607         struct acpi_device      *device = NULL;
608         struct acpi_pci_link    *link = NULL;
609
610         ACPI_FUNCTION_TRACE("acpi_pci_link_get_irq");
611
612         result = acpi_bus_get_device(handle, &device);
613         if (result) {
614                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link device\n"));
615                 return_VALUE(-1);
616         }
617
618         link = (struct acpi_pci_link *) acpi_driver_data(device);
619         if (!link) {
620                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
621                 return_VALUE(-1);
622         }
623
624         /* TBD: Support multiple index (IRQ) entries per Link Device */
625         if (index) {
626                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid index %d\n", index));
627                 return_VALUE(-1);
628         }
629
630         if (acpi_pci_link_allocate(link))
631                 return_VALUE(-1);
632            
633         if (!link->irq.active) {
634                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Link active IRQ is 0!\n"));
635                 return_VALUE(-1);
636         }
637
638         if (edge_level) *edge_level = link->irq.edge_level;
639         if (active_high_low) *active_high_low = link->irq.active_high_low;
640         if (name) *name = acpi_device_bid(link->device);
641         return_VALUE(link->irq.active);
642 }
643
644
645 /* --------------------------------------------------------------------------
646                                  Driver Interface
647    -------------------------------------------------------------------------- */
648
649 static int
650 acpi_pci_link_add (
651         struct acpi_device *device)
652 {
653         int                     result = 0;
654         struct acpi_pci_link    *link = NULL;
655         int                     i = 0;
656         int                     found = 0;
657
658         ACPI_FUNCTION_TRACE("acpi_pci_link_add");
659
660         if (!device)
661                 return_VALUE(-EINVAL);
662
663         link = kmalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
664         if (!link)
665                 return_VALUE(-ENOMEM);
666         memset(link, 0, sizeof(struct acpi_pci_link));
667
668         link->device = device;
669         link->handle = device->handle;
670         strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
671         strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
672         acpi_driver_data(device) = link;
673
674         result = acpi_pci_link_get_possible(link);
675         if (result)
676                 goto end;
677
678         /* query and set link->irq.active */
679         acpi_pci_link_get_current(link);
680
681         printk(PREFIX "%s [%s] (IRQs", acpi_device_name(device),
682                 acpi_device_bid(device));
683         for (i = 0; i < link->irq.possible_count; i++) {
684                 if (link->irq.active == link->irq.possible[i]) {
685                         printk(" *%d", link->irq.possible[i]);
686                         found = 1;
687                 }
688                 else
689                         printk(" %d", link->irq.possible[i]);
690         }
691
692         printk(")");
693
694         if (!found)
695                 printk(" *%d", link->irq.active);
696
697         if(!link->device->status.enabled)
698                 printk(", disabled.");
699
700         printk("\n");
701
702         /* TBD: Acquire/release lock */
703         list_add_tail(&link->node, &acpi_link.entries);
704         acpi_link.count++;
705
706 end:
707         /* disable all links -- to be activated on use */
708         acpi_ut_evaluate_object(link->handle, "_DIS", 0, NULL);
709
710         if (result)
711                 kfree(link);
712
713         return_VALUE(result);
714 }
715
716
717 static int
718 acpi_pci_link_resume (
719         struct acpi_pci_link    *link)
720 {
721         ACPI_FUNCTION_TRACE("acpi_pci_link_resume");
722         
723         if (link->irq.active && link->irq.initialized)
724                 return_VALUE(acpi_pci_link_set(link, link->irq.active));
725         else
726                 return_VALUE(0);
727 }
728
729
730 static int
731 irqrouter_resume(
732         struct sys_device *dev)
733 {
734         struct list_head        *node = NULL;
735         struct acpi_pci_link    *link = NULL;
736
737         ACPI_FUNCTION_TRACE("irqrouter_resume");
738
739         list_for_each(node, &acpi_link.entries) {
740
741                 link = list_entry(node, struct acpi_pci_link, node);
742                 if (!link) {
743                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
744                         continue;
745                 }
746
747                 acpi_pci_link_resume(link);
748         }
749         return_VALUE(0);
750 }
751
752
753 static int
754 acpi_pci_link_remove (
755         struct acpi_device      *device,
756         int                     type)
757 {
758         struct acpi_pci_link *link = NULL;
759
760         ACPI_FUNCTION_TRACE("acpi_pci_link_remove");
761
762         if (!device || !acpi_driver_data(device))
763                 return_VALUE(-EINVAL);
764
765         link = (struct acpi_pci_link *) acpi_driver_data(device);
766
767         /* TBD: Acquire/release lock */
768         list_del(&link->node);
769
770         kfree(link);
771
772         return_VALUE(0);
773 }
774
775 /*
776  * modify acpi_irq_penalty[] from cmdline
777  */
778 static int __init acpi_irq_penalty_update(char *str, int used)
779 {
780         int i;
781
782         for (i = 0; i < 16; i++) {
783                 int retval;
784                 int irq;
785
786                 retval = get_option(&str,&irq);
787
788                 if (!retval)
789                         break;  /* no number found */
790
791                 if (irq < 0)
792                         continue;
793                 
794                 if (irq >= ACPI_MAX_IRQS)
795                         continue;
796
797                 if (used)
798                         acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
799                 else
800                         acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;
801
802                 if (retval != 2)        /* no next number */
803                         break;
804         }
805         return 1;
806 }
807
808 /*
809  * We'd like PNP to call this routine for the
810  * single ISA_USED value for each legacy device.
811  * But instead it calls us with each POSSIBLE setting.
812  * There is no ISA_POSSIBLE weight, so we simply use
813  * the (small) PCI_USING penalty.
814  */
815 void acpi_penalize_isa_irq(int irq)
816 {
817         acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
818 }
819
820 /*
821  * Over-ride default table to reserve additional IRQs for use by ISA
822  * e.g. acpi_irq_isa=5
823  * Useful for telling ACPI how not to interfere with your ISA sound card.
824  */
825 static int __init acpi_irq_isa(char *str)
826 {
827         return acpi_irq_penalty_update(str, 1);
828 }
829 __setup("acpi_irq_isa=", acpi_irq_isa);
830
831 /*
832  * Over-ride default table to free additional IRQs for use by PCI
833  * e.g. acpi_irq_pci=7,15
834  * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
835  */
836 static int __init acpi_irq_pci(char *str)
837 {
838         return acpi_irq_penalty_update(str, 0);
839 }
840 __setup("acpi_irq_pci=", acpi_irq_pci);
841
842 static int __init acpi_irq_nobalance_set(char *str)
843 {
844         acpi_irq_balance = 0;
845         return 1;
846 }
847 __setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
848
849 int __init acpi_irq_balance_set(char *str)
850 {
851         acpi_irq_balance = 1;
852         return 1;
853 }
854 __setup("acpi_irq_balance", acpi_irq_balance_set);
855
856
857 static struct sysdev_class irqrouter_sysdev_class = {
858         set_kset_name("irqrouter"),
859         .resume = irqrouter_resume,
860 };
861
862
863 static struct sys_device device_irqrouter = {
864         .id     = 0,
865         .cls    = &irqrouter_sysdev_class,
866 };
867
868
869 static int __init irqrouter_init_sysfs(void)
870 {
871         int error;
872
873         ACPI_FUNCTION_TRACE("irqrouter_init_sysfs");
874
875         if (acpi_disabled || acpi_noirq)
876                 return_VALUE(0);
877
878         error = sysdev_class_register(&irqrouter_sysdev_class);
879         if (!error)
880                 error = sysdev_register(&device_irqrouter);
881
882         return_VALUE(error);
883 }                                        
884
885 device_initcall(irqrouter_init_sysfs);
886
887
888 static int __init acpi_pci_link_init (void)
889 {
890         ACPI_FUNCTION_TRACE("acpi_pci_link_init");
891
892         if (acpi_noirq)
893                 return_VALUE(0);
894
895         acpi_link.count = 0;
896         INIT_LIST_HEAD(&acpi_link.entries);
897
898         if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
899                 return_VALUE(-ENODEV);
900
901         return_VALUE(0);
902 }
903
904 subsys_initcall(acpi_pci_link_init);