-
Notifications
You must be signed in to change notification settings - Fork 0
/
uda.c
172 lines (138 loc) · 3.43 KB
/
uda.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* uda.c - Enter USB testmodes
*
* Copyright (C) 2016 Felipe Balbi <felipe.balbi@linux.intel.com>
*
* This file is part of the USB Verification Tools Project
*
* USB Tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public Liicense as published by
* the Free Software Foundation, either version 3 of the license, or
* (at your option) any later version.
*
* USB Tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with USB Tools. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <libusb-1.0/libusb.h>
#define __maybe_unused __attribute__((unused))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define DEFAULT_TIMEOUT 5000 /* ms */
#define X_SIZE 1920
#define Y_SIZE 1080
#define FRAME_SIZE ((X_SIZE) * (Y_SIZE))
#define UV_SIZE ((FRAME_SIZE) / 2)
#define FULL_SIZE ((FRAME_SIZE) + (UV_SIZE))
static unsigned char frame[FULL_SIZE];
#define OPTION(n, h, v) \
{ \
.name = #n, \
.has_arg = h, \
.val = v, \
}
static struct option testmode_opts[] = {
OPTION("device", 1, 'D'),
OPTION("help", 0, 'h'),
{ } /* Terminating entry */
};
static int do_test(libusb_device_handle *udevh)
{
int transferred;
int ret;
int i;
ret = libusb_claim_interface(udevh, 0);
if (ret < 0) {
fprintf(stderr, "Can't claim interface 0\n");
return ret;
}
for (i = 0; i < 10; i++) {
ret = libusb_bulk_transfer(udevh, 1, frame, FULL_SIZE,
&transferred, DEFAULT_TIMEOUT);
if (ret < 0) {
fprintf(stderr, "failed %d/%d -> %s\n", transferred,
FULL_SIZE, libusb_error_name(ret));
libusb_release_interface(udevh, 0);
return ret;
}
if (transferred < FULL_SIZE) {
fprintf(stderr, "Couldn't transfer all bytes %d/%d",
transferred, FULL_SIZE);
libusb_release_interface(udevh, 0);
return -1;
}
}
return 0;
}
static void usage(const char *cmd)
{
fprintf(stdout, "Usage: %s -D vid:pid [-h]\n", cmd);
}
int main(int argc, char *argv[])
{
libusb_context *context;
libusb_device_handle *udevh;
unsigned vid = 0xaaaa;
unsigned pid = 0xbbbb;
int ret = 0;
int i;
while (1) {
int optidx;
int opt;
char *token;
opt = getopt_long(argc, argv, "hD:", testmode_opts, &optidx);
if (opt == -1)
break;
switch (opt) {
case 'D':
token = strtok(optarg, ":");
vid = strtoul(token, NULL, 16);
token = strtok(NULL, ":");
pid = strtoul(token, NULL, 16);
break;
case 'h':
usage(argv[0]);
exit(0);
break;
default:
exit(-1);
}
}
libusb_init(&context);
udevh = libusb_open_device_with_vid_pid(context, vid, pid);
if (!udevh) {
perror("couldn't open device");
ret = -ENODEV;
goto out0;
}
for (i = 0; i < FULL_SIZE; i++)
frame[i] = (i % 94) + 33; /* only printable ascii characters */
ret = do_test(udevh);
if (ret < 0) {
printf("failed\n");
goto out1;
}
printf("passed\n");
out1:
libusb_close(udevh);
out0:
libusb_exit(context);
return ret;
}