R&Y: Added DAY RTA Tapp Card and Additional BKK BEM Stored Value Card AIDs to `aid_de...
[RRG-proxmark3.git] / tools / deprecated-hid-flasher / flasher / proxusb.c
blob88495029740ef28e65ccb675e62e4e0e8b426511
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 //
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
7 // the license.
8 //-----------------------------------------------------------------------------
9 // USB utilities
10 //-----------------------------------------------------------------------------
11 #include "proxusb.h"
12 #include "sleep.h"
14 // It seems to be missing for mingw
15 #ifndef ETIMEDOUT
16 #define ETIMEDOUT 116
17 #endif
19 usb_dev_handle *devh = NULL;
20 static unsigned int claimed_iface = 0;
21 unsigned char return_on_error = 0;
22 unsigned char error_occurred = 0;
24 void SendCommandBL(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len) {
25 int ret;
26 PacketCommandOLD c = {CMD_UNKNOWN, {0, 0, 0}, {{0}}};
27 c.cmd = cmd;
28 c.arg[0] = arg0;
29 c.arg[1] = arg1;
30 c.arg[2] = arg2;
31 if (len && data)
32 memcpy(&c.d, data, len);
34 #if 0
35 printf("Sending %d bytes\n", sizeof(PacketCommandOLD));
36 #endif
38 ret = usb_bulk_write(devh, 0x01, (char *)&c, sizeof(PacketCommandOLD), 1000);
39 if (ret < 0) {
40 error_occurred = 1;
41 if (return_on_error)
42 return;
44 fprintf(stderr, "write failed: %s!\nTrying to reopen device...\n",
45 usb_strerror());
47 if (devh) {
48 usb_close(devh);
49 devh = NULL;
51 while (!OpenProxmark(0)) { msleep(1000); }
52 printf(PROXPROMPT);
53 fflush(NULL);
55 return;
59 bool ReceiveCommandPoll(PacketResponseOLD *c) {
60 int ret;
62 memset(c, 0, sizeof(PacketResponseOLD));
63 ret = usb_bulk_read(devh, 0x82, (char *)c, sizeof(PacketResponseOLD), 500);
64 if (ret < 0) {
65 if (ret != -ETIMEDOUT) {
66 error_occurred = 1;
67 if (return_on_error)
68 return false;
70 fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n",
71 usb_strerror(), ret);
73 if (devh) {
74 usb_close(devh);
75 devh = NULL;
77 while (!OpenProxmark(0)) { msleep(1000); }
78 printf(PROXPROMPT);
79 fflush(NULL);
81 return false;
83 } else {
84 if (ret && (ret < sizeof(PacketResponseOLD))) {
85 fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
86 ret, (int)sizeof(PacketResponseOLD));
90 return ret > 0;
93 void ReceiveCommand(PacketResponseOLD *c) {
94 // printf("%s()\n", __FUNCTION__);
95 int retval = 0;
96 do {
97 retval = ReceiveCommandPoll(c);
98 if (retval != 1) printf("ReceiveCommandPoll returned %d\n", retval);
99 } while (retval < 0);
100 // printf("recv %x\n", c->cmd);
103 usb_dev_handle *findProxmark(int verbose, unsigned int *iface) {
104 struct usb_bus *busses, *bus;
105 usb_dev_handle *handle = NULL;
106 struct prox_unit units[50];
107 int iUnit = 0;
109 usb_find_busses();
110 usb_find_devices();
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);
122 if (!handle) {
123 if (verbose)
124 fprintf(stderr, "open fabiled: %s!\n", usb_strerror());
125 //return NULL;
126 continue;
128 *iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
130 struct prox_unit unit = {handle, {0}};
131 usb_get_string_simple(handle, desc->iSerialNumber, unit.serial_number, sizeof(unit.serial_number));
132 units[iUnit++] = unit;
134 //return handle;
139 if (iUnit > 0) {
140 int iSelection = 0;
142 fprintf(stdout, "\nConnected units:\n");
144 for (int i = 0; i < iUnit; i++) {
145 struct usb_device *dev = usb_device(units[i].handle);
146 fprintf(stdout, "\t%d. SN: %s [%s/%s]\n", i + 1, units[i].serial_number, dev->bus->dirname, dev->filename);
148 if (iUnit > 1) {
149 while (iSelection < 1 || iSelection > iUnit) {
150 fprintf(stdout, "Which unit do you want to connect to? ");
151 int res = fscanf(stdin, "%d", &iSelection);
152 if (res != 1) {
153 fprintf(stderr, "Input parse error");
154 fflush(stderr);
155 abort();
158 } else {
159 iSelection = 1;
162 iSelection --;
164 for (int i = 0; i < iUnit; i++) {
165 if (iSelection == i) continue;
166 usb_close(units[i].handle);
167 units[i].handle = NULL;
170 return units[iSelection].handle;
172 return NULL;
175 usb_dev_handle *OpenProxmark(int verbose) {
176 int ret;
177 usb_dev_handle *handle;
178 unsigned int iface;
180 handle = findProxmark(verbose, &iface);
181 if (!handle)
182 return NULL;
184 #ifdef __linux__
185 /* detach kernel driver first */
186 ret = usb_detach_kernel_driver_np(handle, iface);
187 /* don't complain if no driver attached */
188 if (ret < 0 && ret != -61 && verbose)
189 fprintf(stderr, "detach kernel driver failed: (%d) %s!\n", ret, usb_strerror());
190 #endif
192 // Needed for Windows. Optional for Mac OS and Linux
193 ret = usb_set_configuration(handle, 1);
194 if (ret < 0) {
195 if (verbose)
196 fprintf(stderr, "configuration set failed: %s!\n", usb_strerror());
197 return NULL;
200 ret = usb_claim_interface(handle, iface);
201 if (ret < 0) {
202 if (verbose)
203 fprintf(stderr, "claim failed: %s!\n", usb_strerror());
204 return NULL;
206 claimed_iface = iface;
207 devh = handle;
208 return handle;
211 void CloseProxmark(void) {
212 usb_release_interface(devh, claimed_iface);
213 usb_close(devh);
214 devh = NULL;