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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/* $NetBSD: gpiosim.c,v 1.24 2023/05/10 00:09:39 riastradh Exp $ */
/* $OpenBSD: gpiosim.c,v 1.1 2008/11/23 18:46:49 mbalmer Exp $ */
/*
* Copyright (c) 2007 - 2011, 2013 Marc Balmer <marc@msys.ch>
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* 64 bit wide GPIO simulator */
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/gpio.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <sys/ioccom.h>
#include <dev/gpio/gpiovar.h>
#include "gpiosim.h"
#include "ioconf.h"
#define GPIOSIM_NPINS 64
struct gpiosim_softc {
device_t sc_dev;
device_t sc_gdev; /* gpio that attaches here */
uint64_t sc_state;
struct gpio_chipset_tag sc_gpio_gc;
gpio_pin_t sc_gpio_pins[GPIOSIM_NPINS];
struct sysctllog *sc_log;
};
static int gpiosim_match(device_t, cfdata_t, void *);
static void gpiosim_attach(device_t, device_t, void *);
static int gpiosim_detach(device_t, int);
static int gpiosim_sysctl(SYSCTLFN_PROTO);
static int gpiosim_pin_read(void *, int);
static void gpiosim_pin_write(void *, int, int);
static void gpiosim_pin_ctl(void *, int, int);
CFATTACH_DECL_NEW(gpiosim, sizeof(struct gpiosim_softc), gpiosim_match,
gpiosim_attach, gpiosim_detach, NULL);
static int
gpiosim_match(device_t parent, cfdata_t match, void *aux)
{
return 1;
}
void
gpiosimattach(int num __unused)
{
cfdata_t cf;
int n, err;
err = config_cfattach_attach(gpiosim_cd.cd_name, &gpiosim_ca);
if (err)
printf("%s: unable to register cfattach\n", gpiosim_cd.cd_name);
for (n = 0; n < NGPIOSIM; n++) {
cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
cf->cf_name = "gpiosim";
cf->cf_atname = "gpiosim";
cf->cf_unit = n;
cf->cf_fstate = FSTATE_NOTFOUND;
config_attach_pseudo(cf);
}
}
static void
gpiosim_attach(device_t parent, device_t self, void *aux)
{
struct gpiosim_softc *sc = device_private(self);
struct gpiobus_attach_args gba;
const struct sysctlnode *node;
int i;
sc->sc_dev = self;
printf("%s", device_xname(sc->sc_dev));
/* initialize pin array */
for (i = 0; i < GPIOSIM_NPINS; i++) {
sc->sc_gpio_pins[i].pin_num = i;
sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN |
GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
/* read initial state */
sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INPUT;
}
sc->sc_state = 0;
/* create controller tag */
sc->sc_gpio_gc.gp_cookie = sc;
sc->sc_gpio_gc.gp_pin_read = gpiosim_pin_read;
sc->sc_gpio_gc.gp_pin_write = gpiosim_pin_write;
sc->sc_gpio_gc.gp_pin_ctl = gpiosim_pin_ctl;
/* gba.gba_name = "gpio"; */
gba.gba_gc = &sc->sc_gpio_gc;
gba.gba_pins = sc->sc_gpio_pins;
gba.gba_npins = GPIOSIM_NPINS;
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
sysctl_createv(&sc->sc_log, 0, NULL, &node,
0,
CTLTYPE_NODE, device_xname(sc->sc_dev),
SYSCTL_DESCR("GPIO simulator"),
NULL, 0, NULL, 0,
CTL_HW, CTL_CREATE, CTL_EOL);
if (node == NULL) {
aprint_error(": can't create sysctl node\n");
return;
}
sysctl_createv(&sc->sc_log, 0, &node, NULL,
CTLFLAG_READWRITE,
CTLTYPE_QUAD, "value",
SYSCTL_DESCR("Current GPIO simulator value"),
gpiosim_sysctl, 0, (void *)sc, 0,
CTL_CREATE, CTL_EOL);
aprint_normal(": simulating %d pins\n", GPIOSIM_NPINS);
sc->sc_gdev = config_found(self, &gba, gpiobus_print, CFARGS_NONE);
}
static int
gpiosim_detach(device_t self, int flags)
{
struct gpiosim_softc *sc = device_private(self);
int error;
/* Detach the gpio driver that attached here */
error = config_detach_children(self, flags);
if (error)
return error;
pmf_device_deregister(self);
if (sc->sc_log != NULL) {
sysctl_teardown(&sc->sc_log);
sc->sc_log = NULL;
}
return 0;
}
static int
gpiosim_sysctl(SYSCTLFN_ARGS)
{
struct sysctlnode node;
struct gpiosim_softc *sc;
uint64_t val, error;
node = *rnode;
sc = node.sysctl_data;
node.sysctl_data = &val;
val = sc->sc_state;
error = sysctl_lookup(SYSCTLFN_CALL(&node));
if (error || newp == NULL)
return error;
sc->sc_state = val;
return 0;
}
static int
gpiosim_pin_read(void *arg, int pin)
{
struct gpiosim_softc *sc = arg;
if (sc->sc_state & (1LL << pin))
return GPIO_PIN_HIGH;
else
return GPIO_PIN_LOW;
}
static void
gpiosim_pin_write(void *arg, int pin, int value)
{
struct gpiosim_softc *sc = arg;
if (value == 0)
sc->sc_state &= ~(1LL << pin);
else
sc->sc_state |= (1LL << pin);
}
static void
gpiosim_pin_ctl(void *arg, int pin, int flags)
{
struct gpiosim_softc *sc = arg;
sc->sc_gpio_pins[pin].pin_flags = flags;
}
MODULE(MODULE_CLASS_DRIVER, gpiosim, "gpio");
#ifdef _MODULE
static const struct cfiattrdata gpiobus_iattrdata = {
"gpiobus", 0, { { NULL, NULL, 0 },}
};
static const struct cfiattrdata *const gpiosim_attrs[] = {
&gpiobus_iattrdata, NULL
};
CFDRIVER_DECL(gpiosim, DV_DULL, gpiosim_attrs);
extern struct cfattach gpiosim_ca;
static int gpiosimloc[] = {
-1,
-1,
-1
};
static struct cfdata gpiosim_cfdata[] = {
{
.cf_name = "gpiosim",
.cf_atname = "gpiosim",
.cf_unit = 0,
.cf_fstate = FSTATE_STAR,
.cf_loc = gpiosimloc,
.cf_flags = 0,
.cf_pspec = NULL,
},
{ NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
};
#endif
static int
gpiosim_modcmd(modcmd_t cmd, void *opaque)
{
#ifdef _MODULE
int error = 0;
#endif
switch (cmd) {
case MODULE_CMD_INIT:
#ifdef _MODULE
error = config_cfdriver_attach(&gpiosim_cd);
if (error)
return error;
error = config_cfattach_attach(gpiosim_cd.cd_name,
&gpiosim_ca);
if (error) {
config_cfdriver_detach(&gpiosim_cd);
aprint_error("%s: unable to register cfattach\n",
gpiosim_cd.cd_name);
return error;
}
error = config_cfdata_attach(gpiosim_cfdata, 1);
if (error) {
config_cfattach_detach(gpiosim_cd.cd_name,
&gpiosim_ca);
config_cfdriver_detach(&gpiosim_cd);
aprint_error("%s: unable to register cfdata\n",
gpiosim_cd.cd_name);
return error;
}
config_attach_pseudo(gpiosim_cfdata);
#endif
return 0;
case MODULE_CMD_FINI:
#ifdef _MODULE
error = config_cfdata_detach(gpiosim_cfdata);
if (error)
return error;
config_cfattach_detach(gpiosim_cd.cd_name, &gpiosim_ca);
config_cfdriver_detach(&gpiosim_cd);
#endif
return 0;
case MODULE_CMD_AUTOUNLOAD:
/* no auto-unload */
return EBUSY;
default:
return ENOTTY;
}
}
|