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