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