1 | /* |
---|
2 | * Copyright (c) 2016 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * Redistribution and use in source and binary forms, with or without |
---|
11 | * modification, are permitted provided that the following conditions |
---|
12 | * are met: |
---|
13 | * 1. Redistributions of source code must retain the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer. |
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer in the |
---|
17 | * documentation and/or other materials provided with the distribution. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | */ |
---|
31 | |
---|
32 | #include <machine/rtems-bsd-kernel-space.h> |
---|
33 | #include <sys/types.h> |
---|
34 | #include <sys/conf.h> |
---|
35 | |
---|
36 | #include <rtems/seterr.h> |
---|
37 | |
---|
38 | #include <assert.h> |
---|
39 | |
---|
40 | #include "test_cdev01.h" |
---|
41 | |
---|
42 | static d_open_t testopen; |
---|
43 | static d_close_t testclose; |
---|
44 | static d_read_t testread; |
---|
45 | static d_write_t testwrite; |
---|
46 | static d_ioctl_t testioctl; |
---|
47 | static d_poll_t testpoll; |
---|
48 | static d_kqfilter_t testkqfilter; |
---|
49 | |
---|
50 | static struct cdevsw test_cdevsw = { |
---|
51 | .d_version = D_VERSION, |
---|
52 | .d_flags = 0, |
---|
53 | /* FIXME: check for D_PSEUDO | D_NEEDMINOR | D_NEEDGIANT | D_TRACKCLOSE */ |
---|
54 | .d_name = "test", |
---|
55 | .d_open = testopen, |
---|
56 | .d_close = testclose, |
---|
57 | .d_read = testread, |
---|
58 | .d_write = testwrite, |
---|
59 | .d_ioctl = testioctl, |
---|
60 | .d_poll = testpoll, |
---|
61 | .d_kqfilter = testkqfilter, |
---|
62 | }; |
---|
63 | |
---|
64 | static int |
---|
65 | testopen(struct cdev *dev, int oflags, int devtype, struct thread *td) |
---|
66 | { |
---|
67 | test_state *state = dev->si_drv1; |
---|
68 | |
---|
69 | assert(*state == TEST_NEW); |
---|
70 | *state = TEST_OPEN; |
---|
71 | |
---|
72 | return 0; |
---|
73 | } |
---|
74 | |
---|
75 | static int |
---|
76 | testclose(struct cdev *dev, int fflag, int devtype, struct thread *td) |
---|
77 | { |
---|
78 | test_state *state = dev->si_drv1; |
---|
79 | |
---|
80 | assert(*state == TEST_KQFILTER); |
---|
81 | *state = TEST_CLOSED; |
---|
82 | |
---|
83 | return 0; |
---|
84 | } |
---|
85 | |
---|
86 | static int |
---|
87 | testread(struct cdev *dev, struct uio *uio, int ioflag) |
---|
88 | { |
---|
89 | test_state *state = dev->si_drv1; |
---|
90 | |
---|
91 | assert(*state == TEST_OPEN || *state == TEST_IOCTL); |
---|
92 | if(*state == TEST_OPEN) { |
---|
93 | *state = TEST_READ; |
---|
94 | } else { |
---|
95 | *state = TEST_READV; |
---|
96 | } |
---|
97 | |
---|
98 | return 0; |
---|
99 | } |
---|
100 | |
---|
101 | static int |
---|
102 | testwrite(struct cdev *dev, struct uio *uio, int ioflag) |
---|
103 | { |
---|
104 | test_state *state = dev->si_drv1; |
---|
105 | |
---|
106 | assert(*state == TEST_READ || *state == TEST_READV); |
---|
107 | if(*state == TEST_READ) { |
---|
108 | *state = TEST_WRITE; |
---|
109 | } else { |
---|
110 | *state = TEST_WRITEV; |
---|
111 | } |
---|
112 | |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | static int |
---|
117 | testioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, |
---|
118 | struct thread *td) |
---|
119 | { |
---|
120 | test_state *state = dev->si_drv1; |
---|
121 | |
---|
122 | assert(cmd == TEST_IOCTL_CMD); |
---|
123 | assert(*state == TEST_WRITE); |
---|
124 | *state = TEST_IOCTL; |
---|
125 | |
---|
126 | return 0; |
---|
127 | } |
---|
128 | |
---|
129 | static int |
---|
130 | testpoll(struct cdev *dev, int events, struct thread *td) |
---|
131 | { |
---|
132 | test_state *state = dev->si_drv1; |
---|
133 | |
---|
134 | assert(*state == TEST_WRITEV); |
---|
135 | *state = TEST_POLL; |
---|
136 | |
---|
137 | return 1; |
---|
138 | } |
---|
139 | |
---|
140 | static int |
---|
141 | testkqfilter(struct cdev *dev, struct knote *kn) |
---|
142 | { |
---|
143 | test_state *state = dev->si_drv1; |
---|
144 | |
---|
145 | assert(*state == TEST_POLL); |
---|
146 | *state = TEST_KQFILTER; |
---|
147 | |
---|
148 | return TEST_KQ_ERRNO; |
---|
149 | } |
---|
150 | |
---|
151 | void |
---|
152 | test_make_dev(test_state *state, const char *name) |
---|
153 | { |
---|
154 | struct cdev *dev = NULL; |
---|
155 | |
---|
156 | dev = make_dev(&test_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, name); |
---|
157 | assert(dev != NULL); |
---|
158 | dev->si_drv1 = state; |
---|
159 | } |
---|