source: rtems-libbsd/testsuite/cdev01/test_cdev.c @ 8dacfe4

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 8dacfe4 was 8dacfe4, checked in by Christian Mauderer <Christian.Mauderer@…>, on 06/28/16 at 13:13:09

testsuite/cdev01: Add test for cdev functions.

  • Property mode set to 100644
File size: 3.8 KB
Line 
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
42static  d_open_t        testopen;
43static  d_close_t       testclose;
44static  d_read_t        testread;
45static  d_write_t       testwrite;
46static  d_ioctl_t       testioctl;
47static  d_poll_t        testpoll;
48static  d_kqfilter_t    testkqfilter;
49
50static 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
64static  int
65testopen(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
75static  int
76testclose(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
86static  int
87testread(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
101static  int
102testwrite(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
116static  int
117testioctl(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
129static  int
130testpoll(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
140static  int
141testkqfilter(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
151void
152test_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}
Note: See TracBrowser for help on using the repository browser.