]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/proxusb.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
23 #include "proxmark3.h"
26 // It seems to be missing for mingw
31 usb_dev_handle
*devh
= NULL
;
32 static unsigned int claimed_iface
= 0;
33 unsigned char return_on_error
= 0;
34 unsigned char error_occured
= 0;
35 extern unsigned int current_command
;
37 void SendCommand(UsbCommand
*c
)
42 printf("Sending %d bytes\n", sizeof(UsbCommand
));
44 current_command
= c
->cmd
;
45 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
51 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
58 while(!OpenProxmark(0)) { sleep(1); }
66 bool ReceiveCommandPoll(UsbCommand
*c
)
70 memset(c
, 0, sizeof (UsbCommand
));
71 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
73 if (ret
!= -ETIMEDOUT
) {
78 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
85 while(!OpenProxmark(0)) { sleep(1); }
92 if (ret
&& (ret
< sizeof(UsbCommand
))) {
93 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
94 ret
, (int)sizeof(UsbCommand
));
101 void ReceiveCommand(UsbCommand
*c
)
103 // printf("%s()\n", __FUNCTION__);
106 retval
= ReceiveCommandPoll(c
);
107 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
109 // printf("recv %x\n", c->cmd);
112 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
)
114 struct usb_bus
*busses
, *bus
;
115 usb_dev_handle
*handle
= NULL
;
116 struct prox_unit units
[50];
122 busses
= usb_get_busses();
124 for (bus
= busses
; bus
; bus
= bus
->next
) {
125 struct usb_device
*dev
;
127 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
128 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
130 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
131 handle
= usb_open(dev
);
134 fprintf(stderr
, "open fabiled: %s!\n", usb_strerror());
138 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
140 struct prox_unit unit
= {handle
, {0}};
141 usb_get_string_simple(handle
, desc
->iSerialNumber
, unit
.serial_number
, sizeof(unit
.serial_number
));
142 units
[iUnit
++] = unit
;
152 fprintf(stdout
, "\nConnected units:\n");
154 for (int i
= 0; i
< iUnit
; i
++) {
155 struct usb_device
* dev
= usb_device(units
[i
].handle
);
156 fprintf(stdout
, "\t%d. SN: %s [%s/%s]\n", i
+1, units
[i
].serial_number
, dev
->bus
->dirname
, dev
->filename
);
159 while (iSelection
< 1 || iSelection
> iUnit
) {
160 fprintf(stdout
, "Which unit do you want to connect to? ");
161 fscanf(stdin
, "%d", &iSelection
);
168 for (int i
= 0; i
< iUnit
; i
++) {
169 if (iSelection
== i
) continue;
170 usb_close(units
[i
].handle
);
171 units
[i
].handle
= NULL
;
174 return units
[iSelection
].handle
;
180 usb_dev_handle
* OpenProxmark(int verbose
)
183 usb_dev_handle
*handle
= NULL
;
186 handle
= findProxmark(verbose
, &iface
);
191 /* detach kernel driver first */
192 ret
= usb_detach_kernel_driver_np(handle
, iface
);
193 /* don't complain if no driver attached */
194 if (ret
<0 && ret
!= -61 && verbose
)
195 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
198 // Needed for Windows. Optional for Mac OS and Linux
199 ret
= usb_set_configuration(handle
, 1);
202 fprintf(stderr
, "configuration set failed: %s!\n", usb_strerror());
206 ret
= usb_claim_interface(handle
, iface
);
209 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
212 claimed_iface
= iface
;
217 void CloseProxmark(void)
219 usb_release_interface(devh
, claimed_iface
);