]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/usb.c
10 #include "proxmark3.h"
11 #include "../include/usb_cmd.h"
12 usb_dev_handle
*devh
= NULL
;
13 static unsigned int claimed_iface
= 0;
14 unsigned char return_on_error
= 0;
15 unsigned char error_occured
= 0;
16 extern unsigned int current_command
;
18 void SendCommand(UsbCommand
*c
) {
22 printf("Sending %d bytes\n", sizeof(UsbCommand
));
24 current_command
= c
->cmd
;
25 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
31 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
38 while(!(devh
=OpenProxmark(0))) { sleep(1); }
46 bool ReceiveCommandPoll(UsbCommand
*c
) {
49 bzero(c
, sizeof(UsbCommand
));
50 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
52 if (ret
!= -ETIMEDOUT
) {
57 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
64 while(!(devh
=OpenProxmark(0))) { sleep(1); }
71 if (ret
&& (ret
< sizeof(UsbCommand
))) {
72 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
73 ret
, (int)sizeof(UsbCommand
));
80 void ReceiveCommand(UsbCommand
*c
) {
81 // printf("%s()\n", __FUNCTION__);
84 retval
= ReceiveCommandPoll(c
);
85 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
87 // printf("recv %x\n", c->cmd);
90 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
) {
91 struct usb_bus
*busses
, *bus
;
92 usb_dev_handle
*handle
= NULL
;
97 busses
= usb_get_busses();
99 for (bus
= busses
; bus
; bus
= bus
->next
) {
100 struct usb_device
*dev
;
102 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
103 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
105 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
106 handle
= usb_open(dev
);
109 fprintf(stderr
, "open failed: %s!\n", usb_strerror());
113 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
123 usb_dev_handle
* OpenProxmark(int verbose
) {
125 usb_dev_handle
*handle
= NULL
;
129 handle
= findProxmark(verbose
, &iface
);
137 handle
= findProxmark(verbose
, &iface
);
142 /* detach kernel driver first */
143 ret
= usb_detach_kernel_driver_np(handle
, iface
);
144 /* don't complain if no driver attached */
145 if (ret
<0 && ret
!= -61 && verbose
)
146 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
148 ret
= usb_claim_interface(handle
, iface
);
151 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
155 claimed_iface
= iface
;
160 void CloseProxmark(void) {
161 usb_release_interface(devh
, claimed_iface
);