xref: /freebsd/share/examples/kld/cdev/test/testcdev.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1abbcaa0aSDoug Rabson /* 08 Nov 1998*/
2*f0cfa1b1SPedro F. Giffuni /*-
3abbcaa0aSDoug Rabson  * testmisc.c
4abbcaa0aSDoug Rabson  *
5abbcaa0aSDoug Rabson  * Test program to call the sample loaded kld device driver.
6abbcaa0aSDoug Rabson  *
7abbcaa0aSDoug Rabson  * 05 Jun 93	Rajesh Vaidheeswarran		Original
8abbcaa0aSDoug Rabson  *
9abbcaa0aSDoug Rabson  *
10*f0cfa1b1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
11*f0cfa1b1SPedro F. Giffuni  *
12abbcaa0aSDoug Rabson  * Copyright (c) 1993 Rajesh Vaidheeswarran.
13abbcaa0aSDoug Rabson  * All rights reserved.
14abbcaa0aSDoug Rabson  *
15abbcaa0aSDoug Rabson  * Redistribution and use in source and binary forms, with or without
16abbcaa0aSDoug Rabson  * modification, are permitted provided that the following conditions
17abbcaa0aSDoug Rabson  * are met:
18abbcaa0aSDoug Rabson  * 1. Redistributions of source code must retain the above copyright
19abbcaa0aSDoug Rabson  *    notice, this list of conditions and the following disclaimer.
20abbcaa0aSDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
21abbcaa0aSDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
22abbcaa0aSDoug Rabson  *    documentation and/or other materials provided with the distribution.
23abbcaa0aSDoug Rabson  * 3. All advertising materials mentioning features or use of this software
24abbcaa0aSDoug Rabson  *    must display the following acknowledgement:
25abbcaa0aSDoug Rabson  *      This product includes software developed by Rajesh Vaidheeswarran.
26abbcaa0aSDoug Rabson  * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote
27abbcaa0aSDoug Rabson  *    products derived from this software without specific prior written
28abbcaa0aSDoug Rabson  *    permission.
29abbcaa0aSDoug Rabson  *
30abbcaa0aSDoug Rabson  * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY
31abbcaa0aSDoug Rabson  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32abbcaa0aSDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33abbcaa0aSDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE
34abbcaa0aSDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35abbcaa0aSDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36abbcaa0aSDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37abbcaa0aSDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38abbcaa0aSDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39abbcaa0aSDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40abbcaa0aSDoug Rabson  * SUCH DAMAGE.
41abbcaa0aSDoug Rabson  *
42abbcaa0aSDoug Rabson  * Copyright (c) 1993 Terrence R. Lambert.
43abbcaa0aSDoug Rabson  * All rights reserved.
44abbcaa0aSDoug Rabson  *
45abbcaa0aSDoug Rabson  * Redistribution and use in source and binary forms, with or without
46abbcaa0aSDoug Rabson  * modification, are permitted provided that the following conditions
47abbcaa0aSDoug Rabson  * are met:
48abbcaa0aSDoug Rabson  * 1. Redistributions of source code must retain the above copyright
49abbcaa0aSDoug Rabson  *    notice, this list of conditions and the following disclaimer.
50abbcaa0aSDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
51abbcaa0aSDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
52abbcaa0aSDoug Rabson  *    documentation and/or other materials provided with the distribution.
53abbcaa0aSDoug Rabson  * 3. All advertising materials mentioning features or use of this software
54abbcaa0aSDoug Rabson  *    must display the following acknowledgement:
55abbcaa0aSDoug Rabson  *      This product includes software developed by Terrence R. Lambert.
56abbcaa0aSDoug Rabson  * 4. The name Terrence R. Lambert may not be used to endorse or promote
57abbcaa0aSDoug Rabson  *    products derived from this software without specific prior written
58abbcaa0aSDoug Rabson  *    permission.
59abbcaa0aSDoug Rabson  *
60abbcaa0aSDoug Rabson  * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
61abbcaa0aSDoug Rabson  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62abbcaa0aSDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63abbcaa0aSDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
64abbcaa0aSDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65abbcaa0aSDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66abbcaa0aSDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67abbcaa0aSDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68abbcaa0aSDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69abbcaa0aSDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70abbcaa0aSDoug Rabson  * SUCH DAMAGE.
71abbcaa0aSDoug Rabson  *
72abbcaa0aSDoug Rabson  */
7312a91a43SWojciech A. Koszek #include <sys/types.h>
7412a91a43SWojciech A. Koszek #include <sys/ioccom.h>
751a37aa56SDavid E. O'Brien 
76abbcaa0aSDoug Rabson #include <stdio.h>
7712a91a43SWojciech A. Koszek #include <stdlib.h>
78abbcaa0aSDoug Rabson #include <fcntl.h>
791a37aa56SDavid E. O'Brien #include <paths.h>
807320fd3aSMaxim Sobolev #include <string.h>
8112a91a43SWojciech A. Koszek #include <unistd.h>
82abbcaa0aSDoug Rabson 
83abbcaa0aSDoug Rabson #define CDEV_IOCTL1     _IOR('C', 1, u_int)
84abbcaa0aSDoug Rabson #define CDEV_DEVICE	"cdev"
850f2a20dfSDoug Rabson 
867320fd3aSMaxim Sobolev static char writestr[] = "Hello kernel!";
877320fd3aSMaxim Sobolev static char buf[512+1];
887320fd3aSMaxim Sobolev 
89abbcaa0aSDoug Rabson int
main(int argc __unused,char * argv[]__unused)9012a91a43SWojciech A. Koszek main(int argc __unused, char *argv[] __unused)
91abbcaa0aSDoug Rabson {
92abbcaa0aSDoug Rabson     int kernel_fd;
93abbcaa0aSDoug Rabson     int one;
947320fd3aSMaxim Sobolev     int len;
95abbcaa0aSDoug Rabson 
967320fd3aSMaxim Sobolev     if ((kernel_fd = open("/dev/" CDEV_DEVICE, O_RDWR)) == -1) {
97abbcaa0aSDoug Rabson 	perror("/dev/" CDEV_DEVICE);
98abbcaa0aSDoug Rabson 	exit(1);
99abbcaa0aSDoug Rabson     }
100abbcaa0aSDoug Rabson 
101abbcaa0aSDoug Rabson     /* Send ioctl */
102abbcaa0aSDoug Rabson     if (ioctl(kernel_fd, CDEV_IOCTL1, &one) == -1) {
103abbcaa0aSDoug Rabson 	perror("CDEV_IOCTL1");
104abbcaa0aSDoug Rabson     } else {
1051a37aa56SDavid E. O'Brien 	printf( "Sent ioctl CDEV_IOCTL1 to device %s%s\n", _PATH_DEV, CDEV_DEVICE);
106abbcaa0aSDoug Rabson     }
1077320fd3aSMaxim Sobolev 
1087320fd3aSMaxim Sobolev     len = strlen(writestr) + 1;
1097320fd3aSMaxim Sobolev 
1107320fd3aSMaxim Sobolev     /* Write operation */
1117320fd3aSMaxim Sobolev     if (write(kernel_fd, writestr, len) == -1) {
1127320fd3aSMaxim Sobolev 	perror("write()");
1137320fd3aSMaxim Sobolev     } else {
1147320fd3aSMaxim Sobolev 	printf("Written \"%s\" string to device /dev/" CDEV_DEVICE "\n", writestr);
1157320fd3aSMaxim Sobolev     }
1167320fd3aSMaxim Sobolev 
1177320fd3aSMaxim Sobolev     /* Read operation */
1187320fd3aSMaxim Sobolev     if (read(kernel_fd, buf, len) == -1) {
1197320fd3aSMaxim Sobolev 	perror("read()");
1207320fd3aSMaxim Sobolev     } else {
1217320fd3aSMaxim Sobolev 	printf("Read \"%s\" string from device /dev/" CDEV_DEVICE "\n", buf);
1227320fd3aSMaxim Sobolev     }
1237320fd3aSMaxim Sobolev 
124abbcaa0aSDoug Rabson     exit(0);
125abbcaa0aSDoug Rabson }
126