]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - linux/usb.c
9 #include "../winsrc/prox.h"
10 #include "proxmark3.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;
17 void SendCommand(UsbCommand
*c
, BOOL wantAck
) {
21 printf("Sending %d bytes\n", sizeof(UsbCommand
));
23 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
29 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
36 while(!(devh
=OpenProxmark(0))) { sleep(1); }
46 if(ack
.cmd
!= CMD_ACK
) {
53 int ReceiveCommandP(UsbCommand
*c
) {
56 bzero(c
, sizeof(UsbCommand
));
57 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
59 if (ret
!= -ETIMEDOUT
) {
64 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
71 while(!(devh
=OpenProxmark(0))) { sleep(1); }
78 if (ret
&& (ret
< sizeof(UsbCommand
))) {
79 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
80 ret
, (int)sizeof(UsbCommand
));
87 printf("Read %d bytes\n", ret
);
88 for (i
= 0; i
< ret
; i
++) {
89 printf("0x%02X ", ((unsigned char*)c
)[i
]);
101 void ReceiveCommand(UsbCommand
*c
) {
102 while(ReceiveCommandP(c
)<0) {}
105 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
) {
106 struct usb_bus
*busses
, *bus
;
107 usb_dev_handle
*handle
= NULL
;
112 busses
= usb_get_busses();
114 for (bus
= busses
; bus
; bus
= bus
->next
) {
115 struct usb_device
*dev
;
117 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
118 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
120 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
121 handle
= usb_open(dev
);
124 fprintf(stderr
, "open failed: %s!\n", usb_strerror());
128 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
138 usb_dev_handle
* OpenProxmark(int verbose
) {
140 usb_dev_handle
*handle
= NULL
;
144 handle
= findProxmark(verbose
, &iface
);
152 handle
= findProxmark(verbose
, &iface
);
156 /* detach kernel driver first */
157 ret
= usb_detach_kernel_driver_np(handle
, iface
);
158 /* don't complain if no driver attached */
159 if (ret
<0 && ret
!= -61 && verbose
)
160 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
161 ret
= usb_claim_interface(handle
, iface
);
164 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
168 claimed_iface
= iface
;
173 void CloseProxmark(void) {
174 usb_release_interface(devh
, claimed_iface
);