summaryrefslogtreecommitdiff
path: root/src/libusb_dyn.c
blob: d65d9092e14a695ebbd3f7f82dcd2d9bc57b10a0 (plain)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* LIBUSB-WIN32, Generic Windows USB Library
 * Copyright (c) 2002-2005 Stephan Meyer <ste_meyer@web.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */


#include <windows.h>
#include <errno.h>

#include "libusb_dyn.h"

#define LIBUSB_DLL_NAME "libusb0.dll"

typedef usb_dev_handle * (*usb_open_t)(struct usb_device *dev);
typedef int (*usb_close_t)(usb_dev_handle *dev);
typedef int (*usb_get_string_t)(usb_dev_handle *dev, int index, int langid, 
                                char *buf, size_t buflen);
typedef int (*usb_get_string_simple_t)(usb_dev_handle *dev, int index, 
                                       char *buf, size_t buflen);
typedef int (*usb_get_descriptor_by_endpoint_t)(usb_dev_handle *udev, int ep,
                                                unsigned char type, 
                                                unsigned char index,
                                                void *buf, int size);
typedef int (*usb_get_descriptor_t)(usb_dev_handle *udev, unsigned char type,
                                    unsigned char index, void *buf, int size);
typedef int (*usb_bulk_write_t)(usb_dev_handle *dev, int ep, char *bytes, 
                                int size, int timeout);
typedef int (*usb_bulk_read_t)(usb_dev_handle *dev, int ep, char *bytes, 
                               int size, int timeout);
typedef int (*usb_interrupt_write_t)(usb_dev_handle *dev, int ep, char *bytes,
                                     int size, int timeout);
typedef int (*usb_interrupt_read_t)(usb_dev_handle *dev, int ep, char *bytes,
                                    int size, int timeout);
typedef int (*usb_control_msg_t)(usb_dev_handle *dev, int requesttype, 
                                 int request, int value, int index, 
                                 char *bytes, int size, int timeout);
typedef int (*usb_set_configuration_t)(usb_dev_handle *dev, int configuration);
typedef int (*usb_claim_interface_t)(usb_dev_handle *dev, int interface);
typedef int (*usb_release_interface_t)(usb_dev_handle *dev, int interface);
typedef int (*usb_set_altinterface_t)(usb_dev_handle *dev, int alternate);
typedef int (*usb_resetep_t)(usb_dev_handle *dev, unsigned int ep);
typedef int (*usb_clear_halt_t)(usb_dev_handle *dev, unsigned int ep);
typedef int (*usb_reset_t)(usb_dev_handle *dev);
typedef char * (*usb_strerror_t)(void);
typedef void (*usb_init_t)(void);
typedef void (*usb_set_debug_t)(int level);
typedef int (*usb_find_busses_t)(void);
typedef int (*usb_find_devices_t)(void);
typedef struct usb_device * (*usb_device_t)(usb_dev_handle *dev);
typedef struct usb_bus * (*usb_get_busses_t)(void);
typedef int (*usb_install_service_np_t)(void);
typedef int (*usb_uninstall_service_np_t)(void);
typedef int (*usb_install_driver_np_t)(const char *inf_file);
typedef const struct usb_version * (*usb_get_version_t)(void);
typedef int (*usb_isochronous_setup_async_t)(usb_dev_handle *dev, 
                                             void **context,
                                             unsigned char ep, int pktsize);
typedef int (*usb_bulk_setup_async_t)(usb_dev_handle *dev, void **context,
                                      unsigned char ep);
typedef int (*usb_interrupt_setup_async_t)(usb_dev_handle *dev, void **context,
                                           unsigned char ep);
typedef int (*usb_submit_async_t)(void *context, char *bytes, int size);
typedef int (*usb_reap_async_t)(void *context, int timeout);
typedef int (*usb_free_async_t)(void **context);


