pci_subr.c revision 1.9 1 /* $NetBSD: pci_subr.c,v 1.9 1995/06/17 22:40:34 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1994 Charles Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * PCI autoconfiguration support
34 */
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/device.h>
39
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcireg.h>
42
43 int
44 pciprint(aux, pci)
45 void *aux;
46 char *pci;
47 {
48 register struct pci_attach_args *pa = aux;
49
50 printf(" bus %d device %d", pa->pa_bus, pa->pa_device);
51 return (UNCONF);
52 }
53
54 int
55 pcisubmatch(parent, match, aux)
56 struct device *parent;
57 void *match, *aux;
58 {
59 struct cfdata *cf = match;
60 struct pci_attach_args *pa = aux;
61
62 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != pa->pa_bus)
63 return 0;
64 if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != pa->pa_device)
65 return 0;
66 return ((*cf->cf_driver->cd_match)(parent, match, aux));
67 }
68
69 /*
70 * Try to find and attach the PCI device at the give bus and device number.
71 * Return 1 if successful, 0 if unsuccessful.
72 */
73 int
74 pci_attach_subdev(pcidev, bus, device)
75 struct device *pcidev;
76 int bus, device;
77 {
78 pcitag_t tag;
79 pcireg_t id, class;
80 struct pci_attach_args pa;
81 struct cfdata *cf;
82
83 tag = pci_make_tag(bus, device, 0);
84 id = pci_conf_read(tag, PCI_ID_REG);
85 if (id == 0 || id == 0xffffffff)
86 return (0);
87 class = pci_conf_read(tag, PCI_CLASS_REG);
88
89 pa.pa_bus = bus;
90 pa.pa_device = device;
91 pa.pa_tag = tag;
92 pa.pa_id = id;
93 pa.pa_class = class;
94
95 if ((cf = config_search(pcisubmatch, pcidev, &pa)) != NULL)
96 config_attach(pcidev, cf, &pa, pciprint);
97 else {
98 printf("%s bus %d device %d: identifier %08x class %08x%s",
99 pcidev->dv_xname, bus, device, id, class,
100 " not configured\n");
101 return (0);
102 }
103
104 return (1);
105 }
106