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