static usb_open_t _usb_open = NULL;
static usb_close_t _usb_close = NULL;
static usb_get_string_t _usb_get_string = NULL;
static usb_get_string_simple_t _usb_get_string_simple = NULL;
static usb_get_descriptor_by_endpoint_t _usb_get_descriptor_by_endpoint = NULL;
static usb_get_descriptor_t _usb_get_descriptor = NULL;
static usb_bulk_write_t _usb_bulk_write = NULL;
static usb_bulk_read_t _usb_bulk_read = NULL;
static usb_interrupt_write_t _usb_interrupt_write = NULL;
static usb_interrupt_read_t _usb_interrupt_read = NULL;
static usb_control_msg_t _usb_control_msg = NULL;
static usb_set_configuration_t _usb_set_configuration = NULL;
static usb_claim_interface_t _usb_claim_interface = NULL;
static usb_release_interface_t _usb_release_interface = NULL;
static usb_set_altinterface_t _usb_set_altinterface = NULL;
static usb_resetep_t _usb_resetep = NULL;
static usb_clear_halt_t _usb_clear_halt = NULL;
static usb_reset_t _usb_reset = NULL;
static usb_strerror_t _usb_strerror = NULL;
static usb_init_t _usb_init = NULL;
static usb_set_debug_t _usb_set_debug = NULL;
static usb_find_busses_t _usb_find_busses = NULL;
static usb_find_devices_t _usb_find_devices = NULL;
static usb_device_t _usb_device = NULL;
static usb_get_busses_t _usb_get_busses = NULL;
static usb_install_service_np_t _usb_install_service_np = NULL;
static usb_uninstall_service_np_t _usb_uninstall_service_np = NULL;
static usb_install_driver_np_t _usb_install_driver_np = NULL;
static usb_get_version_t _usb_get_version = NULL;
static usb_isochronous_setup_async_t _usb_isochronous_setup_async = NULL;
static usb_bulk_setup_async_t _usb_bulk_setup_async = NULL;
static usb_interrupt_setup_async_t _usb_interrupt_setup_async = NULL;
static usb_submit_async_t _usb_submit_async = NULL;
static usb_reap_async_t _usb_reap_async = NULL;
static usb_free_async_t _usb_free_async = NULL;


void usb_init(void)
{
  HINSTANCE libusb_dll  = LoadLibrary(LIBUSB_DLL_NAME);
  
  if(!libusb_dll)
    return;

  timeBeginPeriod(1);
 
  _usb_open = (usb_open_t)
    GetProcAddress(libusb_dll, "usb_open");
  _usb_close = (usb_close_t)
    GetProcAddress(libusb_dll, "usb_close");
  _usb_get_string = (usb_get_string_t)
    GetProcAddress(libusb_dll, "usb_get_string");
  _usb_get_string_simple = (usb_get_string_simple_t)
    GetProcAddress(libusb_dll, "usb_get_string_simple");
  _usb_get_descriptor_by_endpoint = (usb_get_descriptor_by_endpoint_t)
    GetProcAddress(libusb_dll, "usb_get_descriptor_by_endpoint");
  _usb_get_descriptor = (usb_get_descriptor_t)
    GetProcAddress(libusb_dll, "usb_get_descriptor");
  _usb_bulk_write = (usb_bulk_write_t)
    GetProcAddress(libusb_dll, "usb_bulk_write");
  _usb_bulk_read = (usb_bulk_read_t)
    GetProcAddress(libusb_dll, "usb_bulk_read");
  _usb_interrupt_write = (usb_interrupt_write_t)
    GetProcAddress(libusb_dll, "usb_interrupt_write");
  _usb_interrupt_read = (usb_interrupt_read_t)
    GetProcAddress(libusb_dll, "usb_interrupt_read");
  _usb_control_msg = (usb_control_msg_t)
    GetProcAddress(libusb_dll, "usb_control_msg");
  _usb_set_configuration = (usb_set_configuration_t)
    GetProcAddress(libusb_dll, "usb_set_configuration");
  _usb_claim_interface = (usb_claim_interface_t)
    GetProcAddress(libusb_dll, "usb_claim_interface");
  _usb_release_interface = (usb_release_interface_t)
    GetProcAddress(libusb_dll, "usb_release_interface");
  _usb_set_altinterface = (usb_set_altinterface_t)
    GetProcAddress(libusb_dll, "usb_set_altinterface");
  _usb_resetep = (usb_resetep_t)
    GetProcAddress(libusb_dll, "usb_resetep");
  _usb_clear_halt = (usb_clear_halt_t)
    GetProcAddress(libusb_dll, "usb_clear_halt");
  _usb_reset = (usb_reset_t)
    GetProcAddress(libusb_dll, "usb_reset");
  _usb_strerror = (usb_strerror_t)
    GetProcAddress(libusb_dll, "usb_strerror");
  _usb_init = (usb_init_t)
    GetProcAddress(libusb_dll, "usb_init");
  _usb_set_debug = (usb_set_debug_t)
    GetProcAddress(libusb_dll, "usb_set_debug");
  _usb_find_busses = (usb_find_busses_t)
    GetProcAddress(libusb_dll, "usb_find_busses");
  _usb_find_devices = (usb_find_devices_t)
    GetProcAddress(libusb_dll, "usb_find_devices");
  _usb_device = (usb_device_t)
    GetProcAddress(libusb_dll, "usb_device");
  _usb_get_busses = (usb_get_busses_t)
    GetProcAddress(libusb_dll, "usb_get_busses");
  _usb_install_service_np = (usb_install_service_np_t)
    GetProcAddress(libusb_dll, "usb_install_service_np");
  _usb_uninstall_service_np = (usb_uninstall_service_np_t)
    GetProcAddress(libusb_dll, "usb_uninstall_service_np");
  _usb_install_driver_np = (usb_install_driver_np_t)
    GetProcAddress(libusb_dll, "usb_install_driver_np");
  _usb_get_version = (usb_get_version_t)
    GetProcAddress(libusb_dll, "usb_get_version");
  _usb_isochronous_setup_async = (usb_isochronous_setup_async_t)
    GetProcAddress(libusb_dll, "usb_isochronous_setup_async");
  _usb_bulk_setup_async = (usb_bulk_setup_async_t)
    GetProcAddress(libusb_dll, "usb_bulk_setup_async");
  _usb_interrupt_setup_async = (usb_interrupt_setup_async_t)
    GetProcAddress(libusb_dll, "usb_interrupt_setup_async");
  _usb_submit_async = (usb_submit_async_t)
    GetProcAddress(libusb_dll, "usb_submit_async");
  _usb_reap_async = (usb_reap_async_t)
    GetProcAddress(libusb_dll, "usb_reap_async");
  _usb_free_async = (usb_free_async_t)
    GetProcAddress(libusb_dll, "usb_free_async");

  if(_usb_init)
    _usb_init();
}

