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