]>
git.zerfleddert.de Git - proxmark3-svn/blob - client/hid-flasher/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 //-----------------------------------------------------------------------------
22 #include "proxmark3.h"
25 // It seems to be missing for mingw
30 usb_dev_handle
*devh
= NULL
;
31 static unsigned int claimed_iface
= 0;
32 unsigned char return_on_error
= 0;
33 unsigned char error_occured
= 0;
35 void SendCommand(UsbCommand
*c
)
40 printf("Sending %d bytes\n", sizeof(UsbCommand
));
42 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
48 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
55 while(!OpenProxmark(0)) { sleep(1); }
63 bool ReceiveCommandPoll(UsbCommand
*c
)
67 memset(c
, 0, sizeof (UsbCommand
));
68 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
70 if (ret
!= -ETIMEDOUT
) {
75 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
82 while(!OpenProxmark(0)) { sleep(1); }
89 if (ret
&& (ret
< sizeof(UsbCommand
))) {
90 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
91 ret
, (int)sizeof(UsbCommand
));
98 void ReceiveCommand(UsbCommand
*c
)
100 // printf("%s()\n", __FUNCTION__);
103 retval
= ReceiveCommandPoll(c
);
104 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
106 // printf("recv %x\n", c->cmd);
109 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
)
111 struct usb_bus
*busses
, *bus
;
112 usb_dev_handle
*handle
= NULL
;
113 struct prox_unit units
[50];
119 busses
= usb_get_busses();
121 for (bus
= busses
; bus
; bus
= bus
->next
) {
122 struct usb_device
*dev
;
124 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
125 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
127 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
128 handle
= usb_open(dev
);
131 fprintf(stderr
, "open fabiled: %s!\n", usb_strerror());
135 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
137 struct prox_unit unit
= {handle
, {0}};
138 usb_get_string_simple(handle
, desc
->iSerialNumber
, unit
.serial_number
, sizeof(unit
.serial_number
));
139 units
[iUnit
++] = unit
;
149 fprintf(stdout
, "\nConnected units:\n");
151 for (int i
= 0; i
< iUnit
; i
++) {
152 struct usb_device
* dev
= usb_device(units
[i
].handle
);
153 fprintf(stdout
, "\t%d. SN: %s [%s/%s]\n", i
+1, units
[i
].serial_number
, dev
->bus
->dirname
, dev
->filename
);
156 while (iSelection
< 1 || iSelection
> iUnit
) {
157 fprintf(stdout
, "Which unit do you want to connect to? ");
158 fscanf(stdin
, "%d", &iSelection
);
165 for (int i
= 0; i
< iUnit
; i
++) {
166 if (iSelection
== i
) continue;
167 usb_close(units
[i
].handle
);
168 units
[i
].handle
= NULL
;
171 return units
[iSelection
].handle
;
177 usb_dev_handle
* OpenProxmark(int verbose
)
180 usb_dev_handle
*handle
= NULL
;
183 handle
= findProxmark(verbose
, &iface
);
188 /* detach kernel driver first */
189 ret
= usb_detach_kernel_driver_np(handle
, iface
);
190 /* don't complain if no driver attached */
191 if (ret
<0 && ret
!= -61 && verbose
)
192 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
195 // Needed for Windows. Optional for Mac OS and Linux
196 ret
= usb_set_configuration(handle
, 1);
199 fprintf(stderr
, "configuration set failed: %s!\n", usb_strerror());
203 ret
= usb_claim_interface(handle
, iface
);
206 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
209 claimed_iface
= iface
;
214 void CloseProxmark(void)
216 usb_release_interface(devh
, claimed_iface
);