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