]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/proxusb.c
Add license headers to armsrc/bootrom/common stuff
[proxmark3-svn] / client / proxusb.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <unistd.h>
5 #include <usb.h>
6 #include <strings.h>
7 #include <errno.h>
8
9 #include "sleep.h"
10 #include "proxusb.h"
11 #include "proxmark3.h"
12 #include "usb_cmd.h"
13
14 // It seems to be missing for mingw
15 #ifndef ETIMEDOUT
16 #define ETIMEDOUT 116
17 #endif
18
19 usb_dev_handle *devh = NULL;
20 static unsigned int claimed_iface = 0;
21 unsigned char return_on_error = 0;
22 unsigned char error_occured = 0;
23 extern unsigned int current_command;
24
25 void SendCommand(UsbCommand *c)
26 {
27 int ret;
28
29 #if 0
30 printf("Sending %d bytes\n", sizeof(UsbCommand));
31 #endif
32 current_command = c->cmd;
33 ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000);
34 if (ret<0) {
35 error_occured = 1;
36 if (return_on_error)
37 return;
38
39 fprintf(stderr, "write failed: %s!\nTrying to reopen device...\n",
40 usb_strerror());
41
42 if (devh) {
43 usb_close(devh);
44 devh = NULL;
45 }
46 while(!OpenProxmark(0)) { sleep(1); }
47 printf(PROXPROMPT);
48 fflush(NULL);
49
50 return;
51 }
52 }
53
54 bool ReceiveCommandPoll(UsbCommand *c)
55 {
56 int ret;
57
58 memset(c, 0, sizeof (UsbCommand));
59 ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(UsbCommand), 500);
60 if (ret<0) {
61 if (ret != -ETIMEDOUT) {
62 error_occured = 1;
63 if (return_on_error)
64 return false;
65
66 fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n",
67 usb_strerror(), ret);
68
69 if (devh) {
70 usb_close(devh);
71 devh = NULL;
72 }
73 while(!OpenProxmark(0)) { sleep(1); }
74 printf(PROXPROMPT);
75 fflush(NULL);
76
77 return false;
78 }
79 } else {
80 if (ret && (ret < sizeof(UsbCommand))) {
81 fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
82 ret, (int)sizeof(UsbCommand));
83 }
84 }
85
86 return ret > 0;
87 }
88
89 void ReceiveCommand(UsbCommand *c)
90 {
91 // printf("%s()\n", __FUNCTION__);
92 int retval = 0;
93 do {
94 retval = ReceiveCommandPoll(c);
95 if (retval != 1) printf("ReceiveCommandPoll returned %d\n", retval);
96 } while(retval<0);
97 // printf("recv %x\n", c->cmd);
98 }
99
100 usb_dev_handle* findProxmark(int verbose, unsigned int *iface)
101 {
102 struct usb_bus *busses, *bus;
103 usb_dev_handle *handle = NULL;
104
105 usb_find_busses();
106 usb_find_devices();
107
108 busses = usb_get_busses();
109
110 for (bus = busses; bus; bus = bus->next) {
111 struct usb_device *dev;
112
113 for (dev = bus->devices; dev; dev = dev->next) {
114 struct usb_device_descriptor *desc = &(dev->descriptor);
115
116 if ((desc->idProduct == 0x4b8f) && (desc->idVendor == 0x9ac4)) {
117 handle = usb_open(dev);
118 if (!handle) {
119 if (verbose)
120 fprintf(stderr, "open failed: %s!\n", usb_strerror());
121 return NULL;
122 }
123 *iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber;
124 return handle;
125 }
126
127 }
128 }
129
130 return NULL;
131 }
132
133 usb_dev_handle* OpenProxmark(int verbose)
134 {
135 int ret;
136 usb_dev_handle *handle = NULL;
137 unsigned int iface;
138
139 #ifdef __linux__
140 handle = findProxmark(verbose, &iface);
141 if (!handle)
142 return NULL;
143
144 /* Whatever... */
145 usb_reset(handle);
146 #endif
147
148 handle = findProxmark(verbose, &iface);
149 if (!handle)
150 return NULL;
151
152 #ifdef __linux__
153 /* detach kernel driver first */
154 ret = usb_detach_kernel_driver_np(handle, iface);
155 /* don't complain if no driver attached */
156 if (ret<0 && ret != -61 && verbose)
157 fprintf(stderr, "detach kernel driver failed: (%d) %s!\n", ret, usb_strerror());
158 #endif
159
160 // Needed for Windows. Optional for Mac OS and Linux
161 ret = usb_set_configuration(handle, 1);
162 if (ret < 0) {
163 if (verbose)
164 fprintf(stderr, "configuration set failed: %s!\n", usb_strerror());
165 return NULL;
166 }
167
168 ret = usb_claim_interface(handle, iface);
169 if (ret < 0) {
170 if (verbose)
171 fprintf(stderr, "claim failed: %s!\n", usb_strerror());
172 return NULL;
173 }
174 claimed_iface = iface;
175 devh = handle;
176 return handle;
177 }
178
179 void CloseProxmark(void)
180 {
181 usb_release_interface(devh, claimed_iface);
182 usb_close(devh);
183 devh = NULL;
184 }
Impressum, Datenschutz