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