]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/proxusb.c
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 //-----------------------------------------------------------------------------
21 #include "proxmark3.h"
24 // It seems to be missing for mingw
29 usb_dev_handle
*devh
= NULL
;
30 static unsigned int claimed_iface
= 0;
31 unsigned char return_on_error
= 0;
32 unsigned char error_occured
= 0;
33 extern unsigned int current_command
;
35 void SendCommand(UsbCommand
*c
)
40 printf("Sending %d bytes\n", sizeof(UsbCommand
));
42 current_command
= c
->cmd
;
43 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
49 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
56 while(!OpenProxmark(0)) { sleep(1); }
64 bool ReceiveCommandPoll(UsbCommand
*c
)
68 memset(c
, 0, sizeof (UsbCommand
));
69 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
71 if (ret
!= -ETIMEDOUT
) {
76 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
83 while(!OpenProxmark(0)) { sleep(1); }
90 if (ret
&& (ret
< sizeof(UsbCommand
))) {
91 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
92 ret
, (int)sizeof(UsbCommand
));
99 void ReceiveCommand(UsbCommand
*c
)
101 // printf("%s()\n", __FUNCTION__);
104 retval
= ReceiveCommandPoll(c
);
105 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
107 // printf("recv %x\n", c->cmd);
110 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
)
112 struct usb_bus
*busses
, *bus
;
113 usb_dev_handle
*handle
= NULL
;
118 busses
= usb_get_busses();
120 for (bus
= busses
; bus
; bus
= bus
->next
) {
121 struct usb_device
*dev
;
123 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
124 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
126 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
127 handle
= usb_open(dev
);
130 fprintf(stderr
, "open failed: %s!\n", usb_strerror());
133 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
143 usb_dev_handle
* OpenProxmark(int verbose
)
146 usb_dev_handle
*handle
= NULL
;
150 handle
= findProxmark(verbose
, &iface
);
158 handle
= findProxmark(verbose
, &iface
);
163 /* detach kernel driver first */
164 ret
= usb_detach_kernel_driver_np(handle
, iface
);
165 /* don't complain if no driver attached */
166 if (ret
<0 && ret
!= -61 && verbose
)
167 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
170 // Needed for Windows. Optional for Mac OS and Linux
171 ret
= usb_set_configuration(handle
, 1);
174 fprintf(stderr
, "configuration set failed: %s!\n", usb_strerror());
178 ret
= usb_claim_interface(handle
, iface
);
181 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
184 claimed_iface
= iface
;
189 void CloseProxmark(void)
191 usb_release_interface(devh
, claimed_iface
);