]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - client/proxusb.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
23 #include "proxmark3.h"
26 // It seems to be missing for mingw
31 usb_dev_handle
*devh
= NULL
;
32 static unsigned int claimed_iface
= 0;
33 unsigned char return_on_error
= 0;
34 unsigned char error_occured
= 0;
35 extern unsigned int current_command
;
37 void SendCommand(UsbCommand
*c
)
42 printf("Sending %d bytes\n", sizeof(UsbCommand
));
44 current_command
= c
->cmd
;
45 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
51 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
58 while(!OpenProxmark(0)) { sleep(1); }
66 bool ReceiveCommandPoll(UsbCommand
*c
)
70 memset(c
, 0, sizeof (UsbCommand
));
71 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
73 if (ret
!= -ETIMEDOUT
) {
78 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
85 while(!OpenProxmark(0)) { sleep(1); }
92 if (ret
&& (ret
< sizeof(UsbCommand
))) {
93 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
94 ret
, (int)sizeof(UsbCommand
));
101 void ReceiveCommand(UsbCommand
*c
)
103 // printf("%s()\n", __FUNCTION__);
106 retval
= ReceiveCommandPoll(c
);
107 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
109 // printf("recv %x\n", c->cmd);
112 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
)
114 struct usb_bus
*busses
, *bus
;
115 usb_dev_handle
*handle
= NULL
;
120 busses
= usb_get_busses();
122 for (bus
= busses
; bus
; bus
= bus
->next
) {
123 struct usb_device
*dev
;
125 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
126 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
128 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
129 handle
= usb_open(dev
);
132 fprintf(stderr
, "open failed: %s!\n", usb_strerror());
135 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
145 usb_dev_handle
* OpenProxmark(int verbose
)
148 usb_dev_handle
*handle
= NULL
;
152 handle
= findProxmark(verbose
, &iface
);
160 handle
= findProxmark(verbose
, &iface
);
165 /* detach kernel driver first */
166 ret
= usb_detach_kernel_driver_np(handle
, iface
);
167 /* don't complain if no driver attached */
168 if (ret
<0 && ret
!= -61 && verbose
)
169 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
172 // Needed for Windows. Optional for Mac OS and Linux
173 ret
= usb_set_configuration(handle
, 1);
176 fprintf(stderr
, "configuration set failed: %s!\n", usb_strerror());
180 ret
= usb_claim_interface(handle
, iface
);
183 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
186 claimed_iface
= iface
;
191 void CloseProxmark(void)
193 usb_release_interface(devh
, claimed_iface
);