]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/proxusb.c
11 #include "proxmark3.h"
14 // It seems to be missing for mingw
19 usb_dev_handle
*devh
= NULL
;
20 static unsigned int claimed_iface
= 0;
21 unsigned char return_on_error
= 0;
22 unsigned char error_occured
= 0;
23 extern unsigned int current_command
;
25 void SendCommand(UsbCommand
*c
)
30 printf("Sending %d bytes\n", sizeof(UsbCommand
));
32 current_command
= c
->cmd
;
33 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
39 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
46 while(!OpenProxmark(0)) { sleep(1); }
54 bool ReceiveCommandPoll(UsbCommand
*c
)
58 memset(c
, 0, sizeof (UsbCommand
));
59 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
61 if (ret
!= -ETIMEDOUT
) {
66 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
73 while(!OpenProxmark(0)) { sleep(1); }
80 if (ret
&& (ret
< sizeof(UsbCommand
))) {
81 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
82 ret
, (int)sizeof(UsbCommand
));
89 void ReceiveCommand(UsbCommand
*c
)
91 // printf("%s()\n", __FUNCTION__);
94 retval
= ReceiveCommandPoll(c
);
95 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
97 // printf("recv %x\n", c->cmd);
100 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
)
102 struct usb_bus
*busses
, *bus
;
103 usb_dev_handle
*handle
= NULL
;
108 busses
= usb_get_busses();
110 for (bus
= busses
; bus
; bus
= bus
->next
) {
111 struct usb_device
*dev
;
113 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
114 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
116 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
117 handle
= usb_open(dev
);
120 fprintf(stderr
, "open failed: %s!\n", usb_strerror());
123 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
133 usb_dev_handle
* OpenProxmark(int verbose
)
136 usb_dev_handle
*handle
= NULL
;
140 handle
= findProxmark(verbose
, &iface
);
148 handle
= findProxmark(verbose
, &iface
);
153 /* detach kernel driver first */
154 ret
= usb_detach_kernel_driver_np(handle
, iface
);
155 /* don't complain if no driver attached */
156 if (ret
<0 && ret
!= -61 && verbose
)
157 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
160 // Needed for Windows. Optional for Mac OS and Linux
161 ret
= usb_set_configuration(handle
, 1);
164 fprintf(stderr
, "configuration set failed: %s!\n", usb_strerror());
168 ret
= usb_claim_interface(handle
, iface
);
171 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
174 claimed_iface
= iface
;
179 void CloseProxmark(void)
181 usb_release_interface(devh
, claimed_iface
);