testcdev.c revision 66549
141681Sdfr/* 08 Nov 1998*/
241681Sdfr/*
341681Sdfr * testmisc.c
441681Sdfr *
541681Sdfr * Test program to call the sample loaded kld device driver.
641681Sdfr *
741681Sdfr * 05 Jun 93	Rajesh Vaidheeswarran		Original
841681Sdfr *
941681Sdfr *
1041681Sdfr * Copyright (c) 1993 Rajesh Vaidheeswarran.
1141681Sdfr * All rights reserved.
1241681Sdfr *
1341681Sdfr * Redistribution and use in source and binary forms, with or without
1441681Sdfr * modification, are permitted provided that the following conditions
1541681Sdfr * are met:
1641681Sdfr * 1. Redistributions of source code must retain the above copyright
1741681Sdfr *    notice, this list of conditions and the following disclaimer.
1841681Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1941681Sdfr *    notice, this list of conditions and the following disclaimer in the
2041681Sdfr *    documentation and/or other materials provided with the distribution.
2141681Sdfr * 3. All advertising materials mentioning features or use of this software
2241681Sdfr *    must display the following acknowledgement:
2341681Sdfr *      This product includes software developed by Rajesh Vaidheeswarran.
2441681Sdfr * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote
2541681Sdfr *    products derived from this software without specific prior written
2641681Sdfr *    permission.
2741681Sdfr *
2841681Sdfr * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY
2941681Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3041681Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3141681Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE
3241681Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3341681Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3441681Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3541681Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3641681Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3741681Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3841681Sdfr * SUCH DAMAGE.
3941681Sdfr *
4041681Sdfr * Copyright (c) 1993 Terrence R. Lambert.
4141681Sdfr * All rights reserved.
4241681Sdfr *
4341681Sdfr * Redistribution and use in source and binary forms, with or without
4441681Sdfr * modification, are permitted provided that the following conditions
4541681Sdfr * are met:
4641681Sdfr * 1. Redistributions of source code must retain the above copyright
4741681Sdfr *    notice, this list of conditions and the following disclaimer.
4841681Sdfr * 2. Redistributions in binary form must reproduce the above copyright
4941681Sdfr *    notice, this list of conditions and the following disclaimer in the
5041681Sdfr *    documentation and/or other materials provided with the distribution.
5141681Sdfr * 3. All advertising materials mentioning features or use of this software
5241681Sdfr *    must display the following acknowledgement:
5341681Sdfr *      This product includes software developed by Terrence R. Lambert.
5441681Sdfr * 4. The name Terrence R. Lambert may not be used to endorse or promote
5541681Sdfr *    products derived from this software without specific prior written
5641681Sdfr *    permission.
5741681Sdfr *
5841681Sdfr * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
5941681Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6041681Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6141681Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
6241681Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6341681Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6441681Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6541681Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6641681Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6741681Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6841681Sdfr * SUCH DAMAGE.
6941681Sdfr *
7066549Ssobomax *
7166549Ssobomax * $FreeBSD: head/share/examples/kld/cdev/test/testcdev.c 66549 2000-10-02 14:14:07Z sobomax $
7241681Sdfr */
7341681Sdfr#include <stdio.h>
7441681Sdfr#include <fcntl.h>
7566549Ssobomax#include <string.h>
7641681Sdfr#include <sys/ioccom.h>
7741681Sdfr
7841681Sdfr#define CDEV_IOCTL1     _IOR('C', 1, u_int)
7941681Sdfr#define CDEV_DEVICE	"cdev"
8048275Sdfr
8166549Ssobomaxstatic char writestr[] = "Hello kernel!";
8266549Ssobomaxstatic char buf[512+1];
8366549Ssobomax
8441681Sdfrint
8548275Sdfrmain(int argc, char *argv[])
8641681Sdfr{
8741681Sdfr    int kernel_fd;
8841681Sdfr    int one;
8966549Ssobomax    int len;
9041681Sdfr
9166549Ssobomax    if ((kernel_fd = open("/dev/" CDEV_DEVICE, O_RDWR)) == -1) {
9241681Sdfr	perror("/dev/" CDEV_DEVICE);
9341681Sdfr	exit(1);
9441681Sdfr    }
9541681Sdfr
9641681Sdfr    /* Send ioctl */
9741681Sdfr    if (ioctl(kernel_fd, CDEV_IOCTL1, &one) == -1) {
9841681Sdfr	perror("CDEV_IOCTL1");
9941681Sdfr    } else {
10041681Sdfr	printf( "Sent ioctl CDEV_IOCTL1 to device /dev/" CDEV_DEVICE "\n");
10141681Sdfr    }
10266549Ssobomax
10366549Ssobomax    len = strlen(writestr) + 1;
10466549Ssobomax
10566549Ssobomax    /* Write operation */
10666549Ssobomax    if (write(kernel_fd, writestr, len) == -1) {
10766549Ssobomax	perror("write()");
10866549Ssobomax    } else {
10966549Ssobomax	printf("Written \"%s\" string to device /dev/" CDEV_DEVICE "\n", writestr);
11066549Ssobomax    }
11166549Ssobomax
11266549Ssobomax    /* Read operation */
11366549Ssobomax    if (read(kernel_fd, buf, len) == -1) {
11466549Ssobomax	perror("read()");
11566549Ssobomax    } else {
11666549Ssobomax	printf("Read \"%s\" string from device /dev/" CDEV_DEVICE "\n", buf);
11766549Ssobomax    }
11866549Ssobomax
11941681Sdfr    exit(0);
12041681Sdfr}
121