Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 563 lines (488 sloc) 16.561 kb
0902f21a »
2012-03-11 add license headers and some cleanup
1 /**
2 * libztex.c - Ztex 1.15x fpga board support library
3 *
4 * Copyright (c) 2012 nelisky.btc@gmail.com
5 *
6 * This work is based upon the Java SDK provided by ztex which is
7 * Copyright (C) 2009-2011 ZTEX GmbH.
8 * http://www.ztex.de
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see http://www.gnu.org/licenses/.
21 **/
22
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
23 #include <stdio.h>
24 #include <unistd.h>
25 #include "miner.h"
26 #include "libztex.h"
27
28 #define BUFSIZE 256
29
30 //* Capability index for EEPROM support.
31 #define CAPABILITY_EEPROM 0,0
32 //* Capability index for FPGA configuration support.
33 #define CAPABILITY_FPGA 0,1
34 //* Capability index for FLASH memory support.
35 #define CAPABILITY_FLASH 0,2
36 //* Capability index for DEBUG helper support.
37 #define CAPABILITY_DEBUG 0,3
38 //* Capability index for AVR XMEGA support.
39 #define CAPABILITY_XMEGA 0,4
40 //* Capability index for AVR XMEGA support.
41 #define CAPABILITY_HS_FPGA 0,5
42 //* Capability index for AVR XMEGA support.
43 #define CAPABILITY_MAC_EEPROM 0,6
5f5e1063 »
2012-04-22 Firmware 15d4 support
44 //* Capability index for multi FPGA support.
45 #define CAPABILITY_MULTI_FPGA 0,7
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
46
47
bd46119c »
2012-04-21 Style police on libztex.c.
48 static bool libztex_checkDevice(struct libusb_device *dev)
49 {
50 struct libusb_device_descriptor desc;
c280dd0d »
2012-04-17 Moving space indentation to tabs
51 int err;
52
53 err = libusb_get_device_descriptor(dev, &desc);
54 if (unlikely(err != 0)) {
55 applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
56 return false;
57 }
58 if (!(desc.idVendor == LIBZTEX_IDVENDOR && desc.idProduct == LIBZTEX_IDPRODUCT)) {
9ba2e217 »
2012-04-18 Some cleanup
59 applog(LOG_DEBUG, "Not a ZTEX device %0.4x:%0.4x", desc.idVendor, desc.idProduct);
c280dd0d »
2012-04-17 Moving space indentation to tabs
60 return false;
61 }
62 return true;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
63 }
64
bd46119c »
2012-04-21 Style police on libztex.c.
65 static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
66 {
67 if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
b97e6aa5 »
2012-04-22 Presumably we should return false when the capability is missing in l…
68 (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) {
5f5e1063 »
2012-04-22 Firmware 15d4 support
69 applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, j);
b97e6aa5 »
2012-04-22 Presumably we should return false when the capability is missing in l…
70 return false;
71 }
c280dd0d »
2012-04-17 Moving space indentation to tabs
72 return true;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
73 }
74
bd46119c »
2012-04-21 Style police on libztex.c.
75 static int libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
76 {
c280dd0d »
2012-04-17 Moving space indentation to tabs
77 int i;
bd46119c »
2012-04-21 Style police on libztex.c.
78
79 for (i = 0; i < size - 4; i++) {
80 if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66))
c280dd0d »
2012-04-17 Moving space indentation to tabs
81 return 1;
bd46119c »
2012-04-21 Style police on libztex.c.
82 if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66))
c280dd0d »
2012-04-17 Moving space indentation to tabs
83 return 0;
84 }
85 applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
86 return 0;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
87 }
88
bd46119c »
2012-04-21 Style police on libztex.c.
89 static void libztex_swapBits(unsigned char *buf, int size)
90 {
c280dd0d »
2012-04-17 Moving space indentation to tabs
91 unsigned char c;
bd46119c »
2012-04-21 Style police on libztex.c.
92 int i;
93
94 for (i = 0; i < size; i++) {
c280dd0d »
2012-04-17 Moving space indentation to tabs
95 c = buf[i];
96 buf[i] = ((c & 128) >> 7) |
97 ((c & 64) >> 5) |
98 ((c & 32) >> 3) |
99 ((c & 16) >> 1) |
100 ((c & 8) << 1) |
101 ((c & 4) << 3) |
102 ((c & 2) << 5) |
103 ((c & 1) << 7);
104 }
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
105 }
106
bd46119c »
2012-04-21 Style police on libztex.c.
107 static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
108 {
c280dd0d »
2012-04-17 Moving space indentation to tabs
109 unsigned char buf[9];
bd46119c »
2012-04-21 Style police on libztex.c.
110 int cnt;
111
9ba2e217 »
2012-04-18 Some cleanup
112 if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
c280dd0d »
2012-04-17 Moving space indentation to tabs
113 return -1;
114 cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
115 if (unlikely(cnt < 0)) {
116 applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
117 return cnt;
118 }
9ba2e217 »
2012-04-18 Some cleanup
119 state->fpgaConfigured = (buf[0] == 0);
c280dd0d »
2012-04-17 Moving space indentation to tabs
120 state->fpgaChecksum = buf[1] & 0xff;
bd46119c »
2012-04-21 Style police on libztex.c.
121 state->fpgaBytes = ((buf[5] & 0xff) << 24) | ((buf[4] & 0xff) << 16) | ((buf[3] & 0xff) << 8) | (buf[2] & 0xff);
c280dd0d »
2012-04-17 Moving space indentation to tabs
122 state->fpgaInitB = buf[6] & 0xff;
123 state->fpgaFlashResult = buf[7];
9ba2e217 »
2012-04-18 Some cleanup
124 state->fpgaFlashBitSwap = (buf[8] != 0);
c280dd0d »
2012-04-17 Moving space indentation to tabs
125 return 0;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
126 }
127
bd46119c »
2012-04-21 Style police on libztex.c.
128 static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
129 {
c280dd0d »
2012-04-17 Moving space indentation to tabs
130 struct libztex_fpgastate state;
bd46119c »
2012-04-21 Style police on libztex.c.
131 const int transactionBytes = 2048;
c280dd0d »
2012-04-17 Moving space indentation to tabs
132 unsigned char buf[transactionBytes], cs;
133 int tries, cnt, buf_p, i;
bd46119c »
2012-04-21 Style police on libztex.c.
134 ssize_t pos = 0;
c280dd0d »
2012-04-17 Moving space indentation to tabs
135 FILE *fp;
136
9ba2e217 »
2012-04-18 Some cleanup
137 if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
c280dd0d »
2012-04-17 Moving space indentation to tabs
138 return -1;
139
140 libztex_getFpgaState(ztex, &state);
9ba2e217 »
2012-04-18 Some cleanup
141 if (!force && state.fpgaConfigured) {
142 applog(LOG_DEBUG, "Bitstream already configured");
143 return 1;
c280dd0d »
2012-04-17 Moving space indentation to tabs
144 }
145
bd46119c »
2012-04-21 Style police on libztex.c.
146 for (tries = 10; tries > 0; tries--) {
c280dd0d »
2012-04-17 Moving space indentation to tabs
147 fp = fopen(firmware, "rb");
148 if (!fp) {
149 applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
150 return -2;
151 }
152
153 cs = 0;
154 while (pos < transactionBytes && !feof(fp)) {
155 buf[pos] = getc(fp);
156 cs += buf[pos++];
bd46119c »
2012-04-21 Style police on libztex.c.
157 }
158
c280dd0d »
2012-04-17 Moving space indentation to tabs
159 if (feof(fp))
160 pos--;
161
bd46119c »
2012-04-21 Style police on libztex.c.
162 if (bs != 0 && bs != 1)
163 bs = libztex_detectBitstreamBitOrder(buf, transactionBytes < pos? transactionBytes: pos);
c280dd0d »
2012-04-17 Moving space indentation to tabs
164
165 //* Reset fpga
166 cnt = libztex_resetFpga(ztex);
167 if (unlikely(cnt < 0)) {
168 applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
169 continue;
170 }
171
bd46119c »
2012-04-21 Style police on libztex.c.
172 if (bs == 1)
c280dd0d »
2012-04-17 Moving space indentation to tabs
173 libztex_swapBits(buf, pos);
174
175 buf_p = pos;
176 while (1) {
177 i = 0;
178 while (i < buf_p) {
bd46119c »
2012-04-21 Style police on libztex.c.
179 cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], buf_p - i, 5000);
c280dd0d »
2012-04-17 Moving space indentation to tabs
180 if (unlikely(cnt < 0)) {
181 applog(LOG_ERR, "%s: Failed send fpga data with err %d", ztex->repr, cnt);
182 break;
183 }
184 i += cnt;
185 }
186 if (i < buf_p || buf_p < transactionBytes)
187 break;
188 buf_p = 0;
189 while (buf_p < transactionBytes && !feof(fp)) {
190 buf[buf_p] = getc(fp);
191 cs += buf[buf_p++];
bd46119c »
2012-04-21 Style police on libztex.c.
192 }
c280dd0d »
2012-04-17 Moving space indentation to tabs
193 if (feof(fp))
194 buf_p--;
195 pos += buf_p;
196 if (buf_p == 0)
197 break;
bd46119c »
2012-04-21 Style police on libztex.c.
198 if (bs == 1)
c280dd0d »
2012-04-17 Moving space indentation to tabs
199 libztex_swapBits(buf, buf_p);
200 }
201 if (cnt >= 0)
202 tries = 0;
203
204 fclose(fp);
205 }
206 libztex_getFpgaState(ztex, &state);
207 if (!state.fpgaConfigured) {
208 applog(LOG_ERR, "%s: FPGA configuration failed: DONE pin does not go high", ztex->repr);
209 return 3;
210 }
211 usleep(200000);
9ba2e217 »
2012-04-18 Some cleanup
212 applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr);
c280dd0d »
2012-04-17 Moving space indentation to tabs
213 return 0;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
214 }
215
bd46119c »
2012-04-21 Style police on libztex.c.
216 int libztex_configureFpga(struct libztex_device *ztex)
217 {
c280dd0d »
2012-04-17 Moving space indentation to tabs
218 char buf[256] = "bitstreams/";
bd46119c »
2012-04-21 Style police on libztex.c.
219
c280dd0d »
2012-04-17 Moving space indentation to tabs
220 memset(&buf[11], 0, 245);
221 strcpy(&buf[11], ztex->bitFileName);
222 strcpy(&buf[strlen(buf)], ".bit");
223
bd46119c »
2012-04-21 Style police on libztex.c.
224 return libztex_configureFpgaLS(ztex, buf, true, 2);
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
225 }
226
5f5e1063 »
2012-04-22 Firmware 15d4 support
227 int libztex_numberOfFpgas(struct libztex_device *ztex) {
228 int cnt;
229 unsigned char buf[3];
316ebc3b »
2012-04-25 Code style cleanup
230 if (ztex->numberOfFpgas < 0) {
5f5e1063 »
2012-04-22 Firmware 15d4 support
231 if (libztex_checkCapability(ztex, CAPABILITY_MULTI_FPGA)) {
232 cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x50, 0, 0, buf, 3, 1000);
233 if (unlikely(cnt < 0)) {
234 applog(LOG_ERR, "%s: Failed getMultiFpgaInfo with err %d", ztex->repr, cnt);
235 return cnt;
236 }
316ebc3b »
2012-04-25 Code style cleanup
237 ztex->numberOfFpgas = buf[0] + 1;
5f5e1063 »
2012-04-22 Firmware 15d4 support
238 ztex->selectedFpga = buf[1];
239 ztex->parallelConfigSupport = (buf[2] == 1);
240 } else {
241 ztex->numberOfFpgas = 1;
242 ztex->selectedFpga = 0;
243 ztex->parallelConfigSupport = false;
244 }
245 }
246 return ztex->numberOfFpgas;
247 }
248
249 int libztex_selectFpga(struct libztex_device *ztex, int number) {
250 int cnt, fpgacnt = libztex_numberOfFpgas(ztex);
251 if (number < 0 || number >= fpgacnt) {
252 applog(LOG_WARNING, "%s: Trying to select wrong fpga (%d in %d)", ztex->repr, number, fpgacnt);
253 return 1;
254 }
255 if (ztex->selectedFpga != number && libztex_checkCapability(ztex, CAPABILITY_MULTI_FPGA)) {
256 cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x51, number, 0, NULL, 0, 500);
257 if (unlikely(cnt < 0)) {
258 applog(LOG_ERR, "Ztex check device: Failed to set fpga with err %d", cnt);
259 return cnt;
260 }
261 ztex->selectedFpga = number;
262 }
263 return 0;
264 }
265
266 int libztex_setFreq(struct libztex_device *ztex, uint16_t freq) {
c280dd0d »
2012-04-17 Moving space indentation to tabs
267 int cnt;
6bb3c40d »
2012-04-25 Annotating frequency changes with the changed from value
268 uint16_t oldfreq = ztex->freqM;
bd46119c »
2012-04-21 Style police on libztex.c.
269
9ba2e217 »
2012-04-18 Some cleanup
270 if (freq > ztex->freqMaxM)
c280dd0d »
2012-04-17 Moving space indentation to tabs
271 freq = ztex->freqMaxM;
272
273 cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
274 if (unlikely(cnt < 0)) {
275 applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt);
276 return cnt;
277 }
278 ztex->freqM = freq;
6bb3c40d »
2012-04-25 Annotating frequency changes with the changed from value
279 applog(LOG_WARNING, "%s: Frequency change from %0.2f to %0.2f Mhz",
316ebc3b »
2012-04-25 Code style cleanup
280 ztex->repr, ztex->freqM1 * (oldfreq + 1), ztex->freqM1 * (ztex->freqM + 1));
c280dd0d »
2012-04-17 Moving space indentation to tabs
281
282 return 0;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
283 }
284
bd46119c »
2012-04-21 Style police on libztex.c.
285 int libztex_resetFpga(struct libztex_device *ztex)
286 {
c280dd0d »
2012-04-17 Moving space indentation to tabs
287 return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
5bbce4c4 »
2012-03-12 nonce check and error driven freq scaling
288 }
289
5f5e1063 »
2012-04-22 Firmware 15d4 support
290 int libztex_suspend(struct libztex_device *ztex) {
291 if (ztex->suspendSupported) {
292 return libusb_control_transfer(ztex->hndl, 0x40, 0x84, 0, 0, NULL, 0, 1000);
293 } else {
294 return 0;
295 }
296 }
297
298 int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex) {
c280dd0d »
2012-04-17 Moving space indentation to tabs
299 struct libztex_device *newdev;
5f5e1063 »
2012-04-22 Firmware 15d4 support
300 int i, cnt, err;
c280dd0d »
2012-04-17 Moving space indentation to tabs
301 unsigned char buf[64];
302
303 newdev = malloc(sizeof(struct libztex_device));
bd46119c »
2012-04-21 Style police on libztex.c.
304 newdev->bitFileName = NULL;
5f5e1063 »
2012-04-22 Firmware 15d4 support
305 newdev->numberOfFpgas = -1;
c280dd0d »
2012-04-17 Moving space indentation to tabs
306 newdev->valid = false;
307 newdev->hndl = NULL;
308 *ztex = newdev;
309
310 err = libusb_get_device_descriptor(dev, &newdev->descriptor);
311 if (unlikely(err != 0)) {
312 applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
313 return err;
314 }
315
316 // Check vendorId and productId
317 if (!(newdev->descriptor.idVendor == LIBZTEX_IDVENDOR &&
316ebc3b »
2012-04-25 Code style cleanup
318 newdev->descriptor.idProduct == LIBZTEX_IDPRODUCT)) {
c280dd0d »
2012-04-17 Moving space indentation to tabs
319 applog(LOG_ERR, "Not a ztex device? %0.4X, %0.4X", newdev->descriptor.idVendor, newdev->descriptor.idProduct);
320 return 1;
321 }
322
323 err = libusb_open(dev, &newdev->hndl);
324 if (unlikely(err != 0)) {
325 applog(LOG_ERR, "Ztex check device: Failed to open handle with error %d", err);
326 return err;
327 }
328
329 cnt = libusb_get_string_descriptor_ascii (newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString,
bd46119c »
2012-04-21 Style police on libztex.c.
330 LIBZTEX_SNSTRING_LEN + 1);
c280dd0d »
2012-04-17 Moving space indentation to tabs
331 if (unlikely(cnt < 0)) {
332 applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
333 return cnt;
334 }
335
336 cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
337 if (unlikely(cnt < 0)) {
338 applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
339 return cnt;
340 }
341
316ebc3b »
2012-04-25 Code style cleanup
342 if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') {
c280dd0d »
2012-04-17 Moving space indentation to tabs
343 applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
344 return 2;
345 }
346
347 newdev->productId[0] = buf[6];
348 newdev->productId[1] = buf[7];
349 newdev->productId[2] = buf[8];
350 newdev->productId[3] = buf[9];
351 newdev->fwVersion = buf[10];
352 newdev->interfaceVersion = buf[11];
353 newdev->interfaceCapabilities[0] = buf[12];
354 newdev->interfaceCapabilities[1] = buf[13];
355 newdev->interfaceCapabilities[2] = buf[14];
356 newdev->interfaceCapabilities[3] = buf[15];
357 newdev->interfaceCapabilities[4] = buf[16];
358 newdev->interfaceCapabilities[5] = buf[17];
359 newdev->moduleReserved[0] = buf[18];
360 newdev->moduleReserved[1] = buf[19];
361 newdev->moduleReserved[2] = buf[20];
362 newdev->moduleReserved[3] = buf[21];
363 newdev->moduleReserved[4] = buf[22];
364 newdev->moduleReserved[5] = buf[23];
365 newdev->moduleReserved[6] = buf[24];
366 newdev->moduleReserved[7] = buf[25];
367 newdev->moduleReserved[8] = buf[26];
368 newdev->moduleReserved[9] = buf[27];
369 newdev->moduleReserved[10] = buf[28];
370 newdev->moduleReserved[11] = buf[29];
371
372
373 cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
374 if (unlikely(cnt < 0)) {
375 applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
376 return cnt;
377 }
378
5f5e1063 »
2012-04-22 Firmware 15d4 support
379 if (unlikely(buf[0] != 5)) {
380 if (unlikely(buf[0] != 2 && buf[0] != 4)) {
c280dd0d »
2012-04-17 Moving space indentation to tabs
381 applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
382 return 3;
383 }
5f5e1063 »
2012-04-22 Firmware 15d4 support
384 applog(LOG_WARNING, "Firmware out of date (%d).", buf[0]);
c280dd0d »
2012-04-17 Moving space indentation to tabs
385 }
386
316ebc3b »
2012-04-25 Code style cleanup
387 i = buf[0] > 4? 11: (buf[0] > 2? 10: 8);
5f5e1063 »
2012-04-22 Firmware 15d4 support
388
389 while (cnt < 64 && buf[cnt] != 0)
390 cnt++;
316ebc3b »
2012-04-25 Code style cleanup
391 if (cnt < i + 1) {
5f5e1063 »
2012-04-22 Firmware 15d4 support
392 applog(LOG_ERR, "Invalid bitstream file name .");
393 return 4;
394 }
395
316ebc3b »
2012-04-25 Code style cleanup
396 newdev->bitFileName = malloc(sizeof(char) * (cnt + 1));
5f5e1063 »
2012-04-22 Firmware 15d4 support
397 memcpy(newdev->bitFileName, &buf[i], cnt);
398 newdev->bitFileName[cnt] = 0;
399
c280dd0d »
2012-04-17 Moving space indentation to tabs
400 newdev->numNonces = buf[1] + 1;
9ba2e217 »
2012-04-18 Some cleanup
401 newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
bd46119c »
2012-04-21 Style police on libztex.c.
402 newdev->freqM1 = ((buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
c280dd0d »
2012-04-17 Moving space indentation to tabs
403 newdev->freqMaxM = (buf[7] & 255);
404 newdev->freqM = (buf[6] & 255);
405 newdev->freqMDefault = newdev->freqM;
5f5e1063 »
2012-04-22 Firmware 15d4 support
406 newdev->suspendSupported = (buf[0] == 5);
316ebc3b »
2012-04-25 Code style cleanup
407 newdev->hashesPerClock = buf[0] > 2? (((buf[8] & 255) | ((buf[9] & 255) << 8)) + 1) / 128.0: 1.0;
408 newdev->extraSolutions = buf[0] > 4? buf[10]: 0;
5f5e1063 »
2012-04-22 Firmware 15d4 support
409
410 applog(LOG_DEBUG, "PID: %d numNonces: %d offsNonces: %d freqM1: %f freqMaxM: %d freqM: %d suspendSupported: %s hashesPerClock: %f extraSolutions: %d",
411 buf[0], newdev->numNonces, newdev->offsNonces, newdev->freqM1, newdev->freqMaxM, newdev->freqM, newdev->suspendSupported ? "T": "F",
412 newdev->hashesPerClock, newdev->extraSolutions);
413
414 if (buf[0] < 4) {
415 if (strncmp(newdev->bitFileName, "ztex_ufm1_15b", 13) != 0)
416 newdev->hashesPerClock = 0.5;
417 applog(LOG_WARNING, "HASHES_PER_CLOCK not defined, assuming %0.2f", newdev->hashesPerClock);
418 }
c280dd0d »
2012-04-17 Moving space indentation to tabs
419
bd46119c »
2012-04-21 Style police on libztex.c.
420 for (cnt=0; cnt < 255; cnt++) {
c280dd0d »
2012-04-17 Moving space indentation to tabs
421 newdev->errorCount[cnt] = 0;
422 newdev->errorWeight[cnt] = 0;
423 newdev->errorRate[cnt] = 0;
424 newdev->maxErrorRate[cnt] = 0;
425 }
426
427 newdev->usbbus = libusb_get_bus_number(dev);
428 newdev->usbaddress = libusb_get_device_address(dev);
429 sprintf(newdev->repr, "ZTEX %.3d:%.3d-%s", newdev->usbbus, newdev->usbaddress, newdev->snString);
430 newdev->valid = true;
431 return 0;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
432 }
433
bd46119c »
2012-04-21 Style police on libztex.c.
434 void libztex_destroy_device(struct libztex_device* ztex)
435 {
c280dd0d »
2012-04-17 Moving space indentation to tabs
436 if (ztex->hndl != NULL) {
437 libusb_close(ztex->hndl);
438 ztex->hndl = NULL;
439 }
440 if (ztex->bitFileName != NULL) {
441 free(ztex->bitFileName);
442 ztex->bitFileName = NULL;
443 }
444 free(ztex);
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
445 }
446
bd46119c »
2012-04-21 Style police on libztex.c.
447 int libztex_scanDevices(struct libztex_dev_list*** devs_p)
448 {
449 int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
450 struct libztex_dev_list **devs;
c280dd0d »
2012-04-17 Moving space indentation to tabs
451 struct libztex_device *ztex;
452 int found = 0, pos = 0, err;
bd46119c »
2012-04-21 Style police on libztex.c.
453 libusb_device **list;
454 ssize_t cnt, i = 0;
455
456 cnt = libusb_get_device_list(NULL, &list);
c280dd0d »
2012-04-17 Moving space indentation to tabs
457 if (unlikely(cnt < 0)) {
458 applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", cnt);
459 return 0;
460 }
461
462 for (i = 0; i < cnt; i++) {
463 if (libztex_checkDevice(list[i])) {
464 // Got one!
465 usbdevices[found] = i;
466 found++;
467 }
468 }
469
470 devs = malloc(sizeof(struct libztex_dev_list *) * found);
471 if (devs == NULL) {
472 applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
473 return 0;
474 }
475
476 for (i = 0; i < found; i++) {
477 err = libztex_prepare_device(list[usbdevices[i]], &ztex);
9ba2e217 »
2012-04-18 Some cleanup
478 if (unlikely(err != 0))
c280dd0d »
2012-04-17 Moving space indentation to tabs
479 applog(LOG_ERR, "prepare device: %d", err);
480 // check if valid
481 if (!ztex->valid) {
482 libztex_destroy_device(ztex);
483 continue;
484 }
485 devs[pos] = malloc(sizeof(struct libztex_dev_list));
486 devs[pos]->dev = ztex;
487 devs[pos]->next = NULL;
9ba2e217 »
2012-04-18 Some cleanup
488 if (pos > 0)
bd46119c »
2012-04-21 Style police on libztex.c.
489 devs[pos - 1]->next = devs[pos];
c280dd0d »
2012-04-17 Moving space indentation to tabs
490 pos++;
491 }
492
493 libusb_free_device_list(list, 1);
494 *devs_p = devs;
495 return pos;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
496 }
497
bd46119c »
2012-04-21 Style police on libztex.c.
498 int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
499 {
c280dd0d »
2012-04-17 Moving space indentation to tabs
500 int cnt;
bd46119c »
2012-04-21 Style police on libztex.c.
501
9ba2e217 »
2012-04-18 Some cleanup
502 if (ztex == NULL || ztex->hndl == NULL)
c280dd0d »
2012-04-17 Moving space indentation to tabs
503 return 0;
504 cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000);
9ba2e217 »
2012-04-18 Some cleanup
505 if (unlikely(cnt < 0))
c280dd0d »
2012-04-17 Moving space indentation to tabs
506 applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
507
508 return cnt;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
509 }
510
5f5e1063 »
2012-04-22 Firmware 15d4 support
511 int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[]) {
512 int bufsize = 12 + ztex->extraSolutions * 4;
513 unsigned char *rbuf;
514 int cnt, i, j;
c280dd0d »
2012-04-17 Moving space indentation to tabs
515
9ba2e217 »
2012-04-18 Some cleanup
516 if (ztex->hndl == NULL)
c280dd0d »
2012-04-17 Moving space indentation to tabs
517 return 0;
518
5f5e1063 »
2012-04-22 Firmware 15d4 support
519 rbuf = malloc(sizeof(unsigned char) * (ztex->numNonces * bufsize));
520 if (rbuf == NULL) {
521 applog(LOG_ERR, "%s: Failed to allocate memory for reading nonces", ztex->repr);
522 return 0;
523 }
524 cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, bufsize * ztex->numNonces, 1000);
c280dd0d »
2012-04-17 Moving space indentation to tabs
525 if (unlikely(cnt < 0)) {
526 applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
5f5e1063 »
2012-04-22 Firmware 15d4 support
527 free(rbuf);
c280dd0d »
2012-04-17 Moving space indentation to tabs
528 return cnt;
529 }
530
5f5e1063 »
2012-04-22 Firmware 15d4 support
531 for (i=0; i<ztex->numNonces; i++) {
532 memcpy((char*)&nonces[i].goldenNonce[0], &rbuf[i*bufsize], 4);
533 nonces[i].goldenNonce[0] -= ztex->offsNonces;
534 //applog(LOG_DEBUG, "W %d:0 %0.8x", i, nonces[i].goldenNonce[0]);
535
536 memcpy((char*)&nonces[i].nonce, &rbuf[(i*bufsize)+4], 4);
c280dd0d »
2012-04-17 Moving space indentation to tabs
537 nonces[i].nonce -= ztex->offsNonces;
5f5e1063 »
2012-04-22 Firmware 15d4 support
538 memcpy((char*)&nonces[i].hash7, &rbuf[(i*bufsize)+8], 4);
539
540 for (j=0; j<ztex->extraSolutions; j++) {
541 memcpy((char*)&nonces[i].goldenNonce[j+1], &rbuf[(i*bufsize)+12+(j*4)], 4);
542 nonces[i].goldenNonce[j+1] -= ztex->offsNonces;
543 //applog(LOG_DEBUG, "W %d:%d %0.8x", i, j+1, nonces[i].goldenNonce[j+1]);
544 }
c280dd0d »
2012-04-17 Moving space indentation to tabs
545 }
5f5e1063 »
2012-04-22 Firmware 15d4 support
546
547 free(rbuf);
c280dd0d »
2012-04-17 Moving space indentation to tabs
548 return cnt;
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
549 }
550
bd46119c »
2012-04-21 Style police on libztex.c.
551 void libztex_freeDevList(struct libztex_dev_list **devs)
552 {
c280dd0d »
2012-04-17 Moving space indentation to tabs
553 bool done = false;
bd46119c »
2012-04-21 Style police on libztex.c.
554 ssize_t cnt = 0;
555
c280dd0d »
2012-04-17 Moving space indentation to tabs
556 while (!done) {
9ba2e217 »
2012-04-18 Some cleanup
557 if (devs[cnt]->next == NULL)
c280dd0d »
2012-04-17 Moving space indentation to tabs
558 done = true;
559 free(devs[cnt++]);
560 }
561 free(devs);
99a188f9 »
2012-03-11 initial commit with some support for ztex 1.15x board.
562 }
Something went wrong with that request. Please try again.