Home | History | Annotate | Download | only in user_space
      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: Kai Zhao
     29  * date:   08/25/2003
     30  *
     31  */
     32 
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <errno.h>
     36 #include <sys/stat.h>
     37 #include <sys/ioctl.h>
     38 #include <fcntl.h>
     39 #include <linux/kernel.h>
     40 #include <linux/errno.h>
     41 
     42 #include "user_tagp.h"
     43 #include "../kernel_space/tagp.h"
     44 
     45 static int tagp_fd = -1;	/* file descriptor */
     46 
     47 int tagpopen()
     48 {
     49 
     50 	dev_t devt;
     51 	struct stat st;
     52 	int rc = 0;
     53 
     54 	devt = makedev(TAGP_MAJOR, 0);
     55 
     56 	if (rc) {
     57 		if (errno == ENOENT) {
     58 			/* dev node does not exist. */
     59 			rc = mkdir(DEVICE_NAME, (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(DEVICE_NAME);
     71 			if (!rc) {
     72 				rc = mkdir(DEVICE_NAME, (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(DEVICE_NAME, &st);
     84 	if (rc) {
     85 		if (errno == ENOENT) {
     86 			/* dev node does not exist */
     87 			rc = mknod(DEVICE_NAME,
     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(DEVICE_NAME);
    105 			if (!rc) {
    106 				rc = mknod(DEVICE_NAME,
    107 					   (S_IFCHR | S_IRUSR | S_IWUSR |
    108 					    S_IRGRP | S_IWGRP), devt);
    109 			}
    110 		}
    111 	}
    112 
    113 	tagp_fd = open(DEVICE_NAME, O_RDWR);
    114 
    115 	if (tagp_fd < 0) {
    116 		printf("ERROR: Open of device %s failed %d errno = %d\n",
    117 		       DEVICE_NAME, tagp_fd, errno);
    118 		return errno;
    119 	} else {
    120 		printf("Device opened successfully \n");
    121 		return 0;
    122 	}
    123 
    124 }
    125 
    126 int tagpclose()
    127 {
    128 
    129 	if (tagp_fd != -1) {
    130 		close(tagp_fd);
    131 		tagp_fd = -1;
    132 	}
    133 
    134 	return 0;
    135 }
    136 
    137 int agpgart_io_test()
    138 {
    139 	int tagp_fd = 0;
    140 	char read_buf[BUFFER_LEN];
    141 
    142 	if ((tagp_fd = open("/dev/agpgart", O_RDWR)) < 0) {
    143 		printf("Open /dev/agpgart failed \n");
    144 		return -1;
    145 	}
    146 
    147 	close(tagp_fd);
    148 
    149 	return 0;
    150 }
    151 
    152 int main()
    153 {
    154 	int rc;
    155 
    156 	if (agpgart_io_test())
    157 		printf("Test agpgart io failed\n");
    158 	else
    159 		printf("Test agpgart io success\n");
    160 
    161 	/* open the module */
    162 	rc = tagpopen();
    163 	if (rc) {
    164 		printf("Test AGP Driver may not be loaded\n");
    165 		exit(1);
    166 	}
    167 
    168 	/* make test calls for pci_find_device */
    169 	if (ki_generic(tagp_fd, TEST_PCI_FIND_DEV))
    170 		printf("Success: Expected failure for pci_find_dev test\n");
    171 	else
    172 		printf("Fail on pci_find_dev test\n");
    173 
    174 	/* make test calls for agp_backend_acquier */
    175 	if (ki_generic(tagp_fd, TEST_BACKEND_ACQUIRE))
    176 		printf("Fail on agp_backend_acquier\n");
    177 	else
    178 		printf("Success on agp_backend_acquier\n");
    179 
    180 	/* make test calls for agp_backend_release */
    181 	if (ki_generic(tagp_fd, TEST_BACKEND_RELEASE))
    182 		printf("Fail on agp_backend_release\n");
    183 	else
    184 		printf("Success on agp_backend_release\n");
    185 
    186 	/* make test calls for agp_alloc_bridge */
    187 	if (ki_generic(tagp_fd, TEST_ALLOC_BRIDGE))
    188 		printf("Fail on agp_alloc_bridge \n");
    189 	else
    190 		printf("Success on agp_alloc_bridge\n");
    191 
    192 	/* make test calls for and agp_put_bridge */
    193 	if (ki_generic(tagp_fd, TEST_PUT_BRIDGE))
    194 		printf("Fail on agp_put_bridge\n");
    195 	else
    196 		printf("Success on agp_put_bridge\n");
    197 
    198 	/* make test calls for agp_create_memory and agp_free_memory */
    199 	if (ki_generic(tagp_fd, TEST_CREATE_AND_FREE_MEMORY))
    200 		printf("Fail on agp_create_memory \n");
    201 	else
    202 		printf("Success on agp_create_memory\n");
    203 /*
    204 	if (ki_generic(tagp_fd, TEST_FREE_MEMORY))
    205 		printf("Fail on agp_free_memory\n");
    206 	else
    207 			printf("Success on agp_free_memory\n");
    208 *////////////////////////////////////////////////////////////////////////
    209 	/* make test calls for agp_num_entries */
    210 	if (ki_generic(tagp_fd, TEST_NUM_ENTRIES))
    211 		printf("Fail on agp_num_entries\n");
    212 	else
    213 		printf("Success on agp_num_entries\n");
    214 
    215 	/* make test calls for agp_copy_info */
    216 	if (ki_generic(tagp_fd, TEST_COPY_INFO))
    217 		printf("Fail on agp_copy_info\n");
    218 	else
    219 		printf("Success on agp_copy_info\n");
    220 
    221 	/* make test calls for agp_alloc_memory */
    222 //      if (ki_generic(tagp_fd, TEST_ALLOC_MEMORY_AND_BAND_UNBAND))
    223 //              printf("Fail on agp_alloc_memory_and_band_unband\n");
    224 //      else
    225 //              printf("Success on agp_alloc_memory_and_band_unband\n");
    226 
    227 	/* make test calls for agp_get_version */
    228 	if (ki_generic(tagp_fd, TEST_GET_VERSION))
    229 		printf("Fail on agp_get_version\n");
    230 	else
    231 		printf("Success on agp_get_version\n");
    232 
    233 	/* make test calls for agp_generic_enable */
    234 	if (ki_generic(tagp_fd, TEST_GENERIC_ENABLE))
    235 		printf("Fail on agp_generic_enable\n");
    236 	else
    237 		printf("Success on agp_generic_enable\n");
    238 
    239 	/* make test calls for agp_generic_create_gatt_table */
    240 	if (ki_generic(tagp_fd, TEST_GENERIC_CREATE_GATT_TABLE))
    241 		printf("Fail on agp_generic_create_gatt_table\n");
    242 	else
    243 		printf("Success on agp_generic_create_gatt_table\n");
    244 
    245 	/* make test calls for agp_generic_free_gatt_table */
    246 	if (ki_generic(tagp_fd, TEST_GENERIC_FREE_GATT_TABLE))
    247 		printf("Fail on agp_generic_free_gatt_table\n");
    248 	else
    249 		printf("Success on agp_generic_free_gatt_table\n");
    250 
    251 	/* make test calls for agp_generic_insert_memory */
    252 	if (ki_generic(tagp_fd, TEST_GENERIC_INSERT_MEMROY))
    253 		printf("Fail on agp_generic_insert_memory\n");
    254 	else
    255 		printf("Success on agp_generic_insert_memory\n");
    256 
    257 	/* make test calls for agp_generic_alloc_by_type */
    258 	if (ki_generic(tagp_fd, TEST_GENERIC_ALLOC_BY_TYPE))
    259 		printf("Fail on agp_generic_alloc_by_type\n");
    260 	else
    261 		printf("Success on agp_generic_alloc_by_type\n");
    262 
    263 	/* make test calls for agp_generic_alloc_page */
    264 	if (ki_generic(tagp_fd, TEST_GENERIC_ALLOC_PAGE))
    265 		printf("Fail on agp_generic_alloc_page\n");
    266 	else
    267 		printf("Success on agp_generic_alloc_page\n");
    268 
    269 	/* make test calls for agp_generic_destory_page */
    270 	if (ki_generic(tagp_fd, TEST_GENERIC_ALLOC_PAGE))
    271 		printf("Fail on agp_generic_destory_page\n");
    272 	else
    273 		printf("Success on agp_generic_destory_page\n");
    274 
    275 	/* make test calls for agp_enable */
    276 	if (ki_generic(tagp_fd, TEST_ENABLE))
    277 		printf("Fail on agp_enable\n");
    278 	else
    279 		printf("Success on agp_enable\n");
    280 
    281 	/* make test calls for agp_global_cache_flush */
    282 	if (ki_generic(tagp_fd, TEST_GLOBAL_CACHE_FLUSH))
    283 		printf("Fail on agp_global_cache_flush\n");
    284 	else
    285 		printf("Success on agp_gloabl_cache_flush\n");
    286 
    287 	/* make test calls for agp_generic_mask_memory */
    288 	if (ki_generic(tagp_fd, TEST_GENERIC_MASK_MEMORY))
    289 		printf("Fail on agp_generic_mask_memory\n");
    290 	else
    291 		printf("Success on agp_generic_mask_memory\n");
    292 
    293 	/* close the module */
    294 	rc = tagpclose();
    295 	if (rc) {
    296 		printf("Test AGP Driver may not be closed\n");
    297 		exit(1);
    298 	}
    299 
    300 	return 0;
    301 }
    302