]> mj.ucw.cz Git - pciutils.git/blob - lib/obsd-device.c
libpci: Update manpage documentation for devmem.path
[pciutils.git] / lib / obsd-device.c
1 /*
2  *      The PCI Library -- OpenBSD /dev/pci access
3  *
4  *      Adapted from fbsd-device.c by Matthieu Herrb <matthieu.herrb@laas.fr>, 2006
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL v2+.
7  *
8  *      SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include <fcntl.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <sys/endian.h>
16 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/pciio.h>
19 #include "internal.h"
20
21 static void
22 obsd_config(struct pci_access *a)
23 {
24   pci_define_param(a, "obsd.path", PCI_PATH_OBSD_DEVICE, "Path to the OpenBSD PCI device");
25 }
26
27 static int
28 obsd_detect(struct pci_access *a)
29 {
30   char *name = pci_get_param(a, "obsd.path");
31
32   if (access(name, R_OK))
33     {
34       a->warning("Cannot open %s", name);
35       return 0;
36     }
37   a->debug("...using %s", name);
38   return 1;
39 }
40
41 static void
42 obsd_init(struct pci_access *a)
43 {
44   char *name = pci_get_param(a, "obsd.path");
45
46   a->fd = open(name, O_RDWR, 0);
47   if (a->fd < 0)
48     a->error("obsd_init: %s open failed", name);
49 }
50
51 static void
52 obsd_cleanup(struct pci_access *a)
53 {
54   close(a->fd);
55 }
56
57 static int
58 obsd_read(struct pci_dev *d, int pos, byte *buf, int len)
59 {
60   struct pci_io pi;
61   union {
62           u_int32_t u32;
63           u_int16_t u16[2];
64           u_int8_t u8[4];
65   } u;
66
67   if (!(len == 1 || len == 2 || len == 4))
68     return pci_generic_block_read(d, pos, buf, len);
69
70   if (d->domain || pos >= 256)
71     return 0;
72
73   pi.pi_sel.pc_bus = d->bus;
74   pi.pi_sel.pc_dev = d->dev;
75   pi.pi_sel.pc_func = d->func;
76
77   pi.pi_reg = pos - (pos % 4);
78   pi.pi_width = 4;
79
80   if (ioctl(d->access->fd, PCIOCREAD, &pi) < 0) {
81           if (errno == ENXIO)
82                   pi.pi_data = 0xffffffff;
83           else
84                   d->access->error("obsd_read: ioctl(PCIOCREAD) failed");
85   }
86   u.u32 = pi.pi_data;
87
88   switch (len)
89     {
90     case 1:
91       buf[0] = (u8) u.u8[pos % 4];
92       break;
93     case 2:
94       ((u16 *) buf)[0] = letoh16(u.u16[(pos % 4) / 2]);
95       break;
96     case 4:
97       ((u32 *) buf)[0] = (u32) letoh32(pi.pi_data);
98       break;
99     }
100   return 1;
101 }
102
103 static int
104 obsd_write(struct pci_dev *d, int pos, byte *buf, int len)
105 {
106   struct pci_io pi;
107
108   if (!(len == 1 || len == 2 || len == 4))
109     return pci_generic_block_write(d, pos, buf, len);
110
111   if (d->domain || pos >= 256)
112     return 0;
113
114   pi.pi_sel.pc_bus = d->bus;
115   pi.pi_sel.pc_dev = d->dev;
116   pi.pi_sel.pc_func = d->func;
117
118   pi.pi_reg = pos;
119   pi.pi_width = len;
120
121   switch (len)
122     {
123     case 1:
124       pi.pi_data = buf[0];
125       break;
126     case 2:
127       pi.pi_data = ((u16 *) buf)[0];
128       break;
129     case 4:
130       pi.pi_data = ((u32 *) buf)[0];
131       break;
132     }
133
134   if (ioctl(d->access->fd, PCIOCWRITE, &pi) < 0)
135     d->access->error("obsd_write: ioctl(PCIOCWRITE) failed");
136
137   return 1;
138 }
139
140 struct pci_methods pm_obsd_device = {
141   .name = "obsd-device",
142   .help = "/dev/pci on OpenBSD",
143   .config = obsd_config,
144   .detect = obsd_detect,
145   .init = obsd_init,
146   .cleanup = obsd_cleanup,
147   .scan = pci_generic_scan,
148   .fill_info = pci_generic_fill_info,
149   .read = obsd_read,
150   .write = obsd_write,
151 };