]> mj.ucw.cz Git - pciutils.git/blob - lib/fbsd-device.c
09041e370b740564b59b8d2da37f396407bbe9f4
[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 <fcntl.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <osreldate.h>
14
15 #if __FreeBSD_version < 500000
16 #  include <pci/pcivar.h>
17 #else
18 #  include <dev/pci/pcivar.h>
19 #endif
20
21 #if __FreeBSD_version < 430000
22 #  include <pci/pci_ioctl.h>
23 #else
24 #  include <sys/pciio.h>
25 #endif
26
27 #include "internal.h"
28
29 static void
30 fbsd_config(struct pci_access *a)
31 {
32   a->method_params[PCI_ACCESS_FBSD_DEVICE] = PATH_FBSD_DEVICE;
33 }
34
35 static int
36 fbsd_detect(struct pci_access *a)
37 {
38   char *name = a->method_params[PCI_ACCESS_FBSD_DEVICE];
39
40   if (access(name, R_OK))
41     {
42       a->warning("Cannot open %s", name);
43       return 0;
44     }
45   a->debug("...using %s", name);
46   return 1;
47 }
48
49 static void
50 fbsd_init(struct pci_access *a)
51 {
52   char *name = a->method_params[PCI_ACCESS_FBSD_DEVICE];
53
54   a->fd = open(name, O_RDWR, 0);
55   if (a->fd < 0)
56     {
57       a->error("fbsd_init: %s open failed", name);
58     }
59 }
60
61 static void
62 fbsd_cleanup(struct pci_access *a)
63 {
64   close(a->fd);
65 }
66
67 static int
68 fbsd_read(struct pci_dev *d, int pos, byte *buf, int len)
69 {
70   struct pci_io pi;
71
72   if (!(len == 1 || len == 2 || len == 4))
73     {
74       return pci_generic_block_read(d, pos, buf, len);
75     }
76
77   pi.pi_sel.pc_bus = d->bus;
78   pi.pi_sel.pc_dev = d->dev;
79   pi.pi_sel.pc_func = d->func;
80
81   pi.pi_reg = pos;
82   pi.pi_width = len;
83         
84   if (ioctl(d->access->fd, PCIOCREAD, &pi) < 0)
85     d->access->error("fbsd_read: ioctl(PCIOCREAD) failed");
86   
87   switch (len)
88     {
89     case 1:
90       buf[0] = (u8) pi.pi_data;
91       break;
92     case 2:
93       ((u16 *) buf)[0] = (u16) pi.pi_data;
94       break;
95     case 4:
96       ((u32 *) buf)[0] = (u32) pi.pi_data;
97       break;
98     }
99   return 1;
100 }
101
102 static int
103 fbsd_write(struct pci_dev *d, int pos, byte *buf, int len)
104 {
105   struct pci_io pi;
106
107   if (!(len == 1 || len == 2 || len == 4))
108     {
109       return pci_generic_block_write(d, pos, buf, len);
110     }
111
112   pi.pi_sel.pc_bus = d->bus;
113   pi.pi_sel.pc_dev = d->dev;
114   pi.pi_sel.pc_func = d->func;
115
116   pi.pi_reg = pos;
117   pi.pi_width = len;
118         
119   switch (len)
120     {
121     case 1:
122       pi.pi_data = buf[0];
123       break;
124     case 2:
125       pi.pi_data = ((u16 *) buf)[0];
126       break;
127     case 4:
128       pi.pi_data = ((u32 *) buf)[0];
129       break;
130     }
131   
132   if (ioctl(d->access->fd, PCIOCWRITE, &pi) < 0)
133     {
134       d->access->error("fbsd_write: ioctl(PCIOCWRITE) failed");
135     }
136
137   return 1;
138 }
139
140 struct pci_methods pm_fbsd_device = {
141   "FreeBSD-device",
142   fbsd_config,
143   fbsd_detect,
144   fbsd_init,
145   fbsd_cleanup,
146   pci_generic_scan,
147   pci_generic_fill_info,
148   fbsd_read,
149   fbsd_write,
150   NULL,                                 /* dev_init */
151   NULL                                  /* dev_cleanup */
152 };