6
6
#include <unistd.h>
7
7
8
8
int verbose = 0 ;
9
+ int out_ep = 1 ;
10
+ int in_ep = 2 ;
9
11
10
12
void usage (int error )
11
13
{
@@ -28,35 +30,105 @@ void usage(int error)
28
30
exit (-1 );
29
31
}
30
32
33
+ libusb_device_handle * LIBUSB_CALL open_device_with_vid (
34
+ libusb_context * ctx , uint16_t vendor_id )
35
+ {
36
+ struct libusb_device * * devs ;
37
+ struct libusb_device * found = NULL ;
38
+ struct libusb_device * dev ;
39
+ struct libusb_device_handle * handle = NULL ;
40
+ size_t i = 0 ;
41
+ int r ;
42
+
43
+ if (libusb_get_device_list (ctx , & devs ) < 0 )
44
+ return NULL ;
45
+
46
+ while ((dev = devs [i ++ ]) != NULL ) {
47
+ struct libusb_device_descriptor desc ;
48
+ r = libusb_get_device_descriptor (dev , & desc );
49
+ if (r < 0 )
50
+ goto out ;
51
+ if (verbose )
52
+ printf ("Found device %d idVendor=0x%04x idProduct=0x%04x\n" , i , desc .idVendor , desc .idProduct );
53
+ if (desc .idVendor == vendor_id ) {
54
+ if (desc .idProduct == 0x2763 ||
55
+ desc .idProduct == 0x2764 )
56
+ {
57
+ if (verbose ) printf ("Device located successfully\n" );
58
+ found = dev ;
59
+ break ;
60
+ }
61
+ }
62
+ }
63
+
64
+ if (found ) {
65
+ r = libusb_open (found , & handle );
66
+ if (r < 0 )
67
+ {
68
+ if (verbose ) printf ("Failed to open the requested device\n" );
69
+ handle = NULL ;
70
+ }
71
+ }
72
+
73
+ out :
74
+ libusb_free_device_list (devs , 1 );
75
+ return handle ;
76
+
77
+ }
31
78
32
79
int Initialize_Device (libusb_context * * ctx , libusb_device_handle * * usb_device )
33
80
{
34
81
int ret = 0 ;
82
+ int interface ;
83
+ struct libusb_config_descriptor * config ;
35
84
36
- * usb_device = libusb_open_device_with_vid_pid (* ctx , 0x0a5c , 0x2763 );
85
+ * usb_device = open_device_with_vid (* ctx , 0x0a5c );
37
86
if (* usb_device == NULL )
38
87
{
39
88
return -1 ;
40
89
}
41
90
42
- ret = libusb_claim_interface (* usb_device , 0 );
91
+ libusb_get_active_config_descriptor (libusb_get_device (* usb_device ), & config );
92
+
93
+ if (config -> bNumInterfaces == 1 )
94
+ {
95
+ interface = 0 ;
96
+ out_ep = 1 ;
97
+ in_ep = 2 ;
98
+ }
99
+ else
100
+ {
101
+ interface = 1 ;
102
+ out_ep = 3 ;
103
+ in_ep = 4 ;
104
+ }
105
+
106
+ ret = libusb_claim_interface (* usb_device , interface );
43
107
if (ret )
44
108
{
45
109
printf ("Failed to claim interface\n" );
46
110
return ret ;
47
111
}
48
112
113
+ printf ("Initialised device correctly\n" );
114
+
49
115
return ret ;
50
116
}
51
117
52
118
int ep_write (unsigned char * buf , int len , libusb_device_handle * usb_device )
53
119
{
120
+ int a_len ;
54
121
int ret =
55
122
libusb_control_transfer (usb_device , LIBUSB_REQUEST_TYPE_VENDOR , 0 ,
56
123
len & 0xffff , len >> 16 , NULL , 0 , 1000 );
57
- int a_len ;
58
124
59
- ret = libusb_bulk_transfer (usb_device , 0x01 , buf , len , & a_len , 100000 );
125
+ if (ret != 0 )
126
+ {
127
+ printf ("Failed control transfer\n" );
128
+ return ret ;
129
+ }
130
+
131
+ ret = libusb_bulk_transfer (usb_device , out_ep , buf , len , & a_len , 100000 );
60
132
if (verbose )
61
133
printf ("libusb_bulk_transfer returned %d\n" , ret );
62
134
@@ -187,12 +259,13 @@ int main(int argc, char *argv[])
187
259
exit (-1 );
188
260
}
189
261
190
- libusb_set_debug (ctx , 0 );
262
+ libusb_set_debug (ctx , verbose ? LIBUSB_LOG_LEVEL_WARNING : 0 );
191
263
192
264
do
193
265
{
194
266
FILE * fp_img = NULL ;
195
267
struct libusb_device_descriptor desc ;
268
+ struct libusb_config_descriptor * config ;
196
269
197
270
printf ("Waiting for BCM2835 ...\n" );
198
271
@@ -204,12 +277,15 @@ int main(int argc, char *argv[])
204
277
{
205
278
libusb_get_device_descriptor (libusb_get_device
206
279
(usb_device ), & desc );
280
+ printf ("Found serial number %d\n" , desc .iSerialNumber );
207
281
// Make sure we've re-enumerated since the last time
208
282
if (desc .iSerialNumber == last_serial )
209
283
{
210
284
result = -1 ;
211
285
libusb_close (usb_device );
212
286
}
287
+
288
+ libusb_get_active_config_descriptor (libusb_get_device (usb_device ), & config );
213
289
}
214
290
215
291
if (result )
@@ -275,13 +351,14 @@ int main(int argc, char *argv[])
275
351
exit (-1 );
276
352
}
277
353
354
+ sleep (1 );
278
355
size =
279
356
ep_read ((unsigned char * )& retcode , sizeof (retcode ),
280
357
usb_device );
281
358
282
359
if (retcode == 0 )
283
360
{
284
- if ( verbose ) printf ("Successful\n" );
361
+ printf ("Successful read %d bytes \n" , size );
285
362
286
363
if (fp == fp2 && executable )
287
364
{
@@ -292,6 +369,7 @@ int main(int argc, char *argv[])
292
369
printf ("Failed : 0x%x" , retcode );
293
370
294
371
libusb_close (usb_device );
372
+ sleep (1 );
295
373
}
296
374
while (fp == fp1 || loop );
297
375
0 commit comments