1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2016 Andreas Zschunke <andreas.zschunke@gmx.net>
5  * Copyright (C) 2017 Andrej Valek <andy@skyrain.eu>
6  * Copyright (C) 2017 Uwe Hermann <uwe@hermann-uwe.de>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 #include "protocol.h"
24 
25 #define H4032L_USB_TIMEOUT 500
26 
27 enum h4032l_cmd {
28 	CMD_RESET = 0x00b3, /* Also arms the logic analyzer. */
29 	CMD_CONFIGURE = 0x2b1a,
30 	CMD_STATUS = 0x4b3a,
31 	CMD_GET = 0x6b5a
32 };
33 
34 struct h4032l_status_packet {
35 	uint32_t magic;
36 	uint32_t values;
37 	uint32_t status;
38 	uint32_t usbxi_data;
39 	uint32_t fpga_version;
40 };
41 
abort_acquisition(struct dev_context * devc)42 static void abort_acquisition(struct dev_context *devc)
43 {
44 	int i;
45 
46 	devc->acq_aborted = TRUE;
47 
48 	for (i = devc->num_transfers - 1; i >= 0; i--) {
49 		if (devc->transfers[i])
50 			libusb_cancel_transfer(devc->transfers[i]);
51 	}
52 
53 	devc->status = H4032L_STATUS_IDLE;
54 }
55 
finish_acquisition(struct sr_dev_inst * sdi)56 static void finish_acquisition(struct sr_dev_inst *sdi)
57 {
58 	struct dev_context *devc = sdi->priv;
59 	struct drv_context *drvc = sdi->driver->context;
60 
61 	std_session_send_df_end(sdi);
62 	usb_source_remove(sdi->session, drvc->sr_ctx);
63 
64 	devc->num_transfers = 0;
65 	g_free(devc->transfers);
66 }
67 
free_transfer(struct libusb_transfer * transfer)68 static void free_transfer(struct libusb_transfer *transfer)
69 {
70 	struct sr_dev_inst *sdi = transfer->user_data;
71 	struct dev_context *devc = sdi->priv;
72 	unsigned int i;
73 
74 	if ((transfer->buffer != (unsigned char *)&devc->cmd_pkt) &&
75 	    (transfer->buffer != devc->buf)) {
76 		g_free(transfer->buffer);
77 	}
78 
79 	transfer->buffer = NULL;
80 	libusb_free_transfer(transfer);
81 
82 	for (i = 0; i < devc->num_transfers; i++) {
83 		if (devc->transfers[i] == transfer) {
84 			devc->transfers[i] = NULL;
85 			break;
86 		}
87 	}
88 
89 	if (--devc->submitted_transfers == 0)
90 		finish_acquisition(sdi);
91 }
92 
resubmit_transfer(struct libusb_transfer * transfer)93 static void resubmit_transfer(struct libusb_transfer *transfer)
94 {
95 	int ret;
96 
97 	if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
98 		return;
99 
100 	sr_err("%s: %s", __func__, libusb_error_name(ret));
101 	free_transfer(transfer);
102 }
103 
send_data(struct sr_dev_inst * sdi,uint32_t * data,size_t sample_count)104 static void send_data(struct sr_dev_inst *sdi,
105 	uint32_t *data, size_t sample_count)
106 {
107 	struct dev_context *devc = sdi->priv;
108 	struct sr_datafeed_logic logic = {
109 		.length = sample_count * sizeof(uint32_t),
110 		.unitsize = sizeof(uint32_t),
111 		.data = data
112 	};
113 	const struct sr_datafeed_packet packet = {
114 		.type = SR_DF_LOGIC,
115 		.payload = &logic
116 	};
117 	const struct sr_datafeed_packet trig = {
118 		.type = SR_DF_TRIGGER,
119 		.payload = NULL
120 	};
121 	size_t trigger_offset;
122 
123 	if (devc->trigger_pos >= devc->sent_samples &&
124 		devc->trigger_pos < (devc->sent_samples + sample_count)) {
125 		/* Get trigger position. */
126 		trigger_offset = devc->trigger_pos - devc->sent_samples;
127 		logic.length = trigger_offset * sizeof(uint32_t);
128 		if (logic.length)
129 			sr_session_send(sdi, &packet);
130 
131 		/* Send trigger position. */
132 		sr_session_send(sdi, &trig);
133 
134 		/* Send rest of data. */
135 		logic.length = (sample_count - trigger_offset) * sizeof(uint32_t);
136 		logic.data = data + trigger_offset;
137 		if (logic.length)
138 			sr_session_send(sdi, &packet);
139 	} else {
140 		sr_session_send(sdi, &packet);
141 	}
142 
143 	devc->sent_samples += sample_count;
144 }
145 
h4032l_receive_data(int fd,int revents,void * cb_data)146 SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data)
147 {
148 	struct timeval tv;
149 	struct drv_context *drvc;
150 
151 	(void)fd;
152 	(void)revents;
153 
154 	drvc = (struct drv_context *)cb_data;
155 
156 	tv.tv_sec = tv.tv_usec = 0;
157 	libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
158 
159 	return TRUE;
160 }
161 
h4032l_data_transfer_callback(struct libusb_transfer * transfer)162 void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer)
163 {
164 	struct sr_dev_inst *const sdi = transfer->user_data;
165 	struct dev_context *const devc = sdi->priv;
166 	uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
167 	uint32_t *buf;
168 	uint32_t num_samples;
169 
170 	/*
171 	 * If acquisition has already ended, just free any queued up
172 	 * transfer that come in.
173 	 */
174 	if (devc->acq_aborted) {
175 		free_transfer(transfer);
176 		return;
177 	}
178 
179 	if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
180 		sr_dbg("%s error: %d.", __func__, transfer->status);
181 
182 	/* Cancel pending transfers. */
183 	if (transfer->actual_length == 0) {
184 		resubmit_transfer(transfer);
185 		return;
186 	}
187 
188 	buf = (uint32_t *)transfer->buffer;
189 
190 	num_samples = MIN(devc->remaining_samples, max_samples);
191 	devc->remaining_samples -= num_samples;
192 	send_data(sdi, buf, num_samples);
193 	sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
194 		buf[0], buf[1]);
195 
196 	/* Close data receiving. */
197 	if (devc->remaining_samples == 0) {
198 		if (buf[num_samples] != H4032L_END_PACKET_MAGIC)
199 			sr_err("Mismatch magic number of end poll.");
200 
201 		abort_acquisition(devc);
202 		free_transfer(transfer);
203 	} else {
204 		if (((devc->submitted_transfers - 1) * H4032L_DATA_BUFFER_SIZE) <
205 		    (int32_t)(devc->remaining_samples * sizeof(uint32_t)))
206 			resubmit_transfer(transfer);
207 		else
208 			free_transfer(transfer);
209 	}
210 }
211 
h4032l_usb_callback(struct libusb_transfer * transfer)212 void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer)
213 {
214 	struct sr_dev_inst *const sdi = transfer->user_data;
215 	struct dev_context *const devc = sdi->priv;
216 	struct sr_usb_dev_inst *usb = sdi->conn;
217 	gboolean cmd = FALSE;
218 	uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
219 	uint32_t *buf;
220 	struct h4032l_status_packet *status;
221 	uint32_t num_samples;
222 	int ret;
223 
224 	/*
225 	 * If acquisition has already ended, just free any queued up
226 	 * transfers that come in.
227 	 */
228 	if (devc->acq_aborted) {
229 		free_transfer(transfer);
230 		return;
231 	}
232 
233 	if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
234 		sr_dbg("%s error: %d.", __func__, transfer->status);
235 
236 	buf = (uint32_t *)transfer->buffer;
237 
238 	switch (devc->status) {
239 	case H4032L_STATUS_IDLE:
240 		sr_err("USB callback called in idle.");
241 		break;
242 	case H4032L_STATUS_CMD_CONFIGURE:
243 		/* Select status request as next. */
244 		cmd = TRUE;
245 		devc->cmd_pkt.cmd = CMD_STATUS;
246 		devc->status = H4032L_STATUS_CMD_STATUS;
247 		break;
248 	case H4032L_STATUS_CMD_STATUS:
249 		/* Select status request as next. */
250 		devc->status = H4032L_STATUS_RESPONSE_STATUS;
251 		break;
252 	case H4032L_STATUS_RESPONSE_STATUS:
253 		/*
254 		 * Check magic and if status is complete, then select
255 		 * First Transfer as next.
256 		 */
257 		status = (struct h4032l_status_packet *)transfer->buffer;
258 		if (status->magic != H4032L_STATUS_PACKET_MAGIC)
259 			devc->status = H4032L_STATUS_RESPONSE_STATUS;
260 		else if (status->status == 2)
261 			devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE;
262 		else
263 			devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY;
264 		break;
265 	case H4032L_STATUS_RESPONSE_STATUS_RETRY:
266 		devc->status = H4032L_STATUS_CMD_STATUS;
267 		devc->cmd_pkt.cmd = CMD_STATUS;
268 		cmd = TRUE;
269 		break;
270 	case H4032L_STATUS_RESPONSE_STATUS_CONTINUE:
271 		devc->status = H4032L_STATUS_CMD_GET;
272 		devc->cmd_pkt.cmd = CMD_GET;
273 		cmd = TRUE;
274 		break;
275 	case H4032L_STATUS_CMD_GET:
276 		devc->status = H4032L_STATUS_FIRST_TRANSFER;
277 		/* Trigger has been captured. */
278 		std_session_send_df_header(sdi);
279 		break;
280 	case H4032L_STATUS_FIRST_TRANSFER:
281 		/* Drop packets until H4032L_START_PACKET_MAGIC. */
282 		if (buf[0] != H4032L_START_PACKET_MAGIC) {
283 			sr_dbg("Mismatch magic number of start poll.");
284 			break;
285 		}
286 		devc->status = H4032L_STATUS_TRANSFER;
287 		max_samples--;
288 		buf++;
289 		/* Fallthrough. */
290 	case H4032L_STATUS_TRANSFER:
291 		num_samples = MIN(devc->remaining_samples, max_samples);
292 		devc->remaining_samples -= num_samples;
293 		send_data(sdi, buf, num_samples);
294 		sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
295 		       buf[0], buf[1]);
296 		break;
297 	}
298 
299 	/* Start data receiving. */
300 	if (devc->status == H4032L_STATUS_TRANSFER) {
301 		if ((ret = h4032l_start_data_transfers(sdi)) != SR_OK) {
302 			sr_err("Can not start data transfers: %d", ret);
303 			devc->status = H4032L_STATUS_IDLE;
304 		}
305 	} else if (devc->status != H4032L_STATUS_IDLE) {
306 		if (cmd) {
307 			/* Setup new USB cmd packet, reuse transfer object. */
308 			sr_dbg("New command: %d.", devc->status);
309 			libusb_fill_bulk_transfer(transfer, usb->devhdl,
310 				2 | LIBUSB_ENDPOINT_OUT,
311 				(unsigned char *)&devc->cmd_pkt,
312 				sizeof(struct h4032l_cmd_pkt),
313 				h4032l_usb_callback,
314 				(void *)sdi, H4032L_USB_TIMEOUT);
315 		} else {
316 			/* Setup new USB poll packet, reuse transfer object. */
317 			sr_dbg("Poll: %d.", devc->status);
318 			libusb_fill_bulk_transfer(transfer, usb->devhdl,
319 				6 | LIBUSB_ENDPOINT_IN,
320 				devc->buf, ARRAY_SIZE(devc->buf),
321 				h4032l_usb_callback,
322 				(void *)sdi, H4032L_USB_TIMEOUT);
323 		}
324 		/* Send prepared USB packet. */
325 		if ((ret = libusb_submit_transfer(transfer)) != 0) {
326 			sr_err("Failed to submit transfer: %s.",
327 			       libusb_error_name(ret));
328 			devc->status = H4032L_STATUS_IDLE;
329 		}
330 	} else {
331 		sr_dbg("Now idle.");
332 	}
333 
334 	if (devc->status == H4032L_STATUS_IDLE)
335 		free_transfer(transfer);
336 }
337 
h4032l_voltage2pwm(double voltage)338 uint16_t h4032l_voltage2pwm(double voltage)
339 {
340 	/*
341 	 * word PwmA - channel A Vref PWM value, pseudocode:
342 	 * -6V < ThresholdVoltage < +6V
343 	 * Vref = 1.8 - ThresholdVoltage
344 	 * if Vref > 10.0
345 	 * 	Vref = 10.0
346 	 * if Vref < -5.0
347 	 * 	Vref = -5.0
348 	 * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0)
349 	 * if pwm > 4095
350 	 * 	pwm = 4095
351 	 */
352 	voltage = 1.8 - voltage;
353 	if (voltage > 10.0)
354 		voltage = 10.0;
355 	else if (voltage < -5.0)
356 		voltage = -5.0;
357 
358 	return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0));
359 }
360 
h4032l_start_data_transfers(const struct sr_dev_inst * sdi)361 SR_PRIV int h4032l_start_data_transfers(const struct sr_dev_inst *sdi)
362 {
363 	struct dev_context *devc = sdi->priv;
364 	struct sr_usb_dev_inst *usb = sdi->conn;
365 	struct libusb_transfer *transfer;
366 	uint8_t *buf;
367 	unsigned int num_transfers;
368 	unsigned int i;
369 	int ret;
370 
371 	devc->submitted_transfers = 0;
372 
373 	/*
374 	 * Set number of data transfers regarding to size of buffer.
375 	 * FPGA version 0 can't transfer multiple transfers at once.
376 	 */
377 	if ((num_transfers = MIN(devc->remaining_samples * sizeof(uint32_t) /
378 	    H4032L_DATA_BUFFER_SIZE, devc->fpga_version ?
379 	    H4032L_DATA_TRANSFER_MAX_NUM : 1)) == 0)
380 		num_transfers = 1;
381 
382 	g_free(devc->transfers);
383 	devc->transfers = g_malloc(sizeof(*devc->transfers) * num_transfers);
384 	devc->num_transfers = num_transfers;
385 
386 	for (i = 0; i < num_transfers; i++) {
387 		buf = g_malloc(H4032L_DATA_BUFFER_SIZE);
388 		transfer = libusb_alloc_transfer(0);
389 
390 		libusb_fill_bulk_transfer(transfer, usb->devhdl,
391 			6 | LIBUSB_ENDPOINT_IN,
392 			buf, H4032L_DATA_BUFFER_SIZE,
393 			h4032l_data_transfer_callback,
394 			(void *)sdi, H4032L_USB_TIMEOUT);
395 
396 		/* Send prepared usb packet. */
397 		if ((ret = libusb_submit_transfer(transfer)) != 0) {
398 			sr_err("Failed to submit transfer: %s.",
399 			       libusb_error_name(ret));
400 			libusb_free_transfer(transfer);
401 			g_free(buf);
402 			abort_acquisition(devc);
403 			return SR_ERR;
404 		}
405 		devc->transfers[i] = transfer;
406 		devc->submitted_transfers++;
407 	}
408 
409 	return SR_OK;
410 }
411 
h4032l_start(const struct sr_dev_inst * sdi)412 SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi)
413 {
414 	struct dev_context *devc = sdi->priv;
415 	struct sr_usb_dev_inst *usb = sdi->conn;
416 	struct libusb_transfer *transfer;
417 	unsigned char buf[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
418 	int ret;
419 
420 	/* Send reset command to arm the logic analyzer. */
421 	if ((ret = libusb_control_transfer(usb->devhdl,
422 		LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, CMD_RESET,
423 		0x00, 0x00, buf, ARRAY_SIZE(buf), H4032L_USB_TIMEOUT)) < 0) {
424 		sr_err("Failed to send vendor request %s.",
425 		       libusb_error_name(ret));
426 		return SR_ERR;
427 	}
428 
429 	/* Wait for reset vendor request. */
430 	g_usleep(20 * 1000);
431 
432 	/* Send configure command. */
433 	devc->cmd_pkt.cmd = CMD_CONFIGURE;
434 	devc->status = H4032L_STATUS_CMD_CONFIGURE;
435 	devc->remaining_samples = devc->cmd_pkt.sample_size;
436 
437 	transfer = libusb_alloc_transfer(0);
438 
439 	libusb_fill_bulk_transfer(transfer, usb->devhdl,
440 		2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
441 		sizeof(struct h4032l_cmd_pkt), h4032l_usb_callback,
442 		(void *)sdi, H4032L_USB_TIMEOUT);
443 
444 	if ((ret = libusb_submit_transfer(transfer)) != 0) {
445 		sr_err("Failed to submit transfer: %s.",
446 		       libusb_error_name(ret));
447 		libusb_free_transfer(transfer);
448 		return SR_ERR;
449 	}
450 
451 	devc->transfers = g_malloc0(sizeof(*devc->transfers));
452 	devc->submitted_transfers++;
453 	devc->num_transfers = 1;
454 	devc->transfers[0] = transfer;
455 
456 	return SR_OK;
457 }
458 
h4032l_stop(struct sr_dev_inst * sdi)459 SR_PRIV int h4032l_stop(struct sr_dev_inst *sdi)
460 {
461 	abort_acquisition(sdi->priv);
462 
463 	return SR_OK;
464 }
465 
h4032l_dev_open(struct sr_dev_inst * sdi)466 SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi)
467 {
468 	struct drv_context *drvc = sdi->driver->context;
469 	struct sr_usb_dev_inst *usb = sdi->conn;
470 	struct libusb_device_descriptor des;
471 	libusb_device **devlist;
472 	int ret = SR_ERR, i, device_count;
473 	char connection_id[64];
474 
475 	device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
476 	if (device_count < 0) {
477 		sr_err("Failed to get device list: %s.",
478 		       libusb_error_name(device_count));
479 		return SR_ERR;
480 	}
481 
482 	for (i = 0; i < device_count; i++) {
483 		libusb_get_device_descriptor(devlist[i], &des);
484 
485 		if (des.idVendor != H4032L_USB_VENDOR ||
486 		    des.idProduct != H4032L_USB_PRODUCT)
487 			continue;
488 
489 		if ((sdi->status == SR_ST_INITIALIZING) ||
490 		    (sdi->status == SR_ST_INACTIVE)) {
491 			/* Check device by its physical USB bus/port address. */
492 			if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
493 				continue;
494 
495 			if (strcmp(sdi->connection_id, connection_id))
496 				/* This is not the one. */
497 				continue;
498 		}
499 
500 		if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
501 			if (usb->address == 0xff)
502 				/*
503 				 * First time we touch this device after FW
504 				 * upload, so we don't know the address yet.
505 				 */
506 				usb->address =
507 					libusb_get_device_address(devlist[i]);
508 		} else {
509 			sr_err("Failed to open device: %s.",
510 			       libusb_error_name(ret));
511 			ret = SR_ERR;
512 			break;
513 		}
514 
515 		ret = SR_OK;
516 		break;
517 	}
518 
519 	libusb_free_device_list(devlist, 1);
520 	return ret;
521 }
522 
h4032l_get_fpga_version(const struct sr_dev_inst * sdi)523 SR_PRIV int h4032l_get_fpga_version(const struct sr_dev_inst *sdi)
524 {
525 	struct dev_context *devc = sdi->priv;
526 	struct sr_usb_dev_inst *usb = sdi->conn;
527 	struct h4032l_status_packet *status;
528 	int transferred;
529 	int i, ret;
530 
531 	/* Set command to status. */
532 	devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC;
533 	devc->cmd_pkt.cmd = CMD_STATUS;
534 
535 	/* Send status request. */
536 	if ((ret = libusb_bulk_transfer(usb->devhdl,
537 		2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
538 		sizeof(struct h4032l_cmd_pkt), &transferred, H4032L_USB_TIMEOUT)) < 0) {
539 		sr_err("Unable to send FPGA version request: %s.",
540 		       libusb_error_name(ret));
541 		return SR_ERR;
542 	}
543 
544 	/* Attempt to get FGPA version. */
545 	for (i = 0; i < 10; i++) {
546 		if ((ret = libusb_bulk_transfer(usb->devhdl,
547 			6 | LIBUSB_ENDPOINT_IN, devc->buf,
548 			ARRAY_SIZE(devc->buf), &transferred, H4032L_USB_TIMEOUT)) < 0) {
549 			sr_err("Unable to receive FPGA version: %s.",
550 			       libusb_error_name(ret));
551 			return SR_ERR;
552 		}
553 		status = (struct h4032l_status_packet *)devc->buf;
554 		if (status->magic == H4032L_STATUS_PACKET_MAGIC) {
555 			sr_dbg("FPGA version: 0x%x.", status->fpga_version);
556 			devc->fpga_version = status->fpga_version;
557 			return SR_OK;
558 		}
559 	}
560 
561 	sr_err("Unable to get FPGA version.");
562 
563 	return SR_ERR;
564 }
565