]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/proxusb.c
10 #include "proxmark3.h"
13 usb_dev_handle
*devh
= NULL
;
14 static unsigned int claimed_iface
= 0;
15 unsigned char return_on_error
= 0;
16 unsigned char error_occured
= 0;
17 extern unsigned int current_command
;
19 void SendCommand(UsbCommand
*c
)
24 printf("Sending %d bytes\n", sizeof(UsbCommand
));
26 current_command
= c
->cmd
;
27 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
33 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
40 while(!(devh
=OpenProxmark(0))) { sleep(1); }
48 bool ReceiveCommandPoll(UsbCommand
*c
)
52 bzero(c
, sizeof(UsbCommand
));
53 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
55 if (ret
!= -ETIMEDOUT
) {
60 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
67 while(!(devh
=OpenProxmark(0))) { sleep(1); }
74 if (ret
&& (ret
< sizeof(UsbCommand
))) {
75 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
76 ret
, (int)sizeof(UsbCommand
));
83 void ReceiveCommand(UsbCommand
*c
)
85 // printf("%s()\n", __FUNCTION__);
88 retval
= ReceiveCommandPoll(c
);
89 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
91 // printf("recv %x\n", c->cmd);
94 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
)
96 struct usb_bus
*busses
, *bus
;
97 usb_dev_handle
*handle
= NULL
;
102 busses
= usb_get_busses();
104 for (bus
= busses
; bus
; bus
= bus
->next
) {
105 struct usb_device
*dev
;
107 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
108 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
110 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
111 handle
= usb_open(dev
);
114 fprintf(stderr
, "open failed: %s!\n", usb_strerror());
117 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
127 usb_dev_handle
* OpenProxmark(int verbose
)
130 usb_dev_handle
*handle
= NULL
;
134 handle
= findProxmark(verbose
, &iface
);
142 handle
= findProxmark(verbose
, &iface
);
147 /* detach kernel driver first */
148 ret
= usb_detach_kernel_driver_np(handle
, iface
);
149 /* don't complain if no driver attached */
150 if (ret
<0 && ret
!= -61 && verbose
)
151 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
153 ret
= usb_claim_interface(handle
, iface
);
156 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
160 claimed_iface
= iface
;
165 void CloseProxmark(void)
167 usb_release_interface(devh
, claimed_iface
);