cdc711dc |
1 | #if defined(__GNUC__) && !defined(__STRICT_ANSI__) |
2 | |
3 | #define _GNU_SOURCE 1 |
4 | |
5 | #if defined(RTLD_NEXT) |
6 | #define REAL_LIBC RTLD_NEXT |
7 | #else |
8 | #define REAL_LIBC ((void *) -1L) |
9 | #endif |
10 | |
11 | #include <dlfcn.h> |
12 | #include <stdarg.h> |
13 | #include <stdlib.h> |
14 | #include <string.h> |
15 | #include <unistd.h> |
16 | #include <fcntl.h> |
17 | #include <sys/types.h> |
18 | #include <sys/stat.h> |
19 | #include <sys/time.h> |
20 | #include <stdio.h> |
f10480d1 |
21 | #include <usb.h> |
2a7af812 |
22 | #include <signal.h> |
533f4b68 |
23 | #include <pthread.h> |
da3ba95a |
24 | #include "xilinx.h" |
cdc711dc |
25 | |
9c9fd67c |
26 | static int (*ioctl_func) (int, int, void *) = NULL; |
2c2119eb |
27 | static int windrvrfd = 0; |
dbda1264 |
28 | FILE *modulesfp; |
723d9aa0 |
29 | static int modules_read = 0; |
f10480d1 |
30 | static struct usb_bus *busses = NULL; |
2a7af812 |
31 | static struct usb_device *usbdevice; |
32 | static usb_dev_handle *usb_devhandle = NULL; |
292160ed |
33 | static unsigned long card_type; |
ca18111b |
34 | static int ints_enabled = 0; |
533f4b68 |
35 | static pthread_mutex_t int_wait = PTHREAD_MUTEX_INITIALIZER; |
292160ed |
36 | |
795992ad |
37 | #define NO_WINDRVR 1 |
723d9aa0 |
38 | #undef DEBUG |
39 | |
40 | void hexdump(unsigned char *buf, int len) { |
41 | int i; |
42 | |
43 | for(i=0; i<len; i++) { |
44 | fprintf(stderr,"%02x ", buf[i]); |
45 | if ((i % 16) == 15) |
46 | fprintf(stderr,"\n"); |
47 | } |
48 | } |
cdc711dc |
49 | |
f95f1d2e |
50 | int usb_deviceinfo(unsigned char *buf) { |
51 | int i,j,k,l; |
52 | int len = 0; |
3664a3e3 |
53 | WDU_CONFIGURATION **pConfigs, **pActiveConfig; |
54 | WDU_INTERFACE **pActiveInterface; |
f95f1d2e |
55 | |
56 | if (buf) { |
57 | struct usb_device_info *udi = (struct usb_device_info*)(buf+len); |
58 | |
59 | udi->Descriptor.bLength = sizeof(WDU_DEVICE_DESCRIPTOR); |
2a7af812 |
60 | udi->Descriptor.bDescriptorType = usbdevice->descriptor.bDescriptorType; |
61 | udi->Descriptor.bcdUSB = usbdevice->descriptor.bcdUSB; |
62 | udi->Descriptor.bDeviceClass = usbdevice->descriptor.bDeviceClass; |
63 | udi->Descriptor.bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass; |
64 | udi->Descriptor.bDeviceProtocol = usbdevice->descriptor.bDeviceProtocol; |
65 | udi->Descriptor.bMaxPacketSize0 = usbdevice->descriptor.bMaxPacketSize0; |
66 | udi->Descriptor.idVendor = usbdevice->descriptor.idVendor; |
67 | udi->Descriptor.idProduct = usbdevice->descriptor.idProduct; |
68 | udi->Descriptor.bcdDevice = usbdevice->descriptor.bcdDevice; |
69 | udi->Descriptor.iManufacturer = usbdevice->descriptor.iManufacturer; |
70 | udi->Descriptor.iProduct = usbdevice->descriptor.iProduct; |
71 | udi->Descriptor.iSerialNumber = usbdevice->descriptor.iSerialNumber; |
72 | udi->Descriptor.bNumConfigurations = usbdevice->descriptor.bNumConfigurations; |
f95f1d2e |
73 | |
74 | /* TODO: Fix Pipe0! */ |
75 | udi->Pipe0.dwNumber = 0x00; |
2a7af812 |
76 | udi->Pipe0.dwMaximumPacketSize = usbdevice->descriptor.bMaxPacketSize0; |
f95f1d2e |
77 | udi->Pipe0.type = 0; |
78 | udi->Pipe0.direction = 3; |
79 | udi->Pipe0.dwInterval = 0; |
3664a3e3 |
80 | |
81 | pConfigs = &(udi->pConfigs); |
82 | pActiveConfig = &(udi->pActiveConfig); |
83 | pActiveInterface = &(udi->pActiveInterface[0]); |
f95f1d2e |
84 | } |
85 | |
86 | len = sizeof(struct usb_device_info); |
87 | |
2a7af812 |
88 | for (i=0; i<usbdevice->descriptor.bNumConfigurations; i++) |
f95f1d2e |
89 | { |
2a7af812 |
90 | struct usb_config_descriptor *conf_desc = &usbdevice->config[i]; |
3664a3e3 |
91 | WDU_INTERFACE **pInterfaces; |
92 | WDU_ALTERNATE_SETTING **pAlternateSettings[conf_desc->bNumInterfaces]; |
93 | WDU_ALTERNATE_SETTING **pActiveAltSetting[conf_desc->bNumInterfaces]; |
94 | |
f95f1d2e |
95 | if (buf) { |
96 | WDU_CONFIGURATION *cfg = (WDU_CONFIGURATION*)(buf+len); |
97 | |
3664a3e3 |
98 | *pConfigs = cfg; |
99 | *pActiveConfig = cfg; |
100 | |
f95f1d2e |
101 | cfg->Descriptor.bLength = conf_desc->bLength; |
102 | cfg->Descriptor.bDescriptorType = conf_desc->bDescriptorType; |
103 | cfg->Descriptor.wTotalLength = conf_desc->wTotalLength; |
104 | cfg->Descriptor.bNumInterfaces = conf_desc->bNumInterfaces; |
105 | cfg->Descriptor.bConfigurationValue = conf_desc->bConfigurationValue; |
106 | cfg->Descriptor.iConfiguration = conf_desc->iConfiguration; |
107 | cfg->Descriptor.bmAttributes = conf_desc->bmAttributes; |
108 | cfg->Descriptor.MaxPower = conf_desc->MaxPower; |
109 | |
110 | cfg->dwNumInterfaces = conf_desc->bNumInterfaces; |
3664a3e3 |
111 | |
112 | pInterfaces = &(cfg->pInterfaces); |
f95f1d2e |
113 | } |
114 | len += sizeof(WDU_CONFIGURATION); |
3664a3e3 |
115 | |
f95f1d2e |
116 | if (buf) { |
3664a3e3 |
117 | *pInterfaces = (WDU_INTERFACE*)(buf+len); |
f95f1d2e |
118 | for (j=0; j<conf_desc->bNumInterfaces; j++) { |
119 | WDU_INTERFACE *iface = (WDU_INTERFACE*)(buf+len); |
3664a3e3 |
120 | |
121 | pActiveInterface[j] = iface; |
122 | |
123 | pAlternateSettings[j] = &(iface->pAlternateSettings); |
2a7af812 |
124 | iface->dwNumAltSettings = usbdevice->config[i].interface[j].num_altsetting; |
3664a3e3 |
125 | pActiveAltSetting[j] = &(iface->pActiveAltSetting); |
f95f1d2e |
126 | |
127 | len += sizeof(WDU_INTERFACE); |
128 | } |
129 | } else { |
130 | len += sizeof(WDU_INTERFACE) * conf_desc->bNumInterfaces; |
131 | } |
132 | |
133 | for (j=0; j<conf_desc->bNumInterfaces; j++) |
134 | { |
2a7af812 |
135 | struct usb_interface *interface = &usbdevice->config[i].interface[j]; |
3664a3e3 |
136 | |
137 | if (buf) { |
138 | *pAlternateSettings[j] = (WDU_ALTERNATE_SETTING*)(buf+len); |
139 | /* FIXME: */ |
140 | *pActiveAltSetting[j] = (WDU_ALTERNATE_SETTING*)(buf+len); |
141 | } |
142 | |
f95f1d2e |
143 | for(k=0; k<interface->num_altsetting; k++) |
144 | { |
3664a3e3 |
145 | unsigned char bNumEndpoints = interface->altsetting[k].bNumEndpoints; |
146 | WDU_ENDPOINT_DESCRIPTOR **pEndpointDescriptors; |
147 | WDU_PIPE_INFO **pPipes; |
148 | |
f95f1d2e |
149 | if (buf) { |
150 | WDU_ALTERNATE_SETTING *altset = (WDU_ALTERNATE_SETTING*)(buf+len); |
151 | |
152 | altset->Descriptor.bLength = interface->altsetting[k].bLength; |
153 | altset->Descriptor.bDescriptorType = interface->altsetting[k].bDescriptorType; |
154 | altset->Descriptor.bInterfaceNumber = interface->altsetting[k].bInterfaceNumber; |
155 | altset->Descriptor.bAlternateSetting = interface->altsetting[k].bAlternateSetting; |
156 | altset->Descriptor.bNumEndpoints = interface->altsetting[k].bNumEndpoints; |
157 | altset->Descriptor.bInterfaceClass = interface->altsetting[k].bInterfaceClass; |
158 | altset->Descriptor.bInterfaceSubClass = interface->altsetting[k].bInterfaceSubClass; |
159 | altset->Descriptor.bInterfaceProtocol = interface->altsetting[k].bInterfaceProtocol; |
160 | altset->Descriptor.iInterface = interface->altsetting[k].iInterface; |
3664a3e3 |
161 | pEndpointDescriptors = &(altset->pEndpointDescriptors); |
162 | pPipes = &(altset->pPipes); |
f95f1d2e |
163 | |
164 | } |
165 | len +=sizeof(WDU_ALTERNATE_SETTING); |
166 | |
167 | if (buf) { |
3664a3e3 |
168 | *pEndpointDescriptors = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len); |
f95f1d2e |
169 | for (l = 0; l < bNumEndpoints; l++) { |
170 | WDU_ENDPOINT_DESCRIPTOR *ed = (WDU_ENDPOINT_DESCRIPTOR*)(buf+len); |
f95f1d2e |
171 | |
172 | ed->bLength = interface->altsetting[k].endpoint[l].bLength; |
173 | ed->bDescriptorType = interface->altsetting[k].endpoint[l].bDescriptorType; |
174 | ed->bEndpointAddress = interface->altsetting[k].endpoint[l].bEndpointAddress; |
175 | ed->bmAttributes = interface->altsetting[k].endpoint[l].bmAttributes; |
176 | ed->wMaxPacketSize = interface->altsetting[k].endpoint[l].wMaxPacketSize; |
177 | ed->bInterval = interface->altsetting[k].endpoint[l].bInterval; |
178 | |
179 | len += sizeof(WDU_ENDPOINT_DESCRIPTOR); |
792bf5f2 |
180 | } |
f95f1d2e |
181 | |
3664a3e3 |
182 | *pPipes = (WDU_PIPE_INFO*)(buf+len); |
792bf5f2 |
183 | for (l = 0; l < bNumEndpoints; l++) { |
184 | WDU_PIPE_INFO *pi = (WDU_PIPE_INFO*)(buf+len); |
f95f1d2e |
185 | |
186 | pi->dwNumber = interface->altsetting[k].endpoint[l].bEndpointAddress; |
187 | pi->dwMaximumPacketSize = WDU_GET_MAX_PACKET_SIZE(interface->altsetting[k].endpoint[l].wMaxPacketSize); |
188 | pi->type = interface->altsetting[k].endpoint[l].bmAttributes & USB_ENDPOINT_TYPE_MASK; |
189 | if (pi->type == PIPE_TYPE_CONTROL) |
190 | pi->direction = WDU_DIR_IN_OUT; |
191 | else |
192 | { |
193 | pi->direction = interface->altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK ? WDU_DIR_IN : WDU_DIR_OUT; |
194 | } |
195 | |
196 | pi->dwInterval = interface->altsetting[k].endpoint[l].bInterval; |
197 | |
198 | len += sizeof(WDU_PIPE_INFO); |
199 | } |
200 | } else { |
201 | len +=(sizeof(WDU_ENDPOINT_DESCRIPTOR)+sizeof(WDU_PIPE_INFO))*bNumEndpoints; |
202 | } |
203 | } |
204 | } |
205 | } |
206 | |
207 | return len; |
208 | } |
209 | |
9c9fd67c |
210 | int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) { |
cdc711dc |
211 | struct header_struct* wdheader = (struct header_struct*)wdioctl; |
9c9fd67c |
212 | struct version_struct *version; |
213 | int ret = 0; |
cdc711dc |
214 | |
da3ba95a |
215 | if (wdheader->magic != MAGIC) { |
2c2119eb |
216 | fprintf(stderr,"!!!ERROR: magic header does not match!!!\n"); |
217 | return (*ioctl_func) (fd, request, wdioctl); |
cdc711dc |
218 | } |
219 | |
cdc711dc |
220 | switch(request) { |
da3ba95a |
221 | case VERSION: |
9c9fd67c |
222 | version = (struct version_struct*)(wdheader->data); |
223 | strcpy(version->version, "WinDriver no more"); |
224 | version->versionul = 999; |
723d9aa0 |
225 | #ifdef DEBUG |
9c9fd67c |
226 | fprintf(stderr,"faking VERSION\n"); |
723d9aa0 |
227 | #endif |
cdc711dc |
228 | break; |
2c2119eb |
229 | |
230 | case LICENSE: |
723d9aa0 |
231 | #ifdef DEBUG |
2c2119eb |
232 | fprintf(stderr,"faking LICENSE\n"); |
723d9aa0 |
233 | #endif |
2c2119eb |
234 | break; |
235 | |
9c9fd67c |
236 | case CARD_REGISTER: |
533f4b68 |
237 | /* TODO: Implement for LPT-support */ |
238 | #if 0 |
9c9fd67c |
239 | { |
533f4b68 |
240 | struct card_register* cr = (struct card_register*)(wdheader->data); |
9c9fd67c |
241 | } |
533f4b68 |
242 | #endif |
723d9aa0 |
243 | #ifdef DEBUG |
9c9fd67c |
244 | fprintf(stderr,"faking CARD_REGISTER\n"); |
723d9aa0 |
245 | #endif |
da3ba95a |
246 | break; |
2c2119eb |
247 | |
da3ba95a |
248 | case USB_TRANSFER: |
723d9aa0 |
249 | #ifdef DEBUG |
9c9fd67c |
250 | fprintf(stderr,"in USB_TRANSFER"); |
723d9aa0 |
251 | #endif |
da3ba95a |
252 | { |
253 | struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data); |
254 | |
723d9aa0 |
255 | #ifdef DEBUG |
2c2119eb |
256 | fprintf(stderr," unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n", ut->dwUniqueID, ut->dwPipeNum, ut->fRead, ut->dwOptions, ut->dwBufferSize, ut->dwTimeout); |
9c9fd67c |
257 | fprintf(stderr,"setup packet: "); |
258 | hexdump(ut->SetupPacket, 8); |
259 | fprintf(stderr,"\n"); |
723d9aa0 |
260 | |
9c9fd67c |
261 | if (!ut->fRead && ut->dwBufferSize) |
262 | { |
263 | hexdump(ut->pBuffer, ut->dwBufferSize); |
264 | fprintf(stderr,"\n"); |
265 | } |
723d9aa0 |
266 | #endif |
9c9fd67c |
267 | |
795992ad |
268 | #ifndef NO_WINDRVR |
9c9fd67c |
269 | ret = (*ioctl_func) (fd, request, wdioctl); |
411af373 |
270 | #else |
271 | /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */ |
a70f4f38 |
272 | if (ut->dwPipeNum == 0) { /* control pipe */ |
273 | int requesttype, request, value, index, size; |
274 | requesttype = ut->SetupPacket[0]; |
275 | request = ut->SetupPacket[1]; |
276 | value = ut->SetupPacket[2] | (ut->SetupPacket[3] << 8); |
277 | index = ut->SetupPacket[4] | (ut->SetupPacket[5] << 8); |
278 | size = ut->SetupPacket[6] | (ut->SetupPacket[7] << 8); |
723d9aa0 |
279 | #ifdef DEBUG |
a70f4f38 |
280 | fprintf(stderr, "requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype, request, value, index, size); |
723d9aa0 |
281 | #endif |
a70f4f38 |
282 | ret = usb_control_msg(usb_devhandle, requesttype, request, value, index, ut->pBuffer, size, ut->dwTimeout); |
283 | } else { |
284 | if (ut->fRead) { |
285 | ret = usb_bulk_read(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout); |
286 | |
287 | } else { |
288 | ret = usb_bulk_write(usb_devhandle, ut->dwPipeNum, ut->pBuffer, ut->dwBufferSize, ut->dwTimeout); |
289 | } |
290 | } |
291 | |
411af373 |
292 | if (ret < 0) { |
a70f4f38 |
293 | fprintf(stderr, "usb_transfer: %d (%s)\n", ret, usb_strerror()); |
411af373 |
294 | } else { |
295 | ut->dwBytesTransferred = ret; |
296 | ret = 0; |
297 | } |
292160ed |
298 | #endif |
9c9fd67c |
299 | |
723d9aa0 |
300 | #ifdef DEBUG |
2c2119eb |
301 | fprintf(stderr,"Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write")); |
9c9fd67c |
302 | if (ut->fRead && ut->dwBytesTransferred) |
303 | { |
304 | fprintf(stderr,"Read: "); |
305 | hexdump(ut->pBuffer, ut->dwBytesTransferred); |
ca18111b |
306 | fprintf(stderr,"\n"); |
9c9fd67c |
307 | } |
723d9aa0 |
308 | #endif |
da3ba95a |
309 | } |
cdc711dc |
310 | break; |
2c2119eb |
311 | |
da3ba95a |
312 | case INT_ENABLE: |
723d9aa0 |
313 | #ifdef DEBUG |
ca18111b |
314 | fprintf(stderr,"INT_ENABLE\n"); |
723d9aa0 |
315 | #endif |
da3ba95a |
316 | { |
9c9fd67c |
317 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
cdc711dc |
318 | |
723d9aa0 |
319 | #ifdef DEBUG |
53f639f2 |
320 | fprintf(stderr,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds, it->fEnableOk, it->dwCounter, it->dwLost, it->fStopped); |
723d9aa0 |
321 | #endif |
cdc711dc |
322 | |
9c9fd67c |
323 | it->fEnableOk = 1; |
ca18111b |
324 | ints_enabled = 1; |
533f4b68 |
325 | pthread_mutex_trylock(&int_wait); |
9c9fd67c |
326 | } |
cdc711dc |
327 | |
cdc711dc |
328 | break; |
9c9fd67c |
329 | |
da3ba95a |
330 | case INT_DISABLE: |
723d9aa0 |
331 | #ifdef DEBUG |
9c9fd67c |
332 | fprintf(stderr,"INT_DISABLE\n"); |
723d9aa0 |
333 | #endif |
da3ba95a |
334 | { |
9c9fd67c |
335 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
da3ba95a |
336 | |
723d9aa0 |
337 | #ifdef DEBUG |
53f639f2 |
338 | fprintf(stderr,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds, it->fEnableOk, it->dwCounter, it->dwLost, it->fStopped); |
723d9aa0 |
339 | #endif |
795992ad |
340 | #ifndef NO_WINDRVR |
9c9fd67c |
341 | ret = (*ioctl_func) (fd, request, wdioctl); |
e71b6bf3 |
342 | #else |
343 | it->dwCounter = 0; |
344 | it->fStopped = 1; |
ca18111b |
345 | ints_enabled = 0; |
533f4b68 |
346 | pthread_mutex_unlock(&int_wait); |
292160ed |
347 | #endif |
723d9aa0 |
348 | #ifdef DEBUG |
53f639f2 |
349 | fprintf(stderr,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds, it->fEnableOk, it->dwCounter, it->dwLost, it->fStopped); |
723d9aa0 |
350 | #endif |
da3ba95a |
351 | } |
da3ba95a |
352 | break; |
da3ba95a |
353 | |
9c9fd67c |
354 | case USB_SET_INTERFACE: |
723d9aa0 |
355 | #ifdef DEBUG |
9c9fd67c |
356 | fprintf(stderr,"USB_SET_INTERFACE\n"); |
723d9aa0 |
357 | #endif |
da3ba95a |
358 | { |
9c9fd67c |
359 | struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data); |
360 | |
723d9aa0 |
361 | #ifdef DEBUG |
2c2119eb |
362 | fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions); |
723d9aa0 |
363 | #endif |
795992ad |
364 | #ifndef NO_WINDRVR |
9c9fd67c |
365 | ret = (*ioctl_func) (fd, request, wdioctl); |
2a7af812 |
366 | #else |
367 | if (usbdevice) { |
2a7af812 |
368 | if (!usb_devhandle) |
369 | usb_devhandle = usb_open(usbdevice); |
d0676964 |
370 | |
371 | /* FIXME: Select right interface! */ |
372 | ret = usb_claim_interface(usb_devhandle, usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber); |
373 | if (!ret) { |
411af373 |
374 | if(!ret) { |
375 | ret = usb_set_altinterface(usb_devhandle, usi->dwAlternateSetting); |
376 | if (ret) |
377 | fprintf(stderr, "usb_set_altinterface: %d\n", ret); |
378 | } else { |
379 | fprintf(stderr, "usb_set_configuration: %d (%s)\n", ret, usb_strerror()); |
380 | } |
d0676964 |
381 | } else { |
382 | fprintf(stderr, "usb_claim_interface: %d -> %d (%s)\n", usbdevice->config[0].interface[usi->dwInterfaceNum].altsetting[usi->dwAlternateSetting].bInterfaceNumber, ret, usb_strerror()); |
383 | } |
2a7af812 |
384 | } |
292160ed |
385 | #endif |
723d9aa0 |
386 | #ifdef DEBUG |
2a7af812 |
387 | fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions); |
723d9aa0 |
388 | #endif |
da3ba95a |
389 | } |
cdc711dc |
390 | break; |
da3ba95a |
391 | |
9c9fd67c |
392 | case USB_GET_DEVICE_DATA: |
723d9aa0 |
393 | #ifdef DEBUG |
ca18111b |
394 | fprintf(stderr,"faking USB_GET_DEVICE_DATA\n"); |
723d9aa0 |
395 | #endif |
9c9fd67c |
396 | { |
397 | struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data); |
398 | int pSize; |
399 | |
723d9aa0 |
400 | #ifdef DEBUG |
e71b6bf3 |
401 | fprintf(stderr, "unique: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions); |
723d9aa0 |
402 | #endif |
9c9fd67c |
403 | pSize = ugdd->dwBytes; |
e71b6bf3 |
404 | if (!ugdd->dwBytes) { |
2a7af812 |
405 | if (usbdevice) { |
f95f1d2e |
406 | ugdd->dwBytes = usb_deviceinfo(NULL); |
e71b6bf3 |
407 | } |
408 | } else { |
f95f1d2e |
409 | usb_deviceinfo((unsigned char*)ugdd->pBuf); |
e71b6bf3 |
410 | } |
da3ba95a |
411 | } |
9c9fd67c |
412 | break; |
413 | |
b0f621dd |
414 | case EVENT_REGISTER: |
723d9aa0 |
415 | #ifdef DEBUG |
b0f621dd |
416 | fprintf(stderr,"EVENT_REGISTER\n"); |
723d9aa0 |
417 | #endif |
b0f621dd |
418 | { |
419 | struct event *e = (struct event*)(wdheader->data); |
ac22d975 |
420 | struct usb_bus *bus; |
b0f621dd |
421 | int i; |
422 | |
723d9aa0 |
423 | #ifdef DEBUG |
b0f621dd |
424 | fprintf(stderr,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e->handle, e->dwAction, e->dwStatus, e->dwEventId, e->dwCardType, e->hKernelPlugIn, e->dwOptions, e->u.Usb.deviceId.dwVendorId, e->u.Usb.deviceId.dwProductId, e->u.Usb.dwUniqueID, e->dwEventVer, e->dwNumMatchTables); |
723d9aa0 |
425 | #endif |
ac22d975 |
426 | for (i = 0; i < e->dwNumMatchTables; i++) { |
723d9aa0 |
427 | #ifdef DEBUG |
e71b6bf3 |
428 | fprintf(stderr,"match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e->matchTables[i].VendorId, e->matchTables[i].ProductId, e->matchTables[i].bDeviceClass, e->matchTables[i].bDeviceSubClass, e->matchTables[i].bInterfaceClass, e->matchTables[i].bInterfaceSubClass, e->matchTables[i].bInterfaceProtocol); |
723d9aa0 |
429 | #endif |
b0f621dd |
430 | |
ac22d975 |
431 | for (bus = busses; bus; bus = bus->next) { |
432 | struct usb_device *dev; |
433 | |
434 | for (dev = bus->devices; dev; dev = dev->next) { |
435 | struct usb_device_descriptor *desc = &(dev->descriptor); |
436 | |
437 | if((desc->idVendor == e->matchTables[i].VendorId) && |
438 | (desc->idProduct == e->matchTables[i].ProductId) && |
439 | (desc->bDeviceClass == e->matchTables[i].bDeviceClass) && |
440 | (desc->bDeviceSubClass == e->matchTables[i].bDeviceSubClass)) { |
2a7af812 |
441 | int ac; |
442 | for (ac = 0; ac < desc->bNumConfigurations; ac++) { |
443 | struct usb_interface *interface = dev->config[ac].interface; |
444 | int ai; |
445 | |
446 | for (ai = 0; ai < interface->num_altsetting; ai++) { |
723d9aa0 |
447 | #ifdef DEBUG |
2a7af812 |
448 | fprintf(stderr, "intclass: %x, intsubclass: %x, intproto: %x\n", interface->altsetting[i].bInterfaceClass, interface->altsetting[i].bInterfaceSubClass, interface->altsetting[i].bInterfaceProtocol); |
723d9aa0 |
449 | #endif |
2a7af812 |
450 | if ((interface->altsetting[ai].bInterfaceSubClass == e->matchTables[i].bInterfaceSubClass) && |
451 | (interface->altsetting[ai].bInterfaceProtocol == e->matchTables[i].bInterfaceProtocol)){ |
452 | /* TODO: check interfaceClass! */ |
723d9aa0 |
453 | #ifdef DEBUG |
2a7af812 |
454 | fprintf(stderr,"!!!FOUND DEVICE WITH LIBUSB!!!\n"); |
723d9aa0 |
455 | #endif |
2a7af812 |
456 | usbdevice = dev; |
457 | card_type = e->dwCardType; |
458 | } |
459 | } |
460 | } |
ac22d975 |
461 | } |
462 | } |
463 | } |
464 | } |
465 | |
795992ad |
466 | #ifndef NO_WINDRVR |
b0f621dd |
467 | ret = (*ioctl_func) (fd, request, wdioctl); |
2a7af812 |
468 | #else |
2a7af812 |
469 | e->handle++; |
292160ed |
470 | #endif |
b0f621dd |
471 | |
723d9aa0 |
472 | #ifdef DEBUG |
b0f621dd |
473 | fprintf(stderr,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e->handle, e->dwAction, e->dwStatus, e->dwEventId, e->dwCardType, e->hKernelPlugIn, e->dwOptions, e->u.Usb.deviceId.dwVendorId, e->u.Usb.deviceId.dwProductId, e->u.Usb.dwUniqueID, e->dwEventVer, e->dwNumMatchTables); |
474 | for (i = 0; i < e->dwNumMatchTables; i++) |
e71b6bf3 |
475 | fprintf(stderr,"match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e->matchTables[i].VendorId, e->matchTables[i].ProductId, e->matchTables[i].bDeviceClass, e->matchTables[i].bDeviceSubClass, e->matchTables[i].bInterfaceClass, e->matchTables[i].bInterfaceSubClass, e->matchTables[i].bInterfaceProtocol); |
723d9aa0 |
476 | #endif |
b0f621dd |
477 | } |
478 | break; |
479 | |
da3ba95a |
480 | case TRANSFER: |
9c9fd67c |
481 | fprintf(stderr,"TRANSFER\n"); |
795992ad |
482 | #ifndef NO_WINDRVR |
9c9fd67c |
483 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
484 | #endif |
9c9fd67c |
485 | break; |
486 | |
da3ba95a |
487 | case EVENT_UNREGISTER: |
723d9aa0 |
488 | #ifdef DEBUG |
9c9fd67c |
489 | fprintf(stderr,"EVENT_UNREGISTER\n"); |
723d9aa0 |
490 | #endif |
795992ad |
491 | #ifndef NO_WINDRVR |
9c9fd67c |
492 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
493 | #endif |
9c9fd67c |
494 | break; |
495 | |
da3ba95a |
496 | case INT_WAIT: |
723d9aa0 |
497 | #ifdef DEBUG |
9c9fd67c |
498 | fprintf(stderr,"INT_WAIT\n"); |
723d9aa0 |
499 | #endif |
b0f621dd |
500 | { |
501 | struct interrupt *it = (struct interrupt*)(wdheader->data); |
502 | |
723d9aa0 |
503 | #ifdef DEBUG |
53f639f2 |
504 | fprintf(stderr,"Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds, it->fEnableOk, it->dwCounter, it->dwLost, it->fStopped); |
723d9aa0 |
505 | #endif |
b0f621dd |
506 | |
795992ad |
507 | #ifndef NO_WINDRVR |
b0f621dd |
508 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
509 | #else |
2a7af812 |
510 | if (usbdevice) { |
511 | if (it->dwCounter == 0) { |
512 | it->dwCounter = 1; |
513 | } else { |
533f4b68 |
514 | pthread_mutex_lock(&int_wait); |
2a7af812 |
515 | } |
94038a57 |
516 | } else { |
533f4b68 |
517 | pthread_mutex_lock(&int_wait); |
2a7af812 |
518 | } |
292160ed |
519 | #endif |
520 | |
723d9aa0 |
521 | #ifdef DEBUG |
ca18111b |
522 | fprintf(stderr,"INT_WAIT_RETURN: Handle: %lu, Options: %lx, ncmds: %lu, enableok: %lu, count: %lu, lost: %lu, stopped: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds, it->fEnableOk, it->dwCounter, it->dwLost, it->fStopped); |
723d9aa0 |
523 | #endif |
b0f621dd |
524 | } |
9c9fd67c |
525 | break; |
526 | |
da3ba95a |
527 | case CARD_UNREGISTER: |
9c9fd67c |
528 | fprintf(stderr,"CARD_UNREGISTER\n"); |
795992ad |
529 | #ifndef NO_WINDRVR |
9c9fd67c |
530 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
531 | #endif |
9c9fd67c |
532 | break; |
533 | |
da3ba95a |
534 | case EVENT_PULL: |
723d9aa0 |
535 | #ifdef DEBUG |
9c9fd67c |
536 | fprintf(stderr,"EVENT_PULL\n"); |
723d9aa0 |
537 | #endif |
b1831983 |
538 | { |
539 | struct event *e = (struct event*)(wdheader->data); |
723d9aa0 |
540 | #ifdef DEBUG |
b1831983 |
541 | int i; |
542 | |
2a7af812 |
543 | fprintf(stderr,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e->handle, e->dwAction, e->dwStatus, e->dwEventId, e->dwCardType, e->hKernelPlugIn, e->dwOptions, e->u.Usb.deviceId.dwVendorId, e->u.Usb.deviceId.dwProductId, e->u.Usb.dwUniqueID, e->dwEventVer, e->dwNumMatchTables); |
b1831983 |
544 | for (i = 0; i < e->dwNumMatchTables; i++) |
e71b6bf3 |
545 | fprintf(stderr,"match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e->matchTables[i].VendorId, e->matchTables[i].ProductId, e->matchTables[i].bDeviceClass, e->matchTables[i].bDeviceSubClass, e->matchTables[i].bInterfaceClass, e->matchTables[i].bInterfaceSubClass, e->matchTables[i].bInterfaceProtocol); |
723d9aa0 |
546 | #endif |
b1831983 |
547 | |
795992ad |
548 | #ifndef NO_WINDRVR |
b1831983 |
549 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
550 | #else |
2a7af812 |
551 | if (usbdevice) { |
552 | struct usb_interface *interface = usbdevice->config->interface; |
292160ed |
553 | |
554 | e->dwCardType = card_type; |
555 | e->dwAction = 1; |
556 | e->dwEventId = 109; |
ca18111b |
557 | e->u.Usb.dwUniqueID = 110; |
2a7af812 |
558 | e->matchTables[0].VendorId = usbdevice->descriptor.idVendor; |
559 | e->matchTables[0].ProductId = usbdevice->descriptor.idProduct; |
560 | e->matchTables[0].bDeviceClass = usbdevice->descriptor.bDeviceClass; |
561 | e->matchTables[0].bDeviceSubClass = usbdevice->descriptor.bDeviceSubClass; |
292160ed |
562 | e->matchTables[0].bInterfaceClass = interface->altsetting[0].bInterfaceClass; |
563 | e->matchTables[0].bInterfaceSubClass = interface->altsetting[0].bInterfaceSubClass; |
564 | e->matchTables[0].bInterfaceProtocol = interface->altsetting[0].bInterfaceProtocol; |
565 | } |
566 | #endif |
b1831983 |
567 | |
723d9aa0 |
568 | #ifdef DEBUG |
2a7af812 |
569 | fprintf(stderr,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lx, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e->handle, e->dwAction, e->dwStatus, e->dwEventId, e->dwCardType, e->hKernelPlugIn, e->dwOptions, e->u.Usb.deviceId.dwVendorId, e->u.Usb.deviceId.dwProductId, e->u.Usb.dwUniqueID, e->dwEventVer, e->dwNumMatchTables); |
b1831983 |
570 | for (i = 0; i < e->dwNumMatchTables; i++) |
e71b6bf3 |
571 | fprintf(stderr,"match: dev: %04x:%04x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e->matchTables[i].VendorId, e->matchTables[i].ProductId, e->matchTables[i].bDeviceClass, e->matchTables[i].bDeviceSubClass, e->matchTables[i].bInterfaceClass, e->matchTables[i].bInterfaceSubClass, e->matchTables[i].bInterfaceProtocol); |
723d9aa0 |
572 | #endif |
ca18111b |
573 | |
b1831983 |
574 | } |
9c9fd67c |
575 | break; |
576 | |
da3ba95a |
577 | default: |
292160ed |
578 | fprintf(stderr,"!!!Unsupported IOCTL: %x!!!\n", request); |
795992ad |
579 | #ifndef NO_WINDRVR |
9c9fd67c |
580 | ret = (*ioctl_func) (fd, request, wdioctl); |
292160ed |
581 | #endif |
582 | break; |
cdc711dc |
583 | } |
da3ba95a |
584 | |
9c9fd67c |
585 | return ret; |
cdc711dc |
586 | } |
587 | |
dbda1264 |
588 | int ioctl(int fd, int request, ...) |
589 | { |
590 | va_list args; |
591 | void *argp; |
592 | int ret; |
593 | |
594 | if (!ioctl_func) |
595 | ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl"); |
596 | |
597 | va_start (args, request); |
598 | argp = va_arg (args, void *); |
599 | va_end (args); |
600 | |
601 | if (fd == windrvrfd) |
602 | ret = do_wdioctl(fd, request, argp); |
603 | else |
604 | ret = (*ioctl_func) (fd, request, argp); |
605 | |
606 | return ret; |
607 | } |
cdc711dc |
608 | |
609 | typedef int (*open_funcptr_t) (const char *, int, mode_t); |
610 | |
cdc711dc |
611 | int open (const char *pathname, int flags, ...) |
612 | { |
613 | static open_funcptr_t func = NULL; |
614 | mode_t mode = 0; |
615 | va_list args; |
616 | int fd; |
617 | |
618 | if (!func) |
619 | func = (open_funcptr_t) dlsym (REAL_LIBC, "open"); |
620 | |
621 | if (flags & O_CREAT) { |
622 | va_start(args, flags); |
623 | mode = va_arg(args, mode_t); |
624 | va_end(args); |
625 | } |
626 | |
cdc711dc |
627 | if (!strcmp (pathname, "/dev/windrvr6")) { |
723d9aa0 |
628 | #ifdef DEBUG |
cdc711dc |
629 | fprintf(stderr,"opening windrvr6\n"); |
723d9aa0 |
630 | #endif |
795992ad |
631 | #ifdef NO_WINDRVR |
01b99d52 |
632 | windrvrfd = fd = (*func) ("/dev/null", flags, mode); |
633 | #else |
634 | windrvrfd = fd = (*func) (pathname, flags, mode); |
635 | #endif |
f10480d1 |
636 | if (!busses) { |
637 | usb_init(); |
638 | usb_find_busses(); |
639 | usb_find_devices(); |
640 | |
641 | busses = usb_get_busses(); |
642 | } |
723d9aa0 |
643 | |
644 | return fd; |
cdc711dc |
645 | } |
646 | |
723d9aa0 |
647 | return (*func) (pathname, flags, mode); |
cdc711dc |
648 | } |
649 | |
723d9aa0 |
650 | int close(int fd) { |
651 | static int (*func) (int) = NULL; |
cdc711dc |
652 | |
723d9aa0 |
653 | if (!func) |
654 | func = (int (*) (int)) dlsym(REAL_LIBC, "close"); |
655 | |
656 | if (fd == windrvrfd) { |
657 | #ifdef DEBUG |
658 | fprintf(stderr,"close windrvrfd\n"); |
659 | #endif |
660 | windrvrfd = 0; |
cdc711dc |
661 | } |
cdc711dc |
662 | |
723d9aa0 |
663 | return (*func) (fd); |
cdc711dc |
664 | } |
665 | |
723d9aa0 |
666 | FILE *fopen(const char *path, const char *mode) { |
667 | FILE *ret; |
668 | static FILE* (*func) (const char*, const char*) = NULL; |
ca18111b |
669 | |
670 | if (!func) |
723d9aa0 |
671 | func = (FILE* (*) (const char*, const char*)) dlsym(REAL_LIBC, "fopen"); |
ca18111b |
672 | |
723d9aa0 |
673 | ret = (*func) (path, mode); |
ca18111b |
674 | |
723d9aa0 |
675 | if (!strcmp (path, "/proc/modules")) { |
676 | #ifdef DEBUG |
677 | fprintf(stderr,"opening /proc/modules\n"); |
678 | #endif |
679 | #ifdef NO_WINDRVR |
dbda1264 |
680 | modulesfp = ret; |
723d9aa0 |
681 | modules_read = 0; |
682 | #endif |
683 | } |
cdc711dc |
684 | |
685 | return ret; |
686 | } |
687 | |
dbda1264 |
688 | char *fgets(char *s, int size, FILE *stream) { |
689 | static char* (*func) (char*, int, FILE*) = NULL; |
723d9aa0 |
690 | const char modules[] = "windrvr6 160960 0 - Live 0xf98b0000\n"; |
dbda1264 |
691 | char *ret = NULL; |
692 | |
cdc711dc |
693 | |
694 | if (!func) |
dbda1264 |
695 | func = (char* (*) (char*, int, FILE*)) dlsym(REAL_LIBC, "fgets"); |
723d9aa0 |
696 | |
dbda1264 |
697 | if (modulesfp == stream) { |
698 | if (!modules_read) { |
699 | strcpy(s, modules); |
700 | ret = s; |
701 | modules_read = 1; |
702 | } |
723d9aa0 |
703 | } else { |
dbda1264 |
704 | ret = (*func)(s,size,stream); |
723d9aa0 |
705 | } |
cdc711dc |
706 | |
707 | return ret; |
708 | } |
709 | |
dbda1264 |
710 | int fclose(FILE *fp) { |
711 | static int (*func) (FILE*) = NULL; |
cdc711dc |
712 | |
dbda1264 |
713 | if (!func) |
714 | func = (int (*) (FILE*)) dlsym(REAL_LIBC, "fclose"); |
cdc711dc |
715 | |
dbda1264 |
716 | if (fp == modulesfp) { |
717 | modulesfp = NULL; |
718 | } |
719 | |
720 | return (*func)(fp); |
cdc711dc |
721 | } |
da3ba95a |
722 | #endif |