aboutsummaryrefslogtreecommitdiffstats
path: root/lib/obsd-device.c
blob: 41f16ced536dde2e76cfe71d0617184726c85f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 *	The PCI Library -- OpenBSD /dev/pci access
 *
 *	Adapted from fbsd-device.c by Matthieu Herrb <matthieu.herrb@laas.fr>, 2006
 *
 *	Can be freely distributed and used under the terms of the GNU GPL v2+.
 *
 *	SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/endian.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/pciio.h>
#include "internal.h"

static void
obsd_config(struct pci_access *a)
{
  pci_define_param(a, "obsd.path", PCI_PATH_OBSD_DEVICE, "Path to the OpenBSD PCI device");
}

static int
obsd_detect(struct pci_access *a)
{
  char *name = pci_get_param(a, "obsd.path");

  if (access(name, R_OK))
    {
      a->warning("Cannot open %s", name);
      return 0;
    }
  a->debug("...using %s", name);
  return 1;
}

static void
obsd_init(struct pci_access *a)
{
  char *name = pci_get_param(a, "obsd.path");

  a->fd = open(name, O_RDWR, 0);
  if (a->fd < 0)
    a->error("obsd_init: %s open failed", name);
}

static void
obsd_cleanup(struct pci_access *a)
{
  close(a->fd);
}

static int
obsd_read(struct pci_dev *d, int pos, byte *buf, int len)
{
  struct pci_io pi;
  union {
	  u_int32_t u32;
	  u_int16_t u16[2];
	  u_int8_t u8[4];
  } u;

  if (!(len == 1 || len == 2 || len == 4))
    return pci_generic_block_read(d, pos, buf, len);

  if (d->domain || pos >= 256)
    return 0;

  pi.pi_sel.pc_bus = d->bus;
  pi.pi_sel.pc_dev = d->dev;
  pi.pi_sel.pc_func = d->func;

  pi.pi_reg = pos - (pos % 4);
  pi.pi_width = 4;

  if (ioctl(d->access->fd, PCIOCREAD, &pi) < 0) {
	  if (errno == ENXIO)
		  pi.pi_data = 0xffffffff;
	  else
		  d->access->error("obsd_read: ioctl(PCIOCREAD) failed");
  }
  u.u32 = pi.pi_data;

  switch (len)
    {
    case 1:
      buf[0] = (u8) u.u8[pos % 4];
      break;
    case 2:
      ((u16 *) buf)[0] = letoh16(u.u16[(pos % 4) / 2]);
      break;
    case 4:
      ((u32 *) buf)[0] = (u32) letoh32(pi.pi_data);
      break;
    }
  return 1;
}

static int
obsd_write(struct pci_dev *d, int pos, byte *buf, int len)
{
  struct pci_io pi;

  if (!(len == 1 || len == 2 || len == 4))
    return pci_generic_block_write(d, pos, buf, len);

  if (d->domain || pos >= 256)
    return 0;

  pi.pi_sel.pc_bus = d->bus;
  pi.pi_sel.pc_dev = d->dev;
  pi.pi_sel.pc_func = d->func;

  pi.pi_reg = pos;
  pi.pi_width = len;

  switch (len)
    {
    case 1:
      pi.pi_data = buf[0];
      break;
    case 2:
      pi.pi_data = ((u16 *) buf)[0];
      break;
    case 4:
      pi.pi_data = ((u32 *) buf)[0];
      break;
    }

  if (ioctl(d->access->fd, PCIOCWRITE, &pi) < 0)
    d->access->error("obsd_write: ioctl(PCIOCWRITE) failed");

  return 1;
}

struct pci_methods pm_obsd_device = {
  .name = "obsd-device",
  .help = "/dev/pci on OpenBSD",
  .config = obsd_config,
  .detect = obsd_detect,
  .init = obsd_init,
  .cleanup = obsd_cleanup,
  .scan = pci_generic_scan,
  .fill_info = pci_generic_fill_info,
  .read = obsd_read,
  .write = obsd_write,
};