]> mj.ucw.cz Git - pciutils.git/blob - lib/fbsd-device.c
d6f35bdf08ba50037010f270efaa70416912b18b
[pciutils.git] / lib / fbsd-device.c
1 /*
2  *      The PCI Library -- FreeBSD /dev/pci access
3  *
4  *      Copyright (c) 1999 Jari Kirma <kirma@cs.hut.fi>
5  *      Updated in 2003 by Samy Al Bahra <samy@kerneled.com>
6  *
7  *      Can be freely distributed and used under the terms of the GNU GPL.
8  */
9
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <osreldate.h>
16 #include <stdint.h>
17
18 #ifdef __FreeBSD_kernel_version
19 #  ifndef __FreeBSD_version
20 #    define __FreeBSD_version __FreeBSD_kernel_version
21 #  endif
22 #endif
23
24 #if __FreeBSD_version < 430000 && !defined(__DragonFly__)
25 #  include <pci/pcivar.h>
26 #  include <pci/pci_ioctl.h>
27 #else
28 #  include <sys/pciio.h>
29 #endif
30
31 #include "internal.h"
32
33 static void
34 fbsd_config(struct pci_access *a)
35 {
36   a->method_params[PCI_ACCESS_FBSD_DEVICE] = PCI_PATH_FBSD_DEVICE;
37 }
38
39 static int
40 fbsd_detect(struct pci_access *a)
41 {
42   char *name = a->method_params[PCI_ACCESS_FBSD_DEVICE];
43
44   if (access(name, R_OK))
45     {
46       a->warning("Cannot open %s", name);
47       return 0;
48     }
49   a->debug("...using %s", name);
50   return 1;
51 }
52
53 static void
54 fbsd_init(struct pci_access *a)
55 {
56   char *name = a->method_params[PCI_ACCESS_FBSD_DEVICE];
57
58   a->fd = open(name, O_RDWR, 0);
59   if (a->fd < 0)
60     {
61       a->error("fbsd_init: %s open failed", name);
62     }
63 }
64
65 static void
66 fbsd_cleanup(struct pci_access *a)
67 {
68   close(a->fd);
69 }
70
71 static int
72 fbsd_read(struct pci_dev *d, int pos, byte *buf, int len)
73 {
74   struct pci_io pi;
75
76   if (!(len == 1 || len == 2 || len == 4))
77     {
78       return pci_generic_block_read(d, pos, buf, len);
79     }
80
81   if (pos >= 256)
82     return 0;
83
84 #if __FreeBSD_version >= 700053
85   pi.pi_sel.pc_domain = d->domain;
86 #endif
87   pi.pi_sel.pc_bus = d->bus;
88   pi.pi_sel.pc_dev = d->dev;
89   pi.pi_sel.pc_func = d->func;
90
91   pi.pi_reg = pos;
92   pi.pi_width = len;
93
94   if (ioctl(d->access->fd, PCIOCREAD, &pi) < 0)
95     {
96       if (errno == ENODEV)
97         {
98           return 0;
99         }
100       d->access->error("fbsd_read: ioctl(PCIOCREAD) failed: %s", strerror(errno));
101     }
102
103   switch (len)
104     {
105     case 1:
106       buf[0] = (u8) pi.pi_data;
107       break;
108     case 2:
109       ((u16 *) buf)[0] = cpu_to_le16((u16) pi.pi_data);
110       break;
111     case 4:
112       ((u32 *) buf)[0] = cpu_to_le32((u32) pi.pi_data);
113       break;
114     }
115   return 1;
116 }
117
118 static int
119 fbsd_write(struct pci_dev *d, int pos, byte *buf, int len)
120 {
121   struct pci_io pi;
122
123   if (!(len == 1 || len == 2 || len == 4))
124     {
125       return pci_generic_block_write(d, pos, buf, len);
126     }
127
128   if (pos >= 256)
129     return 0;
130
131 #if __FreeBSD_version >= 700053
132   pi.pi_sel.pc_domain = d->domain;
133 #endif
134   pi.pi_sel.pc_bus = d->bus;
135   pi.pi_sel.pc_dev = d->dev;
136   pi.pi_sel.pc_func = d->func;
137
138   pi.pi_reg = pos;
139   pi.pi_width = len;
140
141   switch (len)
142     {
143     case 1:
144       pi.pi_data = buf[0];
145       break;
146     case 2:
147       pi.pi_data = le16_to_cpu(((u16 *) buf)[0]);
148       break;
149     case 4:
150       pi.pi_data = le32_to_cpu(((u32 *) buf)[0]);
151       break;
152     }
153
154   if (ioctl(d->access->fd, PCIOCWRITE, &pi) < 0)
155     {
156       if (errno == ENODEV)
157         {
158           return 0;
159         }
160       d->access->error("fbsd_write: ioctl(PCIOCWRITE) failed: %s", strerror(errno));
161     }
162
163   return 1;
164 }
165
166 struct pci_methods pm_fbsd_device = {
167   "FreeBSD-device",
168   fbsd_config,
169   fbsd_detect,
170   fbsd_init,
171   fbsd_cleanup,
172   pci_generic_scan,
173   pci_generic_fill_info,
174   fbsd_read,
175   fbsd_write,
176   NULL,                                 /* dev_init */
177   NULL                                  /* dev_cleanup */
178 };