• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
26  * author: Kai Zhao
27  * date:   09/03/2003
28  *
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35 #include <sys/ioctl.h>
36 #include <fcntl.h>
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 
40 #include "../kernel_space/tdrm.h"
41 #include "drm.h"
42 
43 static int tdrm_fd = -1;	/* file descriptor */
44 
tdrmopen()45 int tdrmopen()
46 {
47 
48 	dev_t devt;
49 	struct stat st;
50 	int rc = 0;
51 
52 	devt = makedev(TDRM_MAJOR, 0);
53 
54 	if (rc) {
55 		if (errno == ENOENT) {
56 			/* dev node does not exist. */
57 			rc = mkdir(DEVICE_NAME, (S_IFDIR | S_IRWXU |
58 						 S_IRGRP | S_IXGRP |
59 						 S_IROTH | S_IXOTH));
60 		} else {
61 			printf
62 			    ("ERROR: Problem with Base dev directory.  Error code from stat() is %d\n\n",
63 			     errno);
64 		}
65 
66 	} else {
67 		if (!(st.st_mode & S_IFDIR)) {
68 			rc = unlink(DEVICE_NAME);
69 			if (!rc) {
70 				rc = mkdir(DEVICE_NAME, (S_IFDIR | S_IRWXU |
71 							 S_IRGRP | S_IXGRP |
72 							 S_IROTH | S_IXOTH));
73 			}
74 		}
75 	}
76 
77 	/*
78 	 * Check for the /dev/tbase node, and create if it does not
79 	 * exist.
80 	 */
81 	rc = stat(DEVICE_NAME, &st);
82 	if (rc) {
83 		if (errno == ENOENT) {
84 			/* dev node does not exist */
85 			rc = mknod(DEVICE_NAME,
86 				   (S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
87 				    S_IWGRP), devt);
88 		} else {
89 			printf
90 			    ("ERROR:Problem with tbase device node directory.  Error code form stat() is %d\n\n",
91 			     errno);
92 		}
93 
94 	} else {
95 		/*
96 		 * /dev/tbase CHR device exists.  Check to make sure it is for a
97 		 * block device and that it has the right major and minor.
98 		 */
99 		if ((!(st.st_mode & S_IFCHR)) || (st.st_rdev != devt)) {
100 
101 			/* Recreate the dev node. */
102 			rc = unlink(DEVICE_NAME);
103 			if (!rc) {
104 				rc = mknod(DEVICE_NAME,
105 					   (S_IFCHR | S_IRUSR | S_IWUSR |
106 					    S_IRGRP | S_IWGRP), devt);
107 			}
108 		}
109 	}
110 
111 	tdrm_fd = open(DEVICE_NAME, O_RDWR);
112 
113 	if (tdrm_fd < 0) {
114 		printf("ERROR: Open of device %s failed %d errno = %d\n",
115 		       DEVICE_NAME, tdrm_fd, errno);
116 		return errno;
117 	} else {
118 		printf("Device opened successfully \n");
119 		return 0;
120 	}
121 
122 }
123 
tdrmclose()124 int tdrmclose()
125 {
126 
127 	if (tdrm_fd != -1) {
128 		close(tdrm_fd);
129 		tdrm_fd = -1;
130 	}
131 }
132 
main()133 int main()
134 {
135 	int rc;
136 
137 	/* open the module */
138 	rc = tdrmopen();
139 	if (rc) {
140 		printf("Test DRM Driver may not be loaded\n");
141 		exit(1);
142 	}
143 
144 	printf("\tBegin drm read system ioctl\n");
145 	//
146 	drm_auth_t auth;
147 	if (ioctl(tdrm_fd, DRM_IOCTL_GET_MAGIC, &auth))
148 		printf("Failed on drm ioctl DRM(get_magic) test\n");
149 	else
150 		printf("Success on drm ioctl DRM(get_magic) test\n");
151 
152 	//
153 	drm_stats_t stats;
154 	if (ioctl(tdrm_fd, DRM_IOCTL_GET_STATS, &stats))
155 		printf("Failed on drm ioctl DRM(get_status) test\n");
156 	else
157 		printf("Success on drm ioctl DRM(get_status) test\n");
158 
159 	/*--------------------------------------------------------------
160 	 *                      for drm read write ioctl test
161 	 */
162 
163 	printf("\tBegin read write ioctl test\n");
164 	if (ioctl(tdrm_fd, DRM_IOCTL_IRQ_BUSID))
165 		printf("Failed on drm ioctl DRM(irq_busid)\n");
166 	else
167 		printf("Success on drm ioctl DRM(irq_busid)\n");
168 	if (ioctl(tdrm_fd, DRM_IOCTL_GET_CLIENT))
169 		printf("Failed on drm ioctl DRM(get_client)\n");
170 	else
171 		printf("Success on drm ioctl DRM(get_client)\n");
172 	if (ioctl(tdrm_fd, DRM_IOCTL_BLOCK))
173 		printf("Failed on drm ioctl DRM(block)\n");
174 	else
175 		printf("Success on drm ioctl DRM(block)\n");
176 	if (ioctl(tdrm_fd, DRM_IOCTL_UNBLOCK))
177 		printf("Failed on drm ioctl DRM(unblock)\n");
178 	else
179 		printf("Success on drm ioctl DRM(unblock)\n");
180 	if (ioctl(tdrm_fd, DRM_IOCTL_ADD_CTX))
181 		printf("Failed on drm ioctl DRM(addctx)\n");
182 	else
183 		printf("Success on drm ioctl DRM(addctx)\n");
184 	if (ioctl(tdrm_fd, DRM_IOCTL_RM_CTX))
185 		printf("Failed on drm ioctl DRM(rmctx)\n");
186 	else
187 		printf("Success on drm ioctl DRM(rmctx)\n");
188 	if (ioctl(tdrm_fd, DRM_IOCTL_GET_CTX))
189 		printf("Failed on drm ioctl DRM(getctx)\n");
190 	else
191 		printf("Success on drm ioctl DRM(getctx)\n");
192 	if (ioctl(tdrm_fd, DRM_IOCTL_ADD_DRAW))
193 		printf("Failed on drm ioctl DRM(adddraw)\n");
194 	else
195 		printf("Success on drm ioctl DRM(adddraw)\n");
196 	if (ioctl(tdrm_fd, DRM_IOCTL_RM_DRAW))
197 		printf("Failed on drm ioctl DRM(rmdraw)\n");
198 	else
199 		printf("Success on drm ioctl DRM(rmdraw)\n");
200 
201 	/* test drm stub_register */
202 
203 	if (ioctl(tdrm_fd, TDRM_STUB_REGISTER))
204 		printf("Failed on drm stub_register test\n");
205 	else
206 		printf("Success on drm stub_register test\n");
207 
208 	// test drm DRM(agp_uninit)
209 
210 	if (ioctl(tdrm_fd, TDRM_UNINIT_AGP))
211 		printf("Failed on DRM(agp_uninit) test\n");
212 	else
213 		printf("Success on DRM(agp_uninit) test\n");
214 
215 	// test drm DRM(agp_init)
216 
217 	if (ioctl(tdrm_fd, TDRM_INIT_AGP))
218 		printf("Failed on DRM(agp_init) test\n");
219 	else
220 		printf("Success on DRM(agp_init) test\n");
221 
222 	// test drm stub_unregister
223 
224 	if (ioctl(tdrm_fd, TDRM_STUB_UNREGISTER))
225 		printf("Failed on drm stub_unregister test\n");
226 	else
227 		printf("Success on drm stub_unregister test\n");
228 	// test drm add magic
229 	if (ioctl(tdrm_fd, TDRM_ADD_MAGIC))
230 		printf("Failed on drm DRM(add_magic) test\n");
231 	else
232 		printf("Success on drm DRM(add_magic) test\n");
233 
234 	// test drm remove magic
235 	if (ioctl(tdrm_fd, TDRM_REMOVE_MAGIC))
236 		printf("Failed on drm DRM(remove_magic) test\n");
237 	else
238 		printf("Success on drm DRM(remove_magic) test\n");
239 
240 	// test drm alloc pages
241 	if (ioctl(tdrm_fd, TDRM_ALLOC_PAGES))
242 		printf("Failed on drm DRM(alloc_pages) test\n");
243 	else
244 		printf("Success on drm DRM(alloc_pages) test\n");
245 	// test drm free pages
246 	if (ioctl(tdrm_fd, TDRM_FREE_PAGES))
247 		printf("Failed on drm DRM(free_pages) test\n");
248 	else
249 		printf("Success on drm DRM(free_pages) test\n");
250 	// test drm ctxbitmap_cleanup
251 	if (ioctl(tdrm_fd, TDRM_CTXBITMAP_UNINIT))
252 		printf("Failed on drm DRM(ctxbitmap_cleanup) test\n");
253 	else
254 		printf("Success on drm DRM(ctxbitmap_cleanup) test\n");
255 
256 	// test drm ctxbitmap init
257 	if (ioctl(tdrm_fd, TDRM_CTXBITMAP_INIT))
258 		printf("Failed on drm DRM(ctxbitmap_init) test\n");
259 	else
260 		printf("Success on drm DRM(ctxbitmap_init) test\n");
261 
262 	static drm_version_t version;
263 	if (ioctl(tdrm_fd, DRM_IOCTL_VERSION, &version))
264 		printf("Failed on drm DRM(version) test\n");
265 	else
266 		printf("Success on drm DRM(version) test\n");
267 
268 	static drm_auth_t myauth;
269 	if (ioctl(tdrm_fd, DRM_IOCTL_GET_MAGIC, &myauth))
270 		printf("Failed on drm DRM(get_magic) test\n");
271 	else
272 		printf("Success on drm DRM(get_magic) test\n");
273 
274 	if (ioctl(tdrm_fd, DRM_IOCTL_AUTH_MAGIC, &auth))
275 		printf("Failed on drm authmagic test\n");
276 	else
277 		printf("Success on drm authmagic test\n");
278 
279 	//  test for drm_agpsupport.h
280 	/*
281 	   if (ioctl(tdrm_fd, DRM_IOCTL_AGP_RELEASE))
282 	   printf("Failed on drm DRM(agp_release) test\n");
283 	   else
284 	   printf("Success on drm DRM(agp_release) test\n");
285 	 */
286 	if (ioctl(tdrm_fd, DRM_IOCTL_AGP_ACQUIRE))
287 		printf("Failed on drm DRM(agp_acquire) test\n");
288 	else
289 		printf("Success on drm DRM(agp_acquire) test\n");
290 
291 	if (ioctl(tdrm_fd, DRM_IOCTL_AGP_RELEASE))
292 		printf("Failed on drm DRM(agp_release) test\n");
293 	else
294 		printf("Success on drm DRM(agp_release) test\n");
295 
296 	if (ioctl(tdrm_fd, DRM_IOCTL_AGP_ACQUIRE))
297 		printf("Failed on drm DRM(agp_acquire) test\n");
298 	else
299 		printf("Success on drm DRM(agp_acquire) test\n");
300 
301 	static drm_agp_info_t agp_info;
302 	if (ioctl(tdrm_fd, DRM_IOCTL_AGP_INFO, &agp_info))
303 		printf("Failed on drm DRM(agp_info) test\n");
304 	else
305 		printf("Success on drm DRM(agp_info) test\n");
306 
307 	static drm_agp_buffer_t agp_buffer;
308 	agp_buffer.size = 64;
309 	if (ioctl(tdrm_fd, DRM_IOCTL_AGP_ALLOC, &agp_buffer))
310 		printf("Failed on drm DRM(agp_alloc) test\n");
311 	else
312 		printf("Success on drm DRM(agp_alloc) test\n");
313 
314 	static drm_agp_binding_t bind_buffer;
315 	bind_buffer.handle = agp_buffer.handle;
316 	bind_buffer.offset = 64;
317 
318 	if (ioctl(tdrm_fd, DRM_IOCTL_AGP_BIND, &bind_buffer))
319 		printf("Failed on drm DRM(agp_bind) test\n");
320 	else
321 		printf("Success on drm DRM(agp_bind) test\n");
322 
323 	if (ioctl(tdrm_fd, DRM_IOCTL_AGP_UNBIND, &bind_buffer))
324 		printf("Failed on drm DRM(agp_unbind) test\n");
325 	else
326 		printf("Success on drm DRM(agp_unbind) test\n");
327 	if (ioctl(tdrm_fd, DRM_IOCTL_AGP_FREE, &agp_buffer))
328 		printf("Failed on drm DRM(agp_free) test\n");
329 	else
330 		printf("Success on drm DRM(agp_free) test\n");
331 
332 	// test drm_ctxbitmap.h
333 	static drm_ctx_t getctx;
334 	if (ioctl(tdrm_fd, DRM_IOCTL_GET_CTX, &getctx))
335 		printf("Failed on drm DRM(getctx) test\n");
336 	else
337 		printf("Success on drm DRM(getctx) test\n");
338 
339 	static drm_ctx_t ctx;
340 	if (ioctl(tdrm_fd, DRM_IOCTL_ADD_CTX, &ctx))
341 		printf("Failed on drm DRM(addctx) test\n");
342 	else
343 		printf("Success on drm DRM(addctx) test\n");
344 
345 	if (ioctl(tdrm_fd, DRM_IOCTL_RM_CTX, &ctx))
346 		printf("Failed on drm DRM(rmctx) test\n");
347 	else
348 		printf("Success on drm DRM(rmctx) test\n");
349 
350 	/*
351 	   static drm_ctx_priv_map_t map;
352 	   memset(&map,0,sizeof(drm_ctx_priv_map_t));
353 	   if (ioctl(tdrm_fd, DRM_IOCTL_GET_SAREA_CTX,&map))
354 	   printf("Failed on drm DRM(getsareactx) test\n");
355 	   else
356 	   printf("Success on drm DRM(getsareactx) test\n");
357 	 */
358 	// for drm_drawtable.h
359 	static drm_draw_t draw;
360 	if (ioctl(tdrm_fd, DRM_IOCTL_ADD_DRAW, &draw))
361 		printf("Failed on drm DRM(adddraw) test\n");
362 	else
363 		printf("Success on drm DRM(adddraw) test\n");
364 
365 	if (ioctl(tdrm_fd, DRM_IOCTL_RM_DRAW, &draw))
366 		printf("Failed on drm DRM(rmdraw) test\n");
367 	else
368 		printf("Success on drm DRM(rmdraw) test\n");
369 
370 	//for drm_ioctl.h//
371 	static drm_stats_t status;
372 	if (ioctl(tdrm_fd, DRM_IOCTL_GET_STATS, &status))
373 		printf("Failed on drm DRM(getstatus) test\n");
374 	else
375 		printf("Success on drm DRM(getstatus) test\n");
376 
377 	static drm_client_t client;
378 	if (ioctl(tdrm_fd, DRM_IOCTL_GET_CLIENT, &client))
379 		printf("Failed on drm DRM(getclient) test\n");
380 	else
381 		printf("Success on drm DRM(getclient) test\n");
382 	/*
383 	   static drm_map_t getmap;
384 	   getmap.offset = 0;
385 	   if (ioctl(tdrm_fd, DRM_IOCTL_GET_MAP,&getmap))
386 	   printf("Failed on drm DRM(getmap) test\n");
387 	   else
388 	   printf("Success on drm DRM(getmap) test\n");
389 	 */
390 	static drm_unique_t unique;
391 	unique.unique_len = 0;
392 	if (ioctl(tdrm_fd, DRM_IOCTL_GET_UNIQUE, &unique))
393 		printf("Failed on drm DRM(getunique) test\n");
394 	else
395 		printf("Success on drm DRM(getunique) test\n");
396 	/*
397 	   if (ioctl(tdrm_fd, DRM_IOCTL_SET_UNIQUE,&unique))
398 	   printf("Failed on drm DRM(setunique) test\n");
399 
400 	   else
401 	   printf("Success on drm DRM(setunique) test\n");
402 
403 	 */
404 	rc = tdrmclose();
405 	if (rc) {
406 		printf("Test MOD Driver may not be closed\n");
407 		exit(1);
408 	}
409 
410 }
411