about summary refs log tree commit diff stats
path: root/src/wrapped/wrappedlibusb1.c
blob: 683931be6fa07017d276c298501a334b697e8475 (plain) (blame)
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
#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

#include "wrappedlibs.h"

#include "debug.h"
#include "wrapper.h"
#include "bridge.h"
#include "librarian/library_private.h"
#include "x64emu.h"
#include "emu/x64emu_private.h"
#include "callback.h"
#include "librarian.h"
#include "box64context.h"
#include "emu/x64emu_private.h"
#include "myalign.h"

const char* libusb1Name = "libusb-1.0.so.0";
#define ALTNAME "libusb-1.0.so"

#define LIBNAME libusb1

#define ADDED_FUNCTIONS()           \

#include "generated/wrappedlibusb1types.h"

#include "wrappercallback.h"

#define SUPER() \
GO(0)   \
GO(1)   \
GO(2)   \
GO(3)   \
GO(4)   \
GO(5)   \
GO(6)   \
GO(7)   \
GO(8)   \
GO(9)   \


// hotplug
#define GO(A)   \
static uintptr_t my_hotplug_fct_##A = 0;                                                    \
static int my_hotplug_##A(void* ctx, void* device, int event, void* data)                   \
{                                                                                           \
    return (int)RunFunctionFmt(my_hotplug_fct_##A, "ppip", ctx, device, event, data); \
}
SUPER()
#undef GO
static void* findhotplugFct(void* fct)
{
    if(!fct) return fct;
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_hotplug_fct_##A == (uintptr_t)fct) return my_hotplug_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_hotplug_fct_##A == 0) {my_hotplug_fct_##A = (uintptr_t)fct; return my_hotplug_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libusb-1.0 hotplug callback (%p)\n", fct);
    return NULL;
}
// transfert
#define GO(A)   \
static uintptr_t my_transfert_fct_##A = 0;                      \
static void my_transfert_##A(void* ctx)                         \
{                                                               \
    RunFunctionFmt(my_transfert_fct_##A, "p", ctx);       \
}
SUPER()
#undef GO
static void* findtransfertFct(void* fct)
{
    if(!fct) return fct;
    #define GO(A) if(my_transfert_##A == fct) return my_transfert_##A;
    SUPER()
    #undef GO
    if(GetNativeFnc((uintptr_t)fct))  return GetNativeFnc((uintptr_t)fct);
    #define GO(A) if(my_transfert_fct_##A == (uintptr_t)fct) return my_transfert_##A;
    SUPER()
    #undef GO
    #define GO(A) if(my_transfert_fct_##A == 0) {my_transfert_fct_##A = (uintptr_t)fct; return my_transfert_##A; }
    SUPER()
    #undef GO
    printf_log(LOG_NONE, "Warning, no more slot for libusb-1.0 transfert callback (%p)\n", fct);
    return NULL;
}
static void* reverse_transfert_Fct(void* fct)
{
    if(!fct) return fct;
    if(CheckBridged(my_lib->w.bridge, fct))
        return (void*)CheckBridged(my_lib->w.bridge, fct);
    #define GO(A) if(my_transfert_##A == fct) return (void*)my_transfert_fct_##A;
    SUPER()
    #undef GO
    return (void*)AddBridge(my_lib->w.bridge, vFp, fct, 0, NULL);
}

#undef SUPER

EXPORT int my_libusb_hotplug_register_callback(x64emu_t* emu, void* ctx, int event, int flags, int vendor, int product, int dev_class, void* f, void* data, void* handle)
{
    return my->libusb_hotplug_register_callback(ctx, event, flags, vendor, product, dev_class, findhotplugFct(f), data, handle);
}

struct my_libusb_iso_packet_descriptor {
    unsigned int length;
    unsigned int actual_length;
    int status;
};

typedef struct my_libusb_transfer_s {
    void* dev_handle;
    uint8_t flags;
    unsigned char endpoint;
    unsigned char type;
    unsigned int timeout;
    int status;
    int length;
    int actual_length;
    void* callback;
    void* user_data;
    unsigned char *buffer;
    int num_iso_packets;
    struct my_libusb_iso_packet_descriptor iso_packet_desc[0];
} my_libusb_transfer_t;

EXPORT void* my_libusb_alloc_transfer(x64emu_t* emu, int num)
{
    my_libusb_transfer_t* ret = (my_libusb_transfer_t*)my->libusb_alloc_transfer(num);
    if(ret)
        ret->callback = reverse_transfert_Fct(ret->callback);
    return ret;
}

EXPORT int my_libusb_submit_transfer(x64emu_t* emu, my_libusb_transfer_t* t)
{
    t->callback = findtransfertFct(t->callback);
    return my->libusb_submit_transfer(t); // don't put back callback, it's unknown if it's safe
}

EXPORT int my_libusb_cancel_transfer(x64emu_t* emu, my_libusb_transfer_t* t)
{
    t->callback = findtransfertFct(t->callback);
    return my->libusb_cancel_transfer(t);
}

EXPORT void my_libusb_free_transfer(x64emu_t* emu, my_libusb_transfer_t* t)
{
    t->callback = findtransfertFct(t->callback);
    my->libusb_free_transfer(t);
}

#include "wrappedlib_init.h"