]> mj.ucw.cz Git - pciutils.git/blob - lib/nbsd-libpci.c
a79d878ba4b4d98f6671eb4bb073811bb856fe9f
[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  *      Copyright (c) 2002 Martin Mares <mj@ucw.cz>
8  *
9  *      Can be freely distributed and used under the terms of the GNU GPL.
10  */
11
12 /*
13  *      Read functionality of this driver is briefly tested, and seems
14  *      to supply basic information correctly, but I promise no more.
15  */
16
17 #include <fcntl.h>
18 #include <string.h>
19 #include <unistd.h>
20
21 #include <pci.h>
22
23 #include "internal.h"
24
25 static void
26 nbsd_config(struct pci_access *a)
27 {
28   a->method_params[PCI_ACCESS_NBSD_LIBPCI] = PCI_PATH_NBSD_DEVICE;
29 }
30
31 static int
32 nbsd_detect(struct pci_access *a)
33 {
34   char *name = a->method_params[PCI_ACCESS_NBSD_LIBPCI];
35
36   if (access(name, R_OK))
37     {
38       a->warning("Cannot open %s", name);
39       return 0;
40     }
41
42   if (!access(name, W_OK))
43     {
44       a->writeable = O_RDWR;
45     }
46   a->debug("...using %s", name);
47   return 1;
48 }
49
50 static void
51 nbsd_init(struct pci_access *a)
52 {
53   char *name = a->method_params[PCI_ACCESS_NBSD_LIBPCI];
54   int mode = a->writeable ? O_RDWR : O_RDONLY;
55
56   a->fd = open(name, mode, 0);
57   if (a->fd < 0)
58     a->error("nbsd_init: %s open failed", name);
59 }
60
61 static void
62 nbsd_cleanup(struct pci_access *a)
63 {
64   close(a->fd);
65 }
66
67 static int
68 nbsd_read(struct pci_dev *d, int pos, byte *buf, int len)
69 {
70   pcireg_t val;
71   int shift;
72
73   if (!(len == 1 || len == 2 || len == 4))
74     return pci_generic_block_read(d, pos, buf, len);
75
76   if (pos >= 256)
77     return 0;
78
79   shift = 8*(pos % 4);
80   pos &= ~3;
81
82   if (pcibus_conf_read(d->access->fd, d->bus, d->dev, d->func, pos, &val) < 0)
83     d->access->error("nbsd_read: pci_bus_conf_read() failed");
84
85   switch (len)
86     {
87     case 1:
88       *buf = val >> shift;
89       break;
90     case 2:
91       *(u16*)buf = cpu_to_le16(val >> shift);
92       break;
93     case 4:
94       *(u32*)buf = cpu_to_le32(val);
95       break;
96     }
97   return 1;
98 }
99
100 static int
101 nbsd_write(struct pci_dev *d, int pos, byte *buf, int len)
102 {
103   pcireg_t val = 0;
104   int shift;
105
106   if (!(len == 1 || len == 2 || len == 4))
107     return pci_generic_block_write(d, pos, buf, len);
108
109   if (pos >= 256)
110     return 0;
111
112   /*
113    *  BEWARE: NetBSD seems to support only 32-bit access, so we have
114    *  to emulate byte and word writes by read-modify-write, possibly
115    *  causing troubles.
116    */
117
118   shift = 8*(pos % 4);
119   pos &= ~3;
120   if (len != 4)
121     {
122       if (pcibus_conf_read(d->access->fd, d->bus, d->dev, d->func, pos, &val) < 0)
123         d->access->error("nbsd_write: pci_bus_conf_read() failed");
124     }
125
126   switch (len)
127     {
128     case 1:
129       val = (val & ~(0xff << shift)) | (buf[0] << shift);
130       break;
131     case 2:
132       val = (val & ~(0xffff << shift)) | (le16_to_cpu(*(u16*)buf) << shift);
133       break;
134     case 4:
135       val = le32_to_cpu(*(u32*)buf);
136       break;
137     }
138
139   if (pcibus_conf_write(d->access->fd, d->bus, d->dev, d->func, pos, val) < 0)
140     d->access->error("nbsd_write: pci_bus_conf_write() failed");
141
142   return 1;
143 }
144
145 struct pci_methods pm_nbsd_libpci = {
146   "NetBSD-libpci",
147   nbsd_config,
148   nbsd_detect,
149   nbsd_init,
150   nbsd_cleanup,
151   pci_generic_scan,
152   pci_generic_fill_info,
153   nbsd_read,
154   nbsd_write,
155   NULL,                                 /* dev_init */
156   NULL                                  /* dev_cleanup */
157 };