]> git.zerfleddert.de Git - proxmark3-svn/blob - client/proxusb.c
major USB update
[proxmark3-svn] / client / proxusb.c
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
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdbool.h>
16 #include <unistd.h>
17 #include <usb.h>
18 #include <strings.h>
19 #include <errno.h>
20
21 #include "sleep.h"
22 #include "proxusb.h"
23 #include "proxmark3.h"
24 #include "usb_cmd.h"
25
26 // It seems to be missing for mingw
27 #ifndef ETIMEDOUT
28 #define ETIMEDOUT 116
29 #endif
30
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;
36
37 void SendCommand_(HidCommand *c)
38 {
39 int ret;
40
41 #if 0
42 printf("Sending %d bytes\n", sizeof(HidCommand));
43 #endif
44 current_command = c->cmd;
45 ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(HidCommand), 1000);
46 if (ret<0) {
47 error_occured = 1;
48 if (return_on_error)
49 return;
50
51 fprintf(stderr, "write failed: %s!\nTrying to reopen device...\n",
52 usb_strerror());
53
54 if (devh) {
55 usb_close(devh);
56 devh = NULL;
57 }
58 while(!OpenProxmark(0)) { sleep(1); }
59 printf(PROXPROMPT);
60 fflush(NULL);
61
62 return;
63 }
64 }
65
66 bool ReceiveCommandPoll(HidCommand *c)
67 {
68 int ret;
69
70 memset(c, 0, sizeof (HidCommand));
71 ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(HidCommand), 500);
72 if (ret<0) {
73 if (ret != -ETIMEDOUT) {
74 error_occured = 1;
75 if (return_on_error)
76 return false;
77
78 fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n",
79 usb_strerror(), ret);
80
81 if (devh) {
82 usb_close(devh);
83 devh = NULL;
84 }
85 while(!OpenProxmark(0)) { sleep(1); }
86 printf(PROXPROMPT);
87 fflush(NULL);
88
89 return false;
90 }
91 } else {
92 if (ret && (ret < sizeof(HidCommand))) {
93 fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
94 ret, (int)sizeof(HidCommand));
95 }
96 }
97
98 return ret > 0;
99 }
100
101 void ReceiveCommand(HidCommand *c)
102 {
103 // printf("%s()\n", __FUNCTION__);
104 int retval = 0;
105 do {
106 retval = ReceiveCommandPoll(c);
107 if (retval != 1) printf("ReceiveCommandPoll returned %d\n", retval);
108 } while(retval<0);
109 // printf("recv %x\n", c->cmd);
110 }
111
112 usb_dev_handle* findProxmark(int verbose, unsigned int *iface)
113 {
114 struct usb_bus *busses, *bus;
115 usb_dev_handle *handle = NULL;
116 struct prox_unit units[50];
117 int iUnit = 0;
118
119 usb_find_busses();
120 usb_find_devices();
121
122 busses = usb_get_busses();
123
124 for (bus = busses; bus; bus = bus->next) {
125 struct usb_device *dev;
126
127 for (dev = bus->devices; dev; dev = dev->next) {
128 struct usb_device_descriptor *desc = &(dev->descriptor);
129
130 if ((desc->idProduct == 0x4b8f) && (desc->idVendor == 0x9ac4)) {
131 handle = usb_open(dev);
132 if (!handle) {
133 if (verbose)
134 fprintf(stderr, "open fabiled: %s!\n", usb_strerror());
135 //return NULL;
136 continue;
137 }
138 *iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
139
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;
143
144 //return handle;
145 }
146 }
147 }
148
149 if (iUnit > 0) {
150 int iSelection = 0;
151
152 fprintf(stdout, "\nConnected units:\n");
153
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);
157 }
158 if (iUnit > 1) {
159 while (iSelection < 1 || iSelection > iUnit) {
160 fprintf(stdout, "Which unit do you want to connect to? ");
161 fscanf(stdin, "%d", &iSelection);
162 }
163 }
164 else
165 iSelection = 1;
166 iSelection --;
167
168 for (int i = 0; i < iUnit; i++) {
169 if (iSelection == i) continue;
170 usb_close(units[i].handle);
171 units[i].handle = NULL;
172 }
173
174 return units[iSelection].handle;
175 }
176
177 return NULL;
178 }
179
180 usb_dev_handle* OpenProxmark(int verbose)
181 {
182 int ret;
183 usb_dev_handle *handle = NULL;
184 unsigned int iface;
185
186 handle = findProxmark(verbose, &iface);
187 if (!handle)
188 return NULL;
189
190 #ifdef __linux__
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());
196 #endif
197
198 // Needed for Windows. Optional for Mac OS and Linux
199 ret = usb_set_configuration(handle, 1);
200 if (ret < 0) {
201 if (verbose)
202 fprintf(stderr, "configuration set failed: %s!\n", usb_strerror());
203 return NULL;
204 }
205
206 ret = usb_claim_interface(handle, iface);
207 if (ret < 0) {
208 if (verbose)
209 fprintf(stderr, "claim failed: %s!\n", usb_strerror());
210 return NULL;
211 }
212 claimed_iface = iface;
213 devh = handle;
214 return handle;
215 }
216
217 void CloseProxmark(void)
218 {
219 usb_release_interface(devh, claimed_iface);
220 usb_close(devh);
221 devh = NULL;
222 }
Impressum, Datenschutz