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: releng/10.3/share/examples/kld/cdev/test/testcdev.c 202603 2010-01-18 23:13:22Z wkoszek $ 7241681Sdfr */ 73202603Swkoszek#include <sys/types.h> 74202603Swkoszek#include <sys/ioccom.h> 7569793Sobrien 7641681Sdfr#include <stdio.h> 77202603Swkoszek#include <stdlib.h> 7841681Sdfr#include <fcntl.h> 7969793Sobrien#include <paths.h> 8066549Ssobomax#include <string.h> 81202603Swkoszek#include <unistd.h> 8241681Sdfr 8341681Sdfr#define CDEV_IOCTL1 _IOR('C', 1, u_int) 8441681Sdfr#define CDEV_DEVICE "cdev" 8548275Sdfr 8666549Ssobomaxstatic char writestr[] = "Hello kernel!"; 8766549Ssobomaxstatic char buf[512+1]; 8866549Ssobomax 8941681Sdfrint 90202603Swkoszekmain(int argc __unused, char *argv[] __unused) 9141681Sdfr{ 9241681Sdfr int kernel_fd; 9341681Sdfr int one; 9466549Ssobomax int len; 9541681Sdfr 9666549Ssobomax if ((kernel_fd = open("/dev/" CDEV_DEVICE, O_RDWR)) == -1) { 9741681Sdfr perror("/dev/" CDEV_DEVICE); 9841681Sdfr exit(1); 9941681Sdfr } 10041681Sdfr 10141681Sdfr /* Send ioctl */ 10241681Sdfr if (ioctl(kernel_fd, CDEV_IOCTL1, &one) == -1) { 10341681Sdfr perror("CDEV_IOCTL1"); 10441681Sdfr } else { 10569793Sobrien printf( "Sent ioctl CDEV_IOCTL1 to device %s%s\n", _PATH_DEV, CDEV_DEVICE); 10641681Sdfr } 10766549Ssobomax 10866549Ssobomax len = strlen(writestr) + 1; 10966549Ssobomax 11066549Ssobomax /* Write operation */ 11166549Ssobomax if (write(kernel_fd, writestr, len) == -1) { 11266549Ssobomax perror("write()"); 11366549Ssobomax } else { 11466549Ssobomax printf("Written \"%s\" string to device /dev/" CDEV_DEVICE "\n", writestr); 11566549Ssobomax } 11666549Ssobomax 11766549Ssobomax /* Read operation */ 11866549Ssobomax if (read(kernel_fd, buf, len) == -1) { 11966549Ssobomax perror("read()"); 12066549Ssobomax } else { 12166549Ssobomax printf("Read \"%s\" string from device /dev/" CDEV_DEVICE "\n", buf); 12266549Ssobomax } 12366549Ssobomax 12441681Sdfr exit(0); 12541681Sdfr} 126