]>
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;
34 extern unsigned int current_command
;
36 void SendCommand(UsbCommand
*c
)
41 printf("Sending %d bytes\n", sizeof(UsbCommand
));
43 current_command
= c
->cmd
;
44 ret
= usb_bulk_write(devh
, 0x01, (char*)c
, sizeof(UsbCommand
), 1000);
50 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
57 while(!OpenProxmark(0)) { sleep(1); }
65 bool ReceiveCommandPoll(UsbCommand
*c
)
69 memset(c
, 0, sizeof (UsbCommand
));
70 ret
= usb_bulk_read(devh
, 0x82, (char*)c
, sizeof(UsbCommand
), 500);
72 if (ret
!= -ETIMEDOUT
) {
77 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
84 while(!OpenProxmark(0)) { sleep(1); }
91 if (ret
&& (ret
< sizeof(UsbCommand
))) {
92 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
93 ret
, (int)sizeof(UsbCommand
));
100 void ReceiveCommand(UsbCommand
*c
)
102 // printf("%s()\n", __FUNCTION__);
105 retval
= ReceiveCommandPoll(c
);
106 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
108 // printf("recv %x\n", c->cmd);
111 usb_dev_handle
* findProxmark(int verbose
, unsigned int *iface
)
113 struct usb_bus
*busses
, *bus
;
114 usb_dev_handle
*handle
= NULL
;
115 struct prox_unit units
[50];
121 busses
= usb_get_busses();
123 for (bus
= busses
; bus
; bus
= bus
->next
) {
124 struct usb_device
*dev
;
126 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
127 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
129 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
130 handle
= usb_open(dev
);
133 fprintf(stderr
, "open fabiled: %s!\n", usb_strerror());
137 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
139 struct prox_unit unit
= {handle
, {0}};
140 usb_get_string_simple(handle
, desc
->iSerialNumber
, unit
.serial_number
, sizeof(unit
.serial_number
));
141 units
[iUnit
++] = unit
;
151 fprintf(stdout
, "\nConnected units:\n");
153 for (int i
= 0; i
< iUnit
; i
++) {
154 struct usb_device
* dev
= usb_device(units
[i
].handle
);
155 fprintf(stdout
, "\t%d. SN: %s [%s/%s]\n", i
+1, units
[i
].serial_number
, dev
->bus
->dirname
, dev
->filename
);
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
);