Commit 7fbc3770b471b956d40cb78e8b7e365b54de9cf1

Kano 2013-01-22T21:46:21

USB move usbdev info that needs to stay around into usbinfo

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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
diff --git a/api.c b/api.c
index fe593a7..a2470b0 100644
--- a/api.c
+++ b/api.c
@@ -1575,7 +1575,7 @@ static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom
 		root = api_add_diff(root, "Difficulty Rejected", &(cgpu->diff_rejected), false);
 		root = api_add_diff(root, "Last Share Difficulty", &(cgpu->last_share_diff), false);
 #if defined(USE_MODMINER) || defined(USE_BITFORCE)
-		root = api_add_bool(root, "No Device", &(cgpu->nodev), false);
+		root = api_add_bool(root, "No Device", &(cgpu->usbinfo.nodev), false);
 #endif
 
 		root = print_data(root, buf, isjson, precom);
@@ -1793,7 +1793,7 @@ static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
 #endif
 
 #if defined(USE_MODMINER) || defined(USE_BITFORCE)
-	if (cgpu->nodev) {
+	if (cgpu->usbinfo.nodev) {
 		message(io_data, MSG_USBNODEV, id, NULL, isjson);
 		return;
 	}
diff --git a/driver-bitforce.c b/driver-bitforce.c
index f97bcda..be22c7b 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -92,7 +92,7 @@ static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
 		applog(LOG_DEBUG, "%s%i: reset got err %d",
 			bitforce->drv->name, bitforce->device_id, err);
 
-	if (bitforce->nodev)
+	if (bitforce->usbinfo.nodev)
 		goto failed;
 
 	// Set data control
@@ -102,7 +102,7 @@ static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
 		applog(LOG_DEBUG, "%s%i: setdata got err %d",
 			bitforce->drv->name, bitforce->device_id, err);
 
-	if (bitforce->nodev)
+	if (bitforce->usbinfo.nodev)
 		goto failed;
 
 	// Set the baud
@@ -113,7 +113,7 @@ static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
 		applog(LOG_DEBUG, "%s%i: setbaud got err %d",
 			bitforce->drv->name, bitforce->device_id, err);
 
-	if (bitforce->nodev)
+	if (bitforce->usbinfo.nodev)
 		goto failed;
 
 	// Set Flow Control
@@ -123,7 +123,7 @@ static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
 		applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
 			bitforce->drv->name, bitforce->device_id, err);
 
-	if (bitforce->nodev)
+	if (bitforce->usbinfo.nodev)
 		goto failed;
 
 	// Set Modem Control
@@ -133,7 +133,7 @@ static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
 		applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
 			bitforce->drv->name, bitforce->device_id, err);
 
-	if (bitforce->nodev)
+	if (bitforce->usbinfo.nodev)
 		goto failed;
 
 	// Clear any sent data
@@ -143,7 +143,7 @@ static void bitforce_initialise(struct cgpu_info *bitforce, bool lock)
 		applog(LOG_DEBUG, "%s%i: purgetx got err %d",
 			bitforce->drv->name, bitforce->device_id, err);
 
-	if (bitforce->nodev)
+	if (bitforce->usbinfo.nodev)
 		goto failed;
 
 	// Clear any received data
@@ -178,14 +178,14 @@ static bool bitforce_detect_one(struct libusb_device *dev, struct usb_find_devic
 	if (!usb_init(bitforce, dev, found)) {
 		applog(LOG_ERR, "%s detect (%d:%d) failed to initialise (incorrect device?)",
 			bitforce->drv->dname,
-			(int)libusb_get_bus_number(dev),
-			(int)libusb_get_device_address(dev));
+			(int)(bitforce->usbinfo.bus_number),
+			(int)(bitforce->usbinfo.device_address));
 		goto shin;
 	}
 
 	sprintf(devpath, "%d:%d",
-			(int)(bitforce->usbdev->bus_number),
-			(int)(bitforce->usbdev->device_address));
+			(int)(bitforce->usbinfo.bus_number),
+			(int)(bitforce->usbinfo.device_address));
 
 
 	// Allow 2 complete attempts if the 1st time returns an unrecognised reply