usb_dev_handle *usb_open(struct usb_device *dev)
{
  if(_usb_open)
    return _usb_open(dev);
  else
    return NULL;
}

int usb_close(usb_dev_handle *dev)
{
  if(_usb_close)
    return _usb_close(dev);
  else
    return -ENOFILE;
}

int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf,
                   size_t buflen)
{
  if(_usb_get_string)
    return _usb_get_string(dev, index, langid, buf, buflen);
  else
    return -ENOFILE;
}

int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf,
                          size_t buflen)
{
  if(_usb_get_string_simple)
    return _usb_get_string_simple(dev, index, buf, buflen);
  else
    return -ENOFILE;
}

int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep,
                                   unsigned char type, unsigned char index,
                                   void *buf, int size)
{
  if(_usb_get_descriptor_by_endpoint)
    return _usb_get_descriptor_by_endpoint(udev, ep, type, index, buf, size);
  else
    return -ENOFILE;
}

int usb_get_descriptor(usb_dev_handle *udev, unsigned char type,
                       unsigned char index, void *buf, int size)
{
  if(_usb_get_descriptor)
    return _usb_get_descriptor(udev, type, index, buf, size);
  else
    return -ENOFILE;
}

int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
                   int timeout)
{
  if(_usb_bulk_write)
    return _usb_bulk_write(dev, ep, bytes, size, timeout);
  else
    return -ENOFILE;
}

int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
                  int timeout)
{
  if(_usb_bulk_read)
    return _usb_bulk_read(dev, ep, bytes, size, timeout);
  else
    return -ENOFILE;
}

int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
                        int timeout)
{
  if(_usb_interrupt_write)
    return _usb_interrupt_write(dev, ep, bytes, size, timeout);
  else
    return -ENOFILE;
}

int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,
                       int timeout)
{
  if(_usb_interrupt_read)
    return _usb_interrupt_read(dev, ep, bytes, size, timeout);
  else
    return -ENOFILE;
}

