testcdev.c revision 69793
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 69793 2000-12-09 09:35:55Z obrien $
7241681Sdfr */
7369793Sobrien
7441681Sdfr#include <stdio.h>
7541681Sdfr#include <fcntl.h>
7669793Sobrien#include <paths.h>
7766549Ssobomax#include <string.h>
7841681Sdfr#include <sys/ioccom.h>
7941681Sdfr
8041681Sdfr#define CDEV_IOCTL1     _IOR('C', 1, u_int)
8141681Sdfr#define CDEV_DEVICE	"cdev"
8248275Sdfr
8366549Ssobomaxstatic char writestr[] = "Hello kernel!";
8466549Ssobomaxstatic char buf[512+1];
8566549Ssobomax
8641681Sdfrint
8748275Sdfrmain(int argc, char *argv[])
8841681Sdfr{
8941681Sdfr    int kernel_fd;
9041681Sdfr    int one;
9166549Ssobomax    int len;
9241681Sdfr
9366549Ssobomax    if ((kernel_fd = open("/dev/" CDEV_DEVICE, O_RDWR)) == -1) {
9441681Sdfr	perror("/dev/" CDEV_DEVICE);
9541681Sdfr	exit(1);
9641681Sdfr    }
9741681Sdfr
9841681Sdfr    /* Send ioctl */
9941681Sdfr    if (ioctl(kernel_fd, CDEV_IOCTL1, &one) == -1) {
10041681Sdfr	perror("CDEV_IOCTL1");
10141681Sdfr    } else {
10269793Sobrien	printf( "Sent ioctl CDEV_IOCTL1 to device %s%s\n", _PATH_DEV, CDEV_DEVICE);
10341681Sdfr    }
10466549Ssobomax
10566549Ssobomax    len = strlen(writestr) + 1;
10666549Ssobomax
10766549Ssobomax    /* Write operation */
10866549Ssobomax    if (write(kernel_fd, writestr, len) == -1) {
10966549Ssobomax	perror("write()");
11066549Ssobomax    } else {
11166549Ssobomax	printf("Written \"%s\" string to device /dev/" CDEV_DEVICE "\n", writestr);
11266549Ssobomax    }
11366549Ssobomax
11466549Ssobomax    /* Read operation */
11566549Ssobomax    if (read(kernel_fd, buf, len) == -1) {
11666549Ssobomax	perror("read()");
11766549Ssobomax    } else {
11866549Ssobomax	printf("Read \"%s\" string from device /dev/" CDEV_DEVICE "\n", buf);
11966549Ssobomax    }
12066549Ssobomax
12141681Sdfr    exit(0);
12241681Sdfr}
123