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