Home | History | Annotate | Download | only in user_base
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  *
     19 
     20  * This is the main of your user space test program,
     21  * which will open the correct kernel module, find the
     22  * file descriptor value and use that value to make
     23  * ioctl calls to the system
     24  *
     25  * Use the ki_generic and other ki_testname functions
     26  * to abstract the calls from the main
     27  *
     28  * author: Sean Ruyle
     29  * date:   06/11/2003
     30  *
     31  */
     32 
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <sys/stat.h>
     36 #include <sys/ioctl.h>
     37 #include <fcntl.h>
     38 #include <linux/kernel.h>
     39 #include <linux/errno.h>
     40 #include <errno.h>
     41 
     42 #include "user_tbase.h"
     43 #include "../tbase/tbase.h"
     44 
     45 static int tbase_fd = -1;	/* file descriptor */
     46 
     47 int tbaseopen()
     48 {
     49 
     50 	dev_t devt;
     51 	struct stat st;
     52 	int rc = 0;
     53 
     54 	devt = makedev(TBASEMAJOR, 0);
     55 
     56 	if (rc) {
     57 		if (errno == ENOENT) {
     58 			/* dev node does not exist. */
     59 			rc = mkdir("/dev/tbase", (S_IFDIR | S_IRWXU |
     60 						  S_IRGRP | S_IXGRP |
     61 						  S_IROTH | S_IXOTH));
     62 		} else {
     63 			printf
     64 			    ("ERROR: Problem with Base dev directory.  Error code from stat() is %d\n\n",
     65 			     errno);
     66 		}
     67 
     68 	} else {
     69 		if (!(st.st_mode & S_IFDIR)) {
     70 			rc = unlink("/dev/tbase");
     71 			if (!rc) {
     72 				rc = mkdir("/dev/tbase", (S_IFDIR | S_IRWXU |
     73 							  S_IRGRP | S_IXGRP |
     74 							  S_IROTH | S_IXOTH));
     75 			}
     76 		}
     77 	}
     78 
     79 	/*
     80 	 * Check for the /dev/tbase node, and create if it does not
     81 	 * exist.
     82 	 */
     83 	rc = stat("/dev/tbase", &st);
     84 	if (rc) {
     85 		if (errno == ENOENT) {
     86 			/* dev node does not exist */
     87 			rc = mknod("/dev/tbase",
     88 				   (S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
     89 				    S_IWGRP), devt);
     90 		} else {
     91 			printf
     92 			    ("ERROR:Problem with tbase device node directory.  Error code form stat() is %d\n\n",
     93 			     errno);
     94 		}
     95 
     96 	} else {
     97 		/*
     98 		 * /dev/tbase CHR device exists.  Check to make sure it is for a
     99 		 * block device and that it has the right major and minor.
    100 		 */
    101 		if ((!(st.st_mode & S_IFCHR)) || (st.st_rdev != devt)) {
    102 
    103 			/* Recreate the dev node. */
    104 			rc = unlink("/dev/tbase");
    105 			if (!rc) {
    106 				rc = mknod("/dev/tbase",
    107 					   (S_IFCHR | S_IRUSR | S_IWUSR |
    108 					    S_IRGRP | S_IWGRP), devt);
    109 			}
    110 		}
    111 	}
    112 
    113 	tbase_fd = open("/dev/tbase", O_RDWR);
    114 
    115 	if (tbase_fd < 0) {
    116 		printf("ERROR: Open of device %s failed %d errno = %d\n",
    117 		       "/dev/tbase", tbase_fd, errno);
    118 		return errno;
    119 	} else {
    120 		printf("Device opened successfully \n");
    121 		return 0;
    122 	}
    123 
    124 }
    125 
    126 int tbaseclose()
    127 {
    128 
    129 	if (tbase_fd != -1) {
    130 		close(tbase_fd);
    131 		tbase_fd = -1;
    132 	}
    133 
    134 	return 0;
    135 }
    136 
    137 int main()
    138 {
    139 	int rc;
    140 
    141 	/* open the module */
    142 	rc = tbaseopen();
    143 	if (rc) {
    144 		printf("Test MOD Driver may not be loaded\n");
    145 		exit(1);
    146 	}
    147 
    148 	/* test bus rescan */
    149 	if (ki_generic(tbase_fd, BUS_RESCAN))
    150 		printf("Failed on bus rescan\n");
    151 	else
    152 		printf("Success on bus rescan\n");
    153 
    154 	/* test get driver */
    155 	if (ki_generic(tbase_fd, GET_DRV))
    156 		printf("Failed on get driver\n");
    157 	else
    158 		printf("Success on get driver\n");
    159 
    160 	/* test put driver */
    161 	if (ki_generic(tbase_fd, PUT_DRV))
    162 		printf("Failed on put driver\n");
    163 	else
    164 		printf("Success on put driver\n");
    165 
    166 	/* test register firmware, should return not 0 */
    167 	if (ki_generic(tbase_fd, REG_FIRM))
    168 		printf
    169 		    ("Failed on register firmware\n\tPossibly because parent nodes already set\n");
    170 	else
    171 		printf("Success on register firmware\n");
    172 
    173 	/* test create driver file sysfs */
    174 	if (ki_generic(tbase_fd, CREATE_FILE))
    175 		printf("Failed on creating driver file\n");
    176 	else
    177 		printf("Success on creating driver file\n");
    178 
    179 	/* test device suspend and resume */
    180 	if (ki_generic(tbase_fd, DEV_SUSPEND))
    181 		printf("Failed on suspending device\n");
    182 	else
    183 		printf("Success on suspending device\n");
    184 
    185 	/* test device create file sysfs */
    186 	if (ki_generic(tbase_fd, DEV_FILE))
    187 		printf("Failed on creating device file\n");
    188 	else
    189 		printf("Success on creating device file\n");
    190 
    191 	/* test bus create file sysfs */
    192 	if (ki_generic(tbase_fd, BUS_FILE))
    193 		printf("Failed on creating bus file\n");
    194 	else
    195 		printf("Success on creating bus file\n");
    196 
    197 	/* test register class */
    198 	if (ki_generic(tbase_fd, CLASS_REG))
    199 		printf("Failed on registering class\n");
    200 	else
    201 		printf("Success on registering class\n");
    202 
    203 	/* test get class */
    204 	if (ki_generic(tbase_fd, CLASS_GET))
    205 		printf("Failed on get class\n");
    206 	else
    207 		printf("Success on get class\n");
    208 
    209 	/* test class create file sysfs */
    210 	if (ki_generic(tbase_fd, CLASS_FILE))
    211 		printf("Failed on creating class file\n");
    212 	else
    213 		printf("Success on creating class file\n");
    214 
    215 	/* test unregistering class */
    216 	if (ki_generic(tbase_fd, CLASS_UNREG))
    217 		printf("Failed on unregistering class\n");
    218 	else
    219 		printf("Success on unregistering class\n");
    220 
    221 	/* test register class device */
    222 	if (ki_generic(tbase_fd, CLASSDEV_REG))
    223 		printf
    224 		    ("Failed on registering class device and creating sysfs file\n");
    225 	else
    226 		printf
    227 		    ("Success on registering class device and creating sysfs file\n");
    228 
    229 	/* test register class interface */
    230 	if (ki_generic(tbase_fd, CLASSINT_REG))
    231 		printf("Failed on registering class interface\n");
    232 	else
    233 		printf("Success on registering class interface\n");
    234 
    235 	/* test register sysdev_class */
    236 	if (ki_generic(tbase_fd, SYSDEV_CLS_REG))
    237 		printf("Failed on registering sysdev_class\n");
    238 	else
    239 		printf("Success on registering sysdev_class\n");
    240 
    241 	/* test register sysdev */
    242 	if (ki_generic(tbase_fd, SYSDEV_REG))
    243 		printf("Failed on registering sysdev\n");
    244 	else
    245 		printf("Success on registering sysdev\n");
    246 
    247 	/* test unregister sysdev */
    248 	if (ki_generic(tbase_fd, SYSDEV_UNREG))
    249 		printf("Failed on unregistering sysdev\n");
    250 	else
    251 		printf("Success on unregistering sysdev\n");
    252 
    253 	/* test unregister sysdev_class */
    254 	if (ki_generic(tbase_fd, SYSDEV_CLS_UNREG))
    255 		printf("Failed on unregistering sysdev_class\n");
    256 	else
    257 		printf("Success on unregistering sysdev_class\n");
    258 
    259 	/* close the module */
    260 	rc = tbaseclose();
    261 	if (rc) {
    262 		printf("Test MOD Driver may not be closed\n");
    263 		exit(1);
    264 	}
    265 
    266 	return 0;
    267 }
    268