]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/proxusb.c
15513bf0d0a872d7ad895472e4bb02d201a0386a
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
22 #include "proxmark3.h"
25 // It seems to be missing for mingw
30 usb_dev_handle
*devh
= NULL
;
31 static unsigned int claimed_iface
= 0;
32 unsigned char return_on_error
= 0;
33 unsigned char error_occured
= 0;
34 extern unsigned int current_command
;
36 void SendCommand(UsbCommand
*c
)
41 printf("Sending %d bytes\n", sizeof(UsbCommand
));
43 current_command
= c
->cmd
;
44 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
50 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
57 while(!OpenProxmark(0)) { sleep(1); }
65 bool ReceiveCommandPoll(UsbCommand
*c
)
69 memset(c
, 0, sizeof (UsbCommand
));
70 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
72 if (ret
!= -ETIMEDOUT
) {
77 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
84 while(!OpenProxmark(0)) { sleep(1); }
91 if (ret
&& (ret
< sizeof(UsbCommand
))) {
92 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
93 ret
, (int)sizeof(UsbCommand
));
100 void ReceiveCommand(UsbCommand
*c
)
102 // printf("%s()\n", __FUNCTION__);
105 retval
= ReceiveCommandPoll(c
);
106 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
108 // printf("recv %x\n", c->cmd);
111 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
)
113 struct usb_bus
*busses
, *bus
;
114 usb_dev_handle
*handle
= NULL
;
119 busses
= usb_get_busses();
121 for (bus
= busses
; bus
; bus
= bus
->next
) {
122 struct usb_device
*dev
;
124 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
125 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
127 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
128 handle
= usb_open(dev
);
131 fprintf(stderr
, "open failed: %s!\n", usb_strerror());
134 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
144 usb_dev_handle
* OpenProxmark(int verbose
)
147 usb_dev_handle
*handle
= NULL
;
151 handle
= findProxmark(verbose
, &iface
);
159 handle
= findProxmark(verbose
, &iface
);
164 /* detach kernel driver first */
165 ret
= usb_detach_kernel_driver_np(handle
, iface
);
166 /* don't complain if no driver attached */
167 if (ret
<0 && ret
!= -61 && verbose
)
168 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
171 // Needed for Windows. Optional for Mac OS and Linux
172 ret
= usb_set_configuration(handle
, 1);
175 fprintf(stderr
, "configuration set failed: %s!\n", usb_strerror());
179 ret
= usb_claim_interface(handle
, iface
);
182 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
185 claimed_iface
= iface
;
190 void CloseProxmark(void)
192 usb_release_interface(devh
, claimed_iface
);