user_tbase.c 6.5 KB
Newer Older
M
m00302376 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
/*
 *
 *   Copyright (c) International Business Machines  Corp., 2001
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *

 * This is the main of your user space test program,
 * which will open the correct kernel module, find the
 * file descriptor value and use that value to make
 * ioctl calls to the system
 *
 * Use the ki_generic and other ki_testname functions
 * to abstract the calls from the main
 *
 * author: Sean Ruyle
 * date:   06/11/2003
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <errno.h>

#include "user_tbase.h"
#include "../tbase/tbase.h"

static int tbase_fd = -1;	/* file descriptor */

int tbaseopen()
{

	dev_t devt;
	struct stat st;
	int rc = 0;

	devt = makedev(TBASEMAJOR, 0);

	if (rc) {
		if (errno == ENOENT) {
			/* dev node does not exist. */
			rc = mkdir("/dev/tbase", (S_IFDIR | S_IRWXU |
						  S_IRGRP | S_IXGRP |
						  S_IROTH | S_IXOTH));
		} else {
			printf
			    ("ERROR: Problem with Base dev directory.  Error code from stat() is %d\n\n",
			     errno);
		}

	} else {
		if (!(st.st_mode & S_IFDIR)) {
			rc = unlink("/dev/tbase");
			if (!rc) {
				rc = mkdir("/dev/tbase", (S_IFDIR | S_IRWXU |
							  S_IRGRP | S_IXGRP |
							  S_IROTH | S_IXOTH));
			}
		}
	}

	/*
	 * Check for the /dev/tbase node, and create if it does not
	 * exist.
	 */
	rc = stat("/dev/tbase", &st);
	if (rc) {
		if (errno == ENOENT) {
			/* dev node does not exist */
			rc = mknod("/dev/tbase",
				   (S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
				    S_IWGRP), devt);
		} else {
			printf
			    ("ERROR:Problem with tbase device node directory.  Error code form stat() is %d\n\n",
			     errno);
		}

	} else {
		/*
		 * /dev/tbase CHR device exists.  Check to make sure it is for a
		 * block device and that it has the right major and minor.
		 */
		if ((!(st.st_mode & S_IFCHR)) || (st.st_rdev != devt)) {

			/* Recreate the dev node. */
			rc = unlink("/dev/tbase");
			if (!rc) {
				rc = mknod("/dev/tbase",
					   (S_IFCHR | S_IRUSR | S_IWUSR |
					    S_IRGRP | S_IWGRP), devt);
			}
		}
	}

	tbase_fd = open("/dev/tbase", O_RDWR);

	if (tbase_fd < 0) {
		printf("ERROR: Open of device %s failed %d errno = %d\n",
		       "/dev/tbase", tbase_fd, errno);
		return errno;
	} else {
		printf("Device opened successfully \n");
		return 0;
	}

}

int tbaseclose()
{

	if (tbase_fd != -1) {
		close(tbase_fd);
		tbase_fd = -1;
	}

	return 0;
}

int main()
{
	int rc;

	/* open the module */
	rc = tbaseopen();
	if (rc) {
		printf("Test MOD Driver may not be loaded\n");
		exit(1);
	}

	/* test bus rescan */
	if (ki_generic(tbase_fd, BUS_RESCAN))
		printf("Failed on bus rescan\n");
	else
		printf("Success on bus rescan\n");

	/* test get driver */
	if (ki_generic(tbase_fd, GET_DRV))
		printf("Failed on get driver\n");
	else
		printf("Success on get driver\n");

	/* test put driver */
	if (ki_generic(tbase_fd, PUT_DRV))
		printf("Failed on put driver\n");
	else
		printf("Success on put driver\n");

	/* test register firmware, should return not 0 */
	if (ki_generic(tbase_fd, REG_FIRM))
		printf
		    ("Failed on register firmware\n\tPossibly because parent nodes already set\n");
	else
		printf("Success on register firmware\n");

	/* test create driver file sysfs */
	if (ki_generic(tbase_fd, CREATE_FILE))
		printf("Failed on creating driver file\n");
	else
		printf("Success on creating driver file\n");

	/* test device suspend and resume */
	if (ki_generic(tbase_fd, DEV_SUSPEND))
		printf("Failed on suspending device\n");
	else
		printf("Success on suspending device\n");

	/* test device create file sysfs */
	if (ki_generic(tbase_fd, DEV_FILE))
		printf("Failed on creating device file\n");
	else
		printf("Success on creating device file\n");

	/* test bus create file sysfs */
	if (ki_generic(tbase_fd, BUS_FILE))
		printf("Failed on creating bus file\n");
	else
		printf("Success on creating bus file\n");

	/* test register class */
	if (ki_generic(tbase_fd, CLASS_REG))
		printf("Failed on registering class\n");
	else
		printf("Success on registering class\n");

	/* test get class */
	if (ki_generic(tbase_fd, CLASS_GET))
		printf("Failed on get class\n");
	else
		printf("Success on get class\n");

	/* test class create file sysfs */
	if (ki_generic(tbase_fd, CLASS_FILE))
		printf("Failed on creating class file\n");
	else
		printf("Success on creating class file\n");

	/* test unregistering class */
	if (ki_generic(tbase_fd, CLASS_UNREG))
		printf("Failed on unregistering class\n");
	else
		printf("Success on unregistering class\n");

	/* test register class device */
	if (ki_generic(tbase_fd, CLASSDEV_REG))
		printf
		    ("Failed on registering class device and creating sysfs file\n");
	else
		printf
		    ("Success on registering class device and creating sysfs file\n");

	/* test register class interface */
	if (ki_generic(tbase_fd, CLASSINT_REG))
		printf("Failed on registering class interface\n");
	else
		printf("Success on registering class interface\n");

	/* test register sysdev_class */
	if (ki_generic(tbase_fd, SYSDEV_CLS_REG))
		printf("Failed on registering sysdev_class\n");
	else
		printf("Success on registering sysdev_class\n");

	/* test register sysdev */
	if (ki_generic(tbase_fd, SYSDEV_REG))
		printf("Failed on registering sysdev\n");
	else
		printf("Success on registering sysdev\n");

	/* test unregister sysdev */
	if (ki_generic(tbase_fd, SYSDEV_UNREG))
		printf("Failed on unregistering sysdev\n");
	else
		printf("Success on unregistering sysdev\n");

	/* test unregister sysdev_class */
	if (ki_generic(tbase_fd, SYSDEV_CLS_UNREG))
		printf("Failed on unregistering sysdev_class\n");
	else
		printf("Success on unregistering sysdev_class\n");

	/* close the module */
	rc = tbaseclose();
	if (rc) {
		printf("Test MOD Driver may not be closed\n");
		exit(1);
	}

	return 0;
}