1 /*
2  * This software is Copyright (c) 2016-2017 Denis Burykin
3  * [denis_burykin yahoo com], [denis-burykin2014 yandex ru]
4  * and it is hereby released to the general public under the following terms:
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted.
7  *
8  */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/time.h>
13 #include <errno.h>
14 #include <libusb-1.0/libusb.h>
15 
16 #include "ztex.h"
17 #include "inouttraffic.h"
18 #include "pkt_comm/pkt_comm.h"
19 
20 #include "../path.h"
21 
22 int DEBUG = 0;
23 
fpga_get_io_state(struct libusb_device_handle * handle,struct fpga_io_state * io_state)24 int fpga_get_io_state(struct libusb_device_handle *handle, struct fpga_io_state *io_state)
25 {
26 	int result = vendor_request(handle, 0x84, 0, 0, (unsigned char *)io_state, sizeof(io_state));
27 	if (DEBUG) printf("get_io_state: %x %x %x pkt: 0x%x debug: 0x%x 0x%x\n",
28 		io_state->io_state, io_state->timeout, io_state->app_status,
29 		io_state->pkt_comm_status, io_state->debug2, io_state->debug3);
30 	return result;
31 }
32 
33 // with output limit enabled, FPGA would not send data
34 // until fpga_setup_output()
35 // enabled by default
fpga_output_limit_enable(struct libusb_device_handle * handle,int enable)36 int fpga_output_limit_enable(struct libusb_device_handle *handle, int enable)
37 {
38 	int result = vendor_command(handle, 0x86, enable, 0, NULL, 0);
39 	return result;
40 }
41 
42 // 1. returns number of bytes in FPGA's output FIFO
43 // 2. FPGA starts output of that many
fpga_setup_output(struct libusb_device_handle * handle)44 int fpga_setup_output(struct libusb_device_handle *handle)
45 {
46 	unsigned char output_limit[2] = {0,0};
47 	// vendor_request returns in output FIFO words (OUTPUT_WORD_WIDTH bytes)
48 	int result = vendor_request(handle, 0x85, 0, 0, output_limit, 2);
49 	if (DEBUG) printf("output limit: %d\n", OUTPUT_WORD_WIDTH * ((output_limit[1] << 8) + output_limit[0]) );
50 	if (result < 0)
51 		return result;
52 	else
53 		return OUTPUT_WORD_WIDTH* (unsigned int)( (output_limit[1] << 8) + output_limit[0]);
54 }
55 
56 // Inouttraffic bitstreams have High-Speed interface disabled by default.
57 // fpga_reset() enables High-Speed interface.
58 // consider device_fpga_reset() to reset all FPGA's on device
fpga_reset(struct libusb_device_handle * handle)59 int fpga_reset(struct libusb_device_handle *handle)
60 {
61 	int result = vendor_command(handle, 0x8B, 0, 0, NULL, 0);
62 	return result;
63 }
64 
65 // enable/disable high-speed output.
66 // FPGA comes with High-Speed I/O (hs_io) disabled.
67 // Application usually would not need this:
68 // hs_io status changes internally by e.g. fpga_select() and fpga_reset().
fpga_hs_io_enable(struct libusb_device_handle * handle,int enable)69 int fpga_hs_io_enable(struct libusb_device_handle *handle, int enable)
70 {
71 	int result = vendor_command(handle, 0x80, enable, 0, NULL, 0);
72 	return result;
73 }
74 
75 
76 // =======================================================================
77 //
78 // Following functions all use 'struct device' and 'struct fpga'
79 //
80 // =======================================================================
81 
82 // Creates 'struct device' on top of 'struct ztex_device'
device_new(struct ztex_device * ztex_device)83 struct device *device_new(struct ztex_device *ztex_device)
84 {
85 	struct device *device = malloc(sizeof(struct device));
86 	if (!device)
87 		return NULL;
88 	device->ztex_device = ztex_device;
89 	device->handle = ztex_device->handle;
90 	device->num_of_fpgas = ztex_device->num_of_fpgas;
91 	device->selected_fpga = ztex_device->selected_fpga;
92 	device->num_of_valid_fpgas = 0;
93 
94 	int i;
95 	for (i = 0; i < device->num_of_fpgas; i++) {
96 		device->fpga[i].device = device;
97 		device->fpga[i].num = i;
98 		device->fpga[i].valid = 0;
99 		device->fpga[i].wr.io_state_valid = 0;
100 		device->fpga[i].wr.io_state_timeout_count = 0;
101 		device->fpga[i].wr.wr_count = 0;
102 		device->fpga[i].rd.read_limit_valid = 0;
103 		device->fpga[i].rd.read_count = 0;
104 		device->fpga[i].rd.partial_read_count = 0;
105 		device->fpga[i].cmd_count = 0;
106 		// packet-based communication
107 		device->fpga[i].comm = NULL;
108 
109 		int j;
110 		for (j = 0; j < NUM_PROGCLK_MAX; j ++)
111 			device->fpga[i].freq[j] = 0;
112 	}
113 
114 	int result;
115 	//if (usb_set_configuration(handle, 1) < 0) {
116 	//}
117 	result = libusb_claim_interface(device->handle, 0);
118 	if (result < 0) {
119 		//if (DEBUG)
120 		printf("SN %s: usb_claim_interface error %d (%s)\n",
121 			device->ztex_device->snString, result, libusb_error_name(result));
122 		device_delete(device);
123 		return NULL;
124 	}
125 	/*
126 	if (result < 0) {
127 		device_delete(device);
128 		return NULL;
129 	}*/
130 	device->valid = 1;
131 	return device;
132 }
133 
device_delete(struct device * device)134 void device_delete(struct device *device)
135 {
136 	device_invalidate(device);
137 	free(device);
138 }
139 
140 // device usually invalidated if there's some error
141 // underlying ztex_device also invalidated
device_invalidate(struct device * device)142 void device_invalidate(struct device *device)
143 {
144 	if (!device || !device->valid)
145 		return;
146 	device->valid = 0;
147 
148 	int i;
149 	for (i = 0; i < device->num_of_fpgas; i++) {
150 		if (device->fpga[i].comm)
151 			pkt_comm_delete(device->fpga[i].comm);
152 	}
153 
154 	libusb_release_interface(device->handle, 0);
155 	ztex_device_invalidate(device->ztex_device);
156 }
157 
device_valid(struct device * device)158 int device_valid(struct device *device)
159 {
160 	return device && device->valid;
161 }
162 
163 // Performs fpga_reset() on all FPGA's
164 // It resets FPGAs to its post-configuration state with Global Set Reset (GSR).
165 //
166 // Return values:
167 // < 0 error; would require heavier reset
168 //
169 // Issue. What if only 1 of onboard FPGAs has error.
170 // Currently following approarch used:
171 // Initialization functions (soft_reset, check_bitstream) initialize all onboard FPGAs.
172 // On any error, entire device is put into invalid state.
device_fpga_reset(struct device * device)173 int device_fpga_reset(struct device *device)
174 {
175 	if (DEBUG) printf("SN %s: device_fpga_reset()\n", device->ztex_device->snString);
176 
177 	int result;
178 	int i;
179 	for (i = 0; i < device->num_of_fpgas; i++) {
180 		struct fpga *fpga = &device->fpga[i];
181 
182 		result = fpga_select(fpga);
183 		if (result < 0)
184 			return result;
185 
186 		result = fpga_reset(device->handle);
187 		if (result < 0) {
188 			printf("SN %s #%d: device_fpga_reset: %d (%s)\n", device->ztex_device->snString,
189 					i + 1, result, libusb_error_name(result));
190 			return result;
191 		}
192 		fpga->wr.wr_count = 0;
193 	}
194 	return 0;
195 }
196 
device_list_new(struct ztex_dev_list * ztex_dev_list)197 struct device_list *device_list_new(struct ztex_dev_list *ztex_dev_list)
198 {
199 	struct device_list *device_list = malloc(sizeof(struct device_list));
200 	if (!device_list)
201 		return NULL;
202 	device_list->device = NULL;
203 	device_list->ztex_dev_list = ztex_dev_list;
204 	if (!ztex_dev_list)
205 		return device_list;
206 
207 	struct ztex_device *ztex_dev;
208 	struct device *device;
209 	for (ztex_dev = ztex_dev_list->dev; ztex_dev; ztex_dev = ztex_dev->next) {
210 		if (!ztex_device_valid(ztex_dev))
211 			continue;
212 		device = device_new(ztex_dev);
213 		if (!device)
214 			continue;
215 		device_list_add(device_list, device);
216 	}
217 	return device_list;
218 }
219 
device_list_add(struct device_list * device_list,struct device * device)220 void device_list_add(struct device_list *device_list, struct device *device)
221 {
222 	device->next = device_list->device;
223 	device_list->device = device;
224 }
225 
device_list_merge(struct device_list * device_list,struct device_list * added_list)226 int device_list_merge(struct device_list *device_list, struct device_list *added_list)
227 {
228 	if (!device_list || !added_list) {
229 		printf("device_list_merge: invalid arguments\n");
230 		return 0;
231 	}
232 	int count = 0;
233 	struct device *dev, *dev_next;
234 	for (dev = added_list->device; dev; dev = dev_next) {
235 		dev_next = dev->next;
236 		if (DEBUG) printf("device_list_merge: SN %s, valid %d, next: %d\n",
237 				dev->ztex_device->snString, dev->valid, !!dev_next);
238 		if (!device_valid(dev)) {
239 			device_delete(dev);
240 			continue;
241 		}
242 		device_list_add(device_list, dev);
243 		count++;
244 	}
245 
246 	int ztex_dev_count = ztex_dev_list_merge(device_list->ztex_dev_list,
247 			added_list->ztex_dev_list);
248 	if (ztex_dev_count != count) {
249 		printf("device_list_merge: ztex count %d, device count %d\n",
250 				ztex_dev_count, count);
251 	}
252 
253 	//added_list->dev = NULL;
254 	free(added_list);
255 	return count;
256 }
257 
device_list_count(struct device_list * device_list)258 int device_list_count(struct device_list *device_list)
259 {
260 	int count = 0;
261 	struct device *device;
262 	if (!device_list)
263 		return 0;
264 	for (device = device_list->device; device; device = device->next)
265 		if (device_valid(device))
266 			count++;
267 	return count;
268 }
269 
fpga_test_get_id(struct fpga * fpga)270 int fpga_test_get_id(struct fpga *fpga)
271 {
272 	const int MAGIC_W = 0x5A5A;
273 	struct fpga_echo_request echo;
274 	echo.out[0] = random();
275 	echo.out[1] = random();
276 	int result = vendor_request(fpga->device->handle, 0x88, echo.out[0], echo.out[1],
277 		(unsigned char *)&echo.reply, sizeof(echo.reply));
278 	int test_ok =
279 		(echo.reply.data[0] ^ MAGIC_W) == echo.out[0]
280 		&& (echo.reply.data[1] ^ MAGIC_W) == echo.out[1];
281 	if (test_ok)
282 		fpga->bitstream_type = echo.reply.bitstream_type;
283 	else
284 		fpga->bitstream_type = 0;
285 
286 	if (DEBUG) {
287 		printf("fpga_test_get_id(%d): request 0x%04X 0x%04X, reply 0x%04X 0x%04X",
288 			fpga->num + 1, echo.out[0], echo.out[1], echo.reply.data[0], echo.reply.data[1]);
289 		if (!test_ok) printf(" (must be 0x%04X 0x%04X)", echo.out[0] ^ MAGIC_W, echo.out[1] ^ MAGIC_W);
290 		else printf("(ok)");
291 		printf(", fpga_id %d, bitstream_type 0x%04X\n",
292 			echo.reply.fpga_id, echo.reply.bitstream_type);
293 	}
294 	if (result < 0)
295 		return result;
296 	else
297 		return test_ok && fpga->num == echo.reply.fpga_id;
298 }
299 
300 
301 // Return value:
302 // > 0 all FPGA's has bitstream of specified type
303 // 0 at least 1 FPGA has no bitstream or bitstream of other type
304 // < 0 error
device_check_bitstream_type(struct device * device,unsigned short bitstream_type)305 int device_check_bitstream_type(struct device *device, unsigned short bitstream_type)
306 {
307 	if (!device)
308 		return -1;
309 
310 	int i;
311 	for (i = 0; i < device->num_of_fpgas; i++) {
312 		int result;
313 		result = ztex_select_fpga(device->ztex_device, i);//fpga_select(&device->fpga[i]);
314 		if (result < 0)
315 			return result;
316 		result = fpga_test_get_id(&device->fpga[i]);
317 		//if (DEBUG)
318 		//printf("device_check_bitstream_type: id=%d, result=%d\n",i,result);
319 		if (result < 0)
320 			return result;
321 		if (result > 0) {
322 			if (device->fpga[i].bitstream_type != bitstream_type)
323 				return 0;
324 			//device->fpga[i].valid = 1;
325 			//device->num_of_valid_fpgas ++;
326 		}
327 		else {
328 			return 0;
329 			//device->fpga[i].valid = 0; // no bitstream
330 		}
331 	}
332 	return 1;
333 }
334 
335 ////////////////////////////////////////////////////////////////////////////////////////
336 //
337 // Checks if bitstreams on devices are loaded and of specified type.
338 // if (filename != NULL) performs upload in case of wrong or no bitstream
339 //
340 // If bitstream doesn't function properly - device invalidated
341 //
342 // Returns: number of devices with bitstreams uploaded
343 // < 0 on fatal error
device_list_check_bitstreams(struct device_list * device_list,unsigned short BITSTREAM_TYPE,char * filename)344 int device_list_check_bitstreams(struct device_list *device_list, unsigned short BITSTREAM_TYPE, char *filename)
345 {
346 	int ok_count = 0;
347 	int uploaded_count = 0;
348 	int do_upload = filename != NULL;
349 	FILE *fp = NULL;
350 	struct device *device;
351 
352 	for (device = device_list->device; device; device = device->next) {
353 		if (!device->valid)
354 			continue;
355 
356 		int result;
357 		result = device_check_bitstream_type(device, BITSTREAM_TYPE);
358 		if (result > 0) {
359 			ok_count ++;
360 			continue;
361 		}
362 		else if (result < 0) {
363 			printf("SN %s: device_list_check_bitstreams() failed: %d\n",
364 				device->ztex_device->snString, result);
365 			device_invalidate(device);
366 			continue;
367 		}
368 
369 		if (!do_upload) {
370 			printf("SN %s: device_list_check_bitstreams(): no bitstream or wrong type\n",
371 				device->ztex_device->snString);
372 			device_invalidate(device);
373 			continue;
374 		}
375 
376 		if (!fp)
377 			if ( !(fp = fopen(path_expand(filename), "r")) ) {
378 				printf("fopen(%s): %s\n", path_expand(filename), strerror(errno));
379 				return -1;
380 			}
381 		printf("SN %s: uploading bitstreams.. ", device->ztex_device->snString);
382 		fflush(stdout);
383 
384 		result = ztex_upload_bitstream(device->ztex_device, fp);
385 		if (result < 0) {
386 			printf("failed\n");
387 			device_invalidate(device);
388 		}
389 		else {
390 			printf("ok\n");
391 			ok_count ++;
392 			uploaded_count ++;
393 		}
394 	}
395 	if (fp)
396 		fclose(fp);
397 	return ok_count;
398 }
399 
fpga_set_app_mode(struct fpga * fpga,int app_mode)400 int fpga_set_app_mode(struct fpga *fpga, int app_mode)
401 {
402 	int result = vendor_command(fpga->device->handle, 0x82, app_mode, 0, NULL, 0);
403 	fpga->cmd_count++;
404 	if (DEBUG) printf("fpga_set_app_mode(%d): %d\n", fpga->num + 1, app_mode);
405 	return result;
406 }
407 
device_list_set_app_mode(struct device_list * device_list,int app_mode)408 int device_list_set_app_mode(struct device_list *device_list, int app_mode)
409 {
410 	int count = 0;
411 	struct device *device;
412 	for (device = device_list->device; device; device = device->next) {
413 		if (!device_valid(device))
414 			continue;
415 
416 		int result;
417 		int i;
418 		for (i = 0; i < device->num_of_fpgas; i++) {
419 			struct fpga *fpga = &device->fpga[i];
420 			result = fpga_select(fpga);
421 			if (result < 0) {
422 				device_invalidate(device);
423 				break;
424 			}
425 			result = fpga_set_app_mode(fpga, app_mode);
426 			if (result < 0) {
427 				printf("SN %s set_app_mode %d: error %d (%s).\n", device->ztex_device->snString,
428 					app_mode, result, libusb_error_name(result));
429 				device_invalidate(device);
430 				break;
431 			}
432 		} // for (fpga)
433 		count ++;
434 
435 	} // for (device)
436 	return count;
437 }
438 
device_list_fpga_reset(struct device_list * device_list)439 int device_list_fpga_reset(struct device_list *device_list)
440 {
441 	int count = 0;
442 	struct device *device;
443 	for (device = device_list->device; device; device = device->next) {
444 		if (!device_valid(device))
445 			continue;
446 
447 		int result = device_fpga_reset(device);
448 		if (result < 0) {
449 			device_invalidate(device);
450 			continue;
451 		}
452 		count++;
453 	}
454 	return count;
455 }
456 
457 // unlike ztex_select_fpga(), it waits for I/O timeout
fpga_select(struct fpga * fpga)458 int fpga_select(struct fpga *fpga)
459 {
460 	int result = vendor_command(fpga->device->handle, 0x8E, fpga->num,
461 			0, NULL, 0);
462 	fpga->cmd_count++;
463 	if (DEBUG) printf("fpga_select(%d): %d\n", fpga->num + 1, result);
464 	if (result < 0) {
465 		printf("fpga_select(%d): %s\n", fpga->num + 1, libusb_error_name(result));
466 	}
467 	return result;
468 }
469 
470 // combines fpga_select(), fpga_get_io_state(), fpga_setup_output() in 1 USB request
fpga_select_setup_io(struct fpga * fpga)471 int fpga_select_setup_io(struct fpga *fpga)
472 {
473 	struct fpga_status fpga_status;
474 	int result = vendor_request(fpga->device->handle, 0x8C, fpga->num, 0,
475 		(unsigned char *)&fpga_status, sizeof(fpga_status));
476 	fpga->cmd_count++;
477 	if (result < 0)
478 		return result;
479 	fpga_status.read_limit *= OUTPUT_WORD_WIDTH;
480 	if (DEBUG) {
481 		struct fpga_io_state *io_state = &fpga_status.io_state;
482 		printf("fpga_select_setup_io(%d): state 0x%02x 0x%02x 0x%02x "
483 				"- 0x%02x 0x%02x 0x%02x, limit %u\n",
484 			fpga->num + 1,
485 			io_state->io_state, io_state->timeout, io_state->app_status,
486 			io_state->pkt_comm_status, io_state->debug2, io_state->debug3,
487 			fpga_status.read_limit);
488 	}
489 	fpga->wr.io_state = fpga_status.io_state;
490 	fpga->wr.io_state_valid = 1;
491 	fpga->rd.read_limit = fpga_status.read_limit;
492 	fpga->rd.read_limit_valid = 1;
493 	return result;
494 }
495 
496 
fpga_progclk_raw(struct fpga * fpga,int clk_num,int d_value,int m_value)497 int fpga_progclk_raw(struct fpga *fpga, int clk_num,
498 		int d_value, int m_value)
499 {
500 	int result = vendor_command(fpga->device->handle, 0x93,
501 			fpga->num | clk_num << 8, d_value | m_value << 8, NULL, 0);
502 	if (result < 0)
503 		fprintf(stderr, "fpga_progclk_raw(%d): %s\n", fpga->num + 1,
504 			libusb_error_name(result));
505 	return result;
506 }
507 
508 
fpga_progclk(struct fpga * fpga,int clk_num,int freq)509 int fpga_progclk(struct fpga *fpga, int clk_num, int freq)
510 {
511 	if (DEBUG)
512 		fprintf(stderr, "fpga_progclk(%d,%d,%d)\n", fpga->num + 1,
513 				clk_num, freq);
514 
515 	const struct {
516 		int freq, m, d;
517 	} freq_values[] = {
518 		{135,36,81},// {,,}, {,,}, {,,}, {,,},
519 		{140,35,76}, {141,38,82}, {142,35,75}, {143,32,68}, {144,37,78},
520 		{145,31,65}, {146,37,77}, {147,29,60}, {148,37,76}, {149,24,49},
521 		{150,31,63}, {151,39,79}, {152,39,78}, {153,39,77}, {154,32,63},
522 		{155,25,49}, {156,39,76}, {157,31,60}, {158,39,75}, {159,34,65},
523 		{160,30,57}, {161,36,68}, {162,32,60}, {163,37,69}, {164,34,63},
524 		{165,38,70}, {166,36,66}, {167,39,71}, {168,37,67}, {169,35,63},
525 		{170,33,59}, {171,36,64}, {172,39,69}, {173,37,65}, {174,36,63},
526 		{175,38,66}, {176,33,57}, {177,39,67}, {178,31,53}, {179,30,51},
527 		{180,29,49}, {181,31,52}, {182,39,65}, {183,38,63}, {184,23,38},
528 		{185,31,51},  {187,32,52},
529 		{190,35,56},  {192,36,57},
530 		{195,25,39},  {197,35,54},
531 		{200,27,41},  {202,38,57},
532 		{205,33,49},  {207,32,47},
533 		{210,38,55},  {212,39,56},
534 		{215,29,41},  {217,35,49},
535 		{220,21,29}, {221,32,44}, {222,27,37}, {223,33,45}, {224,28,38},
536 		{225,20,27}, {226,32,43}, {227,38,51}, {228,39,52}, {229,37,49},
537 		{230,31,41}, {231,38,50}, {232,29,38}, {233,23,30}, {234,37,48},
538 		{235,34,44}, {236,35,45}, {237,39,50}, {238,29,37}, {239,37,47},
539 		{240,30,38}, {241,23,29}, {242,39,49}, {243,36,45}, {244,37,46},
540 		{245,25,31}, {246,34,42}, {247,39,48}, {248,31,38}, {249,32,39},
541 		{250,37,45}, {251,33,40}, {252,34,41}, {253,35,42}, {254,36,43},
542 		{255,21,25},  {257,33,39},
543 		{260,35,41},  {262,31,36},
544 		{265,34,39},  {267,37,42},
545 		{270,32,36},  {272,34,38},
546 		{275,38,42},  {277,31,34},
547 		{280,35,38},  {282,39,42},
548 		{285,30,32},
549 		{290,21,22},
550 		{295,33,34},
551 		{310,39,38},
552 		{330,25,23},
553 		{350,23,20},
554 		{0}
555 	};
556 
557 	if (freq < freq_values[0].freq) {
558 		fprintf(stderr, "fpga_progclk(%d, %d, %d): Min. allowed frequency"
559 			" is %d MHz\n", fpga->num + 1, clk_num, freq, freq_values[0].freq);
560 		return -1;
561 	}
562 
563 	int i;
564 	for (i = 1; freq_values[i].freq; i++) {
565 		if (freq < freq_values[i].freq)
566 			break;
567 	}
568 
569 	if (!freq_values[i].freq && freq > 1.5 * freq_values[i-1].freq) {
570 		fprintf(stderr, "fpga_progclk(%d, %d, %d): Max. possible frequency"
571 			" is %d MHz\n", fpga->num + 1, clk_num, freq, freq_values[i-1].freq);
572 		return -1;
573 	}
574 
575 	int result = fpga_progclk_raw(fpga, clk_num, freq_values[i-1].d,
576 			freq_values[i-1].m);
577 
578 	if (result < 0)
579 		return result;
580 	else {
581 		fpga->freq[clk_num] = freq_values[i-1].freq;
582 		return freq_values[i-1].freq;
583 	}
584 }
585 
586 
587 // ***************************************************************
588 //
589 // Functions below are used by tests or obsolete.
590 // For operating the board, use top-level functions from device.c
591 //
592 // ***************************************************************
593 
594 // in OUTPUT_WORD_WIDTH-byte words, default 0. It doesn't register output limit if amount is below output_limit_min
595 // if output_limit_min happens to be greater than buffer size, limit_min equal to buffer size is used.
596 // * This works but looks like not useful, it's commented out in FPGA application
597 //int fpga_set_output_limit_min(struct fpga *fpga, unsigned short limit_min)
598 //{
599 //	int result = vendor_command(fpga->device->handle, 0x83, limit_min, 0, NULL, 0);
600 //	fpga->cmd_count++;
601 //	if (DEBUG) printf("fpga_set_output_limit_min: %d\n", limit_min);
602 //	return result;
603 //}
604 
605 // checks io_state (if necessary) and performs write
fpga_write(struct fpga * fpga)606 int fpga_write(struct fpga *fpga)
607 {
608 	struct fpga_wr *wr = &fpga->wr;
609 	wr->wr_done = 0;
610 	struct fpga_io_state *io_state = &wr->io_state;
611 	int result;
612 
613 	if (!wr->io_state_valid) {
614 		result = fpga_get_io_state(fpga->device->handle, io_state);
615 		fpga->cmd_count++;
616 		if (result < 0) {
617 			return result;
618 		}
619 		if (io_state->timeout < 1) {
620 			if (++wr->io_state_timeout_count >= 2) // timeout value in ~usecs
621 				return ERR_IO_STATE_TIMEOUT;
622 			else
623 				return 0; // write not performed
624 			if (DEBUG) printf("#%d io_state.timeout = %d, skipping write\n",
625 				fpga->num + 1, io_state->timeout);
626 		}
627 		// fpga_get_io_state() OK
628 		wr->io_state_timeout_count = 0;
629 	}
630 	wr->io_state_valid = 0; // io_state is used
631 
632 	//if (io_state->io_state & IO_STATE_OUTPUT_ERR_OVERFLOW) {
633 	//	return ERR_IO_STATE_OVERFLOW;
634 	//}
635 	if (io_state->io_state & IO_STATE_LIMIT_NOT_DONE) {
636 		return ERR_IO_STATE_LIMIT_NOT_DONE;
637 	}
638 	if (io_state->io_state & IO_STATE_SFIFO_NOT_EMPTY) {
639 		return ERR_IO_STATE_SFIFO_NOT_EMPTY;
640 	}
641 	if (io_state->io_state & ~IO_STATE_INPUT_PROG_FULL) {
642 		printf("Unknown error: io_state=0x%02X\n", io_state->io_state);
643 		return -1;
644 	}
645 	if (io_state->io_state & IO_STATE_INPUT_PROG_FULL) {
646 		if (DEBUG) printf("#%d fpga_write_do(): Input full\n", fpga->num + 1);
647 		return 0; // Input full, no write
648 	}
649 
650 	int transferred = 0;
651 	result = libusb_bulk_transfer(fpga->device->handle, 0x06, wr->buf, wr->len, &transferred, USB_RW_TIMEOUT);
652 	if (DEBUG) printf("#%d fpga_write(): %d %d\n", fpga->num + 1, result, transferred);
653 	if (result < 0) {
654 		return result;
655 	}
656 	if (transferred != wr->len) {
657 		return ERR_WR_PARTIAL;
658 	}
659 	wr->wr_count++;
660 	wr->wr_done = 1;
661 	return transferred;
662 }
663 
664 // requests read_limit (if necessary) and performs read
fpga_read(struct fpga * fpga)665 int fpga_read(struct fpga *fpga)
666 {
667 	struct fpga_rd *rd = &fpga->rd;
668 	rd->rd_done = 0;
669 	int result;
670 	int current_read_limit;
671 
672 	if (!rd->read_limit_valid) {
673 		result = fpga_setup_output(fpga->device->handle);
674 		fpga->cmd_count++;
675 		if (result < 0) {
676 			//fprintf(stderr, "fpga_setup_output() returned %d\n", result);
677 			return result;
678 		}
679 		else if (result == 0) { // Nothing to read
680 			if (DEBUG) printf("#%d read_limit==0\n", fpga->num + 1);
681 			return 0;
682 		}
683 		rd->read_limit = result;
684 	}
685 	rd->read_limit_valid = 0;
686 	if (!rd->read_limit)
687 		return 0;
688 
689 	current_read_limit = rd->read_limit;
690 	int offset = 0;
691 	for ( ; ; ) {
692 		int transferred = 0;
693 		result = libusb_bulk_transfer(fpga->device->handle, 0x82, rd->buf + offset,
694 				current_read_limit, &transferred, USB_RW_TIMEOUT);
695 		if (DEBUG) printf("#%d usb_bulk_read(): result=%d, transferred=%d, current_read_limit=%d\n",
696 			fpga->num + 1, result, transferred, current_read_limit);
697 		if (result < 0) {
698 			return result;
699 		}
700 		else if (transferred == 0) {
701 			return ERR_RD_ZEROREAD;
702 		}
703 		else if (transferred != current_read_limit) { // partial read
704 			if (DEBUG) printf("#%d PARTIAL READ: %d of %d\n",
705 				fpga->num + 1, transferred, current_read_limit);
706 			current_read_limit -= transferred;
707 			offset += transferred;
708 			rd->partial_read_count++;
709 			continue;
710 		}
711 		else {
712 			break;
713 		}
714 	} // for (;;)
715 	rd->read_count++;
716 	rd->rd_done = 1;
717 	rd->len = rd->read_limit;
718 	return rd->read_limit;
719 }
720 
721 
722 // Synchronous write with pkt_comm (packet communication)
fpga_pkt_write(struct fpga * fpga)723 int fpga_pkt_write(struct fpga *fpga)
724 {
725 	struct fpga_wr *wr = &fpga->wr;
726 	wr->wr_done = 0;
727 	struct fpga_io_state *io_state = &wr->io_state;
728 	int result;
729 
730 	if (!wr->io_state_valid) {
731 		result = fpga_get_io_state(fpga->device->handle, io_state);
732 		fpga->cmd_count++;
733 		if (result < 0) {
734 			return result;
735 		}
736 		if (io_state->timeout < 1) {
737 			if (++wr->io_state_timeout_count >= 2) // timeout value in ~usecs
738 				return ERR_IO_STATE_TIMEOUT;
739 			else
740 				return 0; // write not performed
741 			if (DEBUG) printf("#%d io_state.timeout = %d, skipping write\n",
742 				fpga->num + 1, io_state->timeout);
743 		}
744 		// fpga_get_io_state() OK
745 		wr->io_state_timeout_count = 0;
746 	}
747 	wr->io_state_valid = 0; // io_state is used
748 
749 	//if (io_state->io_state & IO_STATE_OUTPUT_ERR_OVERFLOW) {
750 	//	return ERR_IO_STATE_OVERFLOW;
751 	//}
752 	if (io_state->io_state & IO_STATE_LIMIT_NOT_DONE) {
753 		return ERR_IO_STATE_LIMIT_NOT_DONE;
754 	}
755 	if (io_state->io_state & IO_STATE_SFIFO_NOT_EMPTY) {
756 		return ERR_IO_STATE_SFIFO_NOT_EMPTY;
757 	}
758 	if (io_state->io_state & ~IO_STATE_INPUT_PROG_FULL) {
759 		printf("Unknown error: io_state=0x%02X\n", io_state->io_state);
760 		return -1;
761 	}
762 	if (io_state->io_state & IO_STATE_INPUT_PROG_FULL) {
763 		if (DEBUG) printf("#%d fpga_write_do(): Input full\n", fpga->num + 1);
764 		return 0; // Input full, no write
765 	}
766 
767 	// get data for transmission
768 	int data_len = 0;
769 	unsigned char *data = pkt_comm_get_output_data(fpga->comm, &data_len);
770 	if (!data) {
771 		if (DEBUG) printf("fpga_pkt_write(): no data for transmission\n");
772 		return 0;
773 	}
774 
775 	if (DEBUG >= 2) {
776 		int i;
777 		for (i=0; i < data_len; i++) {
778 			printf("%02x ", data[i]);
779 		}
780 		printf("\n");
781 	}
782 
783 	int transferred = 0;
784 	result = libusb_bulk_transfer(fpga->device->handle, 0x06, data,
785 			data_len, &transferred, USB_RW_TIMEOUT);
786 	if (DEBUG) printf("#%d fpga_write(): %d %d/%d\n",
787 			fpga->num + 1, result, transferred, data_len);
788 	if (result < 0) {
789 		return result;
790 	}
791 	if (transferred != data_len) {
792 		return ERR_WR_PARTIAL;
793 	}
794 
795 	pkt_comm_output_completed(fpga->comm, data_len, 0);
796 
797 	wr->wr_count++;
798 	wr->wr_done = 1;
799 	return transferred;
800 }
801 
802 // Synchronous read with pkt_comm (packet communication)
fpga_pkt_read(struct fpga * fpga)803 int fpga_pkt_read(struct fpga *fpga)
804 {
805 	struct fpga_rd *rd = &fpga->rd;
806 	rd->rd_done = 0;
807 	int result;
808 	int current_read_limit;
809 
810 	if (!rd->read_limit_valid) {
811 		result = fpga_setup_output(fpga->device->handle);
812 		fpga->cmd_count++;
813 		if (result < 0) {
814 			//fprintf(stderr, "fpga_setup_output() returned %d\n", result);
815 			return result;
816 		}
817 		else if (result == 0) { // Nothing to read
818 			if (DEBUG) printf("#%d read_limit==0\n", fpga->num + 1);
819 			return 0;
820 		}
821 		rd->read_limit = result;
822 	}
823 	rd->read_limit_valid = 0;
824 	if (!rd->read_limit)
825 		return 0;
826 
827 	// get input buffer
828 	unsigned char *input_buf = pkt_comm_input_get_buf(fpga->comm);
829 	if (fpga->comm->error)
830 		return -1;
831 	if (!input_buf)
832 		return 0;
833 
834 	current_read_limit = rd->read_limit;
835 	for ( ; ; ) {
836 		int transferred = 0;
837 		//result = libusb_bulk_transfer(fpga->device->handle, 0x82, rd->buf,
838 		//		current_read_limit, &transferred, USB_RW_TIMEOUT);
839 		result = libusb_bulk_transfer(fpga->device->handle, 0x82, input_buf,
840 				current_read_limit, &transferred, USB_RW_TIMEOUT);
841 		if (DEBUG) printf("#%d usb_bulk_read(): result=%d, transferred=%d, current_read_limit=%d\n",
842 			fpga->num + 1, result, transferred, current_read_limit);
843 		if (result < 0) {
844 			return result;
845 		}
846 		else if (transferred == 0) {
847 			return ERR_RD_ZEROREAD;
848 		}
849 		else if (transferred != current_read_limit) { // partial read
850 			if (DEBUG) printf("#%d PARTIAL READ: %d of %d\n",
851 				fpga->num + 1, transferred, current_read_limit);
852 			current_read_limit -= transferred;
853 			rd->partial_read_count++;
854 			continue;
855 		}
856 		else {
857 			break;
858 		}
859 	} // for (;;)
860 	rd->read_count++;
861 	rd->rd_done = 1;
862 	rd->len = rd->read_limit;
863 /*
864 	// did read into buffer.
865 	int i, j;
866 	for (i=0; i < rd->read_limit; i++) {
867 		//if (!input_buf[i])
868 		//	printf(" ");
869 		//else
870 		if (i && !(i%8)) printf("\n");
871 			printf("%d ", input_buf[i]);
872 	}
873 	printf("\n");
874 */
875 	result = pkt_comm_input_completed(fpga->comm, rd->read_limit, 0);
876 
877 	if (result < 0)
878 		return result;
879 	return rd->read_limit;
880 }
881 
882