forked from luke-jr/bfgminer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver-hashbuster.c
333 lines (284 loc) · 7.94 KB
/
driver-hashbuster.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright 2013-2014 Luke Dashjr
* Copyright 2013 Vladimir Strinski
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include "deviceapi.h"
#include "driver-bitfury.h"
#include "libbitfury.h"
#include "logging.h"
#include "lowlevel.h"
#include "lowl-hid.h"
#include "miner.h"
#define HASHBUSTER_USB_PRODUCT "HashBuster"
#define HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER 62
BFG_REGISTER_DRIVER(hashbuster_drv)
static
bool hashbuster_io(hid_device * const h, void * const buf, const void * const cmd)
{
const uint8_t cmdbyte = *((uint8_t *)cmd);
char x[0x81];
if (unlikely(opt_dev_protocol))
{
bin2hex(x, cmd, 0x40);
applog(LOG_DEBUG, "%s(%p): SEND: %s", __func__, h, x);
}
const bool rv = likely(
0x40 == hid_write(h, cmd, 0x40) &&
0x40 == hid_read (h, buf, 0x40) &&
((uint8_t *)buf)[0] == cmdbyte
);
if (unlikely(opt_dev_protocol))
{
bin2hex(x, buf, 0x40);
applog(LOG_DEBUG, "%s(%p): RECV: %s", __func__, h, x);
}
return rv;
}
static
bool hashbuster_spi_config(hid_device * const h, const uint8_t mode, const uint8_t miso, const uint32_t freq)
{
uint8_t buf[0x40] = {'\x01', '\x01', mode, miso};
switch (freq)
{
case 100000:
buf[4] = '\0';
break;
case 750000:
buf[4] = '\x01';
break;
case 3000000:
buf[4] = '\x02';
break;
case 12000000:
buf[4] = '\x03';
break;
default:
return false;
}
if (!hashbuster_io(h, buf, buf))
return false;
return (buf[1] == '\x0f');
}
static
bool hashbuster_spi_disable(hid_device * const h)
{
uint8_t buf[0x40] = {'\x01'};
if (!hashbuster_io(h, buf, buf))
return false;
return (buf[1] == '\x0f');
}
static
bool hashbuster_spi_reset(hid_device * const h, uint8_t chips)
{
uint8_t buf[0x40] = {'\x02', chips};
if (!hashbuster_io(h, buf, buf))
return false;
return (buf[1] == 0xff);
}
static
bool hashbuster_spi_transfer(hid_device * const h, void * const buf, const void * const data, size_t datasz)
{
if (datasz > HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
return false;
uint8_t cbuf[0x40] = {'\x03', datasz};
memcpy(&cbuf[2], data, datasz);
if (!hashbuster_io(h, cbuf, cbuf))
return false;
if (cbuf[1] != datasz)
return false;
memcpy(buf, &cbuf[2], datasz);
return true;
}
static
bool hashbuster_spi_txrx(struct spi_port * const port)
{
hid_device * const h = port->userp;
const uint8_t *wrbuf = spi_gettxbuf(port);
uint8_t *rdbuf = spi_getrxbuf(port);
size_t bufsz = spi_getbufsz(port);
hashbuster_spi_disable(h);
hashbuster_spi_reset(h, 0x10);
hashbuster_spi_config(h, port->mode, 0, port->speed);
while (bufsz >= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER)
{
if (!hashbuster_spi_transfer(h, rdbuf, wrbuf, HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER))
return false;
rdbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
wrbuf += HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
bufsz -= HASHBUSTER_MAX_BYTES_PER_SPI_TRANSFER;
}
if (bufsz > 0)
{
if (!hashbuster_spi_transfer(h, rdbuf, wrbuf, bufsz))
return false;
}
return true;
}
static
bool hashbuster_lowl_match(const struct lowlevel_device_info * const info)
{
return lowlevel_match_lowlproduct(info, &lowl_hid, HASHBUSTER_USB_PRODUCT);
}
static
int hashbuster_chip_count(hid_device *h)
{
/* Do not allocate spi_port on the stack! OS X, at least, has a 512 KB default stack size for secondary threads */
struct spi_port *spi = malloc(sizeof(*spi));
spi->txrx = hashbuster_spi_txrx;
spi->userp = h;
spi->repr = hashbuster_drv.dname;
spi->logprio = LOG_DEBUG;
spi->speed = 100000;
spi->mode = 0;
const int chip_count = libbitfury_detectChips1(spi);
free(spi);
return chip_count;
}
static
bool hashbuster_lowl_probe(const struct lowlevel_device_info * const info)
{
const char * const product = info->product;
const char * const serial = info->serial;
char * const path = info->path;
hid_device *h;
uint8_t buf[0x40] = {'\xfe'};
if (info->lowl != &lowl_hid)
{
bfg_probe_result_flags = BPR_WRONG_DEVTYPE;
applogr(false, LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not hid!",
__func__, product, serial);
}
if (info->vid != 0xFA04 || info->pid != 0x0011)
{
bfg_probe_result_flags = BPR_WRONG_DEVTYPE;
applogr(false, LOG_DEBUG, "%s: Wrong VID/PID", __func__);
}
h = hid_open_path(path);
if (!h)
applogr(false, LOG_WARNING, "%s: Failed to open HID path %s",
__func__, path);
if ((!hashbuster_io(h, buf, buf)) || buf[1] != 0x07)
{
hid_close(h);
applogr(false, LOG_DEBUG, "%s: Identify sequence didn't match on %s",
__func__, path);
}
const int chip_n = hashbuster_chip_count(h);
hid_close(h);
if (lowlevel_claim(&hashbuster_drv, true, info))
return false;
struct cgpu_info *cgpu;
cgpu = malloc(sizeof(*cgpu));
*cgpu = (struct cgpu_info){
.drv = &hashbuster_drv,
.set_device_funcs = bitfury_set_device_funcs,
.device_data = lowlevel_ref(info),
.threads = 1,
.procs = chip_n,
.device_path = strdup(info->path),
.dev_manufacturer = maybe_strdup(info->manufacturer),
.dev_product = maybe_strdup(product),
.dev_serial = maybe_strdup(serial),
.deven = DEV_ENABLED,
};
return add_cgpu(cgpu);
}
static
bool hashbuster_init(struct thr_info * const thr)
{
struct cgpu_info * const cgpu = thr->cgpu, *proc;
struct bitfury_device *bitfury;
struct spi_port *port;
hid_device *h;
h = hid_open_path(cgpu->device_path);
lowlevel_devinfo_free(cgpu->device_data);
if (!h)
{
hid_close(h);
applogr(false, LOG_ERR, "%s: Failed to open hid device", cgpu->dev_repr);
}
port = malloc(sizeof(*port));
if (!port)
applogr(false, LOG_ERR, "%s: Failed to allocate spi_port", cgpu->dev_repr);
/* Be careful, read lowl-spi.h comments for warnings */
memset(port, 0, sizeof(*port));
port->txrx = hashbuster_spi_txrx;
port->userp = h;
port->cgpu = cgpu;
port->repr = cgpu->dev_repr;
port->logprio = LOG_ERR;
port->speed = 100000;
port->mode = 0;
for (proc = cgpu; proc; proc = proc->next_proc)
{
bitfury = malloc(sizeof(*bitfury));
if (!bitfury)
{
applog(LOG_ERR, "%"PRIpreprv": Failed to allocate bitfury_device",
cgpu->proc_repr);
proc->status = LIFE_DEAD2;
continue;
}
*bitfury = (struct bitfury_device){
.spi = port,
};
proc->device_data = bitfury;
bitfury_init_chip(proc);
bitfury->osc6_bits = 53;
bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
bitfury_init_freq_stat(&bitfury->chip_stat, 52, 56);
}
timer_set_now(&thr->tv_poll);
cgpu->status = LIFE_INIT2;
return true;
}
static
bool hashbuster_get_stats(struct cgpu_info * const cgpu)
{
struct cgpu_info *proc;
if (cgpu != cgpu->device)
return true;
struct bitfury_device * const bitfury = cgpu->device_data;
struct spi_port * const spi = bitfury->spi;
hid_device * const h = spi->userp;
uint8_t buf[0x40] = {'\x04'};
if (!hashbuster_io(h, buf, buf))
return false;
if (buf[1])
{
for (proc = cgpu; proc; proc = proc->next_proc)
proc->temp = buf[1];
}
return true;
}
struct device_drv hashbuster_drv = {
.dname = "hashbuster",
.name = "HBR",
.lowl_match = hashbuster_lowl_match,
.lowl_probe = hashbuster_lowl_probe,
.thread_init = hashbuster_init,
.thread_disable = bitfury_disable,
.thread_enable = bitfury_enable,
.thread_shutdown = bitfury_shutdown,
.minerloop = minerloop_async,
.job_prepare = bitfury_job_prepare,
.job_start = bitfury_noop_job_start,
.poll = bitfury_do_io,
.job_process_results = bitfury_job_process_results,
.get_stats = hashbuster_get_stats,
.get_api_extra_device_detail = bitfury_api_device_detail,
.get_api_extra_device_status = bitfury_api_device_status,
#ifdef HAVE_CURSES
.proc_wlogprint_status = bitfury_wlogprint_status,
.proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
.proc_tui_handle_choice = bitfury_tui_handle_choice,
#endif
};