File: libusb-1.0.patch

package info (click to toggle)
garmin-forerunner-tools 0.10repacked-14
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,500 kB
  • sloc: sh: 9,153; ansic: 8,577; perl: 229; makefile: 66; python: 37
file content (328 lines) | stat: -rw-r--r-- 10,741 bytes parent folder | download | duplicates (4)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
Description: switch to libusb-1.0
Author: Olaf Meeuwissen <paddy-hack@member.fsf.org>
Debian-Bug: #810414

diff --git a/configure.ac b/configure.ac
index 999efd2..99cc357 100644
--- a/configure.ac
+++ b/configure.ac
@@ -9,6 +9,7 @@ AM_INIT_AUTOMAKE
 AC_PROG_CC
 AC_PROG_INSTALL
 AC_PROG_LIBTOOL
+PKG_PROG_PKG_CONFIG
 
 # Checks for header files.
 AC_HEADER_STDC
@@ -27,13 +28,9 @@ AC_CHECK_FUNCS([memset strerror])
 AC_C_BIGENDIAN
 
 # Checks for libusb
-AC_PATH_PROG(LIBUSB_CONFIG,libusb-config)
-if test -n "${LIBUSB_CONFIG}"; then
-   USB_LIBS="`$LIBUSB_CONFIG --libs`"
-   USB_CFLAGS="`$LIBUSB_CONFIG --cflags`"
-else
-   AC_MSG_ERROR(cannot build garmintools without libusb)
-fi
+PKG_CHECK_MODULES([USB],[libusb-1.0],
+        [],
+        [AC_MSG_ERROR(cannot build garmintools without libusb)])
 
 # Checks for python
 AC_ARG_WITH(python,[  --with-python		  compile python bindings.],
diff --git a/src/garmin.h b/src/garmin.h
index 540ac38..c24a8bd 100644
--- a/src/garmin.h
+++ b/src/garmin.h
@@ -22,7 +22,7 @@
 
 
 #include <stdio.h>
-#include <usb.h>
+#include <libusb.h>
 #include <math.h>
 
 
@@ -1925,7 +1925,7 @@ typedef struct garmin_datatypes {
 
 
 typedef struct garmin_usb {
-  usb_dev_handle *          handle;
+  libusb_device_handle *    handle;
   int                       bulk_out;
   int                       bulk_in;
   int                       intr_in;
diff --git a/src/usb_comm.c b/src/usb_comm.c
index 6630784..214f274 100644
--- a/src/usb_comm.c
+++ b/src/usb_comm.c
@@ -21,13 +21,14 @@
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
-#include <usb.h>
+#include <libusb.h>
 #include "garmin.h"
 
 
 #define INTR_TIMEOUT  3000
 #define BULK_TIMEOUT  3000
 
+static libusb_context *ctx = NULL;
 
 /* Close the USB connection with the Garmin device. */
 
@@ -35,8 +36,8 @@ int
 garmin_close ( garmin_unit * garmin )
 {
   if ( garmin->usb.handle != NULL ) {
-    usb_release_interface(garmin->usb.handle,0);
-    usb_close(garmin->usb.handle);
+    libusb_release_interface(garmin->usb.handle,0);
+    libusb_close(garmin->usb.handle);
     garmin->usb.handle = NULL;
   }
 
@@ -53,54 +54,76 @@ garmin_close ( garmin_unit * garmin )
 int
 garmin_open ( garmin_unit * garmin )
 {
-  struct usb_bus *     bi;
-  struct usb_device *  di;
+  libusb_device **     dl;
+  libusb_device *      di;
+  int                  cnt;
   int                  err = 0;
   int                  i;
 
   if ( garmin->usb.handle == NULL ) {
-    usb_init();
-    usb_find_busses();
-    usb_find_devices();
+    if ( ctx == NULL ) {
+      err = libusb_init(&ctx);
+      if ( err ) {
+        printf("libusb_init failed: %s\n", libusb_error_name(err));
+        return ( garmin->usb.handle != NULL );
+      } else if ( garmin->verbose != 0 ) {
+        printf("[garmin] libusb_init succeeded\n");
+      }
+    }
+    cnt = libusb_get_device_list(ctx,&dl);
     
-    for ( bi = usb_busses; bi != NULL; bi = bi->next ) {
-      for ( di = bi->devices; di != NULL; di = di->next ) {
-	if ( di->descriptor.idVendor  == GARMIN_USB_VID &&
-	     di->descriptor.idProduct == GARMIN_USB_PID ) {
-
-	  if ( garmin->verbose != 0 ) {
-	    printf("[garmin] found VID %04x, PID %04x on %s/%s\n",
-		   di->descriptor.idVendor,
-		   di->descriptor.idProduct,
-		   bi->dirname,
-		   di->filename);
-	  }
+    for (i = 0; i < cnt; ++i) {
+      {
+        struct libusb_device_descriptor descriptor;
+        struct libusb_config_descriptor *config;
 
-	  garmin->usb.handle = usb_open(di);
-	  garmin->usb.read_bulk = 0;
+        di = dl[i];
+        err = libusb_get_device_descriptor (di, &descriptor);
 
-	  err = 0;
+        if ( !err &&
+             descriptor.idVendor  == GARMIN_USB_VID &&
+             descriptor.idProduct == GARMIN_USB_PID ) {
 
-	  if ( garmin->usb.handle == NULL ) {
-	    printf("usb_open failed: %s\n",usb_strerror());
-	    err = 1;
-	  } else if ( !err && garmin->verbose != 0 ) {
-	    printf("[garmin] usb_open = %p\n",garmin->usb.handle);
+	  if ( garmin->verbose != 0 ) {
+	    printf("[garmin] found VID %04x, PID %04x",
+		   descriptor.idVendor,
+		   descriptor.idProduct);
 	  }
 
-	  if ( !err && usb_set_configuration(garmin->usb.handle,1) < 0 ) {
-	    printf("usb_set_configuration failed: %s\n",usb_strerror());
-	    err = 1;
-	  } else if ( !err && garmin->verbose != 0 ) {
-	    printf("[garmin] usb_set_configuration[1] succeeded\n");
-	  }
+	  err = libusb_open(di,&garmin->usb.handle);
+	  garmin->usb.read_bulk = 0;
 
-	  if ( !err && usb_claim_interface(garmin->usb.handle,0) < 0 ) {
-	    printf("usb_claim_interface failed: %s\n",usb_strerror());
-	    err = 1;
-	  } else if ( !err && garmin->verbose != 0 ) {
-	    printf("[garmin] usb_claim_interface[0] succeeded\n");
-	  }
+	  if ( err ) {
+	    printf("libusb_open failed: %s\n",libusb_error_name(err));
+            garmin->usb.handle = NULL;
+	  } else if ( garmin->verbose != 0 ) {
+	    printf("[garmin] libusb_open = %p\n",garmin->usb.handle);
+
+            err = libusb_set_configuration(garmin->usb.handle,1);
+            if ( err ) {
+              printf("libusb_set_configuration failed: %s\n",
+                     libusb_error_name(err));
+            } else if ( garmin->verbose != 0 ) {
+              printf("[garmin] libusb_set_configuration[1] succeeded\n");
+
+              err = libusb_claim_interface(garmin->usb.handle,0);
+              if ( err ) {
+                printf("libusb_claim_interface failed: %s\n",
+                       libusb_error_name(err));
+              } else if ( garmin->verbose != 0 ) {
+                printf("[garmin] libusb_claim_interface[0] succeeded\n");
+
+                err = libusb_get_config_descriptor_by_value(di,1,&config);
+                if ( err ) {
+                  printf("libusb_get_config_descriptor_by_value failed: %s\n",
+                         libusb_error_name(err));
+                } else if ( garmin->verbose != 0 ) {
+                  printf("[garmin] libusb_get_config_descriptor_by_value "
+                         "succeeded\n");
+                }
+              }
+            }
+          }
 
 	  if ( !err ) {
 
@@ -110,31 +133,31 @@ garmin_open ( garmin_unit * garmin )
 	    */
 
 	    for ( i = 0; 
-		  i < di->config->interface->altsetting->bNumEndpoints; 
+		  i < config->interface->altsetting->bNumEndpoints;
 		  i++ ) {
-	      struct usb_endpoint_descriptor * ep;
+	      const struct libusb_endpoint_descriptor * ep;
 	      
-	      ep = &di->config->interface->altsetting->endpoint[i];
-	      switch ( ep->bmAttributes & USB_ENDPOINT_TYPE_MASK ) {
-	      case USB_ENDPOINT_TYPE_BULK:
-		if ( ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK ) {
+	      ep = &config->interface->altsetting->endpoint[i];
+	      switch ( ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK ) {
+	      case LIBUSB_TRANSFER_TYPE_BULK:
+		if ( ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK ) {
 		  garmin->usb.bulk_in = 
-		    ep->bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
+		    ep->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK;
 		  if ( garmin->verbose != 0 ) {
 		    printf("[garmin] bulk IN  = %d\n",garmin->usb.bulk_in);
 		  }
 		} else {
 		  garmin->usb.bulk_out = 
-		    ep->bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
+		    ep->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK;
 		  if ( garmin->verbose != 0 ) {
 		    printf("[garmin] bulk OUT = %d\n",garmin->usb.bulk_out);
 		  }
 		}
 		break;
-	      case USB_ENDPOINT_TYPE_INTERRUPT:
-		if ( ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK ) {
+	      case LIBUSB_TRANSFER_TYPE_INTERRUPT:
+		if ( ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK ) {
 		  garmin->usb.intr_in = 
-		    ep->bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK;
+		    ep->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK;
 		  if ( garmin->verbose != 0 ) {
 		    printf("[garmin] intr IN  = %d\n",garmin->usb.intr_in);
 		  }
@@ -154,6 +177,7 @@ garmin_open ( garmin_unit * garmin )
 
       if ( garmin->usb.handle != NULL ) break;
     }
+    libusb_free_device_list (dl, 1);
   }
 
   /* 
@@ -164,9 +188,9 @@ garmin_open ( garmin_unit * garmin )
 
   if ( garmin->usb.handle != NULL && err != 0 ) {
     if ( garmin->verbose != 0 ) {
-      printf("[garmin] (err = %d) usb_close(%p)\n",err,garmin->usb.handle);
+      printf("[garmin] (err = %d) libusb_close(%p)\n",err,garmin->usb.handle);
     }
-    usb_close(garmin->usb.handle);
+    libusb_close(garmin->usb.handle);
     garmin->usb.handle = NULL;
   }
 
@@ -242,11 +266,12 @@ garmin_read ( garmin_unit * garmin, garmin_packet * p )
 
   if ( garmin->usb.handle != NULL ) {
     if ( garmin->usb.read_bulk == 0 ) {
-      r = usb_interrupt_read(garmin->usb.handle,
-			     garmin->usb.intr_in,
-			     p->data,
-			     sizeof(garmin_packet),
-			     INTR_TIMEOUT);
+      libusb_interrupt_transfer(garmin->usb.handle,
+                                garmin->usb.intr_in,
+                                (unsigned char *) p->data,
+                                sizeof(garmin_packet),
+                                &r,
+                                INTR_TIMEOUT);
       /* 
 	 If the packet is a "Pid_Data_Available" packet, we need to read
 	 from the bulk endpoint until we get an empty packet.
@@ -261,11 +286,12 @@ garmin_read ( garmin_unit * garmin, garmin_packet * p )
       }
       
     } else {
-      r = usb_bulk_read(garmin->usb.handle,
-			garmin->usb.bulk_in,
-			p->data,
-			sizeof(garmin_packet),
-			BULK_TIMEOUT);
+      libusb_bulk_transfer(garmin->usb.handle,
+                           garmin->usb.bulk_in,
+                           (unsigned char *) p->data,
+                           sizeof(garmin_packet),
+                           &r,
+                           BULK_TIMEOUT);
     }
   }
 
@@ -280,6 +306,7 @@ garmin_read ( garmin_unit * garmin, garmin_packet * p )
 int
 garmin_write ( garmin_unit * garmin, garmin_packet * p )
 {
+  int err = 0;
   int r = -1;
   int s = garmin_packet_size(p) + PACKET_HEADER_SIZE;
 
@@ -291,13 +318,14 @@ garmin_write ( garmin_unit * garmin, garmin_packet * p )
       garmin_print_packet(p,GARMIN_DIR_WRITE,stdout);
     }
 
-    r = usb_bulk_write(garmin->usb.handle,
-		       garmin->usb.bulk_out,
-		       p->data,
-		       s,
-		       BULK_TIMEOUT);
+    err = libusb_bulk_transfer(garmin->usb.handle,
+                               garmin->usb.bulk_out,
+                               (unsigned char *) p->data,
+                               s,
+                               &r,
+                               BULK_TIMEOUT);
     if ( r != s ) {
-      printf("usb_bulk_write failed: %s\n",usb_strerror());
+      printf("libusb_bulk_write failed: %s\n",libusb_error_name(err));
       exit(1);
     }
   }