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