2 * The PCI Library -- OpenBSD /dev/pci access
4 * Adapted from fbsd-device.c by Matthieu Herrb <matthieu.herrb@laas.fr>, 2006
6 * Can be freely distributed and used under the terms of the GNU GPL.
13 #include <sys/endian.h>
14 #include <sys/types.h>
15 #include <sys/ioctl.h>
16 #include <sys/pciio.h>
20 obsd_config(struct pci_access
*a
)
22 pci_define_param(a
, "obsd.path", PCI_PATH_OBSD_DEVICE
, "Path to the OpenBSD PCI device");
26 obsd_detect(struct pci_access
*a
)
28 char *name
= pci_get_param(a
, "obsd.path");
30 if (access(name
, R_OK
))
32 a
->warning("Cannot open %s", name
);
35 a
->debug("...using %s", name
);
40 obsd_init(struct pci_access
*a
)
42 char *name
= pci_get_param(a
, "obsd.path");
44 a
->fd
= open(name
, O_RDWR
, 0);
46 a
->error("obsd_init: %s open failed", name
);
50 obsd_cleanup(struct pci_access
*a
)
56 obsd_read(struct pci_dev
*d
, int pos
, byte
*buf
, int len
)
65 if (!(len
== 1 || len
== 2 || len
== 4))
66 return pci_generic_block_read(d
, pos
, buf
, len
);
71 pi
.pi_sel
.pc_bus
= d
->bus
;
72 pi
.pi_sel
.pc_dev
= d
->dev
;
73 pi
.pi_sel
.pc_func
= d
->func
;
75 pi
.pi_reg
= pos
- (pos
% 4);
78 if (ioctl(d
->access
->fd
, PCIOCREAD
, &pi
) < 0) {
80 pi
.pi_data
= 0xffffffff;
82 d
->access
->error("obsd_read: ioctl(PCIOCREAD) failed");
89 buf
[0] = (u8
) u
.u8
[pos
% 4];
92 ((u16
*) buf
)[0] = letoh16(u
.u16
[(pos
% 4) / 2]);
95 ((u32
*) buf
)[0] = (u32
) letoh32(pi
.pi_data
);
102 obsd_write(struct pci_dev
*d
, int pos
, byte
*buf
, int len
)
106 if (!(len
== 1 || len
== 2 || len
== 4))
107 return pci_generic_block_write(d
, pos
, buf
, len
);
112 pi
.pi_sel
.pc_bus
= d
->bus
;
113 pi
.pi_sel
.pc_dev
= d
->dev
;
114 pi
.pi_sel
.pc_func
= d
->func
;
125 pi
.pi_data
= ((u16
*) buf
)[0];
128 pi
.pi_data
= ((u32
*) buf
)[0];
132 if (ioctl(d
->access
->fd
, PCIOCWRITE
, &pi
) < 0)
133 d
->access
->error("obsd_write: ioctl(PCIOCWRITE) failed");
138 struct pci_methods pm_obsd_device
= {
140 "/dev/pci on OpenBSD",
146 pci_generic_fill_info
,
151 NULL
/* dev_cleanup */