summaryrefslogtreecommitdiffstats
path: root/testsuite/cdev01/test_cdev.c
blob: 19fb1a5ecea37ec0c880f4728ac58db10c93090c (plain) (blame)
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
/*
 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <machine/rtems-bsd-kernel-space.h>
#include <sys/types.h>
#include <sys/conf.h>

#include <rtems/seterr.h>

#include <assert.h>

#include "test_cdev01.h"

static	d_open_t	testopen;
static	d_close_t	testclose;
static	d_read_t	testread;
static	d_write_t	testwrite;
static	d_ioctl_t	testioctl;
static	d_poll_t	testpoll;
static	d_kqfilter_t	testkqfilter;

static struct cdevsw test_cdevsw = {
	.d_version =	D_VERSION,
	.d_flags =	0,
/* FIXME: check for  D_PSEUDO | D_NEEDMINOR | D_NEEDGIANT | D_TRACKCLOSE */
	.d_name =	"test",
	.d_open =	testopen,
	.d_close =	testclose,
	.d_read =	testread,
	.d_write =	testwrite,
	.d_ioctl =	testioctl,
	.d_poll =	testpoll,
	.d_kqfilter =	testkqfilter,
};

static	int
testopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
{
	test_state *state = dev->si_drv1;

	assert(*state == TEST_NEW);
	*state = TEST_OPEN;

	return 0;
}

static	int
testclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
{
	test_state *state = dev->si_drv1;

	assert(*state == TEST_KQFILTER);
	*state = TEST_CLOSED;

	return 0;
}

static	int
testread(struct cdev *dev, struct uio *uio, int ioflag)
{
	test_state *state = dev->si_drv1;

	assert(*state == TEST_OPEN || *state == TEST_IOCTL);
	if(*state == TEST_OPEN) {
		*state = TEST_READ;
	} else {
		*state = TEST_READV;
	}

	return 0;
}

static	int
testwrite(struct cdev *dev, struct uio *uio, int ioflag)
{
	test_state *state = dev->si_drv1;

	assert(*state == TEST_READ || *state == TEST_READV);
	if(*state == TEST_READ) {
		*state = TEST_WRITE;
	} else {
		*state = TEST_WRITEV;
	}

	return 0;
}

static	int
testioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
    struct thread *td)
{
	test_state *state = dev->si_drv1;

	assert(cmd == TEST_IOCTL_CMD);
	assert(*state == TEST_WRITE);
	*state = TEST_IOCTL;

	return 0;
}

static	int
testpoll(struct cdev *dev, int events, struct thread *td)
{
	test_state *state = dev->si_drv1;

	assert(*state == TEST_WRITEV);
	*state = TEST_POLL;

	return 1;
}

static	int
testkqfilter(struct cdev *dev, struct knote *kn)
{
	test_state *state = dev->si_drv1;

	assert(*state == TEST_POLL);
	*state = TEST_KQFILTER;

	return TEST_KQ_ERRNO;
}

void
test_make_dev(test_state *state, const char *name)
{
	struct cdev *dev = NULL;

	dev = make_dev(&test_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, name);
	assert(dev != NULL);
	dev->si_drv1 = state;
}