int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
                    int value, int index, char *bytes, int size, 
                    int timeout)
{
  if(_usb_control_msg)
    return _usb_control_msg(dev, requesttype, request, value, index, bytes, 
                            size, timeout);
  else
    return -ENOFILE;
}

int usb_set_configuration(usb_dev_handle *dev, int configuration)
{
  if(_usb_set_configuration)
    return _usb_set_configuration(dev, configuration);
  else
    return -ENOFILE;
}

int usb_claim_interface(usb_dev_handle *dev, int interface)
{
  if(_usb_claim_interface)
    return _usb_claim_interface(dev, interface);
  else
    return -ENOFILE;
}

int usb_release_interface(usb_dev_handle *dev, int interface)
{
  if(_usb_release_interface)
    return _usb_release_interface(dev, interface);
  else
    return -ENOFILE;
}

int usb_set_altinterface(usb_dev_handle *dev, int alternate)
{
  if(_usb_set_altinterface)
    return _usb_set_altinterface(dev, alternate);
  else
    return -ENOFILE;
}

int usb_resetep(usb_dev_handle *dev, unsigned int ep)
{
  if(_usb_resetep)
    return _usb_resetep(dev, ep);
  else
    return -ENOFILE;
}

int usb_clear_halt(usb_dev_handle *dev, unsigned int ep)
{
  if(_usb_clear_halt)
    return _usb_clear_halt(dev, ep);
  else
    return -ENOFILE;
}

int usb_reset(usb_dev_handle *dev)
{
  if(_usb_reset)
    return _usb_reset(dev);
  else
    return -ENOFILE;
}

char *usb_strerror(void)
{
  if(_usb_strerror)
    return _usb_strerror();
  else
    return NULL;
}

void usb_set_debug(int level)
{
  if(_usb_set_debug)
    return _usb_set_debug(level);
}

int usb_find_busses(void)
{
  if(_usb_find_busses)
    return _usb_find_busses();
  else
    return -ENOFILE;
}

int usb_find_devices(void)
{
  if(_usb_find_devices)
    return _usb_find_devices();
  else
    return -ENOFILE;
}

struct usb_device *usb_device(usb_dev_handle *dev)
{
  if(_usb_device)
    return _usb_device(dev);
  else
    return NULL;
}

struct usb_bus *usb_get_busses(void)
{
  if(_usb_get_busses)
    return _usb_get_busses();
  else
    return NULL;
}

int usb_install_service_np(void)
{
  if(_usb_install_service_np)
    return _usb_install_service_np();
  else
    return -ENOFILE;
}

int usb_uninstall_service_np(void)
{
  if(_usb_uninstall_service_np)
    return _usb_uninstall_service_np();
  else
    return -ENOFILE;
}

int usb_install_driver_np(const char *inf_file)
{
  if(_usb_install_driver_np)
    return _usb_install_driver_np(inf_file);
  else
    return -ENOFILE;
}

const struct usb_version *usb_get_version(void)
{
  if(_usb_get_version)
    return _usb_get_version();
  else
    return NULL;
}

int usb_isochronous_setup_async(usb_dev_handle *dev, void **context,
                                unsigned char ep, int pktsize)
{
  if(_usb_isochronous_setup_async)
    return _usb_isochronous_setup_async(dev, context, ep, pktsize);
  else
    return -ENOFILE;
}

int usb_bulk_setup_async(usb_dev_handle *dev, void **context,
                         unsigned char ep)
{
  if(_usb_bulk_setup_async)
    return _usb_bulk_setup_async(dev, context, ep);
  else
    return -ENOFILE;
}

int usb_interrupt_setup_async(usb_dev_handle *dev, void **context,
                              unsigned char ep)
{
  if(_usb_interrupt_setup_async)
    return _usb_interrupt_setup_async(dev, context, ep);
  else
    return -ENOFILE;
}

int usb_submit_async(void *context, char *bytes, int size)
{
  if(_usb_submit_async)
    return _usb_submit_async(context, bytes, size);
  else
    return -ENOFILE;
}

int usb_reap_async(void *context, int timeout)
{
  if(_usb_reap_async)
    return _usb_reap_async(context, timeout);
  else
    return -ENOFILE;
}

int usb_free_async(void **context)
{
  if(_usb_free_async)
    return _usb_free_async(context);
  else
    return -ENOFILE;
}
personal git repositories of Harald Welte. Your mileage may vary