Home | History | Annotate | Download | only in user_usb
      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 #include <fcntl.h>
     21 #include <stdlib.h>
     22 #include <stdio.h>
     23 #include <errno.h>
     24 #include <sys/stat.h>
     25 #include <sys/ioctl.h>
     26 #include <linux/kernel.h>
     27 #include <linux/errno.h>
     28 #include "../tusb/tusb.h"
     29 
     30 static int tusb_fd = -1;	//file descriptor
     31 
     32 int tusbopen()
     33 {
     34 
     35 	dev_t devt;
     36 	struct stat st;
     37 	int rc = 0;
     38 
     39 	devt = makedev(TUSB_MAJOR, 0);
     40 
     41 	if (rc) {
     42 		if (errno == ENOENT) {
     43 			/* dev node does not exist. */
     44 			rc = mkdir(DEVICE_NAME, (S_IFDIR | S_IRWXU |
     45 						 S_IRGRP | S_IXGRP |
     46 						 S_IROTH | S_IXOTH));
     47 		} else {
     48 			printf
     49 			    ("ERROR: Problem with Base dev directory.  Error code from stat() is %d\n\n",
     50 			     errno);
     51 		}
     52 
     53 	} else {
     54 		if (!(st.st_mode & S_IFDIR)) {
     55 			rc = unlink(DEVICE_NAME);
     56 			if (!rc) {
     57 				rc = mkdir(DEVICE_NAME, (S_IFDIR | S_IRWXU |
     58 							 S_IRGRP | S_IXGRP |
     59 							 S_IROTH | S_IXOTH));
     60 			}
     61 		}
     62 	}
     63 
     64 	/*
     65 	 * Check for the /dev/tbase node, and create if it does not
     66 	 * exist.
     67 	 */
     68 	rc = stat(DEVICE_NAME, &st);
     69 	if (rc) {
     70 		if (errno == ENOENT) {
     71 			/* dev node does not exist */
     72 			rc = mknod(DEVICE_NAME,
     73 				   (S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
     74 				    S_IWGRP), devt);
     75 		} else {
     76 			printf
     77 			    ("ERROR:Problem with tbase device node directory.  Error code form stat() is %d\n\n",
     78 			     errno);
     79 		}
     80 
     81 	} else {
     82 		/*
     83 		 * /dev/tbase CHR device exists.  Check to make sure it is for a
     84 		 * block device and that it has the right major and minor.
     85 		 */
     86 		if ((!(st.st_mode & S_IFCHR)) || (st.st_rdev != devt)) {
     87 
     88 			/* Recreate the dev node. */
     89 			rc = unlink(DEVICE_NAME);
     90 			if (!rc) {
     91 				rc = mknod(DEVICE_NAME,
     92 					   (S_IFCHR | S_IRUSR | S_IWUSR |
     93 					    S_IRGRP | S_IWGRP), devt);
     94 			}
     95 		}
     96 	}
     97 
     98 	tusb_fd = open(DEVICE_NAME, O_RDWR);
     99 
    100 	if (tusb_fd < 0) {
    101 		printf("ERROR: Open of device %s failed %d errno = %d\n",
    102 		       DEVICE_NAME, tusb_fd, errno);
    103 		return errno;
    104 	} else {
    105 		printf("Device opened successfully \n");
    106 		return 0;
    107 	}
    108 
    109 }
    110 
    111 int tusbclose()
    112 {
    113 	/*
    114 	 * Close the tusb driver
    115 	 */
    116 	if (tusb_fd != -1) {
    117 		close(tusb_fd);
    118 		tusb_fd = -1;
    119 	}
    120 
    121 	return 0;
    122 }
    123 
    124 int main()
    125 {
    126 	int rc = 0;
    127 
    128 	rc = tusbopen();
    129 	if (rc) {
    130 		printf("tusb driver may not be loaded\n");
    131 		exit(1);
    132 	}
    133 
    134 	/* test find device pointer */
    135 	if (ki_generic(tusb_fd, FIND_DEV))
    136 		printf("Failed to find usb device pointer\n");
    137 	else
    138 		printf("Found usb device pointer\n");
    139 
    140 	/* test find usb hostcontroller */
    141 	if (ki_generic(tusb_fd, TEST_FIND_HCD))
    142 		printf("Failed to find usb hcd pointer\n");
    143 	else
    144 		printf("Found usb hcd pointer\n");
    145 
    146 	/* test hcd probe */
    147 	if (ki_generic(tusb_fd, TEST_HCD_PROBE))
    148 		printf("Failed on hcd probe call\n");
    149 	else
    150 		printf("Success hcd probe\n");
    151 
    152 	/* test hcd suspend */
    153 	if (ki_generic(tusb_fd, TEST_HCD_SUSPEND))
    154 		printf("Failed on hcd suspend call\n");
    155 	else
    156 		printf("Success hcd suspend\n");
    157 
    158 	/* test hcd resume */
    159 	if (ki_generic(tusb_fd, TEST_HCD_RESUME))
    160 		printf("Failed on hcd resume call\n");
    161 	else
    162 		printf("Success hcd resume\n");
    163 
    164 #if 0
    165 	/* test hcd remove */
    166 	if (ki_generic(tusb_fd, TEST_HCD_REMOVE))
    167 		printf("Failed on hcd remove call\n");
    168 	else
    169 		printf("Success hcd remove\n");
    170 #endif
    171 
    172 	tusbclose();
    173 
    174 	tst_exit();
    175 }
    176