]>
cvs.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); }
47 printf("waiting for ack\n");
49 if(ack.cmd != CMD_ACK) {
54 printf("not waiting for ack\n");
59 bool ReceiveCommandPoll(UsbCommand
*c
) {
62 bzero(c
, sizeof(UsbCommand
));
63 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
65 if (ret
!= -ETIMEDOUT
) {
70 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
77 while(!(devh
=OpenProxmark(0))) { sleep(1); }
84 if (ret
&& (ret
< sizeof(UsbCommand
))) {
85 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
86 ret
, (int)sizeof(UsbCommand
));
93 printf("Read %d bytes\n", ret
);
94 for (i
= 0; i
< ret
; i
++) {
95 printf("0x%02X ", ((unsigned char*)c
)[i
]);
107 void ReceiveCommand(UsbCommand
*c
) {
108 // printf("%s()\n", __FUNCTION__);
111 retval
= ReceiveCommandPoll(c
);
112 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
114 // printf("recv %x\n", c->cmd);
117 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
) {
118 struct usb_bus
*busses
, *bus
;
119 usb_dev_handle
*handle
= NULL
;
124 busses
= usb_get_busses();
126 for (bus
= busses
; bus
; bus
= bus
->next
) {
127 struct usb_device
*dev
;
129 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
130 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
132 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
133 handle
= usb_open(dev
);
136 fprintf(stderr
, "open failed: %s!\n", usb_strerror());
140 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
150 usb_dev_handle
* OpenProxmark(int verbose
) {
152 usb_dev_handle
*handle
= NULL
;
156 handle
= findProxmark(verbose
, &iface
);
164 handle
= findProxmark(verbose
, &iface
);
169 /* detach kernel driver first */
170 ret
= usb_detach_kernel_driver_np(handle
, iface
);
171 /* don't complain if no driver attached */
172 if (ret
<0 && ret
!= -61 && verbose
)
173 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
175 ret
= usb_claim_interface(handle
, iface
);
178 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
182 claimed_iface
= iface
;
187 void CloseProxmark(void) {
188 usb_release_interface(devh
, claimed_iface
);