]>
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 fprintf(stdout
, "\t%d. SN: %s\n", i
+1, units
[i
].serial_number
);
158 while (iSelection
< 1 || iSelection
> iUnit
) {
159 fprintf(stdout
, "Which unit do you want to connect to? ");
160 fscanf(stdin
, "%d", &iSelection
);
167 for (int i
= 0; i
< iUnit
; i
++) {
168 if (iSelection
== i
) continue;
169 usb_close(units
[i
].handle
);
170 units
[i
].handle
= NULL
;
173 return units
[iSelection
].handle
;
179 usb_dev_handle
* OpenProxmark(int verbose
)
182 usb_dev_handle
*handle
= NULL
;
185 handle
= findProxmark(verbose
, &iface
);
190 /* detach kernel driver first */
191 ret
= usb_detach_kernel_driver_np(handle
, iface
);
192 /* don't complain if no driver attached */
193 if (ret
<0 && ret
!= -61 && verbose
)
194 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
197 // Needed for Windows. Optional for Mac OS and Linux
198 ret
= usb_set_configuration(handle
, 1);
201 fprintf(stderr
, "configuration set failed: %s!\n", usb_strerror());
205 ret
= usb_claim_interface(handle
, iface
);
208 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
211 claimed_iface
= iface
;
216 void CloseProxmark(void)
218 usb_release_interface(devh
, claimed_iface
);