]> mj.ucw.cz Git - pciutils.git/blob - lib/nbsd-libpci.c
Initial NetBSD support.
[pciutils.git] / lib / nbsd-libpci.c
1 /*
2  *      The PCI Library -- NetBSD libpci access
3  *         (based on FreeBSD /dev/pci access)
4  *
5  *      Copyright (c) 1999 Jari Kirma <kirma@cs.hut.fi>
6  *      Copyright (c) 2002 Quentin Garnier <cube@cubidou.net>
7  *
8  *      Can be freely distributed and used under the terms of the GNU GPL.
9  */
10
11 /*
12  *      Read functionality of this driver is briefly tested, and seems
13  *      to supply basic information correctly, but I promise no more.
14  */
15
16 #include <fcntl.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include <pci.h>
21
22 #include "internal.h"
23
24 static void
25 nbsd_config(struct pci_access *a)
26 {
27   a->method_params[PCI_ACCESS_NBSD_LIBPCI] = PATH_NBSD_DEVICE;
28 }
29
30 static int
31 nbsd_detect(struct pci_access *a)
32 {
33   char *name = a->method_params[PCI_ACCESS_NBSD_LIBPCI];
34
35   if (access(name, R_OK))
36     {
37       a->warning("Cannot open %s", name);
38       return 0;
39     }
40   a->debug("...using %s", name);
41   return 1;
42 }
43
44 static void
45 nbsd_init(struct pci_access *a)
46 {
47   char *name = a->method_params[PCI_ACCESS_NBSD_LIBPCI];
48
49   a->fd = open(name, O_RDWR, 0);
50   if (a->fd < 0)
51     {
52       a->error("nbsd_init: %s open failed", name);
53     }
54 }
55
56 static void
57 nbsd_cleanup(struct pci_access *a)
58 {
59   close(a->fd);
60 }
61
62 static int
63 nbsd_read(struct pci_dev *d, int pos, byte *buf, int len)
64 {
65   pcireg_t val;
66
67   if (!(len == 1 || len == 2 || len == 4))
68     {
69       return pci_generic_block_read(d, pos, buf, len);
70     }
71
72         
73   if (pcibus_conf_read(d->access->fd, d->bus, d->dev, d->func, pos, &val) < 0)
74     d->access->error("nbsd_read: pci_bus_conf_read() failed");
75   
76   switch (len)
77     {
78     case 1:
79       buf[0] = (u8) ((val>>16) & 0xff);
80       break;
81     case 2:
82       ((u16 *) buf)[0] = (u16) val;
83       break;
84     case 4:
85       ((u32 *) buf)[0] = (u32) val;
86       break;
87     }
88   return 1;
89 }
90
91 static int
92 nbsd_write(struct pci_dev *d, int pos, byte *buf, int len)
93 {
94   pcireg_t val;
95
96   if (!(len == 1 || len == 2 || len == 4))
97     {
98       return pci_generic_block_write(d, pos, buf, len);
99     }
100
101   switch (len)
102     {
103     case 1:
104       val = buf[0];
105       break;
106     case 2:
107       val = ((u16 *) buf)[0];
108       break;
109     case 4:
110       val = ((u32 *) buf)[0];
111       break;
112     }
113   
114   if (pcibus_conf_write(d->access->fd, d->bus, d->dev, d->func, pos, val) < 0)
115     d->access->error("nbsd_write: pci_bus_conf_write() failed");
116
117   return 1;
118 }
119
120 struct pci_methods pm_nbsd_libpci = {
121   "NetBSD-libpci",
122   nbsd_config,
123   nbsd_detect,
124   nbsd_init,
125   nbsd_cleanup,
126   pci_generic_scan,
127   pci_generic_fill_info,
128   nbsd_read,
129   nbsd_write,
130   NULL,                                 /* dev_init */
131   NULL                                  /* dev_cleanup */
132 };