@@ -361,7 +361,7 @@ static bool bitforce_get_temp(struct cgpu_info *bitforce)
 	char *s;
 
 	// Device is gone
-	if (bitforce->nodev)
+	if (bitforce->usbinfo.nodev)
 		return false;
 
 	/* Do not try to get the temperature if we're polling for a result to
@@ -683,7 +683,7 @@ static int64_t bitforce_scanhash(struct thr_info *thr, struct work *work, int64_
 	int64_t ret;
 
 	// Device is gone
-	if (bitforce->nodev)
+	if (bitforce->usbinfo.nodev)
 		return -1;
 
 	send_ret = bitforce_send_work(thr, work);
diff --git a/driver-modminer.c b/driver-modminer.c
index 24a952a..eb54d91 100644
--- a/driver-modminer.c
+++ b/driver-modminer.c
@@ -129,14 +129,14 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
 	if (!usb_init(modminer, dev, found)) {
 		applog(LOG_ERR, "%s detect (%d:%d) failed to initialise (incorrect device?)",
 			modminer->drv->dname,
-			(int)libusb_get_bus_number(dev),
-			(int)libusb_get_device_address(dev));
+			(int)(modminer->usbinfo.bus_number),
+			(int)(modminer->usbinfo.device_address));
 		goto shin;
 	}
 
 	sprintf(devpath, "%d:%d",
-			(int)(modminer->usbdev->bus_number),
-			(int)(modminer->usbdev->device_address));
+			(int)(modminer->usbinfo.bus_number),
+			(int)(modminer->usbinfo.device_address));
 
 	do_ping(modminer);
 
@@ -204,15 +204,15 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
 		tmp->name = devname;
 
 		sprintf(devpath, "%d:%d:%d",
-			(int)(modminer->usbdev->bus_number),
-			(int)(modminer->usbdev->device_address),
+			(int)(modminer->usbinfo.bus_number),
+			(int)(modminer->usbinfo.device_address),
 			i);
 
 		tmp->device_path = strdup(devpath);
 		tmp->usbdev = modminer->usbdev;
 		// Only the first copy gets the already used stats
 		if (!added)
-			tmp->usbstat = modminer->usbstat;
+			tmp->usbinfo.usbstat = modminer->usbinfo.usbstat;
 		tmp->fpgaid = (char)i;
 		tmp->modminer_mutex = modminer->modminer_mutex;
 		tmp->deven = DEV_ENABLED;
@@ -640,7 +640,7 @@ static const char *modminer_delta_clock(struct thr_info *thr, int delta, bool te
 	int err, amount;
 
 	// Device is gone
-	if (modminer->nodev)
+	if (modminer->usbinfo.nodev)
 		return clocknodev;
 
 	// Only do once if multiple shares per work or multiple reasons
@@ -828,7 +828,7 @@ static void check_temperature(struct thr_info *thr)
 	int amount;
 
 	// Device is gone
-	if (modminer->nodev)
+	if (modminer->usbinfo.nodev)
 		return;
 
 	if (state->one_byte_temp) {
@@ -916,7 +916,7 @@ static uint64_t modminer_process_results(struct thr_info *thr)
 	int temploop;
 
 	// Device is gone
-	if (modminer->nodev)
+	if (modminer->usbinfo.nodev)
 		return -1;
 
 	// If we are overheated it will just keep checking for results
@@ -1079,7 +1079,7 @@ static int64_t modminer_scanhash(struct thr_info *thr, struct work *work, int64_
 	struct timeval tv1, tv2;
 
 	// Device is gone
-	if (thr->cgpu->nodev)
+	if (thr->cgpu->usbinfo.nodev)
 		return -1;
 
 	// Don't start new work if overheated
@@ -1092,7 +1092,7 @@ static int64_t modminer_scanhash(struct thr_info *thr, struct work *work, int64_
 			check_temperature(thr);
 
 			// Device is gone
-			if (thr->cgpu->nodev)
+			if (thr->cgpu->usbinfo.nodev)
 				return -1;
 
 			if (state->overheated == true) {
diff --git a/miner.h b/miner.h
index f9e616d..2a74bdc 100644
--- a/miner.h
+++ b/miner.h
@@ -400,8 +400,7 @@ struct cgpu_info {
 #endif
 	};
 #if defined(USE_MODMINER) || defined(USE_BITFORCE)
-	int usbstat;
-	bool nodev;
+	struct cg_usb_info usbinfo;
 #endif
 #ifdef USE_MODMINER
 	char fpgaid;
diff --git a/usbutils.c b/usbutils.c
index 30be288..9c55ca4 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -641,7 +641,7 @@ static void add_used(libusb_device *dev, bool lock)
 			mutex_unlock(list_lock);
 
 		sprintf(buf, "add_used() duplicate bus_number %d device_address %d",
-				bus_number, device_address);
+				(int)bus_number, (int)device_address);
 		quit(1, buf);
 	}
 
@@ -692,7 +692,7 @@ static void release(uint8_t bus_number, uint8_t device_address, bool lock)
 			mutex_unlock(list_lock);
 
 		sprintf(buf, "release() unknown: bus_number %d device_address %d",
-				bus_number, device_address);
+				(int)bus_number, (int)device_address);
 		quit(1, buf);
 	}
 
@@ -752,26 +752,26 @@ void usb_uninit(struct cgpu_info *cgpu)
 void release_cgpu(struct cgpu_info *cgpu)
 {
 	struct cg_usb_device *cgusb = cgpu->usbdev;
-	uint8_t bus_number;
-	uint8_t device_address;
 	int i;
 
-	cgpu->nodev = true;
+	cgpu->usbinfo.nodev = true;
+	cgpu->usbinfo.nodev_count++;
+	gettimeofday(&(cgpu->usbinfo.last_nodev), NULL);
 
 	// Any devices sharing the same USB device should be marked also
 	// Currently only MMQ shares a USB device
 	for (i = 0; i < total_devices; i++)
 		if (devices[i] != cgpu && devices[i]->usbdev == cgusb) {
-			devices[i]->nodev = true;
+			devices[i]->usbinfo.nodev = true;
+			devices[i]->usbinfo.nodev_count++;
+			memcpy(&(devices[i]->usbinfo.last_nodev),
+				&(cgpu->usbinfo.last_nodev), sizeof(struct timeval));
 			devices[i]->usbdev = NULL;
 		}
 
-	bus_number = cgusb->bus_number;
-	device_address = cgusb->device_address;
-
 	usb_uninit(cgpu);
 
-	release(bus_number, device_address, true);
+	release(cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address, true);
 }
 
 bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found)
@@ -783,12 +783,12 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find
 	unsigned char strbuf[STRBUFLEN+1];
 	int err, i, j, k;
 
+	cgpu->usbinfo.bus_number = libusb_get_bus_number(dev);
+	cgpu->usbinfo.device_address = libusb_get_device_address(dev);
+
 	cgusb = calloc(1, sizeof(*cgusb));
 	cgusb->found = found;
 
-	cgusb->bus_number = libusb_get_bus_number(dev);
-	cgusb->device_address = libusb_get_device_address(dev);
-
 	cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
 
 	err = libusb_get_device_descriptor(dev, cgusb->descriptor);
@@ -828,7 +828,8 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find
 		switch(err) {
 			case LIBUSB_ERROR_BUSY:
 				applog(LOG_WARNING, "USB init, %s device %d:%d in use",
-						found->name, cgusb->bus_number, cgusb->device_address);
+						found->name, (int)(cgpu->usbinfo.bus_number),
+						(int)(cgpu->usbinfo.device_address));
 				break;
 			default:
 				applog(LOG_DEBUG, "USB init, failed to set config to %d, err %d",
@@ -908,7 +909,7 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find
 //	cgusb->fwVersion <- for temp1/temp2 decision? or serial? (driver-modminer.c)
 //	cgusb->interfaceVersion
 
-	applog(LOG_DEBUG, "USB init device bus_number=%d device_address=%d usbver=%04x prod='%s' manuf='%s' serial='%s'", (int)(cgusb->bus_number), (int)(cgusb->device_address), cgusb->usbver, cgusb->prod_string, cgusb->manuf_string, cgusb->serial_string);
+	applog(LOG_DEBUG, "USB init device bus_number=%d device_address=%d usbver=%04x prod='%s' manuf='%s' serial='%s'", (int)(cgpu->usbinfo.bus_number), (int)(cgpu->usbinfo.device_address), cgusb->usbver, cgusb->prod_string, cgusb->manuf_string, cgusb->serial_string);
 
 	cgpu->usbdev = cgusb;
 
@@ -1134,7 +1135,7 @@ static void newstats(struct cgpu_info *cgpu)
 {
 	int i;
 
-	cgpu->usbstat = ++next_stat;
+	cgpu->usbinfo.usbstat = ++next_stat;
 
 	usb_stats = realloc(usb_stats, sizeof(*usb_stats) * next_stat);
 	usb_stats[next_stat-1].name = cgpu->drv->name;
@@ -1148,11 +1149,11 @@ static void newstats(struct cgpu_info *cgpu)
 void update_usb_stats(__maybe_unused struct cgpu_info *cgpu)
 {
 #if DO_USB_STATS
-	if (cgpu->usbstat < 1)
+	if (cgpu->usbinfo.usbstat < 1)
 		newstats(cgpu);
 
 	// we don't know the device_id until after add_cgpu()
-	usb_stats[cgpu->usbstat - 1].device_id = cgpu->device_id;
+	usb_stats[cgpu->usbinfo.usbstat - 1].device_id = cgpu->device_id;
 #endif
 }
 
@@ -1163,10 +1164,10 @@ static void stats(struct cgpu_info *cgpu, struct timeval *tv_start, struct timev
 	double diff;
 	int item;
 
-	if (cgpu->usbstat < 1)
+	if (cgpu->usbinfo.usbstat < 1)
 		newstats(cgpu);
 
-	details = &(usb_stats[cgpu->usbstat - 1].details[cmd * 2 + seq]);
+	details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[cmd * 2 + seq]);
 
 	diff = tdiff(tv_finish, tv_start);
 
@@ -1201,10 +1202,10 @@ static void rejected_inc(struct cgpu_info *cgpu)
 	struct cg_usb_stats_details *details;
 	int item = CMD_ERROR;
 
-	if (cgpu->usbstat < 1)
+	if (cgpu->usbinfo.usbstat < 1)
 		newstats(cgpu);
 
-	details = &(usb_stats[cgpu->usbstat - 1].details[C_REJECTED * 2 + 0]);
+	details = &(usb_stats[cgpu->usbinfo.usbstat - 1].details[C_REJECTED * 2 + 0]);
 
 	details->item[item].count++;
 }
@@ -1222,7 +1223,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
 	int err, got, tot, i;
 	bool first = true;
 
-	if (cgpu->nodev) {
+	if (cgpu->usbinfo.nodev) {
 		*buf = '\0';
 		*processed = 0;
 #if DO_USB_STATS
@@ -1330,7 +1331,7 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
 #endif
 	int err, sent;
 
-	if (cgpu->nodev) {
+	if (cgpu->usbinfo.nodev) {
 		*processed = 0;
 #if DO_USB_STATS
 		rejected_inc(cgpu);
@@ -1364,7 +1365,7 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest
 #endif
 	int err;
 
-	if (cgpu->nodev) {
+	if (cgpu->usbinfo.nodev) {
 #if DO_USB_STATS
 		rejected_inc(cgpu);
 #endif
diff --git a/usbutils.h b/usbutils.h
index 7912be5..d63122f 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -67,8 +67,6 @@ struct cg_usb_device {
 	libusb_device_handle *handle;
 	pthread_mutex_t *mutex;
 	struct libusb_device_descriptor *descriptor;
-	uint8_t bus_number;
-	uint8_t device_address;
 	uint16_t usbver;
 	int speed;
 	char *prod_string;
@@ -78,6 +76,15 @@ struct cg_usb_device {
 	unsigned char interfaceVersion;	// ??
 };
 
+struct cg_usb_info {
+	uint8_t bus_number;
+	uint8_t device_address;
+	int usbstat;
+	bool nodev;
+	int nodev_count;
+	struct timeval last_nodev;
+};
+
 enum usb_cmds {
 	C_REJECTED = 0,
 	C_PING,