]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/usb.c
8f5f38a7179736f69b851157a7ea8d25c25c4c22
11 #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;
18 void SendCommand(UsbCommand
*c
, bool wantAck
) {
22 printf("Sending %d bytes\n", sizeof(UsbCommand
));
24 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
30 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
37 while(!(devh
=OpenProxmark(0))) { sleep(1); }
47 if(ack
.cmd
!= CMD_ACK
) {
54 int ReceiveCommandP(UsbCommand
*c
) {
57 bzero(c
, sizeof(UsbCommand
));
58 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
60 if (ret
!= -ETIMEDOUT
) {
65 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
72 while(!(devh
=OpenProxmark(0))) { sleep(1); }
79 if (ret
&& (ret
< sizeof(UsbCommand
))) {
80 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
81 ret
, (int)sizeof(UsbCommand
));
88 printf("Read %d bytes\n", ret
);
89 for (i
= 0; i
< ret
; i
++) {
90 printf("0x%02X ", ((unsigned char*)c
)[i
]);
102 void ReceiveCommand(UsbCommand
*c
) {
103 while(ReceiveCommandP(c
)<0) {}
106 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
) {
107 struct usb_bus
*busses
, *bus
;
108 usb_dev_handle
*handle
= NULL
;
113 busses
= usb_get_busses();
115 for (bus
= busses
; bus
; bus
= bus
->next
) {
116 struct usb_device
*dev
;
118 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
119 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
121 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
122 handle
= usb_open(dev
);
125 fprintf(stderr
, "open failed: %s!\n", usb_strerror());
129 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
139 usb_dev_handle
* OpenProxmark(int verbose
) {
141 usb_dev_handle
*handle
= NULL
;
145 handle
= findProxmark(verbose
, &iface
);
153 handle
= findProxmark(verbose
, &iface
);
158 /* detach kernel driver first */
159 ret
= usb_detach_kernel_driver_np(handle
, iface
);
160 /* don't complain if no driver attached */
161 if (ret
<0 && ret
!= -61 && verbose
)
162 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
164 ret
= usb_claim_interface(handle
, iface
);
167 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
171 claimed_iface
= iface
;
176 void CloseProxmark(void) {
177 usb_release_interface(devh
, claimed_iface
);